* testsuite/icf_virtual_function_folding_test.cc (class Bar): Add
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
3876f04e
DE
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
673bfd45
DE
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
6502dd73
DJ
226};
227
228static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
229
230/* names of the debugging sections */
231
233a11ab
CS
232/* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235#define INFO_SECTION "debug_info"
236#define ABBREV_SECTION "debug_abbrev"
237#define LINE_SECTION "debug_line"
233a11ab
CS
238#define LOC_SECTION "debug_loc"
239#define MACINFO_SECTION "debug_macinfo"
240#define STR_SECTION "debug_str"
241#define RANGES_SECTION "debug_ranges"
348e048f 242#define TYPES_SECTION "debug_types"
233a11ab
CS
243#define FRAME_SECTION "debug_frame"
244#define EH_FRAME_SECTION "eh_frame"
9291a0cd 245#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
246
247/* local data types */
248
57349743
JB
249/* We hold several abbreviation tables in memory at the same time. */
250#ifndef ABBREV_HASH_SIZE
251#define ABBREV_HASH_SIZE 121
252#endif
253
107d2387
AC
254/* The data in a compilation unit header, after target2host
255 translation, looks like this. */
c906108c 256struct comp_unit_head
a738430d 257{
c764a876 258 unsigned int length;
a738430d 259 short version;
a738430d
MK
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
9cbfa09e 262 unsigned int abbrev_offset;
57349743 263
a738430d
MK
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
57349743 266
a738430d
MK
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
57349743 269
a738430d
MK
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
57349743 273
d00adf39
DE
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
a738430d 277};
c906108c 278
3da10d80
KS
279/* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281struct delayed_method_info
282{
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297};
298
299typedef struct delayed_method_info delayed_method_info;
300DEF_VEC_O (delayed_method_info);
301
e7c27a73
DJ
302/* Internal state when decoding a particular compilation unit. */
303struct dwarf2_cu
304{
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
d00adf39 308 /* The header of the compilation unit. */
e7c27a73 309 struct comp_unit_head header;
e142c38c 310
d00adf39
DE
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
e142c38c
DJ
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492
DJ
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
10b3939b 361 /* A hash table of die offsets for following references. */
51545339 362 htab_t die_hash;
10b3939b
DJ
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
cb1df416
DJ
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
3da10d80
KS
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
ae038cb0
DJ
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
72bf9492
DJ
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
e7c27a73
DJ
391};
392
9291a0cd
TT
393/* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396struct dwarf2_per_cu_quick_data
397{
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417};
418
10b3939b
DJ
419/* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
ae038cb0
DJ
425struct dwarf2_per_cu_data
426{
348e048f 427 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 428 bytes should suffice to store the length of any compilation unit
45452591
DE
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
c764a876 432 unsigned int offset;
348e048f 433 unsigned int length : 29;
ae038cb0
DJ
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
c764a876 437 unsigned int queued : 1;
ae038cb0 438
5afb4e99
DJ
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
348e048f
DE
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
17ea53c3
JK
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
ae038cb0 451 struct dwarf2_cu *cu;
1c379e20 452
9291a0cd
TT
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
ae038cb0
DJ
468};
469
348e048f
DE
470/* Entry in the signatured_types hash table. */
471
472struct signatured_type
473{
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484};
485
93311388
DE
486/* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492struct die_reader_specs
493{
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503};
504
debd256d
JB
505/* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508struct line_header
509{
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
2dc7f7b3 514 unsigned char maximum_ops_per_instruction;
debd256d
JB
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
c906108c 538 {
debd256d
JB
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
aaa75496 543 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 544 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
545 } *file_names;
546
547 /* The start and end of the statement program following this
6502dd73 548 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 549 gdb_byte *statement_program_start, *statement_program_end;
debd256d 550};
c906108c
SS
551
552/* When we construct a partial symbol table entry we only
553 need this much information. */
554struct partial_die_info
555 {
72bf9492 556 /* Offset of this DIE. */
c906108c 557 unsigned int offset;
72bf9492
DJ
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
72bf9492
DJ
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
fa4028e9
JB
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
98bfdba5
PA
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
abc72ce4
DE
580 /* Flag set if fixup_partial_die has been called on this die. */
581 unsigned int fixup_called : 1;
582
72bf9492 583 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 584 sometimes a default name for unnamed DIEs. */
c906108c 585 char *name;
72bf9492 586
abc72ce4
DE
587 /* The linkage name, if present. */
588 const char *linkage_name;
589
72bf9492
DJ
590 /* The scope to prepend to our children. This is generally
591 allocated on the comp_unit_obstack, so will disappear
592 when this compilation unit leaves the cache. */
593 char *scope;
594
595 /* The location description associated with this DIE, if any. */
596 struct dwarf_block *locdesc;
597
598 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
599 CORE_ADDR lowpc;
600 CORE_ADDR highpc;
72bf9492 601
93311388 602 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 603 DW_AT_sibling, if any. */
abc72ce4
DE
604 /* NOTE: This member isn't strictly necessary, read_partial_die could
605 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 606 gdb_byte *sibling;
72bf9492
DJ
607
608 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
609 DW_AT_specification (or DW_AT_abstract_origin or
610 DW_AT_extension). */
611 unsigned int spec_offset;
612
613 /* Pointers to this DIE's parent, first child, and next sibling,
614 if any. */
615 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
616 };
617
618/* This data structure holds the information of an abbrev. */
619struct abbrev_info
620 {
621 unsigned int number; /* number identifying abbrev */
622 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
623 unsigned short has_children; /* boolean */
624 unsigned short num_attrs; /* number of attributes */
c906108c
SS
625 struct attr_abbrev *attrs; /* an array of attribute descriptions */
626 struct abbrev_info *next; /* next in chain */
627 };
628
629struct attr_abbrev
630 {
9d25dd43
DE
631 ENUM_BITFIELD(dwarf_attribute) name : 16;
632 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
633 };
634
b60c80d6
DJ
635/* Attributes have a name and a value */
636struct attribute
637 {
9d25dd43 638 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
639 ENUM_BITFIELD(dwarf_form) form : 15;
640
641 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
642 field should be in u.str (existing only for DW_STRING) but it is kept
643 here for better struct attribute alignment. */
644 unsigned int string_is_canonical : 1;
645
b60c80d6
DJ
646 union
647 {
648 char *str;
649 struct dwarf_block *blk;
43bbcdc2
PH
650 ULONGEST unsnd;
651 LONGEST snd;
b60c80d6 652 CORE_ADDR addr;
348e048f 653 struct signatured_type *signatured_type;
b60c80d6
DJ
654 }
655 u;
656 };
657
c906108c
SS
658/* This data structure holds a complete die structure. */
659struct die_info
660 {
76815b17
DE
661 /* DWARF-2 tag for this DIE. */
662 ENUM_BITFIELD(dwarf_tag) tag : 16;
663
664 /* Number of attributes */
98bfdba5
PA
665 unsigned char num_attrs;
666
667 /* True if we're presently building the full type name for the
668 type derived from this DIE. */
669 unsigned char building_fullname : 1;
76815b17
DE
670
671 /* Abbrev number */
672 unsigned int abbrev;
673
93311388 674 /* Offset in .debug_info or .debug_types section. */
76815b17 675 unsigned int offset;
78ba4af6
JB
676
677 /* The dies in a compilation unit form an n-ary tree. PARENT
678 points to this die's parent; CHILD points to the first child of
679 this node; and all the children of a given node are chained
4950bc1c 680 together via their SIBLING fields. */
639d11d3
DC
681 struct die_info *child; /* Its first child, if any. */
682 struct die_info *sibling; /* Its next sibling, if any. */
683 struct die_info *parent; /* Its parent, if any. */
c906108c 684
b60c80d6
DJ
685 /* An array of attributes, with NUM_ATTRS elements. There may be
686 zero, but it's not common and zero-sized arrays are not
687 sufficiently portable C. */
688 struct attribute attrs[1];
c906108c
SS
689 };
690
5fb290d7
DJ
691struct function_range
692{
693 const char *name;
694 CORE_ADDR lowpc, highpc;
695 int seen_line;
696 struct function_range *next;
697};
698
c906108c
SS
699/* Get at parts of an attribute structure */
700
701#define DW_STRING(attr) ((attr)->u.str)
8285870a 702#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
703#define DW_UNSND(attr) ((attr)->u.unsnd)
704#define DW_BLOCK(attr) ((attr)->u.blk)
705#define DW_SND(attr) ((attr)->u.snd)
706#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 707#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
708
709/* Blocks are a bunch of untyped bytes. */
710struct dwarf_block
711 {
712 unsigned int size;
fe1b8b76 713 gdb_byte *data;
c906108c
SS
714 };
715
c906108c
SS
716#ifndef ATTR_ALLOC_CHUNK
717#define ATTR_ALLOC_CHUNK 4
718#endif
719
c906108c
SS
720/* Allocate fields for structs, unions and enums in this size. */
721#ifndef DW_FIELD_ALLOC_CHUNK
722#define DW_FIELD_ALLOC_CHUNK 4
723#endif
724
c906108c
SS
725/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
726 but this would require a corresponding change in unpack_field_as_long
727 and friends. */
728static int bits_per_byte = 8;
729
730/* The routines that read and process dies for a C struct or C++ class
731 pass lists of data member fields and lists of member function fields
732 in an instance of a field_info structure, as defined below. */
733struct field_info
c5aa993b
JM
734 {
735 /* List of data member and baseclasses fields. */
736 struct nextfield
737 {
738 struct nextfield *next;
739 int accessibility;
740 int virtuality;
741 struct field field;
742 }
7d0ccb61 743 *fields, *baseclasses;
c906108c 744
7d0ccb61 745 /* Number of fields (including baseclasses). */
c5aa993b 746 int nfields;
c906108c 747
c5aa993b
JM
748 /* Number of baseclasses. */
749 int nbaseclasses;
c906108c 750
c5aa993b
JM
751 /* Set if the accesibility of one of the fields is not public. */
752 int non_public_fields;
c906108c 753
c5aa993b
JM
754 /* Member function fields array, entries are allocated in the order they
755 are encountered in the object file. */
756 struct nextfnfield
757 {
758 struct nextfnfield *next;
759 struct fn_field fnfield;
760 }
761 *fnfields;
c906108c 762
c5aa993b
JM
763 /* Member function fieldlist array, contains name of possibly overloaded
764 member function, number of overloaded member functions and a pointer
765 to the head of the member function field chain. */
766 struct fnfieldlist
767 {
768 char *name;
769 int length;
770 struct nextfnfield *head;
771 }
772 *fnfieldlists;
c906108c 773
c5aa993b
JM
774 /* Number of entries in the fnfieldlists array. */
775 int nfnfields;
98751a41
JK
776
777 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
778 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
779 struct typedef_field_list
780 {
781 struct typedef_field field;
782 struct typedef_field_list *next;
783 }
784 *typedef_field_list;
785 unsigned typedef_field_list_count;
c5aa993b 786 };
c906108c 787
10b3939b
DJ
788/* One item on the queue of compilation units to read in full symbols
789 for. */
790struct dwarf2_queue_item
791{
792 struct dwarf2_per_cu_data *per_cu;
793 struct dwarf2_queue_item *next;
794};
795
796/* The current queue. */
797static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
798
ae038cb0
DJ
799/* Loaded secondary compilation units are kept in memory until they
800 have not been referenced for the processing of this many
801 compilation units. Set this to zero to disable caching. Cache
802 sizes of up to at least twenty will improve startup time for
803 typical inter-CU-reference binaries, at an obvious memory cost. */
804static int dwarf2_max_cache_age = 5;
920d2a44
AC
805static void
806show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
807 struct cmd_list_element *c, const char *value)
808{
809 fprintf_filtered (file, _("\
810The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
811 value);
812}
813
ae038cb0 814
c906108c
SS
815/* Various complaints about symbol reading that don't abort the process */
816
4d3c2250
KB
817static void
818dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 819{
4d3c2250 820 complaint (&symfile_complaints,
e2e0b3e5 821 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
822}
823
25e43795
DJ
824static void
825dwarf2_debug_line_missing_file_complaint (void)
826{
827 complaint (&symfile_complaints,
828 _(".debug_line section has line data without a file"));
829}
830
59205f5a
JB
831static void
832dwarf2_debug_line_missing_end_sequence_complaint (void)
833{
834 complaint (&symfile_complaints,
835 _(".debug_line section has line program sequence without an end"));
836}
837
4d3c2250
KB
838static void
839dwarf2_complex_location_expr_complaint (void)
2e276125 840{
e2e0b3e5 841 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
842}
843
4d3c2250
KB
844static void
845dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
846 int arg3)
2e276125 847{
4d3c2250 848 complaint (&symfile_complaints,
e2e0b3e5 849 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
850 arg2, arg3);
851}
852
853static void
854dwarf2_macros_too_long_complaint (void)
2e276125 855{
4d3c2250 856 complaint (&symfile_complaints,
e2e0b3e5 857 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
858}
859
860static void
861dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 862{
4d3c2250 863 complaint (&symfile_complaints,
e2e0b3e5 864 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
865 arg1);
866}
867
868static void
869dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 870{
4d3c2250 871 complaint (&symfile_complaints,
e2e0b3e5 872 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 873}
c906108c 874
c906108c
SS
875/* local function prototypes */
876
4efb68b1 877static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 878
aaa75496
JB
879static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
880 struct objfile *);
881
c67a9c90 882static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 883
72bf9492
DJ
884static void scan_partial_symbols (struct partial_die_info *,
885 CORE_ADDR *, CORE_ADDR *,
5734ee8b 886 int, struct dwarf2_cu *);
c906108c 887
72bf9492
DJ
888static void add_partial_symbol (struct partial_die_info *,
889 struct dwarf2_cu *);
63d06c5c 890
72bf9492
DJ
891static void add_partial_namespace (struct partial_die_info *pdi,
892 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 893 int need_pc, struct dwarf2_cu *cu);
63d06c5c 894
5d7cb8df
JK
895static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
896 CORE_ADDR *highpc, int need_pc,
897 struct dwarf2_cu *cu);
898
72bf9492
DJ
899static void add_partial_enumeration (struct partial_die_info *enum_pdi,
900 struct dwarf2_cu *cu);
91c24f0a 901
bc30ff58
JB
902static void add_partial_subprogram (struct partial_die_info *pdi,
903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 904 int need_pc, struct dwarf2_cu *cu);
bc30ff58 905
fe1b8b76 906static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
907 gdb_byte *buffer, gdb_byte *info_ptr,
908 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 909
a14ed312 910static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 911
a14ed312 912static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 913
e7c27a73 914static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 915
f3dd6933 916static void dwarf2_free_abbrev_table (void *);
c906108c 917
fe1b8b76 918static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 919 struct dwarf2_cu *);
72bf9492 920
57349743 921static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 922 struct dwarf2_cu *);
c906108c 923
93311388
DE
924static struct partial_die_info *load_partial_dies (bfd *,
925 gdb_byte *, gdb_byte *,
926 int, struct dwarf2_cu *);
72bf9492 927
fe1b8b76 928static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
929 struct abbrev_info *abbrev,
930 unsigned int, bfd *,
931 gdb_byte *, gdb_byte *,
932 struct dwarf2_cu *);
c906108c 933
c764a876 934static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 935 struct dwarf2_cu *);
72bf9492
DJ
936
937static void fixup_partial_die (struct partial_die_info *,
938 struct dwarf2_cu *);
939
fe1b8b76
JB
940static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 942
fe1b8b76
JB
943static gdb_byte *read_attribute_value (struct attribute *, unsigned,
944 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 945
fe1b8b76 946static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 947
fe1b8b76 948static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 949
fe1b8b76 950static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 951
fe1b8b76 952static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 953
93311388 954static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 955
fe1b8b76 956static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 957 unsigned int *);
c906108c 958
c764a876
DE
959static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
960
961static LONGEST read_checked_initial_length_and_offset
962 (bfd *, gdb_byte *, const struct comp_unit_head *,
963 unsigned int *, unsigned int *);
613e1657 964
fe1b8b76 965static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
966 unsigned int *);
967
968static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 969
fe1b8b76 970static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 971
9b1c24c8 972static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 973
fe1b8b76
JB
974static char *read_indirect_string (bfd *, gdb_byte *,
975 const struct comp_unit_head *,
976 unsigned int *);
4bdf3d34 977
fe1b8b76 978static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 979
fe1b8b76 980static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 981
fe1b8b76 982static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 983
e142c38c 984static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 985
e142c38c
DJ
986static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987 struct dwarf2_cu *);
c906108c 988
348e048f
DE
989static struct attribute *dwarf2_attr_no_follow (struct die_info *,
990 unsigned int,
991 struct dwarf2_cu *);
992
05cf31d1
JB
993static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
994 struct dwarf2_cu *cu);
995
e142c38c 996static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 997
e142c38c 998static struct die_info *die_specification (struct die_info *die,
f2f0e013 999 struct dwarf2_cu **);
63d06c5c 1000
debd256d
JB
1001static void free_line_header (struct line_header *lh);
1002
aaa75496
JB
1003static void add_file_name (struct line_header *, char *, unsigned int,
1004 unsigned int, unsigned int);
1005
debd256d
JB
1006static struct line_header *(dwarf_decode_line_header
1007 (unsigned int offset,
e7c27a73 1008 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1009
72b9f47f 1010static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1011 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1012
72b9f47f 1013static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1014
a14ed312 1015static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1016 struct dwarf2_cu *);
c906108c 1017
34eaf542
TT
1018static struct symbol *new_symbol_full (struct die_info *, struct type *,
1019 struct dwarf2_cu *, struct symbol *);
1020
a14ed312 1021static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1022 struct dwarf2_cu *);
c906108c 1023
98bfdba5
PA
1024static void dwarf2_const_value_attr (struct attribute *attr,
1025 struct type *type,
1026 const char *name,
1027 struct obstack *obstack,
1028 struct dwarf2_cu *cu, long *value,
1029 gdb_byte **bytes,
1030 struct dwarf2_locexpr_baton **baton);
2df3850c 1031
e7c27a73 1032static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1033
b4ba55a1
JB
1034static int need_gnat_info (struct dwarf2_cu *);
1035
1036static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1037
1038static void set_descriptive_type (struct type *, struct die_info *,
1039 struct dwarf2_cu *);
1040
e7c27a73
DJ
1041static struct type *die_containing_type (struct die_info *,
1042 struct dwarf2_cu *);
c906108c 1043
673bfd45
DE
1044static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045 struct dwarf2_cu *);
c906108c 1046
f792889a 1047static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1048
673bfd45
DE
1049static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
086ed43d 1051static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1052
6e70227d 1053static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1054 const char *suffix, int physname,
1055 struct dwarf2_cu *cu);
63d06c5c 1056
e7c27a73 1057static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1058
348e048f
DE
1059static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
e7c27a73 1061static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1062
e7c27a73 1063static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1064
ff013f42
JK
1065static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066 struct dwarf2_cu *, struct partial_symtab *);
1067
a14ed312 1068static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1069 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070 struct partial_symtab *);
c906108c 1071
fae299cd
DC
1072static void get_scope_pc_bounds (struct die_info *,
1073 CORE_ADDR *, CORE_ADDR *,
1074 struct dwarf2_cu *);
1075
801e3a5b
JB
1076static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077 CORE_ADDR, struct dwarf2_cu *);
1078
a14ed312 1079static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1080 struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1083 struct type *, struct dwarf2_cu *);
c906108c 1084
a14ed312 1085static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1086 struct die_info *, struct type *,
e7c27a73 1087 struct dwarf2_cu *);
c906108c 1088
a14ed312 1089static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1090 struct type *, struct dwarf2_cu *);
c906108c 1091
134d01f1 1092static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1093
e7c27a73 1094static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1097
5d7cb8df
JK
1098static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1099
27aa8d6a
SW
1100static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1101
f55ee35c
JK
1102static struct type *read_module_type (struct die_info *die,
1103 struct dwarf2_cu *cu);
1104
38d518c9 1105static const char *namespace_name (struct die_info *die,
e142c38c 1106 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1107
134d01f1 1108static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1109
e7c27a73 1110static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1111
6e70227d 1112static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1113 struct dwarf2_cu *);
1114
93311388 1115static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1116
93311388
DE
1117static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1118 gdb_byte *info_ptr,
d97bc12b
DE
1119 gdb_byte **new_info_ptr,
1120 struct die_info *parent);
1121
93311388
DE
1122static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1123 gdb_byte *info_ptr,
fe1b8b76 1124 gdb_byte **new_info_ptr,
639d11d3
DC
1125 struct die_info *parent);
1126
93311388
DE
1127static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1128 gdb_byte *info_ptr,
fe1b8b76 1129 gdb_byte **new_info_ptr,
639d11d3
DC
1130 struct die_info *parent);
1131
93311388
DE
1132static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1133 struct die_info **, gdb_byte *,
1134 int *);
1135
e7c27a73 1136static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1137
71c25dea
TT
1138static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1139 struct obstack *);
1140
e142c38c 1141static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1142
98bfdba5
PA
1143static const char *dwarf2_full_name (char *name,
1144 struct die_info *die,
1145 struct dwarf2_cu *cu);
1146
e142c38c 1147static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1148 struct dwarf2_cu **);
9219021c 1149
a14ed312 1150static char *dwarf_tag_name (unsigned int);
c906108c 1151
a14ed312 1152static char *dwarf_attr_name (unsigned int);
c906108c 1153
a14ed312 1154static char *dwarf_form_name (unsigned int);
c906108c 1155
a14ed312 1156static char *dwarf_bool_name (unsigned int);
c906108c 1157
a14ed312 1158static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1159
1160#if 0
a14ed312 1161static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1162#endif
1163
f9aca02d 1164static struct die_info *sibling_die (struct die_info *);
c906108c 1165
d97bc12b
DE
1166static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1167
1168static void dump_die_for_error (struct die_info *);
1169
1170static void dump_die_1 (struct ui_file *, int level, int max_level,
1171 struct die_info *);
c906108c 1172
d97bc12b 1173/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1174
51545339 1175static void store_in_ref_table (struct die_info *,
10b3939b 1176 struct dwarf2_cu *);
c906108c 1177
93311388
DE
1178static int is_ref_attr (struct attribute *);
1179
c764a876 1180static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1181
43bbcdc2 1182static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1183
348e048f
DE
1184static struct die_info *follow_die_ref_or_sig (struct die_info *,
1185 struct attribute *,
1186 struct dwarf2_cu **);
1187
10b3939b
DJ
1188static struct die_info *follow_die_ref (struct die_info *,
1189 struct attribute *,
f2f0e013 1190 struct dwarf2_cu **);
c906108c 1191
348e048f
DE
1192static struct die_info *follow_die_sig (struct die_info *,
1193 struct attribute *,
1194 struct dwarf2_cu **);
1195
1196static void read_signatured_type_at_offset (struct objfile *objfile,
1197 unsigned int offset);
1198
1199static void read_signatured_type (struct objfile *,
1200 struct signatured_type *type_sig);
1201
c906108c
SS
1202/* memory allocation interface */
1203
7b5a2f43 1204static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1205
f3dd6933 1206static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1207
b60c80d6 1208static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1209
e142c38c 1210static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1211
e142c38c
DJ
1212static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1213 struct dwarf2_cu *);
5fb290d7 1214
2e276125 1215static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1216 char *, bfd *, struct dwarf2_cu *);
2e276125 1217
8e19ed76
PS
1218static int attr_form_is_block (struct attribute *);
1219
3690dd37
JB
1220static int attr_form_is_section_offset (struct attribute *);
1221
1222static int attr_form_is_constant (struct attribute *);
1223
8cf6f0b1
TT
1224static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1225 struct dwarf2_loclist_baton *baton,
1226 struct attribute *attr);
1227
93e7bd98
DJ
1228static void dwarf2_symbol_mark_computed (struct attribute *attr,
1229 struct symbol *sym,
1230 struct dwarf2_cu *cu);
4c2df51b 1231
93311388
DE
1232static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1233 struct abbrev_info *abbrev,
1234 struct dwarf2_cu *cu);
4bb7a0a7 1235
72bf9492
DJ
1236static void free_stack_comp_unit (void *);
1237
72bf9492
DJ
1238static hashval_t partial_die_hash (const void *item);
1239
1240static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1241
ae038cb0 1242static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1243 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1244
1245static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1246 (unsigned int offset, struct objfile *objfile);
ae038cb0 1247
9816fde3
JK
1248static void init_one_comp_unit (struct dwarf2_cu *cu,
1249 struct objfile *objfile);
1250
1251static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1252 struct die_info *comp_unit_die);
93311388 1253
ae038cb0
DJ
1254static void free_one_comp_unit (void *);
1255
1256static void free_cached_comp_units (void *);
1257
1258static void age_cached_comp_units (void);
1259
1260static void free_one_cached_comp_unit (void *);
1261
f792889a
DJ
1262static struct type *set_die_type (struct die_info *, struct type *,
1263 struct dwarf2_cu *);
1c379e20 1264
ae038cb0
DJ
1265static void create_all_comp_units (struct objfile *);
1266
1fd400ff
TT
1267static int create_debug_types_hash_table (struct objfile *objfile);
1268
93311388
DE
1269static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1270 struct objfile *);
10b3939b
DJ
1271
1272static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1273
1274static void dwarf2_add_dependence (struct dwarf2_cu *,
1275 struct dwarf2_per_cu_data *);
1276
ae038cb0
DJ
1277static void dwarf2_mark (struct dwarf2_cu *);
1278
1279static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1280
673bfd45
DE
1281static struct type *get_die_type_at_offset (unsigned int,
1282 struct dwarf2_per_cu_data *per_cu);
1283
f792889a 1284static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1285
9291a0cd
TT
1286static void dwarf2_release_queue (void *dummy);
1287
1288static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1289 struct objfile *objfile);
1290
1291static void process_queue (struct objfile *objfile);
1292
1293static void find_file_and_directory (struct die_info *die,
1294 struct dwarf2_cu *cu,
1295 char **name, char **comp_dir);
1296
1297static char *file_full_name (int file, struct line_header *lh,
1298 const char *comp_dir);
1299
1300static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1301 gdb_byte *info_ptr,
1302 gdb_byte *buffer,
1303 unsigned int buffer_size,
1304 bfd *abfd);
1305
1306static void init_cu_die_reader (struct die_reader_specs *reader,
1307 struct dwarf2_cu *cu);
1308
673bfd45 1309static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1310
9291a0cd
TT
1311#if WORDS_BIGENDIAN
1312
1313/* Convert VALUE between big- and little-endian. */
1314static offset_type
1315byte_swap (offset_type value)
1316{
1317 offset_type result;
1318
1319 result = (value & 0xff) << 24;
1320 result |= (value & 0xff00) << 8;
1321 result |= (value & 0xff0000) >> 8;
1322 result |= (value & 0xff000000) >> 24;
1323 return result;
1324}
1325
1326#define MAYBE_SWAP(V) byte_swap (V)
1327
1328#else
1329#define MAYBE_SWAP(V) (V)
1330#endif /* WORDS_BIGENDIAN */
1331
1332/* The suffix for an index file. */
1333#define INDEX_SUFFIX ".gdb-index"
1334
3da10d80
KS
1335static const char *dwarf2_physname (char *name, struct die_info *die,
1336 struct dwarf2_cu *cu);
1337
c906108c
SS
1338/* Try to locate the sections we need for DWARF 2 debugging
1339 information and return true if we have enough to do something. */
1340
1341int
6502dd73 1342dwarf2_has_info (struct objfile *objfile)
c906108c 1343{
be391dca
TT
1344 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1345 if (!dwarf2_per_objfile)
1346 {
1347 /* Initialize per-objfile state. */
1348 struct dwarf2_per_objfile *data
1349 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1350
be391dca
TT
1351 memset (data, 0, sizeof (*data));
1352 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1353 dwarf2_per_objfile = data;
6502dd73 1354
be391dca
TT
1355 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1356 dwarf2_per_objfile->objfile = objfile;
1357 }
1358 return (dwarf2_per_objfile->info.asection != NULL
1359 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1360}
1361
233a11ab
CS
1362/* When loading sections, we can either look for ".<name>", or for
1363 * ".z<name>", which indicates a compressed section. */
1364
1365static int
dce234bc 1366section_is_p (const char *section_name, const char *name)
233a11ab 1367{
dce234bc
PP
1368 return (section_name[0] == '.'
1369 && (strcmp (section_name + 1, name) == 0
1370 || (section_name[1] == 'z'
1371 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1372}
1373
c906108c
SS
1374/* This function is mapped across the sections and remembers the
1375 offset and size of each of the debugging sections we are interested
1376 in. */
1377
1378static void
72dca2f5 1379dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1380{
dce234bc 1381 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1382 {
dce234bc
PP
1383 dwarf2_per_objfile->info.asection = sectp;
1384 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1385 }
dce234bc 1386 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1387 {
dce234bc
PP
1388 dwarf2_per_objfile->abbrev.asection = sectp;
1389 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1390 }
dce234bc 1391 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1392 {
dce234bc
PP
1393 dwarf2_per_objfile->line.asection = sectp;
1394 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1395 }
dce234bc 1396 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1397 {
dce234bc
PP
1398 dwarf2_per_objfile->loc.asection = sectp;
1399 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1400 }
dce234bc 1401 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1402 {
dce234bc
PP
1403 dwarf2_per_objfile->macinfo.asection = sectp;
1404 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1405 }
dce234bc 1406 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1407 {
dce234bc
PP
1408 dwarf2_per_objfile->str.asection = sectp;
1409 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1410 }
dce234bc 1411 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1412 {
dce234bc
PP
1413 dwarf2_per_objfile->frame.asection = sectp;
1414 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1415 }
dce234bc 1416 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1417 {
3799ccc6 1418 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1419
3799ccc6
EZ
1420 if (aflag & SEC_HAS_CONTENTS)
1421 {
dce234bc
PP
1422 dwarf2_per_objfile->eh_frame.asection = sectp;
1423 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1424 }
b6af0555 1425 }
dce234bc 1426 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1427 {
dce234bc
PP
1428 dwarf2_per_objfile->ranges.asection = sectp;
1429 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1430 }
348e048f
DE
1431 else if (section_is_p (sectp->name, TYPES_SECTION))
1432 {
1433 dwarf2_per_objfile->types.asection = sectp;
1434 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1435 }
9291a0cd
TT
1436 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1437 {
1438 dwarf2_per_objfile->gdb_index.asection = sectp;
1439 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1440 }
dce234bc 1441
72dca2f5
FR
1442 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1443 && bfd_section_vma (abfd, sectp) == 0)
1444 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1445}
1446
dce234bc
PP
1447/* Decompress a section that was compressed using zlib. Store the
1448 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1449
1450static void
dce234bc
PP
1451zlib_decompress_section (struct objfile *objfile, asection *sectp,
1452 gdb_byte **outbuf, bfd_size_type *outsize)
1453{
1454 bfd *abfd = objfile->obfd;
1455#ifndef HAVE_ZLIB_H
1456 error (_("Support for zlib-compressed DWARF data (from '%s') "
1457 "is disabled in this copy of GDB"),
1458 bfd_get_filename (abfd));
1459#else
1460 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1461 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1462 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1463 bfd_size_type uncompressed_size;
1464 gdb_byte *uncompressed_buffer;
1465 z_stream strm;
1466 int rc;
1467 int header_size = 12;
1468
1469 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1470 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1471 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1472 bfd_get_filename (abfd));
1473
1474 /* Read the zlib header. In this case, it should be "ZLIB" followed
1475 by the uncompressed section size, 8 bytes in big-endian order. */
1476 if (compressed_size < header_size
1477 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1478 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1479 bfd_get_filename (abfd));
1480 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1481 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[11];
1488
1489 /* It is possible the section consists of several compressed
1490 buffers concatenated together, so we uncompress in a loop. */
1491 strm.zalloc = NULL;
1492 strm.zfree = NULL;
1493 strm.opaque = NULL;
1494 strm.avail_in = compressed_size - header_size;
1495 strm.next_in = (Bytef*) compressed_buffer + header_size;
1496 strm.avail_out = uncompressed_size;
1497 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1498 uncompressed_size);
1499 rc = inflateInit (&strm);
1500 while (strm.avail_in > 0)
1501 {
1502 if (rc != Z_OK)
1503 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1504 bfd_get_filename (abfd), rc);
1505 strm.next_out = ((Bytef*) uncompressed_buffer
1506 + (uncompressed_size - strm.avail_out));
1507 rc = inflate (&strm, Z_FINISH);
1508 if (rc != Z_STREAM_END)
1509 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 rc = inflateReset (&strm);
1512 }
1513 rc = inflateEnd (&strm);
1514 if (rc != Z_OK
1515 || strm.avail_out != 0)
1516 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1517 bfd_get_filename (abfd), rc);
1518
affddf13 1519 do_cleanups (cleanup);
dce234bc
PP
1520 *outbuf = uncompressed_buffer;
1521 *outsize = uncompressed_size;
1522#endif
233a11ab
CS
1523}
1524
dce234bc
PP
1525/* Read the contents of the section SECTP from object file specified by
1526 OBJFILE, store info about the section into INFO.
1527 If the section is compressed, uncompress it before returning. */
c906108c 1528
dce234bc
PP
1529static void
1530dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1531{
dce234bc
PP
1532 bfd *abfd = objfile->obfd;
1533 asection *sectp = info->asection;
1534 gdb_byte *buf, *retbuf;
1535 unsigned char header[4];
c906108c 1536
be391dca
TT
1537 if (info->readin)
1538 return;
dce234bc
PP
1539 info->buffer = NULL;
1540 info->was_mmapped = 0;
be391dca 1541 info->readin = 1;
188dd5d6 1542
dce234bc
PP
1543 if (info->asection == NULL || info->size == 0)
1544 return;
c906108c 1545
dce234bc
PP
1546 /* Check if the file has a 4-byte header indicating compression. */
1547 if (info->size > sizeof (header)
1548 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1549 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1550 {
1551 /* Upon decompression, update the buffer and its size. */
1552 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1553 {
1554 zlib_decompress_section (objfile, sectp, &info->buffer,
1555 &info->size);
1556 return;
1557 }
1558 }
4bdf3d34 1559
dce234bc
PP
1560#ifdef HAVE_MMAP
1561 if (pagesize == 0)
1562 pagesize = getpagesize ();
2e276125 1563
dce234bc
PP
1564 /* Only try to mmap sections which are large enough: we don't want to
1565 waste space due to fragmentation. Also, only try mmap for sections
1566 without relocations. */
1567
1568 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1569 {
1570 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1571 size_t map_length = info->size + sectp->filepos - pg_offset;
1572 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1573 MAP_PRIVATE, pg_offset);
1574
1575 if (retbuf != MAP_FAILED)
1576 {
1577 info->was_mmapped = 1;
1578 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1579#if HAVE_POSIX_MADVISE
1580 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1581#endif
dce234bc
PP
1582 return;
1583 }
1584 }
1585#endif
1586
1587 /* If we get here, we are a normal, not-compressed section. */
1588 info->buffer = buf
1589 = obstack_alloc (&objfile->objfile_obstack, info->size);
1590
1591 /* When debugging .o files, we may need to apply relocations; see
1592 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1593 We never compress sections in .o files, so we only need to
1594 try this when the section is not compressed. */
ac8035ab 1595 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1596 if (retbuf != NULL)
1597 {
1598 info->buffer = retbuf;
1599 return;
1600 }
1601
1602 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1603 || bfd_bread (buf, info->size, abfd) != info->size)
1604 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1605 bfd_get_filename (abfd));
1606}
1607
1608/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1609 SECTION_NAME. */
af34e669 1610
dce234bc
PP
1611void
1612dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1613 asection **sectp, gdb_byte **bufp,
1614 bfd_size_type *sizep)
1615{
1616 struct dwarf2_per_objfile *data
1617 = objfile_data (objfile, dwarf2_objfile_data_key);
1618 struct dwarf2_section_info *info;
a3b2a86b
TT
1619
1620 /* We may see an objfile without any DWARF, in which case we just
1621 return nothing. */
1622 if (data == NULL)
1623 {
1624 *sectp = NULL;
1625 *bufp = NULL;
1626 *sizep = 0;
1627 return;
1628 }
dce234bc
PP
1629 if (section_is_p (section_name, EH_FRAME_SECTION))
1630 info = &data->eh_frame;
1631 else if (section_is_p (section_name, FRAME_SECTION))
1632 info = &data->frame;
0d53c4c4 1633 else
f3574227 1634 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1635
1636 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1637 /* We haven't read this section in yet. Do it now. */
1638 dwarf2_read_section (objfile, info);
1639
1640 *sectp = info->asection;
1641 *bufp = info->buffer;
1642 *sizep = info->size;
1643}
1644
9291a0cd
TT
1645\f
1646
1647/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1648 this CU came. */
2fdf6df6 1649
9291a0cd
TT
1650static void
1651dw2_do_instantiate_symtab (struct objfile *objfile,
1652 struct dwarf2_per_cu_data *per_cu)
1653{
1654 struct cleanup *back_to;
1655
1656 back_to = make_cleanup (dwarf2_release_queue, NULL);
1657
1658 queue_comp_unit (per_cu, objfile);
1659
1660 if (per_cu->from_debug_types)
1661 read_signatured_type_at_offset (objfile, per_cu->offset);
1662 else
1663 load_full_comp_unit (per_cu, objfile);
1664
1665 process_queue (objfile);
1666
1667 /* Age the cache, releasing compilation units that have not
1668 been used recently. */
1669 age_cached_comp_units ();
1670
1671 do_cleanups (back_to);
1672}
1673
1674/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1675 the objfile from which this CU came. Returns the resulting symbol
1676 table. */
2fdf6df6 1677
9291a0cd
TT
1678static struct symtab *
1679dw2_instantiate_symtab (struct objfile *objfile,
1680 struct dwarf2_per_cu_data *per_cu)
1681{
1682 if (!per_cu->v.quick->symtab)
1683 {
1684 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1685 increment_reading_symtab ();
1686 dw2_do_instantiate_symtab (objfile, per_cu);
1687 do_cleanups (back_to);
1688 }
1689 return per_cu->v.quick->symtab;
1690}
1691
1fd400ff 1692/* Return the CU given its index. */
2fdf6df6 1693
1fd400ff
TT
1694static struct dwarf2_per_cu_data *
1695dw2_get_cu (int index)
1696{
1697 if (index >= dwarf2_per_objfile->n_comp_units)
1698 {
1699 index -= dwarf2_per_objfile->n_comp_units;
1700 return dwarf2_per_objfile->type_comp_units[index];
1701 }
1702 return dwarf2_per_objfile->all_comp_units[index];
1703}
1704
9291a0cd
TT
1705/* A helper function that knows how to read a 64-bit value in a way
1706 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1707 otherwise. */
2fdf6df6 1708
9291a0cd
TT
1709static int
1710extract_cu_value (const char *bytes, ULONGEST *result)
1711{
1712 if (sizeof (ULONGEST) < 8)
1713 {
1714 int i;
1715
1716 /* Ignore the upper 4 bytes if they are all zero. */
1717 for (i = 0; i < 4; ++i)
1718 if (bytes[i + 4] != 0)
1719 return 0;
1720
1721 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1722 }
1723 else
1724 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1725 return 1;
1726}
1727
1728/* Read the CU list from the mapped index, and use it to create all
1729 the CU objects for this objfile. Return 0 if something went wrong,
1730 1 if everything went ok. */
2fdf6df6 1731
9291a0cd 1732static int
1fd400ff
TT
1733create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1734 offset_type cu_list_elements)
9291a0cd
TT
1735{
1736 offset_type i;
9291a0cd
TT
1737
1738 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1739 dwarf2_per_objfile->all_comp_units
1740 = obstack_alloc (&objfile->objfile_obstack,
1741 dwarf2_per_objfile->n_comp_units
1742 * sizeof (struct dwarf2_per_cu_data *));
1743
1744 for (i = 0; i < cu_list_elements; i += 2)
1745 {
1746 struct dwarf2_per_cu_data *the_cu;
1747 ULONGEST offset, length;
1748
1749 if (!extract_cu_value (cu_list, &offset)
1750 || !extract_cu_value (cu_list + 8, &length))
1751 return 0;
1752 cu_list += 2 * 8;
1753
1754 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1755 struct dwarf2_per_cu_data);
1756 the_cu->offset = offset;
1757 the_cu->length = length;
1758 the_cu->objfile = objfile;
1759 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1760 struct dwarf2_per_cu_quick_data);
1761 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1762 }
1763
1764 return 1;
1765}
1766
1fd400ff 1767/* Create the signatured type hash table from the index. */
673bfd45 1768
1fd400ff 1769static int
673bfd45
DE
1770create_signatured_type_table_from_index (struct objfile *objfile,
1771 const gdb_byte *bytes,
1772 offset_type elements)
1fd400ff
TT
1773{
1774 offset_type i;
673bfd45 1775 htab_t sig_types_hash;
1fd400ff
TT
1776
1777 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1778 dwarf2_per_objfile->type_comp_units
1779 = obstack_alloc (&objfile->objfile_obstack,
1780 dwarf2_per_objfile->n_type_comp_units
1781 * sizeof (struct dwarf2_per_cu_data *));
1782
673bfd45 1783 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1784
1785 for (i = 0; i < elements; i += 3)
1786 {
1787 struct signatured_type *type_sig;
1788 ULONGEST offset, type_offset, signature;
1789 void **slot;
1790
1791 if (!extract_cu_value (bytes, &offset)
1792 || !extract_cu_value (bytes + 8, &type_offset))
1793 return 0;
1794 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1795 bytes += 3 * 8;
1796
1797 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1798 struct signatured_type);
1799 type_sig->signature = signature;
1800 type_sig->offset = offset;
1801 type_sig->type_offset = type_offset;
1802 type_sig->per_cu.from_debug_types = 1;
1803 type_sig->per_cu.offset = offset;
1804 type_sig->per_cu.objfile = objfile;
1805 type_sig->per_cu.v.quick
1806 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1807 struct dwarf2_per_cu_quick_data);
1808
673bfd45 1809 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1810 *slot = type_sig;
1811
1812 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1813 }
1814
673bfd45 1815 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1816
1817 return 1;
1818}
1819
9291a0cd
TT
1820/* Read the address map data from the mapped index, and use it to
1821 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1822
9291a0cd
TT
1823static void
1824create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1825{
1826 const gdb_byte *iter, *end;
1827 struct obstack temp_obstack;
1828 struct addrmap *mutable_map;
1829 struct cleanup *cleanup;
1830 CORE_ADDR baseaddr;
1831
1832 obstack_init (&temp_obstack);
1833 cleanup = make_cleanup_obstack_free (&temp_obstack);
1834 mutable_map = addrmap_create_mutable (&temp_obstack);
1835
1836 iter = index->address_table;
1837 end = iter + index->address_table_size;
1838
1839 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1840
1841 while (iter < end)
1842 {
1843 ULONGEST hi, lo, cu_index;
1844 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1845 iter += 8;
1846 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1847 iter += 8;
1848 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1849 iter += 4;
1850
1851 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1852 dw2_get_cu (cu_index));
9291a0cd
TT
1853 }
1854
1855 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1856 &objfile->objfile_obstack);
1857 do_cleanups (cleanup);
1858}
1859
1860/* The hash function for strings in the mapped index. This is the
1861 same as the hashtab.c hash function, but we keep a separate copy to
1862 maintain control over the implementation. This is necessary
1863 because the hash function is tied to the format of the mapped index
1864 file. */
2fdf6df6 1865
9291a0cd
TT
1866static hashval_t
1867mapped_index_string_hash (const void *p)
1868{
1869 const unsigned char *str = (const unsigned char *) p;
1870 hashval_t r = 0;
1871 unsigned char c;
1872
1873 while ((c = *str++) != 0)
1874 r = r * 67 + c - 113;
1875
1876 return r;
1877}
1878
1879/* Find a slot in the mapped index INDEX for the object named NAME.
1880 If NAME is found, set *VEC_OUT to point to the CU vector in the
1881 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1882
9291a0cd
TT
1883static int
1884find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1885 offset_type **vec_out)
1886{
1887 offset_type hash = mapped_index_string_hash (name);
1888 offset_type slot, step;
1889
3876f04e
DE
1890 slot = hash & (index->symbol_table_slots - 1);
1891 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1892
1893 for (;;)
1894 {
1895 /* Convert a slot number to an offset into the table. */
1896 offset_type i = 2 * slot;
1897 const char *str;
3876f04e 1898 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1899 return 0;
1900
3876f04e 1901 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1902 if (!strcmp (name, str))
1903 {
1904 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1905 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1906 return 1;
1907 }
1908
3876f04e 1909 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1910 }
1911}
1912
1913/* Read the index file. If everything went ok, initialize the "quick"
1914 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1915
9291a0cd
TT
1916static int
1917dwarf2_read_index (struct objfile *objfile)
1918{
9291a0cd
TT
1919 char *addr;
1920 struct mapped_index *map;
b3b272e1 1921 offset_type *metadata;
ac0b195c
KW
1922 const gdb_byte *cu_list;
1923 const gdb_byte *types_list = NULL;
1924 offset_type version, cu_list_elements;
1925 offset_type types_list_elements = 0;
1fd400ff 1926 int i;
9291a0cd
TT
1927
1928 if (dwarf2_per_objfile->gdb_index.asection == NULL
1929 || dwarf2_per_objfile->gdb_index.size == 0)
1930 return 0;
82430852
JK
1931
1932 /* Older elfutils strip versions could keep the section in the main
1933 executable while splitting it for the separate debug info file. */
1934 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1935 & SEC_HAS_CONTENTS) == 0)
1936 return 0;
1937
9291a0cd
TT
1938 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1939
1940 addr = dwarf2_per_objfile->gdb_index.buffer;
1941 /* Version check. */
1fd400ff 1942 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
1943 /* Versions earlier than 3 emitted every copy of a psymbol. This
1944 causes the index to behave very poorly for certain requests. So,
1945 it seems better to just ignore such indices. */
1946 if (version < 3)
9291a0cd 1947 return 0;
594e8718
JK
1948 /* Indexes with higher version than the one supported by GDB may be no
1949 longer backward compatible. */
1950 if (version > 3)
1951 return 0;
9291a0cd
TT
1952
1953 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1954 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1955
1956 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1957
1958 i = 0;
1959 cu_list = addr + MAYBE_SWAP (metadata[i]);
1960 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1961 / 8);
1fd400ff
TT
1962 ++i;
1963
987d643c
TT
1964 types_list = addr + MAYBE_SWAP (metadata[i]);
1965 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1966 - MAYBE_SWAP (metadata[i]))
1967 / 8);
1968 ++i;
1fd400ff
TT
1969
1970 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1971 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1972 - MAYBE_SWAP (metadata[i]));
1973 ++i;
1974
3876f04e
DE
1975 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1976 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1977 - MAYBE_SWAP (metadata[i]))
1978 / (2 * sizeof (offset_type)));
1fd400ff 1979 ++i;
9291a0cd 1980
1fd400ff
TT
1981 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1982
1983 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1984 return 0;
1985
987d643c 1986 if (types_list_elements
673bfd45
DE
1987 && !create_signatured_type_table_from_index (objfile, types_list,
1988 types_list_elements))
9291a0cd
TT
1989 return 0;
1990
1991 create_addrmap_from_index (objfile, map);
1992
1993 dwarf2_per_objfile->index_table = map;
1994 dwarf2_per_objfile->using_index = 1;
1995
1996 return 1;
1997}
1998
1999/* A helper for the "quick" functions which sets the global
2000 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2001
9291a0cd
TT
2002static void
2003dw2_setup (struct objfile *objfile)
2004{
2005 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2006 gdb_assert (dwarf2_per_objfile);
2007}
2008
2009/* A helper for the "quick" functions which attempts to read the line
2010 table for THIS_CU. */
2fdf6df6 2011
9291a0cd
TT
2012static void
2013dw2_require_line_header (struct objfile *objfile,
2014 struct dwarf2_per_cu_data *this_cu)
2015{
2016 bfd *abfd = objfile->obfd;
2017 struct line_header *lh = NULL;
2018 struct attribute *attr;
2019 struct cleanup *cleanups;
2020 struct die_info *comp_unit_die;
36374493 2021 struct dwarf2_section_info* sec;
9291a0cd
TT
2022 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2023 int has_children, i;
2024 struct dwarf2_cu cu;
2025 unsigned int bytes_read, buffer_size;
2026 struct die_reader_specs reader_specs;
2027 char *name, *comp_dir;
2028
2029 if (this_cu->v.quick->read_lines)
2030 return;
2031 this_cu->v.quick->read_lines = 1;
2032
9816fde3 2033 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2034 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2035
36374493
DE
2036 if (this_cu->from_debug_types)
2037 sec = &dwarf2_per_objfile->types;
2038 else
2039 sec = &dwarf2_per_objfile->info;
2040 dwarf2_read_section (objfile, sec);
2041 buffer_size = sec->size;
2042 buffer = sec->buffer;
9291a0cd
TT
2043 info_ptr = buffer + this_cu->offset;
2044 beg_of_comp_unit = info_ptr;
2045
2046 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2047 buffer, buffer_size,
2048 abfd);
2049
2050 /* Complete the cu_header. */
2051 cu.header.offset = beg_of_comp_unit - buffer;
2052 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2053
2054 this_cu->cu = &cu;
2055 cu.per_cu = this_cu;
2056
2057 dwarf2_read_abbrevs (abfd, &cu);
2058 make_cleanup (dwarf2_free_abbrev_table, &cu);
2059
2060 if (this_cu->from_debug_types)
2061 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2062 init_cu_die_reader (&reader_specs, &cu);
2063 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2064 &has_children);
2065
2066 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2067 if (attr)
2068 {
2069 unsigned int line_offset = DW_UNSND (attr);
2070 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2071 }
2072 if (lh == NULL)
2073 {
2074 do_cleanups (cleanups);
2075 return;
2076 }
2077
2078 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2079
2080 this_cu->v.quick->lines = lh;
2081
2082 this_cu->v.quick->file_names
2083 = obstack_alloc (&objfile->objfile_obstack,
2084 lh->num_file_names * sizeof (char *));
2085 for (i = 0; i < lh->num_file_names; ++i)
2086 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2087
2088 do_cleanups (cleanups);
2089}
2090
2091/* A helper for the "quick" functions which computes and caches the
2092 real path for a given file name from the line table.
2093 dw2_require_line_header must have been called before this is
2094 invoked. */
2fdf6df6 2095
9291a0cd
TT
2096static const char *
2097dw2_require_full_path (struct objfile *objfile,
e254ef6a 2098 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2099 int index)
2100{
e254ef6a
DE
2101 if (!per_cu->v.quick->full_names)
2102 per_cu->v.quick->full_names
9291a0cd 2103 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2104 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2105 sizeof (char *));
2106
e254ef6a
DE
2107 if (!per_cu->v.quick->full_names[index])
2108 per_cu->v.quick->full_names[index]
2109 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2110
e254ef6a 2111 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2112}
2113
2114static struct symtab *
2115dw2_find_last_source_symtab (struct objfile *objfile)
2116{
2117 int index;
2118 dw2_setup (objfile);
2119 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2120 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2121}
2122
2123static void
2124dw2_forget_cached_source_info (struct objfile *objfile)
2125{
2126 int i;
2127
2128 dw2_setup (objfile);
1fd400ff
TT
2129 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2130 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2131 {
e254ef6a 2132 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2133
e254ef6a 2134 if (per_cu->v.quick->full_names)
9291a0cd
TT
2135 {
2136 int j;
2137
e254ef6a 2138 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
87df528e
JK
2139 {
2140 xfree ((void *) per_cu->v.quick->full_names[j]);
2141 per_cu->v.quick->full_names[j] = NULL;
2142 }
9291a0cd
TT
2143 }
2144 }
2145}
2146
2147static int
2148dw2_lookup_symtab (struct objfile *objfile, const char *name,
2149 const char *full_path, const char *real_path,
2150 struct symtab **result)
2151{
2152 int i;
2153 int check_basename = lbasename (name) == name;
2154 struct dwarf2_per_cu_data *base_cu = NULL;
2155
2156 dw2_setup (objfile);
1fd400ff
TT
2157 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2158 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2159 {
2160 int j;
e254ef6a 2161 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2162
e254ef6a 2163 if (per_cu->v.quick->symtab)
9291a0cd
TT
2164 continue;
2165
e254ef6a
DE
2166 dw2_require_line_header (objfile, per_cu);
2167 if (!per_cu->v.quick->lines)
9291a0cd
TT
2168 continue;
2169
e254ef6a 2170 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2171 {
e254ef6a 2172 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2173
2174 if (FILENAME_CMP (name, this_name) == 0)
2175 {
e254ef6a 2176 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2177 return 1;
2178 }
2179
2180 if (check_basename && ! base_cu
2181 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2182 base_cu = per_cu;
9291a0cd
TT
2183
2184 if (full_path != NULL)
2185 {
2186 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2187 per_cu, j);
9291a0cd
TT
2188
2189 if (this_full_name
2190 && FILENAME_CMP (full_path, this_full_name) == 0)
2191 {
e254ef6a 2192 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2193 return 1;
2194 }
2195 }
2196
2197 if (real_path != NULL)
2198 {
2199 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2200 per_cu, j);
9291a0cd
TT
2201
2202 if (this_full_name != NULL)
2203 {
2204 char *rp = gdb_realpath (this_full_name);
2205 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2206 {
2207 xfree (rp);
e254ef6a 2208 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2209 return 1;
2210 }
2211 xfree (rp);
2212 }
2213 }
2214 }
2215 }
2216
2217 if (base_cu)
2218 {
2219 *result = dw2_instantiate_symtab (objfile, base_cu);
2220 return 1;
2221 }
2222
2223 return 0;
2224}
2225
2226static struct symtab *
2227dw2_lookup_symbol (struct objfile *objfile, int block_index,
2228 const char *name, domain_enum domain)
2229{
774b6a14 2230 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2231 instead. */
2232 return NULL;
2233}
2234
2235/* A helper function that expands all symtabs that hold an object
2236 named NAME. */
2fdf6df6 2237
9291a0cd
TT
2238static void
2239dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2240{
2241 dw2_setup (objfile);
2242
2243 if (dwarf2_per_objfile->index_table)
2244 {
2245 offset_type *vec;
2246
2247 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2248 name, &vec))
2249 {
2250 offset_type i, len = MAYBE_SWAP (*vec);
2251 for (i = 0; i < len; ++i)
2252 {
2253 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2254 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2255
e254ef6a 2256 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2257 }
2258 }
2259 }
2260}
2261
774b6a14
TT
2262static void
2263dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2264 int kind, const char *name,
2265 domain_enum domain)
9291a0cd 2266{
774b6a14 2267 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2268}
2269
2270static void
2271dw2_print_stats (struct objfile *objfile)
2272{
2273 int i, count;
2274
2275 dw2_setup (objfile);
2276 count = 0;
1fd400ff
TT
2277 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2278 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2279 {
e254ef6a 2280 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2281
e254ef6a 2282 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2283 ++count;
2284 }
2285 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2286}
2287
2288static void
2289dw2_dump (struct objfile *objfile)
2290{
2291 /* Nothing worth printing. */
2292}
2293
2294static void
2295dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2296 struct section_offsets *delta)
2297{
2298 /* There's nothing to relocate here. */
2299}
2300
2301static void
2302dw2_expand_symtabs_for_function (struct objfile *objfile,
2303 const char *func_name)
2304{
2305 dw2_do_expand_symtabs_matching (objfile, func_name);
2306}
2307
2308static void
2309dw2_expand_all_symtabs (struct objfile *objfile)
2310{
2311 int i;
2312
2313 dw2_setup (objfile);
1fd400ff
TT
2314
2315 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2316 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2317 {
e254ef6a 2318 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2319
e254ef6a 2320 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2321 }
2322}
2323
2324static void
2325dw2_expand_symtabs_with_filename (struct objfile *objfile,
2326 const char *filename)
2327{
2328 int i;
2329
2330 dw2_setup (objfile);
1fd400ff
TT
2331 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2332 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2333 {
2334 int j;
e254ef6a 2335 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2336
e254ef6a 2337 if (per_cu->v.quick->symtab)
9291a0cd
TT
2338 continue;
2339
e254ef6a
DE
2340 dw2_require_line_header (objfile, per_cu);
2341 if (!per_cu->v.quick->lines)
9291a0cd
TT
2342 continue;
2343
e254ef6a 2344 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2345 {
e254ef6a 2346 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2347 if (strcmp (this_name, filename) == 0)
2348 {
e254ef6a 2349 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2350 break;
2351 }
2352 }
2353 }
2354}
2355
dd786858 2356static const char *
9291a0cd
TT
2357dw2_find_symbol_file (struct objfile *objfile, const char *name)
2358{
e254ef6a 2359 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2360 offset_type *vec;
2361
2362 dw2_setup (objfile);
2363
2364 if (!dwarf2_per_objfile->index_table)
2365 return NULL;
2366
2367 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2368 name, &vec))
2369 return NULL;
2370
2371 /* Note that this just looks at the very first one named NAME -- but
2372 actually we are looking for a function. find_main_filename
2373 should be rewritten so that it doesn't require a custom hook. It
2374 could just use the ordinary symbol tables. */
2375 /* vec[0] is the length, which must always be >0. */
e254ef6a 2376 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2377
e254ef6a
DE
2378 dw2_require_line_header (objfile, per_cu);
2379 if (!per_cu->v.quick->lines)
9291a0cd
TT
2380 return NULL;
2381
e254ef6a 2382 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2383}
2384
2385static void
40658b94
PH
2386dw2_map_matching_symbols (const char * name, domain_enum namespace,
2387 struct objfile *objfile, int global,
2388 int (*callback) (struct block *,
2389 struct symbol *, void *),
2edb89d3
JK
2390 void *data, symbol_compare_ftype *match,
2391 symbol_compare_ftype *ordered_compare)
9291a0cd 2392{
40658b94 2393 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2394 current language is Ada for a non-Ada objfile using GNU index. As Ada
2395 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2396}
2397
2398static void
2399dw2_expand_symtabs_matching (struct objfile *objfile,
2400 int (*file_matcher) (const char *, void *),
2401 int (*name_matcher) (const char *, void *),
2402 domain_enum kind,
2403 void *data)
2404{
2405 int i;
2406 offset_type iter;
4b5246aa 2407 struct mapped_index *index;
9291a0cd
TT
2408
2409 dw2_setup (objfile);
2410 if (!dwarf2_per_objfile->index_table)
2411 return;
4b5246aa 2412 index = dwarf2_per_objfile->index_table;
9291a0cd 2413
1fd400ff
TT
2414 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2415 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2416 {
2417 int j;
e254ef6a 2418 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2419
e254ef6a
DE
2420 per_cu->v.quick->mark = 0;
2421 if (per_cu->v.quick->symtab)
9291a0cd
TT
2422 continue;
2423
e254ef6a
DE
2424 dw2_require_line_header (objfile, per_cu);
2425 if (!per_cu->v.quick->lines)
9291a0cd
TT
2426 continue;
2427
e254ef6a 2428 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2429 {
e254ef6a 2430 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2431 {
e254ef6a 2432 per_cu->v.quick->mark = 1;
9291a0cd
TT
2433 break;
2434 }
2435 }
2436 }
2437
3876f04e 2438 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2439 {
2440 offset_type idx = 2 * iter;
2441 const char *name;
2442 offset_type *vec, vec_len, vec_idx;
2443
3876f04e 2444 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2445 continue;
2446
3876f04e 2447 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2448
2449 if (! (*name_matcher) (name, data))
2450 continue;
2451
2452 /* The name was matched, now expand corresponding CUs that were
2453 marked. */
4b5246aa 2454 vec = (offset_type *) (index->constant_pool
3876f04e 2455 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2456 vec_len = MAYBE_SWAP (vec[0]);
2457 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2458 {
e254ef6a 2459 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2460
e254ef6a
DE
2461 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2462 if (per_cu->v.quick->mark)
2463 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2464 }
2465 }
2466}
2467
2468static struct symtab *
2469dw2_find_pc_sect_symtab (struct objfile *objfile,
2470 struct minimal_symbol *msymbol,
2471 CORE_ADDR pc,
2472 struct obj_section *section,
2473 int warn_if_readin)
2474{
2475 struct dwarf2_per_cu_data *data;
2476
2477 dw2_setup (objfile);
2478
2479 if (!objfile->psymtabs_addrmap)
2480 return NULL;
2481
2482 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2483 if (!data)
2484 return NULL;
2485
2486 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2487 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2488 paddress (get_objfile_arch (objfile), pc));
2489
2490 return dw2_instantiate_symtab (objfile, data);
2491}
2492
2493static void
2494dw2_map_symbol_names (struct objfile *objfile,
2495 void (*fun) (const char *, void *),
2496 void *data)
2497{
2498 offset_type iter;
4b5246aa
TT
2499 struct mapped_index *index;
2500
9291a0cd
TT
2501 dw2_setup (objfile);
2502
2503 if (!dwarf2_per_objfile->index_table)
2504 return;
4b5246aa 2505 index = dwarf2_per_objfile->index_table;
9291a0cd 2506
3876f04e 2507 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2508 {
2509 offset_type idx = 2 * iter;
2510 const char *name;
2511 offset_type *vec, vec_len, vec_idx;
2512
3876f04e 2513 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2514 continue;
2515
3876f04e 2516 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2517
2518 (*fun) (name, data);
2519 }
2520}
2521
2522static void
2523dw2_map_symbol_filenames (struct objfile *objfile,
2524 void (*fun) (const char *, const char *, void *),
2525 void *data)
2526{
2527 int i;
2528
2529 dw2_setup (objfile);
1fd400ff
TT
2530 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2531 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2532 {
2533 int j;
e254ef6a 2534 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2535
e254ef6a 2536 if (per_cu->v.quick->symtab)
9291a0cd
TT
2537 continue;
2538
e254ef6a
DE
2539 dw2_require_line_header (objfile, per_cu);
2540 if (!per_cu->v.quick->lines)
9291a0cd
TT
2541 continue;
2542
e254ef6a 2543 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2544 {
e254ef6a
DE
2545 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2546 j);
2547 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2548 }
2549 }
2550}
2551
2552static int
2553dw2_has_symbols (struct objfile *objfile)
2554{
2555 return 1;
2556}
2557
2558const struct quick_symbol_functions dwarf2_gdb_index_functions =
2559{
2560 dw2_has_symbols,
2561 dw2_find_last_source_symtab,
2562 dw2_forget_cached_source_info,
2563 dw2_lookup_symtab,
2564 dw2_lookup_symbol,
774b6a14 2565 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2566 dw2_print_stats,
2567 dw2_dump,
2568 dw2_relocate,
2569 dw2_expand_symtabs_for_function,
2570 dw2_expand_all_symtabs,
2571 dw2_expand_symtabs_with_filename,
2572 dw2_find_symbol_file,
40658b94 2573 dw2_map_matching_symbols,
9291a0cd
TT
2574 dw2_expand_symtabs_matching,
2575 dw2_find_pc_sect_symtab,
2576 dw2_map_symbol_names,
2577 dw2_map_symbol_filenames
2578};
2579
2580/* Initialize for reading DWARF for this objfile. Return 0 if this
2581 file will use psymtabs, or 1 if using the GNU index. */
2582
2583int
2584dwarf2_initialize_objfile (struct objfile *objfile)
2585{
2586 /* If we're about to read full symbols, don't bother with the
2587 indices. In this case we also don't care if some other debug
2588 format is making psymtabs, because they are all about to be
2589 expanded anyway. */
2590 if ((objfile->flags & OBJF_READNOW))
2591 {
2592 int i;
2593
2594 dwarf2_per_objfile->using_index = 1;
2595 create_all_comp_units (objfile);
1fd400ff 2596 create_debug_types_hash_table (objfile);
9291a0cd 2597
1fd400ff
TT
2598 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2599 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2600 {
e254ef6a 2601 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2602
e254ef6a
DE
2603 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2604 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2605 }
2606
2607 /* Return 1 so that gdb sees the "quick" functions. However,
2608 these functions will be no-ops because we will have expanded
2609 all symtabs. */
2610 return 1;
2611 }
2612
2613 if (dwarf2_read_index (objfile))
2614 return 1;
2615
2616 dwarf2_build_psymtabs (objfile);
2617 return 0;
2618}
2619
2620\f
2621
dce234bc
PP
2622/* Build a partial symbol table. */
2623
2624void
f29dff0a 2625dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2626{
f29dff0a 2627 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2628 {
2629 init_psymbol_list (objfile, 1024);
2630 }
2631
d146bf1e 2632 dwarf2_build_psymtabs_hard (objfile);
c906108c 2633}
c906108c 2634
45452591
DE
2635/* Return TRUE if OFFSET is within CU_HEADER. */
2636
2637static inline int
2638offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2639{
2640 unsigned int bottom = cu_header->offset;
2641 unsigned int top = (cu_header->offset
2642 + cu_header->length
2643 + cu_header->initial_length_size);
9a619af0 2644
45452591
DE
2645 return (offset >= bottom && offset < top);
2646}
2647
93311388
DE
2648/* Read in the comp unit header information from the debug_info at info_ptr.
2649 NOTE: This leaves members offset, first_die_offset to be filled in
2650 by the caller. */
107d2387 2651
fe1b8b76 2652static gdb_byte *
107d2387 2653read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2654 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2655{
2656 int signed_addr;
891d2f0b 2657 unsigned int bytes_read;
c764a876
DE
2658
2659 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2660 cu_header->initial_length_size = bytes_read;
2661 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2662 info_ptr += bytes_read;
107d2387
AC
2663 cu_header->version = read_2_bytes (abfd, info_ptr);
2664 info_ptr += 2;
613e1657 2665 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2666 &bytes_read);
613e1657 2667 info_ptr += bytes_read;
107d2387
AC
2668 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2669 info_ptr += 1;
2670 signed_addr = bfd_get_sign_extend_vma (abfd);
2671 if (signed_addr < 0)
8e65ff28 2672 internal_error (__FILE__, __LINE__,
e2e0b3e5 2673 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2674 cu_header->signed_addr_p = signed_addr;
c764a876 2675
107d2387
AC
2676 return info_ptr;
2677}
2678
fe1b8b76
JB
2679static gdb_byte *
2680partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2681 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2682 bfd *abfd)
2683{
fe1b8b76 2684 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2685
2686 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2687
2dc7f7b3 2688 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2689 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2690 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2691 bfd_get_filename (abfd));
72bf9492 2692
dce234bc 2693 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2694 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2695 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2696 (long) header->abbrev_offset,
93311388 2697 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2698 bfd_get_filename (abfd));
2699
2700 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2701 > buffer + buffer_size)
8a3fe4f8
AC
2702 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2703 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2704 (long) header->length,
93311388 2705 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2706 bfd_get_filename (abfd));
2707
2708 return info_ptr;
2709}
2710
348e048f
DE
2711/* Read in the types comp unit header information from .debug_types entry at
2712 types_ptr. The result is a pointer to one past the end of the header. */
2713
2714static gdb_byte *
2715read_type_comp_unit_head (struct comp_unit_head *cu_header,
2716 ULONGEST *signature,
2717 gdb_byte *types_ptr, bfd *abfd)
2718{
348e048f
DE
2719 gdb_byte *initial_types_ptr = types_ptr;
2720
6e70227d 2721 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2722 &dwarf2_per_objfile->types);
348e048f
DE
2723 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2724
2725 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2726
2727 *signature = read_8_bytes (abfd, types_ptr);
2728 types_ptr += 8;
2729 types_ptr += cu_header->offset_size;
2730 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2731
2732 return types_ptr;
2733}
2734
aaa75496
JB
2735/* Allocate a new partial symtab for file named NAME and mark this new
2736 partial symtab as being an include of PST. */
2737
2738static void
2739dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2740 struct objfile *objfile)
2741{
2742 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2743
2744 subpst->section_offsets = pst->section_offsets;
2745 subpst->textlow = 0;
2746 subpst->texthigh = 0;
2747
2748 subpst->dependencies = (struct partial_symtab **)
2749 obstack_alloc (&objfile->objfile_obstack,
2750 sizeof (struct partial_symtab *));
2751 subpst->dependencies[0] = pst;
2752 subpst->number_of_dependencies = 1;
2753
2754 subpst->globals_offset = 0;
2755 subpst->n_global_syms = 0;
2756 subpst->statics_offset = 0;
2757 subpst->n_static_syms = 0;
2758 subpst->symtab = NULL;
2759 subpst->read_symtab = pst->read_symtab;
2760 subpst->readin = 0;
2761
2762 /* No private part is necessary for include psymtabs. This property
2763 can be used to differentiate between such include psymtabs and
10b3939b 2764 the regular ones. */
58a9656e 2765 subpst->read_symtab_private = NULL;
aaa75496
JB
2766}
2767
2768/* Read the Line Number Program data and extract the list of files
2769 included by the source file represented by PST. Build an include
d85a05f0 2770 partial symtab for each of these included files. */
aaa75496
JB
2771
2772static void
2773dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2774 struct die_info *die,
aaa75496
JB
2775 struct partial_symtab *pst)
2776{
2777 struct objfile *objfile = cu->objfile;
2778 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2779 struct line_header *lh = NULL;
2780 struct attribute *attr;
aaa75496 2781
d85a05f0
DJ
2782 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2783 if (attr)
2784 {
2785 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2786
d85a05f0
DJ
2787 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2788 }
aaa75496
JB
2789 if (lh == NULL)
2790 return; /* No linetable, so no includes. */
2791
c6da4cef
DE
2792 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2793 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2794
2795 free_line_header (lh);
2796}
2797
348e048f
DE
2798static hashval_t
2799hash_type_signature (const void *item)
2800{
2801 const struct signatured_type *type_sig = item;
9a619af0 2802
348e048f
DE
2803 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2804 return type_sig->signature;
2805}
2806
2807static int
2808eq_type_signature (const void *item_lhs, const void *item_rhs)
2809{
2810 const struct signatured_type *lhs = item_lhs;
2811 const struct signatured_type *rhs = item_rhs;
9a619af0 2812
348e048f
DE
2813 return lhs->signature == rhs->signature;
2814}
2815
1fd400ff
TT
2816/* Allocate a hash table for signatured types. */
2817
2818static htab_t
673bfd45 2819allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2820{
2821 return htab_create_alloc_ex (41,
2822 hash_type_signature,
2823 eq_type_signature,
2824 NULL,
2825 &objfile->objfile_obstack,
2826 hashtab_obstack_allocate,
2827 dummy_obstack_deallocate);
2828}
2829
2830/* A helper function to add a signatured type CU to a list. */
2831
2832static int
2833add_signatured_type_cu_to_list (void **slot, void *datum)
2834{
2835 struct signatured_type *sigt = *slot;
2836 struct dwarf2_per_cu_data ***datap = datum;
2837
2838 **datap = &sigt->per_cu;
2839 ++*datap;
2840
2841 return 1;
2842}
2843
348e048f
DE
2844/* Create the hash table of all entries in the .debug_types section.
2845 The result is zero if there is an error (e.g. missing .debug_types section),
2846 otherwise non-zero. */
2847
2848static int
2849create_debug_types_hash_table (struct objfile *objfile)
2850{
be391dca 2851 gdb_byte *info_ptr;
348e048f 2852 htab_t types_htab;
1fd400ff 2853 struct dwarf2_per_cu_data **iter;
348e048f 2854
be391dca
TT
2855 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2856 info_ptr = dwarf2_per_objfile->types.buffer;
2857
348e048f
DE
2858 if (info_ptr == NULL)
2859 {
2860 dwarf2_per_objfile->signatured_types = NULL;
2861 return 0;
2862 }
2863
673bfd45 2864 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2865
2866 if (dwarf2_die_debug)
2867 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2868
2869 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2870 {
2871 unsigned int offset;
2872 unsigned int offset_size;
2873 unsigned int type_offset;
2874 unsigned int length, initial_length_size;
2875 unsigned short version;
2876 ULONGEST signature;
2877 struct signatured_type *type_sig;
2878 void **slot;
2879 gdb_byte *ptr = info_ptr;
2880
2881 offset = ptr - dwarf2_per_objfile->types.buffer;
2882
2883 /* We need to read the type's signature in order to build the hash
2884 table, but we don't need to read anything else just yet. */
2885
2886 /* Sanity check to ensure entire cu is present. */
2887 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2888 if (ptr + length + initial_length_size
2889 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2890 {
2891 complaint (&symfile_complaints,
2892 _("debug type entry runs off end of `.debug_types' section, ignored"));
2893 break;
2894 }
2895
2896 offset_size = initial_length_size == 4 ? 4 : 8;
2897 ptr += initial_length_size;
2898 version = bfd_get_16 (objfile->obfd, ptr);
2899 ptr += 2;
2900 ptr += offset_size; /* abbrev offset */
2901 ptr += 1; /* address size */
2902 signature = bfd_get_64 (objfile->obfd, ptr);
2903 ptr += 8;
2904 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2905
2906 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2907 memset (type_sig, 0, sizeof (*type_sig));
2908 type_sig->signature = signature;
2909 type_sig->offset = offset;
2910 type_sig->type_offset = type_offset;
ca1f3406 2911 type_sig->per_cu.objfile = objfile;
1fd400ff 2912 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2913
2914 slot = htab_find_slot (types_htab, type_sig, INSERT);
2915 gdb_assert (slot != NULL);
2916 *slot = type_sig;
2917
2918 if (dwarf2_die_debug)
2919 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2920 offset, phex (signature, sizeof (signature)));
2921
2922 info_ptr = info_ptr + initial_length_size + length;
2923 }
2924
2925 dwarf2_per_objfile->signatured_types = types_htab;
2926
1fd400ff
TT
2927 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2928 dwarf2_per_objfile->type_comp_units
2929 = obstack_alloc (&objfile->objfile_obstack,
2930 dwarf2_per_objfile->n_type_comp_units
2931 * sizeof (struct dwarf2_per_cu_data *));
2932 iter = &dwarf2_per_objfile->type_comp_units[0];
2933 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2934 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2935 == dwarf2_per_objfile->n_type_comp_units);
2936
348e048f
DE
2937 return 1;
2938}
2939
2940/* Lookup a signature based type.
2941 Returns NULL if SIG is not present in the table. */
2942
2943static struct signatured_type *
2944lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2945{
2946 struct signatured_type find_entry, *entry;
2947
2948 if (dwarf2_per_objfile->signatured_types == NULL)
2949 {
2950 complaint (&symfile_complaints,
2951 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2952 return 0;
2953 }
2954
2955 find_entry.signature = sig;
2956 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2957 return entry;
2958}
2959
d85a05f0
DJ
2960/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2961
2962static void
2963init_cu_die_reader (struct die_reader_specs *reader,
2964 struct dwarf2_cu *cu)
2965{
2966 reader->abfd = cu->objfile->obfd;
2967 reader->cu = cu;
2968 if (cu->per_cu->from_debug_types)
be391dca
TT
2969 {
2970 gdb_assert (dwarf2_per_objfile->types.readin);
2971 reader->buffer = dwarf2_per_objfile->types.buffer;
2972 }
d85a05f0 2973 else
be391dca
TT
2974 {
2975 gdb_assert (dwarf2_per_objfile->info.readin);
2976 reader->buffer = dwarf2_per_objfile->info.buffer;
2977 }
d85a05f0
DJ
2978}
2979
2980/* Find the base address of the compilation unit for range lists and
2981 location lists. It will normally be specified by DW_AT_low_pc.
2982 In DWARF-3 draft 4, the base address could be overridden by
2983 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2984 compilation units with discontinuous ranges. */
2985
2986static void
2987dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2988{
2989 struct attribute *attr;
2990
2991 cu->base_known = 0;
2992 cu->base_address = 0;
2993
2994 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2995 if (attr)
2996 {
2997 cu->base_address = DW_ADDR (attr);
2998 cu->base_known = 1;
2999 }
3000 else
3001 {
3002 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3003 if (attr)
3004 {
3005 cu->base_address = DW_ADDR (attr);
3006 cu->base_known = 1;
3007 }
3008 }
3009}
3010
348e048f
DE
3011/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3012 to combine the common parts.
93311388 3013 Process a compilation unit for a psymtab.
348e048f
DE
3014 BUFFER is a pointer to the beginning of the dwarf section buffer,
3015 either .debug_info or debug_types.
93311388
DE
3016 INFO_PTR is a pointer to the start of the CU.
3017 Returns a pointer to the next CU. */
aaa75496 3018
93311388
DE
3019static gdb_byte *
3020process_psymtab_comp_unit (struct objfile *objfile,
3021 struct dwarf2_per_cu_data *this_cu,
3022 gdb_byte *buffer, gdb_byte *info_ptr,
3023 unsigned int buffer_size)
c906108c 3024{
c906108c 3025 bfd *abfd = objfile->obfd;
93311388 3026 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3027 struct die_info *comp_unit_die;
c906108c 3028 struct partial_symtab *pst;
5734ee8b 3029 CORE_ADDR baseaddr;
93311388
DE
3030 struct cleanup *back_to_inner;
3031 struct dwarf2_cu cu;
d85a05f0
DJ
3032 int has_children, has_pc_info;
3033 struct attribute *attr;
d85a05f0
DJ
3034 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3035 struct die_reader_specs reader_specs;
c906108c 3036
9816fde3 3037 init_one_comp_unit (&cu, objfile);
93311388 3038 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3039
93311388
DE
3040 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3041 buffer, buffer_size,
3042 abfd);
10b3939b 3043
93311388
DE
3044 /* Complete the cu_header. */
3045 cu.header.offset = beg_of_comp_unit - buffer;
3046 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3047
93311388 3048 cu.list_in_scope = &file_symbols;
af703f96 3049
328c9494
DJ
3050 /* If this compilation unit was already read in, free the
3051 cached copy in order to read it in again. This is
3052 necessary because we skipped some symbols when we first
3053 read in the compilation unit (see load_partial_dies).
3054 This problem could be avoided, but the benefit is
3055 unclear. */
3056 if (this_cu->cu != NULL)
3057 free_one_cached_comp_unit (this_cu->cu);
3058
3059 /* Note that this is a pointer to our stack frame, being
3060 added to a global data structure. It will be cleaned up
3061 in free_stack_comp_unit when we finish with this
3062 compilation unit. */
3063 this_cu->cu = &cu;
d85a05f0
DJ
3064 cu.per_cu = this_cu;
3065
93311388
DE
3066 /* Read the abbrevs for this compilation unit into a table. */
3067 dwarf2_read_abbrevs (abfd, &cu);
3068 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3069
93311388 3070 /* Read the compilation unit die. */
348e048f
DE
3071 if (this_cu->from_debug_types)
3072 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3073 init_cu_die_reader (&reader_specs, &cu);
3074 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3075 &has_children);
93311388 3076
348e048f
DE
3077 if (this_cu->from_debug_types)
3078 {
3079 /* offset,length haven't been set yet for type units. */
3080 this_cu->offset = cu.header.offset;
3081 this_cu->length = cu.header.length + cu.header.initial_length_size;
3082 }
d85a05f0 3083 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3084 {
93311388
DE
3085 info_ptr = (beg_of_comp_unit + cu.header.length
3086 + cu.header.initial_length_size);
3087 do_cleanups (back_to_inner);
3088 return info_ptr;
3089 }
72bf9492 3090
9816fde3 3091 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3092
93311388 3093 /* Allocate a new partial symbol table structure. */
d85a05f0 3094 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3095 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3096 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3097 /* TEXTLOW and TEXTHIGH are set below. */
3098 0,
3099 objfile->global_psymbols.next,
3100 objfile->static_psymbols.next);
72bf9492 3101
d85a05f0
DJ
3102 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3103 if (attr != NULL)
3104 pst->dirname = DW_STRING (attr);
72bf9492 3105
e38df1d0 3106 pst->read_symtab_private = this_cu;
72bf9492 3107
93311388 3108 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3109
93311388
DE
3110 /* Store the function that reads in the rest of the symbol table */
3111 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3112
9291a0cd 3113 this_cu->v.psymtab = pst;
c906108c 3114
d85a05f0
DJ
3115 dwarf2_find_base_address (comp_unit_die, &cu);
3116
93311388
DE
3117 /* Possibly set the default values of LOWPC and HIGHPC from
3118 `DW_AT_ranges'. */
d85a05f0
DJ
3119 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3120 &best_highpc, &cu, pst);
3121 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3122 /* Store the contiguous range if it is not empty; it can be empty for
3123 CUs with no code. */
3124 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3125 best_lowpc + baseaddr,
3126 best_highpc + baseaddr - 1, pst);
93311388
DE
3127
3128 /* Check if comp unit has_children.
3129 If so, read the rest of the partial symbols from this comp unit.
3130 If not, there's no more debug_info for this comp unit. */
d85a05f0 3131 if (has_children)
93311388
DE
3132 {
3133 struct partial_die_info *first_die;
3134 CORE_ADDR lowpc, highpc;
31ffec48 3135
93311388
DE
3136 lowpc = ((CORE_ADDR) -1);
3137 highpc = ((CORE_ADDR) 0);
c906108c 3138
93311388 3139 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3140
93311388 3141 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3142 ! has_pc_info, &cu);
57c22c6c 3143
93311388
DE
3144 /* If we didn't find a lowpc, set it to highpc to avoid
3145 complaints from `maint check'. */
3146 if (lowpc == ((CORE_ADDR) -1))
3147 lowpc = highpc;
10b3939b 3148
93311388
DE
3149 /* If the compilation unit didn't have an explicit address range,
3150 then use the information extracted from its child dies. */
d85a05f0 3151 if (! has_pc_info)
93311388 3152 {
d85a05f0
DJ
3153 best_lowpc = lowpc;
3154 best_highpc = highpc;
93311388
DE
3155 }
3156 }
d85a05f0
DJ
3157 pst->textlow = best_lowpc + baseaddr;
3158 pst->texthigh = best_highpc + baseaddr;
c906108c 3159
93311388
DE
3160 pst->n_global_syms = objfile->global_psymbols.next -
3161 (objfile->global_psymbols.list + pst->globals_offset);
3162 pst->n_static_syms = objfile->static_psymbols.next -
3163 (objfile->static_psymbols.list + pst->statics_offset);
3164 sort_pst_symbols (pst);
c906108c 3165
93311388
DE
3166 info_ptr = (beg_of_comp_unit + cu.header.length
3167 + cu.header.initial_length_size);
ae038cb0 3168
348e048f
DE
3169 if (this_cu->from_debug_types)
3170 {
3171 /* It's not clear we want to do anything with stmt lists here.
3172 Waiting to see what gcc ultimately does. */
3173 }
d85a05f0 3174 else
93311388
DE
3175 {
3176 /* Get the list of files included in the current compilation unit,
3177 and build a psymtab for each of them. */
d85a05f0 3178 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3179 }
ae038cb0 3180
93311388 3181 do_cleanups (back_to_inner);
ae038cb0 3182
93311388
DE
3183 return info_ptr;
3184}
ff013f42 3185
348e048f
DE
3186/* Traversal function for htab_traverse_noresize.
3187 Process one .debug_types comp-unit. */
3188
3189static int
3190process_type_comp_unit (void **slot, void *info)
3191{
3192 struct signatured_type *entry = (struct signatured_type *) *slot;
3193 struct objfile *objfile = (struct objfile *) info;
3194 struct dwarf2_per_cu_data *this_cu;
3195
3196 this_cu = &entry->per_cu;
348e048f 3197
be391dca 3198 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3199 process_psymtab_comp_unit (objfile, this_cu,
3200 dwarf2_per_objfile->types.buffer,
3201 dwarf2_per_objfile->types.buffer + entry->offset,
3202 dwarf2_per_objfile->types.size);
3203
3204 return 1;
3205}
3206
3207/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3208 Build partial symbol tables for the .debug_types comp-units. */
3209
3210static void
3211build_type_psymtabs (struct objfile *objfile)
3212{
3213 if (! create_debug_types_hash_table (objfile))
3214 return;
3215
3216 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3217 process_type_comp_unit, objfile);
3218}
3219
60606b2c
TT
3220/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3221
3222static void
3223psymtabs_addrmap_cleanup (void *o)
3224{
3225 struct objfile *objfile = o;
ec61707d 3226
60606b2c
TT
3227 objfile->psymtabs_addrmap = NULL;
3228}
3229
93311388
DE
3230/* Build the partial symbol table by doing a quick pass through the
3231 .debug_info and .debug_abbrev sections. */
72bf9492 3232
93311388 3233static void
c67a9c90 3234dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3235{
93311388 3236 gdb_byte *info_ptr;
60606b2c
TT
3237 struct cleanup *back_to, *addrmap_cleanup;
3238 struct obstack temp_obstack;
93311388 3239
98bfdba5
PA
3240 dwarf2_per_objfile->reading_partial_symbols = 1;
3241
be391dca 3242 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3243 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3244
93311388
DE
3245 /* Any cached compilation units will be linked by the per-objfile
3246 read_in_chain. Make sure to free them when we're done. */
3247 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3248
348e048f
DE
3249 build_type_psymtabs (objfile);
3250
93311388 3251 create_all_comp_units (objfile);
c906108c 3252
60606b2c
TT
3253 /* Create a temporary address map on a temporary obstack. We later
3254 copy this to the final obstack. */
3255 obstack_init (&temp_obstack);
3256 make_cleanup_obstack_free (&temp_obstack);
3257 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3258 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3259
93311388
DE
3260 /* Since the objects we're extracting from .debug_info vary in
3261 length, only the individual functions to extract them (like
3262 read_comp_unit_head and load_partial_die) can really know whether
3263 the buffer is large enough to hold another complete object.
c906108c 3264
93311388
DE
3265 At the moment, they don't actually check that. If .debug_info
3266 holds just one extra byte after the last compilation unit's dies,
3267 then read_comp_unit_head will happily read off the end of the
3268 buffer. read_partial_die is similarly casual. Those functions
3269 should be fixed.
c906108c 3270
93311388
DE
3271 For this loop condition, simply checking whether there's any data
3272 left at all should be sufficient. */
c906108c 3273
93311388
DE
3274 while (info_ptr < (dwarf2_per_objfile->info.buffer
3275 + dwarf2_per_objfile->info.size))
3276 {
3277 struct dwarf2_per_cu_data *this_cu;
dd373385 3278
93311388
DE
3279 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3280 objfile);
aaa75496 3281
93311388
DE
3282 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3283 dwarf2_per_objfile->info.buffer,
3284 info_ptr,
3285 dwarf2_per_objfile->info.size);
c906108c 3286 }
ff013f42
JK
3287
3288 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3289 &objfile->objfile_obstack);
60606b2c 3290 discard_cleanups (addrmap_cleanup);
ff013f42 3291
ae038cb0
DJ
3292 do_cleanups (back_to);
3293}
3294
93311388 3295/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3296
3297static void
93311388
DE
3298load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3299 struct objfile *objfile)
ae038cb0
DJ
3300{
3301 bfd *abfd = objfile->obfd;
fe1b8b76 3302 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3303 struct die_info *comp_unit_die;
ae038cb0 3304 struct dwarf2_cu *cu;
1d9ec526 3305 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3306 int has_children;
3307 struct die_reader_specs reader_specs;
98bfdba5 3308 int read_cu = 0;
ae038cb0 3309
348e048f
DE
3310 gdb_assert (! this_cu->from_debug_types);
3311
be391dca 3312 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3313 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3314 beg_of_comp_unit = info_ptr;
3315
98bfdba5
PA
3316 if (this_cu->cu == NULL)
3317 {
9816fde3
JK
3318 cu = xmalloc (sizeof (*cu));
3319 init_one_comp_unit (cu, objfile);
ae038cb0 3320
98bfdba5 3321 read_cu = 1;
ae038cb0 3322
98bfdba5
PA
3323 /* If an error occurs while loading, release our storage. */
3324 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3325
98bfdba5
PA
3326 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3327 dwarf2_per_objfile->info.buffer,
3328 dwarf2_per_objfile->info.size,
3329 abfd);
ae038cb0 3330
98bfdba5
PA
3331 /* Complete the cu_header. */
3332 cu->header.offset = this_cu->offset;
3333 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3334
3335 /* Link this compilation unit into the compilation unit tree. */
3336 this_cu->cu = cu;
3337 cu->per_cu = this_cu;
98bfdba5
PA
3338
3339 /* Link this CU into read_in_chain. */
3340 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3341 dwarf2_per_objfile->read_in_chain = this_cu;
3342 }
3343 else
3344 {
3345 cu = this_cu->cu;
3346 info_ptr += cu->header.first_die_offset;
3347 }
ae038cb0
DJ
3348
3349 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3350 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3351 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3352 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3353
3354 /* Read the compilation unit die. */
d85a05f0
DJ
3355 init_cu_die_reader (&reader_specs, cu);
3356 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3357 &has_children);
ae038cb0 3358
9816fde3 3359 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3360
ae038cb0
DJ
3361 /* Check if comp unit has_children.
3362 If so, read the rest of the partial symbols from this comp unit.
3363 If not, there's no more debug_info for this comp unit. */
d85a05f0 3364 if (has_children)
93311388 3365 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3366
98bfdba5
PA
3367 do_cleanups (free_abbrevs_cleanup);
3368
3369 if (read_cu)
3370 {
3371 /* We've successfully allocated this compilation unit. Let our
3372 caller clean it up when finished with it. */
3373 discard_cleanups (free_cu_cleanup);
3374 }
ae038cb0
DJ
3375}
3376
3377/* Create a list of all compilation units in OBJFILE. We do this only
3378 if an inter-comp-unit reference is found; presumably if there is one,
3379 there will be many, and one will occur early in the .debug_info section.
3380 So there's no point in building this list incrementally. */
3381
3382static void
3383create_all_comp_units (struct objfile *objfile)
3384{
3385 int n_allocated;
3386 int n_comp_units;
3387 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3388 gdb_byte *info_ptr;
3389
3390 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3391 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3392
3393 n_comp_units = 0;
3394 n_allocated = 10;
3395 all_comp_units = xmalloc (n_allocated
3396 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3397
dce234bc 3398 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3399 {
c764a876 3400 unsigned int length, initial_length_size;
ae038cb0 3401 struct dwarf2_per_cu_data *this_cu;
c764a876 3402 unsigned int offset;
ae038cb0 3403
dce234bc 3404 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3405
3406 /* Read just enough information to find out where the next
3407 compilation unit is. */
c764a876
DE
3408 length = read_initial_length (objfile->obfd, info_ptr,
3409 &initial_length_size);
ae038cb0
DJ
3410
3411 /* Save the compilation unit for later lookup. */
3412 this_cu = obstack_alloc (&objfile->objfile_obstack,
3413 sizeof (struct dwarf2_per_cu_data));
3414 memset (this_cu, 0, sizeof (*this_cu));
3415 this_cu->offset = offset;
c764a876 3416 this_cu->length = length + initial_length_size;
9291a0cd 3417 this_cu->objfile = objfile;
ae038cb0
DJ
3418
3419 if (n_comp_units == n_allocated)
3420 {
3421 n_allocated *= 2;
3422 all_comp_units = xrealloc (all_comp_units,
3423 n_allocated
3424 * sizeof (struct dwarf2_per_cu_data *));
3425 }
3426 all_comp_units[n_comp_units++] = this_cu;
3427
3428 info_ptr = info_ptr + this_cu->length;
3429 }
3430
3431 dwarf2_per_objfile->all_comp_units
3432 = obstack_alloc (&objfile->objfile_obstack,
3433 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3434 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3435 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3436 xfree (all_comp_units);
3437 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3438}
3439
5734ee8b
DJ
3440/* Process all loaded DIEs for compilation unit CU, starting at
3441 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3442 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3443 DW_AT_ranges). If NEED_PC is set, then this function will set
3444 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3445 and record the covered ranges in the addrmap. */
c906108c 3446
72bf9492
DJ
3447static void
3448scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3449 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3450{
72bf9492 3451 struct partial_die_info *pdi;
c906108c 3452
91c24f0a
DC
3453 /* Now, march along the PDI's, descending into ones which have
3454 interesting children but skipping the children of the other ones,
3455 until we reach the end of the compilation unit. */
c906108c 3456
72bf9492 3457 pdi = first_die;
91c24f0a 3458
72bf9492
DJ
3459 while (pdi != NULL)
3460 {
3461 fixup_partial_die (pdi, cu);
c906108c 3462
f55ee35c 3463 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3464 children, so we need to look at them. Ditto for anonymous
3465 enums. */
933c6fe4 3466
72bf9492 3467 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3468 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3469 {
72bf9492 3470 switch (pdi->tag)
c906108c
SS
3471 {
3472 case DW_TAG_subprogram:
5734ee8b 3473 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3474 break;
72929c62 3475 case DW_TAG_constant:
c906108c
SS
3476 case DW_TAG_variable:
3477 case DW_TAG_typedef:
91c24f0a 3478 case DW_TAG_union_type:
72bf9492 3479 if (!pdi->is_declaration)
63d06c5c 3480 {
72bf9492 3481 add_partial_symbol (pdi, cu);
63d06c5c
DC
3482 }
3483 break;
c906108c 3484 case DW_TAG_class_type:
680b30c7 3485 case DW_TAG_interface_type:
c906108c 3486 case DW_TAG_structure_type:
72bf9492 3487 if (!pdi->is_declaration)
c906108c 3488 {
72bf9492 3489 add_partial_symbol (pdi, cu);
c906108c
SS
3490 }
3491 break;
91c24f0a 3492 case DW_TAG_enumeration_type:
72bf9492
DJ
3493 if (!pdi->is_declaration)
3494 add_partial_enumeration (pdi, cu);
c906108c
SS
3495 break;
3496 case DW_TAG_base_type:
a02abb62 3497 case DW_TAG_subrange_type:
c906108c 3498 /* File scope base type definitions are added to the partial
c5aa993b 3499 symbol table. */
72bf9492 3500 add_partial_symbol (pdi, cu);
c906108c 3501 break;
d9fa45fe 3502 case DW_TAG_namespace:
5734ee8b 3503 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3504 break;
5d7cb8df
JK
3505 case DW_TAG_module:
3506 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3507 break;
c906108c
SS
3508 default:
3509 break;
3510 }
3511 }
3512
72bf9492
DJ
3513 /* If the die has a sibling, skip to the sibling. */
3514
3515 pdi = pdi->die_sibling;
3516 }
3517}
3518
3519/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3520
72bf9492 3521 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3522 name is concatenated with "::" and the partial DIE's name. For
3523 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3524 Enumerators are an exception; they use the scope of their parent
3525 enumeration type, i.e. the name of the enumeration type is not
3526 prepended to the enumerator.
91c24f0a 3527
72bf9492
DJ
3528 There are two complexities. One is DW_AT_specification; in this
3529 case "parent" means the parent of the target of the specification,
3530 instead of the direct parent of the DIE. The other is compilers
3531 which do not emit DW_TAG_namespace; in this case we try to guess
3532 the fully qualified name of structure types from their members'
3533 linkage names. This must be done using the DIE's children rather
3534 than the children of any DW_AT_specification target. We only need
3535 to do this for structures at the top level, i.e. if the target of
3536 any DW_AT_specification (if any; otherwise the DIE itself) does not
3537 have a parent. */
3538
3539/* Compute the scope prefix associated with PDI's parent, in
3540 compilation unit CU. The result will be allocated on CU's
3541 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3542 field. NULL is returned if no prefix is necessary. */
3543static char *
3544partial_die_parent_scope (struct partial_die_info *pdi,
3545 struct dwarf2_cu *cu)
3546{
3547 char *grandparent_scope;
3548 struct partial_die_info *parent, *real_pdi;
91c24f0a 3549
72bf9492
DJ
3550 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3551 then this means the parent of the specification DIE. */
3552
3553 real_pdi = pdi;
72bf9492 3554 while (real_pdi->has_specification)
10b3939b 3555 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3556
3557 parent = real_pdi->die_parent;
3558 if (parent == NULL)
3559 return NULL;
3560
3561 if (parent->scope_set)
3562 return parent->scope;
3563
3564 fixup_partial_die (parent, cu);
3565
10b3939b 3566 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3567
acebe513
UW
3568 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3569 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3570 Work around this problem here. */
3571 if (cu->language == language_cplus
6e70227d 3572 && parent->tag == DW_TAG_namespace
acebe513
UW
3573 && strcmp (parent->name, "::") == 0
3574 && grandparent_scope == NULL)
3575 {
3576 parent->scope = NULL;
3577 parent->scope_set = 1;
3578 return NULL;
3579 }
3580
72bf9492 3581 if (parent->tag == DW_TAG_namespace
f55ee35c 3582 || parent->tag == DW_TAG_module
72bf9492
DJ
3583 || parent->tag == DW_TAG_structure_type
3584 || parent->tag == DW_TAG_class_type
680b30c7 3585 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3586 || parent->tag == DW_TAG_union_type
3587 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3588 {
3589 if (grandparent_scope == NULL)
3590 parent->scope = parent->name;
3591 else
987504bb 3592 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3593 parent->name, 0, cu);
72bf9492 3594 }
ceeb3d5a 3595 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3596 /* Enumerators should not get the name of the enumeration as a prefix. */
3597 parent->scope = grandparent_scope;
3598 else
3599 {
3600 /* FIXME drow/2004-04-01: What should we be doing with
3601 function-local names? For partial symbols, we should probably be
3602 ignoring them. */
3603 complaint (&symfile_complaints,
e2e0b3e5 3604 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3605 parent->tag, pdi->offset);
3606 parent->scope = grandparent_scope;
c906108c
SS
3607 }
3608
72bf9492
DJ
3609 parent->scope_set = 1;
3610 return parent->scope;
3611}
3612
3613/* Return the fully scoped name associated with PDI, from compilation unit
3614 CU. The result will be allocated with malloc. */
3615static char *
3616partial_die_full_name (struct partial_die_info *pdi,
3617 struct dwarf2_cu *cu)
3618{
3619 char *parent_scope;
3620
98bfdba5
PA
3621 /* If this is a template instantiation, we can not work out the
3622 template arguments from partial DIEs. So, unfortunately, we have
3623 to go through the full DIEs. At least any work we do building
3624 types here will be reused if full symbols are loaded later. */
3625 if (pdi->has_template_arguments)
3626 {
3627 fixup_partial_die (pdi, cu);
3628
3629 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3630 {
3631 struct die_info *die;
3632 struct attribute attr;
3633 struct dwarf2_cu *ref_cu = cu;
3634
3635 attr.name = 0;
3636 attr.form = DW_FORM_ref_addr;
3637 attr.u.addr = pdi->offset;
3638 die = follow_die_ref (NULL, &attr, &ref_cu);
3639
3640 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3641 }
3642 }
3643
72bf9492
DJ
3644 parent_scope = partial_die_parent_scope (pdi, cu);
3645 if (parent_scope == NULL)
3646 return NULL;
3647 else
f55ee35c 3648 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3649}
3650
3651static void
72bf9492 3652add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3653{
e7c27a73 3654 struct objfile *objfile = cu->objfile;
c906108c 3655 CORE_ADDR addr = 0;
decbce07 3656 char *actual_name = NULL;
5c4e30ca 3657 const struct partial_symbol *psym = NULL;
e142c38c 3658 CORE_ADDR baseaddr;
72bf9492 3659 int built_actual_name = 0;
e142c38c
DJ
3660
3661 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3662
94af9270
KS
3663 actual_name = partial_die_full_name (pdi, cu);
3664 if (actual_name)
3665 built_actual_name = 1;
63d06c5c 3666
72bf9492
DJ
3667 if (actual_name == NULL)
3668 actual_name = pdi->name;
3669
c906108c
SS
3670 switch (pdi->tag)
3671 {
3672 case DW_TAG_subprogram:
2cfa0c8d 3673 if (pdi->is_external || cu->language == language_ada)
c906108c 3674 {
2cfa0c8d
JB
3675 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3676 of the global scope. But in Ada, we want to be able to access
3677 nested procedures globally. So all Ada subprograms are stored
3678 in the global scope. */
38d518c9 3679 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3680 mst_text, objfile); */
38d518c9 3681 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3682 built_actual_name,
5c4e30ca
DC
3683 VAR_DOMAIN, LOC_BLOCK,
3684 &objfile->global_psymbols,
3685 0, pdi->lowpc + baseaddr,
e142c38c 3686 cu->language, objfile);
c906108c
SS
3687 }
3688 else
3689 {
38d518c9 3690 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3691 mst_file_text, objfile); */
38d518c9 3692 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3693 built_actual_name,
5c4e30ca
DC
3694 VAR_DOMAIN, LOC_BLOCK,
3695 &objfile->static_psymbols,
3696 0, pdi->lowpc + baseaddr,
e142c38c 3697 cu->language, objfile);
c906108c
SS
3698 }
3699 break;
72929c62
JB
3700 case DW_TAG_constant:
3701 {
3702 struct psymbol_allocation_list *list;
3703
3704 if (pdi->is_external)
3705 list = &objfile->global_psymbols;
3706 else
3707 list = &objfile->static_psymbols;
3708 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3709 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3710 list, 0, 0, cu->language, objfile);
3711
3712 }
3713 break;
c906108c 3714 case DW_TAG_variable:
caac4577
JG
3715 if (pdi->locdesc)
3716 addr = decode_locdesc (pdi->locdesc, cu);
3717
3718 if (pdi->locdesc
3719 && addr == 0
3720 && !dwarf2_per_objfile->has_section_at_zero)
3721 {
3722 /* A global or static variable may also have been stripped
3723 out by the linker if unused, in which case its address
3724 will be nullified; do not add such variables into partial
3725 symbol table then. */
3726 }
3727 else if (pdi->is_external)
c906108c
SS
3728 {
3729 /* Global Variable.
3730 Don't enter into the minimal symbol tables as there is
3731 a minimal symbol table entry from the ELF symbols already.
3732 Enter into partial symbol table if it has a location
3733 descriptor or a type.
3734 If the location descriptor is missing, new_symbol will create
3735 a LOC_UNRESOLVED symbol, the address of the variable will then
3736 be determined from the minimal symbol table whenever the variable
3737 is referenced.
3738 The address for the partial symbol table entry is not
3739 used by GDB, but it comes in handy for debugging partial symbol
3740 table building. */
3741
c906108c 3742 if (pdi->locdesc || pdi->has_type)
38d518c9 3743 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3744 built_actual_name,
5c4e30ca
DC
3745 VAR_DOMAIN, LOC_STATIC,
3746 &objfile->global_psymbols,
3747 0, addr + baseaddr,
e142c38c 3748 cu->language, objfile);
c906108c
SS
3749 }
3750 else
3751 {
3752 /* Static Variable. Skip symbols without location descriptors. */
3753 if (pdi->locdesc == NULL)
decbce07
MS
3754 {
3755 if (built_actual_name)
3756 xfree (actual_name);
3757 return;
3758 }
38d518c9 3759 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3760 mst_file_data, objfile); */
38d518c9 3761 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3762 built_actual_name,
5c4e30ca
DC
3763 VAR_DOMAIN, LOC_STATIC,
3764 &objfile->static_psymbols,
3765 0, addr + baseaddr,
e142c38c 3766 cu->language, objfile);
c906108c
SS
3767 }
3768 break;
3769 case DW_TAG_typedef:
3770 case DW_TAG_base_type:
a02abb62 3771 case DW_TAG_subrange_type:
38d518c9 3772 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3773 built_actual_name,
176620f1 3774 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3775 &objfile->static_psymbols,
e142c38c 3776 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3777 break;
72bf9492
DJ
3778 case DW_TAG_namespace:
3779 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3780 built_actual_name,
72bf9492
DJ
3781 VAR_DOMAIN, LOC_TYPEDEF,
3782 &objfile->global_psymbols,
3783 0, (CORE_ADDR) 0, cu->language, objfile);
3784 break;
c906108c 3785 case DW_TAG_class_type:
680b30c7 3786 case DW_TAG_interface_type:
c906108c
SS
3787 case DW_TAG_structure_type:
3788 case DW_TAG_union_type:
3789 case DW_TAG_enumeration_type:
fa4028e9
JB
3790 /* Skip external references. The DWARF standard says in the section
3791 about "Structure, Union, and Class Type Entries": "An incomplete
3792 structure, union or class type is represented by a structure,
3793 union or class entry that does not have a byte size attribute
3794 and that has a DW_AT_declaration attribute." */
3795 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3796 {
3797 if (built_actual_name)
3798 xfree (actual_name);
3799 return;
3800 }
fa4028e9 3801
63d06c5c
DC
3802 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3803 static vs. global. */
38d518c9 3804 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3805 built_actual_name,
176620f1 3806 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3807 (cu->language == language_cplus
3808 || cu->language == language_java)
63d06c5c
DC
3809 ? &objfile->global_psymbols
3810 : &objfile->static_psymbols,
e142c38c 3811 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3812
c906108c
SS
3813 break;
3814 case DW_TAG_enumerator:
38d518c9 3815 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3816 built_actual_name,
176620f1 3817 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3818 (cu->language == language_cplus
3819 || cu->language == language_java)
f6fe98ef
DJ
3820 ? &objfile->global_psymbols
3821 : &objfile->static_psymbols,
e142c38c 3822 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3823 break;
3824 default:
3825 break;
3826 }
5c4e30ca 3827
72bf9492
DJ
3828 if (built_actual_name)
3829 xfree (actual_name);
c906108c
SS
3830}
3831
5c4e30ca
DC
3832/* Read a partial die corresponding to a namespace; also, add a symbol
3833 corresponding to that namespace to the symbol table. NAMESPACE is
3834 the name of the enclosing namespace. */
91c24f0a 3835
72bf9492
DJ
3836static void
3837add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3838 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3839 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3840{
72bf9492 3841 /* Add a symbol for the namespace. */
e7c27a73 3842
72bf9492 3843 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3844
3845 /* Now scan partial symbols in that namespace. */
3846
91c24f0a 3847 if (pdi->has_children)
5734ee8b 3848 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3849}
3850
5d7cb8df
JK
3851/* Read a partial die corresponding to a Fortran module. */
3852
3853static void
3854add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3855 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3856{
f55ee35c 3857 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3858
3859 if (pdi->has_children)
3860 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3861}
3862
bc30ff58
JB
3863/* Read a partial die corresponding to a subprogram and create a partial
3864 symbol for that subprogram. When the CU language allows it, this
3865 routine also defines a partial symbol for each nested subprogram
3866 that this subprogram contains.
6e70227d 3867
bc30ff58
JB
3868 DIE my also be a lexical block, in which case we simply search
3869 recursively for suprograms defined inside that lexical block.
3870 Again, this is only performed when the CU language allows this
3871 type of definitions. */
3872
3873static void
3874add_partial_subprogram (struct partial_die_info *pdi,
3875 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3876 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3877{
3878 if (pdi->tag == DW_TAG_subprogram)
3879 {
3880 if (pdi->has_pc_info)
3881 {
3882 if (pdi->lowpc < *lowpc)
3883 *lowpc = pdi->lowpc;
3884 if (pdi->highpc > *highpc)
3885 *highpc = pdi->highpc;
5734ee8b
DJ
3886 if (need_pc)
3887 {
3888 CORE_ADDR baseaddr;
3889 struct objfile *objfile = cu->objfile;
3890
3891 baseaddr = ANOFFSET (objfile->section_offsets,
3892 SECT_OFF_TEXT (objfile));
3893 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3894 pdi->lowpc + baseaddr,
3895 pdi->highpc - 1 + baseaddr,
9291a0cd 3896 cu->per_cu->v.psymtab);
5734ee8b 3897 }
bc30ff58 3898 if (!pdi->is_declaration)
e8d05480
JB
3899 /* Ignore subprogram DIEs that do not have a name, they are
3900 illegal. Do not emit a complaint at this point, we will
3901 do so when we convert this psymtab into a symtab. */
3902 if (pdi->name)
3903 add_partial_symbol (pdi, cu);
bc30ff58
JB
3904 }
3905 }
6e70227d 3906
bc30ff58
JB
3907 if (! pdi->has_children)
3908 return;
3909
3910 if (cu->language == language_ada)
3911 {
3912 pdi = pdi->die_child;
3913 while (pdi != NULL)
3914 {
3915 fixup_partial_die (pdi, cu);
3916 if (pdi->tag == DW_TAG_subprogram
3917 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3918 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3919 pdi = pdi->die_sibling;
3920 }
3921 }
3922}
3923
91c24f0a
DC
3924/* Read a partial die corresponding to an enumeration type. */
3925
72bf9492
DJ
3926static void
3927add_partial_enumeration (struct partial_die_info *enum_pdi,
3928 struct dwarf2_cu *cu)
91c24f0a 3929{
72bf9492 3930 struct partial_die_info *pdi;
91c24f0a
DC
3931
3932 if (enum_pdi->name != NULL)
72bf9492
DJ
3933 add_partial_symbol (enum_pdi, cu);
3934
3935 pdi = enum_pdi->die_child;
3936 while (pdi)
91c24f0a 3937 {
72bf9492 3938 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3939 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3940 else
72bf9492
DJ
3941 add_partial_symbol (pdi, cu);
3942 pdi = pdi->die_sibling;
91c24f0a 3943 }
91c24f0a
DC
3944}
3945
4bb7a0a7
DJ
3946/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3947 Return the corresponding abbrev, or NULL if the number is zero (indicating
3948 an empty DIE). In either case *BYTES_READ will be set to the length of
3949 the initial number. */
3950
3951static struct abbrev_info *
fe1b8b76 3952peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3953 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3954{
3955 bfd *abfd = cu->objfile->obfd;
3956 unsigned int abbrev_number;
3957 struct abbrev_info *abbrev;
3958
3959 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3960
3961 if (abbrev_number == 0)
3962 return NULL;
3963
3964 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3965 if (!abbrev)
3966 {
8a3fe4f8 3967 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3968 bfd_get_filename (abfd));
3969 }
3970
3971 return abbrev;
3972}
3973
93311388
DE
3974/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3975 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3976 DIE. Any children of the skipped DIEs will also be skipped. */
3977
fe1b8b76 3978static gdb_byte *
93311388 3979skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3980{
3981 struct abbrev_info *abbrev;
3982 unsigned int bytes_read;
3983
3984 while (1)
3985 {
3986 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3987 if (abbrev == NULL)
3988 return info_ptr + bytes_read;
3989 else
93311388 3990 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3991 }
3992}
3993
93311388
DE
3994/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3995 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3996 abbrev corresponding to that skipped uleb128 should be passed in
3997 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3998 children. */
3999
fe1b8b76 4000static gdb_byte *
93311388
DE
4001skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4002 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4003{
4004 unsigned int bytes_read;
4005 struct attribute attr;
4006 bfd *abfd = cu->objfile->obfd;
4007 unsigned int form, i;
4008
4009 for (i = 0; i < abbrev->num_attrs; i++)
4010 {
4011 /* The only abbrev we care about is DW_AT_sibling. */
4012 if (abbrev->attrs[i].name == DW_AT_sibling)
4013 {
4014 read_attribute (&attr, &abbrev->attrs[i],
4015 abfd, info_ptr, cu);
4016 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4017 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4018 else
93311388 4019 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4020 }
4021
4022 /* If it isn't DW_AT_sibling, skip this attribute. */
4023 form = abbrev->attrs[i].form;
4024 skip_attribute:
4025 switch (form)
4026 {
4bb7a0a7 4027 case DW_FORM_ref_addr:
ae411497
TT
4028 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4029 and later it is offset sized. */
4030 if (cu->header.version == 2)
4031 info_ptr += cu->header.addr_size;
4032 else
4033 info_ptr += cu->header.offset_size;
4034 break;
4035 case DW_FORM_addr:
4bb7a0a7
DJ
4036 info_ptr += cu->header.addr_size;
4037 break;
4038 case DW_FORM_data1:
4039 case DW_FORM_ref1:
4040 case DW_FORM_flag:
4041 info_ptr += 1;
4042 break;
2dc7f7b3
TT
4043 case DW_FORM_flag_present:
4044 break;
4bb7a0a7
DJ
4045 case DW_FORM_data2:
4046 case DW_FORM_ref2:
4047 info_ptr += 2;
4048 break;
4049 case DW_FORM_data4:
4050 case DW_FORM_ref4:
4051 info_ptr += 4;
4052 break;
4053 case DW_FORM_data8:
4054 case DW_FORM_ref8:
348e048f 4055 case DW_FORM_sig8:
4bb7a0a7
DJ
4056 info_ptr += 8;
4057 break;
4058 case DW_FORM_string:
9b1c24c8 4059 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4060 info_ptr += bytes_read;
4061 break;
2dc7f7b3 4062 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4063 case DW_FORM_strp:
4064 info_ptr += cu->header.offset_size;
4065 break;
2dc7f7b3 4066 case DW_FORM_exprloc:
4bb7a0a7
DJ
4067 case DW_FORM_block:
4068 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4069 info_ptr += bytes_read;
4070 break;
4071 case DW_FORM_block1:
4072 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4073 break;
4074 case DW_FORM_block2:
4075 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4076 break;
4077 case DW_FORM_block4:
4078 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4079 break;
4080 case DW_FORM_sdata:
4081 case DW_FORM_udata:
4082 case DW_FORM_ref_udata:
4083 info_ptr = skip_leb128 (abfd, info_ptr);
4084 break;
4085 case DW_FORM_indirect:
4086 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4087 info_ptr += bytes_read;
4088 /* We need to continue parsing from here, so just go back to
4089 the top. */
4090 goto skip_attribute;
4091
4092 default:
8a3fe4f8 4093 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4094 dwarf_form_name (form),
4095 bfd_get_filename (abfd));
4096 }
4097 }
4098
4099 if (abbrev->has_children)
93311388 4100 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4101 else
4102 return info_ptr;
4103}
4104
93311388
DE
4105/* Locate ORIG_PDI's sibling.
4106 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4107 in BUFFER. */
91c24f0a 4108
fe1b8b76 4109static gdb_byte *
93311388
DE
4110locate_pdi_sibling (struct partial_die_info *orig_pdi,
4111 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4112 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4113{
4114 /* Do we know the sibling already? */
72bf9492 4115
91c24f0a
DC
4116 if (orig_pdi->sibling)
4117 return orig_pdi->sibling;
4118
4119 /* Are there any children to deal with? */
4120
4121 if (!orig_pdi->has_children)
4122 return info_ptr;
4123
4bb7a0a7 4124 /* Skip the children the long way. */
91c24f0a 4125
93311388 4126 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4127}
4128
c906108c
SS
4129/* Expand this partial symbol table into a full symbol table. */
4130
4131static void
fba45db2 4132dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4133{
c906108c
SS
4134 if (pst != NULL)
4135 {
4136 if (pst->readin)
4137 {
8a3fe4f8 4138 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4139 }
4140 else
4141 {
4142 if (info_verbose)
4143 {
a3f17187 4144 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4145 gdb_flush (gdb_stdout);
4146 }
4147
10b3939b
DJ
4148 /* Restore our global data. */
4149 dwarf2_per_objfile = objfile_data (pst->objfile,
4150 dwarf2_objfile_data_key);
4151
b2ab525c
KB
4152 /* If this psymtab is constructed from a debug-only objfile, the
4153 has_section_at_zero flag will not necessarily be correct. We
4154 can get the correct value for this flag by looking at the data
4155 associated with the (presumably stripped) associated objfile. */
4156 if (pst->objfile->separate_debug_objfile_backlink)
4157 {
4158 struct dwarf2_per_objfile *dpo_backlink
4159 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4160 dwarf2_objfile_data_key);
9a619af0 4161
b2ab525c
KB
4162 dwarf2_per_objfile->has_section_at_zero
4163 = dpo_backlink->has_section_at_zero;
4164 }
4165
98bfdba5
PA
4166 dwarf2_per_objfile->reading_partial_symbols = 0;
4167
c906108c
SS
4168 psymtab_to_symtab_1 (pst);
4169
4170 /* Finish up the debug error message. */
4171 if (info_verbose)
a3f17187 4172 printf_filtered (_("done.\n"));
c906108c
SS
4173 }
4174 }
4175}
4176
10b3939b
DJ
4177/* Add PER_CU to the queue. */
4178
4179static void
03dd20cc 4180queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4181{
4182 struct dwarf2_queue_item *item;
4183
4184 per_cu->queued = 1;
4185 item = xmalloc (sizeof (*item));
4186 item->per_cu = per_cu;
4187 item->next = NULL;
4188
4189 if (dwarf2_queue == NULL)
4190 dwarf2_queue = item;
4191 else
4192 dwarf2_queue_tail->next = item;
4193
4194 dwarf2_queue_tail = item;
4195}
4196
4197/* Process the queue. */
4198
4199static void
4200process_queue (struct objfile *objfile)
4201{
4202 struct dwarf2_queue_item *item, *next_item;
4203
03dd20cc
DJ
4204 /* The queue starts out with one item, but following a DIE reference
4205 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4206 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4207 {
9291a0cd
TT
4208 if (dwarf2_per_objfile->using_index
4209 ? !item->per_cu->v.quick->symtab
4210 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4211 process_full_comp_unit (item->per_cu);
4212
4213 item->per_cu->queued = 0;
4214 next_item = item->next;
4215 xfree (item);
4216 }
4217
4218 dwarf2_queue_tail = NULL;
4219}
4220
4221/* Free all allocated queue entries. This function only releases anything if
4222 an error was thrown; if the queue was processed then it would have been
4223 freed as we went along. */
4224
4225static void
4226dwarf2_release_queue (void *dummy)
4227{
4228 struct dwarf2_queue_item *item, *last;
4229
4230 item = dwarf2_queue;
4231 while (item)
4232 {
4233 /* Anything still marked queued is likely to be in an
4234 inconsistent state, so discard it. */
4235 if (item->per_cu->queued)
4236 {
4237 if (item->per_cu->cu != NULL)
4238 free_one_cached_comp_unit (item->per_cu->cu);
4239 item->per_cu->queued = 0;
4240 }
4241
4242 last = item;
4243 item = item->next;
4244 xfree (last);
4245 }
4246
4247 dwarf2_queue = dwarf2_queue_tail = NULL;
4248}
4249
4250/* Read in full symbols for PST, and anything it depends on. */
4251
c906108c 4252static void
fba45db2 4253psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4254{
10b3939b 4255 struct dwarf2_per_cu_data *per_cu;
c906108c 4256 struct cleanup *back_to;
aaa75496
JB
4257 int i;
4258
4259 for (i = 0; i < pst->number_of_dependencies; i++)
4260 if (!pst->dependencies[i]->readin)
4261 {
4262 /* Inform about additional files that need to be read in. */
4263 if (info_verbose)
4264 {
a3f17187 4265 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4266 fputs_filtered (" ", gdb_stdout);
4267 wrap_here ("");
4268 fputs_filtered ("and ", gdb_stdout);
4269 wrap_here ("");
4270 printf_filtered ("%s...", pst->dependencies[i]->filename);
4271 wrap_here (""); /* Flush output */
4272 gdb_flush (gdb_stdout);
4273 }
4274 psymtab_to_symtab_1 (pst->dependencies[i]);
4275 }
4276
e38df1d0 4277 per_cu = pst->read_symtab_private;
10b3939b
DJ
4278
4279 if (per_cu == NULL)
aaa75496
JB
4280 {
4281 /* It's an include file, no symbols to read for it.
4282 Everything is in the parent symtab. */
4283 pst->readin = 1;
4284 return;
4285 }
c906108c 4286
9291a0cd 4287 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4288}
4289
93311388 4290/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4291
93311388 4292static void
31ffec48 4293load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4294{
31ffec48 4295 bfd *abfd = objfile->obfd;
10b3939b 4296 struct dwarf2_cu *cu;
c764a876 4297 unsigned int offset;
93311388 4298 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4299 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4300 struct attribute *attr;
98bfdba5 4301 int read_cu = 0;
6502dd73 4302
348e048f
DE
4303 gdb_assert (! per_cu->from_debug_types);
4304
c906108c 4305 /* Set local variables from the partial symbol table info. */
10b3939b 4306 offset = per_cu->offset;
6502dd73 4307
be391dca 4308 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4309 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4310 beg_of_comp_unit = info_ptr;
63d06c5c 4311
98bfdba5
PA
4312 if (per_cu->cu == NULL)
4313 {
9816fde3
JK
4314 cu = xmalloc (sizeof (*cu));
4315 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4316
4317 read_cu = 1;
c906108c 4318
98bfdba5
PA
4319 /* If an error occurs while loading, release our storage. */
4320 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4321
98bfdba5
PA
4322 /* Read in the comp_unit header. */
4323 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4324
98bfdba5
PA
4325 /* Complete the cu_header. */
4326 cu->header.offset = offset;
4327 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4328
98bfdba5
PA
4329 /* Read the abbrevs for this compilation unit. */
4330 dwarf2_read_abbrevs (abfd, cu);
4331 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4332
98bfdba5
PA
4333 /* Link this compilation unit into the compilation unit tree. */
4334 per_cu->cu = cu;
4335 cu->per_cu = per_cu;
98bfdba5
PA
4336
4337 /* Link this CU into read_in_chain. */
4338 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4339 dwarf2_per_objfile->read_in_chain = per_cu;
4340 }
4341 else
4342 {
4343 cu = per_cu->cu;
4344 info_ptr += cu->header.first_die_offset;
4345 }
e142c38c 4346
93311388 4347 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4348
4349 /* We try not to read any attributes in this function, because not
4350 all objfiles needed for references have been loaded yet, and symbol
4351 table processing isn't initialized. But we have to set the CU language,
4352 or we won't be able to build types correctly. */
9816fde3 4353 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4354
a6c727b2
DJ
4355 /* Similarly, if we do not read the producer, we can not apply
4356 producer-specific interpretation. */
4357 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4358 if (attr)
4359 cu->producer = DW_STRING (attr);
4360
98bfdba5
PA
4361 if (read_cu)
4362 {
4363 do_cleanups (free_abbrevs_cleanup);
e142c38c 4364
98bfdba5
PA
4365 /* We've successfully allocated this compilation unit. Let our
4366 caller clean it up when finished with it. */
4367 discard_cleanups (free_cu_cleanup);
4368 }
10b3939b
DJ
4369}
4370
3da10d80
KS
4371/* Add a DIE to the delayed physname list. */
4372
4373static void
4374add_to_method_list (struct type *type, int fnfield_index, int index,
4375 const char *name, struct die_info *die,
4376 struct dwarf2_cu *cu)
4377{
4378 struct delayed_method_info mi;
4379 mi.type = type;
4380 mi.fnfield_index = fnfield_index;
4381 mi.index = index;
4382 mi.name = name;
4383 mi.die = die;
4384 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4385}
4386
4387/* A cleanup for freeing the delayed method list. */
4388
4389static void
4390free_delayed_list (void *ptr)
4391{
4392 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4393 if (cu->method_list != NULL)
4394 {
4395 VEC_free (delayed_method_info, cu->method_list);
4396 cu->method_list = NULL;
4397 }
4398}
4399
4400/* Compute the physnames of any methods on the CU's method list.
4401
4402 The computation of method physnames is delayed in order to avoid the
4403 (bad) condition that one of the method's formal parameters is of an as yet
4404 incomplete type. */
4405
4406static void
4407compute_delayed_physnames (struct dwarf2_cu *cu)
4408{
4409 int i;
4410 struct delayed_method_info *mi;
4411 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4412 {
4413 char *physname;
4414 struct fn_fieldlist *fn_flp
4415 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4416 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4417 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4418 }
4419}
4420
10b3939b
DJ
4421/* Generate full symbol information for PST and CU, whose DIEs have
4422 already been loaded into memory. */
4423
4424static void
4425process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4426{
10b3939b 4427 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4428 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4429 CORE_ADDR lowpc, highpc;
4430 struct symtab *symtab;
3da10d80 4431 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4432 CORE_ADDR baseaddr;
4433
4434 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4435
10b3939b
DJ
4436 buildsym_init ();
4437 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4438 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4439
4440 cu->list_in_scope = &file_symbols;
c906108c 4441
d85a05f0 4442 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4443
c906108c 4444 /* Do line number decoding in read_file_scope () */
10b3939b 4445 process_die (cu->dies, cu);
c906108c 4446
3da10d80
KS
4447 /* Now that we have processed all the DIEs in the CU, all the types
4448 should be complete, and it should now be safe to compute all of the
4449 physnames. */
4450 compute_delayed_physnames (cu);
4451 do_cleanups (delayed_list_cleanup);
4452
fae299cd
DC
4453 /* Some compilers don't define a DW_AT_high_pc attribute for the
4454 compilation unit. If the DW_AT_high_pc is missing, synthesize
4455 it, by scanning the DIE's below the compilation unit. */
10b3939b 4456 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4457
613e1657 4458 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4459
4460 /* Set symtab language to language from DW_AT_language.
4461 If the compilation is from a C file generated by language preprocessors,
4462 do not set the language if it was already deduced by start_subfile. */
4463 if (symtab != NULL
10b3939b 4464 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4465 {
10b3939b 4466 symtab->language = cu->language;
c906108c 4467 }
9291a0cd
TT
4468
4469 if (dwarf2_per_objfile->using_index)
4470 per_cu->v.quick->symtab = symtab;
4471 else
4472 {
4473 struct partial_symtab *pst = per_cu->v.psymtab;
4474 pst->symtab = symtab;
4475 pst->readin = 1;
4476 }
c906108c
SS
4477
4478 do_cleanups (back_to);
4479}
4480
4481/* Process a die and its children. */
4482
4483static void
e7c27a73 4484process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4485{
4486 switch (die->tag)
4487 {
4488 case DW_TAG_padding:
4489 break;
4490 case DW_TAG_compile_unit:
e7c27a73 4491 read_file_scope (die, cu);
c906108c 4492 break;
348e048f
DE
4493 case DW_TAG_type_unit:
4494 read_type_unit_scope (die, cu);
4495 break;
c906108c 4496 case DW_TAG_subprogram:
c906108c 4497 case DW_TAG_inlined_subroutine:
edb3359d 4498 read_func_scope (die, cu);
c906108c
SS
4499 break;
4500 case DW_TAG_lexical_block:
14898363
L
4501 case DW_TAG_try_block:
4502 case DW_TAG_catch_block:
e7c27a73 4503 read_lexical_block_scope (die, cu);
c906108c
SS
4504 break;
4505 case DW_TAG_class_type:
680b30c7 4506 case DW_TAG_interface_type:
c906108c
SS
4507 case DW_TAG_structure_type:
4508 case DW_TAG_union_type:
134d01f1 4509 process_structure_scope (die, cu);
c906108c
SS
4510 break;
4511 case DW_TAG_enumeration_type:
134d01f1 4512 process_enumeration_scope (die, cu);
c906108c 4513 break;
134d01f1 4514
f792889a
DJ
4515 /* These dies have a type, but processing them does not create
4516 a symbol or recurse to process the children. Therefore we can
4517 read them on-demand through read_type_die. */
c906108c 4518 case DW_TAG_subroutine_type:
72019c9c 4519 case DW_TAG_set_type:
c906108c 4520 case DW_TAG_array_type:
c906108c 4521 case DW_TAG_pointer_type:
c906108c 4522 case DW_TAG_ptr_to_member_type:
c906108c 4523 case DW_TAG_reference_type:
c906108c 4524 case DW_TAG_string_type:
c906108c 4525 break;
134d01f1 4526
c906108c 4527 case DW_TAG_base_type:
a02abb62 4528 case DW_TAG_subrange_type:
cb249c71 4529 case DW_TAG_typedef:
134d01f1
DJ
4530 /* Add a typedef symbol for the type definition, if it has a
4531 DW_AT_name. */
f792889a 4532 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4533 break;
c906108c 4534 case DW_TAG_common_block:
e7c27a73 4535 read_common_block (die, cu);
c906108c
SS
4536 break;
4537 case DW_TAG_common_inclusion:
4538 break;
d9fa45fe 4539 case DW_TAG_namespace:
63d06c5c 4540 processing_has_namespace_info = 1;
e7c27a73 4541 read_namespace (die, cu);
d9fa45fe 4542 break;
5d7cb8df 4543 case DW_TAG_module:
f55ee35c 4544 processing_has_namespace_info = 1;
5d7cb8df
JK
4545 read_module (die, cu);
4546 break;
d9fa45fe
DC
4547 case DW_TAG_imported_declaration:
4548 case DW_TAG_imported_module:
63d06c5c 4549 processing_has_namespace_info = 1;
27aa8d6a
SW
4550 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4551 || cu->language != language_fortran))
4552 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4553 dwarf_tag_name (die->tag));
4554 read_import_statement (die, cu);
d9fa45fe 4555 break;
c906108c 4556 default:
e7c27a73 4557 new_symbol (die, NULL, cu);
c906108c
SS
4558 break;
4559 }
4560}
4561
94af9270
KS
4562/* A helper function for dwarf2_compute_name which determines whether DIE
4563 needs to have the name of the scope prepended to the name listed in the
4564 die. */
4565
4566static int
4567die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4568{
1c809c68
TT
4569 struct attribute *attr;
4570
94af9270
KS
4571 switch (die->tag)
4572 {
4573 case DW_TAG_namespace:
4574 case DW_TAG_typedef:
4575 case DW_TAG_class_type:
4576 case DW_TAG_interface_type:
4577 case DW_TAG_structure_type:
4578 case DW_TAG_union_type:
4579 case DW_TAG_enumeration_type:
4580 case DW_TAG_enumerator:
4581 case DW_TAG_subprogram:
4582 case DW_TAG_member:
4583 return 1;
4584
4585 case DW_TAG_variable:
c2b0a229 4586 case DW_TAG_constant:
94af9270
KS
4587 /* We only need to prefix "globally" visible variables. These include
4588 any variable marked with DW_AT_external or any variable that
4589 lives in a namespace. [Variables in anonymous namespaces
4590 require prefixing, but they are not DW_AT_external.] */
4591
4592 if (dwarf2_attr (die, DW_AT_specification, cu))
4593 {
4594 struct dwarf2_cu *spec_cu = cu;
9a619af0 4595
94af9270
KS
4596 return die_needs_namespace (die_specification (die, &spec_cu),
4597 spec_cu);
4598 }
4599
1c809c68 4600 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4601 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4602 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4603 return 0;
4604 /* A variable in a lexical block of some kind does not need a
4605 namespace, even though in C++ such variables may be external
4606 and have a mangled name. */
4607 if (die->parent->tag == DW_TAG_lexical_block
4608 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4609 || die->parent->tag == DW_TAG_catch_block
4610 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4611 return 0;
4612 return 1;
94af9270
KS
4613
4614 default:
4615 return 0;
4616 }
4617}
4618
98bfdba5
PA
4619/* Retrieve the last character from a mem_file. */
4620
4621static void
4622do_ui_file_peek_last (void *object, const char *buffer, long length)
4623{
4624 char *last_char_p = (char *) object;
4625
4626 if (length > 0)
4627 *last_char_p = buffer[length - 1];
4628}
4629
94af9270
KS
4630/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4631 compute the physname for the object, which include a method's
4632 formal parameters (C++/Java) and return type (Java).
4633
af6b7be1
JB
4634 For Ada, return the DIE's linkage name rather than the fully qualified
4635 name. PHYSNAME is ignored..
4636
94af9270
KS
4637 The result is allocated on the objfile_obstack and canonicalized. */
4638
4639static const char *
4640dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4641 int physname)
4642{
4643 if (name == NULL)
4644 name = dwarf2_name (die, cu);
4645
f55ee35c
JK
4646 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4647 compute it by typename_concat inside GDB. */
4648 if (cu->language == language_ada
4649 || (cu->language == language_fortran && physname))
4650 {
4651 /* For Ada unit, we prefer the linkage name over the name, as
4652 the former contains the exported name, which the user expects
4653 to be able to reference. Ideally, we want the user to be able
4654 to reference this entity using either natural or linkage name,
4655 but we haven't started looking at this enhancement yet. */
4656 struct attribute *attr;
4657
4658 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4659 if (attr == NULL)
4660 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4661 if (attr && DW_STRING (attr))
4662 return DW_STRING (attr);
4663 }
4664
94af9270
KS
4665 /* These are the only languages we know how to qualify names in. */
4666 if (name != NULL
f55ee35c
JK
4667 && (cu->language == language_cplus || cu->language == language_java
4668 || cu->language == language_fortran))
94af9270
KS
4669 {
4670 if (die_needs_namespace (die, cu))
4671 {
4672 long length;
4673 char *prefix;
4674 struct ui_file *buf;
4675
4676 prefix = determine_prefix (die, cu);
4677 buf = mem_fileopen ();
4678 if (*prefix != '\0')
4679 {
f55ee35c
JK
4680 char *prefixed_name = typename_concat (NULL, prefix, name,
4681 physname, cu);
9a619af0 4682
94af9270
KS
4683 fputs_unfiltered (prefixed_name, buf);
4684 xfree (prefixed_name);
4685 }
4686 else
4687 fputs_unfiltered (name ? name : "", buf);
4688
98bfdba5
PA
4689 /* Template parameters may be specified in the DIE's DW_AT_name, or
4690 as children with DW_TAG_template_type_param or
4691 DW_TAG_value_type_param. If the latter, add them to the name
4692 here. If the name already has template parameters, then
4693 skip this step; some versions of GCC emit both, and
4694 it is more efficient to use the pre-computed name.
4695
4696 Something to keep in mind about this process: it is very
4697 unlikely, or in some cases downright impossible, to produce
4698 something that will match the mangled name of a function.
4699 If the definition of the function has the same debug info,
4700 we should be able to match up with it anyway. But fallbacks
4701 using the minimal symbol, for instance to find a method
4702 implemented in a stripped copy of libstdc++, will not work.
4703 If we do not have debug info for the definition, we will have to
4704 match them up some other way.
4705
4706 When we do name matching there is a related problem with function
4707 templates; two instantiated function templates are allowed to
4708 differ only by their return types, which we do not add here. */
4709
4710 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4711 {
4712 struct attribute *attr;
4713 struct die_info *child;
4714 int first = 1;
4715
4716 die->building_fullname = 1;
4717
4718 for (child = die->child; child != NULL; child = child->sibling)
4719 {
4720 struct type *type;
4721 long value;
4722 gdb_byte *bytes;
4723 struct dwarf2_locexpr_baton *baton;
4724 struct value *v;
4725
4726 if (child->tag != DW_TAG_template_type_param
4727 && child->tag != DW_TAG_template_value_param)
4728 continue;
4729
4730 if (first)
4731 {
4732 fputs_unfiltered ("<", buf);
4733 first = 0;
4734 }
4735 else
4736 fputs_unfiltered (", ", buf);
4737
4738 attr = dwarf2_attr (child, DW_AT_type, cu);
4739 if (attr == NULL)
4740 {
4741 complaint (&symfile_complaints,
4742 _("template parameter missing DW_AT_type"));
4743 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4744 continue;
4745 }
4746 type = die_type (child, cu);
4747
4748 if (child->tag == DW_TAG_template_type_param)
4749 {
4750 c_print_type (type, "", buf, -1, 0);
4751 continue;
4752 }
4753
4754 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4755 if (attr == NULL)
4756 {
4757 complaint (&symfile_complaints,
4758 _("template parameter missing DW_AT_const_value"));
4759 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4760 continue;
4761 }
4762
4763 dwarf2_const_value_attr (attr, type, name,
4764 &cu->comp_unit_obstack, cu,
4765 &value, &bytes, &baton);
4766
4767 if (TYPE_NOSIGN (type))
4768 /* GDB prints characters as NUMBER 'CHAR'. If that's
4769 changed, this can use value_print instead. */
4770 c_printchar (value, type, buf);
4771 else
4772 {
4773 struct value_print_options opts;
4774
4775 if (baton != NULL)
4776 v = dwarf2_evaluate_loc_desc (type, NULL,
4777 baton->data,
4778 baton->size,
4779 baton->per_cu);
4780 else if (bytes != NULL)
4781 {
4782 v = allocate_value (type);
4783 memcpy (value_contents_writeable (v), bytes,
4784 TYPE_LENGTH (type));
4785 }
4786 else
4787 v = value_from_longest (type, value);
4788
4789 /* Specify decimal so that we do not depend on the radix. */
4790 get_formatted_print_options (&opts, 'd');
4791 opts.raw = 1;
4792 value_print (v, buf, &opts);
4793 release_value (v);
4794 value_free (v);
4795 }
4796 }
4797
4798 die->building_fullname = 0;
4799
4800 if (!first)
4801 {
4802 /* Close the argument list, with a space if necessary
4803 (nested templates). */
4804 char last_char = '\0';
4805 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4806 if (last_char == '>')
4807 fputs_unfiltered (" >", buf);
4808 else
4809 fputs_unfiltered (">", buf);
4810 }
4811 }
4812
94af9270
KS
4813 /* For Java and C++ methods, append formal parameter type
4814 information, if PHYSNAME. */
6e70227d 4815
94af9270
KS
4816 if (physname && die->tag == DW_TAG_subprogram
4817 && (cu->language == language_cplus
4818 || cu->language == language_java))
4819 {
4820 struct type *type = read_type_die (die, cu);
4821
4822 c_type_print_args (type, buf, 0, cu->language);
4823
4824 if (cu->language == language_java)
4825 {
4826 /* For java, we must append the return type to method
4827 names. */
4828 if (die->tag == DW_TAG_subprogram)
4829 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4830 0, 0);
4831 }
4832 else if (cu->language == language_cplus)
4833 {
60430eff
DJ
4834 /* Assume that an artificial first parameter is
4835 "this", but do not crash if it is not. RealView
4836 marks unnamed (and thus unused) parameters as
4837 artificial; there is no way to differentiate
4838 the two cases. */
94af9270
KS
4839 if (TYPE_NFIELDS (type) > 0
4840 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4841 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4842 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4843 fputs_unfiltered (" const", buf);
4844 }
4845 }
4846
4847 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4848 &length);
4849 ui_file_delete (buf);
4850
4851 if (cu->language == language_cplus)
4852 {
4853 char *cname
4854 = dwarf2_canonicalize_name (name, cu,
4855 &cu->objfile->objfile_obstack);
9a619af0 4856
94af9270
KS
4857 if (cname != NULL)
4858 name = cname;
4859 }
4860 }
4861 }
4862
4863 return name;
4864}
4865
0114d602
DJ
4866/* Return the fully qualified name of DIE, based on its DW_AT_name.
4867 If scope qualifiers are appropriate they will be added. The result
4868 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4869 not have a name. NAME may either be from a previous call to
4870 dwarf2_name or NULL.
4871
4872 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4873
4874static const char *
94af9270 4875dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4876{
94af9270
KS
4877 return dwarf2_compute_name (name, die, cu, 0);
4878}
0114d602 4879
94af9270
KS
4880/* Construct a physname for the given DIE in CU. NAME may either be
4881 from a previous call to dwarf2_name or NULL. The result will be
4882 allocated on the objfile_objstack or NULL if the DIE does not have a
4883 name.
0114d602 4884
94af9270 4885 The output string will be canonicalized (if C++/Java). */
0114d602 4886
94af9270
KS
4887static const char *
4888dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4889{
4890 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4891}
4892
27aa8d6a
SW
4893/* Read the import statement specified by the given die and record it. */
4894
4895static void
4896read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4897{
4898 struct attribute *import_attr;
4899 struct die_info *imported_die;
de4affc9 4900 struct dwarf2_cu *imported_cu;
27aa8d6a 4901 const char *imported_name;
794684b6 4902 const char *imported_name_prefix;
13387711
SW
4903 const char *canonical_name;
4904 const char *import_alias;
4905 const char *imported_declaration = NULL;
794684b6 4906 const char *import_prefix;
13387711
SW
4907
4908 char *temp;
27aa8d6a
SW
4909
4910 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4911 if (import_attr == NULL)
4912 {
4913 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4914 dwarf_tag_name (die->tag));
4915 return;
4916 }
4917
de4affc9
CC
4918 imported_cu = cu;
4919 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4920 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4921 if (imported_name == NULL)
4922 {
4923 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4924
4925 The import in the following code:
4926 namespace A
4927 {
4928 typedef int B;
4929 }
4930
4931 int main ()
4932 {
4933 using A::B;
4934 B b;
4935 return b;
4936 }
4937
4938 ...
4939 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4940 <52> DW_AT_decl_file : 1
4941 <53> DW_AT_decl_line : 6
4942 <54> DW_AT_import : <0x75>
4943 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4944 <59> DW_AT_name : B
4945 <5b> DW_AT_decl_file : 1
4946 <5c> DW_AT_decl_line : 2
4947 <5d> DW_AT_type : <0x6e>
4948 ...
4949 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4950 <76> DW_AT_byte_size : 4
4951 <77> DW_AT_encoding : 5 (signed)
4952
4953 imports the wrong die ( 0x75 instead of 0x58 ).
4954 This case will be ignored until the gcc bug is fixed. */
4955 return;
4956 }
4957
82856980
SW
4958 /* Figure out the local name after import. */
4959 import_alias = dwarf2_name (die, cu);
27aa8d6a 4960
794684b6
SW
4961 /* Figure out where the statement is being imported to. */
4962 import_prefix = determine_prefix (die, cu);
4963
4964 /* Figure out what the scope of the imported die is and prepend it
4965 to the name of the imported die. */
de4affc9 4966 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4967
f55ee35c
JK
4968 if (imported_die->tag != DW_TAG_namespace
4969 && imported_die->tag != DW_TAG_module)
794684b6 4970 {
13387711
SW
4971 imported_declaration = imported_name;
4972 canonical_name = imported_name_prefix;
794684b6 4973 }
13387711 4974 else if (strlen (imported_name_prefix) > 0)
794684b6 4975 {
13387711
SW
4976 temp = alloca (strlen (imported_name_prefix)
4977 + 2 + strlen (imported_name) + 1);
4978 strcpy (temp, imported_name_prefix);
4979 strcat (temp, "::");
4980 strcat (temp, imported_name);
4981 canonical_name = temp;
794684b6 4982 }
13387711
SW
4983 else
4984 canonical_name = imported_name;
794684b6 4985
c0cc3a76
SW
4986 cp_add_using_directive (import_prefix,
4987 canonical_name,
4988 import_alias,
13387711 4989 imported_declaration,
c0cc3a76 4990 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4991}
4992
5fb290d7 4993static void
e142c38c 4994initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4995{
e142c38c 4996 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4997}
4998
cb1df416
DJ
4999static void
5000free_cu_line_header (void *arg)
5001{
5002 struct dwarf2_cu *cu = arg;
5003
5004 free_line_header (cu->line_header);
5005 cu->line_header = NULL;
5006}
5007
9291a0cd
TT
5008static void
5009find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5010 char **name, char **comp_dir)
5011{
5012 struct attribute *attr;
5013
5014 *name = NULL;
5015 *comp_dir = NULL;
5016
5017 /* Find the filename. Do not use dwarf2_name here, since the filename
5018 is not a source language identifier. */
5019 attr = dwarf2_attr (die, DW_AT_name, cu);
5020 if (attr)
5021 {
5022 *name = DW_STRING (attr);
5023 }
5024
5025 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5026 if (attr)
5027 *comp_dir = DW_STRING (attr);
5028 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5029 {
5030 *comp_dir = ldirname (*name);
5031 if (*comp_dir != NULL)
5032 make_cleanup (xfree, *comp_dir);
5033 }
5034 if (*comp_dir != NULL)
5035 {
5036 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5037 directory, get rid of it. */
5038 char *cp = strchr (*comp_dir, ':');
5039
5040 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5041 *comp_dir = cp + 1;
5042 }
5043
5044 if (*name == NULL)
5045 *name = "<unknown>";
5046}
5047
c906108c 5048static void
e7c27a73 5049read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5050{
e7c27a73 5051 struct objfile *objfile = cu->objfile;
debd256d 5052 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5053 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5054 CORE_ADDR highpc = ((CORE_ADDR) 0);
5055 struct attribute *attr;
e1024ff1 5056 char *name = NULL;
c906108c
SS
5057 char *comp_dir = NULL;
5058 struct die_info *child_die;
5059 bfd *abfd = objfile->obfd;
debd256d 5060 struct line_header *line_header = 0;
e142c38c 5061 CORE_ADDR baseaddr;
6e70227d 5062
e142c38c 5063 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5064
fae299cd 5065 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5066
5067 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5068 from finish_block. */
2acceee2 5069 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5070 lowpc = highpc;
5071 lowpc += baseaddr;
5072 highpc += baseaddr;
5073
9291a0cd 5074 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5075
e142c38c 5076 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5077 if (attr)
5078 {
e142c38c 5079 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5080 }
5081
b0f35d58 5082 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5083 if (attr)
b0f35d58 5084 cu->producer = DW_STRING (attr);
303b6f5d 5085
f4b8a18d
KW
5086 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5087 standardised yet. As a workaround for the language detection we fall
5088 back to the DW_AT_producer string. */
5089 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5090 cu->language = language_opencl;
5091
c906108c
SS
5092 /* We assume that we're processing GCC output. */
5093 processing_gcc_compilation = 2;
c906108c 5094
df8a16a1
DJ
5095 processing_has_namespace_info = 0;
5096
c906108c
SS
5097 start_symtab (name, comp_dir, lowpc);
5098 record_debugformat ("DWARF 2");
303b6f5d 5099 record_producer (cu->producer);
c906108c 5100
e142c38c 5101 initialize_cu_func_list (cu);
c906108c 5102
cb1df416
DJ
5103 /* Decode line number information if present. We do this before
5104 processing child DIEs, so that the line header table is available
5105 for DW_AT_decl_file. */
e142c38c 5106 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5107 if (attr)
5108 {
debd256d 5109 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5110 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5111 if (line_header)
5112 {
cb1df416
DJ
5113 cu->line_header = line_header;
5114 make_cleanup (free_cu_line_header, cu);
aaa75496 5115 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5116 }
5fb290d7 5117 }
debd256d 5118
cb1df416
DJ
5119 /* Process all dies in compilation unit. */
5120 if (die->child != NULL)
5121 {
5122 child_die = die->child;
5123 while (child_die && child_die->tag)
5124 {
5125 process_die (child_die, cu);
5126 child_die = sibling_die (child_die);
5127 }
5128 }
5129
2e276125
JB
5130 /* Decode macro information, if present. Dwarf 2 macro information
5131 refers to information in the line number info statement program
5132 header, so we can only read it if we've read the header
5133 successfully. */
e142c38c 5134 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5135 if (attr && line_header)
2e276125
JB
5136 {
5137 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5138
2e276125 5139 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5140 comp_dir, abfd, cu);
2e276125 5141 }
debd256d 5142 do_cleanups (back_to);
5fb290d7
DJ
5143}
5144
348e048f
DE
5145/* For TUs we want to skip the first top level sibling if it's not the
5146 actual type being defined by this TU. In this case the first top
5147 level sibling is there to provide context only. */
5148
5149static void
5150read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5151{
5152 struct objfile *objfile = cu->objfile;
5153 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5154 CORE_ADDR lowpc;
5155 struct attribute *attr;
5156 char *name = NULL;
5157 char *comp_dir = NULL;
5158 struct die_info *child_die;
5159 bfd *abfd = objfile->obfd;
348e048f
DE
5160
5161 /* start_symtab needs a low pc, but we don't really have one.
5162 Do what read_file_scope would do in the absence of such info. */
5163 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5164
5165 /* Find the filename. Do not use dwarf2_name here, since the filename
5166 is not a source language identifier. */
5167 attr = dwarf2_attr (die, DW_AT_name, cu);
5168 if (attr)
5169 name = DW_STRING (attr);
5170
5171 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5172 if (attr)
5173 comp_dir = DW_STRING (attr);
5174 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5175 {
5176 comp_dir = ldirname (name);
5177 if (comp_dir != NULL)
5178 make_cleanup (xfree, comp_dir);
5179 }
5180
5181 if (name == NULL)
5182 name = "<unknown>";
5183
5184 attr = dwarf2_attr (die, DW_AT_language, cu);
5185 if (attr)
5186 set_cu_language (DW_UNSND (attr), cu);
5187
5188 /* This isn't technically needed today. It is done for symmetry
5189 with read_file_scope. */
5190 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5191 if (attr)
348e048f
DE
5192 cu->producer = DW_STRING (attr);
5193
5194 /* We assume that we're processing GCC output. */
5195 processing_gcc_compilation = 2;
5196
5197 processing_has_namespace_info = 0;
5198
5199 start_symtab (name, comp_dir, lowpc);
5200 record_debugformat ("DWARF 2");
5201 record_producer (cu->producer);
5202
5203 /* Process the dies in the type unit. */
5204 if (die->child == NULL)
5205 {
5206 dump_die_for_error (die);
5207 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5208 bfd_get_filename (abfd));
5209 }
5210
5211 child_die = die->child;
5212
5213 while (child_die && child_die->tag)
5214 {
5215 process_die (child_die, cu);
5216
5217 child_die = sibling_die (child_die);
5218 }
5219
5220 do_cleanups (back_to);
5221}
5222
5fb290d7 5223static void
e142c38c
DJ
5224add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5225 struct dwarf2_cu *cu)
5fb290d7
DJ
5226{
5227 struct function_range *thisfn;
5228
5229 thisfn = (struct function_range *)
7b5a2f43 5230 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5231 thisfn->name = name;
5232 thisfn->lowpc = lowpc;
5233 thisfn->highpc = highpc;
5234 thisfn->seen_line = 0;
5235 thisfn->next = NULL;
5236
e142c38c
DJ
5237 if (cu->last_fn == NULL)
5238 cu->first_fn = thisfn;
5fb290d7 5239 else
e142c38c 5240 cu->last_fn->next = thisfn;
5fb290d7 5241
e142c38c 5242 cu->last_fn = thisfn;
c906108c
SS
5243}
5244
d389af10
JK
5245/* qsort helper for inherit_abstract_dies. */
5246
5247static int
5248unsigned_int_compar (const void *ap, const void *bp)
5249{
5250 unsigned int a = *(unsigned int *) ap;
5251 unsigned int b = *(unsigned int *) bp;
5252
5253 return (a > b) - (b > a);
5254}
5255
5256/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5257 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5258 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5259
5260static void
5261inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5262{
5263 struct die_info *child_die;
5264 unsigned die_children_count;
5265 /* CU offsets which were referenced by children of the current DIE. */
5266 unsigned *offsets;
5267 unsigned *offsets_end, *offsetp;
5268 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5269 struct die_info *origin_die;
5270 /* Iterator of the ORIGIN_DIE children. */
5271 struct die_info *origin_child_die;
5272 struct cleanup *cleanups;
5273 struct attribute *attr;
cd02d79d
PA
5274 struct dwarf2_cu *origin_cu;
5275 struct pending **origin_previous_list_in_scope;
d389af10
JK
5276
5277 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5278 if (!attr)
5279 return;
5280
cd02d79d
PA
5281 /* Note that following die references may follow to a die in a
5282 different cu. */
5283
5284 origin_cu = cu;
5285 origin_die = follow_die_ref (die, attr, &origin_cu);
5286
5287 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5288 symbols in. */
5289 origin_previous_list_in_scope = origin_cu->list_in_scope;
5290 origin_cu->list_in_scope = cu->list_in_scope;
5291
edb3359d
DJ
5292 if (die->tag != origin_die->tag
5293 && !(die->tag == DW_TAG_inlined_subroutine
5294 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5295 complaint (&symfile_complaints,
5296 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5297 die->offset, origin_die->offset);
5298
5299 child_die = die->child;
5300 die_children_count = 0;
5301 while (child_die && child_die->tag)
5302 {
5303 child_die = sibling_die (child_die);
5304 die_children_count++;
5305 }
5306 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5307 cleanups = make_cleanup (xfree, offsets);
5308
5309 offsets_end = offsets;
5310 child_die = die->child;
5311 while (child_die && child_die->tag)
5312 {
c38f313d
DJ
5313 /* For each CHILD_DIE, find the corresponding child of
5314 ORIGIN_DIE. If there is more than one layer of
5315 DW_AT_abstract_origin, follow them all; there shouldn't be,
5316 but GCC versions at least through 4.4 generate this (GCC PR
5317 40573). */
5318 struct die_info *child_origin_die = child_die;
cd02d79d 5319 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5320
c38f313d
DJ
5321 while (1)
5322 {
cd02d79d
PA
5323 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5324 child_origin_cu);
c38f313d
DJ
5325 if (attr == NULL)
5326 break;
cd02d79d
PA
5327 child_origin_die = follow_die_ref (child_origin_die, attr,
5328 &child_origin_cu);
c38f313d
DJ
5329 }
5330
d389af10
JK
5331 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5332 counterpart may exist. */
c38f313d 5333 if (child_origin_die != child_die)
d389af10 5334 {
edb3359d
DJ
5335 if (child_die->tag != child_origin_die->tag
5336 && !(child_die->tag == DW_TAG_inlined_subroutine
5337 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5338 complaint (&symfile_complaints,
5339 _("Child DIE 0x%x and its abstract origin 0x%x have "
5340 "different tags"), child_die->offset,
5341 child_origin_die->offset);
c38f313d
DJ
5342 if (child_origin_die->parent != origin_die)
5343 complaint (&symfile_complaints,
5344 _("Child DIE 0x%x and its abstract origin 0x%x have "
5345 "different parents"), child_die->offset,
5346 child_origin_die->offset);
5347 else
5348 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5349 }
5350 child_die = sibling_die (child_die);
5351 }
5352 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5353 unsigned_int_compar);
5354 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5355 if (offsetp[-1] == *offsetp)
5356 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5357 "to DIE 0x%x as their abstract origin"),
5358 die->offset, *offsetp);
5359
5360 offsetp = offsets;
5361 origin_child_die = origin_die->child;
5362 while (origin_child_die && origin_child_die->tag)
5363 {
5364 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5365 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5366 offsetp++;
5367 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5368 {
5369 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5370 process_die (origin_child_die, origin_cu);
d389af10
JK
5371 }
5372 origin_child_die = sibling_die (origin_child_die);
5373 }
cd02d79d 5374 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5375
5376 do_cleanups (cleanups);
5377}
5378
c906108c 5379static void
e7c27a73 5380read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5381{
e7c27a73 5382 struct objfile *objfile = cu->objfile;
52f0bd74 5383 struct context_stack *new;
c906108c
SS
5384 CORE_ADDR lowpc;
5385 CORE_ADDR highpc;
5386 struct die_info *child_die;
edb3359d 5387 struct attribute *attr, *call_line, *call_file;
c906108c 5388 char *name;
e142c38c 5389 CORE_ADDR baseaddr;
801e3a5b 5390 struct block *block;
edb3359d 5391 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5392 VEC (symbolp) *template_args = NULL;
5393 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5394
5395 if (inlined_func)
5396 {
5397 /* If we do not have call site information, we can't show the
5398 caller of this inlined function. That's too confusing, so
5399 only use the scope for local variables. */
5400 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5401 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5402 if (call_line == NULL || call_file == NULL)
5403 {
5404 read_lexical_block_scope (die, cu);
5405 return;
5406 }
5407 }
c906108c 5408
e142c38c
DJ
5409 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5410
94af9270 5411 name = dwarf2_name (die, cu);
c906108c 5412
e8d05480
JB
5413 /* Ignore functions with missing or empty names. These are actually
5414 illegal according to the DWARF standard. */
5415 if (name == NULL)
5416 {
5417 complaint (&symfile_complaints,
5418 _("missing name for subprogram DIE at %d"), die->offset);
5419 return;
5420 }
5421
5422 /* Ignore functions with missing or invalid low and high pc attributes. */
5423 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5424 {
ae4d0c03
PM
5425 attr = dwarf2_attr (die, DW_AT_external, cu);
5426 if (!attr || !DW_UNSND (attr))
5427 complaint (&symfile_complaints,
5428 _("cannot get low and high bounds for subprogram DIE at %d"),
5429 die->offset);
e8d05480
JB
5430 return;
5431 }
c906108c
SS
5432
5433 lowpc += baseaddr;
5434 highpc += baseaddr;
5435
5fb290d7 5436 /* Record the function range for dwarf_decode_lines. */
e142c38c 5437 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5438
34eaf542
TT
5439 /* If we have any template arguments, then we must allocate a
5440 different sort of symbol. */
5441 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5442 {
5443 if (child_die->tag == DW_TAG_template_type_param
5444 || child_die->tag == DW_TAG_template_value_param)
5445 {
5446 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5447 struct template_symbol);
5448 templ_func->base.is_cplus_template_function = 1;
5449 break;
5450 }
5451 }
5452
c906108c 5453 new = push_context (0, lowpc);
34eaf542
TT
5454 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5455 (struct symbol *) templ_func);
4c2df51b 5456
4cecd739
DJ
5457 /* If there is a location expression for DW_AT_frame_base, record
5458 it. */
e142c38c 5459 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5460 if (attr)
c034e007
AC
5461 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5462 expression is being recorded directly in the function's symbol
5463 and not in a separate frame-base object. I guess this hack is
5464 to avoid adding some sort of frame-base adjunct/annex to the
5465 function's symbol :-(. The problem with doing this is that it
5466 results in a function symbol with a location expression that
5467 has nothing to do with the location of the function, ouch! The
5468 relationship should be: a function's symbol has-a frame base; a
5469 frame-base has-a location expression. */
e7c27a73 5470 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5471
e142c38c 5472 cu->list_in_scope = &local_symbols;
c906108c 5473
639d11d3 5474 if (die->child != NULL)
c906108c 5475 {
639d11d3 5476 child_die = die->child;
c906108c
SS
5477 while (child_die && child_die->tag)
5478 {
34eaf542
TT
5479 if (child_die->tag == DW_TAG_template_type_param
5480 || child_die->tag == DW_TAG_template_value_param)
5481 {
5482 struct symbol *arg = new_symbol (child_die, NULL, cu);
5483
f1078f66
DJ
5484 if (arg != NULL)
5485 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5486 }
5487 else
5488 process_die (child_die, cu);
c906108c
SS
5489 child_die = sibling_die (child_die);
5490 }
5491 }
5492
d389af10
JK
5493 inherit_abstract_dies (die, cu);
5494
4a811a97
UW
5495 /* If we have a DW_AT_specification, we might need to import using
5496 directives from the context of the specification DIE. See the
5497 comment in determine_prefix. */
5498 if (cu->language == language_cplus
5499 && dwarf2_attr (die, DW_AT_specification, cu))
5500 {
5501 struct dwarf2_cu *spec_cu = cu;
5502 struct die_info *spec_die = die_specification (die, &spec_cu);
5503
5504 while (spec_die)
5505 {
5506 child_die = spec_die->child;
5507 while (child_die && child_die->tag)
5508 {
5509 if (child_die->tag == DW_TAG_imported_module)
5510 process_die (child_die, spec_cu);
5511 child_die = sibling_die (child_die);
5512 }
5513
5514 /* In some cases, GCC generates specification DIEs that
5515 themselves contain DW_AT_specification attributes. */
5516 spec_die = die_specification (spec_die, &spec_cu);
5517 }
5518 }
5519
c906108c
SS
5520 new = pop_context ();
5521 /* Make a block for the local symbols within. */
801e3a5b
JB
5522 block = finish_block (new->name, &local_symbols, new->old_blocks,
5523 lowpc, highpc, objfile);
5524
df8a16a1 5525 /* For C++, set the block's scope. */
f55ee35c 5526 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5527 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5528 determine_prefix (die, cu),
df8a16a1
DJ
5529 processing_has_namespace_info);
5530
801e3a5b
JB
5531 /* If we have address ranges, record them. */
5532 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5533
34eaf542
TT
5534 /* Attach template arguments to function. */
5535 if (! VEC_empty (symbolp, template_args))
5536 {
5537 gdb_assert (templ_func != NULL);
5538
5539 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5540 templ_func->template_arguments
5541 = obstack_alloc (&objfile->objfile_obstack,
5542 (templ_func->n_template_arguments
5543 * sizeof (struct symbol *)));
5544 memcpy (templ_func->template_arguments,
5545 VEC_address (symbolp, template_args),
5546 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5547 VEC_free (symbolp, template_args);
5548 }
5549
208d8187
JB
5550 /* In C++, we can have functions nested inside functions (e.g., when
5551 a function declares a class that has methods). This means that
5552 when we finish processing a function scope, we may need to go
5553 back to building a containing block's symbol lists. */
5554 local_symbols = new->locals;
5555 param_symbols = new->params;
27aa8d6a 5556 using_directives = new->using_directives;
208d8187 5557
921e78cf
JB
5558 /* If we've finished processing a top-level function, subsequent
5559 symbols go in the file symbol list. */
5560 if (outermost_context_p ())
e142c38c 5561 cu->list_in_scope = &file_symbols;
c906108c
SS
5562}
5563
5564/* Process all the DIES contained within a lexical block scope. Start
5565 a new scope, process the dies, and then close the scope. */
5566
5567static void
e7c27a73 5568read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5569{
e7c27a73 5570 struct objfile *objfile = cu->objfile;
52f0bd74 5571 struct context_stack *new;
c906108c
SS
5572 CORE_ADDR lowpc, highpc;
5573 struct die_info *child_die;
e142c38c
DJ
5574 CORE_ADDR baseaddr;
5575
5576 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5577
5578 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5579 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5580 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5581 be nasty. Might be easier to properly extend generic blocks to
af34e669 5582 describe ranges. */
d85a05f0 5583 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5584 return;
5585 lowpc += baseaddr;
5586 highpc += baseaddr;
5587
5588 push_context (0, lowpc);
639d11d3 5589 if (die->child != NULL)
c906108c 5590 {
639d11d3 5591 child_die = die->child;
c906108c
SS
5592 while (child_die && child_die->tag)
5593 {
e7c27a73 5594 process_die (child_die, cu);
c906108c
SS
5595 child_die = sibling_die (child_die);
5596 }
5597 }
5598 new = pop_context ();
5599
8540c487 5600 if (local_symbols != NULL || using_directives != NULL)
c906108c 5601 {
801e3a5b
JB
5602 struct block *block
5603 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5604 highpc, objfile);
5605
5606 /* Note that recording ranges after traversing children, as we
5607 do here, means that recording a parent's ranges entails
5608 walking across all its children's ranges as they appear in
5609 the address map, which is quadratic behavior.
5610
5611 It would be nicer to record the parent's ranges before
5612 traversing its children, simply overriding whatever you find
5613 there. But since we don't even decide whether to create a
5614 block until after we've traversed its children, that's hard
5615 to do. */
5616 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5617 }
5618 local_symbols = new->locals;
27aa8d6a 5619 using_directives = new->using_directives;
c906108c
SS
5620}
5621
43039443 5622/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5623 Return 1 if the attributes are present and valid, otherwise, return 0.
5624 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5625
5626static int
5627dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5628 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5629 struct partial_symtab *ranges_pst)
43039443
JK
5630{
5631 struct objfile *objfile = cu->objfile;
5632 struct comp_unit_head *cu_header = &cu->header;
5633 bfd *obfd = objfile->obfd;
5634 unsigned int addr_size = cu_header->addr_size;
5635 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5636 /* Base address selection entry. */
5637 CORE_ADDR base;
5638 int found_base;
5639 unsigned int dummy;
5640 gdb_byte *buffer;
5641 CORE_ADDR marker;
5642 int low_set;
5643 CORE_ADDR low = 0;
5644 CORE_ADDR high = 0;
ff013f42 5645 CORE_ADDR baseaddr;
43039443 5646
d00adf39
DE
5647 found_base = cu->base_known;
5648 base = cu->base_address;
43039443 5649
be391dca 5650 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5651 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5652 {
5653 complaint (&symfile_complaints,
5654 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5655 offset);
5656 return 0;
5657 }
dce234bc 5658 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5659
5660 /* Read in the largest possible address. */
5661 marker = read_address (obfd, buffer, cu, &dummy);
5662 if ((marker & mask) == mask)
5663 {
5664 /* If we found the largest possible address, then
5665 read the base address. */
5666 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5667 buffer += 2 * addr_size;
5668 offset += 2 * addr_size;
5669 found_base = 1;
5670 }
5671
5672 low_set = 0;
5673
e7030f15 5674 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5675
43039443
JK
5676 while (1)
5677 {
5678 CORE_ADDR range_beginning, range_end;
5679
5680 range_beginning = read_address (obfd, buffer, cu, &dummy);
5681 buffer += addr_size;
5682 range_end = read_address (obfd, buffer, cu, &dummy);
5683 buffer += addr_size;
5684 offset += 2 * addr_size;
5685
5686 /* An end of list marker is a pair of zero addresses. */
5687 if (range_beginning == 0 && range_end == 0)
5688 /* Found the end of list entry. */
5689 break;
5690
5691 /* Each base address selection entry is a pair of 2 values.
5692 The first is the largest possible address, the second is
5693 the base address. Check for a base address here. */
5694 if ((range_beginning & mask) == mask)
5695 {
5696 /* If we found the largest possible address, then
5697 read the base address. */
5698 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5699 found_base = 1;
5700 continue;
5701 }
5702
5703 if (!found_base)
5704 {
5705 /* We have no valid base address for the ranges
5706 data. */
5707 complaint (&symfile_complaints,
5708 _("Invalid .debug_ranges data (no base address)"));
5709 return 0;
5710 }
5711
5712 range_beginning += base;
5713 range_end += base;
5714
ff013f42
JK
5715 if (ranges_pst != NULL && range_beginning < range_end)
5716 addrmap_set_empty (objfile->psymtabs_addrmap,
5717 range_beginning + baseaddr, range_end - 1 + baseaddr,
5718 ranges_pst);
5719
43039443
JK
5720 /* FIXME: This is recording everything as a low-high
5721 segment of consecutive addresses. We should have a
5722 data structure for discontiguous block ranges
5723 instead. */
5724 if (! low_set)
5725 {
5726 low = range_beginning;
5727 high = range_end;
5728 low_set = 1;
5729 }
5730 else
5731 {
5732 if (range_beginning < low)
5733 low = range_beginning;
5734 if (range_end > high)
5735 high = range_end;
5736 }
5737 }
5738
5739 if (! low_set)
5740 /* If the first entry is an end-of-list marker, the range
5741 describes an empty scope, i.e. no instructions. */
5742 return 0;
5743
5744 if (low_return)
5745 *low_return = low;
5746 if (high_return)
5747 *high_return = high;
5748 return 1;
5749}
5750
af34e669
DJ
5751/* Get low and high pc attributes from a die. Return 1 if the attributes
5752 are present and valid, otherwise, return 0. Return -1 if the range is
5753 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5754static int
af34e669 5755dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5756 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5757 struct partial_symtab *pst)
c906108c
SS
5758{
5759 struct attribute *attr;
af34e669
DJ
5760 CORE_ADDR low = 0;
5761 CORE_ADDR high = 0;
5762 int ret = 0;
c906108c 5763
e142c38c 5764 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5765 if (attr)
af34e669
DJ
5766 {
5767 high = DW_ADDR (attr);
e142c38c 5768 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5769 if (attr)
5770 low = DW_ADDR (attr);
5771 else
5772 /* Found high w/o low attribute. */
5773 return 0;
5774
5775 /* Found consecutive range of addresses. */
5776 ret = 1;
5777 }
c906108c 5778 else
af34e669 5779 {
e142c38c 5780 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5781 if (attr != NULL)
5782 {
af34e669 5783 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5784 .debug_ranges section. */
d85a05f0 5785 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5786 return 0;
43039443 5787 /* Found discontinuous range of addresses. */
af34e669
DJ
5788 ret = -1;
5789 }
5790 }
c906108c
SS
5791
5792 if (high < low)
5793 return 0;
5794
5795 /* When using the GNU linker, .gnu.linkonce. sections are used to
5796 eliminate duplicate copies of functions and vtables and such.
5797 The linker will arbitrarily choose one and discard the others.
5798 The AT_*_pc values for such functions refer to local labels in
5799 these sections. If the section from that file was discarded, the
5800 labels are not in the output, so the relocs get a value of 0.
5801 If this is a discarded function, mark the pc bounds as invalid,
5802 so that GDB will ignore it. */
72dca2f5 5803 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5804 return 0;
5805
5806 *lowpc = low;
5807 *highpc = high;
af34e669 5808 return ret;
c906108c
SS
5809}
5810
b084d499
JB
5811/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5812 its low and high PC addresses. Do nothing if these addresses could not
5813 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5814 and HIGHPC to the high address if greater than HIGHPC. */
5815
5816static void
5817dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5818 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5819 struct dwarf2_cu *cu)
5820{
5821 CORE_ADDR low, high;
5822 struct die_info *child = die->child;
5823
d85a05f0 5824 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5825 {
5826 *lowpc = min (*lowpc, low);
5827 *highpc = max (*highpc, high);
5828 }
5829
5830 /* If the language does not allow nested subprograms (either inside
5831 subprograms or lexical blocks), we're done. */
5832 if (cu->language != language_ada)
5833 return;
6e70227d 5834
b084d499
JB
5835 /* Check all the children of the given DIE. If it contains nested
5836 subprograms, then check their pc bounds. Likewise, we need to
5837 check lexical blocks as well, as they may also contain subprogram
5838 definitions. */
5839 while (child && child->tag)
5840 {
5841 if (child->tag == DW_TAG_subprogram
5842 || child->tag == DW_TAG_lexical_block)
5843 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5844 child = sibling_die (child);
5845 }
5846}
5847
fae299cd
DC
5848/* Get the low and high pc's represented by the scope DIE, and store
5849 them in *LOWPC and *HIGHPC. If the correct values can't be
5850 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5851
5852static void
5853get_scope_pc_bounds (struct die_info *die,
5854 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5855 struct dwarf2_cu *cu)
5856{
5857 CORE_ADDR best_low = (CORE_ADDR) -1;
5858 CORE_ADDR best_high = (CORE_ADDR) 0;
5859 CORE_ADDR current_low, current_high;
5860
d85a05f0 5861 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5862 {
5863 best_low = current_low;
5864 best_high = current_high;
5865 }
5866 else
5867 {
5868 struct die_info *child = die->child;
5869
5870 while (child && child->tag)
5871 {
5872 switch (child->tag) {
5873 case DW_TAG_subprogram:
b084d499 5874 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5875 break;
5876 case DW_TAG_namespace:
f55ee35c 5877 case DW_TAG_module:
fae299cd
DC
5878 /* FIXME: carlton/2004-01-16: Should we do this for
5879 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5880 that current GCC's always emit the DIEs corresponding
5881 to definitions of methods of classes as children of a
5882 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5883 the DIEs giving the declarations, which could be
5884 anywhere). But I don't see any reason why the
5885 standards says that they have to be there. */
5886 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5887
5888 if (current_low != ((CORE_ADDR) -1))
5889 {
5890 best_low = min (best_low, current_low);
5891 best_high = max (best_high, current_high);
5892 }
5893 break;
5894 default:
5895 /* Ignore. */
5896 break;
5897 }
5898
5899 child = sibling_die (child);
5900 }
5901 }
5902
5903 *lowpc = best_low;
5904 *highpc = best_high;
5905}
5906
801e3a5b
JB
5907/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5908 in DIE. */
5909static void
5910dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5911 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5912{
5913 struct attribute *attr;
5914
5915 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5916 if (attr)
5917 {
5918 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5919
801e3a5b
JB
5920 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5921 if (attr)
5922 {
5923 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5924
801e3a5b
JB
5925 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5926 }
5927 }
5928
5929 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5930 if (attr)
5931 {
5932 bfd *obfd = cu->objfile->obfd;
5933
5934 /* The value of the DW_AT_ranges attribute is the offset of the
5935 address range list in the .debug_ranges section. */
5936 unsigned long offset = DW_UNSND (attr);
dce234bc 5937 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5938
5939 /* For some target architectures, but not others, the
5940 read_address function sign-extends the addresses it returns.
5941 To recognize base address selection entries, we need a
5942 mask. */
5943 unsigned int addr_size = cu->header.addr_size;
5944 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5945
5946 /* The base address, to which the next pair is relative. Note
5947 that this 'base' is a DWARF concept: most entries in a range
5948 list are relative, to reduce the number of relocs against the
5949 debugging information. This is separate from this function's
5950 'baseaddr' argument, which GDB uses to relocate debugging
5951 information from a shared library based on the address at
5952 which the library was loaded. */
d00adf39
DE
5953 CORE_ADDR base = cu->base_address;
5954 int base_known = cu->base_known;
801e3a5b 5955
be391dca 5956 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5957 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5958 {
5959 complaint (&symfile_complaints,
5960 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5961 offset);
5962 return;
5963 }
5964
5965 for (;;)
5966 {
5967 unsigned int bytes_read;
5968 CORE_ADDR start, end;
5969
5970 start = read_address (obfd, buffer, cu, &bytes_read);
5971 buffer += bytes_read;
5972 end = read_address (obfd, buffer, cu, &bytes_read);
5973 buffer += bytes_read;
5974
5975 /* Did we find the end of the range list? */
5976 if (start == 0 && end == 0)
5977 break;
5978
5979 /* Did we find a base address selection entry? */
5980 else if ((start & base_select_mask) == base_select_mask)
5981 {
5982 base = end;
5983 base_known = 1;
5984 }
5985
5986 /* We found an ordinary address range. */
5987 else
5988 {
5989 if (!base_known)
5990 {
5991 complaint (&symfile_complaints,
5992 _("Invalid .debug_ranges data (no base address)"));
5993 return;
5994 }
5995
6e70227d
DE
5996 record_block_range (block,
5997 baseaddr + base + start,
801e3a5b
JB
5998 baseaddr + base + end - 1);
5999 }
6000 }
6001 }
6002}
6003
c906108c
SS
6004/* Add an aggregate field to the field list. */
6005
6006static void
107d2387 6007dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6008 struct dwarf2_cu *cu)
6e70227d 6009{
e7c27a73 6010 struct objfile *objfile = cu->objfile;
5e2b427d 6011 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6012 struct nextfield *new_field;
6013 struct attribute *attr;
6014 struct field *fp;
6015 char *fieldname = "";
6016
6017 /* Allocate a new field list entry and link it in. */
6018 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6019 make_cleanup (xfree, new_field);
c906108c 6020 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6021
6022 if (die->tag == DW_TAG_inheritance)
6023 {
6024 new_field->next = fip->baseclasses;
6025 fip->baseclasses = new_field;
6026 }
6027 else
6028 {
6029 new_field->next = fip->fields;
6030 fip->fields = new_field;
6031 }
c906108c
SS
6032 fip->nfields++;
6033
6034 /* Handle accessibility and virtuality of field.
6035 The default accessibility for members is public, the default
6036 accessibility for inheritance is private. */
6037 if (die->tag != DW_TAG_inheritance)
6038 new_field->accessibility = DW_ACCESS_public;
6039 else
6040 new_field->accessibility = DW_ACCESS_private;
6041 new_field->virtuality = DW_VIRTUALITY_none;
6042
e142c38c 6043 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6044 if (attr)
6045 new_field->accessibility = DW_UNSND (attr);
6046 if (new_field->accessibility != DW_ACCESS_public)
6047 fip->non_public_fields = 1;
e142c38c 6048 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6049 if (attr)
6050 new_field->virtuality = DW_UNSND (attr);
6051
6052 fp = &new_field->field;
a9a9bd0f 6053
e142c38c 6054 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6055 {
a9a9bd0f 6056 /* Data member other than a C++ static data member. */
6e70227d 6057
c906108c 6058 /* Get type of field. */
e7c27a73 6059 fp->type = die_type (die, cu);
c906108c 6060
d6a843b5 6061 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6062
c906108c 6063 /* Get bit size of field (zero if none). */
e142c38c 6064 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6065 if (attr)
6066 {
6067 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6068 }
6069 else
6070 {
6071 FIELD_BITSIZE (*fp) = 0;
6072 }
6073
6074 /* Get bit offset of field. */
e142c38c 6075 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6076 if (attr)
6077 {
d4b96c9a 6078 int byte_offset = 0;
c6a0999f 6079
3690dd37 6080 if (attr_form_is_section_offset (attr))
d4b96c9a 6081 dwarf2_complex_location_expr_complaint ();
3690dd37 6082 else if (attr_form_is_constant (attr))
c6a0999f 6083 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6084 else if (attr_form_is_block (attr))
c6a0999f 6085 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6086 else
6087 dwarf2_complex_location_expr_complaint ();
c6a0999f 6088
d6a843b5 6089 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6090 }
e142c38c 6091 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6092 if (attr)
6093 {
5e2b427d 6094 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6095 {
6096 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6097 additional bit offset from the MSB of the containing
6098 anonymous object to the MSB of the field. We don't
6099 have to do anything special since we don't need to
6100 know the size of the anonymous object. */
c906108c
SS
6101 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6102 }
6103 else
6104 {
6105 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6106 MSB of the anonymous object, subtract off the number of
6107 bits from the MSB of the field to the MSB of the
6108 object, and then subtract off the number of bits of
6109 the field itself. The result is the bit offset of
6110 the LSB of the field. */
c906108c
SS
6111 int anonymous_size;
6112 int bit_offset = DW_UNSND (attr);
6113
e142c38c 6114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6115 if (attr)
6116 {
6117 /* The size of the anonymous object containing
6118 the bit field is explicit, so use the
6119 indicated size (in bytes). */
6120 anonymous_size = DW_UNSND (attr);
6121 }
6122 else
6123 {
6124 /* The size of the anonymous object containing
6125 the bit field must be inferred from the type
6126 attribute of the data member containing the
6127 bit field. */
6128 anonymous_size = TYPE_LENGTH (fp->type);
6129 }
6130 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6131 - bit_offset - FIELD_BITSIZE (*fp);
6132 }
6133 }
6134
6135 /* Get name of field. */
39cbfefa
DJ
6136 fieldname = dwarf2_name (die, cu);
6137 if (fieldname == NULL)
6138 fieldname = "";
d8151005
DJ
6139
6140 /* The name is already allocated along with this objfile, so we don't
6141 need to duplicate it for the type. */
6142 fp->name = fieldname;
c906108c
SS
6143
6144 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6145 pointer or virtual base class pointer) to private. */
e142c38c 6146 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6147 {
d48cc9dd 6148 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6149 new_field->accessibility = DW_ACCESS_private;
6150 fip->non_public_fields = 1;
6151 }
6152 }
a9a9bd0f 6153 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6154 {
a9a9bd0f
DC
6155 /* C++ static member. */
6156
6157 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6158 is a declaration, but all versions of G++ as of this writing
6159 (so through at least 3.2.1) incorrectly generate
6160 DW_TAG_variable tags. */
6e70227d 6161
c906108c 6162 char *physname;
c906108c 6163
a9a9bd0f 6164 /* Get name of field. */
39cbfefa
DJ
6165 fieldname = dwarf2_name (die, cu);
6166 if (fieldname == NULL)
c906108c
SS
6167 return;
6168
254e6b9e 6169 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6170 if (attr
6171 /* Only create a symbol if this is an external value.
6172 new_symbol checks this and puts the value in the global symbol
6173 table, which we want. If it is not external, new_symbol
6174 will try to put the value in cu->list_in_scope which is wrong. */
6175 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6176 {
6177 /* A static const member, not much different than an enum as far as
6178 we're concerned, except that we can support more types. */
6179 new_symbol (die, NULL, cu);
6180 }
6181
2df3850c 6182 /* Get physical name. */
94af9270 6183 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6184
d8151005
DJ
6185 /* The name is already allocated along with this objfile, so we don't
6186 need to duplicate it for the type. */
6187 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6188 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6189 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6190 }
6191 else if (die->tag == DW_TAG_inheritance)
6192 {
6193 /* C++ base class field. */
e142c38c 6194 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6195 if (attr)
d4b96c9a
JK
6196 {
6197 int byte_offset = 0;
6198
6199 if (attr_form_is_section_offset (attr))
6200 dwarf2_complex_location_expr_complaint ();
6201 else if (attr_form_is_constant (attr))
6202 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6203 else if (attr_form_is_block (attr))
6204 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6205 else
6206 dwarf2_complex_location_expr_complaint ();
6207
6208 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6209 }
c906108c 6210 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6211 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6212 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6213 fip->nbaseclasses++;
6214 }
6215}
6216
98751a41
JK
6217/* Add a typedef defined in the scope of the FIP's class. */
6218
6219static void
6220dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6221 struct dwarf2_cu *cu)
6e70227d 6222{
98751a41
JK
6223 struct objfile *objfile = cu->objfile;
6224 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6225 struct typedef_field_list *new_field;
6226 struct attribute *attr;
6227 struct typedef_field *fp;
6228 char *fieldname = "";
6229
6230 /* Allocate a new field list entry and link it in. */
6231 new_field = xzalloc (sizeof (*new_field));
6232 make_cleanup (xfree, new_field);
6233
6234 gdb_assert (die->tag == DW_TAG_typedef);
6235
6236 fp = &new_field->field;
6237
6238 /* Get name of field. */
6239 fp->name = dwarf2_name (die, cu);
6240 if (fp->name == NULL)
6241 return;
6242
6243 fp->type = read_type_die (die, cu);
6244
6245 new_field->next = fip->typedef_field_list;
6246 fip->typedef_field_list = new_field;
6247 fip->typedef_field_list_count++;
6248}
6249
c906108c
SS
6250/* Create the vector of fields, and attach it to the type. */
6251
6252static void
fba45db2 6253dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6254 struct dwarf2_cu *cu)
c906108c
SS
6255{
6256 int nfields = fip->nfields;
6257
6258 /* Record the field count, allocate space for the array of fields,
6259 and create blank accessibility bitfields if necessary. */
6260 TYPE_NFIELDS (type) = nfields;
6261 TYPE_FIELDS (type) = (struct field *)
6262 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6263 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6264
b4ba55a1 6265 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6266 {
6267 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6268
6269 TYPE_FIELD_PRIVATE_BITS (type) =
6270 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6271 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6272
6273 TYPE_FIELD_PROTECTED_BITS (type) =
6274 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6275 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6276
774b6a14
TT
6277 TYPE_FIELD_IGNORE_BITS (type) =
6278 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6279 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6280 }
6281
6282 /* If the type has baseclasses, allocate and clear a bit vector for
6283 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6284 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6285 {
6286 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6287 unsigned char *pointer;
c906108c
SS
6288
6289 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6290 pointer = TYPE_ALLOC (type, num_bytes);
6291 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6292 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6293 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6294 }
6295
6296 /* Copy the saved-up fields into the field vector. Start from the head
6297 of the list, adding to the tail of the field array, so that they end
6298 up in the same order in the array in which they were added to the list. */
6299 while (nfields-- > 0)
6300 {
7d0ccb61
DJ
6301 struct nextfield *fieldp;
6302
6303 if (fip->fields)
6304 {
6305 fieldp = fip->fields;
6306 fip->fields = fieldp->next;
6307 }
6308 else
6309 {
6310 fieldp = fip->baseclasses;
6311 fip->baseclasses = fieldp->next;
6312 }
6313
6314 TYPE_FIELD (type, nfields) = fieldp->field;
6315 switch (fieldp->accessibility)
c906108c 6316 {
c5aa993b 6317 case DW_ACCESS_private:
b4ba55a1
JB
6318 if (cu->language != language_ada)
6319 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6320 break;
c906108c 6321
c5aa993b 6322 case DW_ACCESS_protected:
b4ba55a1
JB
6323 if (cu->language != language_ada)
6324 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6325 break;
c906108c 6326
c5aa993b
JM
6327 case DW_ACCESS_public:
6328 break;
c906108c 6329
c5aa993b
JM
6330 default:
6331 /* Unknown accessibility. Complain and treat it as public. */
6332 {
e2e0b3e5 6333 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6334 fieldp->accessibility);
c5aa993b
JM
6335 }
6336 break;
c906108c
SS
6337 }
6338 if (nfields < fip->nbaseclasses)
6339 {
7d0ccb61 6340 switch (fieldp->virtuality)
c906108c 6341 {
c5aa993b
JM
6342 case DW_VIRTUALITY_virtual:
6343 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6344 if (cu->language == language_ada)
6345 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6346 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6347 break;
c906108c
SS
6348 }
6349 }
c906108c
SS
6350 }
6351}
6352
c906108c
SS
6353/* Add a member function to the proper fieldlist. */
6354
6355static void
107d2387 6356dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6357 struct type *type, struct dwarf2_cu *cu)
c906108c 6358{
e7c27a73 6359 struct objfile *objfile = cu->objfile;
c906108c
SS
6360 struct attribute *attr;
6361 struct fnfieldlist *flp;
6362 int i;
6363 struct fn_field *fnp;
6364 char *fieldname;
c906108c 6365 struct nextfnfield *new_fnfield;
f792889a 6366 struct type *this_type;
c906108c 6367
b4ba55a1
JB
6368 if (cu->language == language_ada)
6369 error ("unexpected member function in Ada type");
6370
2df3850c 6371 /* Get name of member function. */
39cbfefa
DJ
6372 fieldname = dwarf2_name (die, cu);
6373 if (fieldname == NULL)
2df3850c 6374 return;
c906108c 6375
c906108c
SS
6376 /* Look up member function name in fieldlist. */
6377 for (i = 0; i < fip->nfnfields; i++)
6378 {
27bfe10e 6379 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6380 break;
6381 }
6382
6383 /* Create new list element if necessary. */
6384 if (i < fip->nfnfields)
6385 flp = &fip->fnfieldlists[i];
6386 else
6387 {
6388 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6389 {
6390 fip->fnfieldlists = (struct fnfieldlist *)
6391 xrealloc (fip->fnfieldlists,
6392 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6393 * sizeof (struct fnfieldlist));
c906108c 6394 if (fip->nfnfields == 0)
c13c43fd 6395 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6396 }
6397 flp = &fip->fnfieldlists[fip->nfnfields];
6398 flp->name = fieldname;
6399 flp->length = 0;
6400 flp->head = NULL;
3da10d80 6401 i = fip->nfnfields++;
c906108c
SS
6402 }
6403
6404 /* Create a new member function field and chain it to the field list
6405 entry. */
6406 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6407 make_cleanup (xfree, new_fnfield);
c906108c
SS
6408 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6409 new_fnfield->next = flp->head;
6410 flp->head = new_fnfield;
6411 flp->length++;
6412
6413 /* Fill in the member function field info. */
6414 fnp = &new_fnfield->fnfield;
3da10d80
KS
6415
6416 /* Delay processing of the physname until later. */
6417 if (cu->language == language_cplus || cu->language == language_java)
6418 {
6419 add_to_method_list (type, i, flp->length - 1, fieldname,
6420 die, cu);
6421 }
6422 else
6423 {
6424 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6425 fnp->physname = physname ? physname : "";
6426 }
6427
c906108c 6428 fnp->type = alloc_type (objfile);
f792889a
DJ
6429 this_type = read_type_die (die, cu);
6430 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6431 {
f792889a 6432 int nparams = TYPE_NFIELDS (this_type);
c906108c 6433
f792889a 6434 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6435 of the method itself (TYPE_CODE_METHOD). */
6436 smash_to_method_type (fnp->type, type,
f792889a
DJ
6437 TYPE_TARGET_TYPE (this_type),
6438 TYPE_FIELDS (this_type),
6439 TYPE_NFIELDS (this_type),
6440 TYPE_VARARGS (this_type));
c906108c
SS
6441
6442 /* Handle static member functions.
c5aa993b
JM
6443 Dwarf2 has no clean way to discern C++ static and non-static
6444 member functions. G++ helps GDB by marking the first
6445 parameter for non-static member functions (which is the
6446 this pointer) as artificial. We obtain this information
6447 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6448 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6449 fnp->voffset = VOFFSET_STATIC;
6450 }
6451 else
e2e0b3e5 6452 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6453 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6454
6455 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6456 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6457 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6458
6459 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6460 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6461
6462 /* Get accessibility. */
e142c38c 6463 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6464 if (attr)
6465 {
6466 switch (DW_UNSND (attr))
6467 {
c5aa993b
JM
6468 case DW_ACCESS_private:
6469 fnp->is_private = 1;
6470 break;
6471 case DW_ACCESS_protected:
6472 fnp->is_protected = 1;
6473 break;
c906108c
SS
6474 }
6475 }
6476
b02dede2 6477 /* Check for artificial methods. */
e142c38c 6478 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6479 if (attr && DW_UNSND (attr) != 0)
6480 fnp->is_artificial = 1;
6481
0d564a31 6482 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6483 function. For older versions of GCC, this is an offset in the
6484 appropriate virtual table, as specified by DW_AT_containing_type.
6485 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6486 to the object address. */
6487
e142c38c 6488 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6489 if (attr)
8e19ed76 6490 {
aec5aa8b 6491 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6492 {
aec5aa8b
TT
6493 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6494 {
6495 /* Old-style GCC. */
6496 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6497 }
6498 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6499 || (DW_BLOCK (attr)->size > 1
6500 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6501 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6502 {
6503 struct dwarf_block blk;
6504 int offset;
6505
6506 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6507 ? 1 : 2);
6508 blk.size = DW_BLOCK (attr)->size - offset;
6509 blk.data = DW_BLOCK (attr)->data + offset;
6510 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6511 if ((fnp->voffset % cu->header.addr_size) != 0)
6512 dwarf2_complex_location_expr_complaint ();
6513 else
6514 fnp->voffset /= cu->header.addr_size;
6515 fnp->voffset += 2;
6516 }
6517 else
6518 dwarf2_complex_location_expr_complaint ();
6519
6520 if (!fnp->fcontext)
6521 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6522 }
3690dd37 6523 else if (attr_form_is_section_offset (attr))
8e19ed76 6524 {
4d3c2250 6525 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6526 }
6527 else
6528 {
4d3c2250
KB
6529 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6530 fieldname);
8e19ed76 6531 }
0d564a31 6532 }
d48cc9dd
DJ
6533 else
6534 {
6535 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6536 if (attr && DW_UNSND (attr))
6537 {
6538 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6539 complaint (&symfile_complaints,
6540 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6541 fieldname, die->offset);
9655fd1a 6542 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6543 TYPE_CPLUS_DYNAMIC (type) = 1;
6544 }
6545 }
c906108c
SS
6546}
6547
6548/* Create the vector of member function fields, and attach it to the type. */
6549
6550static void
fba45db2 6551dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6552 struct dwarf2_cu *cu)
c906108c
SS
6553{
6554 struct fnfieldlist *flp;
6555 int total_length = 0;
6556 int i;
6557
b4ba55a1
JB
6558 if (cu->language == language_ada)
6559 error ("unexpected member functions in Ada type");
6560
c906108c
SS
6561 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6562 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6563 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6564
6565 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6566 {
6567 struct nextfnfield *nfp = flp->head;
6568 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6569 int k;
6570
6571 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6572 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6573 fn_flp->fn_fields = (struct fn_field *)
6574 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6575 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6576 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6577
6578 total_length += flp->length;
6579 }
6580
6581 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6582 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6583}
6584
1168df01
JB
6585/* Returns non-zero if NAME is the name of a vtable member in CU's
6586 language, zero otherwise. */
6587static int
6588is_vtable_name (const char *name, struct dwarf2_cu *cu)
6589{
6590 static const char vptr[] = "_vptr";
987504bb 6591 static const char vtable[] = "vtable";
1168df01 6592
987504bb
JJ
6593 /* Look for the C++ and Java forms of the vtable. */
6594 if ((cu->language == language_java
6595 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6596 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6597 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6598 return 1;
6599
6600 return 0;
6601}
6602
c0dd20ea 6603/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6604 functions, with the ABI-specified layout. If TYPE describes
6605 such a structure, smash it into a member function type.
61049d3b
DJ
6606
6607 GCC shouldn't do this; it should just output pointer to member DIEs.
6608 This is GCC PR debug/28767. */
c0dd20ea 6609
0b92b5bb
TT
6610static void
6611quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6612{
0b92b5bb 6613 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6614
6615 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6616 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6617 return;
c0dd20ea
DJ
6618
6619 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6620 if (TYPE_FIELD_NAME (type, 0) == NULL
6621 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6622 || TYPE_FIELD_NAME (type, 1) == NULL
6623 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6624 return;
c0dd20ea
DJ
6625
6626 /* Find the type of the method. */
0b92b5bb 6627 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6628 if (pfn_type == NULL
6629 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6630 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6631 return;
c0dd20ea
DJ
6632
6633 /* Look for the "this" argument. */
6634 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6635 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6636 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6637 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6638 return;
c0dd20ea
DJ
6639
6640 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6641 new_type = alloc_type (objfile);
6642 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6643 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6644 TYPE_VARARGS (pfn_type));
0b92b5bb 6645 smash_to_methodptr_type (type, new_type);
c0dd20ea 6646}
1168df01 6647
c906108c 6648/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6649 (definition) to create a type for the structure or union. Fill in
6650 the type's name and general properties; the members will not be
6651 processed until process_structure_type.
c906108c 6652
c767944b
DJ
6653 NOTE: we need to call these functions regardless of whether or not the
6654 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6655 structure or union. This gets the type entered into our set of
6656 user defined types.
6657
6658 However, if the structure is incomplete (an opaque struct/union)
6659 then suppress creating a symbol table entry for it since gdb only
6660 wants to find the one with the complete definition. Note that if
6661 it is complete, we just call new_symbol, which does it's own
6662 checking about whether the struct/union is anonymous or not (and
6663 suppresses creating a symbol table entry itself). */
6664
f792889a 6665static struct type *
134d01f1 6666read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6667{
e7c27a73 6668 struct objfile *objfile = cu->objfile;
c906108c
SS
6669 struct type *type;
6670 struct attribute *attr;
39cbfefa 6671 char *name;
c906108c 6672
348e048f
DE
6673 /* If the definition of this type lives in .debug_types, read that type.
6674 Don't follow DW_AT_specification though, that will take us back up
6675 the chain and we want to go down. */
6676 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6677 if (attr)
6678 {
6679 struct dwarf2_cu *type_cu = cu;
6680 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6681
348e048f
DE
6682 /* We could just recurse on read_structure_type, but we need to call
6683 get_die_type to ensure only one type for this DIE is created.
6684 This is important, for example, because for c++ classes we need
6685 TYPE_NAME set which is only done by new_symbol. Blech. */
6686 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6687
6688 /* TYPE_CU may not be the same as CU.
6689 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6690 return set_die_type (die, type, cu);
6691 }
6692
c0dd20ea 6693 type = alloc_type (objfile);
c906108c 6694 INIT_CPLUS_SPECIFIC (type);
93311388 6695
39cbfefa
DJ
6696 name = dwarf2_name (die, cu);
6697 if (name != NULL)
c906108c 6698 {
987504bb
JJ
6699 if (cu->language == language_cplus
6700 || cu->language == language_java)
63d06c5c 6701 {
3da10d80
KS
6702 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6703
6704 /* dwarf2_full_name might have already finished building the DIE's
6705 type. If so, there is no need to continue. */
6706 if (get_die_type (die, cu) != NULL)
6707 return get_die_type (die, cu);
6708
6709 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6710 if (die->tag == DW_TAG_structure_type
6711 || die->tag == DW_TAG_class_type)
6712 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6713 }
6714 else
6715 {
d8151005
DJ
6716 /* The name is already allocated along with this objfile, so
6717 we don't need to duplicate it for the type. */
94af9270
KS
6718 TYPE_TAG_NAME (type) = (char *) name;
6719 if (die->tag == DW_TAG_class_type)
6720 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6721 }
c906108c
SS
6722 }
6723
6724 if (die->tag == DW_TAG_structure_type)
6725 {
6726 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6727 }
6728 else if (die->tag == DW_TAG_union_type)
6729 {
6730 TYPE_CODE (type) = TYPE_CODE_UNION;
6731 }
6732 else
6733 {
c906108c
SS
6734 TYPE_CODE (type) = TYPE_CODE_CLASS;
6735 }
6736
0cc2414c
TT
6737 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6738 TYPE_DECLARED_CLASS (type) = 1;
6739
e142c38c 6740 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6741 if (attr)
6742 {
6743 TYPE_LENGTH (type) = DW_UNSND (attr);
6744 }
6745 else
6746 {
6747 TYPE_LENGTH (type) = 0;
6748 }
6749
876cecd0 6750 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6751 if (die_is_declaration (die, cu))
876cecd0 6752 TYPE_STUB (type) = 1;
a6c727b2
DJ
6753 else if (attr == NULL && die->child == NULL
6754 && producer_is_realview (cu->producer))
6755 /* RealView does not output the required DW_AT_declaration
6756 on incomplete types. */
6757 TYPE_STUB (type) = 1;
dc718098 6758
c906108c
SS
6759 /* We need to add the type field to the die immediately so we don't
6760 infinitely recurse when dealing with pointers to the structure
6761 type within the structure itself. */
1c379e20 6762 set_die_type (die, type, cu);
c906108c 6763
7e314c57
JK
6764 /* set_die_type should be already done. */
6765 set_descriptive_type (type, die, cu);
6766
c767944b
DJ
6767 return type;
6768}
6769
6770/* Finish creating a structure or union type, including filling in
6771 its members and creating a symbol for it. */
6772
6773static void
6774process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6775{
6776 struct objfile *objfile = cu->objfile;
6777 struct die_info *child_die = die->child;
6778 struct type *type;
6779
6780 type = get_die_type (die, cu);
6781 if (type == NULL)
6782 type = read_structure_type (die, cu);
6783
e142c38c 6784 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6785 {
6786 struct field_info fi;
6787 struct die_info *child_die;
34eaf542 6788 VEC (symbolp) *template_args = NULL;
c767944b 6789 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6790
6791 memset (&fi, 0, sizeof (struct field_info));
6792
639d11d3 6793 child_die = die->child;
c906108c
SS
6794
6795 while (child_die && child_die->tag)
6796 {
a9a9bd0f
DC
6797 if (child_die->tag == DW_TAG_member
6798 || child_die->tag == DW_TAG_variable)
c906108c 6799 {
a9a9bd0f
DC
6800 /* NOTE: carlton/2002-11-05: A C++ static data member
6801 should be a DW_TAG_member that is a declaration, but
6802 all versions of G++ as of this writing (so through at
6803 least 3.2.1) incorrectly generate DW_TAG_variable
6804 tags for them instead. */
e7c27a73 6805 dwarf2_add_field (&fi, child_die, cu);
c906108c 6806 }
8713b1b1 6807 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6808 {
6809 /* C++ member function. */
e7c27a73 6810 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6811 }
6812 else if (child_die->tag == DW_TAG_inheritance)
6813 {
6814 /* C++ base class field. */
e7c27a73 6815 dwarf2_add_field (&fi, child_die, cu);
c906108c 6816 }
98751a41
JK
6817 else if (child_die->tag == DW_TAG_typedef)
6818 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6819 else if (child_die->tag == DW_TAG_template_type_param
6820 || child_die->tag == DW_TAG_template_value_param)
6821 {
6822 struct symbol *arg = new_symbol (child_die, NULL, cu);
6823
f1078f66
DJ
6824 if (arg != NULL)
6825 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6826 }
6827
c906108c
SS
6828 child_die = sibling_die (child_die);
6829 }
6830
34eaf542
TT
6831 /* Attach template arguments to type. */
6832 if (! VEC_empty (symbolp, template_args))
6833 {
6834 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6835 TYPE_N_TEMPLATE_ARGUMENTS (type)
6836 = VEC_length (symbolp, template_args);
6837 TYPE_TEMPLATE_ARGUMENTS (type)
6838 = obstack_alloc (&objfile->objfile_obstack,
6839 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6840 * sizeof (struct symbol *)));
6841 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6842 VEC_address (symbolp, template_args),
6843 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6844 * sizeof (struct symbol *)));
6845 VEC_free (symbolp, template_args);
6846 }
6847
c906108c
SS
6848 /* Attach fields and member functions to the type. */
6849 if (fi.nfields)
e7c27a73 6850 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6851 if (fi.nfnfields)
6852 {
e7c27a73 6853 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6854
c5aa993b 6855 /* Get the type which refers to the base class (possibly this
c906108c 6856 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6857 class from the DW_AT_containing_type attribute. This use of
6858 DW_AT_containing_type is a GNU extension. */
c906108c 6859
e142c38c 6860 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6861 {
e7c27a73 6862 struct type *t = die_containing_type (die, cu);
c906108c
SS
6863
6864 TYPE_VPTR_BASETYPE (type) = t;
6865 if (type == t)
6866 {
c906108c
SS
6867 int i;
6868
6869 /* Our own class provides vtbl ptr. */
6870 for (i = TYPE_NFIELDS (t) - 1;
6871 i >= TYPE_N_BASECLASSES (t);
6872 --i)
6873 {
6874 char *fieldname = TYPE_FIELD_NAME (t, i);
6875
1168df01 6876 if (is_vtable_name (fieldname, cu))
c906108c
SS
6877 {
6878 TYPE_VPTR_FIELDNO (type) = i;
6879 break;
6880 }
6881 }
6882
6883 /* Complain if virtual function table field not found. */
6884 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6885 complaint (&symfile_complaints,
e2e0b3e5 6886 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6887 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6888 "");
c906108c
SS
6889 }
6890 else
6891 {
6892 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6893 }
6894 }
f6235d4c
EZ
6895 else if (cu->producer
6896 && strncmp (cu->producer,
6897 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6898 {
6899 /* The IBM XLC compiler does not provide direct indication
6900 of the containing type, but the vtable pointer is
6901 always named __vfp. */
6902
6903 int i;
6904
6905 for (i = TYPE_NFIELDS (type) - 1;
6906 i >= TYPE_N_BASECLASSES (type);
6907 --i)
6908 {
6909 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6910 {
6911 TYPE_VPTR_FIELDNO (type) = i;
6912 TYPE_VPTR_BASETYPE (type) = type;
6913 break;
6914 }
6915 }
6916 }
c906108c 6917 }
98751a41
JK
6918
6919 /* Copy fi.typedef_field_list linked list elements content into the
6920 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6921 if (fi.typedef_field_list)
6922 {
6923 int i = fi.typedef_field_list_count;
6924
a0d7a4ff 6925 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6926 TYPE_TYPEDEF_FIELD_ARRAY (type)
6927 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6928 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6929
6930 /* Reverse the list order to keep the debug info elements order. */
6931 while (--i >= 0)
6932 {
6933 struct typedef_field *dest, *src;
6e70227d 6934
98751a41
JK
6935 dest = &TYPE_TYPEDEF_FIELD (type, i);
6936 src = &fi.typedef_field_list->field;
6937 fi.typedef_field_list = fi.typedef_field_list->next;
6938 *dest = *src;
6939 }
6940 }
c767944b
DJ
6941
6942 do_cleanups (back_to);
c906108c 6943 }
63d06c5c 6944
0b92b5bb
TT
6945 quirk_gcc_member_function_pointer (type, cu->objfile);
6946
90aeadfc
DC
6947 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6948 snapshots) has been known to create a die giving a declaration
6949 for a class that has, as a child, a die giving a definition for a
6950 nested class. So we have to process our children even if the
6951 current die is a declaration. Normally, of course, a declaration
6952 won't have any children at all. */
134d01f1 6953
90aeadfc
DC
6954 while (child_die != NULL && child_die->tag)
6955 {
6956 if (child_die->tag == DW_TAG_member
6957 || child_die->tag == DW_TAG_variable
34eaf542
TT
6958 || child_die->tag == DW_TAG_inheritance
6959 || child_die->tag == DW_TAG_template_value_param
6960 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6961 {
90aeadfc 6962 /* Do nothing. */
134d01f1 6963 }
90aeadfc
DC
6964 else
6965 process_die (child_die, cu);
134d01f1 6966
90aeadfc 6967 child_die = sibling_die (child_die);
134d01f1
DJ
6968 }
6969
fa4028e9
JB
6970 /* Do not consider external references. According to the DWARF standard,
6971 these DIEs are identified by the fact that they have no byte_size
6972 attribute, and a declaration attribute. */
6973 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6974 || !die_is_declaration (die, cu))
c767944b 6975 new_symbol (die, type, cu);
134d01f1
DJ
6976}
6977
6978/* Given a DW_AT_enumeration_type die, set its type. We do not
6979 complete the type's fields yet, or create any symbols. */
c906108c 6980
f792889a 6981static struct type *
134d01f1 6982read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6983{
e7c27a73 6984 struct objfile *objfile = cu->objfile;
c906108c 6985 struct type *type;
c906108c 6986 struct attribute *attr;
0114d602 6987 const char *name;
134d01f1 6988
348e048f
DE
6989 /* If the definition of this type lives in .debug_types, read that type.
6990 Don't follow DW_AT_specification though, that will take us back up
6991 the chain and we want to go down. */
6992 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6993 if (attr)
6994 {
6995 struct dwarf2_cu *type_cu = cu;
6996 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6997
348e048f 6998 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6999
7000 /* TYPE_CU may not be the same as CU.
7001 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7002 return set_die_type (die, type, cu);
7003 }
7004
c906108c
SS
7005 type = alloc_type (objfile);
7006
7007 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7008 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7009 if (name != NULL)
0114d602 7010 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7011
e142c38c 7012 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7013 if (attr)
7014 {
7015 TYPE_LENGTH (type) = DW_UNSND (attr);
7016 }
7017 else
7018 {
7019 TYPE_LENGTH (type) = 0;
7020 }
7021
137033e9
JB
7022 /* The enumeration DIE can be incomplete. In Ada, any type can be
7023 declared as private in the package spec, and then defined only
7024 inside the package body. Such types are known as Taft Amendment
7025 Types. When another package uses such a type, an incomplete DIE
7026 may be generated by the compiler. */
02eb380e 7027 if (die_is_declaration (die, cu))
876cecd0 7028 TYPE_STUB (type) = 1;
02eb380e 7029
f792889a 7030 return set_die_type (die, type, cu);
134d01f1
DJ
7031}
7032
7033/* Given a pointer to a die which begins an enumeration, process all
7034 the dies that define the members of the enumeration, and create the
7035 symbol for the enumeration type.
7036
7037 NOTE: We reverse the order of the element list. */
7038
7039static void
7040process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7041{
f792889a 7042 struct type *this_type;
134d01f1 7043
f792889a
DJ
7044 this_type = get_die_type (die, cu);
7045 if (this_type == NULL)
7046 this_type = read_enumeration_type (die, cu);
9dc481d3 7047
639d11d3 7048 if (die->child != NULL)
c906108c 7049 {
9dc481d3
DE
7050 struct die_info *child_die;
7051 struct symbol *sym;
7052 struct field *fields = NULL;
7053 int num_fields = 0;
7054 int unsigned_enum = 1;
7055 char *name;
7056
639d11d3 7057 child_die = die->child;
c906108c
SS
7058 while (child_die && child_die->tag)
7059 {
7060 if (child_die->tag != DW_TAG_enumerator)
7061 {
e7c27a73 7062 process_die (child_die, cu);
c906108c
SS
7063 }
7064 else
7065 {
39cbfefa
DJ
7066 name = dwarf2_name (child_die, cu);
7067 if (name)
c906108c 7068 {
f792889a 7069 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7070 if (SYMBOL_VALUE (sym) < 0)
7071 unsigned_enum = 0;
7072
7073 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7074 {
7075 fields = (struct field *)
7076 xrealloc (fields,
7077 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7078 * sizeof (struct field));
c906108c
SS
7079 }
7080
3567439c 7081 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7082 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7083 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7084 FIELD_BITSIZE (fields[num_fields]) = 0;
7085
7086 num_fields++;
7087 }
7088 }
7089
7090 child_die = sibling_die (child_die);
7091 }
7092
7093 if (num_fields)
7094 {
f792889a
DJ
7095 TYPE_NFIELDS (this_type) = num_fields;
7096 TYPE_FIELDS (this_type) = (struct field *)
7097 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7098 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7099 sizeof (struct field) * num_fields);
b8c9b27d 7100 xfree (fields);
c906108c
SS
7101 }
7102 if (unsigned_enum)
876cecd0 7103 TYPE_UNSIGNED (this_type) = 1;
c906108c 7104 }
134d01f1 7105
f792889a 7106 new_symbol (die, this_type, cu);
c906108c
SS
7107}
7108
7109/* Extract all information from a DW_TAG_array_type DIE and put it in
7110 the DIE's type field. For now, this only handles one dimensional
7111 arrays. */
7112
f792889a 7113static struct type *
e7c27a73 7114read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7115{
e7c27a73 7116 struct objfile *objfile = cu->objfile;
c906108c 7117 struct die_info *child_die;
7e314c57 7118 struct type *type;
c906108c
SS
7119 struct type *element_type, *range_type, *index_type;
7120 struct type **range_types = NULL;
7121 struct attribute *attr;
7122 int ndim = 0;
7123 struct cleanup *back_to;
39cbfefa 7124 char *name;
c906108c 7125
e7c27a73 7126 element_type = die_type (die, cu);
c906108c 7127
7e314c57
JK
7128 /* The die_type call above may have already set the type for this DIE. */
7129 type = get_die_type (die, cu);
7130 if (type)
7131 return type;
7132
c906108c
SS
7133 /* Irix 6.2 native cc creates array types without children for
7134 arrays with unspecified length. */
639d11d3 7135 if (die->child == NULL)
c906108c 7136 {
46bf5051 7137 index_type = objfile_type (objfile)->builtin_int;
c906108c 7138 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7139 type = create_array_type (NULL, element_type, range_type);
7140 return set_die_type (die, type, cu);
c906108c
SS
7141 }
7142
7143 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7144 child_die = die->child;
c906108c
SS
7145 while (child_die && child_die->tag)
7146 {
7147 if (child_die->tag == DW_TAG_subrange_type)
7148 {
f792889a 7149 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7150
f792889a 7151 if (child_type != NULL)
a02abb62
JB
7152 {
7153 /* The range type was succesfully read. Save it for
7154 the array type creation. */
7155 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7156 {
7157 range_types = (struct type **)
7158 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7159 * sizeof (struct type *));
7160 if (ndim == 0)
7161 make_cleanup (free_current_contents, &range_types);
7162 }
f792889a 7163 range_types[ndim++] = child_type;
a02abb62 7164 }
c906108c
SS
7165 }
7166 child_die = sibling_die (child_die);
7167 }
7168
7169 /* Dwarf2 dimensions are output from left to right, create the
7170 necessary array types in backwards order. */
7ca2d3a3 7171
c906108c 7172 type = element_type;
7ca2d3a3
DL
7173
7174 if (read_array_order (die, cu) == DW_ORD_col_major)
7175 {
7176 int i = 0;
9a619af0 7177
7ca2d3a3
DL
7178 while (i < ndim)
7179 type = create_array_type (NULL, type, range_types[i++]);
7180 }
7181 else
7182 {
7183 while (ndim-- > 0)
7184 type = create_array_type (NULL, type, range_types[ndim]);
7185 }
c906108c 7186
f5f8a009
EZ
7187 /* Understand Dwarf2 support for vector types (like they occur on
7188 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7189 array type. This is not part of the Dwarf2/3 standard yet, but a
7190 custom vendor extension. The main difference between a regular
7191 array and the vector variant is that vectors are passed by value
7192 to functions. */
e142c38c 7193 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7194 if (attr)
ea37ba09 7195 make_vector_type (type);
f5f8a009 7196
dbc98a8b
KW
7197 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7198 implementation may choose to implement triple vectors using this
7199 attribute. */
7200 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7201 if (attr)
7202 {
7203 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7204 TYPE_LENGTH (type) = DW_UNSND (attr);
7205 else
7206 complaint (&symfile_complaints, _("\
7207DW_AT_byte_size for array type smaller than the total size of elements"));
7208 }
7209
39cbfefa
DJ
7210 name = dwarf2_name (die, cu);
7211 if (name)
7212 TYPE_NAME (type) = name;
6e70227d 7213
7e314c57
JK
7214 /* Install the type in the die. */
7215 set_die_type (die, type, cu);
7216
7217 /* set_die_type should be already done. */
b4ba55a1
JB
7218 set_descriptive_type (type, die, cu);
7219
c906108c
SS
7220 do_cleanups (back_to);
7221
7e314c57 7222 return type;
c906108c
SS
7223}
7224
7ca2d3a3 7225static enum dwarf_array_dim_ordering
6e70227d 7226read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7227{
7228 struct attribute *attr;
7229
7230 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7231
7232 if (attr) return DW_SND (attr);
7233
7234 /*
7235 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7236 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7237 laid out as per normal fortran.
7238
6e70227d 7239 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7240 version checking.
7241 */
7242
905e0470
PM
7243 if (cu->language == language_fortran
7244 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7245 {
7246 return DW_ORD_row_major;
7247 }
7248
6e70227d 7249 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7250 {
7251 case array_column_major:
7252 return DW_ORD_col_major;
7253 case array_row_major:
7254 default:
7255 return DW_ORD_row_major;
7256 };
7257}
7258
72019c9c
GM
7259/* Extract all information from a DW_TAG_set_type DIE and put it in
7260 the DIE's type field. */
7261
f792889a 7262static struct type *
72019c9c
GM
7263read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7264{
7e314c57
JK
7265 struct type *domain_type, *set_type;
7266 struct attribute *attr;
f792889a 7267
7e314c57
JK
7268 domain_type = die_type (die, cu);
7269
7270 /* The die_type call above may have already set the type for this DIE. */
7271 set_type = get_die_type (die, cu);
7272 if (set_type)
7273 return set_type;
7274
7275 set_type = create_set_type (NULL, domain_type);
7276
7277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7278 if (attr)
7279 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7280
f792889a 7281 return set_die_type (die, set_type, cu);
72019c9c 7282}
7ca2d3a3 7283
c906108c
SS
7284/* First cut: install each common block member as a global variable. */
7285
7286static void
e7c27a73 7287read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7288{
7289 struct die_info *child_die;
7290 struct attribute *attr;
7291 struct symbol *sym;
7292 CORE_ADDR base = (CORE_ADDR) 0;
7293
e142c38c 7294 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7295 if (attr)
7296 {
8e19ed76
PS
7297 /* Support the .debug_loc offsets */
7298 if (attr_form_is_block (attr))
7299 {
e7c27a73 7300 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7301 }
3690dd37 7302 else if (attr_form_is_section_offset (attr))
8e19ed76 7303 {
4d3c2250 7304 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7305 }
7306 else
7307 {
4d3c2250
KB
7308 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7309 "common block member");
8e19ed76 7310 }
c906108c 7311 }
639d11d3 7312 if (die->child != NULL)
c906108c 7313 {
639d11d3 7314 child_die = die->child;
c906108c
SS
7315 while (child_die && child_die->tag)
7316 {
e7c27a73 7317 sym = new_symbol (child_die, NULL, cu);
e142c38c 7318 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7319 if (sym != NULL && attr != NULL)
c906108c 7320 {
d4b96c9a
JK
7321 CORE_ADDR byte_offset = 0;
7322
7323 if (attr_form_is_section_offset (attr))
7324 dwarf2_complex_location_expr_complaint ();
7325 else if (attr_form_is_constant (attr))
7326 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7327 else if (attr_form_is_block (attr))
7328 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7329 else
7330 dwarf2_complex_location_expr_complaint ();
7331
7332 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7333 add_symbol_to_list (sym, &global_symbols);
7334 }
7335 child_die = sibling_die (child_die);
7336 }
7337 }
7338}
7339
0114d602 7340/* Create a type for a C++ namespace. */
d9fa45fe 7341
0114d602
DJ
7342static struct type *
7343read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7344{
e7c27a73 7345 struct objfile *objfile = cu->objfile;
0114d602 7346 const char *previous_prefix, *name;
9219021c 7347 int is_anonymous;
0114d602
DJ
7348 struct type *type;
7349
7350 /* For extensions, reuse the type of the original namespace. */
7351 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7352 {
7353 struct die_info *ext_die;
7354 struct dwarf2_cu *ext_cu = cu;
9a619af0 7355
0114d602
DJ
7356 ext_die = dwarf2_extension (die, &ext_cu);
7357 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7358
7359 /* EXT_CU may not be the same as CU.
7360 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7361 return set_die_type (die, type, cu);
7362 }
9219021c 7363
e142c38c 7364 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7365
7366 /* Now build the name of the current namespace. */
7367
0114d602
DJ
7368 previous_prefix = determine_prefix (die, cu);
7369 if (previous_prefix[0] != '\0')
7370 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7371 previous_prefix, name, 0, cu);
0114d602
DJ
7372
7373 /* Create the type. */
7374 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7375 objfile);
7376 TYPE_NAME (type) = (char *) name;
7377 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7378
60531b24 7379 return set_die_type (die, type, cu);
0114d602
DJ
7380}
7381
7382/* Read a C++ namespace. */
7383
7384static void
7385read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7386{
7387 struct objfile *objfile = cu->objfile;
7388 const char *name;
7389 int is_anonymous;
9219021c 7390
5c4e30ca
DC
7391 /* Add a symbol associated to this if we haven't seen the namespace
7392 before. Also, add a using directive if it's an anonymous
7393 namespace. */
9219021c 7394
f2f0e013 7395 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7396 {
7397 struct type *type;
7398
0114d602 7399 type = read_type_die (die, cu);
e7c27a73 7400 new_symbol (die, type, cu);
5c4e30ca 7401
0114d602 7402 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7403 if (is_anonymous)
0114d602
DJ
7404 {
7405 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7406
c0cc3a76 7407 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7408 NULL, &objfile->objfile_obstack);
0114d602 7409 }
5c4e30ca 7410 }
9219021c 7411
639d11d3 7412 if (die->child != NULL)
d9fa45fe 7413 {
639d11d3 7414 struct die_info *child_die = die->child;
6e70227d 7415
d9fa45fe
DC
7416 while (child_die && child_die->tag)
7417 {
e7c27a73 7418 process_die (child_die, cu);
d9fa45fe
DC
7419 child_die = sibling_die (child_die);
7420 }
7421 }
38d518c9
EZ
7422}
7423
f55ee35c
JK
7424/* Read a Fortran module as type. This DIE can be only a declaration used for
7425 imported module. Still we need that type as local Fortran "use ... only"
7426 declaration imports depend on the created type in determine_prefix. */
7427
7428static struct type *
7429read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7430{
7431 struct objfile *objfile = cu->objfile;
7432 char *module_name;
7433 struct type *type;
7434
7435 module_name = dwarf2_name (die, cu);
7436 if (!module_name)
7437 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7438 die->offset);
7439 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7440
7441 /* determine_prefix uses TYPE_TAG_NAME. */
7442 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7443
7444 return set_die_type (die, type, cu);
7445}
7446
5d7cb8df
JK
7447/* Read a Fortran module. */
7448
7449static void
7450read_module (struct die_info *die, struct dwarf2_cu *cu)
7451{
7452 struct die_info *child_die = die->child;
7453
5d7cb8df
JK
7454 while (child_die && child_die->tag)
7455 {
7456 process_die (child_die, cu);
7457 child_die = sibling_die (child_die);
7458 }
7459}
7460
38d518c9
EZ
7461/* Return the name of the namespace represented by DIE. Set
7462 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7463 namespace. */
7464
7465static const char *
e142c38c 7466namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7467{
7468 struct die_info *current_die;
7469 const char *name = NULL;
7470
7471 /* Loop through the extensions until we find a name. */
7472
7473 for (current_die = die;
7474 current_die != NULL;
f2f0e013 7475 current_die = dwarf2_extension (die, &cu))
38d518c9 7476 {
e142c38c 7477 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7478 if (name != NULL)
7479 break;
7480 }
7481
7482 /* Is it an anonymous namespace? */
7483
7484 *is_anonymous = (name == NULL);
7485 if (*is_anonymous)
7486 name = "(anonymous namespace)";
7487
7488 return name;
d9fa45fe
DC
7489}
7490
c906108c
SS
7491/* Extract all information from a DW_TAG_pointer_type DIE and add to
7492 the user defined type vector. */
7493
f792889a 7494static struct type *
e7c27a73 7495read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7496{
5e2b427d 7497 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7498 struct comp_unit_head *cu_header = &cu->header;
c906108c 7499 struct type *type;
8b2dbe47
KB
7500 struct attribute *attr_byte_size;
7501 struct attribute *attr_address_class;
7502 int byte_size, addr_class;
7e314c57
JK
7503 struct type *target_type;
7504
7505 target_type = die_type (die, cu);
c906108c 7506
7e314c57
JK
7507 /* The die_type call above may have already set the type for this DIE. */
7508 type = get_die_type (die, cu);
7509 if (type)
7510 return type;
7511
7512 type = lookup_pointer_type (target_type);
8b2dbe47 7513
e142c38c 7514 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7515 if (attr_byte_size)
7516 byte_size = DW_UNSND (attr_byte_size);
c906108c 7517 else
8b2dbe47
KB
7518 byte_size = cu_header->addr_size;
7519
e142c38c 7520 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7521 if (attr_address_class)
7522 addr_class = DW_UNSND (attr_address_class);
7523 else
7524 addr_class = DW_ADDR_none;
7525
7526 /* If the pointer size or address class is different than the
7527 default, create a type variant marked as such and set the
7528 length accordingly. */
7529 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7530 {
5e2b427d 7531 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7532 {
7533 int type_flags;
7534
849957d9 7535 type_flags = gdbarch_address_class_type_flags
5e2b427d 7536 (gdbarch, byte_size, addr_class);
876cecd0
TT
7537 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7538 == 0);
8b2dbe47
KB
7539 type = make_type_with_address_space (type, type_flags);
7540 }
7541 else if (TYPE_LENGTH (type) != byte_size)
7542 {
e2e0b3e5 7543 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7544 }
6e70227d 7545 else
9a619af0
MS
7546 {
7547 /* Should we also complain about unhandled address classes? */
7548 }
c906108c 7549 }
8b2dbe47
KB
7550
7551 TYPE_LENGTH (type) = byte_size;
f792889a 7552 return set_die_type (die, type, cu);
c906108c
SS
7553}
7554
7555/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7556 the user defined type vector. */
7557
f792889a 7558static struct type *
e7c27a73 7559read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7560{
7561 struct type *type;
7562 struct type *to_type;
7563 struct type *domain;
7564
e7c27a73
DJ
7565 to_type = die_type (die, cu);
7566 domain = die_containing_type (die, cu);
0d5de010 7567
7e314c57
JK
7568 /* The calls above may have already set the type for this DIE. */
7569 type = get_die_type (die, cu);
7570 if (type)
7571 return type;
7572
0d5de010
DJ
7573 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7574 type = lookup_methodptr_type (to_type);
7575 else
7576 type = lookup_memberptr_type (to_type, domain);
c906108c 7577
f792889a 7578 return set_die_type (die, type, cu);
c906108c
SS
7579}
7580
7581/* Extract all information from a DW_TAG_reference_type DIE and add to
7582 the user defined type vector. */
7583
f792889a 7584static struct type *
e7c27a73 7585read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7586{
e7c27a73 7587 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7588 struct type *type, *target_type;
c906108c
SS
7589 struct attribute *attr;
7590
7e314c57
JK
7591 target_type = die_type (die, cu);
7592
7593 /* The die_type call above may have already set the type for this DIE. */
7594 type = get_die_type (die, cu);
7595 if (type)
7596 return type;
7597
7598 type = lookup_reference_type (target_type);
e142c38c 7599 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7600 if (attr)
7601 {
7602 TYPE_LENGTH (type) = DW_UNSND (attr);
7603 }
7604 else
7605 {
107d2387 7606 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7607 }
f792889a 7608 return set_die_type (die, type, cu);
c906108c
SS
7609}
7610
f792889a 7611static struct type *
e7c27a73 7612read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7613{
f792889a 7614 struct type *base_type, *cv_type;
c906108c 7615
e7c27a73 7616 base_type = die_type (die, cu);
7e314c57
JK
7617
7618 /* The die_type call above may have already set the type for this DIE. */
7619 cv_type = get_die_type (die, cu);
7620 if (cv_type)
7621 return cv_type;
7622
2f608a3a
KW
7623 /* In case the const qualifier is applied to an array type, the element type
7624 is so qualified, not the array type (section 6.7.3 of C99). */
7625 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7626 {
7627 struct type *el_type, *inner_array;
7628
7629 base_type = copy_type (base_type);
7630 inner_array = base_type;
7631
7632 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7633 {
7634 TYPE_TARGET_TYPE (inner_array) =
7635 copy_type (TYPE_TARGET_TYPE (inner_array));
7636 inner_array = TYPE_TARGET_TYPE (inner_array);
7637 }
7638
7639 el_type = TYPE_TARGET_TYPE (inner_array);
7640 TYPE_TARGET_TYPE (inner_array) =
7641 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7642
7643 return set_die_type (die, base_type, cu);
7644 }
7645
f792889a
DJ
7646 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7647 return set_die_type (die, cv_type, cu);
c906108c
SS
7648}
7649
f792889a 7650static struct type *
e7c27a73 7651read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7652{
f792889a 7653 struct type *base_type, *cv_type;
c906108c 7654
e7c27a73 7655 base_type = die_type (die, cu);
7e314c57
JK
7656
7657 /* The die_type call above may have already set the type for this DIE. */
7658 cv_type = get_die_type (die, cu);
7659 if (cv_type)
7660 return cv_type;
7661
f792889a
DJ
7662 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7663 return set_die_type (die, cv_type, cu);
c906108c
SS
7664}
7665
7666/* Extract all information from a DW_TAG_string_type DIE and add to
7667 the user defined type vector. It isn't really a user defined type,
7668 but it behaves like one, with other DIE's using an AT_user_def_type
7669 attribute to reference it. */
7670
f792889a 7671static struct type *
e7c27a73 7672read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7673{
e7c27a73 7674 struct objfile *objfile = cu->objfile;
3b7538c0 7675 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7676 struct type *type, *range_type, *index_type, *char_type;
7677 struct attribute *attr;
7678 unsigned int length;
7679
e142c38c 7680 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7681 if (attr)
7682 {
7683 length = DW_UNSND (attr);
7684 }
7685 else
7686 {
b21b22e0 7687 /* check for the DW_AT_byte_size attribute */
e142c38c 7688 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7689 if (attr)
7690 {
7691 length = DW_UNSND (attr);
7692 }
7693 else
7694 {
7695 length = 1;
7696 }
c906108c 7697 }
6ccb9162 7698
46bf5051 7699 index_type = objfile_type (objfile)->builtin_int;
c906108c 7700 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7701 char_type = language_string_char_type (cu->language_defn, gdbarch);
7702 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7703
f792889a 7704 return set_die_type (die, type, cu);
c906108c
SS
7705}
7706
7707/* Handle DIES due to C code like:
7708
7709 struct foo
c5aa993b
JM
7710 {
7711 int (*funcp)(int a, long l);
7712 int b;
7713 };
c906108c
SS
7714
7715 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7716 */
c906108c 7717
f792889a 7718static struct type *
e7c27a73 7719read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7720{
7721 struct type *type; /* Type that this function returns */
7722 struct type *ftype; /* Function that returns above type */
7723 struct attribute *attr;
7724
e7c27a73 7725 type = die_type (die, cu);
7e314c57
JK
7726
7727 /* The die_type call above may have already set the type for this DIE. */
7728 ftype = get_die_type (die, cu);
7729 if (ftype)
7730 return ftype;
7731
0c8b41f1 7732 ftype = lookup_function_type (type);
c906108c 7733
5b8101ae 7734 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7735 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7736 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7737 || cu->language == language_cplus
5b8101ae
PM
7738 || cu->language == language_java
7739 || cu->language == language_pascal)
876cecd0 7740 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7741 else if (producer_is_realview (cu->producer))
7742 /* RealView does not emit DW_AT_prototyped. We can not
7743 distinguish prototyped and unprototyped functions; default to
7744 prototyped, since that is more common in modern code (and
7745 RealView warns about unprototyped functions). */
7746 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7747
c055b101
CV
7748 /* Store the calling convention in the type if it's available in
7749 the subroutine die. Otherwise set the calling convention to
7750 the default value DW_CC_normal. */
7751 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7752 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7753
7754 /* We need to add the subroutine type to the die immediately so
7755 we don't infinitely recurse when dealing with parameters
7756 declared as the same subroutine type. */
7757 set_die_type (die, ftype, cu);
6e70227d 7758
639d11d3 7759 if (die->child != NULL)
c906108c 7760 {
8072405b 7761 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7762 struct die_info *child_die;
8072405b 7763 int nparams, iparams;
c906108c
SS
7764
7765 /* Count the number of parameters.
7766 FIXME: GDB currently ignores vararg functions, but knows about
7767 vararg member functions. */
8072405b 7768 nparams = 0;
639d11d3 7769 child_die = die->child;
c906108c
SS
7770 while (child_die && child_die->tag)
7771 {
7772 if (child_die->tag == DW_TAG_formal_parameter)
7773 nparams++;
7774 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7775 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7776 child_die = sibling_die (child_die);
7777 }
7778
7779 /* Allocate storage for parameters and fill them in. */
7780 TYPE_NFIELDS (ftype) = nparams;
7781 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7782 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7783
8072405b
JK
7784 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7785 even if we error out during the parameters reading below. */
7786 for (iparams = 0; iparams < nparams; iparams++)
7787 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7788
7789 iparams = 0;
639d11d3 7790 child_die = die->child;
c906108c
SS
7791 while (child_die && child_die->tag)
7792 {
7793 if (child_die->tag == DW_TAG_formal_parameter)
7794 {
3ce3b1ba
PA
7795 struct type *arg_type;
7796
7797 /* DWARF version 2 has no clean way to discern C++
7798 static and non-static member functions. G++ helps
7799 GDB by marking the first parameter for non-static
7800 member functions (which is the this pointer) as
7801 artificial. We pass this information to
7802 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7803
7804 DWARF version 3 added DW_AT_object_pointer, which GCC
7805 4.5 does not yet generate. */
e142c38c 7806 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7807 if (attr)
7808 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7809 else
418835cc
KS
7810 {
7811 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7812
7813 /* GCC/43521: In java, the formal parameter
7814 "this" is sometimes not marked with DW_AT_artificial. */
7815 if (cu->language == language_java)
7816 {
7817 const char *name = dwarf2_name (child_die, cu);
9a619af0 7818
418835cc
KS
7819 if (name && !strcmp (name, "this"))
7820 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7821 }
7822 }
3ce3b1ba
PA
7823 arg_type = die_type (child_die, cu);
7824
7825 /* RealView does not mark THIS as const, which the testsuite
7826 expects. GCC marks THIS as const in method definitions,
7827 but not in the class specifications (GCC PR 43053). */
7828 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7829 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7830 {
7831 int is_this = 0;
7832 struct dwarf2_cu *arg_cu = cu;
7833 const char *name = dwarf2_name (child_die, cu);
7834
7835 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7836 if (attr)
7837 {
7838 /* If the compiler emits this, use it. */
7839 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7840 is_this = 1;
7841 }
7842 else if (name && strcmp (name, "this") == 0)
7843 /* Function definitions will have the argument names. */
7844 is_this = 1;
7845 else if (name == NULL && iparams == 0)
7846 /* Declarations may not have the names, so like
7847 elsewhere in GDB, assume an artificial first
7848 argument is "this". */
7849 is_this = 1;
7850
7851 if (is_this)
7852 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7853 arg_type, 0);
7854 }
7855
7856 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7857 iparams++;
7858 }
7859 child_die = sibling_die (child_die);
7860 }
7861 }
7862
76c10ea2 7863 return ftype;
c906108c
SS
7864}
7865
f792889a 7866static struct type *
e7c27a73 7867read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7868{
e7c27a73 7869 struct objfile *objfile = cu->objfile;
0114d602 7870 const char *name = NULL;
f792889a 7871 struct type *this_type;
c906108c 7872
94af9270 7873 name = dwarf2_full_name (NULL, die, cu);
f792889a 7874 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7875 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7876 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7877 set_die_type (die, this_type, cu);
7878 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7879 return this_type;
c906108c
SS
7880}
7881
7882/* Find a representation of a given base type and install
7883 it in the TYPE field of the die. */
7884
f792889a 7885static struct type *
e7c27a73 7886read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7887{
e7c27a73 7888 struct objfile *objfile = cu->objfile;
c906108c
SS
7889 struct type *type;
7890 struct attribute *attr;
7891 int encoding = 0, size = 0;
39cbfefa 7892 char *name;
6ccb9162
UW
7893 enum type_code code = TYPE_CODE_INT;
7894 int type_flags = 0;
7895 struct type *target_type = NULL;
c906108c 7896
e142c38c 7897 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7898 if (attr)
7899 {
7900 encoding = DW_UNSND (attr);
7901 }
e142c38c 7902 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7903 if (attr)
7904 {
7905 size = DW_UNSND (attr);
7906 }
39cbfefa 7907 name = dwarf2_name (die, cu);
6ccb9162 7908 if (!name)
c906108c 7909 {
6ccb9162
UW
7910 complaint (&symfile_complaints,
7911 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7912 }
6ccb9162
UW
7913
7914 switch (encoding)
c906108c 7915 {
6ccb9162
UW
7916 case DW_ATE_address:
7917 /* Turn DW_ATE_address into a void * pointer. */
7918 code = TYPE_CODE_PTR;
7919 type_flags |= TYPE_FLAG_UNSIGNED;
7920 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7921 break;
7922 case DW_ATE_boolean:
7923 code = TYPE_CODE_BOOL;
7924 type_flags |= TYPE_FLAG_UNSIGNED;
7925 break;
7926 case DW_ATE_complex_float:
7927 code = TYPE_CODE_COMPLEX;
7928 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7929 break;
7930 case DW_ATE_decimal_float:
7931 code = TYPE_CODE_DECFLOAT;
7932 break;
7933 case DW_ATE_float:
7934 code = TYPE_CODE_FLT;
7935 break;
7936 case DW_ATE_signed:
7937 break;
7938 case DW_ATE_unsigned:
7939 type_flags |= TYPE_FLAG_UNSIGNED;
7940 break;
7941 case DW_ATE_signed_char:
6e70227d 7942 if (cu->language == language_ada || cu->language == language_m2
868a0084 7943 || cu->language == language_pascal)
6ccb9162
UW
7944 code = TYPE_CODE_CHAR;
7945 break;
7946 case DW_ATE_unsigned_char:
868a0084
PM
7947 if (cu->language == language_ada || cu->language == language_m2
7948 || cu->language == language_pascal)
6ccb9162
UW
7949 code = TYPE_CODE_CHAR;
7950 type_flags |= TYPE_FLAG_UNSIGNED;
7951 break;
75079b2b
TT
7952 case DW_ATE_UTF:
7953 /* We just treat this as an integer and then recognize the
7954 type by name elsewhere. */
7955 break;
7956
6ccb9162
UW
7957 default:
7958 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7959 dwarf_type_encoding_name (encoding));
7960 break;
c906108c 7961 }
6ccb9162 7962
0114d602
DJ
7963 type = init_type (code, size, type_flags, NULL, objfile);
7964 TYPE_NAME (type) = name;
6ccb9162
UW
7965 TYPE_TARGET_TYPE (type) = target_type;
7966
0114d602 7967 if (name && strcmp (name, "char") == 0)
876cecd0 7968 TYPE_NOSIGN (type) = 1;
0114d602 7969
f792889a 7970 return set_die_type (die, type, cu);
c906108c
SS
7971}
7972
a02abb62
JB
7973/* Read the given DW_AT_subrange DIE. */
7974
f792889a 7975static struct type *
a02abb62
JB
7976read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7977{
5e2b427d 7978 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7979 struct type *base_type;
7980 struct type *range_type;
7981 struct attribute *attr;
43bbcdc2
PH
7982 LONGEST low = 0;
7983 LONGEST high = -1;
39cbfefa 7984 char *name;
43bbcdc2 7985 LONGEST negative_mask;
e77813c8 7986
a02abb62 7987 base_type = die_type (die, cu);
953ac07e
JK
7988 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7989 check_typedef (base_type);
a02abb62 7990
7e314c57
JK
7991 /* The die_type call above may have already set the type for this DIE. */
7992 range_type = get_die_type (die, cu);
7993 if (range_type)
7994 return range_type;
7995
e142c38c 7996 if (cu->language == language_fortran)
6e70227d 7997 {
a02abb62
JB
7998 /* FORTRAN implies a lower bound of 1, if not given. */
7999 low = 1;
8000 }
8001
dd5e6932
DJ
8002 /* FIXME: For variable sized arrays either of these could be
8003 a variable rather than a constant value. We'll allow it,
8004 but we don't know how to handle it. */
e142c38c 8005 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8006 if (attr)
8007 low = dwarf2_get_attr_constant_value (attr, 0);
8008
e142c38c 8009 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8010 if (attr)
6e70227d 8011 {
e77813c8 8012 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8013 {
8014 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8015 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8016 FIXME: GDB does not yet know how to handle dynamic
8017 arrays properly, treat them as arrays with unspecified
8018 length for now.
8019
8020 FIXME: jimb/2003-09-22: GDB does not really know
8021 how to handle arrays of unspecified length
8022 either; we just represent them as zero-length
8023 arrays. Choose an appropriate upper bound given
8024 the lower bound we've computed above. */
8025 high = low - 1;
8026 }
8027 else
8028 high = dwarf2_get_attr_constant_value (attr, 1);
8029 }
e77813c8
PM
8030 else
8031 {
8032 attr = dwarf2_attr (die, DW_AT_count, cu);
8033 if (attr)
8034 {
8035 int count = dwarf2_get_attr_constant_value (attr, 1);
8036 high = low + count - 1;
8037 }
8038 }
8039
8040 /* Dwarf-2 specifications explicitly allows to create subrange types
8041 without specifying a base type.
8042 In that case, the base type must be set to the type of
8043 the lower bound, upper bound or count, in that order, if any of these
8044 three attributes references an object that has a type.
8045 If no base type is found, the Dwarf-2 specifications say that
8046 a signed integer type of size equal to the size of an address should
8047 be used.
8048 For the following C code: `extern char gdb_int [];'
8049 GCC produces an empty range DIE.
8050 FIXME: muller/2010-05-28: Possible references to object for low bound,
8051 high bound or count are not yet handled by this code.
8052 */
8053 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8054 {
8055 struct objfile *objfile = cu->objfile;
8056 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8057 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8058 struct type *int_type = objfile_type (objfile)->builtin_int;
8059
8060 /* Test "int", "long int", and "long long int" objfile types,
8061 and select the first one having a size above or equal to the
8062 architecture address size. */
8063 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8064 base_type = int_type;
8065 else
8066 {
8067 int_type = objfile_type (objfile)->builtin_long;
8068 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8069 base_type = int_type;
8070 else
8071 {
8072 int_type = objfile_type (objfile)->builtin_long_long;
8073 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8074 base_type = int_type;
8075 }
8076 }
8077 }
a02abb62 8078
6e70227d 8079 negative_mask =
43bbcdc2
PH
8080 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8081 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8082 low |= negative_mask;
8083 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8084 high |= negative_mask;
8085
a02abb62
JB
8086 range_type = create_range_type (NULL, base_type, low, high);
8087
bbb0eef6
JK
8088 /* Mark arrays with dynamic length at least as an array of unspecified
8089 length. GDB could check the boundary but before it gets implemented at
8090 least allow accessing the array elements. */
8091 if (attr && attr->form == DW_FORM_block1)
8092 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8093
39cbfefa
DJ
8094 name = dwarf2_name (die, cu);
8095 if (name)
8096 TYPE_NAME (range_type) = name;
6e70227d 8097
e142c38c 8098 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8099 if (attr)
8100 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8101
7e314c57
JK
8102 set_die_type (die, range_type, cu);
8103
8104 /* set_die_type should be already done. */
b4ba55a1
JB
8105 set_descriptive_type (range_type, die, cu);
8106
7e314c57 8107 return range_type;
a02abb62 8108}
6e70227d 8109
f792889a 8110static struct type *
81a17f79
JB
8111read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8112{
8113 struct type *type;
81a17f79 8114
81a17f79
JB
8115 /* For now, we only support the C meaning of an unspecified type: void. */
8116
0114d602
DJ
8117 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8118 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8119
f792889a 8120 return set_die_type (die, type, cu);
81a17f79 8121}
a02abb62 8122
51545339
DJ
8123/* Trivial hash function for die_info: the hash value of a DIE
8124 is its offset in .debug_info for this objfile. */
8125
8126static hashval_t
8127die_hash (const void *item)
8128{
8129 const struct die_info *die = item;
9a619af0 8130
51545339
DJ
8131 return die->offset;
8132}
8133
8134/* Trivial comparison function for die_info structures: two DIEs
8135 are equal if they have the same offset. */
8136
8137static int
8138die_eq (const void *item_lhs, const void *item_rhs)
8139{
8140 const struct die_info *die_lhs = item_lhs;
8141 const struct die_info *die_rhs = item_rhs;
9a619af0 8142
51545339
DJ
8143 return die_lhs->offset == die_rhs->offset;
8144}
8145
c906108c
SS
8146/* Read a whole compilation unit into a linked list of dies. */
8147
f9aca02d 8148static struct die_info *
93311388 8149read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8150{
93311388 8151 struct die_reader_specs reader_specs;
98bfdba5 8152 int read_abbrevs = 0;
1d9ec526 8153 struct cleanup *back_to = NULL;
98bfdba5
PA
8154 struct die_info *die;
8155
8156 if (cu->dwarf2_abbrevs == NULL)
8157 {
8158 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8159 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8160 read_abbrevs = 1;
8161 }
93311388 8162
348e048f 8163 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8164 cu->die_hash
8165 = htab_create_alloc_ex (cu->header.length / 12,
8166 die_hash,
8167 die_eq,
8168 NULL,
8169 &cu->comp_unit_obstack,
8170 hashtab_obstack_allocate,
8171 dummy_obstack_deallocate);
8172
93311388
DE
8173 init_cu_die_reader (&reader_specs, cu);
8174
98bfdba5
PA
8175 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8176
8177 if (read_abbrevs)
8178 do_cleanups (back_to);
8179
8180 return die;
639d11d3
DC
8181}
8182
d97bc12b
DE
8183/* Main entry point for reading a DIE and all children.
8184 Read the DIE and dump it if requested. */
8185
8186static struct die_info *
93311388
DE
8187read_die_and_children (const struct die_reader_specs *reader,
8188 gdb_byte *info_ptr,
d97bc12b
DE
8189 gdb_byte **new_info_ptr,
8190 struct die_info *parent)
8191{
93311388 8192 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8193 new_info_ptr, parent);
8194
8195 if (dwarf2_die_debug)
8196 {
348e048f
DE
8197 fprintf_unfiltered (gdb_stdlog,
8198 "\nRead die from %s of %s:\n",
8199 reader->buffer == dwarf2_per_objfile->info.buffer
8200 ? ".debug_info"
8201 : reader->buffer == dwarf2_per_objfile->types.buffer
8202 ? ".debug_types"
8203 : "unknown section",
8204 reader->abfd->filename);
d97bc12b
DE
8205 dump_die (result, dwarf2_die_debug);
8206 }
8207
8208 return result;
8209}
8210
639d11d3
DC
8211/* Read a single die and all its descendents. Set the die's sibling
8212 field to NULL; set other fields in the die correctly, and set all
8213 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8214 location of the info_ptr after reading all of those dies. PARENT
8215 is the parent of the die in question. */
8216
8217static struct die_info *
93311388
DE
8218read_die_and_children_1 (const struct die_reader_specs *reader,
8219 gdb_byte *info_ptr,
d97bc12b
DE
8220 gdb_byte **new_info_ptr,
8221 struct die_info *parent)
639d11d3
DC
8222{
8223 struct die_info *die;
fe1b8b76 8224 gdb_byte *cur_ptr;
639d11d3
DC
8225 int has_children;
8226
93311388 8227 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8228 if (die == NULL)
8229 {
8230 *new_info_ptr = cur_ptr;
8231 return NULL;
8232 }
93311388 8233 store_in_ref_table (die, reader->cu);
639d11d3
DC
8234
8235 if (has_children)
348e048f 8236 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8237 else
8238 {
8239 die->child = NULL;
8240 *new_info_ptr = cur_ptr;
8241 }
8242
8243 die->sibling = NULL;
8244 die->parent = parent;
8245 return die;
8246}
8247
8248/* Read a die, all of its descendents, and all of its siblings; set
8249 all of the fields of all of the dies correctly. Arguments are as
8250 in read_die_and_children. */
8251
8252static struct die_info *
93311388
DE
8253read_die_and_siblings (const struct die_reader_specs *reader,
8254 gdb_byte *info_ptr,
fe1b8b76 8255 gdb_byte **new_info_ptr,
639d11d3
DC
8256 struct die_info *parent)
8257{
8258 struct die_info *first_die, *last_sibling;
fe1b8b76 8259 gdb_byte *cur_ptr;
639d11d3 8260
c906108c 8261 cur_ptr = info_ptr;
639d11d3
DC
8262 first_die = last_sibling = NULL;
8263
8264 while (1)
c906108c 8265 {
639d11d3 8266 struct die_info *die
93311388 8267 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8268
1d325ec1 8269 if (die == NULL)
c906108c 8270 {
639d11d3
DC
8271 *new_info_ptr = cur_ptr;
8272 return first_die;
c906108c 8273 }
1d325ec1
DJ
8274
8275 if (!first_die)
8276 first_die = die;
c906108c 8277 else
1d325ec1
DJ
8278 last_sibling->sibling = die;
8279
8280 last_sibling = die;
c906108c 8281 }
c906108c
SS
8282}
8283
93311388
DE
8284/* Read the die from the .debug_info section buffer. Set DIEP to
8285 point to a newly allocated die with its information, except for its
8286 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8287 whether the die has children or not. */
8288
8289static gdb_byte *
8290read_full_die (const struct die_reader_specs *reader,
8291 struct die_info **diep, gdb_byte *info_ptr,
8292 int *has_children)
8293{
8294 unsigned int abbrev_number, bytes_read, i, offset;
8295 struct abbrev_info *abbrev;
8296 struct die_info *die;
8297 struct dwarf2_cu *cu = reader->cu;
8298 bfd *abfd = reader->abfd;
8299
8300 offset = info_ptr - reader->buffer;
8301 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8302 info_ptr += bytes_read;
8303 if (!abbrev_number)
8304 {
8305 *diep = NULL;
8306 *has_children = 0;
8307 return info_ptr;
8308 }
8309
8310 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8311 if (!abbrev)
348e048f
DE
8312 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8313 abbrev_number,
8314 bfd_get_filename (abfd));
8315
93311388
DE
8316 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8317 die->offset = offset;
8318 die->tag = abbrev->tag;
8319 die->abbrev = abbrev_number;
8320
8321 die->num_attrs = abbrev->num_attrs;
8322
8323 for (i = 0; i < abbrev->num_attrs; ++i)
8324 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8325 abfd, info_ptr, cu);
8326
8327 *diep = die;
8328 *has_children = abbrev->has_children;
8329 return info_ptr;
8330}
8331
c906108c
SS
8332/* In DWARF version 2, the description of the debugging information is
8333 stored in a separate .debug_abbrev section. Before we read any
8334 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8335 in a hash table. This function also sets flags in CU describing
8336 the data found in the abbrev table. */
c906108c
SS
8337
8338static void
e7c27a73 8339dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8340{
e7c27a73 8341 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8342 gdb_byte *abbrev_ptr;
c906108c
SS
8343 struct abbrev_info *cur_abbrev;
8344 unsigned int abbrev_number, bytes_read, abbrev_name;
8345 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8346 struct attr_abbrev *cur_attrs;
8347 unsigned int allocated_attrs;
c906108c 8348
57349743 8349 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8350 obstack_init (&cu->abbrev_obstack);
8351 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8352 (ABBREV_HASH_SIZE
8353 * sizeof (struct abbrev_info *)));
8354 memset (cu->dwarf2_abbrevs, 0,
8355 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8356
be391dca
TT
8357 dwarf2_read_section (dwarf2_per_objfile->objfile,
8358 &dwarf2_per_objfile->abbrev);
dce234bc 8359 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8360 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8361 abbrev_ptr += bytes_read;
8362
f3dd6933
DJ
8363 allocated_attrs = ATTR_ALLOC_CHUNK;
8364 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8365
c906108c
SS
8366 /* loop until we reach an abbrev number of 0 */
8367 while (abbrev_number)
8368 {
f3dd6933 8369 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8370
8371 /* read in abbrev header */
8372 cur_abbrev->number = abbrev_number;
8373 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8374 abbrev_ptr += bytes_read;
8375 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8376 abbrev_ptr += 1;
8377
72bf9492
DJ
8378 if (cur_abbrev->tag == DW_TAG_namespace)
8379 cu->has_namespace_info = 1;
8380
c906108c
SS
8381 /* now read in declarations */
8382 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8383 abbrev_ptr += bytes_read;
8384 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8385 abbrev_ptr += bytes_read;
8386 while (abbrev_name)
8387 {
f3dd6933 8388 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8389 {
f3dd6933
DJ
8390 allocated_attrs += ATTR_ALLOC_CHUNK;
8391 cur_attrs
8392 = xrealloc (cur_attrs, (allocated_attrs
8393 * sizeof (struct attr_abbrev)));
c906108c 8394 }
ae038cb0
DJ
8395
8396 /* Record whether this compilation unit might have
8397 inter-compilation-unit references. If we don't know what form
8398 this attribute will have, then it might potentially be a
8399 DW_FORM_ref_addr, so we conservatively expect inter-CU
8400 references. */
8401
8402 if (abbrev_form == DW_FORM_ref_addr
8403 || abbrev_form == DW_FORM_indirect)
8404 cu->has_form_ref_addr = 1;
8405
f3dd6933
DJ
8406 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8407 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8408 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8409 abbrev_ptr += bytes_read;
8410 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8411 abbrev_ptr += bytes_read;
8412 }
8413
f3dd6933
DJ
8414 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8415 (cur_abbrev->num_attrs
8416 * sizeof (struct attr_abbrev)));
8417 memcpy (cur_abbrev->attrs, cur_attrs,
8418 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8419
c906108c 8420 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8421 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8422 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8423
8424 /* Get next abbreviation.
8425 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8426 always properly terminated with an abbrev number of 0.
8427 Exit loop if we encounter an abbreviation which we have
8428 already read (which means we are about to read the abbreviations
8429 for the next compile unit) or if the end of the abbreviation
8430 table is reached. */
dce234bc
PP
8431 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8432 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8433 break;
8434 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8435 abbrev_ptr += bytes_read;
e7c27a73 8436 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8437 break;
8438 }
f3dd6933
DJ
8439
8440 xfree (cur_attrs);
c906108c
SS
8441}
8442
f3dd6933 8443/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8444
c906108c 8445static void
f3dd6933 8446dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8447{
f3dd6933 8448 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8449
f3dd6933
DJ
8450 obstack_free (&cu->abbrev_obstack, NULL);
8451 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8452}
8453
8454/* Lookup an abbrev_info structure in the abbrev hash table. */
8455
8456static struct abbrev_info *
e7c27a73 8457dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8458{
8459 unsigned int hash_number;
8460 struct abbrev_info *abbrev;
8461
8462 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8463 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8464
8465 while (abbrev)
8466 {
8467 if (abbrev->number == number)
8468 return abbrev;
8469 else
8470 abbrev = abbrev->next;
8471 }
8472 return NULL;
8473}
8474
72bf9492
DJ
8475/* Returns nonzero if TAG represents a type that we might generate a partial
8476 symbol for. */
8477
8478static int
8479is_type_tag_for_partial (int tag)
8480{
8481 switch (tag)
8482 {
8483#if 0
8484 /* Some types that would be reasonable to generate partial symbols for,
8485 that we don't at present. */
8486 case DW_TAG_array_type:
8487 case DW_TAG_file_type:
8488 case DW_TAG_ptr_to_member_type:
8489 case DW_TAG_set_type:
8490 case DW_TAG_string_type:
8491 case DW_TAG_subroutine_type:
8492#endif
8493 case DW_TAG_base_type:
8494 case DW_TAG_class_type:
680b30c7 8495 case DW_TAG_interface_type:
72bf9492
DJ
8496 case DW_TAG_enumeration_type:
8497 case DW_TAG_structure_type:
8498 case DW_TAG_subrange_type:
8499 case DW_TAG_typedef:
8500 case DW_TAG_union_type:
8501 return 1;
8502 default:
8503 return 0;
8504 }
8505}
8506
8507/* Load all DIEs that are interesting for partial symbols into memory. */
8508
8509static struct partial_die_info *
93311388
DE
8510load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8511 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8512{
8513 struct partial_die_info *part_die;
8514 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8515 struct abbrev_info *abbrev;
8516 unsigned int bytes_read;
5afb4e99 8517 unsigned int load_all = 0;
72bf9492
DJ
8518
8519 int nesting_level = 1;
8520
8521 parent_die = NULL;
8522 last_die = NULL;
8523
5afb4e99
DJ
8524 if (cu->per_cu && cu->per_cu->load_all_dies)
8525 load_all = 1;
8526
72bf9492
DJ
8527 cu->partial_dies
8528 = htab_create_alloc_ex (cu->header.length / 12,
8529 partial_die_hash,
8530 partial_die_eq,
8531 NULL,
8532 &cu->comp_unit_obstack,
8533 hashtab_obstack_allocate,
8534 dummy_obstack_deallocate);
8535
8536 part_die = obstack_alloc (&cu->comp_unit_obstack,
8537 sizeof (struct partial_die_info));
8538
8539 while (1)
8540 {
8541 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8542
8543 /* A NULL abbrev means the end of a series of children. */
8544 if (abbrev == NULL)
8545 {
8546 if (--nesting_level == 0)
8547 {
8548 /* PART_DIE was probably the last thing allocated on the
8549 comp_unit_obstack, so we could call obstack_free
8550 here. We don't do that because the waste is small,
8551 and will be cleaned up when we're done with this
8552 compilation unit. This way, we're also more robust
8553 against other users of the comp_unit_obstack. */
8554 return first_die;
8555 }
8556 info_ptr += bytes_read;
8557 last_die = parent_die;
8558 parent_die = parent_die->die_parent;
8559 continue;
8560 }
8561
98bfdba5
PA
8562 /* Check for template arguments. We never save these; if
8563 they're seen, we just mark the parent, and go on our way. */
8564 if (parent_die != NULL
8565 && cu->language == language_cplus
8566 && (abbrev->tag == DW_TAG_template_type_param
8567 || abbrev->tag == DW_TAG_template_value_param))
8568 {
8569 parent_die->has_template_arguments = 1;
8570
8571 if (!load_all)
8572 {
8573 /* We don't need a partial DIE for the template argument. */
8574 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8575 cu);
8576 continue;
8577 }
8578 }
8579
8580 /* We only recurse into subprograms looking for template arguments.
8581 Skip their other children. */
8582 if (!load_all
8583 && cu->language == language_cplus
8584 && parent_die != NULL
8585 && parent_die->tag == DW_TAG_subprogram)
8586 {
8587 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8588 continue;
8589 }
8590
5afb4e99
DJ
8591 /* Check whether this DIE is interesting enough to save. Normally
8592 we would not be interested in members here, but there may be
8593 later variables referencing them via DW_AT_specification (for
8594 static members). */
8595 if (!load_all
8596 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8597 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8598 && abbrev->tag != DW_TAG_enumerator
8599 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8600 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8601 && abbrev->tag != DW_TAG_variable
5afb4e99 8602 && abbrev->tag != DW_TAG_namespace
f55ee35c 8603 && abbrev->tag != DW_TAG_module
5afb4e99 8604 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8605 {
8606 /* Otherwise we skip to the next sibling, if any. */
93311388 8607 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8608 continue;
8609 }
8610
93311388
DE
8611 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8612 buffer, info_ptr, cu);
72bf9492
DJ
8613
8614 /* This two-pass algorithm for processing partial symbols has a
8615 high cost in cache pressure. Thus, handle some simple cases
8616 here which cover the majority of C partial symbols. DIEs
8617 which neither have specification tags in them, nor could have
8618 specification tags elsewhere pointing at them, can simply be
8619 processed and discarded.
8620
8621 This segment is also optional; scan_partial_symbols and
8622 add_partial_symbol will handle these DIEs if we chain
8623 them in normally. When compilers which do not emit large
8624 quantities of duplicate debug information are more common,
8625 this code can probably be removed. */
8626
8627 /* Any complete simple types at the top level (pretty much all
8628 of them, for a language without namespaces), can be processed
8629 directly. */
8630 if (parent_die == NULL
8631 && part_die->has_specification == 0
8632 && part_die->is_declaration == 0
8633 && (part_die->tag == DW_TAG_typedef
8634 || part_die->tag == DW_TAG_base_type
8635 || part_die->tag == DW_TAG_subrange_type))
8636 {
8637 if (building_psymtab && part_die->name != NULL)
04a679b8 8638 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8639 VAR_DOMAIN, LOC_TYPEDEF,
8640 &cu->objfile->static_psymbols,
8641 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8642 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8643 continue;
8644 }
8645
8646 /* If we're at the second level, and we're an enumerator, and
8647 our parent has no specification (meaning possibly lives in a
8648 namespace elsewhere), then we can add the partial symbol now
8649 instead of queueing it. */
8650 if (part_die->tag == DW_TAG_enumerator
8651 && parent_die != NULL
8652 && parent_die->die_parent == NULL
8653 && parent_die->tag == DW_TAG_enumeration_type
8654 && parent_die->has_specification == 0)
8655 {
8656 if (part_die->name == NULL)
e2e0b3e5 8657 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8658 else if (building_psymtab)
04a679b8 8659 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8660 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8661 (cu->language == language_cplus
8662 || cu->language == language_java)
72bf9492
DJ
8663 ? &cu->objfile->global_psymbols
8664 : &cu->objfile->static_psymbols,
8665 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8666
93311388 8667 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8668 continue;
8669 }
8670
8671 /* We'll save this DIE so link it in. */
8672 part_die->die_parent = parent_die;
8673 part_die->die_sibling = NULL;
8674 part_die->die_child = NULL;
8675
8676 if (last_die && last_die == parent_die)
8677 last_die->die_child = part_die;
8678 else if (last_die)
8679 last_die->die_sibling = part_die;
8680
8681 last_die = part_die;
8682
8683 if (first_die == NULL)
8684 first_die = part_die;
8685
8686 /* Maybe add the DIE to the hash table. Not all DIEs that we
8687 find interesting need to be in the hash table, because we
8688 also have the parent/sibling/child chains; only those that we
8689 might refer to by offset later during partial symbol reading.
8690
8691 For now this means things that might have be the target of a
8692 DW_AT_specification, DW_AT_abstract_origin, or
8693 DW_AT_extension. DW_AT_extension will refer only to
8694 namespaces; DW_AT_abstract_origin refers to functions (and
8695 many things under the function DIE, but we do not recurse
8696 into function DIEs during partial symbol reading) and
8697 possibly variables as well; DW_AT_specification refers to
8698 declarations. Declarations ought to have the DW_AT_declaration
8699 flag. It happens that GCC forgets to put it in sometimes, but
8700 only for functions, not for types.
8701
8702 Adding more things than necessary to the hash table is harmless
8703 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8704 wasted time in find_partial_die, when we reread the compilation
8705 unit with load_all_dies set. */
72bf9492 8706
5afb4e99 8707 if (load_all
72929c62 8708 || abbrev->tag == DW_TAG_constant
5afb4e99 8709 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8710 || abbrev->tag == DW_TAG_variable
8711 || abbrev->tag == DW_TAG_namespace
8712 || part_die->is_declaration)
8713 {
8714 void **slot;
8715
8716 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8717 part_die->offset, INSERT);
8718 *slot = part_die;
8719 }
8720
8721 part_die = obstack_alloc (&cu->comp_unit_obstack,
8722 sizeof (struct partial_die_info));
8723
8724 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8725 we have no reason to follow the children of structures; for other
98bfdba5
PA
8726 languages we have to, so that we can get at method physnames
8727 to infer fully qualified class names, for DW_AT_specification,
8728 and for C++ template arguments. For C++, we also look one level
8729 inside functions to find template arguments (if the name of the
8730 function does not already contain the template arguments).
bc30ff58
JB
8731
8732 For Ada, we need to scan the children of subprograms and lexical
8733 blocks as well because Ada allows the definition of nested
8734 entities that could be interesting for the debugger, such as
8735 nested subprograms for instance. */
72bf9492 8736 if (last_die->has_children
5afb4e99
DJ
8737 && (load_all
8738 || last_die->tag == DW_TAG_namespace
f55ee35c 8739 || last_die->tag == DW_TAG_module
72bf9492 8740 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8741 || (cu->language == language_cplus
8742 && last_die->tag == DW_TAG_subprogram
8743 && (last_die->name == NULL
8744 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8745 || (cu->language != language_c
8746 && (last_die->tag == DW_TAG_class_type
680b30c7 8747 || last_die->tag == DW_TAG_interface_type
72bf9492 8748 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8749 || last_die->tag == DW_TAG_union_type))
8750 || (cu->language == language_ada
8751 && (last_die->tag == DW_TAG_subprogram
8752 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8753 {
8754 nesting_level++;
8755 parent_die = last_die;
8756 continue;
8757 }
8758
8759 /* Otherwise we skip to the next sibling, if any. */
93311388 8760 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8761
8762 /* Back to the top, do it again. */
8763 }
8764}
8765
c906108c
SS
8766/* Read a minimal amount of information into the minimal die structure. */
8767
fe1b8b76 8768static gdb_byte *
72bf9492
DJ
8769read_partial_die (struct partial_die_info *part_die,
8770 struct abbrev_info *abbrev,
8771 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8772 gdb_byte *buffer, gdb_byte *info_ptr,
8773 struct dwarf2_cu *cu)
c906108c 8774{
fa238c03 8775 unsigned int i;
c906108c 8776 struct attribute attr;
c5aa993b 8777 int has_low_pc_attr = 0;
c906108c
SS
8778 int has_high_pc_attr = 0;
8779
72bf9492 8780 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8781
93311388 8782 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8783
8784 info_ptr += abbrev_len;
8785
8786 if (abbrev == NULL)
8787 return info_ptr;
8788
c906108c
SS
8789 part_die->tag = abbrev->tag;
8790 part_die->has_children = abbrev->has_children;
c906108c
SS
8791
8792 for (i = 0; i < abbrev->num_attrs; ++i)
8793 {
e7c27a73 8794 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8795
8796 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8797 partial symbol table. */
c906108c
SS
8798 switch (attr.name)
8799 {
8800 case DW_AT_name:
71c25dea
TT
8801 switch (part_die->tag)
8802 {
8803 case DW_TAG_compile_unit:
348e048f 8804 case DW_TAG_type_unit:
71c25dea
TT
8805 /* Compilation units have a DW_AT_name that is a filename, not
8806 a source language identifier. */
8807 case DW_TAG_enumeration_type:
8808 case DW_TAG_enumerator:
8809 /* These tags always have simple identifiers already; no need
8810 to canonicalize them. */
8811 part_die->name = DW_STRING (&attr);
8812 break;
8813 default:
8814 part_die->name
8815 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8816 &cu->objfile->objfile_obstack);
71c25dea
TT
8817 break;
8818 }
c906108c 8819 break;
31ef98ae 8820 case DW_AT_linkage_name:
c906108c 8821 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8822 /* Note that both forms of linkage name might appear. We
8823 assume they will be the same, and we only store the last
8824 one we see. */
94af9270
KS
8825 if (cu->language == language_ada)
8826 part_die->name = DW_STRING (&attr);
abc72ce4 8827 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8828 break;
8829 case DW_AT_low_pc:
8830 has_low_pc_attr = 1;
8831 part_die->lowpc = DW_ADDR (&attr);
8832 break;
8833 case DW_AT_high_pc:
8834 has_high_pc_attr = 1;
8835 part_die->highpc = DW_ADDR (&attr);
8836 break;
8837 case DW_AT_location:
8e19ed76
PS
8838 /* Support the .debug_loc offsets */
8839 if (attr_form_is_block (&attr))
8840 {
8841 part_die->locdesc = DW_BLOCK (&attr);
8842 }
3690dd37 8843 else if (attr_form_is_section_offset (&attr))
8e19ed76 8844 {
4d3c2250 8845 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8846 }
8847 else
8848 {
4d3c2250
KB
8849 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8850 "partial symbol information");
8e19ed76 8851 }
c906108c 8852 break;
c906108c
SS
8853 case DW_AT_external:
8854 part_die->is_external = DW_UNSND (&attr);
8855 break;
8856 case DW_AT_declaration:
8857 part_die->is_declaration = DW_UNSND (&attr);
8858 break;
8859 case DW_AT_type:
8860 part_die->has_type = 1;
8861 break;
8862 case DW_AT_abstract_origin:
8863 case DW_AT_specification:
72bf9492
DJ
8864 case DW_AT_extension:
8865 part_die->has_specification = 1;
c764a876 8866 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8867 break;
8868 case DW_AT_sibling:
8869 /* Ignore absolute siblings, they might point outside of
8870 the current compile unit. */
8871 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8872 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8873 else
93311388 8874 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8875 break;
fa4028e9
JB
8876 case DW_AT_byte_size:
8877 part_die->has_byte_size = 1;
8878 break;
68511cec
CES
8879 case DW_AT_calling_convention:
8880 /* DWARF doesn't provide a way to identify a program's source-level
8881 entry point. DW_AT_calling_convention attributes are only meant
8882 to describe functions' calling conventions.
8883
8884 However, because it's a necessary piece of information in
8885 Fortran, and because DW_CC_program is the only piece of debugging
8886 information whose definition refers to a 'main program' at all,
8887 several compilers have begun marking Fortran main programs with
8888 DW_CC_program --- even when those functions use the standard
8889 calling conventions.
8890
8891 So until DWARF specifies a way to provide this information and
8892 compilers pick up the new representation, we'll support this
8893 practice. */
8894 if (DW_UNSND (&attr) == DW_CC_program
8895 && cu->language == language_fortran)
01f8c46d
JK
8896 {
8897 set_main_name (part_die->name);
8898
8899 /* As this DIE has a static linkage the name would be difficult
8900 to look up later. */
8901 language_of_main = language_fortran;
8902 }
68511cec 8903 break;
c906108c
SS
8904 default:
8905 break;
8906 }
8907 }
8908
c906108c
SS
8909 /* When using the GNU linker, .gnu.linkonce. sections are used to
8910 eliminate duplicate copies of functions and vtables and such.
8911 The linker will arbitrarily choose one and discard the others.
8912 The AT_*_pc values for such functions refer to local labels in
8913 these sections. If the section from that file was discarded, the
8914 labels are not in the output, so the relocs get a value of 0.
8915 If this is a discarded function, mark the pc bounds as invalid,
8916 so that GDB will ignore it. */
8917 if (has_low_pc_attr && has_high_pc_attr
8918 && part_die->lowpc < part_die->highpc
8919 && (part_die->lowpc != 0
72dca2f5 8920 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8921 part_die->has_pc_info = 1;
85cbf3d3 8922
c906108c
SS
8923 return info_ptr;
8924}
8925
72bf9492
DJ
8926/* Find a cached partial DIE at OFFSET in CU. */
8927
8928static struct partial_die_info *
c764a876 8929find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8930{
8931 struct partial_die_info *lookup_die = NULL;
8932 struct partial_die_info part_die;
8933
8934 part_die.offset = offset;
8935 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8936
72bf9492
DJ
8937 return lookup_die;
8938}
8939
348e048f
DE
8940/* Find a partial DIE at OFFSET, which may or may not be in CU,
8941 except in the case of .debug_types DIEs which do not reference
8942 outside their CU (they do however referencing other types via
8943 DW_FORM_sig8). */
72bf9492
DJ
8944
8945static struct partial_die_info *
c764a876 8946find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8947{
5afb4e99
DJ
8948 struct dwarf2_per_cu_data *per_cu = NULL;
8949 struct partial_die_info *pd = NULL;
72bf9492 8950
348e048f
DE
8951 if (cu->per_cu->from_debug_types)
8952 {
8953 pd = find_partial_die_in_comp_unit (offset, cu);
8954 if (pd != NULL)
8955 return pd;
8956 goto not_found;
8957 }
8958
45452591 8959 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8960 {
8961 pd = find_partial_die_in_comp_unit (offset, cu);
8962 if (pd != NULL)
8963 return pd;
8964 }
72bf9492 8965
ae038cb0
DJ
8966 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8967
98bfdba5
PA
8968 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8969 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8970
8971 per_cu->cu->last_used = 0;
5afb4e99
DJ
8972 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8973
8974 if (pd == NULL && per_cu->load_all_dies == 0)
8975 {
8976 struct cleanup *back_to;
8977 struct partial_die_info comp_unit_die;
8978 struct abbrev_info *abbrev;
8979 unsigned int bytes_read;
8980 char *info_ptr;
8981
8982 per_cu->load_all_dies = 1;
8983
8984 /* Re-read the DIEs. */
8985 back_to = make_cleanup (null_cleanup, 0);
8986 if (per_cu->cu->dwarf2_abbrevs == NULL)
8987 {
8988 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8989 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8990 }
dce234bc 8991 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8992 + per_cu->cu->header.offset
8993 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8994 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8995 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8996 per_cu->cu->objfile->obfd,
8997 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8998 per_cu->cu);
8999 if (comp_unit_die.has_children)
93311388
DE
9000 load_partial_dies (per_cu->cu->objfile->obfd,
9001 dwarf2_per_objfile->info.buffer, info_ptr,
9002 0, per_cu->cu);
5afb4e99
DJ
9003 do_cleanups (back_to);
9004
9005 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9006 }
9007
348e048f
DE
9008 not_found:
9009
5afb4e99
DJ
9010 if (pd == NULL)
9011 internal_error (__FILE__, __LINE__,
c764a876 9012 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
9013 offset, bfd_get_filename (cu->objfile->obfd));
9014 return pd;
72bf9492
DJ
9015}
9016
abc72ce4
DE
9017/* See if we can figure out if the class lives in a namespace. We do
9018 this by looking for a member function; its demangled name will
9019 contain namespace info, if there is any. */
9020
9021static void
9022guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9023 struct dwarf2_cu *cu)
9024{
9025 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9026 what template types look like, because the demangler
9027 frequently doesn't give the same name as the debug info. We
9028 could fix this by only using the demangled name to get the
9029 prefix (but see comment in read_structure_type). */
9030
9031 struct partial_die_info *real_pdi;
9032 struct partial_die_info *child_pdi;
9033
9034 /* If this DIE (this DIE's specification, if any) has a parent, then
9035 we should not do this. We'll prepend the parent's fully qualified
9036 name when we create the partial symbol. */
9037
9038 real_pdi = struct_pdi;
9039 while (real_pdi->has_specification)
9040 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9041
9042 if (real_pdi->die_parent != NULL)
9043 return;
9044
9045 for (child_pdi = struct_pdi->die_child;
9046 child_pdi != NULL;
9047 child_pdi = child_pdi->die_sibling)
9048 {
9049 if (child_pdi->tag == DW_TAG_subprogram
9050 && child_pdi->linkage_name != NULL)
9051 {
9052 char *actual_class_name
9053 = language_class_name_from_physname (cu->language_defn,
9054 child_pdi->linkage_name);
9055 if (actual_class_name != NULL)
9056 {
9057 struct_pdi->name
9058 = obsavestring (actual_class_name,
9059 strlen (actual_class_name),
9060 &cu->objfile->objfile_obstack);
9061 xfree (actual_class_name);
9062 }
9063 break;
9064 }
9065 }
9066}
9067
72bf9492
DJ
9068/* Adjust PART_DIE before generating a symbol for it. This function
9069 may set the is_external flag or change the DIE's name. */
9070
9071static void
9072fixup_partial_die (struct partial_die_info *part_die,
9073 struct dwarf2_cu *cu)
9074{
abc72ce4
DE
9075 /* Once we've fixed up a die, there's no point in doing so again.
9076 This also avoids a memory leak if we were to call
9077 guess_partial_die_structure_name multiple times. */
9078 if (part_die->fixup_called)
9079 return;
9080
72bf9492
DJ
9081 /* If we found a reference attribute and the DIE has no name, try
9082 to find a name in the referred to DIE. */
9083
9084 if (part_die->name == NULL && part_die->has_specification)
9085 {
9086 struct partial_die_info *spec_die;
72bf9492 9087
10b3939b 9088 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9089
10b3939b 9090 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9091
9092 if (spec_die->name)
9093 {
9094 part_die->name = spec_die->name;
9095
9096 /* Copy DW_AT_external attribute if it is set. */
9097 if (spec_die->is_external)
9098 part_die->is_external = spec_die->is_external;
9099 }
9100 }
9101
9102 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9103
9104 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9105 part_die->name = "(anonymous namespace)";
9106
abc72ce4
DE
9107 /* If there is no parent die to provide a namespace, and there are
9108 children, see if we can determine the namespace from their linkage
9109 name.
9110 NOTE: We need to do this even if cu->has_namespace_info != 0.
9111 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9112 if (cu->language == language_cplus
9113 && dwarf2_per_objfile->types.asection != NULL
9114 && part_die->die_parent == NULL
9115 && part_die->has_children
9116 && (part_die->tag == DW_TAG_class_type
9117 || part_die->tag == DW_TAG_structure_type
9118 || part_die->tag == DW_TAG_union_type))
9119 guess_partial_die_structure_name (part_die, cu);
9120
9121 part_die->fixup_called = 1;
72bf9492
DJ
9122}
9123
a8329558 9124/* Read an attribute value described by an attribute form. */
c906108c 9125
fe1b8b76 9126static gdb_byte *
a8329558 9127read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9128 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9129 struct dwarf2_cu *cu)
c906108c 9130{
e7c27a73 9131 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9132 unsigned int bytes_read;
9133 struct dwarf_block *blk;
9134
a8329558
KW
9135 attr->form = form;
9136 switch (form)
c906108c 9137 {
c906108c 9138 case DW_FORM_ref_addr:
ae411497
TT
9139 if (cu->header.version == 2)
9140 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9141 else
9142 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9143 info_ptr += bytes_read;
9144 break;
9145 case DW_FORM_addr:
e7c27a73 9146 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9147 info_ptr += bytes_read;
c906108c
SS
9148 break;
9149 case DW_FORM_block2:
7b5a2f43 9150 blk = dwarf_alloc_block (cu);
c906108c
SS
9151 blk->size = read_2_bytes (abfd, info_ptr);
9152 info_ptr += 2;
9153 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9154 info_ptr += blk->size;
9155 DW_BLOCK (attr) = blk;
9156 break;
9157 case DW_FORM_block4:
7b5a2f43 9158 blk = dwarf_alloc_block (cu);
c906108c
SS
9159 blk->size = read_4_bytes (abfd, info_ptr);
9160 info_ptr += 4;
9161 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9162 info_ptr += blk->size;
9163 DW_BLOCK (attr) = blk;
9164 break;
9165 case DW_FORM_data2:
9166 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9167 info_ptr += 2;
9168 break;
9169 case DW_FORM_data4:
9170 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9171 info_ptr += 4;
9172 break;
9173 case DW_FORM_data8:
9174 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9175 info_ptr += 8;
9176 break;
2dc7f7b3
TT
9177 case DW_FORM_sec_offset:
9178 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9179 info_ptr += bytes_read;
9180 break;
c906108c 9181 case DW_FORM_string:
9b1c24c8 9182 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9183 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9184 info_ptr += bytes_read;
9185 break;
4bdf3d34
JJ
9186 case DW_FORM_strp:
9187 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9188 &bytes_read);
8285870a 9189 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9190 info_ptr += bytes_read;
9191 break;
2dc7f7b3 9192 case DW_FORM_exprloc:
c906108c 9193 case DW_FORM_block:
7b5a2f43 9194 blk = dwarf_alloc_block (cu);
c906108c
SS
9195 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9196 info_ptr += bytes_read;
9197 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9198 info_ptr += blk->size;
9199 DW_BLOCK (attr) = blk;
9200 break;
9201 case DW_FORM_block1:
7b5a2f43 9202 blk = dwarf_alloc_block (cu);
c906108c
SS
9203 blk->size = read_1_byte (abfd, info_ptr);
9204 info_ptr += 1;
9205 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9206 info_ptr += blk->size;
9207 DW_BLOCK (attr) = blk;
9208 break;
9209 case DW_FORM_data1:
9210 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9211 info_ptr += 1;
9212 break;
9213 case DW_FORM_flag:
9214 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9215 info_ptr += 1;
9216 break;
2dc7f7b3
TT
9217 case DW_FORM_flag_present:
9218 DW_UNSND (attr) = 1;
9219 break;
c906108c
SS
9220 case DW_FORM_sdata:
9221 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9222 info_ptr += bytes_read;
9223 break;
9224 case DW_FORM_udata:
9225 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9226 info_ptr += bytes_read;
9227 break;
9228 case DW_FORM_ref1:
10b3939b 9229 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9230 info_ptr += 1;
9231 break;
9232 case DW_FORM_ref2:
10b3939b 9233 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9234 info_ptr += 2;
9235 break;
9236 case DW_FORM_ref4:
10b3939b 9237 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9238 info_ptr += 4;
9239 break;
613e1657 9240 case DW_FORM_ref8:
10b3939b 9241 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9242 info_ptr += 8;
9243 break;
348e048f
DE
9244 case DW_FORM_sig8:
9245 /* Convert the signature to something we can record in DW_UNSND
9246 for later lookup.
9247 NOTE: This is NULL if the type wasn't found. */
9248 DW_SIGNATURED_TYPE (attr) =
9249 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9250 info_ptr += 8;
9251 break;
c906108c 9252 case DW_FORM_ref_udata:
10b3939b
DJ
9253 DW_ADDR (attr) = (cu->header.offset
9254 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9255 info_ptr += bytes_read;
9256 break;
c906108c 9257 case DW_FORM_indirect:
a8329558
KW
9258 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9259 info_ptr += bytes_read;
e7c27a73 9260 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9261 break;
c906108c 9262 default:
8a3fe4f8 9263 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9264 dwarf_form_name (form),
9265 bfd_get_filename (abfd));
c906108c 9266 }
28e94949
JB
9267
9268 /* We have seen instances where the compiler tried to emit a byte
9269 size attribute of -1 which ended up being encoded as an unsigned
9270 0xffffffff. Although 0xffffffff is technically a valid size value,
9271 an object of this size seems pretty unlikely so we can relatively
9272 safely treat these cases as if the size attribute was invalid and
9273 treat them as zero by default. */
9274 if (attr->name == DW_AT_byte_size
9275 && form == DW_FORM_data4
9276 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9277 {
9278 complaint
9279 (&symfile_complaints,
43bbcdc2
PH
9280 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9281 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9282 DW_UNSND (attr) = 0;
9283 }
28e94949 9284
c906108c
SS
9285 return info_ptr;
9286}
9287
a8329558
KW
9288/* Read an attribute described by an abbreviated attribute. */
9289
fe1b8b76 9290static gdb_byte *
a8329558 9291read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9292 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9293{
9294 attr->name = abbrev->name;
e7c27a73 9295 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9296}
9297
c906108c
SS
9298/* read dwarf information from a buffer */
9299
9300static unsigned int
fe1b8b76 9301read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9302{
fe1b8b76 9303 return bfd_get_8 (abfd, buf);
c906108c
SS
9304}
9305
9306static int
fe1b8b76 9307read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9308{
fe1b8b76 9309 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9310}
9311
9312static unsigned int
fe1b8b76 9313read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9314{
fe1b8b76 9315 return bfd_get_16 (abfd, buf);
c906108c
SS
9316}
9317
9318static int
fe1b8b76 9319read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9320{
fe1b8b76 9321 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9322}
9323
9324static unsigned int
fe1b8b76 9325read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9326{
fe1b8b76 9327 return bfd_get_32 (abfd, buf);
c906108c
SS
9328}
9329
9330static int
fe1b8b76 9331read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9332{
fe1b8b76 9333 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9334}
9335
93311388 9336static ULONGEST
fe1b8b76 9337read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9338{
fe1b8b76 9339 return bfd_get_64 (abfd, buf);
c906108c
SS
9340}
9341
9342static CORE_ADDR
fe1b8b76 9343read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9344 unsigned int *bytes_read)
c906108c 9345{
e7c27a73 9346 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9347 CORE_ADDR retval = 0;
9348
107d2387 9349 if (cu_header->signed_addr_p)
c906108c 9350 {
107d2387
AC
9351 switch (cu_header->addr_size)
9352 {
9353 case 2:
fe1b8b76 9354 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9355 break;
9356 case 4:
fe1b8b76 9357 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9358 break;
9359 case 8:
fe1b8b76 9360 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9361 break;
9362 default:
8e65ff28 9363 internal_error (__FILE__, __LINE__,
e2e0b3e5 9364 _("read_address: bad switch, signed [in module %s]"),
659b0389 9365 bfd_get_filename (abfd));
107d2387
AC
9366 }
9367 }
9368 else
9369 {
9370 switch (cu_header->addr_size)
9371 {
9372 case 2:
fe1b8b76 9373 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9374 break;
9375 case 4:
fe1b8b76 9376 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9377 break;
9378 case 8:
fe1b8b76 9379 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9380 break;
9381 default:
8e65ff28 9382 internal_error (__FILE__, __LINE__,
e2e0b3e5 9383 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9384 bfd_get_filename (abfd));
107d2387 9385 }
c906108c 9386 }
64367e0a 9387
107d2387
AC
9388 *bytes_read = cu_header->addr_size;
9389 return retval;
c906108c
SS
9390}
9391
f7ef9339 9392/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9393 specification allows the initial length to take up either 4 bytes
9394 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9395 bytes describe the length and all offsets will be 8 bytes in length
9396 instead of 4.
9397
f7ef9339
KB
9398 An older, non-standard 64-bit format is also handled by this
9399 function. The older format in question stores the initial length
9400 as an 8-byte quantity without an escape value. Lengths greater
9401 than 2^32 aren't very common which means that the initial 4 bytes
9402 is almost always zero. Since a length value of zero doesn't make
9403 sense for the 32-bit format, this initial zero can be considered to
9404 be an escape value which indicates the presence of the older 64-bit
9405 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9406 greater than 4GB. If it becomes necessary to handle lengths
9407 somewhat larger than 4GB, we could allow other small values (such
9408 as the non-sensical values of 1, 2, and 3) to also be used as
9409 escape values indicating the presence of the old format.
f7ef9339 9410
917c78fc
MK
9411 The value returned via bytes_read should be used to increment the
9412 relevant pointer after calling read_initial_length().
c764a876 9413
613e1657
KB
9414 [ Note: read_initial_length() and read_offset() are based on the
9415 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9416 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9417 from:
9418
f7ef9339 9419 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9420
613e1657
KB
9421 This document is only a draft and is subject to change. (So beware.)
9422
f7ef9339 9423 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9424 determined empirically by examining 64-bit ELF files produced by
9425 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9426
9427 - Kevin, July 16, 2002
613e1657
KB
9428 ] */
9429
9430static LONGEST
c764a876 9431read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9432{
fe1b8b76 9433 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9434
dd373385 9435 if (length == 0xffffffff)
613e1657 9436 {
fe1b8b76 9437 length = bfd_get_64 (abfd, buf + 4);
613e1657 9438 *bytes_read = 12;
613e1657 9439 }
dd373385 9440 else if (length == 0)
f7ef9339 9441 {
dd373385 9442 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9443 length = bfd_get_64 (abfd, buf);
f7ef9339 9444 *bytes_read = 8;
f7ef9339 9445 }
613e1657
KB
9446 else
9447 {
9448 *bytes_read = 4;
613e1657
KB
9449 }
9450
c764a876
DE
9451 return length;
9452}
dd373385 9453
c764a876
DE
9454/* Cover function for read_initial_length.
9455 Returns the length of the object at BUF, and stores the size of the
9456 initial length in *BYTES_READ and stores the size that offsets will be in
9457 *OFFSET_SIZE.
9458 If the initial length size is not equivalent to that specified in
9459 CU_HEADER then issue a complaint.
9460 This is useful when reading non-comp-unit headers. */
dd373385 9461
c764a876
DE
9462static LONGEST
9463read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9464 const struct comp_unit_head *cu_header,
9465 unsigned int *bytes_read,
9466 unsigned int *offset_size)
9467{
9468 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9469
9470 gdb_assert (cu_header->initial_length_size == 4
9471 || cu_header->initial_length_size == 8
9472 || cu_header->initial_length_size == 12);
9473
9474 if (cu_header->initial_length_size != *bytes_read)
9475 complaint (&symfile_complaints,
9476 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9477
c764a876 9478 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9479 return length;
613e1657
KB
9480}
9481
9482/* Read an offset from the data stream. The size of the offset is
917c78fc 9483 given by cu_header->offset_size. */
613e1657
KB
9484
9485static LONGEST
fe1b8b76 9486read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9487 unsigned int *bytes_read)
c764a876
DE
9488{
9489 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9490
c764a876
DE
9491 *bytes_read = cu_header->offset_size;
9492 return offset;
9493}
9494
9495/* Read an offset from the data stream. */
9496
9497static LONGEST
9498read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9499{
9500 LONGEST retval = 0;
9501
c764a876 9502 switch (offset_size)
613e1657
KB
9503 {
9504 case 4:
fe1b8b76 9505 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9506 break;
9507 case 8:
fe1b8b76 9508 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9509 break;
9510 default:
8e65ff28 9511 internal_error (__FILE__, __LINE__,
c764a876 9512 _("read_offset_1: bad switch [in module %s]"),
659b0389 9513 bfd_get_filename (abfd));
613e1657
KB
9514 }
9515
917c78fc 9516 return retval;
613e1657
KB
9517}
9518
fe1b8b76
JB
9519static gdb_byte *
9520read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9521{
9522 /* If the size of a host char is 8 bits, we can return a pointer
9523 to the buffer, otherwise we have to copy the data to a buffer
9524 allocated on the temporary obstack. */
4bdf3d34 9525 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9526 return buf;
c906108c
SS
9527}
9528
9529static char *
9b1c24c8 9530read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9531{
9532 /* If the size of a host char is 8 bits, we can return a pointer
9533 to the string, otherwise we have to copy the string to a buffer
9534 allocated on the temporary obstack. */
4bdf3d34 9535 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9536 if (*buf == '\0')
9537 {
9538 *bytes_read_ptr = 1;
9539 return NULL;
9540 }
fe1b8b76
JB
9541 *bytes_read_ptr = strlen ((char *) buf) + 1;
9542 return (char *) buf;
4bdf3d34
JJ
9543}
9544
9545static char *
fe1b8b76 9546read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9547 const struct comp_unit_head *cu_header,
9548 unsigned int *bytes_read_ptr)
9549{
c764a876 9550 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9551
be391dca 9552 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9553 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9554 {
8a3fe4f8 9555 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9556 bfd_get_filename (abfd));
4bdf3d34 9557 return NULL;
c906108c 9558 }
dce234bc 9559 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9560 {
8a3fe4f8 9561 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9562 bfd_get_filename (abfd));
c906108c
SS
9563 return NULL;
9564 }
4bdf3d34 9565 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9566 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9567 return NULL;
dce234bc 9568 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9569}
9570
ce5d95e1 9571static unsigned long
fe1b8b76 9572read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9573{
ce5d95e1
JB
9574 unsigned long result;
9575 unsigned int num_read;
c906108c
SS
9576 int i, shift;
9577 unsigned char byte;
9578
9579 result = 0;
9580 shift = 0;
9581 num_read = 0;
9582 i = 0;
9583 while (1)
9584 {
fe1b8b76 9585 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9586 buf++;
9587 num_read++;
ce5d95e1 9588 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9589 if ((byte & 128) == 0)
9590 {
9591 break;
9592 }
9593 shift += 7;
9594 }
9595 *bytes_read_ptr = num_read;
9596 return result;
9597}
9598
ce5d95e1 9599static long
fe1b8b76 9600read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9601{
ce5d95e1 9602 long result;
77e0b926 9603 int i, shift, num_read;
c906108c
SS
9604 unsigned char byte;
9605
9606 result = 0;
9607 shift = 0;
c906108c
SS
9608 num_read = 0;
9609 i = 0;
9610 while (1)
9611 {
fe1b8b76 9612 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9613 buf++;
9614 num_read++;
ce5d95e1 9615 result |= ((long)(byte & 127) << shift);
c906108c
SS
9616 shift += 7;
9617 if ((byte & 128) == 0)
9618 {
9619 break;
9620 }
9621 }
77e0b926
DJ
9622 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9623 result |= -(((long)1) << shift);
c906108c
SS
9624 *bytes_read_ptr = num_read;
9625 return result;
9626}
9627
4bb7a0a7
DJ
9628/* Return a pointer to just past the end of an LEB128 number in BUF. */
9629
fe1b8b76
JB
9630static gdb_byte *
9631skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9632{
9633 int byte;
9634
9635 while (1)
9636 {
fe1b8b76 9637 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9638 buf++;
9639 if ((byte & 128) == 0)
9640 return buf;
9641 }
9642}
9643
c906108c 9644static void
e142c38c 9645set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9646{
9647 switch (lang)
9648 {
9649 case DW_LANG_C89:
76bee0cc 9650 case DW_LANG_C99:
c906108c 9651 case DW_LANG_C:
e142c38c 9652 cu->language = language_c;
c906108c
SS
9653 break;
9654 case DW_LANG_C_plus_plus:
e142c38c 9655 cu->language = language_cplus;
c906108c 9656 break;
6aecb9c2
JB
9657 case DW_LANG_D:
9658 cu->language = language_d;
9659 break;
c906108c
SS
9660 case DW_LANG_Fortran77:
9661 case DW_LANG_Fortran90:
b21b22e0 9662 case DW_LANG_Fortran95:
e142c38c 9663 cu->language = language_fortran;
c906108c
SS
9664 break;
9665 case DW_LANG_Mips_Assembler:
e142c38c 9666 cu->language = language_asm;
c906108c 9667 break;
bebd888e 9668 case DW_LANG_Java:
e142c38c 9669 cu->language = language_java;
bebd888e 9670 break;
c906108c 9671 case DW_LANG_Ada83:
8aaf0b47 9672 case DW_LANG_Ada95:
bc5f45f8
JB
9673 cu->language = language_ada;
9674 break;
72019c9c
GM
9675 case DW_LANG_Modula2:
9676 cu->language = language_m2;
9677 break;
fe8e67fd
PM
9678 case DW_LANG_Pascal83:
9679 cu->language = language_pascal;
9680 break;
22566fbd
DJ
9681 case DW_LANG_ObjC:
9682 cu->language = language_objc;
9683 break;
c906108c
SS
9684 case DW_LANG_Cobol74:
9685 case DW_LANG_Cobol85:
c906108c 9686 default:
e142c38c 9687 cu->language = language_minimal;
c906108c
SS
9688 break;
9689 }
e142c38c 9690 cu->language_defn = language_def (cu->language);
c906108c
SS
9691}
9692
9693/* Return the named attribute or NULL if not there. */
9694
9695static struct attribute *
e142c38c 9696dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9697{
9698 unsigned int i;
9699 struct attribute *spec = NULL;
9700
9701 for (i = 0; i < die->num_attrs; ++i)
9702 {
9703 if (die->attrs[i].name == name)
10b3939b 9704 return &die->attrs[i];
c906108c
SS
9705 if (die->attrs[i].name == DW_AT_specification
9706 || die->attrs[i].name == DW_AT_abstract_origin)
9707 spec = &die->attrs[i];
9708 }
c906108c 9709
10b3939b 9710 if (spec)
f2f0e013
DJ
9711 {
9712 die = follow_die_ref (die, spec, &cu);
9713 return dwarf2_attr (die, name, cu);
9714 }
c5aa993b 9715
c906108c
SS
9716 return NULL;
9717}
9718
348e048f
DE
9719/* Return the named attribute or NULL if not there,
9720 but do not follow DW_AT_specification, etc.
9721 This is for use in contexts where we're reading .debug_types dies.
9722 Following DW_AT_specification, DW_AT_abstract_origin will take us
9723 back up the chain, and we want to go down. */
9724
9725static struct attribute *
9726dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9727 struct dwarf2_cu *cu)
9728{
9729 unsigned int i;
9730
9731 for (i = 0; i < die->num_attrs; ++i)
9732 if (die->attrs[i].name == name)
9733 return &die->attrs[i];
9734
9735 return NULL;
9736}
9737
05cf31d1
JB
9738/* Return non-zero iff the attribute NAME is defined for the given DIE,
9739 and holds a non-zero value. This function should only be used for
2dc7f7b3 9740 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9741
9742static int
9743dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9744{
9745 struct attribute *attr = dwarf2_attr (die, name, cu);
9746
9747 return (attr && DW_UNSND (attr));
9748}
9749
3ca72b44 9750static int
e142c38c 9751die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9752{
05cf31d1
JB
9753 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9754 which value is non-zero. However, we have to be careful with
9755 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9756 (via dwarf2_flag_true_p) follows this attribute. So we may
9757 end up accidently finding a declaration attribute that belongs
9758 to a different DIE referenced by the specification attribute,
9759 even though the given DIE does not have a declaration attribute. */
9760 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9761 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9762}
9763
63d06c5c 9764/* Return the die giving the specification for DIE, if there is
f2f0e013 9765 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9766 containing the return value on output. If there is no
9767 specification, but there is an abstract origin, that is
9768 returned. */
63d06c5c
DC
9769
9770static struct die_info *
f2f0e013 9771die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9772{
f2f0e013
DJ
9773 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9774 *spec_cu);
63d06c5c 9775
edb3359d
DJ
9776 if (spec_attr == NULL)
9777 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9778
63d06c5c
DC
9779 if (spec_attr == NULL)
9780 return NULL;
9781 else
f2f0e013 9782 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9783}
c906108c 9784
debd256d
JB
9785/* Free the line_header structure *LH, and any arrays and strings it
9786 refers to. */
9787static void
9788free_line_header (struct line_header *lh)
9789{
9790 if (lh->standard_opcode_lengths)
a8bc7b56 9791 xfree (lh->standard_opcode_lengths);
debd256d
JB
9792
9793 /* Remember that all the lh->file_names[i].name pointers are
9794 pointers into debug_line_buffer, and don't need to be freed. */
9795 if (lh->file_names)
a8bc7b56 9796 xfree (lh->file_names);
debd256d
JB
9797
9798 /* Similarly for the include directory names. */
9799 if (lh->include_dirs)
a8bc7b56 9800 xfree (lh->include_dirs);
debd256d 9801
a8bc7b56 9802 xfree (lh);
debd256d
JB
9803}
9804
9805
9806/* Add an entry to LH's include directory table. */
9807static void
9808add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9809{
debd256d
JB
9810 /* Grow the array if necessary. */
9811 if (lh->include_dirs_size == 0)
c5aa993b 9812 {
debd256d
JB
9813 lh->include_dirs_size = 1; /* for testing */
9814 lh->include_dirs = xmalloc (lh->include_dirs_size
9815 * sizeof (*lh->include_dirs));
9816 }
9817 else if (lh->num_include_dirs >= lh->include_dirs_size)
9818 {
9819 lh->include_dirs_size *= 2;
9820 lh->include_dirs = xrealloc (lh->include_dirs,
9821 (lh->include_dirs_size
9822 * sizeof (*lh->include_dirs)));
c5aa993b 9823 }
c906108c 9824
debd256d
JB
9825 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9826}
6e70227d 9827
debd256d
JB
9828
9829/* Add an entry to LH's file name table. */
9830static void
9831add_file_name (struct line_header *lh,
9832 char *name,
9833 unsigned int dir_index,
9834 unsigned int mod_time,
9835 unsigned int length)
9836{
9837 struct file_entry *fe;
9838
9839 /* Grow the array if necessary. */
9840 if (lh->file_names_size == 0)
9841 {
9842 lh->file_names_size = 1; /* for testing */
9843 lh->file_names = xmalloc (lh->file_names_size
9844 * sizeof (*lh->file_names));
9845 }
9846 else if (lh->num_file_names >= lh->file_names_size)
9847 {
9848 lh->file_names_size *= 2;
9849 lh->file_names = xrealloc (lh->file_names,
9850 (lh->file_names_size
9851 * sizeof (*lh->file_names)));
9852 }
9853
9854 fe = &lh->file_names[lh->num_file_names++];
9855 fe->name = name;
9856 fe->dir_index = dir_index;
9857 fe->mod_time = mod_time;
9858 fe->length = length;
aaa75496 9859 fe->included_p = 0;
cb1df416 9860 fe->symtab = NULL;
debd256d 9861}
6e70227d 9862
debd256d
JB
9863
9864/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9865 .debug_line, according to the endianness of ABFD. Return a pointer
9866 to a struct line_header, allocated using xmalloc.
debd256d
JB
9867
9868 NOTE: the strings in the include directory and file name tables of
9869 the returned object point into debug_line_buffer, and must not be
9870 freed. */
9871static struct line_header *
9872dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9873 struct dwarf2_cu *cu)
debd256d
JB
9874{
9875 struct cleanup *back_to;
9876 struct line_header *lh;
fe1b8b76 9877 gdb_byte *line_ptr;
c764a876 9878 unsigned int bytes_read, offset_size;
debd256d
JB
9879 int i;
9880 char *cur_dir, *cur_file;
9881
be391dca 9882 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9883 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9884 {
e2e0b3e5 9885 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9886 return 0;
9887 }
9888
a738430d
MK
9889 /* Make sure that at least there's room for the total_length field.
9890 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9891 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9892 {
4d3c2250 9893 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9894 return 0;
9895 }
9896
9897 lh = xmalloc (sizeof (*lh));
9898 memset (lh, 0, sizeof (*lh));
9899 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9900 (void *) lh);
9901
dce234bc 9902 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9903
a738430d 9904 /* Read in the header. */
6e70227d 9905 lh->total_length =
c764a876
DE
9906 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9907 &bytes_read, &offset_size);
debd256d 9908 line_ptr += bytes_read;
dce234bc
PP
9909 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9910 + dwarf2_per_objfile->line.size))
debd256d 9911 {
4d3c2250 9912 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9913 return 0;
9914 }
9915 lh->statement_program_end = line_ptr + lh->total_length;
9916 lh->version = read_2_bytes (abfd, line_ptr);
9917 line_ptr += 2;
c764a876
DE
9918 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9919 line_ptr += offset_size;
debd256d
JB
9920 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9921 line_ptr += 1;
2dc7f7b3
TT
9922 if (lh->version >= 4)
9923 {
9924 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9925 line_ptr += 1;
9926 }
9927 else
9928 lh->maximum_ops_per_instruction = 1;
9929
9930 if (lh->maximum_ops_per_instruction == 0)
9931 {
9932 lh->maximum_ops_per_instruction = 1;
9933 complaint (&symfile_complaints,
9934 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9935 }
9936
debd256d
JB
9937 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9938 line_ptr += 1;
9939 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9940 line_ptr += 1;
9941 lh->line_range = read_1_byte (abfd, line_ptr);
9942 line_ptr += 1;
9943 lh->opcode_base = read_1_byte (abfd, line_ptr);
9944 line_ptr += 1;
9945 lh->standard_opcode_lengths
fe1b8b76 9946 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9947
9948 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9949 for (i = 1; i < lh->opcode_base; ++i)
9950 {
9951 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9952 line_ptr += 1;
9953 }
9954
a738430d 9955 /* Read directory table. */
9b1c24c8 9956 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9957 {
9958 line_ptr += bytes_read;
9959 add_include_dir (lh, cur_dir);
9960 }
9961 line_ptr += bytes_read;
9962
a738430d 9963 /* Read file name table. */
9b1c24c8 9964 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9965 {
9966 unsigned int dir_index, mod_time, length;
9967
9968 line_ptr += bytes_read;
9969 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9970 line_ptr += bytes_read;
9971 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9972 line_ptr += bytes_read;
9973 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9974 line_ptr += bytes_read;
9975
9976 add_file_name (lh, cur_file, dir_index, mod_time, length);
9977 }
9978 line_ptr += bytes_read;
6e70227d 9979 lh->statement_program_start = line_ptr;
debd256d 9980
dce234bc
PP
9981 if (line_ptr > (dwarf2_per_objfile->line.buffer
9982 + dwarf2_per_objfile->line.size))
4d3c2250 9983 complaint (&symfile_complaints,
e2e0b3e5 9984 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9985
9986 discard_cleanups (back_to);
9987 return lh;
9988}
c906108c 9989
5fb290d7
DJ
9990/* This function exists to work around a bug in certain compilers
9991 (particularly GCC 2.95), in which the first line number marker of a
9992 function does not show up until after the prologue, right before
9993 the second line number marker. This function shifts ADDRESS down
9994 to the beginning of the function if necessary, and is called on
9995 addresses passed to record_line. */
9996
9997static CORE_ADDR
e142c38c 9998check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9999{
10000 struct function_range *fn;
10001
10002 /* Find the function_range containing address. */
e142c38c 10003 if (!cu->first_fn)
5fb290d7
DJ
10004 return address;
10005
e142c38c
DJ
10006 if (!cu->cached_fn)
10007 cu->cached_fn = cu->first_fn;
5fb290d7 10008
e142c38c 10009 fn = cu->cached_fn;
5fb290d7
DJ
10010 while (fn)
10011 if (fn->lowpc <= address && fn->highpc > address)
10012 goto found;
10013 else
10014 fn = fn->next;
10015
e142c38c
DJ
10016 fn = cu->first_fn;
10017 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10018 if (fn->lowpc <= address && fn->highpc > address)
10019 goto found;
10020 else
10021 fn = fn->next;
10022
10023 return address;
10024
10025 found:
10026 if (fn->seen_line)
10027 return address;
10028 if (address != fn->lowpc)
4d3c2250 10029 complaint (&symfile_complaints,
e2e0b3e5 10030 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10031 (unsigned long) address, fn->name);
5fb290d7
DJ
10032 fn->seen_line = 1;
10033 return fn->lowpc;
10034}
10035
c6da4cef
DE
10036/* Subroutine of dwarf_decode_lines to simplify it.
10037 Return the file name of the psymtab for included file FILE_INDEX
10038 in line header LH of PST.
10039 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10040 If space for the result is malloc'd, it will be freed by a cleanup.
10041 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10042
10043static char *
10044psymtab_include_file_name (const struct line_header *lh, int file_index,
10045 const struct partial_symtab *pst,
10046 const char *comp_dir)
10047{
10048 const struct file_entry fe = lh->file_names [file_index];
10049 char *include_name = fe.name;
10050 char *include_name_to_compare = include_name;
10051 char *dir_name = NULL;
72b9f47f
TT
10052 const char *pst_filename;
10053 char *copied_name = NULL;
c6da4cef
DE
10054 int file_is_pst;
10055
10056 if (fe.dir_index)
10057 dir_name = lh->include_dirs[fe.dir_index - 1];
10058
10059 if (!IS_ABSOLUTE_PATH (include_name)
10060 && (dir_name != NULL || comp_dir != NULL))
10061 {
10062 /* Avoid creating a duplicate psymtab for PST.
10063 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10064 Before we do the comparison, however, we need to account
10065 for DIR_NAME and COMP_DIR.
10066 First prepend dir_name (if non-NULL). If we still don't
10067 have an absolute path prepend comp_dir (if non-NULL).
10068 However, the directory we record in the include-file's
10069 psymtab does not contain COMP_DIR (to match the
10070 corresponding symtab(s)).
10071
10072 Example:
10073
10074 bash$ cd /tmp
10075 bash$ gcc -g ./hello.c
10076 include_name = "hello.c"
10077 dir_name = "."
10078 DW_AT_comp_dir = comp_dir = "/tmp"
10079 DW_AT_name = "./hello.c" */
10080
10081 if (dir_name != NULL)
10082 {
10083 include_name = concat (dir_name, SLASH_STRING,
10084 include_name, (char *)NULL);
10085 include_name_to_compare = include_name;
10086 make_cleanup (xfree, include_name);
10087 }
10088 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10089 {
10090 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10091 include_name, (char *)NULL);
10092 }
10093 }
10094
10095 pst_filename = pst->filename;
10096 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10097 {
72b9f47f
TT
10098 copied_name = concat (pst->dirname, SLASH_STRING,
10099 pst_filename, (char *)NULL);
10100 pst_filename = copied_name;
c6da4cef
DE
10101 }
10102
1e3fad37 10103 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10104
10105 if (include_name_to_compare != include_name)
10106 xfree (include_name_to_compare);
72b9f47f
TT
10107 if (copied_name != NULL)
10108 xfree (copied_name);
c6da4cef
DE
10109
10110 if (file_is_pst)
10111 return NULL;
10112 return include_name;
10113}
10114
aaa75496
JB
10115/* Decode the Line Number Program (LNP) for the given line_header
10116 structure and CU. The actual information extracted and the type
10117 of structures created from the LNP depends on the value of PST.
10118
10119 1. If PST is NULL, then this procedure uses the data from the program
10120 to create all necessary symbol tables, and their linetables.
6e70227d 10121
aaa75496
JB
10122 2. If PST is not NULL, this procedure reads the program to determine
10123 the list of files included by the unit represented by PST, and
c6da4cef
DE
10124 builds all the associated partial symbol tables.
10125
10126 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10127 It is used for relative paths in the line table.
10128 NOTE: When processing partial symtabs (pst != NULL),
10129 comp_dir == pst->dirname.
10130
10131 NOTE: It is important that psymtabs have the same file name (via strcmp)
10132 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10133 symtab we don't use it in the name of the psymtabs we create.
10134 E.g. expand_line_sal requires this when finding psymtabs to expand.
10135 A good testcase for this is mb-inline.exp. */
debd256d 10136
c906108c 10137static void
72b9f47f 10138dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10139 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10140{
a8c50c1f 10141 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10142 gdb_byte *line_end;
a8c50c1f 10143 unsigned int bytes_read, extended_len;
c906108c 10144 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10145 CORE_ADDR baseaddr;
10146 struct objfile *objfile = cu->objfile;
fbf65064 10147 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10148 const int decode_for_pst_p = (pst != NULL);
cb1df416 10149 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10150
10151 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10152
debd256d
JB
10153 line_ptr = lh->statement_program_start;
10154 line_end = lh->statement_program_end;
c906108c
SS
10155
10156 /* Read the statement sequences until there's nothing left. */
10157 while (line_ptr < line_end)
10158 {
10159 /* state machine registers */
10160 CORE_ADDR address = 0;
10161 unsigned int file = 1;
10162 unsigned int line = 1;
10163 unsigned int column = 0;
debd256d 10164 int is_stmt = lh->default_is_stmt;
c906108c
SS
10165 int basic_block = 0;
10166 int end_sequence = 0;
fbf65064 10167 CORE_ADDR addr;
2dc7f7b3 10168 unsigned char op_index = 0;
c906108c 10169
aaa75496 10170 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10171 {
aaa75496 10172 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10173 /* lh->include_dirs and lh->file_names are 0-based, but the
10174 directory and file name numbers in the statement program
10175 are 1-based. */
10176 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10177 char *dir = NULL;
a738430d 10178
debd256d
JB
10179 if (fe->dir_index)
10180 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10181
10182 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10183 }
10184
a738430d 10185 /* Decode the table. */
c5aa993b 10186 while (!end_sequence)
c906108c
SS
10187 {
10188 op_code = read_1_byte (abfd, line_ptr);
10189 line_ptr += 1;
59205f5a
JB
10190 if (line_ptr > line_end)
10191 {
10192 dwarf2_debug_line_missing_end_sequence_complaint ();
10193 break;
10194 }
9aa1fe7e 10195
debd256d 10196 if (op_code >= lh->opcode_base)
6e70227d 10197 {
a738430d 10198 /* Special operand. */
debd256d 10199 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10200 address += (((op_index + (adj_opcode / lh->line_range))
10201 / lh->maximum_ops_per_instruction)
10202 * lh->minimum_instruction_length);
10203 op_index = ((op_index + (adj_opcode / lh->line_range))
10204 % lh->maximum_ops_per_instruction);
debd256d 10205 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10206 if (lh->num_file_names < file || file == 0)
25e43795 10207 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10208 /* For now we ignore lines not starting on an
10209 instruction boundary. */
10210 else if (op_index == 0)
25e43795
DJ
10211 {
10212 lh->file_names[file - 1].included_p = 1;
ca5f395d 10213 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10214 {
10215 if (last_subfile != current_subfile)
10216 {
10217 addr = gdbarch_addr_bits_remove (gdbarch, address);
10218 if (last_subfile)
10219 record_line (last_subfile, 0, addr);
10220 last_subfile = current_subfile;
10221 }
25e43795 10222 /* Append row to matrix using current values. */
fbf65064
UW
10223 addr = check_cu_functions (address, cu);
10224 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10225 record_line (current_subfile, line, addr);
366da635 10226 }
25e43795 10227 }
ca5f395d 10228 basic_block = 0;
9aa1fe7e
GK
10229 }
10230 else switch (op_code)
c906108c
SS
10231 {
10232 case DW_LNS_extended_op:
a8c50c1f 10233 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10234 line_ptr += bytes_read;
a8c50c1f 10235 extended_end = line_ptr + extended_len;
c906108c
SS
10236 extended_op = read_1_byte (abfd, line_ptr);
10237 line_ptr += 1;
10238 switch (extended_op)
10239 {
10240 case DW_LNE_end_sequence:
10241 end_sequence = 1;
c906108c
SS
10242 break;
10243 case DW_LNE_set_address:
e7c27a73 10244 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10245 op_index = 0;
107d2387
AC
10246 line_ptr += bytes_read;
10247 address += baseaddr;
c906108c
SS
10248 break;
10249 case DW_LNE_define_file:
debd256d
JB
10250 {
10251 char *cur_file;
10252 unsigned int dir_index, mod_time, length;
6e70227d 10253
9b1c24c8 10254 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10255 line_ptr += bytes_read;
10256 dir_index =
10257 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10258 line_ptr += bytes_read;
10259 mod_time =
10260 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10261 line_ptr += bytes_read;
10262 length =
10263 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10264 line_ptr += bytes_read;
10265 add_file_name (lh, cur_file, dir_index, mod_time, length);
10266 }
c906108c 10267 break;
d0c6ba3d
CC
10268 case DW_LNE_set_discriminator:
10269 /* The discriminator is not interesting to the debugger;
10270 just ignore it. */
10271 line_ptr = extended_end;
10272 break;
c906108c 10273 default:
4d3c2250 10274 complaint (&symfile_complaints,
e2e0b3e5 10275 _("mangled .debug_line section"));
debd256d 10276 return;
c906108c 10277 }
a8c50c1f
DJ
10278 /* Make sure that we parsed the extended op correctly. If e.g.
10279 we expected a different address size than the producer used,
10280 we may have read the wrong number of bytes. */
10281 if (line_ptr != extended_end)
10282 {
10283 complaint (&symfile_complaints,
10284 _("mangled .debug_line section"));
10285 return;
10286 }
c906108c
SS
10287 break;
10288 case DW_LNS_copy:
59205f5a 10289 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10290 dwarf2_debug_line_missing_file_complaint ();
10291 else
366da635 10292 {
25e43795 10293 lh->file_names[file - 1].included_p = 1;
ca5f395d 10294 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10295 {
10296 if (last_subfile != current_subfile)
10297 {
10298 addr = gdbarch_addr_bits_remove (gdbarch, address);
10299 if (last_subfile)
10300 record_line (last_subfile, 0, addr);
10301 last_subfile = current_subfile;
10302 }
10303 addr = check_cu_functions (address, cu);
10304 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10305 record_line (current_subfile, line, addr);
10306 }
366da635 10307 }
c906108c
SS
10308 basic_block = 0;
10309 break;
10310 case DW_LNS_advance_pc:
2dc7f7b3
TT
10311 {
10312 CORE_ADDR adjust
10313 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10314
10315 address += (((op_index + adjust)
10316 / lh->maximum_ops_per_instruction)
10317 * lh->minimum_instruction_length);
10318 op_index = ((op_index + adjust)
10319 % lh->maximum_ops_per_instruction);
10320 line_ptr += bytes_read;
10321 }
c906108c
SS
10322 break;
10323 case DW_LNS_advance_line:
10324 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10325 line_ptr += bytes_read;
10326 break;
10327 case DW_LNS_set_file:
debd256d 10328 {
a738430d
MK
10329 /* The arrays lh->include_dirs and lh->file_names are
10330 0-based, but the directory and file name numbers in
10331 the statement program are 1-based. */
debd256d 10332 struct file_entry *fe;
4f1520fb 10333 char *dir = NULL;
a738430d 10334
debd256d
JB
10335 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10336 line_ptr += bytes_read;
59205f5a 10337 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10338 dwarf2_debug_line_missing_file_complaint ();
10339 else
10340 {
10341 fe = &lh->file_names[file - 1];
10342 if (fe->dir_index)
10343 dir = lh->include_dirs[fe->dir_index - 1];
10344 if (!decode_for_pst_p)
10345 {
10346 last_subfile = current_subfile;
10347 dwarf2_start_subfile (fe->name, dir, comp_dir);
10348 }
10349 }
debd256d 10350 }
c906108c
SS
10351 break;
10352 case DW_LNS_set_column:
10353 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10354 line_ptr += bytes_read;
10355 break;
10356 case DW_LNS_negate_stmt:
10357 is_stmt = (!is_stmt);
10358 break;
10359 case DW_LNS_set_basic_block:
10360 basic_block = 1;
10361 break;
c2c6d25f
JM
10362 /* Add to the address register of the state machine the
10363 address increment value corresponding to special opcode
a738430d
MK
10364 255. I.e., this value is scaled by the minimum
10365 instruction length since special opcode 255 would have
10366 scaled the the increment. */
c906108c 10367 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10368 {
10369 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10370
10371 address += (((op_index + adjust)
10372 / lh->maximum_ops_per_instruction)
10373 * lh->minimum_instruction_length);
10374 op_index = ((op_index + adjust)
10375 % lh->maximum_ops_per_instruction);
10376 }
c906108c
SS
10377 break;
10378 case DW_LNS_fixed_advance_pc:
10379 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10380 op_index = 0;
c906108c
SS
10381 line_ptr += 2;
10382 break;
9aa1fe7e 10383 default:
a738430d
MK
10384 {
10385 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10386 int i;
a738430d 10387
debd256d 10388 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10389 {
10390 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10391 line_ptr += bytes_read;
10392 }
10393 }
c906108c
SS
10394 }
10395 }
59205f5a
JB
10396 if (lh->num_file_names < file || file == 0)
10397 dwarf2_debug_line_missing_file_complaint ();
10398 else
10399 {
10400 lh->file_names[file - 1].included_p = 1;
10401 if (!decode_for_pst_p)
fbf65064
UW
10402 {
10403 addr = gdbarch_addr_bits_remove (gdbarch, address);
10404 record_line (current_subfile, 0, addr);
10405 }
59205f5a 10406 }
c906108c 10407 }
aaa75496
JB
10408
10409 if (decode_for_pst_p)
10410 {
10411 int file_index;
10412
10413 /* Now that we're done scanning the Line Header Program, we can
10414 create the psymtab of each included file. */
10415 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10416 if (lh->file_names[file_index].included_p == 1)
10417 {
c6da4cef
DE
10418 char *include_name =
10419 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10420 if (include_name != NULL)
aaa75496
JB
10421 dwarf2_create_include_psymtab (include_name, pst, objfile);
10422 }
10423 }
cb1df416
DJ
10424 else
10425 {
10426 /* Make sure a symtab is created for every file, even files
10427 which contain only variables (i.e. no code with associated
10428 line numbers). */
10429
10430 int i;
10431 struct file_entry *fe;
10432
10433 for (i = 0; i < lh->num_file_names; i++)
10434 {
10435 char *dir = NULL;
9a619af0 10436
cb1df416
DJ
10437 fe = &lh->file_names[i];
10438 if (fe->dir_index)
10439 dir = lh->include_dirs[fe->dir_index - 1];
10440 dwarf2_start_subfile (fe->name, dir, comp_dir);
10441
10442 /* Skip the main file; we don't need it, and it must be
10443 allocated last, so that it will show up before the
10444 non-primary symtabs in the objfile's symtab list. */
10445 if (current_subfile == first_subfile)
10446 continue;
10447
10448 if (current_subfile->symtab == NULL)
10449 current_subfile->symtab = allocate_symtab (current_subfile->name,
10450 cu->objfile);
10451 fe->symtab = current_subfile->symtab;
10452 }
10453 }
c906108c
SS
10454}
10455
10456/* Start a subfile for DWARF. FILENAME is the name of the file and
10457 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10458 or NULL if not known. COMP_DIR is the compilation directory for the
10459 linetable's compilation unit or NULL if not known.
c906108c
SS
10460 This routine tries to keep line numbers from identical absolute and
10461 relative file names in a common subfile.
10462
10463 Using the `list' example from the GDB testsuite, which resides in
10464 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10465 of /srcdir/list0.c yields the following debugging information for list0.c:
10466
c5aa993b
JM
10467 DW_AT_name: /srcdir/list0.c
10468 DW_AT_comp_dir: /compdir
357e46e7 10469 files.files[0].name: list0.h
c5aa993b 10470 files.files[0].dir: /srcdir
357e46e7 10471 files.files[1].name: list0.c
c5aa993b 10472 files.files[1].dir: /srcdir
c906108c
SS
10473
10474 The line number information for list0.c has to end up in a single
4f1520fb
FR
10475 subfile, so that `break /srcdir/list0.c:1' works as expected.
10476 start_subfile will ensure that this happens provided that we pass the
10477 concatenation of files.files[1].dir and files.files[1].name as the
10478 subfile's name. */
c906108c
SS
10479
10480static void
72b9f47f 10481dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
c906108c 10482{
4f1520fb
FR
10483 char *fullname;
10484
10485 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10486 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10487 second argument to start_subfile. To be consistent, we do the
10488 same here. In order not to lose the line information directory,
10489 we concatenate it to the filename when it makes sense.
10490 Note that the Dwarf3 standard says (speaking of filenames in line
10491 information): ``The directory index is ignored for file names
10492 that represent full path names''. Thus ignoring dirname in the
10493 `else' branch below isn't an issue. */
c906108c 10494
d5166ae1 10495 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10496 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10497 else
10498 fullname = filename;
c906108c 10499
4f1520fb
FR
10500 start_subfile (fullname, comp_dir);
10501
10502 if (fullname != filename)
10503 xfree (fullname);
c906108c
SS
10504}
10505
4c2df51b
DJ
10506static void
10507var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10508 struct dwarf2_cu *cu)
4c2df51b 10509{
e7c27a73
DJ
10510 struct objfile *objfile = cu->objfile;
10511 struct comp_unit_head *cu_header = &cu->header;
10512
4c2df51b
DJ
10513 /* NOTE drow/2003-01-30: There used to be a comment and some special
10514 code here to turn a symbol with DW_AT_external and a
10515 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10516 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10517 with some versions of binutils) where shared libraries could have
10518 relocations against symbols in their debug information - the
10519 minimal symbol would have the right address, but the debug info
10520 would not. It's no longer necessary, because we will explicitly
10521 apply relocations when we read in the debug information now. */
10522
10523 /* A DW_AT_location attribute with no contents indicates that a
10524 variable has been optimized away. */
10525 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10526 {
10527 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10528 return;
10529 }
10530
10531 /* Handle one degenerate form of location expression specially, to
10532 preserve GDB's previous behavior when section offsets are
10533 specified. If this is just a DW_OP_addr then mark this symbol
10534 as LOC_STATIC. */
10535
10536 if (attr_form_is_block (attr)
10537 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10538 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10539 {
891d2f0b 10540 unsigned int dummy;
4c2df51b
DJ
10541
10542 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10543 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10544 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10545 fixup_symbol_section (sym, objfile);
10546 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10547 SYMBOL_SECTION (sym));
4c2df51b
DJ
10548 return;
10549 }
10550
10551 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10552 expression evaluator, and use LOC_COMPUTED only when necessary
10553 (i.e. when the value of a register or memory location is
10554 referenced, or a thread-local block, etc.). Then again, it might
10555 not be worthwhile. I'm assuming that it isn't unless performance
10556 or memory numbers show me otherwise. */
10557
e7c27a73 10558 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10559 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10560}
10561
c906108c
SS
10562/* Given a pointer to a DWARF information entry, figure out if we need
10563 to make a symbol table entry for it, and if so, create a new entry
10564 and return a pointer to it.
10565 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10566 used the passed type.
10567 If SPACE is not NULL, use it to hold the new symbol. If it is
10568 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10569
10570static struct symbol *
34eaf542
TT
10571new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10572 struct symbol *space)
c906108c 10573{
e7c27a73 10574 struct objfile *objfile = cu->objfile;
c906108c
SS
10575 struct symbol *sym = NULL;
10576 char *name;
10577 struct attribute *attr = NULL;
10578 struct attribute *attr2 = NULL;
e142c38c 10579 CORE_ADDR baseaddr;
e37fd15a
SW
10580 struct pending **list_to_add = NULL;
10581
edb3359d 10582 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10583
10584 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10585
94af9270 10586 name = dwarf2_name (die, cu);
c906108c
SS
10587 if (name)
10588 {
94af9270 10589 const char *linkagename;
34eaf542 10590 int suppress_add = 0;
94af9270 10591
34eaf542
TT
10592 if (space)
10593 sym = space;
10594 else
10595 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10596 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10597
10598 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10599 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10600 linkagename = dwarf2_physname (name, die, cu);
10601 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10602
f55ee35c
JK
10603 /* Fortran does not have mangling standard and the mangling does differ
10604 between gfortran, iFort etc. */
10605 if (cu->language == language_fortran
b250c185 10606 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10607 symbol_set_demangled_name (&(sym->ginfo),
10608 (char *) dwarf2_full_name (name, die, cu),
10609 NULL);
f55ee35c 10610
c906108c 10611 /* Default assumptions.
c5aa993b 10612 Use the passed type or decode it from the die. */
176620f1 10613 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10614 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10615 if (type != NULL)
10616 SYMBOL_TYPE (sym) = type;
10617 else
e7c27a73 10618 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10619 attr = dwarf2_attr (die,
10620 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10621 cu);
c906108c
SS
10622 if (attr)
10623 {
10624 SYMBOL_LINE (sym) = DW_UNSND (attr);
10625 }
cb1df416 10626
edb3359d
DJ
10627 attr = dwarf2_attr (die,
10628 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10629 cu);
cb1df416
DJ
10630 if (attr)
10631 {
10632 int file_index = DW_UNSND (attr);
9a619af0 10633
cb1df416
DJ
10634 if (cu->line_header == NULL
10635 || file_index > cu->line_header->num_file_names)
10636 complaint (&symfile_complaints,
10637 _("file index out of range"));
1c3d648d 10638 else if (file_index > 0)
cb1df416
DJ
10639 {
10640 struct file_entry *fe;
9a619af0 10641
cb1df416
DJ
10642 fe = &cu->line_header->file_names[file_index - 1];
10643 SYMBOL_SYMTAB (sym) = fe->symtab;
10644 }
10645 }
10646
c906108c
SS
10647 switch (die->tag)
10648 {
10649 case DW_TAG_label:
e142c38c 10650 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10651 if (attr)
10652 {
10653 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10654 }
0f5238ed
TT
10655 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10656 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10657 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10658 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10659 break;
10660 case DW_TAG_subprogram:
10661 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10662 finish_block. */
10663 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10664 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10665 if ((attr2 && (DW_UNSND (attr2) != 0))
10666 || cu->language == language_ada)
c906108c 10667 {
2cfa0c8d
JB
10668 /* Subprograms marked external are stored as a global symbol.
10669 Ada subprograms, whether marked external or not, are always
10670 stored as a global symbol, because we want to be able to
10671 access them globally. For instance, we want to be able
10672 to break on a nested subprogram without having to
10673 specify the context. */
e37fd15a 10674 list_to_add = &global_symbols;
c906108c
SS
10675 }
10676 else
10677 {
e37fd15a 10678 list_to_add = cu->list_in_scope;
c906108c
SS
10679 }
10680 break;
edb3359d
DJ
10681 case DW_TAG_inlined_subroutine:
10682 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10683 finish_block. */
10684 SYMBOL_CLASS (sym) = LOC_BLOCK;
10685 SYMBOL_INLINED (sym) = 1;
10686 /* Do not add the symbol to any lists. It will be found via
10687 BLOCK_FUNCTION from the blockvector. */
10688 break;
34eaf542
TT
10689 case DW_TAG_template_value_param:
10690 suppress_add = 1;
10691 /* Fall through. */
72929c62 10692 case DW_TAG_constant:
c906108c 10693 case DW_TAG_variable:
254e6b9e 10694 case DW_TAG_member:
c906108c
SS
10695 /* Compilation with minimal debug info may result in variables
10696 with missing type entries. Change the misleading `void' type
10697 to something sensible. */
10698 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10699 SYMBOL_TYPE (sym)
46bf5051 10700 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10701
e142c38c 10702 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10703 /* In the case of DW_TAG_member, we should only be called for
10704 static const members. */
10705 if (die->tag == DW_TAG_member)
10706 {
3863f96c
DE
10707 /* dwarf2_add_field uses die_is_declaration,
10708 so we do the same. */
254e6b9e
DE
10709 gdb_assert (die_is_declaration (die, cu));
10710 gdb_assert (attr);
10711 }
c906108c
SS
10712 if (attr)
10713 {
e7c27a73 10714 dwarf2_const_value (attr, sym, cu);
e142c38c 10715 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10716 if (!suppress_add)
34eaf542
TT
10717 {
10718 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10719 list_to_add = &global_symbols;
34eaf542 10720 else
e37fd15a 10721 list_to_add = cu->list_in_scope;
34eaf542 10722 }
c906108c
SS
10723 break;
10724 }
e142c38c 10725 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10726 if (attr)
10727 {
e7c27a73 10728 var_decode_location (attr, sym, cu);
e142c38c 10729 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10730 if (SYMBOL_CLASS (sym) == LOC_STATIC
10731 && SYMBOL_VALUE_ADDRESS (sym) == 0
10732 && !dwarf2_per_objfile->has_section_at_zero)
10733 {
10734 /* When a static variable is eliminated by the linker,
10735 the corresponding debug information is not stripped
10736 out, but the variable address is set to null;
10737 do not add such variables into symbol table. */
10738 }
10739 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10740 {
f55ee35c
JK
10741 /* Workaround gfortran PR debug/40040 - it uses
10742 DW_AT_location for variables in -fPIC libraries which may
10743 get overriden by other libraries/executable and get
10744 a different address. Resolve it by the minimal symbol
10745 which may come from inferior's executable using copy
10746 relocation. Make this workaround only for gfortran as for
10747 other compilers GDB cannot guess the minimal symbol
10748 Fortran mangling kind. */
10749 if (cu->language == language_fortran && die->parent
10750 && die->parent->tag == DW_TAG_module
10751 && cu->producer
10752 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10753 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10754
1c809c68
TT
10755 /* A variable with DW_AT_external is never static,
10756 but it may be block-scoped. */
10757 list_to_add = (cu->list_in_scope == &file_symbols
10758 ? &global_symbols : cu->list_in_scope);
1c809c68 10759 }
c906108c 10760 else
e37fd15a 10761 list_to_add = cu->list_in_scope;
c906108c
SS
10762 }
10763 else
10764 {
10765 /* We do not know the address of this symbol.
c5aa993b
JM
10766 If it is an external symbol and we have type information
10767 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10768 The address of the variable will then be determined from
10769 the minimal symbol table whenever the variable is
10770 referenced. */
e142c38c 10771 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10772 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10773 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10774 {
0fe7935b
DJ
10775 /* A variable with DW_AT_external is never static, but it
10776 may be block-scoped. */
10777 list_to_add = (cu->list_in_scope == &file_symbols
10778 ? &global_symbols : cu->list_in_scope);
10779
c906108c 10780 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10781 }
442ddf59
JK
10782 else if (!die_is_declaration (die, cu))
10783 {
10784 /* Use the default LOC_OPTIMIZED_OUT class. */
10785 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10786 if (!suppress_add)
10787 list_to_add = cu->list_in_scope;
442ddf59 10788 }
c906108c
SS
10789 }
10790 break;
10791 case DW_TAG_formal_parameter:
edb3359d
DJ
10792 /* If we are inside a function, mark this as an argument. If
10793 not, we might be looking at an argument to an inlined function
10794 when we do not have enough information to show inlined frames;
10795 pretend it's a local variable in that case so that the user can
10796 still see it. */
10797 if (context_stack_depth > 0
10798 && context_stack[context_stack_depth - 1].name != NULL)
10799 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10800 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10801 if (attr)
10802 {
e7c27a73 10803 var_decode_location (attr, sym, cu);
c906108c 10804 }
e142c38c 10805 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10806 if (attr)
10807 {
e7c27a73 10808 dwarf2_const_value (attr, sym, cu);
c906108c 10809 }
f346a30d
PM
10810 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10811 if (attr && DW_UNSND (attr))
10812 {
10813 struct type *ref_type;
10814
10815 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10816 SYMBOL_TYPE (sym) = ref_type;
10817 }
10818
e37fd15a 10819 list_to_add = cu->list_in_scope;
c906108c
SS
10820 break;
10821 case DW_TAG_unspecified_parameters:
10822 /* From varargs functions; gdb doesn't seem to have any
10823 interest in this information, so just ignore it for now.
10824 (FIXME?) */
10825 break;
34eaf542
TT
10826 case DW_TAG_template_type_param:
10827 suppress_add = 1;
10828 /* Fall through. */
c906108c 10829 case DW_TAG_class_type:
680b30c7 10830 case DW_TAG_interface_type:
c906108c
SS
10831 case DW_TAG_structure_type:
10832 case DW_TAG_union_type:
72019c9c 10833 case DW_TAG_set_type:
c906108c
SS
10834 case DW_TAG_enumeration_type:
10835 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10836 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10837
63d06c5c 10838 {
987504bb 10839 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10840 really ever be static objects: otherwise, if you try
10841 to, say, break of a class's method and you're in a file
10842 which doesn't mention that class, it won't work unless
10843 the check for all static symbols in lookup_symbol_aux
10844 saves you. See the OtherFileClass tests in
10845 gdb.c++/namespace.exp. */
10846
e37fd15a 10847 if (!suppress_add)
34eaf542 10848 {
34eaf542
TT
10849 list_to_add = (cu->list_in_scope == &file_symbols
10850 && (cu->language == language_cplus
10851 || cu->language == language_java)
10852 ? &global_symbols : cu->list_in_scope);
63d06c5c 10853
64382290
TT
10854 /* The semantics of C++ state that "struct foo {
10855 ... }" also defines a typedef for "foo". A Java
10856 class declaration also defines a typedef for the
10857 class. */
10858 if (cu->language == language_cplus
10859 || cu->language == language_java
10860 || cu->language == language_ada)
10861 {
10862 /* The symbol's name is already allocated along
10863 with this objfile, so we don't need to
10864 duplicate it for the type. */
10865 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10866 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10867 }
63d06c5c
DC
10868 }
10869 }
c906108c
SS
10870 break;
10871 case DW_TAG_typedef:
63d06c5c
DC
10872 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10873 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10874 list_to_add = cu->list_in_scope;
63d06c5c 10875 break;
c906108c 10876 case DW_TAG_base_type:
a02abb62 10877 case DW_TAG_subrange_type:
c906108c 10878 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10879 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10880 list_to_add = cu->list_in_scope;
c906108c
SS
10881 break;
10882 case DW_TAG_enumerator:
e142c38c 10883 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10884 if (attr)
10885 {
e7c27a73 10886 dwarf2_const_value (attr, sym, cu);
c906108c 10887 }
63d06c5c
DC
10888 {
10889 /* NOTE: carlton/2003-11-10: See comment above in the
10890 DW_TAG_class_type, etc. block. */
10891
e142c38c 10892 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10893 && (cu->language == language_cplus
10894 || cu->language == language_java)
e142c38c 10895 ? &global_symbols : cu->list_in_scope);
63d06c5c 10896 }
c906108c 10897 break;
5c4e30ca
DC
10898 case DW_TAG_namespace:
10899 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10900 list_to_add = &global_symbols;
5c4e30ca 10901 break;
c906108c
SS
10902 default:
10903 /* Not a tag we recognize. Hopefully we aren't processing
10904 trash data, but since we must specifically ignore things
10905 we don't recognize, there is nothing else we should do at
10906 this point. */
e2e0b3e5 10907 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10908 dwarf_tag_name (die->tag));
c906108c
SS
10909 break;
10910 }
df8a16a1 10911
e37fd15a
SW
10912 if (suppress_add)
10913 {
10914 sym->hash_next = objfile->template_symbols;
10915 objfile->template_symbols = sym;
10916 list_to_add = NULL;
10917 }
10918
10919 if (list_to_add != NULL)
10920 add_symbol_to_list (sym, list_to_add);
10921
df8a16a1
DJ
10922 /* For the benefit of old versions of GCC, check for anonymous
10923 namespaces based on the demangled name. */
10924 if (!processing_has_namespace_info
94af9270 10925 && cu->language == language_cplus)
df8a16a1 10926 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10927 }
10928 return (sym);
10929}
10930
34eaf542
TT
10931/* A wrapper for new_symbol_full that always allocates a new symbol. */
10932
10933static struct symbol *
10934new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10935{
10936 return new_symbol_full (die, type, cu, NULL);
10937}
10938
98bfdba5
PA
10939/* Given an attr with a DW_FORM_dataN value in host byte order,
10940 zero-extend it as appropriate for the symbol's type. The DWARF
10941 standard (v4) is not entirely clear about the meaning of using
10942 DW_FORM_dataN for a constant with a signed type, where the type is
10943 wider than the data. The conclusion of a discussion on the DWARF
10944 list was that this is unspecified. We choose to always zero-extend
10945 because that is the interpretation long in use by GCC. */
c906108c 10946
98bfdba5
PA
10947static gdb_byte *
10948dwarf2_const_value_data (struct attribute *attr, struct type *type,
10949 const char *name, struct obstack *obstack,
10950 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10951{
e7c27a73 10952 struct objfile *objfile = cu->objfile;
e17a4113
UW
10953 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10954 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10955 LONGEST l = DW_UNSND (attr);
10956
10957 if (bits < sizeof (*value) * 8)
10958 {
10959 l &= ((LONGEST) 1 << bits) - 1;
10960 *value = l;
10961 }
10962 else if (bits == sizeof (*value) * 8)
10963 *value = l;
10964 else
10965 {
10966 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10967 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10968 return bytes;
10969 }
10970
10971 return NULL;
10972}
10973
10974/* Read a constant value from an attribute. Either set *VALUE, or if
10975 the value does not fit in *VALUE, set *BYTES - either already
10976 allocated on the objfile obstack, or newly allocated on OBSTACK,
10977 or, set *BATON, if we translated the constant to a location
10978 expression. */
10979
10980static void
10981dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10982 const char *name, struct obstack *obstack,
10983 struct dwarf2_cu *cu,
10984 long *value, gdb_byte **bytes,
10985 struct dwarf2_locexpr_baton **baton)
10986{
10987 struct objfile *objfile = cu->objfile;
10988 struct comp_unit_head *cu_header = &cu->header;
c906108c 10989 struct dwarf_block *blk;
98bfdba5
PA
10990 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10991 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10992
10993 *value = 0;
10994 *bytes = NULL;
10995 *baton = NULL;
c906108c
SS
10996
10997 switch (attr->form)
10998 {
10999 case DW_FORM_addr:
ac56253d 11000 {
ac56253d
TT
11001 gdb_byte *data;
11002
98bfdba5
PA
11003 if (TYPE_LENGTH (type) != cu_header->addr_size)
11004 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11005 cu_header->addr_size,
98bfdba5 11006 TYPE_LENGTH (type));
ac56253d
TT
11007 /* Symbols of this form are reasonably rare, so we just
11008 piggyback on the existing location code rather than writing
11009 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11010 *baton = obstack_alloc (&objfile->objfile_obstack,
11011 sizeof (struct dwarf2_locexpr_baton));
11012 (*baton)->per_cu = cu->per_cu;
11013 gdb_assert ((*baton)->per_cu);
ac56253d 11014
98bfdba5
PA
11015 (*baton)->size = 2 + cu_header->addr_size;
11016 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11017 (*baton)->data = data;
ac56253d
TT
11018
11019 data[0] = DW_OP_addr;
11020 store_unsigned_integer (&data[1], cu_header->addr_size,
11021 byte_order, DW_ADDR (attr));
11022 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11023 }
c906108c 11024 break;
4ac36638 11025 case DW_FORM_string:
93b5768b 11026 case DW_FORM_strp:
98bfdba5
PA
11027 /* DW_STRING is already allocated on the objfile obstack, point
11028 directly to it. */
11029 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11030 break;
c906108c
SS
11031 case DW_FORM_block1:
11032 case DW_FORM_block2:
11033 case DW_FORM_block4:
11034 case DW_FORM_block:
2dc7f7b3 11035 case DW_FORM_exprloc:
c906108c 11036 blk = DW_BLOCK (attr);
98bfdba5
PA
11037 if (TYPE_LENGTH (type) != blk->size)
11038 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11039 TYPE_LENGTH (type));
11040 *bytes = blk->data;
c906108c 11041 break;
2df3850c
JM
11042
11043 /* The DW_AT_const_value attributes are supposed to carry the
11044 symbol's value "represented as it would be on the target
11045 architecture." By the time we get here, it's already been
11046 converted to host endianness, so we just need to sign- or
11047 zero-extend it as appropriate. */
11048 case DW_FORM_data1:
98bfdba5 11049 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 11050 break;
c906108c 11051 case DW_FORM_data2:
98bfdba5 11052 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 11053 break;
c906108c 11054 case DW_FORM_data4:
98bfdba5 11055 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 11056 break;
c906108c 11057 case DW_FORM_data8:
98bfdba5 11058 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11059 break;
11060
c906108c 11061 case DW_FORM_sdata:
98bfdba5 11062 *value = DW_SND (attr);
2df3850c
JM
11063 break;
11064
c906108c 11065 case DW_FORM_udata:
98bfdba5 11066 *value = DW_UNSND (attr);
c906108c 11067 break;
2df3850c 11068
c906108c 11069 default:
4d3c2250 11070 complaint (&symfile_complaints,
e2e0b3e5 11071 _("unsupported const value attribute form: '%s'"),
4d3c2250 11072 dwarf_form_name (attr->form));
98bfdba5 11073 *value = 0;
c906108c
SS
11074 break;
11075 }
11076}
11077
2df3850c 11078
98bfdba5
PA
11079/* Copy constant value from an attribute to a symbol. */
11080
2df3850c 11081static void
98bfdba5
PA
11082dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11083 struct dwarf2_cu *cu)
2df3850c 11084{
98bfdba5
PA
11085 struct objfile *objfile = cu->objfile;
11086 struct comp_unit_head *cu_header = &cu->header;
11087 long value;
11088 gdb_byte *bytes;
11089 struct dwarf2_locexpr_baton *baton;
2df3850c 11090
98bfdba5
PA
11091 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11092 SYMBOL_PRINT_NAME (sym),
11093 &objfile->objfile_obstack, cu,
11094 &value, &bytes, &baton);
2df3850c 11095
98bfdba5
PA
11096 if (baton != NULL)
11097 {
11098 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11099 SYMBOL_LOCATION_BATON (sym) = baton;
11100 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11101 }
11102 else if (bytes != NULL)
11103 {
11104 SYMBOL_VALUE_BYTES (sym) = bytes;
11105 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11106 }
11107 else
11108 {
11109 SYMBOL_VALUE (sym) = value;
11110 SYMBOL_CLASS (sym) = LOC_CONST;
11111 }
2df3850c
JM
11112}
11113
c906108c
SS
11114/* Return the type of the die in question using its DW_AT_type attribute. */
11115
11116static struct type *
e7c27a73 11117die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11118{
c906108c 11119 struct attribute *type_attr;
c906108c 11120
e142c38c 11121 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11122 if (!type_attr)
11123 {
11124 /* A missing DW_AT_type represents a void type. */
46bf5051 11125 return objfile_type (cu->objfile)->builtin_void;
c906108c 11126 }
348e048f 11127
673bfd45 11128 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11129}
11130
b4ba55a1
JB
11131/* True iff CU's producer generates GNAT Ada auxiliary information
11132 that allows to find parallel types through that information instead
11133 of having to do expensive parallel lookups by type name. */
11134
11135static int
11136need_gnat_info (struct dwarf2_cu *cu)
11137{
11138 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11139 of GNAT produces this auxiliary information, without any indication
11140 that it is produced. Part of enhancing the FSF version of GNAT
11141 to produce that information will be to put in place an indicator
11142 that we can use in order to determine whether the descriptive type
11143 info is available or not. One suggestion that has been made is
11144 to use a new attribute, attached to the CU die. For now, assume
11145 that the descriptive type info is not available. */
11146 return 0;
11147}
11148
b4ba55a1
JB
11149/* Return the auxiliary type of the die in question using its
11150 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11151 attribute is not present. */
11152
11153static struct type *
11154die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11155{
b4ba55a1 11156 struct attribute *type_attr;
b4ba55a1
JB
11157
11158 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11159 if (!type_attr)
11160 return NULL;
11161
673bfd45 11162 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11163}
11164
11165/* If DIE has a descriptive_type attribute, then set the TYPE's
11166 descriptive type accordingly. */
11167
11168static void
11169set_descriptive_type (struct type *type, struct die_info *die,
11170 struct dwarf2_cu *cu)
11171{
11172 struct type *descriptive_type = die_descriptive_type (die, cu);
11173
11174 if (descriptive_type)
11175 {
11176 ALLOCATE_GNAT_AUX_TYPE (type);
11177 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11178 }
11179}
11180
c906108c
SS
11181/* Return the containing type of the die in question using its
11182 DW_AT_containing_type attribute. */
11183
11184static struct type *
e7c27a73 11185die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11186{
c906108c 11187 struct attribute *type_attr;
c906108c 11188
e142c38c 11189 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11190 if (!type_attr)
11191 error (_("Dwarf Error: Problem turning containing type into gdb type "
11192 "[in module %s]"), cu->objfile->name);
11193
673bfd45 11194 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11195}
11196
673bfd45
DE
11197/* Look up the type of DIE in CU using its type attribute ATTR.
11198 If there is no type substitute an error marker. */
11199
c906108c 11200static struct type *
673bfd45
DE
11201lookup_die_type (struct die_info *die, struct attribute *attr,
11202 struct dwarf2_cu *cu)
c906108c 11203{
f792889a
DJ
11204 struct type *this_type;
11205
673bfd45
DE
11206 /* First see if we have it cached. */
11207
11208 if (is_ref_attr (attr))
11209 {
11210 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11211
11212 this_type = get_die_type_at_offset (offset, cu->per_cu);
11213 }
11214 else if (attr->form == DW_FORM_sig8)
11215 {
11216 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11217 struct dwarf2_cu *sig_cu;
11218 unsigned int offset;
11219
11220 /* sig_type will be NULL if the signatured type is missing from
11221 the debug info. */
11222 if (sig_type == NULL)
11223 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11224 "at 0x%x [in module %s]"),
11225 die->offset, cu->objfile->name);
11226
11227 gdb_assert (sig_type->per_cu.from_debug_types);
11228 offset = sig_type->offset + sig_type->type_offset;
11229 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11230 }
11231 else
11232 {
11233 dump_die_for_error (die);
11234 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11235 dwarf_attr_name (attr->name), cu->objfile->name);
11236 }
11237
11238 /* If not cached we need to read it in. */
11239
11240 if (this_type == NULL)
11241 {
11242 struct die_info *type_die;
11243 struct dwarf2_cu *type_cu = cu;
11244
11245 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11246 /* If the type is cached, we should have found it above. */
11247 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11248 this_type = read_type_die_1 (type_die, type_cu);
11249 }
11250
11251 /* If we still don't have a type use an error marker. */
11252
11253 if (this_type == NULL)
c906108c 11254 {
b00fdb78
TT
11255 char *message, *saved;
11256
11257 /* read_type_die already issued a complaint. */
11258 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11259 cu->objfile->name,
11260 cu->header.offset,
11261 die->offset);
11262 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11263 message, strlen (message));
11264 xfree (message);
11265
11266 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11267 }
673bfd45 11268
f792889a 11269 return this_type;
c906108c
SS
11270}
11271
673bfd45
DE
11272/* Return the type in DIE, CU.
11273 Returns NULL for invalid types.
11274
11275 This first does a lookup in the appropriate type_hash table,
11276 and only reads the die in if necessary.
11277
11278 NOTE: This can be called when reading in partial or full symbols. */
11279
f792889a 11280static struct type *
e7c27a73 11281read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11282{
f792889a
DJ
11283 struct type *this_type;
11284
11285 this_type = get_die_type (die, cu);
11286 if (this_type)
11287 return this_type;
11288
673bfd45
DE
11289 return read_type_die_1 (die, cu);
11290}
11291
11292/* Read the type in DIE, CU.
11293 Returns NULL for invalid types. */
11294
11295static struct type *
11296read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11297{
11298 struct type *this_type = NULL;
11299
c906108c
SS
11300 switch (die->tag)
11301 {
11302 case DW_TAG_class_type:
680b30c7 11303 case DW_TAG_interface_type:
c906108c
SS
11304 case DW_TAG_structure_type:
11305 case DW_TAG_union_type:
f792889a 11306 this_type = read_structure_type (die, cu);
c906108c
SS
11307 break;
11308 case DW_TAG_enumeration_type:
f792889a 11309 this_type = read_enumeration_type (die, cu);
c906108c
SS
11310 break;
11311 case DW_TAG_subprogram:
11312 case DW_TAG_subroutine_type:
edb3359d 11313 case DW_TAG_inlined_subroutine:
f792889a 11314 this_type = read_subroutine_type (die, cu);
c906108c
SS
11315 break;
11316 case DW_TAG_array_type:
f792889a 11317 this_type = read_array_type (die, cu);
c906108c 11318 break;
72019c9c 11319 case DW_TAG_set_type:
f792889a 11320 this_type = read_set_type (die, cu);
72019c9c 11321 break;
c906108c 11322 case DW_TAG_pointer_type:
f792889a 11323 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11324 break;
11325 case DW_TAG_ptr_to_member_type:
f792889a 11326 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11327 break;
11328 case DW_TAG_reference_type:
f792889a 11329 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11330 break;
11331 case DW_TAG_const_type:
f792889a 11332 this_type = read_tag_const_type (die, cu);
c906108c
SS
11333 break;
11334 case DW_TAG_volatile_type:
f792889a 11335 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11336 break;
11337 case DW_TAG_string_type:
f792889a 11338 this_type = read_tag_string_type (die, cu);
c906108c
SS
11339 break;
11340 case DW_TAG_typedef:
f792889a 11341 this_type = read_typedef (die, cu);
c906108c 11342 break;
a02abb62 11343 case DW_TAG_subrange_type:
f792889a 11344 this_type = read_subrange_type (die, cu);
a02abb62 11345 break;
c906108c 11346 case DW_TAG_base_type:
f792889a 11347 this_type = read_base_type (die, cu);
c906108c 11348 break;
81a17f79 11349 case DW_TAG_unspecified_type:
f792889a 11350 this_type = read_unspecified_type (die, cu);
81a17f79 11351 break;
0114d602
DJ
11352 case DW_TAG_namespace:
11353 this_type = read_namespace_type (die, cu);
11354 break;
f55ee35c
JK
11355 case DW_TAG_module:
11356 this_type = read_module_type (die, cu);
11357 break;
c906108c 11358 default:
a1f5b845 11359 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11360 dwarf_tag_name (die->tag));
c906108c
SS
11361 break;
11362 }
63d06c5c 11363
f792889a 11364 return this_type;
63d06c5c
DC
11365}
11366
abc72ce4
DE
11367/* See if we can figure out if the class lives in a namespace. We do
11368 this by looking for a member function; its demangled name will
11369 contain namespace info, if there is any.
11370 Return the computed name or NULL.
11371 Space for the result is allocated on the objfile's obstack.
11372 This is the full-die version of guess_partial_die_structure_name.
11373 In this case we know DIE has no useful parent. */
11374
11375static char *
11376guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11377{
11378 struct die_info *spec_die;
11379 struct dwarf2_cu *spec_cu;
11380 struct die_info *child;
11381
11382 spec_cu = cu;
11383 spec_die = die_specification (die, &spec_cu);
11384 if (spec_die != NULL)
11385 {
11386 die = spec_die;
11387 cu = spec_cu;
11388 }
11389
11390 for (child = die->child;
11391 child != NULL;
11392 child = child->sibling)
11393 {
11394 if (child->tag == DW_TAG_subprogram)
11395 {
11396 struct attribute *attr;
11397
11398 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11399 if (attr == NULL)
11400 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11401 if (attr != NULL)
11402 {
11403 char *actual_name
11404 = language_class_name_from_physname (cu->language_defn,
11405 DW_STRING (attr));
11406 char *name = NULL;
11407
11408 if (actual_name != NULL)
11409 {
11410 char *die_name = dwarf2_name (die, cu);
11411
11412 if (die_name != NULL
11413 && strcmp (die_name, actual_name) != 0)
11414 {
11415 /* Strip off the class name from the full name.
11416 We want the prefix. */
11417 int die_name_len = strlen (die_name);
11418 int actual_name_len = strlen (actual_name);
11419
11420 /* Test for '::' as a sanity check. */
11421 if (actual_name_len > die_name_len + 2
11422 && actual_name[actual_name_len - die_name_len - 1] == ':')
11423 name =
11424 obsavestring (actual_name,
11425 actual_name_len - die_name_len - 2,
11426 &cu->objfile->objfile_obstack);
11427 }
11428 }
11429 xfree (actual_name);
11430 return name;
11431 }
11432 }
11433 }
11434
11435 return NULL;
11436}
11437
fdde2d81 11438/* Return the name of the namespace/class that DIE is defined within,
0114d602 11439 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11440
0114d602
DJ
11441 For example, if we're within the method foo() in the following
11442 code:
11443
11444 namespace N {
11445 class C {
11446 void foo () {
11447 }
11448 };
11449 }
11450
11451 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11452
11453static char *
e142c38c 11454determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11455{
0114d602
DJ
11456 struct die_info *parent, *spec_die;
11457 struct dwarf2_cu *spec_cu;
11458 struct type *parent_type;
63d06c5c 11459
f55ee35c
JK
11460 if (cu->language != language_cplus && cu->language != language_java
11461 && cu->language != language_fortran)
0114d602
DJ
11462 return "";
11463
11464 /* We have to be careful in the presence of DW_AT_specification.
11465 For example, with GCC 3.4, given the code
11466
11467 namespace N {
11468 void foo() {
11469 // Definition of N::foo.
11470 }
11471 }
11472
11473 then we'll have a tree of DIEs like this:
11474
11475 1: DW_TAG_compile_unit
11476 2: DW_TAG_namespace // N
11477 3: DW_TAG_subprogram // declaration of N::foo
11478 4: DW_TAG_subprogram // definition of N::foo
11479 DW_AT_specification // refers to die #3
11480
11481 Thus, when processing die #4, we have to pretend that we're in
11482 the context of its DW_AT_specification, namely the contex of die
11483 #3. */
11484 spec_cu = cu;
11485 spec_die = die_specification (die, &spec_cu);
11486 if (spec_die == NULL)
11487 parent = die->parent;
11488 else
63d06c5c 11489 {
0114d602
DJ
11490 parent = spec_die->parent;
11491 cu = spec_cu;
63d06c5c 11492 }
0114d602
DJ
11493
11494 if (parent == NULL)
11495 return "";
98bfdba5
PA
11496 else if (parent->building_fullname)
11497 {
11498 const char *name;
11499 const char *parent_name;
11500
11501 /* It has been seen on RealView 2.2 built binaries,
11502 DW_TAG_template_type_param types actually _defined_ as
11503 children of the parent class:
11504
11505 enum E {};
11506 template class <class Enum> Class{};
11507 Class<enum E> class_e;
11508
11509 1: DW_TAG_class_type (Class)
11510 2: DW_TAG_enumeration_type (E)
11511 3: DW_TAG_enumerator (enum1:0)
11512 3: DW_TAG_enumerator (enum2:1)
11513 ...
11514 2: DW_TAG_template_type_param
11515 DW_AT_type DW_FORM_ref_udata (E)
11516
11517 Besides being broken debug info, it can put GDB into an
11518 infinite loop. Consider:
11519
11520 When we're building the full name for Class<E>, we'll start
11521 at Class, and go look over its template type parameters,
11522 finding E. We'll then try to build the full name of E, and
11523 reach here. We're now trying to build the full name of E,
11524 and look over the parent DIE for containing scope. In the
11525 broken case, if we followed the parent DIE of E, we'd again
11526 find Class, and once again go look at its template type
11527 arguments, etc., etc. Simply don't consider such parent die
11528 as source-level parent of this die (it can't be, the language
11529 doesn't allow it), and break the loop here. */
11530 name = dwarf2_name (die, cu);
11531 parent_name = dwarf2_name (parent, cu);
11532 complaint (&symfile_complaints,
11533 _("template param type '%s' defined within parent '%s'"),
11534 name ? name : "<unknown>",
11535 parent_name ? parent_name : "<unknown>");
11536 return "";
11537 }
63d06c5c 11538 else
0114d602
DJ
11539 switch (parent->tag)
11540 {
63d06c5c 11541 case DW_TAG_namespace:
0114d602 11542 parent_type = read_type_die (parent, cu);
acebe513
UW
11543 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11544 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11545 Work around this problem here. */
11546 if (cu->language == language_cplus
11547 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11548 return "";
0114d602
DJ
11549 /* We give a name to even anonymous namespaces. */
11550 return TYPE_TAG_NAME (parent_type);
63d06c5c 11551 case DW_TAG_class_type:
680b30c7 11552 case DW_TAG_interface_type:
63d06c5c 11553 case DW_TAG_structure_type:
0114d602 11554 case DW_TAG_union_type:
f55ee35c 11555 case DW_TAG_module:
0114d602
DJ
11556 parent_type = read_type_die (parent, cu);
11557 if (TYPE_TAG_NAME (parent_type) != NULL)
11558 return TYPE_TAG_NAME (parent_type);
11559 else
11560 /* An anonymous structure is only allowed non-static data
11561 members; no typedefs, no member functions, et cetera.
11562 So it does not need a prefix. */
11563 return "";
abc72ce4
DE
11564 case DW_TAG_compile_unit:
11565 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11566 if (cu->language == language_cplus
11567 && dwarf2_per_objfile->types.asection != NULL
11568 && die->child != NULL
11569 && (die->tag == DW_TAG_class_type
11570 || die->tag == DW_TAG_structure_type
11571 || die->tag == DW_TAG_union_type))
11572 {
11573 char *name = guess_full_die_structure_name (die, cu);
11574 if (name != NULL)
11575 return name;
11576 }
11577 return "";
63d06c5c 11578 default:
8176b9b8 11579 return determine_prefix (parent, cu);
63d06c5c 11580 }
63d06c5c
DC
11581}
11582
987504bb
JJ
11583/* Return a newly-allocated string formed by concatenating PREFIX and
11584 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11585 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11586 perform an obconcat, otherwise allocate storage for the result. The CU argument
11587 is used to determine the language and hence, the appropriate separator. */
11588
f55ee35c 11589#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11590
11591static char *
f55ee35c
JK
11592typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11593 int physname, struct dwarf2_cu *cu)
63d06c5c 11594{
f55ee35c 11595 const char *lead = "";
5c315b68 11596 const char *sep;
63d06c5c 11597
987504bb
JJ
11598 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11599 sep = "";
11600 else if (cu->language == language_java)
11601 sep = ".";
f55ee35c
JK
11602 else if (cu->language == language_fortran && physname)
11603 {
11604 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11605 DW_AT_MIPS_linkage_name is preferred and used instead. */
11606
11607 lead = "__";
11608 sep = "_MOD_";
11609 }
987504bb
JJ
11610 else
11611 sep = "::";
63d06c5c 11612
6dd47d34
DE
11613 if (prefix == NULL)
11614 prefix = "";
11615 if (suffix == NULL)
11616 suffix = "";
11617
987504bb
JJ
11618 if (obs == NULL)
11619 {
11620 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11621
f55ee35c
JK
11622 strcpy (retval, lead);
11623 strcat (retval, prefix);
6dd47d34
DE
11624 strcat (retval, sep);
11625 strcat (retval, suffix);
63d06c5c
DC
11626 return retval;
11627 }
987504bb
JJ
11628 else
11629 {
11630 /* We have an obstack. */
f55ee35c 11631 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11632 }
63d06c5c
DC
11633}
11634
c906108c
SS
11635/* Return sibling of die, NULL if no sibling. */
11636
f9aca02d 11637static struct die_info *
fba45db2 11638sibling_die (struct die_info *die)
c906108c 11639{
639d11d3 11640 return die->sibling;
c906108c
SS
11641}
11642
71c25dea
TT
11643/* Get name of a die, return NULL if not found. */
11644
11645static char *
11646dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11647 struct obstack *obstack)
11648{
11649 if (name && cu->language == language_cplus)
11650 {
11651 char *canon_name = cp_canonicalize_string (name);
11652
11653 if (canon_name != NULL)
11654 {
11655 if (strcmp (canon_name, name) != 0)
11656 name = obsavestring (canon_name, strlen (canon_name),
11657 obstack);
11658 xfree (canon_name);
11659 }
11660 }
11661
11662 return name;
c906108c
SS
11663}
11664
9219021c
DC
11665/* Get name of a die, return NULL if not found. */
11666
11667static char *
e142c38c 11668dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11669{
11670 struct attribute *attr;
11671
e142c38c 11672 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11673 if (!attr || !DW_STRING (attr))
11674 return NULL;
11675
11676 switch (die->tag)
11677 {
11678 case DW_TAG_compile_unit:
11679 /* Compilation units have a DW_AT_name that is a filename, not
11680 a source language identifier. */
11681 case DW_TAG_enumeration_type:
11682 case DW_TAG_enumerator:
11683 /* These tags always have simple identifiers already; no need
11684 to canonicalize them. */
11685 return DW_STRING (attr);
907af001 11686
418835cc
KS
11687 case DW_TAG_subprogram:
11688 /* Java constructors will all be named "<init>", so return
11689 the class name when we see this special case. */
11690 if (cu->language == language_java
11691 && DW_STRING (attr) != NULL
11692 && strcmp (DW_STRING (attr), "<init>") == 0)
11693 {
11694 struct dwarf2_cu *spec_cu = cu;
11695 struct die_info *spec_die;
11696
11697 /* GCJ will output '<init>' for Java constructor names.
11698 For this special case, return the name of the parent class. */
11699
11700 /* GCJ may output suprogram DIEs with AT_specification set.
11701 If so, use the name of the specified DIE. */
11702 spec_die = die_specification (die, &spec_cu);
11703 if (spec_die != NULL)
11704 return dwarf2_name (spec_die, spec_cu);
11705
11706 do
11707 {
11708 die = die->parent;
11709 if (die->tag == DW_TAG_class_type)
11710 return dwarf2_name (die, cu);
11711 }
11712 while (die->tag != DW_TAG_compile_unit);
11713 }
907af001
UW
11714 break;
11715
11716 case DW_TAG_class_type:
11717 case DW_TAG_interface_type:
11718 case DW_TAG_structure_type:
11719 case DW_TAG_union_type:
11720 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11721 structures or unions. These were of the form "._%d" in GCC 4.1,
11722 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11723 and GCC 4.4. We work around this problem by ignoring these. */
11724 if (strncmp (DW_STRING (attr), "._", 2) == 0
11725 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11726 return NULL;
11727 break;
11728
71c25dea 11729 default:
907af001
UW
11730 break;
11731 }
11732
11733 if (!DW_STRING_IS_CANONICAL (attr))
11734 {
11735 DW_STRING (attr)
11736 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11737 &cu->objfile->objfile_obstack);
11738 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11739 }
907af001 11740 return DW_STRING (attr);
9219021c
DC
11741}
11742
11743/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11744 is none. *EXT_CU is the CU containing DIE on input, and the CU
11745 containing the return value on output. */
9219021c
DC
11746
11747static struct die_info *
f2f0e013 11748dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11749{
11750 struct attribute *attr;
9219021c 11751
f2f0e013 11752 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11753 if (attr == NULL)
11754 return NULL;
11755
f2f0e013 11756 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11757}
11758
c906108c
SS
11759/* Convert a DIE tag into its string name. */
11760
11761static char *
aa1ee363 11762dwarf_tag_name (unsigned tag)
c906108c
SS
11763{
11764 switch (tag)
11765 {
11766 case DW_TAG_padding:
11767 return "DW_TAG_padding";
11768 case DW_TAG_array_type:
11769 return "DW_TAG_array_type";
11770 case DW_TAG_class_type:
11771 return "DW_TAG_class_type";
11772 case DW_TAG_entry_point:
11773 return "DW_TAG_entry_point";
11774 case DW_TAG_enumeration_type:
11775 return "DW_TAG_enumeration_type";
11776 case DW_TAG_formal_parameter:
11777 return "DW_TAG_formal_parameter";
11778 case DW_TAG_imported_declaration:
11779 return "DW_TAG_imported_declaration";
11780 case DW_TAG_label:
11781 return "DW_TAG_label";
11782 case DW_TAG_lexical_block:
11783 return "DW_TAG_lexical_block";
11784 case DW_TAG_member:
11785 return "DW_TAG_member";
11786 case DW_TAG_pointer_type:
11787 return "DW_TAG_pointer_type";
11788 case DW_TAG_reference_type:
11789 return "DW_TAG_reference_type";
11790 case DW_TAG_compile_unit:
11791 return "DW_TAG_compile_unit";
11792 case DW_TAG_string_type:
11793 return "DW_TAG_string_type";
11794 case DW_TAG_structure_type:
11795 return "DW_TAG_structure_type";
11796 case DW_TAG_subroutine_type:
11797 return "DW_TAG_subroutine_type";
11798 case DW_TAG_typedef:
11799 return "DW_TAG_typedef";
11800 case DW_TAG_union_type:
11801 return "DW_TAG_union_type";
11802 case DW_TAG_unspecified_parameters:
11803 return "DW_TAG_unspecified_parameters";
11804 case DW_TAG_variant:
11805 return "DW_TAG_variant";
11806 case DW_TAG_common_block:
11807 return "DW_TAG_common_block";
11808 case DW_TAG_common_inclusion:
11809 return "DW_TAG_common_inclusion";
11810 case DW_TAG_inheritance:
11811 return "DW_TAG_inheritance";
11812 case DW_TAG_inlined_subroutine:
11813 return "DW_TAG_inlined_subroutine";
11814 case DW_TAG_module:
11815 return "DW_TAG_module";
11816 case DW_TAG_ptr_to_member_type:
11817 return "DW_TAG_ptr_to_member_type";
11818 case DW_TAG_set_type:
11819 return "DW_TAG_set_type";
11820 case DW_TAG_subrange_type:
11821 return "DW_TAG_subrange_type";
11822 case DW_TAG_with_stmt:
11823 return "DW_TAG_with_stmt";
11824 case DW_TAG_access_declaration:
11825 return "DW_TAG_access_declaration";
11826 case DW_TAG_base_type:
11827 return "DW_TAG_base_type";
11828 case DW_TAG_catch_block:
11829 return "DW_TAG_catch_block";
11830 case DW_TAG_const_type:
11831 return "DW_TAG_const_type";
11832 case DW_TAG_constant:
11833 return "DW_TAG_constant";
11834 case DW_TAG_enumerator:
11835 return "DW_TAG_enumerator";
11836 case DW_TAG_file_type:
11837 return "DW_TAG_file_type";
11838 case DW_TAG_friend:
11839 return "DW_TAG_friend";
11840 case DW_TAG_namelist:
11841 return "DW_TAG_namelist";
11842 case DW_TAG_namelist_item:
11843 return "DW_TAG_namelist_item";
11844 case DW_TAG_packed_type:
11845 return "DW_TAG_packed_type";
11846 case DW_TAG_subprogram:
11847 return "DW_TAG_subprogram";
11848 case DW_TAG_template_type_param:
11849 return "DW_TAG_template_type_param";
11850 case DW_TAG_template_value_param:
11851 return "DW_TAG_template_value_param";
11852 case DW_TAG_thrown_type:
11853 return "DW_TAG_thrown_type";
11854 case DW_TAG_try_block:
11855 return "DW_TAG_try_block";
11856 case DW_TAG_variant_part:
11857 return "DW_TAG_variant_part";
11858 case DW_TAG_variable:
11859 return "DW_TAG_variable";
11860 case DW_TAG_volatile_type:
11861 return "DW_TAG_volatile_type";
d9fa45fe
DC
11862 case DW_TAG_dwarf_procedure:
11863 return "DW_TAG_dwarf_procedure";
11864 case DW_TAG_restrict_type:
11865 return "DW_TAG_restrict_type";
11866 case DW_TAG_interface_type:
11867 return "DW_TAG_interface_type";
11868 case DW_TAG_namespace:
11869 return "DW_TAG_namespace";
11870 case DW_TAG_imported_module:
11871 return "DW_TAG_imported_module";
11872 case DW_TAG_unspecified_type:
11873 return "DW_TAG_unspecified_type";
11874 case DW_TAG_partial_unit:
11875 return "DW_TAG_partial_unit";
11876 case DW_TAG_imported_unit:
11877 return "DW_TAG_imported_unit";
b7619582
GF
11878 case DW_TAG_condition:
11879 return "DW_TAG_condition";
11880 case DW_TAG_shared_type:
11881 return "DW_TAG_shared_type";
348e048f
DE
11882 case DW_TAG_type_unit:
11883 return "DW_TAG_type_unit";
c906108c
SS
11884 case DW_TAG_MIPS_loop:
11885 return "DW_TAG_MIPS_loop";
b7619582
GF
11886 case DW_TAG_HP_array_descriptor:
11887 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11888 case DW_TAG_format_label:
11889 return "DW_TAG_format_label";
11890 case DW_TAG_function_template:
11891 return "DW_TAG_function_template";
11892 case DW_TAG_class_template:
11893 return "DW_TAG_class_template";
b7619582
GF
11894 case DW_TAG_GNU_BINCL:
11895 return "DW_TAG_GNU_BINCL";
11896 case DW_TAG_GNU_EINCL:
11897 return "DW_TAG_GNU_EINCL";
11898 case DW_TAG_upc_shared_type:
11899 return "DW_TAG_upc_shared_type";
11900 case DW_TAG_upc_strict_type:
11901 return "DW_TAG_upc_strict_type";
11902 case DW_TAG_upc_relaxed_type:
11903 return "DW_TAG_upc_relaxed_type";
11904 case DW_TAG_PGI_kanji_type:
11905 return "DW_TAG_PGI_kanji_type";
11906 case DW_TAG_PGI_interface_block:
11907 return "DW_TAG_PGI_interface_block";
c906108c
SS
11908 default:
11909 return "DW_TAG_<unknown>";
11910 }
11911}
11912
11913/* Convert a DWARF attribute code into its string name. */
11914
11915static char *
aa1ee363 11916dwarf_attr_name (unsigned attr)
c906108c
SS
11917{
11918 switch (attr)
11919 {
11920 case DW_AT_sibling:
11921 return "DW_AT_sibling";
11922 case DW_AT_location:
11923 return "DW_AT_location";
11924 case DW_AT_name:
11925 return "DW_AT_name";
11926 case DW_AT_ordering:
11927 return "DW_AT_ordering";
11928 case DW_AT_subscr_data:
11929 return "DW_AT_subscr_data";
11930 case DW_AT_byte_size:
11931 return "DW_AT_byte_size";
11932 case DW_AT_bit_offset:
11933 return "DW_AT_bit_offset";
11934 case DW_AT_bit_size:
11935 return "DW_AT_bit_size";
11936 case DW_AT_element_list:
11937 return "DW_AT_element_list";
11938 case DW_AT_stmt_list:
11939 return "DW_AT_stmt_list";
11940 case DW_AT_low_pc:
11941 return "DW_AT_low_pc";
11942 case DW_AT_high_pc:
11943 return "DW_AT_high_pc";
11944 case DW_AT_language:
11945 return "DW_AT_language";
11946 case DW_AT_member:
11947 return "DW_AT_member";
11948 case DW_AT_discr:
11949 return "DW_AT_discr";
11950 case DW_AT_discr_value:
11951 return "DW_AT_discr_value";
11952 case DW_AT_visibility:
11953 return "DW_AT_visibility";
11954 case DW_AT_import:
11955 return "DW_AT_import";
11956 case DW_AT_string_length:
11957 return "DW_AT_string_length";
11958 case DW_AT_common_reference:
11959 return "DW_AT_common_reference";
11960 case DW_AT_comp_dir:
11961 return "DW_AT_comp_dir";
11962 case DW_AT_const_value:
11963 return "DW_AT_const_value";
11964 case DW_AT_containing_type:
11965 return "DW_AT_containing_type";
11966 case DW_AT_default_value:
11967 return "DW_AT_default_value";
11968 case DW_AT_inline:
11969 return "DW_AT_inline";
11970 case DW_AT_is_optional:
11971 return "DW_AT_is_optional";
11972 case DW_AT_lower_bound:
11973 return "DW_AT_lower_bound";
11974 case DW_AT_producer:
11975 return "DW_AT_producer";
11976 case DW_AT_prototyped:
11977 return "DW_AT_prototyped";
11978 case DW_AT_return_addr:
11979 return "DW_AT_return_addr";
11980 case DW_AT_start_scope:
11981 return "DW_AT_start_scope";
09fa0d7c
JK
11982 case DW_AT_bit_stride:
11983 return "DW_AT_bit_stride";
c906108c
SS
11984 case DW_AT_upper_bound:
11985 return "DW_AT_upper_bound";
11986 case DW_AT_abstract_origin:
11987 return "DW_AT_abstract_origin";
11988 case DW_AT_accessibility:
11989 return "DW_AT_accessibility";
11990 case DW_AT_address_class:
11991 return "DW_AT_address_class";
11992 case DW_AT_artificial:
11993 return "DW_AT_artificial";
11994 case DW_AT_base_types:
11995 return "DW_AT_base_types";
11996 case DW_AT_calling_convention:
11997 return "DW_AT_calling_convention";
11998 case DW_AT_count:
11999 return "DW_AT_count";
12000 case DW_AT_data_member_location:
12001 return "DW_AT_data_member_location";
12002 case DW_AT_decl_column:
12003 return "DW_AT_decl_column";
12004 case DW_AT_decl_file:
12005 return "DW_AT_decl_file";
12006 case DW_AT_decl_line:
12007 return "DW_AT_decl_line";
12008 case DW_AT_declaration:
12009 return "DW_AT_declaration";
12010 case DW_AT_discr_list:
12011 return "DW_AT_discr_list";
12012 case DW_AT_encoding:
12013 return "DW_AT_encoding";
12014 case DW_AT_external:
12015 return "DW_AT_external";
12016 case DW_AT_frame_base:
12017 return "DW_AT_frame_base";
12018 case DW_AT_friend:
12019 return "DW_AT_friend";
12020 case DW_AT_identifier_case:
12021 return "DW_AT_identifier_case";
12022 case DW_AT_macro_info:
12023 return "DW_AT_macro_info";
12024 case DW_AT_namelist_items:
12025 return "DW_AT_namelist_items";
12026 case DW_AT_priority:
12027 return "DW_AT_priority";
12028 case DW_AT_segment:
12029 return "DW_AT_segment";
12030 case DW_AT_specification:
12031 return "DW_AT_specification";
12032 case DW_AT_static_link:
12033 return "DW_AT_static_link";
12034 case DW_AT_type:
12035 return "DW_AT_type";
12036 case DW_AT_use_location:
12037 return "DW_AT_use_location";
12038 case DW_AT_variable_parameter:
12039 return "DW_AT_variable_parameter";
12040 case DW_AT_virtuality:
12041 return "DW_AT_virtuality";
12042 case DW_AT_vtable_elem_location:
12043 return "DW_AT_vtable_elem_location";
b7619582 12044 /* DWARF 3 values. */
d9fa45fe
DC
12045 case DW_AT_allocated:
12046 return "DW_AT_allocated";
12047 case DW_AT_associated:
12048 return "DW_AT_associated";
12049 case DW_AT_data_location:
12050 return "DW_AT_data_location";
09fa0d7c
JK
12051 case DW_AT_byte_stride:
12052 return "DW_AT_byte_stride";
d9fa45fe
DC
12053 case DW_AT_entry_pc:
12054 return "DW_AT_entry_pc";
12055 case DW_AT_use_UTF8:
12056 return "DW_AT_use_UTF8";
12057 case DW_AT_extension:
12058 return "DW_AT_extension";
12059 case DW_AT_ranges:
12060 return "DW_AT_ranges";
12061 case DW_AT_trampoline:
12062 return "DW_AT_trampoline";
12063 case DW_AT_call_column:
12064 return "DW_AT_call_column";
12065 case DW_AT_call_file:
12066 return "DW_AT_call_file";
12067 case DW_AT_call_line:
12068 return "DW_AT_call_line";
b7619582
GF
12069 case DW_AT_description:
12070 return "DW_AT_description";
12071 case DW_AT_binary_scale:
12072 return "DW_AT_binary_scale";
12073 case DW_AT_decimal_scale:
12074 return "DW_AT_decimal_scale";
12075 case DW_AT_small:
12076 return "DW_AT_small";
12077 case DW_AT_decimal_sign:
12078 return "DW_AT_decimal_sign";
12079 case DW_AT_digit_count:
12080 return "DW_AT_digit_count";
12081 case DW_AT_picture_string:
12082 return "DW_AT_picture_string";
12083 case DW_AT_mutable:
12084 return "DW_AT_mutable";
12085 case DW_AT_threads_scaled:
12086 return "DW_AT_threads_scaled";
12087 case DW_AT_explicit:
12088 return "DW_AT_explicit";
12089 case DW_AT_object_pointer:
12090 return "DW_AT_object_pointer";
12091 case DW_AT_endianity:
12092 return "DW_AT_endianity";
12093 case DW_AT_elemental:
12094 return "DW_AT_elemental";
12095 case DW_AT_pure:
12096 return "DW_AT_pure";
12097 case DW_AT_recursive:
12098 return "DW_AT_recursive";
348e048f
DE
12099 /* DWARF 4 values. */
12100 case DW_AT_signature:
12101 return "DW_AT_signature";
31ef98ae
TT
12102 case DW_AT_linkage_name:
12103 return "DW_AT_linkage_name";
b7619582 12104 /* SGI/MIPS extensions. */
c764a876 12105#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12106 case DW_AT_MIPS_fde:
12107 return "DW_AT_MIPS_fde";
c764a876 12108#endif
c906108c
SS
12109 case DW_AT_MIPS_loop_begin:
12110 return "DW_AT_MIPS_loop_begin";
12111 case DW_AT_MIPS_tail_loop_begin:
12112 return "DW_AT_MIPS_tail_loop_begin";
12113 case DW_AT_MIPS_epilog_begin:
12114 return "DW_AT_MIPS_epilog_begin";
12115 case DW_AT_MIPS_loop_unroll_factor:
12116 return "DW_AT_MIPS_loop_unroll_factor";
12117 case DW_AT_MIPS_software_pipeline_depth:
12118 return "DW_AT_MIPS_software_pipeline_depth";
12119 case DW_AT_MIPS_linkage_name:
12120 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12121 case DW_AT_MIPS_stride:
12122 return "DW_AT_MIPS_stride";
12123 case DW_AT_MIPS_abstract_name:
12124 return "DW_AT_MIPS_abstract_name";
12125 case DW_AT_MIPS_clone_origin:
12126 return "DW_AT_MIPS_clone_origin";
12127 case DW_AT_MIPS_has_inlines:
12128 return "DW_AT_MIPS_has_inlines";
b7619582 12129 /* HP extensions. */
c764a876 12130#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12131 case DW_AT_HP_block_index:
12132 return "DW_AT_HP_block_index";
c764a876 12133#endif
b7619582
GF
12134 case DW_AT_HP_unmodifiable:
12135 return "DW_AT_HP_unmodifiable";
12136 case DW_AT_HP_actuals_stmt_list:
12137 return "DW_AT_HP_actuals_stmt_list";
12138 case DW_AT_HP_proc_per_section:
12139 return "DW_AT_HP_proc_per_section";
12140 case DW_AT_HP_raw_data_ptr:
12141 return "DW_AT_HP_raw_data_ptr";
12142 case DW_AT_HP_pass_by_reference:
12143 return "DW_AT_HP_pass_by_reference";
12144 case DW_AT_HP_opt_level:
12145 return "DW_AT_HP_opt_level";
12146 case DW_AT_HP_prof_version_id:
12147 return "DW_AT_HP_prof_version_id";
12148 case DW_AT_HP_opt_flags:
12149 return "DW_AT_HP_opt_flags";
12150 case DW_AT_HP_cold_region_low_pc:
12151 return "DW_AT_HP_cold_region_low_pc";
12152 case DW_AT_HP_cold_region_high_pc:
12153 return "DW_AT_HP_cold_region_high_pc";
12154 case DW_AT_HP_all_variables_modifiable:
12155 return "DW_AT_HP_all_variables_modifiable";
12156 case DW_AT_HP_linkage_name:
12157 return "DW_AT_HP_linkage_name";
12158 case DW_AT_HP_prof_flags:
12159 return "DW_AT_HP_prof_flags";
12160 /* GNU extensions. */
c906108c
SS
12161 case DW_AT_sf_names:
12162 return "DW_AT_sf_names";
12163 case DW_AT_src_info:
12164 return "DW_AT_src_info";
12165 case DW_AT_mac_info:
12166 return "DW_AT_mac_info";
12167 case DW_AT_src_coords:
12168 return "DW_AT_src_coords";
12169 case DW_AT_body_begin:
12170 return "DW_AT_body_begin";
12171 case DW_AT_body_end:
12172 return "DW_AT_body_end";
f5f8a009
EZ
12173 case DW_AT_GNU_vector:
12174 return "DW_AT_GNU_vector";
2de00c64
DE
12175 case DW_AT_GNU_odr_signature:
12176 return "DW_AT_GNU_odr_signature";
b7619582
GF
12177 /* VMS extensions. */
12178 case DW_AT_VMS_rtnbeg_pd_address:
12179 return "DW_AT_VMS_rtnbeg_pd_address";
12180 /* UPC extension. */
12181 case DW_AT_upc_threads_scaled:
12182 return "DW_AT_upc_threads_scaled";
12183 /* PGI (STMicroelectronics) extensions. */
12184 case DW_AT_PGI_lbase:
12185 return "DW_AT_PGI_lbase";
12186 case DW_AT_PGI_soffset:
12187 return "DW_AT_PGI_soffset";
12188 case DW_AT_PGI_lstride:
12189 return "DW_AT_PGI_lstride";
c906108c
SS
12190 default:
12191 return "DW_AT_<unknown>";
12192 }
12193}
12194
12195/* Convert a DWARF value form code into its string name. */
12196
12197static char *
aa1ee363 12198dwarf_form_name (unsigned form)
c906108c
SS
12199{
12200 switch (form)
12201 {
12202 case DW_FORM_addr:
12203 return "DW_FORM_addr";
12204 case DW_FORM_block2:
12205 return "DW_FORM_block2";
12206 case DW_FORM_block4:
12207 return "DW_FORM_block4";
12208 case DW_FORM_data2:
12209 return "DW_FORM_data2";
12210 case DW_FORM_data4:
12211 return "DW_FORM_data4";
12212 case DW_FORM_data8:
12213 return "DW_FORM_data8";
12214 case DW_FORM_string:
12215 return "DW_FORM_string";
12216 case DW_FORM_block:
12217 return "DW_FORM_block";
12218 case DW_FORM_block1:
12219 return "DW_FORM_block1";
12220 case DW_FORM_data1:
12221 return "DW_FORM_data1";
12222 case DW_FORM_flag:
12223 return "DW_FORM_flag";
12224 case DW_FORM_sdata:
12225 return "DW_FORM_sdata";
12226 case DW_FORM_strp:
12227 return "DW_FORM_strp";
12228 case DW_FORM_udata:
12229 return "DW_FORM_udata";
12230 case DW_FORM_ref_addr:
12231 return "DW_FORM_ref_addr";
12232 case DW_FORM_ref1:
12233 return "DW_FORM_ref1";
12234 case DW_FORM_ref2:
12235 return "DW_FORM_ref2";
12236 case DW_FORM_ref4:
12237 return "DW_FORM_ref4";
12238 case DW_FORM_ref8:
12239 return "DW_FORM_ref8";
12240 case DW_FORM_ref_udata:
12241 return "DW_FORM_ref_udata";
12242 case DW_FORM_indirect:
12243 return "DW_FORM_indirect";
348e048f
DE
12244 case DW_FORM_sec_offset:
12245 return "DW_FORM_sec_offset";
12246 case DW_FORM_exprloc:
12247 return "DW_FORM_exprloc";
12248 case DW_FORM_flag_present:
12249 return "DW_FORM_flag_present";
12250 case DW_FORM_sig8:
12251 return "DW_FORM_sig8";
c906108c
SS
12252 default:
12253 return "DW_FORM_<unknown>";
12254 }
12255}
12256
12257/* Convert a DWARF stack opcode into its string name. */
12258
9eae7c52
TT
12259const char *
12260dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12261{
12262 switch (op)
12263 {
12264 case DW_OP_addr:
12265 return "DW_OP_addr";
12266 case DW_OP_deref:
12267 return "DW_OP_deref";
12268 case DW_OP_const1u:
12269 return "DW_OP_const1u";
12270 case DW_OP_const1s:
12271 return "DW_OP_const1s";
12272 case DW_OP_const2u:
12273 return "DW_OP_const2u";
12274 case DW_OP_const2s:
12275 return "DW_OP_const2s";
12276 case DW_OP_const4u:
12277 return "DW_OP_const4u";
12278 case DW_OP_const4s:
12279 return "DW_OP_const4s";
12280 case DW_OP_const8u:
12281 return "DW_OP_const8u";
12282 case DW_OP_const8s:
12283 return "DW_OP_const8s";
12284 case DW_OP_constu:
12285 return "DW_OP_constu";
12286 case DW_OP_consts:
12287 return "DW_OP_consts";
12288 case DW_OP_dup:
12289 return "DW_OP_dup";
12290 case DW_OP_drop:
12291 return "DW_OP_drop";
12292 case DW_OP_over:
12293 return "DW_OP_over";
12294 case DW_OP_pick:
12295 return "DW_OP_pick";
12296 case DW_OP_swap:
12297 return "DW_OP_swap";
12298 case DW_OP_rot:
12299 return "DW_OP_rot";
12300 case DW_OP_xderef:
12301 return "DW_OP_xderef";
12302 case DW_OP_abs:
12303 return "DW_OP_abs";
12304 case DW_OP_and:
12305 return "DW_OP_and";
12306 case DW_OP_div:
12307 return "DW_OP_div";
12308 case DW_OP_minus:
12309 return "DW_OP_minus";
12310 case DW_OP_mod:
12311 return "DW_OP_mod";
12312 case DW_OP_mul:
12313 return "DW_OP_mul";
12314 case DW_OP_neg:
12315 return "DW_OP_neg";
12316 case DW_OP_not:
12317 return "DW_OP_not";
12318 case DW_OP_or:
12319 return "DW_OP_or";
12320 case DW_OP_plus:
12321 return "DW_OP_plus";
12322 case DW_OP_plus_uconst:
12323 return "DW_OP_plus_uconst";
12324 case DW_OP_shl:
12325 return "DW_OP_shl";
12326 case DW_OP_shr:
12327 return "DW_OP_shr";
12328 case DW_OP_shra:
12329 return "DW_OP_shra";
12330 case DW_OP_xor:
12331 return "DW_OP_xor";
12332 case DW_OP_bra:
12333 return "DW_OP_bra";
12334 case DW_OP_eq:
12335 return "DW_OP_eq";
12336 case DW_OP_ge:
12337 return "DW_OP_ge";
12338 case DW_OP_gt:
12339 return "DW_OP_gt";
12340 case DW_OP_le:
12341 return "DW_OP_le";
12342 case DW_OP_lt:
12343 return "DW_OP_lt";
12344 case DW_OP_ne:
12345 return "DW_OP_ne";
12346 case DW_OP_skip:
12347 return "DW_OP_skip";
12348 case DW_OP_lit0:
12349 return "DW_OP_lit0";
12350 case DW_OP_lit1:
12351 return "DW_OP_lit1";
12352 case DW_OP_lit2:
12353 return "DW_OP_lit2";
12354 case DW_OP_lit3:
12355 return "DW_OP_lit3";
12356 case DW_OP_lit4:
12357 return "DW_OP_lit4";
12358 case DW_OP_lit5:
12359 return "DW_OP_lit5";
12360 case DW_OP_lit6:
12361 return "DW_OP_lit6";
12362 case DW_OP_lit7:
12363 return "DW_OP_lit7";
12364 case DW_OP_lit8:
12365 return "DW_OP_lit8";
12366 case DW_OP_lit9:
12367 return "DW_OP_lit9";
12368 case DW_OP_lit10:
12369 return "DW_OP_lit10";
12370 case DW_OP_lit11:
12371 return "DW_OP_lit11";
12372 case DW_OP_lit12:
12373 return "DW_OP_lit12";
12374 case DW_OP_lit13:
12375 return "DW_OP_lit13";
12376 case DW_OP_lit14:
12377 return "DW_OP_lit14";
12378 case DW_OP_lit15:
12379 return "DW_OP_lit15";
12380 case DW_OP_lit16:
12381 return "DW_OP_lit16";
12382 case DW_OP_lit17:
12383 return "DW_OP_lit17";
12384 case DW_OP_lit18:
12385 return "DW_OP_lit18";
12386 case DW_OP_lit19:
12387 return "DW_OP_lit19";
12388 case DW_OP_lit20:
12389 return "DW_OP_lit20";
12390 case DW_OP_lit21:
12391 return "DW_OP_lit21";
12392 case DW_OP_lit22:
12393 return "DW_OP_lit22";
12394 case DW_OP_lit23:
12395 return "DW_OP_lit23";
12396 case DW_OP_lit24:
12397 return "DW_OP_lit24";
12398 case DW_OP_lit25:
12399 return "DW_OP_lit25";
12400 case DW_OP_lit26:
12401 return "DW_OP_lit26";
12402 case DW_OP_lit27:
12403 return "DW_OP_lit27";
12404 case DW_OP_lit28:
12405 return "DW_OP_lit28";
12406 case DW_OP_lit29:
12407 return "DW_OP_lit29";
12408 case DW_OP_lit30:
12409 return "DW_OP_lit30";
12410 case DW_OP_lit31:
12411 return "DW_OP_lit31";
12412 case DW_OP_reg0:
12413 return "DW_OP_reg0";
12414 case DW_OP_reg1:
12415 return "DW_OP_reg1";
12416 case DW_OP_reg2:
12417 return "DW_OP_reg2";
12418 case DW_OP_reg3:
12419 return "DW_OP_reg3";
12420 case DW_OP_reg4:
12421 return "DW_OP_reg4";
12422 case DW_OP_reg5:
12423 return "DW_OP_reg5";
12424 case DW_OP_reg6:
12425 return "DW_OP_reg6";
12426 case DW_OP_reg7:
12427 return "DW_OP_reg7";
12428 case DW_OP_reg8:
12429 return "DW_OP_reg8";
12430 case DW_OP_reg9:
12431 return "DW_OP_reg9";
12432 case DW_OP_reg10:
12433 return "DW_OP_reg10";
12434 case DW_OP_reg11:
12435 return "DW_OP_reg11";
12436 case DW_OP_reg12:
12437 return "DW_OP_reg12";
12438 case DW_OP_reg13:
12439 return "DW_OP_reg13";
12440 case DW_OP_reg14:
12441 return "DW_OP_reg14";
12442 case DW_OP_reg15:
12443 return "DW_OP_reg15";
12444 case DW_OP_reg16:
12445 return "DW_OP_reg16";
12446 case DW_OP_reg17:
12447 return "DW_OP_reg17";
12448 case DW_OP_reg18:
12449 return "DW_OP_reg18";
12450 case DW_OP_reg19:
12451 return "DW_OP_reg19";
12452 case DW_OP_reg20:
12453 return "DW_OP_reg20";
12454 case DW_OP_reg21:
12455 return "DW_OP_reg21";
12456 case DW_OP_reg22:
12457 return "DW_OP_reg22";
12458 case DW_OP_reg23:
12459 return "DW_OP_reg23";
12460 case DW_OP_reg24:
12461 return "DW_OP_reg24";
12462 case DW_OP_reg25:
12463 return "DW_OP_reg25";
12464 case DW_OP_reg26:
12465 return "DW_OP_reg26";
12466 case DW_OP_reg27:
12467 return "DW_OP_reg27";
12468 case DW_OP_reg28:
12469 return "DW_OP_reg28";
12470 case DW_OP_reg29:
12471 return "DW_OP_reg29";
12472 case DW_OP_reg30:
12473 return "DW_OP_reg30";
12474 case DW_OP_reg31:
12475 return "DW_OP_reg31";
12476 case DW_OP_breg0:
12477 return "DW_OP_breg0";
12478 case DW_OP_breg1:
12479 return "DW_OP_breg1";
12480 case DW_OP_breg2:
12481 return "DW_OP_breg2";
12482 case DW_OP_breg3:
12483 return "DW_OP_breg3";
12484 case DW_OP_breg4:
12485 return "DW_OP_breg4";
12486 case DW_OP_breg5:
12487 return "DW_OP_breg5";
12488 case DW_OP_breg6:
12489 return "DW_OP_breg6";
12490 case DW_OP_breg7:
12491 return "DW_OP_breg7";
12492 case DW_OP_breg8:
12493 return "DW_OP_breg8";
12494 case DW_OP_breg9:
12495 return "DW_OP_breg9";
12496 case DW_OP_breg10:
12497 return "DW_OP_breg10";
12498 case DW_OP_breg11:
12499 return "DW_OP_breg11";
12500 case DW_OP_breg12:
12501 return "DW_OP_breg12";
12502 case DW_OP_breg13:
12503 return "DW_OP_breg13";
12504 case DW_OP_breg14:
12505 return "DW_OP_breg14";
12506 case DW_OP_breg15:
12507 return "DW_OP_breg15";
12508 case DW_OP_breg16:
12509 return "DW_OP_breg16";
12510 case DW_OP_breg17:
12511 return "DW_OP_breg17";
12512 case DW_OP_breg18:
12513 return "DW_OP_breg18";
12514 case DW_OP_breg19:
12515 return "DW_OP_breg19";
12516 case DW_OP_breg20:
12517 return "DW_OP_breg20";
12518 case DW_OP_breg21:
12519 return "DW_OP_breg21";
12520 case DW_OP_breg22:
12521 return "DW_OP_breg22";
12522 case DW_OP_breg23:
12523 return "DW_OP_breg23";
12524 case DW_OP_breg24:
12525 return "DW_OP_breg24";
12526 case DW_OP_breg25:
12527 return "DW_OP_breg25";
12528 case DW_OP_breg26:
12529 return "DW_OP_breg26";
12530 case DW_OP_breg27:
12531 return "DW_OP_breg27";
12532 case DW_OP_breg28:
12533 return "DW_OP_breg28";
12534 case DW_OP_breg29:
12535 return "DW_OP_breg29";
12536 case DW_OP_breg30:
12537 return "DW_OP_breg30";
12538 case DW_OP_breg31:
12539 return "DW_OP_breg31";
12540 case DW_OP_regx:
12541 return "DW_OP_regx";
12542 case DW_OP_fbreg:
12543 return "DW_OP_fbreg";
12544 case DW_OP_bregx:
12545 return "DW_OP_bregx";
12546 case DW_OP_piece:
12547 return "DW_OP_piece";
12548 case DW_OP_deref_size:
12549 return "DW_OP_deref_size";
12550 case DW_OP_xderef_size:
12551 return "DW_OP_xderef_size";
12552 case DW_OP_nop:
12553 return "DW_OP_nop";
b7619582 12554 /* DWARF 3 extensions. */
ed348acc
EZ
12555 case DW_OP_push_object_address:
12556 return "DW_OP_push_object_address";
12557 case DW_OP_call2:
12558 return "DW_OP_call2";
12559 case DW_OP_call4:
12560 return "DW_OP_call4";
12561 case DW_OP_call_ref:
12562 return "DW_OP_call_ref";
b7619582
GF
12563 case DW_OP_form_tls_address:
12564 return "DW_OP_form_tls_address";
12565 case DW_OP_call_frame_cfa:
12566 return "DW_OP_call_frame_cfa";
12567 case DW_OP_bit_piece:
12568 return "DW_OP_bit_piece";
9eae7c52
TT
12569 /* DWARF 4 extensions. */
12570 case DW_OP_implicit_value:
12571 return "DW_OP_implicit_value";
12572 case DW_OP_stack_value:
12573 return "DW_OP_stack_value";
12574 /* GNU extensions. */
ed348acc
EZ
12575 case DW_OP_GNU_push_tls_address:
12576 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12577 case DW_OP_GNU_uninit:
12578 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12579 case DW_OP_GNU_implicit_pointer:
12580 return "DW_OP_GNU_implicit_pointer";
c906108c 12581 default:
9eae7c52 12582 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12583 }
12584}
12585
12586static char *
fba45db2 12587dwarf_bool_name (unsigned mybool)
c906108c
SS
12588{
12589 if (mybool)
12590 return "TRUE";
12591 else
12592 return "FALSE";
12593}
12594
12595/* Convert a DWARF type code into its string name. */
12596
12597static char *
aa1ee363 12598dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12599{
12600 switch (enc)
12601 {
b7619582
GF
12602 case DW_ATE_void:
12603 return "DW_ATE_void";
c906108c
SS
12604 case DW_ATE_address:
12605 return "DW_ATE_address";
12606 case DW_ATE_boolean:
12607 return "DW_ATE_boolean";
12608 case DW_ATE_complex_float:
12609 return "DW_ATE_complex_float";
12610 case DW_ATE_float:
12611 return "DW_ATE_float";
12612 case DW_ATE_signed:
12613 return "DW_ATE_signed";
12614 case DW_ATE_signed_char:
12615 return "DW_ATE_signed_char";
12616 case DW_ATE_unsigned:
12617 return "DW_ATE_unsigned";
12618 case DW_ATE_unsigned_char:
12619 return "DW_ATE_unsigned_char";
b7619582 12620 /* DWARF 3. */
d9fa45fe
DC
12621 case DW_ATE_imaginary_float:
12622 return "DW_ATE_imaginary_float";
b7619582
GF
12623 case DW_ATE_packed_decimal:
12624 return "DW_ATE_packed_decimal";
12625 case DW_ATE_numeric_string:
12626 return "DW_ATE_numeric_string";
12627 case DW_ATE_edited:
12628 return "DW_ATE_edited";
12629 case DW_ATE_signed_fixed:
12630 return "DW_ATE_signed_fixed";
12631 case DW_ATE_unsigned_fixed:
12632 return "DW_ATE_unsigned_fixed";
12633 case DW_ATE_decimal_float:
12634 return "DW_ATE_decimal_float";
75079b2b
TT
12635 /* DWARF 4. */
12636 case DW_ATE_UTF:
12637 return "DW_ATE_UTF";
b7619582
GF
12638 /* HP extensions. */
12639 case DW_ATE_HP_float80:
12640 return "DW_ATE_HP_float80";
12641 case DW_ATE_HP_complex_float80:
12642 return "DW_ATE_HP_complex_float80";
12643 case DW_ATE_HP_float128:
12644 return "DW_ATE_HP_float128";
12645 case DW_ATE_HP_complex_float128:
12646 return "DW_ATE_HP_complex_float128";
12647 case DW_ATE_HP_floathpintel:
12648 return "DW_ATE_HP_floathpintel";
12649 case DW_ATE_HP_imaginary_float80:
12650 return "DW_ATE_HP_imaginary_float80";
12651 case DW_ATE_HP_imaginary_float128:
12652 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12653 default:
12654 return "DW_ATE_<unknown>";
12655 }
12656}
12657
12658/* Convert a DWARF call frame info operation to its string name. */
12659
12660#if 0
12661static char *
aa1ee363 12662dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12663{
12664 switch (cfi_opc)
12665 {
12666 case DW_CFA_advance_loc:
12667 return "DW_CFA_advance_loc";
12668 case DW_CFA_offset:
12669 return "DW_CFA_offset";
12670 case DW_CFA_restore:
12671 return "DW_CFA_restore";
12672 case DW_CFA_nop:
12673 return "DW_CFA_nop";
12674 case DW_CFA_set_loc:
12675 return "DW_CFA_set_loc";
12676 case DW_CFA_advance_loc1:
12677 return "DW_CFA_advance_loc1";
12678 case DW_CFA_advance_loc2:
12679 return "DW_CFA_advance_loc2";
12680 case DW_CFA_advance_loc4:
12681 return "DW_CFA_advance_loc4";
12682 case DW_CFA_offset_extended:
12683 return "DW_CFA_offset_extended";
12684 case DW_CFA_restore_extended:
12685 return "DW_CFA_restore_extended";
12686 case DW_CFA_undefined:
12687 return "DW_CFA_undefined";
12688 case DW_CFA_same_value:
12689 return "DW_CFA_same_value";
12690 case DW_CFA_register:
12691 return "DW_CFA_register";
12692 case DW_CFA_remember_state:
12693 return "DW_CFA_remember_state";
12694 case DW_CFA_restore_state:
12695 return "DW_CFA_restore_state";
12696 case DW_CFA_def_cfa:
12697 return "DW_CFA_def_cfa";
12698 case DW_CFA_def_cfa_register:
12699 return "DW_CFA_def_cfa_register";
12700 case DW_CFA_def_cfa_offset:
12701 return "DW_CFA_def_cfa_offset";
b7619582 12702 /* DWARF 3. */
985cb1a3
JM
12703 case DW_CFA_def_cfa_expression:
12704 return "DW_CFA_def_cfa_expression";
12705 case DW_CFA_expression:
12706 return "DW_CFA_expression";
12707 case DW_CFA_offset_extended_sf:
12708 return "DW_CFA_offset_extended_sf";
12709 case DW_CFA_def_cfa_sf:
12710 return "DW_CFA_def_cfa_sf";
12711 case DW_CFA_def_cfa_offset_sf:
12712 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12713 case DW_CFA_val_offset:
12714 return "DW_CFA_val_offset";
12715 case DW_CFA_val_offset_sf:
12716 return "DW_CFA_val_offset_sf";
12717 case DW_CFA_val_expression:
12718 return "DW_CFA_val_expression";
12719 /* SGI/MIPS specific. */
c906108c
SS
12720 case DW_CFA_MIPS_advance_loc8:
12721 return "DW_CFA_MIPS_advance_loc8";
b7619582 12722 /* GNU extensions. */
985cb1a3
JM
12723 case DW_CFA_GNU_window_save:
12724 return "DW_CFA_GNU_window_save";
12725 case DW_CFA_GNU_args_size:
12726 return "DW_CFA_GNU_args_size";
12727 case DW_CFA_GNU_negative_offset_extended:
12728 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12729 default:
12730 return "DW_CFA_<unknown>";
12731 }
12732}
12733#endif
12734
f9aca02d 12735static void
d97bc12b 12736dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12737{
12738 unsigned int i;
12739
d97bc12b
DE
12740 print_spaces (indent, f);
12741 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12742 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12743
12744 if (die->parent != NULL)
12745 {
12746 print_spaces (indent, f);
12747 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12748 die->parent->offset);
12749 }
12750
12751 print_spaces (indent, f);
12752 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12753 dwarf_bool_name (die->child != NULL));
c906108c 12754
d97bc12b
DE
12755 print_spaces (indent, f);
12756 fprintf_unfiltered (f, " attributes:\n");
12757
c906108c
SS
12758 for (i = 0; i < die->num_attrs; ++i)
12759 {
d97bc12b
DE
12760 print_spaces (indent, f);
12761 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12762 dwarf_attr_name (die->attrs[i].name),
12763 dwarf_form_name (die->attrs[i].form));
d97bc12b 12764
c906108c
SS
12765 switch (die->attrs[i].form)
12766 {
12767 case DW_FORM_ref_addr:
12768 case DW_FORM_addr:
d97bc12b 12769 fprintf_unfiltered (f, "address: ");
5af949e3 12770 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12771 break;
12772 case DW_FORM_block2:
12773 case DW_FORM_block4:
12774 case DW_FORM_block:
12775 case DW_FORM_block1:
d97bc12b 12776 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12777 break;
2dc7f7b3
TT
12778 case DW_FORM_exprloc:
12779 fprintf_unfiltered (f, "expression: size %u",
12780 DW_BLOCK (&die->attrs[i])->size);
12781 break;
10b3939b
DJ
12782 case DW_FORM_ref1:
12783 case DW_FORM_ref2:
12784 case DW_FORM_ref4:
d97bc12b 12785 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12786 (long) (DW_ADDR (&die->attrs[i])));
12787 break;
c906108c
SS
12788 case DW_FORM_data1:
12789 case DW_FORM_data2:
12790 case DW_FORM_data4:
ce5d95e1 12791 case DW_FORM_data8:
c906108c
SS
12792 case DW_FORM_udata:
12793 case DW_FORM_sdata:
43bbcdc2
PH
12794 fprintf_unfiltered (f, "constant: %s",
12795 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12796 break;
2dc7f7b3
TT
12797 case DW_FORM_sec_offset:
12798 fprintf_unfiltered (f, "section offset: %s",
12799 pulongest (DW_UNSND (&die->attrs[i])));
12800 break;
348e048f
DE
12801 case DW_FORM_sig8:
12802 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12803 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12804 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12805 else
12806 fprintf_unfiltered (f, "signatured type, offset: unknown");
12807 break;
c906108c 12808 case DW_FORM_string:
4bdf3d34 12809 case DW_FORM_strp:
8285870a 12810 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12811 DW_STRING (&die->attrs[i])
8285870a
JK
12812 ? DW_STRING (&die->attrs[i]) : "",
12813 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12814 break;
12815 case DW_FORM_flag:
12816 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12817 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12818 else
d97bc12b 12819 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12820 break;
2dc7f7b3
TT
12821 case DW_FORM_flag_present:
12822 fprintf_unfiltered (f, "flag: TRUE");
12823 break;
a8329558
KW
12824 case DW_FORM_indirect:
12825 /* the reader will have reduced the indirect form to
12826 the "base form" so this form should not occur */
d97bc12b 12827 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12828 break;
c906108c 12829 default:
d97bc12b 12830 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12831 die->attrs[i].form);
d97bc12b 12832 break;
c906108c 12833 }
d97bc12b 12834 fprintf_unfiltered (f, "\n");
c906108c
SS
12835 }
12836}
12837
f9aca02d 12838static void
d97bc12b 12839dump_die_for_error (struct die_info *die)
c906108c 12840{
d97bc12b
DE
12841 dump_die_shallow (gdb_stderr, 0, die);
12842}
12843
12844static void
12845dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12846{
12847 int indent = level * 4;
12848
12849 gdb_assert (die != NULL);
12850
12851 if (level >= max_level)
12852 return;
12853
12854 dump_die_shallow (f, indent, die);
12855
12856 if (die->child != NULL)
c906108c 12857 {
d97bc12b
DE
12858 print_spaces (indent, f);
12859 fprintf_unfiltered (f, " Children:");
12860 if (level + 1 < max_level)
12861 {
12862 fprintf_unfiltered (f, "\n");
12863 dump_die_1 (f, level + 1, max_level, die->child);
12864 }
12865 else
12866 {
12867 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12868 }
12869 }
12870
12871 if (die->sibling != NULL && level > 0)
12872 {
12873 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12874 }
12875}
12876
d97bc12b
DE
12877/* This is called from the pdie macro in gdbinit.in.
12878 It's not static so gcc will keep a copy callable from gdb. */
12879
12880void
12881dump_die (struct die_info *die, int max_level)
12882{
12883 dump_die_1 (gdb_stdlog, 0, max_level, die);
12884}
12885
f9aca02d 12886static void
51545339 12887store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12888{
51545339 12889 void **slot;
c906108c 12890
51545339
DJ
12891 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12892
12893 *slot = die;
c906108c
SS
12894}
12895
93311388
DE
12896static int
12897is_ref_attr (struct attribute *attr)
c906108c 12898{
c906108c
SS
12899 switch (attr->form)
12900 {
12901 case DW_FORM_ref_addr:
c906108c
SS
12902 case DW_FORM_ref1:
12903 case DW_FORM_ref2:
12904 case DW_FORM_ref4:
613e1657 12905 case DW_FORM_ref8:
c906108c 12906 case DW_FORM_ref_udata:
93311388 12907 return 1;
c906108c 12908 default:
93311388 12909 return 0;
c906108c 12910 }
93311388
DE
12911}
12912
12913static unsigned int
12914dwarf2_get_ref_die_offset (struct attribute *attr)
12915{
12916 if (is_ref_attr (attr))
12917 return DW_ADDR (attr);
12918
12919 complaint (&symfile_complaints,
12920 _("unsupported die ref attribute form: '%s'"),
12921 dwarf_form_name (attr->form));
12922 return 0;
c906108c
SS
12923}
12924
43bbcdc2
PH
12925/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12926 * the value held by the attribute is not constant. */
a02abb62 12927
43bbcdc2 12928static LONGEST
a02abb62
JB
12929dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12930{
12931 if (attr->form == DW_FORM_sdata)
12932 return DW_SND (attr);
12933 else if (attr->form == DW_FORM_udata
12934 || attr->form == DW_FORM_data1
12935 || attr->form == DW_FORM_data2
12936 || attr->form == DW_FORM_data4
12937 || attr->form == DW_FORM_data8)
12938 return DW_UNSND (attr);
12939 else
12940 {
e2e0b3e5 12941 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12942 dwarf_form_name (attr->form));
12943 return default_value;
12944 }
12945}
12946
03dd20cc 12947/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12948 unit and add it to our queue.
12949 The result is non-zero if PER_CU was queued, otherwise the result is zero
12950 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12951
348e048f 12952static int
03dd20cc
DJ
12953maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12954 struct dwarf2_per_cu_data *per_cu)
12955{
98bfdba5
PA
12956 /* We may arrive here during partial symbol reading, if we need full
12957 DIEs to process an unusual case (e.g. template arguments). Do
12958 not queue PER_CU, just tell our caller to load its DIEs. */
12959 if (dwarf2_per_objfile->reading_partial_symbols)
12960 {
12961 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12962 return 1;
12963 return 0;
12964 }
12965
03dd20cc
DJ
12966 /* Mark the dependence relation so that we don't flush PER_CU
12967 too early. */
12968 dwarf2_add_dependence (this_cu, per_cu);
12969
12970 /* If it's already on the queue, we have nothing to do. */
12971 if (per_cu->queued)
348e048f 12972 return 0;
03dd20cc
DJ
12973
12974 /* If the compilation unit is already loaded, just mark it as
12975 used. */
12976 if (per_cu->cu != NULL)
12977 {
12978 per_cu->cu->last_used = 0;
348e048f 12979 return 0;
03dd20cc
DJ
12980 }
12981
12982 /* Add it to the queue. */
12983 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12984
12985 return 1;
12986}
12987
12988/* Follow reference or signature attribute ATTR of SRC_DIE.
12989 On entry *REF_CU is the CU of SRC_DIE.
12990 On exit *REF_CU is the CU of the result. */
12991
12992static struct die_info *
12993follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12994 struct dwarf2_cu **ref_cu)
12995{
12996 struct die_info *die;
12997
12998 if (is_ref_attr (attr))
12999 die = follow_die_ref (src_die, attr, ref_cu);
13000 else if (attr->form == DW_FORM_sig8)
13001 die = follow_die_sig (src_die, attr, ref_cu);
13002 else
13003 {
13004 dump_die_for_error (src_die);
13005 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13006 (*ref_cu)->objfile->name);
13007 }
13008
13009 return die;
03dd20cc
DJ
13010}
13011
5c631832 13012/* Follow reference OFFSET.
673bfd45
DE
13013 On entry *REF_CU is the CU of the source die referencing OFFSET.
13014 On exit *REF_CU is the CU of the result.
13015 Returns NULL if OFFSET is invalid. */
f504f079 13016
f9aca02d 13017static struct die_info *
5c631832 13018follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13019{
10b3939b 13020 struct die_info temp_die;
f2f0e013 13021 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13022
348e048f
DE
13023 gdb_assert (cu->per_cu != NULL);
13024
98bfdba5
PA
13025 target_cu = cu;
13026
348e048f
DE
13027 if (cu->per_cu->from_debug_types)
13028 {
13029 /* .debug_types CUs cannot reference anything outside their CU.
13030 If they need to, they have to reference a signatured type via
13031 DW_FORM_sig8. */
13032 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13033 return NULL;
348e048f
DE
13034 }
13035 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13036 {
13037 struct dwarf2_per_cu_data *per_cu;
9a619af0 13038
45452591 13039 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13040
13041 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13042 if (maybe_queue_comp_unit (cu, per_cu))
13043 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13044
10b3939b
DJ
13045 target_cu = per_cu->cu;
13046 }
98bfdba5
PA
13047 else if (cu->dies == NULL)
13048 {
13049 /* We're loading full DIEs during partial symbol reading. */
13050 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13051 load_full_comp_unit (cu->per_cu, cu->objfile);
13052 }
c906108c 13053
f2f0e013 13054 *ref_cu = target_cu;
51545339 13055 temp_die.offset = offset;
5c631832
JK
13056 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13057}
10b3939b 13058
5c631832
JK
13059/* Follow reference attribute ATTR of SRC_DIE.
13060 On entry *REF_CU is the CU of SRC_DIE.
13061 On exit *REF_CU is the CU of the result. */
13062
13063static struct die_info *
13064follow_die_ref (struct die_info *src_die, struct attribute *attr,
13065 struct dwarf2_cu **ref_cu)
13066{
13067 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13068 struct dwarf2_cu *cu = *ref_cu;
13069 struct die_info *die;
13070
13071 die = follow_die_offset (offset, ref_cu);
13072 if (!die)
13073 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13074 "at 0x%x [in module %s]"),
13075 offset, src_die->offset, cu->objfile->name);
348e048f 13076
5c631832
JK
13077 return die;
13078}
13079
13080/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13081 value is intended for DW_OP_call*. */
13082
13083struct dwarf2_locexpr_baton
13084dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13085 struct dwarf2_per_cu_data *per_cu,
13086 CORE_ADDR (*get_frame_pc) (void *baton),
13087 void *baton)
5c631832
JK
13088{
13089 struct dwarf2_cu *cu = per_cu->cu;
13090 struct die_info *die;
13091 struct attribute *attr;
13092 struct dwarf2_locexpr_baton retval;
13093
8cf6f0b1
TT
13094 dw2_setup (per_cu->objfile);
13095
5c631832
JK
13096 die = follow_die_offset (offset, &cu);
13097 if (!die)
13098 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13099 offset, per_cu->cu->objfile->name);
13100
13101 attr = dwarf2_attr (die, DW_AT_location, cu);
13102 if (!attr)
13103 {
13104 /* DWARF: "If there is no such attribute, then there is no effect.". */
13105
13106 retval.data = NULL;
13107 retval.size = 0;
13108 }
8cf6f0b1
TT
13109 else if (attr_form_is_section_offset (attr))
13110 {
13111 struct dwarf2_loclist_baton loclist_baton;
13112 CORE_ADDR pc = (*get_frame_pc) (baton);
13113 size_t size;
13114
13115 fill_in_loclist_baton (cu, &loclist_baton, attr);
13116
13117 retval.data = dwarf2_find_location_expression (&loclist_baton,
13118 &size, pc);
13119 retval.size = size;
13120 }
5c631832
JK
13121 else
13122 {
13123 if (!attr_form_is_block (attr))
13124 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13125 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13126 offset, per_cu->cu->objfile->name);
13127
13128 retval.data = DW_BLOCK (attr)->data;
13129 retval.size = DW_BLOCK (attr)->size;
13130 }
13131 retval.per_cu = cu->per_cu;
13132 return retval;
348e048f
DE
13133}
13134
13135/* Follow the signature attribute ATTR in SRC_DIE.
13136 On entry *REF_CU is the CU of SRC_DIE.
13137 On exit *REF_CU is the CU of the result. */
13138
13139static struct die_info *
13140follow_die_sig (struct die_info *src_die, struct attribute *attr,
13141 struct dwarf2_cu **ref_cu)
13142{
13143 struct objfile *objfile = (*ref_cu)->objfile;
13144 struct die_info temp_die;
13145 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13146 struct dwarf2_cu *sig_cu;
13147 struct die_info *die;
13148
13149 /* sig_type will be NULL if the signatured type is missing from
13150 the debug info. */
13151 if (sig_type == NULL)
13152 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13153 "at 0x%x [in module %s]"),
13154 src_die->offset, objfile->name);
13155
13156 /* If necessary, add it to the queue and load its DIEs. */
13157
13158 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13159 read_signatured_type (objfile, sig_type);
13160
13161 gdb_assert (sig_type->per_cu.cu != NULL);
13162
13163 sig_cu = sig_type->per_cu.cu;
13164 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13165 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13166 if (die)
13167 {
13168 *ref_cu = sig_cu;
13169 return die;
13170 }
13171
13172 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13173 "at 0x%x [in module %s]"),
13174 sig_type->type_offset, src_die->offset, objfile->name);
13175}
13176
13177/* Given an offset of a signatured type, return its signatured_type. */
13178
13179static struct signatured_type *
13180lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13181{
13182 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13183 unsigned int length, initial_length_size;
13184 unsigned int sig_offset;
13185 struct signatured_type find_entry, *type_sig;
13186
13187 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13188 sig_offset = (initial_length_size
13189 + 2 /*version*/
13190 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13191 + 1 /*address_size*/);
13192 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13193 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13194
13195 /* This is only used to lookup previously recorded types.
13196 If we didn't find it, it's our bug. */
13197 gdb_assert (type_sig != NULL);
13198 gdb_assert (offset == type_sig->offset);
13199
13200 return type_sig;
13201}
13202
13203/* Read in signatured type at OFFSET and build its CU and die(s). */
13204
13205static void
13206read_signatured_type_at_offset (struct objfile *objfile,
13207 unsigned int offset)
13208{
13209 struct signatured_type *type_sig;
13210
be391dca
TT
13211 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13212
348e048f
DE
13213 /* We have the section offset, but we need the signature to do the
13214 hash table lookup. */
13215 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13216
13217 gdb_assert (type_sig->per_cu.cu == NULL);
13218
13219 read_signatured_type (objfile, type_sig);
13220
13221 gdb_assert (type_sig->per_cu.cu != NULL);
13222}
13223
13224/* Read in a signatured type and build its CU and DIEs. */
13225
13226static void
13227read_signatured_type (struct objfile *objfile,
13228 struct signatured_type *type_sig)
13229{
1fd400ff 13230 gdb_byte *types_ptr;
348e048f
DE
13231 struct die_reader_specs reader_specs;
13232 struct dwarf2_cu *cu;
13233 ULONGEST signature;
13234 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13235
1fd400ff
TT
13236 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13237 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13238
348e048f
DE
13239 gdb_assert (type_sig->per_cu.cu == NULL);
13240
9816fde3
JK
13241 cu = xmalloc (sizeof (*cu));
13242 init_one_comp_unit (cu, objfile);
13243
348e048f
DE
13244 type_sig->per_cu.cu = cu;
13245 cu->per_cu = &type_sig->per_cu;
13246
13247 /* If an error occurs while loading, release our storage. */
13248 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13249
13250 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13251 types_ptr, objfile->obfd);
13252 gdb_assert (signature == type_sig->signature);
13253
13254 cu->die_hash
13255 = htab_create_alloc_ex (cu->header.length / 12,
13256 die_hash,
13257 die_eq,
13258 NULL,
13259 &cu->comp_unit_obstack,
13260 hashtab_obstack_allocate,
13261 dummy_obstack_deallocate);
13262
13263 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13264 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13265
13266 init_cu_die_reader (&reader_specs, cu);
13267
13268 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13269 NULL /*parent*/);
13270
13271 /* We try not to read any attributes in this function, because not
13272 all objfiles needed for references have been loaded yet, and symbol
13273 table processing isn't initialized. But we have to set the CU language,
13274 or we won't be able to build types correctly. */
9816fde3 13275 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13276
13277 do_cleanups (back_to);
13278
13279 /* We've successfully allocated this compilation unit. Let our caller
13280 clean it up when finished with it. */
13281 discard_cleanups (free_cu_cleanup);
13282
13283 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13284 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13285}
13286
c906108c
SS
13287/* Decode simple location descriptions.
13288 Given a pointer to a dwarf block that defines a location, compute
13289 the location and return the value.
13290
4cecd739
DJ
13291 NOTE drow/2003-11-18: This function is called in two situations
13292 now: for the address of static or global variables (partial symbols
13293 only) and for offsets into structures which are expected to be
13294 (more or less) constant. The partial symbol case should go away,
13295 and only the constant case should remain. That will let this
13296 function complain more accurately. A few special modes are allowed
13297 without complaint for global variables (for instance, global
13298 register values and thread-local values).
c906108c
SS
13299
13300 A location description containing no operations indicates that the
4cecd739 13301 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13302 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13303 callers will only want a very basic result and this can become a
13304 complaint.
c906108c 13305
d53d4ac5 13306 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13307
13308static CORE_ADDR
e7c27a73 13309decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13310{
e7c27a73 13311 struct objfile *objfile = cu->objfile;
c906108c
SS
13312 int i;
13313 int size = blk->size;
fe1b8b76 13314 gdb_byte *data = blk->data;
c906108c
SS
13315 CORE_ADDR stack[64];
13316 int stacki;
13317 unsigned int bytes_read, unsnd;
fe1b8b76 13318 gdb_byte op;
c906108c
SS
13319
13320 i = 0;
13321 stacki = 0;
13322 stack[stacki] = 0;
d53d4ac5 13323 stack[++stacki] = 0;
c906108c
SS
13324
13325 while (i < size)
13326 {
c906108c
SS
13327 op = data[i++];
13328 switch (op)
13329 {
f1bea926
JM
13330 case DW_OP_lit0:
13331 case DW_OP_lit1:
13332 case DW_OP_lit2:
13333 case DW_OP_lit3:
13334 case DW_OP_lit4:
13335 case DW_OP_lit5:
13336 case DW_OP_lit6:
13337 case DW_OP_lit7:
13338 case DW_OP_lit8:
13339 case DW_OP_lit9:
13340 case DW_OP_lit10:
13341 case DW_OP_lit11:
13342 case DW_OP_lit12:
13343 case DW_OP_lit13:
13344 case DW_OP_lit14:
13345 case DW_OP_lit15:
13346 case DW_OP_lit16:
13347 case DW_OP_lit17:
13348 case DW_OP_lit18:
13349 case DW_OP_lit19:
13350 case DW_OP_lit20:
13351 case DW_OP_lit21:
13352 case DW_OP_lit22:
13353 case DW_OP_lit23:
13354 case DW_OP_lit24:
13355 case DW_OP_lit25:
13356 case DW_OP_lit26:
13357 case DW_OP_lit27:
13358 case DW_OP_lit28:
13359 case DW_OP_lit29:
13360 case DW_OP_lit30:
13361 case DW_OP_lit31:
13362 stack[++stacki] = op - DW_OP_lit0;
13363 break;
13364
c906108c
SS
13365 case DW_OP_reg0:
13366 case DW_OP_reg1:
13367 case DW_OP_reg2:
13368 case DW_OP_reg3:
13369 case DW_OP_reg4:
13370 case DW_OP_reg5:
13371 case DW_OP_reg6:
13372 case DW_OP_reg7:
13373 case DW_OP_reg8:
13374 case DW_OP_reg9:
13375 case DW_OP_reg10:
13376 case DW_OP_reg11:
13377 case DW_OP_reg12:
13378 case DW_OP_reg13:
13379 case DW_OP_reg14:
13380 case DW_OP_reg15:
13381 case DW_OP_reg16:
13382 case DW_OP_reg17:
13383 case DW_OP_reg18:
13384 case DW_OP_reg19:
13385 case DW_OP_reg20:
13386 case DW_OP_reg21:
13387 case DW_OP_reg22:
13388 case DW_OP_reg23:
13389 case DW_OP_reg24:
13390 case DW_OP_reg25:
13391 case DW_OP_reg26:
13392 case DW_OP_reg27:
13393 case DW_OP_reg28:
13394 case DW_OP_reg29:
13395 case DW_OP_reg30:
13396 case DW_OP_reg31:
c906108c 13397 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13398 if (i < size)
13399 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13400 break;
13401
13402 case DW_OP_regx:
c906108c
SS
13403 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13404 i += bytes_read;
c906108c 13405 stack[++stacki] = unsnd;
4cecd739
DJ
13406 if (i < size)
13407 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13408 break;
13409
13410 case DW_OP_addr:
107d2387 13411 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13412 cu, &bytes_read);
107d2387 13413 i += bytes_read;
c906108c
SS
13414 break;
13415
13416 case DW_OP_const1u:
13417 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13418 i += 1;
13419 break;
13420
13421 case DW_OP_const1s:
13422 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13423 i += 1;
13424 break;
13425
13426 case DW_OP_const2u:
13427 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13428 i += 2;
13429 break;
13430
13431 case DW_OP_const2s:
13432 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13433 i += 2;
13434 break;
13435
13436 case DW_OP_const4u:
13437 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13438 i += 4;
13439 break;
13440
13441 case DW_OP_const4s:
13442 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13443 i += 4;
13444 break;
13445
13446 case DW_OP_constu:
13447 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13448 &bytes_read);
c906108c
SS
13449 i += bytes_read;
13450 break;
13451
13452 case DW_OP_consts:
13453 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13454 i += bytes_read;
13455 break;
13456
f1bea926
JM
13457 case DW_OP_dup:
13458 stack[stacki + 1] = stack[stacki];
13459 stacki++;
13460 break;
13461
c906108c
SS
13462 case DW_OP_plus:
13463 stack[stacki - 1] += stack[stacki];
13464 stacki--;
13465 break;
13466
13467 case DW_OP_plus_uconst:
13468 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13469 i += bytes_read;
13470 break;
13471
13472 case DW_OP_minus:
f1bea926 13473 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13474 stacki--;
13475 break;
13476
7a292a7a 13477 case DW_OP_deref:
7a292a7a 13478 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13479 this using GDB's address_class enum. This is valid for partial
13480 global symbols, although the variable's address will be bogus
13481 in the psymtab. */
7a292a7a 13482 if (i < size)
4d3c2250 13483 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13484 break;
13485
9d774e44 13486 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13487 /* The top of the stack has the offset from the beginning
13488 of the thread control block at which the variable is located. */
13489 /* Nothing should follow this operator, so the top of stack would
13490 be returned. */
4cecd739
DJ
13491 /* This is valid for partial global symbols, but the variable's
13492 address will be bogus in the psymtab. */
9d774e44 13493 if (i < size)
4d3c2250 13494 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13495 break;
13496
42be36b3
CT
13497 case DW_OP_GNU_uninit:
13498 break;
13499
c906108c 13500 default:
e2e0b3e5 13501 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13502 dwarf_stack_op_name (op, 1));
c906108c
SS
13503 return (stack[stacki]);
13504 }
d53d4ac5
TT
13505
13506 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13507 outside of the allocated space. Also enforce minimum>0. */
13508 if (stacki >= ARRAY_SIZE (stack) - 1)
13509 {
13510 complaint (&symfile_complaints,
13511 _("location description stack overflow"));
13512 return 0;
13513 }
13514
13515 if (stacki <= 0)
13516 {
13517 complaint (&symfile_complaints,
13518 _("location description stack underflow"));
13519 return 0;
13520 }
c906108c
SS
13521 }
13522 return (stack[stacki]);
13523}
13524
13525/* memory allocation interface */
13526
c906108c 13527static struct dwarf_block *
7b5a2f43 13528dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13529{
13530 struct dwarf_block *blk;
13531
13532 blk = (struct dwarf_block *)
7b5a2f43 13533 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13534 return (blk);
13535}
13536
13537static struct abbrev_info *
f3dd6933 13538dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13539{
13540 struct abbrev_info *abbrev;
13541
f3dd6933
DJ
13542 abbrev = (struct abbrev_info *)
13543 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13544 memset (abbrev, 0, sizeof (struct abbrev_info));
13545 return (abbrev);
13546}
13547
13548static struct die_info *
b60c80d6 13549dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13550{
13551 struct die_info *die;
b60c80d6
DJ
13552 size_t size = sizeof (struct die_info);
13553
13554 if (num_attrs > 1)
13555 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13556
b60c80d6 13557 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13558 memset (die, 0, sizeof (struct die_info));
13559 return (die);
13560}
2e276125
JB
13561
13562\f
13563/* Macro support. */
13564
13565
13566/* Return the full name of file number I in *LH's file name table.
13567 Use COMP_DIR as the name of the current directory of the
13568 compilation. The result is allocated using xmalloc; the caller is
13569 responsible for freeing it. */
13570static char *
13571file_full_name (int file, struct line_header *lh, const char *comp_dir)
13572{
6a83a1e6
EZ
13573 /* Is the file number a valid index into the line header's file name
13574 table? Remember that file numbers start with one, not zero. */
13575 if (1 <= file && file <= lh->num_file_names)
13576 {
13577 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13578
6a83a1e6
EZ
13579 if (IS_ABSOLUTE_PATH (fe->name))
13580 return xstrdup (fe->name);
13581 else
13582 {
13583 const char *dir;
13584 int dir_len;
13585 char *full_name;
13586
13587 if (fe->dir_index)
13588 dir = lh->include_dirs[fe->dir_index - 1];
13589 else
13590 dir = comp_dir;
13591
13592 if (dir)
13593 {
13594 dir_len = strlen (dir);
13595 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13596 strcpy (full_name, dir);
13597 full_name[dir_len] = '/';
13598 strcpy (full_name + dir_len + 1, fe->name);
13599 return full_name;
13600 }
13601 else
13602 return xstrdup (fe->name);
13603 }
13604 }
2e276125
JB
13605 else
13606 {
6a83a1e6
EZ
13607 /* The compiler produced a bogus file number. We can at least
13608 record the macro definitions made in the file, even if we
13609 won't be able to find the file by name. */
13610 char fake_name[80];
9a619af0 13611
6a83a1e6 13612 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13613
6e70227d 13614 complaint (&symfile_complaints,
6a83a1e6
EZ
13615 _("bad file number in macro information (%d)"),
13616 file);
2e276125 13617
6a83a1e6 13618 return xstrdup (fake_name);
2e276125
JB
13619 }
13620}
13621
13622
13623static struct macro_source_file *
13624macro_start_file (int file, int line,
13625 struct macro_source_file *current_file,
13626 const char *comp_dir,
13627 struct line_header *lh, struct objfile *objfile)
13628{
13629 /* The full name of this source file. */
13630 char *full_name = file_full_name (file, lh, comp_dir);
13631
13632 /* We don't create a macro table for this compilation unit
13633 at all until we actually get a filename. */
13634 if (! pending_macros)
4a146b47 13635 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13636 objfile->macro_cache);
2e276125
JB
13637
13638 if (! current_file)
13639 /* If we have no current file, then this must be the start_file
13640 directive for the compilation unit's main source file. */
13641 current_file = macro_set_main (pending_macros, full_name);
13642 else
13643 current_file = macro_include (current_file, line, full_name);
13644
13645 xfree (full_name);
6e70227d 13646
2e276125
JB
13647 return current_file;
13648}
13649
13650
13651/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13652 followed by a null byte. */
13653static char *
13654copy_string (const char *buf, int len)
13655{
13656 char *s = xmalloc (len + 1);
9a619af0 13657
2e276125
JB
13658 memcpy (s, buf, len);
13659 s[len] = '\0';
2e276125
JB
13660 return s;
13661}
13662
13663
13664static const char *
13665consume_improper_spaces (const char *p, const char *body)
13666{
13667 if (*p == ' ')
13668 {
4d3c2250 13669 complaint (&symfile_complaints,
e2e0b3e5 13670 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13671 body);
2e276125
JB
13672
13673 while (*p == ' ')
13674 p++;
13675 }
13676
13677 return p;
13678}
13679
13680
13681static void
13682parse_macro_definition (struct macro_source_file *file, int line,
13683 const char *body)
13684{
13685 const char *p;
13686
13687 /* The body string takes one of two forms. For object-like macro
13688 definitions, it should be:
13689
13690 <macro name> " " <definition>
13691
13692 For function-like macro definitions, it should be:
13693
13694 <macro name> "() " <definition>
13695 or
13696 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13697
13698 Spaces may appear only where explicitly indicated, and in the
13699 <definition>.
13700
13701 The Dwarf 2 spec says that an object-like macro's name is always
13702 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13703 the space when the macro's definition is the empty string.
2e276125
JB
13704
13705 The Dwarf 2 spec says that there should be no spaces between the
13706 formal arguments in a function-like macro's formal argument list,
13707 but versions of GCC around March 2002 include spaces after the
13708 commas. */
13709
13710
13711 /* Find the extent of the macro name. The macro name is terminated
13712 by either a space or null character (for an object-like macro) or
13713 an opening paren (for a function-like macro). */
13714 for (p = body; *p; p++)
13715 if (*p == ' ' || *p == '(')
13716 break;
13717
13718 if (*p == ' ' || *p == '\0')
13719 {
13720 /* It's an object-like macro. */
13721 int name_len = p - body;
13722 char *name = copy_string (body, name_len);
13723 const char *replacement;
13724
13725 if (*p == ' ')
13726 replacement = body + name_len + 1;
13727 else
13728 {
4d3c2250 13729 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13730 replacement = body + name_len;
13731 }
6e70227d 13732
2e276125
JB
13733 macro_define_object (file, line, name, replacement);
13734
13735 xfree (name);
13736 }
13737 else if (*p == '(')
13738 {
13739 /* It's a function-like macro. */
13740 char *name = copy_string (body, p - body);
13741 int argc = 0;
13742 int argv_size = 1;
13743 char **argv = xmalloc (argv_size * sizeof (*argv));
13744
13745 p++;
13746
13747 p = consume_improper_spaces (p, body);
13748
13749 /* Parse the formal argument list. */
13750 while (*p && *p != ')')
13751 {
13752 /* Find the extent of the current argument name. */
13753 const char *arg_start = p;
13754
13755 while (*p && *p != ',' && *p != ')' && *p != ' ')
13756 p++;
13757
13758 if (! *p || p == arg_start)
4d3c2250 13759 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13760 else
13761 {
13762 /* Make sure argv has room for the new argument. */
13763 if (argc >= argv_size)
13764 {
13765 argv_size *= 2;
13766 argv = xrealloc (argv, argv_size * sizeof (*argv));
13767 }
13768
13769 argv[argc++] = copy_string (arg_start, p - arg_start);
13770 }
13771
13772 p = consume_improper_spaces (p, body);
13773
13774 /* Consume the comma, if present. */
13775 if (*p == ',')
13776 {
13777 p++;
13778
13779 p = consume_improper_spaces (p, body);
13780 }
13781 }
13782
13783 if (*p == ')')
13784 {
13785 p++;
13786
13787 if (*p == ' ')
13788 /* Perfectly formed definition, no complaints. */
13789 macro_define_function (file, line, name,
6e70227d 13790 argc, (const char **) argv,
2e276125
JB
13791 p + 1);
13792 else if (*p == '\0')
13793 {
13794 /* Complain, but do define it. */
4d3c2250 13795 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13796 macro_define_function (file, line, name,
6e70227d 13797 argc, (const char **) argv,
2e276125
JB
13798 p);
13799 }
13800 else
13801 /* Just complain. */
4d3c2250 13802 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13803 }
13804 else
13805 /* Just complain. */
4d3c2250 13806 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13807
13808 xfree (name);
13809 {
13810 int i;
13811
13812 for (i = 0; i < argc; i++)
13813 xfree (argv[i]);
13814 }
13815 xfree (argv);
13816 }
13817 else
4d3c2250 13818 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13819}
13820
13821
13822static void
13823dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13824 char *comp_dir, bfd *abfd,
e7c27a73 13825 struct dwarf2_cu *cu)
2e276125 13826{
fe1b8b76 13827 gdb_byte *mac_ptr, *mac_end;
2e276125 13828 struct macro_source_file *current_file = 0;
757a13d0
JK
13829 enum dwarf_macinfo_record_type macinfo_type;
13830 int at_commandline;
2e276125 13831
be391dca
TT
13832 dwarf2_read_section (dwarf2_per_objfile->objfile,
13833 &dwarf2_per_objfile->macinfo);
dce234bc 13834 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13835 {
e2e0b3e5 13836 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13837 return;
13838 }
13839
757a13d0
JK
13840 /* First pass: Find the name of the base filename.
13841 This filename is needed in order to process all macros whose definition
13842 (or undefinition) comes from the command line. These macros are defined
13843 before the first DW_MACINFO_start_file entry, and yet still need to be
13844 associated to the base file.
13845
13846 To determine the base file name, we scan the macro definitions until we
13847 reach the first DW_MACINFO_start_file entry. We then initialize
13848 CURRENT_FILE accordingly so that any macro definition found before the
13849 first DW_MACINFO_start_file can still be associated to the base file. */
13850
dce234bc
PP
13851 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13852 mac_end = dwarf2_per_objfile->macinfo.buffer
13853 + dwarf2_per_objfile->macinfo.size;
2e276125 13854
757a13d0 13855 do
2e276125 13856 {
2e276125
JB
13857 /* Do we at least have room for a macinfo type byte? */
13858 if (mac_ptr >= mac_end)
13859 {
757a13d0
JK
13860 /* Complaint is printed during the second pass as GDB will probably
13861 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13862 break;
2e276125
JB
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:
757a13d0
JK
13873 break;
13874
13875 case DW_MACINFO_define:
13876 case DW_MACINFO_undef:
13877 /* Only skip the data by MAC_PTR. */
13878 {
13879 unsigned int bytes_read;
13880
13881 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13882 mac_ptr += bytes_read;
9b1c24c8 13883 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13884 mac_ptr += bytes_read;
13885 }
13886 break;
13887
13888 case DW_MACINFO_start_file:
13889 {
13890 unsigned int bytes_read;
13891 int line, file;
13892
13893 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13894 mac_ptr += bytes_read;
13895 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13896 mac_ptr += bytes_read;
13897
13898 current_file = macro_start_file (file, line, current_file, comp_dir,
13899 lh, cu->objfile);
13900 }
13901 break;
13902
13903 case DW_MACINFO_end_file:
13904 /* No data to skip by MAC_PTR. */
13905 break;
13906
13907 case DW_MACINFO_vendor_ext:
13908 /* Only skip the data by MAC_PTR. */
13909 {
13910 unsigned int bytes_read;
13911
13912 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13913 mac_ptr += bytes_read;
9b1c24c8 13914 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13915 mac_ptr += bytes_read;
13916 }
13917 break;
13918
13919 default:
13920 break;
13921 }
13922 } while (macinfo_type != 0 && current_file == NULL);
13923
13924 /* Second pass: Process all entries.
13925
13926 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13927 command-line macro definitions/undefinitions. This flag is unset when we
13928 reach the first DW_MACINFO_start_file entry. */
13929
dce234bc 13930 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13931
13932 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13933 GDB is still reading the definitions from command line. First
13934 DW_MACINFO_start_file will need to be ignored as it was already executed
13935 to create CURRENT_FILE for the main source holding also the command line
13936 definitions. On first met DW_MACINFO_start_file this flag is reset to
13937 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13938
13939 at_commandline = 1;
13940
13941 do
13942 {
13943 /* Do we at least have room for a macinfo type byte? */
13944 if (mac_ptr >= mac_end)
13945 {
13946 dwarf2_macros_too_long_complaint ();
13947 break;
13948 }
13949
13950 macinfo_type = read_1_byte (abfd, mac_ptr);
13951 mac_ptr++;
13952
13953 switch (macinfo_type)
13954 {
13955 /* A zero macinfo type indicates the end of the macro
13956 information. */
13957 case 0:
13958 break;
2e276125
JB
13959
13960 case DW_MACINFO_define:
13961 case DW_MACINFO_undef:
13962 {
891d2f0b 13963 unsigned int bytes_read;
2e276125
JB
13964 int line;
13965 char *body;
13966
13967 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13968 mac_ptr += bytes_read;
9b1c24c8 13969 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13970 mac_ptr += bytes_read;
13971
13972 if (! current_file)
757a13d0
JK
13973 {
13974 /* DWARF violation as no main source is present. */
13975 complaint (&symfile_complaints,
13976 _("debug info with no main source gives macro %s "
13977 "on line %d: %s"),
6e70227d
DE
13978 macinfo_type == DW_MACINFO_define ?
13979 _("definition") :
905e0470
PM
13980 macinfo_type == DW_MACINFO_undef ?
13981 _("undefinition") :
13982 _("something-or-other"), line, body);
757a13d0
JK
13983 break;
13984 }
13985 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13986 complaint (&symfile_complaints,
757a13d0
JK
13987 _("debug info gives %s macro %s with %s line %d: %s"),
13988 at_commandline ? _("command-line") : _("in-file"),
905e0470 13989 macinfo_type == DW_MACINFO_define ?
6e70227d 13990 _("definition") :
905e0470
PM
13991 macinfo_type == DW_MACINFO_undef ?
13992 _("undefinition") :
13993 _("something-or-other"),
757a13d0
JK
13994 line == 0 ? _("zero") : _("non-zero"), line, body);
13995
13996 if (macinfo_type == DW_MACINFO_define)
13997 parse_macro_definition (current_file, line, body);
13998 else if (macinfo_type == DW_MACINFO_undef)
13999 macro_undef (current_file, line, body);
2e276125
JB
14000 }
14001 break;
14002
14003 case DW_MACINFO_start_file:
14004 {
891d2f0b 14005 unsigned int bytes_read;
2e276125
JB
14006 int line, file;
14007
14008 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14009 mac_ptr += bytes_read;
14010 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14011 mac_ptr += bytes_read;
14012
757a13d0
JK
14013 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14014 complaint (&symfile_complaints,
14015 _("debug info gives source %d included "
14016 "from %s at %s line %d"),
14017 file, at_commandline ? _("command-line") : _("file"),
14018 line == 0 ? _("zero") : _("non-zero"), line);
14019
14020 if (at_commandline)
14021 {
14022 /* This DW_MACINFO_start_file was executed in the pass one. */
14023 at_commandline = 0;
14024 }
14025 else
14026 current_file = macro_start_file (file, line,
14027 current_file, comp_dir,
14028 lh, cu->objfile);
2e276125
JB
14029 }
14030 break;
14031
14032 case DW_MACINFO_end_file:
14033 if (! current_file)
4d3c2250 14034 complaint (&symfile_complaints,
e2e0b3e5 14035 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
14036 else
14037 {
14038 current_file = current_file->included_by;
14039 if (! current_file)
14040 {
14041 enum dwarf_macinfo_record_type next_type;
14042
14043 /* GCC circa March 2002 doesn't produce the zero
14044 type byte marking the end of the compilation
14045 unit. Complain if it's not there, but exit no
14046 matter what. */
14047
14048 /* Do we at least have room for a macinfo type byte? */
14049 if (mac_ptr >= mac_end)
14050 {
4d3c2250 14051 dwarf2_macros_too_long_complaint ();
2e276125
JB
14052 return;
14053 }
14054
14055 /* We don't increment mac_ptr here, so this is just
14056 a look-ahead. */
14057 next_type = read_1_byte (abfd, mac_ptr);
14058 if (next_type != 0)
4d3c2250 14059 complaint (&symfile_complaints,
e2e0b3e5 14060 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
14061
14062 return;
14063 }
14064 }
14065 break;
14066
14067 case DW_MACINFO_vendor_ext:
14068 {
891d2f0b 14069 unsigned int bytes_read;
2e276125
JB
14070 int constant;
14071 char *string;
14072
14073 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14074 mac_ptr += bytes_read;
9b1c24c8 14075 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14076 mac_ptr += bytes_read;
14077
14078 /* We don't recognize any vendor extensions. */
14079 }
14080 break;
14081 }
757a13d0 14082 } while (macinfo_type != 0);
2e276125 14083}
8e19ed76
PS
14084
14085/* Check if the attribute's form is a DW_FORM_block*
14086 if so return true else false. */
14087static int
14088attr_form_is_block (struct attribute *attr)
14089{
14090 return (attr == NULL ? 0 :
14091 attr->form == DW_FORM_block1
14092 || attr->form == DW_FORM_block2
14093 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14094 || attr->form == DW_FORM_block
14095 || attr->form == DW_FORM_exprloc);
8e19ed76 14096}
4c2df51b 14097
c6a0999f
JB
14098/* Return non-zero if ATTR's value is a section offset --- classes
14099 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14100 You may use DW_UNSND (attr) to retrieve such offsets.
14101
14102 Section 7.5.4, "Attribute Encodings", explains that no attribute
14103 may have a value that belongs to more than one of these classes; it
14104 would be ambiguous if we did, because we use the same forms for all
14105 of them. */
3690dd37
JB
14106static int
14107attr_form_is_section_offset (struct attribute *attr)
14108{
14109 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14110 || attr->form == DW_FORM_data8
14111 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14112}
14113
14114
14115/* Return non-zero if ATTR's value falls in the 'constant' class, or
14116 zero otherwise. When this function returns true, you can apply
14117 dwarf2_get_attr_constant_value to it.
14118
14119 However, note that for some attributes you must check
14120 attr_form_is_section_offset before using this test. DW_FORM_data4
14121 and DW_FORM_data8 are members of both the constant class, and of
14122 the classes that contain offsets into other debug sections
14123 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14124 that, if an attribute's can be either a constant or one of the
14125 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14126 taken as section offsets, not constants. */
14127static int
14128attr_form_is_constant (struct attribute *attr)
14129{
14130 switch (attr->form)
14131 {
14132 case DW_FORM_sdata:
14133 case DW_FORM_udata:
14134 case DW_FORM_data1:
14135 case DW_FORM_data2:
14136 case DW_FORM_data4:
14137 case DW_FORM_data8:
14138 return 1;
14139 default:
14140 return 0;
14141 }
14142}
14143
8cf6f0b1
TT
14144/* A helper function that fills in a dwarf2_loclist_baton. */
14145
14146static void
14147fill_in_loclist_baton (struct dwarf2_cu *cu,
14148 struct dwarf2_loclist_baton *baton,
14149 struct attribute *attr)
14150{
14151 dwarf2_read_section (dwarf2_per_objfile->objfile,
14152 &dwarf2_per_objfile->loc);
14153
14154 baton->per_cu = cu->per_cu;
14155 gdb_assert (baton->per_cu);
14156 /* We don't know how long the location list is, but make sure we
14157 don't run off the edge of the section. */
14158 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14159 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14160 baton->base_address = cu->base_address;
14161}
14162
4c2df51b
DJ
14163static void
14164dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14165 struct dwarf2_cu *cu)
4c2df51b 14166{
3690dd37 14167 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14168 /* ".debug_loc" may not exist at all, or the offset may be outside
14169 the section. If so, fall through to the complaint in the
14170 other branch. */
dce234bc 14171 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14172 {
0d53c4c4 14173 struct dwarf2_loclist_baton *baton;
4c2df51b 14174
4a146b47 14175 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14176 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14177
8cf6f0b1 14178 fill_in_loclist_baton (cu, baton, attr);
be391dca 14179
d00adf39 14180 if (cu->base_known == 0)
0d53c4c4 14181 complaint (&symfile_complaints,
e2e0b3e5 14182 _("Location list used without specifying the CU base address."));
4c2df51b 14183
768a979c 14184 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14185 SYMBOL_LOCATION_BATON (sym) = baton;
14186 }
14187 else
14188 {
14189 struct dwarf2_locexpr_baton *baton;
14190
4a146b47 14191 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14192 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14193 baton->per_cu = cu->per_cu;
14194 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14195
14196 if (attr_form_is_block (attr))
14197 {
14198 /* Note that we're just copying the block's data pointer
14199 here, not the actual data. We're still pointing into the
6502dd73
DJ
14200 info_buffer for SYM's objfile; right now we never release
14201 that buffer, but when we do clean up properly this may
14202 need to change. */
0d53c4c4
DJ
14203 baton->size = DW_BLOCK (attr)->size;
14204 baton->data = DW_BLOCK (attr)->data;
14205 }
14206 else
14207 {
14208 dwarf2_invalid_attrib_class_complaint ("location description",
14209 SYMBOL_NATURAL_NAME (sym));
14210 baton->size = 0;
14211 baton->data = NULL;
14212 }
6e70227d 14213
768a979c 14214 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14215 SYMBOL_LOCATION_BATON (sym) = baton;
14216 }
4c2df51b 14217}
6502dd73 14218
9aa1f1e3
TT
14219/* Return the OBJFILE associated with the compilation unit CU. If CU
14220 came from a separate debuginfo file, then the master objfile is
14221 returned. */
ae0d2f24
UW
14222
14223struct objfile *
14224dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14225{
9291a0cd 14226 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14227
14228 /* Return the master objfile, so that we can report and look up the
14229 correct file containing this variable. */
14230 if (objfile->separate_debug_objfile_backlink)
14231 objfile = objfile->separate_debug_objfile_backlink;
14232
14233 return objfile;
14234}
14235
14236/* Return the address size given in the compilation unit header for CU. */
14237
14238CORE_ADDR
14239dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14240{
14241 if (per_cu->cu)
14242 return per_cu->cu->header.addr_size;
14243 else
14244 {
14245 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14246 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14247 struct dwarf2_per_objfile *per_objfile
14248 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14249 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14250 struct comp_unit_head cu_header;
9a619af0 14251
ae0d2f24
UW
14252 memset (&cu_header, 0, sizeof cu_header);
14253 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14254 return cu_header.addr_size;
14255 }
14256}
14257
9eae7c52
TT
14258/* Return the offset size given in the compilation unit header for CU. */
14259
14260int
14261dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14262{
14263 if (per_cu->cu)
14264 return per_cu->cu->header.offset_size;
14265 else
14266 {
14267 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14268 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14269 struct dwarf2_per_objfile *per_objfile
14270 = objfile_data (objfile, dwarf2_objfile_data_key);
14271 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14272 struct comp_unit_head cu_header;
14273
14274 memset (&cu_header, 0, sizeof cu_header);
14275 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14276 return cu_header.offset_size;
14277 }
14278}
14279
9aa1f1e3
TT
14280/* Return the text offset of the CU. The returned offset comes from
14281 this CU's objfile. If this objfile came from a separate debuginfo
14282 file, then the offset may be different from the corresponding
14283 offset in the parent objfile. */
14284
14285CORE_ADDR
14286dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14287{
bb3fa9d0 14288 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14289
14290 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14291}
14292
348e048f
DE
14293/* Locate the .debug_info compilation unit from CU's objfile which contains
14294 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14295
14296static struct dwarf2_per_cu_data *
c764a876 14297dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14298 struct objfile *objfile)
14299{
14300 struct dwarf2_per_cu_data *this_cu;
14301 int low, high;
14302
ae038cb0
DJ
14303 low = 0;
14304 high = dwarf2_per_objfile->n_comp_units - 1;
14305 while (high > low)
14306 {
14307 int mid = low + (high - low) / 2;
9a619af0 14308
ae038cb0
DJ
14309 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14310 high = mid;
14311 else
14312 low = mid + 1;
14313 }
14314 gdb_assert (low == high);
14315 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14316 {
10b3939b 14317 if (low == 0)
8a3fe4f8
AC
14318 error (_("Dwarf Error: could not find partial DIE containing "
14319 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14320 (long) offset, bfd_get_filename (objfile->obfd));
14321
ae038cb0
DJ
14322 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14323 return dwarf2_per_objfile->all_comp_units[low-1];
14324 }
14325 else
14326 {
14327 this_cu = dwarf2_per_objfile->all_comp_units[low];
14328 if (low == dwarf2_per_objfile->n_comp_units - 1
14329 && offset >= this_cu->offset + this_cu->length)
c764a876 14330 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14331 gdb_assert (offset < this_cu->offset + this_cu->length);
14332 return this_cu;
14333 }
14334}
14335
10b3939b
DJ
14336/* Locate the compilation unit from OBJFILE which is located at exactly
14337 OFFSET. Raises an error on failure. */
14338
ae038cb0 14339static struct dwarf2_per_cu_data *
c764a876 14340dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14341{
14342 struct dwarf2_per_cu_data *this_cu;
9a619af0 14343
ae038cb0
DJ
14344 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14345 if (this_cu->offset != offset)
c764a876 14346 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14347 return this_cu;
14348}
14349
9816fde3 14350/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14351
9816fde3
JK
14352static void
14353init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14354{
9816fde3 14355 memset (cu, 0, sizeof (*cu));
93311388
DE
14356 cu->objfile = objfile;
14357 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14358}
14359
14360/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14361
14362static void
14363prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14364{
14365 struct attribute *attr;
14366
14367 /* Set the language we're debugging. */
14368 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14369 if (attr)
14370 set_cu_language (DW_UNSND (attr), cu);
14371 else
14372 set_cu_language (language_minimal, cu);
93311388
DE
14373}
14374
ae038cb0
DJ
14375/* Release one cached compilation unit, CU. We unlink it from the tree
14376 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14377 the caller is responsible for that.
14378 NOTE: DATA is a void * because this function is also used as a
14379 cleanup routine. */
ae038cb0
DJ
14380
14381static void
14382free_one_comp_unit (void *data)
14383{
14384 struct dwarf2_cu *cu = data;
14385
14386 if (cu->per_cu != NULL)
14387 cu->per_cu->cu = NULL;
14388 cu->per_cu = NULL;
14389
14390 obstack_free (&cu->comp_unit_obstack, NULL);
14391
14392 xfree (cu);
14393}
14394
72bf9492 14395/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14396 when we're finished with it. We can't free the pointer itself, but be
14397 sure to unlink it from the cache. Also release any associated storage
14398 and perform cache maintenance.
72bf9492
DJ
14399
14400 Only used during partial symbol parsing. */
14401
14402static void
14403free_stack_comp_unit (void *data)
14404{
14405 struct dwarf2_cu *cu = data;
14406
14407 obstack_free (&cu->comp_unit_obstack, NULL);
14408 cu->partial_dies = NULL;
ae038cb0
DJ
14409
14410 if (cu->per_cu != NULL)
14411 {
14412 /* This compilation unit is on the stack in our caller, so we
14413 should not xfree it. Just unlink it. */
14414 cu->per_cu->cu = NULL;
14415 cu->per_cu = NULL;
14416
14417 /* If we had a per-cu pointer, then we may have other compilation
14418 units loaded, so age them now. */
14419 age_cached_comp_units ();
14420 }
14421}
14422
14423/* Free all cached compilation units. */
14424
14425static void
14426free_cached_comp_units (void *data)
14427{
14428 struct dwarf2_per_cu_data *per_cu, **last_chain;
14429
14430 per_cu = dwarf2_per_objfile->read_in_chain;
14431 last_chain = &dwarf2_per_objfile->read_in_chain;
14432 while (per_cu != NULL)
14433 {
14434 struct dwarf2_per_cu_data *next_cu;
14435
14436 next_cu = per_cu->cu->read_in_chain;
14437
14438 free_one_comp_unit (per_cu->cu);
14439 *last_chain = next_cu;
14440
14441 per_cu = next_cu;
14442 }
14443}
14444
14445/* Increase the age counter on each cached compilation unit, and free
14446 any that are too old. */
14447
14448static void
14449age_cached_comp_units (void)
14450{
14451 struct dwarf2_per_cu_data *per_cu, **last_chain;
14452
14453 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14454 per_cu = dwarf2_per_objfile->read_in_chain;
14455 while (per_cu != NULL)
14456 {
14457 per_cu->cu->last_used ++;
14458 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14459 dwarf2_mark (per_cu->cu);
14460 per_cu = per_cu->cu->read_in_chain;
14461 }
14462
14463 per_cu = dwarf2_per_objfile->read_in_chain;
14464 last_chain = &dwarf2_per_objfile->read_in_chain;
14465 while (per_cu != NULL)
14466 {
14467 struct dwarf2_per_cu_data *next_cu;
14468
14469 next_cu = per_cu->cu->read_in_chain;
14470
14471 if (!per_cu->cu->mark)
14472 {
14473 free_one_comp_unit (per_cu->cu);
14474 *last_chain = next_cu;
14475 }
14476 else
14477 last_chain = &per_cu->cu->read_in_chain;
14478
14479 per_cu = next_cu;
14480 }
14481}
14482
14483/* Remove a single compilation unit from the cache. */
14484
14485static void
14486free_one_cached_comp_unit (void *target_cu)
14487{
14488 struct dwarf2_per_cu_data *per_cu, **last_chain;
14489
14490 per_cu = dwarf2_per_objfile->read_in_chain;
14491 last_chain = &dwarf2_per_objfile->read_in_chain;
14492 while (per_cu != NULL)
14493 {
14494 struct dwarf2_per_cu_data *next_cu;
14495
14496 next_cu = per_cu->cu->read_in_chain;
14497
14498 if (per_cu->cu == target_cu)
14499 {
14500 free_one_comp_unit (per_cu->cu);
14501 *last_chain = next_cu;
14502 break;
14503 }
14504 else
14505 last_chain = &per_cu->cu->read_in_chain;
14506
14507 per_cu = next_cu;
14508 }
14509}
14510
fe3e1990
DJ
14511/* Release all extra memory associated with OBJFILE. */
14512
14513void
14514dwarf2_free_objfile (struct objfile *objfile)
14515{
14516 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14517
14518 if (dwarf2_per_objfile == NULL)
14519 return;
14520
14521 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14522 free_cached_comp_units (NULL);
14523
9291a0cd
TT
14524 if (dwarf2_per_objfile->using_index)
14525 {
14526 int i;
14527
14528 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14529 {
14530 int j;
e254ef6a
DE
14531 struct dwarf2_per_cu_data *per_cu =
14532 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14533
e254ef6a 14534 if (!per_cu->v.quick->lines)
9291a0cd
TT
14535 continue;
14536
e254ef6a 14537 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14538 {
e254ef6a
DE
14539 if (per_cu->v.quick->file_names)
14540 xfree ((void *) per_cu->v.quick->file_names[j]);
14541 if (per_cu->v.quick->full_names)
14542 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14543 }
14544
e254ef6a 14545 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14546 }
14547 }
14548
fe3e1990
DJ
14549 /* Everything else should be on the objfile obstack. */
14550}
14551
1c379e20
DJ
14552/* A pair of DIE offset and GDB type pointer. We store these
14553 in a hash table separate from the DIEs, and preserve them
14554 when the DIEs are flushed out of cache. */
14555
14556struct dwarf2_offset_and_type
14557{
14558 unsigned int offset;
14559 struct type *type;
14560};
14561
14562/* Hash function for a dwarf2_offset_and_type. */
14563
14564static hashval_t
14565offset_and_type_hash (const void *item)
14566{
14567 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14568
1c379e20
DJ
14569 return ofs->offset;
14570}
14571
14572/* Equality function for a dwarf2_offset_and_type. */
14573
14574static int
14575offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14576{
14577 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14578 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14579
1c379e20
DJ
14580 return ofs_lhs->offset == ofs_rhs->offset;
14581}
14582
14583/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14584 table if necessary. For convenience, return TYPE.
14585
14586 The DIEs reading must have careful ordering to:
14587 * Not cause infite loops trying to read in DIEs as a prerequisite for
14588 reading current DIE.
14589 * Not trying to dereference contents of still incompletely read in types
14590 while reading in other DIEs.
14591 * Enable referencing still incompletely read in types just by a pointer to
14592 the type without accessing its fields.
14593
14594 Therefore caller should follow these rules:
14595 * Try to fetch any prerequisite types we may need to build this DIE type
14596 before building the type and calling set_die_type.
e71ec853 14597 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14598 possible before fetching more types to complete the current type.
14599 * Make the type as complete as possible before fetching more types. */
1c379e20 14600
f792889a 14601static struct type *
1c379e20
DJ
14602set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14603{
14604 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14605 struct objfile *objfile = cu->objfile;
14606 htab_t *type_hash_ptr;
1c379e20 14607
b4ba55a1
JB
14608 /* For Ada types, make sure that the gnat-specific data is always
14609 initialized (if not already set). There are a few types where
14610 we should not be doing so, because the type-specific area is
14611 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14612 where the type-specific area is used to store the floatformat).
14613 But this is not a problem, because the gnat-specific information
14614 is actually not needed for these types. */
14615 if (need_gnat_info (cu)
14616 && TYPE_CODE (type) != TYPE_CODE_FUNC
14617 && TYPE_CODE (type) != TYPE_CODE_FLT
14618 && !HAVE_GNAT_AUX_INFO (type))
14619 INIT_GNAT_SPECIFIC (type);
14620
673bfd45
DE
14621 if (cu->per_cu->from_debug_types)
14622 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14623 else
14624 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14625
14626 if (*type_hash_ptr == NULL)
f792889a 14627 {
673bfd45
DE
14628 *type_hash_ptr
14629 = htab_create_alloc_ex (127,
f792889a
DJ
14630 offset_and_type_hash,
14631 offset_and_type_eq,
14632 NULL,
673bfd45 14633 &objfile->objfile_obstack,
f792889a
DJ
14634 hashtab_obstack_allocate,
14635 dummy_obstack_deallocate);
f792889a 14636 }
1c379e20
DJ
14637
14638 ofs.offset = die->offset;
14639 ofs.type = type;
14640 slot = (struct dwarf2_offset_and_type **)
673bfd45 14641 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14642 if (*slot)
14643 complaint (&symfile_complaints,
14644 _("A problem internal to GDB: DIE 0x%x has type already set"),
14645 die->offset);
673bfd45 14646 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14647 **slot = ofs;
f792889a 14648 return type;
1c379e20
DJ
14649}
14650
673bfd45
DE
14651/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14652 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14653
14654static struct type *
673bfd45
DE
14655get_die_type_at_offset (unsigned int offset,
14656 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14657{
14658 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14659 htab_t type_hash;
f792889a 14660
673bfd45
DE
14661 if (per_cu->from_debug_types)
14662 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14663 else
14664 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14665 if (type_hash == NULL)
14666 return NULL;
1c379e20 14667
673bfd45 14668 ofs.offset = offset;
1c379e20
DJ
14669 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14670 if (slot)
14671 return slot->type;
14672 else
14673 return NULL;
14674}
14675
673bfd45
DE
14676/* Look up the type for DIE in the appropriate type_hash table,
14677 or return NULL if DIE does not have a saved type. */
14678
14679static struct type *
14680get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14681{
14682 return get_die_type_at_offset (die->offset, cu->per_cu);
14683}
14684
10b3939b
DJ
14685/* Add a dependence relationship from CU to REF_PER_CU. */
14686
14687static void
14688dwarf2_add_dependence (struct dwarf2_cu *cu,
14689 struct dwarf2_per_cu_data *ref_per_cu)
14690{
14691 void **slot;
14692
14693 if (cu->dependencies == NULL)
14694 cu->dependencies
14695 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14696 NULL, &cu->comp_unit_obstack,
14697 hashtab_obstack_allocate,
14698 dummy_obstack_deallocate);
14699
14700 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14701 if (*slot == NULL)
14702 *slot = ref_per_cu;
14703}
1c379e20 14704
f504f079
DE
14705/* Subroutine of dwarf2_mark to pass to htab_traverse.
14706 Set the mark field in every compilation unit in the
ae038cb0
DJ
14707 cache that we must keep because we are keeping CU. */
14708
10b3939b
DJ
14709static int
14710dwarf2_mark_helper (void **slot, void *data)
14711{
14712 struct dwarf2_per_cu_data *per_cu;
14713
14714 per_cu = (struct dwarf2_per_cu_data *) *slot;
14715 if (per_cu->cu->mark)
14716 return 1;
14717 per_cu->cu->mark = 1;
14718
14719 if (per_cu->cu->dependencies != NULL)
14720 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14721
14722 return 1;
14723}
14724
f504f079
DE
14725/* Set the mark field in CU and in every other compilation unit in the
14726 cache that we must keep because we are keeping CU. */
14727
ae038cb0
DJ
14728static void
14729dwarf2_mark (struct dwarf2_cu *cu)
14730{
14731 if (cu->mark)
14732 return;
14733 cu->mark = 1;
10b3939b
DJ
14734 if (cu->dependencies != NULL)
14735 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14736}
14737
14738static void
14739dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14740{
14741 while (per_cu)
14742 {
14743 per_cu->cu->mark = 0;
14744 per_cu = per_cu->cu->read_in_chain;
14745 }
72bf9492
DJ
14746}
14747
72bf9492
DJ
14748/* Trivial hash function for partial_die_info: the hash value of a DIE
14749 is its offset in .debug_info for this objfile. */
14750
14751static hashval_t
14752partial_die_hash (const void *item)
14753{
14754 const struct partial_die_info *part_die = item;
9a619af0 14755
72bf9492
DJ
14756 return part_die->offset;
14757}
14758
14759/* Trivial comparison function for partial_die_info structures: two DIEs
14760 are equal if they have the same offset. */
14761
14762static int
14763partial_die_eq (const void *item_lhs, const void *item_rhs)
14764{
14765 const struct partial_die_info *part_die_lhs = item_lhs;
14766 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14767
72bf9492
DJ
14768 return part_die_lhs->offset == part_die_rhs->offset;
14769}
14770
ae038cb0
DJ
14771static struct cmd_list_element *set_dwarf2_cmdlist;
14772static struct cmd_list_element *show_dwarf2_cmdlist;
14773
14774static void
14775set_dwarf2_cmd (char *args, int from_tty)
14776{
14777 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14778}
14779
14780static void
14781show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14782{
ae038cb0
DJ
14783 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14784}
14785
dce234bc
PP
14786/* If section described by INFO was mmapped, munmap it now. */
14787
14788static void
14789munmap_section_buffer (struct dwarf2_section_info *info)
14790{
14791 if (info->was_mmapped)
14792 {
14793#ifdef HAVE_MMAP
14794 intptr_t begin = (intptr_t) info->buffer;
14795 intptr_t map_begin = begin & ~(pagesize - 1);
14796 size_t map_length = info->size + begin - map_begin;
9a619af0 14797
dce234bc
PP
14798 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14799#else
14800 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14801 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14802#endif
14803 }
14804}
14805
14806/* munmap debug sections for OBJFILE, if necessary. */
14807
14808static void
c1bd65d0 14809dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14810{
14811 struct dwarf2_per_objfile *data = d;
9a619af0 14812
16be1145
DE
14813 /* This is sorted according to the order they're defined in to make it easier
14814 to keep in sync. */
dce234bc
PP
14815 munmap_section_buffer (&data->info);
14816 munmap_section_buffer (&data->abbrev);
14817 munmap_section_buffer (&data->line);
16be1145 14818 munmap_section_buffer (&data->loc);
dce234bc 14819 munmap_section_buffer (&data->macinfo);
16be1145 14820 munmap_section_buffer (&data->str);
dce234bc 14821 munmap_section_buffer (&data->ranges);
16be1145 14822 munmap_section_buffer (&data->types);
dce234bc
PP
14823 munmap_section_buffer (&data->frame);
14824 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14825 munmap_section_buffer (&data->gdb_index);
14826}
14827
14828\f
14829
14830/* The contents of the hash table we create when building the string
14831 table. */
14832struct strtab_entry
14833{
14834 offset_type offset;
14835 const char *str;
14836};
14837
14838/* Hash function for a strtab_entry. */
b89be57b 14839
9291a0cd
TT
14840static hashval_t
14841hash_strtab_entry (const void *e)
14842{
14843 const struct strtab_entry *entry = e;
14844 return mapped_index_string_hash (entry->str);
14845}
14846
14847/* Equality function for a strtab_entry. */
b89be57b 14848
9291a0cd
TT
14849static int
14850eq_strtab_entry (const void *a, const void *b)
14851{
14852 const struct strtab_entry *ea = a;
14853 const struct strtab_entry *eb = b;
14854 return !strcmp (ea->str, eb->str);
14855}
14856
14857/* Create a strtab_entry hash table. */
b89be57b 14858
9291a0cd
TT
14859static htab_t
14860create_strtab (void)
14861{
14862 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14863 xfree, xcalloc, xfree);
14864}
14865
14866/* Add a string to the constant pool. Return the string's offset in
14867 host order. */
b89be57b 14868
9291a0cd
TT
14869static offset_type
14870add_string (htab_t table, struct obstack *cpool, const char *str)
14871{
14872 void **slot;
14873 struct strtab_entry entry;
14874 struct strtab_entry *result;
14875
14876 entry.str = str;
14877 slot = htab_find_slot (table, &entry, INSERT);
14878 if (*slot)
14879 result = *slot;
14880 else
14881 {
14882 result = XNEW (struct strtab_entry);
14883 result->offset = obstack_object_size (cpool);
14884 result->str = str;
14885 obstack_grow_str0 (cpool, str);
14886 *slot = result;
14887 }
14888 return result->offset;
14889}
14890
14891/* An entry in the symbol table. */
14892struct symtab_index_entry
14893{
14894 /* The name of the symbol. */
14895 const char *name;
14896 /* The offset of the name in the constant pool. */
14897 offset_type index_offset;
14898 /* A sorted vector of the indices of all the CUs that hold an object
14899 of this name. */
14900 VEC (offset_type) *cu_indices;
14901};
14902
14903/* The symbol table. This is a power-of-2-sized hash table. */
14904struct mapped_symtab
14905{
14906 offset_type n_elements;
14907 offset_type size;
14908 struct symtab_index_entry **data;
14909};
14910
14911/* Hash function for a symtab_index_entry. */
b89be57b 14912
9291a0cd
TT
14913static hashval_t
14914hash_symtab_entry (const void *e)
14915{
14916 const struct symtab_index_entry *entry = e;
14917 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14918 sizeof (offset_type) * VEC_length (offset_type,
14919 entry->cu_indices),
14920 0);
14921}
14922
14923/* Equality function for a symtab_index_entry. */
b89be57b 14924
9291a0cd
TT
14925static int
14926eq_symtab_entry (const void *a, const void *b)
14927{
14928 const struct symtab_index_entry *ea = a;
14929 const struct symtab_index_entry *eb = b;
14930 int len = VEC_length (offset_type, ea->cu_indices);
14931 if (len != VEC_length (offset_type, eb->cu_indices))
14932 return 0;
14933 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14934 VEC_address (offset_type, eb->cu_indices),
14935 sizeof (offset_type) * len);
14936}
14937
14938/* Destroy a symtab_index_entry. */
b89be57b 14939
9291a0cd
TT
14940static void
14941delete_symtab_entry (void *p)
14942{
14943 struct symtab_index_entry *entry = p;
14944 VEC_free (offset_type, entry->cu_indices);
14945 xfree (entry);
14946}
14947
14948/* Create a hash table holding symtab_index_entry objects. */
b89be57b 14949
9291a0cd 14950static htab_t
3876f04e 14951create_symbol_hash_table (void)
9291a0cd
TT
14952{
14953 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14954 delete_symtab_entry, xcalloc, xfree);
14955}
14956
14957/* Create a new mapped symtab object. */
b89be57b 14958
9291a0cd
TT
14959static struct mapped_symtab *
14960create_mapped_symtab (void)
14961{
14962 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14963 symtab->n_elements = 0;
14964 symtab->size = 1024;
14965 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14966 return symtab;
14967}
14968
14969/* Destroy a mapped_symtab. */
b89be57b 14970
9291a0cd
TT
14971static void
14972cleanup_mapped_symtab (void *p)
14973{
14974 struct mapped_symtab *symtab = p;
14975 /* The contents of the array are freed when the other hash table is
14976 destroyed. */
14977 xfree (symtab->data);
14978 xfree (symtab);
14979}
14980
14981/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14982 the slot. */
b89be57b 14983
9291a0cd
TT
14984static struct symtab_index_entry **
14985find_slot (struct mapped_symtab *symtab, const char *name)
14986{
14987 offset_type index, step, hash = mapped_index_string_hash (name);
14988
14989 index = hash & (symtab->size - 1);
14990 step = ((hash * 17) & (symtab->size - 1)) | 1;
14991
14992 for (;;)
14993 {
14994 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14995 return &symtab->data[index];
14996 index = (index + step) & (symtab->size - 1);
14997 }
14998}
14999
15000/* Expand SYMTAB's hash table. */
b89be57b 15001
9291a0cd
TT
15002static void
15003hash_expand (struct mapped_symtab *symtab)
15004{
15005 offset_type old_size = symtab->size;
15006 offset_type i;
15007 struct symtab_index_entry **old_entries = symtab->data;
15008
15009 symtab->size *= 2;
15010 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15011
15012 for (i = 0; i < old_size; ++i)
15013 {
15014 if (old_entries[i])
15015 {
15016 struct symtab_index_entry **slot = find_slot (symtab,
15017 old_entries[i]->name);
15018 *slot = old_entries[i];
15019 }
15020 }
15021
15022 xfree (old_entries);
15023}
15024
15025/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15026 is the index of the CU in which the symbol appears. */
b89be57b 15027
9291a0cd
TT
15028static void
15029add_index_entry (struct mapped_symtab *symtab, const char *name,
15030 offset_type cu_index)
15031{
15032 struct symtab_index_entry **slot;
15033
15034 ++symtab->n_elements;
15035 if (4 * symtab->n_elements / 3 >= symtab->size)
15036 hash_expand (symtab);
15037
15038 slot = find_slot (symtab, name);
15039 if (!*slot)
15040 {
15041 *slot = XNEW (struct symtab_index_entry);
15042 (*slot)->name = name;
15043 (*slot)->cu_indices = NULL;
15044 }
15045 /* Don't push an index twice. Due to how we add entries we only
15046 have to check the last one. */
15047 if (VEC_empty (offset_type, (*slot)->cu_indices)
15048 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15049 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15050}
15051
15052/* Add a vector of indices to the constant pool. */
b89be57b 15053
9291a0cd 15054static offset_type
3876f04e 15055add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15056 struct symtab_index_entry *entry)
15057{
15058 void **slot;
15059
3876f04e 15060 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15061 if (!*slot)
15062 {
15063 offset_type len = VEC_length (offset_type, entry->cu_indices);
15064 offset_type val = MAYBE_SWAP (len);
15065 offset_type iter;
15066 int i;
15067
15068 *slot = entry;
15069 entry->index_offset = obstack_object_size (cpool);
15070
15071 obstack_grow (cpool, &val, sizeof (val));
15072 for (i = 0;
15073 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15074 ++i)
15075 {
15076 val = MAYBE_SWAP (iter);
15077 obstack_grow (cpool, &val, sizeof (val));
15078 }
15079 }
15080 else
15081 {
15082 struct symtab_index_entry *old_entry = *slot;
15083 entry->index_offset = old_entry->index_offset;
15084 entry = old_entry;
15085 }
15086 return entry->index_offset;
15087}
15088
15089/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15090 constant pool entries going into the obstack CPOOL. */
b89be57b 15091
9291a0cd
TT
15092static void
15093write_hash_table (struct mapped_symtab *symtab,
15094 struct obstack *output, struct obstack *cpool)
15095{
15096 offset_type i;
3876f04e 15097 htab_t symbol_hash_table;
9291a0cd
TT
15098 htab_t str_table;
15099
3876f04e 15100 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15101 str_table = create_strtab ();
3876f04e 15102
9291a0cd
TT
15103 /* We add all the index vectors to the constant pool first, to
15104 ensure alignment is ok. */
15105 for (i = 0; i < symtab->size; ++i)
15106 {
15107 if (symtab->data[i])
3876f04e 15108 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15109 }
15110
15111 /* Now write out the hash table. */
15112 for (i = 0; i < symtab->size; ++i)
15113 {
15114 offset_type str_off, vec_off;
15115
15116 if (symtab->data[i])
15117 {
15118 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15119 vec_off = symtab->data[i]->index_offset;
15120 }
15121 else
15122 {
15123 /* While 0 is a valid constant pool index, it is not valid
15124 to have 0 for both offsets. */
15125 str_off = 0;
15126 vec_off = 0;
15127 }
15128
15129 str_off = MAYBE_SWAP (str_off);
15130 vec_off = MAYBE_SWAP (vec_off);
15131
15132 obstack_grow (output, &str_off, sizeof (str_off));
15133 obstack_grow (output, &vec_off, sizeof (vec_off));
15134 }
15135
15136 htab_delete (str_table);
3876f04e 15137 htab_delete (symbol_hash_table);
9291a0cd
TT
15138}
15139
15140/* Write an address entry to ADDR_OBSTACK. The addresses are taken
15141 from PST; CU_INDEX is the index of the CU in the vector of all
15142 CUs. */
b89be57b 15143
9291a0cd
TT
15144static void
15145add_address_entry (struct objfile *objfile,
15146 struct obstack *addr_obstack, struct partial_symtab *pst,
15147 unsigned int cu_index)
15148{
15149 offset_type offset;
15150 char addr[8];
15151 CORE_ADDR baseaddr;
15152
1fd400ff
TT
15153 /* Don't bother recording empty ranges. */
15154 if (pst->textlow == pst->texthigh)
15155 return;
15156
9291a0cd
TT
15157 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15158
15159 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15160 obstack_grow (addr_obstack, addr, 8);
15161 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15162 obstack_grow (addr_obstack, addr, 8);
15163 offset = MAYBE_SWAP (cu_index);
15164 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15165}
15166
15167/* Add a list of partial symbols to SYMTAB. */
b89be57b 15168
9291a0cd
TT
15169static void
15170write_psymbols (struct mapped_symtab *symtab,
987d643c 15171 htab_t psyms_seen,
9291a0cd
TT
15172 struct partial_symbol **psymp,
15173 int count,
987d643c
TT
15174 offset_type cu_index,
15175 int is_static)
9291a0cd
TT
15176{
15177 for (; count-- > 0; ++psymp)
15178 {
987d643c
TT
15179 void **slot, *lookup;
15180
9291a0cd
TT
15181 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15182 error (_("Ada is not currently supported by the index"));
987d643c
TT
15183
15184 /* We only want to add a given psymbol once. However, we also
15185 want to account for whether it is global or static. So, we
15186 may add it twice, using slightly different values. */
15187 if (is_static)
15188 {
15189 uintptr_t val = 1 | (uintptr_t) *psymp;
15190
15191 lookup = (void *) val;
15192 }
15193 else
15194 lookup = *psymp;
15195
15196 /* Only add a given psymbol once. */
15197 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15198 if (!*slot)
15199 {
15200 *slot = lookup;
15201 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15202 }
9291a0cd
TT
15203 }
15204}
15205
15206/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15207 exception if there is an error. */
b89be57b 15208
9291a0cd
TT
15209static void
15210write_obstack (FILE *file, struct obstack *obstack)
15211{
15212 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15213 file)
15214 != obstack_object_size (obstack))
15215 error (_("couldn't data write to file"));
15216}
15217
15218/* Unlink a file if the argument is not NULL. */
b89be57b 15219
9291a0cd
TT
15220static void
15221unlink_if_set (void *p)
15222{
15223 char **filename = p;
15224 if (*filename)
15225 unlink (*filename);
15226}
15227
1fd400ff
TT
15228/* A helper struct used when iterating over debug_types. */
15229struct signatured_type_index_data
15230{
15231 struct objfile *objfile;
15232 struct mapped_symtab *symtab;
15233 struct obstack *types_list;
987d643c 15234 htab_t psyms_seen;
1fd400ff
TT
15235 int cu_index;
15236};
15237
15238/* A helper function that writes a single signatured_type to an
15239 obstack. */
b89be57b 15240
1fd400ff
TT
15241static int
15242write_one_signatured_type (void **slot, void *d)
15243{
15244 struct signatured_type_index_data *info = d;
15245 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15246 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15247 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15248 gdb_byte val[8];
15249
15250 write_psymbols (info->symtab,
987d643c 15251 info->psyms_seen,
1fd400ff 15252 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15253 psymtab->n_global_syms, info->cu_index,
15254 0);
1fd400ff 15255 write_psymbols (info->symtab,
987d643c 15256 info->psyms_seen,
1fd400ff 15257 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15258 psymtab->n_static_syms, info->cu_index,
15259 1);
1fd400ff
TT
15260
15261 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15262 obstack_grow (info->types_list, val, 8);
15263 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15264 obstack_grow (info->types_list, val, 8);
15265 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15266 obstack_grow (info->types_list, val, 8);
15267
15268 ++info->cu_index;
15269
15270 return 1;
15271}
15272
987d643c
TT
15273/* A cleanup function for an htab_t. */
15274
15275static void
15276cleanup_htab (void *arg)
15277{
15278 htab_delete (arg);
15279}
15280
9291a0cd 15281/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15282
9291a0cd
TT
15283static void
15284write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15285{
15286 struct cleanup *cleanup;
15287 char *filename, *cleanup_filename;
1fd400ff
TT
15288 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15289 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15290 int i;
15291 FILE *out_file;
15292 struct mapped_symtab *symtab;
15293 offset_type val, size_of_contents, total_len;
15294 struct stat st;
15295 char buf[8];
987d643c 15296 htab_t psyms_seen;
9291a0cd
TT
15297
15298 if (!objfile->psymtabs)
15299 return;
15300 if (dwarf2_per_objfile->using_index)
15301 error (_("Cannot use an index to create the index"));
15302
15303 if (stat (objfile->name, &st) < 0)
15304 perror_with_name (_("Could not stat"));
15305
15306 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15307 INDEX_SUFFIX, (char *) NULL);
15308 cleanup = make_cleanup (xfree, filename);
15309
15310 out_file = fopen (filename, "wb");
15311 if (!out_file)
15312 error (_("Can't open `%s' for writing"), filename);
15313
15314 cleanup_filename = filename;
15315 make_cleanup (unlink_if_set, &cleanup_filename);
15316
15317 symtab = create_mapped_symtab ();
15318 make_cleanup (cleanup_mapped_symtab, symtab);
15319
15320 obstack_init (&addr_obstack);
15321 make_cleanup_obstack_free (&addr_obstack);
15322
15323 obstack_init (&cu_list);
15324 make_cleanup_obstack_free (&cu_list);
15325
1fd400ff
TT
15326 obstack_init (&types_cu_list);
15327 make_cleanup_obstack_free (&types_cu_list);
15328
987d643c
TT
15329 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15330 NULL, xcalloc, xfree);
15331 make_cleanup (cleanup_htab, psyms_seen);
15332
1fd400ff
TT
15333 /* The list is already sorted, so we don't need to do additional
15334 work here. Also, the debug_types entries do not appear in
15335 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15336 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15337 {
e254ef6a
DE
15338 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15339 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15340 gdb_byte val[8];
15341
15342 write_psymbols (symtab,
987d643c 15343 psyms_seen,
9291a0cd 15344 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15345 psymtab->n_global_syms, i,
15346 0);
9291a0cd 15347 write_psymbols (symtab,
987d643c 15348 psyms_seen,
9291a0cd 15349 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15350 psymtab->n_static_syms, i,
15351 1);
9291a0cd
TT
15352
15353 add_address_entry (objfile, &addr_obstack, psymtab, i);
15354
e254ef6a 15355 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15356 obstack_grow (&cu_list, val, 8);
e254ef6a 15357 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15358 obstack_grow (&cu_list, val, 8);
15359 }
15360
1fd400ff
TT
15361 /* Write out the .debug_type entries, if any. */
15362 if (dwarf2_per_objfile->signatured_types)
15363 {
15364 struct signatured_type_index_data sig_data;
15365
15366 sig_data.objfile = objfile;
15367 sig_data.symtab = symtab;
15368 sig_data.types_list = &types_cu_list;
987d643c 15369 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15370 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15371 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15372 write_one_signatured_type, &sig_data);
15373 }
15374
9291a0cd
TT
15375 obstack_init (&constant_pool);
15376 make_cleanup_obstack_free (&constant_pool);
15377 obstack_init (&symtab_obstack);
15378 make_cleanup_obstack_free (&symtab_obstack);
15379 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15380
15381 obstack_init (&contents);
15382 make_cleanup_obstack_free (&contents);
1fd400ff 15383 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15384 total_len = size_of_contents;
15385
15386 /* The version number. */
987d643c 15387 val = MAYBE_SWAP (3);
9291a0cd
TT
15388 obstack_grow (&contents, &val, sizeof (val));
15389
15390 /* The offset of the CU list from the start of the file. */
15391 val = MAYBE_SWAP (total_len);
15392 obstack_grow (&contents, &val, sizeof (val));
15393 total_len += obstack_object_size (&cu_list);
15394
1fd400ff
TT
15395 /* The offset of the types CU list from the start of the file. */
15396 val = MAYBE_SWAP (total_len);
15397 obstack_grow (&contents, &val, sizeof (val));
15398 total_len += obstack_object_size (&types_cu_list);
15399
9291a0cd
TT
15400 /* The offset of the address table from the start of the file. */
15401 val = MAYBE_SWAP (total_len);
15402 obstack_grow (&contents, &val, sizeof (val));
15403 total_len += obstack_object_size (&addr_obstack);
15404
15405 /* The offset of the symbol table from the start of the file. */
15406 val = MAYBE_SWAP (total_len);
15407 obstack_grow (&contents, &val, sizeof (val));
15408 total_len += obstack_object_size (&symtab_obstack);
15409
15410 /* The offset of the constant pool from the start of the file. */
15411 val = MAYBE_SWAP (total_len);
15412 obstack_grow (&contents, &val, sizeof (val));
15413 total_len += obstack_object_size (&constant_pool);
15414
15415 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15416
15417 write_obstack (out_file, &contents);
15418 write_obstack (out_file, &cu_list);
1fd400ff 15419 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15420 write_obstack (out_file, &addr_obstack);
15421 write_obstack (out_file, &symtab_obstack);
15422 write_obstack (out_file, &constant_pool);
15423
15424 fclose (out_file);
15425
15426 /* We want to keep the file, so we set cleanup_filename to NULL
15427 here. See unlink_if_set. */
15428 cleanup_filename = NULL;
15429
15430 do_cleanups (cleanup);
15431}
15432
15433/* The mapped index file format is designed to be directly mmap()able
15434 on any architecture. In most cases, a datum is represented using a
15435 little-endian 32-bit integer value, called an offset_type. Big
15436 endian machines must byte-swap the values before using them.
15437 Exceptions to this rule are noted. The data is laid out such that
15438 alignment is always respected.
15439
15440 A mapped index consists of several sections.
15441
15442 1. The file header. This is a sequence of values, of offset_type
15443 unless otherwise noted:
987d643c
TT
15444
15445 [0] The version number, currently 3. Versions 1 and 2 are
15446 obsolete.
9291a0cd 15447 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15448 [2] The offset, from the start of the file, of the types CU list.
15449 Note that this section can be empty, in which case this offset will
15450 be equal to the next offset.
15451 [3] The offset, from the start of the file, of the address section.
15452 [4] The offset, from the start of the file, of the symbol table.
15453 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15454
15455 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15456 little-endian values, sorted by the CU offset. The first element
15457 in each pair is the offset of a CU in the .debug_info section. The
15458 second element in each pair is the length of that CU. References
15459 to a CU elsewhere in the map are done using a CU index, which is
15460 just the 0-based index into this table. Note that if there are
15461 type CUs, then conceptually CUs and type CUs form a single list for
15462 the purposes of CU indices.
15463
987d643c
TT
15464 3. The types CU list. This is a sequence of triplets of 64-bit
15465 little-endian values. In a triplet, the first value is the CU
15466 offset, the second value is the type offset in the CU, and the
15467 third value is the type signature. The types CU list is not
15468 sorted.
9291a0cd 15469
987d643c 15470 4. The address section. The address section consists of a sequence
9291a0cd
TT
15471 of address entries. Each address entry has three elements.
15472 [0] The low address. This is a 64-bit little-endian value.
15473 [1] The high address. This is a 64-bit little-endian value.
15474 [2] The CU index. This is an offset_type value.
15475
987d643c 15476 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15477 table is always a power of 2. The initial hash and the step are
15478 currently defined by the `find_slot' function.
15479
15480 Each slot in the hash table consists of a pair of offset_type
15481 values. The first value is the offset of the symbol's name in the
15482 constant pool. The second value is the offset of the CU vector in
15483 the constant pool.
15484
15485 If both values are 0, then this slot in the hash table is empty.
15486 This is ok because while 0 is a valid constant pool index, it
15487 cannot be a valid index for both a string and a CU vector.
15488
15489 A string in the constant pool is stored as a \0-terminated string,
15490 as you'd expect.
15491
15492 A CU vector in the constant pool is a sequence of offset_type
15493 values. The first value is the number of CU indices in the vector.
15494 Each subsequent value is the index of a CU in the CU list. This
15495 element in the hash table is used to indicate which CUs define the
15496 symbol.
15497
987d643c 15498 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15499 organized so that alignment is correct: CU vectors are stored
15500 first, followed by strings. */
11570e71 15501
9291a0cd
TT
15502static void
15503save_gdb_index_command (char *arg, int from_tty)
15504{
15505 struct objfile *objfile;
15506
15507 if (!arg || !*arg)
96d19272 15508 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15509
15510 ALL_OBJFILES (objfile)
15511 {
15512 struct stat st;
15513
15514 /* If the objfile does not correspond to an actual file, skip it. */
15515 if (stat (objfile->name, &st) < 0)
15516 continue;
15517
15518 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15519 if (dwarf2_per_objfile)
15520 {
15521 volatile struct gdb_exception except;
15522
15523 TRY_CATCH (except, RETURN_MASK_ERROR)
15524 {
15525 write_psymtabs_to_index (objfile, arg);
15526 }
15527 if (except.reason < 0)
15528 exception_fprintf (gdb_stderr, except,
15529 _("Error while writing index for `%s': "),
15530 objfile->name);
15531 }
15532 }
dce234bc
PP
15533}
15534
9291a0cd
TT
15535\f
15536
9eae7c52
TT
15537int dwarf2_always_disassemble;
15538
15539static void
15540show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15541 struct cmd_list_element *c, const char *value)
15542{
15543 fprintf_filtered (file, _("\
15544Whether to always disassemble DWARF expressions is %s.\n"),
15545 value);
15546}
15547
6502dd73
DJ
15548void _initialize_dwarf2_read (void);
15549
15550void
15551_initialize_dwarf2_read (void)
15552{
96d19272
JK
15553 struct cmd_list_element *c;
15554
dce234bc 15555 dwarf2_objfile_data_key
c1bd65d0 15556 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15557
1bedd215
AC
15558 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15559Set DWARF 2 specific variables.\n\
15560Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15561 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15562 0/*allow-unknown*/, &maintenance_set_cmdlist);
15563
1bedd215
AC
15564 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15565Show DWARF 2 specific variables\n\
15566Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15567 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15568 0/*allow-unknown*/, &maintenance_show_cmdlist);
15569
15570 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15571 &dwarf2_max_cache_age, _("\
15572Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15573Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15574A higher limit means that cached compilation units will be stored\n\
15575in memory longer, and more total memory will be used. Zero disables\n\
15576caching, which can slow down startup."),
2c5b56ce 15577 NULL,
920d2a44 15578 show_dwarf2_max_cache_age,
2c5b56ce 15579 &set_dwarf2_cmdlist,
ae038cb0 15580 &show_dwarf2_cmdlist);
d97bc12b 15581
9eae7c52
TT
15582 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15583 &dwarf2_always_disassemble, _("\
15584Set whether `info address' always disassembles DWARF expressions."), _("\
15585Show whether `info address' always disassembles DWARF expressions."), _("\
15586When enabled, DWARF expressions are always printed in an assembly-like\n\
15587syntax. When disabled, expressions will be printed in a more\n\
15588conversational style, when possible."),
15589 NULL,
15590 show_dwarf2_always_disassemble,
15591 &set_dwarf2_cmdlist,
15592 &show_dwarf2_cmdlist);
15593
d97bc12b
DE
15594 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15595Set debugging of the dwarf2 DIE reader."), _("\
15596Show debugging of the dwarf2 DIE reader."), _("\
15597When enabled (non-zero), DIEs are dumped after they are read in.\n\
15598The value is the maximum depth to print."),
15599 NULL,
15600 NULL,
15601 &setdebuglist, &showdebuglist);
9291a0cd 15602
96d19272 15603 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71
DE
15604 _("\
15605Save a .gdb-index file.\n\
15606Usage: save gdb-index DIRECTORY"),
96d19272
JK
15607 &save_cmdlist);
15608 set_cmd_completer (c, filename_completer);
6502dd73 15609}
This page took 2.169212 seconds and 4 git commands to generate.