Workaround for gcc/45682.
[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;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
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
JB
1009
1010static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 1011 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1012
4f1520fb 1013static void dwarf2_start_subfile (char *, char *, 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. */
1641static void
1642dw2_do_instantiate_symtab (struct objfile *objfile,
1643 struct dwarf2_per_cu_data *per_cu)
1644{
1645 struct cleanup *back_to;
1646
1647 back_to = make_cleanup (dwarf2_release_queue, NULL);
1648
1649 queue_comp_unit (per_cu, objfile);
1650
1651 if (per_cu->from_debug_types)
1652 read_signatured_type_at_offset (objfile, per_cu->offset);
1653 else
1654 load_full_comp_unit (per_cu, objfile);
1655
1656 process_queue (objfile);
1657
1658 /* Age the cache, releasing compilation units that have not
1659 been used recently. */
1660 age_cached_comp_units ();
1661
1662 do_cleanups (back_to);
1663}
1664
1665/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1666 the objfile from which this CU came. Returns the resulting symbol
1667 table. */
1668static struct symtab *
1669dw2_instantiate_symtab (struct objfile *objfile,
1670 struct dwarf2_per_cu_data *per_cu)
1671{
1672 if (!per_cu->v.quick->symtab)
1673 {
1674 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1675 increment_reading_symtab ();
1676 dw2_do_instantiate_symtab (objfile, per_cu);
1677 do_cleanups (back_to);
1678 }
1679 return per_cu->v.quick->symtab;
1680}
1681
1fd400ff
TT
1682/* Return the CU given its index. */
1683static struct dwarf2_per_cu_data *
1684dw2_get_cu (int index)
1685{
1686 if (index >= dwarf2_per_objfile->n_comp_units)
1687 {
1688 index -= dwarf2_per_objfile->n_comp_units;
1689 return dwarf2_per_objfile->type_comp_units[index];
1690 }
1691 return dwarf2_per_objfile->all_comp_units[index];
1692}
1693
9291a0cd
TT
1694/* A helper function that knows how to read a 64-bit value in a way
1695 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1696 otherwise. */
1697static int
1698extract_cu_value (const char *bytes, ULONGEST *result)
1699{
1700 if (sizeof (ULONGEST) < 8)
1701 {
1702 int i;
1703
1704 /* Ignore the upper 4 bytes if they are all zero. */
1705 for (i = 0; i < 4; ++i)
1706 if (bytes[i + 4] != 0)
1707 return 0;
1708
1709 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1710 }
1711 else
1712 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1713 return 1;
1714}
1715
1716/* Read the CU list from the mapped index, and use it to create all
1717 the CU objects for this objfile. Return 0 if something went wrong,
1718 1 if everything went ok. */
1719static int
1fd400ff
TT
1720create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1721 offset_type cu_list_elements)
9291a0cd
TT
1722{
1723 offset_type i;
9291a0cd
TT
1724
1725 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1726 dwarf2_per_objfile->all_comp_units
1727 = obstack_alloc (&objfile->objfile_obstack,
1728 dwarf2_per_objfile->n_comp_units
1729 * sizeof (struct dwarf2_per_cu_data *));
1730
1731 for (i = 0; i < cu_list_elements; i += 2)
1732 {
1733 struct dwarf2_per_cu_data *the_cu;
1734 ULONGEST offset, length;
1735
1736 if (!extract_cu_value (cu_list, &offset)
1737 || !extract_cu_value (cu_list + 8, &length))
1738 return 0;
1739 cu_list += 2 * 8;
1740
1741 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1742 struct dwarf2_per_cu_data);
1743 the_cu->offset = offset;
1744 the_cu->length = length;
1745 the_cu->objfile = objfile;
1746 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1747 struct dwarf2_per_cu_quick_data);
1748 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1749 }
1750
1751 return 1;
1752}
1753
1fd400ff 1754/* Create the signatured type hash table from the index. */
673bfd45 1755
1fd400ff 1756static int
673bfd45
DE
1757create_signatured_type_table_from_index (struct objfile *objfile,
1758 const gdb_byte *bytes,
1759 offset_type elements)
1fd400ff
TT
1760{
1761 offset_type i;
673bfd45 1762 htab_t sig_types_hash;
1fd400ff
TT
1763
1764 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1765 dwarf2_per_objfile->type_comp_units
1766 = obstack_alloc (&objfile->objfile_obstack,
1767 dwarf2_per_objfile->n_type_comp_units
1768 * sizeof (struct dwarf2_per_cu_data *));
1769
673bfd45 1770 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1771
1772 for (i = 0; i < elements; i += 3)
1773 {
1774 struct signatured_type *type_sig;
1775 ULONGEST offset, type_offset, signature;
1776 void **slot;
1777
1778 if (!extract_cu_value (bytes, &offset)
1779 || !extract_cu_value (bytes + 8, &type_offset))
1780 return 0;
1781 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1782 bytes += 3 * 8;
1783
1784 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1785 struct signatured_type);
1786 type_sig->signature = signature;
1787 type_sig->offset = offset;
1788 type_sig->type_offset = type_offset;
1789 type_sig->per_cu.from_debug_types = 1;
1790 type_sig->per_cu.offset = offset;
1791 type_sig->per_cu.objfile = objfile;
1792 type_sig->per_cu.v.quick
1793 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1794 struct dwarf2_per_cu_quick_data);
1795
673bfd45 1796 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1797 *slot = type_sig;
1798
1799 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1800 }
1801
673bfd45 1802 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1803
1804 return 1;
1805}
1806
9291a0cd
TT
1807/* Read the address map data from the mapped index, and use it to
1808 populate the objfile's psymtabs_addrmap. */
1809static void
1810create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1811{
1812 const gdb_byte *iter, *end;
1813 struct obstack temp_obstack;
1814 struct addrmap *mutable_map;
1815 struct cleanup *cleanup;
1816 CORE_ADDR baseaddr;
1817
1818 obstack_init (&temp_obstack);
1819 cleanup = make_cleanup_obstack_free (&temp_obstack);
1820 mutable_map = addrmap_create_mutable (&temp_obstack);
1821
1822 iter = index->address_table;
1823 end = iter + index->address_table_size;
1824
1825 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1826
1827 while (iter < end)
1828 {
1829 ULONGEST hi, lo, cu_index;
1830 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1831 iter += 8;
1832 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1833 iter += 8;
1834 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1835 iter += 4;
1836
1837 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1838 dw2_get_cu (cu_index));
9291a0cd
TT
1839 }
1840
1841 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1842 &objfile->objfile_obstack);
1843 do_cleanups (cleanup);
1844}
1845
1846/* The hash function for strings in the mapped index. This is the
1847 same as the hashtab.c hash function, but we keep a separate copy to
1848 maintain control over the implementation. This is necessary
1849 because the hash function is tied to the format of the mapped index
1850 file. */
1851static hashval_t
1852mapped_index_string_hash (const void *p)
1853{
1854 const unsigned char *str = (const unsigned char *) p;
1855 hashval_t r = 0;
1856 unsigned char c;
1857
1858 while ((c = *str++) != 0)
1859 r = r * 67 + c - 113;
1860
1861 return r;
1862}
1863
1864/* Find a slot in the mapped index INDEX for the object named NAME.
1865 If NAME is found, set *VEC_OUT to point to the CU vector in the
1866 constant pool and return 1. If NAME cannot be found, return 0. */
1867static int
1868find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1869 offset_type **vec_out)
1870{
1871 offset_type hash = mapped_index_string_hash (name);
1872 offset_type slot, step;
1873
1874 slot = hash & (index->index_table_slots - 1);
1875 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1876
1877 for (;;)
1878 {
1879 /* Convert a slot number to an offset into the table. */
1880 offset_type i = 2 * slot;
1881 const char *str;
1882 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1883 return 0;
1884
1885 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1886 if (!strcmp (name, str))
1887 {
1888 *vec_out = (offset_type *) (index->constant_pool
1889 + MAYBE_SWAP (index->index_table[i + 1]));
1890 return 1;
1891 }
1892
1893 slot = (slot + step) & (index->index_table_slots - 1);
1894 }
1895}
1896
1897/* Read the index file. If everything went ok, initialize the "quick"
1898 elements of all the CUs and return 1. Otherwise, return 0. */
1899static int
1900dwarf2_read_index (struct objfile *objfile)
1901{
9291a0cd
TT
1902 char *addr;
1903 struct mapped_index *map;
b3b272e1 1904 offset_type *metadata;
ac0b195c
KW
1905 const gdb_byte *cu_list;
1906 const gdb_byte *types_list = NULL;
1907 offset_type version, cu_list_elements;
1908 offset_type types_list_elements = 0;
1fd400ff 1909 int i;
9291a0cd
TT
1910
1911 if (dwarf2_per_objfile->gdb_index.asection == NULL
1912 || dwarf2_per_objfile->gdb_index.size == 0)
1913 return 0;
82430852
JK
1914
1915 /* Older elfutils strip versions could keep the section in the main
1916 executable while splitting it for the separate debug info file. */
1917 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1918 & SEC_HAS_CONTENTS) == 0)
1919 return 0;
1920
9291a0cd
TT
1921 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1922
1923 addr = dwarf2_per_objfile->gdb_index.buffer;
1924 /* Version check. */
1fd400ff 1925 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
1926 /* Versions earlier than 3 emitted every copy of a psymbol. This
1927 causes the index to behave very poorly for certain requests. So,
1928 it seems better to just ignore such indices. */
1929 if (version < 3)
9291a0cd
TT
1930 return 0;
1931
1932 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1933 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1934
1935 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1936
1937 i = 0;
1938 cu_list = addr + MAYBE_SWAP (metadata[i]);
1939 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1940 / 8);
1fd400ff
TT
1941 ++i;
1942
987d643c
TT
1943 types_list = addr + MAYBE_SWAP (metadata[i]);
1944 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1945 - MAYBE_SWAP (metadata[i]))
1946 / 8);
1947 ++i;
1fd400ff
TT
1948
1949 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1950 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1951 - MAYBE_SWAP (metadata[i]));
1952 ++i;
1953
1954 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1955 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1956 - MAYBE_SWAP (metadata[i]))
9291a0cd 1957 / (2 * sizeof (offset_type)));
1fd400ff 1958 ++i;
9291a0cd 1959
1fd400ff
TT
1960 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1961
1962 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1963 return 0;
1964
987d643c 1965 if (types_list_elements
673bfd45
DE
1966 && !create_signatured_type_table_from_index (objfile, types_list,
1967 types_list_elements))
9291a0cd
TT
1968 return 0;
1969
1970 create_addrmap_from_index (objfile, map);
1971
1972 dwarf2_per_objfile->index_table = map;
1973 dwarf2_per_objfile->using_index = 1;
1974
1975 return 1;
1976}
1977
1978/* A helper for the "quick" functions which sets the global
1979 dwarf2_per_objfile according to OBJFILE. */
1980static void
1981dw2_setup (struct objfile *objfile)
1982{
1983 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1984 gdb_assert (dwarf2_per_objfile);
1985}
1986
1987/* A helper for the "quick" functions which attempts to read the line
1988 table for THIS_CU. */
1989static void
1990dw2_require_line_header (struct objfile *objfile,
1991 struct dwarf2_per_cu_data *this_cu)
1992{
1993 bfd *abfd = objfile->obfd;
1994 struct line_header *lh = NULL;
1995 struct attribute *attr;
1996 struct cleanup *cleanups;
1997 struct die_info *comp_unit_die;
36374493 1998 struct dwarf2_section_info* sec;
9291a0cd
TT
1999 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2000 int has_children, i;
2001 struct dwarf2_cu cu;
2002 unsigned int bytes_read, buffer_size;
2003 struct die_reader_specs reader_specs;
2004 char *name, *comp_dir;
2005
2006 if (this_cu->v.quick->read_lines)
2007 return;
2008 this_cu->v.quick->read_lines = 1;
2009
2010 memset (&cu, 0, sizeof (cu));
2011 cu.objfile = objfile;
2012 obstack_init (&cu.comp_unit_obstack);
2013
2014 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2015
36374493
DE
2016 if (this_cu->from_debug_types)
2017 sec = &dwarf2_per_objfile->types;
2018 else
2019 sec = &dwarf2_per_objfile->info;
2020 dwarf2_read_section (objfile, sec);
2021 buffer_size = sec->size;
2022 buffer = sec->buffer;
9291a0cd
TT
2023 info_ptr = buffer + this_cu->offset;
2024 beg_of_comp_unit = info_ptr;
2025
2026 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2027 buffer, buffer_size,
2028 abfd);
2029
2030 /* Complete the cu_header. */
2031 cu.header.offset = beg_of_comp_unit - buffer;
2032 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2033
2034 this_cu->cu = &cu;
2035 cu.per_cu = this_cu;
2036
2037 dwarf2_read_abbrevs (abfd, &cu);
2038 make_cleanup (dwarf2_free_abbrev_table, &cu);
2039
2040 if (this_cu->from_debug_types)
2041 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2042 init_cu_die_reader (&reader_specs, &cu);
2043 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2044 &has_children);
2045
2046 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2047 if (attr)
2048 {
2049 unsigned int line_offset = DW_UNSND (attr);
2050 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2051 }
2052 if (lh == NULL)
2053 {
2054 do_cleanups (cleanups);
2055 return;
2056 }
2057
2058 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2059
2060 this_cu->v.quick->lines = lh;
2061
2062 this_cu->v.quick->file_names
2063 = obstack_alloc (&objfile->objfile_obstack,
2064 lh->num_file_names * sizeof (char *));
2065 for (i = 0; i < lh->num_file_names; ++i)
2066 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2067
2068 do_cleanups (cleanups);
2069}
2070
2071/* A helper for the "quick" functions which computes and caches the
2072 real path for a given file name from the line table.
2073 dw2_require_line_header must have been called before this is
2074 invoked. */
2075static const char *
2076dw2_require_full_path (struct objfile *objfile,
e254ef6a 2077 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2078 int index)
2079{
e254ef6a
DE
2080 if (!per_cu->v.quick->full_names)
2081 per_cu->v.quick->full_names
9291a0cd 2082 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2083 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2084 sizeof (char *));
2085
e254ef6a
DE
2086 if (!per_cu->v.quick->full_names[index])
2087 per_cu->v.quick->full_names[index]
2088 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2089
e254ef6a 2090 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2091}
2092
2093static struct symtab *
2094dw2_find_last_source_symtab (struct objfile *objfile)
2095{
2096 int index;
2097 dw2_setup (objfile);
2098 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2099 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2100}
2101
2102static void
2103dw2_forget_cached_source_info (struct objfile *objfile)
2104{
2105 int i;
2106
2107 dw2_setup (objfile);
1fd400ff
TT
2108 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2109 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2110 {
e254ef6a 2111 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2112
e254ef6a 2113 if (per_cu->v.quick->full_names)
9291a0cd
TT
2114 {
2115 int j;
2116
e254ef6a
DE
2117 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2118 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
2119 }
2120 }
2121}
2122
2123static int
2124dw2_lookup_symtab (struct objfile *objfile, const char *name,
2125 const char *full_path, const char *real_path,
2126 struct symtab **result)
2127{
2128 int i;
2129 int check_basename = lbasename (name) == name;
2130 struct dwarf2_per_cu_data *base_cu = NULL;
2131
2132 dw2_setup (objfile);
1fd400ff
TT
2133 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2134 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2135 {
2136 int j;
e254ef6a 2137 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2138
e254ef6a 2139 if (per_cu->v.quick->symtab)
9291a0cd
TT
2140 continue;
2141
e254ef6a
DE
2142 dw2_require_line_header (objfile, per_cu);
2143 if (!per_cu->v.quick->lines)
9291a0cd
TT
2144 continue;
2145
e254ef6a 2146 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2147 {
e254ef6a 2148 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2149
2150 if (FILENAME_CMP (name, this_name) == 0)
2151 {
e254ef6a 2152 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2153 return 1;
2154 }
2155
2156 if (check_basename && ! base_cu
2157 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2158 base_cu = per_cu;
9291a0cd
TT
2159
2160 if (full_path != NULL)
2161 {
2162 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2163 per_cu, j);
9291a0cd
TT
2164
2165 if (this_full_name
2166 && FILENAME_CMP (full_path, this_full_name) == 0)
2167 {
e254ef6a 2168 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2169 return 1;
2170 }
2171 }
2172
2173 if (real_path != NULL)
2174 {
2175 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2176 per_cu, j);
9291a0cd
TT
2177
2178 if (this_full_name != NULL)
2179 {
2180 char *rp = gdb_realpath (this_full_name);
2181 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2182 {
2183 xfree (rp);
e254ef6a 2184 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2185 return 1;
2186 }
2187 xfree (rp);
2188 }
2189 }
2190 }
2191 }
2192
2193 if (base_cu)
2194 {
2195 *result = dw2_instantiate_symtab (objfile, base_cu);
2196 return 1;
2197 }
2198
2199 return 0;
2200}
2201
2202static struct symtab *
2203dw2_lookup_symbol (struct objfile *objfile, int block_index,
2204 const char *name, domain_enum domain)
2205{
774b6a14 2206 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2207 instead. */
2208 return NULL;
2209}
2210
2211/* A helper function that expands all symtabs that hold an object
2212 named NAME. */
2213static void
2214dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2215{
2216 dw2_setup (objfile);
2217
2218 if (dwarf2_per_objfile->index_table)
2219 {
2220 offset_type *vec;
2221
2222 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2223 name, &vec))
2224 {
2225 offset_type i, len = MAYBE_SWAP (*vec);
2226 for (i = 0; i < len; ++i)
2227 {
2228 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2229 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2230
e254ef6a 2231 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2232 }
2233 }
2234 }
2235}
2236
774b6a14
TT
2237static void
2238dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2239 int kind, const char *name,
2240 domain_enum domain)
9291a0cd 2241{
774b6a14 2242 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2243}
2244
2245static void
2246dw2_print_stats (struct objfile *objfile)
2247{
2248 int i, count;
2249
2250 dw2_setup (objfile);
2251 count = 0;
1fd400ff
TT
2252 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2253 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2254 {
e254ef6a 2255 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2256
e254ef6a 2257 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2258 ++count;
2259 }
2260 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2261}
2262
2263static void
2264dw2_dump (struct objfile *objfile)
2265{
2266 /* Nothing worth printing. */
2267}
2268
2269static void
2270dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2271 struct section_offsets *delta)
2272{
2273 /* There's nothing to relocate here. */
2274}
2275
2276static void
2277dw2_expand_symtabs_for_function (struct objfile *objfile,
2278 const char *func_name)
2279{
2280 dw2_do_expand_symtabs_matching (objfile, func_name);
2281}
2282
2283static void
2284dw2_expand_all_symtabs (struct objfile *objfile)
2285{
2286 int i;
2287
2288 dw2_setup (objfile);
1fd400ff
TT
2289
2290 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2291 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2292 {
e254ef6a 2293 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2294
e254ef6a 2295 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2296 }
2297}
2298
2299static void
2300dw2_expand_symtabs_with_filename (struct objfile *objfile,
2301 const char *filename)
2302{
2303 int i;
2304
2305 dw2_setup (objfile);
1fd400ff
TT
2306 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2307 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2308 {
2309 int j;
e254ef6a 2310 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2311
e254ef6a 2312 if (per_cu->v.quick->symtab)
9291a0cd
TT
2313 continue;
2314
e254ef6a
DE
2315 dw2_require_line_header (objfile, per_cu);
2316 if (!per_cu->v.quick->lines)
9291a0cd
TT
2317 continue;
2318
e254ef6a 2319 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2320 {
e254ef6a 2321 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2322 if (strcmp (this_name, filename) == 0)
2323 {
e254ef6a 2324 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2325 break;
2326 }
2327 }
2328 }
2329}
2330
dd786858 2331static const char *
9291a0cd
TT
2332dw2_find_symbol_file (struct objfile *objfile, const char *name)
2333{
e254ef6a 2334 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2335 offset_type *vec;
2336
2337 dw2_setup (objfile);
2338
2339 if (!dwarf2_per_objfile->index_table)
2340 return NULL;
2341
2342 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2343 name, &vec))
2344 return NULL;
2345
2346 /* Note that this just looks at the very first one named NAME -- but
2347 actually we are looking for a function. find_main_filename
2348 should be rewritten so that it doesn't require a custom hook. It
2349 could just use the ordinary symbol tables. */
2350 /* vec[0] is the length, which must always be >0. */
e254ef6a 2351 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2352
e254ef6a
DE
2353 dw2_require_line_header (objfile, per_cu);
2354 if (!per_cu->v.quick->lines)
9291a0cd
TT
2355 return NULL;
2356
e254ef6a 2357 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2358}
2359
2360static void
2361dw2_map_ada_symtabs (struct objfile *objfile,
2362 int (*wild_match) (const char *, int, const char *),
2363 int (*is_name_suffix) (const char *),
2364 void (*callback) (struct objfile *,
2365 struct symtab *, void *),
2366 const char *name, int global,
2367 domain_enum namespace, int wild,
2368 void *data)
2369{
a9e6a4bb
JK
2370 /* For now, we don't support Ada. Still the function can be called if the
2371 current language is Ada for a non-Ada objfile using GNU index. As Ada
2372 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2373}
2374
2375static void
2376dw2_expand_symtabs_matching (struct objfile *objfile,
2377 int (*file_matcher) (const char *, void *),
2378 int (*name_matcher) (const char *, void *),
2379 domain_enum kind,
2380 void *data)
2381{
2382 int i;
2383 offset_type iter;
4b5246aa 2384 struct mapped_index *index;
9291a0cd
TT
2385
2386 dw2_setup (objfile);
2387 if (!dwarf2_per_objfile->index_table)
2388 return;
4b5246aa 2389 index = dwarf2_per_objfile->index_table;
9291a0cd 2390
1fd400ff
TT
2391 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2392 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2393 {
2394 int j;
e254ef6a 2395 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2396
e254ef6a
DE
2397 per_cu->v.quick->mark = 0;
2398 if (per_cu->v.quick->symtab)
9291a0cd
TT
2399 continue;
2400
e254ef6a
DE
2401 dw2_require_line_header (objfile, per_cu);
2402 if (!per_cu->v.quick->lines)
9291a0cd
TT
2403 continue;
2404
e254ef6a 2405 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2406 {
e254ef6a 2407 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2408 {
e254ef6a 2409 per_cu->v.quick->mark = 1;
9291a0cd
TT
2410 break;
2411 }
2412 }
2413 }
2414
4b5246aa 2415 for (iter = 0; iter < index->index_table_slots; ++iter)
9291a0cd
TT
2416 {
2417 offset_type idx = 2 * iter;
2418 const char *name;
2419 offset_type *vec, vec_len, vec_idx;
2420
4b5246aa 2421 if (index->index_table[idx] == 0 && index->index_table[idx + 1] == 0)
9291a0cd
TT
2422 continue;
2423
4b5246aa 2424 name = index->constant_pool + MAYBE_SWAP (index->index_table[idx]);
9291a0cd
TT
2425
2426 if (! (*name_matcher) (name, data))
2427 continue;
2428
2429 /* The name was matched, now expand corresponding CUs that were
2430 marked. */
4b5246aa
TT
2431 vec = (offset_type *) (index->constant_pool
2432 + MAYBE_SWAP (index->index_table[idx + 1]));
9291a0cd
TT
2433 vec_len = MAYBE_SWAP (vec[0]);
2434 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2435 {
e254ef6a 2436 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2437
e254ef6a
DE
2438 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2439 if (per_cu->v.quick->mark)
2440 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2441 }
2442 }
2443}
2444
2445static struct symtab *
2446dw2_find_pc_sect_symtab (struct objfile *objfile,
2447 struct minimal_symbol *msymbol,
2448 CORE_ADDR pc,
2449 struct obj_section *section,
2450 int warn_if_readin)
2451{
2452 struct dwarf2_per_cu_data *data;
2453
2454 dw2_setup (objfile);
2455
2456 if (!objfile->psymtabs_addrmap)
2457 return NULL;
2458
2459 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2460 if (!data)
2461 return NULL;
2462
2463 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2464 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2465 paddress (get_objfile_arch (objfile), pc));
2466
2467 return dw2_instantiate_symtab (objfile, data);
2468}
2469
2470static void
2471dw2_map_symbol_names (struct objfile *objfile,
2472 void (*fun) (const char *, void *),
2473 void *data)
2474{
2475 offset_type iter;
4b5246aa
TT
2476 struct mapped_index *index;
2477
9291a0cd
TT
2478 dw2_setup (objfile);
2479
2480 if (!dwarf2_per_objfile->index_table)
2481 return;
4b5246aa 2482 index = dwarf2_per_objfile->index_table;
9291a0cd 2483
4b5246aa 2484 for (iter = 0; iter < index->index_table_slots; ++iter)
9291a0cd
TT
2485 {
2486 offset_type idx = 2 * iter;
2487 const char *name;
2488 offset_type *vec, vec_len, vec_idx;
2489
4b5246aa 2490 if (index->index_table[idx] == 0 && index->index_table[idx + 1] == 0)
9291a0cd
TT
2491 continue;
2492
4b5246aa 2493 name = (index->constant_pool + MAYBE_SWAP (index->index_table[idx]));
9291a0cd
TT
2494
2495 (*fun) (name, data);
2496 }
2497}
2498
2499static void
2500dw2_map_symbol_filenames (struct objfile *objfile,
2501 void (*fun) (const char *, const char *, void *),
2502 void *data)
2503{
2504 int i;
2505
2506 dw2_setup (objfile);
1fd400ff
TT
2507 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2508 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2509 {
2510 int j;
e254ef6a 2511 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2512
e254ef6a 2513 if (per_cu->v.quick->symtab)
9291a0cd
TT
2514 continue;
2515
e254ef6a
DE
2516 dw2_require_line_header (objfile, per_cu);
2517 if (!per_cu->v.quick->lines)
9291a0cd
TT
2518 continue;
2519
e254ef6a 2520 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2521 {
e254ef6a
DE
2522 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2523 j);
2524 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2525 }
2526 }
2527}
2528
2529static int
2530dw2_has_symbols (struct objfile *objfile)
2531{
2532 return 1;
2533}
2534
2535const struct quick_symbol_functions dwarf2_gdb_index_functions =
2536{
2537 dw2_has_symbols,
2538 dw2_find_last_source_symtab,
2539 dw2_forget_cached_source_info,
2540 dw2_lookup_symtab,
2541 dw2_lookup_symbol,
774b6a14 2542 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2543 dw2_print_stats,
2544 dw2_dump,
2545 dw2_relocate,
2546 dw2_expand_symtabs_for_function,
2547 dw2_expand_all_symtabs,
2548 dw2_expand_symtabs_with_filename,
2549 dw2_find_symbol_file,
2550 dw2_map_ada_symtabs,
2551 dw2_expand_symtabs_matching,
2552 dw2_find_pc_sect_symtab,
2553 dw2_map_symbol_names,
2554 dw2_map_symbol_filenames
2555};
2556
2557/* Initialize for reading DWARF for this objfile. Return 0 if this
2558 file will use psymtabs, or 1 if using the GNU index. */
2559
2560int
2561dwarf2_initialize_objfile (struct objfile *objfile)
2562{
2563 /* If we're about to read full symbols, don't bother with the
2564 indices. In this case we also don't care if some other debug
2565 format is making psymtabs, because they are all about to be
2566 expanded anyway. */
2567 if ((objfile->flags & OBJF_READNOW))
2568 {
2569 int i;
2570
2571 dwarf2_per_objfile->using_index = 1;
2572 create_all_comp_units (objfile);
1fd400ff 2573 create_debug_types_hash_table (objfile);
9291a0cd 2574
1fd400ff
TT
2575 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2576 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2577 {
e254ef6a 2578 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2579
e254ef6a
DE
2580 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2581 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2582 }
2583
2584 /* Return 1 so that gdb sees the "quick" functions. However,
2585 these functions will be no-ops because we will have expanded
2586 all symtabs. */
2587 return 1;
2588 }
2589
2590 if (dwarf2_read_index (objfile))
2591 return 1;
2592
2593 dwarf2_build_psymtabs (objfile);
2594 return 0;
2595}
2596
2597\f
2598
dce234bc
PP
2599/* Build a partial symbol table. */
2600
2601void
f29dff0a 2602dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2603{
f29dff0a 2604 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2605 {
2606 init_psymbol_list (objfile, 1024);
2607 }
2608
d146bf1e 2609 dwarf2_build_psymtabs_hard (objfile);
c906108c 2610}
c906108c 2611
45452591
DE
2612/* Return TRUE if OFFSET is within CU_HEADER. */
2613
2614static inline int
2615offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2616{
2617 unsigned int bottom = cu_header->offset;
2618 unsigned int top = (cu_header->offset
2619 + cu_header->length
2620 + cu_header->initial_length_size);
9a619af0 2621
45452591
DE
2622 return (offset >= bottom && offset < top);
2623}
2624
93311388
DE
2625/* Read in the comp unit header information from the debug_info at info_ptr.
2626 NOTE: This leaves members offset, first_die_offset to be filled in
2627 by the caller. */
107d2387 2628
fe1b8b76 2629static gdb_byte *
107d2387 2630read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2631 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2632{
2633 int signed_addr;
891d2f0b 2634 unsigned int bytes_read;
c764a876
DE
2635
2636 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2637 cu_header->initial_length_size = bytes_read;
2638 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2639 info_ptr += bytes_read;
107d2387
AC
2640 cu_header->version = read_2_bytes (abfd, info_ptr);
2641 info_ptr += 2;
613e1657 2642 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2643 &bytes_read);
613e1657 2644 info_ptr += bytes_read;
107d2387
AC
2645 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2646 info_ptr += 1;
2647 signed_addr = bfd_get_sign_extend_vma (abfd);
2648 if (signed_addr < 0)
8e65ff28 2649 internal_error (__FILE__, __LINE__,
e2e0b3e5 2650 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2651 cu_header->signed_addr_p = signed_addr;
c764a876 2652
107d2387
AC
2653 return info_ptr;
2654}
2655
fe1b8b76
JB
2656static gdb_byte *
2657partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2658 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2659 bfd *abfd)
2660{
fe1b8b76 2661 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2662
2663 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2664
2dc7f7b3 2665 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2666 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2667 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2668 bfd_get_filename (abfd));
72bf9492 2669
dce234bc 2670 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2671 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2672 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2673 (long) header->abbrev_offset,
93311388 2674 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2675 bfd_get_filename (abfd));
2676
2677 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2678 > buffer + buffer_size)
8a3fe4f8
AC
2679 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2680 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2681 (long) header->length,
93311388 2682 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2683 bfd_get_filename (abfd));
2684
2685 return info_ptr;
2686}
2687
348e048f
DE
2688/* Read in the types comp unit header information from .debug_types entry at
2689 types_ptr. The result is a pointer to one past the end of the header. */
2690
2691static gdb_byte *
2692read_type_comp_unit_head (struct comp_unit_head *cu_header,
2693 ULONGEST *signature,
2694 gdb_byte *types_ptr, bfd *abfd)
2695{
348e048f
DE
2696 gdb_byte *initial_types_ptr = types_ptr;
2697
6e70227d 2698 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2699 &dwarf2_per_objfile->types);
348e048f
DE
2700 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2701
2702 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2703
2704 *signature = read_8_bytes (abfd, types_ptr);
2705 types_ptr += 8;
2706 types_ptr += cu_header->offset_size;
2707 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2708
2709 return types_ptr;
2710}
2711
aaa75496
JB
2712/* Allocate a new partial symtab for file named NAME and mark this new
2713 partial symtab as being an include of PST. */
2714
2715static void
2716dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2717 struct objfile *objfile)
2718{
2719 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2720
2721 subpst->section_offsets = pst->section_offsets;
2722 subpst->textlow = 0;
2723 subpst->texthigh = 0;
2724
2725 subpst->dependencies = (struct partial_symtab **)
2726 obstack_alloc (&objfile->objfile_obstack,
2727 sizeof (struct partial_symtab *));
2728 subpst->dependencies[0] = pst;
2729 subpst->number_of_dependencies = 1;
2730
2731 subpst->globals_offset = 0;
2732 subpst->n_global_syms = 0;
2733 subpst->statics_offset = 0;
2734 subpst->n_static_syms = 0;
2735 subpst->symtab = NULL;
2736 subpst->read_symtab = pst->read_symtab;
2737 subpst->readin = 0;
2738
2739 /* No private part is necessary for include psymtabs. This property
2740 can be used to differentiate between such include psymtabs and
10b3939b 2741 the regular ones. */
58a9656e 2742 subpst->read_symtab_private = NULL;
aaa75496
JB
2743}
2744
2745/* Read the Line Number Program data and extract the list of files
2746 included by the source file represented by PST. Build an include
d85a05f0 2747 partial symtab for each of these included files. */
aaa75496
JB
2748
2749static void
2750dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2751 struct die_info *die,
aaa75496
JB
2752 struct partial_symtab *pst)
2753{
2754 struct objfile *objfile = cu->objfile;
2755 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2756 struct line_header *lh = NULL;
2757 struct attribute *attr;
aaa75496 2758
d85a05f0
DJ
2759 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2760 if (attr)
2761 {
2762 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2763
d85a05f0
DJ
2764 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2765 }
aaa75496
JB
2766 if (lh == NULL)
2767 return; /* No linetable, so no includes. */
2768
c6da4cef
DE
2769 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2770 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2771
2772 free_line_header (lh);
2773}
2774
348e048f
DE
2775static hashval_t
2776hash_type_signature (const void *item)
2777{
2778 const struct signatured_type *type_sig = item;
9a619af0 2779
348e048f
DE
2780 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2781 return type_sig->signature;
2782}
2783
2784static int
2785eq_type_signature (const void *item_lhs, const void *item_rhs)
2786{
2787 const struct signatured_type *lhs = item_lhs;
2788 const struct signatured_type *rhs = item_rhs;
9a619af0 2789
348e048f
DE
2790 return lhs->signature == rhs->signature;
2791}
2792
1fd400ff
TT
2793/* Allocate a hash table for signatured types. */
2794
2795static htab_t
673bfd45 2796allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2797{
2798 return htab_create_alloc_ex (41,
2799 hash_type_signature,
2800 eq_type_signature,
2801 NULL,
2802 &objfile->objfile_obstack,
2803 hashtab_obstack_allocate,
2804 dummy_obstack_deallocate);
2805}
2806
2807/* A helper function to add a signatured type CU to a list. */
2808
2809static int
2810add_signatured_type_cu_to_list (void **slot, void *datum)
2811{
2812 struct signatured_type *sigt = *slot;
2813 struct dwarf2_per_cu_data ***datap = datum;
2814
2815 **datap = &sigt->per_cu;
2816 ++*datap;
2817
2818 return 1;
2819}
2820
348e048f
DE
2821/* Create the hash table of all entries in the .debug_types section.
2822 The result is zero if there is an error (e.g. missing .debug_types section),
2823 otherwise non-zero. */
2824
2825static int
2826create_debug_types_hash_table (struct objfile *objfile)
2827{
be391dca 2828 gdb_byte *info_ptr;
348e048f 2829 htab_t types_htab;
1fd400ff 2830 struct dwarf2_per_cu_data **iter;
348e048f 2831
be391dca
TT
2832 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2833 info_ptr = dwarf2_per_objfile->types.buffer;
2834
348e048f
DE
2835 if (info_ptr == NULL)
2836 {
2837 dwarf2_per_objfile->signatured_types = NULL;
2838 return 0;
2839 }
2840
673bfd45 2841 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2842
2843 if (dwarf2_die_debug)
2844 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2845
2846 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2847 {
2848 unsigned int offset;
2849 unsigned int offset_size;
2850 unsigned int type_offset;
2851 unsigned int length, initial_length_size;
2852 unsigned short version;
2853 ULONGEST signature;
2854 struct signatured_type *type_sig;
2855 void **slot;
2856 gdb_byte *ptr = info_ptr;
2857
2858 offset = ptr - dwarf2_per_objfile->types.buffer;
2859
2860 /* We need to read the type's signature in order to build the hash
2861 table, but we don't need to read anything else just yet. */
2862
2863 /* Sanity check to ensure entire cu is present. */
2864 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2865 if (ptr + length + initial_length_size
2866 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2867 {
2868 complaint (&symfile_complaints,
2869 _("debug type entry runs off end of `.debug_types' section, ignored"));
2870 break;
2871 }
2872
2873 offset_size = initial_length_size == 4 ? 4 : 8;
2874 ptr += initial_length_size;
2875 version = bfd_get_16 (objfile->obfd, ptr);
2876 ptr += 2;
2877 ptr += offset_size; /* abbrev offset */
2878 ptr += 1; /* address size */
2879 signature = bfd_get_64 (objfile->obfd, ptr);
2880 ptr += 8;
2881 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2882
2883 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2884 memset (type_sig, 0, sizeof (*type_sig));
2885 type_sig->signature = signature;
2886 type_sig->offset = offset;
2887 type_sig->type_offset = type_offset;
ca1f3406 2888 type_sig->per_cu.objfile = objfile;
1fd400ff 2889 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2890
2891 slot = htab_find_slot (types_htab, type_sig, INSERT);
2892 gdb_assert (slot != NULL);
2893 *slot = type_sig;
2894
2895 if (dwarf2_die_debug)
2896 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2897 offset, phex (signature, sizeof (signature)));
2898
2899 info_ptr = info_ptr + initial_length_size + length;
2900 }
2901
2902 dwarf2_per_objfile->signatured_types = types_htab;
2903
1fd400ff
TT
2904 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2905 dwarf2_per_objfile->type_comp_units
2906 = obstack_alloc (&objfile->objfile_obstack,
2907 dwarf2_per_objfile->n_type_comp_units
2908 * sizeof (struct dwarf2_per_cu_data *));
2909 iter = &dwarf2_per_objfile->type_comp_units[0];
2910 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2911 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2912 == dwarf2_per_objfile->n_type_comp_units);
2913
348e048f
DE
2914 return 1;
2915}
2916
2917/* Lookup a signature based type.
2918 Returns NULL if SIG is not present in the table. */
2919
2920static struct signatured_type *
2921lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2922{
2923 struct signatured_type find_entry, *entry;
2924
2925 if (dwarf2_per_objfile->signatured_types == NULL)
2926 {
2927 complaint (&symfile_complaints,
2928 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2929 return 0;
2930 }
2931
2932 find_entry.signature = sig;
2933 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2934 return entry;
2935}
2936
d85a05f0
DJ
2937/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2938
2939static void
2940init_cu_die_reader (struct die_reader_specs *reader,
2941 struct dwarf2_cu *cu)
2942{
2943 reader->abfd = cu->objfile->obfd;
2944 reader->cu = cu;
2945 if (cu->per_cu->from_debug_types)
be391dca
TT
2946 {
2947 gdb_assert (dwarf2_per_objfile->types.readin);
2948 reader->buffer = dwarf2_per_objfile->types.buffer;
2949 }
d85a05f0 2950 else
be391dca
TT
2951 {
2952 gdb_assert (dwarf2_per_objfile->info.readin);
2953 reader->buffer = dwarf2_per_objfile->info.buffer;
2954 }
d85a05f0
DJ
2955}
2956
2957/* Find the base address of the compilation unit for range lists and
2958 location lists. It will normally be specified by DW_AT_low_pc.
2959 In DWARF-3 draft 4, the base address could be overridden by
2960 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2961 compilation units with discontinuous ranges. */
2962
2963static void
2964dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2965{
2966 struct attribute *attr;
2967
2968 cu->base_known = 0;
2969 cu->base_address = 0;
2970
2971 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2972 if (attr)
2973 {
2974 cu->base_address = DW_ADDR (attr);
2975 cu->base_known = 1;
2976 }
2977 else
2978 {
2979 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2980 if (attr)
2981 {
2982 cu->base_address = DW_ADDR (attr);
2983 cu->base_known = 1;
2984 }
2985 }
2986}
2987
348e048f
DE
2988/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2989 to combine the common parts.
93311388 2990 Process a compilation unit for a psymtab.
348e048f
DE
2991 BUFFER is a pointer to the beginning of the dwarf section buffer,
2992 either .debug_info or debug_types.
93311388
DE
2993 INFO_PTR is a pointer to the start of the CU.
2994 Returns a pointer to the next CU. */
aaa75496 2995
93311388
DE
2996static gdb_byte *
2997process_psymtab_comp_unit (struct objfile *objfile,
2998 struct dwarf2_per_cu_data *this_cu,
2999 gdb_byte *buffer, gdb_byte *info_ptr,
3000 unsigned int buffer_size)
c906108c 3001{
c906108c 3002 bfd *abfd = objfile->obfd;
93311388 3003 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3004 struct die_info *comp_unit_die;
c906108c 3005 struct partial_symtab *pst;
5734ee8b 3006 CORE_ADDR baseaddr;
93311388
DE
3007 struct cleanup *back_to_inner;
3008 struct dwarf2_cu cu;
d85a05f0
DJ
3009 int has_children, has_pc_info;
3010 struct attribute *attr;
d85a05f0
DJ
3011 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3012 struct die_reader_specs reader_specs;
c906108c 3013
93311388
DE
3014 memset (&cu, 0, sizeof (cu));
3015 cu.objfile = objfile;
3016 obstack_init (&cu.comp_unit_obstack);
c906108c 3017
93311388 3018 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3019
93311388
DE
3020 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3021 buffer, buffer_size,
3022 abfd);
10b3939b 3023
93311388
DE
3024 /* Complete the cu_header. */
3025 cu.header.offset = beg_of_comp_unit - buffer;
3026 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3027
93311388 3028 cu.list_in_scope = &file_symbols;
af703f96 3029
328c9494
DJ
3030 /* If this compilation unit was already read in, free the
3031 cached copy in order to read it in again. This is
3032 necessary because we skipped some symbols when we first
3033 read in the compilation unit (see load_partial_dies).
3034 This problem could be avoided, but the benefit is
3035 unclear. */
3036 if (this_cu->cu != NULL)
3037 free_one_cached_comp_unit (this_cu->cu);
3038
3039 /* Note that this is a pointer to our stack frame, being
3040 added to a global data structure. It will be cleaned up
3041 in free_stack_comp_unit when we finish with this
3042 compilation unit. */
3043 this_cu->cu = &cu;
d85a05f0
DJ
3044 cu.per_cu = this_cu;
3045
93311388
DE
3046 /* Read the abbrevs for this compilation unit into a table. */
3047 dwarf2_read_abbrevs (abfd, &cu);
3048 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3049
93311388 3050 /* Read the compilation unit die. */
348e048f
DE
3051 if (this_cu->from_debug_types)
3052 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3053 init_cu_die_reader (&reader_specs, &cu);
3054 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3055 &has_children);
93311388 3056
348e048f
DE
3057 if (this_cu->from_debug_types)
3058 {
3059 /* offset,length haven't been set yet for type units. */
3060 this_cu->offset = cu.header.offset;
3061 this_cu->length = cu.header.length + cu.header.initial_length_size;
3062 }
d85a05f0 3063 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3064 {
93311388
DE
3065 info_ptr = (beg_of_comp_unit + cu.header.length
3066 + cu.header.initial_length_size);
3067 do_cleanups (back_to_inner);
3068 return info_ptr;
3069 }
72bf9492 3070
93311388 3071 /* Set the language we're debugging. */
d85a05f0
DJ
3072 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3073 if (attr)
3074 set_cu_language (DW_UNSND (attr), &cu);
3075 else
3076 set_cu_language (language_minimal, &cu);
c906108c 3077
93311388 3078 /* Allocate a new partial symbol table structure. */
d85a05f0 3079 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3080 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3081 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3082 /* TEXTLOW and TEXTHIGH are set below. */
3083 0,
3084 objfile->global_psymbols.next,
3085 objfile->static_psymbols.next);
72bf9492 3086
d85a05f0
DJ
3087 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3088 if (attr != NULL)
3089 pst->dirname = DW_STRING (attr);
72bf9492 3090
e38df1d0 3091 pst->read_symtab_private = this_cu;
72bf9492 3092
93311388 3093 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3094
93311388
DE
3095 /* Store the function that reads in the rest of the symbol table */
3096 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3097
9291a0cd 3098 this_cu->v.psymtab = pst;
c906108c 3099
d85a05f0
DJ
3100 dwarf2_find_base_address (comp_unit_die, &cu);
3101
93311388
DE
3102 /* Possibly set the default values of LOWPC and HIGHPC from
3103 `DW_AT_ranges'. */
d85a05f0
DJ
3104 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3105 &best_highpc, &cu, pst);
3106 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3107 /* Store the contiguous range if it is not empty; it can be empty for
3108 CUs with no code. */
3109 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3110 best_lowpc + baseaddr,
3111 best_highpc + baseaddr - 1, pst);
93311388
DE
3112
3113 /* Check if comp unit has_children.
3114 If so, read the rest of the partial symbols from this comp unit.
3115 If not, there's no more debug_info for this comp unit. */
d85a05f0 3116 if (has_children)
93311388
DE
3117 {
3118 struct partial_die_info *first_die;
3119 CORE_ADDR lowpc, highpc;
31ffec48 3120
93311388
DE
3121 lowpc = ((CORE_ADDR) -1);
3122 highpc = ((CORE_ADDR) 0);
c906108c 3123
93311388 3124 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3125
93311388 3126 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3127 ! has_pc_info, &cu);
57c22c6c 3128
93311388
DE
3129 /* If we didn't find a lowpc, set it to highpc to avoid
3130 complaints from `maint check'. */
3131 if (lowpc == ((CORE_ADDR) -1))
3132 lowpc = highpc;
10b3939b 3133
93311388
DE
3134 /* If the compilation unit didn't have an explicit address range,
3135 then use the information extracted from its child dies. */
d85a05f0 3136 if (! has_pc_info)
93311388 3137 {
d85a05f0
DJ
3138 best_lowpc = lowpc;
3139 best_highpc = highpc;
93311388
DE
3140 }
3141 }
d85a05f0
DJ
3142 pst->textlow = best_lowpc + baseaddr;
3143 pst->texthigh = best_highpc + baseaddr;
c906108c 3144
93311388
DE
3145 pst->n_global_syms = objfile->global_psymbols.next -
3146 (objfile->global_psymbols.list + pst->globals_offset);
3147 pst->n_static_syms = objfile->static_psymbols.next -
3148 (objfile->static_psymbols.list + pst->statics_offset);
3149 sort_pst_symbols (pst);
c906108c 3150
93311388
DE
3151 info_ptr = (beg_of_comp_unit + cu.header.length
3152 + cu.header.initial_length_size);
ae038cb0 3153
348e048f
DE
3154 if (this_cu->from_debug_types)
3155 {
3156 /* It's not clear we want to do anything with stmt lists here.
3157 Waiting to see what gcc ultimately does. */
3158 }
d85a05f0 3159 else
93311388
DE
3160 {
3161 /* Get the list of files included in the current compilation unit,
3162 and build a psymtab for each of them. */
d85a05f0 3163 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3164 }
ae038cb0 3165
93311388 3166 do_cleanups (back_to_inner);
ae038cb0 3167
93311388
DE
3168 return info_ptr;
3169}
ff013f42 3170
348e048f
DE
3171/* Traversal function for htab_traverse_noresize.
3172 Process one .debug_types comp-unit. */
3173
3174static int
3175process_type_comp_unit (void **slot, void *info)
3176{
3177 struct signatured_type *entry = (struct signatured_type *) *slot;
3178 struct objfile *objfile = (struct objfile *) info;
3179 struct dwarf2_per_cu_data *this_cu;
3180
3181 this_cu = &entry->per_cu;
348e048f 3182
be391dca 3183 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3184 process_psymtab_comp_unit (objfile, this_cu,
3185 dwarf2_per_objfile->types.buffer,
3186 dwarf2_per_objfile->types.buffer + entry->offset,
3187 dwarf2_per_objfile->types.size);
3188
3189 return 1;
3190}
3191
3192/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3193 Build partial symbol tables for the .debug_types comp-units. */
3194
3195static void
3196build_type_psymtabs (struct objfile *objfile)
3197{
3198 if (! create_debug_types_hash_table (objfile))
3199 return;
3200
3201 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3202 process_type_comp_unit, objfile);
3203}
3204
60606b2c
TT
3205/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3206
3207static void
3208psymtabs_addrmap_cleanup (void *o)
3209{
3210 struct objfile *objfile = o;
ec61707d 3211
60606b2c
TT
3212 objfile->psymtabs_addrmap = NULL;
3213}
3214
93311388
DE
3215/* Build the partial symbol table by doing a quick pass through the
3216 .debug_info and .debug_abbrev sections. */
72bf9492 3217
93311388 3218static void
c67a9c90 3219dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3220{
93311388 3221 gdb_byte *info_ptr;
60606b2c
TT
3222 struct cleanup *back_to, *addrmap_cleanup;
3223 struct obstack temp_obstack;
93311388 3224
98bfdba5
PA
3225 dwarf2_per_objfile->reading_partial_symbols = 1;
3226
be391dca 3227 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3228 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3229
93311388
DE
3230 /* Any cached compilation units will be linked by the per-objfile
3231 read_in_chain. Make sure to free them when we're done. */
3232 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3233
348e048f
DE
3234 build_type_psymtabs (objfile);
3235
93311388 3236 create_all_comp_units (objfile);
c906108c 3237
60606b2c
TT
3238 /* Create a temporary address map on a temporary obstack. We later
3239 copy this to the final obstack. */
3240 obstack_init (&temp_obstack);
3241 make_cleanup_obstack_free (&temp_obstack);
3242 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3243 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3244
93311388
DE
3245 /* Since the objects we're extracting from .debug_info vary in
3246 length, only the individual functions to extract them (like
3247 read_comp_unit_head and load_partial_die) can really know whether
3248 the buffer is large enough to hold another complete object.
c906108c 3249
93311388
DE
3250 At the moment, they don't actually check that. If .debug_info
3251 holds just one extra byte after the last compilation unit's dies,
3252 then read_comp_unit_head will happily read off the end of the
3253 buffer. read_partial_die is similarly casual. Those functions
3254 should be fixed.
c906108c 3255
93311388
DE
3256 For this loop condition, simply checking whether there's any data
3257 left at all should be sufficient. */
c906108c 3258
93311388
DE
3259 while (info_ptr < (dwarf2_per_objfile->info.buffer
3260 + dwarf2_per_objfile->info.size))
3261 {
3262 struct dwarf2_per_cu_data *this_cu;
dd373385 3263
93311388
DE
3264 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3265 objfile);
aaa75496 3266
93311388
DE
3267 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3268 dwarf2_per_objfile->info.buffer,
3269 info_ptr,
3270 dwarf2_per_objfile->info.size);
c906108c 3271 }
ff013f42
JK
3272
3273 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3274 &objfile->objfile_obstack);
60606b2c 3275 discard_cleanups (addrmap_cleanup);
ff013f42 3276
ae038cb0
DJ
3277 do_cleanups (back_to);
3278}
3279
93311388 3280/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3281
3282static void
93311388
DE
3283load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3284 struct objfile *objfile)
ae038cb0
DJ
3285{
3286 bfd *abfd = objfile->obfd;
fe1b8b76 3287 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3288 struct die_info *comp_unit_die;
ae038cb0 3289 struct dwarf2_cu *cu;
1d9ec526 3290 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3291 struct attribute *attr;
3292 int has_children;
3293 struct die_reader_specs reader_specs;
98bfdba5 3294 int read_cu = 0;
ae038cb0 3295
348e048f
DE
3296 gdb_assert (! this_cu->from_debug_types);
3297
be391dca 3298 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3299 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3300 beg_of_comp_unit = info_ptr;
3301
98bfdba5
PA
3302 if (this_cu->cu == NULL)
3303 {
3304 cu = alloc_one_comp_unit (objfile);
ae038cb0 3305
98bfdba5 3306 read_cu = 1;
ae038cb0 3307
98bfdba5
PA
3308 /* If an error occurs while loading, release our storage. */
3309 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3310
98bfdba5
PA
3311 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3312 dwarf2_per_objfile->info.buffer,
3313 dwarf2_per_objfile->info.size,
3314 abfd);
ae038cb0 3315
98bfdba5
PA
3316 /* Complete the cu_header. */
3317 cu->header.offset = this_cu->offset;
3318 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3319
3320 /* Link this compilation unit into the compilation unit tree. */
3321 this_cu->cu = cu;
3322 cu->per_cu = this_cu;
98bfdba5
PA
3323
3324 /* Link this CU into read_in_chain. */
3325 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3326 dwarf2_per_objfile->read_in_chain = this_cu;
3327 }
3328 else
3329 {
3330 cu = this_cu->cu;
3331 info_ptr += cu->header.first_die_offset;
3332 }
ae038cb0
DJ
3333
3334 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3335 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3336 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3337 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3338
3339 /* Read the compilation unit die. */
d85a05f0
DJ
3340 init_cu_die_reader (&reader_specs, cu);
3341 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3342 &has_children);
ae038cb0
DJ
3343
3344 /* Set the language we're debugging. */
d85a05f0
DJ
3345 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3346 if (attr)
3347 set_cu_language (DW_UNSND (attr), cu);
3348 else
3349 set_cu_language (language_minimal, cu);
ae038cb0 3350
ae038cb0
DJ
3351 /* Check if comp unit has_children.
3352 If so, read the rest of the partial symbols from this comp unit.
3353 If not, there's no more debug_info for this comp unit. */
d85a05f0 3354 if (has_children)
93311388 3355 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3356
98bfdba5
PA
3357 do_cleanups (free_abbrevs_cleanup);
3358
3359 if (read_cu)
3360 {
3361 /* We've successfully allocated this compilation unit. Let our
3362 caller clean it up when finished with it. */
3363 discard_cleanups (free_cu_cleanup);
3364 }
ae038cb0
DJ
3365}
3366
3367/* Create a list of all compilation units in OBJFILE. We do this only
3368 if an inter-comp-unit reference is found; presumably if there is one,
3369 there will be many, and one will occur early in the .debug_info section.
3370 So there's no point in building this list incrementally. */
3371
3372static void
3373create_all_comp_units (struct objfile *objfile)
3374{
3375 int n_allocated;
3376 int n_comp_units;
3377 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3378 gdb_byte *info_ptr;
3379
3380 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3381 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3382
3383 n_comp_units = 0;
3384 n_allocated = 10;
3385 all_comp_units = xmalloc (n_allocated
3386 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3387
dce234bc 3388 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3389 {
c764a876 3390 unsigned int length, initial_length_size;
ae038cb0 3391 struct dwarf2_per_cu_data *this_cu;
c764a876 3392 unsigned int offset;
ae038cb0 3393
dce234bc 3394 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3395
3396 /* Read just enough information to find out where the next
3397 compilation unit is. */
c764a876
DE
3398 length = read_initial_length (objfile->obfd, info_ptr,
3399 &initial_length_size);
ae038cb0
DJ
3400
3401 /* Save the compilation unit for later lookup. */
3402 this_cu = obstack_alloc (&objfile->objfile_obstack,
3403 sizeof (struct dwarf2_per_cu_data));
3404 memset (this_cu, 0, sizeof (*this_cu));
3405 this_cu->offset = offset;
c764a876 3406 this_cu->length = length + initial_length_size;
9291a0cd 3407 this_cu->objfile = objfile;
ae038cb0
DJ
3408
3409 if (n_comp_units == n_allocated)
3410 {
3411 n_allocated *= 2;
3412 all_comp_units = xrealloc (all_comp_units,
3413 n_allocated
3414 * sizeof (struct dwarf2_per_cu_data *));
3415 }
3416 all_comp_units[n_comp_units++] = this_cu;
3417
3418 info_ptr = info_ptr + this_cu->length;
3419 }
3420
3421 dwarf2_per_objfile->all_comp_units
3422 = obstack_alloc (&objfile->objfile_obstack,
3423 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3424 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3425 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3426 xfree (all_comp_units);
3427 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3428}
3429
5734ee8b
DJ
3430/* Process all loaded DIEs for compilation unit CU, starting at
3431 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3432 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3433 DW_AT_ranges). If NEED_PC is set, then this function will set
3434 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3435 and record the covered ranges in the addrmap. */
c906108c 3436
72bf9492
DJ
3437static void
3438scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3439 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3440{
72bf9492 3441 struct partial_die_info *pdi;
c906108c 3442
91c24f0a
DC
3443 /* Now, march along the PDI's, descending into ones which have
3444 interesting children but skipping the children of the other ones,
3445 until we reach the end of the compilation unit. */
c906108c 3446
72bf9492 3447 pdi = first_die;
91c24f0a 3448
72bf9492
DJ
3449 while (pdi != NULL)
3450 {
3451 fixup_partial_die (pdi, cu);
c906108c 3452
f55ee35c 3453 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3454 children, so we need to look at them. Ditto for anonymous
3455 enums. */
933c6fe4 3456
72bf9492 3457 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3458 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3459 {
72bf9492 3460 switch (pdi->tag)
c906108c
SS
3461 {
3462 case DW_TAG_subprogram:
5734ee8b 3463 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3464 break;
72929c62 3465 case DW_TAG_constant:
c906108c
SS
3466 case DW_TAG_variable:
3467 case DW_TAG_typedef:
91c24f0a 3468 case DW_TAG_union_type:
72bf9492 3469 if (!pdi->is_declaration)
63d06c5c 3470 {
72bf9492 3471 add_partial_symbol (pdi, cu);
63d06c5c
DC
3472 }
3473 break;
c906108c 3474 case DW_TAG_class_type:
680b30c7 3475 case DW_TAG_interface_type:
c906108c 3476 case DW_TAG_structure_type:
72bf9492 3477 if (!pdi->is_declaration)
c906108c 3478 {
72bf9492 3479 add_partial_symbol (pdi, cu);
c906108c
SS
3480 }
3481 break;
91c24f0a 3482 case DW_TAG_enumeration_type:
72bf9492
DJ
3483 if (!pdi->is_declaration)
3484 add_partial_enumeration (pdi, cu);
c906108c
SS
3485 break;
3486 case DW_TAG_base_type:
a02abb62 3487 case DW_TAG_subrange_type:
c906108c 3488 /* File scope base type definitions are added to the partial
c5aa993b 3489 symbol table. */
72bf9492 3490 add_partial_symbol (pdi, cu);
c906108c 3491 break;
d9fa45fe 3492 case DW_TAG_namespace:
5734ee8b 3493 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3494 break;
5d7cb8df
JK
3495 case DW_TAG_module:
3496 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3497 break;
c906108c
SS
3498 default:
3499 break;
3500 }
3501 }
3502
72bf9492
DJ
3503 /* If the die has a sibling, skip to the sibling. */
3504
3505 pdi = pdi->die_sibling;
3506 }
3507}
3508
3509/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3510
72bf9492 3511 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3512 name is concatenated with "::" and the partial DIE's name. For
3513 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3514 Enumerators are an exception; they use the scope of their parent
3515 enumeration type, i.e. the name of the enumeration type is not
3516 prepended to the enumerator.
91c24f0a 3517
72bf9492
DJ
3518 There are two complexities. One is DW_AT_specification; in this
3519 case "parent" means the parent of the target of the specification,
3520 instead of the direct parent of the DIE. The other is compilers
3521 which do not emit DW_TAG_namespace; in this case we try to guess
3522 the fully qualified name of structure types from their members'
3523 linkage names. This must be done using the DIE's children rather
3524 than the children of any DW_AT_specification target. We only need
3525 to do this for structures at the top level, i.e. if the target of
3526 any DW_AT_specification (if any; otherwise the DIE itself) does not
3527 have a parent. */
3528
3529/* Compute the scope prefix associated with PDI's parent, in
3530 compilation unit CU. The result will be allocated on CU's
3531 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3532 field. NULL is returned if no prefix is necessary. */
3533static char *
3534partial_die_parent_scope (struct partial_die_info *pdi,
3535 struct dwarf2_cu *cu)
3536{
3537 char *grandparent_scope;
3538 struct partial_die_info *parent, *real_pdi;
91c24f0a 3539
72bf9492
DJ
3540 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3541 then this means the parent of the specification DIE. */
3542
3543 real_pdi = pdi;
72bf9492 3544 while (real_pdi->has_specification)
10b3939b 3545 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3546
3547 parent = real_pdi->die_parent;
3548 if (parent == NULL)
3549 return NULL;
3550
3551 if (parent->scope_set)
3552 return parent->scope;
3553
3554 fixup_partial_die (parent, cu);
3555
10b3939b 3556 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3557
acebe513
UW
3558 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3559 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3560 Work around this problem here. */
3561 if (cu->language == language_cplus
6e70227d 3562 && parent->tag == DW_TAG_namespace
acebe513
UW
3563 && strcmp (parent->name, "::") == 0
3564 && grandparent_scope == NULL)
3565 {
3566 parent->scope = NULL;
3567 parent->scope_set = 1;
3568 return NULL;
3569 }
3570
72bf9492 3571 if (parent->tag == DW_TAG_namespace
f55ee35c 3572 || parent->tag == DW_TAG_module
72bf9492
DJ
3573 || parent->tag == DW_TAG_structure_type
3574 || parent->tag == DW_TAG_class_type
680b30c7 3575 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3576 || parent->tag == DW_TAG_union_type
3577 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3578 {
3579 if (grandparent_scope == NULL)
3580 parent->scope = parent->name;
3581 else
987504bb 3582 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3583 parent->name, 0, cu);
72bf9492 3584 }
ceeb3d5a 3585 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3586 /* Enumerators should not get the name of the enumeration as a prefix. */
3587 parent->scope = grandparent_scope;
3588 else
3589 {
3590 /* FIXME drow/2004-04-01: What should we be doing with
3591 function-local names? For partial symbols, we should probably be
3592 ignoring them. */
3593 complaint (&symfile_complaints,
e2e0b3e5 3594 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3595 parent->tag, pdi->offset);
3596 parent->scope = grandparent_scope;
c906108c
SS
3597 }
3598
72bf9492
DJ
3599 parent->scope_set = 1;
3600 return parent->scope;
3601}
3602
3603/* Return the fully scoped name associated with PDI, from compilation unit
3604 CU. The result will be allocated with malloc. */
3605static char *
3606partial_die_full_name (struct partial_die_info *pdi,
3607 struct dwarf2_cu *cu)
3608{
3609 char *parent_scope;
3610
98bfdba5
PA
3611 /* If this is a template instantiation, we can not work out the
3612 template arguments from partial DIEs. So, unfortunately, we have
3613 to go through the full DIEs. At least any work we do building
3614 types here will be reused if full symbols are loaded later. */
3615 if (pdi->has_template_arguments)
3616 {
3617 fixup_partial_die (pdi, cu);
3618
3619 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3620 {
3621 struct die_info *die;
3622 struct attribute attr;
3623 struct dwarf2_cu *ref_cu = cu;
3624
3625 attr.name = 0;
3626 attr.form = DW_FORM_ref_addr;
3627 attr.u.addr = pdi->offset;
3628 die = follow_die_ref (NULL, &attr, &ref_cu);
3629
3630 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3631 }
3632 }
3633
72bf9492
DJ
3634 parent_scope = partial_die_parent_scope (pdi, cu);
3635 if (parent_scope == NULL)
3636 return NULL;
3637 else
f55ee35c 3638 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3639}
3640
3641static void
72bf9492 3642add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3643{
e7c27a73 3644 struct objfile *objfile = cu->objfile;
c906108c 3645 CORE_ADDR addr = 0;
decbce07 3646 char *actual_name = NULL;
5c4e30ca 3647 const struct partial_symbol *psym = NULL;
e142c38c 3648 CORE_ADDR baseaddr;
72bf9492 3649 int built_actual_name = 0;
e142c38c
DJ
3650
3651 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3652
94af9270
KS
3653 actual_name = partial_die_full_name (pdi, cu);
3654 if (actual_name)
3655 built_actual_name = 1;
63d06c5c 3656
72bf9492
DJ
3657 if (actual_name == NULL)
3658 actual_name = pdi->name;
3659
c906108c
SS
3660 switch (pdi->tag)
3661 {
3662 case DW_TAG_subprogram:
2cfa0c8d 3663 if (pdi->is_external || cu->language == language_ada)
c906108c 3664 {
2cfa0c8d
JB
3665 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3666 of the global scope. But in Ada, we want to be able to access
3667 nested procedures globally. So all Ada subprograms are stored
3668 in the global scope. */
38d518c9 3669 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3670 mst_text, objfile); */
38d518c9 3671 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3672 built_actual_name,
5c4e30ca
DC
3673 VAR_DOMAIN, LOC_BLOCK,
3674 &objfile->global_psymbols,
3675 0, pdi->lowpc + baseaddr,
e142c38c 3676 cu->language, objfile);
c906108c
SS
3677 }
3678 else
3679 {
38d518c9 3680 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3681 mst_file_text, objfile); */
38d518c9 3682 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3683 built_actual_name,
5c4e30ca
DC
3684 VAR_DOMAIN, LOC_BLOCK,
3685 &objfile->static_psymbols,
3686 0, pdi->lowpc + baseaddr,
e142c38c 3687 cu->language, objfile);
c906108c
SS
3688 }
3689 break;
72929c62
JB
3690 case DW_TAG_constant:
3691 {
3692 struct psymbol_allocation_list *list;
3693
3694 if (pdi->is_external)
3695 list = &objfile->global_psymbols;
3696 else
3697 list = &objfile->static_psymbols;
3698 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3699 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3700 list, 0, 0, cu->language, objfile);
3701
3702 }
3703 break;
c906108c 3704 case DW_TAG_variable:
caac4577
JG
3705 if (pdi->locdesc)
3706 addr = decode_locdesc (pdi->locdesc, cu);
3707
3708 if (pdi->locdesc
3709 && addr == 0
3710 && !dwarf2_per_objfile->has_section_at_zero)
3711 {
3712 /* A global or static variable may also have been stripped
3713 out by the linker if unused, in which case its address
3714 will be nullified; do not add such variables into partial
3715 symbol table then. */
3716 }
3717 else if (pdi->is_external)
c906108c
SS
3718 {
3719 /* Global Variable.
3720 Don't enter into the minimal symbol tables as there is
3721 a minimal symbol table entry from the ELF symbols already.
3722 Enter into partial symbol table if it has a location
3723 descriptor or a type.
3724 If the location descriptor is missing, new_symbol will create
3725 a LOC_UNRESOLVED symbol, the address of the variable will then
3726 be determined from the minimal symbol table whenever the variable
3727 is referenced.
3728 The address for the partial symbol table entry is not
3729 used by GDB, but it comes in handy for debugging partial symbol
3730 table building. */
3731
c906108c 3732 if (pdi->locdesc || pdi->has_type)
38d518c9 3733 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3734 built_actual_name,
5c4e30ca
DC
3735 VAR_DOMAIN, LOC_STATIC,
3736 &objfile->global_psymbols,
3737 0, addr + baseaddr,
e142c38c 3738 cu->language, objfile);
c906108c
SS
3739 }
3740 else
3741 {
3742 /* Static Variable. Skip symbols without location descriptors. */
3743 if (pdi->locdesc == NULL)
decbce07
MS
3744 {
3745 if (built_actual_name)
3746 xfree (actual_name);
3747 return;
3748 }
38d518c9 3749 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3750 mst_file_data, objfile); */
38d518c9 3751 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3752 built_actual_name,
5c4e30ca
DC
3753 VAR_DOMAIN, LOC_STATIC,
3754 &objfile->static_psymbols,
3755 0, addr + baseaddr,
e142c38c 3756 cu->language, objfile);
c906108c
SS
3757 }
3758 break;
3759 case DW_TAG_typedef:
3760 case DW_TAG_base_type:
a02abb62 3761 case DW_TAG_subrange_type:
38d518c9 3762 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3763 built_actual_name,
176620f1 3764 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3765 &objfile->static_psymbols,
e142c38c 3766 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3767 break;
72bf9492
DJ
3768 case DW_TAG_namespace:
3769 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3770 built_actual_name,
72bf9492
DJ
3771 VAR_DOMAIN, LOC_TYPEDEF,
3772 &objfile->global_psymbols,
3773 0, (CORE_ADDR) 0, cu->language, objfile);
3774 break;
c906108c 3775 case DW_TAG_class_type:
680b30c7 3776 case DW_TAG_interface_type:
c906108c
SS
3777 case DW_TAG_structure_type:
3778 case DW_TAG_union_type:
3779 case DW_TAG_enumeration_type:
fa4028e9
JB
3780 /* Skip external references. The DWARF standard says in the section
3781 about "Structure, Union, and Class Type Entries": "An incomplete
3782 structure, union or class type is represented by a structure,
3783 union or class entry that does not have a byte size attribute
3784 and that has a DW_AT_declaration attribute." */
3785 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3786 {
3787 if (built_actual_name)
3788 xfree (actual_name);
3789 return;
3790 }
fa4028e9 3791
63d06c5c
DC
3792 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3793 static vs. global. */
38d518c9 3794 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3795 built_actual_name,
176620f1 3796 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3797 (cu->language == language_cplus
3798 || cu->language == language_java)
63d06c5c
DC
3799 ? &objfile->global_psymbols
3800 : &objfile->static_psymbols,
e142c38c 3801 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3802
c906108c
SS
3803 break;
3804 case DW_TAG_enumerator:
38d518c9 3805 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3806 built_actual_name,
176620f1 3807 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3808 (cu->language == language_cplus
3809 || cu->language == language_java)
f6fe98ef
DJ
3810 ? &objfile->global_psymbols
3811 : &objfile->static_psymbols,
e142c38c 3812 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3813 break;
3814 default:
3815 break;
3816 }
5c4e30ca 3817
72bf9492
DJ
3818 if (built_actual_name)
3819 xfree (actual_name);
c906108c
SS
3820}
3821
5c4e30ca
DC
3822/* Read a partial die corresponding to a namespace; also, add a symbol
3823 corresponding to that namespace to the symbol table. NAMESPACE is
3824 the name of the enclosing namespace. */
91c24f0a 3825
72bf9492
DJ
3826static void
3827add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3828 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3829 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3830{
72bf9492 3831 /* Add a symbol for the namespace. */
e7c27a73 3832
72bf9492 3833 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3834
3835 /* Now scan partial symbols in that namespace. */
3836
91c24f0a 3837 if (pdi->has_children)
5734ee8b 3838 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3839}
3840
5d7cb8df
JK
3841/* Read a partial die corresponding to a Fortran module. */
3842
3843static void
3844add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3845 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3846{
f55ee35c 3847 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3848
3849 if (pdi->has_children)
3850 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3851}
3852
bc30ff58
JB
3853/* Read a partial die corresponding to a subprogram and create a partial
3854 symbol for that subprogram. When the CU language allows it, this
3855 routine also defines a partial symbol for each nested subprogram
3856 that this subprogram contains.
6e70227d 3857
bc30ff58
JB
3858 DIE my also be a lexical block, in which case we simply search
3859 recursively for suprograms defined inside that lexical block.
3860 Again, this is only performed when the CU language allows this
3861 type of definitions. */
3862
3863static void
3864add_partial_subprogram (struct partial_die_info *pdi,
3865 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3866 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3867{
3868 if (pdi->tag == DW_TAG_subprogram)
3869 {
3870 if (pdi->has_pc_info)
3871 {
3872 if (pdi->lowpc < *lowpc)
3873 *lowpc = pdi->lowpc;
3874 if (pdi->highpc > *highpc)
3875 *highpc = pdi->highpc;
5734ee8b
DJ
3876 if (need_pc)
3877 {
3878 CORE_ADDR baseaddr;
3879 struct objfile *objfile = cu->objfile;
3880
3881 baseaddr = ANOFFSET (objfile->section_offsets,
3882 SECT_OFF_TEXT (objfile));
3883 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3884 pdi->lowpc + baseaddr,
3885 pdi->highpc - 1 + baseaddr,
9291a0cd 3886 cu->per_cu->v.psymtab);
5734ee8b 3887 }
bc30ff58 3888 if (!pdi->is_declaration)
e8d05480
JB
3889 /* Ignore subprogram DIEs that do not have a name, they are
3890 illegal. Do not emit a complaint at this point, we will
3891 do so when we convert this psymtab into a symtab. */
3892 if (pdi->name)
3893 add_partial_symbol (pdi, cu);
bc30ff58
JB
3894 }
3895 }
6e70227d 3896
bc30ff58
JB
3897 if (! pdi->has_children)
3898 return;
3899
3900 if (cu->language == language_ada)
3901 {
3902 pdi = pdi->die_child;
3903 while (pdi != NULL)
3904 {
3905 fixup_partial_die (pdi, cu);
3906 if (pdi->tag == DW_TAG_subprogram
3907 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3908 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3909 pdi = pdi->die_sibling;
3910 }
3911 }
3912}
3913
91c24f0a
DC
3914/* Read a partial die corresponding to an enumeration type. */
3915
72bf9492
DJ
3916static void
3917add_partial_enumeration (struct partial_die_info *enum_pdi,
3918 struct dwarf2_cu *cu)
91c24f0a 3919{
72bf9492 3920 struct partial_die_info *pdi;
91c24f0a
DC
3921
3922 if (enum_pdi->name != NULL)
72bf9492
DJ
3923 add_partial_symbol (enum_pdi, cu);
3924
3925 pdi = enum_pdi->die_child;
3926 while (pdi)
91c24f0a 3927 {
72bf9492 3928 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3929 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3930 else
72bf9492
DJ
3931 add_partial_symbol (pdi, cu);
3932 pdi = pdi->die_sibling;
91c24f0a 3933 }
91c24f0a
DC
3934}
3935
4bb7a0a7
DJ
3936/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3937 Return the corresponding abbrev, or NULL if the number is zero (indicating
3938 an empty DIE). In either case *BYTES_READ will be set to the length of
3939 the initial number. */
3940
3941static struct abbrev_info *
fe1b8b76 3942peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3943 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3944{
3945 bfd *abfd = cu->objfile->obfd;
3946 unsigned int abbrev_number;
3947 struct abbrev_info *abbrev;
3948
3949 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3950
3951 if (abbrev_number == 0)
3952 return NULL;
3953
3954 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3955 if (!abbrev)
3956 {
8a3fe4f8 3957 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3958 bfd_get_filename (abfd));
3959 }
3960
3961 return abbrev;
3962}
3963
93311388
DE
3964/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3965 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3966 DIE. Any children of the skipped DIEs will also be skipped. */
3967
fe1b8b76 3968static gdb_byte *
93311388 3969skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3970{
3971 struct abbrev_info *abbrev;
3972 unsigned int bytes_read;
3973
3974 while (1)
3975 {
3976 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3977 if (abbrev == NULL)
3978 return info_ptr + bytes_read;
3979 else
93311388 3980 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3981 }
3982}
3983
93311388
DE
3984/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3985 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3986 abbrev corresponding to that skipped uleb128 should be passed in
3987 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3988 children. */
3989
fe1b8b76 3990static gdb_byte *
93311388
DE
3991skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3992 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3993{
3994 unsigned int bytes_read;
3995 struct attribute attr;
3996 bfd *abfd = cu->objfile->obfd;
3997 unsigned int form, i;
3998
3999 for (i = 0; i < abbrev->num_attrs; i++)
4000 {
4001 /* The only abbrev we care about is DW_AT_sibling. */
4002 if (abbrev->attrs[i].name == DW_AT_sibling)
4003 {
4004 read_attribute (&attr, &abbrev->attrs[i],
4005 abfd, info_ptr, cu);
4006 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4007 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4008 else
93311388 4009 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4010 }
4011
4012 /* If it isn't DW_AT_sibling, skip this attribute. */
4013 form = abbrev->attrs[i].form;
4014 skip_attribute:
4015 switch (form)
4016 {
4bb7a0a7 4017 case DW_FORM_ref_addr:
ae411497
TT
4018 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4019 and later it is offset sized. */
4020 if (cu->header.version == 2)
4021 info_ptr += cu->header.addr_size;
4022 else
4023 info_ptr += cu->header.offset_size;
4024 break;
4025 case DW_FORM_addr:
4bb7a0a7
DJ
4026 info_ptr += cu->header.addr_size;
4027 break;
4028 case DW_FORM_data1:
4029 case DW_FORM_ref1:
4030 case DW_FORM_flag:
4031 info_ptr += 1;
4032 break;
2dc7f7b3
TT
4033 case DW_FORM_flag_present:
4034 break;
4bb7a0a7
DJ
4035 case DW_FORM_data2:
4036 case DW_FORM_ref2:
4037 info_ptr += 2;
4038 break;
4039 case DW_FORM_data4:
4040 case DW_FORM_ref4:
4041 info_ptr += 4;
4042 break;
4043 case DW_FORM_data8:
4044 case DW_FORM_ref8:
348e048f 4045 case DW_FORM_sig8:
4bb7a0a7
DJ
4046 info_ptr += 8;
4047 break;
4048 case DW_FORM_string:
9b1c24c8 4049 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4050 info_ptr += bytes_read;
4051 break;
2dc7f7b3 4052 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4053 case DW_FORM_strp:
4054 info_ptr += cu->header.offset_size;
4055 break;
2dc7f7b3 4056 case DW_FORM_exprloc:
4bb7a0a7
DJ
4057 case DW_FORM_block:
4058 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4059 info_ptr += bytes_read;
4060 break;
4061 case DW_FORM_block1:
4062 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4063 break;
4064 case DW_FORM_block2:
4065 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4066 break;
4067 case DW_FORM_block4:
4068 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4069 break;
4070 case DW_FORM_sdata:
4071 case DW_FORM_udata:
4072 case DW_FORM_ref_udata:
4073 info_ptr = skip_leb128 (abfd, info_ptr);
4074 break;
4075 case DW_FORM_indirect:
4076 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4077 info_ptr += bytes_read;
4078 /* We need to continue parsing from here, so just go back to
4079 the top. */
4080 goto skip_attribute;
4081
4082 default:
8a3fe4f8 4083 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4084 dwarf_form_name (form),
4085 bfd_get_filename (abfd));
4086 }
4087 }
4088
4089 if (abbrev->has_children)
93311388 4090 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4091 else
4092 return info_ptr;
4093}
4094
93311388
DE
4095/* Locate ORIG_PDI's sibling.
4096 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4097 in BUFFER. */
91c24f0a 4098
fe1b8b76 4099static gdb_byte *
93311388
DE
4100locate_pdi_sibling (struct partial_die_info *orig_pdi,
4101 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4102 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4103{
4104 /* Do we know the sibling already? */
72bf9492 4105
91c24f0a
DC
4106 if (orig_pdi->sibling)
4107 return orig_pdi->sibling;
4108
4109 /* Are there any children to deal with? */
4110
4111 if (!orig_pdi->has_children)
4112 return info_ptr;
4113
4bb7a0a7 4114 /* Skip the children the long way. */
91c24f0a 4115
93311388 4116 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4117}
4118
c906108c
SS
4119/* Expand this partial symbol table into a full symbol table. */
4120
4121static void
fba45db2 4122dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4123{
c906108c
SS
4124 if (pst != NULL)
4125 {
4126 if (pst->readin)
4127 {
8a3fe4f8 4128 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4129 }
4130 else
4131 {
4132 if (info_verbose)
4133 {
a3f17187 4134 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4135 gdb_flush (gdb_stdout);
4136 }
4137
10b3939b
DJ
4138 /* Restore our global data. */
4139 dwarf2_per_objfile = objfile_data (pst->objfile,
4140 dwarf2_objfile_data_key);
4141
b2ab525c
KB
4142 /* If this psymtab is constructed from a debug-only objfile, the
4143 has_section_at_zero flag will not necessarily be correct. We
4144 can get the correct value for this flag by looking at the data
4145 associated with the (presumably stripped) associated objfile. */
4146 if (pst->objfile->separate_debug_objfile_backlink)
4147 {
4148 struct dwarf2_per_objfile *dpo_backlink
4149 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4150 dwarf2_objfile_data_key);
9a619af0 4151
b2ab525c
KB
4152 dwarf2_per_objfile->has_section_at_zero
4153 = dpo_backlink->has_section_at_zero;
4154 }
4155
98bfdba5
PA
4156 dwarf2_per_objfile->reading_partial_symbols = 0;
4157
c906108c
SS
4158 psymtab_to_symtab_1 (pst);
4159
4160 /* Finish up the debug error message. */
4161 if (info_verbose)
a3f17187 4162 printf_filtered (_("done.\n"));
c906108c
SS
4163 }
4164 }
4165}
4166
10b3939b
DJ
4167/* Add PER_CU to the queue. */
4168
4169static void
03dd20cc 4170queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4171{
4172 struct dwarf2_queue_item *item;
4173
4174 per_cu->queued = 1;
4175 item = xmalloc (sizeof (*item));
4176 item->per_cu = per_cu;
4177 item->next = NULL;
4178
4179 if (dwarf2_queue == NULL)
4180 dwarf2_queue = item;
4181 else
4182 dwarf2_queue_tail->next = item;
4183
4184 dwarf2_queue_tail = item;
4185}
4186
4187/* Process the queue. */
4188
4189static void
4190process_queue (struct objfile *objfile)
4191{
4192 struct dwarf2_queue_item *item, *next_item;
4193
03dd20cc
DJ
4194 /* The queue starts out with one item, but following a DIE reference
4195 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4196 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4197 {
9291a0cd
TT
4198 if (dwarf2_per_objfile->using_index
4199 ? !item->per_cu->v.quick->symtab
4200 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4201 process_full_comp_unit (item->per_cu);
4202
4203 item->per_cu->queued = 0;
4204 next_item = item->next;
4205 xfree (item);
4206 }
4207
4208 dwarf2_queue_tail = NULL;
4209}
4210
4211/* Free all allocated queue entries. This function only releases anything if
4212 an error was thrown; if the queue was processed then it would have been
4213 freed as we went along. */
4214
4215static void
4216dwarf2_release_queue (void *dummy)
4217{
4218 struct dwarf2_queue_item *item, *last;
4219
4220 item = dwarf2_queue;
4221 while (item)
4222 {
4223 /* Anything still marked queued is likely to be in an
4224 inconsistent state, so discard it. */
4225 if (item->per_cu->queued)
4226 {
4227 if (item->per_cu->cu != NULL)
4228 free_one_cached_comp_unit (item->per_cu->cu);
4229 item->per_cu->queued = 0;
4230 }
4231
4232 last = item;
4233 item = item->next;
4234 xfree (last);
4235 }
4236
4237 dwarf2_queue = dwarf2_queue_tail = NULL;
4238}
4239
4240/* Read in full symbols for PST, and anything it depends on. */
4241
c906108c 4242static void
fba45db2 4243psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4244{
10b3939b 4245 struct dwarf2_per_cu_data *per_cu;
c906108c 4246 struct cleanup *back_to;
aaa75496
JB
4247 int i;
4248
4249 for (i = 0; i < pst->number_of_dependencies; i++)
4250 if (!pst->dependencies[i]->readin)
4251 {
4252 /* Inform about additional files that need to be read in. */
4253 if (info_verbose)
4254 {
a3f17187 4255 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4256 fputs_filtered (" ", gdb_stdout);
4257 wrap_here ("");
4258 fputs_filtered ("and ", gdb_stdout);
4259 wrap_here ("");
4260 printf_filtered ("%s...", pst->dependencies[i]->filename);
4261 wrap_here (""); /* Flush output */
4262 gdb_flush (gdb_stdout);
4263 }
4264 psymtab_to_symtab_1 (pst->dependencies[i]);
4265 }
4266
e38df1d0 4267 per_cu = pst->read_symtab_private;
10b3939b
DJ
4268
4269 if (per_cu == NULL)
aaa75496
JB
4270 {
4271 /* It's an include file, no symbols to read for it.
4272 Everything is in the parent symtab. */
4273 pst->readin = 1;
4274 return;
4275 }
c906108c 4276
9291a0cd 4277 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4278}
4279
93311388 4280/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4281
93311388 4282static void
31ffec48 4283load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4284{
31ffec48 4285 bfd *abfd = objfile->obfd;
10b3939b 4286 struct dwarf2_cu *cu;
c764a876 4287 unsigned int offset;
93311388 4288 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4289 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4290 struct attribute *attr;
98bfdba5 4291 int read_cu = 0;
6502dd73 4292
348e048f
DE
4293 gdb_assert (! per_cu->from_debug_types);
4294
c906108c 4295 /* Set local variables from the partial symbol table info. */
10b3939b 4296 offset = per_cu->offset;
6502dd73 4297
be391dca 4298 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4299 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4300 beg_of_comp_unit = info_ptr;
63d06c5c 4301
98bfdba5
PA
4302 if (per_cu->cu == NULL)
4303 {
4304 cu = alloc_one_comp_unit (objfile);
4305
4306 read_cu = 1;
c906108c 4307
98bfdba5
PA
4308 /* If an error occurs while loading, release our storage. */
4309 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4310
98bfdba5
PA
4311 /* Read in the comp_unit header. */
4312 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4313
98bfdba5
PA
4314 /* Complete the cu_header. */
4315 cu->header.offset = offset;
4316 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4317
98bfdba5
PA
4318 /* Read the abbrevs for this compilation unit. */
4319 dwarf2_read_abbrevs (abfd, cu);
4320 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4321
98bfdba5
PA
4322 /* Link this compilation unit into the compilation unit tree. */
4323 per_cu->cu = cu;
4324 cu->per_cu = per_cu;
98bfdba5
PA
4325
4326 /* Link this CU into read_in_chain. */
4327 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4328 dwarf2_per_objfile->read_in_chain = per_cu;
4329 }
4330 else
4331 {
4332 cu = per_cu->cu;
4333 info_ptr += cu->header.first_die_offset;
4334 }
e142c38c 4335
93311388 4336 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4337
4338 /* We try not to read any attributes in this function, because not
4339 all objfiles needed for references have been loaded yet, and symbol
4340 table processing isn't initialized. But we have to set the CU language,
4341 or we won't be able to build types correctly. */
4342 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4343 if (attr)
4344 set_cu_language (DW_UNSND (attr), cu);
4345 else
4346 set_cu_language (language_minimal, cu);
4347
a6c727b2
DJ
4348 /* Similarly, if we do not read the producer, we can not apply
4349 producer-specific interpretation. */
4350 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4351 if (attr)
4352 cu->producer = DW_STRING (attr);
4353
98bfdba5
PA
4354 if (read_cu)
4355 {
4356 do_cleanups (free_abbrevs_cleanup);
e142c38c 4357
98bfdba5
PA
4358 /* We've successfully allocated this compilation unit. Let our
4359 caller clean it up when finished with it. */
4360 discard_cleanups (free_cu_cleanup);
4361 }
10b3939b
DJ
4362}
4363
3da10d80
KS
4364/* Add a DIE to the delayed physname list. */
4365
4366static void
4367add_to_method_list (struct type *type, int fnfield_index, int index,
4368 const char *name, struct die_info *die,
4369 struct dwarf2_cu *cu)
4370{
4371 struct delayed_method_info mi;
4372 mi.type = type;
4373 mi.fnfield_index = fnfield_index;
4374 mi.index = index;
4375 mi.name = name;
4376 mi.die = die;
4377 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4378}
4379
4380/* A cleanup for freeing the delayed method list. */
4381
4382static void
4383free_delayed_list (void *ptr)
4384{
4385 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4386 if (cu->method_list != NULL)
4387 {
4388 VEC_free (delayed_method_info, cu->method_list);
4389 cu->method_list = NULL;
4390 }
4391}
4392
4393/* Compute the physnames of any methods on the CU's method list.
4394
4395 The computation of method physnames is delayed in order to avoid the
4396 (bad) condition that one of the method's formal parameters is of an as yet
4397 incomplete type. */
4398
4399static void
4400compute_delayed_physnames (struct dwarf2_cu *cu)
4401{
4402 int i;
4403 struct delayed_method_info *mi;
4404 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4405 {
4406 char *physname;
4407 struct fn_fieldlist *fn_flp
4408 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4409 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4410 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4411 }
4412}
4413
10b3939b
DJ
4414/* Generate full symbol information for PST and CU, whose DIEs have
4415 already been loaded into memory. */
4416
4417static void
4418process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4419{
10b3939b 4420 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4421 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4422 CORE_ADDR lowpc, highpc;
4423 struct symtab *symtab;
3da10d80 4424 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4425 CORE_ADDR baseaddr;
4426
4427 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4428
10b3939b
DJ
4429 buildsym_init ();
4430 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4431 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4432
4433 cu->list_in_scope = &file_symbols;
c906108c 4434
d85a05f0 4435 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4436
c906108c 4437 /* Do line number decoding in read_file_scope () */
10b3939b 4438 process_die (cu->dies, cu);
c906108c 4439
3da10d80
KS
4440 /* Now that we have processed all the DIEs in the CU, all the types
4441 should be complete, and it should now be safe to compute all of the
4442 physnames. */
4443 compute_delayed_physnames (cu);
4444 do_cleanups (delayed_list_cleanup);
4445
fae299cd
DC
4446 /* Some compilers don't define a DW_AT_high_pc attribute for the
4447 compilation unit. If the DW_AT_high_pc is missing, synthesize
4448 it, by scanning the DIE's below the compilation unit. */
10b3939b 4449 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4450
613e1657 4451 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4452
4453 /* Set symtab language to language from DW_AT_language.
4454 If the compilation is from a C file generated by language preprocessors,
4455 do not set the language if it was already deduced by start_subfile. */
4456 if (symtab != NULL
10b3939b 4457 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4458 {
10b3939b 4459 symtab->language = cu->language;
c906108c 4460 }
9291a0cd
TT
4461
4462 if (dwarf2_per_objfile->using_index)
4463 per_cu->v.quick->symtab = symtab;
4464 else
4465 {
4466 struct partial_symtab *pst = per_cu->v.psymtab;
4467 pst->symtab = symtab;
4468 pst->readin = 1;
4469 }
c906108c
SS
4470
4471 do_cleanups (back_to);
4472}
4473
4474/* Process a die and its children. */
4475
4476static void
e7c27a73 4477process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4478{
4479 switch (die->tag)
4480 {
4481 case DW_TAG_padding:
4482 break;
4483 case DW_TAG_compile_unit:
e7c27a73 4484 read_file_scope (die, cu);
c906108c 4485 break;
348e048f
DE
4486 case DW_TAG_type_unit:
4487 read_type_unit_scope (die, cu);
4488 break;
c906108c 4489 case DW_TAG_subprogram:
c906108c 4490 case DW_TAG_inlined_subroutine:
edb3359d 4491 read_func_scope (die, cu);
c906108c
SS
4492 break;
4493 case DW_TAG_lexical_block:
14898363
L
4494 case DW_TAG_try_block:
4495 case DW_TAG_catch_block:
e7c27a73 4496 read_lexical_block_scope (die, cu);
c906108c
SS
4497 break;
4498 case DW_TAG_class_type:
680b30c7 4499 case DW_TAG_interface_type:
c906108c
SS
4500 case DW_TAG_structure_type:
4501 case DW_TAG_union_type:
134d01f1 4502 process_structure_scope (die, cu);
c906108c
SS
4503 break;
4504 case DW_TAG_enumeration_type:
134d01f1 4505 process_enumeration_scope (die, cu);
c906108c 4506 break;
134d01f1 4507
f792889a
DJ
4508 /* These dies have a type, but processing them does not create
4509 a symbol or recurse to process the children. Therefore we can
4510 read them on-demand through read_type_die. */
c906108c 4511 case DW_TAG_subroutine_type:
72019c9c 4512 case DW_TAG_set_type:
c906108c 4513 case DW_TAG_array_type:
c906108c 4514 case DW_TAG_pointer_type:
c906108c 4515 case DW_TAG_ptr_to_member_type:
c906108c 4516 case DW_TAG_reference_type:
c906108c 4517 case DW_TAG_string_type:
c906108c 4518 break;
134d01f1 4519
c906108c 4520 case DW_TAG_base_type:
a02abb62 4521 case DW_TAG_subrange_type:
cb249c71 4522 case DW_TAG_typedef:
134d01f1
DJ
4523 /* Add a typedef symbol for the type definition, if it has a
4524 DW_AT_name. */
f792889a 4525 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4526 break;
c906108c 4527 case DW_TAG_common_block:
e7c27a73 4528 read_common_block (die, cu);
c906108c
SS
4529 break;
4530 case DW_TAG_common_inclusion:
4531 break;
d9fa45fe 4532 case DW_TAG_namespace:
63d06c5c 4533 processing_has_namespace_info = 1;
e7c27a73 4534 read_namespace (die, cu);
d9fa45fe 4535 break;
5d7cb8df 4536 case DW_TAG_module:
f55ee35c 4537 processing_has_namespace_info = 1;
5d7cb8df
JK
4538 read_module (die, cu);
4539 break;
d9fa45fe
DC
4540 case DW_TAG_imported_declaration:
4541 case DW_TAG_imported_module:
63d06c5c 4542 processing_has_namespace_info = 1;
27aa8d6a
SW
4543 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4544 || cu->language != language_fortran))
4545 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4546 dwarf_tag_name (die->tag));
4547 read_import_statement (die, cu);
d9fa45fe 4548 break;
c906108c 4549 default:
e7c27a73 4550 new_symbol (die, NULL, cu);
c906108c
SS
4551 break;
4552 }
4553}
4554
94af9270
KS
4555/* A helper function for dwarf2_compute_name which determines whether DIE
4556 needs to have the name of the scope prepended to the name listed in the
4557 die. */
4558
4559static int
4560die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4561{
1c809c68
TT
4562 struct attribute *attr;
4563
94af9270
KS
4564 switch (die->tag)
4565 {
4566 case DW_TAG_namespace:
4567 case DW_TAG_typedef:
4568 case DW_TAG_class_type:
4569 case DW_TAG_interface_type:
4570 case DW_TAG_structure_type:
4571 case DW_TAG_union_type:
4572 case DW_TAG_enumeration_type:
4573 case DW_TAG_enumerator:
4574 case DW_TAG_subprogram:
4575 case DW_TAG_member:
4576 return 1;
4577
4578 case DW_TAG_variable:
c2b0a229 4579 case DW_TAG_constant:
94af9270
KS
4580 /* We only need to prefix "globally" visible variables. These include
4581 any variable marked with DW_AT_external or any variable that
4582 lives in a namespace. [Variables in anonymous namespaces
4583 require prefixing, but they are not DW_AT_external.] */
4584
4585 if (dwarf2_attr (die, DW_AT_specification, cu))
4586 {
4587 struct dwarf2_cu *spec_cu = cu;
9a619af0 4588
94af9270
KS
4589 return die_needs_namespace (die_specification (die, &spec_cu),
4590 spec_cu);
4591 }
4592
1c809c68 4593 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4594 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4595 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4596 return 0;
4597 /* A variable in a lexical block of some kind does not need a
4598 namespace, even though in C++ such variables may be external
4599 and have a mangled name. */
4600 if (die->parent->tag == DW_TAG_lexical_block
4601 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4602 || die->parent->tag == DW_TAG_catch_block
4603 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4604 return 0;
4605 return 1;
94af9270
KS
4606
4607 default:
4608 return 0;
4609 }
4610}
4611
98bfdba5
PA
4612/* Retrieve the last character from a mem_file. */
4613
4614static void
4615do_ui_file_peek_last (void *object, const char *buffer, long length)
4616{
4617 char *last_char_p = (char *) object;
4618
4619 if (length > 0)
4620 *last_char_p = buffer[length - 1];
4621}
4622
94af9270
KS
4623/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4624 compute the physname for the object, which include a method's
4625 formal parameters (C++/Java) and return type (Java).
4626
af6b7be1
JB
4627 For Ada, return the DIE's linkage name rather than the fully qualified
4628 name. PHYSNAME is ignored..
4629
94af9270
KS
4630 The result is allocated on the objfile_obstack and canonicalized. */
4631
4632static const char *
4633dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4634 int physname)
4635{
4636 if (name == NULL)
4637 name = dwarf2_name (die, cu);
4638
f55ee35c
JK
4639 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4640 compute it by typename_concat inside GDB. */
4641 if (cu->language == language_ada
4642 || (cu->language == language_fortran && physname))
4643 {
4644 /* For Ada unit, we prefer the linkage name over the name, as
4645 the former contains the exported name, which the user expects
4646 to be able to reference. Ideally, we want the user to be able
4647 to reference this entity using either natural or linkage name,
4648 but we haven't started looking at this enhancement yet. */
4649 struct attribute *attr;
4650
4651 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4652 if (attr == NULL)
4653 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4654 if (attr && DW_STRING (attr))
4655 return DW_STRING (attr);
4656 }
4657
94af9270
KS
4658 /* These are the only languages we know how to qualify names in. */
4659 if (name != NULL
f55ee35c
JK
4660 && (cu->language == language_cplus || cu->language == language_java
4661 || cu->language == language_fortran))
94af9270
KS
4662 {
4663 if (die_needs_namespace (die, cu))
4664 {
4665 long length;
4666 char *prefix;
4667 struct ui_file *buf;
4668
4669 prefix = determine_prefix (die, cu);
4670 buf = mem_fileopen ();
4671 if (*prefix != '\0')
4672 {
f55ee35c
JK
4673 char *prefixed_name = typename_concat (NULL, prefix, name,
4674 physname, cu);
9a619af0 4675
94af9270
KS
4676 fputs_unfiltered (prefixed_name, buf);
4677 xfree (prefixed_name);
4678 }
4679 else
4680 fputs_unfiltered (name ? name : "", buf);
4681
98bfdba5
PA
4682 /* Template parameters may be specified in the DIE's DW_AT_name, or
4683 as children with DW_TAG_template_type_param or
4684 DW_TAG_value_type_param. If the latter, add them to the name
4685 here. If the name already has template parameters, then
4686 skip this step; some versions of GCC emit both, and
4687 it is more efficient to use the pre-computed name.
4688
4689 Something to keep in mind about this process: it is very
4690 unlikely, or in some cases downright impossible, to produce
4691 something that will match the mangled name of a function.
4692 If the definition of the function has the same debug info,
4693 we should be able to match up with it anyway. But fallbacks
4694 using the minimal symbol, for instance to find a method
4695 implemented in a stripped copy of libstdc++, will not work.
4696 If we do not have debug info for the definition, we will have to
4697 match them up some other way.
4698
4699 When we do name matching there is a related problem with function
4700 templates; two instantiated function templates are allowed to
4701 differ only by their return types, which we do not add here. */
4702
4703 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4704 {
4705 struct attribute *attr;
4706 struct die_info *child;
4707 int first = 1;
4708
4709 die->building_fullname = 1;
4710
4711 for (child = die->child; child != NULL; child = child->sibling)
4712 {
4713 struct type *type;
4714 long value;
4715 gdb_byte *bytes;
4716 struct dwarf2_locexpr_baton *baton;
4717 struct value *v;
4718
4719 if (child->tag != DW_TAG_template_type_param
4720 && child->tag != DW_TAG_template_value_param)
4721 continue;
4722
4723 if (first)
4724 {
4725 fputs_unfiltered ("<", buf);
4726 first = 0;
4727 }
4728 else
4729 fputs_unfiltered (", ", buf);
4730
4731 attr = dwarf2_attr (child, DW_AT_type, cu);
4732 if (attr == NULL)
4733 {
4734 complaint (&symfile_complaints,
4735 _("template parameter missing DW_AT_type"));
4736 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4737 continue;
4738 }
4739 type = die_type (child, cu);
4740
4741 if (child->tag == DW_TAG_template_type_param)
4742 {
4743 c_print_type (type, "", buf, -1, 0);
4744 continue;
4745 }
4746
4747 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4748 if (attr == NULL)
4749 {
4750 complaint (&symfile_complaints,
4751 _("template parameter missing DW_AT_const_value"));
4752 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4753 continue;
4754 }
4755
4756 dwarf2_const_value_attr (attr, type, name,
4757 &cu->comp_unit_obstack, cu,
4758 &value, &bytes, &baton);
4759
4760 if (TYPE_NOSIGN (type))
4761 /* GDB prints characters as NUMBER 'CHAR'. If that's
4762 changed, this can use value_print instead. */
4763 c_printchar (value, type, buf);
4764 else
4765 {
4766 struct value_print_options opts;
4767
4768 if (baton != NULL)
4769 v = dwarf2_evaluate_loc_desc (type, NULL,
4770 baton->data,
4771 baton->size,
4772 baton->per_cu);
4773 else if (bytes != NULL)
4774 {
4775 v = allocate_value (type);
4776 memcpy (value_contents_writeable (v), bytes,
4777 TYPE_LENGTH (type));
4778 }
4779 else
4780 v = value_from_longest (type, value);
4781
4782 /* Specify decimal so that we do not depend on the radix. */
4783 get_formatted_print_options (&opts, 'd');
4784 opts.raw = 1;
4785 value_print (v, buf, &opts);
4786 release_value (v);
4787 value_free (v);
4788 }
4789 }
4790
4791 die->building_fullname = 0;
4792
4793 if (!first)
4794 {
4795 /* Close the argument list, with a space if necessary
4796 (nested templates). */
4797 char last_char = '\0';
4798 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4799 if (last_char == '>')
4800 fputs_unfiltered (" >", buf);
4801 else
4802 fputs_unfiltered (">", buf);
4803 }
4804 }
4805
94af9270
KS
4806 /* For Java and C++ methods, append formal parameter type
4807 information, if PHYSNAME. */
6e70227d 4808
94af9270
KS
4809 if (physname && die->tag == DW_TAG_subprogram
4810 && (cu->language == language_cplus
4811 || cu->language == language_java))
4812 {
4813 struct type *type = read_type_die (die, cu);
4814
4815 c_type_print_args (type, buf, 0, cu->language);
4816
4817 if (cu->language == language_java)
4818 {
4819 /* For java, we must append the return type to method
4820 names. */
4821 if (die->tag == DW_TAG_subprogram)
4822 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4823 0, 0);
4824 }
4825 else if (cu->language == language_cplus)
4826 {
60430eff
DJ
4827 /* Assume that an artificial first parameter is
4828 "this", but do not crash if it is not. RealView
4829 marks unnamed (and thus unused) parameters as
4830 artificial; there is no way to differentiate
4831 the two cases. */
94af9270
KS
4832 if (TYPE_NFIELDS (type) > 0
4833 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4834 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4835 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4836 fputs_unfiltered (" const", buf);
4837 }
4838 }
4839
4840 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4841 &length);
4842 ui_file_delete (buf);
4843
4844 if (cu->language == language_cplus)
4845 {
4846 char *cname
4847 = dwarf2_canonicalize_name (name, cu,
4848 &cu->objfile->objfile_obstack);
9a619af0 4849
94af9270
KS
4850 if (cname != NULL)
4851 name = cname;
4852 }
4853 }
4854 }
4855
4856 return name;
4857}
4858
0114d602
DJ
4859/* Return the fully qualified name of DIE, based on its DW_AT_name.
4860 If scope qualifiers are appropriate they will be added. The result
4861 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4862 not have a name. NAME may either be from a previous call to
4863 dwarf2_name or NULL.
4864
4865 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4866
4867static const char *
94af9270 4868dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4869{
94af9270
KS
4870 return dwarf2_compute_name (name, die, cu, 0);
4871}
0114d602 4872
94af9270
KS
4873/* Construct a physname for the given DIE in CU. NAME may either be
4874 from a previous call to dwarf2_name or NULL. The result will be
4875 allocated on the objfile_objstack or NULL if the DIE does not have a
4876 name.
0114d602 4877
94af9270 4878 The output string will be canonicalized (if C++/Java). */
0114d602 4879
94af9270
KS
4880static const char *
4881dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4882{
4883 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4884}
4885
27aa8d6a
SW
4886/* Read the import statement specified by the given die and record it. */
4887
4888static void
4889read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4890{
4891 struct attribute *import_attr;
4892 struct die_info *imported_die;
de4affc9 4893 struct dwarf2_cu *imported_cu;
27aa8d6a 4894 const char *imported_name;
794684b6 4895 const char *imported_name_prefix;
13387711
SW
4896 const char *canonical_name;
4897 const char *import_alias;
4898 const char *imported_declaration = NULL;
794684b6 4899 const char *import_prefix;
13387711
SW
4900
4901 char *temp;
27aa8d6a
SW
4902
4903 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4904 if (import_attr == NULL)
4905 {
4906 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4907 dwarf_tag_name (die->tag));
4908 return;
4909 }
4910
de4affc9
CC
4911 imported_cu = cu;
4912 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4913 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4914 if (imported_name == NULL)
4915 {
4916 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4917
4918 The import in the following code:
4919 namespace A
4920 {
4921 typedef int B;
4922 }
4923
4924 int main ()
4925 {
4926 using A::B;
4927 B b;
4928 return b;
4929 }
4930
4931 ...
4932 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4933 <52> DW_AT_decl_file : 1
4934 <53> DW_AT_decl_line : 6
4935 <54> DW_AT_import : <0x75>
4936 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4937 <59> DW_AT_name : B
4938 <5b> DW_AT_decl_file : 1
4939 <5c> DW_AT_decl_line : 2
4940 <5d> DW_AT_type : <0x6e>
4941 ...
4942 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4943 <76> DW_AT_byte_size : 4
4944 <77> DW_AT_encoding : 5 (signed)
4945
4946 imports the wrong die ( 0x75 instead of 0x58 ).
4947 This case will be ignored until the gcc bug is fixed. */
4948 return;
4949 }
4950
82856980
SW
4951 /* Figure out the local name after import. */
4952 import_alias = dwarf2_name (die, cu);
27aa8d6a 4953
794684b6
SW
4954 /* Figure out where the statement is being imported to. */
4955 import_prefix = determine_prefix (die, cu);
4956
4957 /* Figure out what the scope of the imported die is and prepend it
4958 to the name of the imported die. */
de4affc9 4959 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4960
f55ee35c
JK
4961 if (imported_die->tag != DW_TAG_namespace
4962 && imported_die->tag != DW_TAG_module)
794684b6 4963 {
13387711
SW
4964 imported_declaration = imported_name;
4965 canonical_name = imported_name_prefix;
794684b6 4966 }
13387711 4967 else if (strlen (imported_name_prefix) > 0)
794684b6 4968 {
13387711
SW
4969 temp = alloca (strlen (imported_name_prefix)
4970 + 2 + strlen (imported_name) + 1);
4971 strcpy (temp, imported_name_prefix);
4972 strcat (temp, "::");
4973 strcat (temp, imported_name);
4974 canonical_name = temp;
794684b6 4975 }
13387711
SW
4976 else
4977 canonical_name = imported_name;
794684b6 4978
c0cc3a76
SW
4979 cp_add_using_directive (import_prefix,
4980 canonical_name,
4981 import_alias,
13387711 4982 imported_declaration,
c0cc3a76 4983 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4984}
4985
5fb290d7 4986static void
e142c38c 4987initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4988{
e142c38c 4989 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4990}
4991
cb1df416
DJ
4992static void
4993free_cu_line_header (void *arg)
4994{
4995 struct dwarf2_cu *cu = arg;
4996
4997 free_line_header (cu->line_header);
4998 cu->line_header = NULL;
4999}
5000
9291a0cd
TT
5001static void
5002find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5003 char **name, char **comp_dir)
5004{
5005 struct attribute *attr;
5006
5007 *name = NULL;
5008 *comp_dir = NULL;
5009
5010 /* Find the filename. Do not use dwarf2_name here, since the filename
5011 is not a source language identifier. */
5012 attr = dwarf2_attr (die, DW_AT_name, cu);
5013 if (attr)
5014 {
5015 *name = DW_STRING (attr);
5016 }
5017
5018 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5019 if (attr)
5020 *comp_dir = DW_STRING (attr);
5021 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5022 {
5023 *comp_dir = ldirname (*name);
5024 if (*comp_dir != NULL)
5025 make_cleanup (xfree, *comp_dir);
5026 }
5027 if (*comp_dir != NULL)
5028 {
5029 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5030 directory, get rid of it. */
5031 char *cp = strchr (*comp_dir, ':');
5032
5033 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5034 *comp_dir = cp + 1;
5035 }
5036
5037 if (*name == NULL)
5038 *name = "<unknown>";
5039}
5040
c906108c 5041static void
e7c27a73 5042read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5043{
e7c27a73 5044 struct objfile *objfile = cu->objfile;
debd256d 5045 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5046 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5047 CORE_ADDR highpc = ((CORE_ADDR) 0);
5048 struct attribute *attr;
e1024ff1 5049 char *name = NULL;
c906108c
SS
5050 char *comp_dir = NULL;
5051 struct die_info *child_die;
5052 bfd *abfd = objfile->obfd;
debd256d 5053 struct line_header *line_header = 0;
e142c38c 5054 CORE_ADDR baseaddr;
6e70227d 5055
e142c38c 5056 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5057
fae299cd 5058 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5059
5060 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5061 from finish_block. */
2acceee2 5062 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5063 lowpc = highpc;
5064 lowpc += baseaddr;
5065 highpc += baseaddr;
5066
9291a0cd 5067 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5068
e142c38c 5069 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5070 if (attr)
5071 {
e142c38c 5072 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5073 }
5074
b0f35d58 5075 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5076 if (attr)
b0f35d58 5077 cu->producer = DW_STRING (attr);
303b6f5d 5078
c906108c
SS
5079 /* We assume that we're processing GCC output. */
5080 processing_gcc_compilation = 2;
c906108c 5081
df8a16a1
DJ
5082 processing_has_namespace_info = 0;
5083
c906108c
SS
5084 start_symtab (name, comp_dir, lowpc);
5085 record_debugformat ("DWARF 2");
303b6f5d 5086 record_producer (cu->producer);
c906108c 5087
e142c38c 5088 initialize_cu_func_list (cu);
c906108c 5089
cb1df416
DJ
5090 /* Decode line number information if present. We do this before
5091 processing child DIEs, so that the line header table is available
5092 for DW_AT_decl_file. */
e142c38c 5093 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5094 if (attr)
5095 {
debd256d 5096 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5097 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5098 if (line_header)
5099 {
cb1df416
DJ
5100 cu->line_header = line_header;
5101 make_cleanup (free_cu_line_header, cu);
aaa75496 5102 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5103 }
5fb290d7 5104 }
debd256d 5105
cb1df416
DJ
5106 /* Process all dies in compilation unit. */
5107 if (die->child != NULL)
5108 {
5109 child_die = die->child;
5110 while (child_die && child_die->tag)
5111 {
5112 process_die (child_die, cu);
5113 child_die = sibling_die (child_die);
5114 }
5115 }
5116
2e276125
JB
5117 /* Decode macro information, if present. Dwarf 2 macro information
5118 refers to information in the line number info statement program
5119 header, so we can only read it if we've read the header
5120 successfully. */
e142c38c 5121 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5122 if (attr && line_header)
2e276125
JB
5123 {
5124 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5125
2e276125 5126 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5127 comp_dir, abfd, cu);
2e276125 5128 }
debd256d 5129 do_cleanups (back_to);
5fb290d7
DJ
5130}
5131
348e048f
DE
5132/* For TUs we want to skip the first top level sibling if it's not the
5133 actual type being defined by this TU. In this case the first top
5134 level sibling is there to provide context only. */
5135
5136static void
5137read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5138{
5139 struct objfile *objfile = cu->objfile;
5140 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5141 CORE_ADDR lowpc;
5142 struct attribute *attr;
5143 char *name = NULL;
5144 char *comp_dir = NULL;
5145 struct die_info *child_die;
5146 bfd *abfd = objfile->obfd;
348e048f
DE
5147
5148 /* start_symtab needs a low pc, but we don't really have one.
5149 Do what read_file_scope would do in the absence of such info. */
5150 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5151
5152 /* Find the filename. Do not use dwarf2_name here, since the filename
5153 is not a source language identifier. */
5154 attr = dwarf2_attr (die, DW_AT_name, cu);
5155 if (attr)
5156 name = DW_STRING (attr);
5157
5158 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5159 if (attr)
5160 comp_dir = DW_STRING (attr);
5161 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5162 {
5163 comp_dir = ldirname (name);
5164 if (comp_dir != NULL)
5165 make_cleanup (xfree, comp_dir);
5166 }
5167
5168 if (name == NULL)
5169 name = "<unknown>";
5170
5171 attr = dwarf2_attr (die, DW_AT_language, cu);
5172 if (attr)
5173 set_cu_language (DW_UNSND (attr), cu);
5174
5175 /* This isn't technically needed today. It is done for symmetry
5176 with read_file_scope. */
5177 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5178 if (attr)
348e048f
DE
5179 cu->producer = DW_STRING (attr);
5180
5181 /* We assume that we're processing GCC output. */
5182 processing_gcc_compilation = 2;
5183
5184 processing_has_namespace_info = 0;
5185
5186 start_symtab (name, comp_dir, lowpc);
5187 record_debugformat ("DWARF 2");
5188 record_producer (cu->producer);
5189
5190 /* Process the dies in the type unit. */
5191 if (die->child == NULL)
5192 {
5193 dump_die_for_error (die);
5194 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5195 bfd_get_filename (abfd));
5196 }
5197
5198 child_die = die->child;
5199
5200 while (child_die && child_die->tag)
5201 {
5202 process_die (child_die, cu);
5203
5204 child_die = sibling_die (child_die);
5205 }
5206
5207 do_cleanups (back_to);
5208}
5209
5fb290d7 5210static void
e142c38c
DJ
5211add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5212 struct dwarf2_cu *cu)
5fb290d7
DJ
5213{
5214 struct function_range *thisfn;
5215
5216 thisfn = (struct function_range *)
7b5a2f43 5217 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5218 thisfn->name = name;
5219 thisfn->lowpc = lowpc;
5220 thisfn->highpc = highpc;
5221 thisfn->seen_line = 0;
5222 thisfn->next = NULL;
5223
e142c38c
DJ
5224 if (cu->last_fn == NULL)
5225 cu->first_fn = thisfn;
5fb290d7 5226 else
e142c38c 5227 cu->last_fn->next = thisfn;
5fb290d7 5228
e142c38c 5229 cu->last_fn = thisfn;
c906108c
SS
5230}
5231
d389af10
JK
5232/* qsort helper for inherit_abstract_dies. */
5233
5234static int
5235unsigned_int_compar (const void *ap, const void *bp)
5236{
5237 unsigned int a = *(unsigned int *) ap;
5238 unsigned int b = *(unsigned int *) bp;
5239
5240 return (a > b) - (b > a);
5241}
5242
5243/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5244 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5245 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5246
5247static void
5248inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5249{
5250 struct die_info *child_die;
5251 unsigned die_children_count;
5252 /* CU offsets which were referenced by children of the current DIE. */
5253 unsigned *offsets;
5254 unsigned *offsets_end, *offsetp;
5255 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5256 struct die_info *origin_die;
5257 /* Iterator of the ORIGIN_DIE children. */
5258 struct die_info *origin_child_die;
5259 struct cleanup *cleanups;
5260 struct attribute *attr;
cd02d79d
PA
5261 struct dwarf2_cu *origin_cu;
5262 struct pending **origin_previous_list_in_scope;
d389af10
JK
5263
5264 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5265 if (!attr)
5266 return;
5267
cd02d79d
PA
5268 /* Note that following die references may follow to a die in a
5269 different cu. */
5270
5271 origin_cu = cu;
5272 origin_die = follow_die_ref (die, attr, &origin_cu);
5273
5274 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5275 symbols in. */
5276 origin_previous_list_in_scope = origin_cu->list_in_scope;
5277 origin_cu->list_in_scope = cu->list_in_scope;
5278
edb3359d
DJ
5279 if (die->tag != origin_die->tag
5280 && !(die->tag == DW_TAG_inlined_subroutine
5281 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5282 complaint (&symfile_complaints,
5283 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5284 die->offset, origin_die->offset);
5285
5286 child_die = die->child;
5287 die_children_count = 0;
5288 while (child_die && child_die->tag)
5289 {
5290 child_die = sibling_die (child_die);
5291 die_children_count++;
5292 }
5293 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5294 cleanups = make_cleanup (xfree, offsets);
5295
5296 offsets_end = offsets;
5297 child_die = die->child;
5298 while (child_die && child_die->tag)
5299 {
c38f313d
DJ
5300 /* For each CHILD_DIE, find the corresponding child of
5301 ORIGIN_DIE. If there is more than one layer of
5302 DW_AT_abstract_origin, follow them all; there shouldn't be,
5303 but GCC versions at least through 4.4 generate this (GCC PR
5304 40573). */
5305 struct die_info *child_origin_die = child_die;
cd02d79d 5306 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5307
c38f313d
DJ
5308 while (1)
5309 {
cd02d79d
PA
5310 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5311 child_origin_cu);
c38f313d
DJ
5312 if (attr == NULL)
5313 break;
cd02d79d
PA
5314 child_origin_die = follow_die_ref (child_origin_die, attr,
5315 &child_origin_cu);
c38f313d
DJ
5316 }
5317
d389af10
JK
5318 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5319 counterpart may exist. */
c38f313d 5320 if (child_origin_die != child_die)
d389af10 5321 {
edb3359d
DJ
5322 if (child_die->tag != child_origin_die->tag
5323 && !(child_die->tag == DW_TAG_inlined_subroutine
5324 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5325 complaint (&symfile_complaints,
5326 _("Child DIE 0x%x and its abstract origin 0x%x have "
5327 "different tags"), child_die->offset,
5328 child_origin_die->offset);
c38f313d
DJ
5329 if (child_origin_die->parent != origin_die)
5330 complaint (&symfile_complaints,
5331 _("Child DIE 0x%x and its abstract origin 0x%x have "
5332 "different parents"), child_die->offset,
5333 child_origin_die->offset);
5334 else
5335 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5336 }
5337 child_die = sibling_die (child_die);
5338 }
5339 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5340 unsigned_int_compar);
5341 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5342 if (offsetp[-1] == *offsetp)
5343 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5344 "to DIE 0x%x as their abstract origin"),
5345 die->offset, *offsetp);
5346
5347 offsetp = offsets;
5348 origin_child_die = origin_die->child;
5349 while (origin_child_die && origin_child_die->tag)
5350 {
5351 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5352 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5353 offsetp++;
5354 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5355 {
5356 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5357 process_die (origin_child_die, origin_cu);
d389af10
JK
5358 }
5359 origin_child_die = sibling_die (origin_child_die);
5360 }
cd02d79d 5361 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5362
5363 do_cleanups (cleanups);
5364}
5365
c906108c 5366static void
e7c27a73 5367read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5368{
e7c27a73 5369 struct objfile *objfile = cu->objfile;
52f0bd74 5370 struct context_stack *new;
c906108c
SS
5371 CORE_ADDR lowpc;
5372 CORE_ADDR highpc;
5373 struct die_info *child_die;
edb3359d 5374 struct attribute *attr, *call_line, *call_file;
c906108c 5375 char *name;
e142c38c 5376 CORE_ADDR baseaddr;
801e3a5b 5377 struct block *block;
edb3359d 5378 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5379 VEC (symbolp) *template_args = NULL;
5380 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5381
5382 if (inlined_func)
5383 {
5384 /* If we do not have call site information, we can't show the
5385 caller of this inlined function. That's too confusing, so
5386 only use the scope for local variables. */
5387 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5388 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5389 if (call_line == NULL || call_file == NULL)
5390 {
5391 read_lexical_block_scope (die, cu);
5392 return;
5393 }
5394 }
c906108c 5395
e142c38c
DJ
5396 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5397
94af9270 5398 name = dwarf2_name (die, cu);
c906108c 5399
e8d05480
JB
5400 /* Ignore functions with missing or empty names. These are actually
5401 illegal according to the DWARF standard. */
5402 if (name == NULL)
5403 {
5404 complaint (&symfile_complaints,
5405 _("missing name for subprogram DIE at %d"), die->offset);
5406 return;
5407 }
5408
5409 /* Ignore functions with missing or invalid low and high pc attributes. */
5410 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5411 {
ae4d0c03
PM
5412 attr = dwarf2_attr (die, DW_AT_external, cu);
5413 if (!attr || !DW_UNSND (attr))
5414 complaint (&symfile_complaints,
5415 _("cannot get low and high bounds for subprogram DIE at %d"),
5416 die->offset);
e8d05480
JB
5417 return;
5418 }
c906108c
SS
5419
5420 lowpc += baseaddr;
5421 highpc += baseaddr;
5422
5fb290d7 5423 /* Record the function range for dwarf_decode_lines. */
e142c38c 5424 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5425
34eaf542
TT
5426 /* If we have any template arguments, then we must allocate a
5427 different sort of symbol. */
5428 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5429 {
5430 if (child_die->tag == DW_TAG_template_type_param
5431 || child_die->tag == DW_TAG_template_value_param)
5432 {
5433 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5434 struct template_symbol);
5435 templ_func->base.is_cplus_template_function = 1;
5436 break;
5437 }
5438 }
5439
c906108c 5440 new = push_context (0, lowpc);
34eaf542
TT
5441 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5442 (struct symbol *) templ_func);
4c2df51b 5443
4cecd739
DJ
5444 /* If there is a location expression for DW_AT_frame_base, record
5445 it. */
e142c38c 5446 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5447 if (attr)
c034e007
AC
5448 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5449 expression is being recorded directly in the function's symbol
5450 and not in a separate frame-base object. I guess this hack is
5451 to avoid adding some sort of frame-base adjunct/annex to the
5452 function's symbol :-(. The problem with doing this is that it
5453 results in a function symbol with a location expression that
5454 has nothing to do with the location of the function, ouch! The
5455 relationship should be: a function's symbol has-a frame base; a
5456 frame-base has-a location expression. */
e7c27a73 5457 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5458
e142c38c 5459 cu->list_in_scope = &local_symbols;
c906108c 5460
639d11d3 5461 if (die->child != NULL)
c906108c 5462 {
639d11d3 5463 child_die = die->child;
c906108c
SS
5464 while (child_die && child_die->tag)
5465 {
34eaf542
TT
5466 if (child_die->tag == DW_TAG_template_type_param
5467 || child_die->tag == DW_TAG_template_value_param)
5468 {
5469 struct symbol *arg = new_symbol (child_die, NULL, cu);
5470
f1078f66
DJ
5471 if (arg != NULL)
5472 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5473 }
5474 else
5475 process_die (child_die, cu);
c906108c
SS
5476 child_die = sibling_die (child_die);
5477 }
5478 }
5479
d389af10
JK
5480 inherit_abstract_dies (die, cu);
5481
4a811a97
UW
5482 /* If we have a DW_AT_specification, we might need to import using
5483 directives from the context of the specification DIE. See the
5484 comment in determine_prefix. */
5485 if (cu->language == language_cplus
5486 && dwarf2_attr (die, DW_AT_specification, cu))
5487 {
5488 struct dwarf2_cu *spec_cu = cu;
5489 struct die_info *spec_die = die_specification (die, &spec_cu);
5490
5491 while (spec_die)
5492 {
5493 child_die = spec_die->child;
5494 while (child_die && child_die->tag)
5495 {
5496 if (child_die->tag == DW_TAG_imported_module)
5497 process_die (child_die, spec_cu);
5498 child_die = sibling_die (child_die);
5499 }
5500
5501 /* In some cases, GCC generates specification DIEs that
5502 themselves contain DW_AT_specification attributes. */
5503 spec_die = die_specification (spec_die, &spec_cu);
5504 }
5505 }
5506
c906108c
SS
5507 new = pop_context ();
5508 /* Make a block for the local symbols within. */
801e3a5b
JB
5509 block = finish_block (new->name, &local_symbols, new->old_blocks,
5510 lowpc, highpc, objfile);
5511
df8a16a1 5512 /* For C++, set the block's scope. */
f55ee35c 5513 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5514 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5515 determine_prefix (die, cu),
df8a16a1
DJ
5516 processing_has_namespace_info);
5517
801e3a5b
JB
5518 /* If we have address ranges, record them. */
5519 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5520
34eaf542
TT
5521 /* Attach template arguments to function. */
5522 if (! VEC_empty (symbolp, template_args))
5523 {
5524 gdb_assert (templ_func != NULL);
5525
5526 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5527 templ_func->template_arguments
5528 = obstack_alloc (&objfile->objfile_obstack,
5529 (templ_func->n_template_arguments
5530 * sizeof (struct symbol *)));
5531 memcpy (templ_func->template_arguments,
5532 VEC_address (symbolp, template_args),
5533 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5534 VEC_free (symbolp, template_args);
5535 }
5536
208d8187
JB
5537 /* In C++, we can have functions nested inside functions (e.g., when
5538 a function declares a class that has methods). This means that
5539 when we finish processing a function scope, we may need to go
5540 back to building a containing block's symbol lists. */
5541 local_symbols = new->locals;
5542 param_symbols = new->params;
27aa8d6a 5543 using_directives = new->using_directives;
208d8187 5544
921e78cf
JB
5545 /* If we've finished processing a top-level function, subsequent
5546 symbols go in the file symbol list. */
5547 if (outermost_context_p ())
e142c38c 5548 cu->list_in_scope = &file_symbols;
c906108c
SS
5549}
5550
5551/* Process all the DIES contained within a lexical block scope. Start
5552 a new scope, process the dies, and then close the scope. */
5553
5554static void
e7c27a73 5555read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5556{
e7c27a73 5557 struct objfile *objfile = cu->objfile;
52f0bd74 5558 struct context_stack *new;
c906108c
SS
5559 CORE_ADDR lowpc, highpc;
5560 struct die_info *child_die;
e142c38c
DJ
5561 CORE_ADDR baseaddr;
5562
5563 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5564
5565 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5566 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5567 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5568 be nasty. Might be easier to properly extend generic blocks to
af34e669 5569 describe ranges. */
d85a05f0 5570 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5571 return;
5572 lowpc += baseaddr;
5573 highpc += baseaddr;
5574
5575 push_context (0, lowpc);
639d11d3 5576 if (die->child != NULL)
c906108c 5577 {
639d11d3 5578 child_die = die->child;
c906108c
SS
5579 while (child_die && child_die->tag)
5580 {
e7c27a73 5581 process_die (child_die, cu);
c906108c
SS
5582 child_die = sibling_die (child_die);
5583 }
5584 }
5585 new = pop_context ();
5586
8540c487 5587 if (local_symbols != NULL || using_directives != NULL)
c906108c 5588 {
801e3a5b
JB
5589 struct block *block
5590 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5591 highpc, objfile);
5592
5593 /* Note that recording ranges after traversing children, as we
5594 do here, means that recording a parent's ranges entails
5595 walking across all its children's ranges as they appear in
5596 the address map, which is quadratic behavior.
5597
5598 It would be nicer to record the parent's ranges before
5599 traversing its children, simply overriding whatever you find
5600 there. But since we don't even decide whether to create a
5601 block until after we've traversed its children, that's hard
5602 to do. */
5603 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5604 }
5605 local_symbols = new->locals;
27aa8d6a 5606 using_directives = new->using_directives;
c906108c
SS
5607}
5608
43039443 5609/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5610 Return 1 if the attributes are present and valid, otherwise, return 0.
5611 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5612
5613static int
5614dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5615 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5616 struct partial_symtab *ranges_pst)
43039443
JK
5617{
5618 struct objfile *objfile = cu->objfile;
5619 struct comp_unit_head *cu_header = &cu->header;
5620 bfd *obfd = objfile->obfd;
5621 unsigned int addr_size = cu_header->addr_size;
5622 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5623 /* Base address selection entry. */
5624 CORE_ADDR base;
5625 int found_base;
5626 unsigned int dummy;
5627 gdb_byte *buffer;
5628 CORE_ADDR marker;
5629 int low_set;
5630 CORE_ADDR low = 0;
5631 CORE_ADDR high = 0;
ff013f42 5632 CORE_ADDR baseaddr;
43039443 5633
d00adf39
DE
5634 found_base = cu->base_known;
5635 base = cu->base_address;
43039443 5636
be391dca 5637 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5638 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5639 {
5640 complaint (&symfile_complaints,
5641 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5642 offset);
5643 return 0;
5644 }
dce234bc 5645 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5646
5647 /* Read in the largest possible address. */
5648 marker = read_address (obfd, buffer, cu, &dummy);
5649 if ((marker & mask) == mask)
5650 {
5651 /* If we found the largest possible address, then
5652 read the base address. */
5653 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5654 buffer += 2 * addr_size;
5655 offset += 2 * addr_size;
5656 found_base = 1;
5657 }
5658
5659 low_set = 0;
5660
e7030f15 5661 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5662
43039443
JK
5663 while (1)
5664 {
5665 CORE_ADDR range_beginning, range_end;
5666
5667 range_beginning = read_address (obfd, buffer, cu, &dummy);
5668 buffer += addr_size;
5669 range_end = read_address (obfd, buffer, cu, &dummy);
5670 buffer += addr_size;
5671 offset += 2 * addr_size;
5672
5673 /* An end of list marker is a pair of zero addresses. */
5674 if (range_beginning == 0 && range_end == 0)
5675 /* Found the end of list entry. */
5676 break;
5677
5678 /* Each base address selection entry is a pair of 2 values.
5679 The first is the largest possible address, the second is
5680 the base address. Check for a base address here. */
5681 if ((range_beginning & mask) == mask)
5682 {
5683 /* If we found the largest possible address, then
5684 read the base address. */
5685 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5686 found_base = 1;
5687 continue;
5688 }
5689
5690 if (!found_base)
5691 {
5692 /* We have no valid base address for the ranges
5693 data. */
5694 complaint (&symfile_complaints,
5695 _("Invalid .debug_ranges data (no base address)"));
5696 return 0;
5697 }
5698
5699 range_beginning += base;
5700 range_end += base;
5701
ff013f42
JK
5702 if (ranges_pst != NULL && range_beginning < range_end)
5703 addrmap_set_empty (objfile->psymtabs_addrmap,
5704 range_beginning + baseaddr, range_end - 1 + baseaddr,
5705 ranges_pst);
5706
43039443
JK
5707 /* FIXME: This is recording everything as a low-high
5708 segment of consecutive addresses. We should have a
5709 data structure for discontiguous block ranges
5710 instead. */
5711 if (! low_set)
5712 {
5713 low = range_beginning;
5714 high = range_end;
5715 low_set = 1;
5716 }
5717 else
5718 {
5719 if (range_beginning < low)
5720 low = range_beginning;
5721 if (range_end > high)
5722 high = range_end;
5723 }
5724 }
5725
5726 if (! low_set)
5727 /* If the first entry is an end-of-list marker, the range
5728 describes an empty scope, i.e. no instructions. */
5729 return 0;
5730
5731 if (low_return)
5732 *low_return = low;
5733 if (high_return)
5734 *high_return = high;
5735 return 1;
5736}
5737
af34e669
DJ
5738/* Get low and high pc attributes from a die. Return 1 if the attributes
5739 are present and valid, otherwise, return 0. Return -1 if the range is
5740 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5741static int
af34e669 5742dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5743 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5744 struct partial_symtab *pst)
c906108c
SS
5745{
5746 struct attribute *attr;
af34e669
DJ
5747 CORE_ADDR low = 0;
5748 CORE_ADDR high = 0;
5749 int ret = 0;
c906108c 5750
e142c38c 5751 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5752 if (attr)
af34e669
DJ
5753 {
5754 high = DW_ADDR (attr);
e142c38c 5755 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5756 if (attr)
5757 low = DW_ADDR (attr);
5758 else
5759 /* Found high w/o low attribute. */
5760 return 0;
5761
5762 /* Found consecutive range of addresses. */
5763 ret = 1;
5764 }
c906108c 5765 else
af34e669 5766 {
e142c38c 5767 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5768 if (attr != NULL)
5769 {
af34e669 5770 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5771 .debug_ranges section. */
d85a05f0 5772 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5773 return 0;
43039443 5774 /* Found discontinuous range of addresses. */
af34e669
DJ
5775 ret = -1;
5776 }
5777 }
c906108c
SS
5778
5779 if (high < low)
5780 return 0;
5781
5782 /* When using the GNU linker, .gnu.linkonce. sections are used to
5783 eliminate duplicate copies of functions and vtables and such.
5784 The linker will arbitrarily choose one and discard the others.
5785 The AT_*_pc values for such functions refer to local labels in
5786 these sections. If the section from that file was discarded, the
5787 labels are not in the output, so the relocs get a value of 0.
5788 If this is a discarded function, mark the pc bounds as invalid,
5789 so that GDB will ignore it. */
72dca2f5 5790 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5791 return 0;
5792
5793 *lowpc = low;
5794 *highpc = high;
af34e669 5795 return ret;
c906108c
SS
5796}
5797
b084d499
JB
5798/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5799 its low and high PC addresses. Do nothing if these addresses could not
5800 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5801 and HIGHPC to the high address if greater than HIGHPC. */
5802
5803static void
5804dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5805 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5806 struct dwarf2_cu *cu)
5807{
5808 CORE_ADDR low, high;
5809 struct die_info *child = die->child;
5810
d85a05f0 5811 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5812 {
5813 *lowpc = min (*lowpc, low);
5814 *highpc = max (*highpc, high);
5815 }
5816
5817 /* If the language does not allow nested subprograms (either inside
5818 subprograms or lexical blocks), we're done. */
5819 if (cu->language != language_ada)
5820 return;
6e70227d 5821
b084d499
JB
5822 /* Check all the children of the given DIE. If it contains nested
5823 subprograms, then check their pc bounds. Likewise, we need to
5824 check lexical blocks as well, as they may also contain subprogram
5825 definitions. */
5826 while (child && child->tag)
5827 {
5828 if (child->tag == DW_TAG_subprogram
5829 || child->tag == DW_TAG_lexical_block)
5830 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5831 child = sibling_die (child);
5832 }
5833}
5834
fae299cd
DC
5835/* Get the low and high pc's represented by the scope DIE, and store
5836 them in *LOWPC and *HIGHPC. If the correct values can't be
5837 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5838
5839static void
5840get_scope_pc_bounds (struct die_info *die,
5841 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5842 struct dwarf2_cu *cu)
5843{
5844 CORE_ADDR best_low = (CORE_ADDR) -1;
5845 CORE_ADDR best_high = (CORE_ADDR) 0;
5846 CORE_ADDR current_low, current_high;
5847
d85a05f0 5848 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5849 {
5850 best_low = current_low;
5851 best_high = current_high;
5852 }
5853 else
5854 {
5855 struct die_info *child = die->child;
5856
5857 while (child && child->tag)
5858 {
5859 switch (child->tag) {
5860 case DW_TAG_subprogram:
b084d499 5861 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5862 break;
5863 case DW_TAG_namespace:
f55ee35c 5864 case DW_TAG_module:
fae299cd
DC
5865 /* FIXME: carlton/2004-01-16: Should we do this for
5866 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5867 that current GCC's always emit the DIEs corresponding
5868 to definitions of methods of classes as children of a
5869 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5870 the DIEs giving the declarations, which could be
5871 anywhere). But I don't see any reason why the
5872 standards says that they have to be there. */
5873 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5874
5875 if (current_low != ((CORE_ADDR) -1))
5876 {
5877 best_low = min (best_low, current_low);
5878 best_high = max (best_high, current_high);
5879 }
5880 break;
5881 default:
5882 /* Ignore. */
5883 break;
5884 }
5885
5886 child = sibling_die (child);
5887 }
5888 }
5889
5890 *lowpc = best_low;
5891 *highpc = best_high;
5892}
5893
801e3a5b
JB
5894/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5895 in DIE. */
5896static void
5897dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5898 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5899{
5900 struct attribute *attr;
5901
5902 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5903 if (attr)
5904 {
5905 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5906
801e3a5b
JB
5907 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5908 if (attr)
5909 {
5910 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5911
801e3a5b
JB
5912 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5913 }
5914 }
5915
5916 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5917 if (attr)
5918 {
5919 bfd *obfd = cu->objfile->obfd;
5920
5921 /* The value of the DW_AT_ranges attribute is the offset of the
5922 address range list in the .debug_ranges section. */
5923 unsigned long offset = DW_UNSND (attr);
dce234bc 5924 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5925
5926 /* For some target architectures, but not others, the
5927 read_address function sign-extends the addresses it returns.
5928 To recognize base address selection entries, we need a
5929 mask. */
5930 unsigned int addr_size = cu->header.addr_size;
5931 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5932
5933 /* The base address, to which the next pair is relative. Note
5934 that this 'base' is a DWARF concept: most entries in a range
5935 list are relative, to reduce the number of relocs against the
5936 debugging information. This is separate from this function's
5937 'baseaddr' argument, which GDB uses to relocate debugging
5938 information from a shared library based on the address at
5939 which the library was loaded. */
d00adf39
DE
5940 CORE_ADDR base = cu->base_address;
5941 int base_known = cu->base_known;
801e3a5b 5942
be391dca 5943 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5944 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5945 {
5946 complaint (&symfile_complaints,
5947 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5948 offset);
5949 return;
5950 }
5951
5952 for (;;)
5953 {
5954 unsigned int bytes_read;
5955 CORE_ADDR start, end;
5956
5957 start = read_address (obfd, buffer, cu, &bytes_read);
5958 buffer += bytes_read;
5959 end = read_address (obfd, buffer, cu, &bytes_read);
5960 buffer += bytes_read;
5961
5962 /* Did we find the end of the range list? */
5963 if (start == 0 && end == 0)
5964 break;
5965
5966 /* Did we find a base address selection entry? */
5967 else if ((start & base_select_mask) == base_select_mask)
5968 {
5969 base = end;
5970 base_known = 1;
5971 }
5972
5973 /* We found an ordinary address range. */
5974 else
5975 {
5976 if (!base_known)
5977 {
5978 complaint (&symfile_complaints,
5979 _("Invalid .debug_ranges data (no base address)"));
5980 return;
5981 }
5982
6e70227d
DE
5983 record_block_range (block,
5984 baseaddr + base + start,
801e3a5b
JB
5985 baseaddr + base + end - 1);
5986 }
5987 }
5988 }
5989}
5990
c906108c
SS
5991/* Add an aggregate field to the field list. */
5992
5993static void
107d2387 5994dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 5995 struct dwarf2_cu *cu)
6e70227d 5996{
e7c27a73 5997 struct objfile *objfile = cu->objfile;
5e2b427d 5998 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5999 struct nextfield *new_field;
6000 struct attribute *attr;
6001 struct field *fp;
6002 char *fieldname = "";
6003
6004 /* Allocate a new field list entry and link it in. */
6005 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6006 make_cleanup (xfree, new_field);
c906108c 6007 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6008
6009 if (die->tag == DW_TAG_inheritance)
6010 {
6011 new_field->next = fip->baseclasses;
6012 fip->baseclasses = new_field;
6013 }
6014 else
6015 {
6016 new_field->next = fip->fields;
6017 fip->fields = new_field;
6018 }
c906108c
SS
6019 fip->nfields++;
6020
6021 /* Handle accessibility and virtuality of field.
6022 The default accessibility for members is public, the default
6023 accessibility for inheritance is private. */
6024 if (die->tag != DW_TAG_inheritance)
6025 new_field->accessibility = DW_ACCESS_public;
6026 else
6027 new_field->accessibility = DW_ACCESS_private;
6028 new_field->virtuality = DW_VIRTUALITY_none;
6029
e142c38c 6030 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6031 if (attr)
6032 new_field->accessibility = DW_UNSND (attr);
6033 if (new_field->accessibility != DW_ACCESS_public)
6034 fip->non_public_fields = 1;
e142c38c 6035 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6036 if (attr)
6037 new_field->virtuality = DW_UNSND (attr);
6038
6039 fp = &new_field->field;
a9a9bd0f 6040
e142c38c 6041 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6042 {
a9a9bd0f 6043 /* Data member other than a C++ static data member. */
6e70227d 6044
c906108c 6045 /* Get type of field. */
e7c27a73 6046 fp->type = die_type (die, cu);
c906108c 6047
d6a843b5 6048 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6049
c906108c 6050 /* Get bit size of field (zero if none). */
e142c38c 6051 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6052 if (attr)
6053 {
6054 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6055 }
6056 else
6057 {
6058 FIELD_BITSIZE (*fp) = 0;
6059 }
6060
6061 /* Get bit offset of field. */
e142c38c 6062 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6063 if (attr)
6064 {
d4b96c9a 6065 int byte_offset = 0;
c6a0999f 6066
3690dd37 6067 if (attr_form_is_section_offset (attr))
d4b96c9a 6068 dwarf2_complex_location_expr_complaint ();
3690dd37 6069 else if (attr_form_is_constant (attr))
c6a0999f 6070 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6071 else if (attr_form_is_block (attr))
c6a0999f 6072 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6073 else
6074 dwarf2_complex_location_expr_complaint ();
c6a0999f 6075
d6a843b5 6076 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6077 }
e142c38c 6078 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6079 if (attr)
6080 {
5e2b427d 6081 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6082 {
6083 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6084 additional bit offset from the MSB of the containing
6085 anonymous object to the MSB of the field. We don't
6086 have to do anything special since we don't need to
6087 know the size of the anonymous object. */
c906108c
SS
6088 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6089 }
6090 else
6091 {
6092 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6093 MSB of the anonymous object, subtract off the number of
6094 bits from the MSB of the field to the MSB of the
6095 object, and then subtract off the number of bits of
6096 the field itself. The result is the bit offset of
6097 the LSB of the field. */
c906108c
SS
6098 int anonymous_size;
6099 int bit_offset = DW_UNSND (attr);
6100
e142c38c 6101 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6102 if (attr)
6103 {
6104 /* The size of the anonymous object containing
6105 the bit field is explicit, so use the
6106 indicated size (in bytes). */
6107 anonymous_size = DW_UNSND (attr);
6108 }
6109 else
6110 {
6111 /* The size of the anonymous object containing
6112 the bit field must be inferred from the type
6113 attribute of the data member containing the
6114 bit field. */
6115 anonymous_size = TYPE_LENGTH (fp->type);
6116 }
6117 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6118 - bit_offset - FIELD_BITSIZE (*fp);
6119 }
6120 }
6121
6122 /* Get name of field. */
39cbfefa
DJ
6123 fieldname = dwarf2_name (die, cu);
6124 if (fieldname == NULL)
6125 fieldname = "";
d8151005
DJ
6126
6127 /* The name is already allocated along with this objfile, so we don't
6128 need to duplicate it for the type. */
6129 fp->name = fieldname;
c906108c
SS
6130
6131 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6132 pointer or virtual base class pointer) to private. */
e142c38c 6133 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6134 {
d48cc9dd 6135 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6136 new_field->accessibility = DW_ACCESS_private;
6137 fip->non_public_fields = 1;
6138 }
6139 }
a9a9bd0f 6140 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6141 {
a9a9bd0f
DC
6142 /* C++ static member. */
6143
6144 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6145 is a declaration, but all versions of G++ as of this writing
6146 (so through at least 3.2.1) incorrectly generate
6147 DW_TAG_variable tags. */
6e70227d 6148
c906108c 6149 char *physname;
c906108c 6150
a9a9bd0f 6151 /* Get name of field. */
39cbfefa
DJ
6152 fieldname = dwarf2_name (die, cu);
6153 if (fieldname == NULL)
c906108c
SS
6154 return;
6155
254e6b9e 6156 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6157 if (attr
6158 /* Only create a symbol if this is an external value.
6159 new_symbol checks this and puts the value in the global symbol
6160 table, which we want. If it is not external, new_symbol
6161 will try to put the value in cu->list_in_scope which is wrong. */
6162 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6163 {
6164 /* A static const member, not much different than an enum as far as
6165 we're concerned, except that we can support more types. */
6166 new_symbol (die, NULL, cu);
6167 }
6168
2df3850c 6169 /* Get physical name. */
94af9270 6170 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6171
d8151005
DJ
6172 /* The name is already allocated along with this objfile, so we don't
6173 need to duplicate it for the type. */
6174 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6175 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6176 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6177 }
6178 else if (die->tag == DW_TAG_inheritance)
6179 {
6180 /* C++ base class field. */
e142c38c 6181 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6182 if (attr)
d4b96c9a
JK
6183 {
6184 int byte_offset = 0;
6185
6186 if (attr_form_is_section_offset (attr))
6187 dwarf2_complex_location_expr_complaint ();
6188 else if (attr_form_is_constant (attr))
6189 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6190 else if (attr_form_is_block (attr))
6191 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6192 else
6193 dwarf2_complex_location_expr_complaint ();
6194
6195 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6196 }
c906108c 6197 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6198 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6199 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6200 fip->nbaseclasses++;
6201 }
6202}
6203
98751a41
JK
6204/* Add a typedef defined in the scope of the FIP's class. */
6205
6206static void
6207dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6208 struct dwarf2_cu *cu)
6e70227d 6209{
98751a41
JK
6210 struct objfile *objfile = cu->objfile;
6211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6212 struct typedef_field_list *new_field;
6213 struct attribute *attr;
6214 struct typedef_field *fp;
6215 char *fieldname = "";
6216
6217 /* Allocate a new field list entry and link it in. */
6218 new_field = xzalloc (sizeof (*new_field));
6219 make_cleanup (xfree, new_field);
6220
6221 gdb_assert (die->tag == DW_TAG_typedef);
6222
6223 fp = &new_field->field;
6224
6225 /* Get name of field. */
6226 fp->name = dwarf2_name (die, cu);
6227 if (fp->name == NULL)
6228 return;
6229
6230 fp->type = read_type_die (die, cu);
6231
6232 new_field->next = fip->typedef_field_list;
6233 fip->typedef_field_list = new_field;
6234 fip->typedef_field_list_count++;
6235}
6236
c906108c
SS
6237/* Create the vector of fields, and attach it to the type. */
6238
6239static void
fba45db2 6240dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6241 struct dwarf2_cu *cu)
c906108c
SS
6242{
6243 int nfields = fip->nfields;
6244
6245 /* Record the field count, allocate space for the array of fields,
6246 and create blank accessibility bitfields if necessary. */
6247 TYPE_NFIELDS (type) = nfields;
6248 TYPE_FIELDS (type) = (struct field *)
6249 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6250 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6251
b4ba55a1 6252 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6253 {
6254 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6255
6256 TYPE_FIELD_PRIVATE_BITS (type) =
6257 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6258 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6259
6260 TYPE_FIELD_PROTECTED_BITS (type) =
6261 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6262 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6263
774b6a14
TT
6264 TYPE_FIELD_IGNORE_BITS (type) =
6265 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6266 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6267 }
6268
6269 /* If the type has baseclasses, allocate and clear a bit vector for
6270 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6271 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6272 {
6273 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6274 unsigned char *pointer;
c906108c
SS
6275
6276 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6277 pointer = TYPE_ALLOC (type, num_bytes);
6278 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6279 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6280 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6281 }
6282
6283 /* Copy the saved-up fields into the field vector. Start from the head
6284 of the list, adding to the tail of the field array, so that they end
6285 up in the same order in the array in which they were added to the list. */
6286 while (nfields-- > 0)
6287 {
7d0ccb61
DJ
6288 struct nextfield *fieldp;
6289
6290 if (fip->fields)
6291 {
6292 fieldp = fip->fields;
6293 fip->fields = fieldp->next;
6294 }
6295 else
6296 {
6297 fieldp = fip->baseclasses;
6298 fip->baseclasses = fieldp->next;
6299 }
6300
6301 TYPE_FIELD (type, nfields) = fieldp->field;
6302 switch (fieldp->accessibility)
c906108c 6303 {
c5aa993b 6304 case DW_ACCESS_private:
b4ba55a1
JB
6305 if (cu->language != language_ada)
6306 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6307 break;
c906108c 6308
c5aa993b 6309 case DW_ACCESS_protected:
b4ba55a1
JB
6310 if (cu->language != language_ada)
6311 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6312 break;
c906108c 6313
c5aa993b
JM
6314 case DW_ACCESS_public:
6315 break;
c906108c 6316
c5aa993b
JM
6317 default:
6318 /* Unknown accessibility. Complain and treat it as public. */
6319 {
e2e0b3e5 6320 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6321 fieldp->accessibility);
c5aa993b
JM
6322 }
6323 break;
c906108c
SS
6324 }
6325 if (nfields < fip->nbaseclasses)
6326 {
7d0ccb61 6327 switch (fieldp->virtuality)
c906108c 6328 {
c5aa993b
JM
6329 case DW_VIRTUALITY_virtual:
6330 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6331 if (cu->language == language_ada)
6332 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6333 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6334 break;
c906108c
SS
6335 }
6336 }
c906108c
SS
6337 }
6338}
6339
c906108c
SS
6340/* Add a member function to the proper fieldlist. */
6341
6342static void
107d2387 6343dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6344 struct type *type, struct dwarf2_cu *cu)
c906108c 6345{
e7c27a73 6346 struct objfile *objfile = cu->objfile;
c906108c
SS
6347 struct attribute *attr;
6348 struct fnfieldlist *flp;
6349 int i;
6350 struct fn_field *fnp;
6351 char *fieldname;
c906108c 6352 struct nextfnfield *new_fnfield;
f792889a 6353 struct type *this_type;
c906108c 6354
b4ba55a1
JB
6355 if (cu->language == language_ada)
6356 error ("unexpected member function in Ada type");
6357
2df3850c 6358 /* Get name of member function. */
39cbfefa
DJ
6359 fieldname = dwarf2_name (die, cu);
6360 if (fieldname == NULL)
2df3850c 6361 return;
c906108c 6362
c906108c
SS
6363 /* Look up member function name in fieldlist. */
6364 for (i = 0; i < fip->nfnfields; i++)
6365 {
27bfe10e 6366 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6367 break;
6368 }
6369
6370 /* Create new list element if necessary. */
6371 if (i < fip->nfnfields)
6372 flp = &fip->fnfieldlists[i];
6373 else
6374 {
6375 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6376 {
6377 fip->fnfieldlists = (struct fnfieldlist *)
6378 xrealloc (fip->fnfieldlists,
6379 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6380 * sizeof (struct fnfieldlist));
c906108c 6381 if (fip->nfnfields == 0)
c13c43fd 6382 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6383 }
6384 flp = &fip->fnfieldlists[fip->nfnfields];
6385 flp->name = fieldname;
6386 flp->length = 0;
6387 flp->head = NULL;
3da10d80 6388 i = fip->nfnfields++;
c906108c
SS
6389 }
6390
6391 /* Create a new member function field and chain it to the field list
6392 entry. */
6393 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6394 make_cleanup (xfree, new_fnfield);
c906108c
SS
6395 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6396 new_fnfield->next = flp->head;
6397 flp->head = new_fnfield;
6398 flp->length++;
6399
6400 /* Fill in the member function field info. */
6401 fnp = &new_fnfield->fnfield;
3da10d80
KS
6402
6403 /* Delay processing of the physname until later. */
6404 if (cu->language == language_cplus || cu->language == language_java)
6405 {
6406 add_to_method_list (type, i, flp->length - 1, fieldname,
6407 die, cu);
6408 }
6409 else
6410 {
6411 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6412 fnp->physname = physname ? physname : "";
6413 }
6414
c906108c 6415 fnp->type = alloc_type (objfile);
f792889a
DJ
6416 this_type = read_type_die (die, cu);
6417 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6418 {
f792889a 6419 int nparams = TYPE_NFIELDS (this_type);
c906108c 6420
f792889a 6421 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6422 of the method itself (TYPE_CODE_METHOD). */
6423 smash_to_method_type (fnp->type, type,
f792889a
DJ
6424 TYPE_TARGET_TYPE (this_type),
6425 TYPE_FIELDS (this_type),
6426 TYPE_NFIELDS (this_type),
6427 TYPE_VARARGS (this_type));
c906108c
SS
6428
6429 /* Handle static member functions.
c5aa993b
JM
6430 Dwarf2 has no clean way to discern C++ static and non-static
6431 member functions. G++ helps GDB by marking the first
6432 parameter for non-static member functions (which is the
6433 this pointer) as artificial. We obtain this information
6434 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6435 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6436 fnp->voffset = VOFFSET_STATIC;
6437 }
6438 else
e2e0b3e5 6439 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6440 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6441
6442 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6443 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6444 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6445
6446 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6447 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6448
6449 /* Get accessibility. */
e142c38c 6450 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6451 if (attr)
6452 {
6453 switch (DW_UNSND (attr))
6454 {
c5aa993b
JM
6455 case DW_ACCESS_private:
6456 fnp->is_private = 1;
6457 break;
6458 case DW_ACCESS_protected:
6459 fnp->is_protected = 1;
6460 break;
c906108c
SS
6461 }
6462 }
6463
b02dede2 6464 /* Check for artificial methods. */
e142c38c 6465 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6466 if (attr && DW_UNSND (attr) != 0)
6467 fnp->is_artificial = 1;
6468
0d564a31 6469 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6470 function. For older versions of GCC, this is an offset in the
6471 appropriate virtual table, as specified by DW_AT_containing_type.
6472 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6473 to the object address. */
6474
e142c38c 6475 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6476 if (attr)
8e19ed76 6477 {
aec5aa8b 6478 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6479 {
aec5aa8b
TT
6480 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6481 {
6482 /* Old-style GCC. */
6483 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6484 }
6485 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6486 || (DW_BLOCK (attr)->size > 1
6487 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6488 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6489 {
6490 struct dwarf_block blk;
6491 int offset;
6492
6493 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6494 ? 1 : 2);
6495 blk.size = DW_BLOCK (attr)->size - offset;
6496 blk.data = DW_BLOCK (attr)->data + offset;
6497 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6498 if ((fnp->voffset % cu->header.addr_size) != 0)
6499 dwarf2_complex_location_expr_complaint ();
6500 else
6501 fnp->voffset /= cu->header.addr_size;
6502 fnp->voffset += 2;
6503 }
6504 else
6505 dwarf2_complex_location_expr_complaint ();
6506
6507 if (!fnp->fcontext)
6508 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6509 }
3690dd37 6510 else if (attr_form_is_section_offset (attr))
8e19ed76 6511 {
4d3c2250 6512 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6513 }
6514 else
6515 {
4d3c2250
KB
6516 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6517 fieldname);
8e19ed76 6518 }
0d564a31 6519 }
d48cc9dd
DJ
6520 else
6521 {
6522 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6523 if (attr && DW_UNSND (attr))
6524 {
6525 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6526 complaint (&symfile_complaints,
6527 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6528 fieldname, die->offset);
9655fd1a 6529 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6530 TYPE_CPLUS_DYNAMIC (type) = 1;
6531 }
6532 }
c906108c
SS
6533}
6534
6535/* Create the vector of member function fields, and attach it to the type. */
6536
6537static void
fba45db2 6538dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6539 struct dwarf2_cu *cu)
c906108c
SS
6540{
6541 struct fnfieldlist *flp;
6542 int total_length = 0;
6543 int i;
6544
b4ba55a1
JB
6545 if (cu->language == language_ada)
6546 error ("unexpected member functions in Ada type");
6547
c906108c
SS
6548 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6549 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6550 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6551
6552 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6553 {
6554 struct nextfnfield *nfp = flp->head;
6555 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6556 int k;
6557
6558 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6559 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6560 fn_flp->fn_fields = (struct fn_field *)
6561 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6562 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6563 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6564
6565 total_length += flp->length;
6566 }
6567
6568 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6569 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6570}
6571
1168df01
JB
6572/* Returns non-zero if NAME is the name of a vtable member in CU's
6573 language, zero otherwise. */
6574static int
6575is_vtable_name (const char *name, struct dwarf2_cu *cu)
6576{
6577 static const char vptr[] = "_vptr";
987504bb 6578 static const char vtable[] = "vtable";
1168df01 6579
987504bb
JJ
6580 /* Look for the C++ and Java forms of the vtable. */
6581 if ((cu->language == language_java
6582 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6583 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6584 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6585 return 1;
6586
6587 return 0;
6588}
6589
c0dd20ea 6590/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6591 functions, with the ABI-specified layout. If TYPE describes
6592 such a structure, smash it into a member function type.
61049d3b
DJ
6593
6594 GCC shouldn't do this; it should just output pointer to member DIEs.
6595 This is GCC PR debug/28767. */
c0dd20ea 6596
0b92b5bb
TT
6597static void
6598quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6599{
0b92b5bb 6600 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6601
6602 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6603 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6604 return;
c0dd20ea
DJ
6605
6606 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6607 if (TYPE_FIELD_NAME (type, 0) == NULL
6608 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6609 || TYPE_FIELD_NAME (type, 1) == NULL
6610 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6611 return;
c0dd20ea
DJ
6612
6613 /* Find the type of the method. */
0b92b5bb 6614 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6615 if (pfn_type == NULL
6616 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6617 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6618 return;
c0dd20ea
DJ
6619
6620 /* Look for the "this" argument. */
6621 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6622 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6623 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6624 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6625 return;
c0dd20ea
DJ
6626
6627 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6628 new_type = alloc_type (objfile);
6629 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6630 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6631 TYPE_VARARGS (pfn_type));
0b92b5bb 6632 smash_to_methodptr_type (type, new_type);
c0dd20ea 6633}
1168df01 6634
c906108c 6635/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6636 (definition) to create a type for the structure or union. Fill in
6637 the type's name and general properties; the members will not be
6638 processed until process_structure_type.
c906108c 6639
c767944b
DJ
6640 NOTE: we need to call these functions regardless of whether or not the
6641 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6642 structure or union. This gets the type entered into our set of
6643 user defined types.
6644
6645 However, if the structure is incomplete (an opaque struct/union)
6646 then suppress creating a symbol table entry for it since gdb only
6647 wants to find the one with the complete definition. Note that if
6648 it is complete, we just call new_symbol, which does it's own
6649 checking about whether the struct/union is anonymous or not (and
6650 suppresses creating a symbol table entry itself). */
6651
f792889a 6652static struct type *
134d01f1 6653read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6654{
e7c27a73 6655 struct objfile *objfile = cu->objfile;
c906108c
SS
6656 struct type *type;
6657 struct attribute *attr;
39cbfefa 6658 char *name;
c906108c 6659
348e048f
DE
6660 /* If the definition of this type lives in .debug_types, read that type.
6661 Don't follow DW_AT_specification though, that will take us back up
6662 the chain and we want to go down. */
6663 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6664 if (attr)
6665 {
6666 struct dwarf2_cu *type_cu = cu;
6667 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6668
348e048f
DE
6669 /* We could just recurse on read_structure_type, but we need to call
6670 get_die_type to ensure only one type for this DIE is created.
6671 This is important, for example, because for c++ classes we need
6672 TYPE_NAME set which is only done by new_symbol. Blech. */
6673 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6674
6675 /* TYPE_CU may not be the same as CU.
6676 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6677 return set_die_type (die, type, cu);
6678 }
6679
c0dd20ea 6680 type = alloc_type (objfile);
c906108c 6681 INIT_CPLUS_SPECIFIC (type);
93311388 6682
39cbfefa
DJ
6683 name = dwarf2_name (die, cu);
6684 if (name != NULL)
c906108c 6685 {
987504bb
JJ
6686 if (cu->language == language_cplus
6687 || cu->language == language_java)
63d06c5c 6688 {
3da10d80
KS
6689 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6690
6691 /* dwarf2_full_name might have already finished building the DIE's
6692 type. If so, there is no need to continue. */
6693 if (get_die_type (die, cu) != NULL)
6694 return get_die_type (die, cu);
6695
6696 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6697 if (die->tag == DW_TAG_structure_type
6698 || die->tag == DW_TAG_class_type)
6699 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6700 }
6701 else
6702 {
d8151005
DJ
6703 /* The name is already allocated along with this objfile, so
6704 we don't need to duplicate it for the type. */
94af9270
KS
6705 TYPE_TAG_NAME (type) = (char *) name;
6706 if (die->tag == DW_TAG_class_type)
6707 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6708 }
c906108c
SS
6709 }
6710
6711 if (die->tag == DW_TAG_structure_type)
6712 {
6713 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6714 }
6715 else if (die->tag == DW_TAG_union_type)
6716 {
6717 TYPE_CODE (type) = TYPE_CODE_UNION;
6718 }
6719 else
6720 {
c906108c
SS
6721 TYPE_CODE (type) = TYPE_CODE_CLASS;
6722 }
6723
0cc2414c
TT
6724 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6725 TYPE_DECLARED_CLASS (type) = 1;
6726
e142c38c 6727 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6728 if (attr)
6729 {
6730 TYPE_LENGTH (type) = DW_UNSND (attr);
6731 }
6732 else
6733 {
6734 TYPE_LENGTH (type) = 0;
6735 }
6736
876cecd0 6737 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6738 if (die_is_declaration (die, cu))
876cecd0 6739 TYPE_STUB (type) = 1;
a6c727b2
DJ
6740 else if (attr == NULL && die->child == NULL
6741 && producer_is_realview (cu->producer))
6742 /* RealView does not output the required DW_AT_declaration
6743 on incomplete types. */
6744 TYPE_STUB (type) = 1;
dc718098 6745
c906108c
SS
6746 /* We need to add the type field to the die immediately so we don't
6747 infinitely recurse when dealing with pointers to the structure
6748 type within the structure itself. */
1c379e20 6749 set_die_type (die, type, cu);
c906108c 6750
7e314c57
JK
6751 /* set_die_type should be already done. */
6752 set_descriptive_type (type, die, cu);
6753
c767944b
DJ
6754 return type;
6755}
6756
6757/* Finish creating a structure or union type, including filling in
6758 its members and creating a symbol for it. */
6759
6760static void
6761process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6762{
6763 struct objfile *objfile = cu->objfile;
6764 struct die_info *child_die = die->child;
6765 struct type *type;
6766
6767 type = get_die_type (die, cu);
6768 if (type == NULL)
6769 type = read_structure_type (die, cu);
6770
e142c38c 6771 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6772 {
6773 struct field_info fi;
6774 struct die_info *child_die;
34eaf542 6775 VEC (symbolp) *template_args = NULL;
c767944b 6776 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6777
6778 memset (&fi, 0, sizeof (struct field_info));
6779
639d11d3 6780 child_die = die->child;
c906108c
SS
6781
6782 while (child_die && child_die->tag)
6783 {
a9a9bd0f
DC
6784 if (child_die->tag == DW_TAG_member
6785 || child_die->tag == DW_TAG_variable)
c906108c 6786 {
a9a9bd0f
DC
6787 /* NOTE: carlton/2002-11-05: A C++ static data member
6788 should be a DW_TAG_member that is a declaration, but
6789 all versions of G++ as of this writing (so through at
6790 least 3.2.1) incorrectly generate DW_TAG_variable
6791 tags for them instead. */
e7c27a73 6792 dwarf2_add_field (&fi, child_die, cu);
c906108c 6793 }
8713b1b1 6794 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6795 {
6796 /* C++ member function. */
e7c27a73 6797 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6798 }
6799 else if (child_die->tag == DW_TAG_inheritance)
6800 {
6801 /* C++ base class field. */
e7c27a73 6802 dwarf2_add_field (&fi, child_die, cu);
c906108c 6803 }
98751a41
JK
6804 else if (child_die->tag == DW_TAG_typedef)
6805 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6806 else if (child_die->tag == DW_TAG_template_type_param
6807 || child_die->tag == DW_TAG_template_value_param)
6808 {
6809 struct symbol *arg = new_symbol (child_die, NULL, cu);
6810
f1078f66
DJ
6811 if (arg != NULL)
6812 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6813 }
6814
c906108c
SS
6815 child_die = sibling_die (child_die);
6816 }
6817
34eaf542
TT
6818 /* Attach template arguments to type. */
6819 if (! VEC_empty (symbolp, template_args))
6820 {
6821 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6822 TYPE_N_TEMPLATE_ARGUMENTS (type)
6823 = VEC_length (symbolp, template_args);
6824 TYPE_TEMPLATE_ARGUMENTS (type)
6825 = obstack_alloc (&objfile->objfile_obstack,
6826 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6827 * sizeof (struct symbol *)));
6828 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6829 VEC_address (symbolp, template_args),
6830 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6831 * sizeof (struct symbol *)));
6832 VEC_free (symbolp, template_args);
6833 }
6834
c906108c
SS
6835 /* Attach fields and member functions to the type. */
6836 if (fi.nfields)
e7c27a73 6837 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6838 if (fi.nfnfields)
6839 {
e7c27a73 6840 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6841
c5aa993b 6842 /* Get the type which refers to the base class (possibly this
c906108c 6843 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6844 class from the DW_AT_containing_type attribute. This use of
6845 DW_AT_containing_type is a GNU extension. */
c906108c 6846
e142c38c 6847 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6848 {
e7c27a73 6849 struct type *t = die_containing_type (die, cu);
c906108c
SS
6850
6851 TYPE_VPTR_BASETYPE (type) = t;
6852 if (type == t)
6853 {
c906108c
SS
6854 int i;
6855
6856 /* Our own class provides vtbl ptr. */
6857 for (i = TYPE_NFIELDS (t) - 1;
6858 i >= TYPE_N_BASECLASSES (t);
6859 --i)
6860 {
6861 char *fieldname = TYPE_FIELD_NAME (t, i);
6862
1168df01 6863 if (is_vtable_name (fieldname, cu))
c906108c
SS
6864 {
6865 TYPE_VPTR_FIELDNO (type) = i;
6866 break;
6867 }
6868 }
6869
6870 /* Complain if virtual function table field not found. */
6871 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6872 complaint (&symfile_complaints,
e2e0b3e5 6873 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6874 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6875 "");
c906108c
SS
6876 }
6877 else
6878 {
6879 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6880 }
6881 }
f6235d4c
EZ
6882 else if (cu->producer
6883 && strncmp (cu->producer,
6884 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6885 {
6886 /* The IBM XLC compiler does not provide direct indication
6887 of the containing type, but the vtable pointer is
6888 always named __vfp. */
6889
6890 int i;
6891
6892 for (i = TYPE_NFIELDS (type) - 1;
6893 i >= TYPE_N_BASECLASSES (type);
6894 --i)
6895 {
6896 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6897 {
6898 TYPE_VPTR_FIELDNO (type) = i;
6899 TYPE_VPTR_BASETYPE (type) = type;
6900 break;
6901 }
6902 }
6903 }
c906108c 6904 }
98751a41
JK
6905
6906 /* Copy fi.typedef_field_list linked list elements content into the
6907 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6908 if (fi.typedef_field_list)
6909 {
6910 int i = fi.typedef_field_list_count;
6911
a0d7a4ff 6912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6913 TYPE_TYPEDEF_FIELD_ARRAY (type)
6914 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6915 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6916
6917 /* Reverse the list order to keep the debug info elements order. */
6918 while (--i >= 0)
6919 {
6920 struct typedef_field *dest, *src;
6e70227d 6921
98751a41
JK
6922 dest = &TYPE_TYPEDEF_FIELD (type, i);
6923 src = &fi.typedef_field_list->field;
6924 fi.typedef_field_list = fi.typedef_field_list->next;
6925 *dest = *src;
6926 }
6927 }
c767944b
DJ
6928
6929 do_cleanups (back_to);
c906108c 6930 }
63d06c5c 6931
0b92b5bb
TT
6932 quirk_gcc_member_function_pointer (type, cu->objfile);
6933
90aeadfc
DC
6934 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6935 snapshots) has been known to create a die giving a declaration
6936 for a class that has, as a child, a die giving a definition for a
6937 nested class. So we have to process our children even if the
6938 current die is a declaration. Normally, of course, a declaration
6939 won't have any children at all. */
134d01f1 6940
90aeadfc
DC
6941 while (child_die != NULL && child_die->tag)
6942 {
6943 if (child_die->tag == DW_TAG_member
6944 || child_die->tag == DW_TAG_variable
34eaf542
TT
6945 || child_die->tag == DW_TAG_inheritance
6946 || child_die->tag == DW_TAG_template_value_param
6947 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6948 {
90aeadfc 6949 /* Do nothing. */
134d01f1 6950 }
90aeadfc
DC
6951 else
6952 process_die (child_die, cu);
134d01f1 6953
90aeadfc 6954 child_die = sibling_die (child_die);
134d01f1
DJ
6955 }
6956
fa4028e9
JB
6957 /* Do not consider external references. According to the DWARF standard,
6958 these DIEs are identified by the fact that they have no byte_size
6959 attribute, and a declaration attribute. */
6960 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6961 || !die_is_declaration (die, cu))
c767944b 6962 new_symbol (die, type, cu);
134d01f1
DJ
6963}
6964
6965/* Given a DW_AT_enumeration_type die, set its type. We do not
6966 complete the type's fields yet, or create any symbols. */
c906108c 6967
f792889a 6968static struct type *
134d01f1 6969read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6970{
e7c27a73 6971 struct objfile *objfile = cu->objfile;
c906108c 6972 struct type *type;
c906108c 6973 struct attribute *attr;
0114d602 6974 const char *name;
134d01f1 6975
348e048f
DE
6976 /* If the definition of this type lives in .debug_types, read that type.
6977 Don't follow DW_AT_specification though, that will take us back up
6978 the chain and we want to go down. */
6979 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6980 if (attr)
6981 {
6982 struct dwarf2_cu *type_cu = cu;
6983 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6984
348e048f 6985 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6986
6987 /* TYPE_CU may not be the same as CU.
6988 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6989 return set_die_type (die, type, cu);
6990 }
6991
c906108c
SS
6992 type = alloc_type (objfile);
6993
6994 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6995 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6996 if (name != NULL)
0114d602 6997 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6998
e142c38c 6999 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7000 if (attr)
7001 {
7002 TYPE_LENGTH (type) = DW_UNSND (attr);
7003 }
7004 else
7005 {
7006 TYPE_LENGTH (type) = 0;
7007 }
7008
137033e9
JB
7009 /* The enumeration DIE can be incomplete. In Ada, any type can be
7010 declared as private in the package spec, and then defined only
7011 inside the package body. Such types are known as Taft Amendment
7012 Types. When another package uses such a type, an incomplete DIE
7013 may be generated by the compiler. */
02eb380e 7014 if (die_is_declaration (die, cu))
876cecd0 7015 TYPE_STUB (type) = 1;
02eb380e 7016
f792889a 7017 return set_die_type (die, type, cu);
134d01f1
DJ
7018}
7019
7020/* Given a pointer to a die which begins an enumeration, process all
7021 the dies that define the members of the enumeration, and create the
7022 symbol for the enumeration type.
7023
7024 NOTE: We reverse the order of the element list. */
7025
7026static void
7027process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7028{
f792889a 7029 struct type *this_type;
134d01f1 7030
f792889a
DJ
7031 this_type = get_die_type (die, cu);
7032 if (this_type == NULL)
7033 this_type = read_enumeration_type (die, cu);
9dc481d3 7034
639d11d3 7035 if (die->child != NULL)
c906108c 7036 {
9dc481d3
DE
7037 struct die_info *child_die;
7038 struct symbol *sym;
7039 struct field *fields = NULL;
7040 int num_fields = 0;
7041 int unsigned_enum = 1;
7042 char *name;
7043
639d11d3 7044 child_die = die->child;
c906108c
SS
7045 while (child_die && child_die->tag)
7046 {
7047 if (child_die->tag != DW_TAG_enumerator)
7048 {
e7c27a73 7049 process_die (child_die, cu);
c906108c
SS
7050 }
7051 else
7052 {
39cbfefa
DJ
7053 name = dwarf2_name (child_die, cu);
7054 if (name)
c906108c 7055 {
f792889a 7056 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7057 if (SYMBOL_VALUE (sym) < 0)
7058 unsigned_enum = 0;
7059
7060 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7061 {
7062 fields = (struct field *)
7063 xrealloc (fields,
7064 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7065 * sizeof (struct field));
c906108c
SS
7066 }
7067
3567439c 7068 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7069 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7070 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7071 FIELD_BITSIZE (fields[num_fields]) = 0;
7072
7073 num_fields++;
7074 }
7075 }
7076
7077 child_die = sibling_die (child_die);
7078 }
7079
7080 if (num_fields)
7081 {
f792889a
DJ
7082 TYPE_NFIELDS (this_type) = num_fields;
7083 TYPE_FIELDS (this_type) = (struct field *)
7084 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7085 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7086 sizeof (struct field) * num_fields);
b8c9b27d 7087 xfree (fields);
c906108c
SS
7088 }
7089 if (unsigned_enum)
876cecd0 7090 TYPE_UNSIGNED (this_type) = 1;
c906108c 7091 }
134d01f1 7092
f792889a 7093 new_symbol (die, this_type, cu);
c906108c
SS
7094}
7095
7096/* Extract all information from a DW_TAG_array_type DIE and put it in
7097 the DIE's type field. For now, this only handles one dimensional
7098 arrays. */
7099
f792889a 7100static struct type *
e7c27a73 7101read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7102{
e7c27a73 7103 struct objfile *objfile = cu->objfile;
c906108c 7104 struct die_info *child_die;
7e314c57 7105 struct type *type;
c906108c
SS
7106 struct type *element_type, *range_type, *index_type;
7107 struct type **range_types = NULL;
7108 struct attribute *attr;
7109 int ndim = 0;
7110 struct cleanup *back_to;
39cbfefa 7111 char *name;
c906108c 7112
e7c27a73 7113 element_type = die_type (die, cu);
c906108c 7114
7e314c57
JK
7115 /* The die_type call above may have already set the type for this DIE. */
7116 type = get_die_type (die, cu);
7117 if (type)
7118 return type;
7119
c906108c
SS
7120 /* Irix 6.2 native cc creates array types without children for
7121 arrays with unspecified length. */
639d11d3 7122 if (die->child == NULL)
c906108c 7123 {
46bf5051 7124 index_type = objfile_type (objfile)->builtin_int;
c906108c 7125 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7126 type = create_array_type (NULL, element_type, range_type);
7127 return set_die_type (die, type, cu);
c906108c
SS
7128 }
7129
7130 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7131 child_die = die->child;
c906108c
SS
7132 while (child_die && child_die->tag)
7133 {
7134 if (child_die->tag == DW_TAG_subrange_type)
7135 {
f792889a 7136 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7137
f792889a 7138 if (child_type != NULL)
a02abb62
JB
7139 {
7140 /* The range type was succesfully read. Save it for
7141 the array type creation. */
7142 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7143 {
7144 range_types = (struct type **)
7145 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7146 * sizeof (struct type *));
7147 if (ndim == 0)
7148 make_cleanup (free_current_contents, &range_types);
7149 }
f792889a 7150 range_types[ndim++] = child_type;
a02abb62 7151 }
c906108c
SS
7152 }
7153 child_die = sibling_die (child_die);
7154 }
7155
7156 /* Dwarf2 dimensions are output from left to right, create the
7157 necessary array types in backwards order. */
7ca2d3a3 7158
c906108c 7159 type = element_type;
7ca2d3a3
DL
7160
7161 if (read_array_order (die, cu) == DW_ORD_col_major)
7162 {
7163 int i = 0;
9a619af0 7164
7ca2d3a3
DL
7165 while (i < ndim)
7166 type = create_array_type (NULL, type, range_types[i++]);
7167 }
7168 else
7169 {
7170 while (ndim-- > 0)
7171 type = create_array_type (NULL, type, range_types[ndim]);
7172 }
c906108c 7173
f5f8a009
EZ
7174 /* Understand Dwarf2 support for vector types (like they occur on
7175 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7176 array type. This is not part of the Dwarf2/3 standard yet, but a
7177 custom vendor extension. The main difference between a regular
7178 array and the vector variant is that vectors are passed by value
7179 to functions. */
e142c38c 7180 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7181 if (attr)
ea37ba09 7182 make_vector_type (type);
f5f8a009 7183
39cbfefa
DJ
7184 name = dwarf2_name (die, cu);
7185 if (name)
7186 TYPE_NAME (type) = name;
6e70227d 7187
7e314c57
JK
7188 /* Install the type in the die. */
7189 set_die_type (die, type, cu);
7190
7191 /* set_die_type should be already done. */
b4ba55a1
JB
7192 set_descriptive_type (type, die, cu);
7193
c906108c
SS
7194 do_cleanups (back_to);
7195
7e314c57 7196 return type;
c906108c
SS
7197}
7198
7ca2d3a3 7199static enum dwarf_array_dim_ordering
6e70227d 7200read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7201{
7202 struct attribute *attr;
7203
7204 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7205
7206 if (attr) return DW_SND (attr);
7207
7208 /*
7209 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7210 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7211 laid out as per normal fortran.
7212
6e70227d 7213 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7214 version checking.
7215 */
7216
905e0470
PM
7217 if (cu->language == language_fortran
7218 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7219 {
7220 return DW_ORD_row_major;
7221 }
7222
6e70227d 7223 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7224 {
7225 case array_column_major:
7226 return DW_ORD_col_major;
7227 case array_row_major:
7228 default:
7229 return DW_ORD_row_major;
7230 };
7231}
7232
72019c9c
GM
7233/* Extract all information from a DW_TAG_set_type DIE and put it in
7234 the DIE's type field. */
7235
f792889a 7236static struct type *
72019c9c
GM
7237read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7238{
7e314c57
JK
7239 struct type *domain_type, *set_type;
7240 struct attribute *attr;
f792889a 7241
7e314c57
JK
7242 domain_type = die_type (die, cu);
7243
7244 /* The die_type call above may have already set the type for this DIE. */
7245 set_type = get_die_type (die, cu);
7246 if (set_type)
7247 return set_type;
7248
7249 set_type = create_set_type (NULL, domain_type);
7250
7251 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7252 if (attr)
7253 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7254
f792889a 7255 return set_die_type (die, set_type, cu);
72019c9c 7256}
7ca2d3a3 7257
c906108c
SS
7258/* First cut: install each common block member as a global variable. */
7259
7260static void
e7c27a73 7261read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7262{
7263 struct die_info *child_die;
7264 struct attribute *attr;
7265 struct symbol *sym;
7266 CORE_ADDR base = (CORE_ADDR) 0;
7267
e142c38c 7268 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7269 if (attr)
7270 {
8e19ed76
PS
7271 /* Support the .debug_loc offsets */
7272 if (attr_form_is_block (attr))
7273 {
e7c27a73 7274 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7275 }
3690dd37 7276 else if (attr_form_is_section_offset (attr))
8e19ed76 7277 {
4d3c2250 7278 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7279 }
7280 else
7281 {
4d3c2250
KB
7282 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7283 "common block member");
8e19ed76 7284 }
c906108c 7285 }
639d11d3 7286 if (die->child != NULL)
c906108c 7287 {
639d11d3 7288 child_die = die->child;
c906108c
SS
7289 while (child_die && child_die->tag)
7290 {
e7c27a73 7291 sym = new_symbol (child_die, NULL, cu);
e142c38c 7292 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7293 if (sym != NULL && attr != NULL)
c906108c 7294 {
d4b96c9a
JK
7295 CORE_ADDR byte_offset = 0;
7296
7297 if (attr_form_is_section_offset (attr))
7298 dwarf2_complex_location_expr_complaint ();
7299 else if (attr_form_is_constant (attr))
7300 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7301 else if (attr_form_is_block (attr))
7302 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7303 else
7304 dwarf2_complex_location_expr_complaint ();
7305
7306 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7307 add_symbol_to_list (sym, &global_symbols);
7308 }
7309 child_die = sibling_die (child_die);
7310 }
7311 }
7312}
7313
0114d602 7314/* Create a type for a C++ namespace. */
d9fa45fe 7315
0114d602
DJ
7316static struct type *
7317read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7318{
e7c27a73 7319 struct objfile *objfile = cu->objfile;
0114d602 7320 const char *previous_prefix, *name;
9219021c 7321 int is_anonymous;
0114d602
DJ
7322 struct type *type;
7323
7324 /* For extensions, reuse the type of the original namespace. */
7325 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7326 {
7327 struct die_info *ext_die;
7328 struct dwarf2_cu *ext_cu = cu;
9a619af0 7329
0114d602
DJ
7330 ext_die = dwarf2_extension (die, &ext_cu);
7331 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7332
7333 /* EXT_CU may not be the same as CU.
7334 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7335 return set_die_type (die, type, cu);
7336 }
9219021c 7337
e142c38c 7338 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7339
7340 /* Now build the name of the current namespace. */
7341
0114d602
DJ
7342 previous_prefix = determine_prefix (die, cu);
7343 if (previous_prefix[0] != '\0')
7344 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7345 previous_prefix, name, 0, cu);
0114d602
DJ
7346
7347 /* Create the type. */
7348 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7349 objfile);
7350 TYPE_NAME (type) = (char *) name;
7351 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7352
60531b24 7353 return set_die_type (die, type, cu);
0114d602
DJ
7354}
7355
7356/* Read a C++ namespace. */
7357
7358static void
7359read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7360{
7361 struct objfile *objfile = cu->objfile;
7362 const char *name;
7363 int is_anonymous;
9219021c 7364
5c4e30ca
DC
7365 /* Add a symbol associated to this if we haven't seen the namespace
7366 before. Also, add a using directive if it's an anonymous
7367 namespace. */
9219021c 7368
f2f0e013 7369 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7370 {
7371 struct type *type;
7372
0114d602 7373 type = read_type_die (die, cu);
e7c27a73 7374 new_symbol (die, type, cu);
5c4e30ca 7375
0114d602 7376 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7377 if (is_anonymous)
0114d602
DJ
7378 {
7379 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7380
c0cc3a76 7381 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7382 NULL, &objfile->objfile_obstack);
0114d602 7383 }
5c4e30ca 7384 }
9219021c 7385
639d11d3 7386 if (die->child != NULL)
d9fa45fe 7387 {
639d11d3 7388 struct die_info *child_die = die->child;
6e70227d 7389
d9fa45fe
DC
7390 while (child_die && child_die->tag)
7391 {
e7c27a73 7392 process_die (child_die, cu);
d9fa45fe
DC
7393 child_die = sibling_die (child_die);
7394 }
7395 }
38d518c9
EZ
7396}
7397
f55ee35c
JK
7398/* Read a Fortran module as type. This DIE can be only a declaration used for
7399 imported module. Still we need that type as local Fortran "use ... only"
7400 declaration imports depend on the created type in determine_prefix. */
7401
7402static struct type *
7403read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7404{
7405 struct objfile *objfile = cu->objfile;
7406 char *module_name;
7407 struct type *type;
7408
7409 module_name = dwarf2_name (die, cu);
7410 if (!module_name)
7411 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7412 die->offset);
7413 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7414
7415 /* determine_prefix uses TYPE_TAG_NAME. */
7416 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7417
7418 return set_die_type (die, type, cu);
7419}
7420
5d7cb8df
JK
7421/* Read a Fortran module. */
7422
7423static void
7424read_module (struct die_info *die, struct dwarf2_cu *cu)
7425{
7426 struct die_info *child_die = die->child;
7427
5d7cb8df
JK
7428 while (child_die && child_die->tag)
7429 {
7430 process_die (child_die, cu);
7431 child_die = sibling_die (child_die);
7432 }
7433}
7434
38d518c9
EZ
7435/* Return the name of the namespace represented by DIE. Set
7436 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7437 namespace. */
7438
7439static const char *
e142c38c 7440namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7441{
7442 struct die_info *current_die;
7443 const char *name = NULL;
7444
7445 /* Loop through the extensions until we find a name. */
7446
7447 for (current_die = die;
7448 current_die != NULL;
f2f0e013 7449 current_die = dwarf2_extension (die, &cu))
38d518c9 7450 {
e142c38c 7451 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7452 if (name != NULL)
7453 break;
7454 }
7455
7456 /* Is it an anonymous namespace? */
7457
7458 *is_anonymous = (name == NULL);
7459 if (*is_anonymous)
7460 name = "(anonymous namespace)";
7461
7462 return name;
d9fa45fe
DC
7463}
7464
c906108c
SS
7465/* Extract all information from a DW_TAG_pointer_type DIE and add to
7466 the user defined type vector. */
7467
f792889a 7468static struct type *
e7c27a73 7469read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7470{
5e2b427d 7471 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7472 struct comp_unit_head *cu_header = &cu->header;
c906108c 7473 struct type *type;
8b2dbe47
KB
7474 struct attribute *attr_byte_size;
7475 struct attribute *attr_address_class;
7476 int byte_size, addr_class;
7e314c57
JK
7477 struct type *target_type;
7478
7479 target_type = die_type (die, cu);
c906108c 7480
7e314c57
JK
7481 /* The die_type call above may have already set the type for this DIE. */
7482 type = get_die_type (die, cu);
7483 if (type)
7484 return type;
7485
7486 type = lookup_pointer_type (target_type);
8b2dbe47 7487
e142c38c 7488 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7489 if (attr_byte_size)
7490 byte_size = DW_UNSND (attr_byte_size);
c906108c 7491 else
8b2dbe47
KB
7492 byte_size = cu_header->addr_size;
7493
e142c38c 7494 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7495 if (attr_address_class)
7496 addr_class = DW_UNSND (attr_address_class);
7497 else
7498 addr_class = DW_ADDR_none;
7499
7500 /* If the pointer size or address class is different than the
7501 default, create a type variant marked as such and set the
7502 length accordingly. */
7503 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7504 {
5e2b427d 7505 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7506 {
7507 int type_flags;
7508
849957d9 7509 type_flags = gdbarch_address_class_type_flags
5e2b427d 7510 (gdbarch, byte_size, addr_class);
876cecd0
TT
7511 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7512 == 0);
8b2dbe47
KB
7513 type = make_type_with_address_space (type, type_flags);
7514 }
7515 else if (TYPE_LENGTH (type) != byte_size)
7516 {
e2e0b3e5 7517 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7518 }
6e70227d 7519 else
9a619af0
MS
7520 {
7521 /* Should we also complain about unhandled address classes? */
7522 }
c906108c 7523 }
8b2dbe47
KB
7524
7525 TYPE_LENGTH (type) = byte_size;
f792889a 7526 return set_die_type (die, type, cu);
c906108c
SS
7527}
7528
7529/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7530 the user defined type vector. */
7531
f792889a 7532static struct type *
e7c27a73 7533read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7534{
7535 struct type *type;
7536 struct type *to_type;
7537 struct type *domain;
7538
e7c27a73
DJ
7539 to_type = die_type (die, cu);
7540 domain = die_containing_type (die, cu);
0d5de010 7541
7e314c57
JK
7542 /* The calls above may have already set the type for this DIE. */
7543 type = get_die_type (die, cu);
7544 if (type)
7545 return type;
7546
0d5de010
DJ
7547 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7548 type = lookup_methodptr_type (to_type);
7549 else
7550 type = lookup_memberptr_type (to_type, domain);
c906108c 7551
f792889a 7552 return set_die_type (die, type, cu);
c906108c
SS
7553}
7554
7555/* Extract all information from a DW_TAG_reference_type DIE and add to
7556 the user defined type vector. */
7557
f792889a 7558static struct type *
e7c27a73 7559read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7560{
e7c27a73 7561 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7562 struct type *type, *target_type;
c906108c
SS
7563 struct attribute *attr;
7564
7e314c57
JK
7565 target_type = die_type (die, cu);
7566
7567 /* The die_type call above may have already set the type for this DIE. */
7568 type = get_die_type (die, cu);
7569 if (type)
7570 return type;
7571
7572 type = lookup_reference_type (target_type);
e142c38c 7573 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7574 if (attr)
7575 {
7576 TYPE_LENGTH (type) = DW_UNSND (attr);
7577 }
7578 else
7579 {
107d2387 7580 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7581 }
f792889a 7582 return set_die_type (die, type, cu);
c906108c
SS
7583}
7584
f792889a 7585static struct type *
e7c27a73 7586read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7587{
f792889a 7588 struct type *base_type, *cv_type;
c906108c 7589
e7c27a73 7590 base_type = die_type (die, cu);
7e314c57
JK
7591
7592 /* The die_type call above may have already set the type for this DIE. */
7593 cv_type = get_die_type (die, cu);
7594 if (cv_type)
7595 return cv_type;
7596
f792889a
DJ
7597 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7598 return set_die_type (die, cv_type, cu);
c906108c
SS
7599}
7600
f792889a 7601static struct type *
e7c27a73 7602read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7603{
f792889a 7604 struct type *base_type, *cv_type;
c906108c 7605
e7c27a73 7606 base_type = die_type (die, cu);
7e314c57
JK
7607
7608 /* The die_type call above may have already set the type for this DIE. */
7609 cv_type = get_die_type (die, cu);
7610 if (cv_type)
7611 return cv_type;
7612
f792889a
DJ
7613 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7614 return set_die_type (die, cv_type, cu);
c906108c
SS
7615}
7616
7617/* Extract all information from a DW_TAG_string_type DIE and add to
7618 the user defined type vector. It isn't really a user defined type,
7619 but it behaves like one, with other DIE's using an AT_user_def_type
7620 attribute to reference it. */
7621
f792889a 7622static struct type *
e7c27a73 7623read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7624{
e7c27a73 7625 struct objfile *objfile = cu->objfile;
3b7538c0 7626 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7627 struct type *type, *range_type, *index_type, *char_type;
7628 struct attribute *attr;
7629 unsigned int length;
7630
e142c38c 7631 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7632 if (attr)
7633 {
7634 length = DW_UNSND (attr);
7635 }
7636 else
7637 {
b21b22e0 7638 /* check for the DW_AT_byte_size attribute */
e142c38c 7639 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7640 if (attr)
7641 {
7642 length = DW_UNSND (attr);
7643 }
7644 else
7645 {
7646 length = 1;
7647 }
c906108c 7648 }
6ccb9162 7649
46bf5051 7650 index_type = objfile_type (objfile)->builtin_int;
c906108c 7651 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7652 char_type = language_string_char_type (cu->language_defn, gdbarch);
7653 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7654
f792889a 7655 return set_die_type (die, type, cu);
c906108c
SS
7656}
7657
7658/* Handle DIES due to C code like:
7659
7660 struct foo
c5aa993b
JM
7661 {
7662 int (*funcp)(int a, long l);
7663 int b;
7664 };
c906108c
SS
7665
7666 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7667 */
c906108c 7668
f792889a 7669static struct type *
e7c27a73 7670read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7671{
7672 struct type *type; /* Type that this function returns */
7673 struct type *ftype; /* Function that returns above type */
7674 struct attribute *attr;
7675
e7c27a73 7676 type = die_type (die, cu);
7e314c57
JK
7677
7678 /* The die_type call above may have already set the type for this DIE. */
7679 ftype = get_die_type (die, cu);
7680 if (ftype)
7681 return ftype;
7682
0c8b41f1 7683 ftype = lookup_function_type (type);
c906108c 7684
5b8101ae 7685 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7686 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7687 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7688 || cu->language == language_cplus
5b8101ae
PM
7689 || cu->language == language_java
7690 || cu->language == language_pascal)
876cecd0 7691 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7692 else if (producer_is_realview (cu->producer))
7693 /* RealView does not emit DW_AT_prototyped. We can not
7694 distinguish prototyped and unprototyped functions; default to
7695 prototyped, since that is more common in modern code (and
7696 RealView warns about unprototyped functions). */
7697 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7698
c055b101
CV
7699 /* Store the calling convention in the type if it's available in
7700 the subroutine die. Otherwise set the calling convention to
7701 the default value DW_CC_normal. */
7702 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7703 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7704
7705 /* We need to add the subroutine type to the die immediately so
7706 we don't infinitely recurse when dealing with parameters
7707 declared as the same subroutine type. */
7708 set_die_type (die, ftype, cu);
6e70227d 7709
639d11d3 7710 if (die->child != NULL)
c906108c 7711 {
8072405b 7712 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7713 struct die_info *child_die;
8072405b 7714 int nparams, iparams;
c906108c
SS
7715
7716 /* Count the number of parameters.
7717 FIXME: GDB currently ignores vararg functions, but knows about
7718 vararg member functions. */
8072405b 7719 nparams = 0;
639d11d3 7720 child_die = die->child;
c906108c
SS
7721 while (child_die && child_die->tag)
7722 {
7723 if (child_die->tag == DW_TAG_formal_parameter)
7724 nparams++;
7725 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7726 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7727 child_die = sibling_die (child_die);
7728 }
7729
7730 /* Allocate storage for parameters and fill them in. */
7731 TYPE_NFIELDS (ftype) = nparams;
7732 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7733 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7734
8072405b
JK
7735 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7736 even if we error out during the parameters reading below. */
7737 for (iparams = 0; iparams < nparams; iparams++)
7738 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7739
7740 iparams = 0;
639d11d3 7741 child_die = die->child;
c906108c
SS
7742 while (child_die && child_die->tag)
7743 {
7744 if (child_die->tag == DW_TAG_formal_parameter)
7745 {
3ce3b1ba
PA
7746 struct type *arg_type;
7747
7748 /* DWARF version 2 has no clean way to discern C++
7749 static and non-static member functions. G++ helps
7750 GDB by marking the first parameter for non-static
7751 member functions (which is the this pointer) as
7752 artificial. We pass this information to
7753 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7754
7755 DWARF version 3 added DW_AT_object_pointer, which GCC
7756 4.5 does not yet generate. */
e142c38c 7757 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7758 if (attr)
7759 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7760 else
418835cc
KS
7761 {
7762 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7763
7764 /* GCC/43521: In java, the formal parameter
7765 "this" is sometimes not marked with DW_AT_artificial. */
7766 if (cu->language == language_java)
7767 {
7768 const char *name = dwarf2_name (child_die, cu);
9a619af0 7769
418835cc
KS
7770 if (name && !strcmp (name, "this"))
7771 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7772 }
7773 }
3ce3b1ba
PA
7774 arg_type = die_type (child_die, cu);
7775
7776 /* RealView does not mark THIS as const, which the testsuite
7777 expects. GCC marks THIS as const in method definitions,
7778 but not in the class specifications (GCC PR 43053). */
7779 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7780 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7781 {
7782 int is_this = 0;
7783 struct dwarf2_cu *arg_cu = cu;
7784 const char *name = dwarf2_name (child_die, cu);
7785
7786 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7787 if (attr)
7788 {
7789 /* If the compiler emits this, use it. */
7790 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7791 is_this = 1;
7792 }
7793 else if (name && strcmp (name, "this") == 0)
7794 /* Function definitions will have the argument names. */
7795 is_this = 1;
7796 else if (name == NULL && iparams == 0)
7797 /* Declarations may not have the names, so like
7798 elsewhere in GDB, assume an artificial first
7799 argument is "this". */
7800 is_this = 1;
7801
7802 if (is_this)
7803 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7804 arg_type, 0);
7805 }
7806
7807 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7808 iparams++;
7809 }
7810 child_die = sibling_die (child_die);
7811 }
7812 }
7813
76c10ea2 7814 return ftype;
c906108c
SS
7815}
7816
f792889a 7817static struct type *
e7c27a73 7818read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7819{
e7c27a73 7820 struct objfile *objfile = cu->objfile;
0114d602 7821 const char *name = NULL;
f792889a 7822 struct type *this_type;
c906108c 7823
94af9270 7824 name = dwarf2_full_name (NULL, die, cu);
f792889a 7825 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7826 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7827 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7828 set_die_type (die, this_type, cu);
7829 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7830 return this_type;
c906108c
SS
7831}
7832
7833/* Find a representation of a given base type and install
7834 it in the TYPE field of the die. */
7835
f792889a 7836static struct type *
e7c27a73 7837read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7838{
e7c27a73 7839 struct objfile *objfile = cu->objfile;
c906108c
SS
7840 struct type *type;
7841 struct attribute *attr;
7842 int encoding = 0, size = 0;
39cbfefa 7843 char *name;
6ccb9162
UW
7844 enum type_code code = TYPE_CODE_INT;
7845 int type_flags = 0;
7846 struct type *target_type = NULL;
c906108c 7847
e142c38c 7848 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7849 if (attr)
7850 {
7851 encoding = DW_UNSND (attr);
7852 }
e142c38c 7853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7854 if (attr)
7855 {
7856 size = DW_UNSND (attr);
7857 }
39cbfefa 7858 name = dwarf2_name (die, cu);
6ccb9162 7859 if (!name)
c906108c 7860 {
6ccb9162
UW
7861 complaint (&symfile_complaints,
7862 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7863 }
6ccb9162
UW
7864
7865 switch (encoding)
c906108c 7866 {
6ccb9162
UW
7867 case DW_ATE_address:
7868 /* Turn DW_ATE_address into a void * pointer. */
7869 code = TYPE_CODE_PTR;
7870 type_flags |= TYPE_FLAG_UNSIGNED;
7871 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7872 break;
7873 case DW_ATE_boolean:
7874 code = TYPE_CODE_BOOL;
7875 type_flags |= TYPE_FLAG_UNSIGNED;
7876 break;
7877 case DW_ATE_complex_float:
7878 code = TYPE_CODE_COMPLEX;
7879 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7880 break;
7881 case DW_ATE_decimal_float:
7882 code = TYPE_CODE_DECFLOAT;
7883 break;
7884 case DW_ATE_float:
7885 code = TYPE_CODE_FLT;
7886 break;
7887 case DW_ATE_signed:
7888 break;
7889 case DW_ATE_unsigned:
7890 type_flags |= TYPE_FLAG_UNSIGNED;
7891 break;
7892 case DW_ATE_signed_char:
6e70227d 7893 if (cu->language == language_ada || cu->language == language_m2
868a0084 7894 || cu->language == language_pascal)
6ccb9162
UW
7895 code = TYPE_CODE_CHAR;
7896 break;
7897 case DW_ATE_unsigned_char:
868a0084
PM
7898 if (cu->language == language_ada || cu->language == language_m2
7899 || cu->language == language_pascal)
6ccb9162
UW
7900 code = TYPE_CODE_CHAR;
7901 type_flags |= TYPE_FLAG_UNSIGNED;
7902 break;
75079b2b
TT
7903 case DW_ATE_UTF:
7904 /* We just treat this as an integer and then recognize the
7905 type by name elsewhere. */
7906 break;
7907
6ccb9162
UW
7908 default:
7909 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7910 dwarf_type_encoding_name (encoding));
7911 break;
c906108c 7912 }
6ccb9162 7913
0114d602
DJ
7914 type = init_type (code, size, type_flags, NULL, objfile);
7915 TYPE_NAME (type) = name;
6ccb9162
UW
7916 TYPE_TARGET_TYPE (type) = target_type;
7917
0114d602 7918 if (name && strcmp (name, "char") == 0)
876cecd0 7919 TYPE_NOSIGN (type) = 1;
0114d602 7920
f792889a 7921 return set_die_type (die, type, cu);
c906108c
SS
7922}
7923
a02abb62
JB
7924/* Read the given DW_AT_subrange DIE. */
7925
f792889a 7926static struct type *
a02abb62
JB
7927read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7928{
5e2b427d 7929 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7930 struct type *base_type;
7931 struct type *range_type;
7932 struct attribute *attr;
43bbcdc2
PH
7933 LONGEST low = 0;
7934 LONGEST high = -1;
39cbfefa 7935 char *name;
43bbcdc2 7936 LONGEST negative_mask;
e77813c8 7937
a02abb62 7938 base_type = die_type (die, cu);
953ac07e
JK
7939 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7940 check_typedef (base_type);
a02abb62 7941
7e314c57
JK
7942 /* The die_type call above may have already set the type for this DIE. */
7943 range_type = get_die_type (die, cu);
7944 if (range_type)
7945 return range_type;
7946
e142c38c 7947 if (cu->language == language_fortran)
6e70227d 7948 {
a02abb62
JB
7949 /* FORTRAN implies a lower bound of 1, if not given. */
7950 low = 1;
7951 }
7952
dd5e6932
DJ
7953 /* FIXME: For variable sized arrays either of these could be
7954 a variable rather than a constant value. We'll allow it,
7955 but we don't know how to handle it. */
e142c38c 7956 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7957 if (attr)
7958 low = dwarf2_get_attr_constant_value (attr, 0);
7959
e142c38c 7960 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7961 if (attr)
6e70227d 7962 {
e77813c8 7963 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7964 {
7965 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7966 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7967 FIXME: GDB does not yet know how to handle dynamic
7968 arrays properly, treat them as arrays with unspecified
7969 length for now.
7970
7971 FIXME: jimb/2003-09-22: GDB does not really know
7972 how to handle arrays of unspecified length
7973 either; we just represent them as zero-length
7974 arrays. Choose an appropriate upper bound given
7975 the lower bound we've computed above. */
7976 high = low - 1;
7977 }
7978 else
7979 high = dwarf2_get_attr_constant_value (attr, 1);
7980 }
e77813c8
PM
7981 else
7982 {
7983 attr = dwarf2_attr (die, DW_AT_count, cu);
7984 if (attr)
7985 {
7986 int count = dwarf2_get_attr_constant_value (attr, 1);
7987 high = low + count - 1;
7988 }
7989 }
7990
7991 /* Dwarf-2 specifications explicitly allows to create subrange types
7992 without specifying a base type.
7993 In that case, the base type must be set to the type of
7994 the lower bound, upper bound or count, in that order, if any of these
7995 three attributes references an object that has a type.
7996 If no base type is found, the Dwarf-2 specifications say that
7997 a signed integer type of size equal to the size of an address should
7998 be used.
7999 For the following C code: `extern char gdb_int [];'
8000 GCC produces an empty range DIE.
8001 FIXME: muller/2010-05-28: Possible references to object for low bound,
8002 high bound or count are not yet handled by this code.
8003 */
8004 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8005 {
8006 struct objfile *objfile = cu->objfile;
8007 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8008 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8009 struct type *int_type = objfile_type (objfile)->builtin_int;
8010
8011 /* Test "int", "long int", and "long long int" objfile types,
8012 and select the first one having a size above or equal to the
8013 architecture address size. */
8014 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8015 base_type = int_type;
8016 else
8017 {
8018 int_type = objfile_type (objfile)->builtin_long;
8019 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8020 base_type = int_type;
8021 else
8022 {
8023 int_type = objfile_type (objfile)->builtin_long_long;
8024 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8025 base_type = int_type;
8026 }
8027 }
8028 }
a02abb62 8029
6e70227d 8030 negative_mask =
43bbcdc2
PH
8031 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8032 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8033 low |= negative_mask;
8034 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8035 high |= negative_mask;
8036
a02abb62
JB
8037 range_type = create_range_type (NULL, base_type, low, high);
8038
bbb0eef6
JK
8039 /* Mark arrays with dynamic length at least as an array of unspecified
8040 length. GDB could check the boundary but before it gets implemented at
8041 least allow accessing the array elements. */
8042 if (attr && attr->form == DW_FORM_block1)
8043 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8044
39cbfefa
DJ
8045 name = dwarf2_name (die, cu);
8046 if (name)
8047 TYPE_NAME (range_type) = name;
6e70227d 8048
e142c38c 8049 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8050 if (attr)
8051 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8052
7e314c57
JK
8053 set_die_type (die, range_type, cu);
8054
8055 /* set_die_type should be already done. */
b4ba55a1
JB
8056 set_descriptive_type (range_type, die, cu);
8057
7e314c57 8058 return range_type;
a02abb62 8059}
6e70227d 8060
f792889a 8061static struct type *
81a17f79
JB
8062read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8063{
8064 struct type *type;
81a17f79 8065
81a17f79
JB
8066 /* For now, we only support the C meaning of an unspecified type: void. */
8067
0114d602
DJ
8068 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8069 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8070
f792889a 8071 return set_die_type (die, type, cu);
81a17f79 8072}
a02abb62 8073
51545339
DJ
8074/* Trivial hash function for die_info: the hash value of a DIE
8075 is its offset in .debug_info for this objfile. */
8076
8077static hashval_t
8078die_hash (const void *item)
8079{
8080 const struct die_info *die = item;
9a619af0 8081
51545339
DJ
8082 return die->offset;
8083}
8084
8085/* Trivial comparison function for die_info structures: two DIEs
8086 are equal if they have the same offset. */
8087
8088static int
8089die_eq (const void *item_lhs, const void *item_rhs)
8090{
8091 const struct die_info *die_lhs = item_lhs;
8092 const struct die_info *die_rhs = item_rhs;
9a619af0 8093
51545339
DJ
8094 return die_lhs->offset == die_rhs->offset;
8095}
8096
c906108c
SS
8097/* Read a whole compilation unit into a linked list of dies. */
8098
f9aca02d 8099static struct die_info *
93311388 8100read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8101{
93311388 8102 struct die_reader_specs reader_specs;
98bfdba5 8103 int read_abbrevs = 0;
1d9ec526 8104 struct cleanup *back_to = NULL;
98bfdba5
PA
8105 struct die_info *die;
8106
8107 if (cu->dwarf2_abbrevs == NULL)
8108 {
8109 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8110 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8111 read_abbrevs = 1;
8112 }
93311388 8113
348e048f 8114 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8115 cu->die_hash
8116 = htab_create_alloc_ex (cu->header.length / 12,
8117 die_hash,
8118 die_eq,
8119 NULL,
8120 &cu->comp_unit_obstack,
8121 hashtab_obstack_allocate,
8122 dummy_obstack_deallocate);
8123
93311388
DE
8124 init_cu_die_reader (&reader_specs, cu);
8125
98bfdba5
PA
8126 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8127
8128 if (read_abbrevs)
8129 do_cleanups (back_to);
8130
8131 return die;
639d11d3
DC
8132}
8133
d97bc12b
DE
8134/* Main entry point for reading a DIE and all children.
8135 Read the DIE and dump it if requested. */
8136
8137static struct die_info *
93311388
DE
8138read_die_and_children (const struct die_reader_specs *reader,
8139 gdb_byte *info_ptr,
d97bc12b
DE
8140 gdb_byte **new_info_ptr,
8141 struct die_info *parent)
8142{
93311388 8143 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8144 new_info_ptr, parent);
8145
8146 if (dwarf2_die_debug)
8147 {
348e048f
DE
8148 fprintf_unfiltered (gdb_stdlog,
8149 "\nRead die from %s of %s:\n",
8150 reader->buffer == dwarf2_per_objfile->info.buffer
8151 ? ".debug_info"
8152 : reader->buffer == dwarf2_per_objfile->types.buffer
8153 ? ".debug_types"
8154 : "unknown section",
8155 reader->abfd->filename);
d97bc12b
DE
8156 dump_die (result, dwarf2_die_debug);
8157 }
8158
8159 return result;
8160}
8161
639d11d3
DC
8162/* Read a single die and all its descendents. Set the die's sibling
8163 field to NULL; set other fields in the die correctly, and set all
8164 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8165 location of the info_ptr after reading all of those dies. PARENT
8166 is the parent of the die in question. */
8167
8168static struct die_info *
93311388
DE
8169read_die_and_children_1 (const struct die_reader_specs *reader,
8170 gdb_byte *info_ptr,
d97bc12b
DE
8171 gdb_byte **new_info_ptr,
8172 struct die_info *parent)
639d11d3
DC
8173{
8174 struct die_info *die;
fe1b8b76 8175 gdb_byte *cur_ptr;
639d11d3
DC
8176 int has_children;
8177
93311388 8178 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8179 if (die == NULL)
8180 {
8181 *new_info_ptr = cur_ptr;
8182 return NULL;
8183 }
93311388 8184 store_in_ref_table (die, reader->cu);
639d11d3
DC
8185
8186 if (has_children)
348e048f 8187 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8188 else
8189 {
8190 die->child = NULL;
8191 *new_info_ptr = cur_ptr;
8192 }
8193
8194 die->sibling = NULL;
8195 die->parent = parent;
8196 return die;
8197}
8198
8199/* Read a die, all of its descendents, and all of its siblings; set
8200 all of the fields of all of the dies correctly. Arguments are as
8201 in read_die_and_children. */
8202
8203static struct die_info *
93311388
DE
8204read_die_and_siblings (const struct die_reader_specs *reader,
8205 gdb_byte *info_ptr,
fe1b8b76 8206 gdb_byte **new_info_ptr,
639d11d3
DC
8207 struct die_info *parent)
8208{
8209 struct die_info *first_die, *last_sibling;
fe1b8b76 8210 gdb_byte *cur_ptr;
639d11d3 8211
c906108c 8212 cur_ptr = info_ptr;
639d11d3
DC
8213 first_die = last_sibling = NULL;
8214
8215 while (1)
c906108c 8216 {
639d11d3 8217 struct die_info *die
93311388 8218 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8219
1d325ec1 8220 if (die == NULL)
c906108c 8221 {
639d11d3
DC
8222 *new_info_ptr = cur_ptr;
8223 return first_die;
c906108c 8224 }
1d325ec1
DJ
8225
8226 if (!first_die)
8227 first_die = die;
c906108c 8228 else
1d325ec1
DJ
8229 last_sibling->sibling = die;
8230
8231 last_sibling = die;
c906108c 8232 }
c906108c
SS
8233}
8234
93311388
DE
8235/* Read the die from the .debug_info section buffer. Set DIEP to
8236 point to a newly allocated die with its information, except for its
8237 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8238 whether the die has children or not. */
8239
8240static gdb_byte *
8241read_full_die (const struct die_reader_specs *reader,
8242 struct die_info **diep, gdb_byte *info_ptr,
8243 int *has_children)
8244{
8245 unsigned int abbrev_number, bytes_read, i, offset;
8246 struct abbrev_info *abbrev;
8247 struct die_info *die;
8248 struct dwarf2_cu *cu = reader->cu;
8249 bfd *abfd = reader->abfd;
8250
8251 offset = info_ptr - reader->buffer;
8252 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8253 info_ptr += bytes_read;
8254 if (!abbrev_number)
8255 {
8256 *diep = NULL;
8257 *has_children = 0;
8258 return info_ptr;
8259 }
8260
8261 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8262 if (!abbrev)
348e048f
DE
8263 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8264 abbrev_number,
8265 bfd_get_filename (abfd));
8266
93311388
DE
8267 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8268 die->offset = offset;
8269 die->tag = abbrev->tag;
8270 die->abbrev = abbrev_number;
8271
8272 die->num_attrs = abbrev->num_attrs;
8273
8274 for (i = 0; i < abbrev->num_attrs; ++i)
8275 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8276 abfd, info_ptr, cu);
8277
8278 *diep = die;
8279 *has_children = abbrev->has_children;
8280 return info_ptr;
8281}
8282
c906108c
SS
8283/* In DWARF version 2, the description of the debugging information is
8284 stored in a separate .debug_abbrev section. Before we read any
8285 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8286 in a hash table. This function also sets flags in CU describing
8287 the data found in the abbrev table. */
c906108c
SS
8288
8289static void
e7c27a73 8290dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8291{
e7c27a73 8292 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8293 gdb_byte *abbrev_ptr;
c906108c
SS
8294 struct abbrev_info *cur_abbrev;
8295 unsigned int abbrev_number, bytes_read, abbrev_name;
8296 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8297 struct attr_abbrev *cur_attrs;
8298 unsigned int allocated_attrs;
c906108c 8299
57349743 8300 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8301 obstack_init (&cu->abbrev_obstack);
8302 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8303 (ABBREV_HASH_SIZE
8304 * sizeof (struct abbrev_info *)));
8305 memset (cu->dwarf2_abbrevs, 0,
8306 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8307
be391dca
TT
8308 dwarf2_read_section (dwarf2_per_objfile->objfile,
8309 &dwarf2_per_objfile->abbrev);
dce234bc 8310 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8311 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8312 abbrev_ptr += bytes_read;
8313
f3dd6933
DJ
8314 allocated_attrs = ATTR_ALLOC_CHUNK;
8315 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8316
c906108c
SS
8317 /* loop until we reach an abbrev number of 0 */
8318 while (abbrev_number)
8319 {
f3dd6933 8320 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8321
8322 /* read in abbrev header */
8323 cur_abbrev->number = abbrev_number;
8324 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8325 abbrev_ptr += bytes_read;
8326 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8327 abbrev_ptr += 1;
8328
72bf9492
DJ
8329 if (cur_abbrev->tag == DW_TAG_namespace)
8330 cu->has_namespace_info = 1;
8331
c906108c
SS
8332 /* now read in declarations */
8333 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8334 abbrev_ptr += bytes_read;
8335 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8336 abbrev_ptr += bytes_read;
8337 while (abbrev_name)
8338 {
f3dd6933 8339 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8340 {
f3dd6933
DJ
8341 allocated_attrs += ATTR_ALLOC_CHUNK;
8342 cur_attrs
8343 = xrealloc (cur_attrs, (allocated_attrs
8344 * sizeof (struct attr_abbrev)));
c906108c 8345 }
ae038cb0
DJ
8346
8347 /* Record whether this compilation unit might have
8348 inter-compilation-unit references. If we don't know what form
8349 this attribute will have, then it might potentially be a
8350 DW_FORM_ref_addr, so we conservatively expect inter-CU
8351 references. */
8352
8353 if (abbrev_form == DW_FORM_ref_addr
8354 || abbrev_form == DW_FORM_indirect)
8355 cu->has_form_ref_addr = 1;
8356
f3dd6933
DJ
8357 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8358 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8359 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8360 abbrev_ptr += bytes_read;
8361 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8362 abbrev_ptr += bytes_read;
8363 }
8364
f3dd6933
DJ
8365 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8366 (cur_abbrev->num_attrs
8367 * sizeof (struct attr_abbrev)));
8368 memcpy (cur_abbrev->attrs, cur_attrs,
8369 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8370
c906108c 8371 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8372 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8373 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8374
8375 /* Get next abbreviation.
8376 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8377 always properly terminated with an abbrev number of 0.
8378 Exit loop if we encounter an abbreviation which we have
8379 already read (which means we are about to read the abbreviations
8380 for the next compile unit) or if the end of the abbreviation
8381 table is reached. */
dce234bc
PP
8382 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8383 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8384 break;
8385 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8386 abbrev_ptr += bytes_read;
e7c27a73 8387 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8388 break;
8389 }
f3dd6933
DJ
8390
8391 xfree (cur_attrs);
c906108c
SS
8392}
8393
f3dd6933 8394/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8395
c906108c 8396static void
f3dd6933 8397dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8398{
f3dd6933 8399 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8400
f3dd6933
DJ
8401 obstack_free (&cu->abbrev_obstack, NULL);
8402 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8403}
8404
8405/* Lookup an abbrev_info structure in the abbrev hash table. */
8406
8407static struct abbrev_info *
e7c27a73 8408dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8409{
8410 unsigned int hash_number;
8411 struct abbrev_info *abbrev;
8412
8413 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8414 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8415
8416 while (abbrev)
8417 {
8418 if (abbrev->number == number)
8419 return abbrev;
8420 else
8421 abbrev = abbrev->next;
8422 }
8423 return NULL;
8424}
8425
72bf9492
DJ
8426/* Returns nonzero if TAG represents a type that we might generate a partial
8427 symbol for. */
8428
8429static int
8430is_type_tag_for_partial (int tag)
8431{
8432 switch (tag)
8433 {
8434#if 0
8435 /* Some types that would be reasonable to generate partial symbols for,
8436 that we don't at present. */
8437 case DW_TAG_array_type:
8438 case DW_TAG_file_type:
8439 case DW_TAG_ptr_to_member_type:
8440 case DW_TAG_set_type:
8441 case DW_TAG_string_type:
8442 case DW_TAG_subroutine_type:
8443#endif
8444 case DW_TAG_base_type:
8445 case DW_TAG_class_type:
680b30c7 8446 case DW_TAG_interface_type:
72bf9492
DJ
8447 case DW_TAG_enumeration_type:
8448 case DW_TAG_structure_type:
8449 case DW_TAG_subrange_type:
8450 case DW_TAG_typedef:
8451 case DW_TAG_union_type:
8452 return 1;
8453 default:
8454 return 0;
8455 }
8456}
8457
8458/* Load all DIEs that are interesting for partial symbols into memory. */
8459
8460static struct partial_die_info *
93311388
DE
8461load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8462 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8463{
8464 struct partial_die_info *part_die;
8465 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8466 struct abbrev_info *abbrev;
8467 unsigned int bytes_read;
5afb4e99 8468 unsigned int load_all = 0;
72bf9492
DJ
8469
8470 int nesting_level = 1;
8471
8472 parent_die = NULL;
8473 last_die = NULL;
8474
5afb4e99
DJ
8475 if (cu->per_cu && cu->per_cu->load_all_dies)
8476 load_all = 1;
8477
72bf9492
DJ
8478 cu->partial_dies
8479 = htab_create_alloc_ex (cu->header.length / 12,
8480 partial_die_hash,
8481 partial_die_eq,
8482 NULL,
8483 &cu->comp_unit_obstack,
8484 hashtab_obstack_allocate,
8485 dummy_obstack_deallocate);
8486
8487 part_die = obstack_alloc (&cu->comp_unit_obstack,
8488 sizeof (struct partial_die_info));
8489
8490 while (1)
8491 {
8492 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8493
8494 /* A NULL abbrev means the end of a series of children. */
8495 if (abbrev == NULL)
8496 {
8497 if (--nesting_level == 0)
8498 {
8499 /* PART_DIE was probably the last thing allocated on the
8500 comp_unit_obstack, so we could call obstack_free
8501 here. We don't do that because the waste is small,
8502 and will be cleaned up when we're done with this
8503 compilation unit. This way, we're also more robust
8504 against other users of the comp_unit_obstack. */
8505 return first_die;
8506 }
8507 info_ptr += bytes_read;
8508 last_die = parent_die;
8509 parent_die = parent_die->die_parent;
8510 continue;
8511 }
8512
98bfdba5
PA
8513 /* Check for template arguments. We never save these; if
8514 they're seen, we just mark the parent, and go on our way. */
8515 if (parent_die != NULL
8516 && cu->language == language_cplus
8517 && (abbrev->tag == DW_TAG_template_type_param
8518 || abbrev->tag == DW_TAG_template_value_param))
8519 {
8520 parent_die->has_template_arguments = 1;
8521
8522 if (!load_all)
8523 {
8524 /* We don't need a partial DIE for the template argument. */
8525 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8526 cu);
8527 continue;
8528 }
8529 }
8530
8531 /* We only recurse into subprograms looking for template arguments.
8532 Skip their other children. */
8533 if (!load_all
8534 && cu->language == language_cplus
8535 && parent_die != NULL
8536 && parent_die->tag == DW_TAG_subprogram)
8537 {
8538 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8539 continue;
8540 }
8541
5afb4e99
DJ
8542 /* Check whether this DIE is interesting enough to save. Normally
8543 we would not be interested in members here, but there may be
8544 later variables referencing them via DW_AT_specification (for
8545 static members). */
8546 if (!load_all
8547 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8548 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8549 && abbrev->tag != DW_TAG_enumerator
8550 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8551 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8552 && abbrev->tag != DW_TAG_variable
5afb4e99 8553 && abbrev->tag != DW_TAG_namespace
f55ee35c 8554 && abbrev->tag != DW_TAG_module
5afb4e99 8555 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8556 {
8557 /* Otherwise we skip to the next sibling, if any. */
93311388 8558 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8559 continue;
8560 }
8561
93311388
DE
8562 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8563 buffer, info_ptr, cu);
72bf9492
DJ
8564
8565 /* This two-pass algorithm for processing partial symbols has a
8566 high cost in cache pressure. Thus, handle some simple cases
8567 here which cover the majority of C partial symbols. DIEs
8568 which neither have specification tags in them, nor could have
8569 specification tags elsewhere pointing at them, can simply be
8570 processed and discarded.
8571
8572 This segment is also optional; scan_partial_symbols and
8573 add_partial_symbol will handle these DIEs if we chain
8574 them in normally. When compilers which do not emit large
8575 quantities of duplicate debug information are more common,
8576 this code can probably be removed. */
8577
8578 /* Any complete simple types at the top level (pretty much all
8579 of them, for a language without namespaces), can be processed
8580 directly. */
8581 if (parent_die == NULL
8582 && part_die->has_specification == 0
8583 && part_die->is_declaration == 0
8584 && (part_die->tag == DW_TAG_typedef
8585 || part_die->tag == DW_TAG_base_type
8586 || part_die->tag == DW_TAG_subrange_type))
8587 {
8588 if (building_psymtab && part_die->name != NULL)
04a679b8 8589 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8590 VAR_DOMAIN, LOC_TYPEDEF,
8591 &cu->objfile->static_psymbols,
8592 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8593 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8594 continue;
8595 }
8596
8597 /* If we're at the second level, and we're an enumerator, and
8598 our parent has no specification (meaning possibly lives in a
8599 namespace elsewhere), then we can add the partial symbol now
8600 instead of queueing it. */
8601 if (part_die->tag == DW_TAG_enumerator
8602 && parent_die != NULL
8603 && parent_die->die_parent == NULL
8604 && parent_die->tag == DW_TAG_enumeration_type
8605 && parent_die->has_specification == 0)
8606 {
8607 if (part_die->name == NULL)
e2e0b3e5 8608 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8609 else if (building_psymtab)
04a679b8 8610 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8611 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8612 (cu->language == language_cplus
8613 || cu->language == language_java)
72bf9492
DJ
8614 ? &cu->objfile->global_psymbols
8615 : &cu->objfile->static_psymbols,
8616 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8617
93311388 8618 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8619 continue;
8620 }
8621
8622 /* We'll save this DIE so link it in. */
8623 part_die->die_parent = parent_die;
8624 part_die->die_sibling = NULL;
8625 part_die->die_child = NULL;
8626
8627 if (last_die && last_die == parent_die)
8628 last_die->die_child = part_die;
8629 else if (last_die)
8630 last_die->die_sibling = part_die;
8631
8632 last_die = part_die;
8633
8634 if (first_die == NULL)
8635 first_die = part_die;
8636
8637 /* Maybe add the DIE to the hash table. Not all DIEs that we
8638 find interesting need to be in the hash table, because we
8639 also have the parent/sibling/child chains; only those that we
8640 might refer to by offset later during partial symbol reading.
8641
8642 For now this means things that might have be the target of a
8643 DW_AT_specification, DW_AT_abstract_origin, or
8644 DW_AT_extension. DW_AT_extension will refer only to
8645 namespaces; DW_AT_abstract_origin refers to functions (and
8646 many things under the function DIE, but we do not recurse
8647 into function DIEs during partial symbol reading) and
8648 possibly variables as well; DW_AT_specification refers to
8649 declarations. Declarations ought to have the DW_AT_declaration
8650 flag. It happens that GCC forgets to put it in sometimes, but
8651 only for functions, not for types.
8652
8653 Adding more things than necessary to the hash table is harmless
8654 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8655 wasted time in find_partial_die, when we reread the compilation
8656 unit with load_all_dies set. */
72bf9492 8657
5afb4e99 8658 if (load_all
72929c62 8659 || abbrev->tag == DW_TAG_constant
5afb4e99 8660 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8661 || abbrev->tag == DW_TAG_variable
8662 || abbrev->tag == DW_TAG_namespace
8663 || part_die->is_declaration)
8664 {
8665 void **slot;
8666
8667 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8668 part_die->offset, INSERT);
8669 *slot = part_die;
8670 }
8671
8672 part_die = obstack_alloc (&cu->comp_unit_obstack,
8673 sizeof (struct partial_die_info));
8674
8675 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8676 we have no reason to follow the children of structures; for other
98bfdba5
PA
8677 languages we have to, so that we can get at method physnames
8678 to infer fully qualified class names, for DW_AT_specification,
8679 and for C++ template arguments. For C++, we also look one level
8680 inside functions to find template arguments (if the name of the
8681 function does not already contain the template arguments).
bc30ff58
JB
8682
8683 For Ada, we need to scan the children of subprograms and lexical
8684 blocks as well because Ada allows the definition of nested
8685 entities that could be interesting for the debugger, such as
8686 nested subprograms for instance. */
72bf9492 8687 if (last_die->has_children
5afb4e99
DJ
8688 && (load_all
8689 || last_die->tag == DW_TAG_namespace
f55ee35c 8690 || last_die->tag == DW_TAG_module
72bf9492 8691 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8692 || (cu->language == language_cplus
8693 && last_die->tag == DW_TAG_subprogram
8694 && (last_die->name == NULL
8695 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8696 || (cu->language != language_c
8697 && (last_die->tag == DW_TAG_class_type
680b30c7 8698 || last_die->tag == DW_TAG_interface_type
72bf9492 8699 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8700 || last_die->tag == DW_TAG_union_type))
8701 || (cu->language == language_ada
8702 && (last_die->tag == DW_TAG_subprogram
8703 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8704 {
8705 nesting_level++;
8706 parent_die = last_die;
8707 continue;
8708 }
8709
8710 /* Otherwise we skip to the next sibling, if any. */
93311388 8711 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8712
8713 /* Back to the top, do it again. */
8714 }
8715}
8716
c906108c
SS
8717/* Read a minimal amount of information into the minimal die structure. */
8718
fe1b8b76 8719static gdb_byte *
72bf9492
DJ
8720read_partial_die (struct partial_die_info *part_die,
8721 struct abbrev_info *abbrev,
8722 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8723 gdb_byte *buffer, gdb_byte *info_ptr,
8724 struct dwarf2_cu *cu)
c906108c 8725{
fa238c03 8726 unsigned int i;
c906108c 8727 struct attribute attr;
c5aa993b 8728 int has_low_pc_attr = 0;
c906108c
SS
8729 int has_high_pc_attr = 0;
8730
72bf9492 8731 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8732
93311388 8733 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8734
8735 info_ptr += abbrev_len;
8736
8737 if (abbrev == NULL)
8738 return info_ptr;
8739
c906108c
SS
8740 part_die->tag = abbrev->tag;
8741 part_die->has_children = abbrev->has_children;
c906108c
SS
8742
8743 for (i = 0; i < abbrev->num_attrs; ++i)
8744 {
e7c27a73 8745 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8746
8747 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8748 partial symbol table. */
c906108c
SS
8749 switch (attr.name)
8750 {
8751 case DW_AT_name:
71c25dea
TT
8752 switch (part_die->tag)
8753 {
8754 case DW_TAG_compile_unit:
348e048f 8755 case DW_TAG_type_unit:
71c25dea
TT
8756 /* Compilation units have a DW_AT_name that is a filename, not
8757 a source language identifier. */
8758 case DW_TAG_enumeration_type:
8759 case DW_TAG_enumerator:
8760 /* These tags always have simple identifiers already; no need
8761 to canonicalize them. */
8762 part_die->name = DW_STRING (&attr);
8763 break;
8764 default:
8765 part_die->name
8766 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8767 &cu->objfile->objfile_obstack);
71c25dea
TT
8768 break;
8769 }
c906108c 8770 break;
31ef98ae 8771 case DW_AT_linkage_name:
c906108c 8772 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8773 /* Note that both forms of linkage name might appear. We
8774 assume they will be the same, and we only store the last
8775 one we see. */
94af9270
KS
8776 if (cu->language == language_ada)
8777 part_die->name = DW_STRING (&attr);
abc72ce4 8778 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8779 break;
8780 case DW_AT_low_pc:
8781 has_low_pc_attr = 1;
8782 part_die->lowpc = DW_ADDR (&attr);
8783 break;
8784 case DW_AT_high_pc:
8785 has_high_pc_attr = 1;
8786 part_die->highpc = DW_ADDR (&attr);
8787 break;
8788 case DW_AT_location:
8e19ed76
PS
8789 /* Support the .debug_loc offsets */
8790 if (attr_form_is_block (&attr))
8791 {
8792 part_die->locdesc = DW_BLOCK (&attr);
8793 }
3690dd37 8794 else if (attr_form_is_section_offset (&attr))
8e19ed76 8795 {
4d3c2250 8796 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8797 }
8798 else
8799 {
4d3c2250
KB
8800 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8801 "partial symbol information");
8e19ed76 8802 }
c906108c 8803 break;
c906108c
SS
8804 case DW_AT_external:
8805 part_die->is_external = DW_UNSND (&attr);
8806 break;
8807 case DW_AT_declaration:
8808 part_die->is_declaration = DW_UNSND (&attr);
8809 break;
8810 case DW_AT_type:
8811 part_die->has_type = 1;
8812 break;
8813 case DW_AT_abstract_origin:
8814 case DW_AT_specification:
72bf9492
DJ
8815 case DW_AT_extension:
8816 part_die->has_specification = 1;
c764a876 8817 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8818 break;
8819 case DW_AT_sibling:
8820 /* Ignore absolute siblings, they might point outside of
8821 the current compile unit. */
8822 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8823 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8824 else
93311388 8825 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8826 break;
fa4028e9
JB
8827 case DW_AT_byte_size:
8828 part_die->has_byte_size = 1;
8829 break;
68511cec
CES
8830 case DW_AT_calling_convention:
8831 /* DWARF doesn't provide a way to identify a program's source-level
8832 entry point. DW_AT_calling_convention attributes are only meant
8833 to describe functions' calling conventions.
8834
8835 However, because it's a necessary piece of information in
8836 Fortran, and because DW_CC_program is the only piece of debugging
8837 information whose definition refers to a 'main program' at all,
8838 several compilers have begun marking Fortran main programs with
8839 DW_CC_program --- even when those functions use the standard
8840 calling conventions.
8841
8842 So until DWARF specifies a way to provide this information and
8843 compilers pick up the new representation, we'll support this
8844 practice. */
8845 if (DW_UNSND (&attr) == DW_CC_program
8846 && cu->language == language_fortran)
8847 set_main_name (part_die->name);
8848 break;
c906108c
SS
8849 default:
8850 break;
8851 }
8852 }
8853
c906108c
SS
8854 /* When using the GNU linker, .gnu.linkonce. sections are used to
8855 eliminate duplicate copies of functions and vtables and such.
8856 The linker will arbitrarily choose one and discard the others.
8857 The AT_*_pc values for such functions refer to local labels in
8858 these sections. If the section from that file was discarded, the
8859 labels are not in the output, so the relocs get a value of 0.
8860 If this is a discarded function, mark the pc bounds as invalid,
8861 so that GDB will ignore it. */
8862 if (has_low_pc_attr && has_high_pc_attr
8863 && part_die->lowpc < part_die->highpc
8864 && (part_die->lowpc != 0
72dca2f5 8865 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8866 part_die->has_pc_info = 1;
85cbf3d3 8867
c906108c
SS
8868 return info_ptr;
8869}
8870
72bf9492
DJ
8871/* Find a cached partial DIE at OFFSET in CU. */
8872
8873static struct partial_die_info *
c764a876 8874find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8875{
8876 struct partial_die_info *lookup_die = NULL;
8877 struct partial_die_info part_die;
8878
8879 part_die.offset = offset;
8880 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8881
72bf9492
DJ
8882 return lookup_die;
8883}
8884
348e048f
DE
8885/* Find a partial DIE at OFFSET, which may or may not be in CU,
8886 except in the case of .debug_types DIEs which do not reference
8887 outside their CU (they do however referencing other types via
8888 DW_FORM_sig8). */
72bf9492
DJ
8889
8890static struct partial_die_info *
c764a876 8891find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8892{
5afb4e99
DJ
8893 struct dwarf2_per_cu_data *per_cu = NULL;
8894 struct partial_die_info *pd = NULL;
72bf9492 8895
348e048f
DE
8896 if (cu->per_cu->from_debug_types)
8897 {
8898 pd = find_partial_die_in_comp_unit (offset, cu);
8899 if (pd != NULL)
8900 return pd;
8901 goto not_found;
8902 }
8903
45452591 8904 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8905 {
8906 pd = find_partial_die_in_comp_unit (offset, cu);
8907 if (pd != NULL)
8908 return pd;
8909 }
72bf9492 8910
ae038cb0
DJ
8911 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8912
98bfdba5
PA
8913 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8914 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8915
8916 per_cu->cu->last_used = 0;
5afb4e99
DJ
8917 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8918
8919 if (pd == NULL && per_cu->load_all_dies == 0)
8920 {
8921 struct cleanup *back_to;
8922 struct partial_die_info comp_unit_die;
8923 struct abbrev_info *abbrev;
8924 unsigned int bytes_read;
8925 char *info_ptr;
8926
8927 per_cu->load_all_dies = 1;
8928
8929 /* Re-read the DIEs. */
8930 back_to = make_cleanup (null_cleanup, 0);
8931 if (per_cu->cu->dwarf2_abbrevs == NULL)
8932 {
8933 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8934 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8935 }
dce234bc 8936 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8937 + per_cu->cu->header.offset
8938 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8939 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8940 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8941 per_cu->cu->objfile->obfd,
8942 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8943 per_cu->cu);
8944 if (comp_unit_die.has_children)
93311388
DE
8945 load_partial_dies (per_cu->cu->objfile->obfd,
8946 dwarf2_per_objfile->info.buffer, info_ptr,
8947 0, per_cu->cu);
5afb4e99
DJ
8948 do_cleanups (back_to);
8949
8950 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8951 }
8952
348e048f
DE
8953 not_found:
8954
5afb4e99
DJ
8955 if (pd == NULL)
8956 internal_error (__FILE__, __LINE__,
c764a876 8957 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8958 offset, bfd_get_filename (cu->objfile->obfd));
8959 return pd;
72bf9492
DJ
8960}
8961
abc72ce4
DE
8962/* See if we can figure out if the class lives in a namespace. We do
8963 this by looking for a member function; its demangled name will
8964 contain namespace info, if there is any. */
8965
8966static void
8967guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
8968 struct dwarf2_cu *cu)
8969{
8970 /* NOTE: carlton/2003-10-07: Getting the info this way changes
8971 what template types look like, because the demangler
8972 frequently doesn't give the same name as the debug info. We
8973 could fix this by only using the demangled name to get the
8974 prefix (but see comment in read_structure_type). */
8975
8976 struct partial_die_info *real_pdi;
8977 struct partial_die_info *child_pdi;
8978
8979 /* If this DIE (this DIE's specification, if any) has a parent, then
8980 we should not do this. We'll prepend the parent's fully qualified
8981 name when we create the partial symbol. */
8982
8983 real_pdi = struct_pdi;
8984 while (real_pdi->has_specification)
8985 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
8986
8987 if (real_pdi->die_parent != NULL)
8988 return;
8989
8990 for (child_pdi = struct_pdi->die_child;
8991 child_pdi != NULL;
8992 child_pdi = child_pdi->die_sibling)
8993 {
8994 if (child_pdi->tag == DW_TAG_subprogram
8995 && child_pdi->linkage_name != NULL)
8996 {
8997 char *actual_class_name
8998 = language_class_name_from_physname (cu->language_defn,
8999 child_pdi->linkage_name);
9000 if (actual_class_name != NULL)
9001 {
9002 struct_pdi->name
9003 = obsavestring (actual_class_name,
9004 strlen (actual_class_name),
9005 &cu->objfile->objfile_obstack);
9006 xfree (actual_class_name);
9007 }
9008 break;
9009 }
9010 }
9011}
9012
72bf9492
DJ
9013/* Adjust PART_DIE before generating a symbol for it. This function
9014 may set the is_external flag or change the DIE's name. */
9015
9016static void
9017fixup_partial_die (struct partial_die_info *part_die,
9018 struct dwarf2_cu *cu)
9019{
abc72ce4
DE
9020 /* Once we've fixed up a die, there's no point in doing so again.
9021 This also avoids a memory leak if we were to call
9022 guess_partial_die_structure_name multiple times. */
9023 if (part_die->fixup_called)
9024 return;
9025
72bf9492
DJ
9026 /* If we found a reference attribute and the DIE has no name, try
9027 to find a name in the referred to DIE. */
9028
9029 if (part_die->name == NULL && part_die->has_specification)
9030 {
9031 struct partial_die_info *spec_die;
72bf9492 9032
10b3939b 9033 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9034
10b3939b 9035 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9036
9037 if (spec_die->name)
9038 {
9039 part_die->name = spec_die->name;
9040
9041 /* Copy DW_AT_external attribute if it is set. */
9042 if (spec_die->is_external)
9043 part_die->is_external = spec_die->is_external;
9044 }
9045 }
9046
9047 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9048
9049 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9050 part_die->name = "(anonymous namespace)";
9051
abc72ce4
DE
9052 /* If there is no parent die to provide a namespace, and there are
9053 children, see if we can determine the namespace from their linkage
9054 name.
9055 NOTE: We need to do this even if cu->has_namespace_info != 0.
9056 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9057 if (cu->language == language_cplus
9058 && dwarf2_per_objfile->types.asection != NULL
9059 && part_die->die_parent == NULL
9060 && part_die->has_children
9061 && (part_die->tag == DW_TAG_class_type
9062 || part_die->tag == DW_TAG_structure_type
9063 || part_die->tag == DW_TAG_union_type))
9064 guess_partial_die_structure_name (part_die, cu);
9065
9066 part_die->fixup_called = 1;
72bf9492
DJ
9067}
9068
a8329558 9069/* Read an attribute value described by an attribute form. */
c906108c 9070
fe1b8b76 9071static gdb_byte *
a8329558 9072read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9073 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9074 struct dwarf2_cu *cu)
c906108c 9075{
e7c27a73 9076 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9077 unsigned int bytes_read;
9078 struct dwarf_block *blk;
9079
a8329558
KW
9080 attr->form = form;
9081 switch (form)
c906108c 9082 {
c906108c 9083 case DW_FORM_ref_addr:
ae411497
TT
9084 if (cu->header.version == 2)
9085 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9086 else
9087 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9088 info_ptr += bytes_read;
9089 break;
9090 case DW_FORM_addr:
e7c27a73 9091 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9092 info_ptr += bytes_read;
c906108c
SS
9093 break;
9094 case DW_FORM_block2:
7b5a2f43 9095 blk = dwarf_alloc_block (cu);
c906108c
SS
9096 blk->size = read_2_bytes (abfd, info_ptr);
9097 info_ptr += 2;
9098 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9099 info_ptr += blk->size;
9100 DW_BLOCK (attr) = blk;
9101 break;
9102 case DW_FORM_block4:
7b5a2f43 9103 blk = dwarf_alloc_block (cu);
c906108c
SS
9104 blk->size = read_4_bytes (abfd, info_ptr);
9105 info_ptr += 4;
9106 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9107 info_ptr += blk->size;
9108 DW_BLOCK (attr) = blk;
9109 break;
9110 case DW_FORM_data2:
9111 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9112 info_ptr += 2;
9113 break;
9114 case DW_FORM_data4:
9115 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9116 info_ptr += 4;
9117 break;
9118 case DW_FORM_data8:
9119 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9120 info_ptr += 8;
9121 break;
2dc7f7b3
TT
9122 case DW_FORM_sec_offset:
9123 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9124 info_ptr += bytes_read;
9125 break;
c906108c 9126 case DW_FORM_string:
9b1c24c8 9127 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9128 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9129 info_ptr += bytes_read;
9130 break;
4bdf3d34
JJ
9131 case DW_FORM_strp:
9132 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9133 &bytes_read);
8285870a 9134 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9135 info_ptr += bytes_read;
9136 break;
2dc7f7b3 9137 case DW_FORM_exprloc:
c906108c 9138 case DW_FORM_block:
7b5a2f43 9139 blk = dwarf_alloc_block (cu);
c906108c
SS
9140 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9141 info_ptr += bytes_read;
9142 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9143 info_ptr += blk->size;
9144 DW_BLOCK (attr) = blk;
9145 break;
9146 case DW_FORM_block1:
7b5a2f43 9147 blk = dwarf_alloc_block (cu);
c906108c
SS
9148 blk->size = read_1_byte (abfd, info_ptr);
9149 info_ptr += 1;
9150 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9151 info_ptr += blk->size;
9152 DW_BLOCK (attr) = blk;
9153 break;
9154 case DW_FORM_data1:
9155 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9156 info_ptr += 1;
9157 break;
9158 case DW_FORM_flag:
9159 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9160 info_ptr += 1;
9161 break;
2dc7f7b3
TT
9162 case DW_FORM_flag_present:
9163 DW_UNSND (attr) = 1;
9164 break;
c906108c
SS
9165 case DW_FORM_sdata:
9166 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9167 info_ptr += bytes_read;
9168 break;
9169 case DW_FORM_udata:
9170 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9171 info_ptr += bytes_read;
9172 break;
9173 case DW_FORM_ref1:
10b3939b 9174 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9175 info_ptr += 1;
9176 break;
9177 case DW_FORM_ref2:
10b3939b 9178 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9179 info_ptr += 2;
9180 break;
9181 case DW_FORM_ref4:
10b3939b 9182 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9183 info_ptr += 4;
9184 break;
613e1657 9185 case DW_FORM_ref8:
10b3939b 9186 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9187 info_ptr += 8;
9188 break;
348e048f
DE
9189 case DW_FORM_sig8:
9190 /* Convert the signature to something we can record in DW_UNSND
9191 for later lookup.
9192 NOTE: This is NULL if the type wasn't found. */
9193 DW_SIGNATURED_TYPE (attr) =
9194 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9195 info_ptr += 8;
9196 break;
c906108c 9197 case DW_FORM_ref_udata:
10b3939b
DJ
9198 DW_ADDR (attr) = (cu->header.offset
9199 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9200 info_ptr += bytes_read;
9201 break;
c906108c 9202 case DW_FORM_indirect:
a8329558
KW
9203 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9204 info_ptr += bytes_read;
e7c27a73 9205 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9206 break;
c906108c 9207 default:
8a3fe4f8 9208 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9209 dwarf_form_name (form),
9210 bfd_get_filename (abfd));
c906108c 9211 }
28e94949
JB
9212
9213 /* We have seen instances where the compiler tried to emit a byte
9214 size attribute of -1 which ended up being encoded as an unsigned
9215 0xffffffff. Although 0xffffffff is technically a valid size value,
9216 an object of this size seems pretty unlikely so we can relatively
9217 safely treat these cases as if the size attribute was invalid and
9218 treat them as zero by default. */
9219 if (attr->name == DW_AT_byte_size
9220 && form == DW_FORM_data4
9221 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9222 {
9223 complaint
9224 (&symfile_complaints,
43bbcdc2
PH
9225 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9226 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9227 DW_UNSND (attr) = 0;
9228 }
28e94949 9229
c906108c
SS
9230 return info_ptr;
9231}
9232
a8329558
KW
9233/* Read an attribute described by an abbreviated attribute. */
9234
fe1b8b76 9235static gdb_byte *
a8329558 9236read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9237 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9238{
9239 attr->name = abbrev->name;
e7c27a73 9240 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9241}
9242
c906108c
SS
9243/* read dwarf information from a buffer */
9244
9245static unsigned int
fe1b8b76 9246read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9247{
fe1b8b76 9248 return bfd_get_8 (abfd, buf);
c906108c
SS
9249}
9250
9251static int
fe1b8b76 9252read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9253{
fe1b8b76 9254 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9255}
9256
9257static unsigned int
fe1b8b76 9258read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9259{
fe1b8b76 9260 return bfd_get_16 (abfd, buf);
c906108c
SS
9261}
9262
9263static int
fe1b8b76 9264read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9265{
fe1b8b76 9266 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9267}
9268
9269static unsigned int
fe1b8b76 9270read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9271{
fe1b8b76 9272 return bfd_get_32 (abfd, buf);
c906108c
SS
9273}
9274
9275static int
fe1b8b76 9276read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9277{
fe1b8b76 9278 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9279}
9280
93311388 9281static ULONGEST
fe1b8b76 9282read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9283{
fe1b8b76 9284 return bfd_get_64 (abfd, buf);
c906108c
SS
9285}
9286
9287static CORE_ADDR
fe1b8b76 9288read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9289 unsigned int *bytes_read)
c906108c 9290{
e7c27a73 9291 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9292 CORE_ADDR retval = 0;
9293
107d2387 9294 if (cu_header->signed_addr_p)
c906108c 9295 {
107d2387
AC
9296 switch (cu_header->addr_size)
9297 {
9298 case 2:
fe1b8b76 9299 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9300 break;
9301 case 4:
fe1b8b76 9302 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9303 break;
9304 case 8:
fe1b8b76 9305 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9306 break;
9307 default:
8e65ff28 9308 internal_error (__FILE__, __LINE__,
e2e0b3e5 9309 _("read_address: bad switch, signed [in module %s]"),
659b0389 9310 bfd_get_filename (abfd));
107d2387
AC
9311 }
9312 }
9313 else
9314 {
9315 switch (cu_header->addr_size)
9316 {
9317 case 2:
fe1b8b76 9318 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9319 break;
9320 case 4:
fe1b8b76 9321 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9322 break;
9323 case 8:
fe1b8b76 9324 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9325 break;
9326 default:
8e65ff28 9327 internal_error (__FILE__, __LINE__,
e2e0b3e5 9328 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9329 bfd_get_filename (abfd));
107d2387 9330 }
c906108c 9331 }
64367e0a 9332
107d2387
AC
9333 *bytes_read = cu_header->addr_size;
9334 return retval;
c906108c
SS
9335}
9336
f7ef9339 9337/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9338 specification allows the initial length to take up either 4 bytes
9339 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9340 bytes describe the length and all offsets will be 8 bytes in length
9341 instead of 4.
9342
f7ef9339
KB
9343 An older, non-standard 64-bit format is also handled by this
9344 function. The older format in question stores the initial length
9345 as an 8-byte quantity without an escape value. Lengths greater
9346 than 2^32 aren't very common which means that the initial 4 bytes
9347 is almost always zero. Since a length value of zero doesn't make
9348 sense for the 32-bit format, this initial zero can be considered to
9349 be an escape value which indicates the presence of the older 64-bit
9350 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9351 greater than 4GB. If it becomes necessary to handle lengths
9352 somewhat larger than 4GB, we could allow other small values (such
9353 as the non-sensical values of 1, 2, and 3) to also be used as
9354 escape values indicating the presence of the old format.
f7ef9339 9355
917c78fc
MK
9356 The value returned via bytes_read should be used to increment the
9357 relevant pointer after calling read_initial_length().
c764a876 9358
613e1657
KB
9359 [ Note: read_initial_length() and read_offset() are based on the
9360 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9361 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9362 from:
9363
f7ef9339 9364 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9365
613e1657
KB
9366 This document is only a draft and is subject to change. (So beware.)
9367
f7ef9339 9368 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9369 determined empirically by examining 64-bit ELF files produced by
9370 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9371
9372 - Kevin, July 16, 2002
613e1657
KB
9373 ] */
9374
9375static LONGEST
c764a876 9376read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9377{
fe1b8b76 9378 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9379
dd373385 9380 if (length == 0xffffffff)
613e1657 9381 {
fe1b8b76 9382 length = bfd_get_64 (abfd, buf + 4);
613e1657 9383 *bytes_read = 12;
613e1657 9384 }
dd373385 9385 else if (length == 0)
f7ef9339 9386 {
dd373385 9387 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9388 length = bfd_get_64 (abfd, buf);
f7ef9339 9389 *bytes_read = 8;
f7ef9339 9390 }
613e1657
KB
9391 else
9392 {
9393 *bytes_read = 4;
613e1657
KB
9394 }
9395
c764a876
DE
9396 return length;
9397}
dd373385 9398
c764a876
DE
9399/* Cover function for read_initial_length.
9400 Returns the length of the object at BUF, and stores the size of the
9401 initial length in *BYTES_READ and stores the size that offsets will be in
9402 *OFFSET_SIZE.
9403 If the initial length size is not equivalent to that specified in
9404 CU_HEADER then issue a complaint.
9405 This is useful when reading non-comp-unit headers. */
dd373385 9406
c764a876
DE
9407static LONGEST
9408read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9409 const struct comp_unit_head *cu_header,
9410 unsigned int *bytes_read,
9411 unsigned int *offset_size)
9412{
9413 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9414
9415 gdb_assert (cu_header->initial_length_size == 4
9416 || cu_header->initial_length_size == 8
9417 || cu_header->initial_length_size == 12);
9418
9419 if (cu_header->initial_length_size != *bytes_read)
9420 complaint (&symfile_complaints,
9421 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9422
c764a876 9423 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9424 return length;
613e1657
KB
9425}
9426
9427/* Read an offset from the data stream. The size of the offset is
917c78fc 9428 given by cu_header->offset_size. */
613e1657
KB
9429
9430static LONGEST
fe1b8b76 9431read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9432 unsigned int *bytes_read)
c764a876
DE
9433{
9434 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9435
c764a876
DE
9436 *bytes_read = cu_header->offset_size;
9437 return offset;
9438}
9439
9440/* Read an offset from the data stream. */
9441
9442static LONGEST
9443read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9444{
9445 LONGEST retval = 0;
9446
c764a876 9447 switch (offset_size)
613e1657
KB
9448 {
9449 case 4:
fe1b8b76 9450 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9451 break;
9452 case 8:
fe1b8b76 9453 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9454 break;
9455 default:
8e65ff28 9456 internal_error (__FILE__, __LINE__,
c764a876 9457 _("read_offset_1: bad switch [in module %s]"),
659b0389 9458 bfd_get_filename (abfd));
613e1657
KB
9459 }
9460
917c78fc 9461 return retval;
613e1657
KB
9462}
9463
fe1b8b76
JB
9464static gdb_byte *
9465read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9466{
9467 /* If the size of a host char is 8 bits, we can return a pointer
9468 to the buffer, otherwise we have to copy the data to a buffer
9469 allocated on the temporary obstack. */
4bdf3d34 9470 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9471 return buf;
c906108c
SS
9472}
9473
9474static char *
9b1c24c8 9475read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9476{
9477 /* If the size of a host char is 8 bits, we can return a pointer
9478 to the string, otherwise we have to copy the string to a buffer
9479 allocated on the temporary obstack. */
4bdf3d34 9480 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9481 if (*buf == '\0')
9482 {
9483 *bytes_read_ptr = 1;
9484 return NULL;
9485 }
fe1b8b76
JB
9486 *bytes_read_ptr = strlen ((char *) buf) + 1;
9487 return (char *) buf;
4bdf3d34
JJ
9488}
9489
9490static char *
fe1b8b76 9491read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9492 const struct comp_unit_head *cu_header,
9493 unsigned int *bytes_read_ptr)
9494{
c764a876 9495 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9496
be391dca 9497 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9498 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9499 {
8a3fe4f8 9500 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9501 bfd_get_filename (abfd));
4bdf3d34 9502 return NULL;
c906108c 9503 }
dce234bc 9504 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9505 {
8a3fe4f8 9506 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9507 bfd_get_filename (abfd));
c906108c
SS
9508 return NULL;
9509 }
4bdf3d34 9510 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9511 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9512 return NULL;
dce234bc 9513 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9514}
9515
ce5d95e1 9516static unsigned long
fe1b8b76 9517read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9518{
ce5d95e1
JB
9519 unsigned long result;
9520 unsigned int num_read;
c906108c
SS
9521 int i, shift;
9522 unsigned char byte;
9523
9524 result = 0;
9525 shift = 0;
9526 num_read = 0;
9527 i = 0;
9528 while (1)
9529 {
fe1b8b76 9530 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9531 buf++;
9532 num_read++;
ce5d95e1 9533 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9534 if ((byte & 128) == 0)
9535 {
9536 break;
9537 }
9538 shift += 7;
9539 }
9540 *bytes_read_ptr = num_read;
9541 return result;
9542}
9543
ce5d95e1 9544static long
fe1b8b76 9545read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9546{
ce5d95e1 9547 long result;
77e0b926 9548 int i, shift, num_read;
c906108c
SS
9549 unsigned char byte;
9550
9551 result = 0;
9552 shift = 0;
c906108c
SS
9553 num_read = 0;
9554 i = 0;
9555 while (1)
9556 {
fe1b8b76 9557 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9558 buf++;
9559 num_read++;
ce5d95e1 9560 result |= ((long)(byte & 127) << shift);
c906108c
SS
9561 shift += 7;
9562 if ((byte & 128) == 0)
9563 {
9564 break;
9565 }
9566 }
77e0b926
DJ
9567 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9568 result |= -(((long)1) << shift);
c906108c
SS
9569 *bytes_read_ptr = num_read;
9570 return result;
9571}
9572
4bb7a0a7
DJ
9573/* Return a pointer to just past the end of an LEB128 number in BUF. */
9574
fe1b8b76
JB
9575static gdb_byte *
9576skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9577{
9578 int byte;
9579
9580 while (1)
9581 {
fe1b8b76 9582 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9583 buf++;
9584 if ((byte & 128) == 0)
9585 return buf;
9586 }
9587}
9588
c906108c 9589static void
e142c38c 9590set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9591{
9592 switch (lang)
9593 {
9594 case DW_LANG_C89:
76bee0cc 9595 case DW_LANG_C99:
c906108c 9596 case DW_LANG_C:
e142c38c 9597 cu->language = language_c;
c906108c
SS
9598 break;
9599 case DW_LANG_C_plus_plus:
e142c38c 9600 cu->language = language_cplus;
c906108c 9601 break;
6aecb9c2
JB
9602 case DW_LANG_D:
9603 cu->language = language_d;
9604 break;
c906108c
SS
9605 case DW_LANG_Fortran77:
9606 case DW_LANG_Fortran90:
b21b22e0 9607 case DW_LANG_Fortran95:
e142c38c 9608 cu->language = language_fortran;
c906108c
SS
9609 break;
9610 case DW_LANG_Mips_Assembler:
e142c38c 9611 cu->language = language_asm;
c906108c 9612 break;
bebd888e 9613 case DW_LANG_Java:
e142c38c 9614 cu->language = language_java;
bebd888e 9615 break;
c906108c 9616 case DW_LANG_Ada83:
8aaf0b47 9617 case DW_LANG_Ada95:
bc5f45f8
JB
9618 cu->language = language_ada;
9619 break;
72019c9c
GM
9620 case DW_LANG_Modula2:
9621 cu->language = language_m2;
9622 break;
fe8e67fd
PM
9623 case DW_LANG_Pascal83:
9624 cu->language = language_pascal;
9625 break;
22566fbd
DJ
9626 case DW_LANG_ObjC:
9627 cu->language = language_objc;
9628 break;
c906108c
SS
9629 case DW_LANG_Cobol74:
9630 case DW_LANG_Cobol85:
c906108c 9631 default:
e142c38c 9632 cu->language = language_minimal;
c906108c
SS
9633 break;
9634 }
e142c38c 9635 cu->language_defn = language_def (cu->language);
c906108c
SS
9636}
9637
9638/* Return the named attribute or NULL if not there. */
9639
9640static struct attribute *
e142c38c 9641dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9642{
9643 unsigned int i;
9644 struct attribute *spec = NULL;
9645
9646 for (i = 0; i < die->num_attrs; ++i)
9647 {
9648 if (die->attrs[i].name == name)
10b3939b 9649 return &die->attrs[i];
c906108c
SS
9650 if (die->attrs[i].name == DW_AT_specification
9651 || die->attrs[i].name == DW_AT_abstract_origin)
9652 spec = &die->attrs[i];
9653 }
c906108c 9654
10b3939b 9655 if (spec)
f2f0e013
DJ
9656 {
9657 die = follow_die_ref (die, spec, &cu);
9658 return dwarf2_attr (die, name, cu);
9659 }
c5aa993b 9660
c906108c
SS
9661 return NULL;
9662}
9663
348e048f
DE
9664/* Return the named attribute or NULL if not there,
9665 but do not follow DW_AT_specification, etc.
9666 This is for use in contexts where we're reading .debug_types dies.
9667 Following DW_AT_specification, DW_AT_abstract_origin will take us
9668 back up the chain, and we want to go down. */
9669
9670static struct attribute *
9671dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9672 struct dwarf2_cu *cu)
9673{
9674 unsigned int i;
9675
9676 for (i = 0; i < die->num_attrs; ++i)
9677 if (die->attrs[i].name == name)
9678 return &die->attrs[i];
9679
9680 return NULL;
9681}
9682
05cf31d1
JB
9683/* Return non-zero iff the attribute NAME is defined for the given DIE,
9684 and holds a non-zero value. This function should only be used for
2dc7f7b3 9685 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9686
9687static int
9688dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9689{
9690 struct attribute *attr = dwarf2_attr (die, name, cu);
9691
9692 return (attr && DW_UNSND (attr));
9693}
9694
3ca72b44 9695static int
e142c38c 9696die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9697{
05cf31d1
JB
9698 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9699 which value is non-zero. However, we have to be careful with
9700 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9701 (via dwarf2_flag_true_p) follows this attribute. So we may
9702 end up accidently finding a declaration attribute that belongs
9703 to a different DIE referenced by the specification attribute,
9704 even though the given DIE does not have a declaration attribute. */
9705 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9706 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9707}
9708
63d06c5c 9709/* Return the die giving the specification for DIE, if there is
f2f0e013 9710 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9711 containing the return value on output. If there is no
9712 specification, but there is an abstract origin, that is
9713 returned. */
63d06c5c
DC
9714
9715static struct die_info *
f2f0e013 9716die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9717{
f2f0e013
DJ
9718 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9719 *spec_cu);
63d06c5c 9720
edb3359d
DJ
9721 if (spec_attr == NULL)
9722 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9723
63d06c5c
DC
9724 if (spec_attr == NULL)
9725 return NULL;
9726 else
f2f0e013 9727 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9728}
c906108c 9729
debd256d
JB
9730/* Free the line_header structure *LH, and any arrays and strings it
9731 refers to. */
9732static void
9733free_line_header (struct line_header *lh)
9734{
9735 if (lh->standard_opcode_lengths)
a8bc7b56 9736 xfree (lh->standard_opcode_lengths);
debd256d
JB
9737
9738 /* Remember that all the lh->file_names[i].name pointers are
9739 pointers into debug_line_buffer, and don't need to be freed. */
9740 if (lh->file_names)
a8bc7b56 9741 xfree (lh->file_names);
debd256d
JB
9742
9743 /* Similarly for the include directory names. */
9744 if (lh->include_dirs)
a8bc7b56 9745 xfree (lh->include_dirs);
debd256d 9746
a8bc7b56 9747 xfree (lh);
debd256d
JB
9748}
9749
9750
9751/* Add an entry to LH's include directory table. */
9752static void
9753add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9754{
debd256d
JB
9755 /* Grow the array if necessary. */
9756 if (lh->include_dirs_size == 0)
c5aa993b 9757 {
debd256d
JB
9758 lh->include_dirs_size = 1; /* for testing */
9759 lh->include_dirs = xmalloc (lh->include_dirs_size
9760 * sizeof (*lh->include_dirs));
9761 }
9762 else if (lh->num_include_dirs >= lh->include_dirs_size)
9763 {
9764 lh->include_dirs_size *= 2;
9765 lh->include_dirs = xrealloc (lh->include_dirs,
9766 (lh->include_dirs_size
9767 * sizeof (*lh->include_dirs)));
c5aa993b 9768 }
c906108c 9769
debd256d
JB
9770 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9771}
6e70227d 9772
debd256d
JB
9773
9774/* Add an entry to LH's file name table. */
9775static void
9776add_file_name (struct line_header *lh,
9777 char *name,
9778 unsigned int dir_index,
9779 unsigned int mod_time,
9780 unsigned int length)
9781{
9782 struct file_entry *fe;
9783
9784 /* Grow the array if necessary. */
9785 if (lh->file_names_size == 0)
9786 {
9787 lh->file_names_size = 1; /* for testing */
9788 lh->file_names = xmalloc (lh->file_names_size
9789 * sizeof (*lh->file_names));
9790 }
9791 else if (lh->num_file_names >= lh->file_names_size)
9792 {
9793 lh->file_names_size *= 2;
9794 lh->file_names = xrealloc (lh->file_names,
9795 (lh->file_names_size
9796 * sizeof (*lh->file_names)));
9797 }
9798
9799 fe = &lh->file_names[lh->num_file_names++];
9800 fe->name = name;
9801 fe->dir_index = dir_index;
9802 fe->mod_time = mod_time;
9803 fe->length = length;
aaa75496 9804 fe->included_p = 0;
cb1df416 9805 fe->symtab = NULL;
debd256d 9806}
6e70227d 9807
debd256d
JB
9808
9809/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9810 .debug_line, according to the endianness of ABFD. Return a pointer
9811 to a struct line_header, allocated using xmalloc.
debd256d
JB
9812
9813 NOTE: the strings in the include directory and file name tables of
9814 the returned object point into debug_line_buffer, and must not be
9815 freed. */
9816static struct line_header *
9817dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9818 struct dwarf2_cu *cu)
debd256d
JB
9819{
9820 struct cleanup *back_to;
9821 struct line_header *lh;
fe1b8b76 9822 gdb_byte *line_ptr;
c764a876 9823 unsigned int bytes_read, offset_size;
debd256d
JB
9824 int i;
9825 char *cur_dir, *cur_file;
9826
be391dca 9827 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9828 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9829 {
e2e0b3e5 9830 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9831 return 0;
9832 }
9833
a738430d
MK
9834 /* Make sure that at least there's room for the total_length field.
9835 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9836 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9837 {
4d3c2250 9838 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9839 return 0;
9840 }
9841
9842 lh = xmalloc (sizeof (*lh));
9843 memset (lh, 0, sizeof (*lh));
9844 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9845 (void *) lh);
9846
dce234bc 9847 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9848
a738430d 9849 /* Read in the header. */
6e70227d 9850 lh->total_length =
c764a876
DE
9851 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9852 &bytes_read, &offset_size);
debd256d 9853 line_ptr += bytes_read;
dce234bc
PP
9854 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9855 + dwarf2_per_objfile->line.size))
debd256d 9856 {
4d3c2250 9857 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9858 return 0;
9859 }
9860 lh->statement_program_end = line_ptr + lh->total_length;
9861 lh->version = read_2_bytes (abfd, line_ptr);
9862 line_ptr += 2;
c764a876
DE
9863 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9864 line_ptr += offset_size;
debd256d
JB
9865 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9866 line_ptr += 1;
2dc7f7b3
TT
9867 if (lh->version >= 4)
9868 {
9869 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9870 line_ptr += 1;
9871 }
9872 else
9873 lh->maximum_ops_per_instruction = 1;
9874
9875 if (lh->maximum_ops_per_instruction == 0)
9876 {
9877 lh->maximum_ops_per_instruction = 1;
9878 complaint (&symfile_complaints,
9879 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9880 }
9881
debd256d
JB
9882 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9883 line_ptr += 1;
9884 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9885 line_ptr += 1;
9886 lh->line_range = read_1_byte (abfd, line_ptr);
9887 line_ptr += 1;
9888 lh->opcode_base = read_1_byte (abfd, line_ptr);
9889 line_ptr += 1;
9890 lh->standard_opcode_lengths
fe1b8b76 9891 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9892
9893 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9894 for (i = 1; i < lh->opcode_base; ++i)
9895 {
9896 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9897 line_ptr += 1;
9898 }
9899
a738430d 9900 /* Read directory table. */
9b1c24c8 9901 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9902 {
9903 line_ptr += bytes_read;
9904 add_include_dir (lh, cur_dir);
9905 }
9906 line_ptr += bytes_read;
9907
a738430d 9908 /* Read file name table. */
9b1c24c8 9909 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9910 {
9911 unsigned int dir_index, mod_time, length;
9912
9913 line_ptr += bytes_read;
9914 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9915 line_ptr += bytes_read;
9916 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9917 line_ptr += bytes_read;
9918 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9919 line_ptr += bytes_read;
9920
9921 add_file_name (lh, cur_file, dir_index, mod_time, length);
9922 }
9923 line_ptr += bytes_read;
6e70227d 9924 lh->statement_program_start = line_ptr;
debd256d 9925
dce234bc
PP
9926 if (line_ptr > (dwarf2_per_objfile->line.buffer
9927 + dwarf2_per_objfile->line.size))
4d3c2250 9928 complaint (&symfile_complaints,
e2e0b3e5 9929 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9930
9931 discard_cleanups (back_to);
9932 return lh;
9933}
c906108c 9934
5fb290d7
DJ
9935/* This function exists to work around a bug in certain compilers
9936 (particularly GCC 2.95), in which the first line number marker of a
9937 function does not show up until after the prologue, right before
9938 the second line number marker. This function shifts ADDRESS down
9939 to the beginning of the function if necessary, and is called on
9940 addresses passed to record_line. */
9941
9942static CORE_ADDR
e142c38c 9943check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9944{
9945 struct function_range *fn;
9946
9947 /* Find the function_range containing address. */
e142c38c 9948 if (!cu->first_fn)
5fb290d7
DJ
9949 return address;
9950
e142c38c
DJ
9951 if (!cu->cached_fn)
9952 cu->cached_fn = cu->first_fn;
5fb290d7 9953
e142c38c 9954 fn = cu->cached_fn;
5fb290d7
DJ
9955 while (fn)
9956 if (fn->lowpc <= address && fn->highpc > address)
9957 goto found;
9958 else
9959 fn = fn->next;
9960
e142c38c
DJ
9961 fn = cu->first_fn;
9962 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9963 if (fn->lowpc <= address && fn->highpc > address)
9964 goto found;
9965 else
9966 fn = fn->next;
9967
9968 return address;
9969
9970 found:
9971 if (fn->seen_line)
9972 return address;
9973 if (address != fn->lowpc)
4d3c2250 9974 complaint (&symfile_complaints,
e2e0b3e5 9975 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9976 (unsigned long) address, fn->name);
5fb290d7
DJ
9977 fn->seen_line = 1;
9978 return fn->lowpc;
9979}
9980
c6da4cef
DE
9981/* Subroutine of dwarf_decode_lines to simplify it.
9982 Return the file name of the psymtab for included file FILE_INDEX
9983 in line header LH of PST.
9984 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9985 If space for the result is malloc'd, it will be freed by a cleanup.
9986 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
9987
9988static char *
9989psymtab_include_file_name (const struct line_header *lh, int file_index,
9990 const struct partial_symtab *pst,
9991 const char *comp_dir)
9992{
9993 const struct file_entry fe = lh->file_names [file_index];
9994 char *include_name = fe.name;
9995 char *include_name_to_compare = include_name;
9996 char *dir_name = NULL;
9997 char *pst_filename;
9998 int file_is_pst;
9999
10000 if (fe.dir_index)
10001 dir_name = lh->include_dirs[fe.dir_index - 1];
10002
10003 if (!IS_ABSOLUTE_PATH (include_name)
10004 && (dir_name != NULL || comp_dir != NULL))
10005 {
10006 /* Avoid creating a duplicate psymtab for PST.
10007 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10008 Before we do the comparison, however, we need to account
10009 for DIR_NAME and COMP_DIR.
10010 First prepend dir_name (if non-NULL). If we still don't
10011 have an absolute path prepend comp_dir (if non-NULL).
10012 However, the directory we record in the include-file's
10013 psymtab does not contain COMP_DIR (to match the
10014 corresponding symtab(s)).
10015
10016 Example:
10017
10018 bash$ cd /tmp
10019 bash$ gcc -g ./hello.c
10020 include_name = "hello.c"
10021 dir_name = "."
10022 DW_AT_comp_dir = comp_dir = "/tmp"
10023 DW_AT_name = "./hello.c" */
10024
10025 if (dir_name != NULL)
10026 {
10027 include_name = concat (dir_name, SLASH_STRING,
10028 include_name, (char *)NULL);
10029 include_name_to_compare = include_name;
10030 make_cleanup (xfree, include_name);
10031 }
10032 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10033 {
10034 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10035 include_name, (char *)NULL);
10036 }
10037 }
10038
10039 pst_filename = pst->filename;
10040 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10041 {
10042 pst_filename = concat (pst->dirname, SLASH_STRING,
10043 pst_filename, (char *)NULL);
10044 }
10045
10046 file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
10047
10048 if (include_name_to_compare != include_name)
10049 xfree (include_name_to_compare);
10050 if (pst_filename != pst->filename)
10051 xfree (pst_filename);
10052
10053 if (file_is_pst)
10054 return NULL;
10055 return include_name;
10056}
10057
aaa75496
JB
10058/* Decode the Line Number Program (LNP) for the given line_header
10059 structure and CU. The actual information extracted and the type
10060 of structures created from the LNP depends on the value of PST.
10061
10062 1. If PST is NULL, then this procedure uses the data from the program
10063 to create all necessary symbol tables, and their linetables.
6e70227d 10064
aaa75496
JB
10065 2. If PST is not NULL, this procedure reads the program to determine
10066 the list of files included by the unit represented by PST, and
c6da4cef
DE
10067 builds all the associated partial symbol tables.
10068
10069 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10070 It is used for relative paths in the line table.
10071 NOTE: When processing partial symtabs (pst != NULL),
10072 comp_dir == pst->dirname.
10073
10074 NOTE: It is important that psymtabs have the same file name (via strcmp)
10075 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10076 symtab we don't use it in the name of the psymtabs we create.
10077 E.g. expand_line_sal requires this when finding psymtabs to expand.
10078 A good testcase for this is mb-inline.exp. */
debd256d 10079
c906108c 10080static void
debd256d 10081dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 10082 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10083{
a8c50c1f 10084 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10085 gdb_byte *line_end;
a8c50c1f 10086 unsigned int bytes_read, extended_len;
c906108c 10087 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10088 CORE_ADDR baseaddr;
10089 struct objfile *objfile = cu->objfile;
fbf65064 10090 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10091 const int decode_for_pst_p = (pst != NULL);
cb1df416 10092 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10093
10094 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10095
debd256d
JB
10096 line_ptr = lh->statement_program_start;
10097 line_end = lh->statement_program_end;
c906108c
SS
10098
10099 /* Read the statement sequences until there's nothing left. */
10100 while (line_ptr < line_end)
10101 {
10102 /* state machine registers */
10103 CORE_ADDR address = 0;
10104 unsigned int file = 1;
10105 unsigned int line = 1;
10106 unsigned int column = 0;
debd256d 10107 int is_stmt = lh->default_is_stmt;
c906108c
SS
10108 int basic_block = 0;
10109 int end_sequence = 0;
fbf65064 10110 CORE_ADDR addr;
2dc7f7b3 10111 unsigned char op_index = 0;
c906108c 10112
aaa75496 10113 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10114 {
aaa75496 10115 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10116 /* lh->include_dirs and lh->file_names are 0-based, but the
10117 directory and file name numbers in the statement program
10118 are 1-based. */
10119 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10120 char *dir = NULL;
a738430d 10121
debd256d
JB
10122 if (fe->dir_index)
10123 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10124
10125 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10126 }
10127
a738430d 10128 /* Decode the table. */
c5aa993b 10129 while (!end_sequence)
c906108c
SS
10130 {
10131 op_code = read_1_byte (abfd, line_ptr);
10132 line_ptr += 1;
59205f5a
JB
10133 if (line_ptr > line_end)
10134 {
10135 dwarf2_debug_line_missing_end_sequence_complaint ();
10136 break;
10137 }
9aa1fe7e 10138
debd256d 10139 if (op_code >= lh->opcode_base)
6e70227d 10140 {
a738430d 10141 /* Special operand. */
debd256d 10142 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10143 address += (((op_index + (adj_opcode / lh->line_range))
10144 / lh->maximum_ops_per_instruction)
10145 * lh->minimum_instruction_length);
10146 op_index = ((op_index + (adj_opcode / lh->line_range))
10147 % lh->maximum_ops_per_instruction);
debd256d 10148 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10149 if (lh->num_file_names < file || file == 0)
25e43795 10150 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10151 /* For now we ignore lines not starting on an
10152 instruction boundary. */
10153 else if (op_index == 0)
25e43795
DJ
10154 {
10155 lh->file_names[file - 1].included_p = 1;
ca5f395d 10156 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10157 {
10158 if (last_subfile != current_subfile)
10159 {
10160 addr = gdbarch_addr_bits_remove (gdbarch, address);
10161 if (last_subfile)
10162 record_line (last_subfile, 0, addr);
10163 last_subfile = current_subfile;
10164 }
25e43795 10165 /* Append row to matrix using current values. */
fbf65064
UW
10166 addr = check_cu_functions (address, cu);
10167 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10168 record_line (current_subfile, line, addr);
366da635 10169 }
25e43795 10170 }
ca5f395d 10171 basic_block = 0;
9aa1fe7e
GK
10172 }
10173 else switch (op_code)
c906108c
SS
10174 {
10175 case DW_LNS_extended_op:
a8c50c1f 10176 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10177 line_ptr += bytes_read;
a8c50c1f 10178 extended_end = line_ptr + extended_len;
c906108c
SS
10179 extended_op = read_1_byte (abfd, line_ptr);
10180 line_ptr += 1;
10181 switch (extended_op)
10182 {
10183 case DW_LNE_end_sequence:
10184 end_sequence = 1;
c906108c
SS
10185 break;
10186 case DW_LNE_set_address:
e7c27a73 10187 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10188 op_index = 0;
107d2387
AC
10189 line_ptr += bytes_read;
10190 address += baseaddr;
c906108c
SS
10191 break;
10192 case DW_LNE_define_file:
debd256d
JB
10193 {
10194 char *cur_file;
10195 unsigned int dir_index, mod_time, length;
6e70227d 10196
9b1c24c8 10197 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10198 line_ptr += bytes_read;
10199 dir_index =
10200 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10201 line_ptr += bytes_read;
10202 mod_time =
10203 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10204 line_ptr += bytes_read;
10205 length =
10206 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10207 line_ptr += bytes_read;
10208 add_file_name (lh, cur_file, dir_index, mod_time, length);
10209 }
c906108c 10210 break;
d0c6ba3d
CC
10211 case DW_LNE_set_discriminator:
10212 /* The discriminator is not interesting to the debugger;
10213 just ignore it. */
10214 line_ptr = extended_end;
10215 break;
c906108c 10216 default:
4d3c2250 10217 complaint (&symfile_complaints,
e2e0b3e5 10218 _("mangled .debug_line section"));
debd256d 10219 return;
c906108c 10220 }
a8c50c1f
DJ
10221 /* Make sure that we parsed the extended op correctly. If e.g.
10222 we expected a different address size than the producer used,
10223 we may have read the wrong number of bytes. */
10224 if (line_ptr != extended_end)
10225 {
10226 complaint (&symfile_complaints,
10227 _("mangled .debug_line section"));
10228 return;
10229 }
c906108c
SS
10230 break;
10231 case DW_LNS_copy:
59205f5a 10232 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10233 dwarf2_debug_line_missing_file_complaint ();
10234 else
366da635 10235 {
25e43795 10236 lh->file_names[file - 1].included_p = 1;
ca5f395d 10237 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10238 {
10239 if (last_subfile != current_subfile)
10240 {
10241 addr = gdbarch_addr_bits_remove (gdbarch, address);
10242 if (last_subfile)
10243 record_line (last_subfile, 0, addr);
10244 last_subfile = current_subfile;
10245 }
10246 addr = check_cu_functions (address, cu);
10247 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10248 record_line (current_subfile, line, addr);
10249 }
366da635 10250 }
c906108c
SS
10251 basic_block = 0;
10252 break;
10253 case DW_LNS_advance_pc:
2dc7f7b3
TT
10254 {
10255 CORE_ADDR adjust
10256 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10257
10258 address += (((op_index + adjust)
10259 / lh->maximum_ops_per_instruction)
10260 * lh->minimum_instruction_length);
10261 op_index = ((op_index + adjust)
10262 % lh->maximum_ops_per_instruction);
10263 line_ptr += bytes_read;
10264 }
c906108c
SS
10265 break;
10266 case DW_LNS_advance_line:
10267 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10268 line_ptr += bytes_read;
10269 break;
10270 case DW_LNS_set_file:
debd256d 10271 {
a738430d
MK
10272 /* The arrays lh->include_dirs and lh->file_names are
10273 0-based, but the directory and file name numbers in
10274 the statement program are 1-based. */
debd256d 10275 struct file_entry *fe;
4f1520fb 10276 char *dir = NULL;
a738430d 10277
debd256d
JB
10278 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10279 line_ptr += bytes_read;
59205f5a 10280 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10281 dwarf2_debug_line_missing_file_complaint ();
10282 else
10283 {
10284 fe = &lh->file_names[file - 1];
10285 if (fe->dir_index)
10286 dir = lh->include_dirs[fe->dir_index - 1];
10287 if (!decode_for_pst_p)
10288 {
10289 last_subfile = current_subfile;
10290 dwarf2_start_subfile (fe->name, dir, comp_dir);
10291 }
10292 }
debd256d 10293 }
c906108c
SS
10294 break;
10295 case DW_LNS_set_column:
10296 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10297 line_ptr += bytes_read;
10298 break;
10299 case DW_LNS_negate_stmt:
10300 is_stmt = (!is_stmt);
10301 break;
10302 case DW_LNS_set_basic_block:
10303 basic_block = 1;
10304 break;
c2c6d25f
JM
10305 /* Add to the address register of the state machine the
10306 address increment value corresponding to special opcode
a738430d
MK
10307 255. I.e., this value is scaled by the minimum
10308 instruction length since special opcode 255 would have
10309 scaled the the increment. */
c906108c 10310 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10311 {
10312 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10313
10314 address += (((op_index + adjust)
10315 / lh->maximum_ops_per_instruction)
10316 * lh->minimum_instruction_length);
10317 op_index = ((op_index + adjust)
10318 % lh->maximum_ops_per_instruction);
10319 }
c906108c
SS
10320 break;
10321 case DW_LNS_fixed_advance_pc:
10322 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10323 op_index = 0;
c906108c
SS
10324 line_ptr += 2;
10325 break;
9aa1fe7e 10326 default:
a738430d
MK
10327 {
10328 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10329 int i;
a738430d 10330
debd256d 10331 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10332 {
10333 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10334 line_ptr += bytes_read;
10335 }
10336 }
c906108c
SS
10337 }
10338 }
59205f5a
JB
10339 if (lh->num_file_names < file || file == 0)
10340 dwarf2_debug_line_missing_file_complaint ();
10341 else
10342 {
10343 lh->file_names[file - 1].included_p = 1;
10344 if (!decode_for_pst_p)
fbf65064
UW
10345 {
10346 addr = gdbarch_addr_bits_remove (gdbarch, address);
10347 record_line (current_subfile, 0, addr);
10348 }
59205f5a 10349 }
c906108c 10350 }
aaa75496
JB
10351
10352 if (decode_for_pst_p)
10353 {
10354 int file_index;
10355
10356 /* Now that we're done scanning the Line Header Program, we can
10357 create the psymtab of each included file. */
10358 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10359 if (lh->file_names[file_index].included_p == 1)
10360 {
c6da4cef
DE
10361 char *include_name =
10362 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10363 if (include_name != NULL)
aaa75496
JB
10364 dwarf2_create_include_psymtab (include_name, pst, objfile);
10365 }
10366 }
cb1df416
DJ
10367 else
10368 {
10369 /* Make sure a symtab is created for every file, even files
10370 which contain only variables (i.e. no code with associated
10371 line numbers). */
10372
10373 int i;
10374 struct file_entry *fe;
10375
10376 for (i = 0; i < lh->num_file_names; i++)
10377 {
10378 char *dir = NULL;
9a619af0 10379
cb1df416
DJ
10380 fe = &lh->file_names[i];
10381 if (fe->dir_index)
10382 dir = lh->include_dirs[fe->dir_index - 1];
10383 dwarf2_start_subfile (fe->name, dir, comp_dir);
10384
10385 /* Skip the main file; we don't need it, and it must be
10386 allocated last, so that it will show up before the
10387 non-primary symtabs in the objfile's symtab list. */
10388 if (current_subfile == first_subfile)
10389 continue;
10390
10391 if (current_subfile->symtab == NULL)
10392 current_subfile->symtab = allocate_symtab (current_subfile->name,
10393 cu->objfile);
10394 fe->symtab = current_subfile->symtab;
10395 }
10396 }
c906108c
SS
10397}
10398
10399/* Start a subfile for DWARF. FILENAME is the name of the file and
10400 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10401 or NULL if not known. COMP_DIR is the compilation directory for the
10402 linetable's compilation unit or NULL if not known.
c906108c
SS
10403 This routine tries to keep line numbers from identical absolute and
10404 relative file names in a common subfile.
10405
10406 Using the `list' example from the GDB testsuite, which resides in
10407 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10408 of /srcdir/list0.c yields the following debugging information for list0.c:
10409
c5aa993b
JM
10410 DW_AT_name: /srcdir/list0.c
10411 DW_AT_comp_dir: /compdir
357e46e7 10412 files.files[0].name: list0.h
c5aa993b 10413 files.files[0].dir: /srcdir
357e46e7 10414 files.files[1].name: list0.c
c5aa993b 10415 files.files[1].dir: /srcdir
c906108c
SS
10416
10417 The line number information for list0.c has to end up in a single
4f1520fb
FR
10418 subfile, so that `break /srcdir/list0.c:1' works as expected.
10419 start_subfile will ensure that this happens provided that we pass the
10420 concatenation of files.files[1].dir and files.files[1].name as the
10421 subfile's name. */
c906108c
SS
10422
10423static void
4f1520fb 10424dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10425{
4f1520fb
FR
10426 char *fullname;
10427
10428 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10429 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10430 second argument to start_subfile. To be consistent, we do the
10431 same here. In order not to lose the line information directory,
10432 we concatenate it to the filename when it makes sense.
10433 Note that the Dwarf3 standard says (speaking of filenames in line
10434 information): ``The directory index is ignored for file names
10435 that represent full path names''. Thus ignoring dirname in the
10436 `else' branch below isn't an issue. */
c906108c 10437
d5166ae1 10438 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10439 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10440 else
10441 fullname = filename;
c906108c 10442
4f1520fb
FR
10443 start_subfile (fullname, comp_dir);
10444
10445 if (fullname != filename)
10446 xfree (fullname);
c906108c
SS
10447}
10448
4c2df51b
DJ
10449static void
10450var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10451 struct dwarf2_cu *cu)
4c2df51b 10452{
e7c27a73
DJ
10453 struct objfile *objfile = cu->objfile;
10454 struct comp_unit_head *cu_header = &cu->header;
10455
4c2df51b
DJ
10456 /* NOTE drow/2003-01-30: There used to be a comment and some special
10457 code here to turn a symbol with DW_AT_external and a
10458 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10459 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10460 with some versions of binutils) where shared libraries could have
10461 relocations against symbols in their debug information - the
10462 minimal symbol would have the right address, but the debug info
10463 would not. It's no longer necessary, because we will explicitly
10464 apply relocations when we read in the debug information now. */
10465
10466 /* A DW_AT_location attribute with no contents indicates that a
10467 variable has been optimized away. */
10468 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10469 {
10470 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10471 return;
10472 }
10473
10474 /* Handle one degenerate form of location expression specially, to
10475 preserve GDB's previous behavior when section offsets are
10476 specified. If this is just a DW_OP_addr then mark this symbol
10477 as LOC_STATIC. */
10478
10479 if (attr_form_is_block (attr)
10480 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10481 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10482 {
891d2f0b 10483 unsigned int dummy;
4c2df51b
DJ
10484
10485 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10486 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10487 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10488 fixup_symbol_section (sym, objfile);
10489 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10490 SYMBOL_SECTION (sym));
4c2df51b
DJ
10491 return;
10492 }
10493
10494 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10495 expression evaluator, and use LOC_COMPUTED only when necessary
10496 (i.e. when the value of a register or memory location is
10497 referenced, or a thread-local block, etc.). Then again, it might
10498 not be worthwhile. I'm assuming that it isn't unless performance
10499 or memory numbers show me otherwise. */
10500
e7c27a73 10501 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10502 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10503}
10504
c906108c
SS
10505/* Given a pointer to a DWARF information entry, figure out if we need
10506 to make a symbol table entry for it, and if so, create a new entry
10507 and return a pointer to it.
10508 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10509 used the passed type.
10510 If SPACE is not NULL, use it to hold the new symbol. If it is
10511 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10512
10513static struct symbol *
34eaf542
TT
10514new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10515 struct symbol *space)
c906108c 10516{
e7c27a73 10517 struct objfile *objfile = cu->objfile;
c906108c
SS
10518 struct symbol *sym = NULL;
10519 char *name;
10520 struct attribute *attr = NULL;
10521 struct attribute *attr2 = NULL;
e142c38c 10522 CORE_ADDR baseaddr;
e37fd15a
SW
10523 struct pending **list_to_add = NULL;
10524
edb3359d 10525 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10526
10527 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10528
94af9270 10529 name = dwarf2_name (die, cu);
c906108c
SS
10530 if (name)
10531 {
94af9270 10532 const char *linkagename;
34eaf542 10533 int suppress_add = 0;
94af9270 10534
34eaf542
TT
10535 if (space)
10536 sym = space;
10537 else
10538 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10539 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10540
10541 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10542 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10543 linkagename = dwarf2_physname (name, die, cu);
10544 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10545
f55ee35c
JK
10546 /* Fortran does not have mangling standard and the mangling does differ
10547 between gfortran, iFort etc. */
10548 if (cu->language == language_fortran
b250c185 10549 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10550 symbol_set_demangled_name (&(sym->ginfo),
10551 (char *) dwarf2_full_name (name, die, cu),
10552 NULL);
f55ee35c 10553
c906108c 10554 /* Default assumptions.
c5aa993b 10555 Use the passed type or decode it from the die. */
176620f1 10556 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10557 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10558 if (type != NULL)
10559 SYMBOL_TYPE (sym) = type;
10560 else
e7c27a73 10561 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10562 attr = dwarf2_attr (die,
10563 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10564 cu);
c906108c
SS
10565 if (attr)
10566 {
10567 SYMBOL_LINE (sym) = DW_UNSND (attr);
10568 }
cb1df416 10569
edb3359d
DJ
10570 attr = dwarf2_attr (die,
10571 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10572 cu);
cb1df416
DJ
10573 if (attr)
10574 {
10575 int file_index = DW_UNSND (attr);
9a619af0 10576
cb1df416
DJ
10577 if (cu->line_header == NULL
10578 || file_index > cu->line_header->num_file_names)
10579 complaint (&symfile_complaints,
10580 _("file index out of range"));
1c3d648d 10581 else if (file_index > 0)
cb1df416
DJ
10582 {
10583 struct file_entry *fe;
9a619af0 10584
cb1df416
DJ
10585 fe = &cu->line_header->file_names[file_index - 1];
10586 SYMBOL_SYMTAB (sym) = fe->symtab;
10587 }
10588 }
10589
c906108c
SS
10590 switch (die->tag)
10591 {
10592 case DW_TAG_label:
e142c38c 10593 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10594 if (attr)
10595 {
10596 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10597 }
0f5238ed
TT
10598 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10599 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10600 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10601 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10602 break;
10603 case DW_TAG_subprogram:
10604 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10605 finish_block. */
10606 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10607 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10608 if ((attr2 && (DW_UNSND (attr2) != 0))
10609 || cu->language == language_ada)
c906108c 10610 {
2cfa0c8d
JB
10611 /* Subprograms marked external are stored as a global symbol.
10612 Ada subprograms, whether marked external or not, are always
10613 stored as a global symbol, because we want to be able to
10614 access them globally. For instance, we want to be able
10615 to break on a nested subprogram without having to
10616 specify the context. */
e37fd15a 10617 list_to_add = &global_symbols;
c906108c
SS
10618 }
10619 else
10620 {
e37fd15a 10621 list_to_add = cu->list_in_scope;
c906108c
SS
10622 }
10623 break;
edb3359d
DJ
10624 case DW_TAG_inlined_subroutine:
10625 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10626 finish_block. */
10627 SYMBOL_CLASS (sym) = LOC_BLOCK;
10628 SYMBOL_INLINED (sym) = 1;
10629 /* Do not add the symbol to any lists. It will be found via
10630 BLOCK_FUNCTION from the blockvector. */
10631 break;
34eaf542
TT
10632 case DW_TAG_template_value_param:
10633 suppress_add = 1;
10634 /* Fall through. */
72929c62 10635 case DW_TAG_constant:
c906108c 10636 case DW_TAG_variable:
254e6b9e 10637 case DW_TAG_member:
c906108c
SS
10638 /* Compilation with minimal debug info may result in variables
10639 with missing type entries. Change the misleading `void' type
10640 to something sensible. */
10641 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10642 SYMBOL_TYPE (sym)
46bf5051 10643 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10644
e142c38c 10645 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10646 /* In the case of DW_TAG_member, we should only be called for
10647 static const members. */
10648 if (die->tag == DW_TAG_member)
10649 {
3863f96c
DE
10650 /* dwarf2_add_field uses die_is_declaration,
10651 so we do the same. */
254e6b9e
DE
10652 gdb_assert (die_is_declaration (die, cu));
10653 gdb_assert (attr);
10654 }
c906108c
SS
10655 if (attr)
10656 {
e7c27a73 10657 dwarf2_const_value (attr, sym, cu);
e142c38c 10658 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10659 if (!suppress_add)
34eaf542
TT
10660 {
10661 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10662 list_to_add = &global_symbols;
34eaf542 10663 else
e37fd15a 10664 list_to_add = cu->list_in_scope;
34eaf542 10665 }
c906108c
SS
10666 break;
10667 }
e142c38c 10668 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10669 if (attr)
10670 {
e7c27a73 10671 var_decode_location (attr, sym, cu);
e142c38c 10672 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10673 if (SYMBOL_CLASS (sym) == LOC_STATIC
10674 && SYMBOL_VALUE_ADDRESS (sym) == 0
10675 && !dwarf2_per_objfile->has_section_at_zero)
10676 {
10677 /* When a static variable is eliminated by the linker,
10678 the corresponding debug information is not stripped
10679 out, but the variable address is set to null;
10680 do not add such variables into symbol table. */
10681 }
10682 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10683 {
f55ee35c
JK
10684 /* Workaround gfortran PR debug/40040 - it uses
10685 DW_AT_location for variables in -fPIC libraries which may
10686 get overriden by other libraries/executable and get
10687 a different address. Resolve it by the minimal symbol
10688 which may come from inferior's executable using copy
10689 relocation. Make this workaround only for gfortran as for
10690 other compilers GDB cannot guess the minimal symbol
10691 Fortran mangling kind. */
10692 if (cu->language == language_fortran && die->parent
10693 && die->parent->tag == DW_TAG_module
10694 && cu->producer
10695 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10696 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10697
1c809c68
TT
10698 /* A variable with DW_AT_external is never static,
10699 but it may be block-scoped. */
10700 list_to_add = (cu->list_in_scope == &file_symbols
10701 ? &global_symbols : cu->list_in_scope);
1c809c68 10702 }
c906108c 10703 else
e37fd15a 10704 list_to_add = cu->list_in_scope;
c906108c
SS
10705 }
10706 else
10707 {
10708 /* We do not know the address of this symbol.
c5aa993b
JM
10709 If it is an external symbol and we have type information
10710 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10711 The address of the variable will then be determined from
10712 the minimal symbol table whenever the variable is
10713 referenced. */
e142c38c 10714 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10715 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10716 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10717 {
0fe7935b
DJ
10718 /* A variable with DW_AT_external is never static, but it
10719 may be block-scoped. */
10720 list_to_add = (cu->list_in_scope == &file_symbols
10721 ? &global_symbols : cu->list_in_scope);
10722
c906108c 10723 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10724 }
442ddf59
JK
10725 else if (!die_is_declaration (die, cu))
10726 {
10727 /* Use the default LOC_OPTIMIZED_OUT class. */
10728 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10729 if (!suppress_add)
10730 list_to_add = cu->list_in_scope;
442ddf59 10731 }
c906108c
SS
10732 }
10733 break;
10734 case DW_TAG_formal_parameter:
edb3359d
DJ
10735 /* If we are inside a function, mark this as an argument. If
10736 not, we might be looking at an argument to an inlined function
10737 when we do not have enough information to show inlined frames;
10738 pretend it's a local variable in that case so that the user can
10739 still see it. */
10740 if (context_stack_depth > 0
10741 && context_stack[context_stack_depth - 1].name != NULL)
10742 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10743 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10744 if (attr)
10745 {
e7c27a73 10746 var_decode_location (attr, sym, cu);
c906108c 10747 }
e142c38c 10748 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10749 if (attr)
10750 {
e7c27a73 10751 dwarf2_const_value (attr, sym, cu);
c906108c 10752 }
f346a30d
PM
10753 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10754 if (attr && DW_UNSND (attr))
10755 {
10756 struct type *ref_type;
10757
10758 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10759 SYMBOL_TYPE (sym) = ref_type;
10760 }
10761
e37fd15a 10762 list_to_add = cu->list_in_scope;
c906108c
SS
10763 break;
10764 case DW_TAG_unspecified_parameters:
10765 /* From varargs functions; gdb doesn't seem to have any
10766 interest in this information, so just ignore it for now.
10767 (FIXME?) */
10768 break;
34eaf542
TT
10769 case DW_TAG_template_type_param:
10770 suppress_add = 1;
10771 /* Fall through. */
c906108c 10772 case DW_TAG_class_type:
680b30c7 10773 case DW_TAG_interface_type:
c906108c
SS
10774 case DW_TAG_structure_type:
10775 case DW_TAG_union_type:
72019c9c 10776 case DW_TAG_set_type:
c906108c
SS
10777 case DW_TAG_enumeration_type:
10778 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10779 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10780
63d06c5c 10781 {
987504bb 10782 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10783 really ever be static objects: otherwise, if you try
10784 to, say, break of a class's method and you're in a file
10785 which doesn't mention that class, it won't work unless
10786 the check for all static symbols in lookup_symbol_aux
10787 saves you. See the OtherFileClass tests in
10788 gdb.c++/namespace.exp. */
10789
e37fd15a 10790 if (!suppress_add)
34eaf542 10791 {
34eaf542
TT
10792 list_to_add = (cu->list_in_scope == &file_symbols
10793 && (cu->language == language_cplus
10794 || cu->language == language_java)
10795 ? &global_symbols : cu->list_in_scope);
63d06c5c 10796
64382290
TT
10797 /* The semantics of C++ state that "struct foo {
10798 ... }" also defines a typedef for "foo". A Java
10799 class declaration also defines a typedef for the
10800 class. */
10801 if (cu->language == language_cplus
10802 || cu->language == language_java
10803 || cu->language == language_ada)
10804 {
10805 /* The symbol's name is already allocated along
10806 with this objfile, so we don't need to
10807 duplicate it for the type. */
10808 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10809 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10810 }
63d06c5c
DC
10811 }
10812 }
c906108c
SS
10813 break;
10814 case DW_TAG_typedef:
63d06c5c
DC
10815 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10816 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10817 list_to_add = cu->list_in_scope;
63d06c5c 10818 break;
c906108c 10819 case DW_TAG_base_type:
a02abb62 10820 case DW_TAG_subrange_type:
c906108c 10821 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10822 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10823 list_to_add = cu->list_in_scope;
c906108c
SS
10824 break;
10825 case DW_TAG_enumerator:
e142c38c 10826 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10827 if (attr)
10828 {
e7c27a73 10829 dwarf2_const_value (attr, sym, cu);
c906108c 10830 }
63d06c5c
DC
10831 {
10832 /* NOTE: carlton/2003-11-10: See comment above in the
10833 DW_TAG_class_type, etc. block. */
10834
e142c38c 10835 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10836 && (cu->language == language_cplus
10837 || cu->language == language_java)
e142c38c 10838 ? &global_symbols : cu->list_in_scope);
63d06c5c 10839 }
c906108c 10840 break;
5c4e30ca
DC
10841 case DW_TAG_namespace:
10842 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10843 list_to_add = &global_symbols;
5c4e30ca 10844 break;
c906108c
SS
10845 default:
10846 /* Not a tag we recognize. Hopefully we aren't processing
10847 trash data, but since we must specifically ignore things
10848 we don't recognize, there is nothing else we should do at
10849 this point. */
e2e0b3e5 10850 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10851 dwarf_tag_name (die->tag));
c906108c
SS
10852 break;
10853 }
df8a16a1 10854
e37fd15a
SW
10855 if (suppress_add)
10856 {
10857 sym->hash_next = objfile->template_symbols;
10858 objfile->template_symbols = sym;
10859 list_to_add = NULL;
10860 }
10861
10862 if (list_to_add != NULL)
10863 add_symbol_to_list (sym, list_to_add);
10864
df8a16a1
DJ
10865 /* For the benefit of old versions of GCC, check for anonymous
10866 namespaces based on the demangled name. */
10867 if (!processing_has_namespace_info
94af9270 10868 && cu->language == language_cplus)
df8a16a1 10869 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10870 }
10871 return (sym);
10872}
10873
34eaf542
TT
10874/* A wrapper for new_symbol_full that always allocates a new symbol. */
10875
10876static struct symbol *
10877new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10878{
10879 return new_symbol_full (die, type, cu, NULL);
10880}
10881
98bfdba5
PA
10882/* Given an attr with a DW_FORM_dataN value in host byte order,
10883 zero-extend it as appropriate for the symbol's type. The DWARF
10884 standard (v4) is not entirely clear about the meaning of using
10885 DW_FORM_dataN for a constant with a signed type, where the type is
10886 wider than the data. The conclusion of a discussion on the DWARF
10887 list was that this is unspecified. We choose to always zero-extend
10888 because that is the interpretation long in use by GCC. */
c906108c 10889
98bfdba5
PA
10890static gdb_byte *
10891dwarf2_const_value_data (struct attribute *attr, struct type *type,
10892 const char *name, struct obstack *obstack,
10893 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10894{
e7c27a73 10895 struct objfile *objfile = cu->objfile;
e17a4113
UW
10896 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10897 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10898 LONGEST l = DW_UNSND (attr);
10899
10900 if (bits < sizeof (*value) * 8)
10901 {
10902 l &= ((LONGEST) 1 << bits) - 1;
10903 *value = l;
10904 }
10905 else if (bits == sizeof (*value) * 8)
10906 *value = l;
10907 else
10908 {
10909 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10910 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10911 return bytes;
10912 }
10913
10914 return NULL;
10915}
10916
10917/* Read a constant value from an attribute. Either set *VALUE, or if
10918 the value does not fit in *VALUE, set *BYTES - either already
10919 allocated on the objfile obstack, or newly allocated on OBSTACK,
10920 or, set *BATON, if we translated the constant to a location
10921 expression. */
10922
10923static void
10924dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10925 const char *name, struct obstack *obstack,
10926 struct dwarf2_cu *cu,
10927 long *value, gdb_byte **bytes,
10928 struct dwarf2_locexpr_baton **baton)
10929{
10930 struct objfile *objfile = cu->objfile;
10931 struct comp_unit_head *cu_header = &cu->header;
c906108c 10932 struct dwarf_block *blk;
98bfdba5
PA
10933 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10934 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10935
10936 *value = 0;
10937 *bytes = NULL;
10938 *baton = NULL;
c906108c
SS
10939
10940 switch (attr->form)
10941 {
10942 case DW_FORM_addr:
ac56253d 10943 {
ac56253d
TT
10944 gdb_byte *data;
10945
98bfdba5
PA
10946 if (TYPE_LENGTH (type) != cu_header->addr_size)
10947 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10948 cu_header->addr_size,
98bfdba5 10949 TYPE_LENGTH (type));
ac56253d
TT
10950 /* Symbols of this form are reasonably rare, so we just
10951 piggyback on the existing location code rather than writing
10952 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10953 *baton = obstack_alloc (&objfile->objfile_obstack,
10954 sizeof (struct dwarf2_locexpr_baton));
10955 (*baton)->per_cu = cu->per_cu;
10956 gdb_assert ((*baton)->per_cu);
ac56253d 10957
98bfdba5
PA
10958 (*baton)->size = 2 + cu_header->addr_size;
10959 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10960 (*baton)->data = data;
ac56253d
TT
10961
10962 data[0] = DW_OP_addr;
10963 store_unsigned_integer (&data[1], cu_header->addr_size,
10964 byte_order, DW_ADDR (attr));
10965 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10966 }
c906108c 10967 break;
4ac36638 10968 case DW_FORM_string:
93b5768b 10969 case DW_FORM_strp:
98bfdba5
PA
10970 /* DW_STRING is already allocated on the objfile obstack, point
10971 directly to it. */
10972 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10973 break;
c906108c
SS
10974 case DW_FORM_block1:
10975 case DW_FORM_block2:
10976 case DW_FORM_block4:
10977 case DW_FORM_block:
2dc7f7b3 10978 case DW_FORM_exprloc:
c906108c 10979 blk = DW_BLOCK (attr);
98bfdba5
PA
10980 if (TYPE_LENGTH (type) != blk->size)
10981 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10982 TYPE_LENGTH (type));
10983 *bytes = blk->data;
c906108c 10984 break;
2df3850c
JM
10985
10986 /* The DW_AT_const_value attributes are supposed to carry the
10987 symbol's value "represented as it would be on the target
10988 architecture." By the time we get here, it's already been
10989 converted to host endianness, so we just need to sign- or
10990 zero-extend it as appropriate. */
10991 case DW_FORM_data1:
98bfdba5 10992 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10993 break;
c906108c 10994 case DW_FORM_data2:
98bfdba5 10995 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10996 break;
c906108c 10997 case DW_FORM_data4:
98bfdba5 10998 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10999 break;
c906108c 11000 case DW_FORM_data8:
98bfdba5 11001 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11002 break;
11003
c906108c 11004 case DW_FORM_sdata:
98bfdba5 11005 *value = DW_SND (attr);
2df3850c
JM
11006 break;
11007
c906108c 11008 case DW_FORM_udata:
98bfdba5 11009 *value = DW_UNSND (attr);
c906108c 11010 break;
2df3850c 11011
c906108c 11012 default:
4d3c2250 11013 complaint (&symfile_complaints,
e2e0b3e5 11014 _("unsupported const value attribute form: '%s'"),
4d3c2250 11015 dwarf_form_name (attr->form));
98bfdba5 11016 *value = 0;
c906108c
SS
11017 break;
11018 }
11019}
11020
2df3850c 11021
98bfdba5
PA
11022/* Copy constant value from an attribute to a symbol. */
11023
2df3850c 11024static void
98bfdba5
PA
11025dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11026 struct dwarf2_cu *cu)
2df3850c 11027{
98bfdba5
PA
11028 struct objfile *objfile = cu->objfile;
11029 struct comp_unit_head *cu_header = &cu->header;
11030 long value;
11031 gdb_byte *bytes;
11032 struct dwarf2_locexpr_baton *baton;
2df3850c 11033
98bfdba5
PA
11034 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11035 SYMBOL_PRINT_NAME (sym),
11036 &objfile->objfile_obstack, cu,
11037 &value, &bytes, &baton);
2df3850c 11038
98bfdba5
PA
11039 if (baton != NULL)
11040 {
11041 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11042 SYMBOL_LOCATION_BATON (sym) = baton;
11043 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11044 }
11045 else if (bytes != NULL)
11046 {
11047 SYMBOL_VALUE_BYTES (sym) = bytes;
11048 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11049 }
11050 else
11051 {
11052 SYMBOL_VALUE (sym) = value;
11053 SYMBOL_CLASS (sym) = LOC_CONST;
11054 }
2df3850c
JM
11055}
11056
c906108c
SS
11057/* Return the type of the die in question using its DW_AT_type attribute. */
11058
11059static struct type *
e7c27a73 11060die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11061{
c906108c 11062 struct attribute *type_attr;
c906108c 11063
e142c38c 11064 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11065 if (!type_attr)
11066 {
11067 /* A missing DW_AT_type represents a void type. */
46bf5051 11068 return objfile_type (cu->objfile)->builtin_void;
c906108c 11069 }
348e048f 11070
673bfd45 11071 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11072}
11073
b4ba55a1
JB
11074/* True iff CU's producer generates GNAT Ada auxiliary information
11075 that allows to find parallel types through that information instead
11076 of having to do expensive parallel lookups by type name. */
11077
11078static int
11079need_gnat_info (struct dwarf2_cu *cu)
11080{
11081 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11082 of GNAT produces this auxiliary information, without any indication
11083 that it is produced. Part of enhancing the FSF version of GNAT
11084 to produce that information will be to put in place an indicator
11085 that we can use in order to determine whether the descriptive type
11086 info is available or not. One suggestion that has been made is
11087 to use a new attribute, attached to the CU die. For now, assume
11088 that the descriptive type info is not available. */
11089 return 0;
11090}
11091
b4ba55a1
JB
11092/* Return the auxiliary type of the die in question using its
11093 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11094 attribute is not present. */
11095
11096static struct type *
11097die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11098{
b4ba55a1 11099 struct attribute *type_attr;
b4ba55a1
JB
11100
11101 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11102 if (!type_attr)
11103 return NULL;
11104
673bfd45 11105 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11106}
11107
11108/* If DIE has a descriptive_type attribute, then set the TYPE's
11109 descriptive type accordingly. */
11110
11111static void
11112set_descriptive_type (struct type *type, struct die_info *die,
11113 struct dwarf2_cu *cu)
11114{
11115 struct type *descriptive_type = die_descriptive_type (die, cu);
11116
11117 if (descriptive_type)
11118 {
11119 ALLOCATE_GNAT_AUX_TYPE (type);
11120 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11121 }
11122}
11123
c906108c
SS
11124/* Return the containing type of the die in question using its
11125 DW_AT_containing_type attribute. */
11126
11127static struct type *
e7c27a73 11128die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11129{
c906108c 11130 struct attribute *type_attr;
c906108c 11131
e142c38c 11132 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11133 if (!type_attr)
11134 error (_("Dwarf Error: Problem turning containing type into gdb type "
11135 "[in module %s]"), cu->objfile->name);
11136
673bfd45 11137 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11138}
11139
673bfd45
DE
11140/* Look up the type of DIE in CU using its type attribute ATTR.
11141 If there is no type substitute an error marker. */
11142
c906108c 11143static struct type *
673bfd45
DE
11144lookup_die_type (struct die_info *die, struct attribute *attr,
11145 struct dwarf2_cu *cu)
c906108c 11146{
f792889a
DJ
11147 struct type *this_type;
11148
673bfd45
DE
11149 /* First see if we have it cached. */
11150
11151 if (is_ref_attr (attr))
11152 {
11153 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11154
11155 this_type = get_die_type_at_offset (offset, cu->per_cu);
11156 }
11157 else if (attr->form == DW_FORM_sig8)
11158 {
11159 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11160 struct dwarf2_cu *sig_cu;
11161 unsigned int offset;
11162
11163 /* sig_type will be NULL if the signatured type is missing from
11164 the debug info. */
11165 if (sig_type == NULL)
11166 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11167 "at 0x%x [in module %s]"),
11168 die->offset, cu->objfile->name);
11169
11170 gdb_assert (sig_type->per_cu.from_debug_types);
11171 offset = sig_type->offset + sig_type->type_offset;
11172 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11173 }
11174 else
11175 {
11176 dump_die_for_error (die);
11177 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11178 dwarf_attr_name (attr->name), cu->objfile->name);
11179 }
11180
11181 /* If not cached we need to read it in. */
11182
11183 if (this_type == NULL)
11184 {
11185 struct die_info *type_die;
11186 struct dwarf2_cu *type_cu = cu;
11187
11188 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11189 /* If the type is cached, we should have found it above. */
11190 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11191 this_type = read_type_die_1 (type_die, type_cu);
11192 }
11193
11194 /* If we still don't have a type use an error marker. */
11195
11196 if (this_type == NULL)
c906108c 11197 {
b00fdb78
TT
11198 char *message, *saved;
11199
11200 /* read_type_die already issued a complaint. */
11201 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11202 cu->objfile->name,
11203 cu->header.offset,
11204 die->offset);
11205 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11206 message, strlen (message));
11207 xfree (message);
11208
11209 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11210 }
673bfd45 11211
f792889a 11212 return this_type;
c906108c
SS
11213}
11214
673bfd45
DE
11215/* Return the type in DIE, CU.
11216 Returns NULL for invalid types.
11217
11218 This first does a lookup in the appropriate type_hash table,
11219 and only reads the die in if necessary.
11220
11221 NOTE: This can be called when reading in partial or full symbols. */
11222
f792889a 11223static struct type *
e7c27a73 11224read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11225{
f792889a
DJ
11226 struct type *this_type;
11227
11228 this_type = get_die_type (die, cu);
11229 if (this_type)
11230 return this_type;
11231
673bfd45
DE
11232 return read_type_die_1 (die, cu);
11233}
11234
11235/* Read the type in DIE, CU.
11236 Returns NULL for invalid types. */
11237
11238static struct type *
11239read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11240{
11241 struct type *this_type = NULL;
11242
c906108c
SS
11243 switch (die->tag)
11244 {
11245 case DW_TAG_class_type:
680b30c7 11246 case DW_TAG_interface_type:
c906108c
SS
11247 case DW_TAG_structure_type:
11248 case DW_TAG_union_type:
f792889a 11249 this_type = read_structure_type (die, cu);
c906108c
SS
11250 break;
11251 case DW_TAG_enumeration_type:
f792889a 11252 this_type = read_enumeration_type (die, cu);
c906108c
SS
11253 break;
11254 case DW_TAG_subprogram:
11255 case DW_TAG_subroutine_type:
edb3359d 11256 case DW_TAG_inlined_subroutine:
f792889a 11257 this_type = read_subroutine_type (die, cu);
c906108c
SS
11258 break;
11259 case DW_TAG_array_type:
f792889a 11260 this_type = read_array_type (die, cu);
c906108c 11261 break;
72019c9c 11262 case DW_TAG_set_type:
f792889a 11263 this_type = read_set_type (die, cu);
72019c9c 11264 break;
c906108c 11265 case DW_TAG_pointer_type:
f792889a 11266 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11267 break;
11268 case DW_TAG_ptr_to_member_type:
f792889a 11269 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11270 break;
11271 case DW_TAG_reference_type:
f792889a 11272 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11273 break;
11274 case DW_TAG_const_type:
f792889a 11275 this_type = read_tag_const_type (die, cu);
c906108c
SS
11276 break;
11277 case DW_TAG_volatile_type:
f792889a 11278 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11279 break;
11280 case DW_TAG_string_type:
f792889a 11281 this_type = read_tag_string_type (die, cu);
c906108c
SS
11282 break;
11283 case DW_TAG_typedef:
f792889a 11284 this_type = read_typedef (die, cu);
c906108c 11285 break;
a02abb62 11286 case DW_TAG_subrange_type:
f792889a 11287 this_type = read_subrange_type (die, cu);
a02abb62 11288 break;
c906108c 11289 case DW_TAG_base_type:
f792889a 11290 this_type = read_base_type (die, cu);
c906108c 11291 break;
81a17f79 11292 case DW_TAG_unspecified_type:
f792889a 11293 this_type = read_unspecified_type (die, cu);
81a17f79 11294 break;
0114d602
DJ
11295 case DW_TAG_namespace:
11296 this_type = read_namespace_type (die, cu);
11297 break;
f55ee35c
JK
11298 case DW_TAG_module:
11299 this_type = read_module_type (die, cu);
11300 break;
c906108c 11301 default:
a1f5b845 11302 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11303 dwarf_tag_name (die->tag));
c906108c
SS
11304 break;
11305 }
63d06c5c 11306
f792889a 11307 return this_type;
63d06c5c
DC
11308}
11309
abc72ce4
DE
11310/* See if we can figure out if the class lives in a namespace. We do
11311 this by looking for a member function; its demangled name will
11312 contain namespace info, if there is any.
11313 Return the computed name or NULL.
11314 Space for the result is allocated on the objfile's obstack.
11315 This is the full-die version of guess_partial_die_structure_name.
11316 In this case we know DIE has no useful parent. */
11317
11318static char *
11319guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11320{
11321 struct die_info *spec_die;
11322 struct dwarf2_cu *spec_cu;
11323 struct die_info *child;
11324
11325 spec_cu = cu;
11326 spec_die = die_specification (die, &spec_cu);
11327 if (spec_die != NULL)
11328 {
11329 die = spec_die;
11330 cu = spec_cu;
11331 }
11332
11333 for (child = die->child;
11334 child != NULL;
11335 child = child->sibling)
11336 {
11337 if (child->tag == DW_TAG_subprogram)
11338 {
11339 struct attribute *attr;
11340
11341 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11342 if (attr == NULL)
11343 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11344 if (attr != NULL)
11345 {
11346 char *actual_name
11347 = language_class_name_from_physname (cu->language_defn,
11348 DW_STRING (attr));
11349 char *name = NULL;
11350
11351 if (actual_name != NULL)
11352 {
11353 char *die_name = dwarf2_name (die, cu);
11354
11355 if (die_name != NULL
11356 && strcmp (die_name, actual_name) != 0)
11357 {
11358 /* Strip off the class name from the full name.
11359 We want the prefix. */
11360 int die_name_len = strlen (die_name);
11361 int actual_name_len = strlen (actual_name);
11362
11363 /* Test for '::' as a sanity check. */
11364 if (actual_name_len > die_name_len + 2
11365 && actual_name[actual_name_len - die_name_len - 1] == ':')
11366 name =
11367 obsavestring (actual_name,
11368 actual_name_len - die_name_len - 2,
11369 &cu->objfile->objfile_obstack);
11370 }
11371 }
11372 xfree (actual_name);
11373 return name;
11374 }
11375 }
11376 }
11377
11378 return NULL;
11379}
11380
fdde2d81 11381/* Return the name of the namespace/class that DIE is defined within,
0114d602 11382 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11383
0114d602
DJ
11384 For example, if we're within the method foo() in the following
11385 code:
11386
11387 namespace N {
11388 class C {
11389 void foo () {
11390 }
11391 };
11392 }
11393
11394 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11395
11396static char *
e142c38c 11397determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11398{
0114d602
DJ
11399 struct die_info *parent, *spec_die;
11400 struct dwarf2_cu *spec_cu;
11401 struct type *parent_type;
63d06c5c 11402
f55ee35c
JK
11403 if (cu->language != language_cplus && cu->language != language_java
11404 && cu->language != language_fortran)
0114d602
DJ
11405 return "";
11406
11407 /* We have to be careful in the presence of DW_AT_specification.
11408 For example, with GCC 3.4, given the code
11409
11410 namespace N {
11411 void foo() {
11412 // Definition of N::foo.
11413 }
11414 }
11415
11416 then we'll have a tree of DIEs like this:
11417
11418 1: DW_TAG_compile_unit
11419 2: DW_TAG_namespace // N
11420 3: DW_TAG_subprogram // declaration of N::foo
11421 4: DW_TAG_subprogram // definition of N::foo
11422 DW_AT_specification // refers to die #3
11423
11424 Thus, when processing die #4, we have to pretend that we're in
11425 the context of its DW_AT_specification, namely the contex of die
11426 #3. */
11427 spec_cu = cu;
11428 spec_die = die_specification (die, &spec_cu);
11429 if (spec_die == NULL)
11430 parent = die->parent;
11431 else
63d06c5c 11432 {
0114d602
DJ
11433 parent = spec_die->parent;
11434 cu = spec_cu;
63d06c5c 11435 }
0114d602
DJ
11436
11437 if (parent == NULL)
11438 return "";
98bfdba5
PA
11439 else if (parent->building_fullname)
11440 {
11441 const char *name;
11442 const char *parent_name;
11443
11444 /* It has been seen on RealView 2.2 built binaries,
11445 DW_TAG_template_type_param types actually _defined_ as
11446 children of the parent class:
11447
11448 enum E {};
11449 template class <class Enum> Class{};
11450 Class<enum E> class_e;
11451
11452 1: DW_TAG_class_type (Class)
11453 2: DW_TAG_enumeration_type (E)
11454 3: DW_TAG_enumerator (enum1:0)
11455 3: DW_TAG_enumerator (enum2:1)
11456 ...
11457 2: DW_TAG_template_type_param
11458 DW_AT_type DW_FORM_ref_udata (E)
11459
11460 Besides being broken debug info, it can put GDB into an
11461 infinite loop. Consider:
11462
11463 When we're building the full name for Class<E>, we'll start
11464 at Class, and go look over its template type parameters,
11465 finding E. We'll then try to build the full name of E, and
11466 reach here. We're now trying to build the full name of E,
11467 and look over the parent DIE for containing scope. In the
11468 broken case, if we followed the parent DIE of E, we'd again
11469 find Class, and once again go look at its template type
11470 arguments, etc., etc. Simply don't consider such parent die
11471 as source-level parent of this die (it can't be, the language
11472 doesn't allow it), and break the loop here. */
11473 name = dwarf2_name (die, cu);
11474 parent_name = dwarf2_name (parent, cu);
11475 complaint (&symfile_complaints,
11476 _("template param type '%s' defined within parent '%s'"),
11477 name ? name : "<unknown>",
11478 parent_name ? parent_name : "<unknown>");
11479 return "";
11480 }
63d06c5c 11481 else
0114d602
DJ
11482 switch (parent->tag)
11483 {
63d06c5c 11484 case DW_TAG_namespace:
0114d602 11485 parent_type = read_type_die (parent, cu);
acebe513
UW
11486 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11487 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11488 Work around this problem here. */
11489 if (cu->language == language_cplus
11490 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11491 return "";
0114d602
DJ
11492 /* We give a name to even anonymous namespaces. */
11493 return TYPE_TAG_NAME (parent_type);
63d06c5c 11494 case DW_TAG_class_type:
680b30c7 11495 case DW_TAG_interface_type:
63d06c5c 11496 case DW_TAG_structure_type:
0114d602 11497 case DW_TAG_union_type:
f55ee35c 11498 case DW_TAG_module:
0114d602
DJ
11499 parent_type = read_type_die (parent, cu);
11500 if (TYPE_TAG_NAME (parent_type) != NULL)
11501 return TYPE_TAG_NAME (parent_type);
11502 else
11503 /* An anonymous structure is only allowed non-static data
11504 members; no typedefs, no member functions, et cetera.
11505 So it does not need a prefix. */
11506 return "";
abc72ce4
DE
11507 case DW_TAG_compile_unit:
11508 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11509 if (cu->language == language_cplus
11510 && dwarf2_per_objfile->types.asection != NULL
11511 && die->child != NULL
11512 && (die->tag == DW_TAG_class_type
11513 || die->tag == DW_TAG_structure_type
11514 || die->tag == DW_TAG_union_type))
11515 {
11516 char *name = guess_full_die_structure_name (die, cu);
11517 if (name != NULL)
11518 return name;
11519 }
11520 return "";
63d06c5c 11521 default:
8176b9b8 11522 return determine_prefix (parent, cu);
63d06c5c 11523 }
63d06c5c
DC
11524}
11525
987504bb
JJ
11526/* Return a newly-allocated string formed by concatenating PREFIX and
11527 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11528 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11529 perform an obconcat, otherwise allocate storage for the result. The CU argument
11530 is used to determine the language and hence, the appropriate separator. */
11531
f55ee35c 11532#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11533
11534static char *
f55ee35c
JK
11535typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11536 int physname, struct dwarf2_cu *cu)
63d06c5c 11537{
f55ee35c 11538 const char *lead = "";
5c315b68 11539 const char *sep;
63d06c5c 11540
987504bb
JJ
11541 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11542 sep = "";
11543 else if (cu->language == language_java)
11544 sep = ".";
f55ee35c
JK
11545 else if (cu->language == language_fortran && physname)
11546 {
11547 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11548 DW_AT_MIPS_linkage_name is preferred and used instead. */
11549
11550 lead = "__";
11551 sep = "_MOD_";
11552 }
987504bb
JJ
11553 else
11554 sep = "::";
63d06c5c 11555
6dd47d34
DE
11556 if (prefix == NULL)
11557 prefix = "";
11558 if (suffix == NULL)
11559 suffix = "";
11560
987504bb
JJ
11561 if (obs == NULL)
11562 {
11563 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11564
f55ee35c
JK
11565 strcpy (retval, lead);
11566 strcat (retval, prefix);
6dd47d34
DE
11567 strcat (retval, sep);
11568 strcat (retval, suffix);
63d06c5c
DC
11569 return retval;
11570 }
987504bb
JJ
11571 else
11572 {
11573 /* We have an obstack. */
f55ee35c 11574 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11575 }
63d06c5c
DC
11576}
11577
c906108c
SS
11578/* Return sibling of die, NULL if no sibling. */
11579
f9aca02d 11580static struct die_info *
fba45db2 11581sibling_die (struct die_info *die)
c906108c 11582{
639d11d3 11583 return die->sibling;
c906108c
SS
11584}
11585
71c25dea
TT
11586/* Get name of a die, return NULL if not found. */
11587
11588static char *
11589dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11590 struct obstack *obstack)
11591{
11592 if (name && cu->language == language_cplus)
11593 {
11594 char *canon_name = cp_canonicalize_string (name);
11595
11596 if (canon_name != NULL)
11597 {
11598 if (strcmp (canon_name, name) != 0)
11599 name = obsavestring (canon_name, strlen (canon_name),
11600 obstack);
11601 xfree (canon_name);
11602 }
11603 }
11604
11605 return name;
c906108c
SS
11606}
11607
9219021c
DC
11608/* Get name of a die, return NULL if not found. */
11609
11610static char *
e142c38c 11611dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11612{
11613 struct attribute *attr;
11614
e142c38c 11615 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11616 if (!attr || !DW_STRING (attr))
11617 return NULL;
11618
11619 switch (die->tag)
11620 {
11621 case DW_TAG_compile_unit:
11622 /* Compilation units have a DW_AT_name that is a filename, not
11623 a source language identifier. */
11624 case DW_TAG_enumeration_type:
11625 case DW_TAG_enumerator:
11626 /* These tags always have simple identifiers already; no need
11627 to canonicalize them. */
11628 return DW_STRING (attr);
907af001 11629
418835cc
KS
11630 case DW_TAG_subprogram:
11631 /* Java constructors will all be named "<init>", so return
11632 the class name when we see this special case. */
11633 if (cu->language == language_java
11634 && DW_STRING (attr) != NULL
11635 && strcmp (DW_STRING (attr), "<init>") == 0)
11636 {
11637 struct dwarf2_cu *spec_cu = cu;
11638 struct die_info *spec_die;
11639
11640 /* GCJ will output '<init>' for Java constructor names.
11641 For this special case, return the name of the parent class. */
11642
11643 /* GCJ may output suprogram DIEs with AT_specification set.
11644 If so, use the name of the specified DIE. */
11645 spec_die = die_specification (die, &spec_cu);
11646 if (spec_die != NULL)
11647 return dwarf2_name (spec_die, spec_cu);
11648
11649 do
11650 {
11651 die = die->parent;
11652 if (die->tag == DW_TAG_class_type)
11653 return dwarf2_name (die, cu);
11654 }
11655 while (die->tag != DW_TAG_compile_unit);
11656 }
907af001
UW
11657 break;
11658
11659 case DW_TAG_class_type:
11660 case DW_TAG_interface_type:
11661 case DW_TAG_structure_type:
11662 case DW_TAG_union_type:
11663 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11664 structures or unions. These were of the form "._%d" in GCC 4.1,
11665 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11666 and GCC 4.4. We work around this problem by ignoring these. */
11667 if (strncmp (DW_STRING (attr), "._", 2) == 0
11668 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11669 return NULL;
11670 break;
11671
71c25dea 11672 default:
907af001
UW
11673 break;
11674 }
11675
11676 if (!DW_STRING_IS_CANONICAL (attr))
11677 {
11678 DW_STRING (attr)
11679 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11680 &cu->objfile->objfile_obstack);
11681 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11682 }
907af001 11683 return DW_STRING (attr);
9219021c
DC
11684}
11685
11686/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11687 is none. *EXT_CU is the CU containing DIE on input, and the CU
11688 containing the return value on output. */
9219021c
DC
11689
11690static struct die_info *
f2f0e013 11691dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11692{
11693 struct attribute *attr;
9219021c 11694
f2f0e013 11695 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11696 if (attr == NULL)
11697 return NULL;
11698
f2f0e013 11699 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11700}
11701
c906108c
SS
11702/* Convert a DIE tag into its string name. */
11703
11704static char *
aa1ee363 11705dwarf_tag_name (unsigned tag)
c906108c
SS
11706{
11707 switch (tag)
11708 {
11709 case DW_TAG_padding:
11710 return "DW_TAG_padding";
11711 case DW_TAG_array_type:
11712 return "DW_TAG_array_type";
11713 case DW_TAG_class_type:
11714 return "DW_TAG_class_type";
11715 case DW_TAG_entry_point:
11716 return "DW_TAG_entry_point";
11717 case DW_TAG_enumeration_type:
11718 return "DW_TAG_enumeration_type";
11719 case DW_TAG_formal_parameter:
11720 return "DW_TAG_formal_parameter";
11721 case DW_TAG_imported_declaration:
11722 return "DW_TAG_imported_declaration";
11723 case DW_TAG_label:
11724 return "DW_TAG_label";
11725 case DW_TAG_lexical_block:
11726 return "DW_TAG_lexical_block";
11727 case DW_TAG_member:
11728 return "DW_TAG_member";
11729 case DW_TAG_pointer_type:
11730 return "DW_TAG_pointer_type";
11731 case DW_TAG_reference_type:
11732 return "DW_TAG_reference_type";
11733 case DW_TAG_compile_unit:
11734 return "DW_TAG_compile_unit";
11735 case DW_TAG_string_type:
11736 return "DW_TAG_string_type";
11737 case DW_TAG_structure_type:
11738 return "DW_TAG_structure_type";
11739 case DW_TAG_subroutine_type:
11740 return "DW_TAG_subroutine_type";
11741 case DW_TAG_typedef:
11742 return "DW_TAG_typedef";
11743 case DW_TAG_union_type:
11744 return "DW_TAG_union_type";
11745 case DW_TAG_unspecified_parameters:
11746 return "DW_TAG_unspecified_parameters";
11747 case DW_TAG_variant:
11748 return "DW_TAG_variant";
11749 case DW_TAG_common_block:
11750 return "DW_TAG_common_block";
11751 case DW_TAG_common_inclusion:
11752 return "DW_TAG_common_inclusion";
11753 case DW_TAG_inheritance:
11754 return "DW_TAG_inheritance";
11755 case DW_TAG_inlined_subroutine:
11756 return "DW_TAG_inlined_subroutine";
11757 case DW_TAG_module:
11758 return "DW_TAG_module";
11759 case DW_TAG_ptr_to_member_type:
11760 return "DW_TAG_ptr_to_member_type";
11761 case DW_TAG_set_type:
11762 return "DW_TAG_set_type";
11763 case DW_TAG_subrange_type:
11764 return "DW_TAG_subrange_type";
11765 case DW_TAG_with_stmt:
11766 return "DW_TAG_with_stmt";
11767 case DW_TAG_access_declaration:
11768 return "DW_TAG_access_declaration";
11769 case DW_TAG_base_type:
11770 return "DW_TAG_base_type";
11771 case DW_TAG_catch_block:
11772 return "DW_TAG_catch_block";
11773 case DW_TAG_const_type:
11774 return "DW_TAG_const_type";
11775 case DW_TAG_constant:
11776 return "DW_TAG_constant";
11777 case DW_TAG_enumerator:
11778 return "DW_TAG_enumerator";
11779 case DW_TAG_file_type:
11780 return "DW_TAG_file_type";
11781 case DW_TAG_friend:
11782 return "DW_TAG_friend";
11783 case DW_TAG_namelist:
11784 return "DW_TAG_namelist";
11785 case DW_TAG_namelist_item:
11786 return "DW_TAG_namelist_item";
11787 case DW_TAG_packed_type:
11788 return "DW_TAG_packed_type";
11789 case DW_TAG_subprogram:
11790 return "DW_TAG_subprogram";
11791 case DW_TAG_template_type_param:
11792 return "DW_TAG_template_type_param";
11793 case DW_TAG_template_value_param:
11794 return "DW_TAG_template_value_param";
11795 case DW_TAG_thrown_type:
11796 return "DW_TAG_thrown_type";
11797 case DW_TAG_try_block:
11798 return "DW_TAG_try_block";
11799 case DW_TAG_variant_part:
11800 return "DW_TAG_variant_part";
11801 case DW_TAG_variable:
11802 return "DW_TAG_variable";
11803 case DW_TAG_volatile_type:
11804 return "DW_TAG_volatile_type";
d9fa45fe
DC
11805 case DW_TAG_dwarf_procedure:
11806 return "DW_TAG_dwarf_procedure";
11807 case DW_TAG_restrict_type:
11808 return "DW_TAG_restrict_type";
11809 case DW_TAG_interface_type:
11810 return "DW_TAG_interface_type";
11811 case DW_TAG_namespace:
11812 return "DW_TAG_namespace";
11813 case DW_TAG_imported_module:
11814 return "DW_TAG_imported_module";
11815 case DW_TAG_unspecified_type:
11816 return "DW_TAG_unspecified_type";
11817 case DW_TAG_partial_unit:
11818 return "DW_TAG_partial_unit";
11819 case DW_TAG_imported_unit:
11820 return "DW_TAG_imported_unit";
b7619582
GF
11821 case DW_TAG_condition:
11822 return "DW_TAG_condition";
11823 case DW_TAG_shared_type:
11824 return "DW_TAG_shared_type";
348e048f
DE
11825 case DW_TAG_type_unit:
11826 return "DW_TAG_type_unit";
c906108c
SS
11827 case DW_TAG_MIPS_loop:
11828 return "DW_TAG_MIPS_loop";
b7619582
GF
11829 case DW_TAG_HP_array_descriptor:
11830 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11831 case DW_TAG_format_label:
11832 return "DW_TAG_format_label";
11833 case DW_TAG_function_template:
11834 return "DW_TAG_function_template";
11835 case DW_TAG_class_template:
11836 return "DW_TAG_class_template";
b7619582
GF
11837 case DW_TAG_GNU_BINCL:
11838 return "DW_TAG_GNU_BINCL";
11839 case DW_TAG_GNU_EINCL:
11840 return "DW_TAG_GNU_EINCL";
11841 case DW_TAG_upc_shared_type:
11842 return "DW_TAG_upc_shared_type";
11843 case DW_TAG_upc_strict_type:
11844 return "DW_TAG_upc_strict_type";
11845 case DW_TAG_upc_relaxed_type:
11846 return "DW_TAG_upc_relaxed_type";
11847 case DW_TAG_PGI_kanji_type:
11848 return "DW_TAG_PGI_kanji_type";
11849 case DW_TAG_PGI_interface_block:
11850 return "DW_TAG_PGI_interface_block";
c906108c
SS
11851 default:
11852 return "DW_TAG_<unknown>";
11853 }
11854}
11855
11856/* Convert a DWARF attribute code into its string name. */
11857
11858static char *
aa1ee363 11859dwarf_attr_name (unsigned attr)
c906108c
SS
11860{
11861 switch (attr)
11862 {
11863 case DW_AT_sibling:
11864 return "DW_AT_sibling";
11865 case DW_AT_location:
11866 return "DW_AT_location";
11867 case DW_AT_name:
11868 return "DW_AT_name";
11869 case DW_AT_ordering:
11870 return "DW_AT_ordering";
11871 case DW_AT_subscr_data:
11872 return "DW_AT_subscr_data";
11873 case DW_AT_byte_size:
11874 return "DW_AT_byte_size";
11875 case DW_AT_bit_offset:
11876 return "DW_AT_bit_offset";
11877 case DW_AT_bit_size:
11878 return "DW_AT_bit_size";
11879 case DW_AT_element_list:
11880 return "DW_AT_element_list";
11881 case DW_AT_stmt_list:
11882 return "DW_AT_stmt_list";
11883 case DW_AT_low_pc:
11884 return "DW_AT_low_pc";
11885 case DW_AT_high_pc:
11886 return "DW_AT_high_pc";
11887 case DW_AT_language:
11888 return "DW_AT_language";
11889 case DW_AT_member:
11890 return "DW_AT_member";
11891 case DW_AT_discr:
11892 return "DW_AT_discr";
11893 case DW_AT_discr_value:
11894 return "DW_AT_discr_value";
11895 case DW_AT_visibility:
11896 return "DW_AT_visibility";
11897 case DW_AT_import:
11898 return "DW_AT_import";
11899 case DW_AT_string_length:
11900 return "DW_AT_string_length";
11901 case DW_AT_common_reference:
11902 return "DW_AT_common_reference";
11903 case DW_AT_comp_dir:
11904 return "DW_AT_comp_dir";
11905 case DW_AT_const_value:
11906 return "DW_AT_const_value";
11907 case DW_AT_containing_type:
11908 return "DW_AT_containing_type";
11909 case DW_AT_default_value:
11910 return "DW_AT_default_value";
11911 case DW_AT_inline:
11912 return "DW_AT_inline";
11913 case DW_AT_is_optional:
11914 return "DW_AT_is_optional";
11915 case DW_AT_lower_bound:
11916 return "DW_AT_lower_bound";
11917 case DW_AT_producer:
11918 return "DW_AT_producer";
11919 case DW_AT_prototyped:
11920 return "DW_AT_prototyped";
11921 case DW_AT_return_addr:
11922 return "DW_AT_return_addr";
11923 case DW_AT_start_scope:
11924 return "DW_AT_start_scope";
09fa0d7c
JK
11925 case DW_AT_bit_stride:
11926 return "DW_AT_bit_stride";
c906108c
SS
11927 case DW_AT_upper_bound:
11928 return "DW_AT_upper_bound";
11929 case DW_AT_abstract_origin:
11930 return "DW_AT_abstract_origin";
11931 case DW_AT_accessibility:
11932 return "DW_AT_accessibility";
11933 case DW_AT_address_class:
11934 return "DW_AT_address_class";
11935 case DW_AT_artificial:
11936 return "DW_AT_artificial";
11937 case DW_AT_base_types:
11938 return "DW_AT_base_types";
11939 case DW_AT_calling_convention:
11940 return "DW_AT_calling_convention";
11941 case DW_AT_count:
11942 return "DW_AT_count";
11943 case DW_AT_data_member_location:
11944 return "DW_AT_data_member_location";
11945 case DW_AT_decl_column:
11946 return "DW_AT_decl_column";
11947 case DW_AT_decl_file:
11948 return "DW_AT_decl_file";
11949 case DW_AT_decl_line:
11950 return "DW_AT_decl_line";
11951 case DW_AT_declaration:
11952 return "DW_AT_declaration";
11953 case DW_AT_discr_list:
11954 return "DW_AT_discr_list";
11955 case DW_AT_encoding:
11956 return "DW_AT_encoding";
11957 case DW_AT_external:
11958 return "DW_AT_external";
11959 case DW_AT_frame_base:
11960 return "DW_AT_frame_base";
11961 case DW_AT_friend:
11962 return "DW_AT_friend";
11963 case DW_AT_identifier_case:
11964 return "DW_AT_identifier_case";
11965 case DW_AT_macro_info:
11966 return "DW_AT_macro_info";
11967 case DW_AT_namelist_items:
11968 return "DW_AT_namelist_items";
11969 case DW_AT_priority:
11970 return "DW_AT_priority";
11971 case DW_AT_segment:
11972 return "DW_AT_segment";
11973 case DW_AT_specification:
11974 return "DW_AT_specification";
11975 case DW_AT_static_link:
11976 return "DW_AT_static_link";
11977 case DW_AT_type:
11978 return "DW_AT_type";
11979 case DW_AT_use_location:
11980 return "DW_AT_use_location";
11981 case DW_AT_variable_parameter:
11982 return "DW_AT_variable_parameter";
11983 case DW_AT_virtuality:
11984 return "DW_AT_virtuality";
11985 case DW_AT_vtable_elem_location:
11986 return "DW_AT_vtable_elem_location";
b7619582 11987 /* DWARF 3 values. */
d9fa45fe
DC
11988 case DW_AT_allocated:
11989 return "DW_AT_allocated";
11990 case DW_AT_associated:
11991 return "DW_AT_associated";
11992 case DW_AT_data_location:
11993 return "DW_AT_data_location";
09fa0d7c
JK
11994 case DW_AT_byte_stride:
11995 return "DW_AT_byte_stride";
d9fa45fe
DC
11996 case DW_AT_entry_pc:
11997 return "DW_AT_entry_pc";
11998 case DW_AT_use_UTF8:
11999 return "DW_AT_use_UTF8";
12000 case DW_AT_extension:
12001 return "DW_AT_extension";
12002 case DW_AT_ranges:
12003 return "DW_AT_ranges";
12004 case DW_AT_trampoline:
12005 return "DW_AT_trampoline";
12006 case DW_AT_call_column:
12007 return "DW_AT_call_column";
12008 case DW_AT_call_file:
12009 return "DW_AT_call_file";
12010 case DW_AT_call_line:
12011 return "DW_AT_call_line";
b7619582
GF
12012 case DW_AT_description:
12013 return "DW_AT_description";
12014 case DW_AT_binary_scale:
12015 return "DW_AT_binary_scale";
12016 case DW_AT_decimal_scale:
12017 return "DW_AT_decimal_scale";
12018 case DW_AT_small:
12019 return "DW_AT_small";
12020 case DW_AT_decimal_sign:
12021 return "DW_AT_decimal_sign";
12022 case DW_AT_digit_count:
12023 return "DW_AT_digit_count";
12024 case DW_AT_picture_string:
12025 return "DW_AT_picture_string";
12026 case DW_AT_mutable:
12027 return "DW_AT_mutable";
12028 case DW_AT_threads_scaled:
12029 return "DW_AT_threads_scaled";
12030 case DW_AT_explicit:
12031 return "DW_AT_explicit";
12032 case DW_AT_object_pointer:
12033 return "DW_AT_object_pointer";
12034 case DW_AT_endianity:
12035 return "DW_AT_endianity";
12036 case DW_AT_elemental:
12037 return "DW_AT_elemental";
12038 case DW_AT_pure:
12039 return "DW_AT_pure";
12040 case DW_AT_recursive:
12041 return "DW_AT_recursive";
348e048f
DE
12042 /* DWARF 4 values. */
12043 case DW_AT_signature:
12044 return "DW_AT_signature";
31ef98ae
TT
12045 case DW_AT_linkage_name:
12046 return "DW_AT_linkage_name";
b7619582 12047 /* SGI/MIPS extensions. */
c764a876 12048#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12049 case DW_AT_MIPS_fde:
12050 return "DW_AT_MIPS_fde";
c764a876 12051#endif
c906108c
SS
12052 case DW_AT_MIPS_loop_begin:
12053 return "DW_AT_MIPS_loop_begin";
12054 case DW_AT_MIPS_tail_loop_begin:
12055 return "DW_AT_MIPS_tail_loop_begin";
12056 case DW_AT_MIPS_epilog_begin:
12057 return "DW_AT_MIPS_epilog_begin";
12058 case DW_AT_MIPS_loop_unroll_factor:
12059 return "DW_AT_MIPS_loop_unroll_factor";
12060 case DW_AT_MIPS_software_pipeline_depth:
12061 return "DW_AT_MIPS_software_pipeline_depth";
12062 case DW_AT_MIPS_linkage_name:
12063 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12064 case DW_AT_MIPS_stride:
12065 return "DW_AT_MIPS_stride";
12066 case DW_AT_MIPS_abstract_name:
12067 return "DW_AT_MIPS_abstract_name";
12068 case DW_AT_MIPS_clone_origin:
12069 return "DW_AT_MIPS_clone_origin";
12070 case DW_AT_MIPS_has_inlines:
12071 return "DW_AT_MIPS_has_inlines";
b7619582 12072 /* HP extensions. */
c764a876 12073#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12074 case DW_AT_HP_block_index:
12075 return "DW_AT_HP_block_index";
c764a876 12076#endif
b7619582
GF
12077 case DW_AT_HP_unmodifiable:
12078 return "DW_AT_HP_unmodifiable";
12079 case DW_AT_HP_actuals_stmt_list:
12080 return "DW_AT_HP_actuals_stmt_list";
12081 case DW_AT_HP_proc_per_section:
12082 return "DW_AT_HP_proc_per_section";
12083 case DW_AT_HP_raw_data_ptr:
12084 return "DW_AT_HP_raw_data_ptr";
12085 case DW_AT_HP_pass_by_reference:
12086 return "DW_AT_HP_pass_by_reference";
12087 case DW_AT_HP_opt_level:
12088 return "DW_AT_HP_opt_level";
12089 case DW_AT_HP_prof_version_id:
12090 return "DW_AT_HP_prof_version_id";
12091 case DW_AT_HP_opt_flags:
12092 return "DW_AT_HP_opt_flags";
12093 case DW_AT_HP_cold_region_low_pc:
12094 return "DW_AT_HP_cold_region_low_pc";
12095 case DW_AT_HP_cold_region_high_pc:
12096 return "DW_AT_HP_cold_region_high_pc";
12097 case DW_AT_HP_all_variables_modifiable:
12098 return "DW_AT_HP_all_variables_modifiable";
12099 case DW_AT_HP_linkage_name:
12100 return "DW_AT_HP_linkage_name";
12101 case DW_AT_HP_prof_flags:
12102 return "DW_AT_HP_prof_flags";
12103 /* GNU extensions. */
c906108c
SS
12104 case DW_AT_sf_names:
12105 return "DW_AT_sf_names";
12106 case DW_AT_src_info:
12107 return "DW_AT_src_info";
12108 case DW_AT_mac_info:
12109 return "DW_AT_mac_info";
12110 case DW_AT_src_coords:
12111 return "DW_AT_src_coords";
12112 case DW_AT_body_begin:
12113 return "DW_AT_body_begin";
12114 case DW_AT_body_end:
12115 return "DW_AT_body_end";
f5f8a009
EZ
12116 case DW_AT_GNU_vector:
12117 return "DW_AT_GNU_vector";
2de00c64
DE
12118 case DW_AT_GNU_odr_signature:
12119 return "DW_AT_GNU_odr_signature";
b7619582
GF
12120 /* VMS extensions. */
12121 case DW_AT_VMS_rtnbeg_pd_address:
12122 return "DW_AT_VMS_rtnbeg_pd_address";
12123 /* UPC extension. */
12124 case DW_AT_upc_threads_scaled:
12125 return "DW_AT_upc_threads_scaled";
12126 /* PGI (STMicroelectronics) extensions. */
12127 case DW_AT_PGI_lbase:
12128 return "DW_AT_PGI_lbase";
12129 case DW_AT_PGI_soffset:
12130 return "DW_AT_PGI_soffset";
12131 case DW_AT_PGI_lstride:
12132 return "DW_AT_PGI_lstride";
c906108c
SS
12133 default:
12134 return "DW_AT_<unknown>";
12135 }
12136}
12137
12138/* Convert a DWARF value form code into its string name. */
12139
12140static char *
aa1ee363 12141dwarf_form_name (unsigned form)
c906108c
SS
12142{
12143 switch (form)
12144 {
12145 case DW_FORM_addr:
12146 return "DW_FORM_addr";
12147 case DW_FORM_block2:
12148 return "DW_FORM_block2";
12149 case DW_FORM_block4:
12150 return "DW_FORM_block4";
12151 case DW_FORM_data2:
12152 return "DW_FORM_data2";
12153 case DW_FORM_data4:
12154 return "DW_FORM_data4";
12155 case DW_FORM_data8:
12156 return "DW_FORM_data8";
12157 case DW_FORM_string:
12158 return "DW_FORM_string";
12159 case DW_FORM_block:
12160 return "DW_FORM_block";
12161 case DW_FORM_block1:
12162 return "DW_FORM_block1";
12163 case DW_FORM_data1:
12164 return "DW_FORM_data1";
12165 case DW_FORM_flag:
12166 return "DW_FORM_flag";
12167 case DW_FORM_sdata:
12168 return "DW_FORM_sdata";
12169 case DW_FORM_strp:
12170 return "DW_FORM_strp";
12171 case DW_FORM_udata:
12172 return "DW_FORM_udata";
12173 case DW_FORM_ref_addr:
12174 return "DW_FORM_ref_addr";
12175 case DW_FORM_ref1:
12176 return "DW_FORM_ref1";
12177 case DW_FORM_ref2:
12178 return "DW_FORM_ref2";
12179 case DW_FORM_ref4:
12180 return "DW_FORM_ref4";
12181 case DW_FORM_ref8:
12182 return "DW_FORM_ref8";
12183 case DW_FORM_ref_udata:
12184 return "DW_FORM_ref_udata";
12185 case DW_FORM_indirect:
12186 return "DW_FORM_indirect";
348e048f
DE
12187 case DW_FORM_sec_offset:
12188 return "DW_FORM_sec_offset";
12189 case DW_FORM_exprloc:
12190 return "DW_FORM_exprloc";
12191 case DW_FORM_flag_present:
12192 return "DW_FORM_flag_present";
12193 case DW_FORM_sig8:
12194 return "DW_FORM_sig8";
c906108c
SS
12195 default:
12196 return "DW_FORM_<unknown>";
12197 }
12198}
12199
12200/* Convert a DWARF stack opcode into its string name. */
12201
9eae7c52
TT
12202const char *
12203dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12204{
12205 switch (op)
12206 {
12207 case DW_OP_addr:
12208 return "DW_OP_addr";
12209 case DW_OP_deref:
12210 return "DW_OP_deref";
12211 case DW_OP_const1u:
12212 return "DW_OP_const1u";
12213 case DW_OP_const1s:
12214 return "DW_OP_const1s";
12215 case DW_OP_const2u:
12216 return "DW_OP_const2u";
12217 case DW_OP_const2s:
12218 return "DW_OP_const2s";
12219 case DW_OP_const4u:
12220 return "DW_OP_const4u";
12221 case DW_OP_const4s:
12222 return "DW_OP_const4s";
12223 case DW_OP_const8u:
12224 return "DW_OP_const8u";
12225 case DW_OP_const8s:
12226 return "DW_OP_const8s";
12227 case DW_OP_constu:
12228 return "DW_OP_constu";
12229 case DW_OP_consts:
12230 return "DW_OP_consts";
12231 case DW_OP_dup:
12232 return "DW_OP_dup";
12233 case DW_OP_drop:
12234 return "DW_OP_drop";
12235 case DW_OP_over:
12236 return "DW_OP_over";
12237 case DW_OP_pick:
12238 return "DW_OP_pick";
12239 case DW_OP_swap:
12240 return "DW_OP_swap";
12241 case DW_OP_rot:
12242 return "DW_OP_rot";
12243 case DW_OP_xderef:
12244 return "DW_OP_xderef";
12245 case DW_OP_abs:
12246 return "DW_OP_abs";
12247 case DW_OP_and:
12248 return "DW_OP_and";
12249 case DW_OP_div:
12250 return "DW_OP_div";
12251 case DW_OP_minus:
12252 return "DW_OP_minus";
12253 case DW_OP_mod:
12254 return "DW_OP_mod";
12255 case DW_OP_mul:
12256 return "DW_OP_mul";
12257 case DW_OP_neg:
12258 return "DW_OP_neg";
12259 case DW_OP_not:
12260 return "DW_OP_not";
12261 case DW_OP_or:
12262 return "DW_OP_or";
12263 case DW_OP_plus:
12264 return "DW_OP_plus";
12265 case DW_OP_plus_uconst:
12266 return "DW_OP_plus_uconst";
12267 case DW_OP_shl:
12268 return "DW_OP_shl";
12269 case DW_OP_shr:
12270 return "DW_OP_shr";
12271 case DW_OP_shra:
12272 return "DW_OP_shra";
12273 case DW_OP_xor:
12274 return "DW_OP_xor";
12275 case DW_OP_bra:
12276 return "DW_OP_bra";
12277 case DW_OP_eq:
12278 return "DW_OP_eq";
12279 case DW_OP_ge:
12280 return "DW_OP_ge";
12281 case DW_OP_gt:
12282 return "DW_OP_gt";
12283 case DW_OP_le:
12284 return "DW_OP_le";
12285 case DW_OP_lt:
12286 return "DW_OP_lt";
12287 case DW_OP_ne:
12288 return "DW_OP_ne";
12289 case DW_OP_skip:
12290 return "DW_OP_skip";
12291 case DW_OP_lit0:
12292 return "DW_OP_lit0";
12293 case DW_OP_lit1:
12294 return "DW_OP_lit1";
12295 case DW_OP_lit2:
12296 return "DW_OP_lit2";
12297 case DW_OP_lit3:
12298 return "DW_OP_lit3";
12299 case DW_OP_lit4:
12300 return "DW_OP_lit4";
12301 case DW_OP_lit5:
12302 return "DW_OP_lit5";
12303 case DW_OP_lit6:
12304 return "DW_OP_lit6";
12305 case DW_OP_lit7:
12306 return "DW_OP_lit7";
12307 case DW_OP_lit8:
12308 return "DW_OP_lit8";
12309 case DW_OP_lit9:
12310 return "DW_OP_lit9";
12311 case DW_OP_lit10:
12312 return "DW_OP_lit10";
12313 case DW_OP_lit11:
12314 return "DW_OP_lit11";
12315 case DW_OP_lit12:
12316 return "DW_OP_lit12";
12317 case DW_OP_lit13:
12318 return "DW_OP_lit13";
12319 case DW_OP_lit14:
12320 return "DW_OP_lit14";
12321 case DW_OP_lit15:
12322 return "DW_OP_lit15";
12323 case DW_OP_lit16:
12324 return "DW_OP_lit16";
12325 case DW_OP_lit17:
12326 return "DW_OP_lit17";
12327 case DW_OP_lit18:
12328 return "DW_OP_lit18";
12329 case DW_OP_lit19:
12330 return "DW_OP_lit19";
12331 case DW_OP_lit20:
12332 return "DW_OP_lit20";
12333 case DW_OP_lit21:
12334 return "DW_OP_lit21";
12335 case DW_OP_lit22:
12336 return "DW_OP_lit22";
12337 case DW_OP_lit23:
12338 return "DW_OP_lit23";
12339 case DW_OP_lit24:
12340 return "DW_OP_lit24";
12341 case DW_OP_lit25:
12342 return "DW_OP_lit25";
12343 case DW_OP_lit26:
12344 return "DW_OP_lit26";
12345 case DW_OP_lit27:
12346 return "DW_OP_lit27";
12347 case DW_OP_lit28:
12348 return "DW_OP_lit28";
12349 case DW_OP_lit29:
12350 return "DW_OP_lit29";
12351 case DW_OP_lit30:
12352 return "DW_OP_lit30";
12353 case DW_OP_lit31:
12354 return "DW_OP_lit31";
12355 case DW_OP_reg0:
12356 return "DW_OP_reg0";
12357 case DW_OP_reg1:
12358 return "DW_OP_reg1";
12359 case DW_OP_reg2:
12360 return "DW_OP_reg2";
12361 case DW_OP_reg3:
12362 return "DW_OP_reg3";
12363 case DW_OP_reg4:
12364 return "DW_OP_reg4";
12365 case DW_OP_reg5:
12366 return "DW_OP_reg5";
12367 case DW_OP_reg6:
12368 return "DW_OP_reg6";
12369 case DW_OP_reg7:
12370 return "DW_OP_reg7";
12371 case DW_OP_reg8:
12372 return "DW_OP_reg8";
12373 case DW_OP_reg9:
12374 return "DW_OP_reg9";
12375 case DW_OP_reg10:
12376 return "DW_OP_reg10";
12377 case DW_OP_reg11:
12378 return "DW_OP_reg11";
12379 case DW_OP_reg12:
12380 return "DW_OP_reg12";
12381 case DW_OP_reg13:
12382 return "DW_OP_reg13";
12383 case DW_OP_reg14:
12384 return "DW_OP_reg14";
12385 case DW_OP_reg15:
12386 return "DW_OP_reg15";
12387 case DW_OP_reg16:
12388 return "DW_OP_reg16";
12389 case DW_OP_reg17:
12390 return "DW_OP_reg17";
12391 case DW_OP_reg18:
12392 return "DW_OP_reg18";
12393 case DW_OP_reg19:
12394 return "DW_OP_reg19";
12395 case DW_OP_reg20:
12396 return "DW_OP_reg20";
12397 case DW_OP_reg21:
12398 return "DW_OP_reg21";
12399 case DW_OP_reg22:
12400 return "DW_OP_reg22";
12401 case DW_OP_reg23:
12402 return "DW_OP_reg23";
12403 case DW_OP_reg24:
12404 return "DW_OP_reg24";
12405 case DW_OP_reg25:
12406 return "DW_OP_reg25";
12407 case DW_OP_reg26:
12408 return "DW_OP_reg26";
12409 case DW_OP_reg27:
12410 return "DW_OP_reg27";
12411 case DW_OP_reg28:
12412 return "DW_OP_reg28";
12413 case DW_OP_reg29:
12414 return "DW_OP_reg29";
12415 case DW_OP_reg30:
12416 return "DW_OP_reg30";
12417 case DW_OP_reg31:
12418 return "DW_OP_reg31";
12419 case DW_OP_breg0:
12420 return "DW_OP_breg0";
12421 case DW_OP_breg1:
12422 return "DW_OP_breg1";
12423 case DW_OP_breg2:
12424 return "DW_OP_breg2";
12425 case DW_OP_breg3:
12426 return "DW_OP_breg3";
12427 case DW_OP_breg4:
12428 return "DW_OP_breg4";
12429 case DW_OP_breg5:
12430 return "DW_OP_breg5";
12431 case DW_OP_breg6:
12432 return "DW_OP_breg6";
12433 case DW_OP_breg7:
12434 return "DW_OP_breg7";
12435 case DW_OP_breg8:
12436 return "DW_OP_breg8";
12437 case DW_OP_breg9:
12438 return "DW_OP_breg9";
12439 case DW_OP_breg10:
12440 return "DW_OP_breg10";
12441 case DW_OP_breg11:
12442 return "DW_OP_breg11";
12443 case DW_OP_breg12:
12444 return "DW_OP_breg12";
12445 case DW_OP_breg13:
12446 return "DW_OP_breg13";
12447 case DW_OP_breg14:
12448 return "DW_OP_breg14";
12449 case DW_OP_breg15:
12450 return "DW_OP_breg15";
12451 case DW_OP_breg16:
12452 return "DW_OP_breg16";
12453 case DW_OP_breg17:
12454 return "DW_OP_breg17";
12455 case DW_OP_breg18:
12456 return "DW_OP_breg18";
12457 case DW_OP_breg19:
12458 return "DW_OP_breg19";
12459 case DW_OP_breg20:
12460 return "DW_OP_breg20";
12461 case DW_OP_breg21:
12462 return "DW_OP_breg21";
12463 case DW_OP_breg22:
12464 return "DW_OP_breg22";
12465 case DW_OP_breg23:
12466 return "DW_OP_breg23";
12467 case DW_OP_breg24:
12468 return "DW_OP_breg24";
12469 case DW_OP_breg25:
12470 return "DW_OP_breg25";
12471 case DW_OP_breg26:
12472 return "DW_OP_breg26";
12473 case DW_OP_breg27:
12474 return "DW_OP_breg27";
12475 case DW_OP_breg28:
12476 return "DW_OP_breg28";
12477 case DW_OP_breg29:
12478 return "DW_OP_breg29";
12479 case DW_OP_breg30:
12480 return "DW_OP_breg30";
12481 case DW_OP_breg31:
12482 return "DW_OP_breg31";
12483 case DW_OP_regx:
12484 return "DW_OP_regx";
12485 case DW_OP_fbreg:
12486 return "DW_OP_fbreg";
12487 case DW_OP_bregx:
12488 return "DW_OP_bregx";
12489 case DW_OP_piece:
12490 return "DW_OP_piece";
12491 case DW_OP_deref_size:
12492 return "DW_OP_deref_size";
12493 case DW_OP_xderef_size:
12494 return "DW_OP_xderef_size";
12495 case DW_OP_nop:
12496 return "DW_OP_nop";
b7619582 12497 /* DWARF 3 extensions. */
ed348acc
EZ
12498 case DW_OP_push_object_address:
12499 return "DW_OP_push_object_address";
12500 case DW_OP_call2:
12501 return "DW_OP_call2";
12502 case DW_OP_call4:
12503 return "DW_OP_call4";
12504 case DW_OP_call_ref:
12505 return "DW_OP_call_ref";
b7619582
GF
12506 case DW_OP_form_tls_address:
12507 return "DW_OP_form_tls_address";
12508 case DW_OP_call_frame_cfa:
12509 return "DW_OP_call_frame_cfa";
12510 case DW_OP_bit_piece:
12511 return "DW_OP_bit_piece";
9eae7c52
TT
12512 /* DWARF 4 extensions. */
12513 case DW_OP_implicit_value:
12514 return "DW_OP_implicit_value";
12515 case DW_OP_stack_value:
12516 return "DW_OP_stack_value";
12517 /* GNU extensions. */
ed348acc
EZ
12518 case DW_OP_GNU_push_tls_address:
12519 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12520 case DW_OP_GNU_uninit:
12521 return "DW_OP_GNU_uninit";
c906108c 12522 default:
9eae7c52 12523 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12524 }
12525}
12526
12527static char *
fba45db2 12528dwarf_bool_name (unsigned mybool)
c906108c
SS
12529{
12530 if (mybool)
12531 return "TRUE";
12532 else
12533 return "FALSE";
12534}
12535
12536/* Convert a DWARF type code into its string name. */
12537
12538static char *
aa1ee363 12539dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12540{
12541 switch (enc)
12542 {
b7619582
GF
12543 case DW_ATE_void:
12544 return "DW_ATE_void";
c906108c
SS
12545 case DW_ATE_address:
12546 return "DW_ATE_address";
12547 case DW_ATE_boolean:
12548 return "DW_ATE_boolean";
12549 case DW_ATE_complex_float:
12550 return "DW_ATE_complex_float";
12551 case DW_ATE_float:
12552 return "DW_ATE_float";
12553 case DW_ATE_signed:
12554 return "DW_ATE_signed";
12555 case DW_ATE_signed_char:
12556 return "DW_ATE_signed_char";
12557 case DW_ATE_unsigned:
12558 return "DW_ATE_unsigned";
12559 case DW_ATE_unsigned_char:
12560 return "DW_ATE_unsigned_char";
b7619582 12561 /* DWARF 3. */
d9fa45fe
DC
12562 case DW_ATE_imaginary_float:
12563 return "DW_ATE_imaginary_float";
b7619582
GF
12564 case DW_ATE_packed_decimal:
12565 return "DW_ATE_packed_decimal";
12566 case DW_ATE_numeric_string:
12567 return "DW_ATE_numeric_string";
12568 case DW_ATE_edited:
12569 return "DW_ATE_edited";
12570 case DW_ATE_signed_fixed:
12571 return "DW_ATE_signed_fixed";
12572 case DW_ATE_unsigned_fixed:
12573 return "DW_ATE_unsigned_fixed";
12574 case DW_ATE_decimal_float:
12575 return "DW_ATE_decimal_float";
75079b2b
TT
12576 /* DWARF 4. */
12577 case DW_ATE_UTF:
12578 return "DW_ATE_UTF";
b7619582
GF
12579 /* HP extensions. */
12580 case DW_ATE_HP_float80:
12581 return "DW_ATE_HP_float80";
12582 case DW_ATE_HP_complex_float80:
12583 return "DW_ATE_HP_complex_float80";
12584 case DW_ATE_HP_float128:
12585 return "DW_ATE_HP_float128";
12586 case DW_ATE_HP_complex_float128:
12587 return "DW_ATE_HP_complex_float128";
12588 case DW_ATE_HP_floathpintel:
12589 return "DW_ATE_HP_floathpintel";
12590 case DW_ATE_HP_imaginary_float80:
12591 return "DW_ATE_HP_imaginary_float80";
12592 case DW_ATE_HP_imaginary_float128:
12593 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12594 default:
12595 return "DW_ATE_<unknown>";
12596 }
12597}
12598
12599/* Convert a DWARF call frame info operation to its string name. */
12600
12601#if 0
12602static char *
aa1ee363 12603dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12604{
12605 switch (cfi_opc)
12606 {
12607 case DW_CFA_advance_loc:
12608 return "DW_CFA_advance_loc";
12609 case DW_CFA_offset:
12610 return "DW_CFA_offset";
12611 case DW_CFA_restore:
12612 return "DW_CFA_restore";
12613 case DW_CFA_nop:
12614 return "DW_CFA_nop";
12615 case DW_CFA_set_loc:
12616 return "DW_CFA_set_loc";
12617 case DW_CFA_advance_loc1:
12618 return "DW_CFA_advance_loc1";
12619 case DW_CFA_advance_loc2:
12620 return "DW_CFA_advance_loc2";
12621 case DW_CFA_advance_loc4:
12622 return "DW_CFA_advance_loc4";
12623 case DW_CFA_offset_extended:
12624 return "DW_CFA_offset_extended";
12625 case DW_CFA_restore_extended:
12626 return "DW_CFA_restore_extended";
12627 case DW_CFA_undefined:
12628 return "DW_CFA_undefined";
12629 case DW_CFA_same_value:
12630 return "DW_CFA_same_value";
12631 case DW_CFA_register:
12632 return "DW_CFA_register";
12633 case DW_CFA_remember_state:
12634 return "DW_CFA_remember_state";
12635 case DW_CFA_restore_state:
12636 return "DW_CFA_restore_state";
12637 case DW_CFA_def_cfa:
12638 return "DW_CFA_def_cfa";
12639 case DW_CFA_def_cfa_register:
12640 return "DW_CFA_def_cfa_register";
12641 case DW_CFA_def_cfa_offset:
12642 return "DW_CFA_def_cfa_offset";
b7619582 12643 /* DWARF 3. */
985cb1a3
JM
12644 case DW_CFA_def_cfa_expression:
12645 return "DW_CFA_def_cfa_expression";
12646 case DW_CFA_expression:
12647 return "DW_CFA_expression";
12648 case DW_CFA_offset_extended_sf:
12649 return "DW_CFA_offset_extended_sf";
12650 case DW_CFA_def_cfa_sf:
12651 return "DW_CFA_def_cfa_sf";
12652 case DW_CFA_def_cfa_offset_sf:
12653 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12654 case DW_CFA_val_offset:
12655 return "DW_CFA_val_offset";
12656 case DW_CFA_val_offset_sf:
12657 return "DW_CFA_val_offset_sf";
12658 case DW_CFA_val_expression:
12659 return "DW_CFA_val_expression";
12660 /* SGI/MIPS specific. */
c906108c
SS
12661 case DW_CFA_MIPS_advance_loc8:
12662 return "DW_CFA_MIPS_advance_loc8";
b7619582 12663 /* GNU extensions. */
985cb1a3
JM
12664 case DW_CFA_GNU_window_save:
12665 return "DW_CFA_GNU_window_save";
12666 case DW_CFA_GNU_args_size:
12667 return "DW_CFA_GNU_args_size";
12668 case DW_CFA_GNU_negative_offset_extended:
12669 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12670 default:
12671 return "DW_CFA_<unknown>";
12672 }
12673}
12674#endif
12675
f9aca02d 12676static void
d97bc12b 12677dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12678{
12679 unsigned int i;
12680
d97bc12b
DE
12681 print_spaces (indent, f);
12682 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12683 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12684
12685 if (die->parent != NULL)
12686 {
12687 print_spaces (indent, f);
12688 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12689 die->parent->offset);
12690 }
12691
12692 print_spaces (indent, f);
12693 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12694 dwarf_bool_name (die->child != NULL));
c906108c 12695
d97bc12b
DE
12696 print_spaces (indent, f);
12697 fprintf_unfiltered (f, " attributes:\n");
12698
c906108c
SS
12699 for (i = 0; i < die->num_attrs; ++i)
12700 {
d97bc12b
DE
12701 print_spaces (indent, f);
12702 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12703 dwarf_attr_name (die->attrs[i].name),
12704 dwarf_form_name (die->attrs[i].form));
d97bc12b 12705
c906108c
SS
12706 switch (die->attrs[i].form)
12707 {
12708 case DW_FORM_ref_addr:
12709 case DW_FORM_addr:
d97bc12b 12710 fprintf_unfiltered (f, "address: ");
5af949e3 12711 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12712 break;
12713 case DW_FORM_block2:
12714 case DW_FORM_block4:
12715 case DW_FORM_block:
12716 case DW_FORM_block1:
d97bc12b 12717 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12718 break;
2dc7f7b3
TT
12719 case DW_FORM_exprloc:
12720 fprintf_unfiltered (f, "expression: size %u",
12721 DW_BLOCK (&die->attrs[i])->size);
12722 break;
10b3939b
DJ
12723 case DW_FORM_ref1:
12724 case DW_FORM_ref2:
12725 case DW_FORM_ref4:
d97bc12b 12726 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12727 (long) (DW_ADDR (&die->attrs[i])));
12728 break;
c906108c
SS
12729 case DW_FORM_data1:
12730 case DW_FORM_data2:
12731 case DW_FORM_data4:
ce5d95e1 12732 case DW_FORM_data8:
c906108c
SS
12733 case DW_FORM_udata:
12734 case DW_FORM_sdata:
43bbcdc2
PH
12735 fprintf_unfiltered (f, "constant: %s",
12736 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12737 break;
2dc7f7b3
TT
12738 case DW_FORM_sec_offset:
12739 fprintf_unfiltered (f, "section offset: %s",
12740 pulongest (DW_UNSND (&die->attrs[i])));
12741 break;
348e048f
DE
12742 case DW_FORM_sig8:
12743 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12744 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12745 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12746 else
12747 fprintf_unfiltered (f, "signatured type, offset: unknown");
12748 break;
c906108c 12749 case DW_FORM_string:
4bdf3d34 12750 case DW_FORM_strp:
8285870a 12751 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12752 DW_STRING (&die->attrs[i])
8285870a
JK
12753 ? DW_STRING (&die->attrs[i]) : "",
12754 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12755 break;
12756 case DW_FORM_flag:
12757 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12758 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12759 else
d97bc12b 12760 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12761 break;
2dc7f7b3
TT
12762 case DW_FORM_flag_present:
12763 fprintf_unfiltered (f, "flag: TRUE");
12764 break;
a8329558
KW
12765 case DW_FORM_indirect:
12766 /* the reader will have reduced the indirect form to
12767 the "base form" so this form should not occur */
d97bc12b 12768 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12769 break;
c906108c 12770 default:
d97bc12b 12771 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12772 die->attrs[i].form);
d97bc12b 12773 break;
c906108c 12774 }
d97bc12b 12775 fprintf_unfiltered (f, "\n");
c906108c
SS
12776 }
12777}
12778
f9aca02d 12779static void
d97bc12b 12780dump_die_for_error (struct die_info *die)
c906108c 12781{
d97bc12b
DE
12782 dump_die_shallow (gdb_stderr, 0, die);
12783}
12784
12785static void
12786dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12787{
12788 int indent = level * 4;
12789
12790 gdb_assert (die != NULL);
12791
12792 if (level >= max_level)
12793 return;
12794
12795 dump_die_shallow (f, indent, die);
12796
12797 if (die->child != NULL)
c906108c 12798 {
d97bc12b
DE
12799 print_spaces (indent, f);
12800 fprintf_unfiltered (f, " Children:");
12801 if (level + 1 < max_level)
12802 {
12803 fprintf_unfiltered (f, "\n");
12804 dump_die_1 (f, level + 1, max_level, die->child);
12805 }
12806 else
12807 {
12808 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12809 }
12810 }
12811
12812 if (die->sibling != NULL && level > 0)
12813 {
12814 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12815 }
12816}
12817
d97bc12b
DE
12818/* This is called from the pdie macro in gdbinit.in.
12819 It's not static so gcc will keep a copy callable from gdb. */
12820
12821void
12822dump_die (struct die_info *die, int max_level)
12823{
12824 dump_die_1 (gdb_stdlog, 0, max_level, die);
12825}
12826
f9aca02d 12827static void
51545339 12828store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12829{
51545339 12830 void **slot;
c906108c 12831
51545339
DJ
12832 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12833
12834 *slot = die;
c906108c
SS
12835}
12836
93311388
DE
12837static int
12838is_ref_attr (struct attribute *attr)
c906108c 12839{
c906108c
SS
12840 switch (attr->form)
12841 {
12842 case DW_FORM_ref_addr:
c906108c
SS
12843 case DW_FORM_ref1:
12844 case DW_FORM_ref2:
12845 case DW_FORM_ref4:
613e1657 12846 case DW_FORM_ref8:
c906108c 12847 case DW_FORM_ref_udata:
93311388 12848 return 1;
c906108c 12849 default:
93311388 12850 return 0;
c906108c 12851 }
93311388
DE
12852}
12853
12854static unsigned int
12855dwarf2_get_ref_die_offset (struct attribute *attr)
12856{
12857 if (is_ref_attr (attr))
12858 return DW_ADDR (attr);
12859
12860 complaint (&symfile_complaints,
12861 _("unsupported die ref attribute form: '%s'"),
12862 dwarf_form_name (attr->form));
12863 return 0;
c906108c
SS
12864}
12865
43bbcdc2
PH
12866/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12867 * the value held by the attribute is not constant. */
a02abb62 12868
43bbcdc2 12869static LONGEST
a02abb62
JB
12870dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12871{
12872 if (attr->form == DW_FORM_sdata)
12873 return DW_SND (attr);
12874 else if (attr->form == DW_FORM_udata
12875 || attr->form == DW_FORM_data1
12876 || attr->form == DW_FORM_data2
12877 || attr->form == DW_FORM_data4
12878 || attr->form == DW_FORM_data8)
12879 return DW_UNSND (attr);
12880 else
12881 {
e2e0b3e5 12882 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12883 dwarf_form_name (attr->form));
12884 return default_value;
12885 }
12886}
12887
03dd20cc 12888/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12889 unit and add it to our queue.
12890 The result is non-zero if PER_CU was queued, otherwise the result is zero
12891 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12892
348e048f 12893static int
03dd20cc
DJ
12894maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12895 struct dwarf2_per_cu_data *per_cu)
12896{
98bfdba5
PA
12897 /* We may arrive here during partial symbol reading, if we need full
12898 DIEs to process an unusual case (e.g. template arguments). Do
12899 not queue PER_CU, just tell our caller to load its DIEs. */
12900 if (dwarf2_per_objfile->reading_partial_symbols)
12901 {
12902 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12903 return 1;
12904 return 0;
12905 }
12906
03dd20cc
DJ
12907 /* Mark the dependence relation so that we don't flush PER_CU
12908 too early. */
12909 dwarf2_add_dependence (this_cu, per_cu);
12910
12911 /* If it's already on the queue, we have nothing to do. */
12912 if (per_cu->queued)
348e048f 12913 return 0;
03dd20cc
DJ
12914
12915 /* If the compilation unit is already loaded, just mark it as
12916 used. */
12917 if (per_cu->cu != NULL)
12918 {
12919 per_cu->cu->last_used = 0;
348e048f 12920 return 0;
03dd20cc
DJ
12921 }
12922
12923 /* Add it to the queue. */
12924 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12925
12926 return 1;
12927}
12928
12929/* Follow reference or signature attribute ATTR of SRC_DIE.
12930 On entry *REF_CU is the CU of SRC_DIE.
12931 On exit *REF_CU is the CU of the result. */
12932
12933static struct die_info *
12934follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12935 struct dwarf2_cu **ref_cu)
12936{
12937 struct die_info *die;
12938
12939 if (is_ref_attr (attr))
12940 die = follow_die_ref (src_die, attr, ref_cu);
12941 else if (attr->form == DW_FORM_sig8)
12942 die = follow_die_sig (src_die, attr, ref_cu);
12943 else
12944 {
12945 dump_die_for_error (src_die);
12946 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12947 (*ref_cu)->objfile->name);
12948 }
12949
12950 return die;
03dd20cc
DJ
12951}
12952
5c631832 12953/* Follow reference OFFSET.
673bfd45
DE
12954 On entry *REF_CU is the CU of the source die referencing OFFSET.
12955 On exit *REF_CU is the CU of the result.
12956 Returns NULL if OFFSET is invalid. */
f504f079 12957
f9aca02d 12958static struct die_info *
5c631832 12959follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12960{
10b3939b 12961 struct die_info temp_die;
f2f0e013 12962 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12963
348e048f
DE
12964 gdb_assert (cu->per_cu != NULL);
12965
98bfdba5
PA
12966 target_cu = cu;
12967
348e048f
DE
12968 if (cu->per_cu->from_debug_types)
12969 {
12970 /* .debug_types CUs cannot reference anything outside their CU.
12971 If they need to, they have to reference a signatured type via
12972 DW_FORM_sig8. */
12973 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12974 return NULL;
348e048f
DE
12975 }
12976 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12977 {
12978 struct dwarf2_per_cu_data *per_cu;
9a619af0 12979
45452591 12980 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12981
12982 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12983 if (maybe_queue_comp_unit (cu, per_cu))
12984 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12985
10b3939b
DJ
12986 target_cu = per_cu->cu;
12987 }
98bfdba5
PA
12988 else if (cu->dies == NULL)
12989 {
12990 /* We're loading full DIEs during partial symbol reading. */
12991 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12992 load_full_comp_unit (cu->per_cu, cu->objfile);
12993 }
c906108c 12994
f2f0e013 12995 *ref_cu = target_cu;
51545339 12996 temp_die.offset = offset;
5c631832
JK
12997 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12998}
10b3939b 12999
5c631832
JK
13000/* Follow reference attribute ATTR of SRC_DIE.
13001 On entry *REF_CU is the CU of SRC_DIE.
13002 On exit *REF_CU is the CU of the result. */
13003
13004static struct die_info *
13005follow_die_ref (struct die_info *src_die, struct attribute *attr,
13006 struct dwarf2_cu **ref_cu)
13007{
13008 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13009 struct dwarf2_cu *cu = *ref_cu;
13010 struct die_info *die;
13011
13012 die = follow_die_offset (offset, ref_cu);
13013 if (!die)
13014 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13015 "at 0x%x [in module %s]"),
13016 offset, src_die->offset, cu->objfile->name);
348e048f 13017
5c631832
JK
13018 return die;
13019}
13020
13021/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13022 value is intended for DW_OP_call*. */
13023
13024struct dwarf2_locexpr_baton
13025dwarf2_fetch_die_location_block (unsigned int offset,
13026 struct dwarf2_per_cu_data *per_cu)
13027{
13028 struct dwarf2_cu *cu = per_cu->cu;
13029 struct die_info *die;
13030 struct attribute *attr;
13031 struct dwarf2_locexpr_baton retval;
13032
13033 die = follow_die_offset (offset, &cu);
13034 if (!die)
13035 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13036 offset, per_cu->cu->objfile->name);
13037
13038 attr = dwarf2_attr (die, DW_AT_location, cu);
13039 if (!attr)
13040 {
13041 /* DWARF: "If there is no such attribute, then there is no effect.". */
13042
13043 retval.data = NULL;
13044 retval.size = 0;
13045 }
13046 else
13047 {
13048 if (!attr_form_is_block (attr))
13049 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13050 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13051 offset, per_cu->cu->objfile->name);
13052
13053 retval.data = DW_BLOCK (attr)->data;
13054 retval.size = DW_BLOCK (attr)->size;
13055 }
13056 retval.per_cu = cu->per_cu;
13057 return retval;
348e048f
DE
13058}
13059
13060/* Follow the signature attribute ATTR in SRC_DIE.
13061 On entry *REF_CU is the CU of SRC_DIE.
13062 On exit *REF_CU is the CU of the result. */
13063
13064static struct die_info *
13065follow_die_sig (struct die_info *src_die, struct attribute *attr,
13066 struct dwarf2_cu **ref_cu)
13067{
13068 struct objfile *objfile = (*ref_cu)->objfile;
13069 struct die_info temp_die;
13070 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13071 struct dwarf2_cu *sig_cu;
13072 struct die_info *die;
13073
13074 /* sig_type will be NULL if the signatured type is missing from
13075 the debug info. */
13076 if (sig_type == NULL)
13077 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13078 "at 0x%x [in module %s]"),
13079 src_die->offset, objfile->name);
13080
13081 /* If necessary, add it to the queue and load its DIEs. */
13082
13083 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13084 read_signatured_type (objfile, sig_type);
13085
13086 gdb_assert (sig_type->per_cu.cu != NULL);
13087
13088 sig_cu = sig_type->per_cu.cu;
13089 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13090 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13091 if (die)
13092 {
13093 *ref_cu = sig_cu;
13094 return die;
13095 }
13096
13097 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13098 "at 0x%x [in module %s]"),
13099 sig_type->type_offset, src_die->offset, objfile->name);
13100}
13101
13102/* Given an offset of a signatured type, return its signatured_type. */
13103
13104static struct signatured_type *
13105lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13106{
13107 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13108 unsigned int length, initial_length_size;
13109 unsigned int sig_offset;
13110 struct signatured_type find_entry, *type_sig;
13111
13112 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13113 sig_offset = (initial_length_size
13114 + 2 /*version*/
13115 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13116 + 1 /*address_size*/);
13117 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13118 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13119
13120 /* This is only used to lookup previously recorded types.
13121 If we didn't find it, it's our bug. */
13122 gdb_assert (type_sig != NULL);
13123 gdb_assert (offset == type_sig->offset);
13124
13125 return type_sig;
13126}
13127
13128/* Read in signatured type at OFFSET and build its CU and die(s). */
13129
13130static void
13131read_signatured_type_at_offset (struct objfile *objfile,
13132 unsigned int offset)
13133{
13134 struct signatured_type *type_sig;
13135
be391dca
TT
13136 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13137
348e048f
DE
13138 /* We have the section offset, but we need the signature to do the
13139 hash table lookup. */
13140 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13141
13142 gdb_assert (type_sig->per_cu.cu == NULL);
13143
13144 read_signatured_type (objfile, type_sig);
13145
13146 gdb_assert (type_sig->per_cu.cu != NULL);
13147}
13148
13149/* Read in a signatured type and build its CU and DIEs. */
13150
13151static void
13152read_signatured_type (struct objfile *objfile,
13153 struct signatured_type *type_sig)
13154{
1fd400ff 13155 gdb_byte *types_ptr;
348e048f
DE
13156 struct die_reader_specs reader_specs;
13157 struct dwarf2_cu *cu;
13158 ULONGEST signature;
13159 struct cleanup *back_to, *free_cu_cleanup;
13160 struct attribute *attr;
13161
1fd400ff
TT
13162 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13163 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13164
348e048f
DE
13165 gdb_assert (type_sig->per_cu.cu == NULL);
13166
13167 cu = xmalloc (sizeof (struct dwarf2_cu));
13168 memset (cu, 0, sizeof (struct dwarf2_cu));
13169 obstack_init (&cu->comp_unit_obstack);
13170 cu->objfile = objfile;
13171 type_sig->per_cu.cu = cu;
13172 cu->per_cu = &type_sig->per_cu;
13173
13174 /* If an error occurs while loading, release our storage. */
13175 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13176
13177 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13178 types_ptr, objfile->obfd);
13179 gdb_assert (signature == type_sig->signature);
13180
13181 cu->die_hash
13182 = htab_create_alloc_ex (cu->header.length / 12,
13183 die_hash,
13184 die_eq,
13185 NULL,
13186 &cu->comp_unit_obstack,
13187 hashtab_obstack_allocate,
13188 dummy_obstack_deallocate);
13189
13190 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13191 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13192
13193 init_cu_die_reader (&reader_specs, cu);
13194
13195 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13196 NULL /*parent*/);
13197
13198 /* We try not to read any attributes in this function, because not
13199 all objfiles needed for references have been loaded yet, and symbol
13200 table processing isn't initialized. But we have to set the CU language,
13201 or we won't be able to build types correctly. */
13202 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13203 if (attr)
13204 set_cu_language (DW_UNSND (attr), cu);
13205 else
13206 set_cu_language (language_minimal, cu);
13207
13208 do_cleanups (back_to);
13209
13210 /* We've successfully allocated this compilation unit. Let our caller
13211 clean it up when finished with it. */
13212 discard_cleanups (free_cu_cleanup);
13213
13214 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13215 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13216}
13217
c906108c
SS
13218/* Decode simple location descriptions.
13219 Given a pointer to a dwarf block that defines a location, compute
13220 the location and return the value.
13221
4cecd739
DJ
13222 NOTE drow/2003-11-18: This function is called in two situations
13223 now: for the address of static or global variables (partial symbols
13224 only) and for offsets into structures which are expected to be
13225 (more or less) constant. The partial symbol case should go away,
13226 and only the constant case should remain. That will let this
13227 function complain more accurately. A few special modes are allowed
13228 without complaint for global variables (for instance, global
13229 register values and thread-local values).
c906108c
SS
13230
13231 A location description containing no operations indicates that the
4cecd739 13232 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13233 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13234 callers will only want a very basic result and this can become a
13235 complaint.
c906108c 13236
c906108c
SS
13237 Note that stack[0] is unused except as a default error return.
13238 Note that stack overflow is not yet handled. */
13239
13240static CORE_ADDR
e7c27a73 13241decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13242{
e7c27a73 13243 struct objfile *objfile = cu->objfile;
c906108c
SS
13244 int i;
13245 int size = blk->size;
fe1b8b76 13246 gdb_byte *data = blk->data;
c906108c
SS
13247 CORE_ADDR stack[64];
13248 int stacki;
13249 unsigned int bytes_read, unsnd;
fe1b8b76 13250 gdb_byte op;
c906108c
SS
13251
13252 i = 0;
13253 stacki = 0;
13254 stack[stacki] = 0;
c906108c
SS
13255
13256 while (i < size)
13257 {
c906108c
SS
13258 op = data[i++];
13259 switch (op)
13260 {
f1bea926
JM
13261 case DW_OP_lit0:
13262 case DW_OP_lit1:
13263 case DW_OP_lit2:
13264 case DW_OP_lit3:
13265 case DW_OP_lit4:
13266 case DW_OP_lit5:
13267 case DW_OP_lit6:
13268 case DW_OP_lit7:
13269 case DW_OP_lit8:
13270 case DW_OP_lit9:
13271 case DW_OP_lit10:
13272 case DW_OP_lit11:
13273 case DW_OP_lit12:
13274 case DW_OP_lit13:
13275 case DW_OP_lit14:
13276 case DW_OP_lit15:
13277 case DW_OP_lit16:
13278 case DW_OP_lit17:
13279 case DW_OP_lit18:
13280 case DW_OP_lit19:
13281 case DW_OP_lit20:
13282 case DW_OP_lit21:
13283 case DW_OP_lit22:
13284 case DW_OP_lit23:
13285 case DW_OP_lit24:
13286 case DW_OP_lit25:
13287 case DW_OP_lit26:
13288 case DW_OP_lit27:
13289 case DW_OP_lit28:
13290 case DW_OP_lit29:
13291 case DW_OP_lit30:
13292 case DW_OP_lit31:
13293 stack[++stacki] = op - DW_OP_lit0;
13294 break;
13295
c906108c
SS
13296 case DW_OP_reg0:
13297 case DW_OP_reg1:
13298 case DW_OP_reg2:
13299 case DW_OP_reg3:
13300 case DW_OP_reg4:
13301 case DW_OP_reg5:
13302 case DW_OP_reg6:
13303 case DW_OP_reg7:
13304 case DW_OP_reg8:
13305 case DW_OP_reg9:
13306 case DW_OP_reg10:
13307 case DW_OP_reg11:
13308 case DW_OP_reg12:
13309 case DW_OP_reg13:
13310 case DW_OP_reg14:
13311 case DW_OP_reg15:
13312 case DW_OP_reg16:
13313 case DW_OP_reg17:
13314 case DW_OP_reg18:
13315 case DW_OP_reg19:
13316 case DW_OP_reg20:
13317 case DW_OP_reg21:
13318 case DW_OP_reg22:
13319 case DW_OP_reg23:
13320 case DW_OP_reg24:
13321 case DW_OP_reg25:
13322 case DW_OP_reg26:
13323 case DW_OP_reg27:
13324 case DW_OP_reg28:
13325 case DW_OP_reg29:
13326 case DW_OP_reg30:
13327 case DW_OP_reg31:
c906108c 13328 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13329 if (i < size)
13330 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13331 break;
13332
13333 case DW_OP_regx:
c906108c
SS
13334 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13335 i += bytes_read;
c906108c 13336 stack[++stacki] = unsnd;
4cecd739
DJ
13337 if (i < size)
13338 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13339 break;
13340
13341 case DW_OP_addr:
107d2387 13342 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13343 cu, &bytes_read);
107d2387 13344 i += bytes_read;
c906108c
SS
13345 break;
13346
13347 case DW_OP_const1u:
13348 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13349 i += 1;
13350 break;
13351
13352 case DW_OP_const1s:
13353 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13354 i += 1;
13355 break;
13356
13357 case DW_OP_const2u:
13358 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13359 i += 2;
13360 break;
13361
13362 case DW_OP_const2s:
13363 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13364 i += 2;
13365 break;
13366
13367 case DW_OP_const4u:
13368 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13369 i += 4;
13370 break;
13371
13372 case DW_OP_const4s:
13373 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13374 i += 4;
13375 break;
13376
13377 case DW_OP_constu:
13378 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13379 &bytes_read);
c906108c
SS
13380 i += bytes_read;
13381 break;
13382
13383 case DW_OP_consts:
13384 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13385 i += bytes_read;
13386 break;
13387
f1bea926
JM
13388 case DW_OP_dup:
13389 stack[stacki + 1] = stack[stacki];
13390 stacki++;
13391 break;
13392
c906108c
SS
13393 case DW_OP_plus:
13394 stack[stacki - 1] += stack[stacki];
13395 stacki--;
13396 break;
13397
13398 case DW_OP_plus_uconst:
13399 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13400 i += bytes_read;
13401 break;
13402
13403 case DW_OP_minus:
f1bea926 13404 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13405 stacki--;
13406 break;
13407
7a292a7a 13408 case DW_OP_deref:
7a292a7a 13409 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13410 this using GDB's address_class enum. This is valid for partial
13411 global symbols, although the variable's address will be bogus
13412 in the psymtab. */
7a292a7a 13413 if (i < size)
4d3c2250 13414 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13415 break;
13416
9d774e44 13417 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13418 /* The top of the stack has the offset from the beginning
13419 of the thread control block at which the variable is located. */
13420 /* Nothing should follow this operator, so the top of stack would
13421 be returned. */
4cecd739
DJ
13422 /* This is valid for partial global symbols, but the variable's
13423 address will be bogus in the psymtab. */
9d774e44 13424 if (i < size)
4d3c2250 13425 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13426 break;
13427
42be36b3
CT
13428 case DW_OP_GNU_uninit:
13429 break;
13430
c906108c 13431 default:
e2e0b3e5 13432 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13433 dwarf_stack_op_name (op, 1));
c906108c
SS
13434 return (stack[stacki]);
13435 }
13436 }
13437 return (stack[stacki]);
13438}
13439
13440/* memory allocation interface */
13441
c906108c 13442static struct dwarf_block *
7b5a2f43 13443dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13444{
13445 struct dwarf_block *blk;
13446
13447 blk = (struct dwarf_block *)
7b5a2f43 13448 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13449 return (blk);
13450}
13451
13452static struct abbrev_info *
f3dd6933 13453dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13454{
13455 struct abbrev_info *abbrev;
13456
f3dd6933
DJ
13457 abbrev = (struct abbrev_info *)
13458 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13459 memset (abbrev, 0, sizeof (struct abbrev_info));
13460 return (abbrev);
13461}
13462
13463static struct die_info *
b60c80d6 13464dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13465{
13466 struct die_info *die;
b60c80d6
DJ
13467 size_t size = sizeof (struct die_info);
13468
13469 if (num_attrs > 1)
13470 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13471
b60c80d6 13472 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13473 memset (die, 0, sizeof (struct die_info));
13474 return (die);
13475}
2e276125
JB
13476
13477\f
13478/* Macro support. */
13479
13480
13481/* Return the full name of file number I in *LH's file name table.
13482 Use COMP_DIR as the name of the current directory of the
13483 compilation. The result is allocated using xmalloc; the caller is
13484 responsible for freeing it. */
13485static char *
13486file_full_name (int file, struct line_header *lh, const char *comp_dir)
13487{
6a83a1e6
EZ
13488 /* Is the file number a valid index into the line header's file name
13489 table? Remember that file numbers start with one, not zero. */
13490 if (1 <= file && file <= lh->num_file_names)
13491 {
13492 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13493
6a83a1e6
EZ
13494 if (IS_ABSOLUTE_PATH (fe->name))
13495 return xstrdup (fe->name);
13496 else
13497 {
13498 const char *dir;
13499 int dir_len;
13500 char *full_name;
13501
13502 if (fe->dir_index)
13503 dir = lh->include_dirs[fe->dir_index - 1];
13504 else
13505 dir = comp_dir;
13506
13507 if (dir)
13508 {
13509 dir_len = strlen (dir);
13510 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13511 strcpy (full_name, dir);
13512 full_name[dir_len] = '/';
13513 strcpy (full_name + dir_len + 1, fe->name);
13514 return full_name;
13515 }
13516 else
13517 return xstrdup (fe->name);
13518 }
13519 }
2e276125
JB
13520 else
13521 {
6a83a1e6
EZ
13522 /* The compiler produced a bogus file number. We can at least
13523 record the macro definitions made in the file, even if we
13524 won't be able to find the file by name. */
13525 char fake_name[80];
9a619af0 13526
6a83a1e6 13527 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13528
6e70227d 13529 complaint (&symfile_complaints,
6a83a1e6
EZ
13530 _("bad file number in macro information (%d)"),
13531 file);
2e276125 13532
6a83a1e6 13533 return xstrdup (fake_name);
2e276125
JB
13534 }
13535}
13536
13537
13538static struct macro_source_file *
13539macro_start_file (int file, int line,
13540 struct macro_source_file *current_file,
13541 const char *comp_dir,
13542 struct line_header *lh, struct objfile *objfile)
13543{
13544 /* The full name of this source file. */
13545 char *full_name = file_full_name (file, lh, comp_dir);
13546
13547 /* We don't create a macro table for this compilation unit
13548 at all until we actually get a filename. */
13549 if (! pending_macros)
4a146b47 13550 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13551 objfile->macro_cache);
2e276125
JB
13552
13553 if (! current_file)
13554 /* If we have no current file, then this must be the start_file
13555 directive for the compilation unit's main source file. */
13556 current_file = macro_set_main (pending_macros, full_name);
13557 else
13558 current_file = macro_include (current_file, line, full_name);
13559
13560 xfree (full_name);
6e70227d 13561
2e276125
JB
13562 return current_file;
13563}
13564
13565
13566/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13567 followed by a null byte. */
13568static char *
13569copy_string (const char *buf, int len)
13570{
13571 char *s = xmalloc (len + 1);
9a619af0 13572
2e276125
JB
13573 memcpy (s, buf, len);
13574 s[len] = '\0';
2e276125
JB
13575 return s;
13576}
13577
13578
13579static const char *
13580consume_improper_spaces (const char *p, const char *body)
13581{
13582 if (*p == ' ')
13583 {
4d3c2250 13584 complaint (&symfile_complaints,
e2e0b3e5 13585 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13586 body);
2e276125
JB
13587
13588 while (*p == ' ')
13589 p++;
13590 }
13591
13592 return p;
13593}
13594
13595
13596static void
13597parse_macro_definition (struct macro_source_file *file, int line,
13598 const char *body)
13599{
13600 const char *p;
13601
13602 /* The body string takes one of two forms. For object-like macro
13603 definitions, it should be:
13604
13605 <macro name> " " <definition>
13606
13607 For function-like macro definitions, it should be:
13608
13609 <macro name> "() " <definition>
13610 or
13611 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13612
13613 Spaces may appear only where explicitly indicated, and in the
13614 <definition>.
13615
13616 The Dwarf 2 spec says that an object-like macro's name is always
13617 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13618 the space when the macro's definition is the empty string.
2e276125
JB
13619
13620 The Dwarf 2 spec says that there should be no spaces between the
13621 formal arguments in a function-like macro's formal argument list,
13622 but versions of GCC around March 2002 include spaces after the
13623 commas. */
13624
13625
13626 /* Find the extent of the macro name. The macro name is terminated
13627 by either a space or null character (for an object-like macro) or
13628 an opening paren (for a function-like macro). */
13629 for (p = body; *p; p++)
13630 if (*p == ' ' || *p == '(')
13631 break;
13632
13633 if (*p == ' ' || *p == '\0')
13634 {
13635 /* It's an object-like macro. */
13636 int name_len = p - body;
13637 char *name = copy_string (body, name_len);
13638 const char *replacement;
13639
13640 if (*p == ' ')
13641 replacement = body + name_len + 1;
13642 else
13643 {
4d3c2250 13644 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13645 replacement = body + name_len;
13646 }
6e70227d 13647
2e276125
JB
13648 macro_define_object (file, line, name, replacement);
13649
13650 xfree (name);
13651 }
13652 else if (*p == '(')
13653 {
13654 /* It's a function-like macro. */
13655 char *name = copy_string (body, p - body);
13656 int argc = 0;
13657 int argv_size = 1;
13658 char **argv = xmalloc (argv_size * sizeof (*argv));
13659
13660 p++;
13661
13662 p = consume_improper_spaces (p, body);
13663
13664 /* Parse the formal argument list. */
13665 while (*p && *p != ')')
13666 {
13667 /* Find the extent of the current argument name. */
13668 const char *arg_start = p;
13669
13670 while (*p && *p != ',' && *p != ')' && *p != ' ')
13671 p++;
13672
13673 if (! *p || p == arg_start)
4d3c2250 13674 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13675 else
13676 {
13677 /* Make sure argv has room for the new argument. */
13678 if (argc >= argv_size)
13679 {
13680 argv_size *= 2;
13681 argv = xrealloc (argv, argv_size * sizeof (*argv));
13682 }
13683
13684 argv[argc++] = copy_string (arg_start, p - arg_start);
13685 }
13686
13687 p = consume_improper_spaces (p, body);
13688
13689 /* Consume the comma, if present. */
13690 if (*p == ',')
13691 {
13692 p++;
13693
13694 p = consume_improper_spaces (p, body);
13695 }
13696 }
13697
13698 if (*p == ')')
13699 {
13700 p++;
13701
13702 if (*p == ' ')
13703 /* Perfectly formed definition, no complaints. */
13704 macro_define_function (file, line, name,
6e70227d 13705 argc, (const char **) argv,
2e276125
JB
13706 p + 1);
13707 else if (*p == '\0')
13708 {
13709 /* Complain, but do define it. */
4d3c2250 13710 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13711 macro_define_function (file, line, name,
6e70227d 13712 argc, (const char **) argv,
2e276125
JB
13713 p);
13714 }
13715 else
13716 /* Just complain. */
4d3c2250 13717 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13718 }
13719 else
13720 /* Just complain. */
4d3c2250 13721 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13722
13723 xfree (name);
13724 {
13725 int i;
13726
13727 for (i = 0; i < argc; i++)
13728 xfree (argv[i]);
13729 }
13730 xfree (argv);
13731 }
13732 else
4d3c2250 13733 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13734}
13735
13736
13737static void
13738dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13739 char *comp_dir, bfd *abfd,
e7c27a73 13740 struct dwarf2_cu *cu)
2e276125 13741{
fe1b8b76 13742 gdb_byte *mac_ptr, *mac_end;
2e276125 13743 struct macro_source_file *current_file = 0;
757a13d0
JK
13744 enum dwarf_macinfo_record_type macinfo_type;
13745 int at_commandline;
2e276125 13746
be391dca
TT
13747 dwarf2_read_section (dwarf2_per_objfile->objfile,
13748 &dwarf2_per_objfile->macinfo);
dce234bc 13749 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13750 {
e2e0b3e5 13751 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13752 return;
13753 }
13754
757a13d0
JK
13755 /* First pass: Find the name of the base filename.
13756 This filename is needed in order to process all macros whose definition
13757 (or undefinition) comes from the command line. These macros are defined
13758 before the first DW_MACINFO_start_file entry, and yet still need to be
13759 associated to the base file.
13760
13761 To determine the base file name, we scan the macro definitions until we
13762 reach the first DW_MACINFO_start_file entry. We then initialize
13763 CURRENT_FILE accordingly so that any macro definition found before the
13764 first DW_MACINFO_start_file can still be associated to the base file. */
13765
dce234bc
PP
13766 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13767 mac_end = dwarf2_per_objfile->macinfo.buffer
13768 + dwarf2_per_objfile->macinfo.size;
2e276125 13769
757a13d0 13770 do
2e276125 13771 {
2e276125
JB
13772 /* Do we at least have room for a macinfo type byte? */
13773 if (mac_ptr >= mac_end)
13774 {
757a13d0
JK
13775 /* Complaint is printed during the second pass as GDB will probably
13776 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13777 break;
2e276125
JB
13778 }
13779
13780 macinfo_type = read_1_byte (abfd, mac_ptr);
13781 mac_ptr++;
13782
13783 switch (macinfo_type)
13784 {
13785 /* A zero macinfo type indicates the end of the macro
13786 information. */
13787 case 0:
757a13d0
JK
13788 break;
13789
13790 case DW_MACINFO_define:
13791 case DW_MACINFO_undef:
13792 /* Only skip the data by MAC_PTR. */
13793 {
13794 unsigned int bytes_read;
13795
13796 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13797 mac_ptr += bytes_read;
9b1c24c8 13798 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13799 mac_ptr += bytes_read;
13800 }
13801 break;
13802
13803 case DW_MACINFO_start_file:
13804 {
13805 unsigned int bytes_read;
13806 int line, file;
13807
13808 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13809 mac_ptr += bytes_read;
13810 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13811 mac_ptr += bytes_read;
13812
13813 current_file = macro_start_file (file, line, current_file, comp_dir,
13814 lh, cu->objfile);
13815 }
13816 break;
13817
13818 case DW_MACINFO_end_file:
13819 /* No data to skip by MAC_PTR. */
13820 break;
13821
13822 case DW_MACINFO_vendor_ext:
13823 /* Only skip the data by MAC_PTR. */
13824 {
13825 unsigned int bytes_read;
13826
13827 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13828 mac_ptr += bytes_read;
9b1c24c8 13829 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13830 mac_ptr += bytes_read;
13831 }
13832 break;
13833
13834 default:
13835 break;
13836 }
13837 } while (macinfo_type != 0 && current_file == NULL);
13838
13839 /* Second pass: Process all entries.
13840
13841 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13842 command-line macro definitions/undefinitions. This flag is unset when we
13843 reach the first DW_MACINFO_start_file entry. */
13844
dce234bc 13845 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13846
13847 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13848 GDB is still reading the definitions from command line. First
13849 DW_MACINFO_start_file will need to be ignored as it was already executed
13850 to create CURRENT_FILE for the main source holding also the command line
13851 definitions. On first met DW_MACINFO_start_file this flag is reset to
13852 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13853
13854 at_commandline = 1;
13855
13856 do
13857 {
13858 /* Do we at least have room for a macinfo type byte? */
13859 if (mac_ptr >= mac_end)
13860 {
13861 dwarf2_macros_too_long_complaint ();
13862 break;
13863 }
13864
13865 macinfo_type = read_1_byte (abfd, mac_ptr);
13866 mac_ptr++;
13867
13868 switch (macinfo_type)
13869 {
13870 /* A zero macinfo type indicates the end of the macro
13871 information. */
13872 case 0:
13873 break;
2e276125
JB
13874
13875 case DW_MACINFO_define:
13876 case DW_MACINFO_undef:
13877 {
891d2f0b 13878 unsigned int bytes_read;
2e276125
JB
13879 int line;
13880 char *body;
13881
13882 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13883 mac_ptr += bytes_read;
9b1c24c8 13884 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13885 mac_ptr += bytes_read;
13886
13887 if (! current_file)
757a13d0
JK
13888 {
13889 /* DWARF violation as no main source is present. */
13890 complaint (&symfile_complaints,
13891 _("debug info with no main source gives macro %s "
13892 "on line %d: %s"),
6e70227d
DE
13893 macinfo_type == DW_MACINFO_define ?
13894 _("definition") :
905e0470
PM
13895 macinfo_type == DW_MACINFO_undef ?
13896 _("undefinition") :
13897 _("something-or-other"), line, body);
757a13d0
JK
13898 break;
13899 }
13900 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13901 complaint (&symfile_complaints,
757a13d0
JK
13902 _("debug info gives %s macro %s with %s line %d: %s"),
13903 at_commandline ? _("command-line") : _("in-file"),
905e0470 13904 macinfo_type == DW_MACINFO_define ?
6e70227d 13905 _("definition") :
905e0470
PM
13906 macinfo_type == DW_MACINFO_undef ?
13907 _("undefinition") :
13908 _("something-or-other"),
757a13d0
JK
13909 line == 0 ? _("zero") : _("non-zero"), line, body);
13910
13911 if (macinfo_type == DW_MACINFO_define)
13912 parse_macro_definition (current_file, line, body);
13913 else if (macinfo_type == DW_MACINFO_undef)
13914 macro_undef (current_file, line, body);
2e276125
JB
13915 }
13916 break;
13917
13918 case DW_MACINFO_start_file:
13919 {
891d2f0b 13920 unsigned int bytes_read;
2e276125
JB
13921 int line, file;
13922
13923 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13924 mac_ptr += bytes_read;
13925 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13926 mac_ptr += bytes_read;
13927
757a13d0
JK
13928 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13929 complaint (&symfile_complaints,
13930 _("debug info gives source %d included "
13931 "from %s at %s line %d"),
13932 file, at_commandline ? _("command-line") : _("file"),
13933 line == 0 ? _("zero") : _("non-zero"), line);
13934
13935 if (at_commandline)
13936 {
13937 /* This DW_MACINFO_start_file was executed in the pass one. */
13938 at_commandline = 0;
13939 }
13940 else
13941 current_file = macro_start_file (file, line,
13942 current_file, comp_dir,
13943 lh, cu->objfile);
2e276125
JB
13944 }
13945 break;
13946
13947 case DW_MACINFO_end_file:
13948 if (! current_file)
4d3c2250 13949 complaint (&symfile_complaints,
e2e0b3e5 13950 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13951 else
13952 {
13953 current_file = current_file->included_by;
13954 if (! current_file)
13955 {
13956 enum dwarf_macinfo_record_type next_type;
13957
13958 /* GCC circa March 2002 doesn't produce the zero
13959 type byte marking the end of the compilation
13960 unit. Complain if it's not there, but exit no
13961 matter what. */
13962
13963 /* Do we at least have room for a macinfo type byte? */
13964 if (mac_ptr >= mac_end)
13965 {
4d3c2250 13966 dwarf2_macros_too_long_complaint ();
2e276125
JB
13967 return;
13968 }
13969
13970 /* We don't increment mac_ptr here, so this is just
13971 a look-ahead. */
13972 next_type = read_1_byte (abfd, mac_ptr);
13973 if (next_type != 0)
4d3c2250 13974 complaint (&symfile_complaints,
e2e0b3e5 13975 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13976
13977 return;
13978 }
13979 }
13980 break;
13981
13982 case DW_MACINFO_vendor_ext:
13983 {
891d2f0b 13984 unsigned int bytes_read;
2e276125
JB
13985 int constant;
13986 char *string;
13987
13988 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13989 mac_ptr += bytes_read;
9b1c24c8 13990 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13991 mac_ptr += bytes_read;
13992
13993 /* We don't recognize any vendor extensions. */
13994 }
13995 break;
13996 }
757a13d0 13997 } while (macinfo_type != 0);
2e276125 13998}
8e19ed76
PS
13999
14000/* Check if the attribute's form is a DW_FORM_block*
14001 if so return true else false. */
14002static int
14003attr_form_is_block (struct attribute *attr)
14004{
14005 return (attr == NULL ? 0 :
14006 attr->form == DW_FORM_block1
14007 || attr->form == DW_FORM_block2
14008 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14009 || attr->form == DW_FORM_block
14010 || attr->form == DW_FORM_exprloc);
8e19ed76 14011}
4c2df51b 14012
c6a0999f
JB
14013/* Return non-zero if ATTR's value is a section offset --- classes
14014 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14015 You may use DW_UNSND (attr) to retrieve such offsets.
14016
14017 Section 7.5.4, "Attribute Encodings", explains that no attribute
14018 may have a value that belongs to more than one of these classes; it
14019 would be ambiguous if we did, because we use the same forms for all
14020 of them. */
3690dd37
JB
14021static int
14022attr_form_is_section_offset (struct attribute *attr)
14023{
14024 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14025 || attr->form == DW_FORM_data8
14026 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14027}
14028
14029
14030/* Return non-zero if ATTR's value falls in the 'constant' class, or
14031 zero otherwise. When this function returns true, you can apply
14032 dwarf2_get_attr_constant_value to it.
14033
14034 However, note that for some attributes you must check
14035 attr_form_is_section_offset before using this test. DW_FORM_data4
14036 and DW_FORM_data8 are members of both the constant class, and of
14037 the classes that contain offsets into other debug sections
14038 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14039 that, if an attribute's can be either a constant or one of the
14040 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14041 taken as section offsets, not constants. */
14042static int
14043attr_form_is_constant (struct attribute *attr)
14044{
14045 switch (attr->form)
14046 {
14047 case DW_FORM_sdata:
14048 case DW_FORM_udata:
14049 case DW_FORM_data1:
14050 case DW_FORM_data2:
14051 case DW_FORM_data4:
14052 case DW_FORM_data8:
14053 return 1;
14054 default:
14055 return 0;
14056 }
14057}
14058
4c2df51b
DJ
14059static void
14060dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14061 struct dwarf2_cu *cu)
4c2df51b 14062{
3690dd37 14063 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14064 /* ".debug_loc" may not exist at all, or the offset may be outside
14065 the section. If so, fall through to the complaint in the
14066 other branch. */
dce234bc 14067 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14068 {
0d53c4c4 14069 struct dwarf2_loclist_baton *baton;
4c2df51b 14070
4a146b47 14071 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14072 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
14073 baton->per_cu = cu->per_cu;
14074 gdb_assert (baton->per_cu);
4c2df51b 14075
be391dca
TT
14076 dwarf2_read_section (dwarf2_per_objfile->objfile,
14077 &dwarf2_per_objfile->loc);
14078
0d53c4c4
DJ
14079 /* We don't know how long the location list is, but make sure we
14080 don't run off the edge of the section. */
dce234bc
PP
14081 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14082 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
14083 baton->base_address = cu->base_address;
14084 if (cu->base_known == 0)
0d53c4c4 14085 complaint (&symfile_complaints,
e2e0b3e5 14086 _("Location list used without specifying the CU base address."));
4c2df51b 14087
768a979c 14088 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14089 SYMBOL_LOCATION_BATON (sym) = baton;
14090 }
14091 else
14092 {
14093 struct dwarf2_locexpr_baton *baton;
14094
4a146b47 14095 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14096 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14097 baton->per_cu = cu->per_cu;
14098 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14099
14100 if (attr_form_is_block (attr))
14101 {
14102 /* Note that we're just copying the block's data pointer
14103 here, not the actual data. We're still pointing into the
6502dd73
DJ
14104 info_buffer for SYM's objfile; right now we never release
14105 that buffer, but when we do clean up properly this may
14106 need to change. */
0d53c4c4
DJ
14107 baton->size = DW_BLOCK (attr)->size;
14108 baton->data = DW_BLOCK (attr)->data;
14109 }
14110 else
14111 {
14112 dwarf2_invalid_attrib_class_complaint ("location description",
14113 SYMBOL_NATURAL_NAME (sym));
14114 baton->size = 0;
14115 baton->data = NULL;
14116 }
6e70227d 14117
768a979c 14118 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14119 SYMBOL_LOCATION_BATON (sym) = baton;
14120 }
4c2df51b 14121}
6502dd73 14122
9aa1f1e3
TT
14123/* Return the OBJFILE associated with the compilation unit CU. If CU
14124 came from a separate debuginfo file, then the master objfile is
14125 returned. */
ae0d2f24
UW
14126
14127struct objfile *
14128dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14129{
9291a0cd 14130 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14131
14132 /* Return the master objfile, so that we can report and look up the
14133 correct file containing this variable. */
14134 if (objfile->separate_debug_objfile_backlink)
14135 objfile = objfile->separate_debug_objfile_backlink;
14136
14137 return objfile;
14138}
14139
14140/* Return the address size given in the compilation unit header for CU. */
14141
14142CORE_ADDR
14143dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14144{
14145 if (per_cu->cu)
14146 return per_cu->cu->header.addr_size;
14147 else
14148 {
14149 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14150 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14151 struct dwarf2_per_objfile *per_objfile
14152 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14153 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14154 struct comp_unit_head cu_header;
9a619af0 14155
ae0d2f24
UW
14156 memset (&cu_header, 0, sizeof cu_header);
14157 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14158 return cu_header.addr_size;
14159 }
14160}
14161
9eae7c52
TT
14162/* Return the offset size given in the compilation unit header for CU. */
14163
14164int
14165dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14166{
14167 if (per_cu->cu)
14168 return per_cu->cu->header.offset_size;
14169 else
14170 {
14171 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14172 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14173 struct dwarf2_per_objfile *per_objfile
14174 = objfile_data (objfile, dwarf2_objfile_data_key);
14175 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14176 struct comp_unit_head cu_header;
14177
14178 memset (&cu_header, 0, sizeof cu_header);
14179 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14180 return cu_header.offset_size;
14181 }
14182}
14183
9aa1f1e3
TT
14184/* Return the text offset of the CU. The returned offset comes from
14185 this CU's objfile. If this objfile came from a separate debuginfo
14186 file, then the offset may be different from the corresponding
14187 offset in the parent objfile. */
14188
14189CORE_ADDR
14190dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14191{
bb3fa9d0 14192 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14193
14194 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14195}
14196
348e048f
DE
14197/* Locate the .debug_info compilation unit from CU's objfile which contains
14198 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14199
14200static struct dwarf2_per_cu_data *
c764a876 14201dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14202 struct objfile *objfile)
14203{
14204 struct dwarf2_per_cu_data *this_cu;
14205 int low, high;
14206
ae038cb0
DJ
14207 low = 0;
14208 high = dwarf2_per_objfile->n_comp_units - 1;
14209 while (high > low)
14210 {
14211 int mid = low + (high - low) / 2;
9a619af0 14212
ae038cb0
DJ
14213 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14214 high = mid;
14215 else
14216 low = mid + 1;
14217 }
14218 gdb_assert (low == high);
14219 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14220 {
10b3939b 14221 if (low == 0)
8a3fe4f8
AC
14222 error (_("Dwarf Error: could not find partial DIE containing "
14223 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14224 (long) offset, bfd_get_filename (objfile->obfd));
14225
ae038cb0
DJ
14226 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14227 return dwarf2_per_objfile->all_comp_units[low-1];
14228 }
14229 else
14230 {
14231 this_cu = dwarf2_per_objfile->all_comp_units[low];
14232 if (low == dwarf2_per_objfile->n_comp_units - 1
14233 && offset >= this_cu->offset + this_cu->length)
c764a876 14234 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14235 gdb_assert (offset < this_cu->offset + this_cu->length);
14236 return this_cu;
14237 }
14238}
14239
10b3939b
DJ
14240/* Locate the compilation unit from OBJFILE which is located at exactly
14241 OFFSET. Raises an error on failure. */
14242
ae038cb0 14243static struct dwarf2_per_cu_data *
c764a876 14244dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14245{
14246 struct dwarf2_per_cu_data *this_cu;
9a619af0 14247
ae038cb0
DJ
14248 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14249 if (this_cu->offset != offset)
c764a876 14250 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14251 return this_cu;
14252}
14253
93311388
DE
14254/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14255
14256static struct dwarf2_cu *
14257alloc_one_comp_unit (struct objfile *objfile)
14258{
14259 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14260 cu->objfile = objfile;
14261 obstack_init (&cu->comp_unit_obstack);
14262 return cu;
14263}
14264
ae038cb0
DJ
14265/* Release one cached compilation unit, CU. We unlink it from the tree
14266 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14267 the caller is responsible for that.
14268 NOTE: DATA is a void * because this function is also used as a
14269 cleanup routine. */
ae038cb0
DJ
14270
14271static void
14272free_one_comp_unit (void *data)
14273{
14274 struct dwarf2_cu *cu = data;
14275
14276 if (cu->per_cu != NULL)
14277 cu->per_cu->cu = NULL;
14278 cu->per_cu = NULL;
14279
14280 obstack_free (&cu->comp_unit_obstack, NULL);
14281
14282 xfree (cu);
14283}
14284
72bf9492 14285/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14286 when we're finished with it. We can't free the pointer itself, but be
14287 sure to unlink it from the cache. Also release any associated storage
14288 and perform cache maintenance.
72bf9492
DJ
14289
14290 Only used during partial symbol parsing. */
14291
14292static void
14293free_stack_comp_unit (void *data)
14294{
14295 struct dwarf2_cu *cu = data;
14296
14297 obstack_free (&cu->comp_unit_obstack, NULL);
14298 cu->partial_dies = NULL;
ae038cb0
DJ
14299
14300 if (cu->per_cu != NULL)
14301 {
14302 /* This compilation unit is on the stack in our caller, so we
14303 should not xfree it. Just unlink it. */
14304 cu->per_cu->cu = NULL;
14305 cu->per_cu = NULL;
14306
14307 /* If we had a per-cu pointer, then we may have other compilation
14308 units loaded, so age them now. */
14309 age_cached_comp_units ();
14310 }
14311}
14312
14313/* Free all cached compilation units. */
14314
14315static void
14316free_cached_comp_units (void *data)
14317{
14318 struct dwarf2_per_cu_data *per_cu, **last_chain;
14319
14320 per_cu = dwarf2_per_objfile->read_in_chain;
14321 last_chain = &dwarf2_per_objfile->read_in_chain;
14322 while (per_cu != NULL)
14323 {
14324 struct dwarf2_per_cu_data *next_cu;
14325
14326 next_cu = per_cu->cu->read_in_chain;
14327
14328 free_one_comp_unit (per_cu->cu);
14329 *last_chain = next_cu;
14330
14331 per_cu = next_cu;
14332 }
14333}
14334
14335/* Increase the age counter on each cached compilation unit, and free
14336 any that are too old. */
14337
14338static void
14339age_cached_comp_units (void)
14340{
14341 struct dwarf2_per_cu_data *per_cu, **last_chain;
14342
14343 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14344 per_cu = dwarf2_per_objfile->read_in_chain;
14345 while (per_cu != NULL)
14346 {
14347 per_cu->cu->last_used ++;
14348 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14349 dwarf2_mark (per_cu->cu);
14350 per_cu = per_cu->cu->read_in_chain;
14351 }
14352
14353 per_cu = dwarf2_per_objfile->read_in_chain;
14354 last_chain = &dwarf2_per_objfile->read_in_chain;
14355 while (per_cu != NULL)
14356 {
14357 struct dwarf2_per_cu_data *next_cu;
14358
14359 next_cu = per_cu->cu->read_in_chain;
14360
14361 if (!per_cu->cu->mark)
14362 {
14363 free_one_comp_unit (per_cu->cu);
14364 *last_chain = next_cu;
14365 }
14366 else
14367 last_chain = &per_cu->cu->read_in_chain;
14368
14369 per_cu = next_cu;
14370 }
14371}
14372
14373/* Remove a single compilation unit from the cache. */
14374
14375static void
14376free_one_cached_comp_unit (void *target_cu)
14377{
14378 struct dwarf2_per_cu_data *per_cu, **last_chain;
14379
14380 per_cu = dwarf2_per_objfile->read_in_chain;
14381 last_chain = &dwarf2_per_objfile->read_in_chain;
14382 while (per_cu != NULL)
14383 {
14384 struct dwarf2_per_cu_data *next_cu;
14385
14386 next_cu = per_cu->cu->read_in_chain;
14387
14388 if (per_cu->cu == target_cu)
14389 {
14390 free_one_comp_unit (per_cu->cu);
14391 *last_chain = next_cu;
14392 break;
14393 }
14394 else
14395 last_chain = &per_cu->cu->read_in_chain;
14396
14397 per_cu = next_cu;
14398 }
14399}
14400
fe3e1990
DJ
14401/* Release all extra memory associated with OBJFILE. */
14402
14403void
14404dwarf2_free_objfile (struct objfile *objfile)
14405{
14406 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14407
14408 if (dwarf2_per_objfile == NULL)
14409 return;
14410
14411 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14412 free_cached_comp_units (NULL);
14413
9291a0cd
TT
14414 if (dwarf2_per_objfile->using_index)
14415 {
14416 int i;
14417
14418 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14419 {
14420 int j;
e254ef6a
DE
14421 struct dwarf2_per_cu_data *per_cu =
14422 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14423
e254ef6a 14424 if (!per_cu->v.quick->lines)
9291a0cd
TT
14425 continue;
14426
e254ef6a 14427 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14428 {
e254ef6a
DE
14429 if (per_cu->v.quick->file_names)
14430 xfree ((void *) per_cu->v.quick->file_names[j]);
14431 if (per_cu->v.quick->full_names)
14432 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14433 }
14434
e254ef6a 14435 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14436 }
14437 }
14438
fe3e1990
DJ
14439 /* Everything else should be on the objfile obstack. */
14440}
14441
1c379e20
DJ
14442/* A pair of DIE offset and GDB type pointer. We store these
14443 in a hash table separate from the DIEs, and preserve them
14444 when the DIEs are flushed out of cache. */
14445
14446struct dwarf2_offset_and_type
14447{
14448 unsigned int offset;
14449 struct type *type;
14450};
14451
14452/* Hash function for a dwarf2_offset_and_type. */
14453
14454static hashval_t
14455offset_and_type_hash (const void *item)
14456{
14457 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14458
1c379e20
DJ
14459 return ofs->offset;
14460}
14461
14462/* Equality function for a dwarf2_offset_and_type. */
14463
14464static int
14465offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14466{
14467 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14468 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14469
1c379e20
DJ
14470 return ofs_lhs->offset == ofs_rhs->offset;
14471}
14472
14473/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14474 table if necessary. For convenience, return TYPE.
14475
14476 The DIEs reading must have careful ordering to:
14477 * Not cause infite loops trying to read in DIEs as a prerequisite for
14478 reading current DIE.
14479 * Not trying to dereference contents of still incompletely read in types
14480 while reading in other DIEs.
14481 * Enable referencing still incompletely read in types just by a pointer to
14482 the type without accessing its fields.
14483
14484 Therefore caller should follow these rules:
14485 * Try to fetch any prerequisite types we may need to build this DIE type
14486 before building the type and calling set_die_type.
e71ec853 14487 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14488 possible before fetching more types to complete the current type.
14489 * Make the type as complete as possible before fetching more types. */
1c379e20 14490
f792889a 14491static struct type *
1c379e20
DJ
14492set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14493{
14494 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14495 struct objfile *objfile = cu->objfile;
14496 htab_t *type_hash_ptr;
1c379e20 14497
b4ba55a1
JB
14498 /* For Ada types, make sure that the gnat-specific data is always
14499 initialized (if not already set). There are a few types where
14500 we should not be doing so, because the type-specific area is
14501 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14502 where the type-specific area is used to store the floatformat).
14503 But this is not a problem, because the gnat-specific information
14504 is actually not needed for these types. */
14505 if (need_gnat_info (cu)
14506 && TYPE_CODE (type) != TYPE_CODE_FUNC
14507 && TYPE_CODE (type) != TYPE_CODE_FLT
14508 && !HAVE_GNAT_AUX_INFO (type))
14509 INIT_GNAT_SPECIFIC (type);
14510
673bfd45
DE
14511 if (cu->per_cu->from_debug_types)
14512 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14513 else
14514 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14515
14516 if (*type_hash_ptr == NULL)
f792889a 14517 {
673bfd45
DE
14518 *type_hash_ptr
14519 = htab_create_alloc_ex (127,
f792889a
DJ
14520 offset_and_type_hash,
14521 offset_and_type_eq,
14522 NULL,
673bfd45 14523 &objfile->objfile_obstack,
f792889a
DJ
14524 hashtab_obstack_allocate,
14525 dummy_obstack_deallocate);
f792889a 14526 }
1c379e20
DJ
14527
14528 ofs.offset = die->offset;
14529 ofs.type = type;
14530 slot = (struct dwarf2_offset_and_type **)
673bfd45 14531 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14532 if (*slot)
14533 complaint (&symfile_complaints,
14534 _("A problem internal to GDB: DIE 0x%x has type already set"),
14535 die->offset);
673bfd45 14536 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14537 **slot = ofs;
f792889a 14538 return type;
1c379e20
DJ
14539}
14540
673bfd45
DE
14541/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14542 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14543
14544static struct type *
673bfd45
DE
14545get_die_type_at_offset (unsigned int offset,
14546 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14547{
14548 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14549 htab_t type_hash;
f792889a 14550
673bfd45
DE
14551 if (per_cu->from_debug_types)
14552 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14553 else
14554 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14555 if (type_hash == NULL)
14556 return NULL;
1c379e20 14557
673bfd45 14558 ofs.offset = offset;
1c379e20
DJ
14559 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14560 if (slot)
14561 return slot->type;
14562 else
14563 return NULL;
14564}
14565
673bfd45
DE
14566/* Look up the type for DIE in the appropriate type_hash table,
14567 or return NULL if DIE does not have a saved type. */
14568
14569static struct type *
14570get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14571{
14572 return get_die_type_at_offset (die->offset, cu->per_cu);
14573}
14574
10b3939b
DJ
14575/* Add a dependence relationship from CU to REF_PER_CU. */
14576
14577static void
14578dwarf2_add_dependence (struct dwarf2_cu *cu,
14579 struct dwarf2_per_cu_data *ref_per_cu)
14580{
14581 void **slot;
14582
14583 if (cu->dependencies == NULL)
14584 cu->dependencies
14585 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14586 NULL, &cu->comp_unit_obstack,
14587 hashtab_obstack_allocate,
14588 dummy_obstack_deallocate);
14589
14590 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14591 if (*slot == NULL)
14592 *slot = ref_per_cu;
14593}
1c379e20 14594
f504f079
DE
14595/* Subroutine of dwarf2_mark to pass to htab_traverse.
14596 Set the mark field in every compilation unit in the
ae038cb0
DJ
14597 cache that we must keep because we are keeping CU. */
14598
10b3939b
DJ
14599static int
14600dwarf2_mark_helper (void **slot, void *data)
14601{
14602 struct dwarf2_per_cu_data *per_cu;
14603
14604 per_cu = (struct dwarf2_per_cu_data *) *slot;
14605 if (per_cu->cu->mark)
14606 return 1;
14607 per_cu->cu->mark = 1;
14608
14609 if (per_cu->cu->dependencies != NULL)
14610 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14611
14612 return 1;
14613}
14614
f504f079
DE
14615/* Set the mark field in CU and in every other compilation unit in the
14616 cache that we must keep because we are keeping CU. */
14617
ae038cb0
DJ
14618static void
14619dwarf2_mark (struct dwarf2_cu *cu)
14620{
14621 if (cu->mark)
14622 return;
14623 cu->mark = 1;
10b3939b
DJ
14624 if (cu->dependencies != NULL)
14625 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14626}
14627
14628static void
14629dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14630{
14631 while (per_cu)
14632 {
14633 per_cu->cu->mark = 0;
14634 per_cu = per_cu->cu->read_in_chain;
14635 }
72bf9492
DJ
14636}
14637
72bf9492
DJ
14638/* Trivial hash function for partial_die_info: the hash value of a DIE
14639 is its offset in .debug_info for this objfile. */
14640
14641static hashval_t
14642partial_die_hash (const void *item)
14643{
14644 const struct partial_die_info *part_die = item;
9a619af0 14645
72bf9492
DJ
14646 return part_die->offset;
14647}
14648
14649/* Trivial comparison function for partial_die_info structures: two DIEs
14650 are equal if they have the same offset. */
14651
14652static int
14653partial_die_eq (const void *item_lhs, const void *item_rhs)
14654{
14655 const struct partial_die_info *part_die_lhs = item_lhs;
14656 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14657
72bf9492
DJ
14658 return part_die_lhs->offset == part_die_rhs->offset;
14659}
14660
ae038cb0
DJ
14661static struct cmd_list_element *set_dwarf2_cmdlist;
14662static struct cmd_list_element *show_dwarf2_cmdlist;
14663
14664static void
14665set_dwarf2_cmd (char *args, int from_tty)
14666{
14667 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14668}
14669
14670static void
14671show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14672{
ae038cb0
DJ
14673 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14674}
14675
dce234bc
PP
14676/* If section described by INFO was mmapped, munmap it now. */
14677
14678static void
14679munmap_section_buffer (struct dwarf2_section_info *info)
14680{
14681 if (info->was_mmapped)
14682 {
14683#ifdef HAVE_MMAP
14684 intptr_t begin = (intptr_t) info->buffer;
14685 intptr_t map_begin = begin & ~(pagesize - 1);
14686 size_t map_length = info->size + begin - map_begin;
9a619af0 14687
dce234bc
PP
14688 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14689#else
14690 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14691 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14692#endif
14693 }
14694}
14695
14696/* munmap debug sections for OBJFILE, if necessary. */
14697
14698static void
c1bd65d0 14699dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14700{
14701 struct dwarf2_per_objfile *data = d;
9a619af0 14702
16be1145
DE
14703 /* This is sorted according to the order they're defined in to make it easier
14704 to keep in sync. */
dce234bc
PP
14705 munmap_section_buffer (&data->info);
14706 munmap_section_buffer (&data->abbrev);
14707 munmap_section_buffer (&data->line);
16be1145 14708 munmap_section_buffer (&data->loc);
dce234bc 14709 munmap_section_buffer (&data->macinfo);
16be1145 14710 munmap_section_buffer (&data->str);
dce234bc 14711 munmap_section_buffer (&data->ranges);
16be1145 14712 munmap_section_buffer (&data->types);
dce234bc
PP
14713 munmap_section_buffer (&data->frame);
14714 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14715 munmap_section_buffer (&data->gdb_index);
14716}
14717
14718\f
14719
14720/* The contents of the hash table we create when building the string
14721 table. */
14722struct strtab_entry
14723{
14724 offset_type offset;
14725 const char *str;
14726};
14727
14728/* Hash function for a strtab_entry. */
14729static hashval_t
14730hash_strtab_entry (const void *e)
14731{
14732 const struct strtab_entry *entry = e;
14733 return mapped_index_string_hash (entry->str);
14734}
14735
14736/* Equality function for a strtab_entry. */
14737static int
14738eq_strtab_entry (const void *a, const void *b)
14739{
14740 const struct strtab_entry *ea = a;
14741 const struct strtab_entry *eb = b;
14742 return !strcmp (ea->str, eb->str);
14743}
14744
14745/* Create a strtab_entry hash table. */
14746static htab_t
14747create_strtab (void)
14748{
14749 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14750 xfree, xcalloc, xfree);
14751}
14752
14753/* Add a string to the constant pool. Return the string's offset in
14754 host order. */
14755static offset_type
14756add_string (htab_t table, struct obstack *cpool, const char *str)
14757{
14758 void **slot;
14759 struct strtab_entry entry;
14760 struct strtab_entry *result;
14761
14762 entry.str = str;
14763 slot = htab_find_slot (table, &entry, INSERT);
14764 if (*slot)
14765 result = *slot;
14766 else
14767 {
14768 result = XNEW (struct strtab_entry);
14769 result->offset = obstack_object_size (cpool);
14770 result->str = str;
14771 obstack_grow_str0 (cpool, str);
14772 *slot = result;
14773 }
14774 return result->offset;
14775}
14776
14777/* An entry in the symbol table. */
14778struct symtab_index_entry
14779{
14780 /* The name of the symbol. */
14781 const char *name;
14782 /* The offset of the name in the constant pool. */
14783 offset_type index_offset;
14784 /* A sorted vector of the indices of all the CUs that hold an object
14785 of this name. */
14786 VEC (offset_type) *cu_indices;
14787};
14788
14789/* The symbol table. This is a power-of-2-sized hash table. */
14790struct mapped_symtab
14791{
14792 offset_type n_elements;
14793 offset_type size;
14794 struct symtab_index_entry **data;
14795};
14796
14797/* Hash function for a symtab_index_entry. */
14798static hashval_t
14799hash_symtab_entry (const void *e)
14800{
14801 const struct symtab_index_entry *entry = e;
14802 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14803 sizeof (offset_type) * VEC_length (offset_type,
14804 entry->cu_indices),
14805 0);
14806}
14807
14808/* Equality function for a symtab_index_entry. */
14809static int
14810eq_symtab_entry (const void *a, const void *b)
14811{
14812 const struct symtab_index_entry *ea = a;
14813 const struct symtab_index_entry *eb = b;
14814 int len = VEC_length (offset_type, ea->cu_indices);
14815 if (len != VEC_length (offset_type, eb->cu_indices))
14816 return 0;
14817 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14818 VEC_address (offset_type, eb->cu_indices),
14819 sizeof (offset_type) * len);
14820}
14821
14822/* Destroy a symtab_index_entry. */
14823static void
14824delete_symtab_entry (void *p)
14825{
14826 struct symtab_index_entry *entry = p;
14827 VEC_free (offset_type, entry->cu_indices);
14828 xfree (entry);
14829}
14830
14831/* Create a hash table holding symtab_index_entry objects. */
14832static htab_t
14833create_index_table (void)
14834{
14835 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14836 delete_symtab_entry, xcalloc, xfree);
14837}
14838
14839/* Create a new mapped symtab object. */
14840static struct mapped_symtab *
14841create_mapped_symtab (void)
14842{
14843 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14844 symtab->n_elements = 0;
14845 symtab->size = 1024;
14846 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14847 return symtab;
14848}
14849
14850/* Destroy a mapped_symtab. */
14851static void
14852cleanup_mapped_symtab (void *p)
14853{
14854 struct mapped_symtab *symtab = p;
14855 /* The contents of the array are freed when the other hash table is
14856 destroyed. */
14857 xfree (symtab->data);
14858 xfree (symtab);
14859}
14860
14861/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14862 the slot. */
14863static struct symtab_index_entry **
14864find_slot (struct mapped_symtab *symtab, const char *name)
14865{
14866 offset_type index, step, hash = mapped_index_string_hash (name);
14867
14868 index = hash & (symtab->size - 1);
14869 step = ((hash * 17) & (symtab->size - 1)) | 1;
14870
14871 for (;;)
14872 {
14873 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14874 return &symtab->data[index];
14875 index = (index + step) & (symtab->size - 1);
14876 }
14877}
14878
14879/* Expand SYMTAB's hash table. */
14880static void
14881hash_expand (struct mapped_symtab *symtab)
14882{
14883 offset_type old_size = symtab->size;
14884 offset_type i;
14885 struct symtab_index_entry **old_entries = symtab->data;
14886
14887 symtab->size *= 2;
14888 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14889
14890 for (i = 0; i < old_size; ++i)
14891 {
14892 if (old_entries[i])
14893 {
14894 struct symtab_index_entry **slot = find_slot (symtab,
14895 old_entries[i]->name);
14896 *slot = old_entries[i];
14897 }
14898 }
14899
14900 xfree (old_entries);
14901}
14902
14903/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14904 is the index of the CU in which the symbol appears. */
14905static void
14906add_index_entry (struct mapped_symtab *symtab, const char *name,
14907 offset_type cu_index)
14908{
14909 struct symtab_index_entry **slot;
14910
14911 ++symtab->n_elements;
14912 if (4 * symtab->n_elements / 3 >= symtab->size)
14913 hash_expand (symtab);
14914
14915 slot = find_slot (symtab, name);
14916 if (!*slot)
14917 {
14918 *slot = XNEW (struct symtab_index_entry);
14919 (*slot)->name = name;
14920 (*slot)->cu_indices = NULL;
14921 }
14922 /* Don't push an index twice. Due to how we add entries we only
14923 have to check the last one. */
14924 if (VEC_empty (offset_type, (*slot)->cu_indices)
14925 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14926 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14927}
14928
14929/* Add a vector of indices to the constant pool. */
14930static offset_type
14931add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14932 struct symtab_index_entry *entry)
14933{
14934 void **slot;
14935
14936 slot = htab_find_slot (index_table, entry, INSERT);
14937 if (!*slot)
14938 {
14939 offset_type len = VEC_length (offset_type, entry->cu_indices);
14940 offset_type val = MAYBE_SWAP (len);
14941 offset_type iter;
14942 int i;
14943
14944 *slot = entry;
14945 entry->index_offset = obstack_object_size (cpool);
14946
14947 obstack_grow (cpool, &val, sizeof (val));
14948 for (i = 0;
14949 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14950 ++i)
14951 {
14952 val = MAYBE_SWAP (iter);
14953 obstack_grow (cpool, &val, sizeof (val));
14954 }
14955 }
14956 else
14957 {
14958 struct symtab_index_entry *old_entry = *slot;
14959 entry->index_offset = old_entry->index_offset;
14960 entry = old_entry;
14961 }
14962 return entry->index_offset;
14963}
14964
14965/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14966 constant pool entries going into the obstack CPOOL. */
14967static void
14968write_hash_table (struct mapped_symtab *symtab,
14969 struct obstack *output, struct obstack *cpool)
14970{
14971 offset_type i;
14972 htab_t index_table;
14973 htab_t str_table;
14974
14975 index_table = create_index_table ();
14976 str_table = create_strtab ();
14977 /* We add all the index vectors to the constant pool first, to
14978 ensure alignment is ok. */
14979 for (i = 0; i < symtab->size; ++i)
14980 {
14981 if (symtab->data[i])
14982 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14983 }
14984
14985 /* Now write out the hash table. */
14986 for (i = 0; i < symtab->size; ++i)
14987 {
14988 offset_type str_off, vec_off;
14989
14990 if (symtab->data[i])
14991 {
14992 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14993 vec_off = symtab->data[i]->index_offset;
14994 }
14995 else
14996 {
14997 /* While 0 is a valid constant pool index, it is not valid
14998 to have 0 for both offsets. */
14999 str_off = 0;
15000 vec_off = 0;
15001 }
15002
15003 str_off = MAYBE_SWAP (str_off);
15004 vec_off = MAYBE_SWAP (vec_off);
15005
15006 obstack_grow (output, &str_off, sizeof (str_off));
15007 obstack_grow (output, &vec_off, sizeof (vec_off));
15008 }
15009
15010 htab_delete (str_table);
15011 htab_delete (index_table);
15012}
15013
15014/* Write an address entry to ADDR_OBSTACK. The addresses are taken
15015 from PST; CU_INDEX is the index of the CU in the vector of all
15016 CUs. */
15017static void
15018add_address_entry (struct objfile *objfile,
15019 struct obstack *addr_obstack, struct partial_symtab *pst,
15020 unsigned int cu_index)
15021{
15022 offset_type offset;
15023 char addr[8];
15024 CORE_ADDR baseaddr;
15025
1fd400ff
TT
15026 /* Don't bother recording empty ranges. */
15027 if (pst->textlow == pst->texthigh)
15028 return;
15029
9291a0cd
TT
15030 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15031
15032 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15033 obstack_grow (addr_obstack, addr, 8);
15034 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15035 obstack_grow (addr_obstack, addr, 8);
15036 offset = MAYBE_SWAP (cu_index);
15037 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15038}
15039
15040/* Add a list of partial symbols to SYMTAB. */
15041static void
15042write_psymbols (struct mapped_symtab *symtab,
987d643c 15043 htab_t psyms_seen,
9291a0cd
TT
15044 struct partial_symbol **psymp,
15045 int count,
987d643c
TT
15046 offset_type cu_index,
15047 int is_static)
9291a0cd
TT
15048{
15049 for (; count-- > 0; ++psymp)
15050 {
987d643c
TT
15051 void **slot, *lookup;
15052
9291a0cd
TT
15053 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15054 error (_("Ada is not currently supported by the index"));
987d643c
TT
15055
15056 /* We only want to add a given psymbol once. However, we also
15057 want to account for whether it is global or static. So, we
15058 may add it twice, using slightly different values. */
15059 if (is_static)
15060 {
15061 uintptr_t val = 1 | (uintptr_t) *psymp;
15062
15063 lookup = (void *) val;
15064 }
15065 else
15066 lookup = *psymp;
15067
15068 /* Only add a given psymbol once. */
15069 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15070 if (!*slot)
15071 {
15072 *slot = lookup;
15073 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15074 }
9291a0cd
TT
15075 }
15076}
15077
15078/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15079 exception if there is an error. */
15080static void
15081write_obstack (FILE *file, struct obstack *obstack)
15082{
15083 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15084 file)
15085 != obstack_object_size (obstack))
15086 error (_("couldn't data write to file"));
15087}
15088
15089/* Unlink a file if the argument is not NULL. */
15090static void
15091unlink_if_set (void *p)
15092{
15093 char **filename = p;
15094 if (*filename)
15095 unlink (*filename);
15096}
15097
1fd400ff
TT
15098/* A helper struct used when iterating over debug_types. */
15099struct signatured_type_index_data
15100{
15101 struct objfile *objfile;
15102 struct mapped_symtab *symtab;
15103 struct obstack *types_list;
987d643c 15104 htab_t psyms_seen;
1fd400ff
TT
15105 int cu_index;
15106};
15107
15108/* A helper function that writes a single signatured_type to an
15109 obstack. */
15110static int
15111write_one_signatured_type (void **slot, void *d)
15112{
15113 struct signatured_type_index_data *info = d;
15114 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15115 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15116 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15117 gdb_byte val[8];
15118
15119 write_psymbols (info->symtab,
987d643c 15120 info->psyms_seen,
1fd400ff 15121 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15122 psymtab->n_global_syms, info->cu_index,
15123 0);
1fd400ff 15124 write_psymbols (info->symtab,
987d643c 15125 info->psyms_seen,
1fd400ff 15126 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15127 psymtab->n_static_syms, info->cu_index,
15128 1);
1fd400ff
TT
15129
15130 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15131 obstack_grow (info->types_list, val, 8);
15132 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15133 obstack_grow (info->types_list, val, 8);
15134 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15135 obstack_grow (info->types_list, val, 8);
15136
15137 ++info->cu_index;
15138
15139 return 1;
15140}
15141
987d643c
TT
15142/* A cleanup function for an htab_t. */
15143
15144static void
15145cleanup_htab (void *arg)
15146{
15147 htab_delete (arg);
15148}
15149
9291a0cd
TT
15150/* Create an index file for OBJFILE in the directory DIR. */
15151static void
15152write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15153{
15154 struct cleanup *cleanup;
15155 char *filename, *cleanup_filename;
1fd400ff
TT
15156 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15157 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15158 int i;
15159 FILE *out_file;
15160 struct mapped_symtab *symtab;
15161 offset_type val, size_of_contents, total_len;
15162 struct stat st;
15163 char buf[8];
987d643c 15164 htab_t psyms_seen;
9291a0cd
TT
15165
15166 if (!objfile->psymtabs)
15167 return;
15168 if (dwarf2_per_objfile->using_index)
15169 error (_("Cannot use an index to create the index"));
15170
15171 if (stat (objfile->name, &st) < 0)
15172 perror_with_name (_("Could not stat"));
15173
15174 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15175 INDEX_SUFFIX, (char *) NULL);
15176 cleanup = make_cleanup (xfree, filename);
15177
15178 out_file = fopen (filename, "wb");
15179 if (!out_file)
15180 error (_("Can't open `%s' for writing"), filename);
15181
15182 cleanup_filename = filename;
15183 make_cleanup (unlink_if_set, &cleanup_filename);
15184
15185 symtab = create_mapped_symtab ();
15186 make_cleanup (cleanup_mapped_symtab, symtab);
15187
15188 obstack_init (&addr_obstack);
15189 make_cleanup_obstack_free (&addr_obstack);
15190
15191 obstack_init (&cu_list);
15192 make_cleanup_obstack_free (&cu_list);
15193
1fd400ff
TT
15194 obstack_init (&types_cu_list);
15195 make_cleanup_obstack_free (&types_cu_list);
15196
987d643c
TT
15197 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15198 NULL, xcalloc, xfree);
15199 make_cleanup (cleanup_htab, psyms_seen);
15200
1fd400ff
TT
15201 /* The list is already sorted, so we don't need to do additional
15202 work here. Also, the debug_types entries do not appear in
15203 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15204 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15205 {
e254ef6a
DE
15206 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15207 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15208 gdb_byte val[8];
15209
15210 write_psymbols (symtab,
987d643c 15211 psyms_seen,
9291a0cd 15212 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15213 psymtab->n_global_syms, i,
15214 0);
9291a0cd 15215 write_psymbols (symtab,
987d643c 15216 psyms_seen,
9291a0cd 15217 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15218 psymtab->n_static_syms, i,
15219 1);
9291a0cd
TT
15220
15221 add_address_entry (objfile, &addr_obstack, psymtab, i);
15222
e254ef6a 15223 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15224 obstack_grow (&cu_list, val, 8);
e254ef6a 15225 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15226 obstack_grow (&cu_list, val, 8);
15227 }
15228
1fd400ff
TT
15229 /* Write out the .debug_type entries, if any. */
15230 if (dwarf2_per_objfile->signatured_types)
15231 {
15232 struct signatured_type_index_data sig_data;
15233
15234 sig_data.objfile = objfile;
15235 sig_data.symtab = symtab;
15236 sig_data.types_list = &types_cu_list;
987d643c 15237 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15238 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15239 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15240 write_one_signatured_type, &sig_data);
15241 }
15242
9291a0cd
TT
15243 obstack_init (&constant_pool);
15244 make_cleanup_obstack_free (&constant_pool);
15245 obstack_init (&symtab_obstack);
15246 make_cleanup_obstack_free (&symtab_obstack);
15247 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15248
15249 obstack_init (&contents);
15250 make_cleanup_obstack_free (&contents);
1fd400ff 15251 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15252 total_len = size_of_contents;
15253
15254 /* The version number. */
987d643c 15255 val = MAYBE_SWAP (3);
9291a0cd
TT
15256 obstack_grow (&contents, &val, sizeof (val));
15257
15258 /* The offset of the CU list from the start of the file. */
15259 val = MAYBE_SWAP (total_len);
15260 obstack_grow (&contents, &val, sizeof (val));
15261 total_len += obstack_object_size (&cu_list);
15262
1fd400ff
TT
15263 /* The offset of the types CU list from the start of the file. */
15264 val = MAYBE_SWAP (total_len);
15265 obstack_grow (&contents, &val, sizeof (val));
15266 total_len += obstack_object_size (&types_cu_list);
15267
9291a0cd
TT
15268 /* The offset of the address table from the start of the file. */
15269 val = MAYBE_SWAP (total_len);
15270 obstack_grow (&contents, &val, sizeof (val));
15271 total_len += obstack_object_size (&addr_obstack);
15272
15273 /* The offset of the symbol table from the start of the file. */
15274 val = MAYBE_SWAP (total_len);
15275 obstack_grow (&contents, &val, sizeof (val));
15276 total_len += obstack_object_size (&symtab_obstack);
15277
15278 /* The offset of the constant pool from the start of the file. */
15279 val = MAYBE_SWAP (total_len);
15280 obstack_grow (&contents, &val, sizeof (val));
15281 total_len += obstack_object_size (&constant_pool);
15282
15283 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15284
15285 write_obstack (out_file, &contents);
15286 write_obstack (out_file, &cu_list);
1fd400ff 15287 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15288 write_obstack (out_file, &addr_obstack);
15289 write_obstack (out_file, &symtab_obstack);
15290 write_obstack (out_file, &constant_pool);
15291
15292 fclose (out_file);
15293
15294 /* We want to keep the file, so we set cleanup_filename to NULL
15295 here. See unlink_if_set. */
15296 cleanup_filename = NULL;
15297
15298 do_cleanups (cleanup);
15299}
15300
15301/* The mapped index file format is designed to be directly mmap()able
15302 on any architecture. In most cases, a datum is represented using a
15303 little-endian 32-bit integer value, called an offset_type. Big
15304 endian machines must byte-swap the values before using them.
15305 Exceptions to this rule are noted. The data is laid out such that
15306 alignment is always respected.
15307
15308 A mapped index consists of several sections.
15309
15310 1. The file header. This is a sequence of values, of offset_type
15311 unless otherwise noted:
987d643c
TT
15312
15313 [0] The version number, currently 3. Versions 1 and 2 are
15314 obsolete.
9291a0cd 15315 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15316 [2] The offset, from the start of the file, of the types CU list.
15317 Note that this section can be empty, in which case this offset will
15318 be equal to the next offset.
15319 [3] The offset, from the start of the file, of the address section.
15320 [4] The offset, from the start of the file, of the symbol table.
15321 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15322
15323 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15324 little-endian values, sorted by the CU offset. The first element
15325 in each pair is the offset of a CU in the .debug_info section. The
15326 second element in each pair is the length of that CU. References
15327 to a CU elsewhere in the map are done using a CU index, which is
15328 just the 0-based index into this table. Note that if there are
15329 type CUs, then conceptually CUs and type CUs form a single list for
15330 the purposes of CU indices.
15331
987d643c
TT
15332 3. The types CU list. This is a sequence of triplets of 64-bit
15333 little-endian values. In a triplet, the first value is the CU
15334 offset, the second value is the type offset in the CU, and the
15335 third value is the type signature. The types CU list is not
15336 sorted.
9291a0cd 15337
987d643c 15338 4. The address section. The address section consists of a sequence
9291a0cd
TT
15339 of address entries. Each address entry has three elements.
15340 [0] The low address. This is a 64-bit little-endian value.
15341 [1] The high address. This is a 64-bit little-endian value.
15342 [2] The CU index. This is an offset_type value.
15343
987d643c 15344 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15345 table is always a power of 2. The initial hash and the step are
15346 currently defined by the `find_slot' function.
15347
15348 Each slot in the hash table consists of a pair of offset_type
15349 values. The first value is the offset of the symbol's name in the
15350 constant pool. The second value is the offset of the CU vector in
15351 the constant pool.
15352
15353 If both values are 0, then this slot in the hash table is empty.
15354 This is ok because while 0 is a valid constant pool index, it
15355 cannot be a valid index for both a string and a CU vector.
15356
15357 A string in the constant pool is stored as a \0-terminated string,
15358 as you'd expect.
15359
15360 A CU vector in the constant pool is a sequence of offset_type
15361 values. The first value is the number of CU indices in the vector.
15362 Each subsequent value is the index of a CU in the CU list. This
15363 element in the hash table is used to indicate which CUs define the
15364 symbol.
15365
987d643c 15366 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15367 organized so that alignment is correct: CU vectors are stored
15368 first, followed by strings. */
15369static void
15370save_gdb_index_command (char *arg, int from_tty)
15371{
15372 struct objfile *objfile;
15373
15374 if (!arg || !*arg)
96d19272 15375 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15376
15377 ALL_OBJFILES (objfile)
15378 {
15379 struct stat st;
15380
15381 /* If the objfile does not correspond to an actual file, skip it. */
15382 if (stat (objfile->name, &st) < 0)
15383 continue;
15384
15385 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15386 if (dwarf2_per_objfile)
15387 {
15388 volatile struct gdb_exception except;
15389
15390 TRY_CATCH (except, RETURN_MASK_ERROR)
15391 {
15392 write_psymtabs_to_index (objfile, arg);
15393 }
15394 if (except.reason < 0)
15395 exception_fprintf (gdb_stderr, except,
15396 _("Error while writing index for `%s': "),
15397 objfile->name);
15398 }
15399 }
dce234bc
PP
15400}
15401
9291a0cd
TT
15402\f
15403
9eae7c52
TT
15404int dwarf2_always_disassemble;
15405
15406static void
15407show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15408 struct cmd_list_element *c, const char *value)
15409{
15410 fprintf_filtered (file, _("\
15411Whether to always disassemble DWARF expressions is %s.\n"),
15412 value);
15413}
15414
6502dd73
DJ
15415void _initialize_dwarf2_read (void);
15416
15417void
15418_initialize_dwarf2_read (void)
15419{
96d19272
JK
15420 struct cmd_list_element *c;
15421
dce234bc 15422 dwarf2_objfile_data_key
c1bd65d0 15423 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15424
1bedd215
AC
15425 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15426Set DWARF 2 specific variables.\n\
15427Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15428 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15429 0/*allow-unknown*/, &maintenance_set_cmdlist);
15430
1bedd215
AC
15431 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15432Show DWARF 2 specific variables\n\
15433Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15434 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15435 0/*allow-unknown*/, &maintenance_show_cmdlist);
15436
15437 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15438 &dwarf2_max_cache_age, _("\
15439Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15440Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15441A higher limit means that cached compilation units will be stored\n\
15442in memory longer, and more total memory will be used. Zero disables\n\
15443caching, which can slow down startup."),
2c5b56ce 15444 NULL,
920d2a44 15445 show_dwarf2_max_cache_age,
2c5b56ce 15446 &set_dwarf2_cmdlist,
ae038cb0 15447 &show_dwarf2_cmdlist);
d97bc12b 15448
9eae7c52
TT
15449 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15450 &dwarf2_always_disassemble, _("\
15451Set whether `info address' always disassembles DWARF expressions."), _("\
15452Show whether `info address' always disassembles DWARF expressions."), _("\
15453When enabled, DWARF expressions are always printed in an assembly-like\n\
15454syntax. When disabled, expressions will be printed in a more\n\
15455conversational style, when possible."),
15456 NULL,
15457 show_dwarf2_always_disassemble,
15458 &set_dwarf2_cmdlist,
15459 &show_dwarf2_cmdlist);
15460
d97bc12b
DE
15461 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15462Set debugging of the dwarf2 DIE reader."), _("\
15463Show debugging of the dwarf2 DIE reader."), _("\
15464When enabled (non-zero), DIEs are dumped after they are read in.\n\
15465The value is the maximum depth to print."),
15466 NULL,
15467 NULL,
15468 &setdebuglist, &showdebuglist);
9291a0cd 15469
96d19272
JK
15470 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15471 _("Save a .gdb-index file"),
15472 &save_cmdlist);
15473 set_cmd_completer (c, filename_completer);
6502dd73 15474}
This page took 2.160017 seconds and 4 git commands to generate.