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 1943 return 0;
594e8718
JK
1944 /* Indexes with higher version than the one supported by GDB may be no
1945 longer backward compatible. */
1946 if (version > 3)
1947 return 0;
9291a0cd
TT
1948
1949 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1950 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1951
1952 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1953
1954 i = 0;
1955 cu_list = addr + MAYBE_SWAP (metadata[i]);
1956 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1957 / 8);
1fd400ff
TT
1958 ++i;
1959
987d643c
TT
1960 types_list = addr + MAYBE_SWAP (metadata[i]);
1961 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1962 - MAYBE_SWAP (metadata[i]))
1963 / 8);
1964 ++i;
1fd400ff
TT
1965
1966 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1967 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1968 - MAYBE_SWAP (metadata[i]));
1969 ++i;
1970
3876f04e
DE
1971 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1972 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1973 - MAYBE_SWAP (metadata[i]))
1974 / (2 * sizeof (offset_type)));
1fd400ff 1975 ++i;
9291a0cd 1976
1fd400ff
TT
1977 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1978
1979 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1980 return 0;
1981
987d643c 1982 if (types_list_elements
673bfd45
DE
1983 && !create_signatured_type_table_from_index (objfile, types_list,
1984 types_list_elements))
9291a0cd
TT
1985 return 0;
1986
1987 create_addrmap_from_index (objfile, map);
1988
1989 dwarf2_per_objfile->index_table = map;
1990 dwarf2_per_objfile->using_index = 1;
1991
1992 return 1;
1993}
1994
1995/* A helper for the "quick" functions which sets the global
1996 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 1997
9291a0cd
TT
1998static void
1999dw2_setup (struct objfile *objfile)
2000{
2001 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2002 gdb_assert (dwarf2_per_objfile);
2003}
2004
2005/* A helper for the "quick" functions which attempts to read the line
2006 table for THIS_CU. */
2fdf6df6 2007
9291a0cd
TT
2008static void
2009dw2_require_line_header (struct objfile *objfile,
2010 struct dwarf2_per_cu_data *this_cu)
2011{
2012 bfd *abfd = objfile->obfd;
2013 struct line_header *lh = NULL;
2014 struct attribute *attr;
2015 struct cleanup *cleanups;
2016 struct die_info *comp_unit_die;
36374493 2017 struct dwarf2_section_info* sec;
9291a0cd
TT
2018 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2019 int has_children, i;
2020 struct dwarf2_cu cu;
2021 unsigned int bytes_read, buffer_size;
2022 struct die_reader_specs reader_specs;
2023 char *name, *comp_dir;
2024
2025 if (this_cu->v.quick->read_lines)
2026 return;
2027 this_cu->v.quick->read_lines = 1;
2028
9816fde3 2029 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2030 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2031
36374493
DE
2032 if (this_cu->from_debug_types)
2033 sec = &dwarf2_per_objfile->types;
2034 else
2035 sec = &dwarf2_per_objfile->info;
2036 dwarf2_read_section (objfile, sec);
2037 buffer_size = sec->size;
2038 buffer = sec->buffer;
9291a0cd
TT
2039 info_ptr = buffer + this_cu->offset;
2040 beg_of_comp_unit = info_ptr;
2041
2042 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2043 buffer, buffer_size,
2044 abfd);
2045
2046 /* Complete the cu_header. */
2047 cu.header.offset = beg_of_comp_unit - buffer;
2048 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2049
2050 this_cu->cu = &cu;
2051 cu.per_cu = this_cu;
2052
2053 dwarf2_read_abbrevs (abfd, &cu);
2054 make_cleanup (dwarf2_free_abbrev_table, &cu);
2055
2056 if (this_cu->from_debug_types)
2057 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2058 init_cu_die_reader (&reader_specs, &cu);
2059 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2060 &has_children);
2061
2062 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2063 if (attr)
2064 {
2065 unsigned int line_offset = DW_UNSND (attr);
2066 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2067 }
2068 if (lh == NULL)
2069 {
2070 do_cleanups (cleanups);
2071 return;
2072 }
2073
2074 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2075
2076 this_cu->v.quick->lines = lh;
2077
2078 this_cu->v.quick->file_names
2079 = obstack_alloc (&objfile->objfile_obstack,
2080 lh->num_file_names * sizeof (char *));
2081 for (i = 0; i < lh->num_file_names; ++i)
2082 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2083
2084 do_cleanups (cleanups);
2085}
2086
2087/* A helper for the "quick" functions which computes and caches the
2088 real path for a given file name from the line table.
2089 dw2_require_line_header must have been called before this is
2090 invoked. */
2fdf6df6 2091
9291a0cd
TT
2092static const char *
2093dw2_require_full_path (struct objfile *objfile,
e254ef6a 2094 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2095 int index)
2096{
e254ef6a
DE
2097 if (!per_cu->v.quick->full_names)
2098 per_cu->v.quick->full_names
9291a0cd 2099 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2100 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2101 sizeof (char *));
2102
e254ef6a
DE
2103 if (!per_cu->v.quick->full_names[index])
2104 per_cu->v.quick->full_names[index]
2105 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2106
e254ef6a 2107 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2108}
2109
2110static struct symtab *
2111dw2_find_last_source_symtab (struct objfile *objfile)
2112{
2113 int index;
2114 dw2_setup (objfile);
2115 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2116 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2117}
2118
2119static void
2120dw2_forget_cached_source_info (struct objfile *objfile)
2121{
2122 int i;
2123
2124 dw2_setup (objfile);
1fd400ff
TT
2125 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2126 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2127 {
e254ef6a 2128 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2129
e254ef6a 2130 if (per_cu->v.quick->full_names)
9291a0cd
TT
2131 {
2132 int j;
2133
e254ef6a 2134 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
87df528e
JK
2135 {
2136 xfree ((void *) per_cu->v.quick->full_names[j]);
2137 per_cu->v.quick->full_names[j] = NULL;
2138 }
9291a0cd
TT
2139 }
2140 }
2141}
2142
2143static int
2144dw2_lookup_symtab (struct objfile *objfile, const char *name,
2145 const char *full_path, const char *real_path,
2146 struct symtab **result)
2147{
2148 int i;
2149 int check_basename = lbasename (name) == name;
2150 struct dwarf2_per_cu_data *base_cu = NULL;
2151
2152 dw2_setup (objfile);
1fd400ff
TT
2153 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2154 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2155 {
2156 int j;
e254ef6a 2157 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2158
e254ef6a 2159 if (per_cu->v.quick->symtab)
9291a0cd
TT
2160 continue;
2161
e254ef6a
DE
2162 dw2_require_line_header (objfile, per_cu);
2163 if (!per_cu->v.quick->lines)
9291a0cd
TT
2164 continue;
2165
e254ef6a 2166 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2167 {
e254ef6a 2168 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2169
2170 if (FILENAME_CMP (name, this_name) == 0)
2171 {
e254ef6a 2172 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2173 return 1;
2174 }
2175
2176 if (check_basename && ! base_cu
2177 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2178 base_cu = per_cu;
9291a0cd
TT
2179
2180 if (full_path != NULL)
2181 {
2182 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2183 per_cu, j);
9291a0cd
TT
2184
2185 if (this_full_name
2186 && FILENAME_CMP (full_path, this_full_name) == 0)
2187 {
e254ef6a 2188 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2189 return 1;
2190 }
2191 }
2192
2193 if (real_path != NULL)
2194 {
2195 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2196 per_cu, j);
9291a0cd
TT
2197
2198 if (this_full_name != NULL)
2199 {
2200 char *rp = gdb_realpath (this_full_name);
2201 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2202 {
2203 xfree (rp);
e254ef6a 2204 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2205 return 1;
2206 }
2207 xfree (rp);
2208 }
2209 }
2210 }
2211 }
2212
2213 if (base_cu)
2214 {
2215 *result = dw2_instantiate_symtab (objfile, base_cu);
2216 return 1;
2217 }
2218
2219 return 0;
2220}
2221
2222static struct symtab *
2223dw2_lookup_symbol (struct objfile *objfile, int block_index,
2224 const char *name, domain_enum domain)
2225{
774b6a14 2226 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2227 instead. */
2228 return NULL;
2229}
2230
2231/* A helper function that expands all symtabs that hold an object
2232 named NAME. */
2fdf6df6 2233
9291a0cd
TT
2234static void
2235dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2236{
2237 dw2_setup (objfile);
2238
2239 if (dwarf2_per_objfile->index_table)
2240 {
2241 offset_type *vec;
2242
2243 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2244 name, &vec))
2245 {
2246 offset_type i, len = MAYBE_SWAP (*vec);
2247 for (i = 0; i < len; ++i)
2248 {
2249 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2250 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2251
e254ef6a 2252 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2253 }
2254 }
2255 }
2256}
2257
774b6a14
TT
2258static void
2259dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2260 int kind, const char *name,
2261 domain_enum domain)
9291a0cd 2262{
774b6a14 2263 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2264}
2265
2266static void
2267dw2_print_stats (struct objfile *objfile)
2268{
2269 int i, count;
2270
2271 dw2_setup (objfile);
2272 count = 0;
1fd400ff
TT
2273 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2274 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2275 {
e254ef6a 2276 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2277
e254ef6a 2278 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2279 ++count;
2280 }
2281 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2282}
2283
2284static void
2285dw2_dump (struct objfile *objfile)
2286{
2287 /* Nothing worth printing. */
2288}
2289
2290static void
2291dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2292 struct section_offsets *delta)
2293{
2294 /* There's nothing to relocate here. */
2295}
2296
2297static void
2298dw2_expand_symtabs_for_function (struct objfile *objfile,
2299 const char *func_name)
2300{
2301 dw2_do_expand_symtabs_matching (objfile, func_name);
2302}
2303
2304static void
2305dw2_expand_all_symtabs (struct objfile *objfile)
2306{
2307 int i;
2308
2309 dw2_setup (objfile);
1fd400ff
TT
2310
2311 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2312 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2313 {
e254ef6a 2314 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2315
e254ef6a 2316 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2317 }
2318}
2319
2320static void
2321dw2_expand_symtabs_with_filename (struct objfile *objfile,
2322 const char *filename)
2323{
2324 int i;
2325
2326 dw2_setup (objfile);
1fd400ff
TT
2327 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2328 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2329 {
2330 int j;
e254ef6a 2331 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2332
e254ef6a 2333 if (per_cu->v.quick->symtab)
9291a0cd
TT
2334 continue;
2335
e254ef6a
DE
2336 dw2_require_line_header (objfile, per_cu);
2337 if (!per_cu->v.quick->lines)
9291a0cd
TT
2338 continue;
2339
e254ef6a 2340 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2341 {
e254ef6a 2342 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2343 if (strcmp (this_name, filename) == 0)
2344 {
e254ef6a 2345 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2346 break;
2347 }
2348 }
2349 }
2350}
2351
dd786858 2352static const char *
9291a0cd
TT
2353dw2_find_symbol_file (struct objfile *objfile, const char *name)
2354{
e254ef6a 2355 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2356 offset_type *vec;
2357
2358 dw2_setup (objfile);
2359
2360 if (!dwarf2_per_objfile->index_table)
2361 return NULL;
2362
2363 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2364 name, &vec))
2365 return NULL;
2366
2367 /* Note that this just looks at the very first one named NAME -- but
2368 actually we are looking for a function. find_main_filename
2369 should be rewritten so that it doesn't require a custom hook. It
2370 could just use the ordinary symbol tables. */
2371 /* vec[0] is the length, which must always be >0. */
e254ef6a 2372 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2373
e254ef6a
DE
2374 dw2_require_line_header (objfile, per_cu);
2375 if (!per_cu->v.quick->lines)
9291a0cd
TT
2376 return NULL;
2377
e254ef6a 2378 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2379}
2380
2381static void
40658b94
PH
2382dw2_map_matching_symbols (const char * name, domain_enum namespace,
2383 struct objfile *objfile, int global,
2384 int (*callback) (struct block *,
2385 struct symbol *, void *),
2edb89d3
JK
2386 void *data, symbol_compare_ftype *match,
2387 symbol_compare_ftype *ordered_compare)
9291a0cd 2388{
40658b94 2389 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2390 current language is Ada for a non-Ada objfile using GNU index. As Ada
2391 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2392}
2393
2394static void
2395dw2_expand_symtabs_matching (struct objfile *objfile,
2396 int (*file_matcher) (const char *, void *),
2397 int (*name_matcher) (const char *, void *),
2398 domain_enum kind,
2399 void *data)
2400{
2401 int i;
2402 offset_type iter;
4b5246aa 2403 struct mapped_index *index;
9291a0cd
TT
2404
2405 dw2_setup (objfile);
2406 if (!dwarf2_per_objfile->index_table)
2407 return;
4b5246aa 2408 index = dwarf2_per_objfile->index_table;
9291a0cd 2409
1fd400ff
TT
2410 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2411 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2412 {
2413 int j;
e254ef6a 2414 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2415
e254ef6a
DE
2416 per_cu->v.quick->mark = 0;
2417 if (per_cu->v.quick->symtab)
9291a0cd
TT
2418 continue;
2419
e254ef6a
DE
2420 dw2_require_line_header (objfile, per_cu);
2421 if (!per_cu->v.quick->lines)
9291a0cd
TT
2422 continue;
2423
e254ef6a 2424 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2425 {
e254ef6a 2426 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2427 {
e254ef6a 2428 per_cu->v.quick->mark = 1;
9291a0cd
TT
2429 break;
2430 }
2431 }
2432 }
2433
3876f04e 2434 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2435 {
2436 offset_type idx = 2 * iter;
2437 const char *name;
2438 offset_type *vec, vec_len, vec_idx;
2439
3876f04e 2440 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2441 continue;
2442
3876f04e 2443 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2444
2445 if (! (*name_matcher) (name, data))
2446 continue;
2447
2448 /* The name was matched, now expand corresponding CUs that were
2449 marked. */
4b5246aa 2450 vec = (offset_type *) (index->constant_pool
3876f04e 2451 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2452 vec_len = MAYBE_SWAP (vec[0]);
2453 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2454 {
e254ef6a 2455 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2456
e254ef6a
DE
2457 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2458 if (per_cu->v.quick->mark)
2459 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2460 }
2461 }
2462}
2463
2464static struct symtab *
2465dw2_find_pc_sect_symtab (struct objfile *objfile,
2466 struct minimal_symbol *msymbol,
2467 CORE_ADDR pc,
2468 struct obj_section *section,
2469 int warn_if_readin)
2470{
2471 struct dwarf2_per_cu_data *data;
2472
2473 dw2_setup (objfile);
2474
2475 if (!objfile->psymtabs_addrmap)
2476 return NULL;
2477
2478 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2479 if (!data)
2480 return NULL;
2481
2482 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2483 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2484 paddress (get_objfile_arch (objfile), pc));
2485
2486 return dw2_instantiate_symtab (objfile, data);
2487}
2488
2489static void
2490dw2_map_symbol_names (struct objfile *objfile,
2491 void (*fun) (const char *, void *),
2492 void *data)
2493{
2494 offset_type iter;
4b5246aa
TT
2495 struct mapped_index *index;
2496
9291a0cd
TT
2497 dw2_setup (objfile);
2498
2499 if (!dwarf2_per_objfile->index_table)
2500 return;
4b5246aa 2501 index = dwarf2_per_objfile->index_table;
9291a0cd 2502
3876f04e 2503 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2504 {
2505 offset_type idx = 2 * iter;
2506 const char *name;
2507 offset_type *vec, vec_len, vec_idx;
2508
3876f04e 2509 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2510 continue;
2511
3876f04e 2512 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2513
2514 (*fun) (name, data);
2515 }
2516}
2517
2518static void
2519dw2_map_symbol_filenames (struct objfile *objfile,
2520 void (*fun) (const char *, const char *, void *),
2521 void *data)
2522{
2523 int i;
2524
2525 dw2_setup (objfile);
1fd400ff
TT
2526 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2527 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2528 {
2529 int j;
e254ef6a 2530 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2531
e254ef6a 2532 if (per_cu->v.quick->symtab)
9291a0cd
TT
2533 continue;
2534
e254ef6a
DE
2535 dw2_require_line_header (objfile, per_cu);
2536 if (!per_cu->v.quick->lines)
9291a0cd
TT
2537 continue;
2538
e254ef6a 2539 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2540 {
e254ef6a
DE
2541 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2542 j);
2543 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2544 }
2545 }
2546}
2547
2548static int
2549dw2_has_symbols (struct objfile *objfile)
2550{
2551 return 1;
2552}
2553
2554const struct quick_symbol_functions dwarf2_gdb_index_functions =
2555{
2556 dw2_has_symbols,
2557 dw2_find_last_source_symtab,
2558 dw2_forget_cached_source_info,
2559 dw2_lookup_symtab,
2560 dw2_lookup_symbol,
774b6a14 2561 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2562 dw2_print_stats,
2563 dw2_dump,
2564 dw2_relocate,
2565 dw2_expand_symtabs_for_function,
2566 dw2_expand_all_symtabs,
2567 dw2_expand_symtabs_with_filename,
2568 dw2_find_symbol_file,
40658b94 2569 dw2_map_matching_symbols,
9291a0cd
TT
2570 dw2_expand_symtabs_matching,
2571 dw2_find_pc_sect_symtab,
2572 dw2_map_symbol_names,
2573 dw2_map_symbol_filenames
2574};
2575
2576/* Initialize for reading DWARF for this objfile. Return 0 if this
2577 file will use psymtabs, or 1 if using the GNU index. */
2578
2579int
2580dwarf2_initialize_objfile (struct objfile *objfile)
2581{
2582 /* If we're about to read full symbols, don't bother with the
2583 indices. In this case we also don't care if some other debug
2584 format is making psymtabs, because they are all about to be
2585 expanded anyway. */
2586 if ((objfile->flags & OBJF_READNOW))
2587 {
2588 int i;
2589
2590 dwarf2_per_objfile->using_index = 1;
2591 create_all_comp_units (objfile);
1fd400ff 2592 create_debug_types_hash_table (objfile);
9291a0cd 2593
1fd400ff
TT
2594 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2595 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2596 {
e254ef6a 2597 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2598
e254ef6a
DE
2599 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2600 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2601 }
2602
2603 /* Return 1 so that gdb sees the "quick" functions. However,
2604 these functions will be no-ops because we will have expanded
2605 all symtabs. */
2606 return 1;
2607 }
2608
2609 if (dwarf2_read_index (objfile))
2610 return 1;
2611
2612 dwarf2_build_psymtabs (objfile);
2613 return 0;
2614}
2615
2616\f
2617
dce234bc
PP
2618/* Build a partial symbol table. */
2619
2620void
f29dff0a 2621dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2622{
f29dff0a 2623 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2624 {
2625 init_psymbol_list (objfile, 1024);
2626 }
2627
d146bf1e 2628 dwarf2_build_psymtabs_hard (objfile);
c906108c 2629}
c906108c 2630
45452591
DE
2631/* Return TRUE if OFFSET is within CU_HEADER. */
2632
2633static inline int
2634offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2635{
2636 unsigned int bottom = cu_header->offset;
2637 unsigned int top = (cu_header->offset
2638 + cu_header->length
2639 + cu_header->initial_length_size);
9a619af0 2640
45452591
DE
2641 return (offset >= bottom && offset < top);
2642}
2643
93311388
DE
2644/* Read in the comp unit header information from the debug_info at info_ptr.
2645 NOTE: This leaves members offset, first_die_offset to be filled in
2646 by the caller. */
107d2387 2647
fe1b8b76 2648static gdb_byte *
107d2387 2649read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2650 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2651{
2652 int signed_addr;
891d2f0b 2653 unsigned int bytes_read;
c764a876
DE
2654
2655 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2656 cu_header->initial_length_size = bytes_read;
2657 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2658 info_ptr += bytes_read;
107d2387
AC
2659 cu_header->version = read_2_bytes (abfd, info_ptr);
2660 info_ptr += 2;
613e1657 2661 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2662 &bytes_read);
613e1657 2663 info_ptr += bytes_read;
107d2387
AC
2664 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2665 info_ptr += 1;
2666 signed_addr = bfd_get_sign_extend_vma (abfd);
2667 if (signed_addr < 0)
8e65ff28 2668 internal_error (__FILE__, __LINE__,
e2e0b3e5 2669 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2670 cu_header->signed_addr_p = signed_addr;
c764a876 2671
107d2387
AC
2672 return info_ptr;
2673}
2674
fe1b8b76
JB
2675static gdb_byte *
2676partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2677 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2678 bfd *abfd)
2679{
fe1b8b76 2680 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2681
2682 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2683
2dc7f7b3 2684 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2685 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2686 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2687 bfd_get_filename (abfd));
72bf9492 2688
dce234bc 2689 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2690 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2691 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2692 (long) header->abbrev_offset,
93311388 2693 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2694 bfd_get_filename (abfd));
2695
2696 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2697 > buffer + buffer_size)
8a3fe4f8
AC
2698 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2699 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2700 (long) header->length,
93311388 2701 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2702 bfd_get_filename (abfd));
2703
2704 return info_ptr;
2705}
2706
348e048f
DE
2707/* Read in the types comp unit header information from .debug_types entry at
2708 types_ptr. The result is a pointer to one past the end of the header. */
2709
2710static gdb_byte *
2711read_type_comp_unit_head (struct comp_unit_head *cu_header,
2712 ULONGEST *signature,
2713 gdb_byte *types_ptr, bfd *abfd)
2714{
348e048f
DE
2715 gdb_byte *initial_types_ptr = types_ptr;
2716
6e70227d 2717 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2718 &dwarf2_per_objfile->types);
348e048f
DE
2719 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2720
2721 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2722
2723 *signature = read_8_bytes (abfd, types_ptr);
2724 types_ptr += 8;
2725 types_ptr += cu_header->offset_size;
2726 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2727
2728 return types_ptr;
2729}
2730
aaa75496
JB
2731/* Allocate a new partial symtab for file named NAME and mark this new
2732 partial symtab as being an include of PST. */
2733
2734static void
2735dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2736 struct objfile *objfile)
2737{
2738 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2739
2740 subpst->section_offsets = pst->section_offsets;
2741 subpst->textlow = 0;
2742 subpst->texthigh = 0;
2743
2744 subpst->dependencies = (struct partial_symtab **)
2745 obstack_alloc (&objfile->objfile_obstack,
2746 sizeof (struct partial_symtab *));
2747 subpst->dependencies[0] = pst;
2748 subpst->number_of_dependencies = 1;
2749
2750 subpst->globals_offset = 0;
2751 subpst->n_global_syms = 0;
2752 subpst->statics_offset = 0;
2753 subpst->n_static_syms = 0;
2754 subpst->symtab = NULL;
2755 subpst->read_symtab = pst->read_symtab;
2756 subpst->readin = 0;
2757
2758 /* No private part is necessary for include psymtabs. This property
2759 can be used to differentiate between such include psymtabs and
10b3939b 2760 the regular ones. */
58a9656e 2761 subpst->read_symtab_private = NULL;
aaa75496
JB
2762}
2763
2764/* Read the Line Number Program data and extract the list of files
2765 included by the source file represented by PST. Build an include
d85a05f0 2766 partial symtab for each of these included files. */
aaa75496
JB
2767
2768static void
2769dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2770 struct die_info *die,
aaa75496
JB
2771 struct partial_symtab *pst)
2772{
2773 struct objfile *objfile = cu->objfile;
2774 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2775 struct line_header *lh = NULL;
2776 struct attribute *attr;
aaa75496 2777
d85a05f0
DJ
2778 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2779 if (attr)
2780 {
2781 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2782
d85a05f0
DJ
2783 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2784 }
aaa75496
JB
2785 if (lh == NULL)
2786 return; /* No linetable, so no includes. */
2787
c6da4cef
DE
2788 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2789 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2790
2791 free_line_header (lh);
2792}
2793
348e048f
DE
2794static hashval_t
2795hash_type_signature (const void *item)
2796{
2797 const struct signatured_type *type_sig = item;
9a619af0 2798
348e048f
DE
2799 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2800 return type_sig->signature;
2801}
2802
2803static int
2804eq_type_signature (const void *item_lhs, const void *item_rhs)
2805{
2806 const struct signatured_type *lhs = item_lhs;
2807 const struct signatured_type *rhs = item_rhs;
9a619af0 2808
348e048f
DE
2809 return lhs->signature == rhs->signature;
2810}
2811
1fd400ff
TT
2812/* Allocate a hash table for signatured types. */
2813
2814static htab_t
673bfd45 2815allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2816{
2817 return htab_create_alloc_ex (41,
2818 hash_type_signature,
2819 eq_type_signature,
2820 NULL,
2821 &objfile->objfile_obstack,
2822 hashtab_obstack_allocate,
2823 dummy_obstack_deallocate);
2824}
2825
2826/* A helper function to add a signatured type CU to a list. */
2827
2828static int
2829add_signatured_type_cu_to_list (void **slot, void *datum)
2830{
2831 struct signatured_type *sigt = *slot;
2832 struct dwarf2_per_cu_data ***datap = datum;
2833
2834 **datap = &sigt->per_cu;
2835 ++*datap;
2836
2837 return 1;
2838}
2839
348e048f
DE
2840/* Create the hash table of all entries in the .debug_types section.
2841 The result is zero if there is an error (e.g. missing .debug_types section),
2842 otherwise non-zero. */
2843
2844static int
2845create_debug_types_hash_table (struct objfile *objfile)
2846{
be391dca 2847 gdb_byte *info_ptr;
348e048f 2848 htab_t types_htab;
1fd400ff 2849 struct dwarf2_per_cu_data **iter;
348e048f 2850
be391dca
TT
2851 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2852 info_ptr = dwarf2_per_objfile->types.buffer;
2853
348e048f
DE
2854 if (info_ptr == NULL)
2855 {
2856 dwarf2_per_objfile->signatured_types = NULL;
2857 return 0;
2858 }
2859
673bfd45 2860 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2861
2862 if (dwarf2_die_debug)
2863 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2864
2865 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2866 {
2867 unsigned int offset;
2868 unsigned int offset_size;
2869 unsigned int type_offset;
2870 unsigned int length, initial_length_size;
2871 unsigned short version;
2872 ULONGEST signature;
2873 struct signatured_type *type_sig;
2874 void **slot;
2875 gdb_byte *ptr = info_ptr;
2876
2877 offset = ptr - dwarf2_per_objfile->types.buffer;
2878
2879 /* We need to read the type's signature in order to build the hash
2880 table, but we don't need to read anything else just yet. */
2881
2882 /* Sanity check to ensure entire cu is present. */
2883 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2884 if (ptr + length + initial_length_size
2885 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2886 {
2887 complaint (&symfile_complaints,
2888 _("debug type entry runs off end of `.debug_types' section, ignored"));
2889 break;
2890 }
2891
2892 offset_size = initial_length_size == 4 ? 4 : 8;
2893 ptr += initial_length_size;
2894 version = bfd_get_16 (objfile->obfd, ptr);
2895 ptr += 2;
2896 ptr += offset_size; /* abbrev offset */
2897 ptr += 1; /* address size */
2898 signature = bfd_get_64 (objfile->obfd, ptr);
2899 ptr += 8;
2900 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2901
2902 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2903 memset (type_sig, 0, sizeof (*type_sig));
2904 type_sig->signature = signature;
2905 type_sig->offset = offset;
2906 type_sig->type_offset = type_offset;
ca1f3406 2907 type_sig->per_cu.objfile = objfile;
1fd400ff 2908 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2909
2910 slot = htab_find_slot (types_htab, type_sig, INSERT);
2911 gdb_assert (slot != NULL);
2912 *slot = type_sig;
2913
2914 if (dwarf2_die_debug)
2915 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2916 offset, phex (signature, sizeof (signature)));
2917
2918 info_ptr = info_ptr + initial_length_size + length;
2919 }
2920
2921 dwarf2_per_objfile->signatured_types = types_htab;
2922
1fd400ff
TT
2923 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2924 dwarf2_per_objfile->type_comp_units
2925 = obstack_alloc (&objfile->objfile_obstack,
2926 dwarf2_per_objfile->n_type_comp_units
2927 * sizeof (struct dwarf2_per_cu_data *));
2928 iter = &dwarf2_per_objfile->type_comp_units[0];
2929 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2930 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2931 == dwarf2_per_objfile->n_type_comp_units);
2932
348e048f
DE
2933 return 1;
2934}
2935
2936/* Lookup a signature based type.
2937 Returns NULL if SIG is not present in the table. */
2938
2939static struct signatured_type *
2940lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2941{
2942 struct signatured_type find_entry, *entry;
2943
2944 if (dwarf2_per_objfile->signatured_types == NULL)
2945 {
2946 complaint (&symfile_complaints,
2947 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2948 return 0;
2949 }
2950
2951 find_entry.signature = sig;
2952 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2953 return entry;
2954}
2955
d85a05f0
DJ
2956/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2957
2958static void
2959init_cu_die_reader (struct die_reader_specs *reader,
2960 struct dwarf2_cu *cu)
2961{
2962 reader->abfd = cu->objfile->obfd;
2963 reader->cu = cu;
2964 if (cu->per_cu->from_debug_types)
be391dca
TT
2965 {
2966 gdb_assert (dwarf2_per_objfile->types.readin);
2967 reader->buffer = dwarf2_per_objfile->types.buffer;
2968 }
d85a05f0 2969 else
be391dca
TT
2970 {
2971 gdb_assert (dwarf2_per_objfile->info.readin);
2972 reader->buffer = dwarf2_per_objfile->info.buffer;
2973 }
d85a05f0
DJ
2974}
2975
2976/* Find the base address of the compilation unit for range lists and
2977 location lists. It will normally be specified by DW_AT_low_pc.
2978 In DWARF-3 draft 4, the base address could be overridden by
2979 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2980 compilation units with discontinuous ranges. */
2981
2982static void
2983dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2984{
2985 struct attribute *attr;
2986
2987 cu->base_known = 0;
2988 cu->base_address = 0;
2989
2990 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2991 if (attr)
2992 {
2993 cu->base_address = DW_ADDR (attr);
2994 cu->base_known = 1;
2995 }
2996 else
2997 {
2998 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2999 if (attr)
3000 {
3001 cu->base_address = DW_ADDR (attr);
3002 cu->base_known = 1;
3003 }
3004 }
3005}
3006
348e048f
DE
3007/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3008 to combine the common parts.
93311388 3009 Process a compilation unit for a psymtab.
348e048f
DE
3010 BUFFER is a pointer to the beginning of the dwarf section buffer,
3011 either .debug_info or debug_types.
93311388
DE
3012 INFO_PTR is a pointer to the start of the CU.
3013 Returns a pointer to the next CU. */
aaa75496 3014
93311388
DE
3015static gdb_byte *
3016process_psymtab_comp_unit (struct objfile *objfile,
3017 struct dwarf2_per_cu_data *this_cu,
3018 gdb_byte *buffer, gdb_byte *info_ptr,
3019 unsigned int buffer_size)
c906108c 3020{
c906108c 3021 bfd *abfd = objfile->obfd;
93311388 3022 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3023 struct die_info *comp_unit_die;
c906108c 3024 struct partial_symtab *pst;
5734ee8b 3025 CORE_ADDR baseaddr;
93311388
DE
3026 struct cleanup *back_to_inner;
3027 struct dwarf2_cu cu;
d85a05f0
DJ
3028 int has_children, has_pc_info;
3029 struct attribute *attr;
d85a05f0
DJ
3030 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3031 struct die_reader_specs reader_specs;
c906108c 3032
9816fde3 3033 init_one_comp_unit (&cu, objfile);
93311388 3034 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3035
93311388
DE
3036 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3037 buffer, buffer_size,
3038 abfd);
10b3939b 3039
93311388
DE
3040 /* Complete the cu_header. */
3041 cu.header.offset = beg_of_comp_unit - buffer;
3042 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3043
93311388 3044 cu.list_in_scope = &file_symbols;
af703f96 3045
328c9494
DJ
3046 /* If this compilation unit was already read in, free the
3047 cached copy in order to read it in again. This is
3048 necessary because we skipped some symbols when we first
3049 read in the compilation unit (see load_partial_dies).
3050 This problem could be avoided, but the benefit is
3051 unclear. */
3052 if (this_cu->cu != NULL)
3053 free_one_cached_comp_unit (this_cu->cu);
3054
3055 /* Note that this is a pointer to our stack frame, being
3056 added to a global data structure. It will be cleaned up
3057 in free_stack_comp_unit when we finish with this
3058 compilation unit. */
3059 this_cu->cu = &cu;
d85a05f0
DJ
3060 cu.per_cu = this_cu;
3061
93311388
DE
3062 /* Read the abbrevs for this compilation unit into a table. */
3063 dwarf2_read_abbrevs (abfd, &cu);
3064 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3065
93311388 3066 /* Read the compilation unit die. */
348e048f
DE
3067 if (this_cu->from_debug_types)
3068 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3069 init_cu_die_reader (&reader_specs, &cu);
3070 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3071 &has_children);
93311388 3072
348e048f
DE
3073 if (this_cu->from_debug_types)
3074 {
3075 /* offset,length haven't been set yet for type units. */
3076 this_cu->offset = cu.header.offset;
3077 this_cu->length = cu.header.length + cu.header.initial_length_size;
3078 }
d85a05f0 3079 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3080 {
93311388
DE
3081 info_ptr = (beg_of_comp_unit + cu.header.length
3082 + cu.header.initial_length_size);
3083 do_cleanups (back_to_inner);
3084 return info_ptr;
3085 }
72bf9492 3086
9816fde3 3087 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3088
93311388 3089 /* Allocate a new partial symbol table structure. */
d85a05f0 3090 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3091 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3092 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3093 /* TEXTLOW and TEXTHIGH are set below. */
3094 0,
3095 objfile->global_psymbols.next,
3096 objfile->static_psymbols.next);
72bf9492 3097
d85a05f0
DJ
3098 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3099 if (attr != NULL)
3100 pst->dirname = DW_STRING (attr);
72bf9492 3101
e38df1d0 3102 pst->read_symtab_private = this_cu;
72bf9492 3103
93311388 3104 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3105
93311388
DE
3106 /* Store the function that reads in the rest of the symbol table */
3107 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3108
9291a0cd 3109 this_cu->v.psymtab = pst;
c906108c 3110
d85a05f0
DJ
3111 dwarf2_find_base_address (comp_unit_die, &cu);
3112
93311388
DE
3113 /* Possibly set the default values of LOWPC and HIGHPC from
3114 `DW_AT_ranges'. */
d85a05f0
DJ
3115 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3116 &best_highpc, &cu, pst);
3117 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3118 /* Store the contiguous range if it is not empty; it can be empty for
3119 CUs with no code. */
3120 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3121 best_lowpc + baseaddr,
3122 best_highpc + baseaddr - 1, pst);
93311388
DE
3123
3124 /* Check if comp unit has_children.
3125 If so, read the rest of the partial symbols from this comp unit.
3126 If not, there's no more debug_info for this comp unit. */
d85a05f0 3127 if (has_children)
93311388
DE
3128 {
3129 struct partial_die_info *first_die;
3130 CORE_ADDR lowpc, highpc;
31ffec48 3131
93311388
DE
3132 lowpc = ((CORE_ADDR) -1);
3133 highpc = ((CORE_ADDR) 0);
c906108c 3134
93311388 3135 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3136
93311388 3137 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3138 ! has_pc_info, &cu);
57c22c6c 3139
93311388
DE
3140 /* If we didn't find a lowpc, set it to highpc to avoid
3141 complaints from `maint check'. */
3142 if (lowpc == ((CORE_ADDR) -1))
3143 lowpc = highpc;
10b3939b 3144
93311388
DE
3145 /* If the compilation unit didn't have an explicit address range,
3146 then use the information extracted from its child dies. */
d85a05f0 3147 if (! has_pc_info)
93311388 3148 {
d85a05f0
DJ
3149 best_lowpc = lowpc;
3150 best_highpc = highpc;
93311388
DE
3151 }
3152 }
d85a05f0
DJ
3153 pst->textlow = best_lowpc + baseaddr;
3154 pst->texthigh = best_highpc + baseaddr;
c906108c 3155
93311388
DE
3156 pst->n_global_syms = objfile->global_psymbols.next -
3157 (objfile->global_psymbols.list + pst->globals_offset);
3158 pst->n_static_syms = objfile->static_psymbols.next -
3159 (objfile->static_psymbols.list + pst->statics_offset);
3160 sort_pst_symbols (pst);
c906108c 3161
93311388
DE
3162 info_ptr = (beg_of_comp_unit + cu.header.length
3163 + cu.header.initial_length_size);
ae038cb0 3164
348e048f
DE
3165 if (this_cu->from_debug_types)
3166 {
3167 /* It's not clear we want to do anything with stmt lists here.
3168 Waiting to see what gcc ultimately does. */
3169 }
d85a05f0 3170 else
93311388
DE
3171 {
3172 /* Get the list of files included in the current compilation unit,
3173 and build a psymtab for each of them. */
d85a05f0 3174 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3175 }
ae038cb0 3176
93311388 3177 do_cleanups (back_to_inner);
ae038cb0 3178
93311388
DE
3179 return info_ptr;
3180}
ff013f42 3181
348e048f
DE
3182/* Traversal function for htab_traverse_noresize.
3183 Process one .debug_types comp-unit. */
3184
3185static int
3186process_type_comp_unit (void **slot, void *info)
3187{
3188 struct signatured_type *entry = (struct signatured_type *) *slot;
3189 struct objfile *objfile = (struct objfile *) info;
3190 struct dwarf2_per_cu_data *this_cu;
3191
3192 this_cu = &entry->per_cu;
348e048f 3193
be391dca 3194 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3195 process_psymtab_comp_unit (objfile, this_cu,
3196 dwarf2_per_objfile->types.buffer,
3197 dwarf2_per_objfile->types.buffer + entry->offset,
3198 dwarf2_per_objfile->types.size);
3199
3200 return 1;
3201}
3202
3203/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3204 Build partial symbol tables for the .debug_types comp-units. */
3205
3206static void
3207build_type_psymtabs (struct objfile *objfile)
3208{
3209 if (! create_debug_types_hash_table (objfile))
3210 return;
3211
3212 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3213 process_type_comp_unit, objfile);
3214}
3215
60606b2c
TT
3216/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3217
3218static void
3219psymtabs_addrmap_cleanup (void *o)
3220{
3221 struct objfile *objfile = o;
ec61707d 3222
60606b2c
TT
3223 objfile->psymtabs_addrmap = NULL;
3224}
3225
93311388
DE
3226/* Build the partial symbol table by doing a quick pass through the
3227 .debug_info and .debug_abbrev sections. */
72bf9492 3228
93311388 3229static void
c67a9c90 3230dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3231{
93311388 3232 gdb_byte *info_ptr;
60606b2c
TT
3233 struct cleanup *back_to, *addrmap_cleanup;
3234 struct obstack temp_obstack;
93311388 3235
98bfdba5
PA
3236 dwarf2_per_objfile->reading_partial_symbols = 1;
3237
be391dca 3238 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3239 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3240
93311388
DE
3241 /* Any cached compilation units will be linked by the per-objfile
3242 read_in_chain. Make sure to free them when we're done. */
3243 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3244
348e048f
DE
3245 build_type_psymtabs (objfile);
3246
93311388 3247 create_all_comp_units (objfile);
c906108c 3248
60606b2c
TT
3249 /* Create a temporary address map on a temporary obstack. We later
3250 copy this to the final obstack. */
3251 obstack_init (&temp_obstack);
3252 make_cleanup_obstack_free (&temp_obstack);
3253 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3254 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3255
93311388
DE
3256 /* Since the objects we're extracting from .debug_info vary in
3257 length, only the individual functions to extract them (like
3258 read_comp_unit_head and load_partial_die) can really know whether
3259 the buffer is large enough to hold another complete object.
c906108c 3260
93311388
DE
3261 At the moment, they don't actually check that. If .debug_info
3262 holds just one extra byte after the last compilation unit's dies,
3263 then read_comp_unit_head will happily read off the end of the
3264 buffer. read_partial_die is similarly casual. Those functions
3265 should be fixed.
c906108c 3266
93311388
DE
3267 For this loop condition, simply checking whether there's any data
3268 left at all should be sufficient. */
c906108c 3269
93311388
DE
3270 while (info_ptr < (dwarf2_per_objfile->info.buffer
3271 + dwarf2_per_objfile->info.size))
3272 {
3273 struct dwarf2_per_cu_data *this_cu;
dd373385 3274
93311388
DE
3275 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3276 objfile);
aaa75496 3277
93311388
DE
3278 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3279 dwarf2_per_objfile->info.buffer,
3280 info_ptr,
3281 dwarf2_per_objfile->info.size);
c906108c 3282 }
ff013f42
JK
3283
3284 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3285 &objfile->objfile_obstack);
60606b2c 3286 discard_cleanups (addrmap_cleanup);
ff013f42 3287
ae038cb0
DJ
3288 do_cleanups (back_to);
3289}
3290
93311388 3291/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3292
3293static void
93311388
DE
3294load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3295 struct objfile *objfile)
ae038cb0
DJ
3296{
3297 bfd *abfd = objfile->obfd;
fe1b8b76 3298 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3299 struct die_info *comp_unit_die;
ae038cb0 3300 struct dwarf2_cu *cu;
1d9ec526 3301 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3302 int has_children;
3303 struct die_reader_specs reader_specs;
98bfdba5 3304 int read_cu = 0;
ae038cb0 3305
348e048f
DE
3306 gdb_assert (! this_cu->from_debug_types);
3307
be391dca 3308 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3309 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3310 beg_of_comp_unit = info_ptr;
3311
98bfdba5
PA
3312 if (this_cu->cu == NULL)
3313 {
9816fde3
JK
3314 cu = xmalloc (sizeof (*cu));
3315 init_one_comp_unit (cu, objfile);
ae038cb0 3316
98bfdba5 3317 read_cu = 1;
ae038cb0 3318
98bfdba5
PA
3319 /* If an error occurs while loading, release our storage. */
3320 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3321
98bfdba5
PA
3322 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3323 dwarf2_per_objfile->info.buffer,
3324 dwarf2_per_objfile->info.size,
3325 abfd);
ae038cb0 3326
98bfdba5
PA
3327 /* Complete the cu_header. */
3328 cu->header.offset = this_cu->offset;
3329 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3330
3331 /* Link this compilation unit into the compilation unit tree. */
3332 this_cu->cu = cu;
3333 cu->per_cu = this_cu;
98bfdba5
PA
3334
3335 /* Link this CU into read_in_chain. */
3336 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3337 dwarf2_per_objfile->read_in_chain = this_cu;
3338 }
3339 else
3340 {
3341 cu = this_cu->cu;
3342 info_ptr += cu->header.first_die_offset;
3343 }
ae038cb0
DJ
3344
3345 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3346 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3347 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3348 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3349
3350 /* Read the compilation unit die. */
d85a05f0
DJ
3351 init_cu_die_reader (&reader_specs, cu);
3352 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3353 &has_children);
ae038cb0 3354
9816fde3 3355 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3356
ae038cb0
DJ
3357 /* Check if comp unit has_children.
3358 If so, read the rest of the partial symbols from this comp unit.
3359 If not, there's no more debug_info for this comp unit. */
d85a05f0 3360 if (has_children)
93311388 3361 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3362
98bfdba5
PA
3363 do_cleanups (free_abbrevs_cleanup);
3364
3365 if (read_cu)
3366 {
3367 /* We've successfully allocated this compilation unit. Let our
3368 caller clean it up when finished with it. */
3369 discard_cleanups (free_cu_cleanup);
3370 }
ae038cb0
DJ
3371}
3372
3373/* Create a list of all compilation units in OBJFILE. We do this only
3374 if an inter-comp-unit reference is found; presumably if there is one,
3375 there will be many, and one will occur early in the .debug_info section.
3376 So there's no point in building this list incrementally. */
3377
3378static void
3379create_all_comp_units (struct objfile *objfile)
3380{
3381 int n_allocated;
3382 int n_comp_units;
3383 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3384 gdb_byte *info_ptr;
3385
3386 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3387 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3388
3389 n_comp_units = 0;
3390 n_allocated = 10;
3391 all_comp_units = xmalloc (n_allocated
3392 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3393
dce234bc 3394 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3395 {
c764a876 3396 unsigned int length, initial_length_size;
ae038cb0 3397 struct dwarf2_per_cu_data *this_cu;
c764a876 3398 unsigned int offset;
ae038cb0 3399
dce234bc 3400 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3401
3402 /* Read just enough information to find out where the next
3403 compilation unit is. */
c764a876
DE
3404 length = read_initial_length (objfile->obfd, info_ptr,
3405 &initial_length_size);
ae038cb0
DJ
3406
3407 /* Save the compilation unit for later lookup. */
3408 this_cu = obstack_alloc (&objfile->objfile_obstack,
3409 sizeof (struct dwarf2_per_cu_data));
3410 memset (this_cu, 0, sizeof (*this_cu));
3411 this_cu->offset = offset;
c764a876 3412 this_cu->length = length + initial_length_size;
9291a0cd 3413 this_cu->objfile = objfile;
ae038cb0
DJ
3414
3415 if (n_comp_units == n_allocated)
3416 {
3417 n_allocated *= 2;
3418 all_comp_units = xrealloc (all_comp_units,
3419 n_allocated
3420 * sizeof (struct dwarf2_per_cu_data *));
3421 }
3422 all_comp_units[n_comp_units++] = this_cu;
3423
3424 info_ptr = info_ptr + this_cu->length;
3425 }
3426
3427 dwarf2_per_objfile->all_comp_units
3428 = obstack_alloc (&objfile->objfile_obstack,
3429 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3430 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3431 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3432 xfree (all_comp_units);
3433 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3434}
3435
5734ee8b
DJ
3436/* Process all loaded DIEs for compilation unit CU, starting at
3437 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3438 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3439 DW_AT_ranges). If NEED_PC is set, then this function will set
3440 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3441 and record the covered ranges in the addrmap. */
c906108c 3442
72bf9492
DJ
3443static void
3444scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3445 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3446{
72bf9492 3447 struct partial_die_info *pdi;
c906108c 3448
91c24f0a
DC
3449 /* Now, march along the PDI's, descending into ones which have
3450 interesting children but skipping the children of the other ones,
3451 until we reach the end of the compilation unit. */
c906108c 3452
72bf9492 3453 pdi = first_die;
91c24f0a 3454
72bf9492
DJ
3455 while (pdi != NULL)
3456 {
3457 fixup_partial_die (pdi, cu);
c906108c 3458
f55ee35c 3459 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3460 children, so we need to look at them. Ditto for anonymous
3461 enums. */
933c6fe4 3462
72bf9492 3463 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3464 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3465 {
72bf9492 3466 switch (pdi->tag)
c906108c
SS
3467 {
3468 case DW_TAG_subprogram:
5734ee8b 3469 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3470 break;
72929c62 3471 case DW_TAG_constant:
c906108c
SS
3472 case DW_TAG_variable:
3473 case DW_TAG_typedef:
91c24f0a 3474 case DW_TAG_union_type:
72bf9492 3475 if (!pdi->is_declaration)
63d06c5c 3476 {
72bf9492 3477 add_partial_symbol (pdi, cu);
63d06c5c
DC
3478 }
3479 break;
c906108c 3480 case DW_TAG_class_type:
680b30c7 3481 case DW_TAG_interface_type:
c906108c 3482 case DW_TAG_structure_type:
72bf9492 3483 if (!pdi->is_declaration)
c906108c 3484 {
72bf9492 3485 add_partial_symbol (pdi, cu);
c906108c
SS
3486 }
3487 break;
91c24f0a 3488 case DW_TAG_enumeration_type:
72bf9492
DJ
3489 if (!pdi->is_declaration)
3490 add_partial_enumeration (pdi, cu);
c906108c
SS
3491 break;
3492 case DW_TAG_base_type:
a02abb62 3493 case DW_TAG_subrange_type:
c906108c 3494 /* File scope base type definitions are added to the partial
c5aa993b 3495 symbol table. */
72bf9492 3496 add_partial_symbol (pdi, cu);
c906108c 3497 break;
d9fa45fe 3498 case DW_TAG_namespace:
5734ee8b 3499 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3500 break;
5d7cb8df
JK
3501 case DW_TAG_module:
3502 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3503 break;
c906108c
SS
3504 default:
3505 break;
3506 }
3507 }
3508
72bf9492
DJ
3509 /* If the die has a sibling, skip to the sibling. */
3510
3511 pdi = pdi->die_sibling;
3512 }
3513}
3514
3515/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3516
72bf9492 3517 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3518 name is concatenated with "::" and the partial DIE's name. For
3519 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3520 Enumerators are an exception; they use the scope of their parent
3521 enumeration type, i.e. the name of the enumeration type is not
3522 prepended to the enumerator.
91c24f0a 3523
72bf9492
DJ
3524 There are two complexities. One is DW_AT_specification; in this
3525 case "parent" means the parent of the target of the specification,
3526 instead of the direct parent of the DIE. The other is compilers
3527 which do not emit DW_TAG_namespace; in this case we try to guess
3528 the fully qualified name of structure types from their members'
3529 linkage names. This must be done using the DIE's children rather
3530 than the children of any DW_AT_specification target. We only need
3531 to do this for structures at the top level, i.e. if the target of
3532 any DW_AT_specification (if any; otherwise the DIE itself) does not
3533 have a parent. */
3534
3535/* Compute the scope prefix associated with PDI's parent, in
3536 compilation unit CU. The result will be allocated on CU's
3537 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3538 field. NULL is returned if no prefix is necessary. */
3539static char *
3540partial_die_parent_scope (struct partial_die_info *pdi,
3541 struct dwarf2_cu *cu)
3542{
3543 char *grandparent_scope;
3544 struct partial_die_info *parent, *real_pdi;
91c24f0a 3545
72bf9492
DJ
3546 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3547 then this means the parent of the specification DIE. */
3548
3549 real_pdi = pdi;
72bf9492 3550 while (real_pdi->has_specification)
10b3939b 3551 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3552
3553 parent = real_pdi->die_parent;
3554 if (parent == NULL)
3555 return NULL;
3556
3557 if (parent->scope_set)
3558 return parent->scope;
3559
3560 fixup_partial_die (parent, cu);
3561
10b3939b 3562 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3563
acebe513
UW
3564 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3565 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3566 Work around this problem here. */
3567 if (cu->language == language_cplus
6e70227d 3568 && parent->tag == DW_TAG_namespace
acebe513
UW
3569 && strcmp (parent->name, "::") == 0
3570 && grandparent_scope == NULL)
3571 {
3572 parent->scope = NULL;
3573 parent->scope_set = 1;
3574 return NULL;
3575 }
3576
72bf9492 3577 if (parent->tag == DW_TAG_namespace
f55ee35c 3578 || parent->tag == DW_TAG_module
72bf9492
DJ
3579 || parent->tag == DW_TAG_structure_type
3580 || parent->tag == DW_TAG_class_type
680b30c7 3581 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3582 || parent->tag == DW_TAG_union_type
3583 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3584 {
3585 if (grandparent_scope == NULL)
3586 parent->scope = parent->name;
3587 else
987504bb 3588 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3589 parent->name, 0, cu);
72bf9492 3590 }
ceeb3d5a 3591 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3592 /* Enumerators should not get the name of the enumeration as a prefix. */
3593 parent->scope = grandparent_scope;
3594 else
3595 {
3596 /* FIXME drow/2004-04-01: What should we be doing with
3597 function-local names? For partial symbols, we should probably be
3598 ignoring them. */
3599 complaint (&symfile_complaints,
e2e0b3e5 3600 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3601 parent->tag, pdi->offset);
3602 parent->scope = grandparent_scope;
c906108c
SS
3603 }
3604
72bf9492
DJ
3605 parent->scope_set = 1;
3606 return parent->scope;
3607}
3608
3609/* Return the fully scoped name associated with PDI, from compilation unit
3610 CU. The result will be allocated with malloc. */
3611static char *
3612partial_die_full_name (struct partial_die_info *pdi,
3613 struct dwarf2_cu *cu)
3614{
3615 char *parent_scope;
3616
98bfdba5
PA
3617 /* If this is a template instantiation, we can not work out the
3618 template arguments from partial DIEs. So, unfortunately, we have
3619 to go through the full DIEs. At least any work we do building
3620 types here will be reused if full symbols are loaded later. */
3621 if (pdi->has_template_arguments)
3622 {
3623 fixup_partial_die (pdi, cu);
3624
3625 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3626 {
3627 struct die_info *die;
3628 struct attribute attr;
3629 struct dwarf2_cu *ref_cu = cu;
3630
3631 attr.name = 0;
3632 attr.form = DW_FORM_ref_addr;
3633 attr.u.addr = pdi->offset;
3634 die = follow_die_ref (NULL, &attr, &ref_cu);
3635
3636 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3637 }
3638 }
3639
72bf9492
DJ
3640 parent_scope = partial_die_parent_scope (pdi, cu);
3641 if (parent_scope == NULL)
3642 return NULL;
3643 else
f55ee35c 3644 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3645}
3646
3647static void
72bf9492 3648add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3649{
e7c27a73 3650 struct objfile *objfile = cu->objfile;
c906108c 3651 CORE_ADDR addr = 0;
decbce07 3652 char *actual_name = NULL;
5c4e30ca 3653 const struct partial_symbol *psym = NULL;
e142c38c 3654 CORE_ADDR baseaddr;
72bf9492 3655 int built_actual_name = 0;
e142c38c
DJ
3656
3657 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3658
94af9270
KS
3659 actual_name = partial_die_full_name (pdi, cu);
3660 if (actual_name)
3661 built_actual_name = 1;
63d06c5c 3662
72bf9492
DJ
3663 if (actual_name == NULL)
3664 actual_name = pdi->name;
3665
c906108c
SS
3666 switch (pdi->tag)
3667 {
3668 case DW_TAG_subprogram:
2cfa0c8d 3669 if (pdi->is_external || cu->language == language_ada)
c906108c 3670 {
2cfa0c8d
JB
3671 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3672 of the global scope. But in Ada, we want to be able to access
3673 nested procedures globally. So all Ada subprograms are stored
3674 in the global scope. */
38d518c9 3675 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3676 mst_text, objfile); */
38d518c9 3677 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3678 built_actual_name,
5c4e30ca
DC
3679 VAR_DOMAIN, LOC_BLOCK,
3680 &objfile->global_psymbols,
3681 0, pdi->lowpc + baseaddr,
e142c38c 3682 cu->language, objfile);
c906108c
SS
3683 }
3684 else
3685 {
38d518c9 3686 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3687 mst_file_text, objfile); */
38d518c9 3688 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3689 built_actual_name,
5c4e30ca
DC
3690 VAR_DOMAIN, LOC_BLOCK,
3691 &objfile->static_psymbols,
3692 0, pdi->lowpc + baseaddr,
e142c38c 3693 cu->language, objfile);
c906108c
SS
3694 }
3695 break;
72929c62
JB
3696 case DW_TAG_constant:
3697 {
3698 struct psymbol_allocation_list *list;
3699
3700 if (pdi->is_external)
3701 list = &objfile->global_psymbols;
3702 else
3703 list = &objfile->static_psymbols;
3704 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3705 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3706 list, 0, 0, cu->language, objfile);
3707
3708 }
3709 break;
c906108c 3710 case DW_TAG_variable:
caac4577
JG
3711 if (pdi->locdesc)
3712 addr = decode_locdesc (pdi->locdesc, cu);
3713
3714 if (pdi->locdesc
3715 && addr == 0
3716 && !dwarf2_per_objfile->has_section_at_zero)
3717 {
3718 /* A global or static variable may also have been stripped
3719 out by the linker if unused, in which case its address
3720 will be nullified; do not add such variables into partial
3721 symbol table then. */
3722 }
3723 else if (pdi->is_external)
c906108c
SS
3724 {
3725 /* Global Variable.
3726 Don't enter into the minimal symbol tables as there is
3727 a minimal symbol table entry from the ELF symbols already.
3728 Enter into partial symbol table if it has a location
3729 descriptor or a type.
3730 If the location descriptor is missing, new_symbol will create
3731 a LOC_UNRESOLVED symbol, the address of the variable will then
3732 be determined from the minimal symbol table whenever the variable
3733 is referenced.
3734 The address for the partial symbol table entry is not
3735 used by GDB, but it comes in handy for debugging partial symbol
3736 table building. */
3737
c906108c 3738 if (pdi->locdesc || pdi->has_type)
38d518c9 3739 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3740 built_actual_name,
5c4e30ca
DC
3741 VAR_DOMAIN, LOC_STATIC,
3742 &objfile->global_psymbols,
3743 0, addr + baseaddr,
e142c38c 3744 cu->language, objfile);
c906108c
SS
3745 }
3746 else
3747 {
3748 /* Static Variable. Skip symbols without location descriptors. */
3749 if (pdi->locdesc == NULL)
decbce07
MS
3750 {
3751 if (built_actual_name)
3752 xfree (actual_name);
3753 return;
3754 }
38d518c9 3755 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3756 mst_file_data, objfile); */
38d518c9 3757 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3758 built_actual_name,
5c4e30ca
DC
3759 VAR_DOMAIN, LOC_STATIC,
3760 &objfile->static_psymbols,
3761 0, addr + baseaddr,
e142c38c 3762 cu->language, objfile);
c906108c
SS
3763 }
3764 break;
3765 case DW_TAG_typedef:
3766 case DW_TAG_base_type:
a02abb62 3767 case DW_TAG_subrange_type:
38d518c9 3768 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3769 built_actual_name,
176620f1 3770 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3771 &objfile->static_psymbols,
e142c38c 3772 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3773 break;
72bf9492
DJ
3774 case DW_TAG_namespace:
3775 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3776 built_actual_name,
72bf9492
DJ
3777 VAR_DOMAIN, LOC_TYPEDEF,
3778 &objfile->global_psymbols,
3779 0, (CORE_ADDR) 0, cu->language, objfile);
3780 break;
c906108c 3781 case DW_TAG_class_type:
680b30c7 3782 case DW_TAG_interface_type:
c906108c
SS
3783 case DW_TAG_structure_type:
3784 case DW_TAG_union_type:
3785 case DW_TAG_enumeration_type:
fa4028e9
JB
3786 /* Skip external references. The DWARF standard says in the section
3787 about "Structure, Union, and Class Type Entries": "An incomplete
3788 structure, union or class type is represented by a structure,
3789 union or class entry that does not have a byte size attribute
3790 and that has a DW_AT_declaration attribute." */
3791 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3792 {
3793 if (built_actual_name)
3794 xfree (actual_name);
3795 return;
3796 }
fa4028e9 3797
63d06c5c
DC
3798 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3799 static vs. global. */
38d518c9 3800 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3801 built_actual_name,
176620f1 3802 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3803 (cu->language == language_cplus
3804 || cu->language == language_java)
63d06c5c
DC
3805 ? &objfile->global_psymbols
3806 : &objfile->static_psymbols,
e142c38c 3807 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3808
c906108c
SS
3809 break;
3810 case DW_TAG_enumerator:
38d518c9 3811 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3812 built_actual_name,
176620f1 3813 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3814 (cu->language == language_cplus
3815 || cu->language == language_java)
f6fe98ef
DJ
3816 ? &objfile->global_psymbols
3817 : &objfile->static_psymbols,
e142c38c 3818 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3819 break;
3820 default:
3821 break;
3822 }
5c4e30ca 3823
72bf9492
DJ
3824 if (built_actual_name)
3825 xfree (actual_name);
c906108c
SS
3826}
3827
5c4e30ca
DC
3828/* Read a partial die corresponding to a namespace; also, add a symbol
3829 corresponding to that namespace to the symbol table. NAMESPACE is
3830 the name of the enclosing namespace. */
91c24f0a 3831
72bf9492
DJ
3832static void
3833add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3834 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3835 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3836{
72bf9492 3837 /* Add a symbol for the namespace. */
e7c27a73 3838
72bf9492 3839 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3840
3841 /* Now scan partial symbols in that namespace. */
3842
91c24f0a 3843 if (pdi->has_children)
5734ee8b 3844 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3845}
3846
5d7cb8df
JK
3847/* Read a partial die corresponding to a Fortran module. */
3848
3849static void
3850add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3851 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3852{
f55ee35c 3853 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3854
3855 if (pdi->has_children)
3856 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3857}
3858
bc30ff58
JB
3859/* Read a partial die corresponding to a subprogram and create a partial
3860 symbol for that subprogram. When the CU language allows it, this
3861 routine also defines a partial symbol for each nested subprogram
3862 that this subprogram contains.
6e70227d 3863
bc30ff58
JB
3864 DIE my also be a lexical block, in which case we simply search
3865 recursively for suprograms defined inside that lexical block.
3866 Again, this is only performed when the CU language allows this
3867 type of definitions. */
3868
3869static void
3870add_partial_subprogram (struct partial_die_info *pdi,
3871 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3872 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3873{
3874 if (pdi->tag == DW_TAG_subprogram)
3875 {
3876 if (pdi->has_pc_info)
3877 {
3878 if (pdi->lowpc < *lowpc)
3879 *lowpc = pdi->lowpc;
3880 if (pdi->highpc > *highpc)
3881 *highpc = pdi->highpc;
5734ee8b
DJ
3882 if (need_pc)
3883 {
3884 CORE_ADDR baseaddr;
3885 struct objfile *objfile = cu->objfile;
3886
3887 baseaddr = ANOFFSET (objfile->section_offsets,
3888 SECT_OFF_TEXT (objfile));
3889 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3890 pdi->lowpc + baseaddr,
3891 pdi->highpc - 1 + baseaddr,
9291a0cd 3892 cu->per_cu->v.psymtab);
5734ee8b 3893 }
bc30ff58 3894 if (!pdi->is_declaration)
e8d05480
JB
3895 /* Ignore subprogram DIEs that do not have a name, they are
3896 illegal. Do not emit a complaint at this point, we will
3897 do so when we convert this psymtab into a symtab. */
3898 if (pdi->name)
3899 add_partial_symbol (pdi, cu);
bc30ff58
JB
3900 }
3901 }
6e70227d 3902
bc30ff58
JB
3903 if (! pdi->has_children)
3904 return;
3905
3906 if (cu->language == language_ada)
3907 {
3908 pdi = pdi->die_child;
3909 while (pdi != NULL)
3910 {
3911 fixup_partial_die (pdi, cu);
3912 if (pdi->tag == DW_TAG_subprogram
3913 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3914 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3915 pdi = pdi->die_sibling;
3916 }
3917 }
3918}
3919
91c24f0a
DC
3920/* Read a partial die corresponding to an enumeration type. */
3921
72bf9492
DJ
3922static void
3923add_partial_enumeration (struct partial_die_info *enum_pdi,
3924 struct dwarf2_cu *cu)
91c24f0a 3925{
72bf9492 3926 struct partial_die_info *pdi;
91c24f0a
DC
3927
3928 if (enum_pdi->name != NULL)
72bf9492
DJ
3929 add_partial_symbol (enum_pdi, cu);
3930
3931 pdi = enum_pdi->die_child;
3932 while (pdi)
91c24f0a 3933 {
72bf9492 3934 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3935 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3936 else
72bf9492
DJ
3937 add_partial_symbol (pdi, cu);
3938 pdi = pdi->die_sibling;
91c24f0a 3939 }
91c24f0a
DC
3940}
3941
4bb7a0a7
DJ
3942/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3943 Return the corresponding abbrev, or NULL if the number is zero (indicating
3944 an empty DIE). In either case *BYTES_READ will be set to the length of
3945 the initial number. */
3946
3947static struct abbrev_info *
fe1b8b76 3948peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3949 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3950{
3951 bfd *abfd = cu->objfile->obfd;
3952 unsigned int abbrev_number;
3953 struct abbrev_info *abbrev;
3954
3955 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3956
3957 if (abbrev_number == 0)
3958 return NULL;
3959
3960 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3961 if (!abbrev)
3962 {
8a3fe4f8 3963 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3964 bfd_get_filename (abfd));
3965 }
3966
3967 return abbrev;
3968}
3969
93311388
DE
3970/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3971 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3972 DIE. Any children of the skipped DIEs will also be skipped. */
3973
fe1b8b76 3974static gdb_byte *
93311388 3975skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3976{
3977 struct abbrev_info *abbrev;
3978 unsigned int bytes_read;
3979
3980 while (1)
3981 {
3982 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3983 if (abbrev == NULL)
3984 return info_ptr + bytes_read;
3985 else
93311388 3986 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3987 }
3988}
3989
93311388
DE
3990/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3991 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3992 abbrev corresponding to that skipped uleb128 should be passed in
3993 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3994 children. */
3995
fe1b8b76 3996static gdb_byte *
93311388
DE
3997skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3998 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3999{
4000 unsigned int bytes_read;
4001 struct attribute attr;
4002 bfd *abfd = cu->objfile->obfd;
4003 unsigned int form, i;
4004
4005 for (i = 0; i < abbrev->num_attrs; i++)
4006 {
4007 /* The only abbrev we care about is DW_AT_sibling. */
4008 if (abbrev->attrs[i].name == DW_AT_sibling)
4009 {
4010 read_attribute (&attr, &abbrev->attrs[i],
4011 abfd, info_ptr, cu);
4012 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4013 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4014 else
93311388 4015 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4016 }
4017
4018 /* If it isn't DW_AT_sibling, skip this attribute. */
4019 form = abbrev->attrs[i].form;
4020 skip_attribute:
4021 switch (form)
4022 {
4bb7a0a7 4023 case DW_FORM_ref_addr:
ae411497
TT
4024 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4025 and later it is offset sized. */
4026 if (cu->header.version == 2)
4027 info_ptr += cu->header.addr_size;
4028 else
4029 info_ptr += cu->header.offset_size;
4030 break;
4031 case DW_FORM_addr:
4bb7a0a7
DJ
4032 info_ptr += cu->header.addr_size;
4033 break;
4034 case DW_FORM_data1:
4035 case DW_FORM_ref1:
4036 case DW_FORM_flag:
4037 info_ptr += 1;
4038 break;
2dc7f7b3
TT
4039 case DW_FORM_flag_present:
4040 break;
4bb7a0a7
DJ
4041 case DW_FORM_data2:
4042 case DW_FORM_ref2:
4043 info_ptr += 2;
4044 break;
4045 case DW_FORM_data4:
4046 case DW_FORM_ref4:
4047 info_ptr += 4;
4048 break;
4049 case DW_FORM_data8:
4050 case DW_FORM_ref8:
348e048f 4051 case DW_FORM_sig8:
4bb7a0a7
DJ
4052 info_ptr += 8;
4053 break;
4054 case DW_FORM_string:
9b1c24c8 4055 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4056 info_ptr += bytes_read;
4057 break;
2dc7f7b3 4058 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4059 case DW_FORM_strp:
4060 info_ptr += cu->header.offset_size;
4061 break;
2dc7f7b3 4062 case DW_FORM_exprloc:
4bb7a0a7
DJ
4063 case DW_FORM_block:
4064 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4065 info_ptr += bytes_read;
4066 break;
4067 case DW_FORM_block1:
4068 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4069 break;
4070 case DW_FORM_block2:
4071 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4072 break;
4073 case DW_FORM_block4:
4074 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4075 break;
4076 case DW_FORM_sdata:
4077 case DW_FORM_udata:
4078 case DW_FORM_ref_udata:
4079 info_ptr = skip_leb128 (abfd, info_ptr);
4080 break;
4081 case DW_FORM_indirect:
4082 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4083 info_ptr += bytes_read;
4084 /* We need to continue parsing from here, so just go back to
4085 the top. */
4086 goto skip_attribute;
4087
4088 default:
8a3fe4f8 4089 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4090 dwarf_form_name (form),
4091 bfd_get_filename (abfd));
4092 }
4093 }
4094
4095 if (abbrev->has_children)
93311388 4096 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4097 else
4098 return info_ptr;
4099}
4100
93311388
DE
4101/* Locate ORIG_PDI's sibling.
4102 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4103 in BUFFER. */
91c24f0a 4104
fe1b8b76 4105static gdb_byte *
93311388
DE
4106locate_pdi_sibling (struct partial_die_info *orig_pdi,
4107 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4108 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4109{
4110 /* Do we know the sibling already? */
72bf9492 4111
91c24f0a
DC
4112 if (orig_pdi->sibling)
4113 return orig_pdi->sibling;
4114
4115 /* Are there any children to deal with? */
4116
4117 if (!orig_pdi->has_children)
4118 return info_ptr;
4119
4bb7a0a7 4120 /* Skip the children the long way. */
91c24f0a 4121
93311388 4122 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4123}
4124
c906108c
SS
4125/* Expand this partial symbol table into a full symbol table. */
4126
4127static void
fba45db2 4128dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4129{
c906108c
SS
4130 if (pst != NULL)
4131 {
4132 if (pst->readin)
4133 {
8a3fe4f8 4134 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4135 }
4136 else
4137 {
4138 if (info_verbose)
4139 {
a3f17187 4140 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4141 gdb_flush (gdb_stdout);
4142 }
4143
10b3939b
DJ
4144 /* Restore our global data. */
4145 dwarf2_per_objfile = objfile_data (pst->objfile,
4146 dwarf2_objfile_data_key);
4147
b2ab525c
KB
4148 /* If this psymtab is constructed from a debug-only objfile, the
4149 has_section_at_zero flag will not necessarily be correct. We
4150 can get the correct value for this flag by looking at the data
4151 associated with the (presumably stripped) associated objfile. */
4152 if (pst->objfile->separate_debug_objfile_backlink)
4153 {
4154 struct dwarf2_per_objfile *dpo_backlink
4155 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4156 dwarf2_objfile_data_key);
9a619af0 4157
b2ab525c
KB
4158 dwarf2_per_objfile->has_section_at_zero
4159 = dpo_backlink->has_section_at_zero;
4160 }
4161
98bfdba5
PA
4162 dwarf2_per_objfile->reading_partial_symbols = 0;
4163
c906108c
SS
4164 psymtab_to_symtab_1 (pst);
4165
4166 /* Finish up the debug error message. */
4167 if (info_verbose)
a3f17187 4168 printf_filtered (_("done.\n"));
c906108c
SS
4169 }
4170 }
4171}
4172
10b3939b
DJ
4173/* Add PER_CU to the queue. */
4174
4175static void
03dd20cc 4176queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4177{
4178 struct dwarf2_queue_item *item;
4179
4180 per_cu->queued = 1;
4181 item = xmalloc (sizeof (*item));
4182 item->per_cu = per_cu;
4183 item->next = NULL;
4184
4185 if (dwarf2_queue == NULL)
4186 dwarf2_queue = item;
4187 else
4188 dwarf2_queue_tail->next = item;
4189
4190 dwarf2_queue_tail = item;
4191}
4192
4193/* Process the queue. */
4194
4195static void
4196process_queue (struct objfile *objfile)
4197{
4198 struct dwarf2_queue_item *item, *next_item;
4199
03dd20cc
DJ
4200 /* The queue starts out with one item, but following a DIE reference
4201 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4202 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4203 {
9291a0cd
TT
4204 if (dwarf2_per_objfile->using_index
4205 ? !item->per_cu->v.quick->symtab
4206 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4207 process_full_comp_unit (item->per_cu);
4208
4209 item->per_cu->queued = 0;
4210 next_item = item->next;
4211 xfree (item);
4212 }
4213
4214 dwarf2_queue_tail = NULL;
4215}
4216
4217/* Free all allocated queue entries. This function only releases anything if
4218 an error was thrown; if the queue was processed then it would have been
4219 freed as we went along. */
4220
4221static void
4222dwarf2_release_queue (void *dummy)
4223{
4224 struct dwarf2_queue_item *item, *last;
4225
4226 item = dwarf2_queue;
4227 while (item)
4228 {
4229 /* Anything still marked queued is likely to be in an
4230 inconsistent state, so discard it. */
4231 if (item->per_cu->queued)
4232 {
4233 if (item->per_cu->cu != NULL)
4234 free_one_cached_comp_unit (item->per_cu->cu);
4235 item->per_cu->queued = 0;
4236 }
4237
4238 last = item;
4239 item = item->next;
4240 xfree (last);
4241 }
4242
4243 dwarf2_queue = dwarf2_queue_tail = NULL;
4244}
4245
4246/* Read in full symbols for PST, and anything it depends on. */
4247
c906108c 4248static void
fba45db2 4249psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4250{
10b3939b 4251 struct dwarf2_per_cu_data *per_cu;
c906108c 4252 struct cleanup *back_to;
aaa75496
JB
4253 int i;
4254
4255 for (i = 0; i < pst->number_of_dependencies; i++)
4256 if (!pst->dependencies[i]->readin)
4257 {
4258 /* Inform about additional files that need to be read in. */
4259 if (info_verbose)
4260 {
a3f17187 4261 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4262 fputs_filtered (" ", gdb_stdout);
4263 wrap_here ("");
4264 fputs_filtered ("and ", gdb_stdout);
4265 wrap_here ("");
4266 printf_filtered ("%s...", pst->dependencies[i]->filename);
4267 wrap_here (""); /* Flush output */
4268 gdb_flush (gdb_stdout);
4269 }
4270 psymtab_to_symtab_1 (pst->dependencies[i]);
4271 }
4272
e38df1d0 4273 per_cu = pst->read_symtab_private;
10b3939b
DJ
4274
4275 if (per_cu == NULL)
aaa75496
JB
4276 {
4277 /* It's an include file, no symbols to read for it.
4278 Everything is in the parent symtab. */
4279 pst->readin = 1;
4280 return;
4281 }
c906108c 4282
9291a0cd 4283 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4284}
4285
93311388 4286/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4287
93311388 4288static void
31ffec48 4289load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4290{
31ffec48 4291 bfd *abfd = objfile->obfd;
10b3939b 4292 struct dwarf2_cu *cu;
c764a876 4293 unsigned int offset;
93311388 4294 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4295 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4296 struct attribute *attr;
98bfdba5 4297 int read_cu = 0;
6502dd73 4298
348e048f
DE
4299 gdb_assert (! per_cu->from_debug_types);
4300
c906108c 4301 /* Set local variables from the partial symbol table info. */
10b3939b 4302 offset = per_cu->offset;
6502dd73 4303
be391dca 4304 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4305 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4306 beg_of_comp_unit = info_ptr;
63d06c5c 4307
98bfdba5
PA
4308 if (per_cu->cu == NULL)
4309 {
9816fde3
JK
4310 cu = xmalloc (sizeof (*cu));
4311 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4312
4313 read_cu = 1;
c906108c 4314
98bfdba5
PA
4315 /* If an error occurs while loading, release our storage. */
4316 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4317
98bfdba5
PA
4318 /* Read in the comp_unit header. */
4319 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4320
98bfdba5
PA
4321 /* Complete the cu_header. */
4322 cu->header.offset = offset;
4323 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4324
98bfdba5
PA
4325 /* Read the abbrevs for this compilation unit. */
4326 dwarf2_read_abbrevs (abfd, cu);
4327 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4328
98bfdba5
PA
4329 /* Link this compilation unit into the compilation unit tree. */
4330 per_cu->cu = cu;
4331 cu->per_cu = per_cu;
98bfdba5
PA
4332
4333 /* Link this CU into read_in_chain. */
4334 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4335 dwarf2_per_objfile->read_in_chain = per_cu;
4336 }
4337 else
4338 {
4339 cu = per_cu->cu;
4340 info_ptr += cu->header.first_die_offset;
4341 }
e142c38c 4342
93311388 4343 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4344
4345 /* We try not to read any attributes in this function, because not
4346 all objfiles needed for references have been loaded yet, and symbol
4347 table processing isn't initialized. But we have to set the CU language,
4348 or we won't be able to build types correctly. */
9816fde3 4349 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4350
a6c727b2
DJ
4351 /* Similarly, if we do not read the producer, we can not apply
4352 producer-specific interpretation. */
4353 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4354 if (attr)
4355 cu->producer = DW_STRING (attr);
4356
98bfdba5
PA
4357 if (read_cu)
4358 {
4359 do_cleanups (free_abbrevs_cleanup);
e142c38c 4360
98bfdba5
PA
4361 /* We've successfully allocated this compilation unit. Let our
4362 caller clean it up when finished with it. */
4363 discard_cleanups (free_cu_cleanup);
4364 }
10b3939b
DJ
4365}
4366
3da10d80
KS
4367/* Add a DIE to the delayed physname list. */
4368
4369static void
4370add_to_method_list (struct type *type, int fnfield_index, int index,
4371 const char *name, struct die_info *die,
4372 struct dwarf2_cu *cu)
4373{
4374 struct delayed_method_info mi;
4375 mi.type = type;
4376 mi.fnfield_index = fnfield_index;
4377 mi.index = index;
4378 mi.name = name;
4379 mi.die = die;
4380 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4381}
4382
4383/* A cleanup for freeing the delayed method list. */
4384
4385static void
4386free_delayed_list (void *ptr)
4387{
4388 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4389 if (cu->method_list != NULL)
4390 {
4391 VEC_free (delayed_method_info, cu->method_list);
4392 cu->method_list = NULL;
4393 }
4394}
4395
4396/* Compute the physnames of any methods on the CU's method list.
4397
4398 The computation of method physnames is delayed in order to avoid the
4399 (bad) condition that one of the method's formal parameters is of an as yet
4400 incomplete type. */
4401
4402static void
4403compute_delayed_physnames (struct dwarf2_cu *cu)
4404{
4405 int i;
4406 struct delayed_method_info *mi;
4407 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4408 {
4409 char *physname;
4410 struct fn_fieldlist *fn_flp
4411 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4412 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4413 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4414 }
4415}
4416
10b3939b
DJ
4417/* Generate full symbol information for PST and CU, whose DIEs have
4418 already been loaded into memory. */
4419
4420static void
4421process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4422{
10b3939b 4423 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4424 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4425 CORE_ADDR lowpc, highpc;
4426 struct symtab *symtab;
3da10d80 4427 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4428 CORE_ADDR baseaddr;
4429
4430 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4431
10b3939b
DJ
4432 buildsym_init ();
4433 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4434 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4435
4436 cu->list_in_scope = &file_symbols;
c906108c 4437
d85a05f0 4438 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4439
c906108c 4440 /* Do line number decoding in read_file_scope () */
10b3939b 4441 process_die (cu->dies, cu);
c906108c 4442
3da10d80
KS
4443 /* Now that we have processed all the DIEs in the CU, all the types
4444 should be complete, and it should now be safe to compute all of the
4445 physnames. */
4446 compute_delayed_physnames (cu);
4447 do_cleanups (delayed_list_cleanup);
4448
fae299cd
DC
4449 /* Some compilers don't define a DW_AT_high_pc attribute for the
4450 compilation unit. If the DW_AT_high_pc is missing, synthesize
4451 it, by scanning the DIE's below the compilation unit. */
10b3939b 4452 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4453
613e1657 4454 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4455
4456 /* Set symtab language to language from DW_AT_language.
4457 If the compilation is from a C file generated by language preprocessors,
4458 do not set the language if it was already deduced by start_subfile. */
4459 if (symtab != NULL
10b3939b 4460 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4461 {
10b3939b 4462 symtab->language = cu->language;
c906108c 4463 }
9291a0cd
TT
4464
4465 if (dwarf2_per_objfile->using_index)
4466 per_cu->v.quick->symtab = symtab;
4467 else
4468 {
4469 struct partial_symtab *pst = per_cu->v.psymtab;
4470 pst->symtab = symtab;
4471 pst->readin = 1;
4472 }
c906108c
SS
4473
4474 do_cleanups (back_to);
4475}
4476
4477/* Process a die and its children. */
4478
4479static void
e7c27a73 4480process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4481{
4482 switch (die->tag)
4483 {
4484 case DW_TAG_padding:
4485 break;
4486 case DW_TAG_compile_unit:
e7c27a73 4487 read_file_scope (die, cu);
c906108c 4488 break;
348e048f
DE
4489 case DW_TAG_type_unit:
4490 read_type_unit_scope (die, cu);
4491 break;
c906108c 4492 case DW_TAG_subprogram:
c906108c 4493 case DW_TAG_inlined_subroutine:
edb3359d 4494 read_func_scope (die, cu);
c906108c
SS
4495 break;
4496 case DW_TAG_lexical_block:
14898363
L
4497 case DW_TAG_try_block:
4498 case DW_TAG_catch_block:
e7c27a73 4499 read_lexical_block_scope (die, cu);
c906108c
SS
4500 break;
4501 case DW_TAG_class_type:
680b30c7 4502 case DW_TAG_interface_type:
c906108c
SS
4503 case DW_TAG_structure_type:
4504 case DW_TAG_union_type:
134d01f1 4505 process_structure_scope (die, cu);
c906108c
SS
4506 break;
4507 case DW_TAG_enumeration_type:
134d01f1 4508 process_enumeration_scope (die, cu);
c906108c 4509 break;
134d01f1 4510
f792889a
DJ
4511 /* These dies have a type, but processing them does not create
4512 a symbol or recurse to process the children. Therefore we can
4513 read them on-demand through read_type_die. */
c906108c 4514 case DW_TAG_subroutine_type:
72019c9c 4515 case DW_TAG_set_type:
c906108c 4516 case DW_TAG_array_type:
c906108c 4517 case DW_TAG_pointer_type:
c906108c 4518 case DW_TAG_ptr_to_member_type:
c906108c 4519 case DW_TAG_reference_type:
c906108c 4520 case DW_TAG_string_type:
c906108c 4521 break;
134d01f1 4522
c906108c 4523 case DW_TAG_base_type:
a02abb62 4524 case DW_TAG_subrange_type:
cb249c71 4525 case DW_TAG_typedef:
134d01f1
DJ
4526 /* Add a typedef symbol for the type definition, if it has a
4527 DW_AT_name. */
f792889a 4528 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4529 break;
c906108c 4530 case DW_TAG_common_block:
e7c27a73 4531 read_common_block (die, cu);
c906108c
SS
4532 break;
4533 case DW_TAG_common_inclusion:
4534 break;
d9fa45fe 4535 case DW_TAG_namespace:
63d06c5c 4536 processing_has_namespace_info = 1;
e7c27a73 4537 read_namespace (die, cu);
d9fa45fe 4538 break;
5d7cb8df 4539 case DW_TAG_module:
f55ee35c 4540 processing_has_namespace_info = 1;
5d7cb8df
JK
4541 read_module (die, cu);
4542 break;
d9fa45fe
DC
4543 case DW_TAG_imported_declaration:
4544 case DW_TAG_imported_module:
63d06c5c 4545 processing_has_namespace_info = 1;
27aa8d6a
SW
4546 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4547 || cu->language != language_fortran))
4548 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4549 dwarf_tag_name (die->tag));
4550 read_import_statement (die, cu);
d9fa45fe 4551 break;
c906108c 4552 default:
e7c27a73 4553 new_symbol (die, NULL, cu);
c906108c
SS
4554 break;
4555 }
4556}
4557
94af9270
KS
4558/* A helper function for dwarf2_compute_name which determines whether DIE
4559 needs to have the name of the scope prepended to the name listed in the
4560 die. */
4561
4562static int
4563die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4564{
1c809c68
TT
4565 struct attribute *attr;
4566
94af9270
KS
4567 switch (die->tag)
4568 {
4569 case DW_TAG_namespace:
4570 case DW_TAG_typedef:
4571 case DW_TAG_class_type:
4572 case DW_TAG_interface_type:
4573 case DW_TAG_structure_type:
4574 case DW_TAG_union_type:
4575 case DW_TAG_enumeration_type:
4576 case DW_TAG_enumerator:
4577 case DW_TAG_subprogram:
4578 case DW_TAG_member:
4579 return 1;
4580
4581 case DW_TAG_variable:
c2b0a229 4582 case DW_TAG_constant:
94af9270
KS
4583 /* We only need to prefix "globally" visible variables. These include
4584 any variable marked with DW_AT_external or any variable that
4585 lives in a namespace. [Variables in anonymous namespaces
4586 require prefixing, but they are not DW_AT_external.] */
4587
4588 if (dwarf2_attr (die, DW_AT_specification, cu))
4589 {
4590 struct dwarf2_cu *spec_cu = cu;
9a619af0 4591
94af9270
KS
4592 return die_needs_namespace (die_specification (die, &spec_cu),
4593 spec_cu);
4594 }
4595
1c809c68 4596 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4597 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4598 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4599 return 0;
4600 /* A variable in a lexical block of some kind does not need a
4601 namespace, even though in C++ such variables may be external
4602 and have a mangled name. */
4603 if (die->parent->tag == DW_TAG_lexical_block
4604 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4605 || die->parent->tag == DW_TAG_catch_block
4606 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4607 return 0;
4608 return 1;
94af9270
KS
4609
4610 default:
4611 return 0;
4612 }
4613}
4614
98bfdba5
PA
4615/* Retrieve the last character from a mem_file. */
4616
4617static void
4618do_ui_file_peek_last (void *object, const char *buffer, long length)
4619{
4620 char *last_char_p = (char *) object;
4621
4622 if (length > 0)
4623 *last_char_p = buffer[length - 1];
4624}
4625
94af9270
KS
4626/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4627 compute the physname for the object, which include a method's
4628 formal parameters (C++/Java) and return type (Java).
4629
af6b7be1
JB
4630 For Ada, return the DIE's linkage name rather than the fully qualified
4631 name. PHYSNAME is ignored..
4632
94af9270
KS
4633 The result is allocated on the objfile_obstack and canonicalized. */
4634
4635static const char *
4636dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4637 int physname)
4638{
4639 if (name == NULL)
4640 name = dwarf2_name (die, cu);
4641
f55ee35c
JK
4642 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4643 compute it by typename_concat inside GDB. */
4644 if (cu->language == language_ada
4645 || (cu->language == language_fortran && physname))
4646 {
4647 /* For Ada unit, we prefer the linkage name over the name, as
4648 the former contains the exported name, which the user expects
4649 to be able to reference. Ideally, we want the user to be able
4650 to reference this entity using either natural or linkage name,
4651 but we haven't started looking at this enhancement yet. */
4652 struct attribute *attr;
4653
4654 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4655 if (attr == NULL)
4656 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4657 if (attr && DW_STRING (attr))
4658 return DW_STRING (attr);
4659 }
4660
94af9270
KS
4661 /* These are the only languages we know how to qualify names in. */
4662 if (name != NULL
f55ee35c
JK
4663 && (cu->language == language_cplus || cu->language == language_java
4664 || cu->language == language_fortran))
94af9270
KS
4665 {
4666 if (die_needs_namespace (die, cu))
4667 {
4668 long length;
4669 char *prefix;
4670 struct ui_file *buf;
4671
4672 prefix = determine_prefix (die, cu);
4673 buf = mem_fileopen ();
4674 if (*prefix != '\0')
4675 {
f55ee35c
JK
4676 char *prefixed_name = typename_concat (NULL, prefix, name,
4677 physname, cu);
9a619af0 4678
94af9270
KS
4679 fputs_unfiltered (prefixed_name, buf);
4680 xfree (prefixed_name);
4681 }
4682 else
4683 fputs_unfiltered (name ? name : "", buf);
4684
98bfdba5
PA
4685 /* Template parameters may be specified in the DIE's DW_AT_name, or
4686 as children with DW_TAG_template_type_param or
4687 DW_TAG_value_type_param. If the latter, add them to the name
4688 here. If the name already has template parameters, then
4689 skip this step; some versions of GCC emit both, and
4690 it is more efficient to use the pre-computed name.
4691
4692 Something to keep in mind about this process: it is very
4693 unlikely, or in some cases downright impossible, to produce
4694 something that will match the mangled name of a function.
4695 If the definition of the function has the same debug info,
4696 we should be able to match up with it anyway. But fallbacks
4697 using the minimal symbol, for instance to find a method
4698 implemented in a stripped copy of libstdc++, will not work.
4699 If we do not have debug info for the definition, we will have to
4700 match them up some other way.
4701
4702 When we do name matching there is a related problem with function
4703 templates; two instantiated function templates are allowed to
4704 differ only by their return types, which we do not add here. */
4705
4706 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4707 {
4708 struct attribute *attr;
4709 struct die_info *child;
4710 int first = 1;
4711
4712 die->building_fullname = 1;
4713
4714 for (child = die->child; child != NULL; child = child->sibling)
4715 {
4716 struct type *type;
4717 long value;
4718 gdb_byte *bytes;
4719 struct dwarf2_locexpr_baton *baton;
4720 struct value *v;
4721
4722 if (child->tag != DW_TAG_template_type_param
4723 && child->tag != DW_TAG_template_value_param)
4724 continue;
4725
4726 if (first)
4727 {
4728 fputs_unfiltered ("<", buf);
4729 first = 0;
4730 }
4731 else
4732 fputs_unfiltered (", ", buf);
4733
4734 attr = dwarf2_attr (child, DW_AT_type, cu);
4735 if (attr == NULL)
4736 {
4737 complaint (&symfile_complaints,
4738 _("template parameter missing DW_AT_type"));
4739 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4740 continue;
4741 }
4742 type = die_type (child, cu);
4743
4744 if (child->tag == DW_TAG_template_type_param)
4745 {
4746 c_print_type (type, "", buf, -1, 0);
4747 continue;
4748 }
4749
4750 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4751 if (attr == NULL)
4752 {
4753 complaint (&symfile_complaints,
4754 _("template parameter missing DW_AT_const_value"));
4755 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4756 continue;
4757 }
4758
4759 dwarf2_const_value_attr (attr, type, name,
4760 &cu->comp_unit_obstack, cu,
4761 &value, &bytes, &baton);
4762
4763 if (TYPE_NOSIGN (type))
4764 /* GDB prints characters as NUMBER 'CHAR'. If that's
4765 changed, this can use value_print instead. */
4766 c_printchar (value, type, buf);
4767 else
4768 {
4769 struct value_print_options opts;
4770
4771 if (baton != NULL)
4772 v = dwarf2_evaluate_loc_desc (type, NULL,
4773 baton->data,
4774 baton->size,
4775 baton->per_cu);
4776 else if (bytes != NULL)
4777 {
4778 v = allocate_value (type);
4779 memcpy (value_contents_writeable (v), bytes,
4780 TYPE_LENGTH (type));
4781 }
4782 else
4783 v = value_from_longest (type, value);
4784
4785 /* Specify decimal so that we do not depend on the radix. */
4786 get_formatted_print_options (&opts, 'd');
4787 opts.raw = 1;
4788 value_print (v, buf, &opts);
4789 release_value (v);
4790 value_free (v);
4791 }
4792 }
4793
4794 die->building_fullname = 0;
4795
4796 if (!first)
4797 {
4798 /* Close the argument list, with a space if necessary
4799 (nested templates). */
4800 char last_char = '\0';
4801 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4802 if (last_char == '>')
4803 fputs_unfiltered (" >", buf);
4804 else
4805 fputs_unfiltered (">", buf);
4806 }
4807 }
4808
94af9270
KS
4809 /* For Java and C++ methods, append formal parameter type
4810 information, if PHYSNAME. */
6e70227d 4811
94af9270
KS
4812 if (physname && die->tag == DW_TAG_subprogram
4813 && (cu->language == language_cplus
4814 || cu->language == language_java))
4815 {
4816 struct type *type = read_type_die (die, cu);
4817
4818 c_type_print_args (type, buf, 0, cu->language);
4819
4820 if (cu->language == language_java)
4821 {
4822 /* For java, we must append the return type to method
4823 names. */
4824 if (die->tag == DW_TAG_subprogram)
4825 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4826 0, 0);
4827 }
4828 else if (cu->language == language_cplus)
4829 {
60430eff
DJ
4830 /* Assume that an artificial first parameter is
4831 "this", but do not crash if it is not. RealView
4832 marks unnamed (and thus unused) parameters as
4833 artificial; there is no way to differentiate
4834 the two cases. */
94af9270
KS
4835 if (TYPE_NFIELDS (type) > 0
4836 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4837 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4838 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4839 fputs_unfiltered (" const", buf);
4840 }
4841 }
4842
4843 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4844 &length);
4845 ui_file_delete (buf);
4846
4847 if (cu->language == language_cplus)
4848 {
4849 char *cname
4850 = dwarf2_canonicalize_name (name, cu,
4851 &cu->objfile->objfile_obstack);
9a619af0 4852
94af9270
KS
4853 if (cname != NULL)
4854 name = cname;
4855 }
4856 }
4857 }
4858
4859 return name;
4860}
4861
0114d602
DJ
4862/* Return the fully qualified name of DIE, based on its DW_AT_name.
4863 If scope qualifiers are appropriate they will be added. The result
4864 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4865 not have a name. NAME may either be from a previous call to
4866 dwarf2_name or NULL.
4867
4868 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4869
4870static const char *
94af9270 4871dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4872{
94af9270
KS
4873 return dwarf2_compute_name (name, die, cu, 0);
4874}
0114d602 4875
94af9270
KS
4876/* Construct a physname for the given DIE in CU. NAME may either be
4877 from a previous call to dwarf2_name or NULL. The result will be
4878 allocated on the objfile_objstack or NULL if the DIE does not have a
4879 name.
0114d602 4880
94af9270 4881 The output string will be canonicalized (if C++/Java). */
0114d602 4882
94af9270
KS
4883static const char *
4884dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4885{
4886 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4887}
4888
27aa8d6a
SW
4889/* Read the import statement specified by the given die and record it. */
4890
4891static void
4892read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4893{
4894 struct attribute *import_attr;
4895 struct die_info *imported_die;
de4affc9 4896 struct dwarf2_cu *imported_cu;
27aa8d6a 4897 const char *imported_name;
794684b6 4898 const char *imported_name_prefix;
13387711
SW
4899 const char *canonical_name;
4900 const char *import_alias;
4901 const char *imported_declaration = NULL;
794684b6 4902 const char *import_prefix;
13387711
SW
4903
4904 char *temp;
27aa8d6a
SW
4905
4906 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4907 if (import_attr == NULL)
4908 {
4909 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4910 dwarf_tag_name (die->tag));
4911 return;
4912 }
4913
de4affc9
CC
4914 imported_cu = cu;
4915 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4916 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4917 if (imported_name == NULL)
4918 {
4919 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4920
4921 The import in the following code:
4922 namespace A
4923 {
4924 typedef int B;
4925 }
4926
4927 int main ()
4928 {
4929 using A::B;
4930 B b;
4931 return b;
4932 }
4933
4934 ...
4935 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4936 <52> DW_AT_decl_file : 1
4937 <53> DW_AT_decl_line : 6
4938 <54> DW_AT_import : <0x75>
4939 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4940 <59> DW_AT_name : B
4941 <5b> DW_AT_decl_file : 1
4942 <5c> DW_AT_decl_line : 2
4943 <5d> DW_AT_type : <0x6e>
4944 ...
4945 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4946 <76> DW_AT_byte_size : 4
4947 <77> DW_AT_encoding : 5 (signed)
4948
4949 imports the wrong die ( 0x75 instead of 0x58 ).
4950 This case will be ignored until the gcc bug is fixed. */
4951 return;
4952 }
4953
82856980
SW
4954 /* Figure out the local name after import. */
4955 import_alias = dwarf2_name (die, cu);
27aa8d6a 4956
794684b6
SW
4957 /* Figure out where the statement is being imported to. */
4958 import_prefix = determine_prefix (die, cu);
4959
4960 /* Figure out what the scope of the imported die is and prepend it
4961 to the name of the imported die. */
de4affc9 4962 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4963
f55ee35c
JK
4964 if (imported_die->tag != DW_TAG_namespace
4965 && imported_die->tag != DW_TAG_module)
794684b6 4966 {
13387711
SW
4967 imported_declaration = imported_name;
4968 canonical_name = imported_name_prefix;
794684b6 4969 }
13387711 4970 else if (strlen (imported_name_prefix) > 0)
794684b6 4971 {
13387711
SW
4972 temp = alloca (strlen (imported_name_prefix)
4973 + 2 + strlen (imported_name) + 1);
4974 strcpy (temp, imported_name_prefix);
4975 strcat (temp, "::");
4976 strcat (temp, imported_name);
4977 canonical_name = temp;
794684b6 4978 }
13387711
SW
4979 else
4980 canonical_name = imported_name;
794684b6 4981
c0cc3a76
SW
4982 cp_add_using_directive (import_prefix,
4983 canonical_name,
4984 import_alias,
13387711 4985 imported_declaration,
c0cc3a76 4986 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4987}
4988
5fb290d7 4989static void
e142c38c 4990initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4991{
e142c38c 4992 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4993}
4994
cb1df416
DJ
4995static void
4996free_cu_line_header (void *arg)
4997{
4998 struct dwarf2_cu *cu = arg;
4999
5000 free_line_header (cu->line_header);
5001 cu->line_header = NULL;
5002}
5003
9291a0cd
TT
5004static void
5005find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5006 char **name, char **comp_dir)
5007{
5008 struct attribute *attr;
5009
5010 *name = NULL;
5011 *comp_dir = NULL;
5012
5013 /* Find the filename. Do not use dwarf2_name here, since the filename
5014 is not a source language identifier. */
5015 attr = dwarf2_attr (die, DW_AT_name, cu);
5016 if (attr)
5017 {
5018 *name = DW_STRING (attr);
5019 }
5020
5021 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5022 if (attr)
5023 *comp_dir = DW_STRING (attr);
5024 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5025 {
5026 *comp_dir = ldirname (*name);
5027 if (*comp_dir != NULL)
5028 make_cleanup (xfree, *comp_dir);
5029 }
5030 if (*comp_dir != NULL)
5031 {
5032 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5033 directory, get rid of it. */
5034 char *cp = strchr (*comp_dir, ':');
5035
5036 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5037 *comp_dir = cp + 1;
5038 }
5039
5040 if (*name == NULL)
5041 *name = "<unknown>";
5042}
5043
c906108c 5044static void
e7c27a73 5045read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5046{
e7c27a73 5047 struct objfile *objfile = cu->objfile;
debd256d 5048 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5049 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5050 CORE_ADDR highpc = ((CORE_ADDR) 0);
5051 struct attribute *attr;
e1024ff1 5052 char *name = NULL;
c906108c
SS
5053 char *comp_dir = NULL;
5054 struct die_info *child_die;
5055 bfd *abfd = objfile->obfd;
debd256d 5056 struct line_header *line_header = 0;
e142c38c 5057 CORE_ADDR baseaddr;
6e70227d 5058
e142c38c 5059 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5060
fae299cd 5061 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5062
5063 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5064 from finish_block. */
2acceee2 5065 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5066 lowpc = highpc;
5067 lowpc += baseaddr;
5068 highpc += baseaddr;
5069
9291a0cd 5070 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5071
e142c38c 5072 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5073 if (attr)
5074 {
e142c38c 5075 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5076 }
5077
b0f35d58 5078 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5079 if (attr)
b0f35d58 5080 cu->producer = DW_STRING (attr);
303b6f5d 5081
f4b8a18d
KW
5082 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5083 standardised yet. As a workaround for the language detection we fall
5084 back to the DW_AT_producer string. */
5085 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5086 cu->language = language_opencl;
5087
c906108c
SS
5088 /* We assume that we're processing GCC output. */
5089 processing_gcc_compilation = 2;
c906108c 5090
df8a16a1
DJ
5091 processing_has_namespace_info = 0;
5092
c906108c
SS
5093 start_symtab (name, comp_dir, lowpc);
5094 record_debugformat ("DWARF 2");
303b6f5d 5095 record_producer (cu->producer);
c906108c 5096
e142c38c 5097 initialize_cu_func_list (cu);
c906108c 5098
cb1df416
DJ
5099 /* Decode line number information if present. We do this before
5100 processing child DIEs, so that the line header table is available
5101 for DW_AT_decl_file. */
e142c38c 5102 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5103 if (attr)
5104 {
debd256d 5105 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5106 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5107 if (line_header)
5108 {
cb1df416
DJ
5109 cu->line_header = line_header;
5110 make_cleanup (free_cu_line_header, cu);
aaa75496 5111 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5112 }
5fb290d7 5113 }
debd256d 5114
cb1df416
DJ
5115 /* Process all dies in compilation unit. */
5116 if (die->child != NULL)
5117 {
5118 child_die = die->child;
5119 while (child_die && child_die->tag)
5120 {
5121 process_die (child_die, cu);
5122 child_die = sibling_die (child_die);
5123 }
5124 }
5125
2e276125
JB
5126 /* Decode macro information, if present. Dwarf 2 macro information
5127 refers to information in the line number info statement program
5128 header, so we can only read it if we've read the header
5129 successfully. */
e142c38c 5130 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5131 if (attr && line_header)
2e276125
JB
5132 {
5133 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5134
2e276125 5135 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5136 comp_dir, abfd, cu);
2e276125 5137 }
debd256d 5138 do_cleanups (back_to);
5fb290d7
DJ
5139}
5140
348e048f
DE
5141/* For TUs we want to skip the first top level sibling if it's not the
5142 actual type being defined by this TU. In this case the first top
5143 level sibling is there to provide context only. */
5144
5145static void
5146read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5147{
5148 struct objfile *objfile = cu->objfile;
5149 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5150 CORE_ADDR lowpc;
5151 struct attribute *attr;
5152 char *name = NULL;
5153 char *comp_dir = NULL;
5154 struct die_info *child_die;
5155 bfd *abfd = objfile->obfd;
348e048f
DE
5156
5157 /* start_symtab needs a low pc, but we don't really have one.
5158 Do what read_file_scope would do in the absence of such info. */
5159 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5160
5161 /* Find the filename. Do not use dwarf2_name here, since the filename
5162 is not a source language identifier. */
5163 attr = dwarf2_attr (die, DW_AT_name, cu);
5164 if (attr)
5165 name = DW_STRING (attr);
5166
5167 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5168 if (attr)
5169 comp_dir = DW_STRING (attr);
5170 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5171 {
5172 comp_dir = ldirname (name);
5173 if (comp_dir != NULL)
5174 make_cleanup (xfree, comp_dir);
5175 }
5176
5177 if (name == NULL)
5178 name = "<unknown>";
5179
5180 attr = dwarf2_attr (die, DW_AT_language, cu);
5181 if (attr)
5182 set_cu_language (DW_UNSND (attr), cu);
5183
5184 /* This isn't technically needed today. It is done for symmetry
5185 with read_file_scope. */
5186 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5187 if (attr)
348e048f
DE
5188 cu->producer = DW_STRING (attr);
5189
5190 /* We assume that we're processing GCC output. */
5191 processing_gcc_compilation = 2;
5192
5193 processing_has_namespace_info = 0;
5194
5195 start_symtab (name, comp_dir, lowpc);
5196 record_debugformat ("DWARF 2");
5197 record_producer (cu->producer);
5198
5199 /* Process the dies in the type unit. */
5200 if (die->child == NULL)
5201 {
5202 dump_die_for_error (die);
5203 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5204 bfd_get_filename (abfd));
5205 }
5206
5207 child_die = die->child;
5208
5209 while (child_die && child_die->tag)
5210 {
5211 process_die (child_die, cu);
5212
5213 child_die = sibling_die (child_die);
5214 }
5215
5216 do_cleanups (back_to);
5217}
5218
5fb290d7 5219static void
e142c38c
DJ
5220add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5221 struct dwarf2_cu *cu)
5fb290d7
DJ
5222{
5223 struct function_range *thisfn;
5224
5225 thisfn = (struct function_range *)
7b5a2f43 5226 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5227 thisfn->name = name;
5228 thisfn->lowpc = lowpc;
5229 thisfn->highpc = highpc;
5230 thisfn->seen_line = 0;
5231 thisfn->next = NULL;
5232
e142c38c
DJ
5233 if (cu->last_fn == NULL)
5234 cu->first_fn = thisfn;
5fb290d7 5235 else
e142c38c 5236 cu->last_fn->next = thisfn;
5fb290d7 5237
e142c38c 5238 cu->last_fn = thisfn;
c906108c
SS
5239}
5240
d389af10
JK
5241/* qsort helper for inherit_abstract_dies. */
5242
5243static int
5244unsigned_int_compar (const void *ap, const void *bp)
5245{
5246 unsigned int a = *(unsigned int *) ap;
5247 unsigned int b = *(unsigned int *) bp;
5248
5249 return (a > b) - (b > a);
5250}
5251
5252/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5253 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5254 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5255
5256static void
5257inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5258{
5259 struct die_info *child_die;
5260 unsigned die_children_count;
5261 /* CU offsets which were referenced by children of the current DIE. */
5262 unsigned *offsets;
5263 unsigned *offsets_end, *offsetp;
5264 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5265 struct die_info *origin_die;
5266 /* Iterator of the ORIGIN_DIE children. */
5267 struct die_info *origin_child_die;
5268 struct cleanup *cleanups;
5269 struct attribute *attr;
cd02d79d
PA
5270 struct dwarf2_cu *origin_cu;
5271 struct pending **origin_previous_list_in_scope;
d389af10
JK
5272
5273 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5274 if (!attr)
5275 return;
5276
cd02d79d
PA
5277 /* Note that following die references may follow to a die in a
5278 different cu. */
5279
5280 origin_cu = cu;
5281 origin_die = follow_die_ref (die, attr, &origin_cu);
5282
5283 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5284 symbols in. */
5285 origin_previous_list_in_scope = origin_cu->list_in_scope;
5286 origin_cu->list_in_scope = cu->list_in_scope;
5287
edb3359d
DJ
5288 if (die->tag != origin_die->tag
5289 && !(die->tag == DW_TAG_inlined_subroutine
5290 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5291 complaint (&symfile_complaints,
5292 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5293 die->offset, origin_die->offset);
5294
5295 child_die = die->child;
5296 die_children_count = 0;
5297 while (child_die && child_die->tag)
5298 {
5299 child_die = sibling_die (child_die);
5300 die_children_count++;
5301 }
5302 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5303 cleanups = make_cleanup (xfree, offsets);
5304
5305 offsets_end = offsets;
5306 child_die = die->child;
5307 while (child_die && child_die->tag)
5308 {
c38f313d
DJ
5309 /* For each CHILD_DIE, find the corresponding child of
5310 ORIGIN_DIE. If there is more than one layer of
5311 DW_AT_abstract_origin, follow them all; there shouldn't be,
5312 but GCC versions at least through 4.4 generate this (GCC PR
5313 40573). */
5314 struct die_info *child_origin_die = child_die;
cd02d79d 5315 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5316
c38f313d
DJ
5317 while (1)
5318 {
cd02d79d
PA
5319 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5320 child_origin_cu);
c38f313d
DJ
5321 if (attr == NULL)
5322 break;
cd02d79d
PA
5323 child_origin_die = follow_die_ref (child_origin_die, attr,
5324 &child_origin_cu);
c38f313d
DJ
5325 }
5326
d389af10
JK
5327 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5328 counterpart may exist. */
c38f313d 5329 if (child_origin_die != child_die)
d389af10 5330 {
edb3359d
DJ
5331 if (child_die->tag != child_origin_die->tag
5332 && !(child_die->tag == DW_TAG_inlined_subroutine
5333 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5334 complaint (&symfile_complaints,
5335 _("Child DIE 0x%x and its abstract origin 0x%x have "
5336 "different tags"), child_die->offset,
5337 child_origin_die->offset);
c38f313d
DJ
5338 if (child_origin_die->parent != origin_die)
5339 complaint (&symfile_complaints,
5340 _("Child DIE 0x%x and its abstract origin 0x%x have "
5341 "different parents"), child_die->offset,
5342 child_origin_die->offset);
5343 else
5344 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5345 }
5346 child_die = sibling_die (child_die);
5347 }
5348 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5349 unsigned_int_compar);
5350 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5351 if (offsetp[-1] == *offsetp)
5352 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5353 "to DIE 0x%x as their abstract origin"),
5354 die->offset, *offsetp);
5355
5356 offsetp = offsets;
5357 origin_child_die = origin_die->child;
5358 while (origin_child_die && origin_child_die->tag)
5359 {
5360 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5361 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5362 offsetp++;
5363 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5364 {
5365 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5366 process_die (origin_child_die, origin_cu);
d389af10
JK
5367 }
5368 origin_child_die = sibling_die (origin_child_die);
5369 }
cd02d79d 5370 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5371
5372 do_cleanups (cleanups);
5373}
5374
c906108c 5375static void
e7c27a73 5376read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5377{
e7c27a73 5378 struct objfile *objfile = cu->objfile;
52f0bd74 5379 struct context_stack *new;
c906108c
SS
5380 CORE_ADDR lowpc;
5381 CORE_ADDR highpc;
5382 struct die_info *child_die;
edb3359d 5383 struct attribute *attr, *call_line, *call_file;
c906108c 5384 char *name;
e142c38c 5385 CORE_ADDR baseaddr;
801e3a5b 5386 struct block *block;
edb3359d 5387 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5388 VEC (symbolp) *template_args = NULL;
5389 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5390
5391 if (inlined_func)
5392 {
5393 /* If we do not have call site information, we can't show the
5394 caller of this inlined function. That's too confusing, so
5395 only use the scope for local variables. */
5396 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5397 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5398 if (call_line == NULL || call_file == NULL)
5399 {
5400 read_lexical_block_scope (die, cu);
5401 return;
5402 }
5403 }
c906108c 5404
e142c38c
DJ
5405 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5406
94af9270 5407 name = dwarf2_name (die, cu);
c906108c 5408
e8d05480
JB
5409 /* Ignore functions with missing or empty names. These are actually
5410 illegal according to the DWARF standard. */
5411 if (name == NULL)
5412 {
5413 complaint (&symfile_complaints,
5414 _("missing name for subprogram DIE at %d"), die->offset);
5415 return;
5416 }
5417
5418 /* Ignore functions with missing or invalid low and high pc attributes. */
5419 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5420 {
ae4d0c03
PM
5421 attr = dwarf2_attr (die, DW_AT_external, cu);
5422 if (!attr || !DW_UNSND (attr))
5423 complaint (&symfile_complaints,
5424 _("cannot get low and high bounds for subprogram DIE at %d"),
5425 die->offset);
e8d05480
JB
5426 return;
5427 }
c906108c
SS
5428
5429 lowpc += baseaddr;
5430 highpc += baseaddr;
5431
5fb290d7 5432 /* Record the function range for dwarf_decode_lines. */
e142c38c 5433 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5434
34eaf542
TT
5435 /* If we have any template arguments, then we must allocate a
5436 different sort of symbol. */
5437 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5438 {
5439 if (child_die->tag == DW_TAG_template_type_param
5440 || child_die->tag == DW_TAG_template_value_param)
5441 {
5442 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5443 struct template_symbol);
5444 templ_func->base.is_cplus_template_function = 1;
5445 break;
5446 }
5447 }
5448
c906108c 5449 new = push_context (0, lowpc);
34eaf542
TT
5450 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5451 (struct symbol *) templ_func);
4c2df51b 5452
4cecd739
DJ
5453 /* If there is a location expression for DW_AT_frame_base, record
5454 it. */
e142c38c 5455 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5456 if (attr)
c034e007
AC
5457 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5458 expression is being recorded directly in the function's symbol
5459 and not in a separate frame-base object. I guess this hack is
5460 to avoid adding some sort of frame-base adjunct/annex to the
5461 function's symbol :-(. The problem with doing this is that it
5462 results in a function symbol with a location expression that
5463 has nothing to do with the location of the function, ouch! The
5464 relationship should be: a function's symbol has-a frame base; a
5465 frame-base has-a location expression. */
e7c27a73 5466 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5467
e142c38c 5468 cu->list_in_scope = &local_symbols;
c906108c 5469
639d11d3 5470 if (die->child != NULL)
c906108c 5471 {
639d11d3 5472 child_die = die->child;
c906108c
SS
5473 while (child_die && child_die->tag)
5474 {
34eaf542
TT
5475 if (child_die->tag == DW_TAG_template_type_param
5476 || child_die->tag == DW_TAG_template_value_param)
5477 {
5478 struct symbol *arg = new_symbol (child_die, NULL, cu);
5479
f1078f66
DJ
5480 if (arg != NULL)
5481 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5482 }
5483 else
5484 process_die (child_die, cu);
c906108c
SS
5485 child_die = sibling_die (child_die);
5486 }
5487 }
5488
d389af10
JK
5489 inherit_abstract_dies (die, cu);
5490
4a811a97
UW
5491 /* If we have a DW_AT_specification, we might need to import using
5492 directives from the context of the specification DIE. See the
5493 comment in determine_prefix. */
5494 if (cu->language == language_cplus
5495 && dwarf2_attr (die, DW_AT_specification, cu))
5496 {
5497 struct dwarf2_cu *spec_cu = cu;
5498 struct die_info *spec_die = die_specification (die, &spec_cu);
5499
5500 while (spec_die)
5501 {
5502 child_die = spec_die->child;
5503 while (child_die && child_die->tag)
5504 {
5505 if (child_die->tag == DW_TAG_imported_module)
5506 process_die (child_die, spec_cu);
5507 child_die = sibling_die (child_die);
5508 }
5509
5510 /* In some cases, GCC generates specification DIEs that
5511 themselves contain DW_AT_specification attributes. */
5512 spec_die = die_specification (spec_die, &spec_cu);
5513 }
5514 }
5515
c906108c
SS
5516 new = pop_context ();
5517 /* Make a block for the local symbols within. */
801e3a5b
JB
5518 block = finish_block (new->name, &local_symbols, new->old_blocks,
5519 lowpc, highpc, objfile);
5520
df8a16a1 5521 /* For C++, set the block's scope. */
f55ee35c 5522 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5523 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5524 determine_prefix (die, cu),
df8a16a1
DJ
5525 processing_has_namespace_info);
5526
801e3a5b
JB
5527 /* If we have address ranges, record them. */
5528 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5529
34eaf542
TT
5530 /* Attach template arguments to function. */
5531 if (! VEC_empty (symbolp, template_args))
5532 {
5533 gdb_assert (templ_func != NULL);
5534
5535 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5536 templ_func->template_arguments
5537 = obstack_alloc (&objfile->objfile_obstack,
5538 (templ_func->n_template_arguments
5539 * sizeof (struct symbol *)));
5540 memcpy (templ_func->template_arguments,
5541 VEC_address (symbolp, template_args),
5542 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5543 VEC_free (symbolp, template_args);
5544 }
5545
208d8187
JB
5546 /* In C++, we can have functions nested inside functions (e.g., when
5547 a function declares a class that has methods). This means that
5548 when we finish processing a function scope, we may need to go
5549 back to building a containing block's symbol lists. */
5550 local_symbols = new->locals;
5551 param_symbols = new->params;
27aa8d6a 5552 using_directives = new->using_directives;
208d8187 5553
921e78cf
JB
5554 /* If we've finished processing a top-level function, subsequent
5555 symbols go in the file symbol list. */
5556 if (outermost_context_p ())
e142c38c 5557 cu->list_in_scope = &file_symbols;
c906108c
SS
5558}
5559
5560/* Process all the DIES contained within a lexical block scope. Start
5561 a new scope, process the dies, and then close the scope. */
5562
5563static void
e7c27a73 5564read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5565{
e7c27a73 5566 struct objfile *objfile = cu->objfile;
52f0bd74 5567 struct context_stack *new;
c906108c
SS
5568 CORE_ADDR lowpc, highpc;
5569 struct die_info *child_die;
e142c38c
DJ
5570 CORE_ADDR baseaddr;
5571
5572 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5573
5574 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5575 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5576 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5577 be nasty. Might be easier to properly extend generic blocks to
af34e669 5578 describe ranges. */
d85a05f0 5579 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5580 return;
5581 lowpc += baseaddr;
5582 highpc += baseaddr;
5583
5584 push_context (0, lowpc);
639d11d3 5585 if (die->child != NULL)
c906108c 5586 {
639d11d3 5587 child_die = die->child;
c906108c
SS
5588 while (child_die && child_die->tag)
5589 {
e7c27a73 5590 process_die (child_die, cu);
c906108c
SS
5591 child_die = sibling_die (child_die);
5592 }
5593 }
5594 new = pop_context ();
5595
8540c487 5596 if (local_symbols != NULL || using_directives != NULL)
c906108c 5597 {
801e3a5b
JB
5598 struct block *block
5599 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5600 highpc, objfile);
5601
5602 /* Note that recording ranges after traversing children, as we
5603 do here, means that recording a parent's ranges entails
5604 walking across all its children's ranges as they appear in
5605 the address map, which is quadratic behavior.
5606
5607 It would be nicer to record the parent's ranges before
5608 traversing its children, simply overriding whatever you find
5609 there. But since we don't even decide whether to create a
5610 block until after we've traversed its children, that's hard
5611 to do. */
5612 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5613 }
5614 local_symbols = new->locals;
27aa8d6a 5615 using_directives = new->using_directives;
c906108c
SS
5616}
5617
43039443 5618/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5619 Return 1 if the attributes are present and valid, otherwise, return 0.
5620 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5621
5622static int
5623dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5624 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5625 struct partial_symtab *ranges_pst)
43039443
JK
5626{
5627 struct objfile *objfile = cu->objfile;
5628 struct comp_unit_head *cu_header = &cu->header;
5629 bfd *obfd = objfile->obfd;
5630 unsigned int addr_size = cu_header->addr_size;
5631 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5632 /* Base address selection entry. */
5633 CORE_ADDR base;
5634 int found_base;
5635 unsigned int dummy;
5636 gdb_byte *buffer;
5637 CORE_ADDR marker;
5638 int low_set;
5639 CORE_ADDR low = 0;
5640 CORE_ADDR high = 0;
ff013f42 5641 CORE_ADDR baseaddr;
43039443 5642
d00adf39
DE
5643 found_base = cu->base_known;
5644 base = cu->base_address;
43039443 5645
be391dca 5646 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5647 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5648 {
5649 complaint (&symfile_complaints,
5650 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5651 offset);
5652 return 0;
5653 }
dce234bc 5654 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5655
5656 /* Read in the largest possible address. */
5657 marker = read_address (obfd, buffer, cu, &dummy);
5658 if ((marker & mask) == mask)
5659 {
5660 /* If we found the largest possible address, then
5661 read the base address. */
5662 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5663 buffer += 2 * addr_size;
5664 offset += 2 * addr_size;
5665 found_base = 1;
5666 }
5667
5668 low_set = 0;
5669
e7030f15 5670 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5671
43039443
JK
5672 while (1)
5673 {
5674 CORE_ADDR range_beginning, range_end;
5675
5676 range_beginning = read_address (obfd, buffer, cu, &dummy);
5677 buffer += addr_size;
5678 range_end = read_address (obfd, buffer, cu, &dummy);
5679 buffer += addr_size;
5680 offset += 2 * addr_size;
5681
5682 /* An end of list marker is a pair of zero addresses. */
5683 if (range_beginning == 0 && range_end == 0)
5684 /* Found the end of list entry. */
5685 break;
5686
5687 /* Each base address selection entry is a pair of 2 values.
5688 The first is the largest possible address, the second is
5689 the base address. Check for a base address here. */
5690 if ((range_beginning & mask) == mask)
5691 {
5692 /* If we found the largest possible address, then
5693 read the base address. */
5694 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5695 found_base = 1;
5696 continue;
5697 }
5698
5699 if (!found_base)
5700 {
5701 /* We have no valid base address for the ranges
5702 data. */
5703 complaint (&symfile_complaints,
5704 _("Invalid .debug_ranges data (no base address)"));
5705 return 0;
5706 }
5707
5708 range_beginning += base;
5709 range_end += base;
5710
ff013f42
JK
5711 if (ranges_pst != NULL && range_beginning < range_end)
5712 addrmap_set_empty (objfile->psymtabs_addrmap,
5713 range_beginning + baseaddr, range_end - 1 + baseaddr,
5714 ranges_pst);
5715
43039443
JK
5716 /* FIXME: This is recording everything as a low-high
5717 segment of consecutive addresses. We should have a
5718 data structure for discontiguous block ranges
5719 instead. */
5720 if (! low_set)
5721 {
5722 low = range_beginning;
5723 high = range_end;
5724 low_set = 1;
5725 }
5726 else
5727 {
5728 if (range_beginning < low)
5729 low = range_beginning;
5730 if (range_end > high)
5731 high = range_end;
5732 }
5733 }
5734
5735 if (! low_set)
5736 /* If the first entry is an end-of-list marker, the range
5737 describes an empty scope, i.e. no instructions. */
5738 return 0;
5739
5740 if (low_return)
5741 *low_return = low;
5742 if (high_return)
5743 *high_return = high;
5744 return 1;
5745}
5746
af34e669
DJ
5747/* Get low and high pc attributes from a die. Return 1 if the attributes
5748 are present and valid, otherwise, return 0. Return -1 if the range is
5749 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5750static int
af34e669 5751dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5752 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5753 struct partial_symtab *pst)
c906108c
SS
5754{
5755 struct attribute *attr;
af34e669
DJ
5756 CORE_ADDR low = 0;
5757 CORE_ADDR high = 0;
5758 int ret = 0;
c906108c 5759
e142c38c 5760 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5761 if (attr)
af34e669
DJ
5762 {
5763 high = DW_ADDR (attr);
e142c38c 5764 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5765 if (attr)
5766 low = DW_ADDR (attr);
5767 else
5768 /* Found high w/o low attribute. */
5769 return 0;
5770
5771 /* Found consecutive range of addresses. */
5772 ret = 1;
5773 }
c906108c 5774 else
af34e669 5775 {
e142c38c 5776 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5777 if (attr != NULL)
5778 {
af34e669 5779 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5780 .debug_ranges section. */
d85a05f0 5781 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5782 return 0;
43039443 5783 /* Found discontinuous range of addresses. */
af34e669
DJ
5784 ret = -1;
5785 }
5786 }
c906108c
SS
5787
5788 if (high < low)
5789 return 0;
5790
5791 /* When using the GNU linker, .gnu.linkonce. sections are used to
5792 eliminate duplicate copies of functions and vtables and such.
5793 The linker will arbitrarily choose one and discard the others.
5794 The AT_*_pc values for such functions refer to local labels in
5795 these sections. If the section from that file was discarded, the
5796 labels are not in the output, so the relocs get a value of 0.
5797 If this is a discarded function, mark the pc bounds as invalid,
5798 so that GDB will ignore it. */
72dca2f5 5799 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5800 return 0;
5801
5802 *lowpc = low;
5803 *highpc = high;
af34e669 5804 return ret;
c906108c
SS
5805}
5806
b084d499
JB
5807/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5808 its low and high PC addresses. Do nothing if these addresses could not
5809 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5810 and HIGHPC to the high address if greater than HIGHPC. */
5811
5812static void
5813dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5814 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5815 struct dwarf2_cu *cu)
5816{
5817 CORE_ADDR low, high;
5818 struct die_info *child = die->child;
5819
d85a05f0 5820 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5821 {
5822 *lowpc = min (*lowpc, low);
5823 *highpc = max (*highpc, high);
5824 }
5825
5826 /* If the language does not allow nested subprograms (either inside
5827 subprograms or lexical blocks), we're done. */
5828 if (cu->language != language_ada)
5829 return;
6e70227d 5830
b084d499
JB
5831 /* Check all the children of the given DIE. If it contains nested
5832 subprograms, then check their pc bounds. Likewise, we need to
5833 check lexical blocks as well, as they may also contain subprogram
5834 definitions. */
5835 while (child && child->tag)
5836 {
5837 if (child->tag == DW_TAG_subprogram
5838 || child->tag == DW_TAG_lexical_block)
5839 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5840 child = sibling_die (child);
5841 }
5842}
5843
fae299cd
DC
5844/* Get the low and high pc's represented by the scope DIE, and store
5845 them in *LOWPC and *HIGHPC. If the correct values can't be
5846 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5847
5848static void
5849get_scope_pc_bounds (struct die_info *die,
5850 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5851 struct dwarf2_cu *cu)
5852{
5853 CORE_ADDR best_low = (CORE_ADDR) -1;
5854 CORE_ADDR best_high = (CORE_ADDR) 0;
5855 CORE_ADDR current_low, current_high;
5856
d85a05f0 5857 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5858 {
5859 best_low = current_low;
5860 best_high = current_high;
5861 }
5862 else
5863 {
5864 struct die_info *child = die->child;
5865
5866 while (child && child->tag)
5867 {
5868 switch (child->tag) {
5869 case DW_TAG_subprogram:
b084d499 5870 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5871 break;
5872 case DW_TAG_namespace:
f55ee35c 5873 case DW_TAG_module:
fae299cd
DC
5874 /* FIXME: carlton/2004-01-16: Should we do this for
5875 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5876 that current GCC's always emit the DIEs corresponding
5877 to definitions of methods of classes as children of a
5878 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5879 the DIEs giving the declarations, which could be
5880 anywhere). But I don't see any reason why the
5881 standards says that they have to be there. */
5882 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5883
5884 if (current_low != ((CORE_ADDR) -1))
5885 {
5886 best_low = min (best_low, current_low);
5887 best_high = max (best_high, current_high);
5888 }
5889 break;
5890 default:
5891 /* Ignore. */
5892 break;
5893 }
5894
5895 child = sibling_die (child);
5896 }
5897 }
5898
5899 *lowpc = best_low;
5900 *highpc = best_high;
5901}
5902
801e3a5b
JB
5903/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5904 in DIE. */
5905static void
5906dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5907 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5908{
5909 struct attribute *attr;
5910
5911 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5912 if (attr)
5913 {
5914 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5915
801e3a5b
JB
5916 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5917 if (attr)
5918 {
5919 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5920
801e3a5b
JB
5921 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5922 }
5923 }
5924
5925 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5926 if (attr)
5927 {
5928 bfd *obfd = cu->objfile->obfd;
5929
5930 /* The value of the DW_AT_ranges attribute is the offset of the
5931 address range list in the .debug_ranges section. */
5932 unsigned long offset = DW_UNSND (attr);
dce234bc 5933 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5934
5935 /* For some target architectures, but not others, the
5936 read_address function sign-extends the addresses it returns.
5937 To recognize base address selection entries, we need a
5938 mask. */
5939 unsigned int addr_size = cu->header.addr_size;
5940 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5941
5942 /* The base address, to which the next pair is relative. Note
5943 that this 'base' is a DWARF concept: most entries in a range
5944 list are relative, to reduce the number of relocs against the
5945 debugging information. This is separate from this function's
5946 'baseaddr' argument, which GDB uses to relocate debugging
5947 information from a shared library based on the address at
5948 which the library was loaded. */
d00adf39
DE
5949 CORE_ADDR base = cu->base_address;
5950 int base_known = cu->base_known;
801e3a5b 5951
be391dca 5952 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5953 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5954 {
5955 complaint (&symfile_complaints,
5956 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5957 offset);
5958 return;
5959 }
5960
5961 for (;;)
5962 {
5963 unsigned int bytes_read;
5964 CORE_ADDR start, end;
5965
5966 start = read_address (obfd, buffer, cu, &bytes_read);
5967 buffer += bytes_read;
5968 end = read_address (obfd, buffer, cu, &bytes_read);
5969 buffer += bytes_read;
5970
5971 /* Did we find the end of the range list? */
5972 if (start == 0 && end == 0)
5973 break;
5974
5975 /* Did we find a base address selection entry? */
5976 else if ((start & base_select_mask) == base_select_mask)
5977 {
5978 base = end;
5979 base_known = 1;
5980 }
5981
5982 /* We found an ordinary address range. */
5983 else
5984 {
5985 if (!base_known)
5986 {
5987 complaint (&symfile_complaints,
5988 _("Invalid .debug_ranges data (no base address)"));
5989 return;
5990 }
5991
6e70227d
DE
5992 record_block_range (block,
5993 baseaddr + base + start,
801e3a5b
JB
5994 baseaddr + base + end - 1);
5995 }
5996 }
5997 }
5998}
5999
c906108c
SS
6000/* Add an aggregate field to the field list. */
6001
6002static void
107d2387 6003dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6004 struct dwarf2_cu *cu)
6e70227d 6005{
e7c27a73 6006 struct objfile *objfile = cu->objfile;
5e2b427d 6007 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6008 struct nextfield *new_field;
6009 struct attribute *attr;
6010 struct field *fp;
6011 char *fieldname = "";
6012
6013 /* Allocate a new field list entry and link it in. */
6014 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6015 make_cleanup (xfree, new_field);
c906108c 6016 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6017
6018 if (die->tag == DW_TAG_inheritance)
6019 {
6020 new_field->next = fip->baseclasses;
6021 fip->baseclasses = new_field;
6022 }
6023 else
6024 {
6025 new_field->next = fip->fields;
6026 fip->fields = new_field;
6027 }
c906108c
SS
6028 fip->nfields++;
6029
6030 /* Handle accessibility and virtuality of field.
6031 The default accessibility for members is public, the default
6032 accessibility for inheritance is private. */
6033 if (die->tag != DW_TAG_inheritance)
6034 new_field->accessibility = DW_ACCESS_public;
6035 else
6036 new_field->accessibility = DW_ACCESS_private;
6037 new_field->virtuality = DW_VIRTUALITY_none;
6038
e142c38c 6039 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6040 if (attr)
6041 new_field->accessibility = DW_UNSND (attr);
6042 if (new_field->accessibility != DW_ACCESS_public)
6043 fip->non_public_fields = 1;
e142c38c 6044 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6045 if (attr)
6046 new_field->virtuality = DW_UNSND (attr);
6047
6048 fp = &new_field->field;
a9a9bd0f 6049
e142c38c 6050 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6051 {
a9a9bd0f 6052 /* Data member other than a C++ static data member. */
6e70227d 6053
c906108c 6054 /* Get type of field. */
e7c27a73 6055 fp->type = die_type (die, cu);
c906108c 6056
d6a843b5 6057 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6058
c906108c 6059 /* Get bit size of field (zero if none). */
e142c38c 6060 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6061 if (attr)
6062 {
6063 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6064 }
6065 else
6066 {
6067 FIELD_BITSIZE (*fp) = 0;
6068 }
6069
6070 /* Get bit offset of field. */
e142c38c 6071 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6072 if (attr)
6073 {
d4b96c9a 6074 int byte_offset = 0;
c6a0999f 6075
3690dd37 6076 if (attr_form_is_section_offset (attr))
d4b96c9a 6077 dwarf2_complex_location_expr_complaint ();
3690dd37 6078 else if (attr_form_is_constant (attr))
c6a0999f 6079 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6080 else if (attr_form_is_block (attr))
c6a0999f 6081 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6082 else
6083 dwarf2_complex_location_expr_complaint ();
c6a0999f 6084
d6a843b5 6085 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6086 }
e142c38c 6087 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6088 if (attr)
6089 {
5e2b427d 6090 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6091 {
6092 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6093 additional bit offset from the MSB of the containing
6094 anonymous object to the MSB of the field. We don't
6095 have to do anything special since we don't need to
6096 know the size of the anonymous object. */
c906108c
SS
6097 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6098 }
6099 else
6100 {
6101 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6102 MSB of the anonymous object, subtract off the number of
6103 bits from the MSB of the field to the MSB of the
6104 object, and then subtract off the number of bits of
6105 the field itself. The result is the bit offset of
6106 the LSB of the field. */
c906108c
SS
6107 int anonymous_size;
6108 int bit_offset = DW_UNSND (attr);
6109
e142c38c 6110 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6111 if (attr)
6112 {
6113 /* The size of the anonymous object containing
6114 the bit field is explicit, so use the
6115 indicated size (in bytes). */
6116 anonymous_size = DW_UNSND (attr);
6117 }
6118 else
6119 {
6120 /* The size of the anonymous object containing
6121 the bit field must be inferred from the type
6122 attribute of the data member containing the
6123 bit field. */
6124 anonymous_size = TYPE_LENGTH (fp->type);
6125 }
6126 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6127 - bit_offset - FIELD_BITSIZE (*fp);
6128 }
6129 }
6130
6131 /* Get name of field. */
39cbfefa
DJ
6132 fieldname = dwarf2_name (die, cu);
6133 if (fieldname == NULL)
6134 fieldname = "";
d8151005
DJ
6135
6136 /* The name is already allocated along with this objfile, so we don't
6137 need to duplicate it for the type. */
6138 fp->name = fieldname;
c906108c
SS
6139
6140 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6141 pointer or virtual base class pointer) to private. */
e142c38c 6142 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6143 {
d48cc9dd 6144 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6145 new_field->accessibility = DW_ACCESS_private;
6146 fip->non_public_fields = 1;
6147 }
6148 }
a9a9bd0f 6149 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6150 {
a9a9bd0f
DC
6151 /* C++ static member. */
6152
6153 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6154 is a declaration, but all versions of G++ as of this writing
6155 (so through at least 3.2.1) incorrectly generate
6156 DW_TAG_variable tags. */
6e70227d 6157
c906108c 6158 char *physname;
c906108c 6159
a9a9bd0f 6160 /* Get name of field. */
39cbfefa
DJ
6161 fieldname = dwarf2_name (die, cu);
6162 if (fieldname == NULL)
c906108c
SS
6163 return;
6164
254e6b9e 6165 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6166 if (attr
6167 /* Only create a symbol if this is an external value.
6168 new_symbol checks this and puts the value in the global symbol
6169 table, which we want. If it is not external, new_symbol
6170 will try to put the value in cu->list_in_scope which is wrong. */
6171 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6172 {
6173 /* A static const member, not much different than an enum as far as
6174 we're concerned, except that we can support more types. */
6175 new_symbol (die, NULL, cu);
6176 }
6177
2df3850c 6178 /* Get physical name. */
94af9270 6179 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6180
d8151005
DJ
6181 /* The name is already allocated along with this objfile, so we don't
6182 need to duplicate it for the type. */
6183 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6184 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6185 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6186 }
6187 else if (die->tag == DW_TAG_inheritance)
6188 {
6189 /* C++ base class field. */
e142c38c 6190 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6191 if (attr)
d4b96c9a
JK
6192 {
6193 int byte_offset = 0;
6194
6195 if (attr_form_is_section_offset (attr))
6196 dwarf2_complex_location_expr_complaint ();
6197 else if (attr_form_is_constant (attr))
6198 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6199 else if (attr_form_is_block (attr))
6200 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6201 else
6202 dwarf2_complex_location_expr_complaint ();
6203
6204 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6205 }
c906108c 6206 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6207 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6208 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6209 fip->nbaseclasses++;
6210 }
6211}
6212
98751a41
JK
6213/* Add a typedef defined in the scope of the FIP's class. */
6214
6215static void
6216dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6217 struct dwarf2_cu *cu)
6e70227d 6218{
98751a41
JK
6219 struct objfile *objfile = cu->objfile;
6220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6221 struct typedef_field_list *new_field;
6222 struct attribute *attr;
6223 struct typedef_field *fp;
6224 char *fieldname = "";
6225
6226 /* Allocate a new field list entry and link it in. */
6227 new_field = xzalloc (sizeof (*new_field));
6228 make_cleanup (xfree, new_field);
6229
6230 gdb_assert (die->tag == DW_TAG_typedef);
6231
6232 fp = &new_field->field;
6233
6234 /* Get name of field. */
6235 fp->name = dwarf2_name (die, cu);
6236 if (fp->name == NULL)
6237 return;
6238
6239 fp->type = read_type_die (die, cu);
6240
6241 new_field->next = fip->typedef_field_list;
6242 fip->typedef_field_list = new_field;
6243 fip->typedef_field_list_count++;
6244}
6245
c906108c
SS
6246/* Create the vector of fields, and attach it to the type. */
6247
6248static void
fba45db2 6249dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6250 struct dwarf2_cu *cu)
c906108c
SS
6251{
6252 int nfields = fip->nfields;
6253
6254 /* Record the field count, allocate space for the array of fields,
6255 and create blank accessibility bitfields if necessary. */
6256 TYPE_NFIELDS (type) = nfields;
6257 TYPE_FIELDS (type) = (struct field *)
6258 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6259 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6260
b4ba55a1 6261 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6262 {
6263 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6264
6265 TYPE_FIELD_PRIVATE_BITS (type) =
6266 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6267 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6268
6269 TYPE_FIELD_PROTECTED_BITS (type) =
6270 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6271 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6272
774b6a14
TT
6273 TYPE_FIELD_IGNORE_BITS (type) =
6274 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6275 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6276 }
6277
6278 /* If the type has baseclasses, allocate and clear a bit vector for
6279 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6280 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6281 {
6282 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6283 unsigned char *pointer;
c906108c
SS
6284
6285 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6286 pointer = TYPE_ALLOC (type, num_bytes);
6287 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6288 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6289 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6290 }
6291
6292 /* Copy the saved-up fields into the field vector. Start from the head
6293 of the list, adding to the tail of the field array, so that they end
6294 up in the same order in the array in which they were added to the list. */
6295 while (nfields-- > 0)
6296 {
7d0ccb61
DJ
6297 struct nextfield *fieldp;
6298
6299 if (fip->fields)
6300 {
6301 fieldp = fip->fields;
6302 fip->fields = fieldp->next;
6303 }
6304 else
6305 {
6306 fieldp = fip->baseclasses;
6307 fip->baseclasses = fieldp->next;
6308 }
6309
6310 TYPE_FIELD (type, nfields) = fieldp->field;
6311 switch (fieldp->accessibility)
c906108c 6312 {
c5aa993b 6313 case DW_ACCESS_private:
b4ba55a1
JB
6314 if (cu->language != language_ada)
6315 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6316 break;
c906108c 6317
c5aa993b 6318 case DW_ACCESS_protected:
b4ba55a1
JB
6319 if (cu->language != language_ada)
6320 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6321 break;
c906108c 6322
c5aa993b
JM
6323 case DW_ACCESS_public:
6324 break;
c906108c 6325
c5aa993b
JM
6326 default:
6327 /* Unknown accessibility. Complain and treat it as public. */
6328 {
e2e0b3e5 6329 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6330 fieldp->accessibility);
c5aa993b
JM
6331 }
6332 break;
c906108c
SS
6333 }
6334 if (nfields < fip->nbaseclasses)
6335 {
7d0ccb61 6336 switch (fieldp->virtuality)
c906108c 6337 {
c5aa993b
JM
6338 case DW_VIRTUALITY_virtual:
6339 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6340 if (cu->language == language_ada)
6341 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6342 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6343 break;
c906108c
SS
6344 }
6345 }
c906108c
SS
6346 }
6347}
6348
c906108c
SS
6349/* Add a member function to the proper fieldlist. */
6350
6351static void
107d2387 6352dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6353 struct type *type, struct dwarf2_cu *cu)
c906108c 6354{
e7c27a73 6355 struct objfile *objfile = cu->objfile;
c906108c
SS
6356 struct attribute *attr;
6357 struct fnfieldlist *flp;
6358 int i;
6359 struct fn_field *fnp;
6360 char *fieldname;
c906108c 6361 struct nextfnfield *new_fnfield;
f792889a 6362 struct type *this_type;
c906108c 6363
b4ba55a1
JB
6364 if (cu->language == language_ada)
6365 error ("unexpected member function in Ada type");
6366
2df3850c 6367 /* Get name of member function. */
39cbfefa
DJ
6368 fieldname = dwarf2_name (die, cu);
6369 if (fieldname == NULL)
2df3850c 6370 return;
c906108c 6371
c906108c
SS
6372 /* Look up member function name in fieldlist. */
6373 for (i = 0; i < fip->nfnfields; i++)
6374 {
27bfe10e 6375 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6376 break;
6377 }
6378
6379 /* Create new list element if necessary. */
6380 if (i < fip->nfnfields)
6381 flp = &fip->fnfieldlists[i];
6382 else
6383 {
6384 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6385 {
6386 fip->fnfieldlists = (struct fnfieldlist *)
6387 xrealloc (fip->fnfieldlists,
6388 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6389 * sizeof (struct fnfieldlist));
c906108c 6390 if (fip->nfnfields == 0)
c13c43fd 6391 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6392 }
6393 flp = &fip->fnfieldlists[fip->nfnfields];
6394 flp->name = fieldname;
6395 flp->length = 0;
6396 flp->head = NULL;
3da10d80 6397 i = fip->nfnfields++;
c906108c
SS
6398 }
6399
6400 /* Create a new member function field and chain it to the field list
6401 entry. */
6402 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6403 make_cleanup (xfree, new_fnfield);
c906108c
SS
6404 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6405 new_fnfield->next = flp->head;
6406 flp->head = new_fnfield;
6407 flp->length++;
6408
6409 /* Fill in the member function field info. */
6410 fnp = &new_fnfield->fnfield;
3da10d80
KS
6411
6412 /* Delay processing of the physname until later. */
6413 if (cu->language == language_cplus || cu->language == language_java)
6414 {
6415 add_to_method_list (type, i, flp->length - 1, fieldname,
6416 die, cu);
6417 }
6418 else
6419 {
6420 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6421 fnp->physname = physname ? physname : "";
6422 }
6423
c906108c 6424 fnp->type = alloc_type (objfile);
f792889a
DJ
6425 this_type = read_type_die (die, cu);
6426 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6427 {
f792889a 6428 int nparams = TYPE_NFIELDS (this_type);
c906108c 6429
f792889a 6430 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6431 of the method itself (TYPE_CODE_METHOD). */
6432 smash_to_method_type (fnp->type, type,
f792889a
DJ
6433 TYPE_TARGET_TYPE (this_type),
6434 TYPE_FIELDS (this_type),
6435 TYPE_NFIELDS (this_type),
6436 TYPE_VARARGS (this_type));
c906108c
SS
6437
6438 /* Handle static member functions.
c5aa993b
JM
6439 Dwarf2 has no clean way to discern C++ static and non-static
6440 member functions. G++ helps GDB by marking the first
6441 parameter for non-static member functions (which is the
6442 this pointer) as artificial. We obtain this information
6443 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6444 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6445 fnp->voffset = VOFFSET_STATIC;
6446 }
6447 else
e2e0b3e5 6448 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6449 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6450
6451 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6452 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6453 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6454
6455 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6456 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6457
6458 /* Get accessibility. */
e142c38c 6459 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6460 if (attr)
6461 {
6462 switch (DW_UNSND (attr))
6463 {
c5aa993b
JM
6464 case DW_ACCESS_private:
6465 fnp->is_private = 1;
6466 break;
6467 case DW_ACCESS_protected:
6468 fnp->is_protected = 1;
6469 break;
c906108c
SS
6470 }
6471 }
6472
b02dede2 6473 /* Check for artificial methods. */
e142c38c 6474 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6475 if (attr && DW_UNSND (attr) != 0)
6476 fnp->is_artificial = 1;
6477
0d564a31 6478 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6479 function. For older versions of GCC, this is an offset in the
6480 appropriate virtual table, as specified by DW_AT_containing_type.
6481 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6482 to the object address. */
6483
e142c38c 6484 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6485 if (attr)
8e19ed76 6486 {
aec5aa8b 6487 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6488 {
aec5aa8b
TT
6489 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6490 {
6491 /* Old-style GCC. */
6492 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6493 }
6494 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6495 || (DW_BLOCK (attr)->size > 1
6496 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6497 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6498 {
6499 struct dwarf_block blk;
6500 int offset;
6501
6502 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6503 ? 1 : 2);
6504 blk.size = DW_BLOCK (attr)->size - offset;
6505 blk.data = DW_BLOCK (attr)->data + offset;
6506 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6507 if ((fnp->voffset % cu->header.addr_size) != 0)
6508 dwarf2_complex_location_expr_complaint ();
6509 else
6510 fnp->voffset /= cu->header.addr_size;
6511 fnp->voffset += 2;
6512 }
6513 else
6514 dwarf2_complex_location_expr_complaint ();
6515
6516 if (!fnp->fcontext)
6517 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6518 }
3690dd37 6519 else if (attr_form_is_section_offset (attr))
8e19ed76 6520 {
4d3c2250 6521 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6522 }
6523 else
6524 {
4d3c2250
KB
6525 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6526 fieldname);
8e19ed76 6527 }
0d564a31 6528 }
d48cc9dd
DJ
6529 else
6530 {
6531 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6532 if (attr && DW_UNSND (attr))
6533 {
6534 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6535 complaint (&symfile_complaints,
6536 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6537 fieldname, die->offset);
9655fd1a 6538 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6539 TYPE_CPLUS_DYNAMIC (type) = 1;
6540 }
6541 }
c906108c
SS
6542}
6543
6544/* Create the vector of member function fields, and attach it to the type. */
6545
6546static void
fba45db2 6547dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6548 struct dwarf2_cu *cu)
c906108c
SS
6549{
6550 struct fnfieldlist *flp;
6551 int total_length = 0;
6552 int i;
6553
b4ba55a1
JB
6554 if (cu->language == language_ada)
6555 error ("unexpected member functions in Ada type");
6556
c906108c
SS
6557 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6558 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6559 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6560
6561 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6562 {
6563 struct nextfnfield *nfp = flp->head;
6564 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6565 int k;
6566
6567 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6568 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6569 fn_flp->fn_fields = (struct fn_field *)
6570 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6571 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6572 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6573
6574 total_length += flp->length;
6575 }
6576
6577 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6578 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6579}
6580
1168df01
JB
6581/* Returns non-zero if NAME is the name of a vtable member in CU's
6582 language, zero otherwise. */
6583static int
6584is_vtable_name (const char *name, struct dwarf2_cu *cu)
6585{
6586 static const char vptr[] = "_vptr";
987504bb 6587 static const char vtable[] = "vtable";
1168df01 6588
987504bb
JJ
6589 /* Look for the C++ and Java forms of the vtable. */
6590 if ((cu->language == language_java
6591 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6592 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6593 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6594 return 1;
6595
6596 return 0;
6597}
6598
c0dd20ea 6599/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6600 functions, with the ABI-specified layout. If TYPE describes
6601 such a structure, smash it into a member function type.
61049d3b
DJ
6602
6603 GCC shouldn't do this; it should just output pointer to member DIEs.
6604 This is GCC PR debug/28767. */
c0dd20ea 6605
0b92b5bb
TT
6606static void
6607quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6608{
0b92b5bb 6609 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6610
6611 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6612 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6613 return;
c0dd20ea
DJ
6614
6615 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6616 if (TYPE_FIELD_NAME (type, 0) == NULL
6617 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6618 || TYPE_FIELD_NAME (type, 1) == NULL
6619 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6620 return;
c0dd20ea
DJ
6621
6622 /* Find the type of the method. */
0b92b5bb 6623 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6624 if (pfn_type == NULL
6625 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6626 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6627 return;
c0dd20ea
DJ
6628
6629 /* Look for the "this" argument. */
6630 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6631 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6632 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6633 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6634 return;
c0dd20ea
DJ
6635
6636 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6637 new_type = alloc_type (objfile);
6638 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6639 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6640 TYPE_VARARGS (pfn_type));
0b92b5bb 6641 smash_to_methodptr_type (type, new_type);
c0dd20ea 6642}
1168df01 6643
c906108c 6644/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6645 (definition) to create a type for the structure or union. Fill in
6646 the type's name and general properties; the members will not be
6647 processed until process_structure_type.
c906108c 6648
c767944b
DJ
6649 NOTE: we need to call these functions regardless of whether or not the
6650 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6651 structure or union. This gets the type entered into our set of
6652 user defined types.
6653
6654 However, if the structure is incomplete (an opaque struct/union)
6655 then suppress creating a symbol table entry for it since gdb only
6656 wants to find the one with the complete definition. Note that if
6657 it is complete, we just call new_symbol, which does it's own
6658 checking about whether the struct/union is anonymous or not (and
6659 suppresses creating a symbol table entry itself). */
6660
f792889a 6661static struct type *
134d01f1 6662read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6663{
e7c27a73 6664 struct objfile *objfile = cu->objfile;
c906108c
SS
6665 struct type *type;
6666 struct attribute *attr;
39cbfefa 6667 char *name;
c906108c 6668
348e048f
DE
6669 /* If the definition of this type lives in .debug_types, read that type.
6670 Don't follow DW_AT_specification though, that will take us back up
6671 the chain and we want to go down. */
6672 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6673 if (attr)
6674 {
6675 struct dwarf2_cu *type_cu = cu;
6676 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6677
348e048f
DE
6678 /* We could just recurse on read_structure_type, but we need to call
6679 get_die_type to ensure only one type for this DIE is created.
6680 This is important, for example, because for c++ classes we need
6681 TYPE_NAME set which is only done by new_symbol. Blech. */
6682 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6683
6684 /* TYPE_CU may not be the same as CU.
6685 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6686 return set_die_type (die, type, cu);
6687 }
6688
c0dd20ea 6689 type = alloc_type (objfile);
c906108c 6690 INIT_CPLUS_SPECIFIC (type);
93311388 6691
39cbfefa
DJ
6692 name = dwarf2_name (die, cu);
6693 if (name != NULL)
c906108c 6694 {
987504bb
JJ
6695 if (cu->language == language_cplus
6696 || cu->language == language_java)
63d06c5c 6697 {
3da10d80
KS
6698 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6699
6700 /* dwarf2_full_name might have already finished building the DIE's
6701 type. If so, there is no need to continue. */
6702 if (get_die_type (die, cu) != NULL)
6703 return get_die_type (die, cu);
6704
6705 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6706 if (die->tag == DW_TAG_structure_type
6707 || die->tag == DW_TAG_class_type)
6708 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6709 }
6710 else
6711 {
d8151005
DJ
6712 /* The name is already allocated along with this objfile, so
6713 we don't need to duplicate it for the type. */
94af9270
KS
6714 TYPE_TAG_NAME (type) = (char *) name;
6715 if (die->tag == DW_TAG_class_type)
6716 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6717 }
c906108c
SS
6718 }
6719
6720 if (die->tag == DW_TAG_structure_type)
6721 {
6722 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6723 }
6724 else if (die->tag == DW_TAG_union_type)
6725 {
6726 TYPE_CODE (type) = TYPE_CODE_UNION;
6727 }
6728 else
6729 {
c906108c
SS
6730 TYPE_CODE (type) = TYPE_CODE_CLASS;
6731 }
6732
0cc2414c
TT
6733 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6734 TYPE_DECLARED_CLASS (type) = 1;
6735
e142c38c 6736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6737 if (attr)
6738 {
6739 TYPE_LENGTH (type) = DW_UNSND (attr);
6740 }
6741 else
6742 {
6743 TYPE_LENGTH (type) = 0;
6744 }
6745
876cecd0 6746 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6747 if (die_is_declaration (die, cu))
876cecd0 6748 TYPE_STUB (type) = 1;
a6c727b2
DJ
6749 else if (attr == NULL && die->child == NULL
6750 && producer_is_realview (cu->producer))
6751 /* RealView does not output the required DW_AT_declaration
6752 on incomplete types. */
6753 TYPE_STUB (type) = 1;
dc718098 6754
c906108c
SS
6755 /* We need to add the type field to the die immediately so we don't
6756 infinitely recurse when dealing with pointers to the structure
6757 type within the structure itself. */
1c379e20 6758 set_die_type (die, type, cu);
c906108c 6759
7e314c57
JK
6760 /* set_die_type should be already done. */
6761 set_descriptive_type (type, die, cu);
6762
c767944b
DJ
6763 return type;
6764}
6765
6766/* Finish creating a structure or union type, including filling in
6767 its members and creating a symbol for it. */
6768
6769static void
6770process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6771{
6772 struct objfile *objfile = cu->objfile;
6773 struct die_info *child_die = die->child;
6774 struct type *type;
6775
6776 type = get_die_type (die, cu);
6777 if (type == NULL)
6778 type = read_structure_type (die, cu);
6779
e142c38c 6780 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6781 {
6782 struct field_info fi;
6783 struct die_info *child_die;
34eaf542 6784 VEC (symbolp) *template_args = NULL;
c767944b 6785 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6786
6787 memset (&fi, 0, sizeof (struct field_info));
6788
639d11d3 6789 child_die = die->child;
c906108c
SS
6790
6791 while (child_die && child_die->tag)
6792 {
a9a9bd0f
DC
6793 if (child_die->tag == DW_TAG_member
6794 || child_die->tag == DW_TAG_variable)
c906108c 6795 {
a9a9bd0f
DC
6796 /* NOTE: carlton/2002-11-05: A C++ static data member
6797 should be a DW_TAG_member that is a declaration, but
6798 all versions of G++ as of this writing (so through at
6799 least 3.2.1) incorrectly generate DW_TAG_variable
6800 tags for them instead. */
e7c27a73 6801 dwarf2_add_field (&fi, child_die, cu);
c906108c 6802 }
8713b1b1 6803 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6804 {
6805 /* C++ member function. */
e7c27a73 6806 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6807 }
6808 else if (child_die->tag == DW_TAG_inheritance)
6809 {
6810 /* C++ base class field. */
e7c27a73 6811 dwarf2_add_field (&fi, child_die, cu);
c906108c 6812 }
98751a41
JK
6813 else if (child_die->tag == DW_TAG_typedef)
6814 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6815 else if (child_die->tag == DW_TAG_template_type_param
6816 || child_die->tag == DW_TAG_template_value_param)
6817 {
6818 struct symbol *arg = new_symbol (child_die, NULL, cu);
6819
f1078f66
DJ
6820 if (arg != NULL)
6821 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6822 }
6823
c906108c
SS
6824 child_die = sibling_die (child_die);
6825 }
6826
34eaf542
TT
6827 /* Attach template arguments to type. */
6828 if (! VEC_empty (symbolp, template_args))
6829 {
6830 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6831 TYPE_N_TEMPLATE_ARGUMENTS (type)
6832 = VEC_length (symbolp, template_args);
6833 TYPE_TEMPLATE_ARGUMENTS (type)
6834 = obstack_alloc (&objfile->objfile_obstack,
6835 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6836 * sizeof (struct symbol *)));
6837 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6838 VEC_address (symbolp, template_args),
6839 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6840 * sizeof (struct symbol *)));
6841 VEC_free (symbolp, template_args);
6842 }
6843
c906108c
SS
6844 /* Attach fields and member functions to the type. */
6845 if (fi.nfields)
e7c27a73 6846 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6847 if (fi.nfnfields)
6848 {
e7c27a73 6849 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6850
c5aa993b 6851 /* Get the type which refers to the base class (possibly this
c906108c 6852 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6853 class from the DW_AT_containing_type attribute. This use of
6854 DW_AT_containing_type is a GNU extension. */
c906108c 6855
e142c38c 6856 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6857 {
e7c27a73 6858 struct type *t = die_containing_type (die, cu);
c906108c
SS
6859
6860 TYPE_VPTR_BASETYPE (type) = t;
6861 if (type == t)
6862 {
c906108c
SS
6863 int i;
6864
6865 /* Our own class provides vtbl ptr. */
6866 for (i = TYPE_NFIELDS (t) - 1;
6867 i >= TYPE_N_BASECLASSES (t);
6868 --i)
6869 {
6870 char *fieldname = TYPE_FIELD_NAME (t, i);
6871
1168df01 6872 if (is_vtable_name (fieldname, cu))
c906108c
SS
6873 {
6874 TYPE_VPTR_FIELDNO (type) = i;
6875 break;
6876 }
6877 }
6878
6879 /* Complain if virtual function table field not found. */
6880 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6881 complaint (&symfile_complaints,
e2e0b3e5 6882 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6883 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6884 "");
c906108c
SS
6885 }
6886 else
6887 {
6888 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6889 }
6890 }
f6235d4c
EZ
6891 else if (cu->producer
6892 && strncmp (cu->producer,
6893 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6894 {
6895 /* The IBM XLC compiler does not provide direct indication
6896 of the containing type, but the vtable pointer is
6897 always named __vfp. */
6898
6899 int i;
6900
6901 for (i = TYPE_NFIELDS (type) - 1;
6902 i >= TYPE_N_BASECLASSES (type);
6903 --i)
6904 {
6905 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6906 {
6907 TYPE_VPTR_FIELDNO (type) = i;
6908 TYPE_VPTR_BASETYPE (type) = type;
6909 break;
6910 }
6911 }
6912 }
c906108c 6913 }
98751a41
JK
6914
6915 /* Copy fi.typedef_field_list linked list elements content into the
6916 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6917 if (fi.typedef_field_list)
6918 {
6919 int i = fi.typedef_field_list_count;
6920
a0d7a4ff 6921 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6922 TYPE_TYPEDEF_FIELD_ARRAY (type)
6923 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6924 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6925
6926 /* Reverse the list order to keep the debug info elements order. */
6927 while (--i >= 0)
6928 {
6929 struct typedef_field *dest, *src;
6e70227d 6930
98751a41
JK
6931 dest = &TYPE_TYPEDEF_FIELD (type, i);
6932 src = &fi.typedef_field_list->field;
6933 fi.typedef_field_list = fi.typedef_field_list->next;
6934 *dest = *src;
6935 }
6936 }
c767944b
DJ
6937
6938 do_cleanups (back_to);
c906108c 6939 }
63d06c5c 6940
0b92b5bb
TT
6941 quirk_gcc_member_function_pointer (type, cu->objfile);
6942
90aeadfc
DC
6943 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6944 snapshots) has been known to create a die giving a declaration
6945 for a class that has, as a child, a die giving a definition for a
6946 nested class. So we have to process our children even if the
6947 current die is a declaration. Normally, of course, a declaration
6948 won't have any children at all. */
134d01f1 6949
90aeadfc
DC
6950 while (child_die != NULL && child_die->tag)
6951 {
6952 if (child_die->tag == DW_TAG_member
6953 || child_die->tag == DW_TAG_variable
34eaf542
TT
6954 || child_die->tag == DW_TAG_inheritance
6955 || child_die->tag == DW_TAG_template_value_param
6956 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6957 {
90aeadfc 6958 /* Do nothing. */
134d01f1 6959 }
90aeadfc
DC
6960 else
6961 process_die (child_die, cu);
134d01f1 6962
90aeadfc 6963 child_die = sibling_die (child_die);
134d01f1
DJ
6964 }
6965
fa4028e9
JB
6966 /* Do not consider external references. According to the DWARF standard,
6967 these DIEs are identified by the fact that they have no byte_size
6968 attribute, and a declaration attribute. */
6969 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6970 || !die_is_declaration (die, cu))
c767944b 6971 new_symbol (die, type, cu);
134d01f1
DJ
6972}
6973
6974/* Given a DW_AT_enumeration_type die, set its type. We do not
6975 complete the type's fields yet, or create any symbols. */
c906108c 6976
f792889a 6977static struct type *
134d01f1 6978read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6979{
e7c27a73 6980 struct objfile *objfile = cu->objfile;
c906108c 6981 struct type *type;
c906108c 6982 struct attribute *attr;
0114d602 6983 const char *name;
134d01f1 6984
348e048f
DE
6985 /* If the definition of this type lives in .debug_types, read that type.
6986 Don't follow DW_AT_specification though, that will take us back up
6987 the chain and we want to go down. */
6988 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6989 if (attr)
6990 {
6991 struct dwarf2_cu *type_cu = cu;
6992 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6993
348e048f 6994 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6995
6996 /* TYPE_CU may not be the same as CU.
6997 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6998 return set_die_type (die, type, cu);
6999 }
7000
c906108c
SS
7001 type = alloc_type (objfile);
7002
7003 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7004 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7005 if (name != NULL)
0114d602 7006 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7007
e142c38c 7008 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7009 if (attr)
7010 {
7011 TYPE_LENGTH (type) = DW_UNSND (attr);
7012 }
7013 else
7014 {
7015 TYPE_LENGTH (type) = 0;
7016 }
7017
137033e9
JB
7018 /* The enumeration DIE can be incomplete. In Ada, any type can be
7019 declared as private in the package spec, and then defined only
7020 inside the package body. Such types are known as Taft Amendment
7021 Types. When another package uses such a type, an incomplete DIE
7022 may be generated by the compiler. */
02eb380e 7023 if (die_is_declaration (die, cu))
876cecd0 7024 TYPE_STUB (type) = 1;
02eb380e 7025
f792889a 7026 return set_die_type (die, type, cu);
134d01f1
DJ
7027}
7028
7029/* Given a pointer to a die which begins an enumeration, process all
7030 the dies that define the members of the enumeration, and create the
7031 symbol for the enumeration type.
7032
7033 NOTE: We reverse the order of the element list. */
7034
7035static void
7036process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7037{
f792889a 7038 struct type *this_type;
134d01f1 7039
f792889a
DJ
7040 this_type = get_die_type (die, cu);
7041 if (this_type == NULL)
7042 this_type = read_enumeration_type (die, cu);
9dc481d3 7043
639d11d3 7044 if (die->child != NULL)
c906108c 7045 {
9dc481d3
DE
7046 struct die_info *child_die;
7047 struct symbol *sym;
7048 struct field *fields = NULL;
7049 int num_fields = 0;
7050 int unsigned_enum = 1;
7051 char *name;
7052
639d11d3 7053 child_die = die->child;
c906108c
SS
7054 while (child_die && child_die->tag)
7055 {
7056 if (child_die->tag != DW_TAG_enumerator)
7057 {
e7c27a73 7058 process_die (child_die, cu);
c906108c
SS
7059 }
7060 else
7061 {
39cbfefa
DJ
7062 name = dwarf2_name (child_die, cu);
7063 if (name)
c906108c 7064 {
f792889a 7065 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7066 if (SYMBOL_VALUE (sym) < 0)
7067 unsigned_enum = 0;
7068
7069 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7070 {
7071 fields = (struct field *)
7072 xrealloc (fields,
7073 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7074 * sizeof (struct field));
c906108c
SS
7075 }
7076
3567439c 7077 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7078 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7079 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7080 FIELD_BITSIZE (fields[num_fields]) = 0;
7081
7082 num_fields++;
7083 }
7084 }
7085
7086 child_die = sibling_die (child_die);
7087 }
7088
7089 if (num_fields)
7090 {
f792889a
DJ
7091 TYPE_NFIELDS (this_type) = num_fields;
7092 TYPE_FIELDS (this_type) = (struct field *)
7093 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7094 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7095 sizeof (struct field) * num_fields);
b8c9b27d 7096 xfree (fields);
c906108c
SS
7097 }
7098 if (unsigned_enum)
876cecd0 7099 TYPE_UNSIGNED (this_type) = 1;
c906108c 7100 }
134d01f1 7101
f792889a 7102 new_symbol (die, this_type, cu);
c906108c
SS
7103}
7104
7105/* Extract all information from a DW_TAG_array_type DIE and put it in
7106 the DIE's type field. For now, this only handles one dimensional
7107 arrays. */
7108
f792889a 7109static struct type *
e7c27a73 7110read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7111{
e7c27a73 7112 struct objfile *objfile = cu->objfile;
c906108c 7113 struct die_info *child_die;
7e314c57 7114 struct type *type;
c906108c
SS
7115 struct type *element_type, *range_type, *index_type;
7116 struct type **range_types = NULL;
7117 struct attribute *attr;
7118 int ndim = 0;
7119 struct cleanup *back_to;
39cbfefa 7120 char *name;
c906108c 7121
e7c27a73 7122 element_type = die_type (die, cu);
c906108c 7123
7e314c57
JK
7124 /* The die_type call above may have already set the type for this DIE. */
7125 type = get_die_type (die, cu);
7126 if (type)
7127 return type;
7128
c906108c
SS
7129 /* Irix 6.2 native cc creates array types without children for
7130 arrays with unspecified length. */
639d11d3 7131 if (die->child == NULL)
c906108c 7132 {
46bf5051 7133 index_type = objfile_type (objfile)->builtin_int;
c906108c 7134 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7135 type = create_array_type (NULL, element_type, range_type);
7136 return set_die_type (die, type, cu);
c906108c
SS
7137 }
7138
7139 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7140 child_die = die->child;
c906108c
SS
7141 while (child_die && child_die->tag)
7142 {
7143 if (child_die->tag == DW_TAG_subrange_type)
7144 {
f792889a 7145 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7146
f792889a 7147 if (child_type != NULL)
a02abb62
JB
7148 {
7149 /* The range type was succesfully read. Save it for
7150 the array type creation. */
7151 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7152 {
7153 range_types = (struct type **)
7154 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7155 * sizeof (struct type *));
7156 if (ndim == 0)
7157 make_cleanup (free_current_contents, &range_types);
7158 }
f792889a 7159 range_types[ndim++] = child_type;
a02abb62 7160 }
c906108c
SS
7161 }
7162 child_die = sibling_die (child_die);
7163 }
7164
7165 /* Dwarf2 dimensions are output from left to right, create the
7166 necessary array types in backwards order. */
7ca2d3a3 7167
c906108c 7168 type = element_type;
7ca2d3a3
DL
7169
7170 if (read_array_order (die, cu) == DW_ORD_col_major)
7171 {
7172 int i = 0;
9a619af0 7173
7ca2d3a3
DL
7174 while (i < ndim)
7175 type = create_array_type (NULL, type, range_types[i++]);
7176 }
7177 else
7178 {
7179 while (ndim-- > 0)
7180 type = create_array_type (NULL, type, range_types[ndim]);
7181 }
c906108c 7182
f5f8a009
EZ
7183 /* Understand Dwarf2 support for vector types (like they occur on
7184 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7185 array type. This is not part of the Dwarf2/3 standard yet, but a
7186 custom vendor extension. The main difference between a regular
7187 array and the vector variant is that vectors are passed by value
7188 to functions. */
e142c38c 7189 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7190 if (attr)
ea37ba09 7191 make_vector_type (type);
f5f8a009 7192
dbc98a8b
KW
7193 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7194 implementation may choose to implement triple vectors using this
7195 attribute. */
7196 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7197 if (attr)
7198 {
7199 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7200 TYPE_LENGTH (type) = DW_UNSND (attr);
7201 else
7202 complaint (&symfile_complaints, _("\
7203DW_AT_byte_size for array type smaller than the total size of elements"));
7204 }
7205
39cbfefa
DJ
7206 name = dwarf2_name (die, cu);
7207 if (name)
7208 TYPE_NAME (type) = name;
6e70227d 7209
7e314c57
JK
7210 /* Install the type in the die. */
7211 set_die_type (die, type, cu);
7212
7213 /* set_die_type should be already done. */
b4ba55a1
JB
7214 set_descriptive_type (type, die, cu);
7215
c906108c
SS
7216 do_cleanups (back_to);
7217
7e314c57 7218 return type;
c906108c
SS
7219}
7220
7ca2d3a3 7221static enum dwarf_array_dim_ordering
6e70227d 7222read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7223{
7224 struct attribute *attr;
7225
7226 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7227
7228 if (attr) return DW_SND (attr);
7229
7230 /*
7231 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7232 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7233 laid out as per normal fortran.
7234
6e70227d 7235 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7236 version checking.
7237 */
7238
905e0470
PM
7239 if (cu->language == language_fortran
7240 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7241 {
7242 return DW_ORD_row_major;
7243 }
7244
6e70227d 7245 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7246 {
7247 case array_column_major:
7248 return DW_ORD_col_major;
7249 case array_row_major:
7250 default:
7251 return DW_ORD_row_major;
7252 };
7253}
7254
72019c9c
GM
7255/* Extract all information from a DW_TAG_set_type DIE and put it in
7256 the DIE's type field. */
7257
f792889a 7258static struct type *
72019c9c
GM
7259read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7260{
7e314c57
JK
7261 struct type *domain_type, *set_type;
7262 struct attribute *attr;
f792889a 7263
7e314c57
JK
7264 domain_type = die_type (die, cu);
7265
7266 /* The die_type call above may have already set the type for this DIE. */
7267 set_type = get_die_type (die, cu);
7268 if (set_type)
7269 return set_type;
7270
7271 set_type = create_set_type (NULL, domain_type);
7272
7273 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7274 if (attr)
7275 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7276
f792889a 7277 return set_die_type (die, set_type, cu);
72019c9c 7278}
7ca2d3a3 7279
c906108c
SS
7280/* First cut: install each common block member as a global variable. */
7281
7282static void
e7c27a73 7283read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7284{
7285 struct die_info *child_die;
7286 struct attribute *attr;
7287 struct symbol *sym;
7288 CORE_ADDR base = (CORE_ADDR) 0;
7289
e142c38c 7290 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7291 if (attr)
7292 {
8e19ed76
PS
7293 /* Support the .debug_loc offsets */
7294 if (attr_form_is_block (attr))
7295 {
e7c27a73 7296 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7297 }
3690dd37 7298 else if (attr_form_is_section_offset (attr))
8e19ed76 7299 {
4d3c2250 7300 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7301 }
7302 else
7303 {
4d3c2250
KB
7304 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7305 "common block member");
8e19ed76 7306 }
c906108c 7307 }
639d11d3 7308 if (die->child != NULL)
c906108c 7309 {
639d11d3 7310 child_die = die->child;
c906108c
SS
7311 while (child_die && child_die->tag)
7312 {
e7c27a73 7313 sym = new_symbol (child_die, NULL, cu);
e142c38c 7314 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7315 if (sym != NULL && attr != NULL)
c906108c 7316 {
d4b96c9a
JK
7317 CORE_ADDR byte_offset = 0;
7318
7319 if (attr_form_is_section_offset (attr))
7320 dwarf2_complex_location_expr_complaint ();
7321 else if (attr_form_is_constant (attr))
7322 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7323 else if (attr_form_is_block (attr))
7324 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7325 else
7326 dwarf2_complex_location_expr_complaint ();
7327
7328 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7329 add_symbol_to_list (sym, &global_symbols);
7330 }
7331 child_die = sibling_die (child_die);
7332 }
7333 }
7334}
7335
0114d602 7336/* Create a type for a C++ namespace. */
d9fa45fe 7337
0114d602
DJ
7338static struct type *
7339read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7340{
e7c27a73 7341 struct objfile *objfile = cu->objfile;
0114d602 7342 const char *previous_prefix, *name;
9219021c 7343 int is_anonymous;
0114d602
DJ
7344 struct type *type;
7345
7346 /* For extensions, reuse the type of the original namespace. */
7347 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7348 {
7349 struct die_info *ext_die;
7350 struct dwarf2_cu *ext_cu = cu;
9a619af0 7351
0114d602
DJ
7352 ext_die = dwarf2_extension (die, &ext_cu);
7353 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7354
7355 /* EXT_CU may not be the same as CU.
7356 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7357 return set_die_type (die, type, cu);
7358 }
9219021c 7359
e142c38c 7360 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7361
7362 /* Now build the name of the current namespace. */
7363
0114d602
DJ
7364 previous_prefix = determine_prefix (die, cu);
7365 if (previous_prefix[0] != '\0')
7366 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7367 previous_prefix, name, 0, cu);
0114d602
DJ
7368
7369 /* Create the type. */
7370 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7371 objfile);
7372 TYPE_NAME (type) = (char *) name;
7373 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7374
60531b24 7375 return set_die_type (die, type, cu);
0114d602
DJ
7376}
7377
7378/* Read a C++ namespace. */
7379
7380static void
7381read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7382{
7383 struct objfile *objfile = cu->objfile;
7384 const char *name;
7385 int is_anonymous;
9219021c 7386
5c4e30ca
DC
7387 /* Add a symbol associated to this if we haven't seen the namespace
7388 before. Also, add a using directive if it's an anonymous
7389 namespace. */
9219021c 7390
f2f0e013 7391 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7392 {
7393 struct type *type;
7394
0114d602 7395 type = read_type_die (die, cu);
e7c27a73 7396 new_symbol (die, type, cu);
5c4e30ca 7397
0114d602 7398 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7399 if (is_anonymous)
0114d602
DJ
7400 {
7401 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7402
c0cc3a76 7403 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7404 NULL, &objfile->objfile_obstack);
0114d602 7405 }
5c4e30ca 7406 }
9219021c 7407
639d11d3 7408 if (die->child != NULL)
d9fa45fe 7409 {
639d11d3 7410 struct die_info *child_die = die->child;
6e70227d 7411
d9fa45fe
DC
7412 while (child_die && child_die->tag)
7413 {
e7c27a73 7414 process_die (child_die, cu);
d9fa45fe
DC
7415 child_die = sibling_die (child_die);
7416 }
7417 }
38d518c9
EZ
7418}
7419
f55ee35c
JK
7420/* Read a Fortran module as type. This DIE can be only a declaration used for
7421 imported module. Still we need that type as local Fortran "use ... only"
7422 declaration imports depend on the created type in determine_prefix. */
7423
7424static struct type *
7425read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7426{
7427 struct objfile *objfile = cu->objfile;
7428 char *module_name;
7429 struct type *type;
7430
7431 module_name = dwarf2_name (die, cu);
7432 if (!module_name)
7433 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7434 die->offset);
7435 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7436
7437 /* determine_prefix uses TYPE_TAG_NAME. */
7438 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7439
7440 return set_die_type (die, type, cu);
7441}
7442
5d7cb8df
JK
7443/* Read a Fortran module. */
7444
7445static void
7446read_module (struct die_info *die, struct dwarf2_cu *cu)
7447{
7448 struct die_info *child_die = die->child;
7449
5d7cb8df
JK
7450 while (child_die && child_die->tag)
7451 {
7452 process_die (child_die, cu);
7453 child_die = sibling_die (child_die);
7454 }
7455}
7456
38d518c9
EZ
7457/* Return the name of the namespace represented by DIE. Set
7458 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7459 namespace. */
7460
7461static const char *
e142c38c 7462namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7463{
7464 struct die_info *current_die;
7465 const char *name = NULL;
7466
7467 /* Loop through the extensions until we find a name. */
7468
7469 for (current_die = die;
7470 current_die != NULL;
f2f0e013 7471 current_die = dwarf2_extension (die, &cu))
38d518c9 7472 {
e142c38c 7473 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7474 if (name != NULL)
7475 break;
7476 }
7477
7478 /* Is it an anonymous namespace? */
7479
7480 *is_anonymous = (name == NULL);
7481 if (*is_anonymous)
7482 name = "(anonymous namespace)";
7483
7484 return name;
d9fa45fe
DC
7485}
7486
c906108c
SS
7487/* Extract all information from a DW_TAG_pointer_type DIE and add to
7488 the user defined type vector. */
7489
f792889a 7490static struct type *
e7c27a73 7491read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7492{
5e2b427d 7493 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7494 struct comp_unit_head *cu_header = &cu->header;
c906108c 7495 struct type *type;
8b2dbe47
KB
7496 struct attribute *attr_byte_size;
7497 struct attribute *attr_address_class;
7498 int byte_size, addr_class;
7e314c57
JK
7499 struct type *target_type;
7500
7501 target_type = die_type (die, cu);
c906108c 7502
7e314c57
JK
7503 /* The die_type call above may have already set the type for this DIE. */
7504 type = get_die_type (die, cu);
7505 if (type)
7506 return type;
7507
7508 type = lookup_pointer_type (target_type);
8b2dbe47 7509
e142c38c 7510 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7511 if (attr_byte_size)
7512 byte_size = DW_UNSND (attr_byte_size);
c906108c 7513 else
8b2dbe47
KB
7514 byte_size = cu_header->addr_size;
7515
e142c38c 7516 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7517 if (attr_address_class)
7518 addr_class = DW_UNSND (attr_address_class);
7519 else
7520 addr_class = DW_ADDR_none;
7521
7522 /* If the pointer size or address class is different than the
7523 default, create a type variant marked as such and set the
7524 length accordingly. */
7525 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7526 {
5e2b427d 7527 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7528 {
7529 int type_flags;
7530
849957d9 7531 type_flags = gdbarch_address_class_type_flags
5e2b427d 7532 (gdbarch, byte_size, addr_class);
876cecd0
TT
7533 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7534 == 0);
8b2dbe47
KB
7535 type = make_type_with_address_space (type, type_flags);
7536 }
7537 else if (TYPE_LENGTH (type) != byte_size)
7538 {
e2e0b3e5 7539 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7540 }
6e70227d 7541 else
9a619af0
MS
7542 {
7543 /* Should we also complain about unhandled address classes? */
7544 }
c906108c 7545 }
8b2dbe47
KB
7546
7547 TYPE_LENGTH (type) = byte_size;
f792889a 7548 return set_die_type (die, type, cu);
c906108c
SS
7549}
7550
7551/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7552 the user defined type vector. */
7553
f792889a 7554static struct type *
e7c27a73 7555read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7556{
7557 struct type *type;
7558 struct type *to_type;
7559 struct type *domain;
7560
e7c27a73
DJ
7561 to_type = die_type (die, cu);
7562 domain = die_containing_type (die, cu);
0d5de010 7563
7e314c57
JK
7564 /* The calls above may have already set the type for this DIE. */
7565 type = get_die_type (die, cu);
7566 if (type)
7567 return type;
7568
0d5de010
DJ
7569 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7570 type = lookup_methodptr_type (to_type);
7571 else
7572 type = lookup_memberptr_type (to_type, domain);
c906108c 7573
f792889a 7574 return set_die_type (die, type, cu);
c906108c
SS
7575}
7576
7577/* Extract all information from a DW_TAG_reference_type DIE and add to
7578 the user defined type vector. */
7579
f792889a 7580static struct type *
e7c27a73 7581read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7582{
e7c27a73 7583 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7584 struct type *type, *target_type;
c906108c
SS
7585 struct attribute *attr;
7586
7e314c57
JK
7587 target_type = die_type (die, cu);
7588
7589 /* The die_type call above may have already set the type for this DIE. */
7590 type = get_die_type (die, cu);
7591 if (type)
7592 return type;
7593
7594 type = lookup_reference_type (target_type);
e142c38c 7595 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7596 if (attr)
7597 {
7598 TYPE_LENGTH (type) = DW_UNSND (attr);
7599 }
7600 else
7601 {
107d2387 7602 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7603 }
f792889a 7604 return set_die_type (die, type, cu);
c906108c
SS
7605}
7606
f792889a 7607static struct type *
e7c27a73 7608read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7609{
f792889a 7610 struct type *base_type, *cv_type;
c906108c 7611
e7c27a73 7612 base_type = die_type (die, cu);
7e314c57
JK
7613
7614 /* The die_type call above may have already set the type for this DIE. */
7615 cv_type = get_die_type (die, cu);
7616 if (cv_type)
7617 return cv_type;
7618
2f608a3a
KW
7619 /* In case the const qualifier is applied to an array type, the element type
7620 is so qualified, not the array type (section 6.7.3 of C99). */
7621 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7622 {
7623 struct type *el_type, *inner_array;
7624
7625 base_type = copy_type (base_type);
7626 inner_array = base_type;
7627
7628 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7629 {
7630 TYPE_TARGET_TYPE (inner_array) =
7631 copy_type (TYPE_TARGET_TYPE (inner_array));
7632 inner_array = TYPE_TARGET_TYPE (inner_array);
7633 }
7634
7635 el_type = TYPE_TARGET_TYPE (inner_array);
7636 TYPE_TARGET_TYPE (inner_array) =
7637 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7638
7639 return set_die_type (die, base_type, cu);
7640 }
7641
f792889a
DJ
7642 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7643 return set_die_type (die, cv_type, cu);
c906108c
SS
7644}
7645
f792889a 7646static struct type *
e7c27a73 7647read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7648{
f792889a 7649 struct type *base_type, *cv_type;
c906108c 7650
e7c27a73 7651 base_type = die_type (die, cu);
7e314c57
JK
7652
7653 /* The die_type call above may have already set the type for this DIE. */
7654 cv_type = get_die_type (die, cu);
7655 if (cv_type)
7656 return cv_type;
7657
f792889a
DJ
7658 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7659 return set_die_type (die, cv_type, cu);
c906108c
SS
7660}
7661
7662/* Extract all information from a DW_TAG_string_type DIE and add to
7663 the user defined type vector. It isn't really a user defined type,
7664 but it behaves like one, with other DIE's using an AT_user_def_type
7665 attribute to reference it. */
7666
f792889a 7667static struct type *
e7c27a73 7668read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7669{
e7c27a73 7670 struct objfile *objfile = cu->objfile;
3b7538c0 7671 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7672 struct type *type, *range_type, *index_type, *char_type;
7673 struct attribute *attr;
7674 unsigned int length;
7675
e142c38c 7676 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7677 if (attr)
7678 {
7679 length = DW_UNSND (attr);
7680 }
7681 else
7682 {
b21b22e0 7683 /* check for the DW_AT_byte_size attribute */
e142c38c 7684 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7685 if (attr)
7686 {
7687 length = DW_UNSND (attr);
7688 }
7689 else
7690 {
7691 length = 1;
7692 }
c906108c 7693 }
6ccb9162 7694
46bf5051 7695 index_type = objfile_type (objfile)->builtin_int;
c906108c 7696 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7697 char_type = language_string_char_type (cu->language_defn, gdbarch);
7698 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7699
f792889a 7700 return set_die_type (die, type, cu);
c906108c
SS
7701}
7702
7703/* Handle DIES due to C code like:
7704
7705 struct foo
c5aa993b
JM
7706 {
7707 int (*funcp)(int a, long l);
7708 int b;
7709 };
c906108c
SS
7710
7711 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7712 */
c906108c 7713
f792889a 7714static struct type *
e7c27a73 7715read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7716{
7717 struct type *type; /* Type that this function returns */
7718 struct type *ftype; /* Function that returns above type */
7719 struct attribute *attr;
7720
e7c27a73 7721 type = die_type (die, cu);
7e314c57
JK
7722
7723 /* The die_type call above may have already set the type for this DIE. */
7724 ftype = get_die_type (die, cu);
7725 if (ftype)
7726 return ftype;
7727
0c8b41f1 7728 ftype = lookup_function_type (type);
c906108c 7729
5b8101ae 7730 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7731 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7732 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7733 || cu->language == language_cplus
5b8101ae
PM
7734 || cu->language == language_java
7735 || cu->language == language_pascal)
876cecd0 7736 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7737 else if (producer_is_realview (cu->producer))
7738 /* RealView does not emit DW_AT_prototyped. We can not
7739 distinguish prototyped and unprototyped functions; default to
7740 prototyped, since that is more common in modern code (and
7741 RealView warns about unprototyped functions). */
7742 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7743
c055b101
CV
7744 /* Store the calling convention in the type if it's available in
7745 the subroutine die. Otherwise set the calling convention to
7746 the default value DW_CC_normal. */
7747 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7748 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7749
7750 /* We need to add the subroutine type to the die immediately so
7751 we don't infinitely recurse when dealing with parameters
7752 declared as the same subroutine type. */
7753 set_die_type (die, ftype, cu);
6e70227d 7754
639d11d3 7755 if (die->child != NULL)
c906108c 7756 {
8072405b 7757 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7758 struct die_info *child_die;
8072405b 7759 int nparams, iparams;
c906108c
SS
7760
7761 /* Count the number of parameters.
7762 FIXME: GDB currently ignores vararg functions, but knows about
7763 vararg member functions. */
8072405b 7764 nparams = 0;
639d11d3 7765 child_die = die->child;
c906108c
SS
7766 while (child_die && child_die->tag)
7767 {
7768 if (child_die->tag == DW_TAG_formal_parameter)
7769 nparams++;
7770 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7771 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7772 child_die = sibling_die (child_die);
7773 }
7774
7775 /* Allocate storage for parameters and fill them in. */
7776 TYPE_NFIELDS (ftype) = nparams;
7777 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7778 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7779
8072405b
JK
7780 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7781 even if we error out during the parameters reading below. */
7782 for (iparams = 0; iparams < nparams; iparams++)
7783 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7784
7785 iparams = 0;
639d11d3 7786 child_die = die->child;
c906108c
SS
7787 while (child_die && child_die->tag)
7788 {
7789 if (child_die->tag == DW_TAG_formal_parameter)
7790 {
3ce3b1ba
PA
7791 struct type *arg_type;
7792
7793 /* DWARF version 2 has no clean way to discern C++
7794 static and non-static member functions. G++ helps
7795 GDB by marking the first parameter for non-static
7796 member functions (which is the this pointer) as
7797 artificial. We pass this information to
7798 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7799
7800 DWARF version 3 added DW_AT_object_pointer, which GCC
7801 4.5 does not yet generate. */
e142c38c 7802 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7803 if (attr)
7804 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7805 else
418835cc
KS
7806 {
7807 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7808
7809 /* GCC/43521: In java, the formal parameter
7810 "this" is sometimes not marked with DW_AT_artificial. */
7811 if (cu->language == language_java)
7812 {
7813 const char *name = dwarf2_name (child_die, cu);
9a619af0 7814
418835cc
KS
7815 if (name && !strcmp (name, "this"))
7816 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7817 }
7818 }
3ce3b1ba
PA
7819 arg_type = die_type (child_die, cu);
7820
7821 /* RealView does not mark THIS as const, which the testsuite
7822 expects. GCC marks THIS as const in method definitions,
7823 but not in the class specifications (GCC PR 43053). */
7824 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7825 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7826 {
7827 int is_this = 0;
7828 struct dwarf2_cu *arg_cu = cu;
7829 const char *name = dwarf2_name (child_die, cu);
7830
7831 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7832 if (attr)
7833 {
7834 /* If the compiler emits this, use it. */
7835 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7836 is_this = 1;
7837 }
7838 else if (name && strcmp (name, "this") == 0)
7839 /* Function definitions will have the argument names. */
7840 is_this = 1;
7841 else if (name == NULL && iparams == 0)
7842 /* Declarations may not have the names, so like
7843 elsewhere in GDB, assume an artificial first
7844 argument is "this". */
7845 is_this = 1;
7846
7847 if (is_this)
7848 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7849 arg_type, 0);
7850 }
7851
7852 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7853 iparams++;
7854 }
7855 child_die = sibling_die (child_die);
7856 }
7857 }
7858
76c10ea2 7859 return ftype;
c906108c
SS
7860}
7861
f792889a 7862static struct type *
e7c27a73 7863read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7864{
e7c27a73 7865 struct objfile *objfile = cu->objfile;
0114d602 7866 const char *name = NULL;
f792889a 7867 struct type *this_type;
c906108c 7868
94af9270 7869 name = dwarf2_full_name (NULL, die, cu);
f792889a 7870 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7871 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7872 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7873 set_die_type (die, this_type, cu);
7874 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7875 return this_type;
c906108c
SS
7876}
7877
7878/* Find a representation of a given base type and install
7879 it in the TYPE field of the die. */
7880
f792889a 7881static struct type *
e7c27a73 7882read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7883{
e7c27a73 7884 struct objfile *objfile = cu->objfile;
c906108c
SS
7885 struct type *type;
7886 struct attribute *attr;
7887 int encoding = 0, size = 0;
39cbfefa 7888 char *name;
6ccb9162
UW
7889 enum type_code code = TYPE_CODE_INT;
7890 int type_flags = 0;
7891 struct type *target_type = NULL;
c906108c 7892
e142c38c 7893 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7894 if (attr)
7895 {
7896 encoding = DW_UNSND (attr);
7897 }
e142c38c 7898 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7899 if (attr)
7900 {
7901 size = DW_UNSND (attr);
7902 }
39cbfefa 7903 name = dwarf2_name (die, cu);
6ccb9162 7904 if (!name)
c906108c 7905 {
6ccb9162
UW
7906 complaint (&symfile_complaints,
7907 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7908 }
6ccb9162
UW
7909
7910 switch (encoding)
c906108c 7911 {
6ccb9162
UW
7912 case DW_ATE_address:
7913 /* Turn DW_ATE_address into a void * pointer. */
7914 code = TYPE_CODE_PTR;
7915 type_flags |= TYPE_FLAG_UNSIGNED;
7916 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7917 break;
7918 case DW_ATE_boolean:
7919 code = TYPE_CODE_BOOL;
7920 type_flags |= TYPE_FLAG_UNSIGNED;
7921 break;
7922 case DW_ATE_complex_float:
7923 code = TYPE_CODE_COMPLEX;
7924 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7925 break;
7926 case DW_ATE_decimal_float:
7927 code = TYPE_CODE_DECFLOAT;
7928 break;
7929 case DW_ATE_float:
7930 code = TYPE_CODE_FLT;
7931 break;
7932 case DW_ATE_signed:
7933 break;
7934 case DW_ATE_unsigned:
7935 type_flags |= TYPE_FLAG_UNSIGNED;
7936 break;
7937 case DW_ATE_signed_char:
6e70227d 7938 if (cu->language == language_ada || cu->language == language_m2
868a0084 7939 || cu->language == language_pascal)
6ccb9162
UW
7940 code = TYPE_CODE_CHAR;
7941 break;
7942 case DW_ATE_unsigned_char:
868a0084
PM
7943 if (cu->language == language_ada || cu->language == language_m2
7944 || cu->language == language_pascal)
6ccb9162
UW
7945 code = TYPE_CODE_CHAR;
7946 type_flags |= TYPE_FLAG_UNSIGNED;
7947 break;
75079b2b
TT
7948 case DW_ATE_UTF:
7949 /* We just treat this as an integer and then recognize the
7950 type by name elsewhere. */
7951 break;
7952
6ccb9162
UW
7953 default:
7954 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7955 dwarf_type_encoding_name (encoding));
7956 break;
c906108c 7957 }
6ccb9162 7958
0114d602
DJ
7959 type = init_type (code, size, type_flags, NULL, objfile);
7960 TYPE_NAME (type) = name;
6ccb9162
UW
7961 TYPE_TARGET_TYPE (type) = target_type;
7962
0114d602 7963 if (name && strcmp (name, "char") == 0)
876cecd0 7964 TYPE_NOSIGN (type) = 1;
0114d602 7965
f792889a 7966 return set_die_type (die, type, cu);
c906108c
SS
7967}
7968
a02abb62
JB
7969/* Read the given DW_AT_subrange DIE. */
7970
f792889a 7971static struct type *
a02abb62
JB
7972read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7973{
5e2b427d 7974 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7975 struct type *base_type;
7976 struct type *range_type;
7977 struct attribute *attr;
43bbcdc2
PH
7978 LONGEST low = 0;
7979 LONGEST high = -1;
39cbfefa 7980 char *name;
43bbcdc2 7981 LONGEST negative_mask;
e77813c8 7982
a02abb62 7983 base_type = die_type (die, cu);
953ac07e
JK
7984 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7985 check_typedef (base_type);
a02abb62 7986
7e314c57
JK
7987 /* The die_type call above may have already set the type for this DIE. */
7988 range_type = get_die_type (die, cu);
7989 if (range_type)
7990 return range_type;
7991
e142c38c 7992 if (cu->language == language_fortran)
6e70227d 7993 {
a02abb62
JB
7994 /* FORTRAN implies a lower bound of 1, if not given. */
7995 low = 1;
7996 }
7997
dd5e6932
DJ
7998 /* FIXME: For variable sized arrays either of these could be
7999 a variable rather than a constant value. We'll allow it,
8000 but we don't know how to handle it. */
e142c38c 8001 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8002 if (attr)
8003 low = dwarf2_get_attr_constant_value (attr, 0);
8004
e142c38c 8005 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8006 if (attr)
6e70227d 8007 {
e77813c8 8008 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8009 {
8010 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8011 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8012 FIXME: GDB does not yet know how to handle dynamic
8013 arrays properly, treat them as arrays with unspecified
8014 length for now.
8015
8016 FIXME: jimb/2003-09-22: GDB does not really know
8017 how to handle arrays of unspecified length
8018 either; we just represent them as zero-length
8019 arrays. Choose an appropriate upper bound given
8020 the lower bound we've computed above. */
8021 high = low - 1;
8022 }
8023 else
8024 high = dwarf2_get_attr_constant_value (attr, 1);
8025 }
e77813c8
PM
8026 else
8027 {
8028 attr = dwarf2_attr (die, DW_AT_count, cu);
8029 if (attr)
8030 {
8031 int count = dwarf2_get_attr_constant_value (attr, 1);
8032 high = low + count - 1;
8033 }
8034 }
8035
8036 /* Dwarf-2 specifications explicitly allows to create subrange types
8037 without specifying a base type.
8038 In that case, the base type must be set to the type of
8039 the lower bound, upper bound or count, in that order, if any of these
8040 three attributes references an object that has a type.
8041 If no base type is found, the Dwarf-2 specifications say that
8042 a signed integer type of size equal to the size of an address should
8043 be used.
8044 For the following C code: `extern char gdb_int [];'
8045 GCC produces an empty range DIE.
8046 FIXME: muller/2010-05-28: Possible references to object for low bound,
8047 high bound or count are not yet handled by this code.
8048 */
8049 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8050 {
8051 struct objfile *objfile = cu->objfile;
8052 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8053 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8054 struct type *int_type = objfile_type (objfile)->builtin_int;
8055
8056 /* Test "int", "long int", and "long long int" objfile types,
8057 and select the first one having a size above or equal to the
8058 architecture address size. */
8059 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8060 base_type = int_type;
8061 else
8062 {
8063 int_type = objfile_type (objfile)->builtin_long;
8064 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8065 base_type = int_type;
8066 else
8067 {
8068 int_type = objfile_type (objfile)->builtin_long_long;
8069 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8070 base_type = int_type;
8071 }
8072 }
8073 }
a02abb62 8074
6e70227d 8075 negative_mask =
43bbcdc2
PH
8076 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8077 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8078 low |= negative_mask;
8079 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8080 high |= negative_mask;
8081
a02abb62
JB
8082 range_type = create_range_type (NULL, base_type, low, high);
8083
bbb0eef6
JK
8084 /* Mark arrays with dynamic length at least as an array of unspecified
8085 length. GDB could check the boundary but before it gets implemented at
8086 least allow accessing the array elements. */
8087 if (attr && attr->form == DW_FORM_block1)
8088 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8089
39cbfefa
DJ
8090 name = dwarf2_name (die, cu);
8091 if (name)
8092 TYPE_NAME (range_type) = name;
6e70227d 8093
e142c38c 8094 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8095 if (attr)
8096 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8097
7e314c57
JK
8098 set_die_type (die, range_type, cu);
8099
8100 /* set_die_type should be already done. */
b4ba55a1
JB
8101 set_descriptive_type (range_type, die, cu);
8102
7e314c57 8103 return range_type;
a02abb62 8104}
6e70227d 8105
f792889a 8106static struct type *
81a17f79
JB
8107read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8108{
8109 struct type *type;
81a17f79 8110
81a17f79
JB
8111 /* For now, we only support the C meaning of an unspecified type: void. */
8112
0114d602
DJ
8113 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8114 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8115
f792889a 8116 return set_die_type (die, type, cu);
81a17f79 8117}
a02abb62 8118
51545339
DJ
8119/* Trivial hash function for die_info: the hash value of a DIE
8120 is its offset in .debug_info for this objfile. */
8121
8122static hashval_t
8123die_hash (const void *item)
8124{
8125 const struct die_info *die = item;
9a619af0 8126
51545339
DJ
8127 return die->offset;
8128}
8129
8130/* Trivial comparison function for die_info structures: two DIEs
8131 are equal if they have the same offset. */
8132
8133static int
8134die_eq (const void *item_lhs, const void *item_rhs)
8135{
8136 const struct die_info *die_lhs = item_lhs;
8137 const struct die_info *die_rhs = item_rhs;
9a619af0 8138
51545339
DJ
8139 return die_lhs->offset == die_rhs->offset;
8140}
8141
c906108c
SS
8142/* Read a whole compilation unit into a linked list of dies. */
8143
f9aca02d 8144static struct die_info *
93311388 8145read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8146{
93311388 8147 struct die_reader_specs reader_specs;
98bfdba5 8148 int read_abbrevs = 0;
1d9ec526 8149 struct cleanup *back_to = NULL;
98bfdba5
PA
8150 struct die_info *die;
8151
8152 if (cu->dwarf2_abbrevs == NULL)
8153 {
8154 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8155 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8156 read_abbrevs = 1;
8157 }
93311388 8158
348e048f 8159 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8160 cu->die_hash
8161 = htab_create_alloc_ex (cu->header.length / 12,
8162 die_hash,
8163 die_eq,
8164 NULL,
8165 &cu->comp_unit_obstack,
8166 hashtab_obstack_allocate,
8167 dummy_obstack_deallocate);
8168
93311388
DE
8169 init_cu_die_reader (&reader_specs, cu);
8170
98bfdba5
PA
8171 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8172
8173 if (read_abbrevs)
8174 do_cleanups (back_to);
8175
8176 return die;
639d11d3
DC
8177}
8178
d97bc12b
DE
8179/* Main entry point for reading a DIE and all children.
8180 Read the DIE and dump it if requested. */
8181
8182static struct die_info *
93311388
DE
8183read_die_and_children (const struct die_reader_specs *reader,
8184 gdb_byte *info_ptr,
d97bc12b
DE
8185 gdb_byte **new_info_ptr,
8186 struct die_info *parent)
8187{
93311388 8188 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8189 new_info_ptr, parent);
8190
8191 if (dwarf2_die_debug)
8192 {
348e048f
DE
8193 fprintf_unfiltered (gdb_stdlog,
8194 "\nRead die from %s of %s:\n",
8195 reader->buffer == dwarf2_per_objfile->info.buffer
8196 ? ".debug_info"
8197 : reader->buffer == dwarf2_per_objfile->types.buffer
8198 ? ".debug_types"
8199 : "unknown section",
8200 reader->abfd->filename);
d97bc12b
DE
8201 dump_die (result, dwarf2_die_debug);
8202 }
8203
8204 return result;
8205}
8206
639d11d3
DC
8207/* Read a single die and all its descendents. Set the die's sibling
8208 field to NULL; set other fields in the die correctly, and set all
8209 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8210 location of the info_ptr after reading all of those dies. PARENT
8211 is the parent of the die in question. */
8212
8213static struct die_info *
93311388
DE
8214read_die_and_children_1 (const struct die_reader_specs *reader,
8215 gdb_byte *info_ptr,
d97bc12b
DE
8216 gdb_byte **new_info_ptr,
8217 struct die_info *parent)
639d11d3
DC
8218{
8219 struct die_info *die;
fe1b8b76 8220 gdb_byte *cur_ptr;
639d11d3
DC
8221 int has_children;
8222
93311388 8223 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8224 if (die == NULL)
8225 {
8226 *new_info_ptr = cur_ptr;
8227 return NULL;
8228 }
93311388 8229 store_in_ref_table (die, reader->cu);
639d11d3
DC
8230
8231 if (has_children)
348e048f 8232 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8233 else
8234 {
8235 die->child = NULL;
8236 *new_info_ptr = cur_ptr;
8237 }
8238
8239 die->sibling = NULL;
8240 die->parent = parent;
8241 return die;
8242}
8243
8244/* Read a die, all of its descendents, and all of its siblings; set
8245 all of the fields of all of the dies correctly. Arguments are as
8246 in read_die_and_children. */
8247
8248static struct die_info *
93311388
DE
8249read_die_and_siblings (const struct die_reader_specs *reader,
8250 gdb_byte *info_ptr,
fe1b8b76 8251 gdb_byte **new_info_ptr,
639d11d3
DC
8252 struct die_info *parent)
8253{
8254 struct die_info *first_die, *last_sibling;
fe1b8b76 8255 gdb_byte *cur_ptr;
639d11d3 8256
c906108c 8257 cur_ptr = info_ptr;
639d11d3
DC
8258 first_die = last_sibling = NULL;
8259
8260 while (1)
c906108c 8261 {
639d11d3 8262 struct die_info *die
93311388 8263 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8264
1d325ec1 8265 if (die == NULL)
c906108c 8266 {
639d11d3
DC
8267 *new_info_ptr = cur_ptr;
8268 return first_die;
c906108c 8269 }
1d325ec1
DJ
8270
8271 if (!first_die)
8272 first_die = die;
c906108c 8273 else
1d325ec1
DJ
8274 last_sibling->sibling = die;
8275
8276 last_sibling = die;
c906108c 8277 }
c906108c
SS
8278}
8279
93311388
DE
8280/* Read the die from the .debug_info section buffer. Set DIEP to
8281 point to a newly allocated die with its information, except for its
8282 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8283 whether the die has children or not. */
8284
8285static gdb_byte *
8286read_full_die (const struct die_reader_specs *reader,
8287 struct die_info **diep, gdb_byte *info_ptr,
8288 int *has_children)
8289{
8290 unsigned int abbrev_number, bytes_read, i, offset;
8291 struct abbrev_info *abbrev;
8292 struct die_info *die;
8293 struct dwarf2_cu *cu = reader->cu;
8294 bfd *abfd = reader->abfd;
8295
8296 offset = info_ptr - reader->buffer;
8297 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8298 info_ptr += bytes_read;
8299 if (!abbrev_number)
8300 {
8301 *diep = NULL;
8302 *has_children = 0;
8303 return info_ptr;
8304 }
8305
8306 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8307 if (!abbrev)
348e048f
DE
8308 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8309 abbrev_number,
8310 bfd_get_filename (abfd));
8311
93311388
DE
8312 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8313 die->offset = offset;
8314 die->tag = abbrev->tag;
8315 die->abbrev = abbrev_number;
8316
8317 die->num_attrs = abbrev->num_attrs;
8318
8319 for (i = 0; i < abbrev->num_attrs; ++i)
8320 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8321 abfd, info_ptr, cu);
8322
8323 *diep = die;
8324 *has_children = abbrev->has_children;
8325 return info_ptr;
8326}
8327
c906108c
SS
8328/* In DWARF version 2, the description of the debugging information is
8329 stored in a separate .debug_abbrev section. Before we read any
8330 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8331 in a hash table. This function also sets flags in CU describing
8332 the data found in the abbrev table. */
c906108c
SS
8333
8334static void
e7c27a73 8335dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8336{
e7c27a73 8337 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8338 gdb_byte *abbrev_ptr;
c906108c
SS
8339 struct abbrev_info *cur_abbrev;
8340 unsigned int abbrev_number, bytes_read, abbrev_name;
8341 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8342 struct attr_abbrev *cur_attrs;
8343 unsigned int allocated_attrs;
c906108c 8344
57349743 8345 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8346 obstack_init (&cu->abbrev_obstack);
8347 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8348 (ABBREV_HASH_SIZE
8349 * sizeof (struct abbrev_info *)));
8350 memset (cu->dwarf2_abbrevs, 0,
8351 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8352
be391dca
TT
8353 dwarf2_read_section (dwarf2_per_objfile->objfile,
8354 &dwarf2_per_objfile->abbrev);
dce234bc 8355 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8356 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8357 abbrev_ptr += bytes_read;
8358
f3dd6933
DJ
8359 allocated_attrs = ATTR_ALLOC_CHUNK;
8360 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8361
c906108c
SS
8362 /* loop until we reach an abbrev number of 0 */
8363 while (abbrev_number)
8364 {
f3dd6933 8365 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8366
8367 /* read in abbrev header */
8368 cur_abbrev->number = abbrev_number;
8369 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8370 abbrev_ptr += bytes_read;
8371 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8372 abbrev_ptr += 1;
8373
72bf9492
DJ
8374 if (cur_abbrev->tag == DW_TAG_namespace)
8375 cu->has_namespace_info = 1;
8376
c906108c
SS
8377 /* now read in declarations */
8378 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8379 abbrev_ptr += bytes_read;
8380 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8381 abbrev_ptr += bytes_read;
8382 while (abbrev_name)
8383 {
f3dd6933 8384 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8385 {
f3dd6933
DJ
8386 allocated_attrs += ATTR_ALLOC_CHUNK;
8387 cur_attrs
8388 = xrealloc (cur_attrs, (allocated_attrs
8389 * sizeof (struct attr_abbrev)));
c906108c 8390 }
ae038cb0
DJ
8391
8392 /* Record whether this compilation unit might have
8393 inter-compilation-unit references. If we don't know what form
8394 this attribute will have, then it might potentially be a
8395 DW_FORM_ref_addr, so we conservatively expect inter-CU
8396 references. */
8397
8398 if (abbrev_form == DW_FORM_ref_addr
8399 || abbrev_form == DW_FORM_indirect)
8400 cu->has_form_ref_addr = 1;
8401
f3dd6933
DJ
8402 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8403 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8404 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8405 abbrev_ptr += bytes_read;
8406 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8407 abbrev_ptr += bytes_read;
8408 }
8409
f3dd6933
DJ
8410 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8411 (cur_abbrev->num_attrs
8412 * sizeof (struct attr_abbrev)));
8413 memcpy (cur_abbrev->attrs, cur_attrs,
8414 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8415
c906108c 8416 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8417 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8418 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8419
8420 /* Get next abbreviation.
8421 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8422 always properly terminated with an abbrev number of 0.
8423 Exit loop if we encounter an abbreviation which we have
8424 already read (which means we are about to read the abbreviations
8425 for the next compile unit) or if the end of the abbreviation
8426 table is reached. */
dce234bc
PP
8427 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8428 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8429 break;
8430 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8431 abbrev_ptr += bytes_read;
e7c27a73 8432 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8433 break;
8434 }
f3dd6933
DJ
8435
8436 xfree (cur_attrs);
c906108c
SS
8437}
8438
f3dd6933 8439/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8440
c906108c 8441static void
f3dd6933 8442dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8443{
f3dd6933 8444 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8445
f3dd6933
DJ
8446 obstack_free (&cu->abbrev_obstack, NULL);
8447 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8448}
8449
8450/* Lookup an abbrev_info structure in the abbrev hash table. */
8451
8452static struct abbrev_info *
e7c27a73 8453dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8454{
8455 unsigned int hash_number;
8456 struct abbrev_info *abbrev;
8457
8458 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8459 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8460
8461 while (abbrev)
8462 {
8463 if (abbrev->number == number)
8464 return abbrev;
8465 else
8466 abbrev = abbrev->next;
8467 }
8468 return NULL;
8469}
8470
72bf9492
DJ
8471/* Returns nonzero if TAG represents a type that we might generate a partial
8472 symbol for. */
8473
8474static int
8475is_type_tag_for_partial (int tag)
8476{
8477 switch (tag)
8478 {
8479#if 0
8480 /* Some types that would be reasonable to generate partial symbols for,
8481 that we don't at present. */
8482 case DW_TAG_array_type:
8483 case DW_TAG_file_type:
8484 case DW_TAG_ptr_to_member_type:
8485 case DW_TAG_set_type:
8486 case DW_TAG_string_type:
8487 case DW_TAG_subroutine_type:
8488#endif
8489 case DW_TAG_base_type:
8490 case DW_TAG_class_type:
680b30c7 8491 case DW_TAG_interface_type:
72bf9492
DJ
8492 case DW_TAG_enumeration_type:
8493 case DW_TAG_structure_type:
8494 case DW_TAG_subrange_type:
8495 case DW_TAG_typedef:
8496 case DW_TAG_union_type:
8497 return 1;
8498 default:
8499 return 0;
8500 }
8501}
8502
8503/* Load all DIEs that are interesting for partial symbols into memory. */
8504
8505static struct partial_die_info *
93311388
DE
8506load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8507 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8508{
8509 struct partial_die_info *part_die;
8510 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8511 struct abbrev_info *abbrev;
8512 unsigned int bytes_read;
5afb4e99 8513 unsigned int load_all = 0;
72bf9492
DJ
8514
8515 int nesting_level = 1;
8516
8517 parent_die = NULL;
8518 last_die = NULL;
8519
5afb4e99
DJ
8520 if (cu->per_cu && cu->per_cu->load_all_dies)
8521 load_all = 1;
8522
72bf9492
DJ
8523 cu->partial_dies
8524 = htab_create_alloc_ex (cu->header.length / 12,
8525 partial_die_hash,
8526 partial_die_eq,
8527 NULL,
8528 &cu->comp_unit_obstack,
8529 hashtab_obstack_allocate,
8530 dummy_obstack_deallocate);
8531
8532 part_die = obstack_alloc (&cu->comp_unit_obstack,
8533 sizeof (struct partial_die_info));
8534
8535 while (1)
8536 {
8537 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8538
8539 /* A NULL abbrev means the end of a series of children. */
8540 if (abbrev == NULL)
8541 {
8542 if (--nesting_level == 0)
8543 {
8544 /* PART_DIE was probably the last thing allocated on the
8545 comp_unit_obstack, so we could call obstack_free
8546 here. We don't do that because the waste is small,
8547 and will be cleaned up when we're done with this
8548 compilation unit. This way, we're also more robust
8549 against other users of the comp_unit_obstack. */
8550 return first_die;
8551 }
8552 info_ptr += bytes_read;
8553 last_die = parent_die;
8554 parent_die = parent_die->die_parent;
8555 continue;
8556 }
8557
98bfdba5
PA
8558 /* Check for template arguments. We never save these; if
8559 they're seen, we just mark the parent, and go on our way. */
8560 if (parent_die != NULL
8561 && cu->language == language_cplus
8562 && (abbrev->tag == DW_TAG_template_type_param
8563 || abbrev->tag == DW_TAG_template_value_param))
8564 {
8565 parent_die->has_template_arguments = 1;
8566
8567 if (!load_all)
8568 {
8569 /* We don't need a partial DIE for the template argument. */
8570 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8571 cu);
8572 continue;
8573 }
8574 }
8575
8576 /* We only recurse into subprograms looking for template arguments.
8577 Skip their other children. */
8578 if (!load_all
8579 && cu->language == language_cplus
8580 && parent_die != NULL
8581 && parent_die->tag == DW_TAG_subprogram)
8582 {
8583 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8584 continue;
8585 }
8586
5afb4e99
DJ
8587 /* Check whether this DIE is interesting enough to save. Normally
8588 we would not be interested in members here, but there may be
8589 later variables referencing them via DW_AT_specification (for
8590 static members). */
8591 if (!load_all
8592 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8593 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8594 && abbrev->tag != DW_TAG_enumerator
8595 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8596 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8597 && abbrev->tag != DW_TAG_variable
5afb4e99 8598 && abbrev->tag != DW_TAG_namespace
f55ee35c 8599 && abbrev->tag != DW_TAG_module
5afb4e99 8600 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8601 {
8602 /* Otherwise we skip to the next sibling, if any. */
93311388 8603 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8604 continue;
8605 }
8606
93311388
DE
8607 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8608 buffer, info_ptr, cu);
72bf9492
DJ
8609
8610 /* This two-pass algorithm for processing partial symbols has a
8611 high cost in cache pressure. Thus, handle some simple cases
8612 here which cover the majority of C partial symbols. DIEs
8613 which neither have specification tags in them, nor could have
8614 specification tags elsewhere pointing at them, can simply be
8615 processed and discarded.
8616
8617 This segment is also optional; scan_partial_symbols and
8618 add_partial_symbol will handle these DIEs if we chain
8619 them in normally. When compilers which do not emit large
8620 quantities of duplicate debug information are more common,
8621 this code can probably be removed. */
8622
8623 /* Any complete simple types at the top level (pretty much all
8624 of them, for a language without namespaces), can be processed
8625 directly. */
8626 if (parent_die == NULL
8627 && part_die->has_specification == 0
8628 && part_die->is_declaration == 0
8629 && (part_die->tag == DW_TAG_typedef
8630 || part_die->tag == DW_TAG_base_type
8631 || part_die->tag == DW_TAG_subrange_type))
8632 {
8633 if (building_psymtab && part_die->name != NULL)
04a679b8 8634 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8635 VAR_DOMAIN, LOC_TYPEDEF,
8636 &cu->objfile->static_psymbols,
8637 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8638 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8639 continue;
8640 }
8641
8642 /* If we're at the second level, and we're an enumerator, and
8643 our parent has no specification (meaning possibly lives in a
8644 namespace elsewhere), then we can add the partial symbol now
8645 instead of queueing it. */
8646 if (part_die->tag == DW_TAG_enumerator
8647 && parent_die != NULL
8648 && parent_die->die_parent == NULL
8649 && parent_die->tag == DW_TAG_enumeration_type
8650 && parent_die->has_specification == 0)
8651 {
8652 if (part_die->name == NULL)
e2e0b3e5 8653 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8654 else if (building_psymtab)
04a679b8 8655 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8656 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8657 (cu->language == language_cplus
8658 || cu->language == language_java)
72bf9492
DJ
8659 ? &cu->objfile->global_psymbols
8660 : &cu->objfile->static_psymbols,
8661 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8662
93311388 8663 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8664 continue;
8665 }
8666
8667 /* We'll save this DIE so link it in. */
8668 part_die->die_parent = parent_die;
8669 part_die->die_sibling = NULL;
8670 part_die->die_child = NULL;
8671
8672 if (last_die && last_die == parent_die)
8673 last_die->die_child = part_die;
8674 else if (last_die)
8675 last_die->die_sibling = part_die;
8676
8677 last_die = part_die;
8678
8679 if (first_die == NULL)
8680 first_die = part_die;
8681
8682 /* Maybe add the DIE to the hash table. Not all DIEs that we
8683 find interesting need to be in the hash table, because we
8684 also have the parent/sibling/child chains; only those that we
8685 might refer to by offset later during partial symbol reading.
8686
8687 For now this means things that might have be the target of a
8688 DW_AT_specification, DW_AT_abstract_origin, or
8689 DW_AT_extension. DW_AT_extension will refer only to
8690 namespaces; DW_AT_abstract_origin refers to functions (and
8691 many things under the function DIE, but we do not recurse
8692 into function DIEs during partial symbol reading) and
8693 possibly variables as well; DW_AT_specification refers to
8694 declarations. Declarations ought to have the DW_AT_declaration
8695 flag. It happens that GCC forgets to put it in sometimes, but
8696 only for functions, not for types.
8697
8698 Adding more things than necessary to the hash table is harmless
8699 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8700 wasted time in find_partial_die, when we reread the compilation
8701 unit with load_all_dies set. */
72bf9492 8702
5afb4e99 8703 if (load_all
72929c62 8704 || abbrev->tag == DW_TAG_constant
5afb4e99 8705 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8706 || abbrev->tag == DW_TAG_variable
8707 || abbrev->tag == DW_TAG_namespace
8708 || part_die->is_declaration)
8709 {
8710 void **slot;
8711
8712 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8713 part_die->offset, INSERT);
8714 *slot = part_die;
8715 }
8716
8717 part_die = obstack_alloc (&cu->comp_unit_obstack,
8718 sizeof (struct partial_die_info));
8719
8720 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8721 we have no reason to follow the children of structures; for other
98bfdba5
PA
8722 languages we have to, so that we can get at method physnames
8723 to infer fully qualified class names, for DW_AT_specification,
8724 and for C++ template arguments. For C++, we also look one level
8725 inside functions to find template arguments (if the name of the
8726 function does not already contain the template arguments).
bc30ff58
JB
8727
8728 For Ada, we need to scan the children of subprograms and lexical
8729 blocks as well because Ada allows the definition of nested
8730 entities that could be interesting for the debugger, such as
8731 nested subprograms for instance. */
72bf9492 8732 if (last_die->has_children
5afb4e99
DJ
8733 && (load_all
8734 || last_die->tag == DW_TAG_namespace
f55ee35c 8735 || last_die->tag == DW_TAG_module
72bf9492 8736 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8737 || (cu->language == language_cplus
8738 && last_die->tag == DW_TAG_subprogram
8739 && (last_die->name == NULL
8740 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8741 || (cu->language != language_c
8742 && (last_die->tag == DW_TAG_class_type
680b30c7 8743 || last_die->tag == DW_TAG_interface_type
72bf9492 8744 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8745 || last_die->tag == DW_TAG_union_type))
8746 || (cu->language == language_ada
8747 && (last_die->tag == DW_TAG_subprogram
8748 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8749 {
8750 nesting_level++;
8751 parent_die = last_die;
8752 continue;
8753 }
8754
8755 /* Otherwise we skip to the next sibling, if any. */
93311388 8756 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8757
8758 /* Back to the top, do it again. */
8759 }
8760}
8761
c906108c
SS
8762/* Read a minimal amount of information into the minimal die structure. */
8763
fe1b8b76 8764static gdb_byte *
72bf9492
DJ
8765read_partial_die (struct partial_die_info *part_die,
8766 struct abbrev_info *abbrev,
8767 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8768 gdb_byte *buffer, gdb_byte *info_ptr,
8769 struct dwarf2_cu *cu)
c906108c 8770{
fa238c03 8771 unsigned int i;
c906108c 8772 struct attribute attr;
c5aa993b 8773 int has_low_pc_attr = 0;
c906108c
SS
8774 int has_high_pc_attr = 0;
8775
72bf9492 8776 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8777
93311388 8778 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8779
8780 info_ptr += abbrev_len;
8781
8782 if (abbrev == NULL)
8783 return info_ptr;
8784
c906108c
SS
8785 part_die->tag = abbrev->tag;
8786 part_die->has_children = abbrev->has_children;
c906108c
SS
8787
8788 for (i = 0; i < abbrev->num_attrs; ++i)
8789 {
e7c27a73 8790 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8791
8792 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8793 partial symbol table. */
c906108c
SS
8794 switch (attr.name)
8795 {
8796 case DW_AT_name:
71c25dea
TT
8797 switch (part_die->tag)
8798 {
8799 case DW_TAG_compile_unit:
348e048f 8800 case DW_TAG_type_unit:
71c25dea
TT
8801 /* Compilation units have a DW_AT_name that is a filename, not
8802 a source language identifier. */
8803 case DW_TAG_enumeration_type:
8804 case DW_TAG_enumerator:
8805 /* These tags always have simple identifiers already; no need
8806 to canonicalize them. */
8807 part_die->name = DW_STRING (&attr);
8808 break;
8809 default:
8810 part_die->name
8811 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8812 &cu->objfile->objfile_obstack);
71c25dea
TT
8813 break;
8814 }
c906108c 8815 break;
31ef98ae 8816 case DW_AT_linkage_name:
c906108c 8817 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8818 /* Note that both forms of linkage name might appear. We
8819 assume they will be the same, and we only store the last
8820 one we see. */
94af9270
KS
8821 if (cu->language == language_ada)
8822 part_die->name = DW_STRING (&attr);
abc72ce4 8823 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8824 break;
8825 case DW_AT_low_pc:
8826 has_low_pc_attr = 1;
8827 part_die->lowpc = DW_ADDR (&attr);
8828 break;
8829 case DW_AT_high_pc:
8830 has_high_pc_attr = 1;
8831 part_die->highpc = DW_ADDR (&attr);
8832 break;
8833 case DW_AT_location:
8e19ed76
PS
8834 /* Support the .debug_loc offsets */
8835 if (attr_form_is_block (&attr))
8836 {
8837 part_die->locdesc = DW_BLOCK (&attr);
8838 }
3690dd37 8839 else if (attr_form_is_section_offset (&attr))
8e19ed76 8840 {
4d3c2250 8841 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8842 }
8843 else
8844 {
4d3c2250
KB
8845 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8846 "partial symbol information");
8e19ed76 8847 }
c906108c 8848 break;
c906108c
SS
8849 case DW_AT_external:
8850 part_die->is_external = DW_UNSND (&attr);
8851 break;
8852 case DW_AT_declaration:
8853 part_die->is_declaration = DW_UNSND (&attr);
8854 break;
8855 case DW_AT_type:
8856 part_die->has_type = 1;
8857 break;
8858 case DW_AT_abstract_origin:
8859 case DW_AT_specification:
72bf9492
DJ
8860 case DW_AT_extension:
8861 part_die->has_specification = 1;
c764a876 8862 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8863 break;
8864 case DW_AT_sibling:
8865 /* Ignore absolute siblings, they might point outside of
8866 the current compile unit. */
8867 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8868 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8869 else
93311388 8870 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8871 break;
fa4028e9
JB
8872 case DW_AT_byte_size:
8873 part_die->has_byte_size = 1;
8874 break;
68511cec
CES
8875 case DW_AT_calling_convention:
8876 /* DWARF doesn't provide a way to identify a program's source-level
8877 entry point. DW_AT_calling_convention attributes are only meant
8878 to describe functions' calling conventions.
8879
8880 However, because it's a necessary piece of information in
8881 Fortran, and because DW_CC_program is the only piece of debugging
8882 information whose definition refers to a 'main program' at all,
8883 several compilers have begun marking Fortran main programs with
8884 DW_CC_program --- even when those functions use the standard
8885 calling conventions.
8886
8887 So until DWARF specifies a way to provide this information and
8888 compilers pick up the new representation, we'll support this
8889 practice. */
8890 if (DW_UNSND (&attr) == DW_CC_program
8891 && cu->language == language_fortran)
01f8c46d
JK
8892 {
8893 set_main_name (part_die->name);
8894
8895 /* As this DIE has a static linkage the name would be difficult
8896 to look up later. */
8897 language_of_main = language_fortran;
8898 }
68511cec 8899 break;
c906108c
SS
8900 default:
8901 break;
8902 }
8903 }
8904
c906108c
SS
8905 /* When using the GNU linker, .gnu.linkonce. sections are used to
8906 eliminate duplicate copies of functions and vtables and such.
8907 The linker will arbitrarily choose one and discard the others.
8908 The AT_*_pc values for such functions refer to local labels in
8909 these sections. If the section from that file was discarded, the
8910 labels are not in the output, so the relocs get a value of 0.
8911 If this is a discarded function, mark the pc bounds as invalid,
8912 so that GDB will ignore it. */
8913 if (has_low_pc_attr && has_high_pc_attr
8914 && part_die->lowpc < part_die->highpc
8915 && (part_die->lowpc != 0
72dca2f5 8916 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8917 part_die->has_pc_info = 1;
85cbf3d3 8918
c906108c
SS
8919 return info_ptr;
8920}
8921
72bf9492
DJ
8922/* Find a cached partial DIE at OFFSET in CU. */
8923
8924static struct partial_die_info *
c764a876 8925find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8926{
8927 struct partial_die_info *lookup_die = NULL;
8928 struct partial_die_info part_die;
8929
8930 part_die.offset = offset;
8931 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8932
72bf9492
DJ
8933 return lookup_die;
8934}
8935
348e048f
DE
8936/* Find a partial DIE at OFFSET, which may or may not be in CU,
8937 except in the case of .debug_types DIEs which do not reference
8938 outside their CU (they do however referencing other types via
8939 DW_FORM_sig8). */
72bf9492
DJ
8940
8941static struct partial_die_info *
c764a876 8942find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8943{
5afb4e99
DJ
8944 struct dwarf2_per_cu_data *per_cu = NULL;
8945 struct partial_die_info *pd = NULL;
72bf9492 8946
348e048f
DE
8947 if (cu->per_cu->from_debug_types)
8948 {
8949 pd = find_partial_die_in_comp_unit (offset, cu);
8950 if (pd != NULL)
8951 return pd;
8952 goto not_found;
8953 }
8954
45452591 8955 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8956 {
8957 pd = find_partial_die_in_comp_unit (offset, cu);
8958 if (pd != NULL)
8959 return pd;
8960 }
72bf9492 8961
ae038cb0
DJ
8962 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8963
98bfdba5
PA
8964 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8965 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8966
8967 per_cu->cu->last_used = 0;
5afb4e99
DJ
8968 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8969
8970 if (pd == NULL && per_cu->load_all_dies == 0)
8971 {
8972 struct cleanup *back_to;
8973 struct partial_die_info comp_unit_die;
8974 struct abbrev_info *abbrev;
8975 unsigned int bytes_read;
8976 char *info_ptr;
8977
8978 per_cu->load_all_dies = 1;
8979
8980 /* Re-read the DIEs. */
8981 back_to = make_cleanup (null_cleanup, 0);
8982 if (per_cu->cu->dwarf2_abbrevs == NULL)
8983 {
8984 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8985 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8986 }
dce234bc 8987 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8988 + per_cu->cu->header.offset
8989 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8990 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8991 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8992 per_cu->cu->objfile->obfd,
8993 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8994 per_cu->cu);
8995 if (comp_unit_die.has_children)
93311388
DE
8996 load_partial_dies (per_cu->cu->objfile->obfd,
8997 dwarf2_per_objfile->info.buffer, info_ptr,
8998 0, per_cu->cu);
5afb4e99
DJ
8999 do_cleanups (back_to);
9000
9001 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9002 }
9003
348e048f
DE
9004 not_found:
9005
5afb4e99
DJ
9006 if (pd == NULL)
9007 internal_error (__FILE__, __LINE__,
c764a876 9008 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
9009 offset, bfd_get_filename (cu->objfile->obfd));
9010 return pd;
72bf9492
DJ
9011}
9012
abc72ce4
DE
9013/* See if we can figure out if the class lives in a namespace. We do
9014 this by looking for a member function; its demangled name will
9015 contain namespace info, if there is any. */
9016
9017static void
9018guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9019 struct dwarf2_cu *cu)
9020{
9021 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9022 what template types look like, because the demangler
9023 frequently doesn't give the same name as the debug info. We
9024 could fix this by only using the demangled name to get the
9025 prefix (but see comment in read_structure_type). */
9026
9027 struct partial_die_info *real_pdi;
9028 struct partial_die_info *child_pdi;
9029
9030 /* If this DIE (this DIE's specification, if any) has a parent, then
9031 we should not do this. We'll prepend the parent's fully qualified
9032 name when we create the partial symbol. */
9033
9034 real_pdi = struct_pdi;
9035 while (real_pdi->has_specification)
9036 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9037
9038 if (real_pdi->die_parent != NULL)
9039 return;
9040
9041 for (child_pdi = struct_pdi->die_child;
9042 child_pdi != NULL;
9043 child_pdi = child_pdi->die_sibling)
9044 {
9045 if (child_pdi->tag == DW_TAG_subprogram
9046 && child_pdi->linkage_name != NULL)
9047 {
9048 char *actual_class_name
9049 = language_class_name_from_physname (cu->language_defn,
9050 child_pdi->linkage_name);
9051 if (actual_class_name != NULL)
9052 {
9053 struct_pdi->name
9054 = obsavestring (actual_class_name,
9055 strlen (actual_class_name),
9056 &cu->objfile->objfile_obstack);
9057 xfree (actual_class_name);
9058 }
9059 break;
9060 }
9061 }
9062}
9063
72bf9492
DJ
9064/* Adjust PART_DIE before generating a symbol for it. This function
9065 may set the is_external flag or change the DIE's name. */
9066
9067static void
9068fixup_partial_die (struct partial_die_info *part_die,
9069 struct dwarf2_cu *cu)
9070{
abc72ce4
DE
9071 /* Once we've fixed up a die, there's no point in doing so again.
9072 This also avoids a memory leak if we were to call
9073 guess_partial_die_structure_name multiple times. */
9074 if (part_die->fixup_called)
9075 return;
9076
72bf9492
DJ
9077 /* If we found a reference attribute and the DIE has no name, try
9078 to find a name in the referred to DIE. */
9079
9080 if (part_die->name == NULL && part_die->has_specification)
9081 {
9082 struct partial_die_info *spec_die;
72bf9492 9083
10b3939b 9084 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9085
10b3939b 9086 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9087
9088 if (spec_die->name)
9089 {
9090 part_die->name = spec_die->name;
9091
9092 /* Copy DW_AT_external attribute if it is set. */
9093 if (spec_die->is_external)
9094 part_die->is_external = spec_die->is_external;
9095 }
9096 }
9097
9098 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9099
9100 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9101 part_die->name = "(anonymous namespace)";
9102
abc72ce4
DE
9103 /* If there is no parent die to provide a namespace, and there are
9104 children, see if we can determine the namespace from their linkage
9105 name.
9106 NOTE: We need to do this even if cu->has_namespace_info != 0.
9107 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9108 if (cu->language == language_cplus
9109 && dwarf2_per_objfile->types.asection != NULL
9110 && part_die->die_parent == NULL
9111 && part_die->has_children
9112 && (part_die->tag == DW_TAG_class_type
9113 || part_die->tag == DW_TAG_structure_type
9114 || part_die->tag == DW_TAG_union_type))
9115 guess_partial_die_structure_name (part_die, cu);
9116
9117 part_die->fixup_called = 1;
72bf9492
DJ
9118}
9119
a8329558 9120/* Read an attribute value described by an attribute form. */
c906108c 9121
fe1b8b76 9122static gdb_byte *
a8329558 9123read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9124 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9125 struct dwarf2_cu *cu)
c906108c 9126{
e7c27a73 9127 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9128 unsigned int bytes_read;
9129 struct dwarf_block *blk;
9130
a8329558
KW
9131 attr->form = form;
9132 switch (form)
c906108c 9133 {
c906108c 9134 case DW_FORM_ref_addr:
ae411497
TT
9135 if (cu->header.version == 2)
9136 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9137 else
9138 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9139 info_ptr += bytes_read;
9140 break;
9141 case DW_FORM_addr:
e7c27a73 9142 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9143 info_ptr += bytes_read;
c906108c
SS
9144 break;
9145 case DW_FORM_block2:
7b5a2f43 9146 blk = dwarf_alloc_block (cu);
c906108c
SS
9147 blk->size = read_2_bytes (abfd, info_ptr);
9148 info_ptr += 2;
9149 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9150 info_ptr += blk->size;
9151 DW_BLOCK (attr) = blk;
9152 break;
9153 case DW_FORM_block4:
7b5a2f43 9154 blk = dwarf_alloc_block (cu);
c906108c
SS
9155 blk->size = read_4_bytes (abfd, info_ptr);
9156 info_ptr += 4;
9157 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9158 info_ptr += blk->size;
9159 DW_BLOCK (attr) = blk;
9160 break;
9161 case DW_FORM_data2:
9162 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9163 info_ptr += 2;
9164 break;
9165 case DW_FORM_data4:
9166 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9167 info_ptr += 4;
9168 break;
9169 case DW_FORM_data8:
9170 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9171 info_ptr += 8;
9172 break;
2dc7f7b3
TT
9173 case DW_FORM_sec_offset:
9174 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9175 info_ptr += bytes_read;
9176 break;
c906108c 9177 case DW_FORM_string:
9b1c24c8 9178 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9179 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9180 info_ptr += bytes_read;
9181 break;
4bdf3d34
JJ
9182 case DW_FORM_strp:
9183 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9184 &bytes_read);
8285870a 9185 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9186 info_ptr += bytes_read;
9187 break;
2dc7f7b3 9188 case DW_FORM_exprloc:
c906108c 9189 case DW_FORM_block:
7b5a2f43 9190 blk = dwarf_alloc_block (cu);
c906108c
SS
9191 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9192 info_ptr += bytes_read;
9193 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9194 info_ptr += blk->size;
9195 DW_BLOCK (attr) = blk;
9196 break;
9197 case DW_FORM_block1:
7b5a2f43 9198 blk = dwarf_alloc_block (cu);
c906108c
SS
9199 blk->size = read_1_byte (abfd, info_ptr);
9200 info_ptr += 1;
9201 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9202 info_ptr += blk->size;
9203 DW_BLOCK (attr) = blk;
9204 break;
9205 case DW_FORM_data1:
9206 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9207 info_ptr += 1;
9208 break;
9209 case DW_FORM_flag:
9210 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9211 info_ptr += 1;
9212 break;
2dc7f7b3
TT
9213 case DW_FORM_flag_present:
9214 DW_UNSND (attr) = 1;
9215 break;
c906108c
SS
9216 case DW_FORM_sdata:
9217 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9218 info_ptr += bytes_read;
9219 break;
9220 case DW_FORM_udata:
9221 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9222 info_ptr += bytes_read;
9223 break;
9224 case DW_FORM_ref1:
10b3939b 9225 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9226 info_ptr += 1;
9227 break;
9228 case DW_FORM_ref2:
10b3939b 9229 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9230 info_ptr += 2;
9231 break;
9232 case DW_FORM_ref4:
10b3939b 9233 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9234 info_ptr += 4;
9235 break;
613e1657 9236 case DW_FORM_ref8:
10b3939b 9237 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9238 info_ptr += 8;
9239 break;
348e048f
DE
9240 case DW_FORM_sig8:
9241 /* Convert the signature to something we can record in DW_UNSND
9242 for later lookup.
9243 NOTE: This is NULL if the type wasn't found. */
9244 DW_SIGNATURED_TYPE (attr) =
9245 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9246 info_ptr += 8;
9247 break;
c906108c 9248 case DW_FORM_ref_udata:
10b3939b
DJ
9249 DW_ADDR (attr) = (cu->header.offset
9250 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9251 info_ptr += bytes_read;
9252 break;
c906108c 9253 case DW_FORM_indirect:
a8329558
KW
9254 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9255 info_ptr += bytes_read;
e7c27a73 9256 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9257 break;
c906108c 9258 default:
8a3fe4f8 9259 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9260 dwarf_form_name (form),
9261 bfd_get_filename (abfd));
c906108c 9262 }
28e94949
JB
9263
9264 /* We have seen instances where the compiler tried to emit a byte
9265 size attribute of -1 which ended up being encoded as an unsigned
9266 0xffffffff. Although 0xffffffff is technically a valid size value,
9267 an object of this size seems pretty unlikely so we can relatively
9268 safely treat these cases as if the size attribute was invalid and
9269 treat them as zero by default. */
9270 if (attr->name == DW_AT_byte_size
9271 && form == DW_FORM_data4
9272 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9273 {
9274 complaint
9275 (&symfile_complaints,
43bbcdc2
PH
9276 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9277 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9278 DW_UNSND (attr) = 0;
9279 }
28e94949 9280
c906108c
SS
9281 return info_ptr;
9282}
9283
a8329558
KW
9284/* Read an attribute described by an abbreviated attribute. */
9285
fe1b8b76 9286static gdb_byte *
a8329558 9287read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9288 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9289{
9290 attr->name = abbrev->name;
e7c27a73 9291 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9292}
9293
c906108c
SS
9294/* read dwarf information from a buffer */
9295
9296static unsigned int
fe1b8b76 9297read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9298{
fe1b8b76 9299 return bfd_get_8 (abfd, buf);
c906108c
SS
9300}
9301
9302static int
fe1b8b76 9303read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9304{
fe1b8b76 9305 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9306}
9307
9308static unsigned int
fe1b8b76 9309read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9310{
fe1b8b76 9311 return bfd_get_16 (abfd, buf);
c906108c
SS
9312}
9313
9314static int
fe1b8b76 9315read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9316{
fe1b8b76 9317 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9318}
9319
9320static unsigned int
fe1b8b76 9321read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9322{
fe1b8b76 9323 return bfd_get_32 (abfd, buf);
c906108c
SS
9324}
9325
9326static int
fe1b8b76 9327read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9328{
fe1b8b76 9329 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9330}
9331
93311388 9332static ULONGEST
fe1b8b76 9333read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9334{
fe1b8b76 9335 return bfd_get_64 (abfd, buf);
c906108c
SS
9336}
9337
9338static CORE_ADDR
fe1b8b76 9339read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9340 unsigned int *bytes_read)
c906108c 9341{
e7c27a73 9342 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9343 CORE_ADDR retval = 0;
9344
107d2387 9345 if (cu_header->signed_addr_p)
c906108c 9346 {
107d2387
AC
9347 switch (cu_header->addr_size)
9348 {
9349 case 2:
fe1b8b76 9350 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9351 break;
9352 case 4:
fe1b8b76 9353 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9354 break;
9355 case 8:
fe1b8b76 9356 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9357 break;
9358 default:
8e65ff28 9359 internal_error (__FILE__, __LINE__,
e2e0b3e5 9360 _("read_address: bad switch, signed [in module %s]"),
659b0389 9361 bfd_get_filename (abfd));
107d2387
AC
9362 }
9363 }
9364 else
9365 {
9366 switch (cu_header->addr_size)
9367 {
9368 case 2:
fe1b8b76 9369 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9370 break;
9371 case 4:
fe1b8b76 9372 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9373 break;
9374 case 8:
fe1b8b76 9375 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9376 break;
9377 default:
8e65ff28 9378 internal_error (__FILE__, __LINE__,
e2e0b3e5 9379 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9380 bfd_get_filename (abfd));
107d2387 9381 }
c906108c 9382 }
64367e0a 9383
107d2387
AC
9384 *bytes_read = cu_header->addr_size;
9385 return retval;
c906108c
SS
9386}
9387
f7ef9339 9388/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9389 specification allows the initial length to take up either 4 bytes
9390 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9391 bytes describe the length and all offsets will be 8 bytes in length
9392 instead of 4.
9393
f7ef9339
KB
9394 An older, non-standard 64-bit format is also handled by this
9395 function. The older format in question stores the initial length
9396 as an 8-byte quantity without an escape value. Lengths greater
9397 than 2^32 aren't very common which means that the initial 4 bytes
9398 is almost always zero. Since a length value of zero doesn't make
9399 sense for the 32-bit format, this initial zero can be considered to
9400 be an escape value which indicates the presence of the older 64-bit
9401 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9402 greater than 4GB. If it becomes necessary to handle lengths
9403 somewhat larger than 4GB, we could allow other small values (such
9404 as the non-sensical values of 1, 2, and 3) to also be used as
9405 escape values indicating the presence of the old format.
f7ef9339 9406
917c78fc
MK
9407 The value returned via bytes_read should be used to increment the
9408 relevant pointer after calling read_initial_length().
c764a876 9409
613e1657
KB
9410 [ Note: read_initial_length() and read_offset() are based on the
9411 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9412 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9413 from:
9414
f7ef9339 9415 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9416
613e1657
KB
9417 This document is only a draft and is subject to change. (So beware.)
9418
f7ef9339 9419 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9420 determined empirically by examining 64-bit ELF files produced by
9421 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9422
9423 - Kevin, July 16, 2002
613e1657
KB
9424 ] */
9425
9426static LONGEST
c764a876 9427read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9428{
fe1b8b76 9429 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9430
dd373385 9431 if (length == 0xffffffff)
613e1657 9432 {
fe1b8b76 9433 length = bfd_get_64 (abfd, buf + 4);
613e1657 9434 *bytes_read = 12;
613e1657 9435 }
dd373385 9436 else if (length == 0)
f7ef9339 9437 {
dd373385 9438 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9439 length = bfd_get_64 (abfd, buf);
f7ef9339 9440 *bytes_read = 8;
f7ef9339 9441 }
613e1657
KB
9442 else
9443 {
9444 *bytes_read = 4;
613e1657
KB
9445 }
9446
c764a876
DE
9447 return length;
9448}
dd373385 9449
c764a876
DE
9450/* Cover function for read_initial_length.
9451 Returns the length of the object at BUF, and stores the size of the
9452 initial length in *BYTES_READ and stores the size that offsets will be in
9453 *OFFSET_SIZE.
9454 If the initial length size is not equivalent to that specified in
9455 CU_HEADER then issue a complaint.
9456 This is useful when reading non-comp-unit headers. */
dd373385 9457
c764a876
DE
9458static LONGEST
9459read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9460 const struct comp_unit_head *cu_header,
9461 unsigned int *bytes_read,
9462 unsigned int *offset_size)
9463{
9464 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9465
9466 gdb_assert (cu_header->initial_length_size == 4
9467 || cu_header->initial_length_size == 8
9468 || cu_header->initial_length_size == 12);
9469
9470 if (cu_header->initial_length_size != *bytes_read)
9471 complaint (&symfile_complaints,
9472 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9473
c764a876 9474 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9475 return length;
613e1657
KB
9476}
9477
9478/* Read an offset from the data stream. The size of the offset is
917c78fc 9479 given by cu_header->offset_size. */
613e1657
KB
9480
9481static LONGEST
fe1b8b76 9482read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9483 unsigned int *bytes_read)
c764a876
DE
9484{
9485 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9486
c764a876
DE
9487 *bytes_read = cu_header->offset_size;
9488 return offset;
9489}
9490
9491/* Read an offset from the data stream. */
9492
9493static LONGEST
9494read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9495{
9496 LONGEST retval = 0;
9497
c764a876 9498 switch (offset_size)
613e1657
KB
9499 {
9500 case 4:
fe1b8b76 9501 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9502 break;
9503 case 8:
fe1b8b76 9504 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9505 break;
9506 default:
8e65ff28 9507 internal_error (__FILE__, __LINE__,
c764a876 9508 _("read_offset_1: bad switch [in module %s]"),
659b0389 9509 bfd_get_filename (abfd));
613e1657
KB
9510 }
9511
917c78fc 9512 return retval;
613e1657
KB
9513}
9514
fe1b8b76
JB
9515static gdb_byte *
9516read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9517{
9518 /* If the size of a host char is 8 bits, we can return a pointer
9519 to the buffer, otherwise we have to copy the data to a buffer
9520 allocated on the temporary obstack. */
4bdf3d34 9521 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9522 return buf;
c906108c
SS
9523}
9524
9525static char *
9b1c24c8 9526read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9527{
9528 /* If the size of a host char is 8 bits, we can return a pointer
9529 to the string, otherwise we have to copy the string to a buffer
9530 allocated on the temporary obstack. */
4bdf3d34 9531 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9532 if (*buf == '\0')
9533 {
9534 *bytes_read_ptr = 1;
9535 return NULL;
9536 }
fe1b8b76
JB
9537 *bytes_read_ptr = strlen ((char *) buf) + 1;
9538 return (char *) buf;
4bdf3d34
JJ
9539}
9540
9541static char *
fe1b8b76 9542read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9543 const struct comp_unit_head *cu_header,
9544 unsigned int *bytes_read_ptr)
9545{
c764a876 9546 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9547
be391dca 9548 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9549 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9550 {
8a3fe4f8 9551 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9552 bfd_get_filename (abfd));
4bdf3d34 9553 return NULL;
c906108c 9554 }
dce234bc 9555 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9556 {
8a3fe4f8 9557 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9558 bfd_get_filename (abfd));
c906108c
SS
9559 return NULL;
9560 }
4bdf3d34 9561 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9562 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9563 return NULL;
dce234bc 9564 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9565}
9566
ce5d95e1 9567static unsigned long
fe1b8b76 9568read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9569{
ce5d95e1
JB
9570 unsigned long result;
9571 unsigned int num_read;
c906108c
SS
9572 int i, shift;
9573 unsigned char byte;
9574
9575 result = 0;
9576 shift = 0;
9577 num_read = 0;
9578 i = 0;
9579 while (1)
9580 {
fe1b8b76 9581 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9582 buf++;
9583 num_read++;
ce5d95e1 9584 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9585 if ((byte & 128) == 0)
9586 {
9587 break;
9588 }
9589 shift += 7;
9590 }
9591 *bytes_read_ptr = num_read;
9592 return result;
9593}
9594
ce5d95e1 9595static long
fe1b8b76 9596read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9597{
ce5d95e1 9598 long result;
77e0b926 9599 int i, shift, num_read;
c906108c
SS
9600 unsigned char byte;
9601
9602 result = 0;
9603 shift = 0;
c906108c
SS
9604 num_read = 0;
9605 i = 0;
9606 while (1)
9607 {
fe1b8b76 9608 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9609 buf++;
9610 num_read++;
ce5d95e1 9611 result |= ((long)(byte & 127) << shift);
c906108c
SS
9612 shift += 7;
9613 if ((byte & 128) == 0)
9614 {
9615 break;
9616 }
9617 }
77e0b926
DJ
9618 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9619 result |= -(((long)1) << shift);
c906108c
SS
9620 *bytes_read_ptr = num_read;
9621 return result;
9622}
9623
4bb7a0a7
DJ
9624/* Return a pointer to just past the end of an LEB128 number in BUF. */
9625
fe1b8b76
JB
9626static gdb_byte *
9627skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9628{
9629 int byte;
9630
9631 while (1)
9632 {
fe1b8b76 9633 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9634 buf++;
9635 if ((byte & 128) == 0)
9636 return buf;
9637 }
9638}
9639
c906108c 9640static void
e142c38c 9641set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9642{
9643 switch (lang)
9644 {
9645 case DW_LANG_C89:
76bee0cc 9646 case DW_LANG_C99:
c906108c 9647 case DW_LANG_C:
e142c38c 9648 cu->language = language_c;
c906108c
SS
9649 break;
9650 case DW_LANG_C_plus_plus:
e142c38c 9651 cu->language = language_cplus;
c906108c 9652 break;
6aecb9c2
JB
9653 case DW_LANG_D:
9654 cu->language = language_d;
9655 break;
c906108c
SS
9656 case DW_LANG_Fortran77:
9657 case DW_LANG_Fortran90:
b21b22e0 9658 case DW_LANG_Fortran95:
e142c38c 9659 cu->language = language_fortran;
c906108c
SS
9660 break;
9661 case DW_LANG_Mips_Assembler:
e142c38c 9662 cu->language = language_asm;
c906108c 9663 break;
bebd888e 9664 case DW_LANG_Java:
e142c38c 9665 cu->language = language_java;
bebd888e 9666 break;
c906108c 9667 case DW_LANG_Ada83:
8aaf0b47 9668 case DW_LANG_Ada95:
bc5f45f8
JB
9669 cu->language = language_ada;
9670 break;
72019c9c
GM
9671 case DW_LANG_Modula2:
9672 cu->language = language_m2;
9673 break;
fe8e67fd
PM
9674 case DW_LANG_Pascal83:
9675 cu->language = language_pascal;
9676 break;
22566fbd
DJ
9677 case DW_LANG_ObjC:
9678 cu->language = language_objc;
9679 break;
c906108c
SS
9680 case DW_LANG_Cobol74:
9681 case DW_LANG_Cobol85:
c906108c 9682 default:
e142c38c 9683 cu->language = language_minimal;
c906108c
SS
9684 break;
9685 }
e142c38c 9686 cu->language_defn = language_def (cu->language);
c906108c
SS
9687}
9688
9689/* Return the named attribute or NULL if not there. */
9690
9691static struct attribute *
e142c38c 9692dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9693{
9694 unsigned int i;
9695 struct attribute *spec = NULL;
9696
9697 for (i = 0; i < die->num_attrs; ++i)
9698 {
9699 if (die->attrs[i].name == name)
10b3939b 9700 return &die->attrs[i];
c906108c
SS
9701 if (die->attrs[i].name == DW_AT_specification
9702 || die->attrs[i].name == DW_AT_abstract_origin)
9703 spec = &die->attrs[i];
9704 }
c906108c 9705
10b3939b 9706 if (spec)
f2f0e013
DJ
9707 {
9708 die = follow_die_ref (die, spec, &cu);
9709 return dwarf2_attr (die, name, cu);
9710 }
c5aa993b 9711
c906108c
SS
9712 return NULL;
9713}
9714
348e048f
DE
9715/* Return the named attribute or NULL if not there,
9716 but do not follow DW_AT_specification, etc.
9717 This is for use in contexts where we're reading .debug_types dies.
9718 Following DW_AT_specification, DW_AT_abstract_origin will take us
9719 back up the chain, and we want to go down. */
9720
9721static struct attribute *
9722dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9723 struct dwarf2_cu *cu)
9724{
9725 unsigned int i;
9726
9727 for (i = 0; i < die->num_attrs; ++i)
9728 if (die->attrs[i].name == name)
9729 return &die->attrs[i];
9730
9731 return NULL;
9732}
9733
05cf31d1
JB
9734/* Return non-zero iff the attribute NAME is defined for the given DIE,
9735 and holds a non-zero value. This function should only be used for
2dc7f7b3 9736 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9737
9738static int
9739dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9740{
9741 struct attribute *attr = dwarf2_attr (die, name, cu);
9742
9743 return (attr && DW_UNSND (attr));
9744}
9745
3ca72b44 9746static int
e142c38c 9747die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9748{
05cf31d1
JB
9749 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9750 which value is non-zero. However, we have to be careful with
9751 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9752 (via dwarf2_flag_true_p) follows this attribute. So we may
9753 end up accidently finding a declaration attribute that belongs
9754 to a different DIE referenced by the specification attribute,
9755 even though the given DIE does not have a declaration attribute. */
9756 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9757 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9758}
9759
63d06c5c 9760/* Return the die giving the specification for DIE, if there is
f2f0e013 9761 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9762 containing the return value on output. If there is no
9763 specification, but there is an abstract origin, that is
9764 returned. */
63d06c5c
DC
9765
9766static struct die_info *
f2f0e013 9767die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9768{
f2f0e013
DJ
9769 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9770 *spec_cu);
63d06c5c 9771
edb3359d
DJ
9772 if (spec_attr == NULL)
9773 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9774
63d06c5c
DC
9775 if (spec_attr == NULL)
9776 return NULL;
9777 else
f2f0e013 9778 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9779}
c906108c 9780
debd256d
JB
9781/* Free the line_header structure *LH, and any arrays and strings it
9782 refers to. */
9783static void
9784free_line_header (struct line_header *lh)
9785{
9786 if (lh->standard_opcode_lengths)
a8bc7b56 9787 xfree (lh->standard_opcode_lengths);
debd256d
JB
9788
9789 /* Remember that all the lh->file_names[i].name pointers are
9790 pointers into debug_line_buffer, and don't need to be freed. */
9791 if (lh->file_names)
a8bc7b56 9792 xfree (lh->file_names);
debd256d
JB
9793
9794 /* Similarly for the include directory names. */
9795 if (lh->include_dirs)
a8bc7b56 9796 xfree (lh->include_dirs);
debd256d 9797
a8bc7b56 9798 xfree (lh);
debd256d
JB
9799}
9800
9801
9802/* Add an entry to LH's include directory table. */
9803static void
9804add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9805{
debd256d
JB
9806 /* Grow the array if necessary. */
9807 if (lh->include_dirs_size == 0)
c5aa993b 9808 {
debd256d
JB
9809 lh->include_dirs_size = 1; /* for testing */
9810 lh->include_dirs = xmalloc (lh->include_dirs_size
9811 * sizeof (*lh->include_dirs));
9812 }
9813 else if (lh->num_include_dirs >= lh->include_dirs_size)
9814 {
9815 lh->include_dirs_size *= 2;
9816 lh->include_dirs = xrealloc (lh->include_dirs,
9817 (lh->include_dirs_size
9818 * sizeof (*lh->include_dirs)));
c5aa993b 9819 }
c906108c 9820
debd256d
JB
9821 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9822}
6e70227d 9823
debd256d
JB
9824
9825/* Add an entry to LH's file name table. */
9826static void
9827add_file_name (struct line_header *lh,
9828 char *name,
9829 unsigned int dir_index,
9830 unsigned int mod_time,
9831 unsigned int length)
9832{
9833 struct file_entry *fe;
9834
9835 /* Grow the array if necessary. */
9836 if (lh->file_names_size == 0)
9837 {
9838 lh->file_names_size = 1; /* for testing */
9839 lh->file_names = xmalloc (lh->file_names_size
9840 * sizeof (*lh->file_names));
9841 }
9842 else if (lh->num_file_names >= lh->file_names_size)
9843 {
9844 lh->file_names_size *= 2;
9845 lh->file_names = xrealloc (lh->file_names,
9846 (lh->file_names_size
9847 * sizeof (*lh->file_names)));
9848 }
9849
9850 fe = &lh->file_names[lh->num_file_names++];
9851 fe->name = name;
9852 fe->dir_index = dir_index;
9853 fe->mod_time = mod_time;
9854 fe->length = length;
aaa75496 9855 fe->included_p = 0;
cb1df416 9856 fe->symtab = NULL;
debd256d 9857}
6e70227d 9858
debd256d
JB
9859
9860/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9861 .debug_line, according to the endianness of ABFD. Return a pointer
9862 to a struct line_header, allocated using xmalloc.
debd256d
JB
9863
9864 NOTE: the strings in the include directory and file name tables of
9865 the returned object point into debug_line_buffer, and must not be
9866 freed. */
9867static struct line_header *
9868dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9869 struct dwarf2_cu *cu)
debd256d
JB
9870{
9871 struct cleanup *back_to;
9872 struct line_header *lh;
fe1b8b76 9873 gdb_byte *line_ptr;
c764a876 9874 unsigned int bytes_read, offset_size;
debd256d
JB
9875 int i;
9876 char *cur_dir, *cur_file;
9877
be391dca 9878 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9879 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9880 {
e2e0b3e5 9881 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9882 return 0;
9883 }
9884
a738430d
MK
9885 /* Make sure that at least there's room for the total_length field.
9886 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9887 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9888 {
4d3c2250 9889 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9890 return 0;
9891 }
9892
9893 lh = xmalloc (sizeof (*lh));
9894 memset (lh, 0, sizeof (*lh));
9895 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9896 (void *) lh);
9897
dce234bc 9898 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9899
a738430d 9900 /* Read in the header. */
6e70227d 9901 lh->total_length =
c764a876
DE
9902 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9903 &bytes_read, &offset_size);
debd256d 9904 line_ptr += bytes_read;
dce234bc
PP
9905 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9906 + dwarf2_per_objfile->line.size))
debd256d 9907 {
4d3c2250 9908 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9909 return 0;
9910 }
9911 lh->statement_program_end = line_ptr + lh->total_length;
9912 lh->version = read_2_bytes (abfd, line_ptr);
9913 line_ptr += 2;
c764a876
DE
9914 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9915 line_ptr += offset_size;
debd256d
JB
9916 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9917 line_ptr += 1;
2dc7f7b3
TT
9918 if (lh->version >= 4)
9919 {
9920 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9921 line_ptr += 1;
9922 }
9923 else
9924 lh->maximum_ops_per_instruction = 1;
9925
9926 if (lh->maximum_ops_per_instruction == 0)
9927 {
9928 lh->maximum_ops_per_instruction = 1;
9929 complaint (&symfile_complaints,
9930 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9931 }
9932
debd256d
JB
9933 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9934 line_ptr += 1;
9935 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9936 line_ptr += 1;
9937 lh->line_range = read_1_byte (abfd, line_ptr);
9938 line_ptr += 1;
9939 lh->opcode_base = read_1_byte (abfd, line_ptr);
9940 line_ptr += 1;
9941 lh->standard_opcode_lengths
fe1b8b76 9942 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9943
9944 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9945 for (i = 1; i < lh->opcode_base; ++i)
9946 {
9947 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9948 line_ptr += 1;
9949 }
9950
a738430d 9951 /* Read directory table. */
9b1c24c8 9952 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9953 {
9954 line_ptr += bytes_read;
9955 add_include_dir (lh, cur_dir);
9956 }
9957 line_ptr += bytes_read;
9958
a738430d 9959 /* Read file name table. */
9b1c24c8 9960 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9961 {
9962 unsigned int dir_index, mod_time, length;
9963
9964 line_ptr += bytes_read;
9965 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9966 line_ptr += bytes_read;
9967 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9968 line_ptr += bytes_read;
9969 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9970 line_ptr += bytes_read;
9971
9972 add_file_name (lh, cur_file, dir_index, mod_time, length);
9973 }
9974 line_ptr += bytes_read;
6e70227d 9975 lh->statement_program_start = line_ptr;
debd256d 9976
dce234bc
PP
9977 if (line_ptr > (dwarf2_per_objfile->line.buffer
9978 + dwarf2_per_objfile->line.size))
4d3c2250 9979 complaint (&symfile_complaints,
e2e0b3e5 9980 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9981
9982 discard_cleanups (back_to);
9983 return lh;
9984}
c906108c 9985
5fb290d7
DJ
9986/* This function exists to work around a bug in certain compilers
9987 (particularly GCC 2.95), in which the first line number marker of a
9988 function does not show up until after the prologue, right before
9989 the second line number marker. This function shifts ADDRESS down
9990 to the beginning of the function if necessary, and is called on
9991 addresses passed to record_line. */
9992
9993static CORE_ADDR
e142c38c 9994check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9995{
9996 struct function_range *fn;
9997
9998 /* Find the function_range containing address. */
e142c38c 9999 if (!cu->first_fn)
5fb290d7
DJ
10000 return address;
10001
e142c38c
DJ
10002 if (!cu->cached_fn)
10003 cu->cached_fn = cu->first_fn;
5fb290d7 10004
e142c38c 10005 fn = cu->cached_fn;
5fb290d7
DJ
10006 while (fn)
10007 if (fn->lowpc <= address && fn->highpc > address)
10008 goto found;
10009 else
10010 fn = fn->next;
10011
e142c38c
DJ
10012 fn = cu->first_fn;
10013 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10014 if (fn->lowpc <= address && fn->highpc > address)
10015 goto found;
10016 else
10017 fn = fn->next;
10018
10019 return address;
10020
10021 found:
10022 if (fn->seen_line)
10023 return address;
10024 if (address != fn->lowpc)
4d3c2250 10025 complaint (&symfile_complaints,
e2e0b3e5 10026 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10027 (unsigned long) address, fn->name);
5fb290d7
DJ
10028 fn->seen_line = 1;
10029 return fn->lowpc;
10030}
10031
c6da4cef
DE
10032/* Subroutine of dwarf_decode_lines to simplify it.
10033 Return the file name of the psymtab for included file FILE_INDEX
10034 in line header LH of PST.
10035 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10036 If space for the result is malloc'd, it will be freed by a cleanup.
10037 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10038
10039static char *
10040psymtab_include_file_name (const struct line_header *lh, int file_index,
10041 const struct partial_symtab *pst,
10042 const char *comp_dir)
10043{
10044 const struct file_entry fe = lh->file_names [file_index];
10045 char *include_name = fe.name;
10046 char *include_name_to_compare = include_name;
10047 char *dir_name = NULL;
72b9f47f
TT
10048 const char *pst_filename;
10049 char *copied_name = NULL;
c6da4cef
DE
10050 int file_is_pst;
10051
10052 if (fe.dir_index)
10053 dir_name = lh->include_dirs[fe.dir_index - 1];
10054
10055 if (!IS_ABSOLUTE_PATH (include_name)
10056 && (dir_name != NULL || comp_dir != NULL))
10057 {
10058 /* Avoid creating a duplicate psymtab for PST.
10059 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10060 Before we do the comparison, however, we need to account
10061 for DIR_NAME and COMP_DIR.
10062 First prepend dir_name (if non-NULL). If we still don't
10063 have an absolute path prepend comp_dir (if non-NULL).
10064 However, the directory we record in the include-file's
10065 psymtab does not contain COMP_DIR (to match the
10066 corresponding symtab(s)).
10067
10068 Example:
10069
10070 bash$ cd /tmp
10071 bash$ gcc -g ./hello.c
10072 include_name = "hello.c"
10073 dir_name = "."
10074 DW_AT_comp_dir = comp_dir = "/tmp"
10075 DW_AT_name = "./hello.c" */
10076
10077 if (dir_name != NULL)
10078 {
10079 include_name = concat (dir_name, SLASH_STRING,
10080 include_name, (char *)NULL);
10081 include_name_to_compare = include_name;
10082 make_cleanup (xfree, include_name);
10083 }
10084 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10085 {
10086 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10087 include_name, (char *)NULL);
10088 }
10089 }
10090
10091 pst_filename = pst->filename;
10092 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10093 {
72b9f47f
TT
10094 copied_name = concat (pst->dirname, SLASH_STRING,
10095 pst_filename, (char *)NULL);
10096 pst_filename = copied_name;
c6da4cef
DE
10097 }
10098
1e3fad37 10099 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10100
10101 if (include_name_to_compare != include_name)
10102 xfree (include_name_to_compare);
72b9f47f
TT
10103 if (copied_name != NULL)
10104 xfree (copied_name);
c6da4cef
DE
10105
10106 if (file_is_pst)
10107 return NULL;
10108 return include_name;
10109}
10110
aaa75496
JB
10111/* Decode the Line Number Program (LNP) for the given line_header
10112 structure and CU. The actual information extracted and the type
10113 of structures created from the LNP depends on the value of PST.
10114
10115 1. If PST is NULL, then this procedure uses the data from the program
10116 to create all necessary symbol tables, and their linetables.
6e70227d 10117
aaa75496
JB
10118 2. If PST is not NULL, this procedure reads the program to determine
10119 the list of files included by the unit represented by PST, and
c6da4cef
DE
10120 builds all the associated partial symbol tables.
10121
10122 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10123 It is used for relative paths in the line table.
10124 NOTE: When processing partial symtabs (pst != NULL),
10125 comp_dir == pst->dirname.
10126
10127 NOTE: It is important that psymtabs have the same file name (via strcmp)
10128 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10129 symtab we don't use it in the name of the psymtabs we create.
10130 E.g. expand_line_sal requires this when finding psymtabs to expand.
10131 A good testcase for this is mb-inline.exp. */
debd256d 10132
c906108c 10133static void
72b9f47f 10134dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10135 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10136{
a8c50c1f 10137 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10138 gdb_byte *line_end;
a8c50c1f 10139 unsigned int bytes_read, extended_len;
c906108c 10140 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10141 CORE_ADDR baseaddr;
10142 struct objfile *objfile = cu->objfile;
fbf65064 10143 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10144 const int decode_for_pst_p = (pst != NULL);
cb1df416 10145 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10146
10147 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10148
debd256d
JB
10149 line_ptr = lh->statement_program_start;
10150 line_end = lh->statement_program_end;
c906108c
SS
10151
10152 /* Read the statement sequences until there's nothing left. */
10153 while (line_ptr < line_end)
10154 {
10155 /* state machine registers */
10156 CORE_ADDR address = 0;
10157 unsigned int file = 1;
10158 unsigned int line = 1;
10159 unsigned int column = 0;
debd256d 10160 int is_stmt = lh->default_is_stmt;
c906108c
SS
10161 int basic_block = 0;
10162 int end_sequence = 0;
fbf65064 10163 CORE_ADDR addr;
2dc7f7b3 10164 unsigned char op_index = 0;
c906108c 10165
aaa75496 10166 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10167 {
aaa75496 10168 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10169 /* lh->include_dirs and lh->file_names are 0-based, but the
10170 directory and file name numbers in the statement program
10171 are 1-based. */
10172 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10173 char *dir = NULL;
a738430d 10174
debd256d
JB
10175 if (fe->dir_index)
10176 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10177
10178 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10179 }
10180
a738430d 10181 /* Decode the table. */
c5aa993b 10182 while (!end_sequence)
c906108c
SS
10183 {
10184 op_code = read_1_byte (abfd, line_ptr);
10185 line_ptr += 1;
59205f5a
JB
10186 if (line_ptr > line_end)
10187 {
10188 dwarf2_debug_line_missing_end_sequence_complaint ();
10189 break;
10190 }
9aa1fe7e 10191
debd256d 10192 if (op_code >= lh->opcode_base)
6e70227d 10193 {
a738430d 10194 /* Special operand. */
debd256d 10195 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10196 address += (((op_index + (adj_opcode / lh->line_range))
10197 / lh->maximum_ops_per_instruction)
10198 * lh->minimum_instruction_length);
10199 op_index = ((op_index + (adj_opcode / lh->line_range))
10200 % lh->maximum_ops_per_instruction);
debd256d 10201 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10202 if (lh->num_file_names < file || file == 0)
25e43795 10203 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10204 /* For now we ignore lines not starting on an
10205 instruction boundary. */
10206 else if (op_index == 0)
25e43795
DJ
10207 {
10208 lh->file_names[file - 1].included_p = 1;
ca5f395d 10209 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10210 {
10211 if (last_subfile != current_subfile)
10212 {
10213 addr = gdbarch_addr_bits_remove (gdbarch, address);
10214 if (last_subfile)
10215 record_line (last_subfile, 0, addr);
10216 last_subfile = current_subfile;
10217 }
25e43795 10218 /* Append row to matrix using current values. */
fbf65064
UW
10219 addr = check_cu_functions (address, cu);
10220 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10221 record_line (current_subfile, line, addr);
366da635 10222 }
25e43795 10223 }
ca5f395d 10224 basic_block = 0;
9aa1fe7e
GK
10225 }
10226 else switch (op_code)
c906108c
SS
10227 {
10228 case DW_LNS_extended_op:
a8c50c1f 10229 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10230 line_ptr += bytes_read;
a8c50c1f 10231 extended_end = line_ptr + extended_len;
c906108c
SS
10232 extended_op = read_1_byte (abfd, line_ptr);
10233 line_ptr += 1;
10234 switch (extended_op)
10235 {
10236 case DW_LNE_end_sequence:
10237 end_sequence = 1;
c906108c
SS
10238 break;
10239 case DW_LNE_set_address:
e7c27a73 10240 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10241 op_index = 0;
107d2387
AC
10242 line_ptr += bytes_read;
10243 address += baseaddr;
c906108c
SS
10244 break;
10245 case DW_LNE_define_file:
debd256d
JB
10246 {
10247 char *cur_file;
10248 unsigned int dir_index, mod_time, length;
6e70227d 10249
9b1c24c8 10250 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10251 line_ptr += bytes_read;
10252 dir_index =
10253 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10254 line_ptr += bytes_read;
10255 mod_time =
10256 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10257 line_ptr += bytes_read;
10258 length =
10259 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10260 line_ptr += bytes_read;
10261 add_file_name (lh, cur_file, dir_index, mod_time, length);
10262 }
c906108c 10263 break;
d0c6ba3d
CC
10264 case DW_LNE_set_discriminator:
10265 /* The discriminator is not interesting to the debugger;
10266 just ignore it. */
10267 line_ptr = extended_end;
10268 break;
c906108c 10269 default:
4d3c2250 10270 complaint (&symfile_complaints,
e2e0b3e5 10271 _("mangled .debug_line section"));
debd256d 10272 return;
c906108c 10273 }
a8c50c1f
DJ
10274 /* Make sure that we parsed the extended op correctly. If e.g.
10275 we expected a different address size than the producer used,
10276 we may have read the wrong number of bytes. */
10277 if (line_ptr != extended_end)
10278 {
10279 complaint (&symfile_complaints,
10280 _("mangled .debug_line section"));
10281 return;
10282 }
c906108c
SS
10283 break;
10284 case DW_LNS_copy:
59205f5a 10285 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10286 dwarf2_debug_line_missing_file_complaint ();
10287 else
366da635 10288 {
25e43795 10289 lh->file_names[file - 1].included_p = 1;
ca5f395d 10290 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10291 {
10292 if (last_subfile != current_subfile)
10293 {
10294 addr = gdbarch_addr_bits_remove (gdbarch, address);
10295 if (last_subfile)
10296 record_line (last_subfile, 0, addr);
10297 last_subfile = current_subfile;
10298 }
10299 addr = check_cu_functions (address, cu);
10300 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10301 record_line (current_subfile, line, addr);
10302 }
366da635 10303 }
c906108c
SS
10304 basic_block = 0;
10305 break;
10306 case DW_LNS_advance_pc:
2dc7f7b3
TT
10307 {
10308 CORE_ADDR adjust
10309 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10310
10311 address += (((op_index + adjust)
10312 / lh->maximum_ops_per_instruction)
10313 * lh->minimum_instruction_length);
10314 op_index = ((op_index + adjust)
10315 % lh->maximum_ops_per_instruction);
10316 line_ptr += bytes_read;
10317 }
c906108c
SS
10318 break;
10319 case DW_LNS_advance_line:
10320 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10321 line_ptr += bytes_read;
10322 break;
10323 case DW_LNS_set_file:
debd256d 10324 {
a738430d
MK
10325 /* The arrays lh->include_dirs and lh->file_names are
10326 0-based, but the directory and file name numbers in
10327 the statement program are 1-based. */
debd256d 10328 struct file_entry *fe;
4f1520fb 10329 char *dir = NULL;
a738430d 10330
debd256d
JB
10331 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10332 line_ptr += bytes_read;
59205f5a 10333 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10334 dwarf2_debug_line_missing_file_complaint ();
10335 else
10336 {
10337 fe = &lh->file_names[file - 1];
10338 if (fe->dir_index)
10339 dir = lh->include_dirs[fe->dir_index - 1];
10340 if (!decode_for_pst_p)
10341 {
10342 last_subfile = current_subfile;
10343 dwarf2_start_subfile (fe->name, dir, comp_dir);
10344 }
10345 }
debd256d 10346 }
c906108c
SS
10347 break;
10348 case DW_LNS_set_column:
10349 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10350 line_ptr += bytes_read;
10351 break;
10352 case DW_LNS_negate_stmt:
10353 is_stmt = (!is_stmt);
10354 break;
10355 case DW_LNS_set_basic_block:
10356 basic_block = 1;
10357 break;
c2c6d25f
JM
10358 /* Add to the address register of the state machine the
10359 address increment value corresponding to special opcode
a738430d
MK
10360 255. I.e., this value is scaled by the minimum
10361 instruction length since special opcode 255 would have
10362 scaled the the increment. */
c906108c 10363 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10364 {
10365 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10366
10367 address += (((op_index + adjust)
10368 / lh->maximum_ops_per_instruction)
10369 * lh->minimum_instruction_length);
10370 op_index = ((op_index + adjust)
10371 % lh->maximum_ops_per_instruction);
10372 }
c906108c
SS
10373 break;
10374 case DW_LNS_fixed_advance_pc:
10375 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10376 op_index = 0;
c906108c
SS
10377 line_ptr += 2;
10378 break;
9aa1fe7e 10379 default:
a738430d
MK
10380 {
10381 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10382 int i;
a738430d 10383
debd256d 10384 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10385 {
10386 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10387 line_ptr += bytes_read;
10388 }
10389 }
c906108c
SS
10390 }
10391 }
59205f5a
JB
10392 if (lh->num_file_names < file || file == 0)
10393 dwarf2_debug_line_missing_file_complaint ();
10394 else
10395 {
10396 lh->file_names[file - 1].included_p = 1;
10397 if (!decode_for_pst_p)
fbf65064
UW
10398 {
10399 addr = gdbarch_addr_bits_remove (gdbarch, address);
10400 record_line (current_subfile, 0, addr);
10401 }
59205f5a 10402 }
c906108c 10403 }
aaa75496
JB
10404
10405 if (decode_for_pst_p)
10406 {
10407 int file_index;
10408
10409 /* Now that we're done scanning the Line Header Program, we can
10410 create the psymtab of each included file. */
10411 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10412 if (lh->file_names[file_index].included_p == 1)
10413 {
c6da4cef
DE
10414 char *include_name =
10415 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10416 if (include_name != NULL)
aaa75496
JB
10417 dwarf2_create_include_psymtab (include_name, pst, objfile);
10418 }
10419 }
cb1df416
DJ
10420 else
10421 {
10422 /* Make sure a symtab is created for every file, even files
10423 which contain only variables (i.e. no code with associated
10424 line numbers). */
10425
10426 int i;
10427 struct file_entry *fe;
10428
10429 for (i = 0; i < lh->num_file_names; i++)
10430 {
10431 char *dir = NULL;
9a619af0 10432
cb1df416
DJ
10433 fe = &lh->file_names[i];
10434 if (fe->dir_index)
10435 dir = lh->include_dirs[fe->dir_index - 1];
10436 dwarf2_start_subfile (fe->name, dir, comp_dir);
10437
10438 /* Skip the main file; we don't need it, and it must be
10439 allocated last, so that it will show up before the
10440 non-primary symtabs in the objfile's symtab list. */
10441 if (current_subfile == first_subfile)
10442 continue;
10443
10444 if (current_subfile->symtab == NULL)
10445 current_subfile->symtab = allocate_symtab (current_subfile->name,
10446 cu->objfile);
10447 fe->symtab = current_subfile->symtab;
10448 }
10449 }
c906108c
SS
10450}
10451
10452/* Start a subfile for DWARF. FILENAME is the name of the file and
10453 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10454 or NULL if not known. COMP_DIR is the compilation directory for the
10455 linetable's compilation unit or NULL if not known.
c906108c
SS
10456 This routine tries to keep line numbers from identical absolute and
10457 relative file names in a common subfile.
10458
10459 Using the `list' example from the GDB testsuite, which resides in
10460 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10461 of /srcdir/list0.c yields the following debugging information for list0.c:
10462
c5aa993b
JM
10463 DW_AT_name: /srcdir/list0.c
10464 DW_AT_comp_dir: /compdir
357e46e7 10465 files.files[0].name: list0.h
c5aa993b 10466 files.files[0].dir: /srcdir
357e46e7 10467 files.files[1].name: list0.c
c5aa993b 10468 files.files[1].dir: /srcdir
c906108c
SS
10469
10470 The line number information for list0.c has to end up in a single
4f1520fb
FR
10471 subfile, so that `break /srcdir/list0.c:1' works as expected.
10472 start_subfile will ensure that this happens provided that we pass the
10473 concatenation of files.files[1].dir and files.files[1].name as the
10474 subfile's name. */
c906108c
SS
10475
10476static void
72b9f47f 10477dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
c906108c 10478{
4f1520fb
FR
10479 char *fullname;
10480
10481 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10482 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10483 second argument to start_subfile. To be consistent, we do the
10484 same here. In order not to lose the line information directory,
10485 we concatenate it to the filename when it makes sense.
10486 Note that the Dwarf3 standard says (speaking of filenames in line
10487 information): ``The directory index is ignored for file names
10488 that represent full path names''. Thus ignoring dirname in the
10489 `else' branch below isn't an issue. */
c906108c 10490
d5166ae1 10491 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10492 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10493 else
10494 fullname = filename;
c906108c 10495
4f1520fb
FR
10496 start_subfile (fullname, comp_dir);
10497
10498 if (fullname != filename)
10499 xfree (fullname);
c906108c
SS
10500}
10501
4c2df51b
DJ
10502static void
10503var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10504 struct dwarf2_cu *cu)
4c2df51b 10505{
e7c27a73
DJ
10506 struct objfile *objfile = cu->objfile;
10507 struct comp_unit_head *cu_header = &cu->header;
10508
4c2df51b
DJ
10509 /* NOTE drow/2003-01-30: There used to be a comment and some special
10510 code here to turn a symbol with DW_AT_external and a
10511 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10512 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10513 with some versions of binutils) where shared libraries could have
10514 relocations against symbols in their debug information - the
10515 minimal symbol would have the right address, but the debug info
10516 would not. It's no longer necessary, because we will explicitly
10517 apply relocations when we read in the debug information now. */
10518
10519 /* A DW_AT_location attribute with no contents indicates that a
10520 variable has been optimized away. */
10521 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10522 {
10523 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10524 return;
10525 }
10526
10527 /* Handle one degenerate form of location expression specially, to
10528 preserve GDB's previous behavior when section offsets are
10529 specified. If this is just a DW_OP_addr then mark this symbol
10530 as LOC_STATIC. */
10531
10532 if (attr_form_is_block (attr)
10533 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10534 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10535 {
891d2f0b 10536 unsigned int dummy;
4c2df51b
DJ
10537
10538 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10539 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10540 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10541 fixup_symbol_section (sym, objfile);
10542 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10543 SYMBOL_SECTION (sym));
4c2df51b
DJ
10544 return;
10545 }
10546
10547 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10548 expression evaluator, and use LOC_COMPUTED only when necessary
10549 (i.e. when the value of a register or memory location is
10550 referenced, or a thread-local block, etc.). Then again, it might
10551 not be worthwhile. I'm assuming that it isn't unless performance
10552 or memory numbers show me otherwise. */
10553
e7c27a73 10554 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10555 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10556}
10557
c906108c
SS
10558/* Given a pointer to a DWARF information entry, figure out if we need
10559 to make a symbol table entry for it, and if so, create a new entry
10560 and return a pointer to it.
10561 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10562 used the passed type.
10563 If SPACE is not NULL, use it to hold the new symbol. If it is
10564 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10565
10566static struct symbol *
34eaf542
TT
10567new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10568 struct symbol *space)
c906108c 10569{
e7c27a73 10570 struct objfile *objfile = cu->objfile;
c906108c
SS
10571 struct symbol *sym = NULL;
10572 char *name;
10573 struct attribute *attr = NULL;
10574 struct attribute *attr2 = NULL;
e142c38c 10575 CORE_ADDR baseaddr;
e37fd15a
SW
10576 struct pending **list_to_add = NULL;
10577
edb3359d 10578 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10579
10580 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10581
94af9270 10582 name = dwarf2_name (die, cu);
c906108c
SS
10583 if (name)
10584 {
94af9270 10585 const char *linkagename;
34eaf542 10586 int suppress_add = 0;
94af9270 10587
34eaf542
TT
10588 if (space)
10589 sym = space;
10590 else
10591 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10592 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10593
10594 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10595 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10596 linkagename = dwarf2_physname (name, die, cu);
10597 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10598
f55ee35c
JK
10599 /* Fortran does not have mangling standard and the mangling does differ
10600 between gfortran, iFort etc. */
10601 if (cu->language == language_fortran
b250c185 10602 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10603 symbol_set_demangled_name (&(sym->ginfo),
10604 (char *) dwarf2_full_name (name, die, cu),
10605 NULL);
f55ee35c 10606
c906108c 10607 /* Default assumptions.
c5aa993b 10608 Use the passed type or decode it from the die. */
176620f1 10609 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10610 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10611 if (type != NULL)
10612 SYMBOL_TYPE (sym) = type;
10613 else
e7c27a73 10614 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10615 attr = dwarf2_attr (die,
10616 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10617 cu);
c906108c
SS
10618 if (attr)
10619 {
10620 SYMBOL_LINE (sym) = DW_UNSND (attr);
10621 }
cb1df416 10622
edb3359d
DJ
10623 attr = dwarf2_attr (die,
10624 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10625 cu);
cb1df416
DJ
10626 if (attr)
10627 {
10628 int file_index = DW_UNSND (attr);
9a619af0 10629
cb1df416
DJ
10630 if (cu->line_header == NULL
10631 || file_index > cu->line_header->num_file_names)
10632 complaint (&symfile_complaints,
10633 _("file index out of range"));
1c3d648d 10634 else if (file_index > 0)
cb1df416
DJ
10635 {
10636 struct file_entry *fe;
9a619af0 10637
cb1df416
DJ
10638 fe = &cu->line_header->file_names[file_index - 1];
10639 SYMBOL_SYMTAB (sym) = fe->symtab;
10640 }
10641 }
10642
c906108c
SS
10643 switch (die->tag)
10644 {
10645 case DW_TAG_label:
e142c38c 10646 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10647 if (attr)
10648 {
10649 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10650 }
0f5238ed
TT
10651 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10652 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10653 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10654 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10655 break;
10656 case DW_TAG_subprogram:
10657 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10658 finish_block. */
10659 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10660 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10661 if ((attr2 && (DW_UNSND (attr2) != 0))
10662 || cu->language == language_ada)
c906108c 10663 {
2cfa0c8d
JB
10664 /* Subprograms marked external are stored as a global symbol.
10665 Ada subprograms, whether marked external or not, are always
10666 stored as a global symbol, because we want to be able to
10667 access them globally. For instance, we want to be able
10668 to break on a nested subprogram without having to
10669 specify the context. */
e37fd15a 10670 list_to_add = &global_symbols;
c906108c
SS
10671 }
10672 else
10673 {
e37fd15a 10674 list_to_add = cu->list_in_scope;
c906108c
SS
10675 }
10676 break;
edb3359d
DJ
10677 case DW_TAG_inlined_subroutine:
10678 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10679 finish_block. */
10680 SYMBOL_CLASS (sym) = LOC_BLOCK;
10681 SYMBOL_INLINED (sym) = 1;
10682 /* Do not add the symbol to any lists. It will be found via
10683 BLOCK_FUNCTION from the blockvector. */
10684 break;
34eaf542
TT
10685 case DW_TAG_template_value_param:
10686 suppress_add = 1;
10687 /* Fall through. */
72929c62 10688 case DW_TAG_constant:
c906108c 10689 case DW_TAG_variable:
254e6b9e 10690 case DW_TAG_member:
c906108c
SS
10691 /* Compilation with minimal debug info may result in variables
10692 with missing type entries. Change the misleading `void' type
10693 to something sensible. */
10694 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10695 SYMBOL_TYPE (sym)
46bf5051 10696 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10697
e142c38c 10698 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10699 /* In the case of DW_TAG_member, we should only be called for
10700 static const members. */
10701 if (die->tag == DW_TAG_member)
10702 {
3863f96c
DE
10703 /* dwarf2_add_field uses die_is_declaration,
10704 so we do the same. */
254e6b9e
DE
10705 gdb_assert (die_is_declaration (die, cu));
10706 gdb_assert (attr);
10707 }
c906108c
SS
10708 if (attr)
10709 {
e7c27a73 10710 dwarf2_const_value (attr, sym, cu);
e142c38c 10711 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10712 if (!suppress_add)
34eaf542
TT
10713 {
10714 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10715 list_to_add = &global_symbols;
34eaf542 10716 else
e37fd15a 10717 list_to_add = cu->list_in_scope;
34eaf542 10718 }
c906108c
SS
10719 break;
10720 }
e142c38c 10721 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10722 if (attr)
10723 {
e7c27a73 10724 var_decode_location (attr, sym, cu);
e142c38c 10725 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10726 if (SYMBOL_CLASS (sym) == LOC_STATIC
10727 && SYMBOL_VALUE_ADDRESS (sym) == 0
10728 && !dwarf2_per_objfile->has_section_at_zero)
10729 {
10730 /* When a static variable is eliminated by the linker,
10731 the corresponding debug information is not stripped
10732 out, but the variable address is set to null;
10733 do not add such variables into symbol table. */
10734 }
10735 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10736 {
f55ee35c
JK
10737 /* Workaround gfortran PR debug/40040 - it uses
10738 DW_AT_location for variables in -fPIC libraries which may
10739 get overriden by other libraries/executable and get
10740 a different address. Resolve it by the minimal symbol
10741 which may come from inferior's executable using copy
10742 relocation. Make this workaround only for gfortran as for
10743 other compilers GDB cannot guess the minimal symbol
10744 Fortran mangling kind. */
10745 if (cu->language == language_fortran && die->parent
10746 && die->parent->tag == DW_TAG_module
10747 && cu->producer
10748 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10749 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10750
1c809c68
TT
10751 /* A variable with DW_AT_external is never static,
10752 but it may be block-scoped. */
10753 list_to_add = (cu->list_in_scope == &file_symbols
10754 ? &global_symbols : cu->list_in_scope);
1c809c68 10755 }
c906108c 10756 else
e37fd15a 10757 list_to_add = cu->list_in_scope;
c906108c
SS
10758 }
10759 else
10760 {
10761 /* We do not know the address of this symbol.
c5aa993b
JM
10762 If it is an external symbol and we have type information
10763 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10764 The address of the variable will then be determined from
10765 the minimal symbol table whenever the variable is
10766 referenced. */
e142c38c 10767 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10768 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10769 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10770 {
0fe7935b
DJ
10771 /* A variable with DW_AT_external is never static, but it
10772 may be block-scoped. */
10773 list_to_add = (cu->list_in_scope == &file_symbols
10774 ? &global_symbols : cu->list_in_scope);
10775
c906108c 10776 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10777 }
442ddf59
JK
10778 else if (!die_is_declaration (die, cu))
10779 {
10780 /* Use the default LOC_OPTIMIZED_OUT class. */
10781 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10782 if (!suppress_add)
10783 list_to_add = cu->list_in_scope;
442ddf59 10784 }
c906108c
SS
10785 }
10786 break;
10787 case DW_TAG_formal_parameter:
edb3359d
DJ
10788 /* If we are inside a function, mark this as an argument. If
10789 not, we might be looking at an argument to an inlined function
10790 when we do not have enough information to show inlined frames;
10791 pretend it's a local variable in that case so that the user can
10792 still see it. */
10793 if (context_stack_depth > 0
10794 && context_stack[context_stack_depth - 1].name != NULL)
10795 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10796 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10797 if (attr)
10798 {
e7c27a73 10799 var_decode_location (attr, sym, cu);
c906108c 10800 }
e142c38c 10801 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10802 if (attr)
10803 {
e7c27a73 10804 dwarf2_const_value (attr, sym, cu);
c906108c 10805 }
f346a30d
PM
10806 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10807 if (attr && DW_UNSND (attr))
10808 {
10809 struct type *ref_type;
10810
10811 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10812 SYMBOL_TYPE (sym) = ref_type;
10813 }
10814
e37fd15a 10815 list_to_add = cu->list_in_scope;
c906108c
SS
10816 break;
10817 case DW_TAG_unspecified_parameters:
10818 /* From varargs functions; gdb doesn't seem to have any
10819 interest in this information, so just ignore it for now.
10820 (FIXME?) */
10821 break;
34eaf542
TT
10822 case DW_TAG_template_type_param:
10823 suppress_add = 1;
10824 /* Fall through. */
c906108c 10825 case DW_TAG_class_type:
680b30c7 10826 case DW_TAG_interface_type:
c906108c
SS
10827 case DW_TAG_structure_type:
10828 case DW_TAG_union_type:
72019c9c 10829 case DW_TAG_set_type:
c906108c
SS
10830 case DW_TAG_enumeration_type:
10831 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10832 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10833
63d06c5c 10834 {
987504bb 10835 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10836 really ever be static objects: otherwise, if you try
10837 to, say, break of a class's method and you're in a file
10838 which doesn't mention that class, it won't work unless
10839 the check for all static symbols in lookup_symbol_aux
10840 saves you. See the OtherFileClass tests in
10841 gdb.c++/namespace.exp. */
10842
e37fd15a 10843 if (!suppress_add)
34eaf542 10844 {
34eaf542
TT
10845 list_to_add = (cu->list_in_scope == &file_symbols
10846 && (cu->language == language_cplus
10847 || cu->language == language_java)
10848 ? &global_symbols : cu->list_in_scope);
63d06c5c 10849
64382290
TT
10850 /* The semantics of C++ state that "struct foo {
10851 ... }" also defines a typedef for "foo". A Java
10852 class declaration also defines a typedef for the
10853 class. */
10854 if (cu->language == language_cplus
10855 || cu->language == language_java
10856 || cu->language == language_ada)
10857 {
10858 /* The symbol's name is already allocated along
10859 with this objfile, so we don't need to
10860 duplicate it for the type. */
10861 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10862 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10863 }
63d06c5c
DC
10864 }
10865 }
c906108c
SS
10866 break;
10867 case DW_TAG_typedef:
63d06c5c
DC
10868 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10869 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10870 list_to_add = cu->list_in_scope;
63d06c5c 10871 break;
c906108c 10872 case DW_TAG_base_type:
a02abb62 10873 case DW_TAG_subrange_type:
c906108c 10874 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10875 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10876 list_to_add = cu->list_in_scope;
c906108c
SS
10877 break;
10878 case DW_TAG_enumerator:
e142c38c 10879 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10880 if (attr)
10881 {
e7c27a73 10882 dwarf2_const_value (attr, sym, cu);
c906108c 10883 }
63d06c5c
DC
10884 {
10885 /* NOTE: carlton/2003-11-10: See comment above in the
10886 DW_TAG_class_type, etc. block. */
10887
e142c38c 10888 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10889 && (cu->language == language_cplus
10890 || cu->language == language_java)
e142c38c 10891 ? &global_symbols : cu->list_in_scope);
63d06c5c 10892 }
c906108c 10893 break;
5c4e30ca
DC
10894 case DW_TAG_namespace:
10895 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10896 list_to_add = &global_symbols;
5c4e30ca 10897 break;
c906108c
SS
10898 default:
10899 /* Not a tag we recognize. Hopefully we aren't processing
10900 trash data, but since we must specifically ignore things
10901 we don't recognize, there is nothing else we should do at
10902 this point. */
e2e0b3e5 10903 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10904 dwarf_tag_name (die->tag));
c906108c
SS
10905 break;
10906 }
df8a16a1 10907
e37fd15a
SW
10908 if (suppress_add)
10909 {
10910 sym->hash_next = objfile->template_symbols;
10911 objfile->template_symbols = sym;
10912 list_to_add = NULL;
10913 }
10914
10915 if (list_to_add != NULL)
10916 add_symbol_to_list (sym, list_to_add);
10917
df8a16a1
DJ
10918 /* For the benefit of old versions of GCC, check for anonymous
10919 namespaces based on the demangled name. */
10920 if (!processing_has_namespace_info
94af9270 10921 && cu->language == language_cplus)
df8a16a1 10922 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10923 }
10924 return (sym);
10925}
10926
34eaf542
TT
10927/* A wrapper for new_symbol_full that always allocates a new symbol. */
10928
10929static struct symbol *
10930new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10931{
10932 return new_symbol_full (die, type, cu, NULL);
10933}
10934
98bfdba5
PA
10935/* Given an attr with a DW_FORM_dataN value in host byte order,
10936 zero-extend it as appropriate for the symbol's type. The DWARF
10937 standard (v4) is not entirely clear about the meaning of using
10938 DW_FORM_dataN for a constant with a signed type, where the type is
10939 wider than the data. The conclusion of a discussion on the DWARF
10940 list was that this is unspecified. We choose to always zero-extend
10941 because that is the interpretation long in use by GCC. */
c906108c 10942
98bfdba5
PA
10943static gdb_byte *
10944dwarf2_const_value_data (struct attribute *attr, struct type *type,
10945 const char *name, struct obstack *obstack,
10946 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10947{
e7c27a73 10948 struct objfile *objfile = cu->objfile;
e17a4113
UW
10949 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10950 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10951 LONGEST l = DW_UNSND (attr);
10952
10953 if (bits < sizeof (*value) * 8)
10954 {
10955 l &= ((LONGEST) 1 << bits) - 1;
10956 *value = l;
10957 }
10958 else if (bits == sizeof (*value) * 8)
10959 *value = l;
10960 else
10961 {
10962 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10963 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10964 return bytes;
10965 }
10966
10967 return NULL;
10968}
10969
10970/* Read a constant value from an attribute. Either set *VALUE, or if
10971 the value does not fit in *VALUE, set *BYTES - either already
10972 allocated on the objfile obstack, or newly allocated on OBSTACK,
10973 or, set *BATON, if we translated the constant to a location
10974 expression. */
10975
10976static void
10977dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10978 const char *name, struct obstack *obstack,
10979 struct dwarf2_cu *cu,
10980 long *value, gdb_byte **bytes,
10981 struct dwarf2_locexpr_baton **baton)
10982{
10983 struct objfile *objfile = cu->objfile;
10984 struct comp_unit_head *cu_header = &cu->header;
c906108c 10985 struct dwarf_block *blk;
98bfdba5
PA
10986 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10987 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10988
10989 *value = 0;
10990 *bytes = NULL;
10991 *baton = NULL;
c906108c
SS
10992
10993 switch (attr->form)
10994 {
10995 case DW_FORM_addr:
ac56253d 10996 {
ac56253d
TT
10997 gdb_byte *data;
10998
98bfdba5
PA
10999 if (TYPE_LENGTH (type) != cu_header->addr_size)
11000 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11001 cu_header->addr_size,
98bfdba5 11002 TYPE_LENGTH (type));
ac56253d
TT
11003 /* Symbols of this form are reasonably rare, so we just
11004 piggyback on the existing location code rather than writing
11005 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11006 *baton = obstack_alloc (&objfile->objfile_obstack,
11007 sizeof (struct dwarf2_locexpr_baton));
11008 (*baton)->per_cu = cu->per_cu;
11009 gdb_assert ((*baton)->per_cu);
ac56253d 11010
98bfdba5
PA
11011 (*baton)->size = 2 + cu_header->addr_size;
11012 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11013 (*baton)->data = data;
ac56253d
TT
11014
11015 data[0] = DW_OP_addr;
11016 store_unsigned_integer (&data[1], cu_header->addr_size,
11017 byte_order, DW_ADDR (attr));
11018 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11019 }
c906108c 11020 break;
4ac36638 11021 case DW_FORM_string:
93b5768b 11022 case DW_FORM_strp:
98bfdba5
PA
11023 /* DW_STRING is already allocated on the objfile obstack, point
11024 directly to it. */
11025 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11026 break;
c906108c
SS
11027 case DW_FORM_block1:
11028 case DW_FORM_block2:
11029 case DW_FORM_block4:
11030 case DW_FORM_block:
2dc7f7b3 11031 case DW_FORM_exprloc:
c906108c 11032 blk = DW_BLOCK (attr);
98bfdba5
PA
11033 if (TYPE_LENGTH (type) != blk->size)
11034 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11035 TYPE_LENGTH (type));
11036 *bytes = blk->data;
c906108c 11037 break;
2df3850c
JM
11038
11039 /* The DW_AT_const_value attributes are supposed to carry the
11040 symbol's value "represented as it would be on the target
11041 architecture." By the time we get here, it's already been
11042 converted to host endianness, so we just need to sign- or
11043 zero-extend it as appropriate. */
11044 case DW_FORM_data1:
98bfdba5 11045 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 11046 break;
c906108c 11047 case DW_FORM_data2:
98bfdba5 11048 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 11049 break;
c906108c 11050 case DW_FORM_data4:
98bfdba5 11051 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 11052 break;
c906108c 11053 case DW_FORM_data8:
98bfdba5 11054 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11055 break;
11056
c906108c 11057 case DW_FORM_sdata:
98bfdba5 11058 *value = DW_SND (attr);
2df3850c
JM
11059 break;
11060
c906108c 11061 case DW_FORM_udata:
98bfdba5 11062 *value = DW_UNSND (attr);
c906108c 11063 break;
2df3850c 11064
c906108c 11065 default:
4d3c2250 11066 complaint (&symfile_complaints,
e2e0b3e5 11067 _("unsupported const value attribute form: '%s'"),
4d3c2250 11068 dwarf_form_name (attr->form));
98bfdba5 11069 *value = 0;
c906108c
SS
11070 break;
11071 }
11072}
11073
2df3850c 11074
98bfdba5
PA
11075/* Copy constant value from an attribute to a symbol. */
11076
2df3850c 11077static void
98bfdba5
PA
11078dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11079 struct dwarf2_cu *cu)
2df3850c 11080{
98bfdba5
PA
11081 struct objfile *objfile = cu->objfile;
11082 struct comp_unit_head *cu_header = &cu->header;
11083 long value;
11084 gdb_byte *bytes;
11085 struct dwarf2_locexpr_baton *baton;
2df3850c 11086
98bfdba5
PA
11087 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11088 SYMBOL_PRINT_NAME (sym),
11089 &objfile->objfile_obstack, cu,
11090 &value, &bytes, &baton);
2df3850c 11091
98bfdba5
PA
11092 if (baton != NULL)
11093 {
11094 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11095 SYMBOL_LOCATION_BATON (sym) = baton;
11096 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11097 }
11098 else if (bytes != NULL)
11099 {
11100 SYMBOL_VALUE_BYTES (sym) = bytes;
11101 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11102 }
11103 else
11104 {
11105 SYMBOL_VALUE (sym) = value;
11106 SYMBOL_CLASS (sym) = LOC_CONST;
11107 }
2df3850c
JM
11108}
11109
c906108c
SS
11110/* Return the type of the die in question using its DW_AT_type attribute. */
11111
11112static struct type *
e7c27a73 11113die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11114{
c906108c 11115 struct attribute *type_attr;
c906108c 11116
e142c38c 11117 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11118 if (!type_attr)
11119 {
11120 /* A missing DW_AT_type represents a void type. */
46bf5051 11121 return objfile_type (cu->objfile)->builtin_void;
c906108c 11122 }
348e048f 11123
673bfd45 11124 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11125}
11126
b4ba55a1
JB
11127/* True iff CU's producer generates GNAT Ada auxiliary information
11128 that allows to find parallel types through that information instead
11129 of having to do expensive parallel lookups by type name. */
11130
11131static int
11132need_gnat_info (struct dwarf2_cu *cu)
11133{
11134 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11135 of GNAT produces this auxiliary information, without any indication
11136 that it is produced. Part of enhancing the FSF version of GNAT
11137 to produce that information will be to put in place an indicator
11138 that we can use in order to determine whether the descriptive type
11139 info is available or not. One suggestion that has been made is
11140 to use a new attribute, attached to the CU die. For now, assume
11141 that the descriptive type info is not available. */
11142 return 0;
11143}
11144
b4ba55a1
JB
11145/* Return the auxiliary type of the die in question using its
11146 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11147 attribute is not present. */
11148
11149static struct type *
11150die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11151{
b4ba55a1 11152 struct attribute *type_attr;
b4ba55a1
JB
11153
11154 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11155 if (!type_attr)
11156 return NULL;
11157
673bfd45 11158 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11159}
11160
11161/* If DIE has a descriptive_type attribute, then set the TYPE's
11162 descriptive type accordingly. */
11163
11164static void
11165set_descriptive_type (struct type *type, struct die_info *die,
11166 struct dwarf2_cu *cu)
11167{
11168 struct type *descriptive_type = die_descriptive_type (die, cu);
11169
11170 if (descriptive_type)
11171 {
11172 ALLOCATE_GNAT_AUX_TYPE (type);
11173 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11174 }
11175}
11176
c906108c
SS
11177/* Return the containing type of the die in question using its
11178 DW_AT_containing_type attribute. */
11179
11180static struct type *
e7c27a73 11181die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11182{
c906108c 11183 struct attribute *type_attr;
c906108c 11184
e142c38c 11185 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11186 if (!type_attr)
11187 error (_("Dwarf Error: Problem turning containing type into gdb type "
11188 "[in module %s]"), cu->objfile->name);
11189
673bfd45 11190 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11191}
11192
673bfd45
DE
11193/* Look up the type of DIE in CU using its type attribute ATTR.
11194 If there is no type substitute an error marker. */
11195
c906108c 11196static struct type *
673bfd45
DE
11197lookup_die_type (struct die_info *die, struct attribute *attr,
11198 struct dwarf2_cu *cu)
c906108c 11199{
f792889a
DJ
11200 struct type *this_type;
11201
673bfd45
DE
11202 /* First see if we have it cached. */
11203
11204 if (is_ref_attr (attr))
11205 {
11206 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11207
11208 this_type = get_die_type_at_offset (offset, cu->per_cu);
11209 }
11210 else if (attr->form == DW_FORM_sig8)
11211 {
11212 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11213 struct dwarf2_cu *sig_cu;
11214 unsigned int offset;
11215
11216 /* sig_type will be NULL if the signatured type is missing from
11217 the debug info. */
11218 if (sig_type == NULL)
11219 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11220 "at 0x%x [in module %s]"),
11221 die->offset, cu->objfile->name);
11222
11223 gdb_assert (sig_type->per_cu.from_debug_types);
11224 offset = sig_type->offset + sig_type->type_offset;
11225 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11226 }
11227 else
11228 {
11229 dump_die_for_error (die);
11230 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11231 dwarf_attr_name (attr->name), cu->objfile->name);
11232 }
11233
11234 /* If not cached we need to read it in. */
11235
11236 if (this_type == NULL)
11237 {
11238 struct die_info *type_die;
11239 struct dwarf2_cu *type_cu = cu;
11240
11241 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11242 /* If the type is cached, we should have found it above. */
11243 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11244 this_type = read_type_die_1 (type_die, type_cu);
11245 }
11246
11247 /* If we still don't have a type use an error marker. */
11248
11249 if (this_type == NULL)
c906108c 11250 {
b00fdb78
TT
11251 char *message, *saved;
11252
11253 /* read_type_die already issued a complaint. */
11254 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11255 cu->objfile->name,
11256 cu->header.offset,
11257 die->offset);
11258 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11259 message, strlen (message));
11260 xfree (message);
11261
11262 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11263 }
673bfd45 11264
f792889a 11265 return this_type;
c906108c
SS
11266}
11267
673bfd45
DE
11268/* Return the type in DIE, CU.
11269 Returns NULL for invalid types.
11270
11271 This first does a lookup in the appropriate type_hash table,
11272 and only reads the die in if necessary.
11273
11274 NOTE: This can be called when reading in partial or full symbols. */
11275
f792889a 11276static struct type *
e7c27a73 11277read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11278{
f792889a
DJ
11279 struct type *this_type;
11280
11281 this_type = get_die_type (die, cu);
11282 if (this_type)
11283 return this_type;
11284
673bfd45
DE
11285 return read_type_die_1 (die, cu);
11286}
11287
11288/* Read the type in DIE, CU.
11289 Returns NULL for invalid types. */
11290
11291static struct type *
11292read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11293{
11294 struct type *this_type = NULL;
11295
c906108c
SS
11296 switch (die->tag)
11297 {
11298 case DW_TAG_class_type:
680b30c7 11299 case DW_TAG_interface_type:
c906108c
SS
11300 case DW_TAG_structure_type:
11301 case DW_TAG_union_type:
f792889a 11302 this_type = read_structure_type (die, cu);
c906108c
SS
11303 break;
11304 case DW_TAG_enumeration_type:
f792889a 11305 this_type = read_enumeration_type (die, cu);
c906108c
SS
11306 break;
11307 case DW_TAG_subprogram:
11308 case DW_TAG_subroutine_type:
edb3359d 11309 case DW_TAG_inlined_subroutine:
f792889a 11310 this_type = read_subroutine_type (die, cu);
c906108c
SS
11311 break;
11312 case DW_TAG_array_type:
f792889a 11313 this_type = read_array_type (die, cu);
c906108c 11314 break;
72019c9c 11315 case DW_TAG_set_type:
f792889a 11316 this_type = read_set_type (die, cu);
72019c9c 11317 break;
c906108c 11318 case DW_TAG_pointer_type:
f792889a 11319 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11320 break;
11321 case DW_TAG_ptr_to_member_type:
f792889a 11322 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11323 break;
11324 case DW_TAG_reference_type:
f792889a 11325 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11326 break;
11327 case DW_TAG_const_type:
f792889a 11328 this_type = read_tag_const_type (die, cu);
c906108c
SS
11329 break;
11330 case DW_TAG_volatile_type:
f792889a 11331 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11332 break;
11333 case DW_TAG_string_type:
f792889a 11334 this_type = read_tag_string_type (die, cu);
c906108c
SS
11335 break;
11336 case DW_TAG_typedef:
f792889a 11337 this_type = read_typedef (die, cu);
c906108c 11338 break;
a02abb62 11339 case DW_TAG_subrange_type:
f792889a 11340 this_type = read_subrange_type (die, cu);
a02abb62 11341 break;
c906108c 11342 case DW_TAG_base_type:
f792889a 11343 this_type = read_base_type (die, cu);
c906108c 11344 break;
81a17f79 11345 case DW_TAG_unspecified_type:
f792889a 11346 this_type = read_unspecified_type (die, cu);
81a17f79 11347 break;
0114d602
DJ
11348 case DW_TAG_namespace:
11349 this_type = read_namespace_type (die, cu);
11350 break;
f55ee35c
JK
11351 case DW_TAG_module:
11352 this_type = read_module_type (die, cu);
11353 break;
c906108c 11354 default:
a1f5b845 11355 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11356 dwarf_tag_name (die->tag));
c906108c
SS
11357 break;
11358 }
63d06c5c 11359
f792889a 11360 return this_type;
63d06c5c
DC
11361}
11362
abc72ce4
DE
11363/* See if we can figure out if the class lives in a namespace. We do
11364 this by looking for a member function; its demangled name will
11365 contain namespace info, if there is any.
11366 Return the computed name or NULL.
11367 Space for the result is allocated on the objfile's obstack.
11368 This is the full-die version of guess_partial_die_structure_name.
11369 In this case we know DIE has no useful parent. */
11370
11371static char *
11372guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11373{
11374 struct die_info *spec_die;
11375 struct dwarf2_cu *spec_cu;
11376 struct die_info *child;
11377
11378 spec_cu = cu;
11379 spec_die = die_specification (die, &spec_cu);
11380 if (spec_die != NULL)
11381 {
11382 die = spec_die;
11383 cu = spec_cu;
11384 }
11385
11386 for (child = die->child;
11387 child != NULL;
11388 child = child->sibling)
11389 {
11390 if (child->tag == DW_TAG_subprogram)
11391 {
11392 struct attribute *attr;
11393
11394 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11395 if (attr == NULL)
11396 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11397 if (attr != NULL)
11398 {
11399 char *actual_name
11400 = language_class_name_from_physname (cu->language_defn,
11401 DW_STRING (attr));
11402 char *name = NULL;
11403
11404 if (actual_name != NULL)
11405 {
11406 char *die_name = dwarf2_name (die, cu);
11407
11408 if (die_name != NULL
11409 && strcmp (die_name, actual_name) != 0)
11410 {
11411 /* Strip off the class name from the full name.
11412 We want the prefix. */
11413 int die_name_len = strlen (die_name);
11414 int actual_name_len = strlen (actual_name);
11415
11416 /* Test for '::' as a sanity check. */
11417 if (actual_name_len > die_name_len + 2
11418 && actual_name[actual_name_len - die_name_len - 1] == ':')
11419 name =
11420 obsavestring (actual_name,
11421 actual_name_len - die_name_len - 2,
11422 &cu->objfile->objfile_obstack);
11423 }
11424 }
11425 xfree (actual_name);
11426 return name;
11427 }
11428 }
11429 }
11430
11431 return NULL;
11432}
11433
fdde2d81 11434/* Return the name of the namespace/class that DIE is defined within,
0114d602 11435 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11436
0114d602
DJ
11437 For example, if we're within the method foo() in the following
11438 code:
11439
11440 namespace N {
11441 class C {
11442 void foo () {
11443 }
11444 };
11445 }
11446
11447 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11448
11449static char *
e142c38c 11450determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11451{
0114d602
DJ
11452 struct die_info *parent, *spec_die;
11453 struct dwarf2_cu *spec_cu;
11454 struct type *parent_type;
63d06c5c 11455
f55ee35c
JK
11456 if (cu->language != language_cplus && cu->language != language_java
11457 && cu->language != language_fortran)
0114d602
DJ
11458 return "";
11459
11460 /* We have to be careful in the presence of DW_AT_specification.
11461 For example, with GCC 3.4, given the code
11462
11463 namespace N {
11464 void foo() {
11465 // Definition of N::foo.
11466 }
11467 }
11468
11469 then we'll have a tree of DIEs like this:
11470
11471 1: DW_TAG_compile_unit
11472 2: DW_TAG_namespace // N
11473 3: DW_TAG_subprogram // declaration of N::foo
11474 4: DW_TAG_subprogram // definition of N::foo
11475 DW_AT_specification // refers to die #3
11476
11477 Thus, when processing die #4, we have to pretend that we're in
11478 the context of its DW_AT_specification, namely the contex of die
11479 #3. */
11480 spec_cu = cu;
11481 spec_die = die_specification (die, &spec_cu);
11482 if (spec_die == NULL)
11483 parent = die->parent;
11484 else
63d06c5c 11485 {
0114d602
DJ
11486 parent = spec_die->parent;
11487 cu = spec_cu;
63d06c5c 11488 }
0114d602
DJ
11489
11490 if (parent == NULL)
11491 return "";
98bfdba5
PA
11492 else if (parent->building_fullname)
11493 {
11494 const char *name;
11495 const char *parent_name;
11496
11497 /* It has been seen on RealView 2.2 built binaries,
11498 DW_TAG_template_type_param types actually _defined_ as
11499 children of the parent class:
11500
11501 enum E {};
11502 template class <class Enum> Class{};
11503 Class<enum E> class_e;
11504
11505 1: DW_TAG_class_type (Class)
11506 2: DW_TAG_enumeration_type (E)
11507 3: DW_TAG_enumerator (enum1:0)
11508 3: DW_TAG_enumerator (enum2:1)
11509 ...
11510 2: DW_TAG_template_type_param
11511 DW_AT_type DW_FORM_ref_udata (E)
11512
11513 Besides being broken debug info, it can put GDB into an
11514 infinite loop. Consider:
11515
11516 When we're building the full name for Class<E>, we'll start
11517 at Class, and go look over its template type parameters,
11518 finding E. We'll then try to build the full name of E, and
11519 reach here. We're now trying to build the full name of E,
11520 and look over the parent DIE for containing scope. In the
11521 broken case, if we followed the parent DIE of E, we'd again
11522 find Class, and once again go look at its template type
11523 arguments, etc., etc. Simply don't consider such parent die
11524 as source-level parent of this die (it can't be, the language
11525 doesn't allow it), and break the loop here. */
11526 name = dwarf2_name (die, cu);
11527 parent_name = dwarf2_name (parent, cu);
11528 complaint (&symfile_complaints,
11529 _("template param type '%s' defined within parent '%s'"),
11530 name ? name : "<unknown>",
11531 parent_name ? parent_name : "<unknown>");
11532 return "";
11533 }
63d06c5c 11534 else
0114d602
DJ
11535 switch (parent->tag)
11536 {
63d06c5c 11537 case DW_TAG_namespace:
0114d602 11538 parent_type = read_type_die (parent, cu);
acebe513
UW
11539 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11540 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11541 Work around this problem here. */
11542 if (cu->language == language_cplus
11543 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11544 return "";
0114d602
DJ
11545 /* We give a name to even anonymous namespaces. */
11546 return TYPE_TAG_NAME (parent_type);
63d06c5c 11547 case DW_TAG_class_type:
680b30c7 11548 case DW_TAG_interface_type:
63d06c5c 11549 case DW_TAG_structure_type:
0114d602 11550 case DW_TAG_union_type:
f55ee35c 11551 case DW_TAG_module:
0114d602
DJ
11552 parent_type = read_type_die (parent, cu);
11553 if (TYPE_TAG_NAME (parent_type) != NULL)
11554 return TYPE_TAG_NAME (parent_type);
11555 else
11556 /* An anonymous structure is only allowed non-static data
11557 members; no typedefs, no member functions, et cetera.
11558 So it does not need a prefix. */
11559 return "";
abc72ce4
DE
11560 case DW_TAG_compile_unit:
11561 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11562 if (cu->language == language_cplus
11563 && dwarf2_per_objfile->types.asection != NULL
11564 && die->child != NULL
11565 && (die->tag == DW_TAG_class_type
11566 || die->tag == DW_TAG_structure_type
11567 || die->tag == DW_TAG_union_type))
11568 {
11569 char *name = guess_full_die_structure_name (die, cu);
11570 if (name != NULL)
11571 return name;
11572 }
11573 return "";
63d06c5c 11574 default:
8176b9b8 11575 return determine_prefix (parent, cu);
63d06c5c 11576 }
63d06c5c
DC
11577}
11578
987504bb
JJ
11579/* Return a newly-allocated string formed by concatenating PREFIX and
11580 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11581 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11582 perform an obconcat, otherwise allocate storage for the result. The CU argument
11583 is used to determine the language and hence, the appropriate separator. */
11584
f55ee35c 11585#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11586
11587static char *
f55ee35c
JK
11588typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11589 int physname, struct dwarf2_cu *cu)
63d06c5c 11590{
f55ee35c 11591 const char *lead = "";
5c315b68 11592 const char *sep;
63d06c5c 11593
987504bb
JJ
11594 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11595 sep = "";
11596 else if (cu->language == language_java)
11597 sep = ".";
f55ee35c
JK
11598 else if (cu->language == language_fortran && physname)
11599 {
11600 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11601 DW_AT_MIPS_linkage_name is preferred and used instead. */
11602
11603 lead = "__";
11604 sep = "_MOD_";
11605 }
987504bb
JJ
11606 else
11607 sep = "::";
63d06c5c 11608
6dd47d34
DE
11609 if (prefix == NULL)
11610 prefix = "";
11611 if (suffix == NULL)
11612 suffix = "";
11613
987504bb
JJ
11614 if (obs == NULL)
11615 {
11616 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11617
f55ee35c
JK
11618 strcpy (retval, lead);
11619 strcat (retval, prefix);
6dd47d34
DE
11620 strcat (retval, sep);
11621 strcat (retval, suffix);
63d06c5c
DC
11622 return retval;
11623 }
987504bb
JJ
11624 else
11625 {
11626 /* We have an obstack. */
f55ee35c 11627 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11628 }
63d06c5c
DC
11629}
11630
c906108c
SS
11631/* Return sibling of die, NULL if no sibling. */
11632
f9aca02d 11633static struct die_info *
fba45db2 11634sibling_die (struct die_info *die)
c906108c 11635{
639d11d3 11636 return die->sibling;
c906108c
SS
11637}
11638
71c25dea
TT
11639/* Get name of a die, return NULL if not found. */
11640
11641static char *
11642dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11643 struct obstack *obstack)
11644{
11645 if (name && cu->language == language_cplus)
11646 {
11647 char *canon_name = cp_canonicalize_string (name);
11648
11649 if (canon_name != NULL)
11650 {
11651 if (strcmp (canon_name, name) != 0)
11652 name = obsavestring (canon_name, strlen (canon_name),
11653 obstack);
11654 xfree (canon_name);
11655 }
11656 }
11657
11658 return name;
c906108c
SS
11659}
11660
9219021c
DC
11661/* Get name of a die, return NULL if not found. */
11662
11663static char *
e142c38c 11664dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11665{
11666 struct attribute *attr;
11667
e142c38c 11668 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11669 if (!attr || !DW_STRING (attr))
11670 return NULL;
11671
11672 switch (die->tag)
11673 {
11674 case DW_TAG_compile_unit:
11675 /* Compilation units have a DW_AT_name that is a filename, not
11676 a source language identifier. */
11677 case DW_TAG_enumeration_type:
11678 case DW_TAG_enumerator:
11679 /* These tags always have simple identifiers already; no need
11680 to canonicalize them. */
11681 return DW_STRING (attr);
907af001 11682
418835cc
KS
11683 case DW_TAG_subprogram:
11684 /* Java constructors will all be named "<init>", so return
11685 the class name when we see this special case. */
11686 if (cu->language == language_java
11687 && DW_STRING (attr) != NULL
11688 && strcmp (DW_STRING (attr), "<init>") == 0)
11689 {
11690 struct dwarf2_cu *spec_cu = cu;
11691 struct die_info *spec_die;
11692
11693 /* GCJ will output '<init>' for Java constructor names.
11694 For this special case, return the name of the parent class. */
11695
11696 /* GCJ may output suprogram DIEs with AT_specification set.
11697 If so, use the name of the specified DIE. */
11698 spec_die = die_specification (die, &spec_cu);
11699 if (spec_die != NULL)
11700 return dwarf2_name (spec_die, spec_cu);
11701
11702 do
11703 {
11704 die = die->parent;
11705 if (die->tag == DW_TAG_class_type)
11706 return dwarf2_name (die, cu);
11707 }
11708 while (die->tag != DW_TAG_compile_unit);
11709 }
907af001
UW
11710 break;
11711
11712 case DW_TAG_class_type:
11713 case DW_TAG_interface_type:
11714 case DW_TAG_structure_type:
11715 case DW_TAG_union_type:
11716 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11717 structures or unions. These were of the form "._%d" in GCC 4.1,
11718 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11719 and GCC 4.4. We work around this problem by ignoring these. */
11720 if (strncmp (DW_STRING (attr), "._", 2) == 0
11721 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11722 return NULL;
11723 break;
11724
71c25dea 11725 default:
907af001
UW
11726 break;
11727 }
11728
11729 if (!DW_STRING_IS_CANONICAL (attr))
11730 {
11731 DW_STRING (attr)
11732 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11733 &cu->objfile->objfile_obstack);
11734 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11735 }
907af001 11736 return DW_STRING (attr);
9219021c
DC
11737}
11738
11739/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11740 is none. *EXT_CU is the CU containing DIE on input, and the CU
11741 containing the return value on output. */
9219021c
DC
11742
11743static struct die_info *
f2f0e013 11744dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11745{
11746 struct attribute *attr;
9219021c 11747
f2f0e013 11748 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11749 if (attr == NULL)
11750 return NULL;
11751
f2f0e013 11752 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11753}
11754
c906108c
SS
11755/* Convert a DIE tag into its string name. */
11756
11757static char *
aa1ee363 11758dwarf_tag_name (unsigned tag)
c906108c
SS
11759{
11760 switch (tag)
11761 {
11762 case DW_TAG_padding:
11763 return "DW_TAG_padding";
11764 case DW_TAG_array_type:
11765 return "DW_TAG_array_type";
11766 case DW_TAG_class_type:
11767 return "DW_TAG_class_type";
11768 case DW_TAG_entry_point:
11769 return "DW_TAG_entry_point";
11770 case DW_TAG_enumeration_type:
11771 return "DW_TAG_enumeration_type";
11772 case DW_TAG_formal_parameter:
11773 return "DW_TAG_formal_parameter";
11774 case DW_TAG_imported_declaration:
11775 return "DW_TAG_imported_declaration";
11776 case DW_TAG_label:
11777 return "DW_TAG_label";
11778 case DW_TAG_lexical_block:
11779 return "DW_TAG_lexical_block";
11780 case DW_TAG_member:
11781 return "DW_TAG_member";
11782 case DW_TAG_pointer_type:
11783 return "DW_TAG_pointer_type";
11784 case DW_TAG_reference_type:
11785 return "DW_TAG_reference_type";
11786 case DW_TAG_compile_unit:
11787 return "DW_TAG_compile_unit";
11788 case DW_TAG_string_type:
11789 return "DW_TAG_string_type";
11790 case DW_TAG_structure_type:
11791 return "DW_TAG_structure_type";
11792 case DW_TAG_subroutine_type:
11793 return "DW_TAG_subroutine_type";
11794 case DW_TAG_typedef:
11795 return "DW_TAG_typedef";
11796 case DW_TAG_union_type:
11797 return "DW_TAG_union_type";
11798 case DW_TAG_unspecified_parameters:
11799 return "DW_TAG_unspecified_parameters";
11800 case DW_TAG_variant:
11801 return "DW_TAG_variant";
11802 case DW_TAG_common_block:
11803 return "DW_TAG_common_block";
11804 case DW_TAG_common_inclusion:
11805 return "DW_TAG_common_inclusion";
11806 case DW_TAG_inheritance:
11807 return "DW_TAG_inheritance";
11808 case DW_TAG_inlined_subroutine:
11809 return "DW_TAG_inlined_subroutine";
11810 case DW_TAG_module:
11811 return "DW_TAG_module";
11812 case DW_TAG_ptr_to_member_type:
11813 return "DW_TAG_ptr_to_member_type";
11814 case DW_TAG_set_type:
11815 return "DW_TAG_set_type";
11816 case DW_TAG_subrange_type:
11817 return "DW_TAG_subrange_type";
11818 case DW_TAG_with_stmt:
11819 return "DW_TAG_with_stmt";
11820 case DW_TAG_access_declaration:
11821 return "DW_TAG_access_declaration";
11822 case DW_TAG_base_type:
11823 return "DW_TAG_base_type";
11824 case DW_TAG_catch_block:
11825 return "DW_TAG_catch_block";
11826 case DW_TAG_const_type:
11827 return "DW_TAG_const_type";
11828 case DW_TAG_constant:
11829 return "DW_TAG_constant";
11830 case DW_TAG_enumerator:
11831 return "DW_TAG_enumerator";
11832 case DW_TAG_file_type:
11833 return "DW_TAG_file_type";
11834 case DW_TAG_friend:
11835 return "DW_TAG_friend";
11836 case DW_TAG_namelist:
11837 return "DW_TAG_namelist";
11838 case DW_TAG_namelist_item:
11839 return "DW_TAG_namelist_item";
11840 case DW_TAG_packed_type:
11841 return "DW_TAG_packed_type";
11842 case DW_TAG_subprogram:
11843 return "DW_TAG_subprogram";
11844 case DW_TAG_template_type_param:
11845 return "DW_TAG_template_type_param";
11846 case DW_TAG_template_value_param:
11847 return "DW_TAG_template_value_param";
11848 case DW_TAG_thrown_type:
11849 return "DW_TAG_thrown_type";
11850 case DW_TAG_try_block:
11851 return "DW_TAG_try_block";
11852 case DW_TAG_variant_part:
11853 return "DW_TAG_variant_part";
11854 case DW_TAG_variable:
11855 return "DW_TAG_variable";
11856 case DW_TAG_volatile_type:
11857 return "DW_TAG_volatile_type";
d9fa45fe
DC
11858 case DW_TAG_dwarf_procedure:
11859 return "DW_TAG_dwarf_procedure";
11860 case DW_TAG_restrict_type:
11861 return "DW_TAG_restrict_type";
11862 case DW_TAG_interface_type:
11863 return "DW_TAG_interface_type";
11864 case DW_TAG_namespace:
11865 return "DW_TAG_namespace";
11866 case DW_TAG_imported_module:
11867 return "DW_TAG_imported_module";
11868 case DW_TAG_unspecified_type:
11869 return "DW_TAG_unspecified_type";
11870 case DW_TAG_partial_unit:
11871 return "DW_TAG_partial_unit";
11872 case DW_TAG_imported_unit:
11873 return "DW_TAG_imported_unit";
b7619582
GF
11874 case DW_TAG_condition:
11875 return "DW_TAG_condition";
11876 case DW_TAG_shared_type:
11877 return "DW_TAG_shared_type";
348e048f
DE
11878 case DW_TAG_type_unit:
11879 return "DW_TAG_type_unit";
c906108c
SS
11880 case DW_TAG_MIPS_loop:
11881 return "DW_TAG_MIPS_loop";
b7619582
GF
11882 case DW_TAG_HP_array_descriptor:
11883 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11884 case DW_TAG_format_label:
11885 return "DW_TAG_format_label";
11886 case DW_TAG_function_template:
11887 return "DW_TAG_function_template";
11888 case DW_TAG_class_template:
11889 return "DW_TAG_class_template";
b7619582
GF
11890 case DW_TAG_GNU_BINCL:
11891 return "DW_TAG_GNU_BINCL";
11892 case DW_TAG_GNU_EINCL:
11893 return "DW_TAG_GNU_EINCL";
11894 case DW_TAG_upc_shared_type:
11895 return "DW_TAG_upc_shared_type";
11896 case DW_TAG_upc_strict_type:
11897 return "DW_TAG_upc_strict_type";
11898 case DW_TAG_upc_relaxed_type:
11899 return "DW_TAG_upc_relaxed_type";
11900 case DW_TAG_PGI_kanji_type:
11901 return "DW_TAG_PGI_kanji_type";
11902 case DW_TAG_PGI_interface_block:
11903 return "DW_TAG_PGI_interface_block";
c906108c
SS
11904 default:
11905 return "DW_TAG_<unknown>";
11906 }
11907}
11908
11909/* Convert a DWARF attribute code into its string name. */
11910
11911static char *
aa1ee363 11912dwarf_attr_name (unsigned attr)
c906108c
SS
11913{
11914 switch (attr)
11915 {
11916 case DW_AT_sibling:
11917 return "DW_AT_sibling";
11918 case DW_AT_location:
11919 return "DW_AT_location";
11920 case DW_AT_name:
11921 return "DW_AT_name";
11922 case DW_AT_ordering:
11923 return "DW_AT_ordering";
11924 case DW_AT_subscr_data:
11925 return "DW_AT_subscr_data";
11926 case DW_AT_byte_size:
11927 return "DW_AT_byte_size";
11928 case DW_AT_bit_offset:
11929 return "DW_AT_bit_offset";
11930 case DW_AT_bit_size:
11931 return "DW_AT_bit_size";
11932 case DW_AT_element_list:
11933 return "DW_AT_element_list";
11934 case DW_AT_stmt_list:
11935 return "DW_AT_stmt_list";
11936 case DW_AT_low_pc:
11937 return "DW_AT_low_pc";
11938 case DW_AT_high_pc:
11939 return "DW_AT_high_pc";
11940 case DW_AT_language:
11941 return "DW_AT_language";
11942 case DW_AT_member:
11943 return "DW_AT_member";
11944 case DW_AT_discr:
11945 return "DW_AT_discr";
11946 case DW_AT_discr_value:
11947 return "DW_AT_discr_value";
11948 case DW_AT_visibility:
11949 return "DW_AT_visibility";
11950 case DW_AT_import:
11951 return "DW_AT_import";
11952 case DW_AT_string_length:
11953 return "DW_AT_string_length";
11954 case DW_AT_common_reference:
11955 return "DW_AT_common_reference";
11956 case DW_AT_comp_dir:
11957 return "DW_AT_comp_dir";
11958 case DW_AT_const_value:
11959 return "DW_AT_const_value";
11960 case DW_AT_containing_type:
11961 return "DW_AT_containing_type";
11962 case DW_AT_default_value:
11963 return "DW_AT_default_value";
11964 case DW_AT_inline:
11965 return "DW_AT_inline";
11966 case DW_AT_is_optional:
11967 return "DW_AT_is_optional";
11968 case DW_AT_lower_bound:
11969 return "DW_AT_lower_bound";
11970 case DW_AT_producer:
11971 return "DW_AT_producer";
11972 case DW_AT_prototyped:
11973 return "DW_AT_prototyped";
11974 case DW_AT_return_addr:
11975 return "DW_AT_return_addr";
11976 case DW_AT_start_scope:
11977 return "DW_AT_start_scope";
09fa0d7c
JK
11978 case DW_AT_bit_stride:
11979 return "DW_AT_bit_stride";
c906108c
SS
11980 case DW_AT_upper_bound:
11981 return "DW_AT_upper_bound";
11982 case DW_AT_abstract_origin:
11983 return "DW_AT_abstract_origin";
11984 case DW_AT_accessibility:
11985 return "DW_AT_accessibility";
11986 case DW_AT_address_class:
11987 return "DW_AT_address_class";
11988 case DW_AT_artificial:
11989 return "DW_AT_artificial";
11990 case DW_AT_base_types:
11991 return "DW_AT_base_types";
11992 case DW_AT_calling_convention:
11993 return "DW_AT_calling_convention";
11994 case DW_AT_count:
11995 return "DW_AT_count";
11996 case DW_AT_data_member_location:
11997 return "DW_AT_data_member_location";
11998 case DW_AT_decl_column:
11999 return "DW_AT_decl_column";
12000 case DW_AT_decl_file:
12001 return "DW_AT_decl_file";
12002 case DW_AT_decl_line:
12003 return "DW_AT_decl_line";
12004 case DW_AT_declaration:
12005 return "DW_AT_declaration";
12006 case DW_AT_discr_list:
12007 return "DW_AT_discr_list";
12008 case DW_AT_encoding:
12009 return "DW_AT_encoding";
12010 case DW_AT_external:
12011 return "DW_AT_external";
12012 case DW_AT_frame_base:
12013 return "DW_AT_frame_base";
12014 case DW_AT_friend:
12015 return "DW_AT_friend";
12016 case DW_AT_identifier_case:
12017 return "DW_AT_identifier_case";
12018 case DW_AT_macro_info:
12019 return "DW_AT_macro_info";
12020 case DW_AT_namelist_items:
12021 return "DW_AT_namelist_items";
12022 case DW_AT_priority:
12023 return "DW_AT_priority";
12024 case DW_AT_segment:
12025 return "DW_AT_segment";
12026 case DW_AT_specification:
12027 return "DW_AT_specification";
12028 case DW_AT_static_link:
12029 return "DW_AT_static_link";
12030 case DW_AT_type:
12031 return "DW_AT_type";
12032 case DW_AT_use_location:
12033 return "DW_AT_use_location";
12034 case DW_AT_variable_parameter:
12035 return "DW_AT_variable_parameter";
12036 case DW_AT_virtuality:
12037 return "DW_AT_virtuality";
12038 case DW_AT_vtable_elem_location:
12039 return "DW_AT_vtable_elem_location";
b7619582 12040 /* DWARF 3 values. */
d9fa45fe
DC
12041 case DW_AT_allocated:
12042 return "DW_AT_allocated";
12043 case DW_AT_associated:
12044 return "DW_AT_associated";
12045 case DW_AT_data_location:
12046 return "DW_AT_data_location";
09fa0d7c
JK
12047 case DW_AT_byte_stride:
12048 return "DW_AT_byte_stride";
d9fa45fe
DC
12049 case DW_AT_entry_pc:
12050 return "DW_AT_entry_pc";
12051 case DW_AT_use_UTF8:
12052 return "DW_AT_use_UTF8";
12053 case DW_AT_extension:
12054 return "DW_AT_extension";
12055 case DW_AT_ranges:
12056 return "DW_AT_ranges";
12057 case DW_AT_trampoline:
12058 return "DW_AT_trampoline";
12059 case DW_AT_call_column:
12060 return "DW_AT_call_column";
12061 case DW_AT_call_file:
12062 return "DW_AT_call_file";
12063 case DW_AT_call_line:
12064 return "DW_AT_call_line";
b7619582
GF
12065 case DW_AT_description:
12066 return "DW_AT_description";
12067 case DW_AT_binary_scale:
12068 return "DW_AT_binary_scale";
12069 case DW_AT_decimal_scale:
12070 return "DW_AT_decimal_scale";
12071 case DW_AT_small:
12072 return "DW_AT_small";
12073 case DW_AT_decimal_sign:
12074 return "DW_AT_decimal_sign";
12075 case DW_AT_digit_count:
12076 return "DW_AT_digit_count";
12077 case DW_AT_picture_string:
12078 return "DW_AT_picture_string";
12079 case DW_AT_mutable:
12080 return "DW_AT_mutable";
12081 case DW_AT_threads_scaled:
12082 return "DW_AT_threads_scaled";
12083 case DW_AT_explicit:
12084 return "DW_AT_explicit";
12085 case DW_AT_object_pointer:
12086 return "DW_AT_object_pointer";
12087 case DW_AT_endianity:
12088 return "DW_AT_endianity";
12089 case DW_AT_elemental:
12090 return "DW_AT_elemental";
12091 case DW_AT_pure:
12092 return "DW_AT_pure";
12093 case DW_AT_recursive:
12094 return "DW_AT_recursive";
348e048f
DE
12095 /* DWARF 4 values. */
12096 case DW_AT_signature:
12097 return "DW_AT_signature";
31ef98ae
TT
12098 case DW_AT_linkage_name:
12099 return "DW_AT_linkage_name";
b7619582 12100 /* SGI/MIPS extensions. */
c764a876 12101#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12102 case DW_AT_MIPS_fde:
12103 return "DW_AT_MIPS_fde";
c764a876 12104#endif
c906108c
SS
12105 case DW_AT_MIPS_loop_begin:
12106 return "DW_AT_MIPS_loop_begin";
12107 case DW_AT_MIPS_tail_loop_begin:
12108 return "DW_AT_MIPS_tail_loop_begin";
12109 case DW_AT_MIPS_epilog_begin:
12110 return "DW_AT_MIPS_epilog_begin";
12111 case DW_AT_MIPS_loop_unroll_factor:
12112 return "DW_AT_MIPS_loop_unroll_factor";
12113 case DW_AT_MIPS_software_pipeline_depth:
12114 return "DW_AT_MIPS_software_pipeline_depth";
12115 case DW_AT_MIPS_linkage_name:
12116 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12117 case DW_AT_MIPS_stride:
12118 return "DW_AT_MIPS_stride";
12119 case DW_AT_MIPS_abstract_name:
12120 return "DW_AT_MIPS_abstract_name";
12121 case DW_AT_MIPS_clone_origin:
12122 return "DW_AT_MIPS_clone_origin";
12123 case DW_AT_MIPS_has_inlines:
12124 return "DW_AT_MIPS_has_inlines";
b7619582 12125 /* HP extensions. */
c764a876 12126#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12127 case DW_AT_HP_block_index:
12128 return "DW_AT_HP_block_index";
c764a876 12129#endif
b7619582
GF
12130 case DW_AT_HP_unmodifiable:
12131 return "DW_AT_HP_unmodifiable";
12132 case DW_AT_HP_actuals_stmt_list:
12133 return "DW_AT_HP_actuals_stmt_list";
12134 case DW_AT_HP_proc_per_section:
12135 return "DW_AT_HP_proc_per_section";
12136 case DW_AT_HP_raw_data_ptr:
12137 return "DW_AT_HP_raw_data_ptr";
12138 case DW_AT_HP_pass_by_reference:
12139 return "DW_AT_HP_pass_by_reference";
12140 case DW_AT_HP_opt_level:
12141 return "DW_AT_HP_opt_level";
12142 case DW_AT_HP_prof_version_id:
12143 return "DW_AT_HP_prof_version_id";
12144 case DW_AT_HP_opt_flags:
12145 return "DW_AT_HP_opt_flags";
12146 case DW_AT_HP_cold_region_low_pc:
12147 return "DW_AT_HP_cold_region_low_pc";
12148 case DW_AT_HP_cold_region_high_pc:
12149 return "DW_AT_HP_cold_region_high_pc";
12150 case DW_AT_HP_all_variables_modifiable:
12151 return "DW_AT_HP_all_variables_modifiable";
12152 case DW_AT_HP_linkage_name:
12153 return "DW_AT_HP_linkage_name";
12154 case DW_AT_HP_prof_flags:
12155 return "DW_AT_HP_prof_flags";
12156 /* GNU extensions. */
c906108c
SS
12157 case DW_AT_sf_names:
12158 return "DW_AT_sf_names";
12159 case DW_AT_src_info:
12160 return "DW_AT_src_info";
12161 case DW_AT_mac_info:
12162 return "DW_AT_mac_info";
12163 case DW_AT_src_coords:
12164 return "DW_AT_src_coords";
12165 case DW_AT_body_begin:
12166 return "DW_AT_body_begin";
12167 case DW_AT_body_end:
12168 return "DW_AT_body_end";
f5f8a009
EZ
12169 case DW_AT_GNU_vector:
12170 return "DW_AT_GNU_vector";
2de00c64
DE
12171 case DW_AT_GNU_odr_signature:
12172 return "DW_AT_GNU_odr_signature";
b7619582
GF
12173 /* VMS extensions. */
12174 case DW_AT_VMS_rtnbeg_pd_address:
12175 return "DW_AT_VMS_rtnbeg_pd_address";
12176 /* UPC extension. */
12177 case DW_AT_upc_threads_scaled:
12178 return "DW_AT_upc_threads_scaled";
12179 /* PGI (STMicroelectronics) extensions. */
12180 case DW_AT_PGI_lbase:
12181 return "DW_AT_PGI_lbase";
12182 case DW_AT_PGI_soffset:
12183 return "DW_AT_PGI_soffset";
12184 case DW_AT_PGI_lstride:
12185 return "DW_AT_PGI_lstride";
c906108c
SS
12186 default:
12187 return "DW_AT_<unknown>";
12188 }
12189}
12190
12191/* Convert a DWARF value form code into its string name. */
12192
12193static char *
aa1ee363 12194dwarf_form_name (unsigned form)
c906108c
SS
12195{
12196 switch (form)
12197 {
12198 case DW_FORM_addr:
12199 return "DW_FORM_addr";
12200 case DW_FORM_block2:
12201 return "DW_FORM_block2";
12202 case DW_FORM_block4:
12203 return "DW_FORM_block4";
12204 case DW_FORM_data2:
12205 return "DW_FORM_data2";
12206 case DW_FORM_data4:
12207 return "DW_FORM_data4";
12208 case DW_FORM_data8:
12209 return "DW_FORM_data8";
12210 case DW_FORM_string:
12211 return "DW_FORM_string";
12212 case DW_FORM_block:
12213 return "DW_FORM_block";
12214 case DW_FORM_block1:
12215 return "DW_FORM_block1";
12216 case DW_FORM_data1:
12217 return "DW_FORM_data1";
12218 case DW_FORM_flag:
12219 return "DW_FORM_flag";
12220 case DW_FORM_sdata:
12221 return "DW_FORM_sdata";
12222 case DW_FORM_strp:
12223 return "DW_FORM_strp";
12224 case DW_FORM_udata:
12225 return "DW_FORM_udata";
12226 case DW_FORM_ref_addr:
12227 return "DW_FORM_ref_addr";
12228 case DW_FORM_ref1:
12229 return "DW_FORM_ref1";
12230 case DW_FORM_ref2:
12231 return "DW_FORM_ref2";
12232 case DW_FORM_ref4:
12233 return "DW_FORM_ref4";
12234 case DW_FORM_ref8:
12235 return "DW_FORM_ref8";
12236 case DW_FORM_ref_udata:
12237 return "DW_FORM_ref_udata";
12238 case DW_FORM_indirect:
12239 return "DW_FORM_indirect";
348e048f
DE
12240 case DW_FORM_sec_offset:
12241 return "DW_FORM_sec_offset";
12242 case DW_FORM_exprloc:
12243 return "DW_FORM_exprloc";
12244 case DW_FORM_flag_present:
12245 return "DW_FORM_flag_present";
12246 case DW_FORM_sig8:
12247 return "DW_FORM_sig8";
c906108c
SS
12248 default:
12249 return "DW_FORM_<unknown>";
12250 }
12251}
12252
12253/* Convert a DWARF stack opcode into its string name. */
12254
9eae7c52
TT
12255const char *
12256dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12257{
12258 switch (op)
12259 {
12260 case DW_OP_addr:
12261 return "DW_OP_addr";
12262 case DW_OP_deref:
12263 return "DW_OP_deref";
12264 case DW_OP_const1u:
12265 return "DW_OP_const1u";
12266 case DW_OP_const1s:
12267 return "DW_OP_const1s";
12268 case DW_OP_const2u:
12269 return "DW_OP_const2u";
12270 case DW_OP_const2s:
12271 return "DW_OP_const2s";
12272 case DW_OP_const4u:
12273 return "DW_OP_const4u";
12274 case DW_OP_const4s:
12275 return "DW_OP_const4s";
12276 case DW_OP_const8u:
12277 return "DW_OP_const8u";
12278 case DW_OP_const8s:
12279 return "DW_OP_const8s";
12280 case DW_OP_constu:
12281 return "DW_OP_constu";
12282 case DW_OP_consts:
12283 return "DW_OP_consts";
12284 case DW_OP_dup:
12285 return "DW_OP_dup";
12286 case DW_OP_drop:
12287 return "DW_OP_drop";
12288 case DW_OP_over:
12289 return "DW_OP_over";
12290 case DW_OP_pick:
12291 return "DW_OP_pick";
12292 case DW_OP_swap:
12293 return "DW_OP_swap";
12294 case DW_OP_rot:
12295 return "DW_OP_rot";
12296 case DW_OP_xderef:
12297 return "DW_OP_xderef";
12298 case DW_OP_abs:
12299 return "DW_OP_abs";
12300 case DW_OP_and:
12301 return "DW_OP_and";
12302 case DW_OP_div:
12303 return "DW_OP_div";
12304 case DW_OP_minus:
12305 return "DW_OP_minus";
12306 case DW_OP_mod:
12307 return "DW_OP_mod";
12308 case DW_OP_mul:
12309 return "DW_OP_mul";
12310 case DW_OP_neg:
12311 return "DW_OP_neg";
12312 case DW_OP_not:
12313 return "DW_OP_not";
12314 case DW_OP_or:
12315 return "DW_OP_or";
12316 case DW_OP_plus:
12317 return "DW_OP_plus";
12318 case DW_OP_plus_uconst:
12319 return "DW_OP_plus_uconst";
12320 case DW_OP_shl:
12321 return "DW_OP_shl";
12322 case DW_OP_shr:
12323 return "DW_OP_shr";
12324 case DW_OP_shra:
12325 return "DW_OP_shra";
12326 case DW_OP_xor:
12327 return "DW_OP_xor";
12328 case DW_OP_bra:
12329 return "DW_OP_bra";
12330 case DW_OP_eq:
12331 return "DW_OP_eq";
12332 case DW_OP_ge:
12333 return "DW_OP_ge";
12334 case DW_OP_gt:
12335 return "DW_OP_gt";
12336 case DW_OP_le:
12337 return "DW_OP_le";
12338 case DW_OP_lt:
12339 return "DW_OP_lt";
12340 case DW_OP_ne:
12341 return "DW_OP_ne";
12342 case DW_OP_skip:
12343 return "DW_OP_skip";
12344 case DW_OP_lit0:
12345 return "DW_OP_lit0";
12346 case DW_OP_lit1:
12347 return "DW_OP_lit1";
12348 case DW_OP_lit2:
12349 return "DW_OP_lit2";
12350 case DW_OP_lit3:
12351 return "DW_OP_lit3";
12352 case DW_OP_lit4:
12353 return "DW_OP_lit4";
12354 case DW_OP_lit5:
12355 return "DW_OP_lit5";
12356 case DW_OP_lit6:
12357 return "DW_OP_lit6";
12358 case DW_OP_lit7:
12359 return "DW_OP_lit7";
12360 case DW_OP_lit8:
12361 return "DW_OP_lit8";
12362 case DW_OP_lit9:
12363 return "DW_OP_lit9";
12364 case DW_OP_lit10:
12365 return "DW_OP_lit10";
12366 case DW_OP_lit11:
12367 return "DW_OP_lit11";
12368 case DW_OP_lit12:
12369 return "DW_OP_lit12";
12370 case DW_OP_lit13:
12371 return "DW_OP_lit13";
12372 case DW_OP_lit14:
12373 return "DW_OP_lit14";
12374 case DW_OP_lit15:
12375 return "DW_OP_lit15";
12376 case DW_OP_lit16:
12377 return "DW_OP_lit16";
12378 case DW_OP_lit17:
12379 return "DW_OP_lit17";
12380 case DW_OP_lit18:
12381 return "DW_OP_lit18";
12382 case DW_OP_lit19:
12383 return "DW_OP_lit19";
12384 case DW_OP_lit20:
12385 return "DW_OP_lit20";
12386 case DW_OP_lit21:
12387 return "DW_OP_lit21";
12388 case DW_OP_lit22:
12389 return "DW_OP_lit22";
12390 case DW_OP_lit23:
12391 return "DW_OP_lit23";
12392 case DW_OP_lit24:
12393 return "DW_OP_lit24";
12394 case DW_OP_lit25:
12395 return "DW_OP_lit25";
12396 case DW_OP_lit26:
12397 return "DW_OP_lit26";
12398 case DW_OP_lit27:
12399 return "DW_OP_lit27";
12400 case DW_OP_lit28:
12401 return "DW_OP_lit28";
12402 case DW_OP_lit29:
12403 return "DW_OP_lit29";
12404 case DW_OP_lit30:
12405 return "DW_OP_lit30";
12406 case DW_OP_lit31:
12407 return "DW_OP_lit31";
12408 case DW_OP_reg0:
12409 return "DW_OP_reg0";
12410 case DW_OP_reg1:
12411 return "DW_OP_reg1";
12412 case DW_OP_reg2:
12413 return "DW_OP_reg2";
12414 case DW_OP_reg3:
12415 return "DW_OP_reg3";
12416 case DW_OP_reg4:
12417 return "DW_OP_reg4";
12418 case DW_OP_reg5:
12419 return "DW_OP_reg5";
12420 case DW_OP_reg6:
12421 return "DW_OP_reg6";
12422 case DW_OP_reg7:
12423 return "DW_OP_reg7";
12424 case DW_OP_reg8:
12425 return "DW_OP_reg8";
12426 case DW_OP_reg9:
12427 return "DW_OP_reg9";
12428 case DW_OP_reg10:
12429 return "DW_OP_reg10";
12430 case DW_OP_reg11:
12431 return "DW_OP_reg11";
12432 case DW_OP_reg12:
12433 return "DW_OP_reg12";
12434 case DW_OP_reg13:
12435 return "DW_OP_reg13";
12436 case DW_OP_reg14:
12437 return "DW_OP_reg14";
12438 case DW_OP_reg15:
12439 return "DW_OP_reg15";
12440 case DW_OP_reg16:
12441 return "DW_OP_reg16";
12442 case DW_OP_reg17:
12443 return "DW_OP_reg17";
12444 case DW_OP_reg18:
12445 return "DW_OP_reg18";
12446 case DW_OP_reg19:
12447 return "DW_OP_reg19";
12448 case DW_OP_reg20:
12449 return "DW_OP_reg20";
12450 case DW_OP_reg21:
12451 return "DW_OP_reg21";
12452 case DW_OP_reg22:
12453 return "DW_OP_reg22";
12454 case DW_OP_reg23:
12455 return "DW_OP_reg23";
12456 case DW_OP_reg24:
12457 return "DW_OP_reg24";
12458 case DW_OP_reg25:
12459 return "DW_OP_reg25";
12460 case DW_OP_reg26:
12461 return "DW_OP_reg26";
12462 case DW_OP_reg27:
12463 return "DW_OP_reg27";
12464 case DW_OP_reg28:
12465 return "DW_OP_reg28";
12466 case DW_OP_reg29:
12467 return "DW_OP_reg29";
12468 case DW_OP_reg30:
12469 return "DW_OP_reg30";
12470 case DW_OP_reg31:
12471 return "DW_OP_reg31";
12472 case DW_OP_breg0:
12473 return "DW_OP_breg0";
12474 case DW_OP_breg1:
12475 return "DW_OP_breg1";
12476 case DW_OP_breg2:
12477 return "DW_OP_breg2";
12478 case DW_OP_breg3:
12479 return "DW_OP_breg3";
12480 case DW_OP_breg4:
12481 return "DW_OP_breg4";
12482 case DW_OP_breg5:
12483 return "DW_OP_breg5";
12484 case DW_OP_breg6:
12485 return "DW_OP_breg6";
12486 case DW_OP_breg7:
12487 return "DW_OP_breg7";
12488 case DW_OP_breg8:
12489 return "DW_OP_breg8";
12490 case DW_OP_breg9:
12491 return "DW_OP_breg9";
12492 case DW_OP_breg10:
12493 return "DW_OP_breg10";
12494 case DW_OP_breg11:
12495 return "DW_OP_breg11";
12496 case DW_OP_breg12:
12497 return "DW_OP_breg12";
12498 case DW_OP_breg13:
12499 return "DW_OP_breg13";
12500 case DW_OP_breg14:
12501 return "DW_OP_breg14";
12502 case DW_OP_breg15:
12503 return "DW_OP_breg15";
12504 case DW_OP_breg16:
12505 return "DW_OP_breg16";
12506 case DW_OP_breg17:
12507 return "DW_OP_breg17";
12508 case DW_OP_breg18:
12509 return "DW_OP_breg18";
12510 case DW_OP_breg19:
12511 return "DW_OP_breg19";
12512 case DW_OP_breg20:
12513 return "DW_OP_breg20";
12514 case DW_OP_breg21:
12515 return "DW_OP_breg21";
12516 case DW_OP_breg22:
12517 return "DW_OP_breg22";
12518 case DW_OP_breg23:
12519 return "DW_OP_breg23";
12520 case DW_OP_breg24:
12521 return "DW_OP_breg24";
12522 case DW_OP_breg25:
12523 return "DW_OP_breg25";
12524 case DW_OP_breg26:
12525 return "DW_OP_breg26";
12526 case DW_OP_breg27:
12527 return "DW_OP_breg27";
12528 case DW_OP_breg28:
12529 return "DW_OP_breg28";
12530 case DW_OP_breg29:
12531 return "DW_OP_breg29";
12532 case DW_OP_breg30:
12533 return "DW_OP_breg30";
12534 case DW_OP_breg31:
12535 return "DW_OP_breg31";
12536 case DW_OP_regx:
12537 return "DW_OP_regx";
12538 case DW_OP_fbreg:
12539 return "DW_OP_fbreg";
12540 case DW_OP_bregx:
12541 return "DW_OP_bregx";
12542 case DW_OP_piece:
12543 return "DW_OP_piece";
12544 case DW_OP_deref_size:
12545 return "DW_OP_deref_size";
12546 case DW_OP_xderef_size:
12547 return "DW_OP_xderef_size";
12548 case DW_OP_nop:
12549 return "DW_OP_nop";
b7619582 12550 /* DWARF 3 extensions. */
ed348acc
EZ
12551 case DW_OP_push_object_address:
12552 return "DW_OP_push_object_address";
12553 case DW_OP_call2:
12554 return "DW_OP_call2";
12555 case DW_OP_call4:
12556 return "DW_OP_call4";
12557 case DW_OP_call_ref:
12558 return "DW_OP_call_ref";
b7619582
GF
12559 case DW_OP_form_tls_address:
12560 return "DW_OP_form_tls_address";
12561 case DW_OP_call_frame_cfa:
12562 return "DW_OP_call_frame_cfa";
12563 case DW_OP_bit_piece:
12564 return "DW_OP_bit_piece";
9eae7c52
TT
12565 /* DWARF 4 extensions. */
12566 case DW_OP_implicit_value:
12567 return "DW_OP_implicit_value";
12568 case DW_OP_stack_value:
12569 return "DW_OP_stack_value";
12570 /* GNU extensions. */
ed348acc
EZ
12571 case DW_OP_GNU_push_tls_address:
12572 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12573 case DW_OP_GNU_uninit:
12574 return "DW_OP_GNU_uninit";
c906108c 12575 default:
9eae7c52 12576 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12577 }
12578}
12579
12580static char *
fba45db2 12581dwarf_bool_name (unsigned mybool)
c906108c
SS
12582{
12583 if (mybool)
12584 return "TRUE";
12585 else
12586 return "FALSE";
12587}
12588
12589/* Convert a DWARF type code into its string name. */
12590
12591static char *
aa1ee363 12592dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12593{
12594 switch (enc)
12595 {
b7619582
GF
12596 case DW_ATE_void:
12597 return "DW_ATE_void";
c906108c
SS
12598 case DW_ATE_address:
12599 return "DW_ATE_address";
12600 case DW_ATE_boolean:
12601 return "DW_ATE_boolean";
12602 case DW_ATE_complex_float:
12603 return "DW_ATE_complex_float";
12604 case DW_ATE_float:
12605 return "DW_ATE_float";
12606 case DW_ATE_signed:
12607 return "DW_ATE_signed";
12608 case DW_ATE_signed_char:
12609 return "DW_ATE_signed_char";
12610 case DW_ATE_unsigned:
12611 return "DW_ATE_unsigned";
12612 case DW_ATE_unsigned_char:
12613 return "DW_ATE_unsigned_char";
b7619582 12614 /* DWARF 3. */
d9fa45fe
DC
12615 case DW_ATE_imaginary_float:
12616 return "DW_ATE_imaginary_float";
b7619582
GF
12617 case DW_ATE_packed_decimal:
12618 return "DW_ATE_packed_decimal";
12619 case DW_ATE_numeric_string:
12620 return "DW_ATE_numeric_string";
12621 case DW_ATE_edited:
12622 return "DW_ATE_edited";
12623 case DW_ATE_signed_fixed:
12624 return "DW_ATE_signed_fixed";
12625 case DW_ATE_unsigned_fixed:
12626 return "DW_ATE_unsigned_fixed";
12627 case DW_ATE_decimal_float:
12628 return "DW_ATE_decimal_float";
75079b2b
TT
12629 /* DWARF 4. */
12630 case DW_ATE_UTF:
12631 return "DW_ATE_UTF";
b7619582
GF
12632 /* HP extensions. */
12633 case DW_ATE_HP_float80:
12634 return "DW_ATE_HP_float80";
12635 case DW_ATE_HP_complex_float80:
12636 return "DW_ATE_HP_complex_float80";
12637 case DW_ATE_HP_float128:
12638 return "DW_ATE_HP_float128";
12639 case DW_ATE_HP_complex_float128:
12640 return "DW_ATE_HP_complex_float128";
12641 case DW_ATE_HP_floathpintel:
12642 return "DW_ATE_HP_floathpintel";
12643 case DW_ATE_HP_imaginary_float80:
12644 return "DW_ATE_HP_imaginary_float80";
12645 case DW_ATE_HP_imaginary_float128:
12646 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12647 default:
12648 return "DW_ATE_<unknown>";
12649 }
12650}
12651
12652/* Convert a DWARF call frame info operation to its string name. */
12653
12654#if 0
12655static char *
aa1ee363 12656dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12657{
12658 switch (cfi_opc)
12659 {
12660 case DW_CFA_advance_loc:
12661 return "DW_CFA_advance_loc";
12662 case DW_CFA_offset:
12663 return "DW_CFA_offset";
12664 case DW_CFA_restore:
12665 return "DW_CFA_restore";
12666 case DW_CFA_nop:
12667 return "DW_CFA_nop";
12668 case DW_CFA_set_loc:
12669 return "DW_CFA_set_loc";
12670 case DW_CFA_advance_loc1:
12671 return "DW_CFA_advance_loc1";
12672 case DW_CFA_advance_loc2:
12673 return "DW_CFA_advance_loc2";
12674 case DW_CFA_advance_loc4:
12675 return "DW_CFA_advance_loc4";
12676 case DW_CFA_offset_extended:
12677 return "DW_CFA_offset_extended";
12678 case DW_CFA_restore_extended:
12679 return "DW_CFA_restore_extended";
12680 case DW_CFA_undefined:
12681 return "DW_CFA_undefined";
12682 case DW_CFA_same_value:
12683 return "DW_CFA_same_value";
12684 case DW_CFA_register:
12685 return "DW_CFA_register";
12686 case DW_CFA_remember_state:
12687 return "DW_CFA_remember_state";
12688 case DW_CFA_restore_state:
12689 return "DW_CFA_restore_state";
12690 case DW_CFA_def_cfa:
12691 return "DW_CFA_def_cfa";
12692 case DW_CFA_def_cfa_register:
12693 return "DW_CFA_def_cfa_register";
12694 case DW_CFA_def_cfa_offset:
12695 return "DW_CFA_def_cfa_offset";
b7619582 12696 /* DWARF 3. */
985cb1a3
JM
12697 case DW_CFA_def_cfa_expression:
12698 return "DW_CFA_def_cfa_expression";
12699 case DW_CFA_expression:
12700 return "DW_CFA_expression";
12701 case DW_CFA_offset_extended_sf:
12702 return "DW_CFA_offset_extended_sf";
12703 case DW_CFA_def_cfa_sf:
12704 return "DW_CFA_def_cfa_sf";
12705 case DW_CFA_def_cfa_offset_sf:
12706 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12707 case DW_CFA_val_offset:
12708 return "DW_CFA_val_offset";
12709 case DW_CFA_val_offset_sf:
12710 return "DW_CFA_val_offset_sf";
12711 case DW_CFA_val_expression:
12712 return "DW_CFA_val_expression";
12713 /* SGI/MIPS specific. */
c906108c
SS
12714 case DW_CFA_MIPS_advance_loc8:
12715 return "DW_CFA_MIPS_advance_loc8";
b7619582 12716 /* GNU extensions. */
985cb1a3
JM
12717 case DW_CFA_GNU_window_save:
12718 return "DW_CFA_GNU_window_save";
12719 case DW_CFA_GNU_args_size:
12720 return "DW_CFA_GNU_args_size";
12721 case DW_CFA_GNU_negative_offset_extended:
12722 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12723 default:
12724 return "DW_CFA_<unknown>";
12725 }
12726}
12727#endif
12728
f9aca02d 12729static void
d97bc12b 12730dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12731{
12732 unsigned int i;
12733
d97bc12b
DE
12734 print_spaces (indent, f);
12735 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12736 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12737
12738 if (die->parent != NULL)
12739 {
12740 print_spaces (indent, f);
12741 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12742 die->parent->offset);
12743 }
12744
12745 print_spaces (indent, f);
12746 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12747 dwarf_bool_name (die->child != NULL));
c906108c 12748
d97bc12b
DE
12749 print_spaces (indent, f);
12750 fprintf_unfiltered (f, " attributes:\n");
12751
c906108c
SS
12752 for (i = 0; i < die->num_attrs; ++i)
12753 {
d97bc12b
DE
12754 print_spaces (indent, f);
12755 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12756 dwarf_attr_name (die->attrs[i].name),
12757 dwarf_form_name (die->attrs[i].form));
d97bc12b 12758
c906108c
SS
12759 switch (die->attrs[i].form)
12760 {
12761 case DW_FORM_ref_addr:
12762 case DW_FORM_addr:
d97bc12b 12763 fprintf_unfiltered (f, "address: ");
5af949e3 12764 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12765 break;
12766 case DW_FORM_block2:
12767 case DW_FORM_block4:
12768 case DW_FORM_block:
12769 case DW_FORM_block1:
d97bc12b 12770 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12771 break;
2dc7f7b3
TT
12772 case DW_FORM_exprloc:
12773 fprintf_unfiltered (f, "expression: size %u",
12774 DW_BLOCK (&die->attrs[i])->size);
12775 break;
10b3939b
DJ
12776 case DW_FORM_ref1:
12777 case DW_FORM_ref2:
12778 case DW_FORM_ref4:
d97bc12b 12779 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12780 (long) (DW_ADDR (&die->attrs[i])));
12781 break;
c906108c
SS
12782 case DW_FORM_data1:
12783 case DW_FORM_data2:
12784 case DW_FORM_data4:
ce5d95e1 12785 case DW_FORM_data8:
c906108c
SS
12786 case DW_FORM_udata:
12787 case DW_FORM_sdata:
43bbcdc2
PH
12788 fprintf_unfiltered (f, "constant: %s",
12789 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12790 break;
2dc7f7b3
TT
12791 case DW_FORM_sec_offset:
12792 fprintf_unfiltered (f, "section offset: %s",
12793 pulongest (DW_UNSND (&die->attrs[i])));
12794 break;
348e048f
DE
12795 case DW_FORM_sig8:
12796 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12797 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12798 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12799 else
12800 fprintf_unfiltered (f, "signatured type, offset: unknown");
12801 break;
c906108c 12802 case DW_FORM_string:
4bdf3d34 12803 case DW_FORM_strp:
8285870a 12804 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12805 DW_STRING (&die->attrs[i])
8285870a
JK
12806 ? DW_STRING (&die->attrs[i]) : "",
12807 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12808 break;
12809 case DW_FORM_flag:
12810 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12811 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12812 else
d97bc12b 12813 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12814 break;
2dc7f7b3
TT
12815 case DW_FORM_flag_present:
12816 fprintf_unfiltered (f, "flag: TRUE");
12817 break;
a8329558
KW
12818 case DW_FORM_indirect:
12819 /* the reader will have reduced the indirect form to
12820 the "base form" so this form should not occur */
d97bc12b 12821 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12822 break;
c906108c 12823 default:
d97bc12b 12824 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12825 die->attrs[i].form);
d97bc12b 12826 break;
c906108c 12827 }
d97bc12b 12828 fprintf_unfiltered (f, "\n");
c906108c
SS
12829 }
12830}
12831
f9aca02d 12832static void
d97bc12b 12833dump_die_for_error (struct die_info *die)
c906108c 12834{
d97bc12b
DE
12835 dump_die_shallow (gdb_stderr, 0, die);
12836}
12837
12838static void
12839dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12840{
12841 int indent = level * 4;
12842
12843 gdb_assert (die != NULL);
12844
12845 if (level >= max_level)
12846 return;
12847
12848 dump_die_shallow (f, indent, die);
12849
12850 if (die->child != NULL)
c906108c 12851 {
d97bc12b
DE
12852 print_spaces (indent, f);
12853 fprintf_unfiltered (f, " Children:");
12854 if (level + 1 < max_level)
12855 {
12856 fprintf_unfiltered (f, "\n");
12857 dump_die_1 (f, level + 1, max_level, die->child);
12858 }
12859 else
12860 {
12861 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12862 }
12863 }
12864
12865 if (die->sibling != NULL && level > 0)
12866 {
12867 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12868 }
12869}
12870
d97bc12b
DE
12871/* This is called from the pdie macro in gdbinit.in.
12872 It's not static so gcc will keep a copy callable from gdb. */
12873
12874void
12875dump_die (struct die_info *die, int max_level)
12876{
12877 dump_die_1 (gdb_stdlog, 0, max_level, die);
12878}
12879
f9aca02d 12880static void
51545339 12881store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12882{
51545339 12883 void **slot;
c906108c 12884
51545339
DJ
12885 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12886
12887 *slot = die;
c906108c
SS
12888}
12889
93311388
DE
12890static int
12891is_ref_attr (struct attribute *attr)
c906108c 12892{
c906108c
SS
12893 switch (attr->form)
12894 {
12895 case DW_FORM_ref_addr:
c906108c
SS
12896 case DW_FORM_ref1:
12897 case DW_FORM_ref2:
12898 case DW_FORM_ref4:
613e1657 12899 case DW_FORM_ref8:
c906108c 12900 case DW_FORM_ref_udata:
93311388 12901 return 1;
c906108c 12902 default:
93311388 12903 return 0;
c906108c 12904 }
93311388
DE
12905}
12906
12907static unsigned int
12908dwarf2_get_ref_die_offset (struct attribute *attr)
12909{
12910 if (is_ref_attr (attr))
12911 return DW_ADDR (attr);
12912
12913 complaint (&symfile_complaints,
12914 _("unsupported die ref attribute form: '%s'"),
12915 dwarf_form_name (attr->form));
12916 return 0;
c906108c
SS
12917}
12918
43bbcdc2
PH
12919/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12920 * the value held by the attribute is not constant. */
a02abb62 12921
43bbcdc2 12922static LONGEST
a02abb62
JB
12923dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12924{
12925 if (attr->form == DW_FORM_sdata)
12926 return DW_SND (attr);
12927 else if (attr->form == DW_FORM_udata
12928 || attr->form == DW_FORM_data1
12929 || attr->form == DW_FORM_data2
12930 || attr->form == DW_FORM_data4
12931 || attr->form == DW_FORM_data8)
12932 return DW_UNSND (attr);
12933 else
12934 {
e2e0b3e5 12935 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12936 dwarf_form_name (attr->form));
12937 return default_value;
12938 }
12939}
12940
03dd20cc 12941/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12942 unit and add it to our queue.
12943 The result is non-zero if PER_CU was queued, otherwise the result is zero
12944 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12945
348e048f 12946static int
03dd20cc
DJ
12947maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12948 struct dwarf2_per_cu_data *per_cu)
12949{
98bfdba5
PA
12950 /* We may arrive here during partial symbol reading, if we need full
12951 DIEs to process an unusual case (e.g. template arguments). Do
12952 not queue PER_CU, just tell our caller to load its DIEs. */
12953 if (dwarf2_per_objfile->reading_partial_symbols)
12954 {
12955 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12956 return 1;
12957 return 0;
12958 }
12959
03dd20cc
DJ
12960 /* Mark the dependence relation so that we don't flush PER_CU
12961 too early. */
12962 dwarf2_add_dependence (this_cu, per_cu);
12963
12964 /* If it's already on the queue, we have nothing to do. */
12965 if (per_cu->queued)
348e048f 12966 return 0;
03dd20cc
DJ
12967
12968 /* If the compilation unit is already loaded, just mark it as
12969 used. */
12970 if (per_cu->cu != NULL)
12971 {
12972 per_cu->cu->last_used = 0;
348e048f 12973 return 0;
03dd20cc
DJ
12974 }
12975
12976 /* Add it to the queue. */
12977 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12978
12979 return 1;
12980}
12981
12982/* Follow reference or signature attribute ATTR of SRC_DIE.
12983 On entry *REF_CU is the CU of SRC_DIE.
12984 On exit *REF_CU is the CU of the result. */
12985
12986static struct die_info *
12987follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12988 struct dwarf2_cu **ref_cu)
12989{
12990 struct die_info *die;
12991
12992 if (is_ref_attr (attr))
12993 die = follow_die_ref (src_die, attr, ref_cu);
12994 else if (attr->form == DW_FORM_sig8)
12995 die = follow_die_sig (src_die, attr, ref_cu);
12996 else
12997 {
12998 dump_die_for_error (src_die);
12999 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13000 (*ref_cu)->objfile->name);
13001 }
13002
13003 return die;
03dd20cc
DJ
13004}
13005
5c631832 13006/* Follow reference OFFSET.
673bfd45
DE
13007 On entry *REF_CU is the CU of the source die referencing OFFSET.
13008 On exit *REF_CU is the CU of the result.
13009 Returns NULL if OFFSET is invalid. */
f504f079 13010
f9aca02d 13011static struct die_info *
5c631832 13012follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13013{
10b3939b 13014 struct die_info temp_die;
f2f0e013 13015 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13016
348e048f
DE
13017 gdb_assert (cu->per_cu != NULL);
13018
98bfdba5
PA
13019 target_cu = cu;
13020
348e048f
DE
13021 if (cu->per_cu->from_debug_types)
13022 {
13023 /* .debug_types CUs cannot reference anything outside their CU.
13024 If they need to, they have to reference a signatured type via
13025 DW_FORM_sig8. */
13026 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13027 return NULL;
348e048f
DE
13028 }
13029 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13030 {
13031 struct dwarf2_per_cu_data *per_cu;
9a619af0 13032
45452591 13033 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13034
13035 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13036 if (maybe_queue_comp_unit (cu, per_cu))
13037 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13038
10b3939b
DJ
13039 target_cu = per_cu->cu;
13040 }
98bfdba5
PA
13041 else if (cu->dies == NULL)
13042 {
13043 /* We're loading full DIEs during partial symbol reading. */
13044 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13045 load_full_comp_unit (cu->per_cu, cu->objfile);
13046 }
c906108c 13047
f2f0e013 13048 *ref_cu = target_cu;
51545339 13049 temp_die.offset = offset;
5c631832
JK
13050 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13051}
10b3939b 13052
5c631832
JK
13053/* Follow reference attribute ATTR of SRC_DIE.
13054 On entry *REF_CU is the CU of SRC_DIE.
13055 On exit *REF_CU is the CU of the result. */
13056
13057static struct die_info *
13058follow_die_ref (struct die_info *src_die, struct attribute *attr,
13059 struct dwarf2_cu **ref_cu)
13060{
13061 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13062 struct dwarf2_cu *cu = *ref_cu;
13063 struct die_info *die;
13064
13065 die = follow_die_offset (offset, ref_cu);
13066 if (!die)
13067 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13068 "at 0x%x [in module %s]"),
13069 offset, src_die->offset, cu->objfile->name);
348e048f 13070
5c631832
JK
13071 return die;
13072}
13073
13074/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13075 value is intended for DW_OP_call*. */
13076
13077struct dwarf2_locexpr_baton
13078dwarf2_fetch_die_location_block (unsigned int offset,
13079 struct dwarf2_per_cu_data *per_cu)
13080{
13081 struct dwarf2_cu *cu = per_cu->cu;
13082 struct die_info *die;
13083 struct attribute *attr;
13084 struct dwarf2_locexpr_baton retval;
13085
13086 die = follow_die_offset (offset, &cu);
13087 if (!die)
13088 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13089 offset, per_cu->cu->objfile->name);
13090
13091 attr = dwarf2_attr (die, DW_AT_location, cu);
13092 if (!attr)
13093 {
13094 /* DWARF: "If there is no such attribute, then there is no effect.". */
13095
13096 retval.data = NULL;
13097 retval.size = 0;
13098 }
13099 else
13100 {
13101 if (!attr_form_is_block (attr))
13102 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13103 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13104 offset, per_cu->cu->objfile->name);
13105
13106 retval.data = DW_BLOCK (attr)->data;
13107 retval.size = DW_BLOCK (attr)->size;
13108 }
13109 retval.per_cu = cu->per_cu;
13110 return retval;
348e048f
DE
13111}
13112
13113/* Follow the signature attribute ATTR in SRC_DIE.
13114 On entry *REF_CU is the CU of SRC_DIE.
13115 On exit *REF_CU is the CU of the result. */
13116
13117static struct die_info *
13118follow_die_sig (struct die_info *src_die, struct attribute *attr,
13119 struct dwarf2_cu **ref_cu)
13120{
13121 struct objfile *objfile = (*ref_cu)->objfile;
13122 struct die_info temp_die;
13123 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13124 struct dwarf2_cu *sig_cu;
13125 struct die_info *die;
13126
13127 /* sig_type will be NULL if the signatured type is missing from
13128 the debug info. */
13129 if (sig_type == NULL)
13130 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13131 "at 0x%x [in module %s]"),
13132 src_die->offset, objfile->name);
13133
13134 /* If necessary, add it to the queue and load its DIEs. */
13135
13136 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13137 read_signatured_type (objfile, sig_type);
13138
13139 gdb_assert (sig_type->per_cu.cu != NULL);
13140
13141 sig_cu = sig_type->per_cu.cu;
13142 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13143 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13144 if (die)
13145 {
13146 *ref_cu = sig_cu;
13147 return die;
13148 }
13149
13150 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13151 "at 0x%x [in module %s]"),
13152 sig_type->type_offset, src_die->offset, objfile->name);
13153}
13154
13155/* Given an offset of a signatured type, return its signatured_type. */
13156
13157static struct signatured_type *
13158lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13159{
13160 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13161 unsigned int length, initial_length_size;
13162 unsigned int sig_offset;
13163 struct signatured_type find_entry, *type_sig;
13164
13165 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13166 sig_offset = (initial_length_size
13167 + 2 /*version*/
13168 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13169 + 1 /*address_size*/);
13170 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13171 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13172
13173 /* This is only used to lookup previously recorded types.
13174 If we didn't find it, it's our bug. */
13175 gdb_assert (type_sig != NULL);
13176 gdb_assert (offset == type_sig->offset);
13177
13178 return type_sig;
13179}
13180
13181/* Read in signatured type at OFFSET and build its CU and die(s). */
13182
13183static void
13184read_signatured_type_at_offset (struct objfile *objfile,
13185 unsigned int offset)
13186{
13187 struct signatured_type *type_sig;
13188
be391dca
TT
13189 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13190
348e048f
DE
13191 /* We have the section offset, but we need the signature to do the
13192 hash table lookup. */
13193 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13194
13195 gdb_assert (type_sig->per_cu.cu == NULL);
13196
13197 read_signatured_type (objfile, type_sig);
13198
13199 gdb_assert (type_sig->per_cu.cu != NULL);
13200}
13201
13202/* Read in a signatured type and build its CU and DIEs. */
13203
13204static void
13205read_signatured_type (struct objfile *objfile,
13206 struct signatured_type *type_sig)
13207{
1fd400ff 13208 gdb_byte *types_ptr;
348e048f
DE
13209 struct die_reader_specs reader_specs;
13210 struct dwarf2_cu *cu;
13211 ULONGEST signature;
13212 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13213
1fd400ff
TT
13214 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13215 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13216
348e048f
DE
13217 gdb_assert (type_sig->per_cu.cu == NULL);
13218
9816fde3
JK
13219 cu = xmalloc (sizeof (*cu));
13220 init_one_comp_unit (cu, objfile);
13221
348e048f
DE
13222 type_sig->per_cu.cu = cu;
13223 cu->per_cu = &type_sig->per_cu;
13224
13225 /* If an error occurs while loading, release our storage. */
13226 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13227
13228 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13229 types_ptr, objfile->obfd);
13230 gdb_assert (signature == type_sig->signature);
13231
13232 cu->die_hash
13233 = htab_create_alloc_ex (cu->header.length / 12,
13234 die_hash,
13235 die_eq,
13236 NULL,
13237 &cu->comp_unit_obstack,
13238 hashtab_obstack_allocate,
13239 dummy_obstack_deallocate);
13240
13241 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13242 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13243
13244 init_cu_die_reader (&reader_specs, cu);
13245
13246 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13247 NULL /*parent*/);
13248
13249 /* We try not to read any attributes in this function, because not
13250 all objfiles needed for references have been loaded yet, and symbol
13251 table processing isn't initialized. But we have to set the CU language,
13252 or we won't be able to build types correctly. */
9816fde3 13253 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13254
13255 do_cleanups (back_to);
13256
13257 /* We've successfully allocated this compilation unit. Let our caller
13258 clean it up when finished with it. */
13259 discard_cleanups (free_cu_cleanup);
13260
13261 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13262 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13263}
13264
c906108c
SS
13265/* Decode simple location descriptions.
13266 Given a pointer to a dwarf block that defines a location, compute
13267 the location and return the value.
13268
4cecd739
DJ
13269 NOTE drow/2003-11-18: This function is called in two situations
13270 now: for the address of static or global variables (partial symbols
13271 only) and for offsets into structures which are expected to be
13272 (more or less) constant. The partial symbol case should go away,
13273 and only the constant case should remain. That will let this
13274 function complain more accurately. A few special modes are allowed
13275 without complaint for global variables (for instance, global
13276 register values and thread-local values).
c906108c
SS
13277
13278 A location description containing no operations indicates that the
4cecd739 13279 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13280 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13281 callers will only want a very basic result and this can become a
13282 complaint.
c906108c 13283
d53d4ac5 13284 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13285
13286static CORE_ADDR
e7c27a73 13287decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13288{
e7c27a73 13289 struct objfile *objfile = cu->objfile;
c906108c
SS
13290 int i;
13291 int size = blk->size;
fe1b8b76 13292 gdb_byte *data = blk->data;
c906108c
SS
13293 CORE_ADDR stack[64];
13294 int stacki;
13295 unsigned int bytes_read, unsnd;
fe1b8b76 13296 gdb_byte op;
c906108c
SS
13297
13298 i = 0;
13299 stacki = 0;
13300 stack[stacki] = 0;
d53d4ac5 13301 stack[++stacki] = 0;
c906108c
SS
13302
13303 while (i < size)
13304 {
c906108c
SS
13305 op = data[i++];
13306 switch (op)
13307 {
f1bea926
JM
13308 case DW_OP_lit0:
13309 case DW_OP_lit1:
13310 case DW_OP_lit2:
13311 case DW_OP_lit3:
13312 case DW_OP_lit4:
13313 case DW_OP_lit5:
13314 case DW_OP_lit6:
13315 case DW_OP_lit7:
13316 case DW_OP_lit8:
13317 case DW_OP_lit9:
13318 case DW_OP_lit10:
13319 case DW_OP_lit11:
13320 case DW_OP_lit12:
13321 case DW_OP_lit13:
13322 case DW_OP_lit14:
13323 case DW_OP_lit15:
13324 case DW_OP_lit16:
13325 case DW_OP_lit17:
13326 case DW_OP_lit18:
13327 case DW_OP_lit19:
13328 case DW_OP_lit20:
13329 case DW_OP_lit21:
13330 case DW_OP_lit22:
13331 case DW_OP_lit23:
13332 case DW_OP_lit24:
13333 case DW_OP_lit25:
13334 case DW_OP_lit26:
13335 case DW_OP_lit27:
13336 case DW_OP_lit28:
13337 case DW_OP_lit29:
13338 case DW_OP_lit30:
13339 case DW_OP_lit31:
13340 stack[++stacki] = op - DW_OP_lit0;
13341 break;
13342
c906108c
SS
13343 case DW_OP_reg0:
13344 case DW_OP_reg1:
13345 case DW_OP_reg2:
13346 case DW_OP_reg3:
13347 case DW_OP_reg4:
13348 case DW_OP_reg5:
13349 case DW_OP_reg6:
13350 case DW_OP_reg7:
13351 case DW_OP_reg8:
13352 case DW_OP_reg9:
13353 case DW_OP_reg10:
13354 case DW_OP_reg11:
13355 case DW_OP_reg12:
13356 case DW_OP_reg13:
13357 case DW_OP_reg14:
13358 case DW_OP_reg15:
13359 case DW_OP_reg16:
13360 case DW_OP_reg17:
13361 case DW_OP_reg18:
13362 case DW_OP_reg19:
13363 case DW_OP_reg20:
13364 case DW_OP_reg21:
13365 case DW_OP_reg22:
13366 case DW_OP_reg23:
13367 case DW_OP_reg24:
13368 case DW_OP_reg25:
13369 case DW_OP_reg26:
13370 case DW_OP_reg27:
13371 case DW_OP_reg28:
13372 case DW_OP_reg29:
13373 case DW_OP_reg30:
13374 case DW_OP_reg31:
c906108c 13375 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13376 if (i < size)
13377 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13378 break;
13379
13380 case DW_OP_regx:
c906108c
SS
13381 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13382 i += bytes_read;
c906108c 13383 stack[++stacki] = unsnd;
4cecd739
DJ
13384 if (i < size)
13385 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13386 break;
13387
13388 case DW_OP_addr:
107d2387 13389 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13390 cu, &bytes_read);
107d2387 13391 i += bytes_read;
c906108c
SS
13392 break;
13393
13394 case DW_OP_const1u:
13395 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13396 i += 1;
13397 break;
13398
13399 case DW_OP_const1s:
13400 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13401 i += 1;
13402 break;
13403
13404 case DW_OP_const2u:
13405 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13406 i += 2;
13407 break;
13408
13409 case DW_OP_const2s:
13410 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13411 i += 2;
13412 break;
13413
13414 case DW_OP_const4u:
13415 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13416 i += 4;
13417 break;
13418
13419 case DW_OP_const4s:
13420 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13421 i += 4;
13422 break;
13423
13424 case DW_OP_constu:
13425 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13426 &bytes_read);
c906108c
SS
13427 i += bytes_read;
13428 break;
13429
13430 case DW_OP_consts:
13431 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13432 i += bytes_read;
13433 break;
13434
f1bea926
JM
13435 case DW_OP_dup:
13436 stack[stacki + 1] = stack[stacki];
13437 stacki++;
13438 break;
13439
c906108c
SS
13440 case DW_OP_plus:
13441 stack[stacki - 1] += stack[stacki];
13442 stacki--;
13443 break;
13444
13445 case DW_OP_plus_uconst:
13446 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13447 i += bytes_read;
13448 break;
13449
13450 case DW_OP_minus:
f1bea926 13451 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13452 stacki--;
13453 break;
13454
7a292a7a 13455 case DW_OP_deref:
7a292a7a 13456 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13457 this using GDB's address_class enum. This is valid for partial
13458 global symbols, although the variable's address will be bogus
13459 in the psymtab. */
7a292a7a 13460 if (i < size)
4d3c2250 13461 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13462 break;
13463
9d774e44 13464 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13465 /* The top of the stack has the offset from the beginning
13466 of the thread control block at which the variable is located. */
13467 /* Nothing should follow this operator, so the top of stack would
13468 be returned. */
4cecd739
DJ
13469 /* This is valid for partial global symbols, but the variable's
13470 address will be bogus in the psymtab. */
9d774e44 13471 if (i < size)
4d3c2250 13472 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13473 break;
13474
42be36b3
CT
13475 case DW_OP_GNU_uninit:
13476 break;
13477
c906108c 13478 default:
e2e0b3e5 13479 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13480 dwarf_stack_op_name (op, 1));
c906108c
SS
13481 return (stack[stacki]);
13482 }
d53d4ac5
TT
13483
13484 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13485 outside of the allocated space. Also enforce minimum>0. */
13486 if (stacki >= ARRAY_SIZE (stack) - 1)
13487 {
13488 complaint (&symfile_complaints,
13489 _("location description stack overflow"));
13490 return 0;
13491 }
13492
13493 if (stacki <= 0)
13494 {
13495 complaint (&symfile_complaints,
13496 _("location description stack underflow"));
13497 return 0;
13498 }
c906108c
SS
13499 }
13500 return (stack[stacki]);
13501}
13502
13503/* memory allocation interface */
13504
c906108c 13505static struct dwarf_block *
7b5a2f43 13506dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13507{
13508 struct dwarf_block *blk;
13509
13510 blk = (struct dwarf_block *)
7b5a2f43 13511 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13512 return (blk);
13513}
13514
13515static struct abbrev_info *
f3dd6933 13516dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13517{
13518 struct abbrev_info *abbrev;
13519
f3dd6933
DJ
13520 abbrev = (struct abbrev_info *)
13521 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13522 memset (abbrev, 0, sizeof (struct abbrev_info));
13523 return (abbrev);
13524}
13525
13526static struct die_info *
b60c80d6 13527dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13528{
13529 struct die_info *die;
b60c80d6
DJ
13530 size_t size = sizeof (struct die_info);
13531
13532 if (num_attrs > 1)
13533 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13534
b60c80d6 13535 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13536 memset (die, 0, sizeof (struct die_info));
13537 return (die);
13538}
2e276125
JB
13539
13540\f
13541/* Macro support. */
13542
13543
13544/* Return the full name of file number I in *LH's file name table.
13545 Use COMP_DIR as the name of the current directory of the
13546 compilation. The result is allocated using xmalloc; the caller is
13547 responsible for freeing it. */
13548static char *
13549file_full_name (int file, struct line_header *lh, const char *comp_dir)
13550{
6a83a1e6
EZ
13551 /* Is the file number a valid index into the line header's file name
13552 table? Remember that file numbers start with one, not zero. */
13553 if (1 <= file && file <= lh->num_file_names)
13554 {
13555 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13556
6a83a1e6
EZ
13557 if (IS_ABSOLUTE_PATH (fe->name))
13558 return xstrdup (fe->name);
13559 else
13560 {
13561 const char *dir;
13562 int dir_len;
13563 char *full_name;
13564
13565 if (fe->dir_index)
13566 dir = lh->include_dirs[fe->dir_index - 1];
13567 else
13568 dir = comp_dir;
13569
13570 if (dir)
13571 {
13572 dir_len = strlen (dir);
13573 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13574 strcpy (full_name, dir);
13575 full_name[dir_len] = '/';
13576 strcpy (full_name + dir_len + 1, fe->name);
13577 return full_name;
13578 }
13579 else
13580 return xstrdup (fe->name);
13581 }
13582 }
2e276125
JB
13583 else
13584 {
6a83a1e6
EZ
13585 /* The compiler produced a bogus file number. We can at least
13586 record the macro definitions made in the file, even if we
13587 won't be able to find the file by name. */
13588 char fake_name[80];
9a619af0 13589
6a83a1e6 13590 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13591
6e70227d 13592 complaint (&symfile_complaints,
6a83a1e6
EZ
13593 _("bad file number in macro information (%d)"),
13594 file);
2e276125 13595
6a83a1e6 13596 return xstrdup (fake_name);
2e276125
JB
13597 }
13598}
13599
13600
13601static struct macro_source_file *
13602macro_start_file (int file, int line,
13603 struct macro_source_file *current_file,
13604 const char *comp_dir,
13605 struct line_header *lh, struct objfile *objfile)
13606{
13607 /* The full name of this source file. */
13608 char *full_name = file_full_name (file, lh, comp_dir);
13609
13610 /* We don't create a macro table for this compilation unit
13611 at all until we actually get a filename. */
13612 if (! pending_macros)
4a146b47 13613 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13614 objfile->macro_cache);
2e276125
JB
13615
13616 if (! current_file)
13617 /* If we have no current file, then this must be the start_file
13618 directive for the compilation unit's main source file. */
13619 current_file = macro_set_main (pending_macros, full_name);
13620 else
13621 current_file = macro_include (current_file, line, full_name);
13622
13623 xfree (full_name);
6e70227d 13624
2e276125
JB
13625 return current_file;
13626}
13627
13628
13629/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13630 followed by a null byte. */
13631static char *
13632copy_string (const char *buf, int len)
13633{
13634 char *s = xmalloc (len + 1);
9a619af0 13635
2e276125
JB
13636 memcpy (s, buf, len);
13637 s[len] = '\0';
2e276125
JB
13638 return s;
13639}
13640
13641
13642static const char *
13643consume_improper_spaces (const char *p, const char *body)
13644{
13645 if (*p == ' ')
13646 {
4d3c2250 13647 complaint (&symfile_complaints,
e2e0b3e5 13648 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13649 body);
2e276125
JB
13650
13651 while (*p == ' ')
13652 p++;
13653 }
13654
13655 return p;
13656}
13657
13658
13659static void
13660parse_macro_definition (struct macro_source_file *file, int line,
13661 const char *body)
13662{
13663 const char *p;
13664
13665 /* The body string takes one of two forms. For object-like macro
13666 definitions, it should be:
13667
13668 <macro name> " " <definition>
13669
13670 For function-like macro definitions, it should be:
13671
13672 <macro name> "() " <definition>
13673 or
13674 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13675
13676 Spaces may appear only where explicitly indicated, and in the
13677 <definition>.
13678
13679 The Dwarf 2 spec says that an object-like macro's name is always
13680 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13681 the space when the macro's definition is the empty string.
2e276125
JB
13682
13683 The Dwarf 2 spec says that there should be no spaces between the
13684 formal arguments in a function-like macro's formal argument list,
13685 but versions of GCC around March 2002 include spaces after the
13686 commas. */
13687
13688
13689 /* Find the extent of the macro name. The macro name is terminated
13690 by either a space or null character (for an object-like macro) or
13691 an opening paren (for a function-like macro). */
13692 for (p = body; *p; p++)
13693 if (*p == ' ' || *p == '(')
13694 break;
13695
13696 if (*p == ' ' || *p == '\0')
13697 {
13698 /* It's an object-like macro. */
13699 int name_len = p - body;
13700 char *name = copy_string (body, name_len);
13701 const char *replacement;
13702
13703 if (*p == ' ')
13704 replacement = body + name_len + 1;
13705 else
13706 {
4d3c2250 13707 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13708 replacement = body + name_len;
13709 }
6e70227d 13710
2e276125
JB
13711 macro_define_object (file, line, name, replacement);
13712
13713 xfree (name);
13714 }
13715 else if (*p == '(')
13716 {
13717 /* It's a function-like macro. */
13718 char *name = copy_string (body, p - body);
13719 int argc = 0;
13720 int argv_size = 1;
13721 char **argv = xmalloc (argv_size * sizeof (*argv));
13722
13723 p++;
13724
13725 p = consume_improper_spaces (p, body);
13726
13727 /* Parse the formal argument list. */
13728 while (*p && *p != ')')
13729 {
13730 /* Find the extent of the current argument name. */
13731 const char *arg_start = p;
13732
13733 while (*p && *p != ',' && *p != ')' && *p != ' ')
13734 p++;
13735
13736 if (! *p || p == arg_start)
4d3c2250 13737 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13738 else
13739 {
13740 /* Make sure argv has room for the new argument. */
13741 if (argc >= argv_size)
13742 {
13743 argv_size *= 2;
13744 argv = xrealloc (argv, argv_size * sizeof (*argv));
13745 }
13746
13747 argv[argc++] = copy_string (arg_start, p - arg_start);
13748 }
13749
13750 p = consume_improper_spaces (p, body);
13751
13752 /* Consume the comma, if present. */
13753 if (*p == ',')
13754 {
13755 p++;
13756
13757 p = consume_improper_spaces (p, body);
13758 }
13759 }
13760
13761 if (*p == ')')
13762 {
13763 p++;
13764
13765 if (*p == ' ')
13766 /* Perfectly formed definition, no complaints. */
13767 macro_define_function (file, line, name,
6e70227d 13768 argc, (const char **) argv,
2e276125
JB
13769 p + 1);
13770 else if (*p == '\0')
13771 {
13772 /* Complain, but do define it. */
4d3c2250 13773 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13774 macro_define_function (file, line, name,
6e70227d 13775 argc, (const char **) argv,
2e276125
JB
13776 p);
13777 }
13778 else
13779 /* Just complain. */
4d3c2250 13780 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13781 }
13782 else
13783 /* Just complain. */
4d3c2250 13784 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13785
13786 xfree (name);
13787 {
13788 int i;
13789
13790 for (i = 0; i < argc; i++)
13791 xfree (argv[i]);
13792 }
13793 xfree (argv);
13794 }
13795 else
4d3c2250 13796 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13797}
13798
13799
13800static void
13801dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13802 char *comp_dir, bfd *abfd,
e7c27a73 13803 struct dwarf2_cu *cu)
2e276125 13804{
fe1b8b76 13805 gdb_byte *mac_ptr, *mac_end;
2e276125 13806 struct macro_source_file *current_file = 0;
757a13d0
JK
13807 enum dwarf_macinfo_record_type macinfo_type;
13808 int at_commandline;
2e276125 13809
be391dca
TT
13810 dwarf2_read_section (dwarf2_per_objfile->objfile,
13811 &dwarf2_per_objfile->macinfo);
dce234bc 13812 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13813 {
e2e0b3e5 13814 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13815 return;
13816 }
13817
757a13d0
JK
13818 /* First pass: Find the name of the base filename.
13819 This filename is needed in order to process all macros whose definition
13820 (or undefinition) comes from the command line. These macros are defined
13821 before the first DW_MACINFO_start_file entry, and yet still need to be
13822 associated to the base file.
13823
13824 To determine the base file name, we scan the macro definitions until we
13825 reach the first DW_MACINFO_start_file entry. We then initialize
13826 CURRENT_FILE accordingly so that any macro definition found before the
13827 first DW_MACINFO_start_file can still be associated to the base file. */
13828
dce234bc
PP
13829 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13830 mac_end = dwarf2_per_objfile->macinfo.buffer
13831 + dwarf2_per_objfile->macinfo.size;
2e276125 13832
757a13d0 13833 do
2e276125 13834 {
2e276125
JB
13835 /* Do we at least have room for a macinfo type byte? */
13836 if (mac_ptr >= mac_end)
13837 {
757a13d0
JK
13838 /* Complaint is printed during the second pass as GDB will probably
13839 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13840 break;
2e276125
JB
13841 }
13842
13843 macinfo_type = read_1_byte (abfd, mac_ptr);
13844 mac_ptr++;
13845
13846 switch (macinfo_type)
13847 {
13848 /* A zero macinfo type indicates the end of the macro
13849 information. */
13850 case 0:
757a13d0
JK
13851 break;
13852
13853 case DW_MACINFO_define:
13854 case DW_MACINFO_undef:
13855 /* Only skip the data by MAC_PTR. */
13856 {
13857 unsigned int bytes_read;
13858
13859 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13860 mac_ptr += bytes_read;
9b1c24c8 13861 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13862 mac_ptr += bytes_read;
13863 }
13864 break;
13865
13866 case DW_MACINFO_start_file:
13867 {
13868 unsigned int bytes_read;
13869 int line, file;
13870
13871 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13872 mac_ptr += bytes_read;
13873 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13874 mac_ptr += bytes_read;
13875
13876 current_file = macro_start_file (file, line, current_file, comp_dir,
13877 lh, cu->objfile);
13878 }
13879 break;
13880
13881 case DW_MACINFO_end_file:
13882 /* No data to skip by MAC_PTR. */
13883 break;
13884
13885 case DW_MACINFO_vendor_ext:
13886 /* Only skip the data by MAC_PTR. */
13887 {
13888 unsigned int bytes_read;
13889
13890 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13891 mac_ptr += bytes_read;
9b1c24c8 13892 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13893 mac_ptr += bytes_read;
13894 }
13895 break;
13896
13897 default:
13898 break;
13899 }
13900 } while (macinfo_type != 0 && current_file == NULL);
13901
13902 /* Second pass: Process all entries.
13903
13904 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13905 command-line macro definitions/undefinitions. This flag is unset when we
13906 reach the first DW_MACINFO_start_file entry. */
13907
dce234bc 13908 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13909
13910 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13911 GDB is still reading the definitions from command line. First
13912 DW_MACINFO_start_file will need to be ignored as it was already executed
13913 to create CURRENT_FILE for the main source holding also the command line
13914 definitions. On first met DW_MACINFO_start_file this flag is reset to
13915 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13916
13917 at_commandline = 1;
13918
13919 do
13920 {
13921 /* Do we at least have room for a macinfo type byte? */
13922 if (mac_ptr >= mac_end)
13923 {
13924 dwarf2_macros_too_long_complaint ();
13925 break;
13926 }
13927
13928 macinfo_type = read_1_byte (abfd, mac_ptr);
13929 mac_ptr++;
13930
13931 switch (macinfo_type)
13932 {
13933 /* A zero macinfo type indicates the end of the macro
13934 information. */
13935 case 0:
13936 break;
2e276125
JB
13937
13938 case DW_MACINFO_define:
13939 case DW_MACINFO_undef:
13940 {
891d2f0b 13941 unsigned int bytes_read;
2e276125
JB
13942 int line;
13943 char *body;
13944
13945 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13946 mac_ptr += bytes_read;
9b1c24c8 13947 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13948 mac_ptr += bytes_read;
13949
13950 if (! current_file)
757a13d0
JK
13951 {
13952 /* DWARF violation as no main source is present. */
13953 complaint (&symfile_complaints,
13954 _("debug info with no main source gives macro %s "
13955 "on line %d: %s"),
6e70227d
DE
13956 macinfo_type == DW_MACINFO_define ?
13957 _("definition") :
905e0470
PM
13958 macinfo_type == DW_MACINFO_undef ?
13959 _("undefinition") :
13960 _("something-or-other"), line, body);
757a13d0
JK
13961 break;
13962 }
13963 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13964 complaint (&symfile_complaints,
757a13d0
JK
13965 _("debug info gives %s macro %s with %s line %d: %s"),
13966 at_commandline ? _("command-line") : _("in-file"),
905e0470 13967 macinfo_type == DW_MACINFO_define ?
6e70227d 13968 _("definition") :
905e0470
PM
13969 macinfo_type == DW_MACINFO_undef ?
13970 _("undefinition") :
13971 _("something-or-other"),
757a13d0
JK
13972 line == 0 ? _("zero") : _("non-zero"), line, body);
13973
13974 if (macinfo_type == DW_MACINFO_define)
13975 parse_macro_definition (current_file, line, body);
13976 else if (macinfo_type == DW_MACINFO_undef)
13977 macro_undef (current_file, line, body);
2e276125
JB
13978 }
13979 break;
13980
13981 case DW_MACINFO_start_file:
13982 {
891d2f0b 13983 unsigned int bytes_read;
2e276125
JB
13984 int line, file;
13985
13986 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13987 mac_ptr += bytes_read;
13988 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13989 mac_ptr += bytes_read;
13990
757a13d0
JK
13991 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13992 complaint (&symfile_complaints,
13993 _("debug info gives source %d included "
13994 "from %s at %s line %d"),
13995 file, at_commandline ? _("command-line") : _("file"),
13996 line == 0 ? _("zero") : _("non-zero"), line);
13997
13998 if (at_commandline)
13999 {
14000 /* This DW_MACINFO_start_file was executed in the pass one. */
14001 at_commandline = 0;
14002 }
14003 else
14004 current_file = macro_start_file (file, line,
14005 current_file, comp_dir,
14006 lh, cu->objfile);
2e276125
JB
14007 }
14008 break;
14009
14010 case DW_MACINFO_end_file:
14011 if (! current_file)
4d3c2250 14012 complaint (&symfile_complaints,
e2e0b3e5 14013 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
14014 else
14015 {
14016 current_file = current_file->included_by;
14017 if (! current_file)
14018 {
14019 enum dwarf_macinfo_record_type next_type;
14020
14021 /* GCC circa March 2002 doesn't produce the zero
14022 type byte marking the end of the compilation
14023 unit. Complain if it's not there, but exit no
14024 matter what. */
14025
14026 /* Do we at least have room for a macinfo type byte? */
14027 if (mac_ptr >= mac_end)
14028 {
4d3c2250 14029 dwarf2_macros_too_long_complaint ();
2e276125
JB
14030 return;
14031 }
14032
14033 /* We don't increment mac_ptr here, so this is just
14034 a look-ahead. */
14035 next_type = read_1_byte (abfd, mac_ptr);
14036 if (next_type != 0)
4d3c2250 14037 complaint (&symfile_complaints,
e2e0b3e5 14038 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
14039
14040 return;
14041 }
14042 }
14043 break;
14044
14045 case DW_MACINFO_vendor_ext:
14046 {
891d2f0b 14047 unsigned int bytes_read;
2e276125
JB
14048 int constant;
14049 char *string;
14050
14051 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14052 mac_ptr += bytes_read;
9b1c24c8 14053 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14054 mac_ptr += bytes_read;
14055
14056 /* We don't recognize any vendor extensions. */
14057 }
14058 break;
14059 }
757a13d0 14060 } while (macinfo_type != 0);
2e276125 14061}
8e19ed76
PS
14062
14063/* Check if the attribute's form is a DW_FORM_block*
14064 if so return true else false. */
14065static int
14066attr_form_is_block (struct attribute *attr)
14067{
14068 return (attr == NULL ? 0 :
14069 attr->form == DW_FORM_block1
14070 || attr->form == DW_FORM_block2
14071 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14072 || attr->form == DW_FORM_block
14073 || attr->form == DW_FORM_exprloc);
8e19ed76 14074}
4c2df51b 14075
c6a0999f
JB
14076/* Return non-zero if ATTR's value is a section offset --- classes
14077 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14078 You may use DW_UNSND (attr) to retrieve such offsets.
14079
14080 Section 7.5.4, "Attribute Encodings", explains that no attribute
14081 may have a value that belongs to more than one of these classes; it
14082 would be ambiguous if we did, because we use the same forms for all
14083 of them. */
3690dd37
JB
14084static int
14085attr_form_is_section_offset (struct attribute *attr)
14086{
14087 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14088 || attr->form == DW_FORM_data8
14089 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14090}
14091
14092
14093/* Return non-zero if ATTR's value falls in the 'constant' class, or
14094 zero otherwise. When this function returns true, you can apply
14095 dwarf2_get_attr_constant_value to it.
14096
14097 However, note that for some attributes you must check
14098 attr_form_is_section_offset before using this test. DW_FORM_data4
14099 and DW_FORM_data8 are members of both the constant class, and of
14100 the classes that contain offsets into other debug sections
14101 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14102 that, if an attribute's can be either a constant or one of the
14103 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14104 taken as section offsets, not constants. */
14105static int
14106attr_form_is_constant (struct attribute *attr)
14107{
14108 switch (attr->form)
14109 {
14110 case DW_FORM_sdata:
14111 case DW_FORM_udata:
14112 case DW_FORM_data1:
14113 case DW_FORM_data2:
14114 case DW_FORM_data4:
14115 case DW_FORM_data8:
14116 return 1;
14117 default:
14118 return 0;
14119 }
14120}
14121
4c2df51b
DJ
14122static void
14123dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14124 struct dwarf2_cu *cu)
4c2df51b 14125{
3690dd37 14126 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14127 /* ".debug_loc" may not exist at all, or the offset may be outside
14128 the section. If so, fall through to the complaint in the
14129 other branch. */
dce234bc 14130 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14131 {
0d53c4c4 14132 struct dwarf2_loclist_baton *baton;
4c2df51b 14133
4a146b47 14134 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14135 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
14136 baton->per_cu = cu->per_cu;
14137 gdb_assert (baton->per_cu);
4c2df51b 14138
be391dca
TT
14139 dwarf2_read_section (dwarf2_per_objfile->objfile,
14140 &dwarf2_per_objfile->loc);
14141
0d53c4c4
DJ
14142 /* We don't know how long the location list is, but make sure we
14143 don't run off the edge of the section. */
dce234bc
PP
14144 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14145 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
14146 baton->base_address = cu->base_address;
14147 if (cu->base_known == 0)
0d53c4c4 14148 complaint (&symfile_complaints,
e2e0b3e5 14149 _("Location list used without specifying the CU base address."));
4c2df51b 14150
768a979c 14151 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14152 SYMBOL_LOCATION_BATON (sym) = baton;
14153 }
14154 else
14155 {
14156 struct dwarf2_locexpr_baton *baton;
14157
4a146b47 14158 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14159 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14160 baton->per_cu = cu->per_cu;
14161 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14162
14163 if (attr_form_is_block (attr))
14164 {
14165 /* Note that we're just copying the block's data pointer
14166 here, not the actual data. We're still pointing into the
6502dd73
DJ
14167 info_buffer for SYM's objfile; right now we never release
14168 that buffer, but when we do clean up properly this may
14169 need to change. */
0d53c4c4
DJ
14170 baton->size = DW_BLOCK (attr)->size;
14171 baton->data = DW_BLOCK (attr)->data;
14172 }
14173 else
14174 {
14175 dwarf2_invalid_attrib_class_complaint ("location description",
14176 SYMBOL_NATURAL_NAME (sym));
14177 baton->size = 0;
14178 baton->data = NULL;
14179 }
6e70227d 14180
768a979c 14181 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14182 SYMBOL_LOCATION_BATON (sym) = baton;
14183 }
4c2df51b 14184}
6502dd73 14185
9aa1f1e3
TT
14186/* Return the OBJFILE associated with the compilation unit CU. If CU
14187 came from a separate debuginfo file, then the master objfile is
14188 returned. */
ae0d2f24
UW
14189
14190struct objfile *
14191dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14192{
9291a0cd 14193 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14194
14195 /* Return the master objfile, so that we can report and look up the
14196 correct file containing this variable. */
14197 if (objfile->separate_debug_objfile_backlink)
14198 objfile = objfile->separate_debug_objfile_backlink;
14199
14200 return objfile;
14201}
14202
14203/* Return the address size given in the compilation unit header for CU. */
14204
14205CORE_ADDR
14206dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14207{
14208 if (per_cu->cu)
14209 return per_cu->cu->header.addr_size;
14210 else
14211 {
14212 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14213 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14214 struct dwarf2_per_objfile *per_objfile
14215 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14216 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14217 struct comp_unit_head cu_header;
9a619af0 14218
ae0d2f24
UW
14219 memset (&cu_header, 0, sizeof cu_header);
14220 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14221 return cu_header.addr_size;
14222 }
14223}
14224
9eae7c52
TT
14225/* Return the offset size given in the compilation unit header for CU. */
14226
14227int
14228dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14229{
14230 if (per_cu->cu)
14231 return per_cu->cu->header.offset_size;
14232 else
14233 {
14234 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14235 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14236 struct dwarf2_per_objfile *per_objfile
14237 = objfile_data (objfile, dwarf2_objfile_data_key);
14238 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14239 struct comp_unit_head cu_header;
14240
14241 memset (&cu_header, 0, sizeof cu_header);
14242 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14243 return cu_header.offset_size;
14244 }
14245}
14246
9aa1f1e3
TT
14247/* Return the text offset of the CU. The returned offset comes from
14248 this CU's objfile. If this objfile came from a separate debuginfo
14249 file, then the offset may be different from the corresponding
14250 offset in the parent objfile. */
14251
14252CORE_ADDR
14253dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14254{
bb3fa9d0 14255 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14256
14257 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14258}
14259
348e048f
DE
14260/* Locate the .debug_info compilation unit from CU's objfile which contains
14261 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14262
14263static struct dwarf2_per_cu_data *
c764a876 14264dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14265 struct objfile *objfile)
14266{
14267 struct dwarf2_per_cu_data *this_cu;
14268 int low, high;
14269
ae038cb0
DJ
14270 low = 0;
14271 high = dwarf2_per_objfile->n_comp_units - 1;
14272 while (high > low)
14273 {
14274 int mid = low + (high - low) / 2;
9a619af0 14275
ae038cb0
DJ
14276 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14277 high = mid;
14278 else
14279 low = mid + 1;
14280 }
14281 gdb_assert (low == high);
14282 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14283 {
10b3939b 14284 if (low == 0)
8a3fe4f8
AC
14285 error (_("Dwarf Error: could not find partial DIE containing "
14286 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14287 (long) offset, bfd_get_filename (objfile->obfd));
14288
ae038cb0
DJ
14289 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14290 return dwarf2_per_objfile->all_comp_units[low-1];
14291 }
14292 else
14293 {
14294 this_cu = dwarf2_per_objfile->all_comp_units[low];
14295 if (low == dwarf2_per_objfile->n_comp_units - 1
14296 && offset >= this_cu->offset + this_cu->length)
c764a876 14297 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14298 gdb_assert (offset < this_cu->offset + this_cu->length);
14299 return this_cu;
14300 }
14301}
14302
10b3939b
DJ
14303/* Locate the compilation unit from OBJFILE which is located at exactly
14304 OFFSET. Raises an error on failure. */
14305
ae038cb0 14306static struct dwarf2_per_cu_data *
c764a876 14307dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14308{
14309 struct dwarf2_per_cu_data *this_cu;
9a619af0 14310
ae038cb0
DJ
14311 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14312 if (this_cu->offset != offset)
c764a876 14313 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14314 return this_cu;
14315}
14316
9816fde3 14317/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14318
9816fde3
JK
14319static void
14320init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14321{
9816fde3 14322 memset (cu, 0, sizeof (*cu));
93311388
DE
14323 cu->objfile = objfile;
14324 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14325}
14326
14327/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14328
14329static void
14330prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14331{
14332 struct attribute *attr;
14333
14334 /* Set the language we're debugging. */
14335 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14336 if (attr)
14337 set_cu_language (DW_UNSND (attr), cu);
14338 else
14339 set_cu_language (language_minimal, cu);
93311388
DE
14340}
14341
ae038cb0
DJ
14342/* Release one cached compilation unit, CU. We unlink it from the tree
14343 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14344 the caller is responsible for that.
14345 NOTE: DATA is a void * because this function is also used as a
14346 cleanup routine. */
ae038cb0
DJ
14347
14348static void
14349free_one_comp_unit (void *data)
14350{
14351 struct dwarf2_cu *cu = data;
14352
14353 if (cu->per_cu != NULL)
14354 cu->per_cu->cu = NULL;
14355 cu->per_cu = NULL;
14356
14357 obstack_free (&cu->comp_unit_obstack, NULL);
14358
14359 xfree (cu);
14360}
14361
72bf9492 14362/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14363 when we're finished with it. We can't free the pointer itself, but be
14364 sure to unlink it from the cache. Also release any associated storage
14365 and perform cache maintenance.
72bf9492
DJ
14366
14367 Only used during partial symbol parsing. */
14368
14369static void
14370free_stack_comp_unit (void *data)
14371{
14372 struct dwarf2_cu *cu = data;
14373
14374 obstack_free (&cu->comp_unit_obstack, NULL);
14375 cu->partial_dies = NULL;
ae038cb0
DJ
14376
14377 if (cu->per_cu != NULL)
14378 {
14379 /* This compilation unit is on the stack in our caller, so we
14380 should not xfree it. Just unlink it. */
14381 cu->per_cu->cu = NULL;
14382 cu->per_cu = NULL;
14383
14384 /* If we had a per-cu pointer, then we may have other compilation
14385 units loaded, so age them now. */
14386 age_cached_comp_units ();
14387 }
14388}
14389
14390/* Free all cached compilation units. */
14391
14392static void
14393free_cached_comp_units (void *data)
14394{
14395 struct dwarf2_per_cu_data *per_cu, **last_chain;
14396
14397 per_cu = dwarf2_per_objfile->read_in_chain;
14398 last_chain = &dwarf2_per_objfile->read_in_chain;
14399 while (per_cu != NULL)
14400 {
14401 struct dwarf2_per_cu_data *next_cu;
14402
14403 next_cu = per_cu->cu->read_in_chain;
14404
14405 free_one_comp_unit (per_cu->cu);
14406 *last_chain = next_cu;
14407
14408 per_cu = next_cu;
14409 }
14410}
14411
14412/* Increase the age counter on each cached compilation unit, and free
14413 any that are too old. */
14414
14415static void
14416age_cached_comp_units (void)
14417{
14418 struct dwarf2_per_cu_data *per_cu, **last_chain;
14419
14420 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14421 per_cu = dwarf2_per_objfile->read_in_chain;
14422 while (per_cu != NULL)
14423 {
14424 per_cu->cu->last_used ++;
14425 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14426 dwarf2_mark (per_cu->cu);
14427 per_cu = per_cu->cu->read_in_chain;
14428 }
14429
14430 per_cu = dwarf2_per_objfile->read_in_chain;
14431 last_chain = &dwarf2_per_objfile->read_in_chain;
14432 while (per_cu != NULL)
14433 {
14434 struct dwarf2_per_cu_data *next_cu;
14435
14436 next_cu = per_cu->cu->read_in_chain;
14437
14438 if (!per_cu->cu->mark)
14439 {
14440 free_one_comp_unit (per_cu->cu);
14441 *last_chain = next_cu;
14442 }
14443 else
14444 last_chain = &per_cu->cu->read_in_chain;
14445
14446 per_cu = next_cu;
14447 }
14448}
14449
14450/* Remove a single compilation unit from the cache. */
14451
14452static void
14453free_one_cached_comp_unit (void *target_cu)
14454{
14455 struct dwarf2_per_cu_data *per_cu, **last_chain;
14456
14457 per_cu = dwarf2_per_objfile->read_in_chain;
14458 last_chain = &dwarf2_per_objfile->read_in_chain;
14459 while (per_cu != NULL)
14460 {
14461 struct dwarf2_per_cu_data *next_cu;
14462
14463 next_cu = per_cu->cu->read_in_chain;
14464
14465 if (per_cu->cu == target_cu)
14466 {
14467 free_one_comp_unit (per_cu->cu);
14468 *last_chain = next_cu;
14469 break;
14470 }
14471 else
14472 last_chain = &per_cu->cu->read_in_chain;
14473
14474 per_cu = next_cu;
14475 }
14476}
14477
fe3e1990
DJ
14478/* Release all extra memory associated with OBJFILE. */
14479
14480void
14481dwarf2_free_objfile (struct objfile *objfile)
14482{
14483 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14484
14485 if (dwarf2_per_objfile == NULL)
14486 return;
14487
14488 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14489 free_cached_comp_units (NULL);
14490
9291a0cd
TT
14491 if (dwarf2_per_objfile->using_index)
14492 {
14493 int i;
14494
14495 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14496 {
14497 int j;
e254ef6a
DE
14498 struct dwarf2_per_cu_data *per_cu =
14499 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14500
e254ef6a 14501 if (!per_cu->v.quick->lines)
9291a0cd
TT
14502 continue;
14503
e254ef6a 14504 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14505 {
e254ef6a
DE
14506 if (per_cu->v.quick->file_names)
14507 xfree ((void *) per_cu->v.quick->file_names[j]);
14508 if (per_cu->v.quick->full_names)
14509 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14510 }
14511
e254ef6a 14512 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14513 }
14514 }
14515
fe3e1990
DJ
14516 /* Everything else should be on the objfile obstack. */
14517}
14518
1c379e20
DJ
14519/* A pair of DIE offset and GDB type pointer. We store these
14520 in a hash table separate from the DIEs, and preserve them
14521 when the DIEs are flushed out of cache. */
14522
14523struct dwarf2_offset_and_type
14524{
14525 unsigned int offset;
14526 struct type *type;
14527};
14528
14529/* Hash function for a dwarf2_offset_and_type. */
14530
14531static hashval_t
14532offset_and_type_hash (const void *item)
14533{
14534 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14535
1c379e20
DJ
14536 return ofs->offset;
14537}
14538
14539/* Equality function for a dwarf2_offset_and_type. */
14540
14541static int
14542offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14543{
14544 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14545 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14546
1c379e20
DJ
14547 return ofs_lhs->offset == ofs_rhs->offset;
14548}
14549
14550/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14551 table if necessary. For convenience, return TYPE.
14552
14553 The DIEs reading must have careful ordering to:
14554 * Not cause infite loops trying to read in DIEs as a prerequisite for
14555 reading current DIE.
14556 * Not trying to dereference contents of still incompletely read in types
14557 while reading in other DIEs.
14558 * Enable referencing still incompletely read in types just by a pointer to
14559 the type without accessing its fields.
14560
14561 Therefore caller should follow these rules:
14562 * Try to fetch any prerequisite types we may need to build this DIE type
14563 before building the type and calling set_die_type.
e71ec853 14564 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14565 possible before fetching more types to complete the current type.
14566 * Make the type as complete as possible before fetching more types. */
1c379e20 14567
f792889a 14568static struct type *
1c379e20
DJ
14569set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14570{
14571 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14572 struct objfile *objfile = cu->objfile;
14573 htab_t *type_hash_ptr;
1c379e20 14574
b4ba55a1
JB
14575 /* For Ada types, make sure that the gnat-specific data is always
14576 initialized (if not already set). There are a few types where
14577 we should not be doing so, because the type-specific area is
14578 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14579 where the type-specific area is used to store the floatformat).
14580 But this is not a problem, because the gnat-specific information
14581 is actually not needed for these types. */
14582 if (need_gnat_info (cu)
14583 && TYPE_CODE (type) != TYPE_CODE_FUNC
14584 && TYPE_CODE (type) != TYPE_CODE_FLT
14585 && !HAVE_GNAT_AUX_INFO (type))
14586 INIT_GNAT_SPECIFIC (type);
14587
673bfd45
DE
14588 if (cu->per_cu->from_debug_types)
14589 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14590 else
14591 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14592
14593 if (*type_hash_ptr == NULL)
f792889a 14594 {
673bfd45
DE
14595 *type_hash_ptr
14596 = htab_create_alloc_ex (127,
f792889a
DJ
14597 offset_and_type_hash,
14598 offset_and_type_eq,
14599 NULL,
673bfd45 14600 &objfile->objfile_obstack,
f792889a
DJ
14601 hashtab_obstack_allocate,
14602 dummy_obstack_deallocate);
f792889a 14603 }
1c379e20
DJ
14604
14605 ofs.offset = die->offset;
14606 ofs.type = type;
14607 slot = (struct dwarf2_offset_and_type **)
673bfd45 14608 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14609 if (*slot)
14610 complaint (&symfile_complaints,
14611 _("A problem internal to GDB: DIE 0x%x has type already set"),
14612 die->offset);
673bfd45 14613 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14614 **slot = ofs;
f792889a 14615 return type;
1c379e20
DJ
14616}
14617
673bfd45
DE
14618/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14619 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14620
14621static struct type *
673bfd45
DE
14622get_die_type_at_offset (unsigned int offset,
14623 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14624{
14625 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14626 htab_t type_hash;
f792889a 14627
673bfd45
DE
14628 if (per_cu->from_debug_types)
14629 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14630 else
14631 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14632 if (type_hash == NULL)
14633 return NULL;
1c379e20 14634
673bfd45 14635 ofs.offset = offset;
1c379e20
DJ
14636 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14637 if (slot)
14638 return slot->type;
14639 else
14640 return NULL;
14641}
14642
673bfd45
DE
14643/* Look up the type for DIE in the appropriate type_hash table,
14644 or return NULL if DIE does not have a saved type. */
14645
14646static struct type *
14647get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14648{
14649 return get_die_type_at_offset (die->offset, cu->per_cu);
14650}
14651
10b3939b
DJ
14652/* Add a dependence relationship from CU to REF_PER_CU. */
14653
14654static void
14655dwarf2_add_dependence (struct dwarf2_cu *cu,
14656 struct dwarf2_per_cu_data *ref_per_cu)
14657{
14658 void **slot;
14659
14660 if (cu->dependencies == NULL)
14661 cu->dependencies
14662 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14663 NULL, &cu->comp_unit_obstack,
14664 hashtab_obstack_allocate,
14665 dummy_obstack_deallocate);
14666
14667 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14668 if (*slot == NULL)
14669 *slot = ref_per_cu;
14670}
1c379e20 14671
f504f079
DE
14672/* Subroutine of dwarf2_mark to pass to htab_traverse.
14673 Set the mark field in every compilation unit in the
ae038cb0
DJ
14674 cache that we must keep because we are keeping CU. */
14675
10b3939b
DJ
14676static int
14677dwarf2_mark_helper (void **slot, void *data)
14678{
14679 struct dwarf2_per_cu_data *per_cu;
14680
14681 per_cu = (struct dwarf2_per_cu_data *) *slot;
14682 if (per_cu->cu->mark)
14683 return 1;
14684 per_cu->cu->mark = 1;
14685
14686 if (per_cu->cu->dependencies != NULL)
14687 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14688
14689 return 1;
14690}
14691
f504f079
DE
14692/* Set the mark field in CU and in every other compilation unit in the
14693 cache that we must keep because we are keeping CU. */
14694
ae038cb0
DJ
14695static void
14696dwarf2_mark (struct dwarf2_cu *cu)
14697{
14698 if (cu->mark)
14699 return;
14700 cu->mark = 1;
10b3939b
DJ
14701 if (cu->dependencies != NULL)
14702 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14703}
14704
14705static void
14706dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14707{
14708 while (per_cu)
14709 {
14710 per_cu->cu->mark = 0;
14711 per_cu = per_cu->cu->read_in_chain;
14712 }
72bf9492
DJ
14713}
14714
72bf9492
DJ
14715/* Trivial hash function for partial_die_info: the hash value of a DIE
14716 is its offset in .debug_info for this objfile. */
14717
14718static hashval_t
14719partial_die_hash (const void *item)
14720{
14721 const struct partial_die_info *part_die = item;
9a619af0 14722
72bf9492
DJ
14723 return part_die->offset;
14724}
14725
14726/* Trivial comparison function for partial_die_info structures: two DIEs
14727 are equal if they have the same offset. */
14728
14729static int
14730partial_die_eq (const void *item_lhs, const void *item_rhs)
14731{
14732 const struct partial_die_info *part_die_lhs = item_lhs;
14733 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14734
72bf9492
DJ
14735 return part_die_lhs->offset == part_die_rhs->offset;
14736}
14737
ae038cb0
DJ
14738static struct cmd_list_element *set_dwarf2_cmdlist;
14739static struct cmd_list_element *show_dwarf2_cmdlist;
14740
14741static void
14742set_dwarf2_cmd (char *args, int from_tty)
14743{
14744 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14745}
14746
14747static void
14748show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14749{
ae038cb0
DJ
14750 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14751}
14752
dce234bc
PP
14753/* If section described by INFO was mmapped, munmap it now. */
14754
14755static void
14756munmap_section_buffer (struct dwarf2_section_info *info)
14757{
14758 if (info->was_mmapped)
14759 {
14760#ifdef HAVE_MMAP
14761 intptr_t begin = (intptr_t) info->buffer;
14762 intptr_t map_begin = begin & ~(pagesize - 1);
14763 size_t map_length = info->size + begin - map_begin;
9a619af0 14764
dce234bc
PP
14765 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14766#else
14767 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14768 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14769#endif
14770 }
14771}
14772
14773/* munmap debug sections for OBJFILE, if necessary. */
14774
14775static void
c1bd65d0 14776dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14777{
14778 struct dwarf2_per_objfile *data = d;
9a619af0 14779
16be1145
DE
14780 /* This is sorted according to the order they're defined in to make it easier
14781 to keep in sync. */
dce234bc
PP
14782 munmap_section_buffer (&data->info);
14783 munmap_section_buffer (&data->abbrev);
14784 munmap_section_buffer (&data->line);
16be1145 14785 munmap_section_buffer (&data->loc);
dce234bc 14786 munmap_section_buffer (&data->macinfo);
16be1145 14787 munmap_section_buffer (&data->str);
dce234bc 14788 munmap_section_buffer (&data->ranges);
16be1145 14789 munmap_section_buffer (&data->types);
dce234bc
PP
14790 munmap_section_buffer (&data->frame);
14791 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14792 munmap_section_buffer (&data->gdb_index);
14793}
14794
14795\f
14796
14797/* The contents of the hash table we create when building the string
14798 table. */
14799struct strtab_entry
14800{
14801 offset_type offset;
14802 const char *str;
14803};
14804
14805/* Hash function for a strtab_entry. */
b89be57b 14806
9291a0cd
TT
14807static hashval_t
14808hash_strtab_entry (const void *e)
14809{
14810 const struct strtab_entry *entry = e;
14811 return mapped_index_string_hash (entry->str);
14812}
14813
14814/* Equality function for a strtab_entry. */
b89be57b 14815
9291a0cd
TT
14816static int
14817eq_strtab_entry (const void *a, const void *b)
14818{
14819 const struct strtab_entry *ea = a;
14820 const struct strtab_entry *eb = b;
14821 return !strcmp (ea->str, eb->str);
14822}
14823
14824/* Create a strtab_entry hash table. */
b89be57b 14825
9291a0cd
TT
14826static htab_t
14827create_strtab (void)
14828{
14829 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14830 xfree, xcalloc, xfree);
14831}
14832
14833/* Add a string to the constant pool. Return the string's offset in
14834 host order. */
b89be57b 14835
9291a0cd
TT
14836static offset_type
14837add_string (htab_t table, struct obstack *cpool, const char *str)
14838{
14839 void **slot;
14840 struct strtab_entry entry;
14841 struct strtab_entry *result;
14842
14843 entry.str = str;
14844 slot = htab_find_slot (table, &entry, INSERT);
14845 if (*slot)
14846 result = *slot;
14847 else
14848 {
14849 result = XNEW (struct strtab_entry);
14850 result->offset = obstack_object_size (cpool);
14851 result->str = str;
14852 obstack_grow_str0 (cpool, str);
14853 *slot = result;
14854 }
14855 return result->offset;
14856}
14857
14858/* An entry in the symbol table. */
14859struct symtab_index_entry
14860{
14861 /* The name of the symbol. */
14862 const char *name;
14863 /* The offset of the name in the constant pool. */
14864 offset_type index_offset;
14865 /* A sorted vector of the indices of all the CUs that hold an object
14866 of this name. */
14867 VEC (offset_type) *cu_indices;
14868};
14869
14870/* The symbol table. This is a power-of-2-sized hash table. */
14871struct mapped_symtab
14872{
14873 offset_type n_elements;
14874 offset_type size;
14875 struct symtab_index_entry **data;
14876};
14877
14878/* Hash function for a symtab_index_entry. */
b89be57b 14879
9291a0cd
TT
14880static hashval_t
14881hash_symtab_entry (const void *e)
14882{
14883 const struct symtab_index_entry *entry = e;
14884 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14885 sizeof (offset_type) * VEC_length (offset_type,
14886 entry->cu_indices),
14887 0);
14888}
14889
14890/* Equality function for a symtab_index_entry. */
b89be57b 14891
9291a0cd
TT
14892static int
14893eq_symtab_entry (const void *a, const void *b)
14894{
14895 const struct symtab_index_entry *ea = a;
14896 const struct symtab_index_entry *eb = b;
14897 int len = VEC_length (offset_type, ea->cu_indices);
14898 if (len != VEC_length (offset_type, eb->cu_indices))
14899 return 0;
14900 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14901 VEC_address (offset_type, eb->cu_indices),
14902 sizeof (offset_type) * len);
14903}
14904
14905/* Destroy a symtab_index_entry. */
b89be57b 14906
9291a0cd
TT
14907static void
14908delete_symtab_entry (void *p)
14909{
14910 struct symtab_index_entry *entry = p;
14911 VEC_free (offset_type, entry->cu_indices);
14912 xfree (entry);
14913}
14914
14915/* Create a hash table holding symtab_index_entry objects. */
b89be57b 14916
9291a0cd 14917static htab_t
3876f04e 14918create_symbol_hash_table (void)
9291a0cd
TT
14919{
14920 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14921 delete_symtab_entry, xcalloc, xfree);
14922}
14923
14924/* Create a new mapped symtab object. */
b89be57b 14925
9291a0cd
TT
14926static struct mapped_symtab *
14927create_mapped_symtab (void)
14928{
14929 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14930 symtab->n_elements = 0;
14931 symtab->size = 1024;
14932 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14933 return symtab;
14934}
14935
14936/* Destroy a mapped_symtab. */
b89be57b 14937
9291a0cd
TT
14938static void
14939cleanup_mapped_symtab (void *p)
14940{
14941 struct mapped_symtab *symtab = p;
14942 /* The contents of the array are freed when the other hash table is
14943 destroyed. */
14944 xfree (symtab->data);
14945 xfree (symtab);
14946}
14947
14948/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14949 the slot. */
b89be57b 14950
9291a0cd
TT
14951static struct symtab_index_entry **
14952find_slot (struct mapped_symtab *symtab, const char *name)
14953{
14954 offset_type index, step, hash = mapped_index_string_hash (name);
14955
14956 index = hash & (symtab->size - 1);
14957 step = ((hash * 17) & (symtab->size - 1)) | 1;
14958
14959 for (;;)
14960 {
14961 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14962 return &symtab->data[index];
14963 index = (index + step) & (symtab->size - 1);
14964 }
14965}
14966
14967/* Expand SYMTAB's hash table. */
b89be57b 14968
9291a0cd
TT
14969static void
14970hash_expand (struct mapped_symtab *symtab)
14971{
14972 offset_type old_size = symtab->size;
14973 offset_type i;
14974 struct symtab_index_entry **old_entries = symtab->data;
14975
14976 symtab->size *= 2;
14977 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14978
14979 for (i = 0; i < old_size; ++i)
14980 {
14981 if (old_entries[i])
14982 {
14983 struct symtab_index_entry **slot = find_slot (symtab,
14984 old_entries[i]->name);
14985 *slot = old_entries[i];
14986 }
14987 }
14988
14989 xfree (old_entries);
14990}
14991
14992/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14993 is the index of the CU in which the symbol appears. */
b89be57b 14994
9291a0cd
TT
14995static void
14996add_index_entry (struct mapped_symtab *symtab, const char *name,
14997 offset_type cu_index)
14998{
14999 struct symtab_index_entry **slot;
15000
15001 ++symtab->n_elements;
15002 if (4 * symtab->n_elements / 3 >= symtab->size)
15003 hash_expand (symtab);
15004
15005 slot = find_slot (symtab, name);
15006 if (!*slot)
15007 {
15008 *slot = XNEW (struct symtab_index_entry);
15009 (*slot)->name = name;
15010 (*slot)->cu_indices = NULL;
15011 }
15012 /* Don't push an index twice. Due to how we add entries we only
15013 have to check the last one. */
15014 if (VEC_empty (offset_type, (*slot)->cu_indices)
15015 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15016 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15017}
15018
15019/* Add a vector of indices to the constant pool. */
b89be57b 15020
9291a0cd 15021static offset_type
3876f04e 15022add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15023 struct symtab_index_entry *entry)
15024{
15025 void **slot;
15026
3876f04e 15027 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15028 if (!*slot)
15029 {
15030 offset_type len = VEC_length (offset_type, entry->cu_indices);
15031 offset_type val = MAYBE_SWAP (len);
15032 offset_type iter;
15033 int i;
15034
15035 *slot = entry;
15036 entry->index_offset = obstack_object_size (cpool);
15037
15038 obstack_grow (cpool, &val, sizeof (val));
15039 for (i = 0;
15040 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15041 ++i)
15042 {
15043 val = MAYBE_SWAP (iter);
15044 obstack_grow (cpool, &val, sizeof (val));
15045 }
15046 }
15047 else
15048 {
15049 struct symtab_index_entry *old_entry = *slot;
15050 entry->index_offset = old_entry->index_offset;
15051 entry = old_entry;
15052 }
15053 return entry->index_offset;
15054}
15055
15056/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15057 constant pool entries going into the obstack CPOOL. */
b89be57b 15058
9291a0cd
TT
15059static void
15060write_hash_table (struct mapped_symtab *symtab,
15061 struct obstack *output, struct obstack *cpool)
15062{
15063 offset_type i;
3876f04e 15064 htab_t symbol_hash_table;
9291a0cd
TT
15065 htab_t str_table;
15066
3876f04e 15067 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15068 str_table = create_strtab ();
3876f04e 15069
9291a0cd
TT
15070 /* We add all the index vectors to the constant pool first, to
15071 ensure alignment is ok. */
15072 for (i = 0; i < symtab->size; ++i)
15073 {
15074 if (symtab->data[i])
3876f04e 15075 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15076 }
15077
15078 /* Now write out the hash table. */
15079 for (i = 0; i < symtab->size; ++i)
15080 {
15081 offset_type str_off, vec_off;
15082
15083 if (symtab->data[i])
15084 {
15085 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15086 vec_off = symtab->data[i]->index_offset;
15087 }
15088 else
15089 {
15090 /* While 0 is a valid constant pool index, it is not valid
15091 to have 0 for both offsets. */
15092 str_off = 0;
15093 vec_off = 0;
15094 }
15095
15096 str_off = MAYBE_SWAP (str_off);
15097 vec_off = MAYBE_SWAP (vec_off);
15098
15099 obstack_grow (output, &str_off, sizeof (str_off));
15100 obstack_grow (output, &vec_off, sizeof (vec_off));
15101 }
15102
15103 htab_delete (str_table);
3876f04e 15104 htab_delete (symbol_hash_table);
9291a0cd
TT
15105}
15106
15107/* Write an address entry to ADDR_OBSTACK. The addresses are taken
15108 from PST; CU_INDEX is the index of the CU in the vector of all
15109 CUs. */
b89be57b 15110
9291a0cd
TT
15111static void
15112add_address_entry (struct objfile *objfile,
15113 struct obstack *addr_obstack, struct partial_symtab *pst,
15114 unsigned int cu_index)
15115{
15116 offset_type offset;
15117 char addr[8];
15118 CORE_ADDR baseaddr;
15119
1fd400ff
TT
15120 /* Don't bother recording empty ranges. */
15121 if (pst->textlow == pst->texthigh)
15122 return;
15123
9291a0cd
TT
15124 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15125
15126 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15127 obstack_grow (addr_obstack, addr, 8);
15128 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15129 obstack_grow (addr_obstack, addr, 8);
15130 offset = MAYBE_SWAP (cu_index);
15131 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15132}
15133
15134/* Add a list of partial symbols to SYMTAB. */
b89be57b 15135
9291a0cd
TT
15136static void
15137write_psymbols (struct mapped_symtab *symtab,
987d643c 15138 htab_t psyms_seen,
9291a0cd
TT
15139 struct partial_symbol **psymp,
15140 int count,
987d643c
TT
15141 offset_type cu_index,
15142 int is_static)
9291a0cd
TT
15143{
15144 for (; count-- > 0; ++psymp)
15145 {
987d643c
TT
15146 void **slot, *lookup;
15147
9291a0cd
TT
15148 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15149 error (_("Ada is not currently supported by the index"));
987d643c
TT
15150
15151 /* We only want to add a given psymbol once. However, we also
15152 want to account for whether it is global or static. So, we
15153 may add it twice, using slightly different values. */
15154 if (is_static)
15155 {
15156 uintptr_t val = 1 | (uintptr_t) *psymp;
15157
15158 lookup = (void *) val;
15159 }
15160 else
15161 lookup = *psymp;
15162
15163 /* Only add a given psymbol once. */
15164 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15165 if (!*slot)
15166 {
15167 *slot = lookup;
15168 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15169 }
9291a0cd
TT
15170 }
15171}
15172
15173/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15174 exception if there is an error. */
b89be57b 15175
9291a0cd
TT
15176static void
15177write_obstack (FILE *file, struct obstack *obstack)
15178{
15179 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15180 file)
15181 != obstack_object_size (obstack))
15182 error (_("couldn't data write to file"));
15183}
15184
15185/* Unlink a file if the argument is not NULL. */
b89be57b 15186
9291a0cd
TT
15187static void
15188unlink_if_set (void *p)
15189{
15190 char **filename = p;
15191 if (*filename)
15192 unlink (*filename);
15193}
15194
1fd400ff
TT
15195/* A helper struct used when iterating over debug_types. */
15196struct signatured_type_index_data
15197{
15198 struct objfile *objfile;
15199 struct mapped_symtab *symtab;
15200 struct obstack *types_list;
987d643c 15201 htab_t psyms_seen;
1fd400ff
TT
15202 int cu_index;
15203};
15204
15205/* A helper function that writes a single signatured_type to an
15206 obstack. */
b89be57b 15207
1fd400ff
TT
15208static int
15209write_one_signatured_type (void **slot, void *d)
15210{
15211 struct signatured_type_index_data *info = d;
15212 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15213 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15214 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15215 gdb_byte val[8];
15216
15217 write_psymbols (info->symtab,
987d643c 15218 info->psyms_seen,
1fd400ff 15219 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15220 psymtab->n_global_syms, info->cu_index,
15221 0);
1fd400ff 15222 write_psymbols (info->symtab,
987d643c 15223 info->psyms_seen,
1fd400ff 15224 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15225 psymtab->n_static_syms, info->cu_index,
15226 1);
1fd400ff
TT
15227
15228 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15229 obstack_grow (info->types_list, val, 8);
15230 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15231 obstack_grow (info->types_list, val, 8);
15232 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15233 obstack_grow (info->types_list, val, 8);
15234
15235 ++info->cu_index;
15236
15237 return 1;
15238}
15239
987d643c
TT
15240/* A cleanup function for an htab_t. */
15241
15242static void
15243cleanup_htab (void *arg)
15244{
15245 htab_delete (arg);
15246}
15247
9291a0cd 15248/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15249
9291a0cd
TT
15250static void
15251write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15252{
15253 struct cleanup *cleanup;
15254 char *filename, *cleanup_filename;
1fd400ff
TT
15255 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15256 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15257 int i;
15258 FILE *out_file;
15259 struct mapped_symtab *symtab;
15260 offset_type val, size_of_contents, total_len;
15261 struct stat st;
15262 char buf[8];
987d643c 15263 htab_t psyms_seen;
9291a0cd
TT
15264
15265 if (!objfile->psymtabs)
15266 return;
15267 if (dwarf2_per_objfile->using_index)
15268 error (_("Cannot use an index to create the index"));
15269
15270 if (stat (objfile->name, &st) < 0)
15271 perror_with_name (_("Could not stat"));
15272
15273 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15274 INDEX_SUFFIX, (char *) NULL);
15275 cleanup = make_cleanup (xfree, filename);
15276
15277 out_file = fopen (filename, "wb");
15278 if (!out_file)
15279 error (_("Can't open `%s' for writing"), filename);
15280
15281 cleanup_filename = filename;
15282 make_cleanup (unlink_if_set, &cleanup_filename);
15283
15284 symtab = create_mapped_symtab ();
15285 make_cleanup (cleanup_mapped_symtab, symtab);
15286
15287 obstack_init (&addr_obstack);
15288 make_cleanup_obstack_free (&addr_obstack);
15289
15290 obstack_init (&cu_list);
15291 make_cleanup_obstack_free (&cu_list);
15292
1fd400ff
TT
15293 obstack_init (&types_cu_list);
15294 make_cleanup_obstack_free (&types_cu_list);
15295
987d643c
TT
15296 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15297 NULL, xcalloc, xfree);
15298 make_cleanup (cleanup_htab, psyms_seen);
15299
1fd400ff
TT
15300 /* The list is already sorted, so we don't need to do additional
15301 work here. Also, the debug_types entries do not appear in
15302 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15303 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15304 {
e254ef6a
DE
15305 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15306 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15307 gdb_byte val[8];
15308
15309 write_psymbols (symtab,
987d643c 15310 psyms_seen,
9291a0cd 15311 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15312 psymtab->n_global_syms, i,
15313 0);
9291a0cd 15314 write_psymbols (symtab,
987d643c 15315 psyms_seen,
9291a0cd 15316 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15317 psymtab->n_static_syms, i,
15318 1);
9291a0cd
TT
15319
15320 add_address_entry (objfile, &addr_obstack, psymtab, i);
15321
e254ef6a 15322 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15323 obstack_grow (&cu_list, val, 8);
e254ef6a 15324 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15325 obstack_grow (&cu_list, val, 8);
15326 }
15327
1fd400ff
TT
15328 /* Write out the .debug_type entries, if any. */
15329 if (dwarf2_per_objfile->signatured_types)
15330 {
15331 struct signatured_type_index_data sig_data;
15332
15333 sig_data.objfile = objfile;
15334 sig_data.symtab = symtab;
15335 sig_data.types_list = &types_cu_list;
987d643c 15336 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15337 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15338 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15339 write_one_signatured_type, &sig_data);
15340 }
15341
9291a0cd
TT
15342 obstack_init (&constant_pool);
15343 make_cleanup_obstack_free (&constant_pool);
15344 obstack_init (&symtab_obstack);
15345 make_cleanup_obstack_free (&symtab_obstack);
15346 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15347
15348 obstack_init (&contents);
15349 make_cleanup_obstack_free (&contents);
1fd400ff 15350 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15351 total_len = size_of_contents;
15352
15353 /* The version number. */
987d643c 15354 val = MAYBE_SWAP (3);
9291a0cd
TT
15355 obstack_grow (&contents, &val, sizeof (val));
15356
15357 /* The offset of the CU list from the start of the file. */
15358 val = MAYBE_SWAP (total_len);
15359 obstack_grow (&contents, &val, sizeof (val));
15360 total_len += obstack_object_size (&cu_list);
15361
1fd400ff
TT
15362 /* The offset of the types CU list from the start of the file. */
15363 val = MAYBE_SWAP (total_len);
15364 obstack_grow (&contents, &val, sizeof (val));
15365 total_len += obstack_object_size (&types_cu_list);
15366
9291a0cd
TT
15367 /* The offset of the address table from the start of the file. */
15368 val = MAYBE_SWAP (total_len);
15369 obstack_grow (&contents, &val, sizeof (val));
15370 total_len += obstack_object_size (&addr_obstack);
15371
15372 /* The offset of the symbol table from the start of the file. */
15373 val = MAYBE_SWAP (total_len);
15374 obstack_grow (&contents, &val, sizeof (val));
15375 total_len += obstack_object_size (&symtab_obstack);
15376
15377 /* The offset of the constant pool from the start of the file. */
15378 val = MAYBE_SWAP (total_len);
15379 obstack_grow (&contents, &val, sizeof (val));
15380 total_len += obstack_object_size (&constant_pool);
15381
15382 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15383
15384 write_obstack (out_file, &contents);
15385 write_obstack (out_file, &cu_list);
1fd400ff 15386 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15387 write_obstack (out_file, &addr_obstack);
15388 write_obstack (out_file, &symtab_obstack);
15389 write_obstack (out_file, &constant_pool);
15390
15391 fclose (out_file);
15392
15393 /* We want to keep the file, so we set cleanup_filename to NULL
15394 here. See unlink_if_set. */
15395 cleanup_filename = NULL;
15396
15397 do_cleanups (cleanup);
15398}
15399
15400/* The mapped index file format is designed to be directly mmap()able
15401 on any architecture. In most cases, a datum is represented using a
15402 little-endian 32-bit integer value, called an offset_type. Big
15403 endian machines must byte-swap the values before using them.
15404 Exceptions to this rule are noted. The data is laid out such that
15405 alignment is always respected.
15406
15407 A mapped index consists of several sections.
15408
15409 1. The file header. This is a sequence of values, of offset_type
15410 unless otherwise noted:
987d643c
TT
15411
15412 [0] The version number, currently 3. Versions 1 and 2 are
15413 obsolete.
9291a0cd 15414 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15415 [2] The offset, from the start of the file, of the types CU list.
15416 Note that this section can be empty, in which case this offset will
15417 be equal to the next offset.
15418 [3] The offset, from the start of the file, of the address section.
15419 [4] The offset, from the start of the file, of the symbol table.
15420 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15421
15422 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15423 little-endian values, sorted by the CU offset. The first element
15424 in each pair is the offset of a CU in the .debug_info section. The
15425 second element in each pair is the length of that CU. References
15426 to a CU elsewhere in the map are done using a CU index, which is
15427 just the 0-based index into this table. Note that if there are
15428 type CUs, then conceptually CUs and type CUs form a single list for
15429 the purposes of CU indices.
15430
987d643c
TT
15431 3. The types CU list. This is a sequence of triplets of 64-bit
15432 little-endian values. In a triplet, the first value is the CU
15433 offset, the second value is the type offset in the CU, and the
15434 third value is the type signature. The types CU list is not
15435 sorted.
9291a0cd 15436
987d643c 15437 4. The address section. The address section consists of a sequence
9291a0cd
TT
15438 of address entries. Each address entry has three elements.
15439 [0] The low address. This is a 64-bit little-endian value.
15440 [1] The high address. This is a 64-bit little-endian value.
15441 [2] The CU index. This is an offset_type value.
15442
987d643c 15443 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15444 table is always a power of 2. The initial hash and the step are
15445 currently defined by the `find_slot' function.
15446
15447 Each slot in the hash table consists of a pair of offset_type
15448 values. The first value is the offset of the symbol's name in the
15449 constant pool. The second value is the offset of the CU vector in
15450 the constant pool.
15451
15452 If both values are 0, then this slot in the hash table is empty.
15453 This is ok because while 0 is a valid constant pool index, it
15454 cannot be a valid index for both a string and a CU vector.
15455
15456 A string in the constant pool is stored as a \0-terminated string,
15457 as you'd expect.
15458
15459 A CU vector in the constant pool is a sequence of offset_type
15460 values. The first value is the number of CU indices in the vector.
15461 Each subsequent value is the index of a CU in the CU list. This
15462 element in the hash table is used to indicate which CUs define the
15463 symbol.
15464
987d643c 15465 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15466 organized so that alignment is correct: CU vectors are stored
15467 first, followed by strings. */
11570e71 15468
9291a0cd
TT
15469static void
15470save_gdb_index_command (char *arg, int from_tty)
15471{
15472 struct objfile *objfile;
15473
15474 if (!arg || !*arg)
96d19272 15475 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15476
15477 ALL_OBJFILES (objfile)
15478 {
15479 struct stat st;
15480
15481 /* If the objfile does not correspond to an actual file, skip it. */
15482 if (stat (objfile->name, &st) < 0)
15483 continue;
15484
15485 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15486 if (dwarf2_per_objfile)
15487 {
15488 volatile struct gdb_exception except;
15489
15490 TRY_CATCH (except, RETURN_MASK_ERROR)
15491 {
15492 write_psymtabs_to_index (objfile, arg);
15493 }
15494 if (except.reason < 0)
15495 exception_fprintf (gdb_stderr, except,
15496 _("Error while writing index for `%s': "),
15497 objfile->name);
15498 }
15499 }
dce234bc
PP
15500}
15501
9291a0cd
TT
15502\f
15503
9eae7c52
TT
15504int dwarf2_always_disassemble;
15505
15506static void
15507show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15508 struct cmd_list_element *c, const char *value)
15509{
15510 fprintf_filtered (file, _("\
15511Whether to always disassemble DWARF expressions is %s.\n"),
15512 value);
15513}
15514
6502dd73
DJ
15515void _initialize_dwarf2_read (void);
15516
15517void
15518_initialize_dwarf2_read (void)
15519{
96d19272
JK
15520 struct cmd_list_element *c;
15521
dce234bc 15522 dwarf2_objfile_data_key
c1bd65d0 15523 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15524
1bedd215
AC
15525 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15526Set DWARF 2 specific variables.\n\
15527Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15528 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15529 0/*allow-unknown*/, &maintenance_set_cmdlist);
15530
1bedd215
AC
15531 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15532Show DWARF 2 specific variables\n\
15533Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15534 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15535 0/*allow-unknown*/, &maintenance_show_cmdlist);
15536
15537 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15538 &dwarf2_max_cache_age, _("\
15539Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15540Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15541A higher limit means that cached compilation units will be stored\n\
15542in memory longer, and more total memory will be used. Zero disables\n\
15543caching, which can slow down startup."),
2c5b56ce 15544 NULL,
920d2a44 15545 show_dwarf2_max_cache_age,
2c5b56ce 15546 &set_dwarf2_cmdlist,
ae038cb0 15547 &show_dwarf2_cmdlist);
d97bc12b 15548
9eae7c52
TT
15549 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15550 &dwarf2_always_disassemble, _("\
15551Set whether `info address' always disassembles DWARF expressions."), _("\
15552Show whether `info address' always disassembles DWARF expressions."), _("\
15553When enabled, DWARF expressions are always printed in an assembly-like\n\
15554syntax. When disabled, expressions will be printed in a more\n\
15555conversational style, when possible."),
15556 NULL,
15557 show_dwarf2_always_disassemble,
15558 &set_dwarf2_cmdlist,
15559 &show_dwarf2_cmdlist);
15560
d97bc12b
DE
15561 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15562Set debugging of the dwarf2 DIE reader."), _("\
15563Show debugging of the dwarf2 DIE reader."), _("\
15564When enabled (non-zero), DIEs are dumped after they are read in.\n\
15565The value is the maximum depth to print."),
15566 NULL,
15567 NULL,
15568 &setdebuglist, &showdebuglist);
9291a0cd 15569
96d19272 15570 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71
DE
15571 _("\
15572Save a .gdb-index file.\n\
15573Usage: save gdb-index DIRECTORY"),
96d19272
JK
15574 &save_cmdlist);
15575 set_cmd_completer (c, filename_completer);
6502dd73 15576}
This page took 2.039236 seconds and 4 git commands to generate.