gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
3876f04e
DE
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
673bfd45
DE
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
6502dd73
DJ
226};
227
228static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
229
230/* names of the debugging sections */
231
233a11ab
CS
232/* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235#define INFO_SECTION "debug_info"
236#define ABBREV_SECTION "debug_abbrev"
237#define LINE_SECTION "debug_line"
233a11ab
CS
238#define LOC_SECTION "debug_loc"
239#define MACINFO_SECTION "debug_macinfo"
240#define STR_SECTION "debug_str"
241#define RANGES_SECTION "debug_ranges"
348e048f 242#define TYPES_SECTION "debug_types"
233a11ab
CS
243#define FRAME_SECTION "debug_frame"
244#define EH_FRAME_SECTION "eh_frame"
9291a0cd 245#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
246
247/* local data types */
248
57349743
JB
249/* We hold several abbreviation tables in memory at the same time. */
250#ifndef ABBREV_HASH_SIZE
251#define ABBREV_HASH_SIZE 121
252#endif
253
107d2387
AC
254/* The data in a compilation unit header, after target2host
255 translation, looks like this. */
c906108c 256struct comp_unit_head
a738430d 257{
c764a876 258 unsigned int length;
a738430d 259 short version;
a738430d
MK
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
9cbfa09e 262 unsigned int abbrev_offset;
57349743 263
a738430d
MK
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
57349743 266
a738430d
MK
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
57349743 269
a738430d
MK
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
57349743 273
d00adf39
DE
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
a738430d 277};
c906108c 278
3da10d80
KS
279/* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281struct delayed_method_info
282{
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297};
298
299typedef struct delayed_method_info delayed_method_info;
300DEF_VEC_O (delayed_method_info);
301
e7c27a73
DJ
302/* Internal state when decoding a particular compilation unit. */
303struct dwarf2_cu
304{
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
d00adf39 308 /* The header of the compilation unit. */
e7c27a73 309 struct comp_unit_head header;
e142c38c 310
d00adf39
DE
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
e142c38c
DJ
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492
DJ
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
10b3939b 361 /* A hash table of die offsets for following references. */
51545339 362 htab_t die_hash;
10b3939b
DJ
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
cb1df416
DJ
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
3da10d80
KS
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
ae038cb0
DJ
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
72bf9492
DJ
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
e7c27a73
DJ
391};
392
9291a0cd
TT
393/* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396struct dwarf2_per_cu_quick_data
397{
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417};
418
10b3939b
DJ
419/* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
ae038cb0
DJ
425struct dwarf2_per_cu_data
426{
348e048f 427 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 428 bytes should suffice to store the length of any compilation unit
45452591
DE
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
c764a876 432 unsigned int offset;
348e048f 433 unsigned int length : 29;
ae038cb0
DJ
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
c764a876 437 unsigned int queued : 1;
ae038cb0 438
5afb4e99
DJ
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
348e048f
DE
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
17ea53c3
JK
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
ae038cb0 451 struct dwarf2_cu *cu;
1c379e20 452
9291a0cd
TT
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
ae038cb0
DJ
468};
469
348e048f
DE
470/* Entry in the signatured_types hash table. */
471
472struct signatured_type
473{
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484};
485
93311388
DE
486/* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492struct die_reader_specs
493{
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503};
504
debd256d
JB
505/* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508struct line_header
509{
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
2dc7f7b3 514 unsigned char maximum_ops_per_instruction;
debd256d
JB
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
c906108c 538 {
debd256d
JB
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
aaa75496 543 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 544 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
545 } *file_names;
546
547 /* The start and end of the statement program following this
6502dd73 548 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 549 gdb_byte *statement_program_start, *statement_program_end;
debd256d 550};
c906108c
SS
551
552/* When we construct a partial symbol table entry we only
553 need this much information. */
554struct partial_die_info
555 {
72bf9492 556 /* Offset of this DIE. */
c906108c 557 unsigned int offset;
72bf9492
DJ
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
72bf9492
DJ
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
fa4028e9
JB
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
98bfdba5
PA
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
abc72ce4
DE
580 /* Flag set if fixup_partial_die has been called on this die. */
581 unsigned int fixup_called : 1;
582
72bf9492 583 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 584 sometimes a default name for unnamed DIEs. */
c906108c 585 char *name;
72bf9492 586
abc72ce4
DE
587 /* The linkage name, if present. */
588 const char *linkage_name;
589
72bf9492
DJ
590 /* The scope to prepend to our children. This is generally
591 allocated on the comp_unit_obstack, so will disappear
592 when this compilation unit leaves the cache. */
593 char *scope;
594
595 /* The location description associated with this DIE, if any. */
596 struct dwarf_block *locdesc;
597
598 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
599 CORE_ADDR lowpc;
600 CORE_ADDR highpc;
72bf9492 601
93311388 602 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 603 DW_AT_sibling, if any. */
abc72ce4
DE
604 /* NOTE: This member isn't strictly necessary, read_partial_die could
605 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 606 gdb_byte *sibling;
72bf9492
DJ
607
608 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
609 DW_AT_specification (or DW_AT_abstract_origin or
610 DW_AT_extension). */
611 unsigned int spec_offset;
612
613 /* Pointers to this DIE's parent, first child, and next sibling,
614 if any. */
615 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
616 };
617
618/* This data structure holds the information of an abbrev. */
619struct abbrev_info
620 {
621 unsigned int number; /* number identifying abbrev */
622 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
623 unsigned short has_children; /* boolean */
624 unsigned short num_attrs; /* number of attributes */
c906108c
SS
625 struct attr_abbrev *attrs; /* an array of attribute descriptions */
626 struct abbrev_info *next; /* next in chain */
627 };
628
629struct attr_abbrev
630 {
9d25dd43
DE
631 ENUM_BITFIELD(dwarf_attribute) name : 16;
632 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
633 };
634
b60c80d6
DJ
635/* Attributes have a name and a value */
636struct attribute
637 {
9d25dd43 638 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
639 ENUM_BITFIELD(dwarf_form) form : 15;
640
641 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
642 field should be in u.str (existing only for DW_STRING) but it is kept
643 here for better struct attribute alignment. */
644 unsigned int string_is_canonical : 1;
645
b60c80d6
DJ
646 union
647 {
648 char *str;
649 struct dwarf_block *blk;
43bbcdc2
PH
650 ULONGEST unsnd;
651 LONGEST snd;
b60c80d6 652 CORE_ADDR addr;
348e048f 653 struct signatured_type *signatured_type;
b60c80d6
DJ
654 }
655 u;
656 };
657
c906108c
SS
658/* This data structure holds a complete die structure. */
659struct die_info
660 {
76815b17
DE
661 /* DWARF-2 tag for this DIE. */
662 ENUM_BITFIELD(dwarf_tag) tag : 16;
663
664 /* Number of attributes */
98bfdba5
PA
665 unsigned char num_attrs;
666
667 /* True if we're presently building the full type name for the
668 type derived from this DIE. */
669 unsigned char building_fullname : 1;
76815b17
DE
670
671 /* Abbrev number */
672 unsigned int abbrev;
673
93311388 674 /* Offset in .debug_info or .debug_types section. */
76815b17 675 unsigned int offset;
78ba4af6
JB
676
677 /* The dies in a compilation unit form an n-ary tree. PARENT
678 points to this die's parent; CHILD points to the first child of
679 this node; and all the children of a given node are chained
4950bc1c 680 together via their SIBLING fields. */
639d11d3
DC
681 struct die_info *child; /* Its first child, if any. */
682 struct die_info *sibling; /* Its next sibling, if any. */
683 struct die_info *parent; /* Its parent, if any. */
c906108c 684
b60c80d6
DJ
685 /* An array of attributes, with NUM_ATTRS elements. There may be
686 zero, but it's not common and zero-sized arrays are not
687 sufficiently portable C. */
688 struct attribute attrs[1];
c906108c
SS
689 };
690
5fb290d7
DJ
691struct function_range
692{
693 const char *name;
694 CORE_ADDR lowpc, highpc;
695 int seen_line;
696 struct function_range *next;
697};
698
c906108c
SS
699/* Get at parts of an attribute structure */
700
701#define DW_STRING(attr) ((attr)->u.str)
8285870a 702#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
703#define DW_UNSND(attr) ((attr)->u.unsnd)
704#define DW_BLOCK(attr) ((attr)->u.blk)
705#define DW_SND(attr) ((attr)->u.snd)
706#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 707#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
708
709/* Blocks are a bunch of untyped bytes. */
710struct dwarf_block
711 {
712 unsigned int size;
fe1b8b76 713 gdb_byte *data;
c906108c
SS
714 };
715
c906108c
SS
716#ifndef ATTR_ALLOC_CHUNK
717#define ATTR_ALLOC_CHUNK 4
718#endif
719
c906108c
SS
720/* Allocate fields for structs, unions and enums in this size. */
721#ifndef DW_FIELD_ALLOC_CHUNK
722#define DW_FIELD_ALLOC_CHUNK 4
723#endif
724
c906108c
SS
725/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
726 but this would require a corresponding change in unpack_field_as_long
727 and friends. */
728static int bits_per_byte = 8;
729
730/* The routines that read and process dies for a C struct or C++ class
731 pass lists of data member fields and lists of member function fields
732 in an instance of a field_info structure, as defined below. */
733struct field_info
c5aa993b
JM
734 {
735 /* List of data member and baseclasses fields. */
736 struct nextfield
737 {
738 struct nextfield *next;
739 int accessibility;
740 int virtuality;
741 struct field field;
742 }
7d0ccb61 743 *fields, *baseclasses;
c906108c 744
7d0ccb61 745 /* Number of fields (including baseclasses). */
c5aa993b 746 int nfields;
c906108c 747
c5aa993b
JM
748 /* Number of baseclasses. */
749 int nbaseclasses;
c906108c 750
c5aa993b
JM
751 /* Set if the accesibility of one of the fields is not public. */
752 int non_public_fields;
c906108c 753
c5aa993b
JM
754 /* Member function fields array, entries are allocated in the order they
755 are encountered in the object file. */
756 struct nextfnfield
757 {
758 struct nextfnfield *next;
759 struct fn_field fnfield;
760 }
761 *fnfields;
c906108c 762
c5aa993b
JM
763 /* Member function fieldlist array, contains name of possibly overloaded
764 member function, number of overloaded member functions and a pointer
765 to the head of the member function field chain. */
766 struct fnfieldlist
767 {
768 char *name;
769 int length;
770 struct nextfnfield *head;
771 }
772 *fnfieldlists;
c906108c 773
c5aa993b
JM
774 /* Number of entries in the fnfieldlists array. */
775 int nfnfields;
98751a41
JK
776
777 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
778 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
779 struct typedef_field_list
780 {
781 struct typedef_field field;
782 struct typedef_field_list *next;
783 }
784 *typedef_field_list;
785 unsigned typedef_field_list_count;
c5aa993b 786 };
c906108c 787
10b3939b
DJ
788/* One item on the queue of compilation units to read in full symbols
789 for. */
790struct dwarf2_queue_item
791{
792 struct dwarf2_per_cu_data *per_cu;
793 struct dwarf2_queue_item *next;
794};
795
796/* The current queue. */
797static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
798
ae038cb0
DJ
799/* Loaded secondary compilation units are kept in memory until they
800 have not been referenced for the processing of this many
801 compilation units. Set this to zero to disable caching. Cache
802 sizes of up to at least twenty will improve startup time for
803 typical inter-CU-reference binaries, at an obvious memory cost. */
804static int dwarf2_max_cache_age = 5;
920d2a44
AC
805static void
806show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
807 struct cmd_list_element *c, const char *value)
808{
809 fprintf_filtered (file, _("\
810The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
811 value);
812}
813
ae038cb0 814
c906108c
SS
815/* Various complaints about symbol reading that don't abort the process */
816
4d3c2250
KB
817static void
818dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 819{
4d3c2250 820 complaint (&symfile_complaints,
e2e0b3e5 821 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
822}
823
25e43795
DJ
824static void
825dwarf2_debug_line_missing_file_complaint (void)
826{
827 complaint (&symfile_complaints,
828 _(".debug_line section has line data without a file"));
829}
830
59205f5a
JB
831static void
832dwarf2_debug_line_missing_end_sequence_complaint (void)
833{
834 complaint (&symfile_complaints,
835 _(".debug_line section has line program sequence without an end"));
836}
837
4d3c2250
KB
838static void
839dwarf2_complex_location_expr_complaint (void)
2e276125 840{
e2e0b3e5 841 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
842}
843
4d3c2250
KB
844static void
845dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
846 int arg3)
2e276125 847{
4d3c2250 848 complaint (&symfile_complaints,
e2e0b3e5 849 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
850 arg2, arg3);
851}
852
853static void
854dwarf2_macros_too_long_complaint (void)
2e276125 855{
4d3c2250 856 complaint (&symfile_complaints,
e2e0b3e5 857 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
858}
859
860static void
861dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 862{
4d3c2250 863 complaint (&symfile_complaints,
e2e0b3e5 864 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
865 arg1);
866}
867
868static void
869dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 870{
4d3c2250 871 complaint (&symfile_complaints,
e2e0b3e5 872 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 873}
c906108c 874
c906108c
SS
875/* local function prototypes */
876
4efb68b1 877static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 878
aaa75496
JB
879static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
880 struct objfile *);
881
c67a9c90 882static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 883
72bf9492
DJ
884static void scan_partial_symbols (struct partial_die_info *,
885 CORE_ADDR *, CORE_ADDR *,
5734ee8b 886 int, struct dwarf2_cu *);
c906108c 887
72bf9492
DJ
888static void add_partial_symbol (struct partial_die_info *,
889 struct dwarf2_cu *);
63d06c5c 890
72bf9492
DJ
891static void add_partial_namespace (struct partial_die_info *pdi,
892 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 893 int need_pc, struct dwarf2_cu *cu);
63d06c5c 894
5d7cb8df
JK
895static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
896 CORE_ADDR *highpc, int need_pc,
897 struct dwarf2_cu *cu);
898
72bf9492
DJ
899static void add_partial_enumeration (struct partial_die_info *enum_pdi,
900 struct dwarf2_cu *cu);
91c24f0a 901
bc30ff58
JB
902static void add_partial_subprogram (struct partial_die_info *pdi,
903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 904 int need_pc, struct dwarf2_cu *cu);
bc30ff58 905
fe1b8b76 906static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
907 gdb_byte *buffer, gdb_byte *info_ptr,
908 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 909
a14ed312 910static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 911
a14ed312 912static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 913
e7c27a73 914static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 915
f3dd6933 916static void dwarf2_free_abbrev_table (void *);
c906108c 917
fe1b8b76 918static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 919 struct dwarf2_cu *);
72bf9492 920
57349743 921static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 922 struct dwarf2_cu *);
c906108c 923
93311388
DE
924static struct partial_die_info *load_partial_dies (bfd *,
925 gdb_byte *, gdb_byte *,
926 int, struct dwarf2_cu *);
72bf9492 927
fe1b8b76 928static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
929 struct abbrev_info *abbrev,
930 unsigned int, bfd *,
931 gdb_byte *, gdb_byte *,
932 struct dwarf2_cu *);
c906108c 933
c764a876 934static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 935 struct dwarf2_cu *);
72bf9492
DJ
936
937static void fixup_partial_die (struct partial_die_info *,
938 struct dwarf2_cu *);
939
fe1b8b76
JB
940static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 942
fe1b8b76
JB
943static gdb_byte *read_attribute_value (struct attribute *, unsigned,
944 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 945
fe1b8b76 946static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 947
fe1b8b76 948static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 949
fe1b8b76 950static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 951
fe1b8b76 952static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 953
93311388 954static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 955
fe1b8b76 956static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 957 unsigned int *);
c906108c 958
c764a876
DE
959static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
960
961static LONGEST read_checked_initial_length_and_offset
962 (bfd *, gdb_byte *, const struct comp_unit_head *,
963 unsigned int *, unsigned int *);
613e1657 964
fe1b8b76 965static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
966 unsigned int *);
967
968static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 969
fe1b8b76 970static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 971
9b1c24c8 972static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 973
fe1b8b76
JB
974static char *read_indirect_string (bfd *, gdb_byte *,
975 const struct comp_unit_head *,
976 unsigned int *);
4bdf3d34 977
fe1b8b76 978static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 979
fe1b8b76 980static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 981
fe1b8b76 982static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 983
e142c38c 984static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 985
e142c38c
DJ
986static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987 struct dwarf2_cu *);
c906108c 988
348e048f
DE
989static struct attribute *dwarf2_attr_no_follow (struct die_info *,
990 unsigned int,
991 struct dwarf2_cu *);
992
05cf31d1
JB
993static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
994 struct dwarf2_cu *cu);
995
e142c38c 996static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 997
e142c38c 998static struct die_info *die_specification (struct die_info *die,
f2f0e013 999 struct dwarf2_cu **);
63d06c5c 1000
debd256d
JB
1001static void free_line_header (struct line_header *lh);
1002
aaa75496
JB
1003static void add_file_name (struct line_header *, char *, unsigned int,
1004 unsigned int, unsigned int);
1005
debd256d
JB
1006static struct line_header *(dwarf_decode_line_header
1007 (unsigned int offset,
e7c27a73 1008 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1009
72b9f47f 1010static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1011 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1012
72b9f47f 1013static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1014
a14ed312 1015static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1016 struct dwarf2_cu *);
c906108c 1017
34eaf542
TT
1018static struct symbol *new_symbol_full (struct die_info *, struct type *,
1019 struct dwarf2_cu *, struct symbol *);
1020
a14ed312 1021static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1022 struct dwarf2_cu *);
c906108c 1023
98bfdba5
PA
1024static void dwarf2_const_value_attr (struct attribute *attr,
1025 struct type *type,
1026 const char *name,
1027 struct obstack *obstack,
1028 struct dwarf2_cu *cu, long *value,
1029 gdb_byte **bytes,
1030 struct dwarf2_locexpr_baton **baton);
2df3850c 1031
e7c27a73 1032static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1033
b4ba55a1
JB
1034static int need_gnat_info (struct dwarf2_cu *);
1035
1036static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1037
1038static void set_descriptive_type (struct type *, struct die_info *,
1039 struct dwarf2_cu *);
1040
e7c27a73
DJ
1041static struct type *die_containing_type (struct die_info *,
1042 struct dwarf2_cu *);
c906108c 1043
673bfd45
DE
1044static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045 struct dwarf2_cu *);
c906108c 1046
f792889a 1047static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1048
673bfd45
DE
1049static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
086ed43d 1051static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1052
6e70227d 1053static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1054 const char *suffix, int physname,
1055 struct dwarf2_cu *cu);
63d06c5c 1056
e7c27a73 1057static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1058
348e048f
DE
1059static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
e7c27a73 1061static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1062
e7c27a73 1063static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1064
ff013f42
JK
1065static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066 struct dwarf2_cu *, struct partial_symtab *);
1067
a14ed312 1068static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1069 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070 struct partial_symtab *);
c906108c 1071
fae299cd
DC
1072static void get_scope_pc_bounds (struct die_info *,
1073 CORE_ADDR *, CORE_ADDR *,
1074 struct dwarf2_cu *);
1075
801e3a5b
JB
1076static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077 CORE_ADDR, struct dwarf2_cu *);
1078
a14ed312 1079static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1080 struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1083 struct type *, struct dwarf2_cu *);
c906108c 1084
a14ed312 1085static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1086 struct die_info *, struct type *,
e7c27a73 1087 struct dwarf2_cu *);
c906108c 1088
a14ed312 1089static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1090 struct type *, struct dwarf2_cu *);
c906108c 1091
134d01f1 1092static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1093
e7c27a73 1094static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1097
5d7cb8df
JK
1098static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1099
27aa8d6a
SW
1100static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1101
f55ee35c
JK
1102static struct type *read_module_type (struct die_info *die,
1103 struct dwarf2_cu *cu);
1104
38d518c9 1105static const char *namespace_name (struct die_info *die,
e142c38c 1106 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1107
134d01f1 1108static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1109
e7c27a73 1110static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1111
6e70227d 1112static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1113 struct dwarf2_cu *);
1114
93311388 1115static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1116
93311388
DE
1117static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1118 gdb_byte *info_ptr,
d97bc12b
DE
1119 gdb_byte **new_info_ptr,
1120 struct die_info *parent);
1121
93311388
DE
1122static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1123 gdb_byte *info_ptr,
fe1b8b76 1124 gdb_byte **new_info_ptr,
639d11d3
DC
1125 struct die_info *parent);
1126
93311388
DE
1127static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1128 gdb_byte *info_ptr,
fe1b8b76 1129 gdb_byte **new_info_ptr,
639d11d3
DC
1130 struct die_info *parent);
1131
93311388
DE
1132static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1133 struct die_info **, gdb_byte *,
1134 int *);
1135
e7c27a73 1136static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1137
71c25dea
TT
1138static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1139 struct obstack *);
1140
e142c38c 1141static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1142
98bfdba5
PA
1143static const char *dwarf2_full_name (char *name,
1144 struct die_info *die,
1145 struct dwarf2_cu *cu);
1146
e142c38c 1147static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1148 struct dwarf2_cu **);
9219021c 1149
a14ed312 1150static char *dwarf_tag_name (unsigned int);
c906108c 1151
a14ed312 1152static char *dwarf_attr_name (unsigned int);
c906108c 1153
a14ed312 1154static char *dwarf_form_name (unsigned int);
c906108c 1155
a14ed312 1156static char *dwarf_bool_name (unsigned int);
c906108c 1157
a14ed312 1158static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1159
1160#if 0
a14ed312 1161static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1162#endif
1163
f9aca02d 1164static struct die_info *sibling_die (struct die_info *);
c906108c 1165
d97bc12b
DE
1166static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1167
1168static void dump_die_for_error (struct die_info *);
1169
1170static void dump_die_1 (struct ui_file *, int level, int max_level,
1171 struct die_info *);
c906108c 1172
d97bc12b 1173/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1174
51545339 1175static void store_in_ref_table (struct die_info *,
10b3939b 1176 struct dwarf2_cu *);
c906108c 1177
93311388
DE
1178static int is_ref_attr (struct attribute *);
1179
c764a876 1180static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1181
43bbcdc2 1182static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1183
348e048f
DE
1184static struct die_info *follow_die_ref_or_sig (struct die_info *,
1185 struct attribute *,
1186 struct dwarf2_cu **);
1187
10b3939b
DJ
1188static struct die_info *follow_die_ref (struct die_info *,
1189 struct attribute *,
f2f0e013 1190 struct dwarf2_cu **);
c906108c 1191
348e048f
DE
1192static struct die_info *follow_die_sig (struct die_info *,
1193 struct attribute *,
1194 struct dwarf2_cu **);
1195
1196static void read_signatured_type_at_offset (struct objfile *objfile,
1197 unsigned int offset);
1198
1199static void read_signatured_type (struct objfile *,
1200 struct signatured_type *type_sig);
1201
c906108c
SS
1202/* memory allocation interface */
1203
7b5a2f43 1204static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1205
f3dd6933 1206static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1207
b60c80d6 1208static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1209
e142c38c 1210static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1211
e142c38c
DJ
1212static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1213 struct dwarf2_cu *);
5fb290d7 1214
2e276125 1215static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1216 char *, bfd *, struct dwarf2_cu *);
2e276125 1217
8e19ed76
PS
1218static int attr_form_is_block (struct attribute *);
1219
3690dd37
JB
1220static int attr_form_is_section_offset (struct attribute *);
1221
1222static int attr_form_is_constant (struct attribute *);
1223
93e7bd98
DJ
1224static void dwarf2_symbol_mark_computed (struct attribute *attr,
1225 struct symbol *sym,
1226 struct dwarf2_cu *cu);
4c2df51b 1227
93311388
DE
1228static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1229 struct abbrev_info *abbrev,
1230 struct dwarf2_cu *cu);
4bb7a0a7 1231
72bf9492
DJ
1232static void free_stack_comp_unit (void *);
1233
72bf9492
DJ
1234static hashval_t partial_die_hash (const void *item);
1235
1236static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1237
ae038cb0 1238static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1239 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1240
1241static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1242 (unsigned int offset, struct objfile *objfile);
ae038cb0 1243
9816fde3
JK
1244static void init_one_comp_unit (struct dwarf2_cu *cu,
1245 struct objfile *objfile);
1246
1247static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1248 struct die_info *comp_unit_die);
93311388 1249
ae038cb0
DJ
1250static void free_one_comp_unit (void *);
1251
1252static void free_cached_comp_units (void *);
1253
1254static void age_cached_comp_units (void);
1255
1256static void free_one_cached_comp_unit (void *);
1257
f792889a
DJ
1258static struct type *set_die_type (struct die_info *, struct type *,
1259 struct dwarf2_cu *);
1c379e20 1260
ae038cb0
DJ
1261static void create_all_comp_units (struct objfile *);
1262
1fd400ff
TT
1263static int create_debug_types_hash_table (struct objfile *objfile);
1264
93311388
DE
1265static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1266 struct objfile *);
10b3939b
DJ
1267
1268static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1269
1270static void dwarf2_add_dependence (struct dwarf2_cu *,
1271 struct dwarf2_per_cu_data *);
1272
ae038cb0
DJ
1273static void dwarf2_mark (struct dwarf2_cu *);
1274
1275static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1276
673bfd45
DE
1277static struct type *get_die_type_at_offset (unsigned int,
1278 struct dwarf2_per_cu_data *per_cu);
1279
f792889a 1280static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1281
9291a0cd
TT
1282static void dwarf2_release_queue (void *dummy);
1283
1284static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1285 struct objfile *objfile);
1286
1287static void process_queue (struct objfile *objfile);
1288
1289static void find_file_and_directory (struct die_info *die,
1290 struct dwarf2_cu *cu,
1291 char **name, char **comp_dir);
1292
1293static char *file_full_name (int file, struct line_header *lh,
1294 const char *comp_dir);
1295
1296static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1297 gdb_byte *info_ptr,
1298 gdb_byte *buffer,
1299 unsigned int buffer_size,
1300 bfd *abfd);
1301
1302static void init_cu_die_reader (struct die_reader_specs *reader,
1303 struct dwarf2_cu *cu);
1304
673bfd45 1305static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1306
9291a0cd
TT
1307#if WORDS_BIGENDIAN
1308
1309/* Convert VALUE between big- and little-endian. */
1310static offset_type
1311byte_swap (offset_type value)
1312{
1313 offset_type result;
1314
1315 result = (value & 0xff) << 24;
1316 result |= (value & 0xff00) << 8;
1317 result |= (value & 0xff0000) >> 8;
1318 result |= (value & 0xff000000) >> 24;
1319 return result;
1320}
1321
1322#define MAYBE_SWAP(V) byte_swap (V)
1323
1324#else
1325#define MAYBE_SWAP(V) (V)
1326#endif /* WORDS_BIGENDIAN */
1327
1328/* The suffix for an index file. */
1329#define INDEX_SUFFIX ".gdb-index"
1330
3da10d80
KS
1331static const char *dwarf2_physname (char *name, struct die_info *die,
1332 struct dwarf2_cu *cu);
1333
c906108c
SS
1334/* Try to locate the sections we need for DWARF 2 debugging
1335 information and return true if we have enough to do something. */
1336
1337int
6502dd73 1338dwarf2_has_info (struct objfile *objfile)
c906108c 1339{
be391dca
TT
1340 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1341 if (!dwarf2_per_objfile)
1342 {
1343 /* Initialize per-objfile state. */
1344 struct dwarf2_per_objfile *data
1345 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1346
be391dca
TT
1347 memset (data, 0, sizeof (*data));
1348 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1349 dwarf2_per_objfile = data;
6502dd73 1350
be391dca
TT
1351 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1352 dwarf2_per_objfile->objfile = objfile;
1353 }
1354 return (dwarf2_per_objfile->info.asection != NULL
1355 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1356}
1357
233a11ab
CS
1358/* When loading sections, we can either look for ".<name>", or for
1359 * ".z<name>", which indicates a compressed section. */
1360
1361static int
dce234bc 1362section_is_p (const char *section_name, const char *name)
233a11ab 1363{
dce234bc
PP
1364 return (section_name[0] == '.'
1365 && (strcmp (section_name + 1, name) == 0
1366 || (section_name[1] == 'z'
1367 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1368}
1369
c906108c
SS
1370/* This function is mapped across the sections and remembers the
1371 offset and size of each of the debugging sections we are interested
1372 in. */
1373
1374static void
72dca2f5 1375dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1376{
dce234bc 1377 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1378 {
dce234bc
PP
1379 dwarf2_per_objfile->info.asection = sectp;
1380 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1381 }
dce234bc 1382 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1383 {
dce234bc
PP
1384 dwarf2_per_objfile->abbrev.asection = sectp;
1385 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1386 }
dce234bc 1387 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1388 {
dce234bc
PP
1389 dwarf2_per_objfile->line.asection = sectp;
1390 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1391 }
dce234bc 1392 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1393 {
dce234bc
PP
1394 dwarf2_per_objfile->loc.asection = sectp;
1395 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1396 }
dce234bc 1397 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->macinfo.asection = sectp;
1400 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1401 }
dce234bc 1402 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1403 {
dce234bc
PP
1404 dwarf2_per_objfile->str.asection = sectp;
1405 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1406 }
dce234bc 1407 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1408 {
dce234bc
PP
1409 dwarf2_per_objfile->frame.asection = sectp;
1410 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1411 }
dce234bc 1412 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1413 {
3799ccc6 1414 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1415
3799ccc6
EZ
1416 if (aflag & SEC_HAS_CONTENTS)
1417 {
dce234bc
PP
1418 dwarf2_per_objfile->eh_frame.asection = sectp;
1419 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1420 }
b6af0555 1421 }
dce234bc 1422 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1423 {
dce234bc
PP
1424 dwarf2_per_objfile->ranges.asection = sectp;
1425 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1426 }
348e048f
DE
1427 else if (section_is_p (sectp->name, TYPES_SECTION))
1428 {
1429 dwarf2_per_objfile->types.asection = sectp;
1430 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1431 }
9291a0cd
TT
1432 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1433 {
1434 dwarf2_per_objfile->gdb_index.asection = sectp;
1435 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1436 }
dce234bc 1437
72dca2f5
FR
1438 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1439 && bfd_section_vma (abfd, sectp) == 0)
1440 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1441}
1442
dce234bc
PP
1443/* Decompress a section that was compressed using zlib. Store the
1444 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1445
1446static void
dce234bc
PP
1447zlib_decompress_section (struct objfile *objfile, asection *sectp,
1448 gdb_byte **outbuf, bfd_size_type *outsize)
1449{
1450 bfd *abfd = objfile->obfd;
1451#ifndef HAVE_ZLIB_H
1452 error (_("Support for zlib-compressed DWARF data (from '%s') "
1453 "is disabled in this copy of GDB"),
1454 bfd_get_filename (abfd));
1455#else
1456 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1457 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1458 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1459 bfd_size_type uncompressed_size;
1460 gdb_byte *uncompressed_buffer;
1461 z_stream strm;
1462 int rc;
1463 int header_size = 12;
1464
1465 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1466 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1467 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1468 bfd_get_filename (abfd));
1469
1470 /* Read the zlib header. In this case, it should be "ZLIB" followed
1471 by the uncompressed section size, 8 bytes in big-endian order. */
1472 if (compressed_size < header_size
1473 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1474 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1475 bfd_get_filename (abfd));
1476 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1477 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1478 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1479 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1480 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1481 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[11];
1484
1485 /* It is possible the section consists of several compressed
1486 buffers concatenated together, so we uncompress in a loop. */
1487 strm.zalloc = NULL;
1488 strm.zfree = NULL;
1489 strm.opaque = NULL;
1490 strm.avail_in = compressed_size - header_size;
1491 strm.next_in = (Bytef*) compressed_buffer + header_size;
1492 strm.avail_out = uncompressed_size;
1493 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1494 uncompressed_size);
1495 rc = inflateInit (&strm);
1496 while (strm.avail_in > 0)
1497 {
1498 if (rc != Z_OK)
1499 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1500 bfd_get_filename (abfd), rc);
1501 strm.next_out = ((Bytef*) uncompressed_buffer
1502 + (uncompressed_size - strm.avail_out));
1503 rc = inflate (&strm, Z_FINISH);
1504 if (rc != Z_STREAM_END)
1505 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1506 bfd_get_filename (abfd), rc);
1507 rc = inflateReset (&strm);
1508 }
1509 rc = inflateEnd (&strm);
1510 if (rc != Z_OK
1511 || strm.avail_out != 0)
1512 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1513 bfd_get_filename (abfd), rc);
1514
affddf13 1515 do_cleanups (cleanup);
dce234bc
PP
1516 *outbuf = uncompressed_buffer;
1517 *outsize = uncompressed_size;
1518#endif
233a11ab
CS
1519}
1520
dce234bc
PP
1521/* Read the contents of the section SECTP from object file specified by
1522 OBJFILE, store info about the section into INFO.
1523 If the section is compressed, uncompress it before returning. */
c906108c 1524
dce234bc
PP
1525static void
1526dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1527{
dce234bc
PP
1528 bfd *abfd = objfile->obfd;
1529 asection *sectp = info->asection;
1530 gdb_byte *buf, *retbuf;
1531 unsigned char header[4];
c906108c 1532
be391dca
TT
1533 if (info->readin)
1534 return;
dce234bc
PP
1535 info->buffer = NULL;
1536 info->was_mmapped = 0;
be391dca 1537 info->readin = 1;
188dd5d6 1538
dce234bc
PP
1539 if (info->asection == NULL || info->size == 0)
1540 return;
c906108c 1541
dce234bc
PP
1542 /* Check if the file has a 4-byte header indicating compression. */
1543 if (info->size > sizeof (header)
1544 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1545 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1546 {
1547 /* Upon decompression, update the buffer and its size. */
1548 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1549 {
1550 zlib_decompress_section (objfile, sectp, &info->buffer,
1551 &info->size);
1552 return;
1553 }
1554 }
4bdf3d34 1555
dce234bc
PP
1556#ifdef HAVE_MMAP
1557 if (pagesize == 0)
1558 pagesize = getpagesize ();
2e276125 1559
dce234bc
PP
1560 /* Only try to mmap sections which are large enough: we don't want to
1561 waste space due to fragmentation. Also, only try mmap for sections
1562 without relocations. */
1563
1564 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1565 {
1566 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1567 size_t map_length = info->size + sectp->filepos - pg_offset;
1568 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1569 MAP_PRIVATE, pg_offset);
1570
1571 if (retbuf != MAP_FAILED)
1572 {
1573 info->was_mmapped = 1;
1574 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1575#if HAVE_POSIX_MADVISE
1576 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1577#endif
dce234bc
PP
1578 return;
1579 }
1580 }
1581#endif
1582
1583 /* If we get here, we are a normal, not-compressed section. */
1584 info->buffer = buf
1585 = obstack_alloc (&objfile->objfile_obstack, info->size);
1586
1587 /* When debugging .o files, we may need to apply relocations; see
1588 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1589 We never compress sections in .o files, so we only need to
1590 try this when the section is not compressed. */
ac8035ab 1591 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1592 if (retbuf != NULL)
1593 {
1594 info->buffer = retbuf;
1595 return;
1596 }
1597
1598 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1599 || bfd_bread (buf, info->size, abfd) != info->size)
1600 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1601 bfd_get_filename (abfd));
1602}
1603
1604/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1605 SECTION_NAME. */
af34e669 1606
dce234bc
PP
1607void
1608dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1609 asection **sectp, gdb_byte **bufp,
1610 bfd_size_type *sizep)
1611{
1612 struct dwarf2_per_objfile *data
1613 = objfile_data (objfile, dwarf2_objfile_data_key);
1614 struct dwarf2_section_info *info;
a3b2a86b
TT
1615
1616 /* We may see an objfile without any DWARF, in which case we just
1617 return nothing. */
1618 if (data == NULL)
1619 {
1620 *sectp = NULL;
1621 *bufp = NULL;
1622 *sizep = 0;
1623 return;
1624 }
dce234bc
PP
1625 if (section_is_p (section_name, EH_FRAME_SECTION))
1626 info = &data->eh_frame;
1627 else if (section_is_p (section_name, FRAME_SECTION))
1628 info = &data->frame;
0d53c4c4 1629 else
f3574227 1630 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1631
1632 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1633 /* We haven't read this section in yet. Do it now. */
1634 dwarf2_read_section (objfile, info);
1635
1636 *sectp = info->asection;
1637 *bufp = info->buffer;
1638 *sizep = info->size;
1639}
1640
9291a0cd
TT
1641\f
1642
1643/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1644 this CU came. */
2fdf6df6 1645
9291a0cd
TT
1646static void
1647dw2_do_instantiate_symtab (struct objfile *objfile,
1648 struct dwarf2_per_cu_data *per_cu)
1649{
1650 struct cleanup *back_to;
1651
1652 back_to = make_cleanup (dwarf2_release_queue, NULL);
1653
1654 queue_comp_unit (per_cu, objfile);
1655
1656 if (per_cu->from_debug_types)
1657 read_signatured_type_at_offset (objfile, per_cu->offset);
1658 else
1659 load_full_comp_unit (per_cu, objfile);
1660
1661 process_queue (objfile);
1662
1663 /* Age the cache, releasing compilation units that have not
1664 been used recently. */
1665 age_cached_comp_units ();
1666
1667 do_cleanups (back_to);
1668}
1669
1670/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1671 the objfile from which this CU came. Returns the resulting symbol
1672 table. */
2fdf6df6 1673
9291a0cd
TT
1674static struct symtab *
1675dw2_instantiate_symtab (struct objfile *objfile,
1676 struct dwarf2_per_cu_data *per_cu)
1677{
1678 if (!per_cu->v.quick->symtab)
1679 {
1680 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1681 increment_reading_symtab ();
1682 dw2_do_instantiate_symtab (objfile, per_cu);
1683 do_cleanups (back_to);
1684 }
1685 return per_cu->v.quick->symtab;
1686}
1687
1fd400ff 1688/* Return the CU given its index. */
2fdf6df6 1689
1fd400ff
TT
1690static struct dwarf2_per_cu_data *
1691dw2_get_cu (int index)
1692{
1693 if (index >= dwarf2_per_objfile->n_comp_units)
1694 {
1695 index -= dwarf2_per_objfile->n_comp_units;
1696 return dwarf2_per_objfile->type_comp_units[index];
1697 }
1698 return dwarf2_per_objfile->all_comp_units[index];
1699}
1700
9291a0cd
TT
1701/* A helper function that knows how to read a 64-bit value in a way
1702 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1703 otherwise. */
2fdf6df6 1704
9291a0cd
TT
1705static int
1706extract_cu_value (const char *bytes, ULONGEST *result)
1707{
1708 if (sizeof (ULONGEST) < 8)
1709 {
1710 int i;
1711
1712 /* Ignore the upper 4 bytes if they are all zero. */
1713 for (i = 0; i < 4; ++i)
1714 if (bytes[i + 4] != 0)
1715 return 0;
1716
1717 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1718 }
1719 else
1720 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1721 return 1;
1722}
1723
1724/* Read the CU list from the mapped index, and use it to create all
1725 the CU objects for this objfile. Return 0 if something went wrong,
1726 1 if everything went ok. */
2fdf6df6 1727
9291a0cd 1728static int
1fd400ff
TT
1729create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1730 offset_type cu_list_elements)
9291a0cd
TT
1731{
1732 offset_type i;
9291a0cd
TT
1733
1734 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1735 dwarf2_per_objfile->all_comp_units
1736 = obstack_alloc (&objfile->objfile_obstack,
1737 dwarf2_per_objfile->n_comp_units
1738 * sizeof (struct dwarf2_per_cu_data *));
1739
1740 for (i = 0; i < cu_list_elements; i += 2)
1741 {
1742 struct dwarf2_per_cu_data *the_cu;
1743 ULONGEST offset, length;
1744
1745 if (!extract_cu_value (cu_list, &offset)
1746 || !extract_cu_value (cu_list + 8, &length))
1747 return 0;
1748 cu_list += 2 * 8;
1749
1750 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1751 struct dwarf2_per_cu_data);
1752 the_cu->offset = offset;
1753 the_cu->length = length;
1754 the_cu->objfile = objfile;
1755 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1756 struct dwarf2_per_cu_quick_data);
1757 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1758 }
1759
1760 return 1;
1761}
1762
1fd400ff 1763/* Create the signatured type hash table from the index. */
673bfd45 1764
1fd400ff 1765static int
673bfd45
DE
1766create_signatured_type_table_from_index (struct objfile *objfile,
1767 const gdb_byte *bytes,
1768 offset_type elements)
1fd400ff
TT
1769{
1770 offset_type i;
673bfd45 1771 htab_t sig_types_hash;
1fd400ff
TT
1772
1773 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1774 dwarf2_per_objfile->type_comp_units
1775 = obstack_alloc (&objfile->objfile_obstack,
1776 dwarf2_per_objfile->n_type_comp_units
1777 * sizeof (struct dwarf2_per_cu_data *));
1778
673bfd45 1779 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1780
1781 for (i = 0; i < elements; i += 3)
1782 {
1783 struct signatured_type *type_sig;
1784 ULONGEST offset, type_offset, signature;
1785 void **slot;
1786
1787 if (!extract_cu_value (bytes, &offset)
1788 || !extract_cu_value (bytes + 8, &type_offset))
1789 return 0;
1790 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1791 bytes += 3 * 8;
1792
1793 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1794 struct signatured_type);
1795 type_sig->signature = signature;
1796 type_sig->offset = offset;
1797 type_sig->type_offset = type_offset;
1798 type_sig->per_cu.from_debug_types = 1;
1799 type_sig->per_cu.offset = offset;
1800 type_sig->per_cu.objfile = objfile;
1801 type_sig->per_cu.v.quick
1802 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1803 struct dwarf2_per_cu_quick_data);
1804
673bfd45 1805 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1806 *slot = type_sig;
1807
1808 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1809 }
1810
673bfd45 1811 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1812
1813 return 1;
1814}
1815
9291a0cd
TT
1816/* Read the address map data from the mapped index, and use it to
1817 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1818
9291a0cd
TT
1819static void
1820create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1821{
1822 const gdb_byte *iter, *end;
1823 struct obstack temp_obstack;
1824 struct addrmap *mutable_map;
1825 struct cleanup *cleanup;
1826 CORE_ADDR baseaddr;
1827
1828 obstack_init (&temp_obstack);
1829 cleanup = make_cleanup_obstack_free (&temp_obstack);
1830 mutable_map = addrmap_create_mutable (&temp_obstack);
1831
1832 iter = index->address_table;
1833 end = iter + index->address_table_size;
1834
1835 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1836
1837 while (iter < end)
1838 {
1839 ULONGEST hi, lo, cu_index;
1840 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1841 iter += 8;
1842 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1843 iter += 8;
1844 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1845 iter += 4;
1846
1847 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1848 dw2_get_cu (cu_index));
9291a0cd
TT
1849 }
1850
1851 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1852 &objfile->objfile_obstack);
1853 do_cleanups (cleanup);
1854}
1855
1856/* The hash function for strings in the mapped index. This is the
1857 same as the hashtab.c hash function, but we keep a separate copy to
1858 maintain control over the implementation. This is necessary
1859 because the hash function is tied to the format of the mapped index
1860 file. */
2fdf6df6 1861
9291a0cd
TT
1862static hashval_t
1863mapped_index_string_hash (const void *p)
1864{
1865 const unsigned char *str = (const unsigned char *) p;
1866 hashval_t r = 0;
1867 unsigned char c;
1868
1869 while ((c = *str++) != 0)
1870 r = r * 67 + c - 113;
1871
1872 return r;
1873}
1874
1875/* Find a slot in the mapped index INDEX for the object named NAME.
1876 If NAME is found, set *VEC_OUT to point to the CU vector in the
1877 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1878
9291a0cd
TT
1879static int
1880find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1881 offset_type **vec_out)
1882{
1883 offset_type hash = mapped_index_string_hash (name);
1884 offset_type slot, step;
1885
3876f04e
DE
1886 slot = hash & (index->symbol_table_slots - 1);
1887 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1888
1889 for (;;)
1890 {
1891 /* Convert a slot number to an offset into the table. */
1892 offset_type i = 2 * slot;
1893 const char *str;
3876f04e 1894 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1895 return 0;
1896
3876f04e 1897 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1898 if (!strcmp (name, str))
1899 {
1900 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1901 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1902 return 1;
1903 }
1904
3876f04e 1905 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1906 }
1907}
1908
1909/* Read the index file. If everything went ok, initialize the "quick"
1910 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1911
9291a0cd
TT
1912static int
1913dwarf2_read_index (struct objfile *objfile)
1914{
9291a0cd
TT
1915 char *addr;
1916 struct mapped_index *map;
b3b272e1 1917 offset_type *metadata;
ac0b195c
KW
1918 const gdb_byte *cu_list;
1919 const gdb_byte *types_list = NULL;
1920 offset_type version, cu_list_elements;
1921 offset_type types_list_elements = 0;
1fd400ff 1922 int i;
9291a0cd
TT
1923
1924 if (dwarf2_per_objfile->gdb_index.asection == NULL
1925 || dwarf2_per_objfile->gdb_index.size == 0)
1926 return 0;
82430852
JK
1927
1928 /* Older elfutils strip versions could keep the section in the main
1929 executable while splitting it for the separate debug info file. */
1930 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1931 & SEC_HAS_CONTENTS) == 0)
1932 return 0;
1933
9291a0cd
TT
1934 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1935
1936 addr = dwarf2_per_objfile->gdb_index.buffer;
1937 /* Version check. */
1fd400ff 1938 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
1939 /* Versions earlier than 3 emitted every copy of a psymbol. This
1940 causes the index to behave very poorly for certain requests. So,
1941 it seems better to just ignore such indices. */
1942 if (version < 3)
9291a0cd
TT
1943 return 0;
1944
1945 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1946 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1947
1948 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1949
1950 i = 0;
1951 cu_list = addr + MAYBE_SWAP (metadata[i]);
1952 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1953 / 8);
1fd400ff
TT
1954 ++i;
1955
987d643c
TT
1956 types_list = addr + MAYBE_SWAP (metadata[i]);
1957 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1958 - MAYBE_SWAP (metadata[i]))
1959 / 8);
1960 ++i;
1fd400ff
TT
1961
1962 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1963 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1964 - MAYBE_SWAP (metadata[i]));
1965 ++i;
1966
3876f04e
DE
1967 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1968 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1969 - MAYBE_SWAP (metadata[i]))
1970 / (2 * sizeof (offset_type)));
1fd400ff 1971 ++i;
9291a0cd 1972
1fd400ff
TT
1973 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1974
1975 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1976 return 0;
1977
987d643c 1978 if (types_list_elements
673bfd45
DE
1979 && !create_signatured_type_table_from_index (objfile, types_list,
1980 types_list_elements))
9291a0cd
TT
1981 return 0;
1982
1983 create_addrmap_from_index (objfile, map);
1984
1985 dwarf2_per_objfile->index_table = map;
1986 dwarf2_per_objfile->using_index = 1;
1987
1988 return 1;
1989}
1990
1991/* A helper for the "quick" functions which sets the global
1992 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 1993
9291a0cd
TT
1994static void
1995dw2_setup (struct objfile *objfile)
1996{
1997 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1998 gdb_assert (dwarf2_per_objfile);
1999}
2000
2001/* A helper for the "quick" functions which attempts to read the line
2002 table for THIS_CU. */
2fdf6df6 2003
9291a0cd
TT
2004static void
2005dw2_require_line_header (struct objfile *objfile,
2006 struct dwarf2_per_cu_data *this_cu)
2007{
2008 bfd *abfd = objfile->obfd;
2009 struct line_header *lh = NULL;
2010 struct attribute *attr;
2011 struct cleanup *cleanups;
2012 struct die_info *comp_unit_die;
36374493 2013 struct dwarf2_section_info* sec;
9291a0cd
TT
2014 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2015 int has_children, i;
2016 struct dwarf2_cu cu;
2017 unsigned int bytes_read, buffer_size;
2018 struct die_reader_specs reader_specs;
2019 char *name, *comp_dir;
2020
2021 if (this_cu->v.quick->read_lines)
2022 return;
2023 this_cu->v.quick->read_lines = 1;
2024
9816fde3 2025 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2026 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2027
36374493
DE
2028 if (this_cu->from_debug_types)
2029 sec = &dwarf2_per_objfile->types;
2030 else
2031 sec = &dwarf2_per_objfile->info;
2032 dwarf2_read_section (objfile, sec);
2033 buffer_size = sec->size;
2034 buffer = sec->buffer;
9291a0cd
TT
2035 info_ptr = buffer + this_cu->offset;
2036 beg_of_comp_unit = info_ptr;
2037
2038 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2039 buffer, buffer_size,
2040 abfd);
2041
2042 /* Complete the cu_header. */
2043 cu.header.offset = beg_of_comp_unit - buffer;
2044 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2045
2046 this_cu->cu = &cu;
2047 cu.per_cu = this_cu;
2048
2049 dwarf2_read_abbrevs (abfd, &cu);
2050 make_cleanup (dwarf2_free_abbrev_table, &cu);
2051
2052 if (this_cu->from_debug_types)
2053 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2054 init_cu_die_reader (&reader_specs, &cu);
2055 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2056 &has_children);
2057
2058 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2059 if (attr)
2060 {
2061 unsigned int line_offset = DW_UNSND (attr);
2062 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2063 }
2064 if (lh == NULL)
2065 {
2066 do_cleanups (cleanups);
2067 return;
2068 }
2069
2070 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2071
2072 this_cu->v.quick->lines = lh;
2073
2074 this_cu->v.quick->file_names
2075 = obstack_alloc (&objfile->objfile_obstack,
2076 lh->num_file_names * sizeof (char *));
2077 for (i = 0; i < lh->num_file_names; ++i)
2078 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2079
2080 do_cleanups (cleanups);
2081}
2082
2083/* A helper for the "quick" functions which computes and caches the
2084 real path for a given file name from the line table.
2085 dw2_require_line_header must have been called before this is
2086 invoked. */
2fdf6df6 2087
9291a0cd
TT
2088static const char *
2089dw2_require_full_path (struct objfile *objfile,
e254ef6a 2090 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2091 int index)
2092{
e254ef6a
DE
2093 if (!per_cu->v.quick->full_names)
2094 per_cu->v.quick->full_names
9291a0cd 2095 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2096 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2097 sizeof (char *));
2098
e254ef6a
DE
2099 if (!per_cu->v.quick->full_names[index])
2100 per_cu->v.quick->full_names[index]
2101 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2102
e254ef6a 2103 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2104}
2105
2106static struct symtab *
2107dw2_find_last_source_symtab (struct objfile *objfile)
2108{
2109 int index;
2110 dw2_setup (objfile);
2111 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2112 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2113}
2114
2115static void
2116dw2_forget_cached_source_info (struct objfile *objfile)
2117{
2118 int i;
2119
2120 dw2_setup (objfile);
1fd400ff
TT
2121 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2122 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2123 {
e254ef6a 2124 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2125
e254ef6a 2126 if (per_cu->v.quick->full_names)
9291a0cd
TT
2127 {
2128 int j;
2129
e254ef6a 2130 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
87df528e
JK
2131 {
2132 xfree ((void *) per_cu->v.quick->full_names[j]);
2133 per_cu->v.quick->full_names[j] = NULL;
2134 }
9291a0cd
TT
2135 }
2136 }
2137}
2138
2139static int
2140dw2_lookup_symtab (struct objfile *objfile, const char *name,
2141 const char *full_path, const char *real_path,
2142 struct symtab **result)
2143{
2144 int i;
2145 int check_basename = lbasename (name) == name;
2146 struct dwarf2_per_cu_data *base_cu = NULL;
2147
2148 dw2_setup (objfile);
1fd400ff
TT
2149 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2150 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2151 {
2152 int j;
e254ef6a 2153 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2154
e254ef6a 2155 if (per_cu->v.quick->symtab)
9291a0cd
TT
2156 continue;
2157
e254ef6a
DE
2158 dw2_require_line_header (objfile, per_cu);
2159 if (!per_cu->v.quick->lines)
9291a0cd
TT
2160 continue;
2161
e254ef6a 2162 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2163 {
e254ef6a 2164 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2165
2166 if (FILENAME_CMP (name, this_name) == 0)
2167 {
e254ef6a 2168 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2169 return 1;
2170 }
2171
2172 if (check_basename && ! base_cu
2173 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2174 base_cu = per_cu;
9291a0cd
TT
2175
2176 if (full_path != NULL)
2177 {
2178 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2179 per_cu, j);
9291a0cd
TT
2180
2181 if (this_full_name
2182 && FILENAME_CMP (full_path, this_full_name) == 0)
2183 {
e254ef6a 2184 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2185 return 1;
2186 }
2187 }
2188
2189 if (real_path != NULL)
2190 {
2191 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2192 per_cu, j);
9291a0cd
TT
2193
2194 if (this_full_name != NULL)
2195 {
2196 char *rp = gdb_realpath (this_full_name);
2197 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2198 {
2199 xfree (rp);
e254ef6a 2200 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2201 return 1;
2202 }
2203 xfree (rp);
2204 }
2205 }
2206 }
2207 }
2208
2209 if (base_cu)
2210 {
2211 *result = dw2_instantiate_symtab (objfile, base_cu);
2212 return 1;
2213 }
2214
2215 return 0;
2216}
2217
2218static struct symtab *
2219dw2_lookup_symbol (struct objfile *objfile, int block_index,
2220 const char *name, domain_enum domain)
2221{
774b6a14 2222 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2223 instead. */
2224 return NULL;
2225}
2226
2227/* A helper function that expands all symtabs that hold an object
2228 named NAME. */
2fdf6df6 2229
9291a0cd
TT
2230static void
2231dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2232{
2233 dw2_setup (objfile);
2234
2235 if (dwarf2_per_objfile->index_table)
2236 {
2237 offset_type *vec;
2238
2239 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2240 name, &vec))
2241 {
2242 offset_type i, len = MAYBE_SWAP (*vec);
2243 for (i = 0; i < len; ++i)
2244 {
2245 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2246 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2247
e254ef6a 2248 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2249 }
2250 }
2251 }
2252}
2253
774b6a14
TT
2254static void
2255dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2256 int kind, const char *name,
2257 domain_enum domain)
9291a0cd 2258{
774b6a14 2259 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2260}
2261
2262static void
2263dw2_print_stats (struct objfile *objfile)
2264{
2265 int i, count;
2266
2267 dw2_setup (objfile);
2268 count = 0;
1fd400ff
TT
2269 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2270 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2271 {
e254ef6a 2272 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2273
e254ef6a 2274 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2275 ++count;
2276 }
2277 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2278}
2279
2280static void
2281dw2_dump (struct objfile *objfile)
2282{
2283 /* Nothing worth printing. */
2284}
2285
2286static void
2287dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2288 struct section_offsets *delta)
2289{
2290 /* There's nothing to relocate here. */
2291}
2292
2293static void
2294dw2_expand_symtabs_for_function (struct objfile *objfile,
2295 const char *func_name)
2296{
2297 dw2_do_expand_symtabs_matching (objfile, func_name);
2298}
2299
2300static void
2301dw2_expand_all_symtabs (struct objfile *objfile)
2302{
2303 int i;
2304
2305 dw2_setup (objfile);
1fd400ff
TT
2306
2307 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2308 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2309 {
e254ef6a 2310 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2311
e254ef6a 2312 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2313 }
2314}
2315
2316static void
2317dw2_expand_symtabs_with_filename (struct objfile *objfile,
2318 const char *filename)
2319{
2320 int i;
2321
2322 dw2_setup (objfile);
1fd400ff
TT
2323 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2324 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2325 {
2326 int j;
e254ef6a 2327 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2328
e254ef6a 2329 if (per_cu->v.quick->symtab)
9291a0cd
TT
2330 continue;
2331
e254ef6a
DE
2332 dw2_require_line_header (objfile, per_cu);
2333 if (!per_cu->v.quick->lines)
9291a0cd
TT
2334 continue;
2335
e254ef6a 2336 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2337 {
e254ef6a 2338 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2339 if (strcmp (this_name, filename) == 0)
2340 {
e254ef6a 2341 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2342 break;
2343 }
2344 }
2345 }
2346}
2347
dd786858 2348static const char *
9291a0cd
TT
2349dw2_find_symbol_file (struct objfile *objfile, const char *name)
2350{
e254ef6a 2351 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2352 offset_type *vec;
2353
2354 dw2_setup (objfile);
2355
2356 if (!dwarf2_per_objfile->index_table)
2357 return NULL;
2358
2359 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2360 name, &vec))
2361 return NULL;
2362
2363 /* Note that this just looks at the very first one named NAME -- but
2364 actually we are looking for a function. find_main_filename
2365 should be rewritten so that it doesn't require a custom hook. It
2366 could just use the ordinary symbol tables. */
2367 /* vec[0] is the length, which must always be >0. */
e254ef6a 2368 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2369
e254ef6a
DE
2370 dw2_require_line_header (objfile, per_cu);
2371 if (!per_cu->v.quick->lines)
9291a0cd
TT
2372 return NULL;
2373
e254ef6a 2374 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2375}
2376
2377static void
40658b94
PH
2378dw2_map_matching_symbols (const char * name, domain_enum namespace,
2379 struct objfile *objfile, int global,
2380 int (*callback) (struct block *,
2381 struct symbol *, void *),
2382 void *data,
2383 int (*match) (const char *, const char *),
2384 int (*ordered_compare) (const char *,
2385 const char *))
9291a0cd 2386{
40658b94 2387 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2388 current language is Ada for a non-Ada objfile using GNU index. As Ada
2389 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2390}
2391
2392static void
2393dw2_expand_symtabs_matching (struct objfile *objfile,
2394 int (*file_matcher) (const char *, void *),
2395 int (*name_matcher) (const char *, void *),
2396 domain_enum kind,
2397 void *data)
2398{
2399 int i;
2400 offset_type iter;
4b5246aa 2401 struct mapped_index *index;
9291a0cd
TT
2402
2403 dw2_setup (objfile);
2404 if (!dwarf2_per_objfile->index_table)
2405 return;
4b5246aa 2406 index = dwarf2_per_objfile->index_table;
9291a0cd 2407
1fd400ff
TT
2408 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2409 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2410 {
2411 int j;
e254ef6a 2412 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2413
e254ef6a
DE
2414 per_cu->v.quick->mark = 0;
2415 if (per_cu->v.quick->symtab)
9291a0cd
TT
2416 continue;
2417
e254ef6a
DE
2418 dw2_require_line_header (objfile, per_cu);
2419 if (!per_cu->v.quick->lines)
9291a0cd
TT
2420 continue;
2421
e254ef6a 2422 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2423 {
e254ef6a 2424 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2425 {
e254ef6a 2426 per_cu->v.quick->mark = 1;
9291a0cd
TT
2427 break;
2428 }
2429 }
2430 }
2431
3876f04e 2432 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2433 {
2434 offset_type idx = 2 * iter;
2435 const char *name;
2436 offset_type *vec, vec_len, vec_idx;
2437
3876f04e 2438 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2439 continue;
2440
3876f04e 2441 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2442
2443 if (! (*name_matcher) (name, data))
2444 continue;
2445
2446 /* The name was matched, now expand corresponding CUs that were
2447 marked. */
4b5246aa 2448 vec = (offset_type *) (index->constant_pool
3876f04e 2449 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2450 vec_len = MAYBE_SWAP (vec[0]);
2451 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2452 {
e254ef6a 2453 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2454
e254ef6a
DE
2455 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2456 if (per_cu->v.quick->mark)
2457 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2458 }
2459 }
2460}
2461
2462static struct symtab *
2463dw2_find_pc_sect_symtab (struct objfile *objfile,
2464 struct minimal_symbol *msymbol,
2465 CORE_ADDR pc,
2466 struct obj_section *section,
2467 int warn_if_readin)
2468{
2469 struct dwarf2_per_cu_data *data;
2470
2471 dw2_setup (objfile);
2472
2473 if (!objfile->psymtabs_addrmap)
2474 return NULL;
2475
2476 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2477 if (!data)
2478 return NULL;
2479
2480 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2481 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2482 paddress (get_objfile_arch (objfile), pc));
2483
2484 return dw2_instantiate_symtab (objfile, data);
2485}
2486
2487static void
2488dw2_map_symbol_names (struct objfile *objfile,
2489 void (*fun) (const char *, void *),
2490 void *data)
2491{
2492 offset_type iter;
4b5246aa
TT
2493 struct mapped_index *index;
2494
9291a0cd
TT
2495 dw2_setup (objfile);
2496
2497 if (!dwarf2_per_objfile->index_table)
2498 return;
4b5246aa 2499 index = dwarf2_per_objfile->index_table;
9291a0cd 2500
3876f04e 2501 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2502 {
2503 offset_type idx = 2 * iter;
2504 const char *name;
2505 offset_type *vec, vec_len, vec_idx;
2506
3876f04e 2507 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2508 continue;
2509
3876f04e 2510 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2511
2512 (*fun) (name, data);
2513 }
2514}
2515
2516static void
2517dw2_map_symbol_filenames (struct objfile *objfile,
2518 void (*fun) (const char *, const char *, void *),
2519 void *data)
2520{
2521 int i;
2522
2523 dw2_setup (objfile);
1fd400ff
TT
2524 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2525 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2526 {
2527 int j;
e254ef6a 2528 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2529
e254ef6a 2530 if (per_cu->v.quick->symtab)
9291a0cd
TT
2531 continue;
2532
e254ef6a
DE
2533 dw2_require_line_header (objfile, per_cu);
2534 if (!per_cu->v.quick->lines)
9291a0cd
TT
2535 continue;
2536
e254ef6a 2537 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2538 {
e254ef6a
DE
2539 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2540 j);
2541 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2542 }
2543 }
2544}
2545
2546static int
2547dw2_has_symbols (struct objfile *objfile)
2548{
2549 return 1;
2550}
2551
2552const struct quick_symbol_functions dwarf2_gdb_index_functions =
2553{
2554 dw2_has_symbols,
2555 dw2_find_last_source_symtab,
2556 dw2_forget_cached_source_info,
2557 dw2_lookup_symtab,
2558 dw2_lookup_symbol,
774b6a14 2559 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2560 dw2_print_stats,
2561 dw2_dump,
2562 dw2_relocate,
2563 dw2_expand_symtabs_for_function,
2564 dw2_expand_all_symtabs,
2565 dw2_expand_symtabs_with_filename,
2566 dw2_find_symbol_file,
40658b94 2567 dw2_map_matching_symbols,
9291a0cd
TT
2568 dw2_expand_symtabs_matching,
2569 dw2_find_pc_sect_symtab,
2570 dw2_map_symbol_names,
2571 dw2_map_symbol_filenames
2572};
2573
2574/* Initialize for reading DWARF for this objfile. Return 0 if this
2575 file will use psymtabs, or 1 if using the GNU index. */
2576
2577int
2578dwarf2_initialize_objfile (struct objfile *objfile)
2579{
2580 /* If we're about to read full symbols, don't bother with the
2581 indices. In this case we also don't care if some other debug
2582 format is making psymtabs, because they are all about to be
2583 expanded anyway. */
2584 if ((objfile->flags & OBJF_READNOW))
2585 {
2586 int i;
2587
2588 dwarf2_per_objfile->using_index = 1;
2589 create_all_comp_units (objfile);
1fd400ff 2590 create_debug_types_hash_table (objfile);
9291a0cd 2591
1fd400ff
TT
2592 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2593 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2594 {
e254ef6a 2595 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2596
e254ef6a
DE
2597 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2598 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2599 }
2600
2601 /* Return 1 so that gdb sees the "quick" functions. However,
2602 these functions will be no-ops because we will have expanded
2603 all symtabs. */
2604 return 1;
2605 }
2606
2607 if (dwarf2_read_index (objfile))
2608 return 1;
2609
2610 dwarf2_build_psymtabs (objfile);
2611 return 0;
2612}
2613
2614\f
2615
dce234bc
PP
2616/* Build a partial symbol table. */
2617
2618void
f29dff0a 2619dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2620{
f29dff0a 2621 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2622 {
2623 init_psymbol_list (objfile, 1024);
2624 }
2625
d146bf1e 2626 dwarf2_build_psymtabs_hard (objfile);
c906108c 2627}
c906108c 2628
45452591
DE
2629/* Return TRUE if OFFSET is within CU_HEADER. */
2630
2631static inline int
2632offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2633{
2634 unsigned int bottom = cu_header->offset;
2635 unsigned int top = (cu_header->offset
2636 + cu_header->length
2637 + cu_header->initial_length_size);
9a619af0 2638
45452591
DE
2639 return (offset >= bottom && offset < top);
2640}
2641
93311388
DE
2642/* Read in the comp unit header information from the debug_info at info_ptr.
2643 NOTE: This leaves members offset, first_die_offset to be filled in
2644 by the caller. */
107d2387 2645
fe1b8b76 2646static gdb_byte *
107d2387 2647read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2648 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2649{
2650 int signed_addr;
891d2f0b 2651 unsigned int bytes_read;
c764a876
DE
2652
2653 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2654 cu_header->initial_length_size = bytes_read;
2655 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2656 info_ptr += bytes_read;
107d2387
AC
2657 cu_header->version = read_2_bytes (abfd, info_ptr);
2658 info_ptr += 2;
613e1657 2659 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2660 &bytes_read);
613e1657 2661 info_ptr += bytes_read;
107d2387
AC
2662 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2663 info_ptr += 1;
2664 signed_addr = bfd_get_sign_extend_vma (abfd);
2665 if (signed_addr < 0)
8e65ff28 2666 internal_error (__FILE__, __LINE__,
e2e0b3e5 2667 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2668 cu_header->signed_addr_p = signed_addr;
c764a876 2669
107d2387
AC
2670 return info_ptr;
2671}
2672
fe1b8b76
JB
2673static gdb_byte *
2674partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2675 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2676 bfd *abfd)
2677{
fe1b8b76 2678 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2679
2680 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2681
2dc7f7b3 2682 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2683 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2684 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2685 bfd_get_filename (abfd));
72bf9492 2686
dce234bc 2687 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2688 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2689 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2690 (long) header->abbrev_offset,
93311388 2691 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2692 bfd_get_filename (abfd));
2693
2694 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2695 > buffer + buffer_size)
8a3fe4f8
AC
2696 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2697 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2698 (long) header->length,
93311388 2699 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2700 bfd_get_filename (abfd));
2701
2702 return info_ptr;
2703}
2704
348e048f
DE
2705/* Read in the types comp unit header information from .debug_types entry at
2706 types_ptr. The result is a pointer to one past the end of the header. */
2707
2708static gdb_byte *
2709read_type_comp_unit_head (struct comp_unit_head *cu_header,
2710 ULONGEST *signature,
2711 gdb_byte *types_ptr, bfd *abfd)
2712{
348e048f
DE
2713 gdb_byte *initial_types_ptr = types_ptr;
2714
6e70227d 2715 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2716 &dwarf2_per_objfile->types);
348e048f
DE
2717 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2718
2719 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2720
2721 *signature = read_8_bytes (abfd, types_ptr);
2722 types_ptr += 8;
2723 types_ptr += cu_header->offset_size;
2724 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2725
2726 return types_ptr;
2727}
2728
aaa75496
JB
2729/* Allocate a new partial symtab for file named NAME and mark this new
2730 partial symtab as being an include of PST. */
2731
2732static void
2733dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2734 struct objfile *objfile)
2735{
2736 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2737
2738 subpst->section_offsets = pst->section_offsets;
2739 subpst->textlow = 0;
2740 subpst->texthigh = 0;
2741
2742 subpst->dependencies = (struct partial_symtab **)
2743 obstack_alloc (&objfile->objfile_obstack,
2744 sizeof (struct partial_symtab *));
2745 subpst->dependencies[0] = pst;
2746 subpst->number_of_dependencies = 1;
2747
2748 subpst->globals_offset = 0;
2749 subpst->n_global_syms = 0;
2750 subpst->statics_offset = 0;
2751 subpst->n_static_syms = 0;
2752 subpst->symtab = NULL;
2753 subpst->read_symtab = pst->read_symtab;
2754 subpst->readin = 0;
2755
2756 /* No private part is necessary for include psymtabs. This property
2757 can be used to differentiate between such include psymtabs and
10b3939b 2758 the regular ones. */
58a9656e 2759 subpst->read_symtab_private = NULL;
aaa75496
JB
2760}
2761
2762/* Read the Line Number Program data and extract the list of files
2763 included by the source file represented by PST. Build an include
d85a05f0 2764 partial symtab for each of these included files. */
aaa75496
JB
2765
2766static void
2767dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2768 struct die_info *die,
aaa75496
JB
2769 struct partial_symtab *pst)
2770{
2771 struct objfile *objfile = cu->objfile;
2772 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2773 struct line_header *lh = NULL;
2774 struct attribute *attr;
aaa75496 2775
d85a05f0
DJ
2776 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2777 if (attr)
2778 {
2779 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2780
d85a05f0
DJ
2781 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2782 }
aaa75496
JB
2783 if (lh == NULL)
2784 return; /* No linetable, so no includes. */
2785
c6da4cef
DE
2786 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2787 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2788
2789 free_line_header (lh);
2790}
2791
348e048f
DE
2792static hashval_t
2793hash_type_signature (const void *item)
2794{
2795 const struct signatured_type *type_sig = item;
9a619af0 2796
348e048f
DE
2797 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2798 return type_sig->signature;
2799}
2800
2801static int
2802eq_type_signature (const void *item_lhs, const void *item_rhs)
2803{
2804 const struct signatured_type *lhs = item_lhs;
2805 const struct signatured_type *rhs = item_rhs;
9a619af0 2806
348e048f
DE
2807 return lhs->signature == rhs->signature;
2808}
2809
1fd400ff
TT
2810/* Allocate a hash table for signatured types. */
2811
2812static htab_t
673bfd45 2813allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2814{
2815 return htab_create_alloc_ex (41,
2816 hash_type_signature,
2817 eq_type_signature,
2818 NULL,
2819 &objfile->objfile_obstack,
2820 hashtab_obstack_allocate,
2821 dummy_obstack_deallocate);
2822}
2823
2824/* A helper function to add a signatured type CU to a list. */
2825
2826static int
2827add_signatured_type_cu_to_list (void **slot, void *datum)
2828{
2829 struct signatured_type *sigt = *slot;
2830 struct dwarf2_per_cu_data ***datap = datum;
2831
2832 **datap = &sigt->per_cu;
2833 ++*datap;
2834
2835 return 1;
2836}
2837
348e048f
DE
2838/* Create the hash table of all entries in the .debug_types section.
2839 The result is zero if there is an error (e.g. missing .debug_types section),
2840 otherwise non-zero. */
2841
2842static int
2843create_debug_types_hash_table (struct objfile *objfile)
2844{
be391dca 2845 gdb_byte *info_ptr;
348e048f 2846 htab_t types_htab;
1fd400ff 2847 struct dwarf2_per_cu_data **iter;
348e048f 2848
be391dca
TT
2849 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2850 info_ptr = dwarf2_per_objfile->types.buffer;
2851
348e048f
DE
2852 if (info_ptr == NULL)
2853 {
2854 dwarf2_per_objfile->signatured_types = NULL;
2855 return 0;
2856 }
2857
673bfd45 2858 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2859
2860 if (dwarf2_die_debug)
2861 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2862
2863 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2864 {
2865 unsigned int offset;
2866 unsigned int offset_size;
2867 unsigned int type_offset;
2868 unsigned int length, initial_length_size;
2869 unsigned short version;
2870 ULONGEST signature;
2871 struct signatured_type *type_sig;
2872 void **slot;
2873 gdb_byte *ptr = info_ptr;
2874
2875 offset = ptr - dwarf2_per_objfile->types.buffer;
2876
2877 /* We need to read the type's signature in order to build the hash
2878 table, but we don't need to read anything else just yet. */
2879
2880 /* Sanity check to ensure entire cu is present. */
2881 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2882 if (ptr + length + initial_length_size
2883 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2884 {
2885 complaint (&symfile_complaints,
2886 _("debug type entry runs off end of `.debug_types' section, ignored"));
2887 break;
2888 }
2889
2890 offset_size = initial_length_size == 4 ? 4 : 8;
2891 ptr += initial_length_size;
2892 version = bfd_get_16 (objfile->obfd, ptr);
2893 ptr += 2;
2894 ptr += offset_size; /* abbrev offset */
2895 ptr += 1; /* address size */
2896 signature = bfd_get_64 (objfile->obfd, ptr);
2897 ptr += 8;
2898 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2899
2900 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2901 memset (type_sig, 0, sizeof (*type_sig));
2902 type_sig->signature = signature;
2903 type_sig->offset = offset;
2904 type_sig->type_offset = type_offset;
ca1f3406 2905 type_sig->per_cu.objfile = objfile;
1fd400ff 2906 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2907
2908 slot = htab_find_slot (types_htab, type_sig, INSERT);
2909 gdb_assert (slot != NULL);
2910 *slot = type_sig;
2911
2912 if (dwarf2_die_debug)
2913 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2914 offset, phex (signature, sizeof (signature)));
2915
2916 info_ptr = info_ptr + initial_length_size + length;
2917 }
2918
2919 dwarf2_per_objfile->signatured_types = types_htab;
2920
1fd400ff
TT
2921 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2922 dwarf2_per_objfile->type_comp_units
2923 = obstack_alloc (&objfile->objfile_obstack,
2924 dwarf2_per_objfile->n_type_comp_units
2925 * sizeof (struct dwarf2_per_cu_data *));
2926 iter = &dwarf2_per_objfile->type_comp_units[0];
2927 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2928 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2929 == dwarf2_per_objfile->n_type_comp_units);
2930
348e048f
DE
2931 return 1;
2932}
2933
2934/* Lookup a signature based type.
2935 Returns NULL if SIG is not present in the table. */
2936
2937static struct signatured_type *
2938lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2939{
2940 struct signatured_type find_entry, *entry;
2941
2942 if (dwarf2_per_objfile->signatured_types == NULL)
2943 {
2944 complaint (&symfile_complaints,
2945 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2946 return 0;
2947 }
2948
2949 find_entry.signature = sig;
2950 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2951 return entry;
2952}
2953
d85a05f0
DJ
2954/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2955
2956static void
2957init_cu_die_reader (struct die_reader_specs *reader,
2958 struct dwarf2_cu *cu)
2959{
2960 reader->abfd = cu->objfile->obfd;
2961 reader->cu = cu;
2962 if (cu->per_cu->from_debug_types)
be391dca
TT
2963 {
2964 gdb_assert (dwarf2_per_objfile->types.readin);
2965 reader->buffer = dwarf2_per_objfile->types.buffer;
2966 }
d85a05f0 2967 else
be391dca
TT
2968 {
2969 gdb_assert (dwarf2_per_objfile->info.readin);
2970 reader->buffer = dwarf2_per_objfile->info.buffer;
2971 }
d85a05f0
DJ
2972}
2973
2974/* Find the base address of the compilation unit for range lists and
2975 location lists. It will normally be specified by DW_AT_low_pc.
2976 In DWARF-3 draft 4, the base address could be overridden by
2977 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2978 compilation units with discontinuous ranges. */
2979
2980static void
2981dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2982{
2983 struct attribute *attr;
2984
2985 cu->base_known = 0;
2986 cu->base_address = 0;
2987
2988 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2989 if (attr)
2990 {
2991 cu->base_address = DW_ADDR (attr);
2992 cu->base_known = 1;
2993 }
2994 else
2995 {
2996 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2997 if (attr)
2998 {
2999 cu->base_address = DW_ADDR (attr);
3000 cu->base_known = 1;
3001 }
3002 }
3003}
3004
348e048f
DE
3005/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3006 to combine the common parts.
93311388 3007 Process a compilation unit for a psymtab.
348e048f
DE
3008 BUFFER is a pointer to the beginning of the dwarf section buffer,
3009 either .debug_info or debug_types.
93311388
DE
3010 INFO_PTR is a pointer to the start of the CU.
3011 Returns a pointer to the next CU. */
aaa75496 3012
93311388
DE
3013static gdb_byte *
3014process_psymtab_comp_unit (struct objfile *objfile,
3015 struct dwarf2_per_cu_data *this_cu,
3016 gdb_byte *buffer, gdb_byte *info_ptr,
3017 unsigned int buffer_size)
c906108c 3018{
c906108c 3019 bfd *abfd = objfile->obfd;
93311388 3020 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3021 struct die_info *comp_unit_die;
c906108c 3022 struct partial_symtab *pst;
5734ee8b 3023 CORE_ADDR baseaddr;
93311388
DE
3024 struct cleanup *back_to_inner;
3025 struct dwarf2_cu cu;
d85a05f0
DJ
3026 int has_children, has_pc_info;
3027 struct attribute *attr;
d85a05f0
DJ
3028 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3029 struct die_reader_specs reader_specs;
c906108c 3030
9816fde3 3031 init_one_comp_unit (&cu, objfile);
93311388 3032 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3033
93311388
DE
3034 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3035 buffer, buffer_size,
3036 abfd);
10b3939b 3037
93311388
DE
3038 /* Complete the cu_header. */
3039 cu.header.offset = beg_of_comp_unit - buffer;
3040 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3041
93311388 3042 cu.list_in_scope = &file_symbols;
af703f96 3043
328c9494
DJ
3044 /* If this compilation unit was already read in, free the
3045 cached copy in order to read it in again. This is
3046 necessary because we skipped some symbols when we first
3047 read in the compilation unit (see load_partial_dies).
3048 This problem could be avoided, but the benefit is
3049 unclear. */
3050 if (this_cu->cu != NULL)
3051 free_one_cached_comp_unit (this_cu->cu);
3052
3053 /* Note that this is a pointer to our stack frame, being
3054 added to a global data structure. It will be cleaned up
3055 in free_stack_comp_unit when we finish with this
3056 compilation unit. */
3057 this_cu->cu = &cu;
d85a05f0
DJ
3058 cu.per_cu = this_cu;
3059
93311388
DE
3060 /* Read the abbrevs for this compilation unit into a table. */
3061 dwarf2_read_abbrevs (abfd, &cu);
3062 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3063
93311388 3064 /* Read the compilation unit die. */
348e048f
DE
3065 if (this_cu->from_debug_types)
3066 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3067 init_cu_die_reader (&reader_specs, &cu);
3068 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3069 &has_children);
93311388 3070
348e048f
DE
3071 if (this_cu->from_debug_types)
3072 {
3073 /* offset,length haven't been set yet for type units. */
3074 this_cu->offset = cu.header.offset;
3075 this_cu->length = cu.header.length + cu.header.initial_length_size;
3076 }
d85a05f0 3077 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3078 {
93311388
DE
3079 info_ptr = (beg_of_comp_unit + cu.header.length
3080 + cu.header.initial_length_size);
3081 do_cleanups (back_to_inner);
3082 return info_ptr;
3083 }
72bf9492 3084
9816fde3 3085 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3086
93311388 3087 /* Allocate a new partial symbol table structure. */
d85a05f0 3088 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3089 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3090 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3091 /* TEXTLOW and TEXTHIGH are set below. */
3092 0,
3093 objfile->global_psymbols.next,
3094 objfile->static_psymbols.next);
72bf9492 3095
d85a05f0
DJ
3096 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3097 if (attr != NULL)
3098 pst->dirname = DW_STRING (attr);
72bf9492 3099
e38df1d0 3100 pst->read_symtab_private = this_cu;
72bf9492 3101
93311388 3102 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3103
93311388
DE
3104 /* Store the function that reads in the rest of the symbol table */
3105 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3106
9291a0cd 3107 this_cu->v.psymtab = pst;
c906108c 3108
d85a05f0
DJ
3109 dwarf2_find_base_address (comp_unit_die, &cu);
3110
93311388
DE
3111 /* Possibly set the default values of LOWPC and HIGHPC from
3112 `DW_AT_ranges'. */
d85a05f0
DJ
3113 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3114 &best_highpc, &cu, pst);
3115 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3116 /* Store the contiguous range if it is not empty; it can be empty for
3117 CUs with no code. */
3118 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3119 best_lowpc + baseaddr,
3120 best_highpc + baseaddr - 1, pst);
93311388
DE
3121
3122 /* Check if comp unit has_children.
3123 If so, read the rest of the partial symbols from this comp unit.
3124 If not, there's no more debug_info for this comp unit. */
d85a05f0 3125 if (has_children)
93311388
DE
3126 {
3127 struct partial_die_info *first_die;
3128 CORE_ADDR lowpc, highpc;
31ffec48 3129
93311388
DE
3130 lowpc = ((CORE_ADDR) -1);
3131 highpc = ((CORE_ADDR) 0);
c906108c 3132
93311388 3133 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3134
93311388 3135 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3136 ! has_pc_info, &cu);
57c22c6c 3137
93311388
DE
3138 /* If we didn't find a lowpc, set it to highpc to avoid
3139 complaints from `maint check'. */
3140 if (lowpc == ((CORE_ADDR) -1))
3141 lowpc = highpc;
10b3939b 3142
93311388
DE
3143 /* If the compilation unit didn't have an explicit address range,
3144 then use the information extracted from its child dies. */
d85a05f0 3145 if (! has_pc_info)
93311388 3146 {
d85a05f0
DJ
3147 best_lowpc = lowpc;
3148 best_highpc = highpc;
93311388
DE
3149 }
3150 }
d85a05f0
DJ
3151 pst->textlow = best_lowpc + baseaddr;
3152 pst->texthigh = best_highpc + baseaddr;
c906108c 3153
93311388
DE
3154 pst->n_global_syms = objfile->global_psymbols.next -
3155 (objfile->global_psymbols.list + pst->globals_offset);
3156 pst->n_static_syms = objfile->static_psymbols.next -
3157 (objfile->static_psymbols.list + pst->statics_offset);
3158 sort_pst_symbols (pst);
c906108c 3159
93311388
DE
3160 info_ptr = (beg_of_comp_unit + cu.header.length
3161 + cu.header.initial_length_size);
ae038cb0 3162
348e048f
DE
3163 if (this_cu->from_debug_types)
3164 {
3165 /* It's not clear we want to do anything with stmt lists here.
3166 Waiting to see what gcc ultimately does. */
3167 }
d85a05f0 3168 else
93311388
DE
3169 {
3170 /* Get the list of files included in the current compilation unit,
3171 and build a psymtab for each of them. */
d85a05f0 3172 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3173 }
ae038cb0 3174
93311388 3175 do_cleanups (back_to_inner);
ae038cb0 3176
93311388
DE
3177 return info_ptr;
3178}
ff013f42 3179
348e048f
DE
3180/* Traversal function for htab_traverse_noresize.
3181 Process one .debug_types comp-unit. */
3182
3183static int
3184process_type_comp_unit (void **slot, void *info)
3185{
3186 struct signatured_type *entry = (struct signatured_type *) *slot;
3187 struct objfile *objfile = (struct objfile *) info;
3188 struct dwarf2_per_cu_data *this_cu;
3189
3190 this_cu = &entry->per_cu;
348e048f 3191
be391dca 3192 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3193 process_psymtab_comp_unit (objfile, this_cu,
3194 dwarf2_per_objfile->types.buffer,
3195 dwarf2_per_objfile->types.buffer + entry->offset,
3196 dwarf2_per_objfile->types.size);
3197
3198 return 1;
3199}
3200
3201/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3202 Build partial symbol tables for the .debug_types comp-units. */
3203
3204static void
3205build_type_psymtabs (struct objfile *objfile)
3206{
3207 if (! create_debug_types_hash_table (objfile))
3208 return;
3209
3210 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3211 process_type_comp_unit, objfile);
3212}
3213
60606b2c
TT
3214/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3215
3216static void
3217psymtabs_addrmap_cleanup (void *o)
3218{
3219 struct objfile *objfile = o;
ec61707d 3220
60606b2c
TT
3221 objfile->psymtabs_addrmap = NULL;
3222}
3223
93311388
DE
3224/* Build the partial symbol table by doing a quick pass through the
3225 .debug_info and .debug_abbrev sections. */
72bf9492 3226
93311388 3227static void
c67a9c90 3228dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3229{
93311388 3230 gdb_byte *info_ptr;
60606b2c
TT
3231 struct cleanup *back_to, *addrmap_cleanup;
3232 struct obstack temp_obstack;
93311388 3233
98bfdba5
PA
3234 dwarf2_per_objfile->reading_partial_symbols = 1;
3235
be391dca 3236 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3237 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3238
93311388
DE
3239 /* Any cached compilation units will be linked by the per-objfile
3240 read_in_chain. Make sure to free them when we're done. */
3241 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3242
348e048f
DE
3243 build_type_psymtabs (objfile);
3244
93311388 3245 create_all_comp_units (objfile);
c906108c 3246
60606b2c
TT
3247 /* Create a temporary address map on a temporary obstack. We later
3248 copy this to the final obstack. */
3249 obstack_init (&temp_obstack);
3250 make_cleanup_obstack_free (&temp_obstack);
3251 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3252 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3253
93311388
DE
3254 /* Since the objects we're extracting from .debug_info vary in
3255 length, only the individual functions to extract them (like
3256 read_comp_unit_head and load_partial_die) can really know whether
3257 the buffer is large enough to hold another complete object.
c906108c 3258
93311388
DE
3259 At the moment, they don't actually check that. If .debug_info
3260 holds just one extra byte after the last compilation unit's dies,
3261 then read_comp_unit_head will happily read off the end of the
3262 buffer. read_partial_die is similarly casual. Those functions
3263 should be fixed.
c906108c 3264
93311388
DE
3265 For this loop condition, simply checking whether there's any data
3266 left at all should be sufficient. */
c906108c 3267
93311388
DE
3268 while (info_ptr < (dwarf2_per_objfile->info.buffer
3269 + dwarf2_per_objfile->info.size))
3270 {
3271 struct dwarf2_per_cu_data *this_cu;
dd373385 3272
93311388
DE
3273 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3274 objfile);
aaa75496 3275
93311388
DE
3276 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3277 dwarf2_per_objfile->info.buffer,
3278 info_ptr,
3279 dwarf2_per_objfile->info.size);
c906108c 3280 }
ff013f42
JK
3281
3282 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3283 &objfile->objfile_obstack);
60606b2c 3284 discard_cleanups (addrmap_cleanup);
ff013f42 3285
ae038cb0
DJ
3286 do_cleanups (back_to);
3287}
3288
93311388 3289/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3290
3291static void
93311388
DE
3292load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3293 struct objfile *objfile)
ae038cb0
DJ
3294{
3295 bfd *abfd = objfile->obfd;
fe1b8b76 3296 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3297 struct die_info *comp_unit_die;
ae038cb0 3298 struct dwarf2_cu *cu;
1d9ec526 3299 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3300 int has_children;
3301 struct die_reader_specs reader_specs;
98bfdba5 3302 int read_cu = 0;
ae038cb0 3303
348e048f
DE
3304 gdb_assert (! this_cu->from_debug_types);
3305
be391dca 3306 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3307 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3308 beg_of_comp_unit = info_ptr;
3309
98bfdba5
PA
3310 if (this_cu->cu == NULL)
3311 {
9816fde3
JK
3312 cu = xmalloc (sizeof (*cu));
3313 init_one_comp_unit (cu, objfile);
ae038cb0 3314
98bfdba5 3315 read_cu = 1;
ae038cb0 3316
98bfdba5
PA
3317 /* If an error occurs while loading, release our storage. */
3318 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3319
98bfdba5
PA
3320 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3321 dwarf2_per_objfile->info.buffer,
3322 dwarf2_per_objfile->info.size,
3323 abfd);
ae038cb0 3324
98bfdba5
PA
3325 /* Complete the cu_header. */
3326 cu->header.offset = this_cu->offset;
3327 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3328
3329 /* Link this compilation unit into the compilation unit tree. */
3330 this_cu->cu = cu;
3331 cu->per_cu = this_cu;
98bfdba5
PA
3332
3333 /* Link this CU into read_in_chain. */
3334 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3335 dwarf2_per_objfile->read_in_chain = this_cu;
3336 }
3337 else
3338 {
3339 cu = this_cu->cu;
3340 info_ptr += cu->header.first_die_offset;
3341 }
ae038cb0
DJ
3342
3343 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3344 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3345 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3346 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3347
3348 /* Read the compilation unit die. */
d85a05f0
DJ
3349 init_cu_die_reader (&reader_specs, cu);
3350 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3351 &has_children);
ae038cb0 3352
9816fde3 3353 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3354
ae038cb0
DJ
3355 /* Check if comp unit has_children.
3356 If so, read the rest of the partial symbols from this comp unit.
3357 If not, there's no more debug_info for this comp unit. */
d85a05f0 3358 if (has_children)
93311388 3359 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3360
98bfdba5
PA
3361 do_cleanups (free_abbrevs_cleanup);
3362
3363 if (read_cu)
3364 {
3365 /* We've successfully allocated this compilation unit. Let our
3366 caller clean it up when finished with it. */
3367 discard_cleanups (free_cu_cleanup);
3368 }
ae038cb0
DJ
3369}
3370
3371/* Create a list of all compilation units in OBJFILE. We do this only
3372 if an inter-comp-unit reference is found; presumably if there is one,
3373 there will be many, and one will occur early in the .debug_info section.
3374 So there's no point in building this list incrementally. */
3375
3376static void
3377create_all_comp_units (struct objfile *objfile)
3378{
3379 int n_allocated;
3380 int n_comp_units;
3381 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3382 gdb_byte *info_ptr;
3383
3384 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3385 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3386
3387 n_comp_units = 0;
3388 n_allocated = 10;
3389 all_comp_units = xmalloc (n_allocated
3390 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3391
dce234bc 3392 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3393 {
c764a876 3394 unsigned int length, initial_length_size;
ae038cb0 3395 struct dwarf2_per_cu_data *this_cu;
c764a876 3396 unsigned int offset;
ae038cb0 3397
dce234bc 3398 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3399
3400 /* Read just enough information to find out where the next
3401 compilation unit is. */
c764a876
DE
3402 length = read_initial_length (objfile->obfd, info_ptr,
3403 &initial_length_size);
ae038cb0
DJ
3404
3405 /* Save the compilation unit for later lookup. */
3406 this_cu = obstack_alloc (&objfile->objfile_obstack,
3407 sizeof (struct dwarf2_per_cu_data));
3408 memset (this_cu, 0, sizeof (*this_cu));
3409 this_cu->offset = offset;
c764a876 3410 this_cu->length = length + initial_length_size;
9291a0cd 3411 this_cu->objfile = objfile;
ae038cb0
DJ
3412
3413 if (n_comp_units == n_allocated)
3414 {
3415 n_allocated *= 2;
3416 all_comp_units = xrealloc (all_comp_units,
3417 n_allocated
3418 * sizeof (struct dwarf2_per_cu_data *));
3419 }
3420 all_comp_units[n_comp_units++] = this_cu;
3421
3422 info_ptr = info_ptr + this_cu->length;
3423 }
3424
3425 dwarf2_per_objfile->all_comp_units
3426 = obstack_alloc (&objfile->objfile_obstack,
3427 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3428 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3429 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3430 xfree (all_comp_units);
3431 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3432}
3433
5734ee8b
DJ
3434/* Process all loaded DIEs for compilation unit CU, starting at
3435 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3436 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3437 DW_AT_ranges). If NEED_PC is set, then this function will set
3438 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3439 and record the covered ranges in the addrmap. */
c906108c 3440
72bf9492
DJ
3441static void
3442scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3443 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3444{
72bf9492 3445 struct partial_die_info *pdi;
c906108c 3446
91c24f0a
DC
3447 /* Now, march along the PDI's, descending into ones which have
3448 interesting children but skipping the children of the other ones,
3449 until we reach the end of the compilation unit. */
c906108c 3450
72bf9492 3451 pdi = first_die;
91c24f0a 3452
72bf9492
DJ
3453 while (pdi != NULL)
3454 {
3455 fixup_partial_die (pdi, cu);
c906108c 3456
f55ee35c 3457 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3458 children, so we need to look at them. Ditto for anonymous
3459 enums. */
933c6fe4 3460
72bf9492 3461 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3462 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3463 {
72bf9492 3464 switch (pdi->tag)
c906108c
SS
3465 {
3466 case DW_TAG_subprogram:
5734ee8b 3467 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3468 break;
72929c62 3469 case DW_TAG_constant:
c906108c
SS
3470 case DW_TAG_variable:
3471 case DW_TAG_typedef:
91c24f0a 3472 case DW_TAG_union_type:
72bf9492 3473 if (!pdi->is_declaration)
63d06c5c 3474 {
72bf9492 3475 add_partial_symbol (pdi, cu);
63d06c5c
DC
3476 }
3477 break;
c906108c 3478 case DW_TAG_class_type:
680b30c7 3479 case DW_TAG_interface_type:
c906108c 3480 case DW_TAG_structure_type:
72bf9492 3481 if (!pdi->is_declaration)
c906108c 3482 {
72bf9492 3483 add_partial_symbol (pdi, cu);
c906108c
SS
3484 }
3485 break;
91c24f0a 3486 case DW_TAG_enumeration_type:
72bf9492
DJ
3487 if (!pdi->is_declaration)
3488 add_partial_enumeration (pdi, cu);
c906108c
SS
3489 break;
3490 case DW_TAG_base_type:
a02abb62 3491 case DW_TAG_subrange_type:
c906108c 3492 /* File scope base type definitions are added to the partial
c5aa993b 3493 symbol table. */
72bf9492 3494 add_partial_symbol (pdi, cu);
c906108c 3495 break;
d9fa45fe 3496 case DW_TAG_namespace:
5734ee8b 3497 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3498 break;
5d7cb8df
JK
3499 case DW_TAG_module:
3500 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3501 break;
c906108c
SS
3502 default:
3503 break;
3504 }
3505 }
3506
72bf9492
DJ
3507 /* If the die has a sibling, skip to the sibling. */
3508
3509 pdi = pdi->die_sibling;
3510 }
3511}
3512
3513/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3514
72bf9492 3515 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3516 name is concatenated with "::" and the partial DIE's name. For
3517 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3518 Enumerators are an exception; they use the scope of their parent
3519 enumeration type, i.e. the name of the enumeration type is not
3520 prepended to the enumerator.
91c24f0a 3521
72bf9492
DJ
3522 There are two complexities. One is DW_AT_specification; in this
3523 case "parent" means the parent of the target of the specification,
3524 instead of the direct parent of the DIE. The other is compilers
3525 which do not emit DW_TAG_namespace; in this case we try to guess
3526 the fully qualified name of structure types from their members'
3527 linkage names. This must be done using the DIE's children rather
3528 than the children of any DW_AT_specification target. We only need
3529 to do this for structures at the top level, i.e. if the target of
3530 any DW_AT_specification (if any; otherwise the DIE itself) does not
3531 have a parent. */
3532
3533/* Compute the scope prefix associated with PDI's parent, in
3534 compilation unit CU. The result will be allocated on CU's
3535 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3536 field. NULL is returned if no prefix is necessary. */
3537static char *
3538partial_die_parent_scope (struct partial_die_info *pdi,
3539 struct dwarf2_cu *cu)
3540{
3541 char *grandparent_scope;
3542 struct partial_die_info *parent, *real_pdi;
91c24f0a 3543
72bf9492
DJ
3544 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3545 then this means the parent of the specification DIE. */
3546
3547 real_pdi = pdi;
72bf9492 3548 while (real_pdi->has_specification)
10b3939b 3549 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3550
3551 parent = real_pdi->die_parent;
3552 if (parent == NULL)
3553 return NULL;
3554
3555 if (parent->scope_set)
3556 return parent->scope;
3557
3558 fixup_partial_die (parent, cu);
3559
10b3939b 3560 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3561
acebe513
UW
3562 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3563 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3564 Work around this problem here. */
3565 if (cu->language == language_cplus
6e70227d 3566 && parent->tag == DW_TAG_namespace
acebe513
UW
3567 && strcmp (parent->name, "::") == 0
3568 && grandparent_scope == NULL)
3569 {
3570 parent->scope = NULL;
3571 parent->scope_set = 1;
3572 return NULL;
3573 }
3574
72bf9492 3575 if (parent->tag == DW_TAG_namespace
f55ee35c 3576 || parent->tag == DW_TAG_module
72bf9492
DJ
3577 || parent->tag == DW_TAG_structure_type
3578 || parent->tag == DW_TAG_class_type
680b30c7 3579 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3580 || parent->tag == DW_TAG_union_type
3581 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3582 {
3583 if (grandparent_scope == NULL)
3584 parent->scope = parent->name;
3585 else
987504bb 3586 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3587 parent->name, 0, cu);
72bf9492 3588 }
ceeb3d5a 3589 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3590 /* Enumerators should not get the name of the enumeration as a prefix. */
3591 parent->scope = grandparent_scope;
3592 else
3593 {
3594 /* FIXME drow/2004-04-01: What should we be doing with
3595 function-local names? For partial symbols, we should probably be
3596 ignoring them. */
3597 complaint (&symfile_complaints,
e2e0b3e5 3598 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3599 parent->tag, pdi->offset);
3600 parent->scope = grandparent_scope;
c906108c
SS
3601 }
3602
72bf9492
DJ
3603 parent->scope_set = 1;
3604 return parent->scope;
3605}
3606
3607/* Return the fully scoped name associated with PDI, from compilation unit
3608 CU. The result will be allocated with malloc. */
3609static char *
3610partial_die_full_name (struct partial_die_info *pdi,
3611 struct dwarf2_cu *cu)
3612{
3613 char *parent_scope;
3614
98bfdba5
PA
3615 /* If this is a template instantiation, we can not work out the
3616 template arguments from partial DIEs. So, unfortunately, we have
3617 to go through the full DIEs. At least any work we do building
3618 types here will be reused if full symbols are loaded later. */
3619 if (pdi->has_template_arguments)
3620 {
3621 fixup_partial_die (pdi, cu);
3622
3623 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3624 {
3625 struct die_info *die;
3626 struct attribute attr;
3627 struct dwarf2_cu *ref_cu = cu;
3628
3629 attr.name = 0;
3630 attr.form = DW_FORM_ref_addr;
3631 attr.u.addr = pdi->offset;
3632 die = follow_die_ref (NULL, &attr, &ref_cu);
3633
3634 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3635 }
3636 }
3637
72bf9492
DJ
3638 parent_scope = partial_die_parent_scope (pdi, cu);
3639 if (parent_scope == NULL)
3640 return NULL;
3641 else
f55ee35c 3642 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3643}
3644
3645static void
72bf9492 3646add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3647{
e7c27a73 3648 struct objfile *objfile = cu->objfile;
c906108c 3649 CORE_ADDR addr = 0;
decbce07 3650 char *actual_name = NULL;
5c4e30ca 3651 const struct partial_symbol *psym = NULL;
e142c38c 3652 CORE_ADDR baseaddr;
72bf9492 3653 int built_actual_name = 0;
e142c38c
DJ
3654
3655 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3656
94af9270
KS
3657 actual_name = partial_die_full_name (pdi, cu);
3658 if (actual_name)
3659 built_actual_name = 1;
63d06c5c 3660
72bf9492
DJ
3661 if (actual_name == NULL)
3662 actual_name = pdi->name;
3663
c906108c
SS
3664 switch (pdi->tag)
3665 {
3666 case DW_TAG_subprogram:
2cfa0c8d 3667 if (pdi->is_external || cu->language == language_ada)
c906108c 3668 {
2cfa0c8d
JB
3669 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3670 of the global scope. But in Ada, we want to be able to access
3671 nested procedures globally. So all Ada subprograms are stored
3672 in the global scope. */
38d518c9 3673 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3674 mst_text, objfile); */
38d518c9 3675 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3676 built_actual_name,
5c4e30ca
DC
3677 VAR_DOMAIN, LOC_BLOCK,
3678 &objfile->global_psymbols,
3679 0, pdi->lowpc + baseaddr,
e142c38c 3680 cu->language, objfile);
c906108c
SS
3681 }
3682 else
3683 {
38d518c9 3684 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3685 mst_file_text, objfile); */
38d518c9 3686 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3687 built_actual_name,
5c4e30ca
DC
3688 VAR_DOMAIN, LOC_BLOCK,
3689 &objfile->static_psymbols,
3690 0, pdi->lowpc + baseaddr,
e142c38c 3691 cu->language, objfile);
c906108c
SS
3692 }
3693 break;
72929c62
JB
3694 case DW_TAG_constant:
3695 {
3696 struct psymbol_allocation_list *list;
3697
3698 if (pdi->is_external)
3699 list = &objfile->global_psymbols;
3700 else
3701 list = &objfile->static_psymbols;
3702 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3703 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3704 list, 0, 0, cu->language, objfile);
3705
3706 }
3707 break;
c906108c 3708 case DW_TAG_variable:
caac4577
JG
3709 if (pdi->locdesc)
3710 addr = decode_locdesc (pdi->locdesc, cu);
3711
3712 if (pdi->locdesc
3713 && addr == 0
3714 && !dwarf2_per_objfile->has_section_at_zero)
3715 {
3716 /* A global or static variable may also have been stripped
3717 out by the linker if unused, in which case its address
3718 will be nullified; do not add such variables into partial
3719 symbol table then. */
3720 }
3721 else if (pdi->is_external)
c906108c
SS
3722 {
3723 /* Global Variable.
3724 Don't enter into the minimal symbol tables as there is
3725 a minimal symbol table entry from the ELF symbols already.
3726 Enter into partial symbol table if it has a location
3727 descriptor or a type.
3728 If the location descriptor is missing, new_symbol will create
3729 a LOC_UNRESOLVED symbol, the address of the variable will then
3730 be determined from the minimal symbol table whenever the variable
3731 is referenced.
3732 The address for the partial symbol table entry is not
3733 used by GDB, but it comes in handy for debugging partial symbol
3734 table building. */
3735
c906108c 3736 if (pdi->locdesc || pdi->has_type)
38d518c9 3737 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3738 built_actual_name,
5c4e30ca
DC
3739 VAR_DOMAIN, LOC_STATIC,
3740 &objfile->global_psymbols,
3741 0, addr + baseaddr,
e142c38c 3742 cu->language, objfile);
c906108c
SS
3743 }
3744 else
3745 {
3746 /* Static Variable. Skip symbols without location descriptors. */
3747 if (pdi->locdesc == NULL)
decbce07
MS
3748 {
3749 if (built_actual_name)
3750 xfree (actual_name);
3751 return;
3752 }
38d518c9 3753 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3754 mst_file_data, objfile); */
38d518c9 3755 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3756 built_actual_name,
5c4e30ca
DC
3757 VAR_DOMAIN, LOC_STATIC,
3758 &objfile->static_psymbols,
3759 0, addr + baseaddr,
e142c38c 3760 cu->language, objfile);
c906108c
SS
3761 }
3762 break;
3763 case DW_TAG_typedef:
3764 case DW_TAG_base_type:
a02abb62 3765 case DW_TAG_subrange_type:
38d518c9 3766 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3767 built_actual_name,
176620f1 3768 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3769 &objfile->static_psymbols,
e142c38c 3770 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3771 break;
72bf9492
DJ
3772 case DW_TAG_namespace:
3773 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3774 built_actual_name,
72bf9492
DJ
3775 VAR_DOMAIN, LOC_TYPEDEF,
3776 &objfile->global_psymbols,
3777 0, (CORE_ADDR) 0, cu->language, objfile);
3778 break;
c906108c 3779 case DW_TAG_class_type:
680b30c7 3780 case DW_TAG_interface_type:
c906108c
SS
3781 case DW_TAG_structure_type:
3782 case DW_TAG_union_type:
3783 case DW_TAG_enumeration_type:
fa4028e9
JB
3784 /* Skip external references. The DWARF standard says in the section
3785 about "Structure, Union, and Class Type Entries": "An incomplete
3786 structure, union or class type is represented by a structure,
3787 union or class entry that does not have a byte size attribute
3788 and that has a DW_AT_declaration attribute." */
3789 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3790 {
3791 if (built_actual_name)
3792 xfree (actual_name);
3793 return;
3794 }
fa4028e9 3795
63d06c5c
DC
3796 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3797 static vs. global. */
38d518c9 3798 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3799 built_actual_name,
176620f1 3800 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3801 (cu->language == language_cplus
3802 || cu->language == language_java)
63d06c5c
DC
3803 ? &objfile->global_psymbols
3804 : &objfile->static_psymbols,
e142c38c 3805 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3806
c906108c
SS
3807 break;
3808 case DW_TAG_enumerator:
38d518c9 3809 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3810 built_actual_name,
176620f1 3811 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3812 (cu->language == language_cplus
3813 || cu->language == language_java)
f6fe98ef
DJ
3814 ? &objfile->global_psymbols
3815 : &objfile->static_psymbols,
e142c38c 3816 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3817 break;
3818 default:
3819 break;
3820 }
5c4e30ca 3821
72bf9492
DJ
3822 if (built_actual_name)
3823 xfree (actual_name);
c906108c
SS
3824}
3825
5c4e30ca
DC
3826/* Read a partial die corresponding to a namespace; also, add a symbol
3827 corresponding to that namespace to the symbol table. NAMESPACE is
3828 the name of the enclosing namespace. */
91c24f0a 3829
72bf9492
DJ
3830static void
3831add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3832 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3833 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3834{
72bf9492 3835 /* Add a symbol for the namespace. */
e7c27a73 3836
72bf9492 3837 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3838
3839 /* Now scan partial symbols in that namespace. */
3840
91c24f0a 3841 if (pdi->has_children)
5734ee8b 3842 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3843}
3844
5d7cb8df
JK
3845/* Read a partial die corresponding to a Fortran module. */
3846
3847static void
3848add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3849 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3850{
f55ee35c 3851 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3852
3853 if (pdi->has_children)
3854 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3855}
3856
bc30ff58
JB
3857/* Read a partial die corresponding to a subprogram and create a partial
3858 symbol for that subprogram. When the CU language allows it, this
3859 routine also defines a partial symbol for each nested subprogram
3860 that this subprogram contains.
6e70227d 3861
bc30ff58
JB
3862 DIE my also be a lexical block, in which case we simply search
3863 recursively for suprograms defined inside that lexical block.
3864 Again, this is only performed when the CU language allows this
3865 type of definitions. */
3866
3867static void
3868add_partial_subprogram (struct partial_die_info *pdi,
3869 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3870 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3871{
3872 if (pdi->tag == DW_TAG_subprogram)
3873 {
3874 if (pdi->has_pc_info)
3875 {
3876 if (pdi->lowpc < *lowpc)
3877 *lowpc = pdi->lowpc;
3878 if (pdi->highpc > *highpc)
3879 *highpc = pdi->highpc;
5734ee8b
DJ
3880 if (need_pc)
3881 {
3882 CORE_ADDR baseaddr;
3883 struct objfile *objfile = cu->objfile;
3884
3885 baseaddr = ANOFFSET (objfile->section_offsets,
3886 SECT_OFF_TEXT (objfile));
3887 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3888 pdi->lowpc + baseaddr,
3889 pdi->highpc - 1 + baseaddr,
9291a0cd 3890 cu->per_cu->v.psymtab);
5734ee8b 3891 }
bc30ff58 3892 if (!pdi->is_declaration)
e8d05480
JB
3893 /* Ignore subprogram DIEs that do not have a name, they are
3894 illegal. Do not emit a complaint at this point, we will
3895 do so when we convert this psymtab into a symtab. */
3896 if (pdi->name)
3897 add_partial_symbol (pdi, cu);
bc30ff58
JB
3898 }
3899 }
6e70227d 3900
bc30ff58
JB
3901 if (! pdi->has_children)
3902 return;
3903
3904 if (cu->language == language_ada)
3905 {
3906 pdi = pdi->die_child;
3907 while (pdi != NULL)
3908 {
3909 fixup_partial_die (pdi, cu);
3910 if (pdi->tag == DW_TAG_subprogram
3911 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3912 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3913 pdi = pdi->die_sibling;
3914 }
3915 }
3916}
3917
91c24f0a
DC
3918/* Read a partial die corresponding to an enumeration type. */
3919
72bf9492
DJ
3920static void
3921add_partial_enumeration (struct partial_die_info *enum_pdi,
3922 struct dwarf2_cu *cu)
91c24f0a 3923{
72bf9492 3924 struct partial_die_info *pdi;
91c24f0a
DC
3925
3926 if (enum_pdi->name != NULL)
72bf9492
DJ
3927 add_partial_symbol (enum_pdi, cu);
3928
3929 pdi = enum_pdi->die_child;
3930 while (pdi)
91c24f0a 3931 {
72bf9492 3932 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3933 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3934 else
72bf9492
DJ
3935 add_partial_symbol (pdi, cu);
3936 pdi = pdi->die_sibling;
91c24f0a 3937 }
91c24f0a
DC
3938}
3939
4bb7a0a7
DJ
3940/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3941 Return the corresponding abbrev, or NULL if the number is zero (indicating
3942 an empty DIE). In either case *BYTES_READ will be set to the length of
3943 the initial number. */
3944
3945static struct abbrev_info *
fe1b8b76 3946peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3947 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3948{
3949 bfd *abfd = cu->objfile->obfd;
3950 unsigned int abbrev_number;
3951 struct abbrev_info *abbrev;
3952
3953 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3954
3955 if (abbrev_number == 0)
3956 return NULL;
3957
3958 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3959 if (!abbrev)
3960 {
8a3fe4f8 3961 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3962 bfd_get_filename (abfd));
3963 }
3964
3965 return abbrev;
3966}
3967
93311388
DE
3968/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3969 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3970 DIE. Any children of the skipped DIEs will also be skipped. */
3971
fe1b8b76 3972static gdb_byte *
93311388 3973skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3974{
3975 struct abbrev_info *abbrev;
3976 unsigned int bytes_read;
3977
3978 while (1)
3979 {
3980 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3981 if (abbrev == NULL)
3982 return info_ptr + bytes_read;
3983 else
93311388 3984 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3985 }
3986}
3987
93311388
DE
3988/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3989 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3990 abbrev corresponding to that skipped uleb128 should be passed in
3991 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3992 children. */
3993
fe1b8b76 3994static gdb_byte *
93311388
DE
3995skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3996 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3997{
3998 unsigned int bytes_read;
3999 struct attribute attr;
4000 bfd *abfd = cu->objfile->obfd;
4001 unsigned int form, i;
4002
4003 for (i = 0; i < abbrev->num_attrs; i++)
4004 {
4005 /* The only abbrev we care about is DW_AT_sibling. */
4006 if (abbrev->attrs[i].name == DW_AT_sibling)
4007 {
4008 read_attribute (&attr, &abbrev->attrs[i],
4009 abfd, info_ptr, cu);
4010 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4011 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4012 else
93311388 4013 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4014 }
4015
4016 /* If it isn't DW_AT_sibling, skip this attribute. */
4017 form = abbrev->attrs[i].form;
4018 skip_attribute:
4019 switch (form)
4020 {
4bb7a0a7 4021 case DW_FORM_ref_addr:
ae411497
TT
4022 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4023 and later it is offset sized. */
4024 if (cu->header.version == 2)
4025 info_ptr += cu->header.addr_size;
4026 else
4027 info_ptr += cu->header.offset_size;
4028 break;
4029 case DW_FORM_addr:
4bb7a0a7
DJ
4030 info_ptr += cu->header.addr_size;
4031 break;
4032 case DW_FORM_data1:
4033 case DW_FORM_ref1:
4034 case DW_FORM_flag:
4035 info_ptr += 1;
4036 break;
2dc7f7b3
TT
4037 case DW_FORM_flag_present:
4038 break;
4bb7a0a7
DJ
4039 case DW_FORM_data2:
4040 case DW_FORM_ref2:
4041 info_ptr += 2;
4042 break;
4043 case DW_FORM_data4:
4044 case DW_FORM_ref4:
4045 info_ptr += 4;
4046 break;
4047 case DW_FORM_data8:
4048 case DW_FORM_ref8:
348e048f 4049 case DW_FORM_sig8:
4bb7a0a7
DJ
4050 info_ptr += 8;
4051 break;
4052 case DW_FORM_string:
9b1c24c8 4053 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4054 info_ptr += bytes_read;
4055 break;
2dc7f7b3 4056 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4057 case DW_FORM_strp:
4058 info_ptr += cu->header.offset_size;
4059 break;
2dc7f7b3 4060 case DW_FORM_exprloc:
4bb7a0a7
DJ
4061 case DW_FORM_block:
4062 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4063 info_ptr += bytes_read;
4064 break;
4065 case DW_FORM_block1:
4066 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4067 break;
4068 case DW_FORM_block2:
4069 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4070 break;
4071 case DW_FORM_block4:
4072 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4073 break;
4074 case DW_FORM_sdata:
4075 case DW_FORM_udata:
4076 case DW_FORM_ref_udata:
4077 info_ptr = skip_leb128 (abfd, info_ptr);
4078 break;
4079 case DW_FORM_indirect:
4080 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4081 info_ptr += bytes_read;
4082 /* We need to continue parsing from here, so just go back to
4083 the top. */
4084 goto skip_attribute;
4085
4086 default:
8a3fe4f8 4087 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4088 dwarf_form_name (form),
4089 bfd_get_filename (abfd));
4090 }
4091 }
4092
4093 if (abbrev->has_children)
93311388 4094 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4095 else
4096 return info_ptr;
4097}
4098
93311388
DE
4099/* Locate ORIG_PDI's sibling.
4100 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4101 in BUFFER. */
91c24f0a 4102
fe1b8b76 4103static gdb_byte *
93311388
DE
4104locate_pdi_sibling (struct partial_die_info *orig_pdi,
4105 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4106 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4107{
4108 /* Do we know the sibling already? */
72bf9492 4109
91c24f0a
DC
4110 if (orig_pdi->sibling)
4111 return orig_pdi->sibling;
4112
4113 /* Are there any children to deal with? */
4114
4115 if (!orig_pdi->has_children)
4116 return info_ptr;
4117
4bb7a0a7 4118 /* Skip the children the long way. */
91c24f0a 4119
93311388 4120 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4121}
4122
c906108c
SS
4123/* Expand this partial symbol table into a full symbol table. */
4124
4125static void
fba45db2 4126dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4127{
c906108c
SS
4128 if (pst != NULL)
4129 {
4130 if (pst->readin)
4131 {
8a3fe4f8 4132 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4133 }
4134 else
4135 {
4136 if (info_verbose)
4137 {
a3f17187 4138 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4139 gdb_flush (gdb_stdout);
4140 }
4141
10b3939b
DJ
4142 /* Restore our global data. */
4143 dwarf2_per_objfile = objfile_data (pst->objfile,
4144 dwarf2_objfile_data_key);
4145
b2ab525c
KB
4146 /* If this psymtab is constructed from a debug-only objfile, the
4147 has_section_at_zero flag will not necessarily be correct. We
4148 can get the correct value for this flag by looking at the data
4149 associated with the (presumably stripped) associated objfile. */
4150 if (pst->objfile->separate_debug_objfile_backlink)
4151 {
4152 struct dwarf2_per_objfile *dpo_backlink
4153 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4154 dwarf2_objfile_data_key);
9a619af0 4155
b2ab525c
KB
4156 dwarf2_per_objfile->has_section_at_zero
4157 = dpo_backlink->has_section_at_zero;
4158 }
4159
98bfdba5
PA
4160 dwarf2_per_objfile->reading_partial_symbols = 0;
4161
c906108c
SS
4162 psymtab_to_symtab_1 (pst);
4163
4164 /* Finish up the debug error message. */
4165 if (info_verbose)
a3f17187 4166 printf_filtered (_("done.\n"));
c906108c
SS
4167 }
4168 }
4169}
4170
10b3939b
DJ
4171/* Add PER_CU to the queue. */
4172
4173static void
03dd20cc 4174queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4175{
4176 struct dwarf2_queue_item *item;
4177
4178 per_cu->queued = 1;
4179 item = xmalloc (sizeof (*item));
4180 item->per_cu = per_cu;
4181 item->next = NULL;
4182
4183 if (dwarf2_queue == NULL)
4184 dwarf2_queue = item;
4185 else
4186 dwarf2_queue_tail->next = item;
4187
4188 dwarf2_queue_tail = item;
4189}
4190
4191/* Process the queue. */
4192
4193static void
4194process_queue (struct objfile *objfile)
4195{
4196 struct dwarf2_queue_item *item, *next_item;
4197
03dd20cc
DJ
4198 /* The queue starts out with one item, but following a DIE reference
4199 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4200 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4201 {
9291a0cd
TT
4202 if (dwarf2_per_objfile->using_index
4203 ? !item->per_cu->v.quick->symtab
4204 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4205 process_full_comp_unit (item->per_cu);
4206
4207 item->per_cu->queued = 0;
4208 next_item = item->next;
4209 xfree (item);
4210 }
4211
4212 dwarf2_queue_tail = NULL;
4213}
4214
4215/* Free all allocated queue entries. This function only releases anything if
4216 an error was thrown; if the queue was processed then it would have been
4217 freed as we went along. */
4218
4219static void
4220dwarf2_release_queue (void *dummy)
4221{
4222 struct dwarf2_queue_item *item, *last;
4223
4224 item = dwarf2_queue;
4225 while (item)
4226 {
4227 /* Anything still marked queued is likely to be in an
4228 inconsistent state, so discard it. */
4229 if (item->per_cu->queued)
4230 {
4231 if (item->per_cu->cu != NULL)
4232 free_one_cached_comp_unit (item->per_cu->cu);
4233 item->per_cu->queued = 0;
4234 }
4235
4236 last = item;
4237 item = item->next;
4238 xfree (last);
4239 }
4240
4241 dwarf2_queue = dwarf2_queue_tail = NULL;
4242}
4243
4244/* Read in full symbols for PST, and anything it depends on. */
4245
c906108c 4246static void
fba45db2 4247psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4248{
10b3939b 4249 struct dwarf2_per_cu_data *per_cu;
c906108c 4250 struct cleanup *back_to;
aaa75496
JB
4251 int i;
4252
4253 for (i = 0; i < pst->number_of_dependencies; i++)
4254 if (!pst->dependencies[i]->readin)
4255 {
4256 /* Inform about additional files that need to be read in. */
4257 if (info_verbose)
4258 {
a3f17187 4259 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4260 fputs_filtered (" ", gdb_stdout);
4261 wrap_here ("");
4262 fputs_filtered ("and ", gdb_stdout);
4263 wrap_here ("");
4264 printf_filtered ("%s...", pst->dependencies[i]->filename);
4265 wrap_here (""); /* Flush output */
4266 gdb_flush (gdb_stdout);
4267 }
4268 psymtab_to_symtab_1 (pst->dependencies[i]);
4269 }
4270
e38df1d0 4271 per_cu = pst->read_symtab_private;
10b3939b
DJ
4272
4273 if (per_cu == NULL)
aaa75496
JB
4274 {
4275 /* It's an include file, no symbols to read for it.
4276 Everything is in the parent symtab. */
4277 pst->readin = 1;
4278 return;
4279 }
c906108c 4280
9291a0cd 4281 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4282}
4283
93311388 4284/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4285
93311388 4286static void
31ffec48 4287load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4288{
31ffec48 4289 bfd *abfd = objfile->obfd;
10b3939b 4290 struct dwarf2_cu *cu;
c764a876 4291 unsigned int offset;
93311388 4292 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4293 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4294 struct attribute *attr;
98bfdba5 4295 int read_cu = 0;
6502dd73 4296
348e048f
DE
4297 gdb_assert (! per_cu->from_debug_types);
4298
c906108c 4299 /* Set local variables from the partial symbol table info. */
10b3939b 4300 offset = per_cu->offset;
6502dd73 4301
be391dca 4302 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4303 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4304 beg_of_comp_unit = info_ptr;
63d06c5c 4305
98bfdba5
PA
4306 if (per_cu->cu == NULL)
4307 {
9816fde3
JK
4308 cu = xmalloc (sizeof (*cu));
4309 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4310
4311 read_cu = 1;
c906108c 4312
98bfdba5
PA
4313 /* If an error occurs while loading, release our storage. */
4314 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4315
98bfdba5
PA
4316 /* Read in the comp_unit header. */
4317 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4318
98bfdba5
PA
4319 /* Complete the cu_header. */
4320 cu->header.offset = offset;
4321 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4322
98bfdba5
PA
4323 /* Read the abbrevs for this compilation unit. */
4324 dwarf2_read_abbrevs (abfd, cu);
4325 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4326
98bfdba5
PA
4327 /* Link this compilation unit into the compilation unit tree. */
4328 per_cu->cu = cu;
4329 cu->per_cu = per_cu;
98bfdba5
PA
4330
4331 /* Link this CU into read_in_chain. */
4332 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4333 dwarf2_per_objfile->read_in_chain = per_cu;
4334 }
4335 else
4336 {
4337 cu = per_cu->cu;
4338 info_ptr += cu->header.first_die_offset;
4339 }
e142c38c 4340
93311388 4341 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4342
4343 /* We try not to read any attributes in this function, because not
4344 all objfiles needed for references have been loaded yet, and symbol
4345 table processing isn't initialized. But we have to set the CU language,
4346 or we won't be able to build types correctly. */
9816fde3 4347 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4348
a6c727b2
DJ
4349 /* Similarly, if we do not read the producer, we can not apply
4350 producer-specific interpretation. */
4351 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4352 if (attr)
4353 cu->producer = DW_STRING (attr);
4354
98bfdba5
PA
4355 if (read_cu)
4356 {
4357 do_cleanups (free_abbrevs_cleanup);
e142c38c 4358
98bfdba5
PA
4359 /* We've successfully allocated this compilation unit. Let our
4360 caller clean it up when finished with it. */
4361 discard_cleanups (free_cu_cleanup);
4362 }
10b3939b
DJ
4363}
4364
3da10d80
KS
4365/* Add a DIE to the delayed physname list. */
4366
4367static void
4368add_to_method_list (struct type *type, int fnfield_index, int index,
4369 const char *name, struct die_info *die,
4370 struct dwarf2_cu *cu)
4371{
4372 struct delayed_method_info mi;
4373 mi.type = type;
4374 mi.fnfield_index = fnfield_index;
4375 mi.index = index;
4376 mi.name = name;
4377 mi.die = die;
4378 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4379}
4380
4381/* A cleanup for freeing the delayed method list. */
4382
4383static void
4384free_delayed_list (void *ptr)
4385{
4386 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4387 if (cu->method_list != NULL)
4388 {
4389 VEC_free (delayed_method_info, cu->method_list);
4390 cu->method_list = NULL;
4391 }
4392}
4393
4394/* Compute the physnames of any methods on the CU's method list.
4395
4396 The computation of method physnames is delayed in order to avoid the
4397 (bad) condition that one of the method's formal parameters is of an as yet
4398 incomplete type. */
4399
4400static void
4401compute_delayed_physnames (struct dwarf2_cu *cu)
4402{
4403 int i;
4404 struct delayed_method_info *mi;
4405 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4406 {
4407 char *physname;
4408 struct fn_fieldlist *fn_flp
4409 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4410 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4411 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4412 }
4413}
4414
10b3939b
DJ
4415/* Generate full symbol information for PST and CU, whose DIEs have
4416 already been loaded into memory. */
4417
4418static void
4419process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4420{
10b3939b 4421 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4422 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4423 CORE_ADDR lowpc, highpc;
4424 struct symtab *symtab;
3da10d80 4425 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4426 CORE_ADDR baseaddr;
4427
4428 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4429
10b3939b
DJ
4430 buildsym_init ();
4431 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4432 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4433
4434 cu->list_in_scope = &file_symbols;
c906108c 4435
d85a05f0 4436 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4437
c906108c 4438 /* Do line number decoding in read_file_scope () */
10b3939b 4439 process_die (cu->dies, cu);
c906108c 4440
3da10d80
KS
4441 /* Now that we have processed all the DIEs in the CU, all the types
4442 should be complete, and it should now be safe to compute all of the
4443 physnames. */
4444 compute_delayed_physnames (cu);
4445 do_cleanups (delayed_list_cleanup);
4446
fae299cd
DC
4447 /* Some compilers don't define a DW_AT_high_pc attribute for the
4448 compilation unit. If the DW_AT_high_pc is missing, synthesize
4449 it, by scanning the DIE's below the compilation unit. */
10b3939b 4450 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4451
613e1657 4452 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4453
4454 /* Set symtab language to language from DW_AT_language.
4455 If the compilation is from a C file generated by language preprocessors,
4456 do not set the language if it was already deduced by start_subfile. */
4457 if (symtab != NULL
10b3939b 4458 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4459 {
10b3939b 4460 symtab->language = cu->language;
c906108c 4461 }
9291a0cd
TT
4462
4463 if (dwarf2_per_objfile->using_index)
4464 per_cu->v.quick->symtab = symtab;
4465 else
4466 {
4467 struct partial_symtab *pst = per_cu->v.psymtab;
4468 pst->symtab = symtab;
4469 pst->readin = 1;
4470 }
c906108c
SS
4471
4472 do_cleanups (back_to);
4473}
4474
4475/* Process a die and its children. */
4476
4477static void
e7c27a73 4478process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4479{
4480 switch (die->tag)
4481 {
4482 case DW_TAG_padding:
4483 break;
4484 case DW_TAG_compile_unit:
e7c27a73 4485 read_file_scope (die, cu);
c906108c 4486 break;
348e048f
DE
4487 case DW_TAG_type_unit:
4488 read_type_unit_scope (die, cu);
4489 break;
c906108c 4490 case DW_TAG_subprogram:
c906108c 4491 case DW_TAG_inlined_subroutine:
edb3359d 4492 read_func_scope (die, cu);
c906108c
SS
4493 break;
4494 case DW_TAG_lexical_block:
14898363
L
4495 case DW_TAG_try_block:
4496 case DW_TAG_catch_block:
e7c27a73 4497 read_lexical_block_scope (die, cu);
c906108c
SS
4498 break;
4499 case DW_TAG_class_type:
680b30c7 4500 case DW_TAG_interface_type:
c906108c
SS
4501 case DW_TAG_structure_type:
4502 case DW_TAG_union_type:
134d01f1 4503 process_structure_scope (die, cu);
c906108c
SS
4504 break;
4505 case DW_TAG_enumeration_type:
134d01f1 4506 process_enumeration_scope (die, cu);
c906108c 4507 break;
134d01f1 4508
f792889a
DJ
4509 /* These dies have a type, but processing them does not create
4510 a symbol or recurse to process the children. Therefore we can
4511 read them on-demand through read_type_die. */
c906108c 4512 case DW_TAG_subroutine_type:
72019c9c 4513 case DW_TAG_set_type:
c906108c 4514 case DW_TAG_array_type:
c906108c 4515 case DW_TAG_pointer_type:
c906108c 4516 case DW_TAG_ptr_to_member_type:
c906108c 4517 case DW_TAG_reference_type:
c906108c 4518 case DW_TAG_string_type:
c906108c 4519 break;
134d01f1 4520
c906108c 4521 case DW_TAG_base_type:
a02abb62 4522 case DW_TAG_subrange_type:
cb249c71 4523 case DW_TAG_typedef:
134d01f1
DJ
4524 /* Add a typedef symbol for the type definition, if it has a
4525 DW_AT_name. */
f792889a 4526 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4527 break;
c906108c 4528 case DW_TAG_common_block:
e7c27a73 4529 read_common_block (die, cu);
c906108c
SS
4530 break;
4531 case DW_TAG_common_inclusion:
4532 break;
d9fa45fe 4533 case DW_TAG_namespace:
63d06c5c 4534 processing_has_namespace_info = 1;
e7c27a73 4535 read_namespace (die, cu);
d9fa45fe 4536 break;
5d7cb8df 4537 case DW_TAG_module:
f55ee35c 4538 processing_has_namespace_info = 1;
5d7cb8df
JK
4539 read_module (die, cu);
4540 break;
d9fa45fe
DC
4541 case DW_TAG_imported_declaration:
4542 case DW_TAG_imported_module:
63d06c5c 4543 processing_has_namespace_info = 1;
27aa8d6a
SW
4544 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4545 || cu->language != language_fortran))
4546 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4547 dwarf_tag_name (die->tag));
4548 read_import_statement (die, cu);
d9fa45fe 4549 break;
c906108c 4550 default:
e7c27a73 4551 new_symbol (die, NULL, cu);
c906108c
SS
4552 break;
4553 }
4554}
4555
94af9270
KS
4556/* A helper function for dwarf2_compute_name which determines whether DIE
4557 needs to have the name of the scope prepended to the name listed in the
4558 die. */
4559
4560static int
4561die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4562{
1c809c68
TT
4563 struct attribute *attr;
4564
94af9270
KS
4565 switch (die->tag)
4566 {
4567 case DW_TAG_namespace:
4568 case DW_TAG_typedef:
4569 case DW_TAG_class_type:
4570 case DW_TAG_interface_type:
4571 case DW_TAG_structure_type:
4572 case DW_TAG_union_type:
4573 case DW_TAG_enumeration_type:
4574 case DW_TAG_enumerator:
4575 case DW_TAG_subprogram:
4576 case DW_TAG_member:
4577 return 1;
4578
4579 case DW_TAG_variable:
c2b0a229 4580 case DW_TAG_constant:
94af9270
KS
4581 /* We only need to prefix "globally" visible variables. These include
4582 any variable marked with DW_AT_external or any variable that
4583 lives in a namespace. [Variables in anonymous namespaces
4584 require prefixing, but they are not DW_AT_external.] */
4585
4586 if (dwarf2_attr (die, DW_AT_specification, cu))
4587 {
4588 struct dwarf2_cu *spec_cu = cu;
9a619af0 4589
94af9270
KS
4590 return die_needs_namespace (die_specification (die, &spec_cu),
4591 spec_cu);
4592 }
4593
1c809c68 4594 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4595 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4596 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4597 return 0;
4598 /* A variable in a lexical block of some kind does not need a
4599 namespace, even though in C++ such variables may be external
4600 and have a mangled name. */
4601 if (die->parent->tag == DW_TAG_lexical_block
4602 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4603 || die->parent->tag == DW_TAG_catch_block
4604 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4605 return 0;
4606 return 1;
94af9270
KS
4607
4608 default:
4609 return 0;
4610 }
4611}
4612
98bfdba5
PA
4613/* Retrieve the last character from a mem_file. */
4614
4615static void
4616do_ui_file_peek_last (void *object, const char *buffer, long length)
4617{
4618 char *last_char_p = (char *) object;
4619
4620 if (length > 0)
4621 *last_char_p = buffer[length - 1];
4622}
4623
94af9270
KS
4624/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4625 compute the physname for the object, which include a method's
4626 formal parameters (C++/Java) and return type (Java).
4627
af6b7be1
JB
4628 For Ada, return the DIE's linkage name rather than the fully qualified
4629 name. PHYSNAME is ignored..
4630
94af9270
KS
4631 The result is allocated on the objfile_obstack and canonicalized. */
4632
4633static const char *
4634dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4635 int physname)
4636{
4637 if (name == NULL)
4638 name = dwarf2_name (die, cu);
4639
f55ee35c
JK
4640 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4641 compute it by typename_concat inside GDB. */
4642 if (cu->language == language_ada
4643 || (cu->language == language_fortran && physname))
4644 {
4645 /* For Ada unit, we prefer the linkage name over the name, as
4646 the former contains the exported name, which the user expects
4647 to be able to reference. Ideally, we want the user to be able
4648 to reference this entity using either natural or linkage name,
4649 but we haven't started looking at this enhancement yet. */
4650 struct attribute *attr;
4651
4652 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4653 if (attr == NULL)
4654 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4655 if (attr && DW_STRING (attr))
4656 return DW_STRING (attr);
4657 }
4658
94af9270
KS
4659 /* These are the only languages we know how to qualify names in. */
4660 if (name != NULL
f55ee35c
JK
4661 && (cu->language == language_cplus || cu->language == language_java
4662 || cu->language == language_fortran))
94af9270
KS
4663 {
4664 if (die_needs_namespace (die, cu))
4665 {
4666 long length;
4667 char *prefix;
4668 struct ui_file *buf;
4669
4670 prefix = determine_prefix (die, cu);
4671 buf = mem_fileopen ();
4672 if (*prefix != '\0')
4673 {
f55ee35c
JK
4674 char *prefixed_name = typename_concat (NULL, prefix, name,
4675 physname, cu);
9a619af0 4676
94af9270
KS
4677 fputs_unfiltered (prefixed_name, buf);
4678 xfree (prefixed_name);
4679 }
4680 else
4681 fputs_unfiltered (name ? name : "", buf);
4682
98bfdba5
PA
4683 /* Template parameters may be specified in the DIE's DW_AT_name, or
4684 as children with DW_TAG_template_type_param or
4685 DW_TAG_value_type_param. If the latter, add them to the name
4686 here. If the name already has template parameters, then
4687 skip this step; some versions of GCC emit both, and
4688 it is more efficient to use the pre-computed name.
4689
4690 Something to keep in mind about this process: it is very
4691 unlikely, or in some cases downright impossible, to produce
4692 something that will match the mangled name of a function.
4693 If the definition of the function has the same debug info,
4694 we should be able to match up with it anyway. But fallbacks
4695 using the minimal symbol, for instance to find a method
4696 implemented in a stripped copy of libstdc++, will not work.
4697 If we do not have debug info for the definition, we will have to
4698 match them up some other way.
4699
4700 When we do name matching there is a related problem with function
4701 templates; two instantiated function templates are allowed to
4702 differ only by their return types, which we do not add here. */
4703
4704 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4705 {
4706 struct attribute *attr;
4707 struct die_info *child;
4708 int first = 1;
4709
4710 die->building_fullname = 1;
4711
4712 for (child = die->child; child != NULL; child = child->sibling)
4713 {
4714 struct type *type;
4715 long value;
4716 gdb_byte *bytes;
4717 struct dwarf2_locexpr_baton *baton;
4718 struct value *v;
4719
4720 if (child->tag != DW_TAG_template_type_param
4721 && child->tag != DW_TAG_template_value_param)
4722 continue;
4723
4724 if (first)
4725 {
4726 fputs_unfiltered ("<", buf);
4727 first = 0;
4728 }
4729 else
4730 fputs_unfiltered (", ", buf);
4731
4732 attr = dwarf2_attr (child, DW_AT_type, cu);
4733 if (attr == NULL)
4734 {
4735 complaint (&symfile_complaints,
4736 _("template parameter missing DW_AT_type"));
4737 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4738 continue;
4739 }
4740 type = die_type (child, cu);
4741
4742 if (child->tag == DW_TAG_template_type_param)
4743 {
4744 c_print_type (type, "", buf, -1, 0);
4745 continue;
4746 }
4747
4748 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4749 if (attr == NULL)
4750 {
4751 complaint (&symfile_complaints,
4752 _("template parameter missing DW_AT_const_value"));
4753 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4754 continue;
4755 }
4756
4757 dwarf2_const_value_attr (attr, type, name,
4758 &cu->comp_unit_obstack, cu,
4759 &value, &bytes, &baton);
4760
4761 if (TYPE_NOSIGN (type))
4762 /* GDB prints characters as NUMBER 'CHAR'. If that's
4763 changed, this can use value_print instead. */
4764 c_printchar (value, type, buf);
4765 else
4766 {
4767 struct value_print_options opts;
4768
4769 if (baton != NULL)
4770 v = dwarf2_evaluate_loc_desc (type, NULL,
4771 baton->data,
4772 baton->size,
4773 baton->per_cu);
4774 else if (bytes != NULL)
4775 {
4776 v = allocate_value (type);
4777 memcpy (value_contents_writeable (v), bytes,
4778 TYPE_LENGTH (type));
4779 }
4780 else
4781 v = value_from_longest (type, value);
4782
4783 /* Specify decimal so that we do not depend on the radix. */
4784 get_formatted_print_options (&opts, 'd');
4785 opts.raw = 1;
4786 value_print (v, buf, &opts);
4787 release_value (v);
4788 value_free (v);
4789 }
4790 }
4791
4792 die->building_fullname = 0;
4793
4794 if (!first)
4795 {
4796 /* Close the argument list, with a space if necessary
4797 (nested templates). */
4798 char last_char = '\0';
4799 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4800 if (last_char == '>')
4801 fputs_unfiltered (" >", buf);
4802 else
4803 fputs_unfiltered (">", buf);
4804 }
4805 }
4806
94af9270
KS
4807 /* For Java and C++ methods, append formal parameter type
4808 information, if PHYSNAME. */
6e70227d 4809
94af9270
KS
4810 if (physname && die->tag == DW_TAG_subprogram
4811 && (cu->language == language_cplus
4812 || cu->language == language_java))
4813 {
4814 struct type *type = read_type_die (die, cu);
4815
4816 c_type_print_args (type, buf, 0, cu->language);
4817
4818 if (cu->language == language_java)
4819 {
4820 /* For java, we must append the return type to method
4821 names. */
4822 if (die->tag == DW_TAG_subprogram)
4823 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4824 0, 0);
4825 }
4826 else if (cu->language == language_cplus)
4827 {
60430eff
DJ
4828 /* Assume that an artificial first parameter is
4829 "this", but do not crash if it is not. RealView
4830 marks unnamed (and thus unused) parameters as
4831 artificial; there is no way to differentiate
4832 the two cases. */
94af9270
KS
4833 if (TYPE_NFIELDS (type) > 0
4834 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4835 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4836 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4837 fputs_unfiltered (" const", buf);
4838 }
4839 }
4840
4841 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4842 &length);
4843 ui_file_delete (buf);
4844
4845 if (cu->language == language_cplus)
4846 {
4847 char *cname
4848 = dwarf2_canonicalize_name (name, cu,
4849 &cu->objfile->objfile_obstack);
9a619af0 4850
94af9270
KS
4851 if (cname != NULL)
4852 name = cname;
4853 }
4854 }
4855 }
4856
4857 return name;
4858}
4859
0114d602
DJ
4860/* Return the fully qualified name of DIE, based on its DW_AT_name.
4861 If scope qualifiers are appropriate they will be added. The result
4862 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4863 not have a name. NAME may either be from a previous call to
4864 dwarf2_name or NULL.
4865
4866 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4867
4868static const char *
94af9270 4869dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4870{
94af9270
KS
4871 return dwarf2_compute_name (name, die, cu, 0);
4872}
0114d602 4873
94af9270
KS
4874/* Construct a physname for the given DIE in CU. NAME may either be
4875 from a previous call to dwarf2_name or NULL. The result will be
4876 allocated on the objfile_objstack or NULL if the DIE does not have a
4877 name.
0114d602 4878
94af9270 4879 The output string will be canonicalized (if C++/Java). */
0114d602 4880
94af9270
KS
4881static const char *
4882dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4883{
4884 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4885}
4886
27aa8d6a
SW
4887/* Read the import statement specified by the given die and record it. */
4888
4889static void
4890read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4891{
4892 struct attribute *import_attr;
4893 struct die_info *imported_die;
de4affc9 4894 struct dwarf2_cu *imported_cu;
27aa8d6a 4895 const char *imported_name;
794684b6 4896 const char *imported_name_prefix;
13387711
SW
4897 const char *canonical_name;
4898 const char *import_alias;
4899 const char *imported_declaration = NULL;
794684b6 4900 const char *import_prefix;
13387711
SW
4901
4902 char *temp;
27aa8d6a
SW
4903
4904 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4905 if (import_attr == NULL)
4906 {
4907 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4908 dwarf_tag_name (die->tag));
4909 return;
4910 }
4911
de4affc9
CC
4912 imported_cu = cu;
4913 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4914 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4915 if (imported_name == NULL)
4916 {
4917 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4918
4919 The import in the following code:
4920 namespace A
4921 {
4922 typedef int B;
4923 }
4924
4925 int main ()
4926 {
4927 using A::B;
4928 B b;
4929 return b;
4930 }
4931
4932 ...
4933 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4934 <52> DW_AT_decl_file : 1
4935 <53> DW_AT_decl_line : 6
4936 <54> DW_AT_import : <0x75>
4937 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4938 <59> DW_AT_name : B
4939 <5b> DW_AT_decl_file : 1
4940 <5c> DW_AT_decl_line : 2
4941 <5d> DW_AT_type : <0x6e>
4942 ...
4943 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4944 <76> DW_AT_byte_size : 4
4945 <77> DW_AT_encoding : 5 (signed)
4946
4947 imports the wrong die ( 0x75 instead of 0x58 ).
4948 This case will be ignored until the gcc bug is fixed. */
4949 return;
4950 }
4951
82856980
SW
4952 /* Figure out the local name after import. */
4953 import_alias = dwarf2_name (die, cu);
27aa8d6a 4954
794684b6
SW
4955 /* Figure out where the statement is being imported to. */
4956 import_prefix = determine_prefix (die, cu);
4957
4958 /* Figure out what the scope of the imported die is and prepend it
4959 to the name of the imported die. */
de4affc9 4960 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4961
f55ee35c
JK
4962 if (imported_die->tag != DW_TAG_namespace
4963 && imported_die->tag != DW_TAG_module)
794684b6 4964 {
13387711
SW
4965 imported_declaration = imported_name;
4966 canonical_name = imported_name_prefix;
794684b6 4967 }
13387711 4968 else if (strlen (imported_name_prefix) > 0)
794684b6 4969 {
13387711
SW
4970 temp = alloca (strlen (imported_name_prefix)
4971 + 2 + strlen (imported_name) + 1);
4972 strcpy (temp, imported_name_prefix);
4973 strcat (temp, "::");
4974 strcat (temp, imported_name);
4975 canonical_name = temp;
794684b6 4976 }
13387711
SW
4977 else
4978 canonical_name = imported_name;
794684b6 4979
c0cc3a76
SW
4980 cp_add_using_directive (import_prefix,
4981 canonical_name,
4982 import_alias,
13387711 4983 imported_declaration,
c0cc3a76 4984 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4985}
4986
5fb290d7 4987static void
e142c38c 4988initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4989{
e142c38c 4990 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4991}
4992
cb1df416
DJ
4993static void
4994free_cu_line_header (void *arg)
4995{
4996 struct dwarf2_cu *cu = arg;
4997
4998 free_line_header (cu->line_header);
4999 cu->line_header = NULL;
5000}
5001
9291a0cd
TT
5002static void
5003find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5004 char **name, char **comp_dir)
5005{
5006 struct attribute *attr;
5007
5008 *name = NULL;
5009 *comp_dir = NULL;
5010
5011 /* Find the filename. Do not use dwarf2_name here, since the filename
5012 is not a source language identifier. */
5013 attr = dwarf2_attr (die, DW_AT_name, cu);
5014 if (attr)
5015 {
5016 *name = DW_STRING (attr);
5017 }
5018
5019 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5020 if (attr)
5021 *comp_dir = DW_STRING (attr);
5022 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5023 {
5024 *comp_dir = ldirname (*name);
5025 if (*comp_dir != NULL)
5026 make_cleanup (xfree, *comp_dir);
5027 }
5028 if (*comp_dir != NULL)
5029 {
5030 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5031 directory, get rid of it. */
5032 char *cp = strchr (*comp_dir, ':');
5033
5034 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5035 *comp_dir = cp + 1;
5036 }
5037
5038 if (*name == NULL)
5039 *name = "<unknown>";
5040}
5041
c906108c 5042static void
e7c27a73 5043read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5044{
e7c27a73 5045 struct objfile *objfile = cu->objfile;
debd256d 5046 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5047 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5048 CORE_ADDR highpc = ((CORE_ADDR) 0);
5049 struct attribute *attr;
e1024ff1 5050 char *name = NULL;
c906108c
SS
5051 char *comp_dir = NULL;
5052 struct die_info *child_die;
5053 bfd *abfd = objfile->obfd;
debd256d 5054 struct line_header *line_header = 0;
e142c38c 5055 CORE_ADDR baseaddr;
6e70227d 5056
e142c38c 5057 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5058
fae299cd 5059 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5060
5061 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5062 from finish_block. */
2acceee2 5063 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5064 lowpc = highpc;
5065 lowpc += baseaddr;
5066 highpc += baseaddr;
5067
9291a0cd 5068 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5069
e142c38c 5070 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5071 if (attr)
5072 {
e142c38c 5073 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5074 }
5075
b0f35d58 5076 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5077 if (attr)
b0f35d58 5078 cu->producer = DW_STRING (attr);
303b6f5d 5079
f4b8a18d
KW
5080 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5081 standardised yet. As a workaround for the language detection we fall
5082 back to the DW_AT_producer string. */
5083 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5084 cu->language = language_opencl;
5085
c906108c
SS
5086 /* We assume that we're processing GCC output. */
5087 processing_gcc_compilation = 2;
c906108c 5088
df8a16a1
DJ
5089 processing_has_namespace_info = 0;
5090
c906108c
SS
5091 start_symtab (name, comp_dir, lowpc);
5092 record_debugformat ("DWARF 2");
303b6f5d 5093 record_producer (cu->producer);
c906108c 5094
e142c38c 5095 initialize_cu_func_list (cu);
c906108c 5096
cb1df416
DJ
5097 /* Decode line number information if present. We do this before
5098 processing child DIEs, so that the line header table is available
5099 for DW_AT_decl_file. */
e142c38c 5100 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5101 if (attr)
5102 {
debd256d 5103 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5104 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5105 if (line_header)
5106 {
cb1df416
DJ
5107 cu->line_header = line_header;
5108 make_cleanup (free_cu_line_header, cu);
aaa75496 5109 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5110 }
5fb290d7 5111 }
debd256d 5112
cb1df416
DJ
5113 /* Process all dies in compilation unit. */
5114 if (die->child != NULL)
5115 {
5116 child_die = die->child;
5117 while (child_die && child_die->tag)
5118 {
5119 process_die (child_die, cu);
5120 child_die = sibling_die (child_die);
5121 }
5122 }
5123
2e276125
JB
5124 /* Decode macro information, if present. Dwarf 2 macro information
5125 refers to information in the line number info statement program
5126 header, so we can only read it if we've read the header
5127 successfully. */
e142c38c 5128 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5129 if (attr && line_header)
2e276125
JB
5130 {
5131 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5132
2e276125 5133 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5134 comp_dir, abfd, cu);
2e276125 5135 }
debd256d 5136 do_cleanups (back_to);
5fb290d7
DJ
5137}
5138
348e048f
DE
5139/* For TUs we want to skip the first top level sibling if it's not the
5140 actual type being defined by this TU. In this case the first top
5141 level sibling is there to provide context only. */
5142
5143static void
5144read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5145{
5146 struct objfile *objfile = cu->objfile;
5147 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5148 CORE_ADDR lowpc;
5149 struct attribute *attr;
5150 char *name = NULL;
5151 char *comp_dir = NULL;
5152 struct die_info *child_die;
5153 bfd *abfd = objfile->obfd;
348e048f
DE
5154
5155 /* start_symtab needs a low pc, but we don't really have one.
5156 Do what read_file_scope would do in the absence of such info. */
5157 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5158
5159 /* Find the filename. Do not use dwarf2_name here, since the filename
5160 is not a source language identifier. */
5161 attr = dwarf2_attr (die, DW_AT_name, cu);
5162 if (attr)
5163 name = DW_STRING (attr);
5164
5165 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5166 if (attr)
5167 comp_dir = DW_STRING (attr);
5168 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5169 {
5170 comp_dir = ldirname (name);
5171 if (comp_dir != NULL)
5172 make_cleanup (xfree, comp_dir);
5173 }
5174
5175 if (name == NULL)
5176 name = "<unknown>";
5177
5178 attr = dwarf2_attr (die, DW_AT_language, cu);
5179 if (attr)
5180 set_cu_language (DW_UNSND (attr), cu);
5181
5182 /* This isn't technically needed today. It is done for symmetry
5183 with read_file_scope. */
5184 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5185 if (attr)
348e048f
DE
5186 cu->producer = DW_STRING (attr);
5187
5188 /* We assume that we're processing GCC output. */
5189 processing_gcc_compilation = 2;
5190
5191 processing_has_namespace_info = 0;
5192
5193 start_symtab (name, comp_dir, lowpc);
5194 record_debugformat ("DWARF 2");
5195 record_producer (cu->producer);
5196
5197 /* Process the dies in the type unit. */
5198 if (die->child == NULL)
5199 {
5200 dump_die_for_error (die);
5201 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5202 bfd_get_filename (abfd));
5203 }
5204
5205 child_die = die->child;
5206
5207 while (child_die && child_die->tag)
5208 {
5209 process_die (child_die, cu);
5210
5211 child_die = sibling_die (child_die);
5212 }
5213
5214 do_cleanups (back_to);
5215}
5216
5fb290d7 5217static void
e142c38c
DJ
5218add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5219 struct dwarf2_cu *cu)
5fb290d7
DJ
5220{
5221 struct function_range *thisfn;
5222
5223 thisfn = (struct function_range *)
7b5a2f43 5224 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5225 thisfn->name = name;
5226 thisfn->lowpc = lowpc;
5227 thisfn->highpc = highpc;
5228 thisfn->seen_line = 0;
5229 thisfn->next = NULL;
5230
e142c38c
DJ
5231 if (cu->last_fn == NULL)
5232 cu->first_fn = thisfn;
5fb290d7 5233 else
e142c38c 5234 cu->last_fn->next = thisfn;
5fb290d7 5235
e142c38c 5236 cu->last_fn = thisfn;
c906108c
SS
5237}
5238
d389af10
JK
5239/* qsort helper for inherit_abstract_dies. */
5240
5241static int
5242unsigned_int_compar (const void *ap, const void *bp)
5243{
5244 unsigned int a = *(unsigned int *) ap;
5245 unsigned int b = *(unsigned int *) bp;
5246
5247 return (a > b) - (b > a);
5248}
5249
5250/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5251 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5252 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5253
5254static void
5255inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5256{
5257 struct die_info *child_die;
5258 unsigned die_children_count;
5259 /* CU offsets which were referenced by children of the current DIE. */
5260 unsigned *offsets;
5261 unsigned *offsets_end, *offsetp;
5262 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5263 struct die_info *origin_die;
5264 /* Iterator of the ORIGIN_DIE children. */
5265 struct die_info *origin_child_die;
5266 struct cleanup *cleanups;
5267 struct attribute *attr;
cd02d79d
PA
5268 struct dwarf2_cu *origin_cu;
5269 struct pending **origin_previous_list_in_scope;
d389af10
JK
5270
5271 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5272 if (!attr)
5273 return;
5274
cd02d79d
PA
5275 /* Note that following die references may follow to a die in a
5276 different cu. */
5277
5278 origin_cu = cu;
5279 origin_die = follow_die_ref (die, attr, &origin_cu);
5280
5281 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5282 symbols in. */
5283 origin_previous_list_in_scope = origin_cu->list_in_scope;
5284 origin_cu->list_in_scope = cu->list_in_scope;
5285
edb3359d
DJ
5286 if (die->tag != origin_die->tag
5287 && !(die->tag == DW_TAG_inlined_subroutine
5288 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5289 complaint (&symfile_complaints,
5290 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5291 die->offset, origin_die->offset);
5292
5293 child_die = die->child;
5294 die_children_count = 0;
5295 while (child_die && child_die->tag)
5296 {
5297 child_die = sibling_die (child_die);
5298 die_children_count++;
5299 }
5300 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5301 cleanups = make_cleanup (xfree, offsets);
5302
5303 offsets_end = offsets;
5304 child_die = die->child;
5305 while (child_die && child_die->tag)
5306 {
c38f313d
DJ
5307 /* For each CHILD_DIE, find the corresponding child of
5308 ORIGIN_DIE. If there is more than one layer of
5309 DW_AT_abstract_origin, follow them all; there shouldn't be,
5310 but GCC versions at least through 4.4 generate this (GCC PR
5311 40573). */
5312 struct die_info *child_origin_die = child_die;
cd02d79d 5313 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5314
c38f313d
DJ
5315 while (1)
5316 {
cd02d79d
PA
5317 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5318 child_origin_cu);
c38f313d
DJ
5319 if (attr == NULL)
5320 break;
cd02d79d
PA
5321 child_origin_die = follow_die_ref (child_origin_die, attr,
5322 &child_origin_cu);
c38f313d
DJ
5323 }
5324
d389af10
JK
5325 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5326 counterpart may exist. */
c38f313d 5327 if (child_origin_die != child_die)
d389af10 5328 {
edb3359d
DJ
5329 if (child_die->tag != child_origin_die->tag
5330 && !(child_die->tag == DW_TAG_inlined_subroutine
5331 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5332 complaint (&symfile_complaints,
5333 _("Child DIE 0x%x and its abstract origin 0x%x have "
5334 "different tags"), child_die->offset,
5335 child_origin_die->offset);
c38f313d
DJ
5336 if (child_origin_die->parent != origin_die)
5337 complaint (&symfile_complaints,
5338 _("Child DIE 0x%x and its abstract origin 0x%x have "
5339 "different parents"), child_die->offset,
5340 child_origin_die->offset);
5341 else
5342 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5343 }
5344 child_die = sibling_die (child_die);
5345 }
5346 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5347 unsigned_int_compar);
5348 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5349 if (offsetp[-1] == *offsetp)
5350 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5351 "to DIE 0x%x as their abstract origin"),
5352 die->offset, *offsetp);
5353
5354 offsetp = offsets;
5355 origin_child_die = origin_die->child;
5356 while (origin_child_die && origin_child_die->tag)
5357 {
5358 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5359 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5360 offsetp++;
5361 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5362 {
5363 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5364 process_die (origin_child_die, origin_cu);
d389af10
JK
5365 }
5366 origin_child_die = sibling_die (origin_child_die);
5367 }
cd02d79d 5368 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5369
5370 do_cleanups (cleanups);
5371}
5372
c906108c 5373static void
e7c27a73 5374read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5375{
e7c27a73 5376 struct objfile *objfile = cu->objfile;
52f0bd74 5377 struct context_stack *new;
c906108c
SS
5378 CORE_ADDR lowpc;
5379 CORE_ADDR highpc;
5380 struct die_info *child_die;
edb3359d 5381 struct attribute *attr, *call_line, *call_file;
c906108c 5382 char *name;
e142c38c 5383 CORE_ADDR baseaddr;
801e3a5b 5384 struct block *block;
edb3359d 5385 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5386 VEC (symbolp) *template_args = NULL;
5387 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5388
5389 if (inlined_func)
5390 {
5391 /* If we do not have call site information, we can't show the
5392 caller of this inlined function. That's too confusing, so
5393 only use the scope for local variables. */
5394 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5395 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5396 if (call_line == NULL || call_file == NULL)
5397 {
5398 read_lexical_block_scope (die, cu);
5399 return;
5400 }
5401 }
c906108c 5402
e142c38c
DJ
5403 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5404
94af9270 5405 name = dwarf2_name (die, cu);
c906108c 5406
e8d05480
JB
5407 /* Ignore functions with missing or empty names. These are actually
5408 illegal according to the DWARF standard. */
5409 if (name == NULL)
5410 {
5411 complaint (&symfile_complaints,
5412 _("missing name for subprogram DIE at %d"), die->offset);
5413 return;
5414 }
5415
5416 /* Ignore functions with missing or invalid low and high pc attributes. */
5417 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5418 {
ae4d0c03
PM
5419 attr = dwarf2_attr (die, DW_AT_external, cu);
5420 if (!attr || !DW_UNSND (attr))
5421 complaint (&symfile_complaints,
5422 _("cannot get low and high bounds for subprogram DIE at %d"),
5423 die->offset);
e8d05480
JB
5424 return;
5425 }
c906108c
SS
5426
5427 lowpc += baseaddr;
5428 highpc += baseaddr;
5429
5fb290d7 5430 /* Record the function range for dwarf_decode_lines. */
e142c38c 5431 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5432
34eaf542
TT
5433 /* If we have any template arguments, then we must allocate a
5434 different sort of symbol. */
5435 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5436 {
5437 if (child_die->tag == DW_TAG_template_type_param
5438 || child_die->tag == DW_TAG_template_value_param)
5439 {
5440 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5441 struct template_symbol);
5442 templ_func->base.is_cplus_template_function = 1;
5443 break;
5444 }
5445 }
5446
c906108c 5447 new = push_context (0, lowpc);
34eaf542
TT
5448 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5449 (struct symbol *) templ_func);
4c2df51b 5450
4cecd739
DJ
5451 /* If there is a location expression for DW_AT_frame_base, record
5452 it. */
e142c38c 5453 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5454 if (attr)
c034e007
AC
5455 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5456 expression is being recorded directly in the function's symbol
5457 and not in a separate frame-base object. I guess this hack is
5458 to avoid adding some sort of frame-base adjunct/annex to the
5459 function's symbol :-(. The problem with doing this is that it
5460 results in a function symbol with a location expression that
5461 has nothing to do with the location of the function, ouch! The
5462 relationship should be: a function's symbol has-a frame base; a
5463 frame-base has-a location expression. */
e7c27a73 5464 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5465
e142c38c 5466 cu->list_in_scope = &local_symbols;
c906108c 5467
639d11d3 5468 if (die->child != NULL)
c906108c 5469 {
639d11d3 5470 child_die = die->child;
c906108c
SS
5471 while (child_die && child_die->tag)
5472 {
34eaf542
TT
5473 if (child_die->tag == DW_TAG_template_type_param
5474 || child_die->tag == DW_TAG_template_value_param)
5475 {
5476 struct symbol *arg = new_symbol (child_die, NULL, cu);
5477
f1078f66
DJ
5478 if (arg != NULL)
5479 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5480 }
5481 else
5482 process_die (child_die, cu);
c906108c
SS
5483 child_die = sibling_die (child_die);
5484 }
5485 }
5486
d389af10
JK
5487 inherit_abstract_dies (die, cu);
5488
4a811a97
UW
5489 /* If we have a DW_AT_specification, we might need to import using
5490 directives from the context of the specification DIE. See the
5491 comment in determine_prefix. */
5492 if (cu->language == language_cplus
5493 && dwarf2_attr (die, DW_AT_specification, cu))
5494 {
5495 struct dwarf2_cu *spec_cu = cu;
5496 struct die_info *spec_die = die_specification (die, &spec_cu);
5497
5498 while (spec_die)
5499 {
5500 child_die = spec_die->child;
5501 while (child_die && child_die->tag)
5502 {
5503 if (child_die->tag == DW_TAG_imported_module)
5504 process_die (child_die, spec_cu);
5505 child_die = sibling_die (child_die);
5506 }
5507
5508 /* In some cases, GCC generates specification DIEs that
5509 themselves contain DW_AT_specification attributes. */
5510 spec_die = die_specification (spec_die, &spec_cu);
5511 }
5512 }
5513
c906108c
SS
5514 new = pop_context ();
5515 /* Make a block for the local symbols within. */
801e3a5b
JB
5516 block = finish_block (new->name, &local_symbols, new->old_blocks,
5517 lowpc, highpc, objfile);
5518
df8a16a1 5519 /* For C++, set the block's scope. */
f55ee35c 5520 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5521 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5522 determine_prefix (die, cu),
df8a16a1
DJ
5523 processing_has_namespace_info);
5524
801e3a5b
JB
5525 /* If we have address ranges, record them. */
5526 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5527
34eaf542
TT
5528 /* Attach template arguments to function. */
5529 if (! VEC_empty (symbolp, template_args))
5530 {
5531 gdb_assert (templ_func != NULL);
5532
5533 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5534 templ_func->template_arguments
5535 = obstack_alloc (&objfile->objfile_obstack,
5536 (templ_func->n_template_arguments
5537 * sizeof (struct symbol *)));
5538 memcpy (templ_func->template_arguments,
5539 VEC_address (symbolp, template_args),
5540 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5541 VEC_free (symbolp, template_args);
5542 }
5543
208d8187
JB
5544 /* In C++, we can have functions nested inside functions (e.g., when
5545 a function declares a class that has methods). This means that
5546 when we finish processing a function scope, we may need to go
5547 back to building a containing block's symbol lists. */
5548 local_symbols = new->locals;
5549 param_symbols = new->params;
27aa8d6a 5550 using_directives = new->using_directives;
208d8187 5551
921e78cf
JB
5552 /* If we've finished processing a top-level function, subsequent
5553 symbols go in the file symbol list. */
5554 if (outermost_context_p ())
e142c38c 5555 cu->list_in_scope = &file_symbols;
c906108c
SS
5556}
5557
5558/* Process all the DIES contained within a lexical block scope. Start
5559 a new scope, process the dies, and then close the scope. */
5560
5561static void
e7c27a73 5562read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5563{
e7c27a73 5564 struct objfile *objfile = cu->objfile;
52f0bd74 5565 struct context_stack *new;
c906108c
SS
5566 CORE_ADDR lowpc, highpc;
5567 struct die_info *child_die;
e142c38c
DJ
5568 CORE_ADDR baseaddr;
5569
5570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5571
5572 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5573 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5574 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5575 be nasty. Might be easier to properly extend generic blocks to
af34e669 5576 describe ranges. */
d85a05f0 5577 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5578 return;
5579 lowpc += baseaddr;
5580 highpc += baseaddr;
5581
5582 push_context (0, lowpc);
639d11d3 5583 if (die->child != NULL)
c906108c 5584 {
639d11d3 5585 child_die = die->child;
c906108c
SS
5586 while (child_die && child_die->tag)
5587 {
e7c27a73 5588 process_die (child_die, cu);
c906108c
SS
5589 child_die = sibling_die (child_die);
5590 }
5591 }
5592 new = pop_context ();
5593
8540c487 5594 if (local_symbols != NULL || using_directives != NULL)
c906108c 5595 {
801e3a5b
JB
5596 struct block *block
5597 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5598 highpc, objfile);
5599
5600 /* Note that recording ranges after traversing children, as we
5601 do here, means that recording a parent's ranges entails
5602 walking across all its children's ranges as they appear in
5603 the address map, which is quadratic behavior.
5604
5605 It would be nicer to record the parent's ranges before
5606 traversing its children, simply overriding whatever you find
5607 there. But since we don't even decide whether to create a
5608 block until after we've traversed its children, that's hard
5609 to do. */
5610 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5611 }
5612 local_symbols = new->locals;
27aa8d6a 5613 using_directives = new->using_directives;
c906108c
SS
5614}
5615
43039443 5616/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5617 Return 1 if the attributes are present and valid, otherwise, return 0.
5618 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5619
5620static int
5621dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5622 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5623 struct partial_symtab *ranges_pst)
43039443
JK
5624{
5625 struct objfile *objfile = cu->objfile;
5626 struct comp_unit_head *cu_header = &cu->header;
5627 bfd *obfd = objfile->obfd;
5628 unsigned int addr_size = cu_header->addr_size;
5629 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5630 /* Base address selection entry. */
5631 CORE_ADDR base;
5632 int found_base;
5633 unsigned int dummy;
5634 gdb_byte *buffer;
5635 CORE_ADDR marker;
5636 int low_set;
5637 CORE_ADDR low = 0;
5638 CORE_ADDR high = 0;
ff013f42 5639 CORE_ADDR baseaddr;
43039443 5640
d00adf39
DE
5641 found_base = cu->base_known;
5642 base = cu->base_address;
43039443 5643
be391dca 5644 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5645 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5646 {
5647 complaint (&symfile_complaints,
5648 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5649 offset);
5650 return 0;
5651 }
dce234bc 5652 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5653
5654 /* Read in the largest possible address. */
5655 marker = read_address (obfd, buffer, cu, &dummy);
5656 if ((marker & mask) == mask)
5657 {
5658 /* If we found the largest possible address, then
5659 read the base address. */
5660 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5661 buffer += 2 * addr_size;
5662 offset += 2 * addr_size;
5663 found_base = 1;
5664 }
5665
5666 low_set = 0;
5667
e7030f15 5668 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5669
43039443
JK
5670 while (1)
5671 {
5672 CORE_ADDR range_beginning, range_end;
5673
5674 range_beginning = read_address (obfd, buffer, cu, &dummy);
5675 buffer += addr_size;
5676 range_end = read_address (obfd, buffer, cu, &dummy);
5677 buffer += addr_size;
5678 offset += 2 * addr_size;
5679
5680 /* An end of list marker is a pair of zero addresses. */
5681 if (range_beginning == 0 && range_end == 0)
5682 /* Found the end of list entry. */
5683 break;
5684
5685 /* Each base address selection entry is a pair of 2 values.
5686 The first is the largest possible address, the second is
5687 the base address. Check for a base address here. */
5688 if ((range_beginning & mask) == mask)
5689 {
5690 /* If we found the largest possible address, then
5691 read the base address. */
5692 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5693 found_base = 1;
5694 continue;
5695 }
5696
5697 if (!found_base)
5698 {
5699 /* We have no valid base address for the ranges
5700 data. */
5701 complaint (&symfile_complaints,
5702 _("Invalid .debug_ranges data (no base address)"));
5703 return 0;
5704 }
5705
5706 range_beginning += base;
5707 range_end += base;
5708
ff013f42
JK
5709 if (ranges_pst != NULL && range_beginning < range_end)
5710 addrmap_set_empty (objfile->psymtabs_addrmap,
5711 range_beginning + baseaddr, range_end - 1 + baseaddr,
5712 ranges_pst);
5713
43039443
JK
5714 /* FIXME: This is recording everything as a low-high
5715 segment of consecutive addresses. We should have a
5716 data structure for discontiguous block ranges
5717 instead. */
5718 if (! low_set)
5719 {
5720 low = range_beginning;
5721 high = range_end;
5722 low_set = 1;
5723 }
5724 else
5725 {
5726 if (range_beginning < low)
5727 low = range_beginning;
5728 if (range_end > high)
5729 high = range_end;
5730 }
5731 }
5732
5733 if (! low_set)
5734 /* If the first entry is an end-of-list marker, the range
5735 describes an empty scope, i.e. no instructions. */
5736 return 0;
5737
5738 if (low_return)
5739 *low_return = low;
5740 if (high_return)
5741 *high_return = high;
5742 return 1;
5743}
5744
af34e669
DJ
5745/* Get low and high pc attributes from a die. Return 1 if the attributes
5746 are present and valid, otherwise, return 0. Return -1 if the range is
5747 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5748static int
af34e669 5749dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5750 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5751 struct partial_symtab *pst)
c906108c
SS
5752{
5753 struct attribute *attr;
af34e669
DJ
5754 CORE_ADDR low = 0;
5755 CORE_ADDR high = 0;
5756 int ret = 0;
c906108c 5757
e142c38c 5758 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5759 if (attr)
af34e669
DJ
5760 {
5761 high = DW_ADDR (attr);
e142c38c 5762 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5763 if (attr)
5764 low = DW_ADDR (attr);
5765 else
5766 /* Found high w/o low attribute. */
5767 return 0;
5768
5769 /* Found consecutive range of addresses. */
5770 ret = 1;
5771 }
c906108c 5772 else
af34e669 5773 {
e142c38c 5774 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5775 if (attr != NULL)
5776 {
af34e669 5777 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5778 .debug_ranges section. */
d85a05f0 5779 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5780 return 0;
43039443 5781 /* Found discontinuous range of addresses. */
af34e669
DJ
5782 ret = -1;
5783 }
5784 }
c906108c
SS
5785
5786 if (high < low)
5787 return 0;
5788
5789 /* When using the GNU linker, .gnu.linkonce. sections are used to
5790 eliminate duplicate copies of functions and vtables and such.
5791 The linker will arbitrarily choose one and discard the others.
5792 The AT_*_pc values for such functions refer to local labels in
5793 these sections. If the section from that file was discarded, the
5794 labels are not in the output, so the relocs get a value of 0.
5795 If this is a discarded function, mark the pc bounds as invalid,
5796 so that GDB will ignore it. */
72dca2f5 5797 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5798 return 0;
5799
5800 *lowpc = low;
5801 *highpc = high;
af34e669 5802 return ret;
c906108c
SS
5803}
5804
b084d499
JB
5805/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5806 its low and high PC addresses. Do nothing if these addresses could not
5807 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5808 and HIGHPC to the high address if greater than HIGHPC. */
5809
5810static void
5811dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5812 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5813 struct dwarf2_cu *cu)
5814{
5815 CORE_ADDR low, high;
5816 struct die_info *child = die->child;
5817
d85a05f0 5818 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5819 {
5820 *lowpc = min (*lowpc, low);
5821 *highpc = max (*highpc, high);
5822 }
5823
5824 /* If the language does not allow nested subprograms (either inside
5825 subprograms or lexical blocks), we're done. */
5826 if (cu->language != language_ada)
5827 return;
6e70227d 5828
b084d499
JB
5829 /* Check all the children of the given DIE. If it contains nested
5830 subprograms, then check their pc bounds. Likewise, we need to
5831 check lexical blocks as well, as they may also contain subprogram
5832 definitions. */
5833 while (child && child->tag)
5834 {
5835 if (child->tag == DW_TAG_subprogram
5836 || child->tag == DW_TAG_lexical_block)
5837 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5838 child = sibling_die (child);
5839 }
5840}
5841
fae299cd
DC
5842/* Get the low and high pc's represented by the scope DIE, and store
5843 them in *LOWPC and *HIGHPC. If the correct values can't be
5844 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5845
5846static void
5847get_scope_pc_bounds (struct die_info *die,
5848 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5849 struct dwarf2_cu *cu)
5850{
5851 CORE_ADDR best_low = (CORE_ADDR) -1;
5852 CORE_ADDR best_high = (CORE_ADDR) 0;
5853 CORE_ADDR current_low, current_high;
5854
d85a05f0 5855 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5856 {
5857 best_low = current_low;
5858 best_high = current_high;
5859 }
5860 else
5861 {
5862 struct die_info *child = die->child;
5863
5864 while (child && child->tag)
5865 {
5866 switch (child->tag) {
5867 case DW_TAG_subprogram:
b084d499 5868 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5869 break;
5870 case DW_TAG_namespace:
f55ee35c 5871 case DW_TAG_module:
fae299cd
DC
5872 /* FIXME: carlton/2004-01-16: Should we do this for
5873 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5874 that current GCC's always emit the DIEs corresponding
5875 to definitions of methods of classes as children of a
5876 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5877 the DIEs giving the declarations, which could be
5878 anywhere). But I don't see any reason why the
5879 standards says that they have to be there. */
5880 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5881
5882 if (current_low != ((CORE_ADDR) -1))
5883 {
5884 best_low = min (best_low, current_low);
5885 best_high = max (best_high, current_high);
5886 }
5887 break;
5888 default:
5889 /* Ignore. */
5890 break;
5891 }
5892
5893 child = sibling_die (child);
5894 }
5895 }
5896
5897 *lowpc = best_low;
5898 *highpc = best_high;
5899}
5900
801e3a5b
JB
5901/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5902 in DIE. */
5903static void
5904dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5905 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5906{
5907 struct attribute *attr;
5908
5909 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5910 if (attr)
5911 {
5912 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5913
801e3a5b
JB
5914 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5915 if (attr)
5916 {
5917 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5918
801e3a5b
JB
5919 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5920 }
5921 }
5922
5923 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5924 if (attr)
5925 {
5926 bfd *obfd = cu->objfile->obfd;
5927
5928 /* The value of the DW_AT_ranges attribute is the offset of the
5929 address range list in the .debug_ranges section. */
5930 unsigned long offset = DW_UNSND (attr);
dce234bc 5931 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5932
5933 /* For some target architectures, but not others, the
5934 read_address function sign-extends the addresses it returns.
5935 To recognize base address selection entries, we need a
5936 mask. */
5937 unsigned int addr_size = cu->header.addr_size;
5938 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5939
5940 /* The base address, to which the next pair is relative. Note
5941 that this 'base' is a DWARF concept: most entries in a range
5942 list are relative, to reduce the number of relocs against the
5943 debugging information. This is separate from this function's
5944 'baseaddr' argument, which GDB uses to relocate debugging
5945 information from a shared library based on the address at
5946 which the library was loaded. */
d00adf39
DE
5947 CORE_ADDR base = cu->base_address;
5948 int base_known = cu->base_known;
801e3a5b 5949
be391dca 5950 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5951 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5952 {
5953 complaint (&symfile_complaints,
5954 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5955 offset);
5956 return;
5957 }
5958
5959 for (;;)
5960 {
5961 unsigned int bytes_read;
5962 CORE_ADDR start, end;
5963
5964 start = read_address (obfd, buffer, cu, &bytes_read);
5965 buffer += bytes_read;
5966 end = read_address (obfd, buffer, cu, &bytes_read);
5967 buffer += bytes_read;
5968
5969 /* Did we find the end of the range list? */
5970 if (start == 0 && end == 0)
5971 break;
5972
5973 /* Did we find a base address selection entry? */
5974 else if ((start & base_select_mask) == base_select_mask)
5975 {
5976 base = end;
5977 base_known = 1;
5978 }
5979
5980 /* We found an ordinary address range. */
5981 else
5982 {
5983 if (!base_known)
5984 {
5985 complaint (&symfile_complaints,
5986 _("Invalid .debug_ranges data (no base address)"));
5987 return;
5988 }
5989
6e70227d
DE
5990 record_block_range (block,
5991 baseaddr + base + start,
801e3a5b
JB
5992 baseaddr + base + end - 1);
5993 }
5994 }
5995 }
5996}
5997
c906108c
SS
5998/* Add an aggregate field to the field list. */
5999
6000static void
107d2387 6001dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6002 struct dwarf2_cu *cu)
6e70227d 6003{
e7c27a73 6004 struct objfile *objfile = cu->objfile;
5e2b427d 6005 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6006 struct nextfield *new_field;
6007 struct attribute *attr;
6008 struct field *fp;
6009 char *fieldname = "";
6010
6011 /* Allocate a new field list entry and link it in. */
6012 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6013 make_cleanup (xfree, new_field);
c906108c 6014 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6015
6016 if (die->tag == DW_TAG_inheritance)
6017 {
6018 new_field->next = fip->baseclasses;
6019 fip->baseclasses = new_field;
6020 }
6021 else
6022 {
6023 new_field->next = fip->fields;
6024 fip->fields = new_field;
6025 }
c906108c
SS
6026 fip->nfields++;
6027
6028 /* Handle accessibility and virtuality of field.
6029 The default accessibility for members is public, the default
6030 accessibility for inheritance is private. */
6031 if (die->tag != DW_TAG_inheritance)
6032 new_field->accessibility = DW_ACCESS_public;
6033 else
6034 new_field->accessibility = DW_ACCESS_private;
6035 new_field->virtuality = DW_VIRTUALITY_none;
6036
e142c38c 6037 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6038 if (attr)
6039 new_field->accessibility = DW_UNSND (attr);
6040 if (new_field->accessibility != DW_ACCESS_public)
6041 fip->non_public_fields = 1;
e142c38c 6042 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6043 if (attr)
6044 new_field->virtuality = DW_UNSND (attr);
6045
6046 fp = &new_field->field;
a9a9bd0f 6047
e142c38c 6048 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6049 {
a9a9bd0f 6050 /* Data member other than a C++ static data member. */
6e70227d 6051
c906108c 6052 /* Get type of field. */
e7c27a73 6053 fp->type = die_type (die, cu);
c906108c 6054
d6a843b5 6055 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6056
c906108c 6057 /* Get bit size of field (zero if none). */
e142c38c 6058 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6059 if (attr)
6060 {
6061 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6062 }
6063 else
6064 {
6065 FIELD_BITSIZE (*fp) = 0;
6066 }
6067
6068 /* Get bit offset of field. */
e142c38c 6069 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6070 if (attr)
6071 {
d4b96c9a 6072 int byte_offset = 0;
c6a0999f 6073
3690dd37 6074 if (attr_form_is_section_offset (attr))
d4b96c9a 6075 dwarf2_complex_location_expr_complaint ();
3690dd37 6076 else if (attr_form_is_constant (attr))
c6a0999f 6077 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6078 else if (attr_form_is_block (attr))
c6a0999f 6079 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6080 else
6081 dwarf2_complex_location_expr_complaint ();
c6a0999f 6082
d6a843b5 6083 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6084 }
e142c38c 6085 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6086 if (attr)
6087 {
5e2b427d 6088 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6089 {
6090 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6091 additional bit offset from the MSB of the containing
6092 anonymous object to the MSB of the field. We don't
6093 have to do anything special since we don't need to
6094 know the size of the anonymous object. */
c906108c
SS
6095 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6096 }
6097 else
6098 {
6099 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6100 MSB of the anonymous object, subtract off the number of
6101 bits from the MSB of the field to the MSB of the
6102 object, and then subtract off the number of bits of
6103 the field itself. The result is the bit offset of
6104 the LSB of the field. */
c906108c
SS
6105 int anonymous_size;
6106 int bit_offset = DW_UNSND (attr);
6107
e142c38c 6108 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6109 if (attr)
6110 {
6111 /* The size of the anonymous object containing
6112 the bit field is explicit, so use the
6113 indicated size (in bytes). */
6114 anonymous_size = DW_UNSND (attr);
6115 }
6116 else
6117 {
6118 /* The size of the anonymous object containing
6119 the bit field must be inferred from the type
6120 attribute of the data member containing the
6121 bit field. */
6122 anonymous_size = TYPE_LENGTH (fp->type);
6123 }
6124 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6125 - bit_offset - FIELD_BITSIZE (*fp);
6126 }
6127 }
6128
6129 /* Get name of field. */
39cbfefa
DJ
6130 fieldname = dwarf2_name (die, cu);
6131 if (fieldname == NULL)
6132 fieldname = "";
d8151005
DJ
6133
6134 /* The name is already allocated along with this objfile, so we don't
6135 need to duplicate it for the type. */
6136 fp->name = fieldname;
c906108c
SS
6137
6138 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6139 pointer or virtual base class pointer) to private. */
e142c38c 6140 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6141 {
d48cc9dd 6142 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6143 new_field->accessibility = DW_ACCESS_private;
6144 fip->non_public_fields = 1;
6145 }
6146 }
a9a9bd0f 6147 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6148 {
a9a9bd0f
DC
6149 /* C++ static member. */
6150
6151 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6152 is a declaration, but all versions of G++ as of this writing
6153 (so through at least 3.2.1) incorrectly generate
6154 DW_TAG_variable tags. */
6e70227d 6155
c906108c 6156 char *physname;
c906108c 6157
a9a9bd0f 6158 /* Get name of field. */
39cbfefa
DJ
6159 fieldname = dwarf2_name (die, cu);
6160 if (fieldname == NULL)
c906108c
SS
6161 return;
6162
254e6b9e 6163 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6164 if (attr
6165 /* Only create a symbol if this is an external value.
6166 new_symbol checks this and puts the value in the global symbol
6167 table, which we want. If it is not external, new_symbol
6168 will try to put the value in cu->list_in_scope which is wrong. */
6169 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6170 {
6171 /* A static const member, not much different than an enum as far as
6172 we're concerned, except that we can support more types. */
6173 new_symbol (die, NULL, cu);
6174 }
6175
2df3850c 6176 /* Get physical name. */
94af9270 6177 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6178
d8151005
DJ
6179 /* The name is already allocated along with this objfile, so we don't
6180 need to duplicate it for the type. */
6181 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6182 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6183 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6184 }
6185 else if (die->tag == DW_TAG_inheritance)
6186 {
6187 /* C++ base class field. */
e142c38c 6188 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6189 if (attr)
d4b96c9a
JK
6190 {
6191 int byte_offset = 0;
6192
6193 if (attr_form_is_section_offset (attr))
6194 dwarf2_complex_location_expr_complaint ();
6195 else if (attr_form_is_constant (attr))
6196 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6197 else if (attr_form_is_block (attr))
6198 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6199 else
6200 dwarf2_complex_location_expr_complaint ();
6201
6202 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6203 }
c906108c 6204 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6205 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6206 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6207 fip->nbaseclasses++;
6208 }
6209}
6210
98751a41
JK
6211/* Add a typedef defined in the scope of the FIP's class. */
6212
6213static void
6214dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6215 struct dwarf2_cu *cu)
6e70227d 6216{
98751a41
JK
6217 struct objfile *objfile = cu->objfile;
6218 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6219 struct typedef_field_list *new_field;
6220 struct attribute *attr;
6221 struct typedef_field *fp;
6222 char *fieldname = "";
6223
6224 /* Allocate a new field list entry and link it in. */
6225 new_field = xzalloc (sizeof (*new_field));
6226 make_cleanup (xfree, new_field);
6227
6228 gdb_assert (die->tag == DW_TAG_typedef);
6229
6230 fp = &new_field->field;
6231
6232 /* Get name of field. */
6233 fp->name = dwarf2_name (die, cu);
6234 if (fp->name == NULL)
6235 return;
6236
6237 fp->type = read_type_die (die, cu);
6238
6239 new_field->next = fip->typedef_field_list;
6240 fip->typedef_field_list = new_field;
6241 fip->typedef_field_list_count++;
6242}
6243
c906108c
SS
6244/* Create the vector of fields, and attach it to the type. */
6245
6246static void
fba45db2 6247dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6248 struct dwarf2_cu *cu)
c906108c
SS
6249{
6250 int nfields = fip->nfields;
6251
6252 /* Record the field count, allocate space for the array of fields,
6253 and create blank accessibility bitfields if necessary. */
6254 TYPE_NFIELDS (type) = nfields;
6255 TYPE_FIELDS (type) = (struct field *)
6256 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6257 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6258
b4ba55a1 6259 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6260 {
6261 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6262
6263 TYPE_FIELD_PRIVATE_BITS (type) =
6264 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6265 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6266
6267 TYPE_FIELD_PROTECTED_BITS (type) =
6268 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6269 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6270
774b6a14
TT
6271 TYPE_FIELD_IGNORE_BITS (type) =
6272 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6273 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6274 }
6275
6276 /* If the type has baseclasses, allocate and clear a bit vector for
6277 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6278 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6279 {
6280 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6281 unsigned char *pointer;
c906108c
SS
6282
6283 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6284 pointer = TYPE_ALLOC (type, num_bytes);
6285 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6286 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6287 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6288 }
6289
6290 /* Copy the saved-up fields into the field vector. Start from the head
6291 of the list, adding to the tail of the field array, so that they end
6292 up in the same order in the array in which they were added to the list. */
6293 while (nfields-- > 0)
6294 {
7d0ccb61
DJ
6295 struct nextfield *fieldp;
6296
6297 if (fip->fields)
6298 {
6299 fieldp = fip->fields;
6300 fip->fields = fieldp->next;
6301 }
6302 else
6303 {
6304 fieldp = fip->baseclasses;
6305 fip->baseclasses = fieldp->next;
6306 }
6307
6308 TYPE_FIELD (type, nfields) = fieldp->field;
6309 switch (fieldp->accessibility)
c906108c 6310 {
c5aa993b 6311 case DW_ACCESS_private:
b4ba55a1
JB
6312 if (cu->language != language_ada)
6313 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6314 break;
c906108c 6315
c5aa993b 6316 case DW_ACCESS_protected:
b4ba55a1
JB
6317 if (cu->language != language_ada)
6318 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6319 break;
c906108c 6320
c5aa993b
JM
6321 case DW_ACCESS_public:
6322 break;
c906108c 6323
c5aa993b
JM
6324 default:
6325 /* Unknown accessibility. Complain and treat it as public. */
6326 {
e2e0b3e5 6327 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6328 fieldp->accessibility);
c5aa993b
JM
6329 }
6330 break;
c906108c
SS
6331 }
6332 if (nfields < fip->nbaseclasses)
6333 {
7d0ccb61 6334 switch (fieldp->virtuality)
c906108c 6335 {
c5aa993b
JM
6336 case DW_VIRTUALITY_virtual:
6337 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6338 if (cu->language == language_ada)
6339 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6340 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6341 break;
c906108c
SS
6342 }
6343 }
c906108c
SS
6344 }
6345}
6346
c906108c
SS
6347/* Add a member function to the proper fieldlist. */
6348
6349static void
107d2387 6350dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6351 struct type *type, struct dwarf2_cu *cu)
c906108c 6352{
e7c27a73 6353 struct objfile *objfile = cu->objfile;
c906108c
SS
6354 struct attribute *attr;
6355 struct fnfieldlist *flp;
6356 int i;
6357 struct fn_field *fnp;
6358 char *fieldname;
c906108c 6359 struct nextfnfield *new_fnfield;
f792889a 6360 struct type *this_type;
c906108c 6361
b4ba55a1
JB
6362 if (cu->language == language_ada)
6363 error ("unexpected member function in Ada type");
6364
2df3850c 6365 /* Get name of member function. */
39cbfefa
DJ
6366 fieldname = dwarf2_name (die, cu);
6367 if (fieldname == NULL)
2df3850c 6368 return;
c906108c 6369
c906108c
SS
6370 /* Look up member function name in fieldlist. */
6371 for (i = 0; i < fip->nfnfields; i++)
6372 {
27bfe10e 6373 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6374 break;
6375 }
6376
6377 /* Create new list element if necessary. */
6378 if (i < fip->nfnfields)
6379 flp = &fip->fnfieldlists[i];
6380 else
6381 {
6382 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6383 {
6384 fip->fnfieldlists = (struct fnfieldlist *)
6385 xrealloc (fip->fnfieldlists,
6386 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6387 * sizeof (struct fnfieldlist));
c906108c 6388 if (fip->nfnfields == 0)
c13c43fd 6389 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6390 }
6391 flp = &fip->fnfieldlists[fip->nfnfields];
6392 flp->name = fieldname;
6393 flp->length = 0;
6394 flp->head = NULL;
3da10d80 6395 i = fip->nfnfields++;
c906108c
SS
6396 }
6397
6398 /* Create a new member function field and chain it to the field list
6399 entry. */
6400 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6401 make_cleanup (xfree, new_fnfield);
c906108c
SS
6402 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6403 new_fnfield->next = flp->head;
6404 flp->head = new_fnfield;
6405 flp->length++;
6406
6407 /* Fill in the member function field info. */
6408 fnp = &new_fnfield->fnfield;
3da10d80
KS
6409
6410 /* Delay processing of the physname until later. */
6411 if (cu->language == language_cplus || cu->language == language_java)
6412 {
6413 add_to_method_list (type, i, flp->length - 1, fieldname,
6414 die, cu);
6415 }
6416 else
6417 {
6418 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6419 fnp->physname = physname ? physname : "";
6420 }
6421
c906108c 6422 fnp->type = alloc_type (objfile);
f792889a
DJ
6423 this_type = read_type_die (die, cu);
6424 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6425 {
f792889a 6426 int nparams = TYPE_NFIELDS (this_type);
c906108c 6427
f792889a 6428 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6429 of the method itself (TYPE_CODE_METHOD). */
6430 smash_to_method_type (fnp->type, type,
f792889a
DJ
6431 TYPE_TARGET_TYPE (this_type),
6432 TYPE_FIELDS (this_type),
6433 TYPE_NFIELDS (this_type),
6434 TYPE_VARARGS (this_type));
c906108c
SS
6435
6436 /* Handle static member functions.
c5aa993b
JM
6437 Dwarf2 has no clean way to discern C++ static and non-static
6438 member functions. G++ helps GDB by marking the first
6439 parameter for non-static member functions (which is the
6440 this pointer) as artificial. We obtain this information
6441 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6442 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6443 fnp->voffset = VOFFSET_STATIC;
6444 }
6445 else
e2e0b3e5 6446 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6447 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6448
6449 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6450 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6451 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6452
6453 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6454 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6455
6456 /* Get accessibility. */
e142c38c 6457 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6458 if (attr)
6459 {
6460 switch (DW_UNSND (attr))
6461 {
c5aa993b
JM
6462 case DW_ACCESS_private:
6463 fnp->is_private = 1;
6464 break;
6465 case DW_ACCESS_protected:
6466 fnp->is_protected = 1;
6467 break;
c906108c
SS
6468 }
6469 }
6470
b02dede2 6471 /* Check for artificial methods. */
e142c38c 6472 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6473 if (attr && DW_UNSND (attr) != 0)
6474 fnp->is_artificial = 1;
6475
0d564a31 6476 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6477 function. For older versions of GCC, this is an offset in the
6478 appropriate virtual table, as specified by DW_AT_containing_type.
6479 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6480 to the object address. */
6481
e142c38c 6482 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6483 if (attr)
8e19ed76 6484 {
aec5aa8b 6485 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6486 {
aec5aa8b
TT
6487 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6488 {
6489 /* Old-style GCC. */
6490 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6491 }
6492 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6493 || (DW_BLOCK (attr)->size > 1
6494 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6495 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6496 {
6497 struct dwarf_block blk;
6498 int offset;
6499
6500 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6501 ? 1 : 2);
6502 blk.size = DW_BLOCK (attr)->size - offset;
6503 blk.data = DW_BLOCK (attr)->data + offset;
6504 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6505 if ((fnp->voffset % cu->header.addr_size) != 0)
6506 dwarf2_complex_location_expr_complaint ();
6507 else
6508 fnp->voffset /= cu->header.addr_size;
6509 fnp->voffset += 2;
6510 }
6511 else
6512 dwarf2_complex_location_expr_complaint ();
6513
6514 if (!fnp->fcontext)
6515 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6516 }
3690dd37 6517 else if (attr_form_is_section_offset (attr))
8e19ed76 6518 {
4d3c2250 6519 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6520 }
6521 else
6522 {
4d3c2250
KB
6523 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6524 fieldname);
8e19ed76 6525 }
0d564a31 6526 }
d48cc9dd
DJ
6527 else
6528 {
6529 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6530 if (attr && DW_UNSND (attr))
6531 {
6532 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6533 complaint (&symfile_complaints,
6534 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6535 fieldname, die->offset);
9655fd1a 6536 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6537 TYPE_CPLUS_DYNAMIC (type) = 1;
6538 }
6539 }
c906108c
SS
6540}
6541
6542/* Create the vector of member function fields, and attach it to the type. */
6543
6544static void
fba45db2 6545dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6546 struct dwarf2_cu *cu)
c906108c
SS
6547{
6548 struct fnfieldlist *flp;
6549 int total_length = 0;
6550 int i;
6551
b4ba55a1
JB
6552 if (cu->language == language_ada)
6553 error ("unexpected member functions in Ada type");
6554
c906108c
SS
6555 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6556 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6557 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6558
6559 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6560 {
6561 struct nextfnfield *nfp = flp->head;
6562 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6563 int k;
6564
6565 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6566 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6567 fn_flp->fn_fields = (struct fn_field *)
6568 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6569 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6570 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6571
6572 total_length += flp->length;
6573 }
6574
6575 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6576 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6577}
6578
1168df01
JB
6579/* Returns non-zero if NAME is the name of a vtable member in CU's
6580 language, zero otherwise. */
6581static int
6582is_vtable_name (const char *name, struct dwarf2_cu *cu)
6583{
6584 static const char vptr[] = "_vptr";
987504bb 6585 static const char vtable[] = "vtable";
1168df01 6586
987504bb
JJ
6587 /* Look for the C++ and Java forms of the vtable. */
6588 if ((cu->language == language_java
6589 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6590 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6591 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6592 return 1;
6593
6594 return 0;
6595}
6596
c0dd20ea 6597/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6598 functions, with the ABI-specified layout. If TYPE describes
6599 such a structure, smash it into a member function type.
61049d3b
DJ
6600
6601 GCC shouldn't do this; it should just output pointer to member DIEs.
6602 This is GCC PR debug/28767. */
c0dd20ea 6603
0b92b5bb
TT
6604static void
6605quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6606{
0b92b5bb 6607 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6608
6609 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6610 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6611 return;
c0dd20ea
DJ
6612
6613 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6614 if (TYPE_FIELD_NAME (type, 0) == NULL
6615 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6616 || TYPE_FIELD_NAME (type, 1) == NULL
6617 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6618 return;
c0dd20ea
DJ
6619
6620 /* Find the type of the method. */
0b92b5bb 6621 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6622 if (pfn_type == NULL
6623 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6624 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6625 return;
c0dd20ea
DJ
6626
6627 /* Look for the "this" argument. */
6628 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6629 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6630 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6631 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6632 return;
c0dd20ea
DJ
6633
6634 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6635 new_type = alloc_type (objfile);
6636 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6637 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6638 TYPE_VARARGS (pfn_type));
0b92b5bb 6639 smash_to_methodptr_type (type, new_type);
c0dd20ea 6640}
1168df01 6641
c906108c 6642/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6643 (definition) to create a type for the structure or union. Fill in
6644 the type's name and general properties; the members will not be
6645 processed until process_structure_type.
c906108c 6646
c767944b
DJ
6647 NOTE: we need to call these functions regardless of whether or not the
6648 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6649 structure or union. This gets the type entered into our set of
6650 user defined types.
6651
6652 However, if the structure is incomplete (an opaque struct/union)
6653 then suppress creating a symbol table entry for it since gdb only
6654 wants to find the one with the complete definition. Note that if
6655 it is complete, we just call new_symbol, which does it's own
6656 checking about whether the struct/union is anonymous or not (and
6657 suppresses creating a symbol table entry itself). */
6658
f792889a 6659static struct type *
134d01f1 6660read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6661{
e7c27a73 6662 struct objfile *objfile = cu->objfile;
c906108c
SS
6663 struct type *type;
6664 struct attribute *attr;
39cbfefa 6665 char *name;
c906108c 6666
348e048f
DE
6667 /* If the definition of this type lives in .debug_types, read that type.
6668 Don't follow DW_AT_specification though, that will take us back up
6669 the chain and we want to go down. */
6670 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6671 if (attr)
6672 {
6673 struct dwarf2_cu *type_cu = cu;
6674 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6675
348e048f
DE
6676 /* We could just recurse on read_structure_type, but we need to call
6677 get_die_type to ensure only one type for this DIE is created.
6678 This is important, for example, because for c++ classes we need
6679 TYPE_NAME set which is only done by new_symbol. Blech. */
6680 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6681
6682 /* TYPE_CU may not be the same as CU.
6683 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6684 return set_die_type (die, type, cu);
6685 }
6686
c0dd20ea 6687 type = alloc_type (objfile);
c906108c 6688 INIT_CPLUS_SPECIFIC (type);
93311388 6689
39cbfefa
DJ
6690 name = dwarf2_name (die, cu);
6691 if (name != NULL)
c906108c 6692 {
987504bb
JJ
6693 if (cu->language == language_cplus
6694 || cu->language == language_java)
63d06c5c 6695 {
3da10d80
KS
6696 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6697
6698 /* dwarf2_full_name might have already finished building the DIE's
6699 type. If so, there is no need to continue. */
6700 if (get_die_type (die, cu) != NULL)
6701 return get_die_type (die, cu);
6702
6703 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6704 if (die->tag == DW_TAG_structure_type
6705 || die->tag == DW_TAG_class_type)
6706 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6707 }
6708 else
6709 {
d8151005
DJ
6710 /* The name is already allocated along with this objfile, so
6711 we don't need to duplicate it for the type. */
94af9270
KS
6712 TYPE_TAG_NAME (type) = (char *) name;
6713 if (die->tag == DW_TAG_class_type)
6714 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6715 }
c906108c
SS
6716 }
6717
6718 if (die->tag == DW_TAG_structure_type)
6719 {
6720 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6721 }
6722 else if (die->tag == DW_TAG_union_type)
6723 {
6724 TYPE_CODE (type) = TYPE_CODE_UNION;
6725 }
6726 else
6727 {
c906108c
SS
6728 TYPE_CODE (type) = TYPE_CODE_CLASS;
6729 }
6730
0cc2414c
TT
6731 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6732 TYPE_DECLARED_CLASS (type) = 1;
6733
e142c38c 6734 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6735 if (attr)
6736 {
6737 TYPE_LENGTH (type) = DW_UNSND (attr);
6738 }
6739 else
6740 {
6741 TYPE_LENGTH (type) = 0;
6742 }
6743
876cecd0 6744 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6745 if (die_is_declaration (die, cu))
876cecd0 6746 TYPE_STUB (type) = 1;
a6c727b2
DJ
6747 else if (attr == NULL && die->child == NULL
6748 && producer_is_realview (cu->producer))
6749 /* RealView does not output the required DW_AT_declaration
6750 on incomplete types. */
6751 TYPE_STUB (type) = 1;
dc718098 6752
c906108c
SS
6753 /* We need to add the type field to the die immediately so we don't
6754 infinitely recurse when dealing with pointers to the structure
6755 type within the structure itself. */
1c379e20 6756 set_die_type (die, type, cu);
c906108c 6757
7e314c57
JK
6758 /* set_die_type should be already done. */
6759 set_descriptive_type (type, die, cu);
6760
c767944b
DJ
6761 return type;
6762}
6763
6764/* Finish creating a structure or union type, including filling in
6765 its members and creating a symbol for it. */
6766
6767static void
6768process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6769{
6770 struct objfile *objfile = cu->objfile;
6771 struct die_info *child_die = die->child;
6772 struct type *type;
6773
6774 type = get_die_type (die, cu);
6775 if (type == NULL)
6776 type = read_structure_type (die, cu);
6777
e142c38c 6778 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6779 {
6780 struct field_info fi;
6781 struct die_info *child_die;
34eaf542 6782 VEC (symbolp) *template_args = NULL;
c767944b 6783 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6784
6785 memset (&fi, 0, sizeof (struct field_info));
6786
639d11d3 6787 child_die = die->child;
c906108c
SS
6788
6789 while (child_die && child_die->tag)
6790 {
a9a9bd0f
DC
6791 if (child_die->tag == DW_TAG_member
6792 || child_die->tag == DW_TAG_variable)
c906108c 6793 {
a9a9bd0f
DC
6794 /* NOTE: carlton/2002-11-05: A C++ static data member
6795 should be a DW_TAG_member that is a declaration, but
6796 all versions of G++ as of this writing (so through at
6797 least 3.2.1) incorrectly generate DW_TAG_variable
6798 tags for them instead. */
e7c27a73 6799 dwarf2_add_field (&fi, child_die, cu);
c906108c 6800 }
8713b1b1 6801 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6802 {
6803 /* C++ member function. */
e7c27a73 6804 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6805 }
6806 else if (child_die->tag == DW_TAG_inheritance)
6807 {
6808 /* C++ base class field. */
e7c27a73 6809 dwarf2_add_field (&fi, child_die, cu);
c906108c 6810 }
98751a41
JK
6811 else if (child_die->tag == DW_TAG_typedef)
6812 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6813 else if (child_die->tag == DW_TAG_template_type_param
6814 || child_die->tag == DW_TAG_template_value_param)
6815 {
6816 struct symbol *arg = new_symbol (child_die, NULL, cu);
6817
f1078f66
DJ
6818 if (arg != NULL)
6819 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6820 }
6821
c906108c
SS
6822 child_die = sibling_die (child_die);
6823 }
6824
34eaf542
TT
6825 /* Attach template arguments to type. */
6826 if (! VEC_empty (symbolp, template_args))
6827 {
6828 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6829 TYPE_N_TEMPLATE_ARGUMENTS (type)
6830 = VEC_length (symbolp, template_args);
6831 TYPE_TEMPLATE_ARGUMENTS (type)
6832 = obstack_alloc (&objfile->objfile_obstack,
6833 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6834 * sizeof (struct symbol *)));
6835 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6836 VEC_address (symbolp, template_args),
6837 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6838 * sizeof (struct symbol *)));
6839 VEC_free (symbolp, template_args);
6840 }
6841
c906108c
SS
6842 /* Attach fields and member functions to the type. */
6843 if (fi.nfields)
e7c27a73 6844 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6845 if (fi.nfnfields)
6846 {
e7c27a73 6847 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6848
c5aa993b 6849 /* Get the type which refers to the base class (possibly this
c906108c 6850 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6851 class from the DW_AT_containing_type attribute. This use of
6852 DW_AT_containing_type is a GNU extension. */
c906108c 6853
e142c38c 6854 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6855 {
e7c27a73 6856 struct type *t = die_containing_type (die, cu);
c906108c
SS
6857
6858 TYPE_VPTR_BASETYPE (type) = t;
6859 if (type == t)
6860 {
c906108c
SS
6861 int i;
6862
6863 /* Our own class provides vtbl ptr. */
6864 for (i = TYPE_NFIELDS (t) - 1;
6865 i >= TYPE_N_BASECLASSES (t);
6866 --i)
6867 {
6868 char *fieldname = TYPE_FIELD_NAME (t, i);
6869
1168df01 6870 if (is_vtable_name (fieldname, cu))
c906108c
SS
6871 {
6872 TYPE_VPTR_FIELDNO (type) = i;
6873 break;
6874 }
6875 }
6876
6877 /* Complain if virtual function table field not found. */
6878 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6879 complaint (&symfile_complaints,
e2e0b3e5 6880 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6881 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6882 "");
c906108c
SS
6883 }
6884 else
6885 {
6886 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6887 }
6888 }
f6235d4c
EZ
6889 else if (cu->producer
6890 && strncmp (cu->producer,
6891 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6892 {
6893 /* The IBM XLC compiler does not provide direct indication
6894 of the containing type, but the vtable pointer is
6895 always named __vfp. */
6896
6897 int i;
6898
6899 for (i = TYPE_NFIELDS (type) - 1;
6900 i >= TYPE_N_BASECLASSES (type);
6901 --i)
6902 {
6903 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6904 {
6905 TYPE_VPTR_FIELDNO (type) = i;
6906 TYPE_VPTR_BASETYPE (type) = type;
6907 break;
6908 }
6909 }
6910 }
c906108c 6911 }
98751a41
JK
6912
6913 /* Copy fi.typedef_field_list linked list elements content into the
6914 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6915 if (fi.typedef_field_list)
6916 {
6917 int i = fi.typedef_field_list_count;
6918
a0d7a4ff 6919 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6920 TYPE_TYPEDEF_FIELD_ARRAY (type)
6921 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6922 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6923
6924 /* Reverse the list order to keep the debug info elements order. */
6925 while (--i >= 0)
6926 {
6927 struct typedef_field *dest, *src;
6e70227d 6928
98751a41
JK
6929 dest = &TYPE_TYPEDEF_FIELD (type, i);
6930 src = &fi.typedef_field_list->field;
6931 fi.typedef_field_list = fi.typedef_field_list->next;
6932 *dest = *src;
6933 }
6934 }
c767944b
DJ
6935
6936 do_cleanups (back_to);
c906108c 6937 }
63d06c5c 6938
0b92b5bb
TT
6939 quirk_gcc_member_function_pointer (type, cu->objfile);
6940
90aeadfc
DC
6941 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6942 snapshots) has been known to create a die giving a declaration
6943 for a class that has, as a child, a die giving a definition for a
6944 nested class. So we have to process our children even if the
6945 current die is a declaration. Normally, of course, a declaration
6946 won't have any children at all. */
134d01f1 6947
90aeadfc
DC
6948 while (child_die != NULL && child_die->tag)
6949 {
6950 if (child_die->tag == DW_TAG_member
6951 || child_die->tag == DW_TAG_variable
34eaf542
TT
6952 || child_die->tag == DW_TAG_inheritance
6953 || child_die->tag == DW_TAG_template_value_param
6954 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6955 {
90aeadfc 6956 /* Do nothing. */
134d01f1 6957 }
90aeadfc
DC
6958 else
6959 process_die (child_die, cu);
134d01f1 6960
90aeadfc 6961 child_die = sibling_die (child_die);
134d01f1
DJ
6962 }
6963
fa4028e9
JB
6964 /* Do not consider external references. According to the DWARF standard,
6965 these DIEs are identified by the fact that they have no byte_size
6966 attribute, and a declaration attribute. */
6967 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6968 || !die_is_declaration (die, cu))
c767944b 6969 new_symbol (die, type, cu);
134d01f1
DJ
6970}
6971
6972/* Given a DW_AT_enumeration_type die, set its type. We do not
6973 complete the type's fields yet, or create any symbols. */
c906108c 6974
f792889a 6975static struct type *
134d01f1 6976read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6977{
e7c27a73 6978 struct objfile *objfile = cu->objfile;
c906108c 6979 struct type *type;
c906108c 6980 struct attribute *attr;
0114d602 6981 const char *name;
134d01f1 6982
348e048f
DE
6983 /* If the definition of this type lives in .debug_types, read that type.
6984 Don't follow DW_AT_specification though, that will take us back up
6985 the chain and we want to go down. */
6986 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6987 if (attr)
6988 {
6989 struct dwarf2_cu *type_cu = cu;
6990 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6991
348e048f 6992 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6993
6994 /* TYPE_CU may not be the same as CU.
6995 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6996 return set_die_type (die, type, cu);
6997 }
6998
c906108c
SS
6999 type = alloc_type (objfile);
7000
7001 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7002 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7003 if (name != NULL)
0114d602 7004 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7005
e142c38c 7006 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7007 if (attr)
7008 {
7009 TYPE_LENGTH (type) = DW_UNSND (attr);
7010 }
7011 else
7012 {
7013 TYPE_LENGTH (type) = 0;
7014 }
7015
137033e9
JB
7016 /* The enumeration DIE can be incomplete. In Ada, any type can be
7017 declared as private in the package spec, and then defined only
7018 inside the package body. Such types are known as Taft Amendment
7019 Types. When another package uses such a type, an incomplete DIE
7020 may be generated by the compiler. */
02eb380e 7021 if (die_is_declaration (die, cu))
876cecd0 7022 TYPE_STUB (type) = 1;
02eb380e 7023
f792889a 7024 return set_die_type (die, type, cu);
134d01f1
DJ
7025}
7026
7027/* Given a pointer to a die which begins an enumeration, process all
7028 the dies that define the members of the enumeration, and create the
7029 symbol for the enumeration type.
7030
7031 NOTE: We reverse the order of the element list. */
7032
7033static void
7034process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7035{
f792889a 7036 struct type *this_type;
134d01f1 7037
f792889a
DJ
7038 this_type = get_die_type (die, cu);
7039 if (this_type == NULL)
7040 this_type = read_enumeration_type (die, cu);
9dc481d3 7041
639d11d3 7042 if (die->child != NULL)
c906108c 7043 {
9dc481d3
DE
7044 struct die_info *child_die;
7045 struct symbol *sym;
7046 struct field *fields = NULL;
7047 int num_fields = 0;
7048 int unsigned_enum = 1;
7049 char *name;
7050
639d11d3 7051 child_die = die->child;
c906108c
SS
7052 while (child_die && child_die->tag)
7053 {
7054 if (child_die->tag != DW_TAG_enumerator)
7055 {
e7c27a73 7056 process_die (child_die, cu);
c906108c
SS
7057 }
7058 else
7059 {
39cbfefa
DJ
7060 name = dwarf2_name (child_die, cu);
7061 if (name)
c906108c 7062 {
f792889a 7063 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7064 if (SYMBOL_VALUE (sym) < 0)
7065 unsigned_enum = 0;
7066
7067 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7068 {
7069 fields = (struct field *)
7070 xrealloc (fields,
7071 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7072 * sizeof (struct field));
c906108c
SS
7073 }
7074
3567439c 7075 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7076 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7077 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7078 FIELD_BITSIZE (fields[num_fields]) = 0;
7079
7080 num_fields++;
7081 }
7082 }
7083
7084 child_die = sibling_die (child_die);
7085 }
7086
7087 if (num_fields)
7088 {
f792889a
DJ
7089 TYPE_NFIELDS (this_type) = num_fields;
7090 TYPE_FIELDS (this_type) = (struct field *)
7091 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7092 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7093 sizeof (struct field) * num_fields);
b8c9b27d 7094 xfree (fields);
c906108c
SS
7095 }
7096 if (unsigned_enum)
876cecd0 7097 TYPE_UNSIGNED (this_type) = 1;
c906108c 7098 }
134d01f1 7099
f792889a 7100 new_symbol (die, this_type, cu);
c906108c
SS
7101}
7102
7103/* Extract all information from a DW_TAG_array_type DIE and put it in
7104 the DIE's type field. For now, this only handles one dimensional
7105 arrays. */
7106
f792889a 7107static struct type *
e7c27a73 7108read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7109{
e7c27a73 7110 struct objfile *objfile = cu->objfile;
c906108c 7111 struct die_info *child_die;
7e314c57 7112 struct type *type;
c906108c
SS
7113 struct type *element_type, *range_type, *index_type;
7114 struct type **range_types = NULL;
7115 struct attribute *attr;
7116 int ndim = 0;
7117 struct cleanup *back_to;
39cbfefa 7118 char *name;
c906108c 7119
e7c27a73 7120 element_type = die_type (die, cu);
c906108c 7121
7e314c57
JK
7122 /* The die_type call above may have already set the type for this DIE. */
7123 type = get_die_type (die, cu);
7124 if (type)
7125 return type;
7126
c906108c
SS
7127 /* Irix 6.2 native cc creates array types without children for
7128 arrays with unspecified length. */
639d11d3 7129 if (die->child == NULL)
c906108c 7130 {
46bf5051 7131 index_type = objfile_type (objfile)->builtin_int;
c906108c 7132 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7133 type = create_array_type (NULL, element_type, range_type);
7134 return set_die_type (die, type, cu);
c906108c
SS
7135 }
7136
7137 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7138 child_die = die->child;
c906108c
SS
7139 while (child_die && child_die->tag)
7140 {
7141 if (child_die->tag == DW_TAG_subrange_type)
7142 {
f792889a 7143 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7144
f792889a 7145 if (child_type != NULL)
a02abb62
JB
7146 {
7147 /* The range type was succesfully read. Save it for
7148 the array type creation. */
7149 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7150 {
7151 range_types = (struct type **)
7152 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7153 * sizeof (struct type *));
7154 if (ndim == 0)
7155 make_cleanup (free_current_contents, &range_types);
7156 }
f792889a 7157 range_types[ndim++] = child_type;
a02abb62 7158 }
c906108c
SS
7159 }
7160 child_die = sibling_die (child_die);
7161 }
7162
7163 /* Dwarf2 dimensions are output from left to right, create the
7164 necessary array types in backwards order. */
7ca2d3a3 7165
c906108c 7166 type = element_type;
7ca2d3a3
DL
7167
7168 if (read_array_order (die, cu) == DW_ORD_col_major)
7169 {
7170 int i = 0;
9a619af0 7171
7ca2d3a3
DL
7172 while (i < ndim)
7173 type = create_array_type (NULL, type, range_types[i++]);
7174 }
7175 else
7176 {
7177 while (ndim-- > 0)
7178 type = create_array_type (NULL, type, range_types[ndim]);
7179 }
c906108c 7180
f5f8a009
EZ
7181 /* Understand Dwarf2 support for vector types (like they occur on
7182 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7183 array type. This is not part of the Dwarf2/3 standard yet, but a
7184 custom vendor extension. The main difference between a regular
7185 array and the vector variant is that vectors are passed by value
7186 to functions. */
e142c38c 7187 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7188 if (attr)
ea37ba09 7189 make_vector_type (type);
f5f8a009 7190
dbc98a8b
KW
7191 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7192 implementation may choose to implement triple vectors using this
7193 attribute. */
7194 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7195 if (attr)
7196 {
7197 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7198 TYPE_LENGTH (type) = DW_UNSND (attr);
7199 else
7200 complaint (&symfile_complaints, _("\
7201DW_AT_byte_size for array type smaller than the total size of elements"));
7202 }
7203
39cbfefa
DJ
7204 name = dwarf2_name (die, cu);
7205 if (name)
7206 TYPE_NAME (type) = name;
6e70227d 7207
7e314c57
JK
7208 /* Install the type in the die. */
7209 set_die_type (die, type, cu);
7210
7211 /* set_die_type should be already done. */
b4ba55a1
JB
7212 set_descriptive_type (type, die, cu);
7213
c906108c
SS
7214 do_cleanups (back_to);
7215
7e314c57 7216 return type;
c906108c
SS
7217}
7218
7ca2d3a3 7219static enum dwarf_array_dim_ordering
6e70227d 7220read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7221{
7222 struct attribute *attr;
7223
7224 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7225
7226 if (attr) return DW_SND (attr);
7227
7228 /*
7229 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7230 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7231 laid out as per normal fortran.
7232
6e70227d 7233 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7234 version checking.
7235 */
7236
905e0470
PM
7237 if (cu->language == language_fortran
7238 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7239 {
7240 return DW_ORD_row_major;
7241 }
7242
6e70227d 7243 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7244 {
7245 case array_column_major:
7246 return DW_ORD_col_major;
7247 case array_row_major:
7248 default:
7249 return DW_ORD_row_major;
7250 };
7251}
7252
72019c9c
GM
7253/* Extract all information from a DW_TAG_set_type DIE and put it in
7254 the DIE's type field. */
7255
f792889a 7256static struct type *
72019c9c
GM
7257read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7258{
7e314c57
JK
7259 struct type *domain_type, *set_type;
7260 struct attribute *attr;
f792889a 7261
7e314c57
JK
7262 domain_type = die_type (die, cu);
7263
7264 /* The die_type call above may have already set the type for this DIE. */
7265 set_type = get_die_type (die, cu);
7266 if (set_type)
7267 return set_type;
7268
7269 set_type = create_set_type (NULL, domain_type);
7270
7271 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7272 if (attr)
7273 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7274
f792889a 7275 return set_die_type (die, set_type, cu);
72019c9c 7276}
7ca2d3a3 7277
c906108c
SS
7278/* First cut: install each common block member as a global variable. */
7279
7280static void
e7c27a73 7281read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7282{
7283 struct die_info *child_die;
7284 struct attribute *attr;
7285 struct symbol *sym;
7286 CORE_ADDR base = (CORE_ADDR) 0;
7287
e142c38c 7288 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7289 if (attr)
7290 {
8e19ed76
PS
7291 /* Support the .debug_loc offsets */
7292 if (attr_form_is_block (attr))
7293 {
e7c27a73 7294 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7295 }
3690dd37 7296 else if (attr_form_is_section_offset (attr))
8e19ed76 7297 {
4d3c2250 7298 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7299 }
7300 else
7301 {
4d3c2250
KB
7302 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7303 "common block member");
8e19ed76 7304 }
c906108c 7305 }
639d11d3 7306 if (die->child != NULL)
c906108c 7307 {
639d11d3 7308 child_die = die->child;
c906108c
SS
7309 while (child_die && child_die->tag)
7310 {
e7c27a73 7311 sym = new_symbol (child_die, NULL, cu);
e142c38c 7312 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7313 if (sym != NULL && attr != NULL)
c906108c 7314 {
d4b96c9a
JK
7315 CORE_ADDR byte_offset = 0;
7316
7317 if (attr_form_is_section_offset (attr))
7318 dwarf2_complex_location_expr_complaint ();
7319 else if (attr_form_is_constant (attr))
7320 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7321 else if (attr_form_is_block (attr))
7322 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7323 else
7324 dwarf2_complex_location_expr_complaint ();
7325
7326 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7327 add_symbol_to_list (sym, &global_symbols);
7328 }
7329 child_die = sibling_die (child_die);
7330 }
7331 }
7332}
7333
0114d602 7334/* Create a type for a C++ namespace. */
d9fa45fe 7335
0114d602
DJ
7336static struct type *
7337read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7338{
e7c27a73 7339 struct objfile *objfile = cu->objfile;
0114d602 7340 const char *previous_prefix, *name;
9219021c 7341 int is_anonymous;
0114d602
DJ
7342 struct type *type;
7343
7344 /* For extensions, reuse the type of the original namespace. */
7345 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7346 {
7347 struct die_info *ext_die;
7348 struct dwarf2_cu *ext_cu = cu;
9a619af0 7349
0114d602
DJ
7350 ext_die = dwarf2_extension (die, &ext_cu);
7351 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7352
7353 /* EXT_CU may not be the same as CU.
7354 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7355 return set_die_type (die, type, cu);
7356 }
9219021c 7357
e142c38c 7358 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7359
7360 /* Now build the name of the current namespace. */
7361
0114d602
DJ
7362 previous_prefix = determine_prefix (die, cu);
7363 if (previous_prefix[0] != '\0')
7364 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7365 previous_prefix, name, 0, cu);
0114d602
DJ
7366
7367 /* Create the type. */
7368 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7369 objfile);
7370 TYPE_NAME (type) = (char *) name;
7371 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7372
60531b24 7373 return set_die_type (die, type, cu);
0114d602
DJ
7374}
7375
7376/* Read a C++ namespace. */
7377
7378static void
7379read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7380{
7381 struct objfile *objfile = cu->objfile;
7382 const char *name;
7383 int is_anonymous;
9219021c 7384
5c4e30ca
DC
7385 /* Add a symbol associated to this if we haven't seen the namespace
7386 before. Also, add a using directive if it's an anonymous
7387 namespace. */
9219021c 7388
f2f0e013 7389 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7390 {
7391 struct type *type;
7392
0114d602 7393 type = read_type_die (die, cu);
e7c27a73 7394 new_symbol (die, type, cu);
5c4e30ca 7395
0114d602 7396 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7397 if (is_anonymous)
0114d602
DJ
7398 {
7399 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7400
c0cc3a76 7401 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7402 NULL, &objfile->objfile_obstack);
0114d602 7403 }
5c4e30ca 7404 }
9219021c 7405
639d11d3 7406 if (die->child != NULL)
d9fa45fe 7407 {
639d11d3 7408 struct die_info *child_die = die->child;
6e70227d 7409
d9fa45fe
DC
7410 while (child_die && child_die->tag)
7411 {
e7c27a73 7412 process_die (child_die, cu);
d9fa45fe
DC
7413 child_die = sibling_die (child_die);
7414 }
7415 }
38d518c9
EZ
7416}
7417
f55ee35c
JK
7418/* Read a Fortran module as type. This DIE can be only a declaration used for
7419 imported module. Still we need that type as local Fortran "use ... only"
7420 declaration imports depend on the created type in determine_prefix. */
7421
7422static struct type *
7423read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7424{
7425 struct objfile *objfile = cu->objfile;
7426 char *module_name;
7427 struct type *type;
7428
7429 module_name = dwarf2_name (die, cu);
7430 if (!module_name)
7431 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7432 die->offset);
7433 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7434
7435 /* determine_prefix uses TYPE_TAG_NAME. */
7436 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7437
7438 return set_die_type (die, type, cu);
7439}
7440
5d7cb8df
JK
7441/* Read a Fortran module. */
7442
7443static void
7444read_module (struct die_info *die, struct dwarf2_cu *cu)
7445{
7446 struct die_info *child_die = die->child;
7447
5d7cb8df
JK
7448 while (child_die && child_die->tag)
7449 {
7450 process_die (child_die, cu);
7451 child_die = sibling_die (child_die);
7452 }
7453}
7454
38d518c9
EZ
7455/* Return the name of the namespace represented by DIE. Set
7456 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7457 namespace. */
7458
7459static const char *
e142c38c 7460namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7461{
7462 struct die_info *current_die;
7463 const char *name = NULL;
7464
7465 /* Loop through the extensions until we find a name. */
7466
7467 for (current_die = die;
7468 current_die != NULL;
f2f0e013 7469 current_die = dwarf2_extension (die, &cu))
38d518c9 7470 {
e142c38c 7471 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7472 if (name != NULL)
7473 break;
7474 }
7475
7476 /* Is it an anonymous namespace? */
7477
7478 *is_anonymous = (name == NULL);
7479 if (*is_anonymous)
7480 name = "(anonymous namespace)";
7481
7482 return name;
d9fa45fe
DC
7483}
7484
c906108c
SS
7485/* Extract all information from a DW_TAG_pointer_type DIE and add to
7486 the user defined type vector. */
7487
f792889a 7488static struct type *
e7c27a73 7489read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7490{
5e2b427d 7491 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7492 struct comp_unit_head *cu_header = &cu->header;
c906108c 7493 struct type *type;
8b2dbe47
KB
7494 struct attribute *attr_byte_size;
7495 struct attribute *attr_address_class;
7496 int byte_size, addr_class;
7e314c57
JK
7497 struct type *target_type;
7498
7499 target_type = die_type (die, cu);
c906108c 7500
7e314c57
JK
7501 /* The die_type call above may have already set the type for this DIE. */
7502 type = get_die_type (die, cu);
7503 if (type)
7504 return type;
7505
7506 type = lookup_pointer_type (target_type);
8b2dbe47 7507
e142c38c 7508 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7509 if (attr_byte_size)
7510 byte_size = DW_UNSND (attr_byte_size);
c906108c 7511 else
8b2dbe47
KB
7512 byte_size = cu_header->addr_size;
7513
e142c38c 7514 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7515 if (attr_address_class)
7516 addr_class = DW_UNSND (attr_address_class);
7517 else
7518 addr_class = DW_ADDR_none;
7519
7520 /* If the pointer size or address class is different than the
7521 default, create a type variant marked as such and set the
7522 length accordingly. */
7523 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7524 {
5e2b427d 7525 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7526 {
7527 int type_flags;
7528
849957d9 7529 type_flags = gdbarch_address_class_type_flags
5e2b427d 7530 (gdbarch, byte_size, addr_class);
876cecd0
TT
7531 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7532 == 0);
8b2dbe47
KB
7533 type = make_type_with_address_space (type, type_flags);
7534 }
7535 else if (TYPE_LENGTH (type) != byte_size)
7536 {
e2e0b3e5 7537 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7538 }
6e70227d 7539 else
9a619af0
MS
7540 {
7541 /* Should we also complain about unhandled address classes? */
7542 }
c906108c 7543 }
8b2dbe47
KB
7544
7545 TYPE_LENGTH (type) = byte_size;
f792889a 7546 return set_die_type (die, type, cu);
c906108c
SS
7547}
7548
7549/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7550 the user defined type vector. */
7551
f792889a 7552static struct type *
e7c27a73 7553read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7554{
7555 struct type *type;
7556 struct type *to_type;
7557 struct type *domain;
7558
e7c27a73
DJ
7559 to_type = die_type (die, cu);
7560 domain = die_containing_type (die, cu);
0d5de010 7561
7e314c57
JK
7562 /* The calls above may have already set the type for this DIE. */
7563 type = get_die_type (die, cu);
7564 if (type)
7565 return type;
7566
0d5de010
DJ
7567 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7568 type = lookup_methodptr_type (to_type);
7569 else
7570 type = lookup_memberptr_type (to_type, domain);
c906108c 7571
f792889a 7572 return set_die_type (die, type, cu);
c906108c
SS
7573}
7574
7575/* Extract all information from a DW_TAG_reference_type DIE and add to
7576 the user defined type vector. */
7577
f792889a 7578static struct type *
e7c27a73 7579read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7580{
e7c27a73 7581 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7582 struct type *type, *target_type;
c906108c
SS
7583 struct attribute *attr;
7584
7e314c57
JK
7585 target_type = die_type (die, cu);
7586
7587 /* The die_type call above may have already set the type for this DIE. */
7588 type = get_die_type (die, cu);
7589 if (type)
7590 return type;
7591
7592 type = lookup_reference_type (target_type);
e142c38c 7593 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7594 if (attr)
7595 {
7596 TYPE_LENGTH (type) = DW_UNSND (attr);
7597 }
7598 else
7599 {
107d2387 7600 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7601 }
f792889a 7602 return set_die_type (die, type, cu);
c906108c
SS
7603}
7604
f792889a 7605static struct type *
e7c27a73 7606read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7607{
f792889a 7608 struct type *base_type, *cv_type;
c906108c 7609
e7c27a73 7610 base_type = die_type (die, cu);
7e314c57
JK
7611
7612 /* The die_type call above may have already set the type for this DIE. */
7613 cv_type = get_die_type (die, cu);
7614 if (cv_type)
7615 return cv_type;
7616
2f608a3a
KW
7617 /* In case the const qualifier is applied to an array type, the element type
7618 is so qualified, not the array type (section 6.7.3 of C99). */
7619 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7620 {
7621 struct type *el_type, *inner_array;
7622
7623 base_type = copy_type (base_type);
7624 inner_array = base_type;
7625
7626 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7627 {
7628 TYPE_TARGET_TYPE (inner_array) =
7629 copy_type (TYPE_TARGET_TYPE (inner_array));
7630 inner_array = TYPE_TARGET_TYPE (inner_array);
7631 }
7632
7633 el_type = TYPE_TARGET_TYPE (inner_array);
7634 TYPE_TARGET_TYPE (inner_array) =
7635 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7636
7637 return set_die_type (die, base_type, cu);
7638 }
7639
f792889a
DJ
7640 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7641 return set_die_type (die, cv_type, cu);
c906108c
SS
7642}
7643
f792889a 7644static struct type *
e7c27a73 7645read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7646{
f792889a 7647 struct type *base_type, *cv_type;
c906108c 7648
e7c27a73 7649 base_type = die_type (die, cu);
7e314c57
JK
7650
7651 /* The die_type call above may have already set the type for this DIE. */
7652 cv_type = get_die_type (die, cu);
7653 if (cv_type)
7654 return cv_type;
7655
f792889a
DJ
7656 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7657 return set_die_type (die, cv_type, cu);
c906108c
SS
7658}
7659
7660/* Extract all information from a DW_TAG_string_type DIE and add to
7661 the user defined type vector. It isn't really a user defined type,
7662 but it behaves like one, with other DIE's using an AT_user_def_type
7663 attribute to reference it. */
7664
f792889a 7665static struct type *
e7c27a73 7666read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7667{
e7c27a73 7668 struct objfile *objfile = cu->objfile;
3b7538c0 7669 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7670 struct type *type, *range_type, *index_type, *char_type;
7671 struct attribute *attr;
7672 unsigned int length;
7673
e142c38c 7674 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7675 if (attr)
7676 {
7677 length = DW_UNSND (attr);
7678 }
7679 else
7680 {
b21b22e0 7681 /* check for the DW_AT_byte_size attribute */
e142c38c 7682 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7683 if (attr)
7684 {
7685 length = DW_UNSND (attr);
7686 }
7687 else
7688 {
7689 length = 1;
7690 }
c906108c 7691 }
6ccb9162 7692
46bf5051 7693 index_type = objfile_type (objfile)->builtin_int;
c906108c 7694 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7695 char_type = language_string_char_type (cu->language_defn, gdbarch);
7696 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7697
f792889a 7698 return set_die_type (die, type, cu);
c906108c
SS
7699}
7700
7701/* Handle DIES due to C code like:
7702
7703 struct foo
c5aa993b
JM
7704 {
7705 int (*funcp)(int a, long l);
7706 int b;
7707 };
c906108c
SS
7708
7709 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7710 */
c906108c 7711
f792889a 7712static struct type *
e7c27a73 7713read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7714{
7715 struct type *type; /* Type that this function returns */
7716 struct type *ftype; /* Function that returns above type */
7717 struct attribute *attr;
7718
e7c27a73 7719 type = die_type (die, cu);
7e314c57
JK
7720
7721 /* The die_type call above may have already set the type for this DIE. */
7722 ftype = get_die_type (die, cu);
7723 if (ftype)
7724 return ftype;
7725
0c8b41f1 7726 ftype = lookup_function_type (type);
c906108c 7727
5b8101ae 7728 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7729 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7730 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7731 || cu->language == language_cplus
5b8101ae
PM
7732 || cu->language == language_java
7733 || cu->language == language_pascal)
876cecd0 7734 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7735 else if (producer_is_realview (cu->producer))
7736 /* RealView does not emit DW_AT_prototyped. We can not
7737 distinguish prototyped and unprototyped functions; default to
7738 prototyped, since that is more common in modern code (and
7739 RealView warns about unprototyped functions). */
7740 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7741
c055b101
CV
7742 /* Store the calling convention in the type if it's available in
7743 the subroutine die. Otherwise set the calling convention to
7744 the default value DW_CC_normal. */
7745 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7746 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7747
7748 /* We need to add the subroutine type to the die immediately so
7749 we don't infinitely recurse when dealing with parameters
7750 declared as the same subroutine type. */
7751 set_die_type (die, ftype, cu);
6e70227d 7752
639d11d3 7753 if (die->child != NULL)
c906108c 7754 {
8072405b 7755 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7756 struct die_info *child_die;
8072405b 7757 int nparams, iparams;
c906108c
SS
7758
7759 /* Count the number of parameters.
7760 FIXME: GDB currently ignores vararg functions, but knows about
7761 vararg member functions. */
8072405b 7762 nparams = 0;
639d11d3 7763 child_die = die->child;
c906108c
SS
7764 while (child_die && child_die->tag)
7765 {
7766 if (child_die->tag == DW_TAG_formal_parameter)
7767 nparams++;
7768 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7769 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7770 child_die = sibling_die (child_die);
7771 }
7772
7773 /* Allocate storage for parameters and fill them in. */
7774 TYPE_NFIELDS (ftype) = nparams;
7775 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7776 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7777
8072405b
JK
7778 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7779 even if we error out during the parameters reading below. */
7780 for (iparams = 0; iparams < nparams; iparams++)
7781 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7782
7783 iparams = 0;
639d11d3 7784 child_die = die->child;
c906108c
SS
7785 while (child_die && child_die->tag)
7786 {
7787 if (child_die->tag == DW_TAG_formal_parameter)
7788 {
3ce3b1ba
PA
7789 struct type *arg_type;
7790
7791 /* DWARF version 2 has no clean way to discern C++
7792 static and non-static member functions. G++ helps
7793 GDB by marking the first parameter for non-static
7794 member functions (which is the this pointer) as
7795 artificial. We pass this information to
7796 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7797
7798 DWARF version 3 added DW_AT_object_pointer, which GCC
7799 4.5 does not yet generate. */
e142c38c 7800 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7801 if (attr)
7802 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7803 else
418835cc
KS
7804 {
7805 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7806
7807 /* GCC/43521: In java, the formal parameter
7808 "this" is sometimes not marked with DW_AT_artificial. */
7809 if (cu->language == language_java)
7810 {
7811 const char *name = dwarf2_name (child_die, cu);
9a619af0 7812
418835cc
KS
7813 if (name && !strcmp (name, "this"))
7814 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7815 }
7816 }
3ce3b1ba
PA
7817 arg_type = die_type (child_die, cu);
7818
7819 /* RealView does not mark THIS as const, which the testsuite
7820 expects. GCC marks THIS as const in method definitions,
7821 but not in the class specifications (GCC PR 43053). */
7822 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7823 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7824 {
7825 int is_this = 0;
7826 struct dwarf2_cu *arg_cu = cu;
7827 const char *name = dwarf2_name (child_die, cu);
7828
7829 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7830 if (attr)
7831 {
7832 /* If the compiler emits this, use it. */
7833 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7834 is_this = 1;
7835 }
7836 else if (name && strcmp (name, "this") == 0)
7837 /* Function definitions will have the argument names. */
7838 is_this = 1;
7839 else if (name == NULL && iparams == 0)
7840 /* Declarations may not have the names, so like
7841 elsewhere in GDB, assume an artificial first
7842 argument is "this". */
7843 is_this = 1;
7844
7845 if (is_this)
7846 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7847 arg_type, 0);
7848 }
7849
7850 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7851 iparams++;
7852 }
7853 child_die = sibling_die (child_die);
7854 }
7855 }
7856
76c10ea2 7857 return ftype;
c906108c
SS
7858}
7859
f792889a 7860static struct type *
e7c27a73 7861read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7862{
e7c27a73 7863 struct objfile *objfile = cu->objfile;
0114d602 7864 const char *name = NULL;
f792889a 7865 struct type *this_type;
c906108c 7866
94af9270 7867 name = dwarf2_full_name (NULL, die, cu);
f792889a 7868 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7869 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7870 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7871 set_die_type (die, this_type, cu);
7872 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7873 return this_type;
c906108c
SS
7874}
7875
7876/* Find a representation of a given base type and install
7877 it in the TYPE field of the die. */
7878
f792889a 7879static struct type *
e7c27a73 7880read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7881{
e7c27a73 7882 struct objfile *objfile = cu->objfile;
c906108c
SS
7883 struct type *type;
7884 struct attribute *attr;
7885 int encoding = 0, size = 0;
39cbfefa 7886 char *name;
6ccb9162
UW
7887 enum type_code code = TYPE_CODE_INT;
7888 int type_flags = 0;
7889 struct type *target_type = NULL;
c906108c 7890
e142c38c 7891 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7892 if (attr)
7893 {
7894 encoding = DW_UNSND (attr);
7895 }
e142c38c 7896 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7897 if (attr)
7898 {
7899 size = DW_UNSND (attr);
7900 }
39cbfefa 7901 name = dwarf2_name (die, cu);
6ccb9162 7902 if (!name)
c906108c 7903 {
6ccb9162
UW
7904 complaint (&symfile_complaints,
7905 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7906 }
6ccb9162
UW
7907
7908 switch (encoding)
c906108c 7909 {
6ccb9162
UW
7910 case DW_ATE_address:
7911 /* Turn DW_ATE_address into a void * pointer. */
7912 code = TYPE_CODE_PTR;
7913 type_flags |= TYPE_FLAG_UNSIGNED;
7914 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7915 break;
7916 case DW_ATE_boolean:
7917 code = TYPE_CODE_BOOL;
7918 type_flags |= TYPE_FLAG_UNSIGNED;
7919 break;
7920 case DW_ATE_complex_float:
7921 code = TYPE_CODE_COMPLEX;
7922 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7923 break;
7924 case DW_ATE_decimal_float:
7925 code = TYPE_CODE_DECFLOAT;
7926 break;
7927 case DW_ATE_float:
7928 code = TYPE_CODE_FLT;
7929 break;
7930 case DW_ATE_signed:
7931 break;
7932 case DW_ATE_unsigned:
7933 type_flags |= TYPE_FLAG_UNSIGNED;
7934 break;
7935 case DW_ATE_signed_char:
6e70227d 7936 if (cu->language == language_ada || cu->language == language_m2
868a0084 7937 || cu->language == language_pascal)
6ccb9162
UW
7938 code = TYPE_CODE_CHAR;
7939 break;
7940 case DW_ATE_unsigned_char:
868a0084
PM
7941 if (cu->language == language_ada || cu->language == language_m2
7942 || cu->language == language_pascal)
6ccb9162
UW
7943 code = TYPE_CODE_CHAR;
7944 type_flags |= TYPE_FLAG_UNSIGNED;
7945 break;
75079b2b
TT
7946 case DW_ATE_UTF:
7947 /* We just treat this as an integer and then recognize the
7948 type by name elsewhere. */
7949 break;
7950
6ccb9162
UW
7951 default:
7952 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7953 dwarf_type_encoding_name (encoding));
7954 break;
c906108c 7955 }
6ccb9162 7956
0114d602
DJ
7957 type = init_type (code, size, type_flags, NULL, objfile);
7958 TYPE_NAME (type) = name;
6ccb9162
UW
7959 TYPE_TARGET_TYPE (type) = target_type;
7960
0114d602 7961 if (name && strcmp (name, "char") == 0)
876cecd0 7962 TYPE_NOSIGN (type) = 1;
0114d602 7963
f792889a 7964 return set_die_type (die, type, cu);
c906108c
SS
7965}
7966
a02abb62
JB
7967/* Read the given DW_AT_subrange DIE. */
7968
f792889a 7969static struct type *
a02abb62
JB
7970read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7971{
5e2b427d 7972 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7973 struct type *base_type;
7974 struct type *range_type;
7975 struct attribute *attr;
43bbcdc2
PH
7976 LONGEST low = 0;
7977 LONGEST high = -1;
39cbfefa 7978 char *name;
43bbcdc2 7979 LONGEST negative_mask;
e77813c8 7980
a02abb62 7981 base_type = die_type (die, cu);
953ac07e
JK
7982 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7983 check_typedef (base_type);
a02abb62 7984
7e314c57
JK
7985 /* The die_type call above may have already set the type for this DIE. */
7986 range_type = get_die_type (die, cu);
7987 if (range_type)
7988 return range_type;
7989
e142c38c 7990 if (cu->language == language_fortran)
6e70227d 7991 {
a02abb62
JB
7992 /* FORTRAN implies a lower bound of 1, if not given. */
7993 low = 1;
7994 }
7995
dd5e6932
DJ
7996 /* FIXME: For variable sized arrays either of these could be
7997 a variable rather than a constant value. We'll allow it,
7998 but we don't know how to handle it. */
e142c38c 7999 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8000 if (attr)
8001 low = dwarf2_get_attr_constant_value (attr, 0);
8002
e142c38c 8003 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8004 if (attr)
6e70227d 8005 {
e77813c8 8006 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8007 {
8008 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8009 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8010 FIXME: GDB does not yet know how to handle dynamic
8011 arrays properly, treat them as arrays with unspecified
8012 length for now.
8013
8014 FIXME: jimb/2003-09-22: GDB does not really know
8015 how to handle arrays of unspecified length
8016 either; we just represent them as zero-length
8017 arrays. Choose an appropriate upper bound given
8018 the lower bound we've computed above. */
8019 high = low - 1;
8020 }
8021 else
8022 high = dwarf2_get_attr_constant_value (attr, 1);
8023 }
e77813c8
PM
8024 else
8025 {
8026 attr = dwarf2_attr (die, DW_AT_count, cu);
8027 if (attr)
8028 {
8029 int count = dwarf2_get_attr_constant_value (attr, 1);
8030 high = low + count - 1;
8031 }
8032 }
8033
8034 /* Dwarf-2 specifications explicitly allows to create subrange types
8035 without specifying a base type.
8036 In that case, the base type must be set to the type of
8037 the lower bound, upper bound or count, in that order, if any of these
8038 three attributes references an object that has a type.
8039 If no base type is found, the Dwarf-2 specifications say that
8040 a signed integer type of size equal to the size of an address should
8041 be used.
8042 For the following C code: `extern char gdb_int [];'
8043 GCC produces an empty range DIE.
8044 FIXME: muller/2010-05-28: Possible references to object for low bound,
8045 high bound or count are not yet handled by this code.
8046 */
8047 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8048 {
8049 struct objfile *objfile = cu->objfile;
8050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8051 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8052 struct type *int_type = objfile_type (objfile)->builtin_int;
8053
8054 /* Test "int", "long int", and "long long int" objfile types,
8055 and select the first one having a size above or equal to the
8056 architecture address size. */
8057 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8058 base_type = int_type;
8059 else
8060 {
8061 int_type = objfile_type (objfile)->builtin_long;
8062 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8063 base_type = int_type;
8064 else
8065 {
8066 int_type = objfile_type (objfile)->builtin_long_long;
8067 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8068 base_type = int_type;
8069 }
8070 }
8071 }
a02abb62 8072
6e70227d 8073 negative_mask =
43bbcdc2
PH
8074 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8075 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8076 low |= negative_mask;
8077 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8078 high |= negative_mask;
8079
a02abb62
JB
8080 range_type = create_range_type (NULL, base_type, low, high);
8081
bbb0eef6
JK
8082 /* Mark arrays with dynamic length at least as an array of unspecified
8083 length. GDB could check the boundary but before it gets implemented at
8084 least allow accessing the array elements. */
8085 if (attr && attr->form == DW_FORM_block1)
8086 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8087
39cbfefa
DJ
8088 name = dwarf2_name (die, cu);
8089 if (name)
8090 TYPE_NAME (range_type) = name;
6e70227d 8091
e142c38c 8092 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8093 if (attr)
8094 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8095
7e314c57
JK
8096 set_die_type (die, range_type, cu);
8097
8098 /* set_die_type should be already done. */
b4ba55a1
JB
8099 set_descriptive_type (range_type, die, cu);
8100
7e314c57 8101 return range_type;
a02abb62 8102}
6e70227d 8103
f792889a 8104static struct type *
81a17f79
JB
8105read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8106{
8107 struct type *type;
81a17f79 8108
81a17f79
JB
8109 /* For now, we only support the C meaning of an unspecified type: void. */
8110
0114d602
DJ
8111 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8112 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8113
f792889a 8114 return set_die_type (die, type, cu);
81a17f79 8115}
a02abb62 8116
51545339
DJ
8117/* Trivial hash function for die_info: the hash value of a DIE
8118 is its offset in .debug_info for this objfile. */
8119
8120static hashval_t
8121die_hash (const void *item)
8122{
8123 const struct die_info *die = item;
9a619af0 8124
51545339
DJ
8125 return die->offset;
8126}
8127
8128/* Trivial comparison function for die_info structures: two DIEs
8129 are equal if they have the same offset. */
8130
8131static int
8132die_eq (const void *item_lhs, const void *item_rhs)
8133{
8134 const struct die_info *die_lhs = item_lhs;
8135 const struct die_info *die_rhs = item_rhs;
9a619af0 8136
51545339
DJ
8137 return die_lhs->offset == die_rhs->offset;
8138}
8139
c906108c
SS
8140/* Read a whole compilation unit into a linked list of dies. */
8141
f9aca02d 8142static struct die_info *
93311388 8143read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8144{
93311388 8145 struct die_reader_specs reader_specs;
98bfdba5 8146 int read_abbrevs = 0;
1d9ec526 8147 struct cleanup *back_to = NULL;
98bfdba5
PA
8148 struct die_info *die;
8149
8150 if (cu->dwarf2_abbrevs == NULL)
8151 {
8152 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8153 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8154 read_abbrevs = 1;
8155 }
93311388 8156
348e048f 8157 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8158 cu->die_hash
8159 = htab_create_alloc_ex (cu->header.length / 12,
8160 die_hash,
8161 die_eq,
8162 NULL,
8163 &cu->comp_unit_obstack,
8164 hashtab_obstack_allocate,
8165 dummy_obstack_deallocate);
8166
93311388
DE
8167 init_cu_die_reader (&reader_specs, cu);
8168
98bfdba5
PA
8169 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8170
8171 if (read_abbrevs)
8172 do_cleanups (back_to);
8173
8174 return die;
639d11d3
DC
8175}
8176
d97bc12b
DE
8177/* Main entry point for reading a DIE and all children.
8178 Read the DIE and dump it if requested. */
8179
8180static struct die_info *
93311388
DE
8181read_die_and_children (const struct die_reader_specs *reader,
8182 gdb_byte *info_ptr,
d97bc12b
DE
8183 gdb_byte **new_info_ptr,
8184 struct die_info *parent)
8185{
93311388 8186 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8187 new_info_ptr, parent);
8188
8189 if (dwarf2_die_debug)
8190 {
348e048f
DE
8191 fprintf_unfiltered (gdb_stdlog,
8192 "\nRead die from %s of %s:\n",
8193 reader->buffer == dwarf2_per_objfile->info.buffer
8194 ? ".debug_info"
8195 : reader->buffer == dwarf2_per_objfile->types.buffer
8196 ? ".debug_types"
8197 : "unknown section",
8198 reader->abfd->filename);
d97bc12b
DE
8199 dump_die (result, dwarf2_die_debug);
8200 }
8201
8202 return result;
8203}
8204
639d11d3
DC
8205/* Read a single die and all its descendents. Set the die's sibling
8206 field to NULL; set other fields in the die correctly, and set all
8207 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8208 location of the info_ptr after reading all of those dies. PARENT
8209 is the parent of the die in question. */
8210
8211static struct die_info *
93311388
DE
8212read_die_and_children_1 (const struct die_reader_specs *reader,
8213 gdb_byte *info_ptr,
d97bc12b
DE
8214 gdb_byte **new_info_ptr,
8215 struct die_info *parent)
639d11d3
DC
8216{
8217 struct die_info *die;
fe1b8b76 8218 gdb_byte *cur_ptr;
639d11d3
DC
8219 int has_children;
8220
93311388 8221 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8222 if (die == NULL)
8223 {
8224 *new_info_ptr = cur_ptr;
8225 return NULL;
8226 }
93311388 8227 store_in_ref_table (die, reader->cu);
639d11d3
DC
8228
8229 if (has_children)
348e048f 8230 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8231 else
8232 {
8233 die->child = NULL;
8234 *new_info_ptr = cur_ptr;
8235 }
8236
8237 die->sibling = NULL;
8238 die->parent = parent;
8239 return die;
8240}
8241
8242/* Read a die, all of its descendents, and all of its siblings; set
8243 all of the fields of all of the dies correctly. Arguments are as
8244 in read_die_and_children. */
8245
8246static struct die_info *
93311388
DE
8247read_die_and_siblings (const struct die_reader_specs *reader,
8248 gdb_byte *info_ptr,
fe1b8b76 8249 gdb_byte **new_info_ptr,
639d11d3
DC
8250 struct die_info *parent)
8251{
8252 struct die_info *first_die, *last_sibling;
fe1b8b76 8253 gdb_byte *cur_ptr;
639d11d3 8254
c906108c 8255 cur_ptr = info_ptr;
639d11d3
DC
8256 first_die = last_sibling = NULL;
8257
8258 while (1)
c906108c 8259 {
639d11d3 8260 struct die_info *die
93311388 8261 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8262
1d325ec1 8263 if (die == NULL)
c906108c 8264 {
639d11d3
DC
8265 *new_info_ptr = cur_ptr;
8266 return first_die;
c906108c 8267 }
1d325ec1
DJ
8268
8269 if (!first_die)
8270 first_die = die;
c906108c 8271 else
1d325ec1
DJ
8272 last_sibling->sibling = die;
8273
8274 last_sibling = die;
c906108c 8275 }
c906108c
SS
8276}
8277
93311388
DE
8278/* Read the die from the .debug_info section buffer. Set DIEP to
8279 point to a newly allocated die with its information, except for its
8280 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8281 whether the die has children or not. */
8282
8283static gdb_byte *
8284read_full_die (const struct die_reader_specs *reader,
8285 struct die_info **diep, gdb_byte *info_ptr,
8286 int *has_children)
8287{
8288 unsigned int abbrev_number, bytes_read, i, offset;
8289 struct abbrev_info *abbrev;
8290 struct die_info *die;
8291 struct dwarf2_cu *cu = reader->cu;
8292 bfd *abfd = reader->abfd;
8293
8294 offset = info_ptr - reader->buffer;
8295 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8296 info_ptr += bytes_read;
8297 if (!abbrev_number)
8298 {
8299 *diep = NULL;
8300 *has_children = 0;
8301 return info_ptr;
8302 }
8303
8304 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8305 if (!abbrev)
348e048f
DE
8306 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8307 abbrev_number,
8308 bfd_get_filename (abfd));
8309
93311388
DE
8310 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8311 die->offset = offset;
8312 die->tag = abbrev->tag;
8313 die->abbrev = abbrev_number;
8314
8315 die->num_attrs = abbrev->num_attrs;
8316
8317 for (i = 0; i < abbrev->num_attrs; ++i)
8318 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8319 abfd, info_ptr, cu);
8320
8321 *diep = die;
8322 *has_children = abbrev->has_children;
8323 return info_ptr;
8324}
8325
c906108c
SS
8326/* In DWARF version 2, the description of the debugging information is
8327 stored in a separate .debug_abbrev section. Before we read any
8328 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8329 in a hash table. This function also sets flags in CU describing
8330 the data found in the abbrev table. */
c906108c
SS
8331
8332static void
e7c27a73 8333dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8334{
e7c27a73 8335 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8336 gdb_byte *abbrev_ptr;
c906108c
SS
8337 struct abbrev_info *cur_abbrev;
8338 unsigned int abbrev_number, bytes_read, abbrev_name;
8339 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8340 struct attr_abbrev *cur_attrs;
8341 unsigned int allocated_attrs;
c906108c 8342
57349743 8343 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8344 obstack_init (&cu->abbrev_obstack);
8345 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8346 (ABBREV_HASH_SIZE
8347 * sizeof (struct abbrev_info *)));
8348 memset (cu->dwarf2_abbrevs, 0,
8349 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8350
be391dca
TT
8351 dwarf2_read_section (dwarf2_per_objfile->objfile,
8352 &dwarf2_per_objfile->abbrev);
dce234bc 8353 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8354 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8355 abbrev_ptr += bytes_read;
8356
f3dd6933
DJ
8357 allocated_attrs = ATTR_ALLOC_CHUNK;
8358 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8359
c906108c
SS
8360 /* loop until we reach an abbrev number of 0 */
8361 while (abbrev_number)
8362 {
f3dd6933 8363 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8364
8365 /* read in abbrev header */
8366 cur_abbrev->number = abbrev_number;
8367 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8368 abbrev_ptr += bytes_read;
8369 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8370 abbrev_ptr += 1;
8371
72bf9492
DJ
8372 if (cur_abbrev->tag == DW_TAG_namespace)
8373 cu->has_namespace_info = 1;
8374
c906108c
SS
8375 /* now read in declarations */
8376 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8377 abbrev_ptr += bytes_read;
8378 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8379 abbrev_ptr += bytes_read;
8380 while (abbrev_name)
8381 {
f3dd6933 8382 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8383 {
f3dd6933
DJ
8384 allocated_attrs += ATTR_ALLOC_CHUNK;
8385 cur_attrs
8386 = xrealloc (cur_attrs, (allocated_attrs
8387 * sizeof (struct attr_abbrev)));
c906108c 8388 }
ae038cb0
DJ
8389
8390 /* Record whether this compilation unit might have
8391 inter-compilation-unit references. If we don't know what form
8392 this attribute will have, then it might potentially be a
8393 DW_FORM_ref_addr, so we conservatively expect inter-CU
8394 references. */
8395
8396 if (abbrev_form == DW_FORM_ref_addr
8397 || abbrev_form == DW_FORM_indirect)
8398 cu->has_form_ref_addr = 1;
8399
f3dd6933
DJ
8400 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8401 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8402 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8403 abbrev_ptr += bytes_read;
8404 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8405 abbrev_ptr += bytes_read;
8406 }
8407
f3dd6933
DJ
8408 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8409 (cur_abbrev->num_attrs
8410 * sizeof (struct attr_abbrev)));
8411 memcpy (cur_abbrev->attrs, cur_attrs,
8412 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8413
c906108c 8414 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8415 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8416 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8417
8418 /* Get next abbreviation.
8419 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8420 always properly terminated with an abbrev number of 0.
8421 Exit loop if we encounter an abbreviation which we have
8422 already read (which means we are about to read the abbreviations
8423 for the next compile unit) or if the end of the abbreviation
8424 table is reached. */
dce234bc
PP
8425 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8426 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8427 break;
8428 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8429 abbrev_ptr += bytes_read;
e7c27a73 8430 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8431 break;
8432 }
f3dd6933
DJ
8433
8434 xfree (cur_attrs);
c906108c
SS
8435}
8436
f3dd6933 8437/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8438
c906108c 8439static void
f3dd6933 8440dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8441{
f3dd6933 8442 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8443
f3dd6933
DJ
8444 obstack_free (&cu->abbrev_obstack, NULL);
8445 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8446}
8447
8448/* Lookup an abbrev_info structure in the abbrev hash table. */
8449
8450static struct abbrev_info *
e7c27a73 8451dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8452{
8453 unsigned int hash_number;
8454 struct abbrev_info *abbrev;
8455
8456 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8457 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8458
8459 while (abbrev)
8460 {
8461 if (abbrev->number == number)
8462 return abbrev;
8463 else
8464 abbrev = abbrev->next;
8465 }
8466 return NULL;
8467}
8468
72bf9492
DJ
8469/* Returns nonzero if TAG represents a type that we might generate a partial
8470 symbol for. */
8471
8472static int
8473is_type_tag_for_partial (int tag)
8474{
8475 switch (tag)
8476 {
8477#if 0
8478 /* Some types that would be reasonable to generate partial symbols for,
8479 that we don't at present. */
8480 case DW_TAG_array_type:
8481 case DW_TAG_file_type:
8482 case DW_TAG_ptr_to_member_type:
8483 case DW_TAG_set_type:
8484 case DW_TAG_string_type:
8485 case DW_TAG_subroutine_type:
8486#endif
8487 case DW_TAG_base_type:
8488 case DW_TAG_class_type:
680b30c7 8489 case DW_TAG_interface_type:
72bf9492
DJ
8490 case DW_TAG_enumeration_type:
8491 case DW_TAG_structure_type:
8492 case DW_TAG_subrange_type:
8493 case DW_TAG_typedef:
8494 case DW_TAG_union_type:
8495 return 1;
8496 default:
8497 return 0;
8498 }
8499}
8500
8501/* Load all DIEs that are interesting for partial symbols into memory. */
8502
8503static struct partial_die_info *
93311388
DE
8504load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8505 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8506{
8507 struct partial_die_info *part_die;
8508 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8509 struct abbrev_info *abbrev;
8510 unsigned int bytes_read;
5afb4e99 8511 unsigned int load_all = 0;
72bf9492
DJ
8512
8513 int nesting_level = 1;
8514
8515 parent_die = NULL;
8516 last_die = NULL;
8517
5afb4e99
DJ
8518 if (cu->per_cu && cu->per_cu->load_all_dies)
8519 load_all = 1;
8520
72bf9492
DJ
8521 cu->partial_dies
8522 = htab_create_alloc_ex (cu->header.length / 12,
8523 partial_die_hash,
8524 partial_die_eq,
8525 NULL,
8526 &cu->comp_unit_obstack,
8527 hashtab_obstack_allocate,
8528 dummy_obstack_deallocate);
8529
8530 part_die = obstack_alloc (&cu->comp_unit_obstack,
8531 sizeof (struct partial_die_info));
8532
8533 while (1)
8534 {
8535 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8536
8537 /* A NULL abbrev means the end of a series of children. */
8538 if (abbrev == NULL)
8539 {
8540 if (--nesting_level == 0)
8541 {
8542 /* PART_DIE was probably the last thing allocated on the
8543 comp_unit_obstack, so we could call obstack_free
8544 here. We don't do that because the waste is small,
8545 and will be cleaned up when we're done with this
8546 compilation unit. This way, we're also more robust
8547 against other users of the comp_unit_obstack. */
8548 return first_die;
8549 }
8550 info_ptr += bytes_read;
8551 last_die = parent_die;
8552 parent_die = parent_die->die_parent;
8553 continue;
8554 }
8555
98bfdba5
PA
8556 /* Check for template arguments. We never save these; if
8557 they're seen, we just mark the parent, and go on our way. */
8558 if (parent_die != NULL
8559 && cu->language == language_cplus
8560 && (abbrev->tag == DW_TAG_template_type_param
8561 || abbrev->tag == DW_TAG_template_value_param))
8562 {
8563 parent_die->has_template_arguments = 1;
8564
8565 if (!load_all)
8566 {
8567 /* We don't need a partial DIE for the template argument. */
8568 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8569 cu);
8570 continue;
8571 }
8572 }
8573
8574 /* We only recurse into subprograms looking for template arguments.
8575 Skip their other children. */
8576 if (!load_all
8577 && cu->language == language_cplus
8578 && parent_die != NULL
8579 && parent_die->tag == DW_TAG_subprogram)
8580 {
8581 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8582 continue;
8583 }
8584
5afb4e99
DJ
8585 /* Check whether this DIE is interesting enough to save. Normally
8586 we would not be interested in members here, but there may be
8587 later variables referencing them via DW_AT_specification (for
8588 static members). */
8589 if (!load_all
8590 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8591 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8592 && abbrev->tag != DW_TAG_enumerator
8593 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8594 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8595 && abbrev->tag != DW_TAG_variable
5afb4e99 8596 && abbrev->tag != DW_TAG_namespace
f55ee35c 8597 && abbrev->tag != DW_TAG_module
5afb4e99 8598 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8599 {
8600 /* Otherwise we skip to the next sibling, if any. */
93311388 8601 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8602 continue;
8603 }
8604
93311388
DE
8605 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8606 buffer, info_ptr, cu);
72bf9492
DJ
8607
8608 /* This two-pass algorithm for processing partial symbols has a
8609 high cost in cache pressure. Thus, handle some simple cases
8610 here which cover the majority of C partial symbols. DIEs
8611 which neither have specification tags in them, nor could have
8612 specification tags elsewhere pointing at them, can simply be
8613 processed and discarded.
8614
8615 This segment is also optional; scan_partial_symbols and
8616 add_partial_symbol will handle these DIEs if we chain
8617 them in normally. When compilers which do not emit large
8618 quantities of duplicate debug information are more common,
8619 this code can probably be removed. */
8620
8621 /* Any complete simple types at the top level (pretty much all
8622 of them, for a language without namespaces), can be processed
8623 directly. */
8624 if (parent_die == NULL
8625 && part_die->has_specification == 0
8626 && part_die->is_declaration == 0
8627 && (part_die->tag == DW_TAG_typedef
8628 || part_die->tag == DW_TAG_base_type
8629 || part_die->tag == DW_TAG_subrange_type))
8630 {
8631 if (building_psymtab && part_die->name != NULL)
04a679b8 8632 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8633 VAR_DOMAIN, LOC_TYPEDEF,
8634 &cu->objfile->static_psymbols,
8635 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8636 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8637 continue;
8638 }
8639
8640 /* If we're at the second level, and we're an enumerator, and
8641 our parent has no specification (meaning possibly lives in a
8642 namespace elsewhere), then we can add the partial symbol now
8643 instead of queueing it. */
8644 if (part_die->tag == DW_TAG_enumerator
8645 && parent_die != NULL
8646 && parent_die->die_parent == NULL
8647 && parent_die->tag == DW_TAG_enumeration_type
8648 && parent_die->has_specification == 0)
8649 {
8650 if (part_die->name == NULL)
e2e0b3e5 8651 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8652 else if (building_psymtab)
04a679b8 8653 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8654 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8655 (cu->language == language_cplus
8656 || cu->language == language_java)
72bf9492
DJ
8657 ? &cu->objfile->global_psymbols
8658 : &cu->objfile->static_psymbols,
8659 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8660
93311388 8661 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8662 continue;
8663 }
8664
8665 /* We'll save this DIE so link it in. */
8666 part_die->die_parent = parent_die;
8667 part_die->die_sibling = NULL;
8668 part_die->die_child = NULL;
8669
8670 if (last_die && last_die == parent_die)
8671 last_die->die_child = part_die;
8672 else if (last_die)
8673 last_die->die_sibling = part_die;
8674
8675 last_die = part_die;
8676
8677 if (first_die == NULL)
8678 first_die = part_die;
8679
8680 /* Maybe add the DIE to the hash table. Not all DIEs that we
8681 find interesting need to be in the hash table, because we
8682 also have the parent/sibling/child chains; only those that we
8683 might refer to by offset later during partial symbol reading.
8684
8685 For now this means things that might have be the target of a
8686 DW_AT_specification, DW_AT_abstract_origin, or
8687 DW_AT_extension. DW_AT_extension will refer only to
8688 namespaces; DW_AT_abstract_origin refers to functions (and
8689 many things under the function DIE, but we do not recurse
8690 into function DIEs during partial symbol reading) and
8691 possibly variables as well; DW_AT_specification refers to
8692 declarations. Declarations ought to have the DW_AT_declaration
8693 flag. It happens that GCC forgets to put it in sometimes, but
8694 only for functions, not for types.
8695
8696 Adding more things than necessary to the hash table is harmless
8697 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8698 wasted time in find_partial_die, when we reread the compilation
8699 unit with load_all_dies set. */
72bf9492 8700
5afb4e99 8701 if (load_all
72929c62 8702 || abbrev->tag == DW_TAG_constant
5afb4e99 8703 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8704 || abbrev->tag == DW_TAG_variable
8705 || abbrev->tag == DW_TAG_namespace
8706 || part_die->is_declaration)
8707 {
8708 void **slot;
8709
8710 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8711 part_die->offset, INSERT);
8712 *slot = part_die;
8713 }
8714
8715 part_die = obstack_alloc (&cu->comp_unit_obstack,
8716 sizeof (struct partial_die_info));
8717
8718 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8719 we have no reason to follow the children of structures; for other
98bfdba5
PA
8720 languages we have to, so that we can get at method physnames
8721 to infer fully qualified class names, for DW_AT_specification,
8722 and for C++ template arguments. For C++, we also look one level
8723 inside functions to find template arguments (if the name of the
8724 function does not already contain the template arguments).
bc30ff58
JB
8725
8726 For Ada, we need to scan the children of subprograms and lexical
8727 blocks as well because Ada allows the definition of nested
8728 entities that could be interesting for the debugger, such as
8729 nested subprograms for instance. */
72bf9492 8730 if (last_die->has_children
5afb4e99
DJ
8731 && (load_all
8732 || last_die->tag == DW_TAG_namespace
f55ee35c 8733 || last_die->tag == DW_TAG_module
72bf9492 8734 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8735 || (cu->language == language_cplus
8736 && last_die->tag == DW_TAG_subprogram
8737 && (last_die->name == NULL
8738 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8739 || (cu->language != language_c
8740 && (last_die->tag == DW_TAG_class_type
680b30c7 8741 || last_die->tag == DW_TAG_interface_type
72bf9492 8742 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8743 || last_die->tag == DW_TAG_union_type))
8744 || (cu->language == language_ada
8745 && (last_die->tag == DW_TAG_subprogram
8746 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8747 {
8748 nesting_level++;
8749 parent_die = last_die;
8750 continue;
8751 }
8752
8753 /* Otherwise we skip to the next sibling, if any. */
93311388 8754 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8755
8756 /* Back to the top, do it again. */
8757 }
8758}
8759
c906108c
SS
8760/* Read a minimal amount of information into the minimal die structure. */
8761
fe1b8b76 8762static gdb_byte *
72bf9492
DJ
8763read_partial_die (struct partial_die_info *part_die,
8764 struct abbrev_info *abbrev,
8765 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8766 gdb_byte *buffer, gdb_byte *info_ptr,
8767 struct dwarf2_cu *cu)
c906108c 8768{
fa238c03 8769 unsigned int i;
c906108c 8770 struct attribute attr;
c5aa993b 8771 int has_low_pc_attr = 0;
c906108c
SS
8772 int has_high_pc_attr = 0;
8773
72bf9492 8774 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8775
93311388 8776 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8777
8778 info_ptr += abbrev_len;
8779
8780 if (abbrev == NULL)
8781 return info_ptr;
8782
c906108c
SS
8783 part_die->tag = abbrev->tag;
8784 part_die->has_children = abbrev->has_children;
c906108c
SS
8785
8786 for (i = 0; i < abbrev->num_attrs; ++i)
8787 {
e7c27a73 8788 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8789
8790 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8791 partial symbol table. */
c906108c
SS
8792 switch (attr.name)
8793 {
8794 case DW_AT_name:
71c25dea
TT
8795 switch (part_die->tag)
8796 {
8797 case DW_TAG_compile_unit:
348e048f 8798 case DW_TAG_type_unit:
71c25dea
TT
8799 /* Compilation units have a DW_AT_name that is a filename, not
8800 a source language identifier. */
8801 case DW_TAG_enumeration_type:
8802 case DW_TAG_enumerator:
8803 /* These tags always have simple identifiers already; no need
8804 to canonicalize them. */
8805 part_die->name = DW_STRING (&attr);
8806 break;
8807 default:
8808 part_die->name
8809 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8810 &cu->objfile->objfile_obstack);
71c25dea
TT
8811 break;
8812 }
c906108c 8813 break;
31ef98ae 8814 case DW_AT_linkage_name:
c906108c 8815 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8816 /* Note that both forms of linkage name might appear. We
8817 assume they will be the same, and we only store the last
8818 one we see. */
94af9270
KS
8819 if (cu->language == language_ada)
8820 part_die->name = DW_STRING (&attr);
abc72ce4 8821 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8822 break;
8823 case DW_AT_low_pc:
8824 has_low_pc_attr = 1;
8825 part_die->lowpc = DW_ADDR (&attr);
8826 break;
8827 case DW_AT_high_pc:
8828 has_high_pc_attr = 1;
8829 part_die->highpc = DW_ADDR (&attr);
8830 break;
8831 case DW_AT_location:
8e19ed76
PS
8832 /* Support the .debug_loc offsets */
8833 if (attr_form_is_block (&attr))
8834 {
8835 part_die->locdesc = DW_BLOCK (&attr);
8836 }
3690dd37 8837 else if (attr_form_is_section_offset (&attr))
8e19ed76 8838 {
4d3c2250 8839 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8840 }
8841 else
8842 {
4d3c2250
KB
8843 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8844 "partial symbol information");
8e19ed76 8845 }
c906108c 8846 break;
c906108c
SS
8847 case DW_AT_external:
8848 part_die->is_external = DW_UNSND (&attr);
8849 break;
8850 case DW_AT_declaration:
8851 part_die->is_declaration = DW_UNSND (&attr);
8852 break;
8853 case DW_AT_type:
8854 part_die->has_type = 1;
8855 break;
8856 case DW_AT_abstract_origin:
8857 case DW_AT_specification:
72bf9492
DJ
8858 case DW_AT_extension:
8859 part_die->has_specification = 1;
c764a876 8860 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8861 break;
8862 case DW_AT_sibling:
8863 /* Ignore absolute siblings, they might point outside of
8864 the current compile unit. */
8865 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8866 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8867 else
93311388 8868 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8869 break;
fa4028e9
JB
8870 case DW_AT_byte_size:
8871 part_die->has_byte_size = 1;
8872 break;
68511cec
CES
8873 case DW_AT_calling_convention:
8874 /* DWARF doesn't provide a way to identify a program's source-level
8875 entry point. DW_AT_calling_convention attributes are only meant
8876 to describe functions' calling conventions.
8877
8878 However, because it's a necessary piece of information in
8879 Fortran, and because DW_CC_program is the only piece of debugging
8880 information whose definition refers to a 'main program' at all,
8881 several compilers have begun marking Fortran main programs with
8882 DW_CC_program --- even when those functions use the standard
8883 calling conventions.
8884
8885 So until DWARF specifies a way to provide this information and
8886 compilers pick up the new representation, we'll support this
8887 practice. */
8888 if (DW_UNSND (&attr) == DW_CC_program
8889 && cu->language == language_fortran)
01f8c46d
JK
8890 {
8891 set_main_name (part_die->name);
8892
8893 /* As this DIE has a static linkage the name would be difficult
8894 to look up later. */
8895 language_of_main = language_fortran;
8896 }
68511cec 8897 break;
c906108c
SS
8898 default:
8899 break;
8900 }
8901 }
8902
c906108c
SS
8903 /* When using the GNU linker, .gnu.linkonce. sections are used to
8904 eliminate duplicate copies of functions and vtables and such.
8905 The linker will arbitrarily choose one and discard the others.
8906 The AT_*_pc values for such functions refer to local labels in
8907 these sections. If the section from that file was discarded, the
8908 labels are not in the output, so the relocs get a value of 0.
8909 If this is a discarded function, mark the pc bounds as invalid,
8910 so that GDB will ignore it. */
8911 if (has_low_pc_attr && has_high_pc_attr
8912 && part_die->lowpc < part_die->highpc
8913 && (part_die->lowpc != 0
72dca2f5 8914 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8915 part_die->has_pc_info = 1;
85cbf3d3 8916
c906108c
SS
8917 return info_ptr;
8918}
8919
72bf9492
DJ
8920/* Find a cached partial DIE at OFFSET in CU. */
8921
8922static struct partial_die_info *
c764a876 8923find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8924{
8925 struct partial_die_info *lookup_die = NULL;
8926 struct partial_die_info part_die;
8927
8928 part_die.offset = offset;
8929 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8930
72bf9492
DJ
8931 return lookup_die;
8932}
8933
348e048f
DE
8934/* Find a partial DIE at OFFSET, which may or may not be in CU,
8935 except in the case of .debug_types DIEs which do not reference
8936 outside their CU (they do however referencing other types via
8937 DW_FORM_sig8). */
72bf9492
DJ
8938
8939static struct partial_die_info *
c764a876 8940find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8941{
5afb4e99
DJ
8942 struct dwarf2_per_cu_data *per_cu = NULL;
8943 struct partial_die_info *pd = NULL;
72bf9492 8944
348e048f
DE
8945 if (cu->per_cu->from_debug_types)
8946 {
8947 pd = find_partial_die_in_comp_unit (offset, cu);
8948 if (pd != NULL)
8949 return pd;
8950 goto not_found;
8951 }
8952
45452591 8953 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8954 {
8955 pd = find_partial_die_in_comp_unit (offset, cu);
8956 if (pd != NULL)
8957 return pd;
8958 }
72bf9492 8959
ae038cb0
DJ
8960 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8961
98bfdba5
PA
8962 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8963 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8964
8965 per_cu->cu->last_used = 0;
5afb4e99
DJ
8966 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8967
8968 if (pd == NULL && per_cu->load_all_dies == 0)
8969 {
8970 struct cleanup *back_to;
8971 struct partial_die_info comp_unit_die;
8972 struct abbrev_info *abbrev;
8973 unsigned int bytes_read;
8974 char *info_ptr;
8975
8976 per_cu->load_all_dies = 1;
8977
8978 /* Re-read the DIEs. */
8979 back_to = make_cleanup (null_cleanup, 0);
8980 if (per_cu->cu->dwarf2_abbrevs == NULL)
8981 {
8982 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8983 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8984 }
dce234bc 8985 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8986 + per_cu->cu->header.offset
8987 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8988 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8989 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8990 per_cu->cu->objfile->obfd,
8991 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8992 per_cu->cu);
8993 if (comp_unit_die.has_children)
93311388
DE
8994 load_partial_dies (per_cu->cu->objfile->obfd,
8995 dwarf2_per_objfile->info.buffer, info_ptr,
8996 0, per_cu->cu);
5afb4e99
DJ
8997 do_cleanups (back_to);
8998
8999 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9000 }
9001
348e048f
DE
9002 not_found:
9003
5afb4e99
DJ
9004 if (pd == NULL)
9005 internal_error (__FILE__, __LINE__,
c764a876 9006 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
9007 offset, bfd_get_filename (cu->objfile->obfd));
9008 return pd;
72bf9492
DJ
9009}
9010
abc72ce4
DE
9011/* See if we can figure out if the class lives in a namespace. We do
9012 this by looking for a member function; its demangled name will
9013 contain namespace info, if there is any. */
9014
9015static void
9016guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9017 struct dwarf2_cu *cu)
9018{
9019 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9020 what template types look like, because the demangler
9021 frequently doesn't give the same name as the debug info. We
9022 could fix this by only using the demangled name to get the
9023 prefix (but see comment in read_structure_type). */
9024
9025 struct partial_die_info *real_pdi;
9026 struct partial_die_info *child_pdi;
9027
9028 /* If this DIE (this DIE's specification, if any) has a parent, then
9029 we should not do this. We'll prepend the parent's fully qualified
9030 name when we create the partial symbol. */
9031
9032 real_pdi = struct_pdi;
9033 while (real_pdi->has_specification)
9034 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9035
9036 if (real_pdi->die_parent != NULL)
9037 return;
9038
9039 for (child_pdi = struct_pdi->die_child;
9040 child_pdi != NULL;
9041 child_pdi = child_pdi->die_sibling)
9042 {
9043 if (child_pdi->tag == DW_TAG_subprogram
9044 && child_pdi->linkage_name != NULL)
9045 {
9046 char *actual_class_name
9047 = language_class_name_from_physname (cu->language_defn,
9048 child_pdi->linkage_name);
9049 if (actual_class_name != NULL)
9050 {
9051 struct_pdi->name
9052 = obsavestring (actual_class_name,
9053 strlen (actual_class_name),
9054 &cu->objfile->objfile_obstack);
9055 xfree (actual_class_name);
9056 }
9057 break;
9058 }
9059 }
9060}
9061
72bf9492
DJ
9062/* Adjust PART_DIE before generating a symbol for it. This function
9063 may set the is_external flag or change the DIE's name. */
9064
9065static void
9066fixup_partial_die (struct partial_die_info *part_die,
9067 struct dwarf2_cu *cu)
9068{
abc72ce4
DE
9069 /* Once we've fixed up a die, there's no point in doing so again.
9070 This also avoids a memory leak if we were to call
9071 guess_partial_die_structure_name multiple times. */
9072 if (part_die->fixup_called)
9073 return;
9074
72bf9492
DJ
9075 /* If we found a reference attribute and the DIE has no name, try
9076 to find a name in the referred to DIE. */
9077
9078 if (part_die->name == NULL && part_die->has_specification)
9079 {
9080 struct partial_die_info *spec_die;
72bf9492 9081
10b3939b 9082 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9083
10b3939b 9084 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9085
9086 if (spec_die->name)
9087 {
9088 part_die->name = spec_die->name;
9089
9090 /* Copy DW_AT_external attribute if it is set. */
9091 if (spec_die->is_external)
9092 part_die->is_external = spec_die->is_external;
9093 }
9094 }
9095
9096 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9097
9098 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9099 part_die->name = "(anonymous namespace)";
9100
abc72ce4
DE
9101 /* If there is no parent die to provide a namespace, and there are
9102 children, see if we can determine the namespace from their linkage
9103 name.
9104 NOTE: We need to do this even if cu->has_namespace_info != 0.
9105 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9106 if (cu->language == language_cplus
9107 && dwarf2_per_objfile->types.asection != NULL
9108 && part_die->die_parent == NULL
9109 && part_die->has_children
9110 && (part_die->tag == DW_TAG_class_type
9111 || part_die->tag == DW_TAG_structure_type
9112 || part_die->tag == DW_TAG_union_type))
9113 guess_partial_die_structure_name (part_die, cu);
9114
9115 part_die->fixup_called = 1;
72bf9492
DJ
9116}
9117
a8329558 9118/* Read an attribute value described by an attribute form. */
c906108c 9119
fe1b8b76 9120static gdb_byte *
a8329558 9121read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9122 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9123 struct dwarf2_cu *cu)
c906108c 9124{
e7c27a73 9125 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9126 unsigned int bytes_read;
9127 struct dwarf_block *blk;
9128
a8329558
KW
9129 attr->form = form;
9130 switch (form)
c906108c 9131 {
c906108c 9132 case DW_FORM_ref_addr:
ae411497
TT
9133 if (cu->header.version == 2)
9134 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9135 else
9136 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9137 info_ptr += bytes_read;
9138 break;
9139 case DW_FORM_addr:
e7c27a73 9140 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9141 info_ptr += bytes_read;
c906108c
SS
9142 break;
9143 case DW_FORM_block2:
7b5a2f43 9144 blk = dwarf_alloc_block (cu);
c906108c
SS
9145 blk->size = read_2_bytes (abfd, info_ptr);
9146 info_ptr += 2;
9147 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9148 info_ptr += blk->size;
9149 DW_BLOCK (attr) = blk;
9150 break;
9151 case DW_FORM_block4:
7b5a2f43 9152 blk = dwarf_alloc_block (cu);
c906108c
SS
9153 blk->size = read_4_bytes (abfd, info_ptr);
9154 info_ptr += 4;
9155 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9156 info_ptr += blk->size;
9157 DW_BLOCK (attr) = blk;
9158 break;
9159 case DW_FORM_data2:
9160 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9161 info_ptr += 2;
9162 break;
9163 case DW_FORM_data4:
9164 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9165 info_ptr += 4;
9166 break;
9167 case DW_FORM_data8:
9168 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9169 info_ptr += 8;
9170 break;
2dc7f7b3
TT
9171 case DW_FORM_sec_offset:
9172 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9173 info_ptr += bytes_read;
9174 break;
c906108c 9175 case DW_FORM_string:
9b1c24c8 9176 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9177 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9178 info_ptr += bytes_read;
9179 break;
4bdf3d34
JJ
9180 case DW_FORM_strp:
9181 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9182 &bytes_read);
8285870a 9183 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9184 info_ptr += bytes_read;
9185 break;
2dc7f7b3 9186 case DW_FORM_exprloc:
c906108c 9187 case DW_FORM_block:
7b5a2f43 9188 blk = dwarf_alloc_block (cu);
c906108c
SS
9189 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9190 info_ptr += bytes_read;
9191 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9192 info_ptr += blk->size;
9193 DW_BLOCK (attr) = blk;
9194 break;
9195 case DW_FORM_block1:
7b5a2f43 9196 blk = dwarf_alloc_block (cu);
c906108c
SS
9197 blk->size = read_1_byte (abfd, info_ptr);
9198 info_ptr += 1;
9199 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9200 info_ptr += blk->size;
9201 DW_BLOCK (attr) = blk;
9202 break;
9203 case DW_FORM_data1:
9204 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9205 info_ptr += 1;
9206 break;
9207 case DW_FORM_flag:
9208 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9209 info_ptr += 1;
9210 break;
2dc7f7b3
TT
9211 case DW_FORM_flag_present:
9212 DW_UNSND (attr) = 1;
9213 break;
c906108c
SS
9214 case DW_FORM_sdata:
9215 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9216 info_ptr += bytes_read;
9217 break;
9218 case DW_FORM_udata:
9219 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9220 info_ptr += bytes_read;
9221 break;
9222 case DW_FORM_ref1:
10b3939b 9223 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9224 info_ptr += 1;
9225 break;
9226 case DW_FORM_ref2:
10b3939b 9227 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9228 info_ptr += 2;
9229 break;
9230 case DW_FORM_ref4:
10b3939b 9231 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9232 info_ptr += 4;
9233 break;
613e1657 9234 case DW_FORM_ref8:
10b3939b 9235 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9236 info_ptr += 8;
9237 break;
348e048f
DE
9238 case DW_FORM_sig8:
9239 /* Convert the signature to something we can record in DW_UNSND
9240 for later lookup.
9241 NOTE: This is NULL if the type wasn't found. */
9242 DW_SIGNATURED_TYPE (attr) =
9243 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9244 info_ptr += 8;
9245 break;
c906108c 9246 case DW_FORM_ref_udata:
10b3939b
DJ
9247 DW_ADDR (attr) = (cu->header.offset
9248 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9249 info_ptr += bytes_read;
9250 break;
c906108c 9251 case DW_FORM_indirect:
a8329558
KW
9252 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9253 info_ptr += bytes_read;
e7c27a73 9254 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9255 break;
c906108c 9256 default:
8a3fe4f8 9257 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9258 dwarf_form_name (form),
9259 bfd_get_filename (abfd));
c906108c 9260 }
28e94949
JB
9261
9262 /* We have seen instances where the compiler tried to emit a byte
9263 size attribute of -1 which ended up being encoded as an unsigned
9264 0xffffffff. Although 0xffffffff is technically a valid size value,
9265 an object of this size seems pretty unlikely so we can relatively
9266 safely treat these cases as if the size attribute was invalid and
9267 treat them as zero by default. */
9268 if (attr->name == DW_AT_byte_size
9269 && form == DW_FORM_data4
9270 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9271 {
9272 complaint
9273 (&symfile_complaints,
43bbcdc2
PH
9274 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9275 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9276 DW_UNSND (attr) = 0;
9277 }
28e94949 9278
c906108c
SS
9279 return info_ptr;
9280}
9281
a8329558
KW
9282/* Read an attribute described by an abbreviated attribute. */
9283
fe1b8b76 9284static gdb_byte *
a8329558 9285read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9286 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9287{
9288 attr->name = abbrev->name;
e7c27a73 9289 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9290}
9291
c906108c
SS
9292/* read dwarf information from a buffer */
9293
9294static unsigned int
fe1b8b76 9295read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9296{
fe1b8b76 9297 return bfd_get_8 (abfd, buf);
c906108c
SS
9298}
9299
9300static int
fe1b8b76 9301read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9302{
fe1b8b76 9303 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9304}
9305
9306static unsigned int
fe1b8b76 9307read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9308{
fe1b8b76 9309 return bfd_get_16 (abfd, buf);
c906108c
SS
9310}
9311
9312static int
fe1b8b76 9313read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9314{
fe1b8b76 9315 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9316}
9317
9318static unsigned int
fe1b8b76 9319read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9320{
fe1b8b76 9321 return bfd_get_32 (abfd, buf);
c906108c
SS
9322}
9323
9324static int
fe1b8b76 9325read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9326{
fe1b8b76 9327 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9328}
9329
93311388 9330static ULONGEST
fe1b8b76 9331read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9332{
fe1b8b76 9333 return bfd_get_64 (abfd, buf);
c906108c
SS
9334}
9335
9336static CORE_ADDR
fe1b8b76 9337read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9338 unsigned int *bytes_read)
c906108c 9339{
e7c27a73 9340 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9341 CORE_ADDR retval = 0;
9342
107d2387 9343 if (cu_header->signed_addr_p)
c906108c 9344 {
107d2387
AC
9345 switch (cu_header->addr_size)
9346 {
9347 case 2:
fe1b8b76 9348 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9349 break;
9350 case 4:
fe1b8b76 9351 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9352 break;
9353 case 8:
fe1b8b76 9354 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9355 break;
9356 default:
8e65ff28 9357 internal_error (__FILE__, __LINE__,
e2e0b3e5 9358 _("read_address: bad switch, signed [in module %s]"),
659b0389 9359 bfd_get_filename (abfd));
107d2387
AC
9360 }
9361 }
9362 else
9363 {
9364 switch (cu_header->addr_size)
9365 {
9366 case 2:
fe1b8b76 9367 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9368 break;
9369 case 4:
fe1b8b76 9370 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9371 break;
9372 case 8:
fe1b8b76 9373 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9374 break;
9375 default:
8e65ff28 9376 internal_error (__FILE__, __LINE__,
e2e0b3e5 9377 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9378 bfd_get_filename (abfd));
107d2387 9379 }
c906108c 9380 }
64367e0a 9381
107d2387
AC
9382 *bytes_read = cu_header->addr_size;
9383 return retval;
c906108c
SS
9384}
9385
f7ef9339 9386/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9387 specification allows the initial length to take up either 4 bytes
9388 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9389 bytes describe the length and all offsets will be 8 bytes in length
9390 instead of 4.
9391
f7ef9339
KB
9392 An older, non-standard 64-bit format is also handled by this
9393 function. The older format in question stores the initial length
9394 as an 8-byte quantity without an escape value. Lengths greater
9395 than 2^32 aren't very common which means that the initial 4 bytes
9396 is almost always zero. Since a length value of zero doesn't make
9397 sense for the 32-bit format, this initial zero can be considered to
9398 be an escape value which indicates the presence of the older 64-bit
9399 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9400 greater than 4GB. If it becomes necessary to handle lengths
9401 somewhat larger than 4GB, we could allow other small values (such
9402 as the non-sensical values of 1, 2, and 3) to also be used as
9403 escape values indicating the presence of the old format.
f7ef9339 9404
917c78fc
MK
9405 The value returned via bytes_read should be used to increment the
9406 relevant pointer after calling read_initial_length().
c764a876 9407
613e1657
KB
9408 [ Note: read_initial_length() and read_offset() are based on the
9409 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9410 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9411 from:
9412
f7ef9339 9413 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9414
613e1657
KB
9415 This document is only a draft and is subject to change. (So beware.)
9416
f7ef9339 9417 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9418 determined empirically by examining 64-bit ELF files produced by
9419 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9420
9421 - Kevin, July 16, 2002
613e1657
KB
9422 ] */
9423
9424static LONGEST
c764a876 9425read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9426{
fe1b8b76 9427 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9428
dd373385 9429 if (length == 0xffffffff)
613e1657 9430 {
fe1b8b76 9431 length = bfd_get_64 (abfd, buf + 4);
613e1657 9432 *bytes_read = 12;
613e1657 9433 }
dd373385 9434 else if (length == 0)
f7ef9339 9435 {
dd373385 9436 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9437 length = bfd_get_64 (abfd, buf);
f7ef9339 9438 *bytes_read = 8;
f7ef9339 9439 }
613e1657
KB
9440 else
9441 {
9442 *bytes_read = 4;
613e1657
KB
9443 }
9444
c764a876
DE
9445 return length;
9446}
dd373385 9447
c764a876
DE
9448/* Cover function for read_initial_length.
9449 Returns the length of the object at BUF, and stores the size of the
9450 initial length in *BYTES_READ and stores the size that offsets will be in
9451 *OFFSET_SIZE.
9452 If the initial length size is not equivalent to that specified in
9453 CU_HEADER then issue a complaint.
9454 This is useful when reading non-comp-unit headers. */
dd373385 9455
c764a876
DE
9456static LONGEST
9457read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9458 const struct comp_unit_head *cu_header,
9459 unsigned int *bytes_read,
9460 unsigned int *offset_size)
9461{
9462 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9463
9464 gdb_assert (cu_header->initial_length_size == 4
9465 || cu_header->initial_length_size == 8
9466 || cu_header->initial_length_size == 12);
9467
9468 if (cu_header->initial_length_size != *bytes_read)
9469 complaint (&symfile_complaints,
9470 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9471
c764a876 9472 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9473 return length;
613e1657
KB
9474}
9475
9476/* Read an offset from the data stream. The size of the offset is
917c78fc 9477 given by cu_header->offset_size. */
613e1657
KB
9478
9479static LONGEST
fe1b8b76 9480read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9481 unsigned int *bytes_read)
c764a876
DE
9482{
9483 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9484
c764a876
DE
9485 *bytes_read = cu_header->offset_size;
9486 return offset;
9487}
9488
9489/* Read an offset from the data stream. */
9490
9491static LONGEST
9492read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9493{
9494 LONGEST retval = 0;
9495
c764a876 9496 switch (offset_size)
613e1657
KB
9497 {
9498 case 4:
fe1b8b76 9499 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9500 break;
9501 case 8:
fe1b8b76 9502 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9503 break;
9504 default:
8e65ff28 9505 internal_error (__FILE__, __LINE__,
c764a876 9506 _("read_offset_1: bad switch [in module %s]"),
659b0389 9507 bfd_get_filename (abfd));
613e1657
KB
9508 }
9509
917c78fc 9510 return retval;
613e1657
KB
9511}
9512
fe1b8b76
JB
9513static gdb_byte *
9514read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9515{
9516 /* If the size of a host char is 8 bits, we can return a pointer
9517 to the buffer, otherwise we have to copy the data to a buffer
9518 allocated on the temporary obstack. */
4bdf3d34 9519 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9520 return buf;
c906108c
SS
9521}
9522
9523static char *
9b1c24c8 9524read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9525{
9526 /* If the size of a host char is 8 bits, we can return a pointer
9527 to the string, otherwise we have to copy the string to a buffer
9528 allocated on the temporary obstack. */
4bdf3d34 9529 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9530 if (*buf == '\0')
9531 {
9532 *bytes_read_ptr = 1;
9533 return NULL;
9534 }
fe1b8b76
JB
9535 *bytes_read_ptr = strlen ((char *) buf) + 1;
9536 return (char *) buf;
4bdf3d34
JJ
9537}
9538
9539static char *
fe1b8b76 9540read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9541 const struct comp_unit_head *cu_header,
9542 unsigned int *bytes_read_ptr)
9543{
c764a876 9544 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9545
be391dca 9546 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9547 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9548 {
8a3fe4f8 9549 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9550 bfd_get_filename (abfd));
4bdf3d34 9551 return NULL;
c906108c 9552 }
dce234bc 9553 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9554 {
8a3fe4f8 9555 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9556 bfd_get_filename (abfd));
c906108c
SS
9557 return NULL;
9558 }
4bdf3d34 9559 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9560 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9561 return NULL;
dce234bc 9562 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9563}
9564
ce5d95e1 9565static unsigned long
fe1b8b76 9566read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9567{
ce5d95e1
JB
9568 unsigned long result;
9569 unsigned int num_read;
c906108c
SS
9570 int i, shift;
9571 unsigned char byte;
9572
9573 result = 0;
9574 shift = 0;
9575 num_read = 0;
9576 i = 0;
9577 while (1)
9578 {
fe1b8b76 9579 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9580 buf++;
9581 num_read++;
ce5d95e1 9582 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9583 if ((byte & 128) == 0)
9584 {
9585 break;
9586 }
9587 shift += 7;
9588 }
9589 *bytes_read_ptr = num_read;
9590 return result;
9591}
9592
ce5d95e1 9593static long
fe1b8b76 9594read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9595{
ce5d95e1 9596 long result;
77e0b926 9597 int i, shift, num_read;
c906108c
SS
9598 unsigned char byte;
9599
9600 result = 0;
9601 shift = 0;
c906108c
SS
9602 num_read = 0;
9603 i = 0;
9604 while (1)
9605 {
fe1b8b76 9606 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9607 buf++;
9608 num_read++;
ce5d95e1 9609 result |= ((long)(byte & 127) << shift);
c906108c
SS
9610 shift += 7;
9611 if ((byte & 128) == 0)
9612 {
9613 break;
9614 }
9615 }
77e0b926
DJ
9616 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9617 result |= -(((long)1) << shift);
c906108c
SS
9618 *bytes_read_ptr = num_read;
9619 return result;
9620}
9621
4bb7a0a7
DJ
9622/* Return a pointer to just past the end of an LEB128 number in BUF. */
9623
fe1b8b76
JB
9624static gdb_byte *
9625skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9626{
9627 int byte;
9628
9629 while (1)
9630 {
fe1b8b76 9631 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9632 buf++;
9633 if ((byte & 128) == 0)
9634 return buf;
9635 }
9636}
9637
c906108c 9638static void
e142c38c 9639set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9640{
9641 switch (lang)
9642 {
9643 case DW_LANG_C89:
76bee0cc 9644 case DW_LANG_C99:
c906108c 9645 case DW_LANG_C:
e142c38c 9646 cu->language = language_c;
c906108c
SS
9647 break;
9648 case DW_LANG_C_plus_plus:
e142c38c 9649 cu->language = language_cplus;
c906108c 9650 break;
6aecb9c2
JB
9651 case DW_LANG_D:
9652 cu->language = language_d;
9653 break;
c906108c
SS
9654 case DW_LANG_Fortran77:
9655 case DW_LANG_Fortran90:
b21b22e0 9656 case DW_LANG_Fortran95:
e142c38c 9657 cu->language = language_fortran;
c906108c
SS
9658 break;
9659 case DW_LANG_Mips_Assembler:
e142c38c 9660 cu->language = language_asm;
c906108c 9661 break;
bebd888e 9662 case DW_LANG_Java:
e142c38c 9663 cu->language = language_java;
bebd888e 9664 break;
c906108c 9665 case DW_LANG_Ada83:
8aaf0b47 9666 case DW_LANG_Ada95:
bc5f45f8
JB
9667 cu->language = language_ada;
9668 break;
72019c9c
GM
9669 case DW_LANG_Modula2:
9670 cu->language = language_m2;
9671 break;
fe8e67fd
PM
9672 case DW_LANG_Pascal83:
9673 cu->language = language_pascal;
9674 break;
22566fbd
DJ
9675 case DW_LANG_ObjC:
9676 cu->language = language_objc;
9677 break;
c906108c
SS
9678 case DW_LANG_Cobol74:
9679 case DW_LANG_Cobol85:
c906108c 9680 default:
e142c38c 9681 cu->language = language_minimal;
c906108c
SS
9682 break;
9683 }
e142c38c 9684 cu->language_defn = language_def (cu->language);
c906108c
SS
9685}
9686
9687/* Return the named attribute or NULL if not there. */
9688
9689static struct attribute *
e142c38c 9690dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9691{
9692 unsigned int i;
9693 struct attribute *spec = NULL;
9694
9695 for (i = 0; i < die->num_attrs; ++i)
9696 {
9697 if (die->attrs[i].name == name)
10b3939b 9698 return &die->attrs[i];
c906108c
SS
9699 if (die->attrs[i].name == DW_AT_specification
9700 || die->attrs[i].name == DW_AT_abstract_origin)
9701 spec = &die->attrs[i];
9702 }
c906108c 9703
10b3939b 9704 if (spec)
f2f0e013
DJ
9705 {
9706 die = follow_die_ref (die, spec, &cu);
9707 return dwarf2_attr (die, name, cu);
9708 }
c5aa993b 9709
c906108c
SS
9710 return NULL;
9711}
9712
348e048f
DE
9713/* Return the named attribute or NULL if not there,
9714 but do not follow DW_AT_specification, etc.
9715 This is for use in contexts where we're reading .debug_types dies.
9716 Following DW_AT_specification, DW_AT_abstract_origin will take us
9717 back up the chain, and we want to go down. */
9718
9719static struct attribute *
9720dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9721 struct dwarf2_cu *cu)
9722{
9723 unsigned int i;
9724
9725 for (i = 0; i < die->num_attrs; ++i)
9726 if (die->attrs[i].name == name)
9727 return &die->attrs[i];
9728
9729 return NULL;
9730}
9731
05cf31d1
JB
9732/* Return non-zero iff the attribute NAME is defined for the given DIE,
9733 and holds a non-zero value. This function should only be used for
2dc7f7b3 9734 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9735
9736static int
9737dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9738{
9739 struct attribute *attr = dwarf2_attr (die, name, cu);
9740
9741 return (attr && DW_UNSND (attr));
9742}
9743
3ca72b44 9744static int
e142c38c 9745die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9746{
05cf31d1
JB
9747 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9748 which value is non-zero. However, we have to be careful with
9749 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9750 (via dwarf2_flag_true_p) follows this attribute. So we may
9751 end up accidently finding a declaration attribute that belongs
9752 to a different DIE referenced by the specification attribute,
9753 even though the given DIE does not have a declaration attribute. */
9754 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9755 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9756}
9757
63d06c5c 9758/* Return the die giving the specification for DIE, if there is
f2f0e013 9759 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9760 containing the return value on output. If there is no
9761 specification, but there is an abstract origin, that is
9762 returned. */
63d06c5c
DC
9763
9764static struct die_info *
f2f0e013 9765die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9766{
f2f0e013
DJ
9767 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9768 *spec_cu);
63d06c5c 9769
edb3359d
DJ
9770 if (spec_attr == NULL)
9771 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9772
63d06c5c
DC
9773 if (spec_attr == NULL)
9774 return NULL;
9775 else
f2f0e013 9776 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9777}
c906108c 9778
debd256d
JB
9779/* Free the line_header structure *LH, and any arrays and strings it
9780 refers to. */
9781static void
9782free_line_header (struct line_header *lh)
9783{
9784 if (lh->standard_opcode_lengths)
a8bc7b56 9785 xfree (lh->standard_opcode_lengths);
debd256d
JB
9786
9787 /* Remember that all the lh->file_names[i].name pointers are
9788 pointers into debug_line_buffer, and don't need to be freed. */
9789 if (lh->file_names)
a8bc7b56 9790 xfree (lh->file_names);
debd256d
JB
9791
9792 /* Similarly for the include directory names. */
9793 if (lh->include_dirs)
a8bc7b56 9794 xfree (lh->include_dirs);
debd256d 9795
a8bc7b56 9796 xfree (lh);
debd256d
JB
9797}
9798
9799
9800/* Add an entry to LH's include directory table. */
9801static void
9802add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9803{
debd256d
JB
9804 /* Grow the array if necessary. */
9805 if (lh->include_dirs_size == 0)
c5aa993b 9806 {
debd256d
JB
9807 lh->include_dirs_size = 1; /* for testing */
9808 lh->include_dirs = xmalloc (lh->include_dirs_size
9809 * sizeof (*lh->include_dirs));
9810 }
9811 else if (lh->num_include_dirs >= lh->include_dirs_size)
9812 {
9813 lh->include_dirs_size *= 2;
9814 lh->include_dirs = xrealloc (lh->include_dirs,
9815 (lh->include_dirs_size
9816 * sizeof (*lh->include_dirs)));
c5aa993b 9817 }
c906108c 9818
debd256d
JB
9819 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9820}
6e70227d 9821
debd256d
JB
9822
9823/* Add an entry to LH's file name table. */
9824static void
9825add_file_name (struct line_header *lh,
9826 char *name,
9827 unsigned int dir_index,
9828 unsigned int mod_time,
9829 unsigned int length)
9830{
9831 struct file_entry *fe;
9832
9833 /* Grow the array if necessary. */
9834 if (lh->file_names_size == 0)
9835 {
9836 lh->file_names_size = 1; /* for testing */
9837 lh->file_names = xmalloc (lh->file_names_size
9838 * sizeof (*lh->file_names));
9839 }
9840 else if (lh->num_file_names >= lh->file_names_size)
9841 {
9842 lh->file_names_size *= 2;
9843 lh->file_names = xrealloc (lh->file_names,
9844 (lh->file_names_size
9845 * sizeof (*lh->file_names)));
9846 }
9847
9848 fe = &lh->file_names[lh->num_file_names++];
9849 fe->name = name;
9850 fe->dir_index = dir_index;
9851 fe->mod_time = mod_time;
9852 fe->length = length;
aaa75496 9853 fe->included_p = 0;
cb1df416 9854 fe->symtab = NULL;
debd256d 9855}
6e70227d 9856
debd256d
JB
9857
9858/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9859 .debug_line, according to the endianness of ABFD. Return a pointer
9860 to a struct line_header, allocated using xmalloc.
debd256d
JB
9861
9862 NOTE: the strings in the include directory and file name tables of
9863 the returned object point into debug_line_buffer, and must not be
9864 freed. */
9865static struct line_header *
9866dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9867 struct dwarf2_cu *cu)
debd256d
JB
9868{
9869 struct cleanup *back_to;
9870 struct line_header *lh;
fe1b8b76 9871 gdb_byte *line_ptr;
c764a876 9872 unsigned int bytes_read, offset_size;
debd256d
JB
9873 int i;
9874 char *cur_dir, *cur_file;
9875
be391dca 9876 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9877 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9878 {
e2e0b3e5 9879 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9880 return 0;
9881 }
9882
a738430d
MK
9883 /* Make sure that at least there's room for the total_length field.
9884 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9885 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9886 {
4d3c2250 9887 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9888 return 0;
9889 }
9890
9891 lh = xmalloc (sizeof (*lh));
9892 memset (lh, 0, sizeof (*lh));
9893 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9894 (void *) lh);
9895
dce234bc 9896 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9897
a738430d 9898 /* Read in the header. */
6e70227d 9899 lh->total_length =
c764a876
DE
9900 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9901 &bytes_read, &offset_size);
debd256d 9902 line_ptr += bytes_read;
dce234bc
PP
9903 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9904 + dwarf2_per_objfile->line.size))
debd256d 9905 {
4d3c2250 9906 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9907 return 0;
9908 }
9909 lh->statement_program_end = line_ptr + lh->total_length;
9910 lh->version = read_2_bytes (abfd, line_ptr);
9911 line_ptr += 2;
c764a876
DE
9912 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9913 line_ptr += offset_size;
debd256d
JB
9914 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9915 line_ptr += 1;
2dc7f7b3
TT
9916 if (lh->version >= 4)
9917 {
9918 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9919 line_ptr += 1;
9920 }
9921 else
9922 lh->maximum_ops_per_instruction = 1;
9923
9924 if (lh->maximum_ops_per_instruction == 0)
9925 {
9926 lh->maximum_ops_per_instruction = 1;
9927 complaint (&symfile_complaints,
9928 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9929 }
9930
debd256d
JB
9931 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9932 line_ptr += 1;
9933 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9934 line_ptr += 1;
9935 lh->line_range = read_1_byte (abfd, line_ptr);
9936 line_ptr += 1;
9937 lh->opcode_base = read_1_byte (abfd, line_ptr);
9938 line_ptr += 1;
9939 lh->standard_opcode_lengths
fe1b8b76 9940 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9941
9942 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9943 for (i = 1; i < lh->opcode_base; ++i)
9944 {
9945 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9946 line_ptr += 1;
9947 }
9948
a738430d 9949 /* Read directory table. */
9b1c24c8 9950 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9951 {
9952 line_ptr += bytes_read;
9953 add_include_dir (lh, cur_dir);
9954 }
9955 line_ptr += bytes_read;
9956
a738430d 9957 /* Read file name table. */
9b1c24c8 9958 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9959 {
9960 unsigned int dir_index, mod_time, length;
9961
9962 line_ptr += bytes_read;
9963 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9964 line_ptr += bytes_read;
9965 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9966 line_ptr += bytes_read;
9967 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9968 line_ptr += bytes_read;
9969
9970 add_file_name (lh, cur_file, dir_index, mod_time, length);
9971 }
9972 line_ptr += bytes_read;
6e70227d 9973 lh->statement_program_start = line_ptr;
debd256d 9974
dce234bc
PP
9975 if (line_ptr > (dwarf2_per_objfile->line.buffer
9976 + dwarf2_per_objfile->line.size))
4d3c2250 9977 complaint (&symfile_complaints,
e2e0b3e5 9978 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9979
9980 discard_cleanups (back_to);
9981 return lh;
9982}
c906108c 9983
5fb290d7
DJ
9984/* This function exists to work around a bug in certain compilers
9985 (particularly GCC 2.95), in which the first line number marker of a
9986 function does not show up until after the prologue, right before
9987 the second line number marker. This function shifts ADDRESS down
9988 to the beginning of the function if necessary, and is called on
9989 addresses passed to record_line. */
9990
9991static CORE_ADDR
e142c38c 9992check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9993{
9994 struct function_range *fn;
9995
9996 /* Find the function_range containing address. */
e142c38c 9997 if (!cu->first_fn)
5fb290d7
DJ
9998 return address;
9999
e142c38c
DJ
10000 if (!cu->cached_fn)
10001 cu->cached_fn = cu->first_fn;
5fb290d7 10002
e142c38c 10003 fn = cu->cached_fn;
5fb290d7
DJ
10004 while (fn)
10005 if (fn->lowpc <= address && fn->highpc > address)
10006 goto found;
10007 else
10008 fn = fn->next;
10009
e142c38c
DJ
10010 fn = cu->first_fn;
10011 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10012 if (fn->lowpc <= address && fn->highpc > address)
10013 goto found;
10014 else
10015 fn = fn->next;
10016
10017 return address;
10018
10019 found:
10020 if (fn->seen_line)
10021 return address;
10022 if (address != fn->lowpc)
4d3c2250 10023 complaint (&symfile_complaints,
e2e0b3e5 10024 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10025 (unsigned long) address, fn->name);
5fb290d7
DJ
10026 fn->seen_line = 1;
10027 return fn->lowpc;
10028}
10029
c6da4cef
DE
10030/* Subroutine of dwarf_decode_lines to simplify it.
10031 Return the file name of the psymtab for included file FILE_INDEX
10032 in line header LH of PST.
10033 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10034 If space for the result is malloc'd, it will be freed by a cleanup.
10035 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10036
10037static char *
10038psymtab_include_file_name (const struct line_header *lh, int file_index,
10039 const struct partial_symtab *pst,
10040 const char *comp_dir)
10041{
10042 const struct file_entry fe = lh->file_names [file_index];
10043 char *include_name = fe.name;
10044 char *include_name_to_compare = include_name;
10045 char *dir_name = NULL;
72b9f47f
TT
10046 const char *pst_filename;
10047 char *copied_name = NULL;
c6da4cef
DE
10048 int file_is_pst;
10049
10050 if (fe.dir_index)
10051 dir_name = lh->include_dirs[fe.dir_index - 1];
10052
10053 if (!IS_ABSOLUTE_PATH (include_name)
10054 && (dir_name != NULL || comp_dir != NULL))
10055 {
10056 /* Avoid creating a duplicate psymtab for PST.
10057 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10058 Before we do the comparison, however, we need to account
10059 for DIR_NAME and COMP_DIR.
10060 First prepend dir_name (if non-NULL). If we still don't
10061 have an absolute path prepend comp_dir (if non-NULL).
10062 However, the directory we record in the include-file's
10063 psymtab does not contain COMP_DIR (to match the
10064 corresponding symtab(s)).
10065
10066 Example:
10067
10068 bash$ cd /tmp
10069 bash$ gcc -g ./hello.c
10070 include_name = "hello.c"
10071 dir_name = "."
10072 DW_AT_comp_dir = comp_dir = "/tmp"
10073 DW_AT_name = "./hello.c" */
10074
10075 if (dir_name != NULL)
10076 {
10077 include_name = concat (dir_name, SLASH_STRING,
10078 include_name, (char *)NULL);
10079 include_name_to_compare = include_name;
10080 make_cleanup (xfree, include_name);
10081 }
10082 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10083 {
10084 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10085 include_name, (char *)NULL);
10086 }
10087 }
10088
10089 pst_filename = pst->filename;
10090 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10091 {
72b9f47f
TT
10092 copied_name = concat (pst->dirname, SLASH_STRING,
10093 pst_filename, (char *)NULL);
10094 pst_filename = copied_name;
c6da4cef
DE
10095 }
10096
1e3fad37 10097 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10098
10099 if (include_name_to_compare != include_name)
10100 xfree (include_name_to_compare);
72b9f47f
TT
10101 if (copied_name != NULL)
10102 xfree (copied_name);
c6da4cef
DE
10103
10104 if (file_is_pst)
10105 return NULL;
10106 return include_name;
10107}
10108
aaa75496
JB
10109/* Decode the Line Number Program (LNP) for the given line_header
10110 structure and CU. The actual information extracted and the type
10111 of structures created from the LNP depends on the value of PST.
10112
10113 1. If PST is NULL, then this procedure uses the data from the program
10114 to create all necessary symbol tables, and their linetables.
6e70227d 10115
aaa75496
JB
10116 2. If PST is not NULL, this procedure reads the program to determine
10117 the list of files included by the unit represented by PST, and
c6da4cef
DE
10118 builds all the associated partial symbol tables.
10119
10120 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10121 It is used for relative paths in the line table.
10122 NOTE: When processing partial symtabs (pst != NULL),
10123 comp_dir == pst->dirname.
10124
10125 NOTE: It is important that psymtabs have the same file name (via strcmp)
10126 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10127 symtab we don't use it in the name of the psymtabs we create.
10128 E.g. expand_line_sal requires this when finding psymtabs to expand.
10129 A good testcase for this is mb-inline.exp. */
debd256d 10130
c906108c 10131static void
72b9f47f 10132dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10133 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10134{
a8c50c1f 10135 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10136 gdb_byte *line_end;
a8c50c1f 10137 unsigned int bytes_read, extended_len;
c906108c 10138 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10139 CORE_ADDR baseaddr;
10140 struct objfile *objfile = cu->objfile;
fbf65064 10141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10142 const int decode_for_pst_p = (pst != NULL);
cb1df416 10143 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10144
10145 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10146
debd256d
JB
10147 line_ptr = lh->statement_program_start;
10148 line_end = lh->statement_program_end;
c906108c
SS
10149
10150 /* Read the statement sequences until there's nothing left. */
10151 while (line_ptr < line_end)
10152 {
10153 /* state machine registers */
10154 CORE_ADDR address = 0;
10155 unsigned int file = 1;
10156 unsigned int line = 1;
10157 unsigned int column = 0;
debd256d 10158 int is_stmt = lh->default_is_stmt;
c906108c
SS
10159 int basic_block = 0;
10160 int end_sequence = 0;
fbf65064 10161 CORE_ADDR addr;
2dc7f7b3 10162 unsigned char op_index = 0;
c906108c 10163
aaa75496 10164 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10165 {
aaa75496 10166 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10167 /* lh->include_dirs and lh->file_names are 0-based, but the
10168 directory and file name numbers in the statement program
10169 are 1-based. */
10170 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10171 char *dir = NULL;
a738430d 10172
debd256d
JB
10173 if (fe->dir_index)
10174 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10175
10176 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10177 }
10178
a738430d 10179 /* Decode the table. */
c5aa993b 10180 while (!end_sequence)
c906108c
SS
10181 {
10182 op_code = read_1_byte (abfd, line_ptr);
10183 line_ptr += 1;
59205f5a
JB
10184 if (line_ptr > line_end)
10185 {
10186 dwarf2_debug_line_missing_end_sequence_complaint ();
10187 break;
10188 }
9aa1fe7e 10189
debd256d 10190 if (op_code >= lh->opcode_base)
6e70227d 10191 {
a738430d 10192 /* Special operand. */
debd256d 10193 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10194 address += (((op_index + (adj_opcode / lh->line_range))
10195 / lh->maximum_ops_per_instruction)
10196 * lh->minimum_instruction_length);
10197 op_index = ((op_index + (adj_opcode / lh->line_range))
10198 % lh->maximum_ops_per_instruction);
debd256d 10199 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10200 if (lh->num_file_names < file || file == 0)
25e43795 10201 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10202 /* For now we ignore lines not starting on an
10203 instruction boundary. */
10204 else if (op_index == 0)
25e43795
DJ
10205 {
10206 lh->file_names[file - 1].included_p = 1;
ca5f395d 10207 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10208 {
10209 if (last_subfile != current_subfile)
10210 {
10211 addr = gdbarch_addr_bits_remove (gdbarch, address);
10212 if (last_subfile)
10213 record_line (last_subfile, 0, addr);
10214 last_subfile = current_subfile;
10215 }
25e43795 10216 /* Append row to matrix using current values. */
fbf65064
UW
10217 addr = check_cu_functions (address, cu);
10218 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10219 record_line (current_subfile, line, addr);
366da635 10220 }
25e43795 10221 }
ca5f395d 10222 basic_block = 0;
9aa1fe7e
GK
10223 }
10224 else switch (op_code)
c906108c
SS
10225 {
10226 case DW_LNS_extended_op:
a8c50c1f 10227 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10228 line_ptr += bytes_read;
a8c50c1f 10229 extended_end = line_ptr + extended_len;
c906108c
SS
10230 extended_op = read_1_byte (abfd, line_ptr);
10231 line_ptr += 1;
10232 switch (extended_op)
10233 {
10234 case DW_LNE_end_sequence:
10235 end_sequence = 1;
c906108c
SS
10236 break;
10237 case DW_LNE_set_address:
e7c27a73 10238 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10239 op_index = 0;
107d2387
AC
10240 line_ptr += bytes_read;
10241 address += baseaddr;
c906108c
SS
10242 break;
10243 case DW_LNE_define_file:
debd256d
JB
10244 {
10245 char *cur_file;
10246 unsigned int dir_index, mod_time, length;
6e70227d 10247
9b1c24c8 10248 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10249 line_ptr += bytes_read;
10250 dir_index =
10251 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10252 line_ptr += bytes_read;
10253 mod_time =
10254 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10255 line_ptr += bytes_read;
10256 length =
10257 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10258 line_ptr += bytes_read;
10259 add_file_name (lh, cur_file, dir_index, mod_time, length);
10260 }
c906108c 10261 break;
d0c6ba3d
CC
10262 case DW_LNE_set_discriminator:
10263 /* The discriminator is not interesting to the debugger;
10264 just ignore it. */
10265 line_ptr = extended_end;
10266 break;
c906108c 10267 default:
4d3c2250 10268 complaint (&symfile_complaints,
e2e0b3e5 10269 _("mangled .debug_line section"));
debd256d 10270 return;
c906108c 10271 }
a8c50c1f
DJ
10272 /* Make sure that we parsed the extended op correctly. If e.g.
10273 we expected a different address size than the producer used,
10274 we may have read the wrong number of bytes. */
10275 if (line_ptr != extended_end)
10276 {
10277 complaint (&symfile_complaints,
10278 _("mangled .debug_line section"));
10279 return;
10280 }
c906108c
SS
10281 break;
10282 case DW_LNS_copy:
59205f5a 10283 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10284 dwarf2_debug_line_missing_file_complaint ();
10285 else
366da635 10286 {
25e43795 10287 lh->file_names[file - 1].included_p = 1;
ca5f395d 10288 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10289 {
10290 if (last_subfile != current_subfile)
10291 {
10292 addr = gdbarch_addr_bits_remove (gdbarch, address);
10293 if (last_subfile)
10294 record_line (last_subfile, 0, addr);
10295 last_subfile = current_subfile;
10296 }
10297 addr = check_cu_functions (address, cu);
10298 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10299 record_line (current_subfile, line, addr);
10300 }
366da635 10301 }
c906108c
SS
10302 basic_block = 0;
10303 break;
10304 case DW_LNS_advance_pc:
2dc7f7b3
TT
10305 {
10306 CORE_ADDR adjust
10307 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10308
10309 address += (((op_index + adjust)
10310 / lh->maximum_ops_per_instruction)
10311 * lh->minimum_instruction_length);
10312 op_index = ((op_index + adjust)
10313 % lh->maximum_ops_per_instruction);
10314 line_ptr += bytes_read;
10315 }
c906108c
SS
10316 break;
10317 case DW_LNS_advance_line:
10318 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10319 line_ptr += bytes_read;
10320 break;
10321 case DW_LNS_set_file:
debd256d 10322 {
a738430d
MK
10323 /* The arrays lh->include_dirs and lh->file_names are
10324 0-based, but the directory and file name numbers in
10325 the statement program are 1-based. */
debd256d 10326 struct file_entry *fe;
4f1520fb 10327 char *dir = NULL;
a738430d 10328
debd256d
JB
10329 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10330 line_ptr += bytes_read;
59205f5a 10331 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10332 dwarf2_debug_line_missing_file_complaint ();
10333 else
10334 {
10335 fe = &lh->file_names[file - 1];
10336 if (fe->dir_index)
10337 dir = lh->include_dirs[fe->dir_index - 1];
10338 if (!decode_for_pst_p)
10339 {
10340 last_subfile = current_subfile;
10341 dwarf2_start_subfile (fe->name, dir, comp_dir);
10342 }
10343 }
debd256d 10344 }
c906108c
SS
10345 break;
10346 case DW_LNS_set_column:
10347 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10348 line_ptr += bytes_read;
10349 break;
10350 case DW_LNS_negate_stmt:
10351 is_stmt = (!is_stmt);
10352 break;
10353 case DW_LNS_set_basic_block:
10354 basic_block = 1;
10355 break;
c2c6d25f
JM
10356 /* Add to the address register of the state machine the
10357 address increment value corresponding to special opcode
a738430d
MK
10358 255. I.e., this value is scaled by the minimum
10359 instruction length since special opcode 255 would have
10360 scaled the the increment. */
c906108c 10361 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10362 {
10363 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10364
10365 address += (((op_index + adjust)
10366 / lh->maximum_ops_per_instruction)
10367 * lh->minimum_instruction_length);
10368 op_index = ((op_index + adjust)
10369 % lh->maximum_ops_per_instruction);
10370 }
c906108c
SS
10371 break;
10372 case DW_LNS_fixed_advance_pc:
10373 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10374 op_index = 0;
c906108c
SS
10375 line_ptr += 2;
10376 break;
9aa1fe7e 10377 default:
a738430d
MK
10378 {
10379 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10380 int i;
a738430d 10381
debd256d 10382 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10383 {
10384 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10385 line_ptr += bytes_read;
10386 }
10387 }
c906108c
SS
10388 }
10389 }
59205f5a
JB
10390 if (lh->num_file_names < file || file == 0)
10391 dwarf2_debug_line_missing_file_complaint ();
10392 else
10393 {
10394 lh->file_names[file - 1].included_p = 1;
10395 if (!decode_for_pst_p)
fbf65064
UW
10396 {
10397 addr = gdbarch_addr_bits_remove (gdbarch, address);
10398 record_line (current_subfile, 0, addr);
10399 }
59205f5a 10400 }
c906108c 10401 }
aaa75496
JB
10402
10403 if (decode_for_pst_p)
10404 {
10405 int file_index;
10406
10407 /* Now that we're done scanning the Line Header Program, we can
10408 create the psymtab of each included file. */
10409 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10410 if (lh->file_names[file_index].included_p == 1)
10411 {
c6da4cef
DE
10412 char *include_name =
10413 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10414 if (include_name != NULL)
aaa75496
JB
10415 dwarf2_create_include_psymtab (include_name, pst, objfile);
10416 }
10417 }
cb1df416
DJ
10418 else
10419 {
10420 /* Make sure a symtab is created for every file, even files
10421 which contain only variables (i.e. no code with associated
10422 line numbers). */
10423
10424 int i;
10425 struct file_entry *fe;
10426
10427 for (i = 0; i < lh->num_file_names; i++)
10428 {
10429 char *dir = NULL;
9a619af0 10430
cb1df416
DJ
10431 fe = &lh->file_names[i];
10432 if (fe->dir_index)
10433 dir = lh->include_dirs[fe->dir_index - 1];
10434 dwarf2_start_subfile (fe->name, dir, comp_dir);
10435
10436 /* Skip the main file; we don't need it, and it must be
10437 allocated last, so that it will show up before the
10438 non-primary symtabs in the objfile's symtab list. */
10439 if (current_subfile == first_subfile)
10440 continue;
10441
10442 if (current_subfile->symtab == NULL)
10443 current_subfile->symtab = allocate_symtab (current_subfile->name,
10444 cu->objfile);
10445 fe->symtab = current_subfile->symtab;
10446 }
10447 }
c906108c
SS
10448}
10449
10450/* Start a subfile for DWARF. FILENAME is the name of the file and
10451 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10452 or NULL if not known. COMP_DIR is the compilation directory for the
10453 linetable's compilation unit or NULL if not known.
c906108c
SS
10454 This routine tries to keep line numbers from identical absolute and
10455 relative file names in a common subfile.
10456
10457 Using the `list' example from the GDB testsuite, which resides in
10458 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10459 of /srcdir/list0.c yields the following debugging information for list0.c:
10460
c5aa993b
JM
10461 DW_AT_name: /srcdir/list0.c
10462 DW_AT_comp_dir: /compdir
357e46e7 10463 files.files[0].name: list0.h
c5aa993b 10464 files.files[0].dir: /srcdir
357e46e7 10465 files.files[1].name: list0.c
c5aa993b 10466 files.files[1].dir: /srcdir
c906108c
SS
10467
10468 The line number information for list0.c has to end up in a single
4f1520fb
FR
10469 subfile, so that `break /srcdir/list0.c:1' works as expected.
10470 start_subfile will ensure that this happens provided that we pass the
10471 concatenation of files.files[1].dir and files.files[1].name as the
10472 subfile's name. */
c906108c
SS
10473
10474static void
72b9f47f 10475dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
c906108c 10476{
4f1520fb
FR
10477 char *fullname;
10478
10479 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10480 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10481 second argument to start_subfile. To be consistent, we do the
10482 same here. In order not to lose the line information directory,
10483 we concatenate it to the filename when it makes sense.
10484 Note that the Dwarf3 standard says (speaking of filenames in line
10485 information): ``The directory index is ignored for file names
10486 that represent full path names''. Thus ignoring dirname in the
10487 `else' branch below isn't an issue. */
c906108c 10488
d5166ae1 10489 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10490 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10491 else
10492 fullname = filename;
c906108c 10493
4f1520fb
FR
10494 start_subfile (fullname, comp_dir);
10495
10496 if (fullname != filename)
10497 xfree (fullname);
c906108c
SS
10498}
10499
4c2df51b
DJ
10500static void
10501var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10502 struct dwarf2_cu *cu)
4c2df51b 10503{
e7c27a73
DJ
10504 struct objfile *objfile = cu->objfile;
10505 struct comp_unit_head *cu_header = &cu->header;
10506
4c2df51b
DJ
10507 /* NOTE drow/2003-01-30: There used to be a comment and some special
10508 code here to turn a symbol with DW_AT_external and a
10509 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10510 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10511 with some versions of binutils) where shared libraries could have
10512 relocations against symbols in their debug information - the
10513 minimal symbol would have the right address, but the debug info
10514 would not. It's no longer necessary, because we will explicitly
10515 apply relocations when we read in the debug information now. */
10516
10517 /* A DW_AT_location attribute with no contents indicates that a
10518 variable has been optimized away. */
10519 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10520 {
10521 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10522 return;
10523 }
10524
10525 /* Handle one degenerate form of location expression specially, to
10526 preserve GDB's previous behavior when section offsets are
10527 specified. If this is just a DW_OP_addr then mark this symbol
10528 as LOC_STATIC. */
10529
10530 if (attr_form_is_block (attr)
10531 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10532 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10533 {
891d2f0b 10534 unsigned int dummy;
4c2df51b
DJ
10535
10536 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10537 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10538 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10539 fixup_symbol_section (sym, objfile);
10540 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10541 SYMBOL_SECTION (sym));
4c2df51b
DJ
10542 return;
10543 }
10544
10545 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10546 expression evaluator, and use LOC_COMPUTED only when necessary
10547 (i.e. when the value of a register or memory location is
10548 referenced, or a thread-local block, etc.). Then again, it might
10549 not be worthwhile. I'm assuming that it isn't unless performance
10550 or memory numbers show me otherwise. */
10551
e7c27a73 10552 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10553 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10554}
10555
c906108c
SS
10556/* Given a pointer to a DWARF information entry, figure out if we need
10557 to make a symbol table entry for it, and if so, create a new entry
10558 and return a pointer to it.
10559 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10560 used the passed type.
10561 If SPACE is not NULL, use it to hold the new symbol. If it is
10562 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10563
10564static struct symbol *
34eaf542
TT
10565new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10566 struct symbol *space)
c906108c 10567{
e7c27a73 10568 struct objfile *objfile = cu->objfile;
c906108c
SS
10569 struct symbol *sym = NULL;
10570 char *name;
10571 struct attribute *attr = NULL;
10572 struct attribute *attr2 = NULL;
e142c38c 10573 CORE_ADDR baseaddr;
e37fd15a
SW
10574 struct pending **list_to_add = NULL;
10575
edb3359d 10576 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10577
10578 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10579
94af9270 10580 name = dwarf2_name (die, cu);
c906108c
SS
10581 if (name)
10582 {
94af9270 10583 const char *linkagename;
34eaf542 10584 int suppress_add = 0;
94af9270 10585
34eaf542
TT
10586 if (space)
10587 sym = space;
10588 else
10589 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10590 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10591
10592 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10593 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10594 linkagename = dwarf2_physname (name, die, cu);
10595 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10596
f55ee35c
JK
10597 /* Fortran does not have mangling standard and the mangling does differ
10598 between gfortran, iFort etc. */
10599 if (cu->language == language_fortran
b250c185 10600 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10601 symbol_set_demangled_name (&(sym->ginfo),
10602 (char *) dwarf2_full_name (name, die, cu),
10603 NULL);
f55ee35c 10604
c906108c 10605 /* Default assumptions.
c5aa993b 10606 Use the passed type or decode it from the die. */
176620f1 10607 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10608 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10609 if (type != NULL)
10610 SYMBOL_TYPE (sym) = type;
10611 else
e7c27a73 10612 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10613 attr = dwarf2_attr (die,
10614 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10615 cu);
c906108c
SS
10616 if (attr)
10617 {
10618 SYMBOL_LINE (sym) = DW_UNSND (attr);
10619 }
cb1df416 10620
edb3359d
DJ
10621 attr = dwarf2_attr (die,
10622 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10623 cu);
cb1df416
DJ
10624 if (attr)
10625 {
10626 int file_index = DW_UNSND (attr);
9a619af0 10627
cb1df416
DJ
10628 if (cu->line_header == NULL
10629 || file_index > cu->line_header->num_file_names)
10630 complaint (&symfile_complaints,
10631 _("file index out of range"));
1c3d648d 10632 else if (file_index > 0)
cb1df416
DJ
10633 {
10634 struct file_entry *fe;
9a619af0 10635
cb1df416
DJ
10636 fe = &cu->line_header->file_names[file_index - 1];
10637 SYMBOL_SYMTAB (sym) = fe->symtab;
10638 }
10639 }
10640
c906108c
SS
10641 switch (die->tag)
10642 {
10643 case DW_TAG_label:
e142c38c 10644 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10645 if (attr)
10646 {
10647 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10648 }
0f5238ed
TT
10649 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10650 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10651 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10652 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10653 break;
10654 case DW_TAG_subprogram:
10655 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10656 finish_block. */
10657 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10658 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10659 if ((attr2 && (DW_UNSND (attr2) != 0))
10660 || cu->language == language_ada)
c906108c 10661 {
2cfa0c8d
JB
10662 /* Subprograms marked external are stored as a global symbol.
10663 Ada subprograms, whether marked external or not, are always
10664 stored as a global symbol, because we want to be able to
10665 access them globally. For instance, we want to be able
10666 to break on a nested subprogram without having to
10667 specify the context. */
e37fd15a 10668 list_to_add = &global_symbols;
c906108c
SS
10669 }
10670 else
10671 {
e37fd15a 10672 list_to_add = cu->list_in_scope;
c906108c
SS
10673 }
10674 break;
edb3359d
DJ
10675 case DW_TAG_inlined_subroutine:
10676 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10677 finish_block. */
10678 SYMBOL_CLASS (sym) = LOC_BLOCK;
10679 SYMBOL_INLINED (sym) = 1;
10680 /* Do not add the symbol to any lists. It will be found via
10681 BLOCK_FUNCTION from the blockvector. */
10682 break;
34eaf542
TT
10683 case DW_TAG_template_value_param:
10684 suppress_add = 1;
10685 /* Fall through. */
72929c62 10686 case DW_TAG_constant:
c906108c 10687 case DW_TAG_variable:
254e6b9e 10688 case DW_TAG_member:
c906108c
SS
10689 /* Compilation with minimal debug info may result in variables
10690 with missing type entries. Change the misleading `void' type
10691 to something sensible. */
10692 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10693 SYMBOL_TYPE (sym)
46bf5051 10694 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10695
e142c38c 10696 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10697 /* In the case of DW_TAG_member, we should only be called for
10698 static const members. */
10699 if (die->tag == DW_TAG_member)
10700 {
3863f96c
DE
10701 /* dwarf2_add_field uses die_is_declaration,
10702 so we do the same. */
254e6b9e
DE
10703 gdb_assert (die_is_declaration (die, cu));
10704 gdb_assert (attr);
10705 }
c906108c
SS
10706 if (attr)
10707 {
e7c27a73 10708 dwarf2_const_value (attr, sym, cu);
e142c38c 10709 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10710 if (!suppress_add)
34eaf542
TT
10711 {
10712 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10713 list_to_add = &global_symbols;
34eaf542 10714 else
e37fd15a 10715 list_to_add = cu->list_in_scope;
34eaf542 10716 }
c906108c
SS
10717 break;
10718 }
e142c38c 10719 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10720 if (attr)
10721 {
e7c27a73 10722 var_decode_location (attr, sym, cu);
e142c38c 10723 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10724 if (SYMBOL_CLASS (sym) == LOC_STATIC
10725 && SYMBOL_VALUE_ADDRESS (sym) == 0
10726 && !dwarf2_per_objfile->has_section_at_zero)
10727 {
10728 /* When a static variable is eliminated by the linker,
10729 the corresponding debug information is not stripped
10730 out, but the variable address is set to null;
10731 do not add such variables into symbol table. */
10732 }
10733 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10734 {
f55ee35c
JK
10735 /* Workaround gfortran PR debug/40040 - it uses
10736 DW_AT_location for variables in -fPIC libraries which may
10737 get overriden by other libraries/executable and get
10738 a different address. Resolve it by the minimal symbol
10739 which may come from inferior's executable using copy
10740 relocation. Make this workaround only for gfortran as for
10741 other compilers GDB cannot guess the minimal symbol
10742 Fortran mangling kind. */
10743 if (cu->language == language_fortran && die->parent
10744 && die->parent->tag == DW_TAG_module
10745 && cu->producer
10746 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10747 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10748
1c809c68
TT
10749 /* A variable with DW_AT_external is never static,
10750 but it may be block-scoped. */
10751 list_to_add = (cu->list_in_scope == &file_symbols
10752 ? &global_symbols : cu->list_in_scope);
1c809c68 10753 }
c906108c 10754 else
e37fd15a 10755 list_to_add = cu->list_in_scope;
c906108c
SS
10756 }
10757 else
10758 {
10759 /* We do not know the address of this symbol.
c5aa993b
JM
10760 If it is an external symbol and we have type information
10761 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10762 The address of the variable will then be determined from
10763 the minimal symbol table whenever the variable is
10764 referenced. */
e142c38c 10765 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10766 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10767 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10768 {
0fe7935b
DJ
10769 /* A variable with DW_AT_external is never static, but it
10770 may be block-scoped. */
10771 list_to_add = (cu->list_in_scope == &file_symbols
10772 ? &global_symbols : cu->list_in_scope);
10773
c906108c 10774 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10775 }
442ddf59
JK
10776 else if (!die_is_declaration (die, cu))
10777 {
10778 /* Use the default LOC_OPTIMIZED_OUT class. */
10779 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10780 if (!suppress_add)
10781 list_to_add = cu->list_in_scope;
442ddf59 10782 }
c906108c
SS
10783 }
10784 break;
10785 case DW_TAG_formal_parameter:
edb3359d
DJ
10786 /* If we are inside a function, mark this as an argument. If
10787 not, we might be looking at an argument to an inlined function
10788 when we do not have enough information to show inlined frames;
10789 pretend it's a local variable in that case so that the user can
10790 still see it. */
10791 if (context_stack_depth > 0
10792 && context_stack[context_stack_depth - 1].name != NULL)
10793 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10794 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10795 if (attr)
10796 {
e7c27a73 10797 var_decode_location (attr, sym, cu);
c906108c 10798 }
e142c38c 10799 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10800 if (attr)
10801 {
e7c27a73 10802 dwarf2_const_value (attr, sym, cu);
c906108c 10803 }
f346a30d
PM
10804 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10805 if (attr && DW_UNSND (attr))
10806 {
10807 struct type *ref_type;
10808
10809 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10810 SYMBOL_TYPE (sym) = ref_type;
10811 }
10812
e37fd15a 10813 list_to_add = cu->list_in_scope;
c906108c
SS
10814 break;
10815 case DW_TAG_unspecified_parameters:
10816 /* From varargs functions; gdb doesn't seem to have any
10817 interest in this information, so just ignore it for now.
10818 (FIXME?) */
10819 break;
34eaf542
TT
10820 case DW_TAG_template_type_param:
10821 suppress_add = 1;
10822 /* Fall through. */
c906108c 10823 case DW_TAG_class_type:
680b30c7 10824 case DW_TAG_interface_type:
c906108c
SS
10825 case DW_TAG_structure_type:
10826 case DW_TAG_union_type:
72019c9c 10827 case DW_TAG_set_type:
c906108c
SS
10828 case DW_TAG_enumeration_type:
10829 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10830 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10831
63d06c5c 10832 {
987504bb 10833 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10834 really ever be static objects: otherwise, if you try
10835 to, say, break of a class's method and you're in a file
10836 which doesn't mention that class, it won't work unless
10837 the check for all static symbols in lookup_symbol_aux
10838 saves you. See the OtherFileClass tests in
10839 gdb.c++/namespace.exp. */
10840
e37fd15a 10841 if (!suppress_add)
34eaf542 10842 {
34eaf542
TT
10843 list_to_add = (cu->list_in_scope == &file_symbols
10844 && (cu->language == language_cplus
10845 || cu->language == language_java)
10846 ? &global_symbols : cu->list_in_scope);
63d06c5c 10847
64382290
TT
10848 /* The semantics of C++ state that "struct foo {
10849 ... }" also defines a typedef for "foo". A Java
10850 class declaration also defines a typedef for the
10851 class. */
10852 if (cu->language == language_cplus
10853 || cu->language == language_java
10854 || cu->language == language_ada)
10855 {
10856 /* The symbol's name is already allocated along
10857 with this objfile, so we don't need to
10858 duplicate it for the type. */
10859 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10860 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10861 }
63d06c5c
DC
10862 }
10863 }
c906108c
SS
10864 break;
10865 case DW_TAG_typedef:
63d06c5c
DC
10866 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10867 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10868 list_to_add = cu->list_in_scope;
63d06c5c 10869 break;
c906108c 10870 case DW_TAG_base_type:
a02abb62 10871 case DW_TAG_subrange_type:
c906108c 10872 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10873 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10874 list_to_add = cu->list_in_scope;
c906108c
SS
10875 break;
10876 case DW_TAG_enumerator:
e142c38c 10877 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10878 if (attr)
10879 {
e7c27a73 10880 dwarf2_const_value (attr, sym, cu);
c906108c 10881 }
63d06c5c
DC
10882 {
10883 /* NOTE: carlton/2003-11-10: See comment above in the
10884 DW_TAG_class_type, etc. block. */
10885
e142c38c 10886 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10887 && (cu->language == language_cplus
10888 || cu->language == language_java)
e142c38c 10889 ? &global_symbols : cu->list_in_scope);
63d06c5c 10890 }
c906108c 10891 break;
5c4e30ca
DC
10892 case DW_TAG_namespace:
10893 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10894 list_to_add = &global_symbols;
5c4e30ca 10895 break;
c906108c
SS
10896 default:
10897 /* Not a tag we recognize. Hopefully we aren't processing
10898 trash data, but since we must specifically ignore things
10899 we don't recognize, there is nothing else we should do at
10900 this point. */
e2e0b3e5 10901 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10902 dwarf_tag_name (die->tag));
c906108c
SS
10903 break;
10904 }
df8a16a1 10905
e37fd15a
SW
10906 if (suppress_add)
10907 {
10908 sym->hash_next = objfile->template_symbols;
10909 objfile->template_symbols = sym;
10910 list_to_add = NULL;
10911 }
10912
10913 if (list_to_add != NULL)
10914 add_symbol_to_list (sym, list_to_add);
10915
df8a16a1
DJ
10916 /* For the benefit of old versions of GCC, check for anonymous
10917 namespaces based on the demangled name. */
10918 if (!processing_has_namespace_info
94af9270 10919 && cu->language == language_cplus)
df8a16a1 10920 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10921 }
10922 return (sym);
10923}
10924
34eaf542
TT
10925/* A wrapper for new_symbol_full that always allocates a new symbol. */
10926
10927static struct symbol *
10928new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10929{
10930 return new_symbol_full (die, type, cu, NULL);
10931}
10932
98bfdba5
PA
10933/* Given an attr with a DW_FORM_dataN value in host byte order,
10934 zero-extend it as appropriate for the symbol's type. The DWARF
10935 standard (v4) is not entirely clear about the meaning of using
10936 DW_FORM_dataN for a constant with a signed type, where the type is
10937 wider than the data. The conclusion of a discussion on the DWARF
10938 list was that this is unspecified. We choose to always zero-extend
10939 because that is the interpretation long in use by GCC. */
c906108c 10940
98bfdba5
PA
10941static gdb_byte *
10942dwarf2_const_value_data (struct attribute *attr, struct type *type,
10943 const char *name, struct obstack *obstack,
10944 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10945{
e7c27a73 10946 struct objfile *objfile = cu->objfile;
e17a4113
UW
10947 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10948 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10949 LONGEST l = DW_UNSND (attr);
10950
10951 if (bits < sizeof (*value) * 8)
10952 {
10953 l &= ((LONGEST) 1 << bits) - 1;
10954 *value = l;
10955 }
10956 else if (bits == sizeof (*value) * 8)
10957 *value = l;
10958 else
10959 {
10960 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10961 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10962 return bytes;
10963 }
10964
10965 return NULL;
10966}
10967
10968/* Read a constant value from an attribute. Either set *VALUE, or if
10969 the value does not fit in *VALUE, set *BYTES - either already
10970 allocated on the objfile obstack, or newly allocated on OBSTACK,
10971 or, set *BATON, if we translated the constant to a location
10972 expression. */
10973
10974static void
10975dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10976 const char *name, struct obstack *obstack,
10977 struct dwarf2_cu *cu,
10978 long *value, gdb_byte **bytes,
10979 struct dwarf2_locexpr_baton **baton)
10980{
10981 struct objfile *objfile = cu->objfile;
10982 struct comp_unit_head *cu_header = &cu->header;
c906108c 10983 struct dwarf_block *blk;
98bfdba5
PA
10984 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10985 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10986
10987 *value = 0;
10988 *bytes = NULL;
10989 *baton = NULL;
c906108c
SS
10990
10991 switch (attr->form)
10992 {
10993 case DW_FORM_addr:
ac56253d 10994 {
ac56253d
TT
10995 gdb_byte *data;
10996
98bfdba5
PA
10997 if (TYPE_LENGTH (type) != cu_header->addr_size)
10998 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10999 cu_header->addr_size,
98bfdba5 11000 TYPE_LENGTH (type));
ac56253d
TT
11001 /* Symbols of this form are reasonably rare, so we just
11002 piggyback on the existing location code rather than writing
11003 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11004 *baton = obstack_alloc (&objfile->objfile_obstack,
11005 sizeof (struct dwarf2_locexpr_baton));
11006 (*baton)->per_cu = cu->per_cu;
11007 gdb_assert ((*baton)->per_cu);
ac56253d 11008
98bfdba5
PA
11009 (*baton)->size = 2 + cu_header->addr_size;
11010 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11011 (*baton)->data = data;
ac56253d
TT
11012
11013 data[0] = DW_OP_addr;
11014 store_unsigned_integer (&data[1], cu_header->addr_size,
11015 byte_order, DW_ADDR (attr));
11016 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11017 }
c906108c 11018 break;
4ac36638 11019 case DW_FORM_string:
93b5768b 11020 case DW_FORM_strp:
98bfdba5
PA
11021 /* DW_STRING is already allocated on the objfile obstack, point
11022 directly to it. */
11023 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11024 break;
c906108c
SS
11025 case DW_FORM_block1:
11026 case DW_FORM_block2:
11027 case DW_FORM_block4:
11028 case DW_FORM_block:
2dc7f7b3 11029 case DW_FORM_exprloc:
c906108c 11030 blk = DW_BLOCK (attr);
98bfdba5
PA
11031 if (TYPE_LENGTH (type) != blk->size)
11032 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11033 TYPE_LENGTH (type));
11034 *bytes = blk->data;
c906108c 11035 break;
2df3850c
JM
11036
11037 /* The DW_AT_const_value attributes are supposed to carry the
11038 symbol's value "represented as it would be on the target
11039 architecture." By the time we get here, it's already been
11040 converted to host endianness, so we just need to sign- or
11041 zero-extend it as appropriate. */
11042 case DW_FORM_data1:
98bfdba5 11043 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 11044 break;
c906108c 11045 case DW_FORM_data2:
98bfdba5 11046 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 11047 break;
c906108c 11048 case DW_FORM_data4:
98bfdba5 11049 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 11050 break;
c906108c 11051 case DW_FORM_data8:
98bfdba5 11052 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11053 break;
11054
c906108c 11055 case DW_FORM_sdata:
98bfdba5 11056 *value = DW_SND (attr);
2df3850c
JM
11057 break;
11058
c906108c 11059 case DW_FORM_udata:
98bfdba5 11060 *value = DW_UNSND (attr);
c906108c 11061 break;
2df3850c 11062
c906108c 11063 default:
4d3c2250 11064 complaint (&symfile_complaints,
e2e0b3e5 11065 _("unsupported const value attribute form: '%s'"),
4d3c2250 11066 dwarf_form_name (attr->form));
98bfdba5 11067 *value = 0;
c906108c
SS
11068 break;
11069 }
11070}
11071
2df3850c 11072
98bfdba5
PA
11073/* Copy constant value from an attribute to a symbol. */
11074
2df3850c 11075static void
98bfdba5
PA
11076dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11077 struct dwarf2_cu *cu)
2df3850c 11078{
98bfdba5
PA
11079 struct objfile *objfile = cu->objfile;
11080 struct comp_unit_head *cu_header = &cu->header;
11081 long value;
11082 gdb_byte *bytes;
11083 struct dwarf2_locexpr_baton *baton;
2df3850c 11084
98bfdba5
PA
11085 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11086 SYMBOL_PRINT_NAME (sym),
11087 &objfile->objfile_obstack, cu,
11088 &value, &bytes, &baton);
2df3850c 11089
98bfdba5
PA
11090 if (baton != NULL)
11091 {
11092 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11093 SYMBOL_LOCATION_BATON (sym) = baton;
11094 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11095 }
11096 else if (bytes != NULL)
11097 {
11098 SYMBOL_VALUE_BYTES (sym) = bytes;
11099 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11100 }
11101 else
11102 {
11103 SYMBOL_VALUE (sym) = value;
11104 SYMBOL_CLASS (sym) = LOC_CONST;
11105 }
2df3850c
JM
11106}
11107
c906108c
SS
11108/* Return the type of the die in question using its DW_AT_type attribute. */
11109
11110static struct type *
e7c27a73 11111die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11112{
c906108c 11113 struct attribute *type_attr;
c906108c 11114
e142c38c 11115 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11116 if (!type_attr)
11117 {
11118 /* A missing DW_AT_type represents a void type. */
46bf5051 11119 return objfile_type (cu->objfile)->builtin_void;
c906108c 11120 }
348e048f 11121
673bfd45 11122 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11123}
11124
b4ba55a1
JB
11125/* True iff CU's producer generates GNAT Ada auxiliary information
11126 that allows to find parallel types through that information instead
11127 of having to do expensive parallel lookups by type name. */
11128
11129static int
11130need_gnat_info (struct dwarf2_cu *cu)
11131{
11132 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11133 of GNAT produces this auxiliary information, without any indication
11134 that it is produced. Part of enhancing the FSF version of GNAT
11135 to produce that information will be to put in place an indicator
11136 that we can use in order to determine whether the descriptive type
11137 info is available or not. One suggestion that has been made is
11138 to use a new attribute, attached to the CU die. For now, assume
11139 that the descriptive type info is not available. */
11140 return 0;
11141}
11142
b4ba55a1
JB
11143/* Return the auxiliary type of the die in question using its
11144 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11145 attribute is not present. */
11146
11147static struct type *
11148die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11149{
b4ba55a1 11150 struct attribute *type_attr;
b4ba55a1
JB
11151
11152 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11153 if (!type_attr)
11154 return NULL;
11155
673bfd45 11156 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11157}
11158
11159/* If DIE has a descriptive_type attribute, then set the TYPE's
11160 descriptive type accordingly. */
11161
11162static void
11163set_descriptive_type (struct type *type, struct die_info *die,
11164 struct dwarf2_cu *cu)
11165{
11166 struct type *descriptive_type = die_descriptive_type (die, cu);
11167
11168 if (descriptive_type)
11169 {
11170 ALLOCATE_GNAT_AUX_TYPE (type);
11171 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11172 }
11173}
11174
c906108c
SS
11175/* Return the containing type of the die in question using its
11176 DW_AT_containing_type attribute. */
11177
11178static struct type *
e7c27a73 11179die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11180{
c906108c 11181 struct attribute *type_attr;
c906108c 11182
e142c38c 11183 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11184 if (!type_attr)
11185 error (_("Dwarf Error: Problem turning containing type into gdb type "
11186 "[in module %s]"), cu->objfile->name);
11187
673bfd45 11188 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11189}
11190
673bfd45
DE
11191/* Look up the type of DIE in CU using its type attribute ATTR.
11192 If there is no type substitute an error marker. */
11193
c906108c 11194static struct type *
673bfd45
DE
11195lookup_die_type (struct die_info *die, struct attribute *attr,
11196 struct dwarf2_cu *cu)
c906108c 11197{
f792889a
DJ
11198 struct type *this_type;
11199
673bfd45
DE
11200 /* First see if we have it cached. */
11201
11202 if (is_ref_attr (attr))
11203 {
11204 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11205
11206 this_type = get_die_type_at_offset (offset, cu->per_cu);
11207 }
11208 else if (attr->form == DW_FORM_sig8)
11209 {
11210 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11211 struct dwarf2_cu *sig_cu;
11212 unsigned int offset;
11213
11214 /* sig_type will be NULL if the signatured type is missing from
11215 the debug info. */
11216 if (sig_type == NULL)
11217 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11218 "at 0x%x [in module %s]"),
11219 die->offset, cu->objfile->name);
11220
11221 gdb_assert (sig_type->per_cu.from_debug_types);
11222 offset = sig_type->offset + sig_type->type_offset;
11223 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11224 }
11225 else
11226 {
11227 dump_die_for_error (die);
11228 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11229 dwarf_attr_name (attr->name), cu->objfile->name);
11230 }
11231
11232 /* If not cached we need to read it in. */
11233
11234 if (this_type == NULL)
11235 {
11236 struct die_info *type_die;
11237 struct dwarf2_cu *type_cu = cu;
11238
11239 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11240 /* If the type is cached, we should have found it above. */
11241 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11242 this_type = read_type_die_1 (type_die, type_cu);
11243 }
11244
11245 /* If we still don't have a type use an error marker. */
11246
11247 if (this_type == NULL)
c906108c 11248 {
b00fdb78
TT
11249 char *message, *saved;
11250
11251 /* read_type_die already issued a complaint. */
11252 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11253 cu->objfile->name,
11254 cu->header.offset,
11255 die->offset);
11256 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11257 message, strlen (message));
11258 xfree (message);
11259
11260 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11261 }
673bfd45 11262
f792889a 11263 return this_type;
c906108c
SS
11264}
11265
673bfd45
DE
11266/* Return the type in DIE, CU.
11267 Returns NULL for invalid types.
11268
11269 This first does a lookup in the appropriate type_hash table,
11270 and only reads the die in if necessary.
11271
11272 NOTE: This can be called when reading in partial or full symbols. */
11273
f792889a 11274static struct type *
e7c27a73 11275read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11276{
f792889a
DJ
11277 struct type *this_type;
11278
11279 this_type = get_die_type (die, cu);
11280 if (this_type)
11281 return this_type;
11282
673bfd45
DE
11283 return read_type_die_1 (die, cu);
11284}
11285
11286/* Read the type in DIE, CU.
11287 Returns NULL for invalid types. */
11288
11289static struct type *
11290read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11291{
11292 struct type *this_type = NULL;
11293
c906108c
SS
11294 switch (die->tag)
11295 {
11296 case DW_TAG_class_type:
680b30c7 11297 case DW_TAG_interface_type:
c906108c
SS
11298 case DW_TAG_structure_type:
11299 case DW_TAG_union_type:
f792889a 11300 this_type = read_structure_type (die, cu);
c906108c
SS
11301 break;
11302 case DW_TAG_enumeration_type:
f792889a 11303 this_type = read_enumeration_type (die, cu);
c906108c
SS
11304 break;
11305 case DW_TAG_subprogram:
11306 case DW_TAG_subroutine_type:
edb3359d 11307 case DW_TAG_inlined_subroutine:
f792889a 11308 this_type = read_subroutine_type (die, cu);
c906108c
SS
11309 break;
11310 case DW_TAG_array_type:
f792889a 11311 this_type = read_array_type (die, cu);
c906108c 11312 break;
72019c9c 11313 case DW_TAG_set_type:
f792889a 11314 this_type = read_set_type (die, cu);
72019c9c 11315 break;
c906108c 11316 case DW_TAG_pointer_type:
f792889a 11317 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11318 break;
11319 case DW_TAG_ptr_to_member_type:
f792889a 11320 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11321 break;
11322 case DW_TAG_reference_type:
f792889a 11323 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11324 break;
11325 case DW_TAG_const_type:
f792889a 11326 this_type = read_tag_const_type (die, cu);
c906108c
SS
11327 break;
11328 case DW_TAG_volatile_type:
f792889a 11329 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11330 break;
11331 case DW_TAG_string_type:
f792889a 11332 this_type = read_tag_string_type (die, cu);
c906108c
SS
11333 break;
11334 case DW_TAG_typedef:
f792889a 11335 this_type = read_typedef (die, cu);
c906108c 11336 break;
a02abb62 11337 case DW_TAG_subrange_type:
f792889a 11338 this_type = read_subrange_type (die, cu);
a02abb62 11339 break;
c906108c 11340 case DW_TAG_base_type:
f792889a 11341 this_type = read_base_type (die, cu);
c906108c 11342 break;
81a17f79 11343 case DW_TAG_unspecified_type:
f792889a 11344 this_type = read_unspecified_type (die, cu);
81a17f79 11345 break;
0114d602
DJ
11346 case DW_TAG_namespace:
11347 this_type = read_namespace_type (die, cu);
11348 break;
f55ee35c
JK
11349 case DW_TAG_module:
11350 this_type = read_module_type (die, cu);
11351 break;
c906108c 11352 default:
a1f5b845 11353 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11354 dwarf_tag_name (die->tag));
c906108c
SS
11355 break;
11356 }
63d06c5c 11357
f792889a 11358 return this_type;
63d06c5c
DC
11359}
11360
abc72ce4
DE
11361/* See if we can figure out if the class lives in a namespace. We do
11362 this by looking for a member function; its demangled name will
11363 contain namespace info, if there is any.
11364 Return the computed name or NULL.
11365 Space for the result is allocated on the objfile's obstack.
11366 This is the full-die version of guess_partial_die_structure_name.
11367 In this case we know DIE has no useful parent. */
11368
11369static char *
11370guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11371{
11372 struct die_info *spec_die;
11373 struct dwarf2_cu *spec_cu;
11374 struct die_info *child;
11375
11376 spec_cu = cu;
11377 spec_die = die_specification (die, &spec_cu);
11378 if (spec_die != NULL)
11379 {
11380 die = spec_die;
11381 cu = spec_cu;
11382 }
11383
11384 for (child = die->child;
11385 child != NULL;
11386 child = child->sibling)
11387 {
11388 if (child->tag == DW_TAG_subprogram)
11389 {
11390 struct attribute *attr;
11391
11392 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11393 if (attr == NULL)
11394 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11395 if (attr != NULL)
11396 {
11397 char *actual_name
11398 = language_class_name_from_physname (cu->language_defn,
11399 DW_STRING (attr));
11400 char *name = NULL;
11401
11402 if (actual_name != NULL)
11403 {
11404 char *die_name = dwarf2_name (die, cu);
11405
11406 if (die_name != NULL
11407 && strcmp (die_name, actual_name) != 0)
11408 {
11409 /* Strip off the class name from the full name.
11410 We want the prefix. */
11411 int die_name_len = strlen (die_name);
11412 int actual_name_len = strlen (actual_name);
11413
11414 /* Test for '::' as a sanity check. */
11415 if (actual_name_len > die_name_len + 2
11416 && actual_name[actual_name_len - die_name_len - 1] == ':')
11417 name =
11418 obsavestring (actual_name,
11419 actual_name_len - die_name_len - 2,
11420 &cu->objfile->objfile_obstack);
11421 }
11422 }
11423 xfree (actual_name);
11424 return name;
11425 }
11426 }
11427 }
11428
11429 return NULL;
11430}
11431
fdde2d81 11432/* Return the name of the namespace/class that DIE is defined within,
0114d602 11433 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11434
0114d602
DJ
11435 For example, if we're within the method foo() in the following
11436 code:
11437
11438 namespace N {
11439 class C {
11440 void foo () {
11441 }
11442 };
11443 }
11444
11445 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11446
11447static char *
e142c38c 11448determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11449{
0114d602
DJ
11450 struct die_info *parent, *spec_die;
11451 struct dwarf2_cu *spec_cu;
11452 struct type *parent_type;
63d06c5c 11453
f55ee35c
JK
11454 if (cu->language != language_cplus && cu->language != language_java
11455 && cu->language != language_fortran)
0114d602
DJ
11456 return "";
11457
11458 /* We have to be careful in the presence of DW_AT_specification.
11459 For example, with GCC 3.4, given the code
11460
11461 namespace N {
11462 void foo() {
11463 // Definition of N::foo.
11464 }
11465 }
11466
11467 then we'll have a tree of DIEs like this:
11468
11469 1: DW_TAG_compile_unit
11470 2: DW_TAG_namespace // N
11471 3: DW_TAG_subprogram // declaration of N::foo
11472 4: DW_TAG_subprogram // definition of N::foo
11473 DW_AT_specification // refers to die #3
11474
11475 Thus, when processing die #4, we have to pretend that we're in
11476 the context of its DW_AT_specification, namely the contex of die
11477 #3. */
11478 spec_cu = cu;
11479 spec_die = die_specification (die, &spec_cu);
11480 if (spec_die == NULL)
11481 parent = die->parent;
11482 else
63d06c5c 11483 {
0114d602
DJ
11484 parent = spec_die->parent;
11485 cu = spec_cu;
63d06c5c 11486 }
0114d602
DJ
11487
11488 if (parent == NULL)
11489 return "";
98bfdba5
PA
11490 else if (parent->building_fullname)
11491 {
11492 const char *name;
11493 const char *parent_name;
11494
11495 /* It has been seen on RealView 2.2 built binaries,
11496 DW_TAG_template_type_param types actually _defined_ as
11497 children of the parent class:
11498
11499 enum E {};
11500 template class <class Enum> Class{};
11501 Class<enum E> class_e;
11502
11503 1: DW_TAG_class_type (Class)
11504 2: DW_TAG_enumeration_type (E)
11505 3: DW_TAG_enumerator (enum1:0)
11506 3: DW_TAG_enumerator (enum2:1)
11507 ...
11508 2: DW_TAG_template_type_param
11509 DW_AT_type DW_FORM_ref_udata (E)
11510
11511 Besides being broken debug info, it can put GDB into an
11512 infinite loop. Consider:
11513
11514 When we're building the full name for Class<E>, we'll start
11515 at Class, and go look over its template type parameters,
11516 finding E. We'll then try to build the full name of E, and
11517 reach here. We're now trying to build the full name of E,
11518 and look over the parent DIE for containing scope. In the
11519 broken case, if we followed the parent DIE of E, we'd again
11520 find Class, and once again go look at its template type
11521 arguments, etc., etc. Simply don't consider such parent die
11522 as source-level parent of this die (it can't be, the language
11523 doesn't allow it), and break the loop here. */
11524 name = dwarf2_name (die, cu);
11525 parent_name = dwarf2_name (parent, cu);
11526 complaint (&symfile_complaints,
11527 _("template param type '%s' defined within parent '%s'"),
11528 name ? name : "<unknown>",
11529 parent_name ? parent_name : "<unknown>");
11530 return "";
11531 }
63d06c5c 11532 else
0114d602
DJ
11533 switch (parent->tag)
11534 {
63d06c5c 11535 case DW_TAG_namespace:
0114d602 11536 parent_type = read_type_die (parent, cu);
acebe513
UW
11537 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11538 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11539 Work around this problem here. */
11540 if (cu->language == language_cplus
11541 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11542 return "";
0114d602
DJ
11543 /* We give a name to even anonymous namespaces. */
11544 return TYPE_TAG_NAME (parent_type);
63d06c5c 11545 case DW_TAG_class_type:
680b30c7 11546 case DW_TAG_interface_type:
63d06c5c 11547 case DW_TAG_structure_type:
0114d602 11548 case DW_TAG_union_type:
f55ee35c 11549 case DW_TAG_module:
0114d602
DJ
11550 parent_type = read_type_die (parent, cu);
11551 if (TYPE_TAG_NAME (parent_type) != NULL)
11552 return TYPE_TAG_NAME (parent_type);
11553 else
11554 /* An anonymous structure is only allowed non-static data
11555 members; no typedefs, no member functions, et cetera.
11556 So it does not need a prefix. */
11557 return "";
abc72ce4
DE
11558 case DW_TAG_compile_unit:
11559 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11560 if (cu->language == language_cplus
11561 && dwarf2_per_objfile->types.asection != NULL
11562 && die->child != NULL
11563 && (die->tag == DW_TAG_class_type
11564 || die->tag == DW_TAG_structure_type
11565 || die->tag == DW_TAG_union_type))
11566 {
11567 char *name = guess_full_die_structure_name (die, cu);
11568 if (name != NULL)
11569 return name;
11570 }
11571 return "";
63d06c5c 11572 default:
8176b9b8 11573 return determine_prefix (parent, cu);
63d06c5c 11574 }
63d06c5c
DC
11575}
11576
987504bb
JJ
11577/* Return a newly-allocated string formed by concatenating PREFIX and
11578 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11579 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11580 perform an obconcat, otherwise allocate storage for the result. The CU argument
11581 is used to determine the language and hence, the appropriate separator. */
11582
f55ee35c 11583#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11584
11585static char *
f55ee35c
JK
11586typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11587 int physname, struct dwarf2_cu *cu)
63d06c5c 11588{
f55ee35c 11589 const char *lead = "";
5c315b68 11590 const char *sep;
63d06c5c 11591
987504bb
JJ
11592 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11593 sep = "";
11594 else if (cu->language == language_java)
11595 sep = ".";
f55ee35c
JK
11596 else if (cu->language == language_fortran && physname)
11597 {
11598 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11599 DW_AT_MIPS_linkage_name is preferred and used instead. */
11600
11601 lead = "__";
11602 sep = "_MOD_";
11603 }
987504bb
JJ
11604 else
11605 sep = "::";
63d06c5c 11606
6dd47d34
DE
11607 if (prefix == NULL)
11608 prefix = "";
11609 if (suffix == NULL)
11610 suffix = "";
11611
987504bb
JJ
11612 if (obs == NULL)
11613 {
11614 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11615
f55ee35c
JK
11616 strcpy (retval, lead);
11617 strcat (retval, prefix);
6dd47d34
DE
11618 strcat (retval, sep);
11619 strcat (retval, suffix);
63d06c5c
DC
11620 return retval;
11621 }
987504bb
JJ
11622 else
11623 {
11624 /* We have an obstack. */
f55ee35c 11625 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11626 }
63d06c5c
DC
11627}
11628
c906108c
SS
11629/* Return sibling of die, NULL if no sibling. */
11630
f9aca02d 11631static struct die_info *
fba45db2 11632sibling_die (struct die_info *die)
c906108c 11633{
639d11d3 11634 return die->sibling;
c906108c
SS
11635}
11636
71c25dea
TT
11637/* Get name of a die, return NULL if not found. */
11638
11639static char *
11640dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11641 struct obstack *obstack)
11642{
11643 if (name && cu->language == language_cplus)
11644 {
11645 char *canon_name = cp_canonicalize_string (name);
11646
11647 if (canon_name != NULL)
11648 {
11649 if (strcmp (canon_name, name) != 0)
11650 name = obsavestring (canon_name, strlen (canon_name),
11651 obstack);
11652 xfree (canon_name);
11653 }
11654 }
11655
11656 return name;
c906108c
SS
11657}
11658
9219021c
DC
11659/* Get name of a die, return NULL if not found. */
11660
11661static char *
e142c38c 11662dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11663{
11664 struct attribute *attr;
11665
e142c38c 11666 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11667 if (!attr || !DW_STRING (attr))
11668 return NULL;
11669
11670 switch (die->tag)
11671 {
11672 case DW_TAG_compile_unit:
11673 /* Compilation units have a DW_AT_name that is a filename, not
11674 a source language identifier. */
11675 case DW_TAG_enumeration_type:
11676 case DW_TAG_enumerator:
11677 /* These tags always have simple identifiers already; no need
11678 to canonicalize them. */
11679 return DW_STRING (attr);
907af001 11680
418835cc
KS
11681 case DW_TAG_subprogram:
11682 /* Java constructors will all be named "<init>", so return
11683 the class name when we see this special case. */
11684 if (cu->language == language_java
11685 && DW_STRING (attr) != NULL
11686 && strcmp (DW_STRING (attr), "<init>") == 0)
11687 {
11688 struct dwarf2_cu *spec_cu = cu;
11689 struct die_info *spec_die;
11690
11691 /* GCJ will output '<init>' for Java constructor names.
11692 For this special case, return the name of the parent class. */
11693
11694 /* GCJ may output suprogram DIEs with AT_specification set.
11695 If so, use the name of the specified DIE. */
11696 spec_die = die_specification (die, &spec_cu);
11697 if (spec_die != NULL)
11698 return dwarf2_name (spec_die, spec_cu);
11699
11700 do
11701 {
11702 die = die->parent;
11703 if (die->tag == DW_TAG_class_type)
11704 return dwarf2_name (die, cu);
11705 }
11706 while (die->tag != DW_TAG_compile_unit);
11707 }
907af001
UW
11708 break;
11709
11710 case DW_TAG_class_type:
11711 case DW_TAG_interface_type:
11712 case DW_TAG_structure_type:
11713 case DW_TAG_union_type:
11714 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11715 structures or unions. These were of the form "._%d" in GCC 4.1,
11716 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11717 and GCC 4.4. We work around this problem by ignoring these. */
11718 if (strncmp (DW_STRING (attr), "._", 2) == 0
11719 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11720 return NULL;
11721 break;
11722
71c25dea 11723 default:
907af001
UW
11724 break;
11725 }
11726
11727 if (!DW_STRING_IS_CANONICAL (attr))
11728 {
11729 DW_STRING (attr)
11730 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11731 &cu->objfile->objfile_obstack);
11732 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11733 }
907af001 11734 return DW_STRING (attr);
9219021c
DC
11735}
11736
11737/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11738 is none. *EXT_CU is the CU containing DIE on input, and the CU
11739 containing the return value on output. */
9219021c
DC
11740
11741static struct die_info *
f2f0e013 11742dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11743{
11744 struct attribute *attr;
9219021c 11745
f2f0e013 11746 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11747 if (attr == NULL)
11748 return NULL;
11749
f2f0e013 11750 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11751}
11752
c906108c
SS
11753/* Convert a DIE tag into its string name. */
11754
11755static char *
aa1ee363 11756dwarf_tag_name (unsigned tag)
c906108c
SS
11757{
11758 switch (tag)
11759 {
11760 case DW_TAG_padding:
11761 return "DW_TAG_padding";
11762 case DW_TAG_array_type:
11763 return "DW_TAG_array_type";
11764 case DW_TAG_class_type:
11765 return "DW_TAG_class_type";
11766 case DW_TAG_entry_point:
11767 return "DW_TAG_entry_point";
11768 case DW_TAG_enumeration_type:
11769 return "DW_TAG_enumeration_type";
11770 case DW_TAG_formal_parameter:
11771 return "DW_TAG_formal_parameter";
11772 case DW_TAG_imported_declaration:
11773 return "DW_TAG_imported_declaration";
11774 case DW_TAG_label:
11775 return "DW_TAG_label";
11776 case DW_TAG_lexical_block:
11777 return "DW_TAG_lexical_block";
11778 case DW_TAG_member:
11779 return "DW_TAG_member";
11780 case DW_TAG_pointer_type:
11781 return "DW_TAG_pointer_type";
11782 case DW_TAG_reference_type:
11783 return "DW_TAG_reference_type";
11784 case DW_TAG_compile_unit:
11785 return "DW_TAG_compile_unit";
11786 case DW_TAG_string_type:
11787 return "DW_TAG_string_type";
11788 case DW_TAG_structure_type:
11789 return "DW_TAG_structure_type";
11790 case DW_TAG_subroutine_type:
11791 return "DW_TAG_subroutine_type";
11792 case DW_TAG_typedef:
11793 return "DW_TAG_typedef";
11794 case DW_TAG_union_type:
11795 return "DW_TAG_union_type";
11796 case DW_TAG_unspecified_parameters:
11797 return "DW_TAG_unspecified_parameters";
11798 case DW_TAG_variant:
11799 return "DW_TAG_variant";
11800 case DW_TAG_common_block:
11801 return "DW_TAG_common_block";
11802 case DW_TAG_common_inclusion:
11803 return "DW_TAG_common_inclusion";
11804 case DW_TAG_inheritance:
11805 return "DW_TAG_inheritance";
11806 case DW_TAG_inlined_subroutine:
11807 return "DW_TAG_inlined_subroutine";
11808 case DW_TAG_module:
11809 return "DW_TAG_module";
11810 case DW_TAG_ptr_to_member_type:
11811 return "DW_TAG_ptr_to_member_type";
11812 case DW_TAG_set_type:
11813 return "DW_TAG_set_type";
11814 case DW_TAG_subrange_type:
11815 return "DW_TAG_subrange_type";
11816 case DW_TAG_with_stmt:
11817 return "DW_TAG_with_stmt";
11818 case DW_TAG_access_declaration:
11819 return "DW_TAG_access_declaration";
11820 case DW_TAG_base_type:
11821 return "DW_TAG_base_type";
11822 case DW_TAG_catch_block:
11823 return "DW_TAG_catch_block";
11824 case DW_TAG_const_type:
11825 return "DW_TAG_const_type";
11826 case DW_TAG_constant:
11827 return "DW_TAG_constant";
11828 case DW_TAG_enumerator:
11829 return "DW_TAG_enumerator";
11830 case DW_TAG_file_type:
11831 return "DW_TAG_file_type";
11832 case DW_TAG_friend:
11833 return "DW_TAG_friend";
11834 case DW_TAG_namelist:
11835 return "DW_TAG_namelist";
11836 case DW_TAG_namelist_item:
11837 return "DW_TAG_namelist_item";
11838 case DW_TAG_packed_type:
11839 return "DW_TAG_packed_type";
11840 case DW_TAG_subprogram:
11841 return "DW_TAG_subprogram";
11842 case DW_TAG_template_type_param:
11843 return "DW_TAG_template_type_param";
11844 case DW_TAG_template_value_param:
11845 return "DW_TAG_template_value_param";
11846 case DW_TAG_thrown_type:
11847 return "DW_TAG_thrown_type";
11848 case DW_TAG_try_block:
11849 return "DW_TAG_try_block";
11850 case DW_TAG_variant_part:
11851 return "DW_TAG_variant_part";
11852 case DW_TAG_variable:
11853 return "DW_TAG_variable";
11854 case DW_TAG_volatile_type:
11855 return "DW_TAG_volatile_type";
d9fa45fe
DC
11856 case DW_TAG_dwarf_procedure:
11857 return "DW_TAG_dwarf_procedure";
11858 case DW_TAG_restrict_type:
11859 return "DW_TAG_restrict_type";
11860 case DW_TAG_interface_type:
11861 return "DW_TAG_interface_type";
11862 case DW_TAG_namespace:
11863 return "DW_TAG_namespace";
11864 case DW_TAG_imported_module:
11865 return "DW_TAG_imported_module";
11866 case DW_TAG_unspecified_type:
11867 return "DW_TAG_unspecified_type";
11868 case DW_TAG_partial_unit:
11869 return "DW_TAG_partial_unit";
11870 case DW_TAG_imported_unit:
11871 return "DW_TAG_imported_unit";
b7619582
GF
11872 case DW_TAG_condition:
11873 return "DW_TAG_condition";
11874 case DW_TAG_shared_type:
11875 return "DW_TAG_shared_type";
348e048f
DE
11876 case DW_TAG_type_unit:
11877 return "DW_TAG_type_unit";
c906108c
SS
11878 case DW_TAG_MIPS_loop:
11879 return "DW_TAG_MIPS_loop";
b7619582
GF
11880 case DW_TAG_HP_array_descriptor:
11881 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11882 case DW_TAG_format_label:
11883 return "DW_TAG_format_label";
11884 case DW_TAG_function_template:
11885 return "DW_TAG_function_template";
11886 case DW_TAG_class_template:
11887 return "DW_TAG_class_template";
b7619582
GF
11888 case DW_TAG_GNU_BINCL:
11889 return "DW_TAG_GNU_BINCL";
11890 case DW_TAG_GNU_EINCL:
11891 return "DW_TAG_GNU_EINCL";
11892 case DW_TAG_upc_shared_type:
11893 return "DW_TAG_upc_shared_type";
11894 case DW_TAG_upc_strict_type:
11895 return "DW_TAG_upc_strict_type";
11896 case DW_TAG_upc_relaxed_type:
11897 return "DW_TAG_upc_relaxed_type";
11898 case DW_TAG_PGI_kanji_type:
11899 return "DW_TAG_PGI_kanji_type";
11900 case DW_TAG_PGI_interface_block:
11901 return "DW_TAG_PGI_interface_block";
c906108c
SS
11902 default:
11903 return "DW_TAG_<unknown>";
11904 }
11905}
11906
11907/* Convert a DWARF attribute code into its string name. */
11908
11909static char *
aa1ee363 11910dwarf_attr_name (unsigned attr)
c906108c
SS
11911{
11912 switch (attr)
11913 {
11914 case DW_AT_sibling:
11915 return "DW_AT_sibling";
11916 case DW_AT_location:
11917 return "DW_AT_location";
11918 case DW_AT_name:
11919 return "DW_AT_name";
11920 case DW_AT_ordering:
11921 return "DW_AT_ordering";
11922 case DW_AT_subscr_data:
11923 return "DW_AT_subscr_data";
11924 case DW_AT_byte_size:
11925 return "DW_AT_byte_size";
11926 case DW_AT_bit_offset:
11927 return "DW_AT_bit_offset";
11928 case DW_AT_bit_size:
11929 return "DW_AT_bit_size";
11930 case DW_AT_element_list:
11931 return "DW_AT_element_list";
11932 case DW_AT_stmt_list:
11933 return "DW_AT_stmt_list";
11934 case DW_AT_low_pc:
11935 return "DW_AT_low_pc";
11936 case DW_AT_high_pc:
11937 return "DW_AT_high_pc";
11938 case DW_AT_language:
11939 return "DW_AT_language";
11940 case DW_AT_member:
11941 return "DW_AT_member";
11942 case DW_AT_discr:
11943 return "DW_AT_discr";
11944 case DW_AT_discr_value:
11945 return "DW_AT_discr_value";
11946 case DW_AT_visibility:
11947 return "DW_AT_visibility";
11948 case DW_AT_import:
11949 return "DW_AT_import";
11950 case DW_AT_string_length:
11951 return "DW_AT_string_length";
11952 case DW_AT_common_reference:
11953 return "DW_AT_common_reference";
11954 case DW_AT_comp_dir:
11955 return "DW_AT_comp_dir";
11956 case DW_AT_const_value:
11957 return "DW_AT_const_value";
11958 case DW_AT_containing_type:
11959 return "DW_AT_containing_type";
11960 case DW_AT_default_value:
11961 return "DW_AT_default_value";
11962 case DW_AT_inline:
11963 return "DW_AT_inline";
11964 case DW_AT_is_optional:
11965 return "DW_AT_is_optional";
11966 case DW_AT_lower_bound:
11967 return "DW_AT_lower_bound";
11968 case DW_AT_producer:
11969 return "DW_AT_producer";
11970 case DW_AT_prototyped:
11971 return "DW_AT_prototyped";
11972 case DW_AT_return_addr:
11973 return "DW_AT_return_addr";
11974 case DW_AT_start_scope:
11975 return "DW_AT_start_scope";
09fa0d7c
JK
11976 case DW_AT_bit_stride:
11977 return "DW_AT_bit_stride";
c906108c
SS
11978 case DW_AT_upper_bound:
11979 return "DW_AT_upper_bound";
11980 case DW_AT_abstract_origin:
11981 return "DW_AT_abstract_origin";
11982 case DW_AT_accessibility:
11983 return "DW_AT_accessibility";
11984 case DW_AT_address_class:
11985 return "DW_AT_address_class";
11986 case DW_AT_artificial:
11987 return "DW_AT_artificial";
11988 case DW_AT_base_types:
11989 return "DW_AT_base_types";
11990 case DW_AT_calling_convention:
11991 return "DW_AT_calling_convention";
11992 case DW_AT_count:
11993 return "DW_AT_count";
11994 case DW_AT_data_member_location:
11995 return "DW_AT_data_member_location";
11996 case DW_AT_decl_column:
11997 return "DW_AT_decl_column";
11998 case DW_AT_decl_file:
11999 return "DW_AT_decl_file";
12000 case DW_AT_decl_line:
12001 return "DW_AT_decl_line";
12002 case DW_AT_declaration:
12003 return "DW_AT_declaration";
12004 case DW_AT_discr_list:
12005 return "DW_AT_discr_list";
12006 case DW_AT_encoding:
12007 return "DW_AT_encoding";
12008 case DW_AT_external:
12009 return "DW_AT_external";
12010 case DW_AT_frame_base:
12011 return "DW_AT_frame_base";
12012 case DW_AT_friend:
12013 return "DW_AT_friend";
12014 case DW_AT_identifier_case:
12015 return "DW_AT_identifier_case";
12016 case DW_AT_macro_info:
12017 return "DW_AT_macro_info";
12018 case DW_AT_namelist_items:
12019 return "DW_AT_namelist_items";
12020 case DW_AT_priority:
12021 return "DW_AT_priority";
12022 case DW_AT_segment:
12023 return "DW_AT_segment";
12024 case DW_AT_specification:
12025 return "DW_AT_specification";
12026 case DW_AT_static_link:
12027 return "DW_AT_static_link";
12028 case DW_AT_type:
12029 return "DW_AT_type";
12030 case DW_AT_use_location:
12031 return "DW_AT_use_location";
12032 case DW_AT_variable_parameter:
12033 return "DW_AT_variable_parameter";
12034 case DW_AT_virtuality:
12035 return "DW_AT_virtuality";
12036 case DW_AT_vtable_elem_location:
12037 return "DW_AT_vtable_elem_location";
b7619582 12038 /* DWARF 3 values. */
d9fa45fe
DC
12039 case DW_AT_allocated:
12040 return "DW_AT_allocated";
12041 case DW_AT_associated:
12042 return "DW_AT_associated";
12043 case DW_AT_data_location:
12044 return "DW_AT_data_location";
09fa0d7c
JK
12045 case DW_AT_byte_stride:
12046 return "DW_AT_byte_stride";
d9fa45fe
DC
12047 case DW_AT_entry_pc:
12048 return "DW_AT_entry_pc";
12049 case DW_AT_use_UTF8:
12050 return "DW_AT_use_UTF8";
12051 case DW_AT_extension:
12052 return "DW_AT_extension";
12053 case DW_AT_ranges:
12054 return "DW_AT_ranges";
12055 case DW_AT_trampoline:
12056 return "DW_AT_trampoline";
12057 case DW_AT_call_column:
12058 return "DW_AT_call_column";
12059 case DW_AT_call_file:
12060 return "DW_AT_call_file";
12061 case DW_AT_call_line:
12062 return "DW_AT_call_line";
b7619582
GF
12063 case DW_AT_description:
12064 return "DW_AT_description";
12065 case DW_AT_binary_scale:
12066 return "DW_AT_binary_scale";
12067 case DW_AT_decimal_scale:
12068 return "DW_AT_decimal_scale";
12069 case DW_AT_small:
12070 return "DW_AT_small";
12071 case DW_AT_decimal_sign:
12072 return "DW_AT_decimal_sign";
12073 case DW_AT_digit_count:
12074 return "DW_AT_digit_count";
12075 case DW_AT_picture_string:
12076 return "DW_AT_picture_string";
12077 case DW_AT_mutable:
12078 return "DW_AT_mutable";
12079 case DW_AT_threads_scaled:
12080 return "DW_AT_threads_scaled";
12081 case DW_AT_explicit:
12082 return "DW_AT_explicit";
12083 case DW_AT_object_pointer:
12084 return "DW_AT_object_pointer";
12085 case DW_AT_endianity:
12086 return "DW_AT_endianity";
12087 case DW_AT_elemental:
12088 return "DW_AT_elemental";
12089 case DW_AT_pure:
12090 return "DW_AT_pure";
12091 case DW_AT_recursive:
12092 return "DW_AT_recursive";
348e048f
DE
12093 /* DWARF 4 values. */
12094 case DW_AT_signature:
12095 return "DW_AT_signature";
31ef98ae
TT
12096 case DW_AT_linkage_name:
12097 return "DW_AT_linkage_name";
b7619582 12098 /* SGI/MIPS extensions. */
c764a876 12099#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12100 case DW_AT_MIPS_fde:
12101 return "DW_AT_MIPS_fde";
c764a876 12102#endif
c906108c
SS
12103 case DW_AT_MIPS_loop_begin:
12104 return "DW_AT_MIPS_loop_begin";
12105 case DW_AT_MIPS_tail_loop_begin:
12106 return "DW_AT_MIPS_tail_loop_begin";
12107 case DW_AT_MIPS_epilog_begin:
12108 return "DW_AT_MIPS_epilog_begin";
12109 case DW_AT_MIPS_loop_unroll_factor:
12110 return "DW_AT_MIPS_loop_unroll_factor";
12111 case DW_AT_MIPS_software_pipeline_depth:
12112 return "DW_AT_MIPS_software_pipeline_depth";
12113 case DW_AT_MIPS_linkage_name:
12114 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12115 case DW_AT_MIPS_stride:
12116 return "DW_AT_MIPS_stride";
12117 case DW_AT_MIPS_abstract_name:
12118 return "DW_AT_MIPS_abstract_name";
12119 case DW_AT_MIPS_clone_origin:
12120 return "DW_AT_MIPS_clone_origin";
12121 case DW_AT_MIPS_has_inlines:
12122 return "DW_AT_MIPS_has_inlines";
b7619582 12123 /* HP extensions. */
c764a876 12124#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12125 case DW_AT_HP_block_index:
12126 return "DW_AT_HP_block_index";
c764a876 12127#endif
b7619582
GF
12128 case DW_AT_HP_unmodifiable:
12129 return "DW_AT_HP_unmodifiable";
12130 case DW_AT_HP_actuals_stmt_list:
12131 return "DW_AT_HP_actuals_stmt_list";
12132 case DW_AT_HP_proc_per_section:
12133 return "DW_AT_HP_proc_per_section";
12134 case DW_AT_HP_raw_data_ptr:
12135 return "DW_AT_HP_raw_data_ptr";
12136 case DW_AT_HP_pass_by_reference:
12137 return "DW_AT_HP_pass_by_reference";
12138 case DW_AT_HP_opt_level:
12139 return "DW_AT_HP_opt_level";
12140 case DW_AT_HP_prof_version_id:
12141 return "DW_AT_HP_prof_version_id";
12142 case DW_AT_HP_opt_flags:
12143 return "DW_AT_HP_opt_flags";
12144 case DW_AT_HP_cold_region_low_pc:
12145 return "DW_AT_HP_cold_region_low_pc";
12146 case DW_AT_HP_cold_region_high_pc:
12147 return "DW_AT_HP_cold_region_high_pc";
12148 case DW_AT_HP_all_variables_modifiable:
12149 return "DW_AT_HP_all_variables_modifiable";
12150 case DW_AT_HP_linkage_name:
12151 return "DW_AT_HP_linkage_name";
12152 case DW_AT_HP_prof_flags:
12153 return "DW_AT_HP_prof_flags";
12154 /* GNU extensions. */
c906108c
SS
12155 case DW_AT_sf_names:
12156 return "DW_AT_sf_names";
12157 case DW_AT_src_info:
12158 return "DW_AT_src_info";
12159 case DW_AT_mac_info:
12160 return "DW_AT_mac_info";
12161 case DW_AT_src_coords:
12162 return "DW_AT_src_coords";
12163 case DW_AT_body_begin:
12164 return "DW_AT_body_begin";
12165 case DW_AT_body_end:
12166 return "DW_AT_body_end";
f5f8a009
EZ
12167 case DW_AT_GNU_vector:
12168 return "DW_AT_GNU_vector";
2de00c64
DE
12169 case DW_AT_GNU_odr_signature:
12170 return "DW_AT_GNU_odr_signature";
b7619582
GF
12171 /* VMS extensions. */
12172 case DW_AT_VMS_rtnbeg_pd_address:
12173 return "DW_AT_VMS_rtnbeg_pd_address";
12174 /* UPC extension. */
12175 case DW_AT_upc_threads_scaled:
12176 return "DW_AT_upc_threads_scaled";
12177 /* PGI (STMicroelectronics) extensions. */
12178 case DW_AT_PGI_lbase:
12179 return "DW_AT_PGI_lbase";
12180 case DW_AT_PGI_soffset:
12181 return "DW_AT_PGI_soffset";
12182 case DW_AT_PGI_lstride:
12183 return "DW_AT_PGI_lstride";
c906108c
SS
12184 default:
12185 return "DW_AT_<unknown>";
12186 }
12187}
12188
12189/* Convert a DWARF value form code into its string name. */
12190
12191static char *
aa1ee363 12192dwarf_form_name (unsigned form)
c906108c
SS
12193{
12194 switch (form)
12195 {
12196 case DW_FORM_addr:
12197 return "DW_FORM_addr";
12198 case DW_FORM_block2:
12199 return "DW_FORM_block2";
12200 case DW_FORM_block4:
12201 return "DW_FORM_block4";
12202 case DW_FORM_data2:
12203 return "DW_FORM_data2";
12204 case DW_FORM_data4:
12205 return "DW_FORM_data4";
12206 case DW_FORM_data8:
12207 return "DW_FORM_data8";
12208 case DW_FORM_string:
12209 return "DW_FORM_string";
12210 case DW_FORM_block:
12211 return "DW_FORM_block";
12212 case DW_FORM_block1:
12213 return "DW_FORM_block1";
12214 case DW_FORM_data1:
12215 return "DW_FORM_data1";
12216 case DW_FORM_flag:
12217 return "DW_FORM_flag";
12218 case DW_FORM_sdata:
12219 return "DW_FORM_sdata";
12220 case DW_FORM_strp:
12221 return "DW_FORM_strp";
12222 case DW_FORM_udata:
12223 return "DW_FORM_udata";
12224 case DW_FORM_ref_addr:
12225 return "DW_FORM_ref_addr";
12226 case DW_FORM_ref1:
12227 return "DW_FORM_ref1";
12228 case DW_FORM_ref2:
12229 return "DW_FORM_ref2";
12230 case DW_FORM_ref4:
12231 return "DW_FORM_ref4";
12232 case DW_FORM_ref8:
12233 return "DW_FORM_ref8";
12234 case DW_FORM_ref_udata:
12235 return "DW_FORM_ref_udata";
12236 case DW_FORM_indirect:
12237 return "DW_FORM_indirect";
348e048f
DE
12238 case DW_FORM_sec_offset:
12239 return "DW_FORM_sec_offset";
12240 case DW_FORM_exprloc:
12241 return "DW_FORM_exprloc";
12242 case DW_FORM_flag_present:
12243 return "DW_FORM_flag_present";
12244 case DW_FORM_sig8:
12245 return "DW_FORM_sig8";
c906108c
SS
12246 default:
12247 return "DW_FORM_<unknown>";
12248 }
12249}
12250
12251/* Convert a DWARF stack opcode into its string name. */
12252
9eae7c52
TT
12253const char *
12254dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12255{
12256 switch (op)
12257 {
12258 case DW_OP_addr:
12259 return "DW_OP_addr";
12260 case DW_OP_deref:
12261 return "DW_OP_deref";
12262 case DW_OP_const1u:
12263 return "DW_OP_const1u";
12264 case DW_OP_const1s:
12265 return "DW_OP_const1s";
12266 case DW_OP_const2u:
12267 return "DW_OP_const2u";
12268 case DW_OP_const2s:
12269 return "DW_OP_const2s";
12270 case DW_OP_const4u:
12271 return "DW_OP_const4u";
12272 case DW_OP_const4s:
12273 return "DW_OP_const4s";
12274 case DW_OP_const8u:
12275 return "DW_OP_const8u";
12276 case DW_OP_const8s:
12277 return "DW_OP_const8s";
12278 case DW_OP_constu:
12279 return "DW_OP_constu";
12280 case DW_OP_consts:
12281 return "DW_OP_consts";
12282 case DW_OP_dup:
12283 return "DW_OP_dup";
12284 case DW_OP_drop:
12285 return "DW_OP_drop";
12286 case DW_OP_over:
12287 return "DW_OP_over";
12288 case DW_OP_pick:
12289 return "DW_OP_pick";
12290 case DW_OP_swap:
12291 return "DW_OP_swap";
12292 case DW_OP_rot:
12293 return "DW_OP_rot";
12294 case DW_OP_xderef:
12295 return "DW_OP_xderef";
12296 case DW_OP_abs:
12297 return "DW_OP_abs";
12298 case DW_OP_and:
12299 return "DW_OP_and";
12300 case DW_OP_div:
12301 return "DW_OP_div";
12302 case DW_OP_minus:
12303 return "DW_OP_minus";
12304 case DW_OP_mod:
12305 return "DW_OP_mod";
12306 case DW_OP_mul:
12307 return "DW_OP_mul";
12308 case DW_OP_neg:
12309 return "DW_OP_neg";
12310 case DW_OP_not:
12311 return "DW_OP_not";
12312 case DW_OP_or:
12313 return "DW_OP_or";
12314 case DW_OP_plus:
12315 return "DW_OP_plus";
12316 case DW_OP_plus_uconst:
12317 return "DW_OP_plus_uconst";
12318 case DW_OP_shl:
12319 return "DW_OP_shl";
12320 case DW_OP_shr:
12321 return "DW_OP_shr";
12322 case DW_OP_shra:
12323 return "DW_OP_shra";
12324 case DW_OP_xor:
12325 return "DW_OP_xor";
12326 case DW_OP_bra:
12327 return "DW_OP_bra";
12328 case DW_OP_eq:
12329 return "DW_OP_eq";
12330 case DW_OP_ge:
12331 return "DW_OP_ge";
12332 case DW_OP_gt:
12333 return "DW_OP_gt";
12334 case DW_OP_le:
12335 return "DW_OP_le";
12336 case DW_OP_lt:
12337 return "DW_OP_lt";
12338 case DW_OP_ne:
12339 return "DW_OP_ne";
12340 case DW_OP_skip:
12341 return "DW_OP_skip";
12342 case DW_OP_lit0:
12343 return "DW_OP_lit0";
12344 case DW_OP_lit1:
12345 return "DW_OP_lit1";
12346 case DW_OP_lit2:
12347 return "DW_OP_lit2";
12348 case DW_OP_lit3:
12349 return "DW_OP_lit3";
12350 case DW_OP_lit4:
12351 return "DW_OP_lit4";
12352 case DW_OP_lit5:
12353 return "DW_OP_lit5";
12354 case DW_OP_lit6:
12355 return "DW_OP_lit6";
12356 case DW_OP_lit7:
12357 return "DW_OP_lit7";
12358 case DW_OP_lit8:
12359 return "DW_OP_lit8";
12360 case DW_OP_lit9:
12361 return "DW_OP_lit9";
12362 case DW_OP_lit10:
12363 return "DW_OP_lit10";
12364 case DW_OP_lit11:
12365 return "DW_OP_lit11";
12366 case DW_OP_lit12:
12367 return "DW_OP_lit12";
12368 case DW_OP_lit13:
12369 return "DW_OP_lit13";
12370 case DW_OP_lit14:
12371 return "DW_OP_lit14";
12372 case DW_OP_lit15:
12373 return "DW_OP_lit15";
12374 case DW_OP_lit16:
12375 return "DW_OP_lit16";
12376 case DW_OP_lit17:
12377 return "DW_OP_lit17";
12378 case DW_OP_lit18:
12379 return "DW_OP_lit18";
12380 case DW_OP_lit19:
12381 return "DW_OP_lit19";
12382 case DW_OP_lit20:
12383 return "DW_OP_lit20";
12384 case DW_OP_lit21:
12385 return "DW_OP_lit21";
12386 case DW_OP_lit22:
12387 return "DW_OP_lit22";
12388 case DW_OP_lit23:
12389 return "DW_OP_lit23";
12390 case DW_OP_lit24:
12391 return "DW_OP_lit24";
12392 case DW_OP_lit25:
12393 return "DW_OP_lit25";
12394 case DW_OP_lit26:
12395 return "DW_OP_lit26";
12396 case DW_OP_lit27:
12397 return "DW_OP_lit27";
12398 case DW_OP_lit28:
12399 return "DW_OP_lit28";
12400 case DW_OP_lit29:
12401 return "DW_OP_lit29";
12402 case DW_OP_lit30:
12403 return "DW_OP_lit30";
12404 case DW_OP_lit31:
12405 return "DW_OP_lit31";
12406 case DW_OP_reg0:
12407 return "DW_OP_reg0";
12408 case DW_OP_reg1:
12409 return "DW_OP_reg1";
12410 case DW_OP_reg2:
12411 return "DW_OP_reg2";
12412 case DW_OP_reg3:
12413 return "DW_OP_reg3";
12414 case DW_OP_reg4:
12415 return "DW_OP_reg4";
12416 case DW_OP_reg5:
12417 return "DW_OP_reg5";
12418 case DW_OP_reg6:
12419 return "DW_OP_reg6";
12420 case DW_OP_reg7:
12421 return "DW_OP_reg7";
12422 case DW_OP_reg8:
12423 return "DW_OP_reg8";
12424 case DW_OP_reg9:
12425 return "DW_OP_reg9";
12426 case DW_OP_reg10:
12427 return "DW_OP_reg10";
12428 case DW_OP_reg11:
12429 return "DW_OP_reg11";
12430 case DW_OP_reg12:
12431 return "DW_OP_reg12";
12432 case DW_OP_reg13:
12433 return "DW_OP_reg13";
12434 case DW_OP_reg14:
12435 return "DW_OP_reg14";
12436 case DW_OP_reg15:
12437 return "DW_OP_reg15";
12438 case DW_OP_reg16:
12439 return "DW_OP_reg16";
12440 case DW_OP_reg17:
12441 return "DW_OP_reg17";
12442 case DW_OP_reg18:
12443 return "DW_OP_reg18";
12444 case DW_OP_reg19:
12445 return "DW_OP_reg19";
12446 case DW_OP_reg20:
12447 return "DW_OP_reg20";
12448 case DW_OP_reg21:
12449 return "DW_OP_reg21";
12450 case DW_OP_reg22:
12451 return "DW_OP_reg22";
12452 case DW_OP_reg23:
12453 return "DW_OP_reg23";
12454 case DW_OP_reg24:
12455 return "DW_OP_reg24";
12456 case DW_OP_reg25:
12457 return "DW_OP_reg25";
12458 case DW_OP_reg26:
12459 return "DW_OP_reg26";
12460 case DW_OP_reg27:
12461 return "DW_OP_reg27";
12462 case DW_OP_reg28:
12463 return "DW_OP_reg28";
12464 case DW_OP_reg29:
12465 return "DW_OP_reg29";
12466 case DW_OP_reg30:
12467 return "DW_OP_reg30";
12468 case DW_OP_reg31:
12469 return "DW_OP_reg31";
12470 case DW_OP_breg0:
12471 return "DW_OP_breg0";
12472 case DW_OP_breg1:
12473 return "DW_OP_breg1";
12474 case DW_OP_breg2:
12475 return "DW_OP_breg2";
12476 case DW_OP_breg3:
12477 return "DW_OP_breg3";
12478 case DW_OP_breg4:
12479 return "DW_OP_breg4";
12480 case DW_OP_breg5:
12481 return "DW_OP_breg5";
12482 case DW_OP_breg6:
12483 return "DW_OP_breg6";
12484 case DW_OP_breg7:
12485 return "DW_OP_breg7";
12486 case DW_OP_breg8:
12487 return "DW_OP_breg8";
12488 case DW_OP_breg9:
12489 return "DW_OP_breg9";
12490 case DW_OP_breg10:
12491 return "DW_OP_breg10";
12492 case DW_OP_breg11:
12493 return "DW_OP_breg11";
12494 case DW_OP_breg12:
12495 return "DW_OP_breg12";
12496 case DW_OP_breg13:
12497 return "DW_OP_breg13";
12498 case DW_OP_breg14:
12499 return "DW_OP_breg14";
12500 case DW_OP_breg15:
12501 return "DW_OP_breg15";
12502 case DW_OP_breg16:
12503 return "DW_OP_breg16";
12504 case DW_OP_breg17:
12505 return "DW_OP_breg17";
12506 case DW_OP_breg18:
12507 return "DW_OP_breg18";
12508 case DW_OP_breg19:
12509 return "DW_OP_breg19";
12510 case DW_OP_breg20:
12511 return "DW_OP_breg20";
12512 case DW_OP_breg21:
12513 return "DW_OP_breg21";
12514 case DW_OP_breg22:
12515 return "DW_OP_breg22";
12516 case DW_OP_breg23:
12517 return "DW_OP_breg23";
12518 case DW_OP_breg24:
12519 return "DW_OP_breg24";
12520 case DW_OP_breg25:
12521 return "DW_OP_breg25";
12522 case DW_OP_breg26:
12523 return "DW_OP_breg26";
12524 case DW_OP_breg27:
12525 return "DW_OP_breg27";
12526 case DW_OP_breg28:
12527 return "DW_OP_breg28";
12528 case DW_OP_breg29:
12529 return "DW_OP_breg29";
12530 case DW_OP_breg30:
12531 return "DW_OP_breg30";
12532 case DW_OP_breg31:
12533 return "DW_OP_breg31";
12534 case DW_OP_regx:
12535 return "DW_OP_regx";
12536 case DW_OP_fbreg:
12537 return "DW_OP_fbreg";
12538 case DW_OP_bregx:
12539 return "DW_OP_bregx";
12540 case DW_OP_piece:
12541 return "DW_OP_piece";
12542 case DW_OP_deref_size:
12543 return "DW_OP_deref_size";
12544 case DW_OP_xderef_size:
12545 return "DW_OP_xderef_size";
12546 case DW_OP_nop:
12547 return "DW_OP_nop";
b7619582 12548 /* DWARF 3 extensions. */
ed348acc
EZ
12549 case DW_OP_push_object_address:
12550 return "DW_OP_push_object_address";
12551 case DW_OP_call2:
12552 return "DW_OP_call2";
12553 case DW_OP_call4:
12554 return "DW_OP_call4";
12555 case DW_OP_call_ref:
12556 return "DW_OP_call_ref";
b7619582
GF
12557 case DW_OP_form_tls_address:
12558 return "DW_OP_form_tls_address";
12559 case DW_OP_call_frame_cfa:
12560 return "DW_OP_call_frame_cfa";
12561 case DW_OP_bit_piece:
12562 return "DW_OP_bit_piece";
9eae7c52
TT
12563 /* DWARF 4 extensions. */
12564 case DW_OP_implicit_value:
12565 return "DW_OP_implicit_value";
12566 case DW_OP_stack_value:
12567 return "DW_OP_stack_value";
12568 /* GNU extensions. */
ed348acc
EZ
12569 case DW_OP_GNU_push_tls_address:
12570 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12571 case DW_OP_GNU_uninit:
12572 return "DW_OP_GNU_uninit";
c906108c 12573 default:
9eae7c52 12574 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12575 }
12576}
12577
12578static char *
fba45db2 12579dwarf_bool_name (unsigned mybool)
c906108c
SS
12580{
12581 if (mybool)
12582 return "TRUE";
12583 else
12584 return "FALSE";
12585}
12586
12587/* Convert a DWARF type code into its string name. */
12588
12589static char *
aa1ee363 12590dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12591{
12592 switch (enc)
12593 {
b7619582
GF
12594 case DW_ATE_void:
12595 return "DW_ATE_void";
c906108c
SS
12596 case DW_ATE_address:
12597 return "DW_ATE_address";
12598 case DW_ATE_boolean:
12599 return "DW_ATE_boolean";
12600 case DW_ATE_complex_float:
12601 return "DW_ATE_complex_float";
12602 case DW_ATE_float:
12603 return "DW_ATE_float";
12604 case DW_ATE_signed:
12605 return "DW_ATE_signed";
12606 case DW_ATE_signed_char:
12607 return "DW_ATE_signed_char";
12608 case DW_ATE_unsigned:
12609 return "DW_ATE_unsigned";
12610 case DW_ATE_unsigned_char:
12611 return "DW_ATE_unsigned_char";
b7619582 12612 /* DWARF 3. */
d9fa45fe
DC
12613 case DW_ATE_imaginary_float:
12614 return "DW_ATE_imaginary_float";
b7619582
GF
12615 case DW_ATE_packed_decimal:
12616 return "DW_ATE_packed_decimal";
12617 case DW_ATE_numeric_string:
12618 return "DW_ATE_numeric_string";
12619 case DW_ATE_edited:
12620 return "DW_ATE_edited";
12621 case DW_ATE_signed_fixed:
12622 return "DW_ATE_signed_fixed";
12623 case DW_ATE_unsigned_fixed:
12624 return "DW_ATE_unsigned_fixed";
12625 case DW_ATE_decimal_float:
12626 return "DW_ATE_decimal_float";
75079b2b
TT
12627 /* DWARF 4. */
12628 case DW_ATE_UTF:
12629 return "DW_ATE_UTF";
b7619582
GF
12630 /* HP extensions. */
12631 case DW_ATE_HP_float80:
12632 return "DW_ATE_HP_float80";
12633 case DW_ATE_HP_complex_float80:
12634 return "DW_ATE_HP_complex_float80";
12635 case DW_ATE_HP_float128:
12636 return "DW_ATE_HP_float128";
12637 case DW_ATE_HP_complex_float128:
12638 return "DW_ATE_HP_complex_float128";
12639 case DW_ATE_HP_floathpintel:
12640 return "DW_ATE_HP_floathpintel";
12641 case DW_ATE_HP_imaginary_float80:
12642 return "DW_ATE_HP_imaginary_float80";
12643 case DW_ATE_HP_imaginary_float128:
12644 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12645 default:
12646 return "DW_ATE_<unknown>";
12647 }
12648}
12649
12650/* Convert a DWARF call frame info operation to its string name. */
12651
12652#if 0
12653static char *
aa1ee363 12654dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12655{
12656 switch (cfi_opc)
12657 {
12658 case DW_CFA_advance_loc:
12659 return "DW_CFA_advance_loc";
12660 case DW_CFA_offset:
12661 return "DW_CFA_offset";
12662 case DW_CFA_restore:
12663 return "DW_CFA_restore";
12664 case DW_CFA_nop:
12665 return "DW_CFA_nop";
12666 case DW_CFA_set_loc:
12667 return "DW_CFA_set_loc";
12668 case DW_CFA_advance_loc1:
12669 return "DW_CFA_advance_loc1";
12670 case DW_CFA_advance_loc2:
12671 return "DW_CFA_advance_loc2";
12672 case DW_CFA_advance_loc4:
12673 return "DW_CFA_advance_loc4";
12674 case DW_CFA_offset_extended:
12675 return "DW_CFA_offset_extended";
12676 case DW_CFA_restore_extended:
12677 return "DW_CFA_restore_extended";
12678 case DW_CFA_undefined:
12679 return "DW_CFA_undefined";
12680 case DW_CFA_same_value:
12681 return "DW_CFA_same_value";
12682 case DW_CFA_register:
12683 return "DW_CFA_register";
12684 case DW_CFA_remember_state:
12685 return "DW_CFA_remember_state";
12686 case DW_CFA_restore_state:
12687 return "DW_CFA_restore_state";
12688 case DW_CFA_def_cfa:
12689 return "DW_CFA_def_cfa";
12690 case DW_CFA_def_cfa_register:
12691 return "DW_CFA_def_cfa_register";
12692 case DW_CFA_def_cfa_offset:
12693 return "DW_CFA_def_cfa_offset";
b7619582 12694 /* DWARF 3. */
985cb1a3
JM
12695 case DW_CFA_def_cfa_expression:
12696 return "DW_CFA_def_cfa_expression";
12697 case DW_CFA_expression:
12698 return "DW_CFA_expression";
12699 case DW_CFA_offset_extended_sf:
12700 return "DW_CFA_offset_extended_sf";
12701 case DW_CFA_def_cfa_sf:
12702 return "DW_CFA_def_cfa_sf";
12703 case DW_CFA_def_cfa_offset_sf:
12704 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12705 case DW_CFA_val_offset:
12706 return "DW_CFA_val_offset";
12707 case DW_CFA_val_offset_sf:
12708 return "DW_CFA_val_offset_sf";
12709 case DW_CFA_val_expression:
12710 return "DW_CFA_val_expression";
12711 /* SGI/MIPS specific. */
c906108c
SS
12712 case DW_CFA_MIPS_advance_loc8:
12713 return "DW_CFA_MIPS_advance_loc8";
b7619582 12714 /* GNU extensions. */
985cb1a3
JM
12715 case DW_CFA_GNU_window_save:
12716 return "DW_CFA_GNU_window_save";
12717 case DW_CFA_GNU_args_size:
12718 return "DW_CFA_GNU_args_size";
12719 case DW_CFA_GNU_negative_offset_extended:
12720 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12721 default:
12722 return "DW_CFA_<unknown>";
12723 }
12724}
12725#endif
12726
f9aca02d 12727static void
d97bc12b 12728dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12729{
12730 unsigned int i;
12731
d97bc12b
DE
12732 print_spaces (indent, f);
12733 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12734 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12735
12736 if (die->parent != NULL)
12737 {
12738 print_spaces (indent, f);
12739 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12740 die->parent->offset);
12741 }
12742
12743 print_spaces (indent, f);
12744 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12745 dwarf_bool_name (die->child != NULL));
c906108c 12746
d97bc12b
DE
12747 print_spaces (indent, f);
12748 fprintf_unfiltered (f, " attributes:\n");
12749
c906108c
SS
12750 for (i = 0; i < die->num_attrs; ++i)
12751 {
d97bc12b
DE
12752 print_spaces (indent, f);
12753 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12754 dwarf_attr_name (die->attrs[i].name),
12755 dwarf_form_name (die->attrs[i].form));
d97bc12b 12756
c906108c
SS
12757 switch (die->attrs[i].form)
12758 {
12759 case DW_FORM_ref_addr:
12760 case DW_FORM_addr:
d97bc12b 12761 fprintf_unfiltered (f, "address: ");
5af949e3 12762 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12763 break;
12764 case DW_FORM_block2:
12765 case DW_FORM_block4:
12766 case DW_FORM_block:
12767 case DW_FORM_block1:
d97bc12b 12768 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12769 break;
2dc7f7b3
TT
12770 case DW_FORM_exprloc:
12771 fprintf_unfiltered (f, "expression: size %u",
12772 DW_BLOCK (&die->attrs[i])->size);
12773 break;
10b3939b
DJ
12774 case DW_FORM_ref1:
12775 case DW_FORM_ref2:
12776 case DW_FORM_ref4:
d97bc12b 12777 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12778 (long) (DW_ADDR (&die->attrs[i])));
12779 break;
c906108c
SS
12780 case DW_FORM_data1:
12781 case DW_FORM_data2:
12782 case DW_FORM_data4:
ce5d95e1 12783 case DW_FORM_data8:
c906108c
SS
12784 case DW_FORM_udata:
12785 case DW_FORM_sdata:
43bbcdc2
PH
12786 fprintf_unfiltered (f, "constant: %s",
12787 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12788 break;
2dc7f7b3
TT
12789 case DW_FORM_sec_offset:
12790 fprintf_unfiltered (f, "section offset: %s",
12791 pulongest (DW_UNSND (&die->attrs[i])));
12792 break;
348e048f
DE
12793 case DW_FORM_sig8:
12794 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12795 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12796 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12797 else
12798 fprintf_unfiltered (f, "signatured type, offset: unknown");
12799 break;
c906108c 12800 case DW_FORM_string:
4bdf3d34 12801 case DW_FORM_strp:
8285870a 12802 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12803 DW_STRING (&die->attrs[i])
8285870a
JK
12804 ? DW_STRING (&die->attrs[i]) : "",
12805 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12806 break;
12807 case DW_FORM_flag:
12808 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12809 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12810 else
d97bc12b 12811 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12812 break;
2dc7f7b3
TT
12813 case DW_FORM_flag_present:
12814 fprintf_unfiltered (f, "flag: TRUE");
12815 break;
a8329558
KW
12816 case DW_FORM_indirect:
12817 /* the reader will have reduced the indirect form to
12818 the "base form" so this form should not occur */
d97bc12b 12819 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12820 break;
c906108c 12821 default:
d97bc12b 12822 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12823 die->attrs[i].form);
d97bc12b 12824 break;
c906108c 12825 }
d97bc12b 12826 fprintf_unfiltered (f, "\n");
c906108c
SS
12827 }
12828}
12829
f9aca02d 12830static void
d97bc12b 12831dump_die_for_error (struct die_info *die)
c906108c 12832{
d97bc12b
DE
12833 dump_die_shallow (gdb_stderr, 0, die);
12834}
12835
12836static void
12837dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12838{
12839 int indent = level * 4;
12840
12841 gdb_assert (die != NULL);
12842
12843 if (level >= max_level)
12844 return;
12845
12846 dump_die_shallow (f, indent, die);
12847
12848 if (die->child != NULL)
c906108c 12849 {
d97bc12b
DE
12850 print_spaces (indent, f);
12851 fprintf_unfiltered (f, " Children:");
12852 if (level + 1 < max_level)
12853 {
12854 fprintf_unfiltered (f, "\n");
12855 dump_die_1 (f, level + 1, max_level, die->child);
12856 }
12857 else
12858 {
12859 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12860 }
12861 }
12862
12863 if (die->sibling != NULL && level > 0)
12864 {
12865 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12866 }
12867}
12868
d97bc12b
DE
12869/* This is called from the pdie macro in gdbinit.in.
12870 It's not static so gcc will keep a copy callable from gdb. */
12871
12872void
12873dump_die (struct die_info *die, int max_level)
12874{
12875 dump_die_1 (gdb_stdlog, 0, max_level, die);
12876}
12877
f9aca02d 12878static void
51545339 12879store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12880{
51545339 12881 void **slot;
c906108c 12882
51545339
DJ
12883 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12884
12885 *slot = die;
c906108c
SS
12886}
12887
93311388
DE
12888static int
12889is_ref_attr (struct attribute *attr)
c906108c 12890{
c906108c
SS
12891 switch (attr->form)
12892 {
12893 case DW_FORM_ref_addr:
c906108c
SS
12894 case DW_FORM_ref1:
12895 case DW_FORM_ref2:
12896 case DW_FORM_ref4:
613e1657 12897 case DW_FORM_ref8:
c906108c 12898 case DW_FORM_ref_udata:
93311388 12899 return 1;
c906108c 12900 default:
93311388 12901 return 0;
c906108c 12902 }
93311388
DE
12903}
12904
12905static unsigned int
12906dwarf2_get_ref_die_offset (struct attribute *attr)
12907{
12908 if (is_ref_attr (attr))
12909 return DW_ADDR (attr);
12910
12911 complaint (&symfile_complaints,
12912 _("unsupported die ref attribute form: '%s'"),
12913 dwarf_form_name (attr->form));
12914 return 0;
c906108c
SS
12915}
12916
43bbcdc2
PH
12917/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12918 * the value held by the attribute is not constant. */
a02abb62 12919
43bbcdc2 12920static LONGEST
a02abb62
JB
12921dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12922{
12923 if (attr->form == DW_FORM_sdata)
12924 return DW_SND (attr);
12925 else if (attr->form == DW_FORM_udata
12926 || attr->form == DW_FORM_data1
12927 || attr->form == DW_FORM_data2
12928 || attr->form == DW_FORM_data4
12929 || attr->form == DW_FORM_data8)
12930 return DW_UNSND (attr);
12931 else
12932 {
e2e0b3e5 12933 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12934 dwarf_form_name (attr->form));
12935 return default_value;
12936 }
12937}
12938
03dd20cc 12939/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12940 unit and add it to our queue.
12941 The result is non-zero if PER_CU was queued, otherwise the result is zero
12942 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12943
348e048f 12944static int
03dd20cc
DJ
12945maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12946 struct dwarf2_per_cu_data *per_cu)
12947{
98bfdba5
PA
12948 /* We may arrive here during partial symbol reading, if we need full
12949 DIEs to process an unusual case (e.g. template arguments). Do
12950 not queue PER_CU, just tell our caller to load its DIEs. */
12951 if (dwarf2_per_objfile->reading_partial_symbols)
12952 {
12953 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12954 return 1;
12955 return 0;
12956 }
12957
03dd20cc
DJ
12958 /* Mark the dependence relation so that we don't flush PER_CU
12959 too early. */
12960 dwarf2_add_dependence (this_cu, per_cu);
12961
12962 /* If it's already on the queue, we have nothing to do. */
12963 if (per_cu->queued)
348e048f 12964 return 0;
03dd20cc
DJ
12965
12966 /* If the compilation unit is already loaded, just mark it as
12967 used. */
12968 if (per_cu->cu != NULL)
12969 {
12970 per_cu->cu->last_used = 0;
348e048f 12971 return 0;
03dd20cc
DJ
12972 }
12973
12974 /* Add it to the queue. */
12975 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12976
12977 return 1;
12978}
12979
12980/* Follow reference or signature attribute ATTR of SRC_DIE.
12981 On entry *REF_CU is the CU of SRC_DIE.
12982 On exit *REF_CU is the CU of the result. */
12983
12984static struct die_info *
12985follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12986 struct dwarf2_cu **ref_cu)
12987{
12988 struct die_info *die;
12989
12990 if (is_ref_attr (attr))
12991 die = follow_die_ref (src_die, attr, ref_cu);
12992 else if (attr->form == DW_FORM_sig8)
12993 die = follow_die_sig (src_die, attr, ref_cu);
12994 else
12995 {
12996 dump_die_for_error (src_die);
12997 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12998 (*ref_cu)->objfile->name);
12999 }
13000
13001 return die;
03dd20cc
DJ
13002}
13003
5c631832 13004/* Follow reference OFFSET.
673bfd45
DE
13005 On entry *REF_CU is the CU of the source die referencing OFFSET.
13006 On exit *REF_CU is the CU of the result.
13007 Returns NULL if OFFSET is invalid. */
f504f079 13008
f9aca02d 13009static struct die_info *
5c631832 13010follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13011{
10b3939b 13012 struct die_info temp_die;
f2f0e013 13013 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13014
348e048f
DE
13015 gdb_assert (cu->per_cu != NULL);
13016
98bfdba5
PA
13017 target_cu = cu;
13018
348e048f
DE
13019 if (cu->per_cu->from_debug_types)
13020 {
13021 /* .debug_types CUs cannot reference anything outside their CU.
13022 If they need to, they have to reference a signatured type via
13023 DW_FORM_sig8. */
13024 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13025 return NULL;
348e048f
DE
13026 }
13027 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13028 {
13029 struct dwarf2_per_cu_data *per_cu;
9a619af0 13030
45452591 13031 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13032
13033 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13034 if (maybe_queue_comp_unit (cu, per_cu))
13035 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13036
10b3939b
DJ
13037 target_cu = per_cu->cu;
13038 }
98bfdba5
PA
13039 else if (cu->dies == NULL)
13040 {
13041 /* We're loading full DIEs during partial symbol reading. */
13042 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13043 load_full_comp_unit (cu->per_cu, cu->objfile);
13044 }
c906108c 13045
f2f0e013 13046 *ref_cu = target_cu;
51545339 13047 temp_die.offset = offset;
5c631832
JK
13048 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13049}
10b3939b 13050
5c631832
JK
13051/* Follow reference attribute ATTR of SRC_DIE.
13052 On entry *REF_CU is the CU of SRC_DIE.
13053 On exit *REF_CU is the CU of the result. */
13054
13055static struct die_info *
13056follow_die_ref (struct die_info *src_die, struct attribute *attr,
13057 struct dwarf2_cu **ref_cu)
13058{
13059 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13060 struct dwarf2_cu *cu = *ref_cu;
13061 struct die_info *die;
13062
13063 die = follow_die_offset (offset, ref_cu);
13064 if (!die)
13065 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13066 "at 0x%x [in module %s]"),
13067 offset, src_die->offset, cu->objfile->name);
348e048f 13068
5c631832
JK
13069 return die;
13070}
13071
13072/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13073 value is intended for DW_OP_call*. */
13074
13075struct dwarf2_locexpr_baton
13076dwarf2_fetch_die_location_block (unsigned int offset,
13077 struct dwarf2_per_cu_data *per_cu)
13078{
13079 struct dwarf2_cu *cu = per_cu->cu;
13080 struct die_info *die;
13081 struct attribute *attr;
13082 struct dwarf2_locexpr_baton retval;
13083
13084 die = follow_die_offset (offset, &cu);
13085 if (!die)
13086 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13087 offset, per_cu->cu->objfile->name);
13088
13089 attr = dwarf2_attr (die, DW_AT_location, cu);
13090 if (!attr)
13091 {
13092 /* DWARF: "If there is no such attribute, then there is no effect.". */
13093
13094 retval.data = NULL;
13095 retval.size = 0;
13096 }
13097 else
13098 {
13099 if (!attr_form_is_block (attr))
13100 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13101 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13102 offset, per_cu->cu->objfile->name);
13103
13104 retval.data = DW_BLOCK (attr)->data;
13105 retval.size = DW_BLOCK (attr)->size;
13106 }
13107 retval.per_cu = cu->per_cu;
13108 return retval;
348e048f
DE
13109}
13110
13111/* Follow the signature attribute ATTR in SRC_DIE.
13112 On entry *REF_CU is the CU of SRC_DIE.
13113 On exit *REF_CU is the CU of the result. */
13114
13115static struct die_info *
13116follow_die_sig (struct die_info *src_die, struct attribute *attr,
13117 struct dwarf2_cu **ref_cu)
13118{
13119 struct objfile *objfile = (*ref_cu)->objfile;
13120 struct die_info temp_die;
13121 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13122 struct dwarf2_cu *sig_cu;
13123 struct die_info *die;
13124
13125 /* sig_type will be NULL if the signatured type is missing from
13126 the debug info. */
13127 if (sig_type == NULL)
13128 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13129 "at 0x%x [in module %s]"),
13130 src_die->offset, objfile->name);
13131
13132 /* If necessary, add it to the queue and load its DIEs. */
13133
13134 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13135 read_signatured_type (objfile, sig_type);
13136
13137 gdb_assert (sig_type->per_cu.cu != NULL);
13138
13139 sig_cu = sig_type->per_cu.cu;
13140 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13141 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13142 if (die)
13143 {
13144 *ref_cu = sig_cu;
13145 return die;
13146 }
13147
13148 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13149 "at 0x%x [in module %s]"),
13150 sig_type->type_offset, src_die->offset, objfile->name);
13151}
13152
13153/* Given an offset of a signatured type, return its signatured_type. */
13154
13155static struct signatured_type *
13156lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13157{
13158 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13159 unsigned int length, initial_length_size;
13160 unsigned int sig_offset;
13161 struct signatured_type find_entry, *type_sig;
13162
13163 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13164 sig_offset = (initial_length_size
13165 + 2 /*version*/
13166 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13167 + 1 /*address_size*/);
13168 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13169 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13170
13171 /* This is only used to lookup previously recorded types.
13172 If we didn't find it, it's our bug. */
13173 gdb_assert (type_sig != NULL);
13174 gdb_assert (offset == type_sig->offset);
13175
13176 return type_sig;
13177}
13178
13179/* Read in signatured type at OFFSET and build its CU and die(s). */
13180
13181static void
13182read_signatured_type_at_offset (struct objfile *objfile,
13183 unsigned int offset)
13184{
13185 struct signatured_type *type_sig;
13186
be391dca
TT
13187 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13188
348e048f
DE
13189 /* We have the section offset, but we need the signature to do the
13190 hash table lookup. */
13191 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13192
13193 gdb_assert (type_sig->per_cu.cu == NULL);
13194
13195 read_signatured_type (objfile, type_sig);
13196
13197 gdb_assert (type_sig->per_cu.cu != NULL);
13198}
13199
13200/* Read in a signatured type and build its CU and DIEs. */
13201
13202static void
13203read_signatured_type (struct objfile *objfile,
13204 struct signatured_type *type_sig)
13205{
1fd400ff 13206 gdb_byte *types_ptr;
348e048f
DE
13207 struct die_reader_specs reader_specs;
13208 struct dwarf2_cu *cu;
13209 ULONGEST signature;
13210 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13211
1fd400ff
TT
13212 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13213 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13214
348e048f
DE
13215 gdb_assert (type_sig->per_cu.cu == NULL);
13216
9816fde3
JK
13217 cu = xmalloc (sizeof (*cu));
13218 init_one_comp_unit (cu, objfile);
13219
348e048f
DE
13220 type_sig->per_cu.cu = cu;
13221 cu->per_cu = &type_sig->per_cu;
13222
13223 /* If an error occurs while loading, release our storage. */
13224 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13225
13226 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13227 types_ptr, objfile->obfd);
13228 gdb_assert (signature == type_sig->signature);
13229
13230 cu->die_hash
13231 = htab_create_alloc_ex (cu->header.length / 12,
13232 die_hash,
13233 die_eq,
13234 NULL,
13235 &cu->comp_unit_obstack,
13236 hashtab_obstack_allocate,
13237 dummy_obstack_deallocate);
13238
13239 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13240 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13241
13242 init_cu_die_reader (&reader_specs, cu);
13243
13244 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13245 NULL /*parent*/);
13246
13247 /* We try not to read any attributes in this function, because not
13248 all objfiles needed for references have been loaded yet, and symbol
13249 table processing isn't initialized. But we have to set the CU language,
13250 or we won't be able to build types correctly. */
9816fde3 13251 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13252
13253 do_cleanups (back_to);
13254
13255 /* We've successfully allocated this compilation unit. Let our caller
13256 clean it up when finished with it. */
13257 discard_cleanups (free_cu_cleanup);
13258
13259 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13260 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13261}
13262
c906108c
SS
13263/* Decode simple location descriptions.
13264 Given a pointer to a dwarf block that defines a location, compute
13265 the location and return the value.
13266
4cecd739
DJ
13267 NOTE drow/2003-11-18: This function is called in two situations
13268 now: for the address of static or global variables (partial symbols
13269 only) and for offsets into structures which are expected to be
13270 (more or less) constant. The partial symbol case should go away,
13271 and only the constant case should remain. That will let this
13272 function complain more accurately. A few special modes are allowed
13273 without complaint for global variables (for instance, global
13274 register values and thread-local values).
c906108c
SS
13275
13276 A location description containing no operations indicates that the
4cecd739 13277 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13278 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13279 callers will only want a very basic result and this can become a
13280 complaint.
c906108c 13281
c906108c
SS
13282 Note that stack[0] is unused except as a default error return.
13283 Note that stack overflow is not yet handled. */
13284
13285static CORE_ADDR
e7c27a73 13286decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13287{
e7c27a73 13288 struct objfile *objfile = cu->objfile;
c906108c
SS
13289 int i;
13290 int size = blk->size;
fe1b8b76 13291 gdb_byte *data = blk->data;
c906108c
SS
13292 CORE_ADDR stack[64];
13293 int stacki;
13294 unsigned int bytes_read, unsnd;
fe1b8b76 13295 gdb_byte op;
c906108c
SS
13296
13297 i = 0;
13298 stacki = 0;
13299 stack[stacki] = 0;
c906108c
SS
13300
13301 while (i < size)
13302 {
c906108c
SS
13303 op = data[i++];
13304 switch (op)
13305 {
f1bea926
JM
13306 case DW_OP_lit0:
13307 case DW_OP_lit1:
13308 case DW_OP_lit2:
13309 case DW_OP_lit3:
13310 case DW_OP_lit4:
13311 case DW_OP_lit5:
13312 case DW_OP_lit6:
13313 case DW_OP_lit7:
13314 case DW_OP_lit8:
13315 case DW_OP_lit9:
13316 case DW_OP_lit10:
13317 case DW_OP_lit11:
13318 case DW_OP_lit12:
13319 case DW_OP_lit13:
13320 case DW_OP_lit14:
13321 case DW_OP_lit15:
13322 case DW_OP_lit16:
13323 case DW_OP_lit17:
13324 case DW_OP_lit18:
13325 case DW_OP_lit19:
13326 case DW_OP_lit20:
13327 case DW_OP_lit21:
13328 case DW_OP_lit22:
13329 case DW_OP_lit23:
13330 case DW_OP_lit24:
13331 case DW_OP_lit25:
13332 case DW_OP_lit26:
13333 case DW_OP_lit27:
13334 case DW_OP_lit28:
13335 case DW_OP_lit29:
13336 case DW_OP_lit30:
13337 case DW_OP_lit31:
13338 stack[++stacki] = op - DW_OP_lit0;
13339 break;
13340
c906108c
SS
13341 case DW_OP_reg0:
13342 case DW_OP_reg1:
13343 case DW_OP_reg2:
13344 case DW_OP_reg3:
13345 case DW_OP_reg4:
13346 case DW_OP_reg5:
13347 case DW_OP_reg6:
13348 case DW_OP_reg7:
13349 case DW_OP_reg8:
13350 case DW_OP_reg9:
13351 case DW_OP_reg10:
13352 case DW_OP_reg11:
13353 case DW_OP_reg12:
13354 case DW_OP_reg13:
13355 case DW_OP_reg14:
13356 case DW_OP_reg15:
13357 case DW_OP_reg16:
13358 case DW_OP_reg17:
13359 case DW_OP_reg18:
13360 case DW_OP_reg19:
13361 case DW_OP_reg20:
13362 case DW_OP_reg21:
13363 case DW_OP_reg22:
13364 case DW_OP_reg23:
13365 case DW_OP_reg24:
13366 case DW_OP_reg25:
13367 case DW_OP_reg26:
13368 case DW_OP_reg27:
13369 case DW_OP_reg28:
13370 case DW_OP_reg29:
13371 case DW_OP_reg30:
13372 case DW_OP_reg31:
c906108c 13373 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13374 if (i < size)
13375 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13376 break;
13377
13378 case DW_OP_regx:
c906108c
SS
13379 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13380 i += bytes_read;
c906108c 13381 stack[++stacki] = unsnd;
4cecd739
DJ
13382 if (i < size)
13383 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13384 break;
13385
13386 case DW_OP_addr:
107d2387 13387 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13388 cu, &bytes_read);
107d2387 13389 i += bytes_read;
c906108c
SS
13390 break;
13391
13392 case DW_OP_const1u:
13393 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13394 i += 1;
13395 break;
13396
13397 case DW_OP_const1s:
13398 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13399 i += 1;
13400 break;
13401
13402 case DW_OP_const2u:
13403 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13404 i += 2;
13405 break;
13406
13407 case DW_OP_const2s:
13408 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13409 i += 2;
13410 break;
13411
13412 case DW_OP_const4u:
13413 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13414 i += 4;
13415 break;
13416
13417 case DW_OP_const4s:
13418 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13419 i += 4;
13420 break;
13421
13422 case DW_OP_constu:
13423 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13424 &bytes_read);
c906108c
SS
13425 i += bytes_read;
13426 break;
13427
13428 case DW_OP_consts:
13429 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13430 i += bytes_read;
13431 break;
13432
f1bea926
JM
13433 case DW_OP_dup:
13434 stack[stacki + 1] = stack[stacki];
13435 stacki++;
13436 break;
13437
c906108c
SS
13438 case DW_OP_plus:
13439 stack[stacki - 1] += stack[stacki];
13440 stacki--;
13441 break;
13442
13443 case DW_OP_plus_uconst:
13444 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13445 i += bytes_read;
13446 break;
13447
13448 case DW_OP_minus:
f1bea926 13449 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13450 stacki--;
13451 break;
13452
7a292a7a 13453 case DW_OP_deref:
7a292a7a 13454 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13455 this using GDB's address_class enum. This is valid for partial
13456 global symbols, although the variable's address will be bogus
13457 in the psymtab. */
7a292a7a 13458 if (i < size)
4d3c2250 13459 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13460 break;
13461
9d774e44 13462 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13463 /* The top of the stack has the offset from the beginning
13464 of the thread control block at which the variable is located. */
13465 /* Nothing should follow this operator, so the top of stack would
13466 be returned. */
4cecd739
DJ
13467 /* This is valid for partial global symbols, but the variable's
13468 address will be bogus in the psymtab. */
9d774e44 13469 if (i < size)
4d3c2250 13470 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13471 break;
13472
42be36b3
CT
13473 case DW_OP_GNU_uninit:
13474 break;
13475
c906108c 13476 default:
e2e0b3e5 13477 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13478 dwarf_stack_op_name (op, 1));
c906108c
SS
13479 return (stack[stacki]);
13480 }
13481 }
13482 return (stack[stacki]);
13483}
13484
13485/* memory allocation interface */
13486
c906108c 13487static struct dwarf_block *
7b5a2f43 13488dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13489{
13490 struct dwarf_block *blk;
13491
13492 blk = (struct dwarf_block *)
7b5a2f43 13493 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13494 return (blk);
13495}
13496
13497static struct abbrev_info *
f3dd6933 13498dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13499{
13500 struct abbrev_info *abbrev;
13501
f3dd6933
DJ
13502 abbrev = (struct abbrev_info *)
13503 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13504 memset (abbrev, 0, sizeof (struct abbrev_info));
13505 return (abbrev);
13506}
13507
13508static struct die_info *
b60c80d6 13509dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13510{
13511 struct die_info *die;
b60c80d6
DJ
13512 size_t size = sizeof (struct die_info);
13513
13514 if (num_attrs > 1)
13515 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13516
b60c80d6 13517 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13518 memset (die, 0, sizeof (struct die_info));
13519 return (die);
13520}
2e276125
JB
13521
13522\f
13523/* Macro support. */
13524
13525
13526/* Return the full name of file number I in *LH's file name table.
13527 Use COMP_DIR as the name of the current directory of the
13528 compilation. The result is allocated using xmalloc; the caller is
13529 responsible for freeing it. */
13530static char *
13531file_full_name (int file, struct line_header *lh, const char *comp_dir)
13532{
6a83a1e6
EZ
13533 /* Is the file number a valid index into the line header's file name
13534 table? Remember that file numbers start with one, not zero. */
13535 if (1 <= file && file <= lh->num_file_names)
13536 {
13537 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13538
6a83a1e6
EZ
13539 if (IS_ABSOLUTE_PATH (fe->name))
13540 return xstrdup (fe->name);
13541 else
13542 {
13543 const char *dir;
13544 int dir_len;
13545 char *full_name;
13546
13547 if (fe->dir_index)
13548 dir = lh->include_dirs[fe->dir_index - 1];
13549 else
13550 dir = comp_dir;
13551
13552 if (dir)
13553 {
13554 dir_len = strlen (dir);
13555 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13556 strcpy (full_name, dir);
13557 full_name[dir_len] = '/';
13558 strcpy (full_name + dir_len + 1, fe->name);
13559 return full_name;
13560 }
13561 else
13562 return xstrdup (fe->name);
13563 }
13564 }
2e276125
JB
13565 else
13566 {
6a83a1e6
EZ
13567 /* The compiler produced a bogus file number. We can at least
13568 record the macro definitions made in the file, even if we
13569 won't be able to find the file by name. */
13570 char fake_name[80];
9a619af0 13571
6a83a1e6 13572 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13573
6e70227d 13574 complaint (&symfile_complaints,
6a83a1e6
EZ
13575 _("bad file number in macro information (%d)"),
13576 file);
2e276125 13577
6a83a1e6 13578 return xstrdup (fake_name);
2e276125
JB
13579 }
13580}
13581
13582
13583static struct macro_source_file *
13584macro_start_file (int file, int line,
13585 struct macro_source_file *current_file,
13586 const char *comp_dir,
13587 struct line_header *lh, struct objfile *objfile)
13588{
13589 /* The full name of this source file. */
13590 char *full_name = file_full_name (file, lh, comp_dir);
13591
13592 /* We don't create a macro table for this compilation unit
13593 at all until we actually get a filename. */
13594 if (! pending_macros)
4a146b47 13595 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13596 objfile->macro_cache);
2e276125
JB
13597
13598 if (! current_file)
13599 /* If we have no current file, then this must be the start_file
13600 directive for the compilation unit's main source file. */
13601 current_file = macro_set_main (pending_macros, full_name);
13602 else
13603 current_file = macro_include (current_file, line, full_name);
13604
13605 xfree (full_name);
6e70227d 13606
2e276125
JB
13607 return current_file;
13608}
13609
13610
13611/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13612 followed by a null byte. */
13613static char *
13614copy_string (const char *buf, int len)
13615{
13616 char *s = xmalloc (len + 1);
9a619af0 13617
2e276125
JB
13618 memcpy (s, buf, len);
13619 s[len] = '\0';
2e276125
JB
13620 return s;
13621}
13622
13623
13624static const char *
13625consume_improper_spaces (const char *p, const char *body)
13626{
13627 if (*p == ' ')
13628 {
4d3c2250 13629 complaint (&symfile_complaints,
e2e0b3e5 13630 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13631 body);
2e276125
JB
13632
13633 while (*p == ' ')
13634 p++;
13635 }
13636
13637 return p;
13638}
13639
13640
13641static void
13642parse_macro_definition (struct macro_source_file *file, int line,
13643 const char *body)
13644{
13645 const char *p;
13646
13647 /* The body string takes one of two forms. For object-like macro
13648 definitions, it should be:
13649
13650 <macro name> " " <definition>
13651
13652 For function-like macro definitions, it should be:
13653
13654 <macro name> "() " <definition>
13655 or
13656 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13657
13658 Spaces may appear only where explicitly indicated, and in the
13659 <definition>.
13660
13661 The Dwarf 2 spec says that an object-like macro's name is always
13662 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13663 the space when the macro's definition is the empty string.
2e276125
JB
13664
13665 The Dwarf 2 spec says that there should be no spaces between the
13666 formal arguments in a function-like macro's formal argument list,
13667 but versions of GCC around March 2002 include spaces after the
13668 commas. */
13669
13670
13671 /* Find the extent of the macro name. The macro name is terminated
13672 by either a space or null character (for an object-like macro) or
13673 an opening paren (for a function-like macro). */
13674 for (p = body; *p; p++)
13675 if (*p == ' ' || *p == '(')
13676 break;
13677
13678 if (*p == ' ' || *p == '\0')
13679 {
13680 /* It's an object-like macro. */
13681 int name_len = p - body;
13682 char *name = copy_string (body, name_len);
13683 const char *replacement;
13684
13685 if (*p == ' ')
13686 replacement = body + name_len + 1;
13687 else
13688 {
4d3c2250 13689 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13690 replacement = body + name_len;
13691 }
6e70227d 13692
2e276125
JB
13693 macro_define_object (file, line, name, replacement);
13694
13695 xfree (name);
13696 }
13697 else if (*p == '(')
13698 {
13699 /* It's a function-like macro. */
13700 char *name = copy_string (body, p - body);
13701 int argc = 0;
13702 int argv_size = 1;
13703 char **argv = xmalloc (argv_size * sizeof (*argv));
13704
13705 p++;
13706
13707 p = consume_improper_spaces (p, body);
13708
13709 /* Parse the formal argument list. */
13710 while (*p && *p != ')')
13711 {
13712 /* Find the extent of the current argument name. */
13713 const char *arg_start = p;
13714
13715 while (*p && *p != ',' && *p != ')' && *p != ' ')
13716 p++;
13717
13718 if (! *p || p == arg_start)
4d3c2250 13719 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13720 else
13721 {
13722 /* Make sure argv has room for the new argument. */
13723 if (argc >= argv_size)
13724 {
13725 argv_size *= 2;
13726 argv = xrealloc (argv, argv_size * sizeof (*argv));
13727 }
13728
13729 argv[argc++] = copy_string (arg_start, p - arg_start);
13730 }
13731
13732 p = consume_improper_spaces (p, body);
13733
13734 /* Consume the comma, if present. */
13735 if (*p == ',')
13736 {
13737 p++;
13738
13739 p = consume_improper_spaces (p, body);
13740 }
13741 }
13742
13743 if (*p == ')')
13744 {
13745 p++;
13746
13747 if (*p == ' ')
13748 /* Perfectly formed definition, no complaints. */
13749 macro_define_function (file, line, name,
6e70227d 13750 argc, (const char **) argv,
2e276125
JB
13751 p + 1);
13752 else if (*p == '\0')
13753 {
13754 /* Complain, but do define it. */
4d3c2250 13755 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13756 macro_define_function (file, line, name,
6e70227d 13757 argc, (const char **) argv,
2e276125
JB
13758 p);
13759 }
13760 else
13761 /* Just complain. */
4d3c2250 13762 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13763 }
13764 else
13765 /* Just complain. */
4d3c2250 13766 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13767
13768 xfree (name);
13769 {
13770 int i;
13771
13772 for (i = 0; i < argc; i++)
13773 xfree (argv[i]);
13774 }
13775 xfree (argv);
13776 }
13777 else
4d3c2250 13778 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13779}
13780
13781
13782static void
13783dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13784 char *comp_dir, bfd *abfd,
e7c27a73 13785 struct dwarf2_cu *cu)
2e276125 13786{
fe1b8b76 13787 gdb_byte *mac_ptr, *mac_end;
2e276125 13788 struct macro_source_file *current_file = 0;
757a13d0
JK
13789 enum dwarf_macinfo_record_type macinfo_type;
13790 int at_commandline;
2e276125 13791
be391dca
TT
13792 dwarf2_read_section (dwarf2_per_objfile->objfile,
13793 &dwarf2_per_objfile->macinfo);
dce234bc 13794 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13795 {
e2e0b3e5 13796 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13797 return;
13798 }
13799
757a13d0
JK
13800 /* First pass: Find the name of the base filename.
13801 This filename is needed in order to process all macros whose definition
13802 (or undefinition) comes from the command line. These macros are defined
13803 before the first DW_MACINFO_start_file entry, and yet still need to be
13804 associated to the base file.
13805
13806 To determine the base file name, we scan the macro definitions until we
13807 reach the first DW_MACINFO_start_file entry. We then initialize
13808 CURRENT_FILE accordingly so that any macro definition found before the
13809 first DW_MACINFO_start_file can still be associated to the base file. */
13810
dce234bc
PP
13811 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13812 mac_end = dwarf2_per_objfile->macinfo.buffer
13813 + dwarf2_per_objfile->macinfo.size;
2e276125 13814
757a13d0 13815 do
2e276125 13816 {
2e276125
JB
13817 /* Do we at least have room for a macinfo type byte? */
13818 if (mac_ptr >= mac_end)
13819 {
757a13d0
JK
13820 /* Complaint is printed during the second pass as GDB will probably
13821 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13822 break;
2e276125
JB
13823 }
13824
13825 macinfo_type = read_1_byte (abfd, mac_ptr);
13826 mac_ptr++;
13827
13828 switch (macinfo_type)
13829 {
13830 /* A zero macinfo type indicates the end of the macro
13831 information. */
13832 case 0:
757a13d0
JK
13833 break;
13834
13835 case DW_MACINFO_define:
13836 case DW_MACINFO_undef:
13837 /* Only skip the data by MAC_PTR. */
13838 {
13839 unsigned int bytes_read;
13840
13841 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13842 mac_ptr += bytes_read;
9b1c24c8 13843 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13844 mac_ptr += bytes_read;
13845 }
13846 break;
13847
13848 case DW_MACINFO_start_file:
13849 {
13850 unsigned int bytes_read;
13851 int line, file;
13852
13853 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13854 mac_ptr += bytes_read;
13855 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13856 mac_ptr += bytes_read;
13857
13858 current_file = macro_start_file (file, line, current_file, comp_dir,
13859 lh, cu->objfile);
13860 }
13861 break;
13862
13863 case DW_MACINFO_end_file:
13864 /* No data to skip by MAC_PTR. */
13865 break;
13866
13867 case DW_MACINFO_vendor_ext:
13868 /* Only skip the data by MAC_PTR. */
13869 {
13870 unsigned int bytes_read;
13871
13872 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13873 mac_ptr += bytes_read;
9b1c24c8 13874 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13875 mac_ptr += bytes_read;
13876 }
13877 break;
13878
13879 default:
13880 break;
13881 }
13882 } while (macinfo_type != 0 && current_file == NULL);
13883
13884 /* Second pass: Process all entries.
13885
13886 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13887 command-line macro definitions/undefinitions. This flag is unset when we
13888 reach the first DW_MACINFO_start_file entry. */
13889
dce234bc 13890 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13891
13892 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13893 GDB is still reading the definitions from command line. First
13894 DW_MACINFO_start_file will need to be ignored as it was already executed
13895 to create CURRENT_FILE for the main source holding also the command line
13896 definitions. On first met DW_MACINFO_start_file this flag is reset to
13897 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13898
13899 at_commandline = 1;
13900
13901 do
13902 {
13903 /* Do we at least have room for a macinfo type byte? */
13904 if (mac_ptr >= mac_end)
13905 {
13906 dwarf2_macros_too_long_complaint ();
13907 break;
13908 }
13909
13910 macinfo_type = read_1_byte (abfd, mac_ptr);
13911 mac_ptr++;
13912
13913 switch (macinfo_type)
13914 {
13915 /* A zero macinfo type indicates the end of the macro
13916 information. */
13917 case 0:
13918 break;
2e276125
JB
13919
13920 case DW_MACINFO_define:
13921 case DW_MACINFO_undef:
13922 {
891d2f0b 13923 unsigned int bytes_read;
2e276125
JB
13924 int line;
13925 char *body;
13926
13927 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13928 mac_ptr += bytes_read;
9b1c24c8 13929 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13930 mac_ptr += bytes_read;
13931
13932 if (! current_file)
757a13d0
JK
13933 {
13934 /* DWARF violation as no main source is present. */
13935 complaint (&symfile_complaints,
13936 _("debug info with no main source gives macro %s "
13937 "on line %d: %s"),
6e70227d
DE
13938 macinfo_type == DW_MACINFO_define ?
13939 _("definition") :
905e0470
PM
13940 macinfo_type == DW_MACINFO_undef ?
13941 _("undefinition") :
13942 _("something-or-other"), line, body);
757a13d0
JK
13943 break;
13944 }
13945 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13946 complaint (&symfile_complaints,
757a13d0
JK
13947 _("debug info gives %s macro %s with %s line %d: %s"),
13948 at_commandline ? _("command-line") : _("in-file"),
905e0470 13949 macinfo_type == DW_MACINFO_define ?
6e70227d 13950 _("definition") :
905e0470
PM
13951 macinfo_type == DW_MACINFO_undef ?
13952 _("undefinition") :
13953 _("something-or-other"),
757a13d0
JK
13954 line == 0 ? _("zero") : _("non-zero"), line, body);
13955
13956 if (macinfo_type == DW_MACINFO_define)
13957 parse_macro_definition (current_file, line, body);
13958 else if (macinfo_type == DW_MACINFO_undef)
13959 macro_undef (current_file, line, body);
2e276125
JB
13960 }
13961 break;
13962
13963 case DW_MACINFO_start_file:
13964 {
891d2f0b 13965 unsigned int bytes_read;
2e276125
JB
13966 int line, file;
13967
13968 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13969 mac_ptr += bytes_read;
13970 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13971 mac_ptr += bytes_read;
13972
757a13d0
JK
13973 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13974 complaint (&symfile_complaints,
13975 _("debug info gives source %d included "
13976 "from %s at %s line %d"),
13977 file, at_commandline ? _("command-line") : _("file"),
13978 line == 0 ? _("zero") : _("non-zero"), line);
13979
13980 if (at_commandline)
13981 {
13982 /* This DW_MACINFO_start_file was executed in the pass one. */
13983 at_commandline = 0;
13984 }
13985 else
13986 current_file = macro_start_file (file, line,
13987 current_file, comp_dir,
13988 lh, cu->objfile);
2e276125
JB
13989 }
13990 break;
13991
13992 case DW_MACINFO_end_file:
13993 if (! current_file)
4d3c2250 13994 complaint (&symfile_complaints,
e2e0b3e5 13995 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13996 else
13997 {
13998 current_file = current_file->included_by;
13999 if (! current_file)
14000 {
14001 enum dwarf_macinfo_record_type next_type;
14002
14003 /* GCC circa March 2002 doesn't produce the zero
14004 type byte marking the end of the compilation
14005 unit. Complain if it's not there, but exit no
14006 matter what. */
14007
14008 /* Do we at least have room for a macinfo type byte? */
14009 if (mac_ptr >= mac_end)
14010 {
4d3c2250 14011 dwarf2_macros_too_long_complaint ();
2e276125
JB
14012 return;
14013 }
14014
14015 /* We don't increment mac_ptr here, so this is just
14016 a look-ahead. */
14017 next_type = read_1_byte (abfd, mac_ptr);
14018 if (next_type != 0)
4d3c2250 14019 complaint (&symfile_complaints,
e2e0b3e5 14020 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
14021
14022 return;
14023 }
14024 }
14025 break;
14026
14027 case DW_MACINFO_vendor_ext:
14028 {
891d2f0b 14029 unsigned int bytes_read;
2e276125
JB
14030 int constant;
14031 char *string;
14032
14033 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14034 mac_ptr += bytes_read;
9b1c24c8 14035 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14036 mac_ptr += bytes_read;
14037
14038 /* We don't recognize any vendor extensions. */
14039 }
14040 break;
14041 }
757a13d0 14042 } while (macinfo_type != 0);
2e276125 14043}
8e19ed76
PS
14044
14045/* Check if the attribute's form is a DW_FORM_block*
14046 if so return true else false. */
14047static int
14048attr_form_is_block (struct attribute *attr)
14049{
14050 return (attr == NULL ? 0 :
14051 attr->form == DW_FORM_block1
14052 || attr->form == DW_FORM_block2
14053 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14054 || attr->form == DW_FORM_block
14055 || attr->form == DW_FORM_exprloc);
8e19ed76 14056}
4c2df51b 14057
c6a0999f
JB
14058/* Return non-zero if ATTR's value is a section offset --- classes
14059 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14060 You may use DW_UNSND (attr) to retrieve such offsets.
14061
14062 Section 7.5.4, "Attribute Encodings", explains that no attribute
14063 may have a value that belongs to more than one of these classes; it
14064 would be ambiguous if we did, because we use the same forms for all
14065 of them. */
3690dd37
JB
14066static int
14067attr_form_is_section_offset (struct attribute *attr)
14068{
14069 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14070 || attr->form == DW_FORM_data8
14071 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14072}
14073
14074
14075/* Return non-zero if ATTR's value falls in the 'constant' class, or
14076 zero otherwise. When this function returns true, you can apply
14077 dwarf2_get_attr_constant_value to it.
14078
14079 However, note that for some attributes you must check
14080 attr_form_is_section_offset before using this test. DW_FORM_data4
14081 and DW_FORM_data8 are members of both the constant class, and of
14082 the classes that contain offsets into other debug sections
14083 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14084 that, if an attribute's can be either a constant or one of the
14085 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14086 taken as section offsets, not constants. */
14087static int
14088attr_form_is_constant (struct attribute *attr)
14089{
14090 switch (attr->form)
14091 {
14092 case DW_FORM_sdata:
14093 case DW_FORM_udata:
14094 case DW_FORM_data1:
14095 case DW_FORM_data2:
14096 case DW_FORM_data4:
14097 case DW_FORM_data8:
14098 return 1;
14099 default:
14100 return 0;
14101 }
14102}
14103
4c2df51b
DJ
14104static void
14105dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14106 struct dwarf2_cu *cu)
4c2df51b 14107{
3690dd37 14108 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14109 /* ".debug_loc" may not exist at all, or the offset may be outside
14110 the section. If so, fall through to the complaint in the
14111 other branch. */
dce234bc 14112 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14113 {
0d53c4c4 14114 struct dwarf2_loclist_baton *baton;
4c2df51b 14115
4a146b47 14116 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14117 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
14118 baton->per_cu = cu->per_cu;
14119 gdb_assert (baton->per_cu);
4c2df51b 14120
be391dca
TT
14121 dwarf2_read_section (dwarf2_per_objfile->objfile,
14122 &dwarf2_per_objfile->loc);
14123
0d53c4c4
DJ
14124 /* We don't know how long the location list is, but make sure we
14125 don't run off the edge of the section. */
dce234bc
PP
14126 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14127 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
14128 baton->base_address = cu->base_address;
14129 if (cu->base_known == 0)
0d53c4c4 14130 complaint (&symfile_complaints,
e2e0b3e5 14131 _("Location list used without specifying the CU base address."));
4c2df51b 14132
768a979c 14133 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14134 SYMBOL_LOCATION_BATON (sym) = baton;
14135 }
14136 else
14137 {
14138 struct dwarf2_locexpr_baton *baton;
14139
4a146b47 14140 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14141 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14142 baton->per_cu = cu->per_cu;
14143 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14144
14145 if (attr_form_is_block (attr))
14146 {
14147 /* Note that we're just copying the block's data pointer
14148 here, not the actual data. We're still pointing into the
6502dd73
DJ
14149 info_buffer for SYM's objfile; right now we never release
14150 that buffer, but when we do clean up properly this may
14151 need to change. */
0d53c4c4
DJ
14152 baton->size = DW_BLOCK (attr)->size;
14153 baton->data = DW_BLOCK (attr)->data;
14154 }
14155 else
14156 {
14157 dwarf2_invalid_attrib_class_complaint ("location description",
14158 SYMBOL_NATURAL_NAME (sym));
14159 baton->size = 0;
14160 baton->data = NULL;
14161 }
6e70227d 14162
768a979c 14163 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14164 SYMBOL_LOCATION_BATON (sym) = baton;
14165 }
4c2df51b 14166}
6502dd73 14167
9aa1f1e3
TT
14168/* Return the OBJFILE associated with the compilation unit CU. If CU
14169 came from a separate debuginfo file, then the master objfile is
14170 returned. */
ae0d2f24
UW
14171
14172struct objfile *
14173dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14174{
9291a0cd 14175 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14176
14177 /* Return the master objfile, so that we can report and look up the
14178 correct file containing this variable. */
14179 if (objfile->separate_debug_objfile_backlink)
14180 objfile = objfile->separate_debug_objfile_backlink;
14181
14182 return objfile;
14183}
14184
14185/* Return the address size given in the compilation unit header for CU. */
14186
14187CORE_ADDR
14188dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14189{
14190 if (per_cu->cu)
14191 return per_cu->cu->header.addr_size;
14192 else
14193 {
14194 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14195 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14196 struct dwarf2_per_objfile *per_objfile
14197 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14198 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14199 struct comp_unit_head cu_header;
9a619af0 14200
ae0d2f24
UW
14201 memset (&cu_header, 0, sizeof cu_header);
14202 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14203 return cu_header.addr_size;
14204 }
14205}
14206
9eae7c52
TT
14207/* Return the offset size given in the compilation unit header for CU. */
14208
14209int
14210dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14211{
14212 if (per_cu->cu)
14213 return per_cu->cu->header.offset_size;
14214 else
14215 {
14216 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14217 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14218 struct dwarf2_per_objfile *per_objfile
14219 = objfile_data (objfile, dwarf2_objfile_data_key);
14220 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14221 struct comp_unit_head cu_header;
14222
14223 memset (&cu_header, 0, sizeof cu_header);
14224 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14225 return cu_header.offset_size;
14226 }
14227}
14228
9aa1f1e3
TT
14229/* Return the text offset of the CU. The returned offset comes from
14230 this CU's objfile. If this objfile came from a separate debuginfo
14231 file, then the offset may be different from the corresponding
14232 offset in the parent objfile. */
14233
14234CORE_ADDR
14235dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14236{
bb3fa9d0 14237 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14238
14239 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14240}
14241
348e048f
DE
14242/* Locate the .debug_info compilation unit from CU's objfile which contains
14243 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14244
14245static struct dwarf2_per_cu_data *
c764a876 14246dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14247 struct objfile *objfile)
14248{
14249 struct dwarf2_per_cu_data *this_cu;
14250 int low, high;
14251
ae038cb0
DJ
14252 low = 0;
14253 high = dwarf2_per_objfile->n_comp_units - 1;
14254 while (high > low)
14255 {
14256 int mid = low + (high - low) / 2;
9a619af0 14257
ae038cb0
DJ
14258 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14259 high = mid;
14260 else
14261 low = mid + 1;
14262 }
14263 gdb_assert (low == high);
14264 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14265 {
10b3939b 14266 if (low == 0)
8a3fe4f8
AC
14267 error (_("Dwarf Error: could not find partial DIE containing "
14268 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14269 (long) offset, bfd_get_filename (objfile->obfd));
14270
ae038cb0
DJ
14271 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14272 return dwarf2_per_objfile->all_comp_units[low-1];
14273 }
14274 else
14275 {
14276 this_cu = dwarf2_per_objfile->all_comp_units[low];
14277 if (low == dwarf2_per_objfile->n_comp_units - 1
14278 && offset >= this_cu->offset + this_cu->length)
c764a876 14279 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14280 gdb_assert (offset < this_cu->offset + this_cu->length);
14281 return this_cu;
14282 }
14283}
14284
10b3939b
DJ
14285/* Locate the compilation unit from OBJFILE which is located at exactly
14286 OFFSET. Raises an error on failure. */
14287
ae038cb0 14288static struct dwarf2_per_cu_data *
c764a876 14289dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14290{
14291 struct dwarf2_per_cu_data *this_cu;
9a619af0 14292
ae038cb0
DJ
14293 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14294 if (this_cu->offset != offset)
c764a876 14295 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14296 return this_cu;
14297}
14298
9816fde3 14299/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14300
9816fde3
JK
14301static void
14302init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14303{
9816fde3 14304 memset (cu, 0, sizeof (*cu));
93311388
DE
14305 cu->objfile = objfile;
14306 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14307}
14308
14309/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14310
14311static void
14312prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14313{
14314 struct attribute *attr;
14315
14316 /* Set the language we're debugging. */
14317 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14318 if (attr)
14319 set_cu_language (DW_UNSND (attr), cu);
14320 else
14321 set_cu_language (language_minimal, cu);
93311388
DE
14322}
14323
ae038cb0
DJ
14324/* Release one cached compilation unit, CU. We unlink it from the tree
14325 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14326 the caller is responsible for that.
14327 NOTE: DATA is a void * because this function is also used as a
14328 cleanup routine. */
ae038cb0
DJ
14329
14330static void
14331free_one_comp_unit (void *data)
14332{
14333 struct dwarf2_cu *cu = data;
14334
14335 if (cu->per_cu != NULL)
14336 cu->per_cu->cu = NULL;
14337 cu->per_cu = NULL;
14338
14339 obstack_free (&cu->comp_unit_obstack, NULL);
14340
14341 xfree (cu);
14342}
14343
72bf9492 14344/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14345 when we're finished with it. We can't free the pointer itself, but be
14346 sure to unlink it from the cache. Also release any associated storage
14347 and perform cache maintenance.
72bf9492
DJ
14348
14349 Only used during partial symbol parsing. */
14350
14351static void
14352free_stack_comp_unit (void *data)
14353{
14354 struct dwarf2_cu *cu = data;
14355
14356 obstack_free (&cu->comp_unit_obstack, NULL);
14357 cu->partial_dies = NULL;
ae038cb0
DJ
14358
14359 if (cu->per_cu != NULL)
14360 {
14361 /* This compilation unit is on the stack in our caller, so we
14362 should not xfree it. Just unlink it. */
14363 cu->per_cu->cu = NULL;
14364 cu->per_cu = NULL;
14365
14366 /* If we had a per-cu pointer, then we may have other compilation
14367 units loaded, so age them now. */
14368 age_cached_comp_units ();
14369 }
14370}
14371
14372/* Free all cached compilation units. */
14373
14374static void
14375free_cached_comp_units (void *data)
14376{
14377 struct dwarf2_per_cu_data *per_cu, **last_chain;
14378
14379 per_cu = dwarf2_per_objfile->read_in_chain;
14380 last_chain = &dwarf2_per_objfile->read_in_chain;
14381 while (per_cu != NULL)
14382 {
14383 struct dwarf2_per_cu_data *next_cu;
14384
14385 next_cu = per_cu->cu->read_in_chain;
14386
14387 free_one_comp_unit (per_cu->cu);
14388 *last_chain = next_cu;
14389
14390 per_cu = next_cu;
14391 }
14392}
14393
14394/* Increase the age counter on each cached compilation unit, and free
14395 any that are too old. */
14396
14397static void
14398age_cached_comp_units (void)
14399{
14400 struct dwarf2_per_cu_data *per_cu, **last_chain;
14401
14402 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14403 per_cu = dwarf2_per_objfile->read_in_chain;
14404 while (per_cu != NULL)
14405 {
14406 per_cu->cu->last_used ++;
14407 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14408 dwarf2_mark (per_cu->cu);
14409 per_cu = per_cu->cu->read_in_chain;
14410 }
14411
14412 per_cu = dwarf2_per_objfile->read_in_chain;
14413 last_chain = &dwarf2_per_objfile->read_in_chain;
14414 while (per_cu != NULL)
14415 {
14416 struct dwarf2_per_cu_data *next_cu;
14417
14418 next_cu = per_cu->cu->read_in_chain;
14419
14420 if (!per_cu->cu->mark)
14421 {
14422 free_one_comp_unit (per_cu->cu);
14423 *last_chain = next_cu;
14424 }
14425 else
14426 last_chain = &per_cu->cu->read_in_chain;
14427
14428 per_cu = next_cu;
14429 }
14430}
14431
14432/* Remove a single compilation unit from the cache. */
14433
14434static void
14435free_one_cached_comp_unit (void *target_cu)
14436{
14437 struct dwarf2_per_cu_data *per_cu, **last_chain;
14438
14439 per_cu = dwarf2_per_objfile->read_in_chain;
14440 last_chain = &dwarf2_per_objfile->read_in_chain;
14441 while (per_cu != NULL)
14442 {
14443 struct dwarf2_per_cu_data *next_cu;
14444
14445 next_cu = per_cu->cu->read_in_chain;
14446
14447 if (per_cu->cu == target_cu)
14448 {
14449 free_one_comp_unit (per_cu->cu);
14450 *last_chain = next_cu;
14451 break;
14452 }
14453 else
14454 last_chain = &per_cu->cu->read_in_chain;
14455
14456 per_cu = next_cu;
14457 }
14458}
14459
fe3e1990
DJ
14460/* Release all extra memory associated with OBJFILE. */
14461
14462void
14463dwarf2_free_objfile (struct objfile *objfile)
14464{
14465 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14466
14467 if (dwarf2_per_objfile == NULL)
14468 return;
14469
14470 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14471 free_cached_comp_units (NULL);
14472
9291a0cd
TT
14473 if (dwarf2_per_objfile->using_index)
14474 {
14475 int i;
14476
14477 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14478 {
14479 int j;
e254ef6a
DE
14480 struct dwarf2_per_cu_data *per_cu =
14481 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14482
e254ef6a 14483 if (!per_cu->v.quick->lines)
9291a0cd
TT
14484 continue;
14485
e254ef6a 14486 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14487 {
e254ef6a
DE
14488 if (per_cu->v.quick->file_names)
14489 xfree ((void *) per_cu->v.quick->file_names[j]);
14490 if (per_cu->v.quick->full_names)
14491 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14492 }
14493
e254ef6a 14494 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14495 }
14496 }
14497
fe3e1990
DJ
14498 /* Everything else should be on the objfile obstack. */
14499}
14500
1c379e20
DJ
14501/* A pair of DIE offset and GDB type pointer. We store these
14502 in a hash table separate from the DIEs, and preserve them
14503 when the DIEs are flushed out of cache. */
14504
14505struct dwarf2_offset_and_type
14506{
14507 unsigned int offset;
14508 struct type *type;
14509};
14510
14511/* Hash function for a dwarf2_offset_and_type. */
14512
14513static hashval_t
14514offset_and_type_hash (const void *item)
14515{
14516 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14517
1c379e20
DJ
14518 return ofs->offset;
14519}
14520
14521/* Equality function for a dwarf2_offset_and_type. */
14522
14523static int
14524offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14525{
14526 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14527 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14528
1c379e20
DJ
14529 return ofs_lhs->offset == ofs_rhs->offset;
14530}
14531
14532/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14533 table if necessary. For convenience, return TYPE.
14534
14535 The DIEs reading must have careful ordering to:
14536 * Not cause infite loops trying to read in DIEs as a prerequisite for
14537 reading current DIE.
14538 * Not trying to dereference contents of still incompletely read in types
14539 while reading in other DIEs.
14540 * Enable referencing still incompletely read in types just by a pointer to
14541 the type without accessing its fields.
14542
14543 Therefore caller should follow these rules:
14544 * Try to fetch any prerequisite types we may need to build this DIE type
14545 before building the type and calling set_die_type.
e71ec853 14546 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14547 possible before fetching more types to complete the current type.
14548 * Make the type as complete as possible before fetching more types. */
1c379e20 14549
f792889a 14550static struct type *
1c379e20
DJ
14551set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14552{
14553 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14554 struct objfile *objfile = cu->objfile;
14555 htab_t *type_hash_ptr;
1c379e20 14556
b4ba55a1
JB
14557 /* For Ada types, make sure that the gnat-specific data is always
14558 initialized (if not already set). There are a few types where
14559 we should not be doing so, because the type-specific area is
14560 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14561 where the type-specific area is used to store the floatformat).
14562 But this is not a problem, because the gnat-specific information
14563 is actually not needed for these types. */
14564 if (need_gnat_info (cu)
14565 && TYPE_CODE (type) != TYPE_CODE_FUNC
14566 && TYPE_CODE (type) != TYPE_CODE_FLT
14567 && !HAVE_GNAT_AUX_INFO (type))
14568 INIT_GNAT_SPECIFIC (type);
14569
673bfd45
DE
14570 if (cu->per_cu->from_debug_types)
14571 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14572 else
14573 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14574
14575 if (*type_hash_ptr == NULL)
f792889a 14576 {
673bfd45
DE
14577 *type_hash_ptr
14578 = htab_create_alloc_ex (127,
f792889a
DJ
14579 offset_and_type_hash,
14580 offset_and_type_eq,
14581 NULL,
673bfd45 14582 &objfile->objfile_obstack,
f792889a
DJ
14583 hashtab_obstack_allocate,
14584 dummy_obstack_deallocate);
f792889a 14585 }
1c379e20
DJ
14586
14587 ofs.offset = die->offset;
14588 ofs.type = type;
14589 slot = (struct dwarf2_offset_and_type **)
673bfd45 14590 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14591 if (*slot)
14592 complaint (&symfile_complaints,
14593 _("A problem internal to GDB: DIE 0x%x has type already set"),
14594 die->offset);
673bfd45 14595 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14596 **slot = ofs;
f792889a 14597 return type;
1c379e20
DJ
14598}
14599
673bfd45
DE
14600/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14601 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14602
14603static struct type *
673bfd45
DE
14604get_die_type_at_offset (unsigned int offset,
14605 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14606{
14607 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14608 htab_t type_hash;
f792889a 14609
673bfd45
DE
14610 if (per_cu->from_debug_types)
14611 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14612 else
14613 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14614 if (type_hash == NULL)
14615 return NULL;
1c379e20 14616
673bfd45 14617 ofs.offset = offset;
1c379e20
DJ
14618 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14619 if (slot)
14620 return slot->type;
14621 else
14622 return NULL;
14623}
14624
673bfd45
DE
14625/* Look up the type for DIE in the appropriate type_hash table,
14626 or return NULL if DIE does not have a saved type. */
14627
14628static struct type *
14629get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14630{
14631 return get_die_type_at_offset (die->offset, cu->per_cu);
14632}
14633
10b3939b
DJ
14634/* Add a dependence relationship from CU to REF_PER_CU. */
14635
14636static void
14637dwarf2_add_dependence (struct dwarf2_cu *cu,
14638 struct dwarf2_per_cu_data *ref_per_cu)
14639{
14640 void **slot;
14641
14642 if (cu->dependencies == NULL)
14643 cu->dependencies
14644 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14645 NULL, &cu->comp_unit_obstack,
14646 hashtab_obstack_allocate,
14647 dummy_obstack_deallocate);
14648
14649 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14650 if (*slot == NULL)
14651 *slot = ref_per_cu;
14652}
1c379e20 14653
f504f079
DE
14654/* Subroutine of dwarf2_mark to pass to htab_traverse.
14655 Set the mark field in every compilation unit in the
ae038cb0
DJ
14656 cache that we must keep because we are keeping CU. */
14657
10b3939b
DJ
14658static int
14659dwarf2_mark_helper (void **slot, void *data)
14660{
14661 struct dwarf2_per_cu_data *per_cu;
14662
14663 per_cu = (struct dwarf2_per_cu_data *) *slot;
14664 if (per_cu->cu->mark)
14665 return 1;
14666 per_cu->cu->mark = 1;
14667
14668 if (per_cu->cu->dependencies != NULL)
14669 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14670
14671 return 1;
14672}
14673
f504f079
DE
14674/* Set the mark field in CU and in every other compilation unit in the
14675 cache that we must keep because we are keeping CU. */
14676
ae038cb0
DJ
14677static void
14678dwarf2_mark (struct dwarf2_cu *cu)
14679{
14680 if (cu->mark)
14681 return;
14682 cu->mark = 1;
10b3939b
DJ
14683 if (cu->dependencies != NULL)
14684 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14685}
14686
14687static void
14688dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14689{
14690 while (per_cu)
14691 {
14692 per_cu->cu->mark = 0;
14693 per_cu = per_cu->cu->read_in_chain;
14694 }
72bf9492
DJ
14695}
14696
72bf9492
DJ
14697/* Trivial hash function for partial_die_info: the hash value of a DIE
14698 is its offset in .debug_info for this objfile. */
14699
14700static hashval_t
14701partial_die_hash (const void *item)
14702{
14703 const struct partial_die_info *part_die = item;
9a619af0 14704
72bf9492
DJ
14705 return part_die->offset;
14706}
14707
14708/* Trivial comparison function for partial_die_info structures: two DIEs
14709 are equal if they have the same offset. */
14710
14711static int
14712partial_die_eq (const void *item_lhs, const void *item_rhs)
14713{
14714 const struct partial_die_info *part_die_lhs = item_lhs;
14715 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14716
72bf9492
DJ
14717 return part_die_lhs->offset == part_die_rhs->offset;
14718}
14719
ae038cb0
DJ
14720static struct cmd_list_element *set_dwarf2_cmdlist;
14721static struct cmd_list_element *show_dwarf2_cmdlist;
14722
14723static void
14724set_dwarf2_cmd (char *args, int from_tty)
14725{
14726 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14727}
14728
14729static void
14730show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14731{
ae038cb0
DJ
14732 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14733}
14734
dce234bc
PP
14735/* If section described by INFO was mmapped, munmap it now. */
14736
14737static void
14738munmap_section_buffer (struct dwarf2_section_info *info)
14739{
14740 if (info->was_mmapped)
14741 {
14742#ifdef HAVE_MMAP
14743 intptr_t begin = (intptr_t) info->buffer;
14744 intptr_t map_begin = begin & ~(pagesize - 1);
14745 size_t map_length = info->size + begin - map_begin;
9a619af0 14746
dce234bc
PP
14747 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14748#else
14749 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14750 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14751#endif
14752 }
14753}
14754
14755/* munmap debug sections for OBJFILE, if necessary. */
14756
14757static void
c1bd65d0 14758dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14759{
14760 struct dwarf2_per_objfile *data = d;
9a619af0 14761
16be1145
DE
14762 /* This is sorted according to the order they're defined in to make it easier
14763 to keep in sync. */
dce234bc
PP
14764 munmap_section_buffer (&data->info);
14765 munmap_section_buffer (&data->abbrev);
14766 munmap_section_buffer (&data->line);
16be1145 14767 munmap_section_buffer (&data->loc);
dce234bc 14768 munmap_section_buffer (&data->macinfo);
16be1145 14769 munmap_section_buffer (&data->str);
dce234bc 14770 munmap_section_buffer (&data->ranges);
16be1145 14771 munmap_section_buffer (&data->types);
dce234bc
PP
14772 munmap_section_buffer (&data->frame);
14773 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14774 munmap_section_buffer (&data->gdb_index);
14775}
14776
14777\f
14778
14779/* The contents of the hash table we create when building the string
14780 table. */
14781struct strtab_entry
14782{
14783 offset_type offset;
14784 const char *str;
14785};
14786
14787/* Hash function for a strtab_entry. */
b89be57b 14788
9291a0cd
TT
14789static hashval_t
14790hash_strtab_entry (const void *e)
14791{
14792 const struct strtab_entry *entry = e;
14793 return mapped_index_string_hash (entry->str);
14794}
14795
14796/* Equality function for a strtab_entry. */
b89be57b 14797
9291a0cd
TT
14798static int
14799eq_strtab_entry (const void *a, const void *b)
14800{
14801 const struct strtab_entry *ea = a;
14802 const struct strtab_entry *eb = b;
14803 return !strcmp (ea->str, eb->str);
14804}
14805
14806/* Create a strtab_entry hash table. */
b89be57b 14807
9291a0cd
TT
14808static htab_t
14809create_strtab (void)
14810{
14811 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14812 xfree, xcalloc, xfree);
14813}
14814
14815/* Add a string to the constant pool. Return the string's offset in
14816 host order. */
b89be57b 14817
9291a0cd
TT
14818static offset_type
14819add_string (htab_t table, struct obstack *cpool, const char *str)
14820{
14821 void **slot;
14822 struct strtab_entry entry;
14823 struct strtab_entry *result;
14824
14825 entry.str = str;
14826 slot = htab_find_slot (table, &entry, INSERT);
14827 if (*slot)
14828 result = *slot;
14829 else
14830 {
14831 result = XNEW (struct strtab_entry);
14832 result->offset = obstack_object_size (cpool);
14833 result->str = str;
14834 obstack_grow_str0 (cpool, str);
14835 *slot = result;
14836 }
14837 return result->offset;
14838}
14839
14840/* An entry in the symbol table. */
14841struct symtab_index_entry
14842{
14843 /* The name of the symbol. */
14844 const char *name;
14845 /* The offset of the name in the constant pool. */
14846 offset_type index_offset;
14847 /* A sorted vector of the indices of all the CUs that hold an object
14848 of this name. */
14849 VEC (offset_type) *cu_indices;
14850};
14851
14852/* The symbol table. This is a power-of-2-sized hash table. */
14853struct mapped_symtab
14854{
14855 offset_type n_elements;
14856 offset_type size;
14857 struct symtab_index_entry **data;
14858};
14859
14860/* Hash function for a symtab_index_entry. */
b89be57b 14861
9291a0cd
TT
14862static hashval_t
14863hash_symtab_entry (const void *e)
14864{
14865 const struct symtab_index_entry *entry = e;
14866 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14867 sizeof (offset_type) * VEC_length (offset_type,
14868 entry->cu_indices),
14869 0);
14870}
14871
14872/* Equality function for a symtab_index_entry. */
b89be57b 14873
9291a0cd
TT
14874static int
14875eq_symtab_entry (const void *a, const void *b)
14876{
14877 const struct symtab_index_entry *ea = a;
14878 const struct symtab_index_entry *eb = b;
14879 int len = VEC_length (offset_type, ea->cu_indices);
14880 if (len != VEC_length (offset_type, eb->cu_indices))
14881 return 0;
14882 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14883 VEC_address (offset_type, eb->cu_indices),
14884 sizeof (offset_type) * len);
14885}
14886
14887/* Destroy a symtab_index_entry. */
b89be57b 14888
9291a0cd
TT
14889static void
14890delete_symtab_entry (void *p)
14891{
14892 struct symtab_index_entry *entry = p;
14893 VEC_free (offset_type, entry->cu_indices);
14894 xfree (entry);
14895}
14896
14897/* Create a hash table holding symtab_index_entry objects. */
b89be57b 14898
9291a0cd 14899static htab_t
3876f04e 14900create_symbol_hash_table (void)
9291a0cd
TT
14901{
14902 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14903 delete_symtab_entry, xcalloc, xfree);
14904}
14905
14906/* Create a new mapped symtab object. */
b89be57b 14907
9291a0cd
TT
14908static struct mapped_symtab *
14909create_mapped_symtab (void)
14910{
14911 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14912 symtab->n_elements = 0;
14913 symtab->size = 1024;
14914 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14915 return symtab;
14916}
14917
14918/* Destroy a mapped_symtab. */
b89be57b 14919
9291a0cd
TT
14920static void
14921cleanup_mapped_symtab (void *p)
14922{
14923 struct mapped_symtab *symtab = p;
14924 /* The contents of the array are freed when the other hash table is
14925 destroyed. */
14926 xfree (symtab->data);
14927 xfree (symtab);
14928}
14929
14930/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14931 the slot. */
b89be57b 14932
9291a0cd
TT
14933static struct symtab_index_entry **
14934find_slot (struct mapped_symtab *symtab, const char *name)
14935{
14936 offset_type index, step, hash = mapped_index_string_hash (name);
14937
14938 index = hash & (symtab->size - 1);
14939 step = ((hash * 17) & (symtab->size - 1)) | 1;
14940
14941 for (;;)
14942 {
14943 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14944 return &symtab->data[index];
14945 index = (index + step) & (symtab->size - 1);
14946 }
14947}
14948
14949/* Expand SYMTAB's hash table. */
b89be57b 14950
9291a0cd
TT
14951static void
14952hash_expand (struct mapped_symtab *symtab)
14953{
14954 offset_type old_size = symtab->size;
14955 offset_type i;
14956 struct symtab_index_entry **old_entries = symtab->data;
14957
14958 symtab->size *= 2;
14959 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14960
14961 for (i = 0; i < old_size; ++i)
14962 {
14963 if (old_entries[i])
14964 {
14965 struct symtab_index_entry **slot = find_slot (symtab,
14966 old_entries[i]->name);
14967 *slot = old_entries[i];
14968 }
14969 }
14970
14971 xfree (old_entries);
14972}
14973
14974/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14975 is the index of the CU in which the symbol appears. */
b89be57b 14976
9291a0cd
TT
14977static void
14978add_index_entry (struct mapped_symtab *symtab, const char *name,
14979 offset_type cu_index)
14980{
14981 struct symtab_index_entry **slot;
14982
14983 ++symtab->n_elements;
14984 if (4 * symtab->n_elements / 3 >= symtab->size)
14985 hash_expand (symtab);
14986
14987 slot = find_slot (symtab, name);
14988 if (!*slot)
14989 {
14990 *slot = XNEW (struct symtab_index_entry);
14991 (*slot)->name = name;
14992 (*slot)->cu_indices = NULL;
14993 }
14994 /* Don't push an index twice. Due to how we add entries we only
14995 have to check the last one. */
14996 if (VEC_empty (offset_type, (*slot)->cu_indices)
14997 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14998 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14999}
15000
15001/* Add a vector of indices to the constant pool. */
b89be57b 15002
9291a0cd 15003static offset_type
3876f04e 15004add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15005 struct symtab_index_entry *entry)
15006{
15007 void **slot;
15008
3876f04e 15009 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15010 if (!*slot)
15011 {
15012 offset_type len = VEC_length (offset_type, entry->cu_indices);
15013 offset_type val = MAYBE_SWAP (len);
15014 offset_type iter;
15015 int i;
15016
15017 *slot = entry;
15018 entry->index_offset = obstack_object_size (cpool);
15019
15020 obstack_grow (cpool, &val, sizeof (val));
15021 for (i = 0;
15022 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15023 ++i)
15024 {
15025 val = MAYBE_SWAP (iter);
15026 obstack_grow (cpool, &val, sizeof (val));
15027 }
15028 }
15029 else
15030 {
15031 struct symtab_index_entry *old_entry = *slot;
15032 entry->index_offset = old_entry->index_offset;
15033 entry = old_entry;
15034 }
15035 return entry->index_offset;
15036}
15037
15038/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15039 constant pool entries going into the obstack CPOOL. */
b89be57b 15040
9291a0cd
TT
15041static void
15042write_hash_table (struct mapped_symtab *symtab,
15043 struct obstack *output, struct obstack *cpool)
15044{
15045 offset_type i;
3876f04e 15046 htab_t symbol_hash_table;
9291a0cd
TT
15047 htab_t str_table;
15048
3876f04e 15049 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15050 str_table = create_strtab ();
3876f04e 15051
9291a0cd
TT
15052 /* We add all the index vectors to the constant pool first, to
15053 ensure alignment is ok. */
15054 for (i = 0; i < symtab->size; ++i)
15055 {
15056 if (symtab->data[i])
3876f04e 15057 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15058 }
15059
15060 /* Now write out the hash table. */
15061 for (i = 0; i < symtab->size; ++i)
15062 {
15063 offset_type str_off, vec_off;
15064
15065 if (symtab->data[i])
15066 {
15067 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15068 vec_off = symtab->data[i]->index_offset;
15069 }
15070 else
15071 {
15072 /* While 0 is a valid constant pool index, it is not valid
15073 to have 0 for both offsets. */
15074 str_off = 0;
15075 vec_off = 0;
15076 }
15077
15078 str_off = MAYBE_SWAP (str_off);
15079 vec_off = MAYBE_SWAP (vec_off);
15080
15081 obstack_grow (output, &str_off, sizeof (str_off));
15082 obstack_grow (output, &vec_off, sizeof (vec_off));
15083 }
15084
15085 htab_delete (str_table);
3876f04e 15086 htab_delete (symbol_hash_table);
9291a0cd
TT
15087}
15088
15089/* Write an address entry to ADDR_OBSTACK. The addresses are taken
15090 from PST; CU_INDEX is the index of the CU in the vector of all
15091 CUs. */
b89be57b 15092
9291a0cd
TT
15093static void
15094add_address_entry (struct objfile *objfile,
15095 struct obstack *addr_obstack, struct partial_symtab *pst,
15096 unsigned int cu_index)
15097{
15098 offset_type offset;
15099 char addr[8];
15100 CORE_ADDR baseaddr;
15101
1fd400ff
TT
15102 /* Don't bother recording empty ranges. */
15103 if (pst->textlow == pst->texthigh)
15104 return;
15105
9291a0cd
TT
15106 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15107
15108 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15109 obstack_grow (addr_obstack, addr, 8);
15110 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15111 obstack_grow (addr_obstack, addr, 8);
15112 offset = MAYBE_SWAP (cu_index);
15113 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15114}
15115
15116/* Add a list of partial symbols to SYMTAB. */
b89be57b 15117
9291a0cd
TT
15118static void
15119write_psymbols (struct mapped_symtab *symtab,
987d643c 15120 htab_t psyms_seen,
9291a0cd
TT
15121 struct partial_symbol **psymp,
15122 int count,
987d643c
TT
15123 offset_type cu_index,
15124 int is_static)
9291a0cd
TT
15125{
15126 for (; count-- > 0; ++psymp)
15127 {
987d643c
TT
15128 void **slot, *lookup;
15129
9291a0cd
TT
15130 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15131 error (_("Ada is not currently supported by the index"));
987d643c
TT
15132
15133 /* We only want to add a given psymbol once. However, we also
15134 want to account for whether it is global or static. So, we
15135 may add it twice, using slightly different values. */
15136 if (is_static)
15137 {
15138 uintptr_t val = 1 | (uintptr_t) *psymp;
15139
15140 lookup = (void *) val;
15141 }
15142 else
15143 lookup = *psymp;
15144
15145 /* Only add a given psymbol once. */
15146 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15147 if (!*slot)
15148 {
15149 *slot = lookup;
15150 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15151 }
9291a0cd
TT
15152 }
15153}
15154
15155/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15156 exception if there is an error. */
b89be57b 15157
9291a0cd
TT
15158static void
15159write_obstack (FILE *file, struct obstack *obstack)
15160{
15161 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15162 file)
15163 != obstack_object_size (obstack))
15164 error (_("couldn't data write to file"));
15165}
15166
15167/* Unlink a file if the argument is not NULL. */
b89be57b 15168
9291a0cd
TT
15169static void
15170unlink_if_set (void *p)
15171{
15172 char **filename = p;
15173 if (*filename)
15174 unlink (*filename);
15175}
15176
1fd400ff
TT
15177/* A helper struct used when iterating over debug_types. */
15178struct signatured_type_index_data
15179{
15180 struct objfile *objfile;
15181 struct mapped_symtab *symtab;
15182 struct obstack *types_list;
987d643c 15183 htab_t psyms_seen;
1fd400ff
TT
15184 int cu_index;
15185};
15186
15187/* A helper function that writes a single signatured_type to an
15188 obstack. */
b89be57b 15189
1fd400ff
TT
15190static int
15191write_one_signatured_type (void **slot, void *d)
15192{
15193 struct signatured_type_index_data *info = d;
15194 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15195 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15196 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15197 gdb_byte val[8];
15198
15199 write_psymbols (info->symtab,
987d643c 15200 info->psyms_seen,
1fd400ff 15201 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15202 psymtab->n_global_syms, info->cu_index,
15203 0);
1fd400ff 15204 write_psymbols (info->symtab,
987d643c 15205 info->psyms_seen,
1fd400ff 15206 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15207 psymtab->n_static_syms, info->cu_index,
15208 1);
1fd400ff
TT
15209
15210 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15211 obstack_grow (info->types_list, val, 8);
15212 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15213 obstack_grow (info->types_list, val, 8);
15214 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15215 obstack_grow (info->types_list, val, 8);
15216
15217 ++info->cu_index;
15218
15219 return 1;
15220}
15221
987d643c
TT
15222/* A cleanup function for an htab_t. */
15223
15224static void
15225cleanup_htab (void *arg)
15226{
15227 htab_delete (arg);
15228}
15229
9291a0cd 15230/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15231
9291a0cd
TT
15232static void
15233write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15234{
15235 struct cleanup *cleanup;
15236 char *filename, *cleanup_filename;
1fd400ff
TT
15237 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15238 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15239 int i;
15240 FILE *out_file;
15241 struct mapped_symtab *symtab;
15242 offset_type val, size_of_contents, total_len;
15243 struct stat st;
15244 char buf[8];
987d643c 15245 htab_t psyms_seen;
9291a0cd
TT
15246
15247 if (!objfile->psymtabs)
15248 return;
15249 if (dwarf2_per_objfile->using_index)
15250 error (_("Cannot use an index to create the index"));
15251
15252 if (stat (objfile->name, &st) < 0)
15253 perror_with_name (_("Could not stat"));
15254
15255 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15256 INDEX_SUFFIX, (char *) NULL);
15257 cleanup = make_cleanup (xfree, filename);
15258
15259 out_file = fopen (filename, "wb");
15260 if (!out_file)
15261 error (_("Can't open `%s' for writing"), filename);
15262
15263 cleanup_filename = filename;
15264 make_cleanup (unlink_if_set, &cleanup_filename);
15265
15266 symtab = create_mapped_symtab ();
15267 make_cleanup (cleanup_mapped_symtab, symtab);
15268
15269 obstack_init (&addr_obstack);
15270 make_cleanup_obstack_free (&addr_obstack);
15271
15272 obstack_init (&cu_list);
15273 make_cleanup_obstack_free (&cu_list);
15274
1fd400ff
TT
15275 obstack_init (&types_cu_list);
15276 make_cleanup_obstack_free (&types_cu_list);
15277
987d643c
TT
15278 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15279 NULL, xcalloc, xfree);
15280 make_cleanup (cleanup_htab, psyms_seen);
15281
1fd400ff
TT
15282 /* The list is already sorted, so we don't need to do additional
15283 work here. Also, the debug_types entries do not appear in
15284 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15285 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15286 {
e254ef6a
DE
15287 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15288 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15289 gdb_byte val[8];
15290
15291 write_psymbols (symtab,
987d643c 15292 psyms_seen,
9291a0cd 15293 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15294 psymtab->n_global_syms, i,
15295 0);
9291a0cd 15296 write_psymbols (symtab,
987d643c 15297 psyms_seen,
9291a0cd 15298 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15299 psymtab->n_static_syms, i,
15300 1);
9291a0cd
TT
15301
15302 add_address_entry (objfile, &addr_obstack, psymtab, i);
15303
e254ef6a 15304 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15305 obstack_grow (&cu_list, val, 8);
e254ef6a 15306 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15307 obstack_grow (&cu_list, val, 8);
15308 }
15309
1fd400ff
TT
15310 /* Write out the .debug_type entries, if any. */
15311 if (dwarf2_per_objfile->signatured_types)
15312 {
15313 struct signatured_type_index_data sig_data;
15314
15315 sig_data.objfile = objfile;
15316 sig_data.symtab = symtab;
15317 sig_data.types_list = &types_cu_list;
987d643c 15318 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15319 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15320 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15321 write_one_signatured_type, &sig_data);
15322 }
15323
9291a0cd
TT
15324 obstack_init (&constant_pool);
15325 make_cleanup_obstack_free (&constant_pool);
15326 obstack_init (&symtab_obstack);
15327 make_cleanup_obstack_free (&symtab_obstack);
15328 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15329
15330 obstack_init (&contents);
15331 make_cleanup_obstack_free (&contents);
1fd400ff 15332 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15333 total_len = size_of_contents;
15334
15335 /* The version number. */
987d643c 15336 val = MAYBE_SWAP (3);
9291a0cd
TT
15337 obstack_grow (&contents, &val, sizeof (val));
15338
15339 /* The offset of the CU list from the start of the file. */
15340 val = MAYBE_SWAP (total_len);
15341 obstack_grow (&contents, &val, sizeof (val));
15342 total_len += obstack_object_size (&cu_list);
15343
1fd400ff
TT
15344 /* The offset of the types CU list from the start of the file. */
15345 val = MAYBE_SWAP (total_len);
15346 obstack_grow (&contents, &val, sizeof (val));
15347 total_len += obstack_object_size (&types_cu_list);
15348
9291a0cd
TT
15349 /* The offset of the address table from the start of the file. */
15350 val = MAYBE_SWAP (total_len);
15351 obstack_grow (&contents, &val, sizeof (val));
15352 total_len += obstack_object_size (&addr_obstack);
15353
15354 /* The offset of the symbol table from the start of the file. */
15355 val = MAYBE_SWAP (total_len);
15356 obstack_grow (&contents, &val, sizeof (val));
15357 total_len += obstack_object_size (&symtab_obstack);
15358
15359 /* The offset of the constant pool from the start of the file. */
15360 val = MAYBE_SWAP (total_len);
15361 obstack_grow (&contents, &val, sizeof (val));
15362 total_len += obstack_object_size (&constant_pool);
15363
15364 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15365
15366 write_obstack (out_file, &contents);
15367 write_obstack (out_file, &cu_list);
1fd400ff 15368 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15369 write_obstack (out_file, &addr_obstack);
15370 write_obstack (out_file, &symtab_obstack);
15371 write_obstack (out_file, &constant_pool);
15372
15373 fclose (out_file);
15374
15375 /* We want to keep the file, so we set cleanup_filename to NULL
15376 here. See unlink_if_set. */
15377 cleanup_filename = NULL;
15378
15379 do_cleanups (cleanup);
15380}
15381
15382/* The mapped index file format is designed to be directly mmap()able
15383 on any architecture. In most cases, a datum is represented using a
15384 little-endian 32-bit integer value, called an offset_type. Big
15385 endian machines must byte-swap the values before using them.
15386 Exceptions to this rule are noted. The data is laid out such that
15387 alignment is always respected.
15388
15389 A mapped index consists of several sections.
15390
15391 1. The file header. This is a sequence of values, of offset_type
15392 unless otherwise noted:
987d643c
TT
15393
15394 [0] The version number, currently 3. Versions 1 and 2 are
15395 obsolete.
9291a0cd 15396 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15397 [2] The offset, from the start of the file, of the types CU list.
15398 Note that this section can be empty, in which case this offset will
15399 be equal to the next offset.
15400 [3] The offset, from the start of the file, of the address section.
15401 [4] The offset, from the start of the file, of the symbol table.
15402 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15403
15404 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15405 little-endian values, sorted by the CU offset. The first element
15406 in each pair is the offset of a CU in the .debug_info section. The
15407 second element in each pair is the length of that CU. References
15408 to a CU elsewhere in the map are done using a CU index, which is
15409 just the 0-based index into this table. Note that if there are
15410 type CUs, then conceptually CUs and type CUs form a single list for
15411 the purposes of CU indices.
15412
987d643c
TT
15413 3. The types CU list. This is a sequence of triplets of 64-bit
15414 little-endian values. In a triplet, the first value is the CU
15415 offset, the second value is the type offset in the CU, and the
15416 third value is the type signature. The types CU list is not
15417 sorted.
9291a0cd 15418
987d643c 15419 4. The address section. The address section consists of a sequence
9291a0cd
TT
15420 of address entries. Each address entry has three elements.
15421 [0] The low address. This is a 64-bit little-endian value.
15422 [1] The high address. This is a 64-bit little-endian value.
15423 [2] The CU index. This is an offset_type value.
15424
987d643c 15425 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15426 table is always a power of 2. The initial hash and the step are
15427 currently defined by the `find_slot' function.
15428
15429 Each slot in the hash table consists of a pair of offset_type
15430 values. The first value is the offset of the symbol's name in the
15431 constant pool. The second value is the offset of the CU vector in
15432 the constant pool.
15433
15434 If both values are 0, then this slot in the hash table is empty.
15435 This is ok because while 0 is a valid constant pool index, it
15436 cannot be a valid index for both a string and a CU vector.
15437
15438 A string in the constant pool is stored as a \0-terminated string,
15439 as you'd expect.
15440
15441 A CU vector in the constant pool is a sequence of offset_type
15442 values. The first value is the number of CU indices in the vector.
15443 Each subsequent value is the index of a CU in the CU list. This
15444 element in the hash table is used to indicate which CUs define the
15445 symbol.
15446
987d643c 15447 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15448 organized so that alignment is correct: CU vectors are stored
15449 first, followed by strings. */
11570e71 15450
9291a0cd
TT
15451static void
15452save_gdb_index_command (char *arg, int from_tty)
15453{
15454 struct objfile *objfile;
15455
15456 if (!arg || !*arg)
96d19272 15457 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15458
15459 ALL_OBJFILES (objfile)
15460 {
15461 struct stat st;
15462
15463 /* If the objfile does not correspond to an actual file, skip it. */
15464 if (stat (objfile->name, &st) < 0)
15465 continue;
15466
15467 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15468 if (dwarf2_per_objfile)
15469 {
15470 volatile struct gdb_exception except;
15471
15472 TRY_CATCH (except, RETURN_MASK_ERROR)
15473 {
15474 write_psymtabs_to_index (objfile, arg);
15475 }
15476 if (except.reason < 0)
15477 exception_fprintf (gdb_stderr, except,
15478 _("Error while writing index for `%s': "),
15479 objfile->name);
15480 }
15481 }
dce234bc
PP
15482}
15483
9291a0cd
TT
15484\f
15485
9eae7c52
TT
15486int dwarf2_always_disassemble;
15487
15488static void
15489show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15490 struct cmd_list_element *c, const char *value)
15491{
15492 fprintf_filtered (file, _("\
15493Whether to always disassemble DWARF expressions is %s.\n"),
15494 value);
15495}
15496
6502dd73
DJ
15497void _initialize_dwarf2_read (void);
15498
15499void
15500_initialize_dwarf2_read (void)
15501{
96d19272
JK
15502 struct cmd_list_element *c;
15503
dce234bc 15504 dwarf2_objfile_data_key
c1bd65d0 15505 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15506
1bedd215
AC
15507 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15508Set DWARF 2 specific variables.\n\
15509Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15510 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15511 0/*allow-unknown*/, &maintenance_set_cmdlist);
15512
1bedd215
AC
15513 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15514Show DWARF 2 specific variables\n\
15515Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15516 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15517 0/*allow-unknown*/, &maintenance_show_cmdlist);
15518
15519 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15520 &dwarf2_max_cache_age, _("\
15521Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15522Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15523A higher limit means that cached compilation units will be stored\n\
15524in memory longer, and more total memory will be used. Zero disables\n\
15525caching, which can slow down startup."),
2c5b56ce 15526 NULL,
920d2a44 15527 show_dwarf2_max_cache_age,
2c5b56ce 15528 &set_dwarf2_cmdlist,
ae038cb0 15529 &show_dwarf2_cmdlist);
d97bc12b 15530
9eae7c52
TT
15531 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15532 &dwarf2_always_disassemble, _("\
15533Set whether `info address' always disassembles DWARF expressions."), _("\
15534Show whether `info address' always disassembles DWARF expressions."), _("\
15535When enabled, DWARF expressions are always printed in an assembly-like\n\
15536syntax. When disabled, expressions will be printed in a more\n\
15537conversational style, when possible."),
15538 NULL,
15539 show_dwarf2_always_disassemble,
15540 &set_dwarf2_cmdlist,
15541 &show_dwarf2_cmdlist);
15542
d97bc12b
DE
15543 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15544Set debugging of the dwarf2 DIE reader."), _("\
15545Show debugging of the dwarf2 DIE reader."), _("\
15546When enabled (non-zero), DIEs are dumped after they are read in.\n\
15547The value is the maximum depth to print."),
15548 NULL,
15549 NULL,
15550 &setdebuglist, &showdebuglist);
9291a0cd 15551
96d19272 15552 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71
DE
15553 _("\
15554Save a .gdb-index file.\n\
15555Usage: save gdb-index DIRECTORY"),
96d19272
JK
15556 &save_cmdlist);
15557 set_cmd_completer (c, filename_completer);
6502dd73 15558}
This page took 1.999601 seconds and 4 git commands to generate.