* dwarf2read.c (dw2_lookup_symtab): Remove duplicate call to
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
3876f04e
DE
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
673bfd45
DE
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
6502dd73
DJ
226};
227
228static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
229
230/* names of the debugging sections */
231
233a11ab
CS
232/* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235#define INFO_SECTION "debug_info"
236#define ABBREV_SECTION "debug_abbrev"
237#define LINE_SECTION "debug_line"
233a11ab
CS
238#define LOC_SECTION "debug_loc"
239#define MACINFO_SECTION "debug_macinfo"
240#define STR_SECTION "debug_str"
241#define RANGES_SECTION "debug_ranges"
348e048f 242#define TYPES_SECTION "debug_types"
233a11ab
CS
243#define FRAME_SECTION "debug_frame"
244#define EH_FRAME_SECTION "eh_frame"
9291a0cd 245#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
246
247/* local data types */
248
57349743
JB
249/* We hold several abbreviation tables in memory at the same time. */
250#ifndef ABBREV_HASH_SIZE
251#define ABBREV_HASH_SIZE 121
252#endif
253
107d2387
AC
254/* The data in a compilation unit header, after target2host
255 translation, looks like this. */
c906108c 256struct comp_unit_head
a738430d 257{
c764a876 258 unsigned int length;
a738430d 259 short version;
a738430d
MK
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
9cbfa09e 262 unsigned int abbrev_offset;
57349743 263
a738430d
MK
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
57349743 266
a738430d
MK
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
57349743 269
a738430d
MK
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
57349743 273
d00adf39
DE
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
a738430d 277};
c906108c 278
3da10d80
KS
279/* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281struct delayed_method_info
282{
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297};
298
299typedef struct delayed_method_info delayed_method_info;
300DEF_VEC_O (delayed_method_info);
301
e7c27a73
DJ
302/* Internal state when decoding a particular compilation unit. */
303struct dwarf2_cu
304{
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
d00adf39 308 /* The header of the compilation unit. */
e7c27a73 309 struct comp_unit_head header;
e142c38c 310
d00adf39
DE
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
e142c38c
DJ
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492
DJ
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
10b3939b 361 /* A hash table of die offsets for following references. */
51545339 362 htab_t die_hash;
10b3939b
DJ
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
cb1df416
DJ
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
3da10d80
KS
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
ae038cb0
DJ
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
72bf9492
DJ
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
e7c27a73
DJ
391};
392
9291a0cd
TT
393/* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396struct dwarf2_per_cu_quick_data
397{
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417};
418
10b3939b
DJ
419/* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
ae038cb0
DJ
425struct dwarf2_per_cu_data
426{
348e048f 427 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 428 bytes should suffice to store the length of any compilation unit
45452591
DE
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
c764a876 432 unsigned int offset;
348e048f 433 unsigned int length : 29;
ae038cb0
DJ
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
c764a876 437 unsigned int queued : 1;
ae038cb0 438
5afb4e99
DJ
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
348e048f
DE
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
17ea53c3
JK
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
ae038cb0 451 struct dwarf2_cu *cu;
1c379e20 452
9291a0cd
TT
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
ae038cb0
DJ
468};
469
348e048f
DE
470/* Entry in the signatured_types hash table. */
471
472struct signatured_type
473{
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484};
485
93311388
DE
486/* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492struct die_reader_specs
493{
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503};
504
debd256d
JB
505/* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508struct line_header
509{
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
2dc7f7b3 514 unsigned char maximum_ops_per_instruction;
debd256d
JB
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
c906108c 538 {
debd256d
JB
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
aaa75496 543 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 544 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
545 } *file_names;
546
547 /* The start and end of the statement program following this
6502dd73 548 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 549 gdb_byte *statement_program_start, *statement_program_end;
debd256d 550};
c906108c
SS
551
552/* When we construct a partial symbol table entry we only
553 need this much information. */
554struct partial_die_info
555 {
72bf9492 556 /* Offset of this DIE. */
c906108c 557 unsigned int offset;
72bf9492
DJ
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
72bf9492
DJ
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
fa4028e9
JB
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
98bfdba5
PA
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
abc72ce4
DE
580 /* Flag set if fixup_partial_die has been called on this die. */
581 unsigned int fixup_called : 1;
582
72bf9492 583 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 584 sometimes a default name for unnamed DIEs. */
c906108c 585 char *name;
72bf9492 586
abc72ce4
DE
587 /* The linkage name, if present. */
588 const char *linkage_name;
589
72bf9492
DJ
590 /* The scope to prepend to our children. This is generally
591 allocated on the comp_unit_obstack, so will disappear
592 when this compilation unit leaves the cache. */
593 char *scope;
594
595 /* The location description associated with this DIE, if any. */
596 struct dwarf_block *locdesc;
597
598 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
599 CORE_ADDR lowpc;
600 CORE_ADDR highpc;
72bf9492 601
93311388 602 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 603 DW_AT_sibling, if any. */
abc72ce4
DE
604 /* NOTE: This member isn't strictly necessary, read_partial_die could
605 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 606 gdb_byte *sibling;
72bf9492
DJ
607
608 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
609 DW_AT_specification (or DW_AT_abstract_origin or
610 DW_AT_extension). */
611 unsigned int spec_offset;
612
613 /* Pointers to this DIE's parent, first child, and next sibling,
614 if any. */
615 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
616 };
617
618/* This data structure holds the information of an abbrev. */
619struct abbrev_info
620 {
621 unsigned int number; /* number identifying abbrev */
622 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
623 unsigned short has_children; /* boolean */
624 unsigned short num_attrs; /* number of attributes */
c906108c
SS
625 struct attr_abbrev *attrs; /* an array of attribute descriptions */
626 struct abbrev_info *next; /* next in chain */
627 };
628
629struct attr_abbrev
630 {
9d25dd43
DE
631 ENUM_BITFIELD(dwarf_attribute) name : 16;
632 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
633 };
634
b60c80d6
DJ
635/* Attributes have a name and a value */
636struct attribute
637 {
9d25dd43 638 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
639 ENUM_BITFIELD(dwarf_form) form : 15;
640
641 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
642 field should be in u.str (existing only for DW_STRING) but it is kept
643 here for better struct attribute alignment. */
644 unsigned int string_is_canonical : 1;
645
b60c80d6
DJ
646 union
647 {
648 char *str;
649 struct dwarf_block *blk;
43bbcdc2
PH
650 ULONGEST unsnd;
651 LONGEST snd;
b60c80d6 652 CORE_ADDR addr;
348e048f 653 struct signatured_type *signatured_type;
b60c80d6
DJ
654 }
655 u;
656 };
657
c906108c
SS
658/* This data structure holds a complete die structure. */
659struct die_info
660 {
76815b17
DE
661 /* DWARF-2 tag for this DIE. */
662 ENUM_BITFIELD(dwarf_tag) tag : 16;
663
664 /* Number of attributes */
98bfdba5
PA
665 unsigned char num_attrs;
666
667 /* True if we're presently building the full type name for the
668 type derived from this DIE. */
669 unsigned char building_fullname : 1;
76815b17
DE
670
671 /* Abbrev number */
672 unsigned int abbrev;
673
93311388 674 /* Offset in .debug_info or .debug_types section. */
76815b17 675 unsigned int offset;
78ba4af6
JB
676
677 /* The dies in a compilation unit form an n-ary tree. PARENT
678 points to this die's parent; CHILD points to the first child of
679 this node; and all the children of a given node are chained
4950bc1c 680 together via their SIBLING fields. */
639d11d3
DC
681 struct die_info *child; /* Its first child, if any. */
682 struct die_info *sibling; /* Its next sibling, if any. */
683 struct die_info *parent; /* Its parent, if any. */
c906108c 684
b60c80d6
DJ
685 /* An array of attributes, with NUM_ATTRS elements. There may be
686 zero, but it's not common and zero-sized arrays are not
687 sufficiently portable C. */
688 struct attribute attrs[1];
c906108c
SS
689 };
690
5fb290d7
DJ
691struct function_range
692{
693 const char *name;
694 CORE_ADDR lowpc, highpc;
695 int seen_line;
696 struct function_range *next;
697};
698
c906108c
SS
699/* Get at parts of an attribute structure */
700
701#define DW_STRING(attr) ((attr)->u.str)
8285870a 702#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
703#define DW_UNSND(attr) ((attr)->u.unsnd)
704#define DW_BLOCK(attr) ((attr)->u.blk)
705#define DW_SND(attr) ((attr)->u.snd)
706#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 707#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
708
709/* Blocks are a bunch of untyped bytes. */
710struct dwarf_block
711 {
712 unsigned int size;
fe1b8b76 713 gdb_byte *data;
c906108c
SS
714 };
715
c906108c
SS
716#ifndef ATTR_ALLOC_CHUNK
717#define ATTR_ALLOC_CHUNK 4
718#endif
719
c906108c
SS
720/* Allocate fields for structs, unions and enums in this size. */
721#ifndef DW_FIELD_ALLOC_CHUNK
722#define DW_FIELD_ALLOC_CHUNK 4
723#endif
724
c906108c
SS
725/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
726 but this would require a corresponding change in unpack_field_as_long
727 and friends. */
728static int bits_per_byte = 8;
729
730/* The routines that read and process dies for a C struct or C++ class
731 pass lists of data member fields and lists of member function fields
732 in an instance of a field_info structure, as defined below. */
733struct field_info
c5aa993b
JM
734 {
735 /* List of data member and baseclasses fields. */
736 struct nextfield
737 {
738 struct nextfield *next;
739 int accessibility;
740 int virtuality;
741 struct field field;
742 }
7d0ccb61 743 *fields, *baseclasses;
c906108c 744
7d0ccb61 745 /* Number of fields (including baseclasses). */
c5aa993b 746 int nfields;
c906108c 747
c5aa993b
JM
748 /* Number of baseclasses. */
749 int nbaseclasses;
c906108c 750
c5aa993b
JM
751 /* Set if the accesibility of one of the fields is not public. */
752 int non_public_fields;
c906108c 753
c5aa993b
JM
754 /* Member function fields array, entries are allocated in the order they
755 are encountered in the object file. */
756 struct nextfnfield
757 {
758 struct nextfnfield *next;
759 struct fn_field fnfield;
760 }
761 *fnfields;
c906108c 762
c5aa993b
JM
763 /* Member function fieldlist array, contains name of possibly overloaded
764 member function, number of overloaded member functions and a pointer
765 to the head of the member function field chain. */
766 struct fnfieldlist
767 {
768 char *name;
769 int length;
770 struct nextfnfield *head;
771 }
772 *fnfieldlists;
c906108c 773
c5aa993b
JM
774 /* Number of entries in the fnfieldlists array. */
775 int nfnfields;
98751a41
JK
776
777 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
778 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
779 struct typedef_field_list
780 {
781 struct typedef_field field;
782 struct typedef_field_list *next;
783 }
784 *typedef_field_list;
785 unsigned typedef_field_list_count;
c5aa993b 786 };
c906108c 787
10b3939b
DJ
788/* One item on the queue of compilation units to read in full symbols
789 for. */
790struct dwarf2_queue_item
791{
792 struct dwarf2_per_cu_data *per_cu;
793 struct dwarf2_queue_item *next;
794};
795
796/* The current queue. */
797static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
798
ae038cb0
DJ
799/* Loaded secondary compilation units are kept in memory until they
800 have not been referenced for the processing of this many
801 compilation units. Set this to zero to disable caching. Cache
802 sizes of up to at least twenty will improve startup time for
803 typical inter-CU-reference binaries, at an obvious memory cost. */
804static int dwarf2_max_cache_age = 5;
920d2a44
AC
805static void
806show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
807 struct cmd_list_element *c, const char *value)
808{
809 fprintf_filtered (file, _("\
810The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
811 value);
812}
813
ae038cb0 814
c906108c
SS
815/* Various complaints about symbol reading that don't abort the process */
816
4d3c2250
KB
817static void
818dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 819{
4d3c2250 820 complaint (&symfile_complaints,
e2e0b3e5 821 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
822}
823
25e43795
DJ
824static void
825dwarf2_debug_line_missing_file_complaint (void)
826{
827 complaint (&symfile_complaints,
828 _(".debug_line section has line data without a file"));
829}
830
59205f5a
JB
831static void
832dwarf2_debug_line_missing_end_sequence_complaint (void)
833{
834 complaint (&symfile_complaints,
835 _(".debug_line section has line program sequence without an end"));
836}
837
4d3c2250
KB
838static void
839dwarf2_complex_location_expr_complaint (void)
2e276125 840{
e2e0b3e5 841 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
842}
843
4d3c2250
KB
844static void
845dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
846 int arg3)
2e276125 847{
4d3c2250 848 complaint (&symfile_complaints,
e2e0b3e5 849 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
850 arg2, arg3);
851}
852
853static void
854dwarf2_macros_too_long_complaint (void)
2e276125 855{
4d3c2250 856 complaint (&symfile_complaints,
e2e0b3e5 857 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
858}
859
860static void
861dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 862{
4d3c2250 863 complaint (&symfile_complaints,
e2e0b3e5 864 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
865 arg1);
866}
867
868static void
869dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 870{
4d3c2250 871 complaint (&symfile_complaints,
e2e0b3e5 872 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 873}
c906108c 874
c906108c
SS
875/* local function prototypes */
876
4efb68b1 877static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 878
aaa75496
JB
879static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
880 struct objfile *);
881
c67a9c90 882static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 883
72bf9492
DJ
884static void scan_partial_symbols (struct partial_die_info *,
885 CORE_ADDR *, CORE_ADDR *,
5734ee8b 886 int, struct dwarf2_cu *);
c906108c 887
72bf9492
DJ
888static void add_partial_symbol (struct partial_die_info *,
889 struct dwarf2_cu *);
63d06c5c 890
72bf9492
DJ
891static void add_partial_namespace (struct partial_die_info *pdi,
892 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 893 int need_pc, struct dwarf2_cu *cu);
63d06c5c 894
5d7cb8df
JK
895static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
896 CORE_ADDR *highpc, int need_pc,
897 struct dwarf2_cu *cu);
898
72bf9492
DJ
899static void add_partial_enumeration (struct partial_die_info *enum_pdi,
900 struct dwarf2_cu *cu);
91c24f0a 901
bc30ff58
JB
902static void add_partial_subprogram (struct partial_die_info *pdi,
903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 904 int need_pc, struct dwarf2_cu *cu);
bc30ff58 905
fe1b8b76 906static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
907 gdb_byte *buffer, gdb_byte *info_ptr,
908 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 909
a14ed312 910static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 911
a14ed312 912static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 913
e7c27a73 914static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 915
f3dd6933 916static void dwarf2_free_abbrev_table (void *);
c906108c 917
fe1b8b76 918static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 919 struct dwarf2_cu *);
72bf9492 920
57349743 921static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 922 struct dwarf2_cu *);
c906108c 923
93311388
DE
924static struct partial_die_info *load_partial_dies (bfd *,
925 gdb_byte *, gdb_byte *,
926 int, struct dwarf2_cu *);
72bf9492 927
fe1b8b76 928static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
929 struct abbrev_info *abbrev,
930 unsigned int, bfd *,
931 gdb_byte *, gdb_byte *,
932 struct dwarf2_cu *);
c906108c 933
c764a876 934static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 935 struct dwarf2_cu *);
72bf9492
DJ
936
937static void fixup_partial_die (struct partial_die_info *,
938 struct dwarf2_cu *);
939
fe1b8b76
JB
940static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 942
fe1b8b76
JB
943static gdb_byte *read_attribute_value (struct attribute *, unsigned,
944 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 945
fe1b8b76 946static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 947
fe1b8b76 948static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 949
fe1b8b76 950static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 951
fe1b8b76 952static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 953
93311388 954static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 955
fe1b8b76 956static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 957 unsigned int *);
c906108c 958
c764a876
DE
959static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
960
961static LONGEST read_checked_initial_length_and_offset
962 (bfd *, gdb_byte *, const struct comp_unit_head *,
963 unsigned int *, unsigned int *);
613e1657 964
fe1b8b76 965static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
966 unsigned int *);
967
968static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 969
fe1b8b76 970static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 971
9b1c24c8 972static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 973
fe1b8b76
JB
974static char *read_indirect_string (bfd *, gdb_byte *,
975 const struct comp_unit_head *,
976 unsigned int *);
4bdf3d34 977
fe1b8b76 978static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 979
fe1b8b76 980static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 981
fe1b8b76 982static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 983
e142c38c 984static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 985
e142c38c
DJ
986static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987 struct dwarf2_cu *);
c906108c 988
348e048f
DE
989static struct attribute *dwarf2_attr_no_follow (struct die_info *,
990 unsigned int,
991 struct dwarf2_cu *);
992
05cf31d1
JB
993static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
994 struct dwarf2_cu *cu);
995
e142c38c 996static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 997
e142c38c 998static struct die_info *die_specification (struct die_info *die,
f2f0e013 999 struct dwarf2_cu **);
63d06c5c 1000
debd256d
JB
1001static void free_line_header (struct line_header *lh);
1002
aaa75496
JB
1003static void add_file_name (struct line_header *, char *, unsigned int,
1004 unsigned int, unsigned int);
1005
debd256d
JB
1006static struct line_header *(dwarf_decode_line_header
1007 (unsigned int offset,
e7c27a73 1008 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1009
72b9f47f 1010static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1011 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1012
72b9f47f 1013static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1014
a14ed312 1015static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1016 struct dwarf2_cu *);
c906108c 1017
34eaf542
TT
1018static struct symbol *new_symbol_full (struct die_info *, struct type *,
1019 struct dwarf2_cu *, struct symbol *);
1020
a14ed312 1021static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1022 struct dwarf2_cu *);
c906108c 1023
98bfdba5
PA
1024static void dwarf2_const_value_attr (struct attribute *attr,
1025 struct type *type,
1026 const char *name,
1027 struct obstack *obstack,
1028 struct dwarf2_cu *cu, long *value,
1029 gdb_byte **bytes,
1030 struct dwarf2_locexpr_baton **baton);
2df3850c 1031
e7c27a73 1032static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1033
b4ba55a1
JB
1034static int need_gnat_info (struct dwarf2_cu *);
1035
1036static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1037
1038static void set_descriptive_type (struct type *, struct die_info *,
1039 struct dwarf2_cu *);
1040
e7c27a73
DJ
1041static struct type *die_containing_type (struct die_info *,
1042 struct dwarf2_cu *);
c906108c 1043
673bfd45
DE
1044static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045 struct dwarf2_cu *);
c906108c 1046
f792889a 1047static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1048
673bfd45
DE
1049static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
086ed43d 1051static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1052
6e70227d 1053static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1054 const char *suffix, int physname,
1055 struct dwarf2_cu *cu);
63d06c5c 1056
e7c27a73 1057static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1058
348e048f
DE
1059static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
e7c27a73 1061static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1062
e7c27a73 1063static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1064
ff013f42
JK
1065static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066 struct dwarf2_cu *, struct partial_symtab *);
1067
a14ed312 1068static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1069 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070 struct partial_symtab *);
c906108c 1071
fae299cd
DC
1072static void get_scope_pc_bounds (struct die_info *,
1073 CORE_ADDR *, CORE_ADDR *,
1074 struct dwarf2_cu *);
1075
801e3a5b
JB
1076static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077 CORE_ADDR, struct dwarf2_cu *);
1078
a14ed312 1079static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1080 struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1083 struct type *, struct dwarf2_cu *);
c906108c 1084
a14ed312 1085static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1086 struct die_info *, struct type *,
e7c27a73 1087 struct dwarf2_cu *);
c906108c 1088
a14ed312 1089static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1090 struct type *, struct dwarf2_cu *);
c906108c 1091
134d01f1 1092static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1093
e7c27a73 1094static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1097
5d7cb8df
JK
1098static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1099
27aa8d6a
SW
1100static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1101
f55ee35c
JK
1102static struct type *read_module_type (struct die_info *die,
1103 struct dwarf2_cu *cu);
1104
38d518c9 1105static const char *namespace_name (struct die_info *die,
e142c38c 1106 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1107
134d01f1 1108static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1109
e7c27a73 1110static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1111
6e70227d 1112static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1113 struct dwarf2_cu *);
1114
93311388 1115static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1116
93311388
DE
1117static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1118 gdb_byte *info_ptr,
d97bc12b
DE
1119 gdb_byte **new_info_ptr,
1120 struct die_info *parent);
1121
93311388
DE
1122static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1123 gdb_byte *info_ptr,
fe1b8b76 1124 gdb_byte **new_info_ptr,
639d11d3
DC
1125 struct die_info *parent);
1126
93311388
DE
1127static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1128 gdb_byte *info_ptr,
fe1b8b76 1129 gdb_byte **new_info_ptr,
639d11d3
DC
1130 struct die_info *parent);
1131
93311388
DE
1132static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1133 struct die_info **, gdb_byte *,
1134 int *);
1135
e7c27a73 1136static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1137
71c25dea
TT
1138static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1139 struct obstack *);
1140
e142c38c 1141static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1142
98bfdba5
PA
1143static const char *dwarf2_full_name (char *name,
1144 struct die_info *die,
1145 struct dwarf2_cu *cu);
1146
e142c38c 1147static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1148 struct dwarf2_cu **);
9219021c 1149
a14ed312 1150static char *dwarf_tag_name (unsigned int);
c906108c 1151
a14ed312 1152static char *dwarf_attr_name (unsigned int);
c906108c 1153
a14ed312 1154static char *dwarf_form_name (unsigned int);
c906108c 1155
a14ed312 1156static char *dwarf_bool_name (unsigned int);
c906108c 1157
a14ed312 1158static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1159
1160#if 0
a14ed312 1161static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1162#endif
1163
f9aca02d 1164static struct die_info *sibling_die (struct die_info *);
c906108c 1165
d97bc12b
DE
1166static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1167
1168static void dump_die_for_error (struct die_info *);
1169
1170static void dump_die_1 (struct ui_file *, int level, int max_level,
1171 struct die_info *);
c906108c 1172
d97bc12b 1173/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1174
51545339 1175static void store_in_ref_table (struct die_info *,
10b3939b 1176 struct dwarf2_cu *);
c906108c 1177
93311388
DE
1178static int is_ref_attr (struct attribute *);
1179
c764a876 1180static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1181
43bbcdc2 1182static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1183
348e048f
DE
1184static struct die_info *follow_die_ref_or_sig (struct die_info *,
1185 struct attribute *,
1186 struct dwarf2_cu **);
1187
10b3939b
DJ
1188static struct die_info *follow_die_ref (struct die_info *,
1189 struct attribute *,
f2f0e013 1190 struct dwarf2_cu **);
c906108c 1191
348e048f
DE
1192static struct die_info *follow_die_sig (struct die_info *,
1193 struct attribute *,
1194 struct dwarf2_cu **);
1195
1196static void read_signatured_type_at_offset (struct objfile *objfile,
1197 unsigned int offset);
1198
1199static void read_signatured_type (struct objfile *,
1200 struct signatured_type *type_sig);
1201
c906108c
SS
1202/* memory allocation interface */
1203
7b5a2f43 1204static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1205
f3dd6933 1206static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1207
b60c80d6 1208static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1209
e142c38c 1210static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1211
e142c38c
DJ
1212static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1213 struct dwarf2_cu *);
5fb290d7 1214
2e276125 1215static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1216 char *, bfd *, struct dwarf2_cu *);
2e276125 1217
8e19ed76
PS
1218static int attr_form_is_block (struct attribute *);
1219
3690dd37
JB
1220static int attr_form_is_section_offset (struct attribute *);
1221
1222static int attr_form_is_constant (struct attribute *);
1223
8cf6f0b1
TT
1224static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1225 struct dwarf2_loclist_baton *baton,
1226 struct attribute *attr);
1227
93e7bd98
DJ
1228static void dwarf2_symbol_mark_computed (struct attribute *attr,
1229 struct symbol *sym,
1230 struct dwarf2_cu *cu);
4c2df51b 1231
93311388
DE
1232static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1233 struct abbrev_info *abbrev,
1234 struct dwarf2_cu *cu);
4bb7a0a7 1235
72bf9492
DJ
1236static void free_stack_comp_unit (void *);
1237
72bf9492
DJ
1238static hashval_t partial_die_hash (const void *item);
1239
1240static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1241
ae038cb0 1242static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1243 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1244
1245static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1246 (unsigned int offset, struct objfile *objfile);
ae038cb0 1247
9816fde3
JK
1248static void init_one_comp_unit (struct dwarf2_cu *cu,
1249 struct objfile *objfile);
1250
1251static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1252 struct die_info *comp_unit_die);
93311388 1253
ae038cb0
DJ
1254static void free_one_comp_unit (void *);
1255
1256static void free_cached_comp_units (void *);
1257
1258static void age_cached_comp_units (void);
1259
1260static void free_one_cached_comp_unit (void *);
1261
f792889a
DJ
1262static struct type *set_die_type (struct die_info *, struct type *,
1263 struct dwarf2_cu *);
1c379e20 1264
ae038cb0
DJ
1265static void create_all_comp_units (struct objfile *);
1266
1fd400ff
TT
1267static int create_debug_types_hash_table (struct objfile *objfile);
1268
93311388
DE
1269static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1270 struct objfile *);
10b3939b
DJ
1271
1272static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1273
1274static void dwarf2_add_dependence (struct dwarf2_cu *,
1275 struct dwarf2_per_cu_data *);
1276
ae038cb0
DJ
1277static void dwarf2_mark (struct dwarf2_cu *);
1278
1279static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1280
673bfd45
DE
1281static struct type *get_die_type_at_offset (unsigned int,
1282 struct dwarf2_per_cu_data *per_cu);
1283
f792889a 1284static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1285
9291a0cd
TT
1286static void dwarf2_release_queue (void *dummy);
1287
1288static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1289 struct objfile *objfile);
1290
1291static void process_queue (struct objfile *objfile);
1292
1293static void find_file_and_directory (struct die_info *die,
1294 struct dwarf2_cu *cu,
1295 char **name, char **comp_dir);
1296
1297static char *file_full_name (int file, struct line_header *lh,
1298 const char *comp_dir);
1299
1300static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1301 gdb_byte *info_ptr,
1302 gdb_byte *buffer,
1303 unsigned int buffer_size,
1304 bfd *abfd);
1305
1306static void init_cu_die_reader (struct die_reader_specs *reader,
1307 struct dwarf2_cu *cu);
1308
673bfd45 1309static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1310
9291a0cd
TT
1311#if WORDS_BIGENDIAN
1312
1313/* Convert VALUE between big- and little-endian. */
1314static offset_type
1315byte_swap (offset_type value)
1316{
1317 offset_type result;
1318
1319 result = (value & 0xff) << 24;
1320 result |= (value & 0xff00) << 8;
1321 result |= (value & 0xff0000) >> 8;
1322 result |= (value & 0xff000000) >> 24;
1323 return result;
1324}
1325
1326#define MAYBE_SWAP(V) byte_swap (V)
1327
1328#else
1329#define MAYBE_SWAP(V) (V)
1330#endif /* WORDS_BIGENDIAN */
1331
1332/* The suffix for an index file. */
1333#define INDEX_SUFFIX ".gdb-index"
1334
3da10d80
KS
1335static const char *dwarf2_physname (char *name, struct die_info *die,
1336 struct dwarf2_cu *cu);
1337
c906108c
SS
1338/* Try to locate the sections we need for DWARF 2 debugging
1339 information and return true if we have enough to do something. */
1340
1341int
6502dd73 1342dwarf2_has_info (struct objfile *objfile)
c906108c 1343{
be391dca
TT
1344 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1345 if (!dwarf2_per_objfile)
1346 {
1347 /* Initialize per-objfile state. */
1348 struct dwarf2_per_objfile *data
1349 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1350
be391dca
TT
1351 memset (data, 0, sizeof (*data));
1352 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1353 dwarf2_per_objfile = data;
6502dd73 1354
be391dca
TT
1355 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1356 dwarf2_per_objfile->objfile = objfile;
1357 }
1358 return (dwarf2_per_objfile->info.asection != NULL
1359 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1360}
1361
233a11ab
CS
1362/* When loading sections, we can either look for ".<name>", or for
1363 * ".z<name>", which indicates a compressed section. */
1364
1365static int
dce234bc 1366section_is_p (const char *section_name, const char *name)
233a11ab 1367{
dce234bc
PP
1368 return (section_name[0] == '.'
1369 && (strcmp (section_name + 1, name) == 0
1370 || (section_name[1] == 'z'
1371 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1372}
1373
c906108c
SS
1374/* This function is mapped across the sections and remembers the
1375 offset and size of each of the debugging sections we are interested
1376 in. */
1377
1378static void
72dca2f5 1379dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1380{
dce234bc 1381 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1382 {
dce234bc
PP
1383 dwarf2_per_objfile->info.asection = sectp;
1384 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1385 }
dce234bc 1386 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1387 {
dce234bc
PP
1388 dwarf2_per_objfile->abbrev.asection = sectp;
1389 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1390 }
dce234bc 1391 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1392 {
dce234bc
PP
1393 dwarf2_per_objfile->line.asection = sectp;
1394 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1395 }
dce234bc 1396 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1397 {
dce234bc
PP
1398 dwarf2_per_objfile->loc.asection = sectp;
1399 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1400 }
dce234bc 1401 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1402 {
dce234bc
PP
1403 dwarf2_per_objfile->macinfo.asection = sectp;
1404 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1405 }
dce234bc 1406 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1407 {
dce234bc
PP
1408 dwarf2_per_objfile->str.asection = sectp;
1409 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1410 }
dce234bc 1411 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1412 {
dce234bc
PP
1413 dwarf2_per_objfile->frame.asection = sectp;
1414 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1415 }
dce234bc 1416 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1417 {
3799ccc6 1418 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1419
3799ccc6
EZ
1420 if (aflag & SEC_HAS_CONTENTS)
1421 {
dce234bc
PP
1422 dwarf2_per_objfile->eh_frame.asection = sectp;
1423 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1424 }
b6af0555 1425 }
dce234bc 1426 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1427 {
dce234bc
PP
1428 dwarf2_per_objfile->ranges.asection = sectp;
1429 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1430 }
348e048f
DE
1431 else if (section_is_p (sectp->name, TYPES_SECTION))
1432 {
1433 dwarf2_per_objfile->types.asection = sectp;
1434 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1435 }
9291a0cd
TT
1436 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1437 {
1438 dwarf2_per_objfile->gdb_index.asection = sectp;
1439 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1440 }
dce234bc 1441
72dca2f5
FR
1442 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1443 && bfd_section_vma (abfd, sectp) == 0)
1444 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1445}
1446
dce234bc
PP
1447/* Decompress a section that was compressed using zlib. Store the
1448 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1449
1450static void
dce234bc
PP
1451zlib_decompress_section (struct objfile *objfile, asection *sectp,
1452 gdb_byte **outbuf, bfd_size_type *outsize)
1453{
1454 bfd *abfd = objfile->obfd;
1455#ifndef HAVE_ZLIB_H
1456 error (_("Support for zlib-compressed DWARF data (from '%s') "
1457 "is disabled in this copy of GDB"),
1458 bfd_get_filename (abfd));
1459#else
1460 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1461 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1462 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1463 bfd_size_type uncompressed_size;
1464 gdb_byte *uncompressed_buffer;
1465 z_stream strm;
1466 int rc;
1467 int header_size = 12;
1468
1469 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1470 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1471 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1472 bfd_get_filename (abfd));
1473
1474 /* Read the zlib header. In this case, it should be "ZLIB" followed
1475 by the uncompressed section size, 8 bytes in big-endian order. */
1476 if (compressed_size < header_size
1477 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1478 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1479 bfd_get_filename (abfd));
1480 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1481 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[11];
1488
1489 /* It is possible the section consists of several compressed
1490 buffers concatenated together, so we uncompress in a loop. */
1491 strm.zalloc = NULL;
1492 strm.zfree = NULL;
1493 strm.opaque = NULL;
1494 strm.avail_in = compressed_size - header_size;
1495 strm.next_in = (Bytef*) compressed_buffer + header_size;
1496 strm.avail_out = uncompressed_size;
1497 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1498 uncompressed_size);
1499 rc = inflateInit (&strm);
1500 while (strm.avail_in > 0)
1501 {
1502 if (rc != Z_OK)
1503 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1504 bfd_get_filename (abfd), rc);
1505 strm.next_out = ((Bytef*) uncompressed_buffer
1506 + (uncompressed_size - strm.avail_out));
1507 rc = inflate (&strm, Z_FINISH);
1508 if (rc != Z_STREAM_END)
1509 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 rc = inflateReset (&strm);
1512 }
1513 rc = inflateEnd (&strm);
1514 if (rc != Z_OK
1515 || strm.avail_out != 0)
1516 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1517 bfd_get_filename (abfd), rc);
1518
affddf13 1519 do_cleanups (cleanup);
dce234bc
PP
1520 *outbuf = uncompressed_buffer;
1521 *outsize = uncompressed_size;
1522#endif
233a11ab
CS
1523}
1524
dce234bc
PP
1525/* Read the contents of the section SECTP from object file specified by
1526 OBJFILE, store info about the section into INFO.
1527 If the section is compressed, uncompress it before returning. */
c906108c 1528
dce234bc
PP
1529static void
1530dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1531{
dce234bc
PP
1532 bfd *abfd = objfile->obfd;
1533 asection *sectp = info->asection;
1534 gdb_byte *buf, *retbuf;
1535 unsigned char header[4];
c906108c 1536
be391dca
TT
1537 if (info->readin)
1538 return;
dce234bc
PP
1539 info->buffer = NULL;
1540 info->was_mmapped = 0;
be391dca 1541 info->readin = 1;
188dd5d6 1542
dce234bc
PP
1543 if (info->asection == NULL || info->size == 0)
1544 return;
c906108c 1545
dce234bc
PP
1546 /* Check if the file has a 4-byte header indicating compression. */
1547 if (info->size > sizeof (header)
1548 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1549 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1550 {
1551 /* Upon decompression, update the buffer and its size. */
1552 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1553 {
1554 zlib_decompress_section (objfile, sectp, &info->buffer,
1555 &info->size);
1556 return;
1557 }
1558 }
4bdf3d34 1559
dce234bc
PP
1560#ifdef HAVE_MMAP
1561 if (pagesize == 0)
1562 pagesize = getpagesize ();
2e276125 1563
dce234bc
PP
1564 /* Only try to mmap sections which are large enough: we don't want to
1565 waste space due to fragmentation. Also, only try mmap for sections
1566 without relocations. */
1567
1568 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1569 {
1570 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1571 size_t map_length = info->size + sectp->filepos - pg_offset;
1572 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1573 MAP_PRIVATE, pg_offset);
1574
1575 if (retbuf != MAP_FAILED)
1576 {
1577 info->was_mmapped = 1;
1578 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1579#if HAVE_POSIX_MADVISE
1580 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1581#endif
dce234bc
PP
1582 return;
1583 }
1584 }
1585#endif
1586
1587 /* If we get here, we are a normal, not-compressed section. */
1588 info->buffer = buf
1589 = obstack_alloc (&objfile->objfile_obstack, info->size);
1590
1591 /* When debugging .o files, we may need to apply relocations; see
1592 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1593 We never compress sections in .o files, so we only need to
1594 try this when the section is not compressed. */
ac8035ab 1595 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1596 if (retbuf != NULL)
1597 {
1598 info->buffer = retbuf;
1599 return;
1600 }
1601
1602 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1603 || bfd_bread (buf, info->size, abfd) != info->size)
1604 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1605 bfd_get_filename (abfd));
1606}
1607
1608/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1609 SECTION_NAME. */
af34e669 1610
dce234bc
PP
1611void
1612dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1613 asection **sectp, gdb_byte **bufp,
1614 bfd_size_type *sizep)
1615{
1616 struct dwarf2_per_objfile *data
1617 = objfile_data (objfile, dwarf2_objfile_data_key);
1618 struct dwarf2_section_info *info;
a3b2a86b
TT
1619
1620 /* We may see an objfile without any DWARF, in which case we just
1621 return nothing. */
1622 if (data == NULL)
1623 {
1624 *sectp = NULL;
1625 *bufp = NULL;
1626 *sizep = 0;
1627 return;
1628 }
dce234bc
PP
1629 if (section_is_p (section_name, EH_FRAME_SECTION))
1630 info = &data->eh_frame;
1631 else if (section_is_p (section_name, FRAME_SECTION))
1632 info = &data->frame;
0d53c4c4 1633 else
f3574227 1634 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1635
1636 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1637 /* We haven't read this section in yet. Do it now. */
1638 dwarf2_read_section (objfile, info);
1639
1640 *sectp = info->asection;
1641 *bufp = info->buffer;
1642 *sizep = info->size;
1643}
1644
9291a0cd
TT
1645\f
1646
1647/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1648 this CU came. */
2fdf6df6 1649
9291a0cd
TT
1650static void
1651dw2_do_instantiate_symtab (struct objfile *objfile,
1652 struct dwarf2_per_cu_data *per_cu)
1653{
1654 struct cleanup *back_to;
1655
1656 back_to = make_cleanup (dwarf2_release_queue, NULL);
1657
1658 queue_comp_unit (per_cu, objfile);
1659
1660 if (per_cu->from_debug_types)
1661 read_signatured_type_at_offset (objfile, per_cu->offset);
1662 else
1663 load_full_comp_unit (per_cu, objfile);
1664
1665 process_queue (objfile);
1666
1667 /* Age the cache, releasing compilation units that have not
1668 been used recently. */
1669 age_cached_comp_units ();
1670
1671 do_cleanups (back_to);
1672}
1673
1674/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1675 the objfile from which this CU came. Returns the resulting symbol
1676 table. */
2fdf6df6 1677
9291a0cd
TT
1678static struct symtab *
1679dw2_instantiate_symtab (struct objfile *objfile,
1680 struct dwarf2_per_cu_data *per_cu)
1681{
1682 if (!per_cu->v.quick->symtab)
1683 {
1684 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1685 increment_reading_symtab ();
1686 dw2_do_instantiate_symtab (objfile, per_cu);
1687 do_cleanups (back_to);
1688 }
1689 return per_cu->v.quick->symtab;
1690}
1691
1fd400ff 1692/* Return the CU given its index. */
2fdf6df6 1693
1fd400ff
TT
1694static struct dwarf2_per_cu_data *
1695dw2_get_cu (int index)
1696{
1697 if (index >= dwarf2_per_objfile->n_comp_units)
1698 {
1699 index -= dwarf2_per_objfile->n_comp_units;
1700 return dwarf2_per_objfile->type_comp_units[index];
1701 }
1702 return dwarf2_per_objfile->all_comp_units[index];
1703}
1704
9291a0cd
TT
1705/* A helper function that knows how to read a 64-bit value in a way
1706 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1707 otherwise. */
2fdf6df6 1708
9291a0cd
TT
1709static int
1710extract_cu_value (const char *bytes, ULONGEST *result)
1711{
1712 if (sizeof (ULONGEST) < 8)
1713 {
1714 int i;
1715
1716 /* Ignore the upper 4 bytes if they are all zero. */
1717 for (i = 0; i < 4; ++i)
1718 if (bytes[i + 4] != 0)
1719 return 0;
1720
1721 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1722 }
1723 else
1724 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1725 return 1;
1726}
1727
1728/* Read the CU list from the mapped index, and use it to create all
1729 the CU objects for this objfile. Return 0 if something went wrong,
1730 1 if everything went ok. */
2fdf6df6 1731
9291a0cd 1732static int
1fd400ff
TT
1733create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1734 offset_type cu_list_elements)
9291a0cd
TT
1735{
1736 offset_type i;
9291a0cd
TT
1737
1738 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1739 dwarf2_per_objfile->all_comp_units
1740 = obstack_alloc (&objfile->objfile_obstack,
1741 dwarf2_per_objfile->n_comp_units
1742 * sizeof (struct dwarf2_per_cu_data *));
1743
1744 for (i = 0; i < cu_list_elements; i += 2)
1745 {
1746 struct dwarf2_per_cu_data *the_cu;
1747 ULONGEST offset, length;
1748
1749 if (!extract_cu_value (cu_list, &offset)
1750 || !extract_cu_value (cu_list + 8, &length))
1751 return 0;
1752 cu_list += 2 * 8;
1753
1754 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1755 struct dwarf2_per_cu_data);
1756 the_cu->offset = offset;
1757 the_cu->length = length;
1758 the_cu->objfile = objfile;
1759 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1760 struct dwarf2_per_cu_quick_data);
1761 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1762 }
1763
1764 return 1;
1765}
1766
1fd400ff 1767/* Create the signatured type hash table from the index. */
673bfd45 1768
1fd400ff 1769static int
673bfd45
DE
1770create_signatured_type_table_from_index (struct objfile *objfile,
1771 const gdb_byte *bytes,
1772 offset_type elements)
1fd400ff
TT
1773{
1774 offset_type i;
673bfd45 1775 htab_t sig_types_hash;
1fd400ff
TT
1776
1777 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1778 dwarf2_per_objfile->type_comp_units
1779 = obstack_alloc (&objfile->objfile_obstack,
1780 dwarf2_per_objfile->n_type_comp_units
1781 * sizeof (struct dwarf2_per_cu_data *));
1782
673bfd45 1783 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1784
1785 for (i = 0; i < elements; i += 3)
1786 {
1787 struct signatured_type *type_sig;
1788 ULONGEST offset, type_offset, signature;
1789 void **slot;
1790
1791 if (!extract_cu_value (bytes, &offset)
1792 || !extract_cu_value (bytes + 8, &type_offset))
1793 return 0;
1794 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1795 bytes += 3 * 8;
1796
1797 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1798 struct signatured_type);
1799 type_sig->signature = signature;
1800 type_sig->offset = offset;
1801 type_sig->type_offset = type_offset;
1802 type_sig->per_cu.from_debug_types = 1;
1803 type_sig->per_cu.offset = offset;
1804 type_sig->per_cu.objfile = objfile;
1805 type_sig->per_cu.v.quick
1806 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1807 struct dwarf2_per_cu_quick_data);
1808
673bfd45 1809 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1810 *slot = type_sig;
1811
1812 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1813 }
1814
673bfd45 1815 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1816
1817 return 1;
1818}
1819
9291a0cd
TT
1820/* Read the address map data from the mapped index, and use it to
1821 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1822
9291a0cd
TT
1823static void
1824create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1825{
1826 const gdb_byte *iter, *end;
1827 struct obstack temp_obstack;
1828 struct addrmap *mutable_map;
1829 struct cleanup *cleanup;
1830 CORE_ADDR baseaddr;
1831
1832 obstack_init (&temp_obstack);
1833 cleanup = make_cleanup_obstack_free (&temp_obstack);
1834 mutable_map = addrmap_create_mutable (&temp_obstack);
1835
1836 iter = index->address_table;
1837 end = iter + index->address_table_size;
1838
1839 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1840
1841 while (iter < end)
1842 {
1843 ULONGEST hi, lo, cu_index;
1844 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1845 iter += 8;
1846 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1847 iter += 8;
1848 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1849 iter += 4;
1850
1851 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1852 dw2_get_cu (cu_index));
9291a0cd
TT
1853 }
1854
1855 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1856 &objfile->objfile_obstack);
1857 do_cleanups (cleanup);
1858}
1859
1860/* The hash function for strings in the mapped index. This is the
1861 same as the hashtab.c hash function, but we keep a separate copy to
1862 maintain control over the implementation. This is necessary
1863 because the hash function is tied to the format of the mapped index
1864 file. */
2fdf6df6 1865
9291a0cd
TT
1866static hashval_t
1867mapped_index_string_hash (const void *p)
1868{
1869 const unsigned char *str = (const unsigned char *) p;
1870 hashval_t r = 0;
1871 unsigned char c;
1872
1873 while ((c = *str++) != 0)
1874 r = r * 67 + c - 113;
1875
1876 return r;
1877}
1878
1879/* Find a slot in the mapped index INDEX for the object named NAME.
1880 If NAME is found, set *VEC_OUT to point to the CU vector in the
1881 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1882
9291a0cd
TT
1883static int
1884find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1885 offset_type **vec_out)
1886{
1887 offset_type hash = mapped_index_string_hash (name);
1888 offset_type slot, step;
1889
3876f04e
DE
1890 slot = hash & (index->symbol_table_slots - 1);
1891 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1892
1893 for (;;)
1894 {
1895 /* Convert a slot number to an offset into the table. */
1896 offset_type i = 2 * slot;
1897 const char *str;
3876f04e 1898 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1899 return 0;
1900
3876f04e 1901 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1902 if (!strcmp (name, str))
1903 {
1904 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1905 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1906 return 1;
1907 }
1908
3876f04e 1909 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1910 }
1911}
1912
1913/* Read the index file. If everything went ok, initialize the "quick"
1914 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1915
9291a0cd
TT
1916static int
1917dwarf2_read_index (struct objfile *objfile)
1918{
9291a0cd
TT
1919 char *addr;
1920 struct mapped_index *map;
b3b272e1 1921 offset_type *metadata;
ac0b195c
KW
1922 const gdb_byte *cu_list;
1923 const gdb_byte *types_list = NULL;
1924 offset_type version, cu_list_elements;
1925 offset_type types_list_elements = 0;
1fd400ff 1926 int i;
9291a0cd
TT
1927
1928 if (dwarf2_per_objfile->gdb_index.asection == NULL
1929 || dwarf2_per_objfile->gdb_index.size == 0)
1930 return 0;
82430852
JK
1931
1932 /* Older elfutils strip versions could keep the section in the main
1933 executable while splitting it for the separate debug info file. */
1934 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1935 & SEC_HAS_CONTENTS) == 0)
1936 return 0;
1937
9291a0cd
TT
1938 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1939
1940 addr = dwarf2_per_objfile->gdb_index.buffer;
1941 /* Version check. */
1fd400ff 1942 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
1943 /* Versions earlier than 3 emitted every copy of a psymbol. This
1944 causes the index to behave very poorly for certain requests. So,
1945 it seems better to just ignore such indices. */
1946 if (version < 3)
9291a0cd 1947 return 0;
594e8718
JK
1948 /* Indexes with higher version than the one supported by GDB may be no
1949 longer backward compatible. */
1950 if (version > 3)
1951 return 0;
9291a0cd
TT
1952
1953 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1954 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1955
1956 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1957
1958 i = 0;
1959 cu_list = addr + MAYBE_SWAP (metadata[i]);
1960 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1961 / 8);
1fd400ff
TT
1962 ++i;
1963
987d643c
TT
1964 types_list = addr + MAYBE_SWAP (metadata[i]);
1965 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1966 - MAYBE_SWAP (metadata[i]))
1967 / 8);
1968 ++i;
1fd400ff
TT
1969
1970 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1971 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1972 - MAYBE_SWAP (metadata[i]));
1973 ++i;
1974
3876f04e
DE
1975 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1976 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1977 - MAYBE_SWAP (metadata[i]))
1978 / (2 * sizeof (offset_type)));
1fd400ff 1979 ++i;
9291a0cd 1980
1fd400ff
TT
1981 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1982
1983 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1984 return 0;
1985
987d643c 1986 if (types_list_elements
673bfd45
DE
1987 && !create_signatured_type_table_from_index (objfile, types_list,
1988 types_list_elements))
9291a0cd
TT
1989 return 0;
1990
1991 create_addrmap_from_index (objfile, map);
1992
1993 dwarf2_per_objfile->index_table = map;
1994 dwarf2_per_objfile->using_index = 1;
1995
1996 return 1;
1997}
1998
1999/* A helper for the "quick" functions which sets the global
2000 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2001
9291a0cd
TT
2002static void
2003dw2_setup (struct objfile *objfile)
2004{
2005 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2006 gdb_assert (dwarf2_per_objfile);
2007}
2008
2009/* A helper for the "quick" functions which attempts to read the line
2010 table for THIS_CU. */
2fdf6df6 2011
9291a0cd
TT
2012static void
2013dw2_require_line_header (struct objfile *objfile,
2014 struct dwarf2_per_cu_data *this_cu)
2015{
2016 bfd *abfd = objfile->obfd;
2017 struct line_header *lh = NULL;
2018 struct attribute *attr;
2019 struct cleanup *cleanups;
2020 struct die_info *comp_unit_die;
36374493 2021 struct dwarf2_section_info* sec;
9291a0cd
TT
2022 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2023 int has_children, i;
2024 struct dwarf2_cu cu;
2025 unsigned int bytes_read, buffer_size;
2026 struct die_reader_specs reader_specs;
2027 char *name, *comp_dir;
2028
2029 if (this_cu->v.quick->read_lines)
2030 return;
2031 this_cu->v.quick->read_lines = 1;
2032
9816fde3 2033 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2034 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2035
36374493
DE
2036 if (this_cu->from_debug_types)
2037 sec = &dwarf2_per_objfile->types;
2038 else
2039 sec = &dwarf2_per_objfile->info;
2040 dwarf2_read_section (objfile, sec);
2041 buffer_size = sec->size;
2042 buffer = sec->buffer;
9291a0cd
TT
2043 info_ptr = buffer + this_cu->offset;
2044 beg_of_comp_unit = info_ptr;
2045
2046 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2047 buffer, buffer_size,
2048 abfd);
2049
2050 /* Complete the cu_header. */
2051 cu.header.offset = beg_of_comp_unit - buffer;
2052 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2053
2054 this_cu->cu = &cu;
2055 cu.per_cu = this_cu;
2056
2057 dwarf2_read_abbrevs (abfd, &cu);
2058 make_cleanup (dwarf2_free_abbrev_table, &cu);
2059
2060 if (this_cu->from_debug_types)
2061 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2062 init_cu_die_reader (&reader_specs, &cu);
2063 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2064 &has_children);
2065
2066 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2067 if (attr)
2068 {
2069 unsigned int line_offset = DW_UNSND (attr);
2070 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2071 }
2072 if (lh == NULL)
2073 {
2074 do_cleanups (cleanups);
2075 return;
2076 }
2077
2078 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2079
2080 this_cu->v.quick->lines = lh;
2081
2082 this_cu->v.quick->file_names
2083 = obstack_alloc (&objfile->objfile_obstack,
2084 lh->num_file_names * sizeof (char *));
2085 for (i = 0; i < lh->num_file_names; ++i)
2086 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2087
2088 do_cleanups (cleanups);
2089}
2090
2091/* A helper for the "quick" functions which computes and caches the
2092 real path for a given file name from the line table.
2093 dw2_require_line_header must have been called before this is
2094 invoked. */
2fdf6df6 2095
9291a0cd
TT
2096static const char *
2097dw2_require_full_path (struct objfile *objfile,
e254ef6a 2098 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2099 int index)
2100{
e254ef6a
DE
2101 if (!per_cu->v.quick->full_names)
2102 per_cu->v.quick->full_names
9291a0cd 2103 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2104 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2105 sizeof (char *));
2106
e254ef6a
DE
2107 if (!per_cu->v.quick->full_names[index])
2108 per_cu->v.quick->full_names[index]
2109 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2110
e254ef6a 2111 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2112}
2113
2114static struct symtab *
2115dw2_find_last_source_symtab (struct objfile *objfile)
2116{
2117 int index;
2118 dw2_setup (objfile);
2119 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2120 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2121}
2122
2123static void
2124dw2_forget_cached_source_info (struct objfile *objfile)
2125{
2126 int i;
2127
2128 dw2_setup (objfile);
1fd400ff
TT
2129 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2130 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2131 {
e254ef6a 2132 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2133
e254ef6a 2134 if (per_cu->v.quick->full_names)
9291a0cd
TT
2135 {
2136 int j;
2137
e254ef6a 2138 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
87df528e
JK
2139 {
2140 xfree ((void *) per_cu->v.quick->full_names[j]);
2141 per_cu->v.quick->full_names[j] = NULL;
2142 }
9291a0cd
TT
2143 }
2144 }
2145}
2146
2147static int
2148dw2_lookup_symtab (struct objfile *objfile, const char *name,
2149 const char *full_path, const char *real_path,
2150 struct symtab **result)
2151{
2152 int i;
2153 int check_basename = lbasename (name) == name;
2154 struct dwarf2_per_cu_data *base_cu = NULL;
2155
2156 dw2_setup (objfile);
1fd400ff
TT
2157 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2158 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2159 {
2160 int j;
e254ef6a 2161 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2162
e254ef6a 2163 if (per_cu->v.quick->symtab)
9291a0cd
TT
2164 continue;
2165
e254ef6a
DE
2166 dw2_require_line_header (objfile, per_cu);
2167 if (!per_cu->v.quick->lines)
9291a0cd
TT
2168 continue;
2169
e254ef6a 2170 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2171 {
e254ef6a 2172 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2173
2174 if (FILENAME_CMP (name, this_name) == 0)
2175 {
e254ef6a 2176 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2177 return 1;
2178 }
2179
2180 if (check_basename && ! base_cu
2181 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2182 base_cu = per_cu;
9291a0cd
TT
2183
2184 if (full_path != NULL)
2185 {
2186 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2187 per_cu, j);
9291a0cd
TT
2188
2189 if (this_full_name
2190 && FILENAME_CMP (full_path, this_full_name) == 0)
2191 {
e254ef6a 2192 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2193 return 1;
2194 }
2195 }
2196
2197 if (real_path != NULL)
2198 {
2199 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2200 per_cu, j);
9291a0cd 2201
74dd2ca6
DE
2202 if (this_full_name != NULL
2203 && FILENAME_CMP (real_path, this_full_name) == 0)
9291a0cd 2204 {
74dd2ca6
DE
2205 *result = dw2_instantiate_symtab (objfile, per_cu);
2206 return 1;
9291a0cd
TT
2207 }
2208 }
2209 }
2210 }
2211
2212 if (base_cu)
2213 {
2214 *result = dw2_instantiate_symtab (objfile, base_cu);
2215 return 1;
2216 }
2217
2218 return 0;
2219}
2220
2221static struct symtab *
2222dw2_lookup_symbol (struct objfile *objfile, int block_index,
2223 const char *name, domain_enum domain)
2224{
774b6a14 2225 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2226 instead. */
2227 return NULL;
2228}
2229
2230/* A helper function that expands all symtabs that hold an object
2231 named NAME. */
2fdf6df6 2232
9291a0cd
TT
2233static void
2234dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2235{
2236 dw2_setup (objfile);
2237
2238 if (dwarf2_per_objfile->index_table)
2239 {
2240 offset_type *vec;
2241
2242 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2243 name, &vec))
2244 {
2245 offset_type i, len = MAYBE_SWAP (*vec);
2246 for (i = 0; i < len; ++i)
2247 {
2248 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2249 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2250
e254ef6a 2251 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2252 }
2253 }
2254 }
2255}
2256
774b6a14
TT
2257static void
2258dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2259 int kind, const char *name,
2260 domain_enum domain)
9291a0cd 2261{
774b6a14 2262 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2263}
2264
2265static void
2266dw2_print_stats (struct objfile *objfile)
2267{
2268 int i, count;
2269
2270 dw2_setup (objfile);
2271 count = 0;
1fd400ff
TT
2272 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2273 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2274 {
e254ef6a 2275 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2276
e254ef6a 2277 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2278 ++count;
2279 }
2280 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2281}
2282
2283static void
2284dw2_dump (struct objfile *objfile)
2285{
2286 /* Nothing worth printing. */
2287}
2288
2289static void
2290dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2291 struct section_offsets *delta)
2292{
2293 /* There's nothing to relocate here. */
2294}
2295
2296static void
2297dw2_expand_symtabs_for_function (struct objfile *objfile,
2298 const char *func_name)
2299{
2300 dw2_do_expand_symtabs_matching (objfile, func_name);
2301}
2302
2303static void
2304dw2_expand_all_symtabs (struct objfile *objfile)
2305{
2306 int i;
2307
2308 dw2_setup (objfile);
1fd400ff
TT
2309
2310 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2311 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2312 {
e254ef6a 2313 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2314
e254ef6a 2315 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2316 }
2317}
2318
2319static void
2320dw2_expand_symtabs_with_filename (struct objfile *objfile,
2321 const char *filename)
2322{
2323 int i;
2324
2325 dw2_setup (objfile);
1fd400ff
TT
2326 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2327 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2328 {
2329 int j;
e254ef6a 2330 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2331
e254ef6a 2332 if (per_cu->v.quick->symtab)
9291a0cd
TT
2333 continue;
2334
e254ef6a
DE
2335 dw2_require_line_header (objfile, per_cu);
2336 if (!per_cu->v.quick->lines)
9291a0cd
TT
2337 continue;
2338
e254ef6a 2339 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2340 {
e254ef6a 2341 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2342 if (strcmp (this_name, filename) == 0)
2343 {
e254ef6a 2344 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2345 break;
2346 }
2347 }
2348 }
2349}
2350
dd786858 2351static const char *
9291a0cd
TT
2352dw2_find_symbol_file (struct objfile *objfile, const char *name)
2353{
e254ef6a 2354 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2355 offset_type *vec;
2356
2357 dw2_setup (objfile);
2358
2359 if (!dwarf2_per_objfile->index_table)
2360 return NULL;
2361
2362 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2363 name, &vec))
2364 return NULL;
2365
2366 /* Note that this just looks at the very first one named NAME -- but
2367 actually we are looking for a function. find_main_filename
2368 should be rewritten so that it doesn't require a custom hook. It
2369 could just use the ordinary symbol tables. */
2370 /* vec[0] is the length, which must always be >0. */
e254ef6a 2371 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2372
e254ef6a
DE
2373 dw2_require_line_header (objfile, per_cu);
2374 if (!per_cu->v.quick->lines)
9291a0cd
TT
2375 return NULL;
2376
e254ef6a 2377 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2378}
2379
2380static void
40658b94
PH
2381dw2_map_matching_symbols (const char * name, domain_enum namespace,
2382 struct objfile *objfile, int global,
2383 int (*callback) (struct block *,
2384 struct symbol *, void *),
2edb89d3
JK
2385 void *data, symbol_compare_ftype *match,
2386 symbol_compare_ftype *ordered_compare)
9291a0cd 2387{
40658b94 2388 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2389 current language is Ada for a non-Ada objfile using GNU index. As Ada
2390 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2391}
2392
2393static void
2394dw2_expand_symtabs_matching (struct objfile *objfile,
2395 int (*file_matcher) (const char *, void *),
2396 int (*name_matcher) (const char *, void *),
2397 domain_enum kind,
2398 void *data)
2399{
2400 int i;
2401 offset_type iter;
4b5246aa 2402 struct mapped_index *index;
9291a0cd
TT
2403
2404 dw2_setup (objfile);
2405 if (!dwarf2_per_objfile->index_table)
2406 return;
4b5246aa 2407 index = dwarf2_per_objfile->index_table;
9291a0cd 2408
1fd400ff
TT
2409 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2410 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2411 {
2412 int j;
e254ef6a 2413 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2414
e254ef6a
DE
2415 per_cu->v.quick->mark = 0;
2416 if (per_cu->v.quick->symtab)
9291a0cd
TT
2417 continue;
2418
e254ef6a
DE
2419 dw2_require_line_header (objfile, per_cu);
2420 if (!per_cu->v.quick->lines)
9291a0cd
TT
2421 continue;
2422
e254ef6a 2423 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2424 {
e254ef6a 2425 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2426 {
e254ef6a 2427 per_cu->v.quick->mark = 1;
9291a0cd
TT
2428 break;
2429 }
2430 }
2431 }
2432
3876f04e 2433 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2434 {
2435 offset_type idx = 2 * iter;
2436 const char *name;
2437 offset_type *vec, vec_len, vec_idx;
2438
3876f04e 2439 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2440 continue;
2441
3876f04e 2442 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2443
2444 if (! (*name_matcher) (name, data))
2445 continue;
2446
2447 /* The name was matched, now expand corresponding CUs that were
2448 marked. */
4b5246aa 2449 vec = (offset_type *) (index->constant_pool
3876f04e 2450 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2451 vec_len = MAYBE_SWAP (vec[0]);
2452 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2453 {
e254ef6a 2454 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2455
e254ef6a
DE
2456 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2457 if (per_cu->v.quick->mark)
2458 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2459 }
2460 }
2461}
2462
2463static struct symtab *
2464dw2_find_pc_sect_symtab (struct objfile *objfile,
2465 struct minimal_symbol *msymbol,
2466 CORE_ADDR pc,
2467 struct obj_section *section,
2468 int warn_if_readin)
2469{
2470 struct dwarf2_per_cu_data *data;
2471
2472 dw2_setup (objfile);
2473
2474 if (!objfile->psymtabs_addrmap)
2475 return NULL;
2476
2477 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2478 if (!data)
2479 return NULL;
2480
2481 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2482 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2483 paddress (get_objfile_arch (objfile), pc));
2484
2485 return dw2_instantiate_symtab (objfile, data);
2486}
2487
2488static void
2489dw2_map_symbol_names (struct objfile *objfile,
2490 void (*fun) (const char *, void *),
2491 void *data)
2492{
2493 offset_type iter;
4b5246aa
TT
2494 struct mapped_index *index;
2495
9291a0cd
TT
2496 dw2_setup (objfile);
2497
2498 if (!dwarf2_per_objfile->index_table)
2499 return;
4b5246aa 2500 index = dwarf2_per_objfile->index_table;
9291a0cd 2501
3876f04e 2502 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2503 {
2504 offset_type idx = 2 * iter;
2505 const char *name;
2506 offset_type *vec, vec_len, vec_idx;
2507
3876f04e 2508 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2509 continue;
2510
3876f04e 2511 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2512
2513 (*fun) (name, data);
2514 }
2515}
2516
2517static void
2518dw2_map_symbol_filenames (struct objfile *objfile,
2519 void (*fun) (const char *, const char *, void *),
2520 void *data)
2521{
2522 int i;
2523
2524 dw2_setup (objfile);
1fd400ff
TT
2525 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2526 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2527 {
2528 int j;
e254ef6a 2529 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2530
e254ef6a 2531 if (per_cu->v.quick->symtab)
9291a0cd
TT
2532 continue;
2533
e254ef6a
DE
2534 dw2_require_line_header (objfile, per_cu);
2535 if (!per_cu->v.quick->lines)
9291a0cd
TT
2536 continue;
2537
e254ef6a 2538 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2539 {
e254ef6a
DE
2540 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2541 j);
2542 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2543 }
2544 }
2545}
2546
2547static int
2548dw2_has_symbols (struct objfile *objfile)
2549{
2550 return 1;
2551}
2552
2553const struct quick_symbol_functions dwarf2_gdb_index_functions =
2554{
2555 dw2_has_symbols,
2556 dw2_find_last_source_symtab,
2557 dw2_forget_cached_source_info,
2558 dw2_lookup_symtab,
2559 dw2_lookup_symbol,
774b6a14 2560 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2561 dw2_print_stats,
2562 dw2_dump,
2563 dw2_relocate,
2564 dw2_expand_symtabs_for_function,
2565 dw2_expand_all_symtabs,
2566 dw2_expand_symtabs_with_filename,
2567 dw2_find_symbol_file,
40658b94 2568 dw2_map_matching_symbols,
9291a0cd
TT
2569 dw2_expand_symtabs_matching,
2570 dw2_find_pc_sect_symtab,
2571 dw2_map_symbol_names,
2572 dw2_map_symbol_filenames
2573};
2574
2575/* Initialize for reading DWARF for this objfile. Return 0 if this
2576 file will use psymtabs, or 1 if using the GNU index. */
2577
2578int
2579dwarf2_initialize_objfile (struct objfile *objfile)
2580{
2581 /* If we're about to read full symbols, don't bother with the
2582 indices. In this case we also don't care if some other debug
2583 format is making psymtabs, because they are all about to be
2584 expanded anyway. */
2585 if ((objfile->flags & OBJF_READNOW))
2586 {
2587 int i;
2588
2589 dwarf2_per_objfile->using_index = 1;
2590 create_all_comp_units (objfile);
1fd400ff 2591 create_debug_types_hash_table (objfile);
9291a0cd 2592
1fd400ff
TT
2593 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2594 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2595 {
e254ef6a 2596 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2597
e254ef6a
DE
2598 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2599 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2600 }
2601
2602 /* Return 1 so that gdb sees the "quick" functions. However,
2603 these functions will be no-ops because we will have expanded
2604 all symtabs. */
2605 return 1;
2606 }
2607
2608 if (dwarf2_read_index (objfile))
2609 return 1;
2610
2611 dwarf2_build_psymtabs (objfile);
2612 return 0;
2613}
2614
2615\f
2616
dce234bc
PP
2617/* Build a partial symbol table. */
2618
2619void
f29dff0a 2620dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2621{
f29dff0a 2622 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2623 {
2624 init_psymbol_list (objfile, 1024);
2625 }
2626
d146bf1e 2627 dwarf2_build_psymtabs_hard (objfile);
c906108c 2628}
c906108c 2629
45452591
DE
2630/* Return TRUE if OFFSET is within CU_HEADER. */
2631
2632static inline int
2633offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2634{
2635 unsigned int bottom = cu_header->offset;
2636 unsigned int top = (cu_header->offset
2637 + cu_header->length
2638 + cu_header->initial_length_size);
9a619af0 2639
45452591
DE
2640 return (offset >= bottom && offset < top);
2641}
2642
93311388
DE
2643/* Read in the comp unit header information from the debug_info at info_ptr.
2644 NOTE: This leaves members offset, first_die_offset to be filled in
2645 by the caller. */
107d2387 2646
fe1b8b76 2647static gdb_byte *
107d2387 2648read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2649 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2650{
2651 int signed_addr;
891d2f0b 2652 unsigned int bytes_read;
c764a876
DE
2653
2654 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2655 cu_header->initial_length_size = bytes_read;
2656 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2657 info_ptr += bytes_read;
107d2387
AC
2658 cu_header->version = read_2_bytes (abfd, info_ptr);
2659 info_ptr += 2;
613e1657 2660 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2661 &bytes_read);
613e1657 2662 info_ptr += bytes_read;
107d2387
AC
2663 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2664 info_ptr += 1;
2665 signed_addr = bfd_get_sign_extend_vma (abfd);
2666 if (signed_addr < 0)
8e65ff28 2667 internal_error (__FILE__, __LINE__,
e2e0b3e5 2668 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2669 cu_header->signed_addr_p = signed_addr;
c764a876 2670
107d2387
AC
2671 return info_ptr;
2672}
2673
fe1b8b76
JB
2674static gdb_byte *
2675partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2676 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2677 bfd *abfd)
2678{
fe1b8b76 2679 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2680
2681 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2682
2dc7f7b3 2683 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2684 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2685 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2686 bfd_get_filename (abfd));
72bf9492 2687
dce234bc 2688 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2689 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2690 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2691 (long) header->abbrev_offset,
93311388 2692 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2693 bfd_get_filename (abfd));
2694
2695 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2696 > buffer + buffer_size)
8a3fe4f8
AC
2697 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2698 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2699 (long) header->length,
93311388 2700 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2701 bfd_get_filename (abfd));
2702
2703 return info_ptr;
2704}
2705
348e048f
DE
2706/* Read in the types comp unit header information from .debug_types entry at
2707 types_ptr. The result is a pointer to one past the end of the header. */
2708
2709static gdb_byte *
2710read_type_comp_unit_head (struct comp_unit_head *cu_header,
2711 ULONGEST *signature,
2712 gdb_byte *types_ptr, bfd *abfd)
2713{
348e048f
DE
2714 gdb_byte *initial_types_ptr = types_ptr;
2715
6e70227d 2716 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2717 &dwarf2_per_objfile->types);
348e048f
DE
2718 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2719
2720 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2721
2722 *signature = read_8_bytes (abfd, types_ptr);
2723 types_ptr += 8;
2724 types_ptr += cu_header->offset_size;
2725 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2726
2727 return types_ptr;
2728}
2729
aaa75496
JB
2730/* Allocate a new partial symtab for file named NAME and mark this new
2731 partial symtab as being an include of PST. */
2732
2733static void
2734dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2735 struct objfile *objfile)
2736{
2737 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2738
2739 subpst->section_offsets = pst->section_offsets;
2740 subpst->textlow = 0;
2741 subpst->texthigh = 0;
2742
2743 subpst->dependencies = (struct partial_symtab **)
2744 obstack_alloc (&objfile->objfile_obstack,
2745 sizeof (struct partial_symtab *));
2746 subpst->dependencies[0] = pst;
2747 subpst->number_of_dependencies = 1;
2748
2749 subpst->globals_offset = 0;
2750 subpst->n_global_syms = 0;
2751 subpst->statics_offset = 0;
2752 subpst->n_static_syms = 0;
2753 subpst->symtab = NULL;
2754 subpst->read_symtab = pst->read_symtab;
2755 subpst->readin = 0;
2756
2757 /* No private part is necessary for include psymtabs. This property
2758 can be used to differentiate between such include psymtabs and
10b3939b 2759 the regular ones. */
58a9656e 2760 subpst->read_symtab_private = NULL;
aaa75496
JB
2761}
2762
2763/* Read the Line Number Program data and extract the list of files
2764 included by the source file represented by PST. Build an include
d85a05f0 2765 partial symtab for each of these included files. */
aaa75496
JB
2766
2767static void
2768dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2769 struct die_info *die,
aaa75496
JB
2770 struct partial_symtab *pst)
2771{
2772 struct objfile *objfile = cu->objfile;
2773 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2774 struct line_header *lh = NULL;
2775 struct attribute *attr;
aaa75496 2776
d85a05f0
DJ
2777 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2778 if (attr)
2779 {
2780 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2781
d85a05f0
DJ
2782 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2783 }
aaa75496
JB
2784 if (lh == NULL)
2785 return; /* No linetable, so no includes. */
2786
c6da4cef
DE
2787 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2788 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2789
2790 free_line_header (lh);
2791}
2792
348e048f
DE
2793static hashval_t
2794hash_type_signature (const void *item)
2795{
2796 const struct signatured_type *type_sig = item;
9a619af0 2797
348e048f
DE
2798 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2799 return type_sig->signature;
2800}
2801
2802static int
2803eq_type_signature (const void *item_lhs, const void *item_rhs)
2804{
2805 const struct signatured_type *lhs = item_lhs;
2806 const struct signatured_type *rhs = item_rhs;
9a619af0 2807
348e048f
DE
2808 return lhs->signature == rhs->signature;
2809}
2810
1fd400ff
TT
2811/* Allocate a hash table for signatured types. */
2812
2813static htab_t
673bfd45 2814allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2815{
2816 return htab_create_alloc_ex (41,
2817 hash_type_signature,
2818 eq_type_signature,
2819 NULL,
2820 &objfile->objfile_obstack,
2821 hashtab_obstack_allocate,
2822 dummy_obstack_deallocate);
2823}
2824
2825/* A helper function to add a signatured type CU to a list. */
2826
2827static int
2828add_signatured_type_cu_to_list (void **slot, void *datum)
2829{
2830 struct signatured_type *sigt = *slot;
2831 struct dwarf2_per_cu_data ***datap = datum;
2832
2833 **datap = &sigt->per_cu;
2834 ++*datap;
2835
2836 return 1;
2837}
2838
348e048f
DE
2839/* Create the hash table of all entries in the .debug_types section.
2840 The result is zero if there is an error (e.g. missing .debug_types section),
2841 otherwise non-zero. */
2842
2843static int
2844create_debug_types_hash_table (struct objfile *objfile)
2845{
be391dca 2846 gdb_byte *info_ptr;
348e048f 2847 htab_t types_htab;
1fd400ff 2848 struct dwarf2_per_cu_data **iter;
348e048f 2849
be391dca
TT
2850 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2851 info_ptr = dwarf2_per_objfile->types.buffer;
2852
348e048f
DE
2853 if (info_ptr == NULL)
2854 {
2855 dwarf2_per_objfile->signatured_types = NULL;
2856 return 0;
2857 }
2858
673bfd45 2859 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2860
2861 if (dwarf2_die_debug)
2862 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2863
2864 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2865 {
2866 unsigned int offset;
2867 unsigned int offset_size;
2868 unsigned int type_offset;
2869 unsigned int length, initial_length_size;
2870 unsigned short version;
2871 ULONGEST signature;
2872 struct signatured_type *type_sig;
2873 void **slot;
2874 gdb_byte *ptr = info_ptr;
2875
2876 offset = ptr - dwarf2_per_objfile->types.buffer;
2877
2878 /* We need to read the type's signature in order to build the hash
2879 table, but we don't need to read anything else just yet. */
2880
2881 /* Sanity check to ensure entire cu is present. */
2882 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2883 if (ptr + length + initial_length_size
2884 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2885 {
2886 complaint (&symfile_complaints,
2887 _("debug type entry runs off end of `.debug_types' section, ignored"));
2888 break;
2889 }
2890
2891 offset_size = initial_length_size == 4 ? 4 : 8;
2892 ptr += initial_length_size;
2893 version = bfd_get_16 (objfile->obfd, ptr);
2894 ptr += 2;
2895 ptr += offset_size; /* abbrev offset */
2896 ptr += 1; /* address size */
2897 signature = bfd_get_64 (objfile->obfd, ptr);
2898 ptr += 8;
2899 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2900
2901 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2902 memset (type_sig, 0, sizeof (*type_sig));
2903 type_sig->signature = signature;
2904 type_sig->offset = offset;
2905 type_sig->type_offset = type_offset;
ca1f3406 2906 type_sig->per_cu.objfile = objfile;
1fd400ff 2907 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2908
2909 slot = htab_find_slot (types_htab, type_sig, INSERT);
2910 gdb_assert (slot != NULL);
2911 *slot = type_sig;
2912
2913 if (dwarf2_die_debug)
2914 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2915 offset, phex (signature, sizeof (signature)));
2916
2917 info_ptr = info_ptr + initial_length_size + length;
2918 }
2919
2920 dwarf2_per_objfile->signatured_types = types_htab;
2921
1fd400ff
TT
2922 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2923 dwarf2_per_objfile->type_comp_units
2924 = obstack_alloc (&objfile->objfile_obstack,
2925 dwarf2_per_objfile->n_type_comp_units
2926 * sizeof (struct dwarf2_per_cu_data *));
2927 iter = &dwarf2_per_objfile->type_comp_units[0];
2928 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2929 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2930 == dwarf2_per_objfile->n_type_comp_units);
2931
348e048f
DE
2932 return 1;
2933}
2934
2935/* Lookup a signature based type.
2936 Returns NULL if SIG is not present in the table. */
2937
2938static struct signatured_type *
2939lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2940{
2941 struct signatured_type find_entry, *entry;
2942
2943 if (dwarf2_per_objfile->signatured_types == NULL)
2944 {
2945 complaint (&symfile_complaints,
2946 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2947 return 0;
2948 }
2949
2950 find_entry.signature = sig;
2951 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2952 return entry;
2953}
2954
d85a05f0
DJ
2955/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2956
2957static void
2958init_cu_die_reader (struct die_reader_specs *reader,
2959 struct dwarf2_cu *cu)
2960{
2961 reader->abfd = cu->objfile->obfd;
2962 reader->cu = cu;
2963 if (cu->per_cu->from_debug_types)
be391dca
TT
2964 {
2965 gdb_assert (dwarf2_per_objfile->types.readin);
2966 reader->buffer = dwarf2_per_objfile->types.buffer;
2967 }
d85a05f0 2968 else
be391dca
TT
2969 {
2970 gdb_assert (dwarf2_per_objfile->info.readin);
2971 reader->buffer = dwarf2_per_objfile->info.buffer;
2972 }
d85a05f0
DJ
2973}
2974
2975/* Find the base address of the compilation unit for range lists and
2976 location lists. It will normally be specified by DW_AT_low_pc.
2977 In DWARF-3 draft 4, the base address could be overridden by
2978 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2979 compilation units with discontinuous ranges. */
2980
2981static void
2982dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2983{
2984 struct attribute *attr;
2985
2986 cu->base_known = 0;
2987 cu->base_address = 0;
2988
2989 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2990 if (attr)
2991 {
2992 cu->base_address = DW_ADDR (attr);
2993 cu->base_known = 1;
2994 }
2995 else
2996 {
2997 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2998 if (attr)
2999 {
3000 cu->base_address = DW_ADDR (attr);
3001 cu->base_known = 1;
3002 }
3003 }
3004}
3005
348e048f
DE
3006/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3007 to combine the common parts.
93311388 3008 Process a compilation unit for a psymtab.
348e048f
DE
3009 BUFFER is a pointer to the beginning of the dwarf section buffer,
3010 either .debug_info or debug_types.
93311388
DE
3011 INFO_PTR is a pointer to the start of the CU.
3012 Returns a pointer to the next CU. */
aaa75496 3013
93311388
DE
3014static gdb_byte *
3015process_psymtab_comp_unit (struct objfile *objfile,
3016 struct dwarf2_per_cu_data *this_cu,
3017 gdb_byte *buffer, gdb_byte *info_ptr,
3018 unsigned int buffer_size)
c906108c 3019{
c906108c 3020 bfd *abfd = objfile->obfd;
93311388 3021 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3022 struct die_info *comp_unit_die;
c906108c 3023 struct partial_symtab *pst;
5734ee8b 3024 CORE_ADDR baseaddr;
93311388
DE
3025 struct cleanup *back_to_inner;
3026 struct dwarf2_cu cu;
d85a05f0
DJ
3027 int has_children, has_pc_info;
3028 struct attribute *attr;
d85a05f0
DJ
3029 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3030 struct die_reader_specs reader_specs;
c906108c 3031
9816fde3 3032 init_one_comp_unit (&cu, objfile);
93311388 3033 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3034
93311388
DE
3035 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3036 buffer, buffer_size,
3037 abfd);
10b3939b 3038
93311388
DE
3039 /* Complete the cu_header. */
3040 cu.header.offset = beg_of_comp_unit - buffer;
3041 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3042
93311388 3043 cu.list_in_scope = &file_symbols;
af703f96 3044
328c9494
DJ
3045 /* If this compilation unit was already read in, free the
3046 cached copy in order to read it in again. This is
3047 necessary because we skipped some symbols when we first
3048 read in the compilation unit (see load_partial_dies).
3049 This problem could be avoided, but the benefit is
3050 unclear. */
3051 if (this_cu->cu != NULL)
3052 free_one_cached_comp_unit (this_cu->cu);
3053
3054 /* Note that this is a pointer to our stack frame, being
3055 added to a global data structure. It will be cleaned up
3056 in free_stack_comp_unit when we finish with this
3057 compilation unit. */
3058 this_cu->cu = &cu;
d85a05f0
DJ
3059 cu.per_cu = this_cu;
3060
93311388
DE
3061 /* Read the abbrevs for this compilation unit into a table. */
3062 dwarf2_read_abbrevs (abfd, &cu);
3063 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3064
93311388 3065 /* Read the compilation unit die. */
348e048f
DE
3066 if (this_cu->from_debug_types)
3067 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3068 init_cu_die_reader (&reader_specs, &cu);
3069 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3070 &has_children);
93311388 3071
348e048f
DE
3072 if (this_cu->from_debug_types)
3073 {
3074 /* offset,length haven't been set yet for type units. */
3075 this_cu->offset = cu.header.offset;
3076 this_cu->length = cu.header.length + cu.header.initial_length_size;
3077 }
d85a05f0 3078 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3079 {
93311388
DE
3080 info_ptr = (beg_of_comp_unit + cu.header.length
3081 + cu.header.initial_length_size);
3082 do_cleanups (back_to_inner);
3083 return info_ptr;
3084 }
72bf9492 3085
9816fde3 3086 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3087
93311388 3088 /* Allocate a new partial symbol table structure. */
d85a05f0 3089 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3090 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3091 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3092 /* TEXTLOW and TEXTHIGH are set below. */
3093 0,
3094 objfile->global_psymbols.next,
3095 objfile->static_psymbols.next);
72bf9492 3096
d85a05f0
DJ
3097 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3098 if (attr != NULL)
3099 pst->dirname = DW_STRING (attr);
72bf9492 3100
e38df1d0 3101 pst->read_symtab_private = this_cu;
72bf9492 3102
93311388 3103 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3104
93311388
DE
3105 /* Store the function that reads in the rest of the symbol table */
3106 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3107
9291a0cd 3108 this_cu->v.psymtab = pst;
c906108c 3109
d85a05f0
DJ
3110 dwarf2_find_base_address (comp_unit_die, &cu);
3111
93311388
DE
3112 /* Possibly set the default values of LOWPC and HIGHPC from
3113 `DW_AT_ranges'. */
d85a05f0
DJ
3114 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3115 &best_highpc, &cu, pst);
3116 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3117 /* Store the contiguous range if it is not empty; it can be empty for
3118 CUs with no code. */
3119 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3120 best_lowpc + baseaddr,
3121 best_highpc + baseaddr - 1, pst);
93311388
DE
3122
3123 /* Check if comp unit has_children.
3124 If so, read the rest of the partial symbols from this comp unit.
3125 If not, there's no more debug_info for this comp unit. */
d85a05f0 3126 if (has_children)
93311388
DE
3127 {
3128 struct partial_die_info *first_die;
3129 CORE_ADDR lowpc, highpc;
31ffec48 3130
93311388
DE
3131 lowpc = ((CORE_ADDR) -1);
3132 highpc = ((CORE_ADDR) 0);
c906108c 3133
93311388 3134 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3135
93311388 3136 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3137 ! has_pc_info, &cu);
57c22c6c 3138
93311388
DE
3139 /* If we didn't find a lowpc, set it to highpc to avoid
3140 complaints from `maint check'. */
3141 if (lowpc == ((CORE_ADDR) -1))
3142 lowpc = highpc;
10b3939b 3143
93311388
DE
3144 /* If the compilation unit didn't have an explicit address range,
3145 then use the information extracted from its child dies. */
d85a05f0 3146 if (! has_pc_info)
93311388 3147 {
d85a05f0
DJ
3148 best_lowpc = lowpc;
3149 best_highpc = highpc;
93311388
DE
3150 }
3151 }
d85a05f0
DJ
3152 pst->textlow = best_lowpc + baseaddr;
3153 pst->texthigh = best_highpc + baseaddr;
c906108c 3154
93311388
DE
3155 pst->n_global_syms = objfile->global_psymbols.next -
3156 (objfile->global_psymbols.list + pst->globals_offset);
3157 pst->n_static_syms = objfile->static_psymbols.next -
3158 (objfile->static_psymbols.list + pst->statics_offset);
3159 sort_pst_symbols (pst);
c906108c 3160
93311388
DE
3161 info_ptr = (beg_of_comp_unit + cu.header.length
3162 + cu.header.initial_length_size);
ae038cb0 3163
348e048f
DE
3164 if (this_cu->from_debug_types)
3165 {
3166 /* It's not clear we want to do anything with stmt lists here.
3167 Waiting to see what gcc ultimately does. */
3168 }
d85a05f0 3169 else
93311388
DE
3170 {
3171 /* Get the list of files included in the current compilation unit,
3172 and build a psymtab for each of them. */
d85a05f0 3173 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3174 }
ae038cb0 3175
93311388 3176 do_cleanups (back_to_inner);
ae038cb0 3177
93311388
DE
3178 return info_ptr;
3179}
ff013f42 3180
348e048f
DE
3181/* Traversal function for htab_traverse_noresize.
3182 Process one .debug_types comp-unit. */
3183
3184static int
3185process_type_comp_unit (void **slot, void *info)
3186{
3187 struct signatured_type *entry = (struct signatured_type *) *slot;
3188 struct objfile *objfile = (struct objfile *) info;
3189 struct dwarf2_per_cu_data *this_cu;
3190
3191 this_cu = &entry->per_cu;
348e048f 3192
be391dca 3193 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3194 process_psymtab_comp_unit (objfile, this_cu,
3195 dwarf2_per_objfile->types.buffer,
3196 dwarf2_per_objfile->types.buffer + entry->offset,
3197 dwarf2_per_objfile->types.size);
3198
3199 return 1;
3200}
3201
3202/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3203 Build partial symbol tables for the .debug_types comp-units. */
3204
3205static void
3206build_type_psymtabs (struct objfile *objfile)
3207{
3208 if (! create_debug_types_hash_table (objfile))
3209 return;
3210
3211 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3212 process_type_comp_unit, objfile);
3213}
3214
60606b2c
TT
3215/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3216
3217static void
3218psymtabs_addrmap_cleanup (void *o)
3219{
3220 struct objfile *objfile = o;
ec61707d 3221
60606b2c
TT
3222 objfile->psymtabs_addrmap = NULL;
3223}
3224
93311388
DE
3225/* Build the partial symbol table by doing a quick pass through the
3226 .debug_info and .debug_abbrev sections. */
72bf9492 3227
93311388 3228static void
c67a9c90 3229dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3230{
93311388 3231 gdb_byte *info_ptr;
60606b2c
TT
3232 struct cleanup *back_to, *addrmap_cleanup;
3233 struct obstack temp_obstack;
93311388 3234
98bfdba5
PA
3235 dwarf2_per_objfile->reading_partial_symbols = 1;
3236
be391dca 3237 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3238 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3239
93311388
DE
3240 /* Any cached compilation units will be linked by the per-objfile
3241 read_in_chain. Make sure to free them when we're done. */
3242 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3243
348e048f
DE
3244 build_type_psymtabs (objfile);
3245
93311388 3246 create_all_comp_units (objfile);
c906108c 3247
60606b2c
TT
3248 /* Create a temporary address map on a temporary obstack. We later
3249 copy this to the final obstack. */
3250 obstack_init (&temp_obstack);
3251 make_cleanup_obstack_free (&temp_obstack);
3252 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3253 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3254
93311388
DE
3255 /* Since the objects we're extracting from .debug_info vary in
3256 length, only the individual functions to extract them (like
3257 read_comp_unit_head and load_partial_die) can really know whether
3258 the buffer is large enough to hold another complete object.
c906108c 3259
93311388
DE
3260 At the moment, they don't actually check that. If .debug_info
3261 holds just one extra byte after the last compilation unit's dies,
3262 then read_comp_unit_head will happily read off the end of the
3263 buffer. read_partial_die is similarly casual. Those functions
3264 should be fixed.
c906108c 3265
93311388
DE
3266 For this loop condition, simply checking whether there's any data
3267 left at all should be sufficient. */
c906108c 3268
93311388
DE
3269 while (info_ptr < (dwarf2_per_objfile->info.buffer
3270 + dwarf2_per_objfile->info.size))
3271 {
3272 struct dwarf2_per_cu_data *this_cu;
dd373385 3273
93311388
DE
3274 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3275 objfile);
aaa75496 3276
93311388
DE
3277 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3278 dwarf2_per_objfile->info.buffer,
3279 info_ptr,
3280 dwarf2_per_objfile->info.size);
c906108c 3281 }
ff013f42
JK
3282
3283 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3284 &objfile->objfile_obstack);
60606b2c 3285 discard_cleanups (addrmap_cleanup);
ff013f42 3286
ae038cb0
DJ
3287 do_cleanups (back_to);
3288}
3289
93311388 3290/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3291
3292static void
93311388
DE
3293load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3294 struct objfile *objfile)
ae038cb0
DJ
3295{
3296 bfd *abfd = objfile->obfd;
fe1b8b76 3297 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3298 struct die_info *comp_unit_die;
ae038cb0 3299 struct dwarf2_cu *cu;
1d9ec526 3300 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3301 int has_children;
3302 struct die_reader_specs reader_specs;
98bfdba5 3303 int read_cu = 0;
ae038cb0 3304
348e048f
DE
3305 gdb_assert (! this_cu->from_debug_types);
3306
be391dca 3307 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3308 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3309 beg_of_comp_unit = info_ptr;
3310
98bfdba5
PA
3311 if (this_cu->cu == NULL)
3312 {
9816fde3
JK
3313 cu = xmalloc (sizeof (*cu));
3314 init_one_comp_unit (cu, objfile);
ae038cb0 3315
98bfdba5 3316 read_cu = 1;
ae038cb0 3317
98bfdba5
PA
3318 /* If an error occurs while loading, release our storage. */
3319 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3320
98bfdba5
PA
3321 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3322 dwarf2_per_objfile->info.buffer,
3323 dwarf2_per_objfile->info.size,
3324 abfd);
ae038cb0 3325
98bfdba5
PA
3326 /* Complete the cu_header. */
3327 cu->header.offset = this_cu->offset;
3328 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3329
3330 /* Link this compilation unit into the compilation unit tree. */
3331 this_cu->cu = cu;
3332 cu->per_cu = this_cu;
98bfdba5
PA
3333
3334 /* Link this CU into read_in_chain. */
3335 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3336 dwarf2_per_objfile->read_in_chain = this_cu;
3337 }
3338 else
3339 {
3340 cu = this_cu->cu;
3341 info_ptr += cu->header.first_die_offset;
3342 }
ae038cb0
DJ
3343
3344 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3345 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3346 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3347 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3348
3349 /* Read the compilation unit die. */
d85a05f0
DJ
3350 init_cu_die_reader (&reader_specs, cu);
3351 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3352 &has_children);
ae038cb0 3353
9816fde3 3354 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3355
ae038cb0
DJ
3356 /* Check if comp unit has_children.
3357 If so, read the rest of the partial symbols from this comp unit.
3358 If not, there's no more debug_info for this comp unit. */
d85a05f0 3359 if (has_children)
93311388 3360 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3361
98bfdba5
PA
3362 do_cleanups (free_abbrevs_cleanup);
3363
3364 if (read_cu)
3365 {
3366 /* We've successfully allocated this compilation unit. Let our
3367 caller clean it up when finished with it. */
3368 discard_cleanups (free_cu_cleanup);
3369 }
ae038cb0
DJ
3370}
3371
3372/* Create a list of all compilation units in OBJFILE. We do this only
3373 if an inter-comp-unit reference is found; presumably if there is one,
3374 there will be many, and one will occur early in the .debug_info section.
3375 So there's no point in building this list incrementally. */
3376
3377static void
3378create_all_comp_units (struct objfile *objfile)
3379{
3380 int n_allocated;
3381 int n_comp_units;
3382 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3383 gdb_byte *info_ptr;
3384
3385 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3386 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3387
3388 n_comp_units = 0;
3389 n_allocated = 10;
3390 all_comp_units = xmalloc (n_allocated
3391 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3392
dce234bc 3393 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3394 {
c764a876 3395 unsigned int length, initial_length_size;
ae038cb0 3396 struct dwarf2_per_cu_data *this_cu;
c764a876 3397 unsigned int offset;
ae038cb0 3398
dce234bc 3399 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3400
3401 /* Read just enough information to find out where the next
3402 compilation unit is. */
c764a876
DE
3403 length = read_initial_length (objfile->obfd, info_ptr,
3404 &initial_length_size);
ae038cb0
DJ
3405
3406 /* Save the compilation unit for later lookup. */
3407 this_cu = obstack_alloc (&objfile->objfile_obstack,
3408 sizeof (struct dwarf2_per_cu_data));
3409 memset (this_cu, 0, sizeof (*this_cu));
3410 this_cu->offset = offset;
c764a876 3411 this_cu->length = length + initial_length_size;
9291a0cd 3412 this_cu->objfile = objfile;
ae038cb0
DJ
3413
3414 if (n_comp_units == n_allocated)
3415 {
3416 n_allocated *= 2;
3417 all_comp_units = xrealloc (all_comp_units,
3418 n_allocated
3419 * sizeof (struct dwarf2_per_cu_data *));
3420 }
3421 all_comp_units[n_comp_units++] = this_cu;
3422
3423 info_ptr = info_ptr + this_cu->length;
3424 }
3425
3426 dwarf2_per_objfile->all_comp_units
3427 = obstack_alloc (&objfile->objfile_obstack,
3428 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3429 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3430 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3431 xfree (all_comp_units);
3432 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3433}
3434
5734ee8b
DJ
3435/* Process all loaded DIEs for compilation unit CU, starting at
3436 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3437 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3438 DW_AT_ranges). If NEED_PC is set, then this function will set
3439 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3440 and record the covered ranges in the addrmap. */
c906108c 3441
72bf9492
DJ
3442static void
3443scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3444 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3445{
72bf9492 3446 struct partial_die_info *pdi;
c906108c 3447
91c24f0a
DC
3448 /* Now, march along the PDI's, descending into ones which have
3449 interesting children but skipping the children of the other ones,
3450 until we reach the end of the compilation unit. */
c906108c 3451
72bf9492 3452 pdi = first_die;
91c24f0a 3453
72bf9492
DJ
3454 while (pdi != NULL)
3455 {
3456 fixup_partial_die (pdi, cu);
c906108c 3457
f55ee35c 3458 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3459 children, so we need to look at them. Ditto for anonymous
3460 enums. */
933c6fe4 3461
72bf9492 3462 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3463 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3464 {
72bf9492 3465 switch (pdi->tag)
c906108c
SS
3466 {
3467 case DW_TAG_subprogram:
5734ee8b 3468 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3469 break;
72929c62 3470 case DW_TAG_constant:
c906108c
SS
3471 case DW_TAG_variable:
3472 case DW_TAG_typedef:
91c24f0a 3473 case DW_TAG_union_type:
72bf9492 3474 if (!pdi->is_declaration)
63d06c5c 3475 {
72bf9492 3476 add_partial_symbol (pdi, cu);
63d06c5c
DC
3477 }
3478 break;
c906108c 3479 case DW_TAG_class_type:
680b30c7 3480 case DW_TAG_interface_type:
c906108c 3481 case DW_TAG_structure_type:
72bf9492 3482 if (!pdi->is_declaration)
c906108c 3483 {
72bf9492 3484 add_partial_symbol (pdi, cu);
c906108c
SS
3485 }
3486 break;
91c24f0a 3487 case DW_TAG_enumeration_type:
72bf9492
DJ
3488 if (!pdi->is_declaration)
3489 add_partial_enumeration (pdi, cu);
c906108c
SS
3490 break;
3491 case DW_TAG_base_type:
a02abb62 3492 case DW_TAG_subrange_type:
c906108c 3493 /* File scope base type definitions are added to the partial
c5aa993b 3494 symbol table. */
72bf9492 3495 add_partial_symbol (pdi, cu);
c906108c 3496 break;
d9fa45fe 3497 case DW_TAG_namespace:
5734ee8b 3498 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3499 break;
5d7cb8df
JK
3500 case DW_TAG_module:
3501 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3502 break;
c906108c
SS
3503 default:
3504 break;
3505 }
3506 }
3507
72bf9492
DJ
3508 /* If the die has a sibling, skip to the sibling. */
3509
3510 pdi = pdi->die_sibling;
3511 }
3512}
3513
3514/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3515
72bf9492 3516 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3517 name is concatenated with "::" and the partial DIE's name. For
3518 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3519 Enumerators are an exception; they use the scope of their parent
3520 enumeration type, i.e. the name of the enumeration type is not
3521 prepended to the enumerator.
91c24f0a 3522
72bf9492
DJ
3523 There are two complexities. One is DW_AT_specification; in this
3524 case "parent" means the parent of the target of the specification,
3525 instead of the direct parent of the DIE. The other is compilers
3526 which do not emit DW_TAG_namespace; in this case we try to guess
3527 the fully qualified name of structure types from their members'
3528 linkage names. This must be done using the DIE's children rather
3529 than the children of any DW_AT_specification target. We only need
3530 to do this for structures at the top level, i.e. if the target of
3531 any DW_AT_specification (if any; otherwise the DIE itself) does not
3532 have a parent. */
3533
3534/* Compute the scope prefix associated with PDI's parent, in
3535 compilation unit CU. The result will be allocated on CU's
3536 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3537 field. NULL is returned if no prefix is necessary. */
3538static char *
3539partial_die_parent_scope (struct partial_die_info *pdi,
3540 struct dwarf2_cu *cu)
3541{
3542 char *grandparent_scope;
3543 struct partial_die_info *parent, *real_pdi;
91c24f0a 3544
72bf9492
DJ
3545 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3546 then this means the parent of the specification DIE. */
3547
3548 real_pdi = pdi;
72bf9492 3549 while (real_pdi->has_specification)
10b3939b 3550 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3551
3552 parent = real_pdi->die_parent;
3553 if (parent == NULL)
3554 return NULL;
3555
3556 if (parent->scope_set)
3557 return parent->scope;
3558
3559 fixup_partial_die (parent, cu);
3560
10b3939b 3561 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3562
acebe513
UW
3563 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3564 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3565 Work around this problem here. */
3566 if (cu->language == language_cplus
6e70227d 3567 && parent->tag == DW_TAG_namespace
acebe513
UW
3568 && strcmp (parent->name, "::") == 0
3569 && grandparent_scope == NULL)
3570 {
3571 parent->scope = NULL;
3572 parent->scope_set = 1;
3573 return NULL;
3574 }
3575
72bf9492 3576 if (parent->tag == DW_TAG_namespace
f55ee35c 3577 || parent->tag == DW_TAG_module
72bf9492
DJ
3578 || parent->tag == DW_TAG_structure_type
3579 || parent->tag == DW_TAG_class_type
680b30c7 3580 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3581 || parent->tag == DW_TAG_union_type
3582 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3583 {
3584 if (grandparent_scope == NULL)
3585 parent->scope = parent->name;
3586 else
987504bb 3587 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3588 parent->name, 0, cu);
72bf9492 3589 }
ceeb3d5a 3590 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3591 /* Enumerators should not get the name of the enumeration as a prefix. */
3592 parent->scope = grandparent_scope;
3593 else
3594 {
3595 /* FIXME drow/2004-04-01: What should we be doing with
3596 function-local names? For partial symbols, we should probably be
3597 ignoring them. */
3598 complaint (&symfile_complaints,
e2e0b3e5 3599 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3600 parent->tag, pdi->offset);
3601 parent->scope = grandparent_scope;
c906108c
SS
3602 }
3603
72bf9492
DJ
3604 parent->scope_set = 1;
3605 return parent->scope;
3606}
3607
3608/* Return the fully scoped name associated with PDI, from compilation unit
3609 CU. The result will be allocated with malloc. */
3610static char *
3611partial_die_full_name (struct partial_die_info *pdi,
3612 struct dwarf2_cu *cu)
3613{
3614 char *parent_scope;
3615
98bfdba5
PA
3616 /* If this is a template instantiation, we can not work out the
3617 template arguments from partial DIEs. So, unfortunately, we have
3618 to go through the full DIEs. At least any work we do building
3619 types here will be reused if full symbols are loaded later. */
3620 if (pdi->has_template_arguments)
3621 {
3622 fixup_partial_die (pdi, cu);
3623
3624 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3625 {
3626 struct die_info *die;
3627 struct attribute attr;
3628 struct dwarf2_cu *ref_cu = cu;
3629
3630 attr.name = 0;
3631 attr.form = DW_FORM_ref_addr;
3632 attr.u.addr = pdi->offset;
3633 die = follow_die_ref (NULL, &attr, &ref_cu);
3634
3635 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3636 }
3637 }
3638
72bf9492
DJ
3639 parent_scope = partial_die_parent_scope (pdi, cu);
3640 if (parent_scope == NULL)
3641 return NULL;
3642 else
f55ee35c 3643 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3644}
3645
3646static void
72bf9492 3647add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3648{
e7c27a73 3649 struct objfile *objfile = cu->objfile;
c906108c 3650 CORE_ADDR addr = 0;
decbce07 3651 char *actual_name = NULL;
5c4e30ca 3652 const struct partial_symbol *psym = NULL;
e142c38c 3653 CORE_ADDR baseaddr;
72bf9492 3654 int built_actual_name = 0;
e142c38c
DJ
3655
3656 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3657
94af9270
KS
3658 actual_name = partial_die_full_name (pdi, cu);
3659 if (actual_name)
3660 built_actual_name = 1;
63d06c5c 3661
72bf9492
DJ
3662 if (actual_name == NULL)
3663 actual_name = pdi->name;
3664
c906108c
SS
3665 switch (pdi->tag)
3666 {
3667 case DW_TAG_subprogram:
2cfa0c8d 3668 if (pdi->is_external || cu->language == language_ada)
c906108c 3669 {
2cfa0c8d
JB
3670 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3671 of the global scope. But in Ada, we want to be able to access
3672 nested procedures globally. So all Ada subprograms are stored
3673 in the global scope. */
38d518c9 3674 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3675 mst_text, objfile); */
38d518c9 3676 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3677 built_actual_name,
5c4e30ca
DC
3678 VAR_DOMAIN, LOC_BLOCK,
3679 &objfile->global_psymbols,
3680 0, pdi->lowpc + baseaddr,
e142c38c 3681 cu->language, objfile);
c906108c
SS
3682 }
3683 else
3684 {
38d518c9 3685 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3686 mst_file_text, objfile); */
38d518c9 3687 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3688 built_actual_name,
5c4e30ca
DC
3689 VAR_DOMAIN, LOC_BLOCK,
3690 &objfile->static_psymbols,
3691 0, pdi->lowpc + baseaddr,
e142c38c 3692 cu->language, objfile);
c906108c
SS
3693 }
3694 break;
72929c62
JB
3695 case DW_TAG_constant:
3696 {
3697 struct psymbol_allocation_list *list;
3698
3699 if (pdi->is_external)
3700 list = &objfile->global_psymbols;
3701 else
3702 list = &objfile->static_psymbols;
3703 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3704 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3705 list, 0, 0, cu->language, objfile);
3706
3707 }
3708 break;
c906108c 3709 case DW_TAG_variable:
caac4577
JG
3710 if (pdi->locdesc)
3711 addr = decode_locdesc (pdi->locdesc, cu);
3712
3713 if (pdi->locdesc
3714 && addr == 0
3715 && !dwarf2_per_objfile->has_section_at_zero)
3716 {
3717 /* A global or static variable may also have been stripped
3718 out by the linker if unused, in which case its address
3719 will be nullified; do not add such variables into partial
3720 symbol table then. */
3721 }
3722 else if (pdi->is_external)
c906108c
SS
3723 {
3724 /* Global Variable.
3725 Don't enter into the minimal symbol tables as there is
3726 a minimal symbol table entry from the ELF symbols already.
3727 Enter into partial symbol table if it has a location
3728 descriptor or a type.
3729 If the location descriptor is missing, new_symbol will create
3730 a LOC_UNRESOLVED symbol, the address of the variable will then
3731 be determined from the minimal symbol table whenever the variable
3732 is referenced.
3733 The address for the partial symbol table entry is not
3734 used by GDB, but it comes in handy for debugging partial symbol
3735 table building. */
3736
c906108c 3737 if (pdi->locdesc || pdi->has_type)
38d518c9 3738 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3739 built_actual_name,
5c4e30ca
DC
3740 VAR_DOMAIN, LOC_STATIC,
3741 &objfile->global_psymbols,
3742 0, addr + baseaddr,
e142c38c 3743 cu->language, objfile);
c906108c
SS
3744 }
3745 else
3746 {
3747 /* Static Variable. Skip symbols without location descriptors. */
3748 if (pdi->locdesc == NULL)
decbce07
MS
3749 {
3750 if (built_actual_name)
3751 xfree (actual_name);
3752 return;
3753 }
38d518c9 3754 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3755 mst_file_data, objfile); */
38d518c9 3756 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3757 built_actual_name,
5c4e30ca
DC
3758 VAR_DOMAIN, LOC_STATIC,
3759 &objfile->static_psymbols,
3760 0, addr + baseaddr,
e142c38c 3761 cu->language, objfile);
c906108c
SS
3762 }
3763 break;
3764 case DW_TAG_typedef:
3765 case DW_TAG_base_type:
a02abb62 3766 case DW_TAG_subrange_type:
38d518c9 3767 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3768 built_actual_name,
176620f1 3769 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3770 &objfile->static_psymbols,
e142c38c 3771 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3772 break;
72bf9492
DJ
3773 case DW_TAG_namespace:
3774 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3775 built_actual_name,
72bf9492
DJ
3776 VAR_DOMAIN, LOC_TYPEDEF,
3777 &objfile->global_psymbols,
3778 0, (CORE_ADDR) 0, cu->language, objfile);
3779 break;
c906108c 3780 case DW_TAG_class_type:
680b30c7 3781 case DW_TAG_interface_type:
c906108c
SS
3782 case DW_TAG_structure_type:
3783 case DW_TAG_union_type:
3784 case DW_TAG_enumeration_type:
fa4028e9
JB
3785 /* Skip external references. The DWARF standard says in the section
3786 about "Structure, Union, and Class Type Entries": "An incomplete
3787 structure, union or class type is represented by a structure,
3788 union or class entry that does not have a byte size attribute
3789 and that has a DW_AT_declaration attribute." */
3790 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3791 {
3792 if (built_actual_name)
3793 xfree (actual_name);
3794 return;
3795 }
fa4028e9 3796
63d06c5c
DC
3797 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3798 static vs. global. */
38d518c9 3799 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3800 built_actual_name,
176620f1 3801 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3802 (cu->language == language_cplus
3803 || cu->language == language_java)
63d06c5c
DC
3804 ? &objfile->global_psymbols
3805 : &objfile->static_psymbols,
e142c38c 3806 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3807
c906108c
SS
3808 break;
3809 case DW_TAG_enumerator:
38d518c9 3810 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3811 built_actual_name,
176620f1 3812 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3813 (cu->language == language_cplus
3814 || cu->language == language_java)
f6fe98ef
DJ
3815 ? &objfile->global_psymbols
3816 : &objfile->static_psymbols,
e142c38c 3817 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3818 break;
3819 default:
3820 break;
3821 }
5c4e30ca 3822
72bf9492
DJ
3823 if (built_actual_name)
3824 xfree (actual_name);
c906108c
SS
3825}
3826
5c4e30ca
DC
3827/* Read a partial die corresponding to a namespace; also, add a symbol
3828 corresponding to that namespace to the symbol table. NAMESPACE is
3829 the name of the enclosing namespace. */
91c24f0a 3830
72bf9492
DJ
3831static void
3832add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3833 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3834 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3835{
72bf9492 3836 /* Add a symbol for the namespace. */
e7c27a73 3837
72bf9492 3838 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3839
3840 /* Now scan partial symbols in that namespace. */
3841
91c24f0a 3842 if (pdi->has_children)
5734ee8b 3843 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3844}
3845
5d7cb8df
JK
3846/* Read a partial die corresponding to a Fortran module. */
3847
3848static void
3849add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3850 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3851{
f55ee35c 3852 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3853
3854 if (pdi->has_children)
3855 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3856}
3857
bc30ff58
JB
3858/* Read a partial die corresponding to a subprogram and create a partial
3859 symbol for that subprogram. When the CU language allows it, this
3860 routine also defines a partial symbol for each nested subprogram
3861 that this subprogram contains.
6e70227d 3862
bc30ff58
JB
3863 DIE my also be a lexical block, in which case we simply search
3864 recursively for suprograms defined inside that lexical block.
3865 Again, this is only performed when the CU language allows this
3866 type of definitions. */
3867
3868static void
3869add_partial_subprogram (struct partial_die_info *pdi,
3870 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3871 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3872{
3873 if (pdi->tag == DW_TAG_subprogram)
3874 {
3875 if (pdi->has_pc_info)
3876 {
3877 if (pdi->lowpc < *lowpc)
3878 *lowpc = pdi->lowpc;
3879 if (pdi->highpc > *highpc)
3880 *highpc = pdi->highpc;
5734ee8b
DJ
3881 if (need_pc)
3882 {
3883 CORE_ADDR baseaddr;
3884 struct objfile *objfile = cu->objfile;
3885
3886 baseaddr = ANOFFSET (objfile->section_offsets,
3887 SECT_OFF_TEXT (objfile));
3888 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3889 pdi->lowpc + baseaddr,
3890 pdi->highpc - 1 + baseaddr,
9291a0cd 3891 cu->per_cu->v.psymtab);
5734ee8b 3892 }
bc30ff58 3893 if (!pdi->is_declaration)
e8d05480
JB
3894 /* Ignore subprogram DIEs that do not have a name, they are
3895 illegal. Do not emit a complaint at this point, we will
3896 do so when we convert this psymtab into a symtab. */
3897 if (pdi->name)
3898 add_partial_symbol (pdi, cu);
bc30ff58
JB
3899 }
3900 }
6e70227d 3901
bc30ff58
JB
3902 if (! pdi->has_children)
3903 return;
3904
3905 if (cu->language == language_ada)
3906 {
3907 pdi = pdi->die_child;
3908 while (pdi != NULL)
3909 {
3910 fixup_partial_die (pdi, cu);
3911 if (pdi->tag == DW_TAG_subprogram
3912 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3913 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3914 pdi = pdi->die_sibling;
3915 }
3916 }
3917}
3918
91c24f0a
DC
3919/* Read a partial die corresponding to an enumeration type. */
3920
72bf9492
DJ
3921static void
3922add_partial_enumeration (struct partial_die_info *enum_pdi,
3923 struct dwarf2_cu *cu)
91c24f0a 3924{
72bf9492 3925 struct partial_die_info *pdi;
91c24f0a
DC
3926
3927 if (enum_pdi->name != NULL)
72bf9492
DJ
3928 add_partial_symbol (enum_pdi, cu);
3929
3930 pdi = enum_pdi->die_child;
3931 while (pdi)
91c24f0a 3932 {
72bf9492 3933 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3934 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3935 else
72bf9492
DJ
3936 add_partial_symbol (pdi, cu);
3937 pdi = pdi->die_sibling;
91c24f0a 3938 }
91c24f0a
DC
3939}
3940
4bb7a0a7
DJ
3941/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3942 Return the corresponding abbrev, or NULL if the number is zero (indicating
3943 an empty DIE). In either case *BYTES_READ will be set to the length of
3944 the initial number. */
3945
3946static struct abbrev_info *
fe1b8b76 3947peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3948 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3949{
3950 bfd *abfd = cu->objfile->obfd;
3951 unsigned int abbrev_number;
3952 struct abbrev_info *abbrev;
3953
3954 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3955
3956 if (abbrev_number == 0)
3957 return NULL;
3958
3959 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3960 if (!abbrev)
3961 {
8a3fe4f8 3962 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3963 bfd_get_filename (abfd));
3964 }
3965
3966 return abbrev;
3967}
3968
93311388
DE
3969/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3970 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3971 DIE. Any children of the skipped DIEs will also be skipped. */
3972
fe1b8b76 3973static gdb_byte *
93311388 3974skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3975{
3976 struct abbrev_info *abbrev;
3977 unsigned int bytes_read;
3978
3979 while (1)
3980 {
3981 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3982 if (abbrev == NULL)
3983 return info_ptr + bytes_read;
3984 else
93311388 3985 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3986 }
3987}
3988
93311388
DE
3989/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3990 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3991 abbrev corresponding to that skipped uleb128 should be passed in
3992 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3993 children. */
3994
fe1b8b76 3995static gdb_byte *
93311388
DE
3996skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3997 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3998{
3999 unsigned int bytes_read;
4000 struct attribute attr;
4001 bfd *abfd = cu->objfile->obfd;
4002 unsigned int form, i;
4003
4004 for (i = 0; i < abbrev->num_attrs; i++)
4005 {
4006 /* The only abbrev we care about is DW_AT_sibling. */
4007 if (abbrev->attrs[i].name == DW_AT_sibling)
4008 {
4009 read_attribute (&attr, &abbrev->attrs[i],
4010 abfd, info_ptr, cu);
4011 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4012 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4013 else
93311388 4014 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4015 }
4016
4017 /* If it isn't DW_AT_sibling, skip this attribute. */
4018 form = abbrev->attrs[i].form;
4019 skip_attribute:
4020 switch (form)
4021 {
4bb7a0a7 4022 case DW_FORM_ref_addr:
ae411497
TT
4023 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4024 and later it is offset sized. */
4025 if (cu->header.version == 2)
4026 info_ptr += cu->header.addr_size;
4027 else
4028 info_ptr += cu->header.offset_size;
4029 break;
4030 case DW_FORM_addr:
4bb7a0a7
DJ
4031 info_ptr += cu->header.addr_size;
4032 break;
4033 case DW_FORM_data1:
4034 case DW_FORM_ref1:
4035 case DW_FORM_flag:
4036 info_ptr += 1;
4037 break;
2dc7f7b3
TT
4038 case DW_FORM_flag_present:
4039 break;
4bb7a0a7
DJ
4040 case DW_FORM_data2:
4041 case DW_FORM_ref2:
4042 info_ptr += 2;
4043 break;
4044 case DW_FORM_data4:
4045 case DW_FORM_ref4:
4046 info_ptr += 4;
4047 break;
4048 case DW_FORM_data8:
4049 case DW_FORM_ref8:
348e048f 4050 case DW_FORM_sig8:
4bb7a0a7
DJ
4051 info_ptr += 8;
4052 break;
4053 case DW_FORM_string:
9b1c24c8 4054 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4055 info_ptr += bytes_read;
4056 break;
2dc7f7b3 4057 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4058 case DW_FORM_strp:
4059 info_ptr += cu->header.offset_size;
4060 break;
2dc7f7b3 4061 case DW_FORM_exprloc:
4bb7a0a7
DJ
4062 case DW_FORM_block:
4063 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4064 info_ptr += bytes_read;
4065 break;
4066 case DW_FORM_block1:
4067 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4068 break;
4069 case DW_FORM_block2:
4070 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4071 break;
4072 case DW_FORM_block4:
4073 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4074 break;
4075 case DW_FORM_sdata:
4076 case DW_FORM_udata:
4077 case DW_FORM_ref_udata:
4078 info_ptr = skip_leb128 (abfd, info_ptr);
4079 break;
4080 case DW_FORM_indirect:
4081 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4082 info_ptr += bytes_read;
4083 /* We need to continue parsing from here, so just go back to
4084 the top. */
4085 goto skip_attribute;
4086
4087 default:
8a3fe4f8 4088 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4089 dwarf_form_name (form),
4090 bfd_get_filename (abfd));
4091 }
4092 }
4093
4094 if (abbrev->has_children)
93311388 4095 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4096 else
4097 return info_ptr;
4098}
4099
93311388
DE
4100/* Locate ORIG_PDI's sibling.
4101 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4102 in BUFFER. */
91c24f0a 4103
fe1b8b76 4104static gdb_byte *
93311388
DE
4105locate_pdi_sibling (struct partial_die_info *orig_pdi,
4106 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4107 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4108{
4109 /* Do we know the sibling already? */
72bf9492 4110
91c24f0a
DC
4111 if (orig_pdi->sibling)
4112 return orig_pdi->sibling;
4113
4114 /* Are there any children to deal with? */
4115
4116 if (!orig_pdi->has_children)
4117 return info_ptr;
4118
4bb7a0a7 4119 /* Skip the children the long way. */
91c24f0a 4120
93311388 4121 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4122}
4123
c906108c
SS
4124/* Expand this partial symbol table into a full symbol table. */
4125
4126static void
fba45db2 4127dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4128{
c906108c
SS
4129 if (pst != NULL)
4130 {
4131 if (pst->readin)
4132 {
8a3fe4f8 4133 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4134 }
4135 else
4136 {
4137 if (info_verbose)
4138 {
a3f17187 4139 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4140 gdb_flush (gdb_stdout);
4141 }
4142
10b3939b
DJ
4143 /* Restore our global data. */
4144 dwarf2_per_objfile = objfile_data (pst->objfile,
4145 dwarf2_objfile_data_key);
4146
b2ab525c
KB
4147 /* If this psymtab is constructed from a debug-only objfile, the
4148 has_section_at_zero flag will not necessarily be correct. We
4149 can get the correct value for this flag by looking at the data
4150 associated with the (presumably stripped) associated objfile. */
4151 if (pst->objfile->separate_debug_objfile_backlink)
4152 {
4153 struct dwarf2_per_objfile *dpo_backlink
4154 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4155 dwarf2_objfile_data_key);
9a619af0 4156
b2ab525c
KB
4157 dwarf2_per_objfile->has_section_at_zero
4158 = dpo_backlink->has_section_at_zero;
4159 }
4160
98bfdba5
PA
4161 dwarf2_per_objfile->reading_partial_symbols = 0;
4162
c906108c
SS
4163 psymtab_to_symtab_1 (pst);
4164
4165 /* Finish up the debug error message. */
4166 if (info_verbose)
a3f17187 4167 printf_filtered (_("done.\n"));
c906108c
SS
4168 }
4169 }
4170}
4171
10b3939b
DJ
4172/* Add PER_CU to the queue. */
4173
4174static void
03dd20cc 4175queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4176{
4177 struct dwarf2_queue_item *item;
4178
4179 per_cu->queued = 1;
4180 item = xmalloc (sizeof (*item));
4181 item->per_cu = per_cu;
4182 item->next = NULL;
4183
4184 if (dwarf2_queue == NULL)
4185 dwarf2_queue = item;
4186 else
4187 dwarf2_queue_tail->next = item;
4188
4189 dwarf2_queue_tail = item;
4190}
4191
4192/* Process the queue. */
4193
4194static void
4195process_queue (struct objfile *objfile)
4196{
4197 struct dwarf2_queue_item *item, *next_item;
4198
03dd20cc
DJ
4199 /* The queue starts out with one item, but following a DIE reference
4200 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4201 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4202 {
9291a0cd
TT
4203 if (dwarf2_per_objfile->using_index
4204 ? !item->per_cu->v.quick->symtab
4205 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4206 process_full_comp_unit (item->per_cu);
4207
4208 item->per_cu->queued = 0;
4209 next_item = item->next;
4210 xfree (item);
4211 }
4212
4213 dwarf2_queue_tail = NULL;
4214}
4215
4216/* Free all allocated queue entries. This function only releases anything if
4217 an error was thrown; if the queue was processed then it would have been
4218 freed as we went along. */
4219
4220static void
4221dwarf2_release_queue (void *dummy)
4222{
4223 struct dwarf2_queue_item *item, *last;
4224
4225 item = dwarf2_queue;
4226 while (item)
4227 {
4228 /* Anything still marked queued is likely to be in an
4229 inconsistent state, so discard it. */
4230 if (item->per_cu->queued)
4231 {
4232 if (item->per_cu->cu != NULL)
4233 free_one_cached_comp_unit (item->per_cu->cu);
4234 item->per_cu->queued = 0;
4235 }
4236
4237 last = item;
4238 item = item->next;
4239 xfree (last);
4240 }
4241
4242 dwarf2_queue = dwarf2_queue_tail = NULL;
4243}
4244
4245/* Read in full symbols for PST, and anything it depends on. */
4246
c906108c 4247static void
fba45db2 4248psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4249{
10b3939b 4250 struct dwarf2_per_cu_data *per_cu;
c906108c 4251 struct cleanup *back_to;
aaa75496
JB
4252 int i;
4253
4254 for (i = 0; i < pst->number_of_dependencies; i++)
4255 if (!pst->dependencies[i]->readin)
4256 {
4257 /* Inform about additional files that need to be read in. */
4258 if (info_verbose)
4259 {
a3f17187 4260 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4261 fputs_filtered (" ", gdb_stdout);
4262 wrap_here ("");
4263 fputs_filtered ("and ", gdb_stdout);
4264 wrap_here ("");
4265 printf_filtered ("%s...", pst->dependencies[i]->filename);
4266 wrap_here (""); /* Flush output */
4267 gdb_flush (gdb_stdout);
4268 }
4269 psymtab_to_symtab_1 (pst->dependencies[i]);
4270 }
4271
e38df1d0 4272 per_cu = pst->read_symtab_private;
10b3939b
DJ
4273
4274 if (per_cu == NULL)
aaa75496
JB
4275 {
4276 /* It's an include file, no symbols to read for it.
4277 Everything is in the parent symtab. */
4278 pst->readin = 1;
4279 return;
4280 }
c906108c 4281
9291a0cd 4282 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4283}
4284
93311388 4285/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4286
93311388 4287static void
31ffec48 4288load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4289{
31ffec48 4290 bfd *abfd = objfile->obfd;
10b3939b 4291 struct dwarf2_cu *cu;
c764a876 4292 unsigned int offset;
93311388 4293 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4294 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4295 struct attribute *attr;
98bfdba5 4296 int read_cu = 0;
6502dd73 4297
348e048f
DE
4298 gdb_assert (! per_cu->from_debug_types);
4299
c906108c 4300 /* Set local variables from the partial symbol table info. */
10b3939b 4301 offset = per_cu->offset;
6502dd73 4302
be391dca 4303 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4304 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4305 beg_of_comp_unit = info_ptr;
63d06c5c 4306
98bfdba5
PA
4307 if (per_cu->cu == NULL)
4308 {
9816fde3
JK
4309 cu = xmalloc (sizeof (*cu));
4310 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4311
4312 read_cu = 1;
c906108c 4313
98bfdba5
PA
4314 /* If an error occurs while loading, release our storage. */
4315 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4316
98bfdba5
PA
4317 /* Read in the comp_unit header. */
4318 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4319
98bfdba5
PA
4320 /* Complete the cu_header. */
4321 cu->header.offset = offset;
4322 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4323
98bfdba5
PA
4324 /* Read the abbrevs for this compilation unit. */
4325 dwarf2_read_abbrevs (abfd, cu);
4326 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4327
98bfdba5
PA
4328 /* Link this compilation unit into the compilation unit tree. */
4329 per_cu->cu = cu;
4330 cu->per_cu = per_cu;
98bfdba5
PA
4331
4332 /* Link this CU into read_in_chain. */
4333 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4334 dwarf2_per_objfile->read_in_chain = per_cu;
4335 }
4336 else
4337 {
4338 cu = per_cu->cu;
4339 info_ptr += cu->header.first_die_offset;
4340 }
e142c38c 4341
93311388 4342 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4343
4344 /* We try not to read any attributes in this function, because not
4345 all objfiles needed for references have been loaded yet, and symbol
4346 table processing isn't initialized. But we have to set the CU language,
4347 or we won't be able to build types correctly. */
9816fde3 4348 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4349
a6c727b2
DJ
4350 /* Similarly, if we do not read the producer, we can not apply
4351 producer-specific interpretation. */
4352 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4353 if (attr)
4354 cu->producer = DW_STRING (attr);
4355
98bfdba5
PA
4356 if (read_cu)
4357 {
4358 do_cleanups (free_abbrevs_cleanup);
e142c38c 4359
98bfdba5
PA
4360 /* We've successfully allocated this compilation unit. Let our
4361 caller clean it up when finished with it. */
4362 discard_cleanups (free_cu_cleanup);
4363 }
10b3939b
DJ
4364}
4365
3da10d80
KS
4366/* Add a DIE to the delayed physname list. */
4367
4368static void
4369add_to_method_list (struct type *type, int fnfield_index, int index,
4370 const char *name, struct die_info *die,
4371 struct dwarf2_cu *cu)
4372{
4373 struct delayed_method_info mi;
4374 mi.type = type;
4375 mi.fnfield_index = fnfield_index;
4376 mi.index = index;
4377 mi.name = name;
4378 mi.die = die;
4379 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4380}
4381
4382/* A cleanup for freeing the delayed method list. */
4383
4384static void
4385free_delayed_list (void *ptr)
4386{
4387 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4388 if (cu->method_list != NULL)
4389 {
4390 VEC_free (delayed_method_info, cu->method_list);
4391 cu->method_list = NULL;
4392 }
4393}
4394
4395/* Compute the physnames of any methods on the CU's method list.
4396
4397 The computation of method physnames is delayed in order to avoid the
4398 (bad) condition that one of the method's formal parameters is of an as yet
4399 incomplete type. */
4400
4401static void
4402compute_delayed_physnames (struct dwarf2_cu *cu)
4403{
4404 int i;
4405 struct delayed_method_info *mi;
4406 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4407 {
4408 char *physname;
4409 struct fn_fieldlist *fn_flp
4410 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4411 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4412 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4413 }
4414}
4415
10b3939b
DJ
4416/* Generate full symbol information for PST and CU, whose DIEs have
4417 already been loaded into memory. */
4418
4419static void
4420process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4421{
10b3939b 4422 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4423 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4424 CORE_ADDR lowpc, highpc;
4425 struct symtab *symtab;
3da10d80 4426 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4427 CORE_ADDR baseaddr;
4428
4429 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4430
10b3939b
DJ
4431 buildsym_init ();
4432 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4433 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4434
4435 cu->list_in_scope = &file_symbols;
c906108c 4436
d85a05f0 4437 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4438
c906108c 4439 /* Do line number decoding in read_file_scope () */
10b3939b 4440 process_die (cu->dies, cu);
c906108c 4441
3da10d80
KS
4442 /* Now that we have processed all the DIEs in the CU, all the types
4443 should be complete, and it should now be safe to compute all of the
4444 physnames. */
4445 compute_delayed_physnames (cu);
4446 do_cleanups (delayed_list_cleanup);
4447
fae299cd
DC
4448 /* Some compilers don't define a DW_AT_high_pc attribute for the
4449 compilation unit. If the DW_AT_high_pc is missing, synthesize
4450 it, by scanning the DIE's below the compilation unit. */
10b3939b 4451 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4452
613e1657 4453 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4454
4455 /* Set symtab language to language from DW_AT_language.
4456 If the compilation is from a C file generated by language preprocessors,
4457 do not set the language if it was already deduced by start_subfile. */
4458 if (symtab != NULL
10b3939b 4459 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4460 {
10b3939b 4461 symtab->language = cu->language;
c906108c 4462 }
9291a0cd
TT
4463
4464 if (dwarf2_per_objfile->using_index)
4465 per_cu->v.quick->symtab = symtab;
4466 else
4467 {
4468 struct partial_symtab *pst = per_cu->v.psymtab;
4469 pst->symtab = symtab;
4470 pst->readin = 1;
4471 }
c906108c
SS
4472
4473 do_cleanups (back_to);
4474}
4475
4476/* Process a die and its children. */
4477
4478static void
e7c27a73 4479process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4480{
4481 switch (die->tag)
4482 {
4483 case DW_TAG_padding:
4484 break;
4485 case DW_TAG_compile_unit:
e7c27a73 4486 read_file_scope (die, cu);
c906108c 4487 break;
348e048f
DE
4488 case DW_TAG_type_unit:
4489 read_type_unit_scope (die, cu);
4490 break;
c906108c 4491 case DW_TAG_subprogram:
c906108c 4492 case DW_TAG_inlined_subroutine:
edb3359d 4493 read_func_scope (die, cu);
c906108c
SS
4494 break;
4495 case DW_TAG_lexical_block:
14898363
L
4496 case DW_TAG_try_block:
4497 case DW_TAG_catch_block:
e7c27a73 4498 read_lexical_block_scope (die, cu);
c906108c
SS
4499 break;
4500 case DW_TAG_class_type:
680b30c7 4501 case DW_TAG_interface_type:
c906108c
SS
4502 case DW_TAG_structure_type:
4503 case DW_TAG_union_type:
134d01f1 4504 process_structure_scope (die, cu);
c906108c
SS
4505 break;
4506 case DW_TAG_enumeration_type:
134d01f1 4507 process_enumeration_scope (die, cu);
c906108c 4508 break;
134d01f1 4509
f792889a
DJ
4510 /* These dies have a type, but processing them does not create
4511 a symbol or recurse to process the children. Therefore we can
4512 read them on-demand through read_type_die. */
c906108c 4513 case DW_TAG_subroutine_type:
72019c9c 4514 case DW_TAG_set_type:
c906108c 4515 case DW_TAG_array_type:
c906108c 4516 case DW_TAG_pointer_type:
c906108c 4517 case DW_TAG_ptr_to_member_type:
c906108c 4518 case DW_TAG_reference_type:
c906108c 4519 case DW_TAG_string_type:
c906108c 4520 break;
134d01f1 4521
c906108c 4522 case DW_TAG_base_type:
a02abb62 4523 case DW_TAG_subrange_type:
cb249c71 4524 case DW_TAG_typedef:
134d01f1
DJ
4525 /* Add a typedef symbol for the type definition, if it has a
4526 DW_AT_name. */
f792889a 4527 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4528 break;
c906108c 4529 case DW_TAG_common_block:
e7c27a73 4530 read_common_block (die, cu);
c906108c
SS
4531 break;
4532 case DW_TAG_common_inclusion:
4533 break;
d9fa45fe 4534 case DW_TAG_namespace:
63d06c5c 4535 processing_has_namespace_info = 1;
e7c27a73 4536 read_namespace (die, cu);
d9fa45fe 4537 break;
5d7cb8df 4538 case DW_TAG_module:
f55ee35c 4539 processing_has_namespace_info = 1;
5d7cb8df
JK
4540 read_module (die, cu);
4541 break;
d9fa45fe
DC
4542 case DW_TAG_imported_declaration:
4543 case DW_TAG_imported_module:
63d06c5c 4544 processing_has_namespace_info = 1;
27aa8d6a
SW
4545 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4546 || cu->language != language_fortran))
4547 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4548 dwarf_tag_name (die->tag));
4549 read_import_statement (die, cu);
d9fa45fe 4550 break;
c906108c 4551 default:
e7c27a73 4552 new_symbol (die, NULL, cu);
c906108c
SS
4553 break;
4554 }
4555}
4556
94af9270
KS
4557/* A helper function for dwarf2_compute_name which determines whether DIE
4558 needs to have the name of the scope prepended to the name listed in the
4559 die. */
4560
4561static int
4562die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4563{
1c809c68
TT
4564 struct attribute *attr;
4565
94af9270
KS
4566 switch (die->tag)
4567 {
4568 case DW_TAG_namespace:
4569 case DW_TAG_typedef:
4570 case DW_TAG_class_type:
4571 case DW_TAG_interface_type:
4572 case DW_TAG_structure_type:
4573 case DW_TAG_union_type:
4574 case DW_TAG_enumeration_type:
4575 case DW_TAG_enumerator:
4576 case DW_TAG_subprogram:
4577 case DW_TAG_member:
4578 return 1;
4579
4580 case DW_TAG_variable:
c2b0a229 4581 case DW_TAG_constant:
94af9270
KS
4582 /* We only need to prefix "globally" visible variables. These include
4583 any variable marked with DW_AT_external or any variable that
4584 lives in a namespace. [Variables in anonymous namespaces
4585 require prefixing, but they are not DW_AT_external.] */
4586
4587 if (dwarf2_attr (die, DW_AT_specification, cu))
4588 {
4589 struct dwarf2_cu *spec_cu = cu;
9a619af0 4590
94af9270
KS
4591 return die_needs_namespace (die_specification (die, &spec_cu),
4592 spec_cu);
4593 }
4594
1c809c68 4595 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4596 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4597 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4598 return 0;
4599 /* A variable in a lexical block of some kind does not need a
4600 namespace, even though in C++ such variables may be external
4601 and have a mangled name. */
4602 if (die->parent->tag == DW_TAG_lexical_block
4603 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4604 || die->parent->tag == DW_TAG_catch_block
4605 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4606 return 0;
4607 return 1;
94af9270
KS
4608
4609 default:
4610 return 0;
4611 }
4612}
4613
98bfdba5
PA
4614/* Retrieve the last character from a mem_file. */
4615
4616static void
4617do_ui_file_peek_last (void *object, const char *buffer, long length)
4618{
4619 char *last_char_p = (char *) object;
4620
4621 if (length > 0)
4622 *last_char_p = buffer[length - 1];
4623}
4624
94af9270
KS
4625/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4626 compute the physname for the object, which include a method's
4627 formal parameters (C++/Java) and return type (Java).
4628
af6b7be1
JB
4629 For Ada, return the DIE's linkage name rather than the fully qualified
4630 name. PHYSNAME is ignored..
4631
94af9270
KS
4632 The result is allocated on the objfile_obstack and canonicalized. */
4633
4634static const char *
4635dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4636 int physname)
4637{
4638 if (name == NULL)
4639 name = dwarf2_name (die, cu);
4640
f55ee35c
JK
4641 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4642 compute it by typename_concat inside GDB. */
4643 if (cu->language == language_ada
4644 || (cu->language == language_fortran && physname))
4645 {
4646 /* For Ada unit, we prefer the linkage name over the name, as
4647 the former contains the exported name, which the user expects
4648 to be able to reference. Ideally, we want the user to be able
4649 to reference this entity using either natural or linkage name,
4650 but we haven't started looking at this enhancement yet. */
4651 struct attribute *attr;
4652
4653 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4654 if (attr == NULL)
4655 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4656 if (attr && DW_STRING (attr))
4657 return DW_STRING (attr);
4658 }
4659
94af9270
KS
4660 /* These are the only languages we know how to qualify names in. */
4661 if (name != NULL
f55ee35c
JK
4662 && (cu->language == language_cplus || cu->language == language_java
4663 || cu->language == language_fortran))
94af9270
KS
4664 {
4665 if (die_needs_namespace (die, cu))
4666 {
4667 long length;
4668 char *prefix;
4669 struct ui_file *buf;
4670
4671 prefix = determine_prefix (die, cu);
4672 buf = mem_fileopen ();
4673 if (*prefix != '\0')
4674 {
f55ee35c
JK
4675 char *prefixed_name = typename_concat (NULL, prefix, name,
4676 physname, cu);
9a619af0 4677
94af9270
KS
4678 fputs_unfiltered (prefixed_name, buf);
4679 xfree (prefixed_name);
4680 }
4681 else
4682 fputs_unfiltered (name ? name : "", buf);
4683
98bfdba5
PA
4684 /* Template parameters may be specified in the DIE's DW_AT_name, or
4685 as children with DW_TAG_template_type_param or
4686 DW_TAG_value_type_param. If the latter, add them to the name
4687 here. If the name already has template parameters, then
4688 skip this step; some versions of GCC emit both, and
4689 it is more efficient to use the pre-computed name.
4690
4691 Something to keep in mind about this process: it is very
4692 unlikely, or in some cases downright impossible, to produce
4693 something that will match the mangled name of a function.
4694 If the definition of the function has the same debug info,
4695 we should be able to match up with it anyway. But fallbacks
4696 using the minimal symbol, for instance to find a method
4697 implemented in a stripped copy of libstdc++, will not work.
4698 If we do not have debug info for the definition, we will have to
4699 match them up some other way.
4700
4701 When we do name matching there is a related problem with function
4702 templates; two instantiated function templates are allowed to
4703 differ only by their return types, which we do not add here. */
4704
4705 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4706 {
4707 struct attribute *attr;
4708 struct die_info *child;
4709 int first = 1;
4710
4711 die->building_fullname = 1;
4712
4713 for (child = die->child; child != NULL; child = child->sibling)
4714 {
4715 struct type *type;
4716 long value;
4717 gdb_byte *bytes;
4718 struct dwarf2_locexpr_baton *baton;
4719 struct value *v;
4720
4721 if (child->tag != DW_TAG_template_type_param
4722 && child->tag != DW_TAG_template_value_param)
4723 continue;
4724
4725 if (first)
4726 {
4727 fputs_unfiltered ("<", buf);
4728 first = 0;
4729 }
4730 else
4731 fputs_unfiltered (", ", buf);
4732
4733 attr = dwarf2_attr (child, DW_AT_type, cu);
4734 if (attr == NULL)
4735 {
4736 complaint (&symfile_complaints,
4737 _("template parameter missing DW_AT_type"));
4738 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4739 continue;
4740 }
4741 type = die_type (child, cu);
4742
4743 if (child->tag == DW_TAG_template_type_param)
4744 {
4745 c_print_type (type, "", buf, -1, 0);
4746 continue;
4747 }
4748
4749 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4750 if (attr == NULL)
4751 {
4752 complaint (&symfile_complaints,
4753 _("template parameter missing DW_AT_const_value"));
4754 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4755 continue;
4756 }
4757
4758 dwarf2_const_value_attr (attr, type, name,
4759 &cu->comp_unit_obstack, cu,
4760 &value, &bytes, &baton);
4761
4762 if (TYPE_NOSIGN (type))
4763 /* GDB prints characters as NUMBER 'CHAR'. If that's
4764 changed, this can use value_print instead. */
4765 c_printchar (value, type, buf);
4766 else
4767 {
4768 struct value_print_options opts;
4769
4770 if (baton != NULL)
4771 v = dwarf2_evaluate_loc_desc (type, NULL,
4772 baton->data,
4773 baton->size,
4774 baton->per_cu);
4775 else if (bytes != NULL)
4776 {
4777 v = allocate_value (type);
4778 memcpy (value_contents_writeable (v), bytes,
4779 TYPE_LENGTH (type));
4780 }
4781 else
4782 v = value_from_longest (type, value);
4783
4784 /* Specify decimal so that we do not depend on the radix. */
4785 get_formatted_print_options (&opts, 'd');
4786 opts.raw = 1;
4787 value_print (v, buf, &opts);
4788 release_value (v);
4789 value_free (v);
4790 }
4791 }
4792
4793 die->building_fullname = 0;
4794
4795 if (!first)
4796 {
4797 /* Close the argument list, with a space if necessary
4798 (nested templates). */
4799 char last_char = '\0';
4800 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4801 if (last_char == '>')
4802 fputs_unfiltered (" >", buf);
4803 else
4804 fputs_unfiltered (">", buf);
4805 }
4806 }
4807
94af9270
KS
4808 /* For Java and C++ methods, append formal parameter type
4809 information, if PHYSNAME. */
6e70227d 4810
94af9270
KS
4811 if (physname && die->tag == DW_TAG_subprogram
4812 && (cu->language == language_cplus
4813 || cu->language == language_java))
4814 {
4815 struct type *type = read_type_die (die, cu);
4816
4817 c_type_print_args (type, buf, 0, cu->language);
4818
4819 if (cu->language == language_java)
4820 {
4821 /* For java, we must append the return type to method
4822 names. */
4823 if (die->tag == DW_TAG_subprogram)
4824 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4825 0, 0);
4826 }
4827 else if (cu->language == language_cplus)
4828 {
60430eff
DJ
4829 /* Assume that an artificial first parameter is
4830 "this", but do not crash if it is not. RealView
4831 marks unnamed (and thus unused) parameters as
4832 artificial; there is no way to differentiate
4833 the two cases. */
94af9270
KS
4834 if (TYPE_NFIELDS (type) > 0
4835 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4836 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4837 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4838 fputs_unfiltered (" const", buf);
4839 }
4840 }
4841
4842 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4843 &length);
4844 ui_file_delete (buf);
4845
4846 if (cu->language == language_cplus)
4847 {
4848 char *cname
4849 = dwarf2_canonicalize_name (name, cu,
4850 &cu->objfile->objfile_obstack);
9a619af0 4851
94af9270
KS
4852 if (cname != NULL)
4853 name = cname;
4854 }
4855 }
4856 }
4857
4858 return name;
4859}
4860
0114d602
DJ
4861/* Return the fully qualified name of DIE, based on its DW_AT_name.
4862 If scope qualifiers are appropriate they will be added. The result
4863 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4864 not have a name. NAME may either be from a previous call to
4865 dwarf2_name or NULL.
4866
4867 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4868
4869static const char *
94af9270 4870dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4871{
94af9270
KS
4872 return dwarf2_compute_name (name, die, cu, 0);
4873}
0114d602 4874
94af9270
KS
4875/* Construct a physname for the given DIE in CU. NAME may either be
4876 from a previous call to dwarf2_name or NULL. The result will be
4877 allocated on the objfile_objstack or NULL if the DIE does not have a
4878 name.
0114d602 4879
94af9270 4880 The output string will be canonicalized (if C++/Java). */
0114d602 4881
94af9270
KS
4882static const char *
4883dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4884{
4885 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4886}
4887
27aa8d6a
SW
4888/* Read the import statement specified by the given die and record it. */
4889
4890static void
4891read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4892{
4893 struct attribute *import_attr;
4894 struct die_info *imported_die;
de4affc9 4895 struct dwarf2_cu *imported_cu;
27aa8d6a 4896 const char *imported_name;
794684b6 4897 const char *imported_name_prefix;
13387711
SW
4898 const char *canonical_name;
4899 const char *import_alias;
4900 const char *imported_declaration = NULL;
794684b6 4901 const char *import_prefix;
13387711
SW
4902
4903 char *temp;
27aa8d6a
SW
4904
4905 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4906 if (import_attr == NULL)
4907 {
4908 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4909 dwarf_tag_name (die->tag));
4910 return;
4911 }
4912
de4affc9
CC
4913 imported_cu = cu;
4914 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4915 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4916 if (imported_name == NULL)
4917 {
4918 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4919
4920 The import in the following code:
4921 namespace A
4922 {
4923 typedef int B;
4924 }
4925
4926 int main ()
4927 {
4928 using A::B;
4929 B b;
4930 return b;
4931 }
4932
4933 ...
4934 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4935 <52> DW_AT_decl_file : 1
4936 <53> DW_AT_decl_line : 6
4937 <54> DW_AT_import : <0x75>
4938 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4939 <59> DW_AT_name : B
4940 <5b> DW_AT_decl_file : 1
4941 <5c> DW_AT_decl_line : 2
4942 <5d> DW_AT_type : <0x6e>
4943 ...
4944 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4945 <76> DW_AT_byte_size : 4
4946 <77> DW_AT_encoding : 5 (signed)
4947
4948 imports the wrong die ( 0x75 instead of 0x58 ).
4949 This case will be ignored until the gcc bug is fixed. */
4950 return;
4951 }
4952
82856980
SW
4953 /* Figure out the local name after import. */
4954 import_alias = dwarf2_name (die, cu);
27aa8d6a 4955
794684b6
SW
4956 /* Figure out where the statement is being imported to. */
4957 import_prefix = determine_prefix (die, cu);
4958
4959 /* Figure out what the scope of the imported die is and prepend it
4960 to the name of the imported die. */
de4affc9 4961 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4962
f55ee35c
JK
4963 if (imported_die->tag != DW_TAG_namespace
4964 && imported_die->tag != DW_TAG_module)
794684b6 4965 {
13387711
SW
4966 imported_declaration = imported_name;
4967 canonical_name = imported_name_prefix;
794684b6 4968 }
13387711 4969 else if (strlen (imported_name_prefix) > 0)
794684b6 4970 {
13387711
SW
4971 temp = alloca (strlen (imported_name_prefix)
4972 + 2 + strlen (imported_name) + 1);
4973 strcpy (temp, imported_name_prefix);
4974 strcat (temp, "::");
4975 strcat (temp, imported_name);
4976 canonical_name = temp;
794684b6 4977 }
13387711
SW
4978 else
4979 canonical_name = imported_name;
794684b6 4980
c0cc3a76
SW
4981 cp_add_using_directive (import_prefix,
4982 canonical_name,
4983 import_alias,
13387711 4984 imported_declaration,
c0cc3a76 4985 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4986}
4987
5fb290d7 4988static void
e142c38c 4989initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4990{
e142c38c 4991 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4992}
4993
cb1df416
DJ
4994static void
4995free_cu_line_header (void *arg)
4996{
4997 struct dwarf2_cu *cu = arg;
4998
4999 free_line_header (cu->line_header);
5000 cu->line_header = NULL;
5001}
5002
9291a0cd
TT
5003static void
5004find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5005 char **name, char **comp_dir)
5006{
5007 struct attribute *attr;
5008
5009 *name = NULL;
5010 *comp_dir = NULL;
5011
5012 /* Find the filename. Do not use dwarf2_name here, since the filename
5013 is not a source language identifier. */
5014 attr = dwarf2_attr (die, DW_AT_name, cu);
5015 if (attr)
5016 {
5017 *name = DW_STRING (attr);
5018 }
5019
5020 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5021 if (attr)
5022 *comp_dir = DW_STRING (attr);
5023 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5024 {
5025 *comp_dir = ldirname (*name);
5026 if (*comp_dir != NULL)
5027 make_cleanup (xfree, *comp_dir);
5028 }
5029 if (*comp_dir != NULL)
5030 {
5031 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5032 directory, get rid of it. */
5033 char *cp = strchr (*comp_dir, ':');
5034
5035 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5036 *comp_dir = cp + 1;
5037 }
5038
5039 if (*name == NULL)
5040 *name = "<unknown>";
5041}
5042
c906108c 5043static void
e7c27a73 5044read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5045{
e7c27a73 5046 struct objfile *objfile = cu->objfile;
debd256d 5047 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5048 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5049 CORE_ADDR highpc = ((CORE_ADDR) 0);
5050 struct attribute *attr;
e1024ff1 5051 char *name = NULL;
c906108c
SS
5052 char *comp_dir = NULL;
5053 struct die_info *child_die;
5054 bfd *abfd = objfile->obfd;
debd256d 5055 struct line_header *line_header = 0;
e142c38c 5056 CORE_ADDR baseaddr;
6e70227d 5057
e142c38c 5058 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5059
fae299cd 5060 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5061
5062 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5063 from finish_block. */
2acceee2 5064 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5065 lowpc = highpc;
5066 lowpc += baseaddr;
5067 highpc += baseaddr;
5068
9291a0cd 5069 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5070
e142c38c 5071 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5072 if (attr)
5073 {
e142c38c 5074 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5075 }
5076
b0f35d58 5077 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5078 if (attr)
b0f35d58 5079 cu->producer = DW_STRING (attr);
303b6f5d 5080
f4b8a18d
KW
5081 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5082 standardised yet. As a workaround for the language detection we fall
5083 back to the DW_AT_producer string. */
5084 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5085 cu->language = language_opencl;
5086
c906108c
SS
5087 /* We assume that we're processing GCC output. */
5088 processing_gcc_compilation = 2;
c906108c 5089
df8a16a1
DJ
5090 processing_has_namespace_info = 0;
5091
c906108c
SS
5092 start_symtab (name, comp_dir, lowpc);
5093 record_debugformat ("DWARF 2");
303b6f5d 5094 record_producer (cu->producer);
c906108c 5095
e142c38c 5096 initialize_cu_func_list (cu);
c906108c 5097
cb1df416
DJ
5098 /* Decode line number information if present. We do this before
5099 processing child DIEs, so that the line header table is available
5100 for DW_AT_decl_file. */
e142c38c 5101 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5102 if (attr)
5103 {
debd256d 5104 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5105 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5106 if (line_header)
5107 {
cb1df416
DJ
5108 cu->line_header = line_header;
5109 make_cleanup (free_cu_line_header, cu);
aaa75496 5110 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5111 }
5fb290d7 5112 }
debd256d 5113
cb1df416
DJ
5114 /* Process all dies in compilation unit. */
5115 if (die->child != NULL)
5116 {
5117 child_die = die->child;
5118 while (child_die && child_die->tag)
5119 {
5120 process_die (child_die, cu);
5121 child_die = sibling_die (child_die);
5122 }
5123 }
5124
2e276125
JB
5125 /* Decode macro information, if present. Dwarf 2 macro information
5126 refers to information in the line number info statement program
5127 header, so we can only read it if we've read the header
5128 successfully. */
e142c38c 5129 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5130 if (attr && line_header)
2e276125
JB
5131 {
5132 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5133
2e276125 5134 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5135 comp_dir, abfd, cu);
2e276125 5136 }
debd256d 5137 do_cleanups (back_to);
5fb290d7
DJ
5138}
5139
348e048f
DE
5140/* For TUs we want to skip the first top level sibling if it's not the
5141 actual type being defined by this TU. In this case the first top
5142 level sibling is there to provide context only. */
5143
5144static void
5145read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5146{
5147 struct objfile *objfile = cu->objfile;
5148 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5149 CORE_ADDR lowpc;
5150 struct attribute *attr;
5151 char *name = NULL;
5152 char *comp_dir = NULL;
5153 struct die_info *child_die;
5154 bfd *abfd = objfile->obfd;
348e048f
DE
5155
5156 /* start_symtab needs a low pc, but we don't really have one.
5157 Do what read_file_scope would do in the absence of such info. */
5158 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5159
5160 /* Find the filename. Do not use dwarf2_name here, since the filename
5161 is not a source language identifier. */
5162 attr = dwarf2_attr (die, DW_AT_name, cu);
5163 if (attr)
5164 name = DW_STRING (attr);
5165
5166 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5167 if (attr)
5168 comp_dir = DW_STRING (attr);
5169 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5170 {
5171 comp_dir = ldirname (name);
5172 if (comp_dir != NULL)
5173 make_cleanup (xfree, comp_dir);
5174 }
5175
5176 if (name == NULL)
5177 name = "<unknown>";
5178
5179 attr = dwarf2_attr (die, DW_AT_language, cu);
5180 if (attr)
5181 set_cu_language (DW_UNSND (attr), cu);
5182
5183 /* This isn't technically needed today. It is done for symmetry
5184 with read_file_scope. */
5185 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5186 if (attr)
348e048f
DE
5187 cu->producer = DW_STRING (attr);
5188
5189 /* We assume that we're processing GCC output. */
5190 processing_gcc_compilation = 2;
5191
5192 processing_has_namespace_info = 0;
5193
5194 start_symtab (name, comp_dir, lowpc);
5195 record_debugformat ("DWARF 2");
5196 record_producer (cu->producer);
5197
5198 /* Process the dies in the type unit. */
5199 if (die->child == NULL)
5200 {
5201 dump_die_for_error (die);
5202 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5203 bfd_get_filename (abfd));
5204 }
5205
5206 child_die = die->child;
5207
5208 while (child_die && child_die->tag)
5209 {
5210 process_die (child_die, cu);
5211
5212 child_die = sibling_die (child_die);
5213 }
5214
5215 do_cleanups (back_to);
5216}
5217
5fb290d7 5218static void
e142c38c
DJ
5219add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5220 struct dwarf2_cu *cu)
5fb290d7
DJ
5221{
5222 struct function_range *thisfn;
5223
5224 thisfn = (struct function_range *)
7b5a2f43 5225 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5226 thisfn->name = name;
5227 thisfn->lowpc = lowpc;
5228 thisfn->highpc = highpc;
5229 thisfn->seen_line = 0;
5230 thisfn->next = NULL;
5231
e142c38c
DJ
5232 if (cu->last_fn == NULL)
5233 cu->first_fn = thisfn;
5fb290d7 5234 else
e142c38c 5235 cu->last_fn->next = thisfn;
5fb290d7 5236
e142c38c 5237 cu->last_fn = thisfn;
c906108c
SS
5238}
5239
d389af10
JK
5240/* qsort helper for inherit_abstract_dies. */
5241
5242static int
5243unsigned_int_compar (const void *ap, const void *bp)
5244{
5245 unsigned int a = *(unsigned int *) ap;
5246 unsigned int b = *(unsigned int *) bp;
5247
5248 return (a > b) - (b > a);
5249}
5250
5251/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5252 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5253 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5254
5255static void
5256inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5257{
5258 struct die_info *child_die;
5259 unsigned die_children_count;
5260 /* CU offsets which were referenced by children of the current DIE. */
5261 unsigned *offsets;
5262 unsigned *offsets_end, *offsetp;
5263 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5264 struct die_info *origin_die;
5265 /* Iterator of the ORIGIN_DIE children. */
5266 struct die_info *origin_child_die;
5267 struct cleanup *cleanups;
5268 struct attribute *attr;
cd02d79d
PA
5269 struct dwarf2_cu *origin_cu;
5270 struct pending **origin_previous_list_in_scope;
d389af10
JK
5271
5272 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5273 if (!attr)
5274 return;
5275
cd02d79d
PA
5276 /* Note that following die references may follow to a die in a
5277 different cu. */
5278
5279 origin_cu = cu;
5280 origin_die = follow_die_ref (die, attr, &origin_cu);
5281
5282 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5283 symbols in. */
5284 origin_previous_list_in_scope = origin_cu->list_in_scope;
5285 origin_cu->list_in_scope = cu->list_in_scope;
5286
edb3359d
DJ
5287 if (die->tag != origin_die->tag
5288 && !(die->tag == DW_TAG_inlined_subroutine
5289 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5290 complaint (&symfile_complaints,
5291 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5292 die->offset, origin_die->offset);
5293
5294 child_die = die->child;
5295 die_children_count = 0;
5296 while (child_die && child_die->tag)
5297 {
5298 child_die = sibling_die (child_die);
5299 die_children_count++;
5300 }
5301 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5302 cleanups = make_cleanup (xfree, offsets);
5303
5304 offsets_end = offsets;
5305 child_die = die->child;
5306 while (child_die && child_die->tag)
5307 {
c38f313d
DJ
5308 /* For each CHILD_DIE, find the corresponding child of
5309 ORIGIN_DIE. If there is more than one layer of
5310 DW_AT_abstract_origin, follow them all; there shouldn't be,
5311 but GCC versions at least through 4.4 generate this (GCC PR
5312 40573). */
5313 struct die_info *child_origin_die = child_die;
cd02d79d 5314 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5315
c38f313d
DJ
5316 while (1)
5317 {
cd02d79d
PA
5318 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5319 child_origin_cu);
c38f313d
DJ
5320 if (attr == NULL)
5321 break;
cd02d79d
PA
5322 child_origin_die = follow_die_ref (child_origin_die, attr,
5323 &child_origin_cu);
c38f313d
DJ
5324 }
5325
d389af10
JK
5326 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5327 counterpart may exist. */
c38f313d 5328 if (child_origin_die != child_die)
d389af10 5329 {
edb3359d
DJ
5330 if (child_die->tag != child_origin_die->tag
5331 && !(child_die->tag == DW_TAG_inlined_subroutine
5332 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5333 complaint (&symfile_complaints,
5334 _("Child DIE 0x%x and its abstract origin 0x%x have "
5335 "different tags"), child_die->offset,
5336 child_origin_die->offset);
c38f313d
DJ
5337 if (child_origin_die->parent != origin_die)
5338 complaint (&symfile_complaints,
5339 _("Child DIE 0x%x and its abstract origin 0x%x have "
5340 "different parents"), child_die->offset,
5341 child_origin_die->offset);
5342 else
5343 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5344 }
5345 child_die = sibling_die (child_die);
5346 }
5347 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5348 unsigned_int_compar);
5349 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5350 if (offsetp[-1] == *offsetp)
5351 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5352 "to DIE 0x%x as their abstract origin"),
5353 die->offset, *offsetp);
5354
5355 offsetp = offsets;
5356 origin_child_die = origin_die->child;
5357 while (origin_child_die && origin_child_die->tag)
5358 {
5359 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5360 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5361 offsetp++;
5362 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5363 {
5364 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5365 process_die (origin_child_die, origin_cu);
d389af10
JK
5366 }
5367 origin_child_die = sibling_die (origin_child_die);
5368 }
cd02d79d 5369 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5370
5371 do_cleanups (cleanups);
5372}
5373
c906108c 5374static void
e7c27a73 5375read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5376{
e7c27a73 5377 struct objfile *objfile = cu->objfile;
52f0bd74 5378 struct context_stack *new;
c906108c
SS
5379 CORE_ADDR lowpc;
5380 CORE_ADDR highpc;
5381 struct die_info *child_die;
edb3359d 5382 struct attribute *attr, *call_line, *call_file;
c906108c 5383 char *name;
e142c38c 5384 CORE_ADDR baseaddr;
801e3a5b 5385 struct block *block;
edb3359d 5386 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5387 VEC (symbolp) *template_args = NULL;
5388 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5389
5390 if (inlined_func)
5391 {
5392 /* If we do not have call site information, we can't show the
5393 caller of this inlined function. That's too confusing, so
5394 only use the scope for local variables. */
5395 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5396 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5397 if (call_line == NULL || call_file == NULL)
5398 {
5399 read_lexical_block_scope (die, cu);
5400 return;
5401 }
5402 }
c906108c 5403
e142c38c
DJ
5404 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5405
94af9270 5406 name = dwarf2_name (die, cu);
c906108c 5407
e8d05480
JB
5408 /* Ignore functions with missing or empty names. These are actually
5409 illegal according to the DWARF standard. */
5410 if (name == NULL)
5411 {
5412 complaint (&symfile_complaints,
5413 _("missing name for subprogram DIE at %d"), die->offset);
5414 return;
5415 }
5416
5417 /* Ignore functions with missing or invalid low and high pc attributes. */
5418 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5419 {
ae4d0c03
PM
5420 attr = dwarf2_attr (die, DW_AT_external, cu);
5421 if (!attr || !DW_UNSND (attr))
5422 complaint (&symfile_complaints,
5423 _("cannot get low and high bounds for subprogram DIE at %d"),
5424 die->offset);
e8d05480
JB
5425 return;
5426 }
c906108c
SS
5427
5428 lowpc += baseaddr;
5429 highpc += baseaddr;
5430
5fb290d7 5431 /* Record the function range for dwarf_decode_lines. */
e142c38c 5432 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5433
34eaf542
TT
5434 /* If we have any template arguments, then we must allocate a
5435 different sort of symbol. */
5436 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5437 {
5438 if (child_die->tag == DW_TAG_template_type_param
5439 || child_die->tag == DW_TAG_template_value_param)
5440 {
5441 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5442 struct template_symbol);
5443 templ_func->base.is_cplus_template_function = 1;
5444 break;
5445 }
5446 }
5447
c906108c 5448 new = push_context (0, lowpc);
34eaf542
TT
5449 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5450 (struct symbol *) templ_func);
4c2df51b 5451
4cecd739
DJ
5452 /* If there is a location expression for DW_AT_frame_base, record
5453 it. */
e142c38c 5454 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5455 if (attr)
c034e007
AC
5456 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5457 expression is being recorded directly in the function's symbol
5458 and not in a separate frame-base object. I guess this hack is
5459 to avoid adding some sort of frame-base adjunct/annex to the
5460 function's symbol :-(. The problem with doing this is that it
5461 results in a function symbol with a location expression that
5462 has nothing to do with the location of the function, ouch! The
5463 relationship should be: a function's symbol has-a frame base; a
5464 frame-base has-a location expression. */
e7c27a73 5465 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5466
e142c38c 5467 cu->list_in_scope = &local_symbols;
c906108c 5468
639d11d3 5469 if (die->child != NULL)
c906108c 5470 {
639d11d3 5471 child_die = die->child;
c906108c
SS
5472 while (child_die && child_die->tag)
5473 {
34eaf542
TT
5474 if (child_die->tag == DW_TAG_template_type_param
5475 || child_die->tag == DW_TAG_template_value_param)
5476 {
5477 struct symbol *arg = new_symbol (child_die, NULL, cu);
5478
f1078f66
DJ
5479 if (arg != NULL)
5480 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5481 }
5482 else
5483 process_die (child_die, cu);
c906108c
SS
5484 child_die = sibling_die (child_die);
5485 }
5486 }
5487
d389af10
JK
5488 inherit_abstract_dies (die, cu);
5489
4a811a97
UW
5490 /* If we have a DW_AT_specification, we might need to import using
5491 directives from the context of the specification DIE. See the
5492 comment in determine_prefix. */
5493 if (cu->language == language_cplus
5494 && dwarf2_attr (die, DW_AT_specification, cu))
5495 {
5496 struct dwarf2_cu *spec_cu = cu;
5497 struct die_info *spec_die = die_specification (die, &spec_cu);
5498
5499 while (spec_die)
5500 {
5501 child_die = spec_die->child;
5502 while (child_die && child_die->tag)
5503 {
5504 if (child_die->tag == DW_TAG_imported_module)
5505 process_die (child_die, spec_cu);
5506 child_die = sibling_die (child_die);
5507 }
5508
5509 /* In some cases, GCC generates specification DIEs that
5510 themselves contain DW_AT_specification attributes. */
5511 spec_die = die_specification (spec_die, &spec_cu);
5512 }
5513 }
5514
c906108c
SS
5515 new = pop_context ();
5516 /* Make a block for the local symbols within. */
801e3a5b
JB
5517 block = finish_block (new->name, &local_symbols, new->old_blocks,
5518 lowpc, highpc, objfile);
5519
df8a16a1 5520 /* For C++, set the block's scope. */
f55ee35c 5521 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5522 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5523 determine_prefix (die, cu),
df8a16a1
DJ
5524 processing_has_namespace_info);
5525
801e3a5b
JB
5526 /* If we have address ranges, record them. */
5527 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5528
34eaf542
TT
5529 /* Attach template arguments to function. */
5530 if (! VEC_empty (symbolp, template_args))
5531 {
5532 gdb_assert (templ_func != NULL);
5533
5534 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5535 templ_func->template_arguments
5536 = obstack_alloc (&objfile->objfile_obstack,
5537 (templ_func->n_template_arguments
5538 * sizeof (struct symbol *)));
5539 memcpy (templ_func->template_arguments,
5540 VEC_address (symbolp, template_args),
5541 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5542 VEC_free (symbolp, template_args);
5543 }
5544
208d8187
JB
5545 /* In C++, we can have functions nested inside functions (e.g., when
5546 a function declares a class that has methods). This means that
5547 when we finish processing a function scope, we may need to go
5548 back to building a containing block's symbol lists. */
5549 local_symbols = new->locals;
5550 param_symbols = new->params;
27aa8d6a 5551 using_directives = new->using_directives;
208d8187 5552
921e78cf
JB
5553 /* If we've finished processing a top-level function, subsequent
5554 symbols go in the file symbol list. */
5555 if (outermost_context_p ())
e142c38c 5556 cu->list_in_scope = &file_symbols;
c906108c
SS
5557}
5558
5559/* Process all the DIES contained within a lexical block scope. Start
5560 a new scope, process the dies, and then close the scope. */
5561
5562static void
e7c27a73 5563read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5564{
e7c27a73 5565 struct objfile *objfile = cu->objfile;
52f0bd74 5566 struct context_stack *new;
c906108c
SS
5567 CORE_ADDR lowpc, highpc;
5568 struct die_info *child_die;
e142c38c
DJ
5569 CORE_ADDR baseaddr;
5570
5571 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5572
5573 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5574 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5575 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5576 be nasty. Might be easier to properly extend generic blocks to
af34e669 5577 describe ranges. */
d85a05f0 5578 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5579 return;
5580 lowpc += baseaddr;
5581 highpc += baseaddr;
5582
5583 push_context (0, lowpc);
639d11d3 5584 if (die->child != NULL)
c906108c 5585 {
639d11d3 5586 child_die = die->child;
c906108c
SS
5587 while (child_die && child_die->tag)
5588 {
e7c27a73 5589 process_die (child_die, cu);
c906108c
SS
5590 child_die = sibling_die (child_die);
5591 }
5592 }
5593 new = pop_context ();
5594
8540c487 5595 if (local_symbols != NULL || using_directives != NULL)
c906108c 5596 {
801e3a5b
JB
5597 struct block *block
5598 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5599 highpc, objfile);
5600
5601 /* Note that recording ranges after traversing children, as we
5602 do here, means that recording a parent's ranges entails
5603 walking across all its children's ranges as they appear in
5604 the address map, which is quadratic behavior.
5605
5606 It would be nicer to record the parent's ranges before
5607 traversing its children, simply overriding whatever you find
5608 there. But since we don't even decide whether to create a
5609 block until after we've traversed its children, that's hard
5610 to do. */
5611 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5612 }
5613 local_symbols = new->locals;
27aa8d6a 5614 using_directives = new->using_directives;
c906108c
SS
5615}
5616
43039443 5617/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5618 Return 1 if the attributes are present and valid, otherwise, return 0.
5619 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5620
5621static int
5622dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5623 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5624 struct partial_symtab *ranges_pst)
43039443
JK
5625{
5626 struct objfile *objfile = cu->objfile;
5627 struct comp_unit_head *cu_header = &cu->header;
5628 bfd *obfd = objfile->obfd;
5629 unsigned int addr_size = cu_header->addr_size;
5630 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5631 /* Base address selection entry. */
5632 CORE_ADDR base;
5633 int found_base;
5634 unsigned int dummy;
5635 gdb_byte *buffer;
5636 CORE_ADDR marker;
5637 int low_set;
5638 CORE_ADDR low = 0;
5639 CORE_ADDR high = 0;
ff013f42 5640 CORE_ADDR baseaddr;
43039443 5641
d00adf39
DE
5642 found_base = cu->base_known;
5643 base = cu->base_address;
43039443 5644
be391dca 5645 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5646 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5647 {
5648 complaint (&symfile_complaints,
5649 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5650 offset);
5651 return 0;
5652 }
dce234bc 5653 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5654
5655 /* Read in the largest possible address. */
5656 marker = read_address (obfd, buffer, cu, &dummy);
5657 if ((marker & mask) == mask)
5658 {
5659 /* If we found the largest possible address, then
5660 read the base address. */
5661 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5662 buffer += 2 * addr_size;
5663 offset += 2 * addr_size;
5664 found_base = 1;
5665 }
5666
5667 low_set = 0;
5668
e7030f15 5669 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5670
43039443
JK
5671 while (1)
5672 {
5673 CORE_ADDR range_beginning, range_end;
5674
5675 range_beginning = read_address (obfd, buffer, cu, &dummy);
5676 buffer += addr_size;
5677 range_end = read_address (obfd, buffer, cu, &dummy);
5678 buffer += addr_size;
5679 offset += 2 * addr_size;
5680
5681 /* An end of list marker is a pair of zero addresses. */
5682 if (range_beginning == 0 && range_end == 0)
5683 /* Found the end of list entry. */
5684 break;
5685
5686 /* Each base address selection entry is a pair of 2 values.
5687 The first is the largest possible address, the second is
5688 the base address. Check for a base address here. */
5689 if ((range_beginning & mask) == mask)
5690 {
5691 /* If we found the largest possible address, then
5692 read the base address. */
5693 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5694 found_base = 1;
5695 continue;
5696 }
5697
5698 if (!found_base)
5699 {
5700 /* We have no valid base address for the ranges
5701 data. */
5702 complaint (&symfile_complaints,
5703 _("Invalid .debug_ranges data (no base address)"));
5704 return 0;
5705 }
5706
5707 range_beginning += base;
5708 range_end += base;
5709
ff013f42
JK
5710 if (ranges_pst != NULL && range_beginning < range_end)
5711 addrmap_set_empty (objfile->psymtabs_addrmap,
5712 range_beginning + baseaddr, range_end - 1 + baseaddr,
5713 ranges_pst);
5714
43039443
JK
5715 /* FIXME: This is recording everything as a low-high
5716 segment of consecutive addresses. We should have a
5717 data structure for discontiguous block ranges
5718 instead. */
5719 if (! low_set)
5720 {
5721 low = range_beginning;
5722 high = range_end;
5723 low_set = 1;
5724 }
5725 else
5726 {
5727 if (range_beginning < low)
5728 low = range_beginning;
5729 if (range_end > high)
5730 high = range_end;
5731 }
5732 }
5733
5734 if (! low_set)
5735 /* If the first entry is an end-of-list marker, the range
5736 describes an empty scope, i.e. no instructions. */
5737 return 0;
5738
5739 if (low_return)
5740 *low_return = low;
5741 if (high_return)
5742 *high_return = high;
5743 return 1;
5744}
5745
af34e669
DJ
5746/* Get low and high pc attributes from a die. Return 1 if the attributes
5747 are present and valid, otherwise, return 0. Return -1 if the range is
5748 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5749static int
af34e669 5750dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5751 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5752 struct partial_symtab *pst)
c906108c
SS
5753{
5754 struct attribute *attr;
af34e669
DJ
5755 CORE_ADDR low = 0;
5756 CORE_ADDR high = 0;
5757 int ret = 0;
c906108c 5758
e142c38c 5759 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5760 if (attr)
af34e669
DJ
5761 {
5762 high = DW_ADDR (attr);
e142c38c 5763 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5764 if (attr)
5765 low = DW_ADDR (attr);
5766 else
5767 /* Found high w/o low attribute. */
5768 return 0;
5769
5770 /* Found consecutive range of addresses. */
5771 ret = 1;
5772 }
c906108c 5773 else
af34e669 5774 {
e142c38c 5775 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5776 if (attr != NULL)
5777 {
af34e669 5778 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5779 .debug_ranges section. */
d85a05f0 5780 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5781 return 0;
43039443 5782 /* Found discontinuous range of addresses. */
af34e669
DJ
5783 ret = -1;
5784 }
5785 }
c906108c
SS
5786
5787 if (high < low)
5788 return 0;
5789
5790 /* When using the GNU linker, .gnu.linkonce. sections are used to
5791 eliminate duplicate copies of functions and vtables and such.
5792 The linker will arbitrarily choose one and discard the others.
5793 The AT_*_pc values for such functions refer to local labels in
5794 these sections. If the section from that file was discarded, the
5795 labels are not in the output, so the relocs get a value of 0.
5796 If this is a discarded function, mark the pc bounds as invalid,
5797 so that GDB will ignore it. */
72dca2f5 5798 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5799 return 0;
5800
5801 *lowpc = low;
5802 *highpc = high;
af34e669 5803 return ret;
c906108c
SS
5804}
5805
b084d499
JB
5806/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5807 its low and high PC addresses. Do nothing if these addresses could not
5808 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5809 and HIGHPC to the high address if greater than HIGHPC. */
5810
5811static void
5812dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5813 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5814 struct dwarf2_cu *cu)
5815{
5816 CORE_ADDR low, high;
5817 struct die_info *child = die->child;
5818
d85a05f0 5819 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5820 {
5821 *lowpc = min (*lowpc, low);
5822 *highpc = max (*highpc, high);
5823 }
5824
5825 /* If the language does not allow nested subprograms (either inside
5826 subprograms or lexical blocks), we're done. */
5827 if (cu->language != language_ada)
5828 return;
6e70227d 5829
b084d499
JB
5830 /* Check all the children of the given DIE. If it contains nested
5831 subprograms, then check their pc bounds. Likewise, we need to
5832 check lexical blocks as well, as they may also contain subprogram
5833 definitions. */
5834 while (child && child->tag)
5835 {
5836 if (child->tag == DW_TAG_subprogram
5837 || child->tag == DW_TAG_lexical_block)
5838 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5839 child = sibling_die (child);
5840 }
5841}
5842
fae299cd
DC
5843/* Get the low and high pc's represented by the scope DIE, and store
5844 them in *LOWPC and *HIGHPC. If the correct values can't be
5845 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5846
5847static void
5848get_scope_pc_bounds (struct die_info *die,
5849 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5850 struct dwarf2_cu *cu)
5851{
5852 CORE_ADDR best_low = (CORE_ADDR) -1;
5853 CORE_ADDR best_high = (CORE_ADDR) 0;
5854 CORE_ADDR current_low, current_high;
5855
d85a05f0 5856 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5857 {
5858 best_low = current_low;
5859 best_high = current_high;
5860 }
5861 else
5862 {
5863 struct die_info *child = die->child;
5864
5865 while (child && child->tag)
5866 {
5867 switch (child->tag) {
5868 case DW_TAG_subprogram:
b084d499 5869 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5870 break;
5871 case DW_TAG_namespace:
f55ee35c 5872 case DW_TAG_module:
fae299cd
DC
5873 /* FIXME: carlton/2004-01-16: Should we do this for
5874 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5875 that current GCC's always emit the DIEs corresponding
5876 to definitions of methods of classes as children of a
5877 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5878 the DIEs giving the declarations, which could be
5879 anywhere). But I don't see any reason why the
5880 standards says that they have to be there. */
5881 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5882
5883 if (current_low != ((CORE_ADDR) -1))
5884 {
5885 best_low = min (best_low, current_low);
5886 best_high = max (best_high, current_high);
5887 }
5888 break;
5889 default:
5890 /* Ignore. */
5891 break;
5892 }
5893
5894 child = sibling_die (child);
5895 }
5896 }
5897
5898 *lowpc = best_low;
5899 *highpc = best_high;
5900}
5901
801e3a5b
JB
5902/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5903 in DIE. */
5904static void
5905dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5906 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5907{
5908 struct attribute *attr;
5909
5910 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5911 if (attr)
5912 {
5913 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5914
801e3a5b
JB
5915 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5916 if (attr)
5917 {
5918 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5919
801e3a5b
JB
5920 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5921 }
5922 }
5923
5924 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5925 if (attr)
5926 {
5927 bfd *obfd = cu->objfile->obfd;
5928
5929 /* The value of the DW_AT_ranges attribute is the offset of the
5930 address range list in the .debug_ranges section. */
5931 unsigned long offset = DW_UNSND (attr);
dce234bc 5932 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5933
5934 /* For some target architectures, but not others, the
5935 read_address function sign-extends the addresses it returns.
5936 To recognize base address selection entries, we need a
5937 mask. */
5938 unsigned int addr_size = cu->header.addr_size;
5939 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5940
5941 /* The base address, to which the next pair is relative. Note
5942 that this 'base' is a DWARF concept: most entries in a range
5943 list are relative, to reduce the number of relocs against the
5944 debugging information. This is separate from this function's
5945 'baseaddr' argument, which GDB uses to relocate debugging
5946 information from a shared library based on the address at
5947 which the library was loaded. */
d00adf39
DE
5948 CORE_ADDR base = cu->base_address;
5949 int base_known = cu->base_known;
801e3a5b 5950
be391dca 5951 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5952 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5953 {
5954 complaint (&symfile_complaints,
5955 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5956 offset);
5957 return;
5958 }
5959
5960 for (;;)
5961 {
5962 unsigned int bytes_read;
5963 CORE_ADDR start, end;
5964
5965 start = read_address (obfd, buffer, cu, &bytes_read);
5966 buffer += bytes_read;
5967 end = read_address (obfd, buffer, cu, &bytes_read);
5968 buffer += bytes_read;
5969
5970 /* Did we find the end of the range list? */
5971 if (start == 0 && end == 0)
5972 break;
5973
5974 /* Did we find a base address selection entry? */
5975 else if ((start & base_select_mask) == base_select_mask)
5976 {
5977 base = end;
5978 base_known = 1;
5979 }
5980
5981 /* We found an ordinary address range. */
5982 else
5983 {
5984 if (!base_known)
5985 {
5986 complaint (&symfile_complaints,
5987 _("Invalid .debug_ranges data (no base address)"));
5988 return;
5989 }
5990
6e70227d
DE
5991 record_block_range (block,
5992 baseaddr + base + start,
801e3a5b
JB
5993 baseaddr + base + end - 1);
5994 }
5995 }
5996 }
5997}
5998
c906108c
SS
5999/* Add an aggregate field to the field list. */
6000
6001static void
107d2387 6002dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6003 struct dwarf2_cu *cu)
6e70227d 6004{
e7c27a73 6005 struct objfile *objfile = cu->objfile;
5e2b427d 6006 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6007 struct nextfield *new_field;
6008 struct attribute *attr;
6009 struct field *fp;
6010 char *fieldname = "";
6011
6012 /* Allocate a new field list entry and link it in. */
6013 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6014 make_cleanup (xfree, new_field);
c906108c 6015 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6016
6017 if (die->tag == DW_TAG_inheritance)
6018 {
6019 new_field->next = fip->baseclasses;
6020 fip->baseclasses = new_field;
6021 }
6022 else
6023 {
6024 new_field->next = fip->fields;
6025 fip->fields = new_field;
6026 }
c906108c
SS
6027 fip->nfields++;
6028
6029 /* Handle accessibility and virtuality of field.
6030 The default accessibility for members is public, the default
6031 accessibility for inheritance is private. */
6032 if (die->tag != DW_TAG_inheritance)
6033 new_field->accessibility = DW_ACCESS_public;
6034 else
6035 new_field->accessibility = DW_ACCESS_private;
6036 new_field->virtuality = DW_VIRTUALITY_none;
6037
e142c38c 6038 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6039 if (attr)
6040 new_field->accessibility = DW_UNSND (attr);
6041 if (new_field->accessibility != DW_ACCESS_public)
6042 fip->non_public_fields = 1;
e142c38c 6043 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6044 if (attr)
6045 new_field->virtuality = DW_UNSND (attr);
6046
6047 fp = &new_field->field;
a9a9bd0f 6048
e142c38c 6049 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6050 {
a9a9bd0f 6051 /* Data member other than a C++ static data member. */
6e70227d 6052
c906108c 6053 /* Get type of field. */
e7c27a73 6054 fp->type = die_type (die, cu);
c906108c 6055
d6a843b5 6056 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6057
c906108c 6058 /* Get bit size of field (zero if none). */
e142c38c 6059 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6060 if (attr)
6061 {
6062 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6063 }
6064 else
6065 {
6066 FIELD_BITSIZE (*fp) = 0;
6067 }
6068
6069 /* Get bit offset of field. */
e142c38c 6070 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6071 if (attr)
6072 {
d4b96c9a 6073 int byte_offset = 0;
c6a0999f 6074
3690dd37 6075 if (attr_form_is_section_offset (attr))
d4b96c9a 6076 dwarf2_complex_location_expr_complaint ();
3690dd37 6077 else if (attr_form_is_constant (attr))
c6a0999f 6078 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6079 else if (attr_form_is_block (attr))
c6a0999f 6080 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6081 else
6082 dwarf2_complex_location_expr_complaint ();
c6a0999f 6083
d6a843b5 6084 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6085 }
e142c38c 6086 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6087 if (attr)
6088 {
5e2b427d 6089 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6090 {
6091 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6092 additional bit offset from the MSB of the containing
6093 anonymous object to the MSB of the field. We don't
6094 have to do anything special since we don't need to
6095 know the size of the anonymous object. */
c906108c
SS
6096 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6097 }
6098 else
6099 {
6100 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6101 MSB of the anonymous object, subtract off the number of
6102 bits from the MSB of the field to the MSB of the
6103 object, and then subtract off the number of bits of
6104 the field itself. The result is the bit offset of
6105 the LSB of the field. */
c906108c
SS
6106 int anonymous_size;
6107 int bit_offset = DW_UNSND (attr);
6108
e142c38c 6109 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6110 if (attr)
6111 {
6112 /* The size of the anonymous object containing
6113 the bit field is explicit, so use the
6114 indicated size (in bytes). */
6115 anonymous_size = DW_UNSND (attr);
6116 }
6117 else
6118 {
6119 /* The size of the anonymous object containing
6120 the bit field must be inferred from the type
6121 attribute of the data member containing the
6122 bit field. */
6123 anonymous_size = TYPE_LENGTH (fp->type);
6124 }
6125 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6126 - bit_offset - FIELD_BITSIZE (*fp);
6127 }
6128 }
6129
6130 /* Get name of field. */
39cbfefa
DJ
6131 fieldname = dwarf2_name (die, cu);
6132 if (fieldname == NULL)
6133 fieldname = "";
d8151005
DJ
6134
6135 /* The name is already allocated along with this objfile, so we don't
6136 need to duplicate it for the type. */
6137 fp->name = fieldname;
c906108c
SS
6138
6139 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6140 pointer or virtual base class pointer) to private. */
e142c38c 6141 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6142 {
d48cc9dd 6143 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6144 new_field->accessibility = DW_ACCESS_private;
6145 fip->non_public_fields = 1;
6146 }
6147 }
a9a9bd0f 6148 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6149 {
a9a9bd0f
DC
6150 /* C++ static member. */
6151
6152 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6153 is a declaration, but all versions of G++ as of this writing
6154 (so through at least 3.2.1) incorrectly generate
6155 DW_TAG_variable tags. */
6e70227d 6156
c906108c 6157 char *physname;
c906108c 6158
a9a9bd0f 6159 /* Get name of field. */
39cbfefa
DJ
6160 fieldname = dwarf2_name (die, cu);
6161 if (fieldname == NULL)
c906108c
SS
6162 return;
6163
254e6b9e 6164 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6165 if (attr
6166 /* Only create a symbol if this is an external value.
6167 new_symbol checks this and puts the value in the global symbol
6168 table, which we want. If it is not external, new_symbol
6169 will try to put the value in cu->list_in_scope which is wrong. */
6170 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6171 {
6172 /* A static const member, not much different than an enum as far as
6173 we're concerned, except that we can support more types. */
6174 new_symbol (die, NULL, cu);
6175 }
6176
2df3850c 6177 /* Get physical name. */
94af9270 6178 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6179
d8151005
DJ
6180 /* The name is already allocated along with this objfile, so we don't
6181 need to duplicate it for the type. */
6182 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6183 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6184 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6185 }
6186 else if (die->tag == DW_TAG_inheritance)
6187 {
6188 /* C++ base class field. */
e142c38c 6189 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6190 if (attr)
d4b96c9a
JK
6191 {
6192 int byte_offset = 0;
6193
6194 if (attr_form_is_section_offset (attr))
6195 dwarf2_complex_location_expr_complaint ();
6196 else if (attr_form_is_constant (attr))
6197 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6198 else if (attr_form_is_block (attr))
6199 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6200 else
6201 dwarf2_complex_location_expr_complaint ();
6202
6203 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6204 }
c906108c 6205 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6206 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6207 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6208 fip->nbaseclasses++;
6209 }
6210}
6211
98751a41
JK
6212/* Add a typedef defined in the scope of the FIP's class. */
6213
6214static void
6215dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6216 struct dwarf2_cu *cu)
6e70227d 6217{
98751a41
JK
6218 struct objfile *objfile = cu->objfile;
6219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6220 struct typedef_field_list *new_field;
6221 struct attribute *attr;
6222 struct typedef_field *fp;
6223 char *fieldname = "";
6224
6225 /* Allocate a new field list entry and link it in. */
6226 new_field = xzalloc (sizeof (*new_field));
6227 make_cleanup (xfree, new_field);
6228
6229 gdb_assert (die->tag == DW_TAG_typedef);
6230
6231 fp = &new_field->field;
6232
6233 /* Get name of field. */
6234 fp->name = dwarf2_name (die, cu);
6235 if (fp->name == NULL)
6236 return;
6237
6238 fp->type = read_type_die (die, cu);
6239
6240 new_field->next = fip->typedef_field_list;
6241 fip->typedef_field_list = new_field;
6242 fip->typedef_field_list_count++;
6243}
6244
c906108c
SS
6245/* Create the vector of fields, and attach it to the type. */
6246
6247static void
fba45db2 6248dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6249 struct dwarf2_cu *cu)
c906108c
SS
6250{
6251 int nfields = fip->nfields;
6252
6253 /* Record the field count, allocate space for the array of fields,
6254 and create blank accessibility bitfields if necessary. */
6255 TYPE_NFIELDS (type) = nfields;
6256 TYPE_FIELDS (type) = (struct field *)
6257 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6258 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6259
b4ba55a1 6260 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6261 {
6262 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6263
6264 TYPE_FIELD_PRIVATE_BITS (type) =
6265 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6266 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6267
6268 TYPE_FIELD_PROTECTED_BITS (type) =
6269 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6270 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6271
774b6a14
TT
6272 TYPE_FIELD_IGNORE_BITS (type) =
6273 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6274 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6275 }
6276
6277 /* If the type has baseclasses, allocate and clear a bit vector for
6278 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6279 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6280 {
6281 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6282 unsigned char *pointer;
c906108c
SS
6283
6284 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6285 pointer = TYPE_ALLOC (type, num_bytes);
6286 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6287 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6288 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6289 }
6290
6291 /* Copy the saved-up fields into the field vector. Start from the head
6292 of the list, adding to the tail of the field array, so that they end
6293 up in the same order in the array in which they were added to the list. */
6294 while (nfields-- > 0)
6295 {
7d0ccb61
DJ
6296 struct nextfield *fieldp;
6297
6298 if (fip->fields)
6299 {
6300 fieldp = fip->fields;
6301 fip->fields = fieldp->next;
6302 }
6303 else
6304 {
6305 fieldp = fip->baseclasses;
6306 fip->baseclasses = fieldp->next;
6307 }
6308
6309 TYPE_FIELD (type, nfields) = fieldp->field;
6310 switch (fieldp->accessibility)
c906108c 6311 {
c5aa993b 6312 case DW_ACCESS_private:
b4ba55a1
JB
6313 if (cu->language != language_ada)
6314 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6315 break;
c906108c 6316
c5aa993b 6317 case DW_ACCESS_protected:
b4ba55a1
JB
6318 if (cu->language != language_ada)
6319 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6320 break;
c906108c 6321
c5aa993b
JM
6322 case DW_ACCESS_public:
6323 break;
c906108c 6324
c5aa993b
JM
6325 default:
6326 /* Unknown accessibility. Complain and treat it as public. */
6327 {
e2e0b3e5 6328 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6329 fieldp->accessibility);
c5aa993b
JM
6330 }
6331 break;
c906108c
SS
6332 }
6333 if (nfields < fip->nbaseclasses)
6334 {
7d0ccb61 6335 switch (fieldp->virtuality)
c906108c 6336 {
c5aa993b
JM
6337 case DW_VIRTUALITY_virtual:
6338 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6339 if (cu->language == language_ada)
6340 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6341 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6342 break;
c906108c
SS
6343 }
6344 }
c906108c
SS
6345 }
6346}
6347
c906108c
SS
6348/* Add a member function to the proper fieldlist. */
6349
6350static void
107d2387 6351dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6352 struct type *type, struct dwarf2_cu *cu)
c906108c 6353{
e7c27a73 6354 struct objfile *objfile = cu->objfile;
c906108c
SS
6355 struct attribute *attr;
6356 struct fnfieldlist *flp;
6357 int i;
6358 struct fn_field *fnp;
6359 char *fieldname;
c906108c 6360 struct nextfnfield *new_fnfield;
f792889a 6361 struct type *this_type;
c906108c 6362
b4ba55a1
JB
6363 if (cu->language == language_ada)
6364 error ("unexpected member function in Ada type");
6365
2df3850c 6366 /* Get name of member function. */
39cbfefa
DJ
6367 fieldname = dwarf2_name (die, cu);
6368 if (fieldname == NULL)
2df3850c 6369 return;
c906108c 6370
c906108c
SS
6371 /* Look up member function name in fieldlist. */
6372 for (i = 0; i < fip->nfnfields; i++)
6373 {
27bfe10e 6374 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6375 break;
6376 }
6377
6378 /* Create new list element if necessary. */
6379 if (i < fip->nfnfields)
6380 flp = &fip->fnfieldlists[i];
6381 else
6382 {
6383 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6384 {
6385 fip->fnfieldlists = (struct fnfieldlist *)
6386 xrealloc (fip->fnfieldlists,
6387 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6388 * sizeof (struct fnfieldlist));
c906108c 6389 if (fip->nfnfields == 0)
c13c43fd 6390 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6391 }
6392 flp = &fip->fnfieldlists[fip->nfnfields];
6393 flp->name = fieldname;
6394 flp->length = 0;
6395 flp->head = NULL;
3da10d80 6396 i = fip->nfnfields++;
c906108c
SS
6397 }
6398
6399 /* Create a new member function field and chain it to the field list
6400 entry. */
6401 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6402 make_cleanup (xfree, new_fnfield);
c906108c
SS
6403 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6404 new_fnfield->next = flp->head;
6405 flp->head = new_fnfield;
6406 flp->length++;
6407
6408 /* Fill in the member function field info. */
6409 fnp = &new_fnfield->fnfield;
3da10d80
KS
6410
6411 /* Delay processing of the physname until later. */
6412 if (cu->language == language_cplus || cu->language == language_java)
6413 {
6414 add_to_method_list (type, i, flp->length - 1, fieldname,
6415 die, cu);
6416 }
6417 else
6418 {
6419 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6420 fnp->physname = physname ? physname : "";
6421 }
6422
c906108c 6423 fnp->type = alloc_type (objfile);
f792889a
DJ
6424 this_type = read_type_die (die, cu);
6425 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6426 {
f792889a 6427 int nparams = TYPE_NFIELDS (this_type);
c906108c 6428
f792889a 6429 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6430 of the method itself (TYPE_CODE_METHOD). */
6431 smash_to_method_type (fnp->type, type,
f792889a
DJ
6432 TYPE_TARGET_TYPE (this_type),
6433 TYPE_FIELDS (this_type),
6434 TYPE_NFIELDS (this_type),
6435 TYPE_VARARGS (this_type));
c906108c
SS
6436
6437 /* Handle static member functions.
c5aa993b
JM
6438 Dwarf2 has no clean way to discern C++ static and non-static
6439 member functions. G++ helps GDB by marking the first
6440 parameter for non-static member functions (which is the
6441 this pointer) as artificial. We obtain this information
6442 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6443 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6444 fnp->voffset = VOFFSET_STATIC;
6445 }
6446 else
e2e0b3e5 6447 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6448 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6449
6450 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6451 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6452 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6453
6454 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6455 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6456
6457 /* Get accessibility. */
e142c38c 6458 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6459 if (attr)
6460 {
6461 switch (DW_UNSND (attr))
6462 {
c5aa993b
JM
6463 case DW_ACCESS_private:
6464 fnp->is_private = 1;
6465 break;
6466 case DW_ACCESS_protected:
6467 fnp->is_protected = 1;
6468 break;
c906108c
SS
6469 }
6470 }
6471
b02dede2 6472 /* Check for artificial methods. */
e142c38c 6473 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6474 if (attr && DW_UNSND (attr) != 0)
6475 fnp->is_artificial = 1;
6476
0d564a31 6477 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6478 function. For older versions of GCC, this is an offset in the
6479 appropriate virtual table, as specified by DW_AT_containing_type.
6480 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6481 to the object address. */
6482
e142c38c 6483 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6484 if (attr)
8e19ed76 6485 {
aec5aa8b 6486 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6487 {
aec5aa8b
TT
6488 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6489 {
6490 /* Old-style GCC. */
6491 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6492 }
6493 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6494 || (DW_BLOCK (attr)->size > 1
6495 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6496 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6497 {
6498 struct dwarf_block blk;
6499 int offset;
6500
6501 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6502 ? 1 : 2);
6503 blk.size = DW_BLOCK (attr)->size - offset;
6504 blk.data = DW_BLOCK (attr)->data + offset;
6505 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6506 if ((fnp->voffset % cu->header.addr_size) != 0)
6507 dwarf2_complex_location_expr_complaint ();
6508 else
6509 fnp->voffset /= cu->header.addr_size;
6510 fnp->voffset += 2;
6511 }
6512 else
6513 dwarf2_complex_location_expr_complaint ();
6514
6515 if (!fnp->fcontext)
6516 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6517 }
3690dd37 6518 else if (attr_form_is_section_offset (attr))
8e19ed76 6519 {
4d3c2250 6520 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6521 }
6522 else
6523 {
4d3c2250
KB
6524 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6525 fieldname);
8e19ed76 6526 }
0d564a31 6527 }
d48cc9dd
DJ
6528 else
6529 {
6530 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6531 if (attr && DW_UNSND (attr))
6532 {
6533 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6534 complaint (&symfile_complaints,
6535 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6536 fieldname, die->offset);
9655fd1a 6537 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6538 TYPE_CPLUS_DYNAMIC (type) = 1;
6539 }
6540 }
c906108c
SS
6541}
6542
6543/* Create the vector of member function fields, and attach it to the type. */
6544
6545static void
fba45db2 6546dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6547 struct dwarf2_cu *cu)
c906108c
SS
6548{
6549 struct fnfieldlist *flp;
6550 int total_length = 0;
6551 int i;
6552
b4ba55a1
JB
6553 if (cu->language == language_ada)
6554 error ("unexpected member functions in Ada type");
6555
c906108c
SS
6556 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6557 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6558 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6559
6560 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6561 {
6562 struct nextfnfield *nfp = flp->head;
6563 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6564 int k;
6565
6566 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6567 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6568 fn_flp->fn_fields = (struct fn_field *)
6569 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6570 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6571 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6572
6573 total_length += flp->length;
6574 }
6575
6576 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6577 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6578}
6579
1168df01
JB
6580/* Returns non-zero if NAME is the name of a vtable member in CU's
6581 language, zero otherwise. */
6582static int
6583is_vtable_name (const char *name, struct dwarf2_cu *cu)
6584{
6585 static const char vptr[] = "_vptr";
987504bb 6586 static const char vtable[] = "vtable";
1168df01 6587
987504bb
JJ
6588 /* Look for the C++ and Java forms of the vtable. */
6589 if ((cu->language == language_java
6590 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6591 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6592 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6593 return 1;
6594
6595 return 0;
6596}
6597
c0dd20ea 6598/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6599 functions, with the ABI-specified layout. If TYPE describes
6600 such a structure, smash it into a member function type.
61049d3b
DJ
6601
6602 GCC shouldn't do this; it should just output pointer to member DIEs.
6603 This is GCC PR debug/28767. */
c0dd20ea 6604
0b92b5bb
TT
6605static void
6606quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6607{
0b92b5bb 6608 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6609
6610 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6611 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6612 return;
c0dd20ea
DJ
6613
6614 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6615 if (TYPE_FIELD_NAME (type, 0) == NULL
6616 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6617 || TYPE_FIELD_NAME (type, 1) == NULL
6618 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6619 return;
c0dd20ea
DJ
6620
6621 /* Find the type of the method. */
0b92b5bb 6622 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6623 if (pfn_type == NULL
6624 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6625 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6626 return;
c0dd20ea
DJ
6627
6628 /* Look for the "this" argument. */
6629 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6630 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6631 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6632 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6633 return;
c0dd20ea
DJ
6634
6635 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6636 new_type = alloc_type (objfile);
6637 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6638 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6639 TYPE_VARARGS (pfn_type));
0b92b5bb 6640 smash_to_methodptr_type (type, new_type);
c0dd20ea 6641}
1168df01 6642
c906108c 6643/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6644 (definition) to create a type for the structure or union. Fill in
6645 the type's name and general properties; the members will not be
6646 processed until process_structure_type.
c906108c 6647
c767944b
DJ
6648 NOTE: we need to call these functions regardless of whether or not the
6649 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6650 structure or union. This gets the type entered into our set of
6651 user defined types.
6652
6653 However, if the structure is incomplete (an opaque struct/union)
6654 then suppress creating a symbol table entry for it since gdb only
6655 wants to find the one with the complete definition. Note that if
6656 it is complete, we just call new_symbol, which does it's own
6657 checking about whether the struct/union is anonymous or not (and
6658 suppresses creating a symbol table entry itself). */
6659
f792889a 6660static struct type *
134d01f1 6661read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6662{
e7c27a73 6663 struct objfile *objfile = cu->objfile;
c906108c
SS
6664 struct type *type;
6665 struct attribute *attr;
39cbfefa 6666 char *name;
c906108c 6667
348e048f
DE
6668 /* If the definition of this type lives in .debug_types, read that type.
6669 Don't follow DW_AT_specification though, that will take us back up
6670 the chain and we want to go down. */
6671 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6672 if (attr)
6673 {
6674 struct dwarf2_cu *type_cu = cu;
6675 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6676
348e048f
DE
6677 /* We could just recurse on read_structure_type, but we need to call
6678 get_die_type to ensure only one type for this DIE is created.
6679 This is important, for example, because for c++ classes we need
6680 TYPE_NAME set which is only done by new_symbol. Blech. */
6681 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6682
6683 /* TYPE_CU may not be the same as CU.
6684 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6685 return set_die_type (die, type, cu);
6686 }
6687
c0dd20ea 6688 type = alloc_type (objfile);
c906108c 6689 INIT_CPLUS_SPECIFIC (type);
93311388 6690
39cbfefa
DJ
6691 name = dwarf2_name (die, cu);
6692 if (name != NULL)
c906108c 6693 {
987504bb
JJ
6694 if (cu->language == language_cplus
6695 || cu->language == language_java)
63d06c5c 6696 {
3da10d80
KS
6697 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6698
6699 /* dwarf2_full_name might have already finished building the DIE's
6700 type. If so, there is no need to continue. */
6701 if (get_die_type (die, cu) != NULL)
6702 return get_die_type (die, cu);
6703
6704 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6705 if (die->tag == DW_TAG_structure_type
6706 || die->tag == DW_TAG_class_type)
6707 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6708 }
6709 else
6710 {
d8151005
DJ
6711 /* The name is already allocated along with this objfile, so
6712 we don't need to duplicate it for the type. */
94af9270
KS
6713 TYPE_TAG_NAME (type) = (char *) name;
6714 if (die->tag == DW_TAG_class_type)
6715 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6716 }
c906108c
SS
6717 }
6718
6719 if (die->tag == DW_TAG_structure_type)
6720 {
6721 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6722 }
6723 else if (die->tag == DW_TAG_union_type)
6724 {
6725 TYPE_CODE (type) = TYPE_CODE_UNION;
6726 }
6727 else
6728 {
c906108c
SS
6729 TYPE_CODE (type) = TYPE_CODE_CLASS;
6730 }
6731
0cc2414c
TT
6732 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6733 TYPE_DECLARED_CLASS (type) = 1;
6734
e142c38c 6735 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6736 if (attr)
6737 {
6738 TYPE_LENGTH (type) = DW_UNSND (attr);
6739 }
6740 else
6741 {
6742 TYPE_LENGTH (type) = 0;
6743 }
6744
876cecd0 6745 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6746 if (die_is_declaration (die, cu))
876cecd0 6747 TYPE_STUB (type) = 1;
a6c727b2
DJ
6748 else if (attr == NULL && die->child == NULL
6749 && producer_is_realview (cu->producer))
6750 /* RealView does not output the required DW_AT_declaration
6751 on incomplete types. */
6752 TYPE_STUB (type) = 1;
dc718098 6753
c906108c
SS
6754 /* We need to add the type field to the die immediately so we don't
6755 infinitely recurse when dealing with pointers to the structure
6756 type within the structure itself. */
1c379e20 6757 set_die_type (die, type, cu);
c906108c 6758
7e314c57
JK
6759 /* set_die_type should be already done. */
6760 set_descriptive_type (type, die, cu);
6761
c767944b
DJ
6762 return type;
6763}
6764
6765/* Finish creating a structure or union type, including filling in
6766 its members and creating a symbol for it. */
6767
6768static void
6769process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6770{
6771 struct objfile *objfile = cu->objfile;
6772 struct die_info *child_die = die->child;
6773 struct type *type;
6774
6775 type = get_die_type (die, cu);
6776 if (type == NULL)
6777 type = read_structure_type (die, cu);
6778
e142c38c 6779 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6780 {
6781 struct field_info fi;
6782 struct die_info *child_die;
34eaf542 6783 VEC (symbolp) *template_args = NULL;
c767944b 6784 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6785
6786 memset (&fi, 0, sizeof (struct field_info));
6787
639d11d3 6788 child_die = die->child;
c906108c
SS
6789
6790 while (child_die && child_die->tag)
6791 {
a9a9bd0f
DC
6792 if (child_die->tag == DW_TAG_member
6793 || child_die->tag == DW_TAG_variable)
c906108c 6794 {
a9a9bd0f
DC
6795 /* NOTE: carlton/2002-11-05: A C++ static data member
6796 should be a DW_TAG_member that is a declaration, but
6797 all versions of G++ as of this writing (so through at
6798 least 3.2.1) incorrectly generate DW_TAG_variable
6799 tags for them instead. */
e7c27a73 6800 dwarf2_add_field (&fi, child_die, cu);
c906108c 6801 }
8713b1b1 6802 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6803 {
6804 /* C++ member function. */
e7c27a73 6805 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6806 }
6807 else if (child_die->tag == DW_TAG_inheritance)
6808 {
6809 /* C++ base class field. */
e7c27a73 6810 dwarf2_add_field (&fi, child_die, cu);
c906108c 6811 }
98751a41
JK
6812 else if (child_die->tag == DW_TAG_typedef)
6813 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6814 else if (child_die->tag == DW_TAG_template_type_param
6815 || child_die->tag == DW_TAG_template_value_param)
6816 {
6817 struct symbol *arg = new_symbol (child_die, NULL, cu);
6818
f1078f66
DJ
6819 if (arg != NULL)
6820 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6821 }
6822
c906108c
SS
6823 child_die = sibling_die (child_die);
6824 }
6825
34eaf542
TT
6826 /* Attach template arguments to type. */
6827 if (! VEC_empty (symbolp, template_args))
6828 {
6829 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6830 TYPE_N_TEMPLATE_ARGUMENTS (type)
6831 = VEC_length (symbolp, template_args);
6832 TYPE_TEMPLATE_ARGUMENTS (type)
6833 = obstack_alloc (&objfile->objfile_obstack,
6834 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6835 * sizeof (struct symbol *)));
6836 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6837 VEC_address (symbolp, template_args),
6838 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6839 * sizeof (struct symbol *)));
6840 VEC_free (symbolp, template_args);
6841 }
6842
c906108c
SS
6843 /* Attach fields and member functions to the type. */
6844 if (fi.nfields)
e7c27a73 6845 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6846 if (fi.nfnfields)
6847 {
e7c27a73 6848 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6849
c5aa993b 6850 /* Get the type which refers to the base class (possibly this
c906108c 6851 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6852 class from the DW_AT_containing_type attribute. This use of
6853 DW_AT_containing_type is a GNU extension. */
c906108c 6854
e142c38c 6855 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6856 {
e7c27a73 6857 struct type *t = die_containing_type (die, cu);
c906108c
SS
6858
6859 TYPE_VPTR_BASETYPE (type) = t;
6860 if (type == t)
6861 {
c906108c
SS
6862 int i;
6863
6864 /* Our own class provides vtbl ptr. */
6865 for (i = TYPE_NFIELDS (t) - 1;
6866 i >= TYPE_N_BASECLASSES (t);
6867 --i)
6868 {
6869 char *fieldname = TYPE_FIELD_NAME (t, i);
6870
1168df01 6871 if (is_vtable_name (fieldname, cu))
c906108c
SS
6872 {
6873 TYPE_VPTR_FIELDNO (type) = i;
6874 break;
6875 }
6876 }
6877
6878 /* Complain if virtual function table field not found. */
6879 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6880 complaint (&symfile_complaints,
e2e0b3e5 6881 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6882 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6883 "");
c906108c
SS
6884 }
6885 else
6886 {
6887 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6888 }
6889 }
f6235d4c
EZ
6890 else if (cu->producer
6891 && strncmp (cu->producer,
6892 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6893 {
6894 /* The IBM XLC compiler does not provide direct indication
6895 of the containing type, but the vtable pointer is
6896 always named __vfp. */
6897
6898 int i;
6899
6900 for (i = TYPE_NFIELDS (type) - 1;
6901 i >= TYPE_N_BASECLASSES (type);
6902 --i)
6903 {
6904 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6905 {
6906 TYPE_VPTR_FIELDNO (type) = i;
6907 TYPE_VPTR_BASETYPE (type) = type;
6908 break;
6909 }
6910 }
6911 }
c906108c 6912 }
98751a41
JK
6913
6914 /* Copy fi.typedef_field_list linked list elements content into the
6915 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6916 if (fi.typedef_field_list)
6917 {
6918 int i = fi.typedef_field_list_count;
6919
a0d7a4ff 6920 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6921 TYPE_TYPEDEF_FIELD_ARRAY (type)
6922 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6923 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6924
6925 /* Reverse the list order to keep the debug info elements order. */
6926 while (--i >= 0)
6927 {
6928 struct typedef_field *dest, *src;
6e70227d 6929
98751a41
JK
6930 dest = &TYPE_TYPEDEF_FIELD (type, i);
6931 src = &fi.typedef_field_list->field;
6932 fi.typedef_field_list = fi.typedef_field_list->next;
6933 *dest = *src;
6934 }
6935 }
c767944b
DJ
6936
6937 do_cleanups (back_to);
c906108c 6938 }
63d06c5c 6939
0b92b5bb
TT
6940 quirk_gcc_member_function_pointer (type, cu->objfile);
6941
90aeadfc
DC
6942 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6943 snapshots) has been known to create a die giving a declaration
6944 for a class that has, as a child, a die giving a definition for a
6945 nested class. So we have to process our children even if the
6946 current die is a declaration. Normally, of course, a declaration
6947 won't have any children at all. */
134d01f1 6948
90aeadfc
DC
6949 while (child_die != NULL && child_die->tag)
6950 {
6951 if (child_die->tag == DW_TAG_member
6952 || child_die->tag == DW_TAG_variable
34eaf542
TT
6953 || child_die->tag == DW_TAG_inheritance
6954 || child_die->tag == DW_TAG_template_value_param
6955 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6956 {
90aeadfc 6957 /* Do nothing. */
134d01f1 6958 }
90aeadfc
DC
6959 else
6960 process_die (child_die, cu);
134d01f1 6961
90aeadfc 6962 child_die = sibling_die (child_die);
134d01f1
DJ
6963 }
6964
fa4028e9
JB
6965 /* Do not consider external references. According to the DWARF standard,
6966 these DIEs are identified by the fact that they have no byte_size
6967 attribute, and a declaration attribute. */
6968 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6969 || !die_is_declaration (die, cu))
c767944b 6970 new_symbol (die, type, cu);
134d01f1
DJ
6971}
6972
6973/* Given a DW_AT_enumeration_type die, set its type. We do not
6974 complete the type's fields yet, or create any symbols. */
c906108c 6975
f792889a 6976static struct type *
134d01f1 6977read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6978{
e7c27a73 6979 struct objfile *objfile = cu->objfile;
c906108c 6980 struct type *type;
c906108c 6981 struct attribute *attr;
0114d602 6982 const char *name;
134d01f1 6983
348e048f
DE
6984 /* If the definition of this type lives in .debug_types, read that type.
6985 Don't follow DW_AT_specification though, that will take us back up
6986 the chain and we want to go down. */
6987 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6988 if (attr)
6989 {
6990 struct dwarf2_cu *type_cu = cu;
6991 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6992
348e048f 6993 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6994
6995 /* TYPE_CU may not be the same as CU.
6996 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6997 return set_die_type (die, type, cu);
6998 }
6999
c906108c
SS
7000 type = alloc_type (objfile);
7001
7002 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7003 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7004 if (name != NULL)
0114d602 7005 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7006
e142c38c 7007 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7008 if (attr)
7009 {
7010 TYPE_LENGTH (type) = DW_UNSND (attr);
7011 }
7012 else
7013 {
7014 TYPE_LENGTH (type) = 0;
7015 }
7016
137033e9
JB
7017 /* The enumeration DIE can be incomplete. In Ada, any type can be
7018 declared as private in the package spec, and then defined only
7019 inside the package body. Such types are known as Taft Amendment
7020 Types. When another package uses such a type, an incomplete DIE
7021 may be generated by the compiler. */
02eb380e 7022 if (die_is_declaration (die, cu))
876cecd0 7023 TYPE_STUB (type) = 1;
02eb380e 7024
f792889a 7025 return set_die_type (die, type, cu);
134d01f1
DJ
7026}
7027
7028/* Given a pointer to a die which begins an enumeration, process all
7029 the dies that define the members of the enumeration, and create the
7030 symbol for the enumeration type.
7031
7032 NOTE: We reverse the order of the element list. */
7033
7034static void
7035process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7036{
f792889a 7037 struct type *this_type;
134d01f1 7038
f792889a
DJ
7039 this_type = get_die_type (die, cu);
7040 if (this_type == NULL)
7041 this_type = read_enumeration_type (die, cu);
9dc481d3 7042
639d11d3 7043 if (die->child != NULL)
c906108c 7044 {
9dc481d3
DE
7045 struct die_info *child_die;
7046 struct symbol *sym;
7047 struct field *fields = NULL;
7048 int num_fields = 0;
7049 int unsigned_enum = 1;
7050 char *name;
7051
639d11d3 7052 child_die = die->child;
c906108c
SS
7053 while (child_die && child_die->tag)
7054 {
7055 if (child_die->tag != DW_TAG_enumerator)
7056 {
e7c27a73 7057 process_die (child_die, cu);
c906108c
SS
7058 }
7059 else
7060 {
39cbfefa
DJ
7061 name = dwarf2_name (child_die, cu);
7062 if (name)
c906108c 7063 {
f792889a 7064 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7065 if (SYMBOL_VALUE (sym) < 0)
7066 unsigned_enum = 0;
7067
7068 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7069 {
7070 fields = (struct field *)
7071 xrealloc (fields,
7072 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7073 * sizeof (struct field));
c906108c
SS
7074 }
7075
3567439c 7076 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7077 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7078 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7079 FIELD_BITSIZE (fields[num_fields]) = 0;
7080
7081 num_fields++;
7082 }
7083 }
7084
7085 child_die = sibling_die (child_die);
7086 }
7087
7088 if (num_fields)
7089 {
f792889a
DJ
7090 TYPE_NFIELDS (this_type) = num_fields;
7091 TYPE_FIELDS (this_type) = (struct field *)
7092 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7093 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7094 sizeof (struct field) * num_fields);
b8c9b27d 7095 xfree (fields);
c906108c
SS
7096 }
7097 if (unsigned_enum)
876cecd0 7098 TYPE_UNSIGNED (this_type) = 1;
c906108c 7099 }
134d01f1 7100
f792889a 7101 new_symbol (die, this_type, cu);
c906108c
SS
7102}
7103
7104/* Extract all information from a DW_TAG_array_type DIE and put it in
7105 the DIE's type field. For now, this only handles one dimensional
7106 arrays. */
7107
f792889a 7108static struct type *
e7c27a73 7109read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7110{
e7c27a73 7111 struct objfile *objfile = cu->objfile;
c906108c 7112 struct die_info *child_die;
7e314c57 7113 struct type *type;
c906108c
SS
7114 struct type *element_type, *range_type, *index_type;
7115 struct type **range_types = NULL;
7116 struct attribute *attr;
7117 int ndim = 0;
7118 struct cleanup *back_to;
39cbfefa 7119 char *name;
c906108c 7120
e7c27a73 7121 element_type = die_type (die, cu);
c906108c 7122
7e314c57
JK
7123 /* The die_type call above may have already set the type for this DIE. */
7124 type = get_die_type (die, cu);
7125 if (type)
7126 return type;
7127
c906108c
SS
7128 /* Irix 6.2 native cc creates array types without children for
7129 arrays with unspecified length. */
639d11d3 7130 if (die->child == NULL)
c906108c 7131 {
46bf5051 7132 index_type = objfile_type (objfile)->builtin_int;
c906108c 7133 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7134 type = create_array_type (NULL, element_type, range_type);
7135 return set_die_type (die, type, cu);
c906108c
SS
7136 }
7137
7138 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7139 child_die = die->child;
c906108c
SS
7140 while (child_die && child_die->tag)
7141 {
7142 if (child_die->tag == DW_TAG_subrange_type)
7143 {
f792889a 7144 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7145
f792889a 7146 if (child_type != NULL)
a02abb62
JB
7147 {
7148 /* The range type was succesfully read. Save it for
7149 the array type creation. */
7150 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7151 {
7152 range_types = (struct type **)
7153 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7154 * sizeof (struct type *));
7155 if (ndim == 0)
7156 make_cleanup (free_current_contents, &range_types);
7157 }
f792889a 7158 range_types[ndim++] = child_type;
a02abb62 7159 }
c906108c
SS
7160 }
7161 child_die = sibling_die (child_die);
7162 }
7163
7164 /* Dwarf2 dimensions are output from left to right, create the
7165 necessary array types in backwards order. */
7ca2d3a3 7166
c906108c 7167 type = element_type;
7ca2d3a3
DL
7168
7169 if (read_array_order (die, cu) == DW_ORD_col_major)
7170 {
7171 int i = 0;
9a619af0 7172
7ca2d3a3
DL
7173 while (i < ndim)
7174 type = create_array_type (NULL, type, range_types[i++]);
7175 }
7176 else
7177 {
7178 while (ndim-- > 0)
7179 type = create_array_type (NULL, type, range_types[ndim]);
7180 }
c906108c 7181
f5f8a009
EZ
7182 /* Understand Dwarf2 support for vector types (like they occur on
7183 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7184 array type. This is not part of the Dwarf2/3 standard yet, but a
7185 custom vendor extension. The main difference between a regular
7186 array and the vector variant is that vectors are passed by value
7187 to functions. */
e142c38c 7188 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7189 if (attr)
ea37ba09 7190 make_vector_type (type);
f5f8a009 7191
dbc98a8b
KW
7192 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7193 implementation may choose to implement triple vectors using this
7194 attribute. */
7195 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7196 if (attr)
7197 {
7198 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7199 TYPE_LENGTH (type) = DW_UNSND (attr);
7200 else
7201 complaint (&symfile_complaints, _("\
7202DW_AT_byte_size for array type smaller than the total size of elements"));
7203 }
7204
39cbfefa
DJ
7205 name = dwarf2_name (die, cu);
7206 if (name)
7207 TYPE_NAME (type) = name;
6e70227d 7208
7e314c57
JK
7209 /* Install the type in the die. */
7210 set_die_type (die, type, cu);
7211
7212 /* set_die_type should be already done. */
b4ba55a1
JB
7213 set_descriptive_type (type, die, cu);
7214
c906108c
SS
7215 do_cleanups (back_to);
7216
7e314c57 7217 return type;
c906108c
SS
7218}
7219
7ca2d3a3 7220static enum dwarf_array_dim_ordering
6e70227d 7221read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7222{
7223 struct attribute *attr;
7224
7225 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7226
7227 if (attr) return DW_SND (attr);
7228
7229 /*
7230 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7231 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7232 laid out as per normal fortran.
7233
6e70227d 7234 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7235 version checking.
7236 */
7237
905e0470
PM
7238 if (cu->language == language_fortran
7239 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7240 {
7241 return DW_ORD_row_major;
7242 }
7243
6e70227d 7244 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7245 {
7246 case array_column_major:
7247 return DW_ORD_col_major;
7248 case array_row_major:
7249 default:
7250 return DW_ORD_row_major;
7251 };
7252}
7253
72019c9c
GM
7254/* Extract all information from a DW_TAG_set_type DIE and put it in
7255 the DIE's type field. */
7256
f792889a 7257static struct type *
72019c9c
GM
7258read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7259{
7e314c57
JK
7260 struct type *domain_type, *set_type;
7261 struct attribute *attr;
f792889a 7262
7e314c57
JK
7263 domain_type = die_type (die, cu);
7264
7265 /* The die_type call above may have already set the type for this DIE. */
7266 set_type = get_die_type (die, cu);
7267 if (set_type)
7268 return set_type;
7269
7270 set_type = create_set_type (NULL, domain_type);
7271
7272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7273 if (attr)
7274 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7275
f792889a 7276 return set_die_type (die, set_type, cu);
72019c9c 7277}
7ca2d3a3 7278
c906108c
SS
7279/* First cut: install each common block member as a global variable. */
7280
7281static void
e7c27a73 7282read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7283{
7284 struct die_info *child_die;
7285 struct attribute *attr;
7286 struct symbol *sym;
7287 CORE_ADDR base = (CORE_ADDR) 0;
7288
e142c38c 7289 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7290 if (attr)
7291 {
8e19ed76
PS
7292 /* Support the .debug_loc offsets */
7293 if (attr_form_is_block (attr))
7294 {
e7c27a73 7295 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7296 }
3690dd37 7297 else if (attr_form_is_section_offset (attr))
8e19ed76 7298 {
4d3c2250 7299 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7300 }
7301 else
7302 {
4d3c2250
KB
7303 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7304 "common block member");
8e19ed76 7305 }
c906108c 7306 }
639d11d3 7307 if (die->child != NULL)
c906108c 7308 {
639d11d3 7309 child_die = die->child;
c906108c
SS
7310 while (child_die && child_die->tag)
7311 {
e7c27a73 7312 sym = new_symbol (child_die, NULL, cu);
e142c38c 7313 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7314 if (sym != NULL && attr != NULL)
c906108c 7315 {
d4b96c9a
JK
7316 CORE_ADDR byte_offset = 0;
7317
7318 if (attr_form_is_section_offset (attr))
7319 dwarf2_complex_location_expr_complaint ();
7320 else if (attr_form_is_constant (attr))
7321 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7322 else if (attr_form_is_block (attr))
7323 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7324 else
7325 dwarf2_complex_location_expr_complaint ();
7326
7327 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7328 add_symbol_to_list (sym, &global_symbols);
7329 }
7330 child_die = sibling_die (child_die);
7331 }
7332 }
7333}
7334
0114d602 7335/* Create a type for a C++ namespace. */
d9fa45fe 7336
0114d602
DJ
7337static struct type *
7338read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7339{
e7c27a73 7340 struct objfile *objfile = cu->objfile;
0114d602 7341 const char *previous_prefix, *name;
9219021c 7342 int is_anonymous;
0114d602
DJ
7343 struct type *type;
7344
7345 /* For extensions, reuse the type of the original namespace. */
7346 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7347 {
7348 struct die_info *ext_die;
7349 struct dwarf2_cu *ext_cu = cu;
9a619af0 7350
0114d602
DJ
7351 ext_die = dwarf2_extension (die, &ext_cu);
7352 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7353
7354 /* EXT_CU may not be the same as CU.
7355 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7356 return set_die_type (die, type, cu);
7357 }
9219021c 7358
e142c38c 7359 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7360
7361 /* Now build the name of the current namespace. */
7362
0114d602
DJ
7363 previous_prefix = determine_prefix (die, cu);
7364 if (previous_prefix[0] != '\0')
7365 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7366 previous_prefix, name, 0, cu);
0114d602
DJ
7367
7368 /* Create the type. */
7369 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7370 objfile);
7371 TYPE_NAME (type) = (char *) name;
7372 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7373
60531b24 7374 return set_die_type (die, type, cu);
0114d602
DJ
7375}
7376
7377/* Read a C++ namespace. */
7378
7379static void
7380read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7381{
7382 struct objfile *objfile = cu->objfile;
7383 const char *name;
7384 int is_anonymous;
9219021c 7385
5c4e30ca
DC
7386 /* Add a symbol associated to this if we haven't seen the namespace
7387 before. Also, add a using directive if it's an anonymous
7388 namespace. */
9219021c 7389
f2f0e013 7390 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7391 {
7392 struct type *type;
7393
0114d602 7394 type = read_type_die (die, cu);
e7c27a73 7395 new_symbol (die, type, cu);
5c4e30ca 7396
0114d602 7397 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7398 if (is_anonymous)
0114d602
DJ
7399 {
7400 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7401
c0cc3a76 7402 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7403 NULL, &objfile->objfile_obstack);
0114d602 7404 }
5c4e30ca 7405 }
9219021c 7406
639d11d3 7407 if (die->child != NULL)
d9fa45fe 7408 {
639d11d3 7409 struct die_info *child_die = die->child;
6e70227d 7410
d9fa45fe
DC
7411 while (child_die && child_die->tag)
7412 {
e7c27a73 7413 process_die (child_die, cu);
d9fa45fe
DC
7414 child_die = sibling_die (child_die);
7415 }
7416 }
38d518c9
EZ
7417}
7418
f55ee35c
JK
7419/* Read a Fortran module as type. This DIE can be only a declaration used for
7420 imported module. Still we need that type as local Fortran "use ... only"
7421 declaration imports depend on the created type in determine_prefix. */
7422
7423static struct type *
7424read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7425{
7426 struct objfile *objfile = cu->objfile;
7427 char *module_name;
7428 struct type *type;
7429
7430 module_name = dwarf2_name (die, cu);
7431 if (!module_name)
7432 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7433 die->offset);
7434 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7435
7436 /* determine_prefix uses TYPE_TAG_NAME. */
7437 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7438
7439 return set_die_type (die, type, cu);
7440}
7441
5d7cb8df
JK
7442/* Read a Fortran module. */
7443
7444static void
7445read_module (struct die_info *die, struct dwarf2_cu *cu)
7446{
7447 struct die_info *child_die = die->child;
7448
5d7cb8df
JK
7449 while (child_die && child_die->tag)
7450 {
7451 process_die (child_die, cu);
7452 child_die = sibling_die (child_die);
7453 }
7454}
7455
38d518c9
EZ
7456/* Return the name of the namespace represented by DIE. Set
7457 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7458 namespace. */
7459
7460static const char *
e142c38c 7461namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7462{
7463 struct die_info *current_die;
7464 const char *name = NULL;
7465
7466 /* Loop through the extensions until we find a name. */
7467
7468 for (current_die = die;
7469 current_die != NULL;
f2f0e013 7470 current_die = dwarf2_extension (die, &cu))
38d518c9 7471 {
e142c38c 7472 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7473 if (name != NULL)
7474 break;
7475 }
7476
7477 /* Is it an anonymous namespace? */
7478
7479 *is_anonymous = (name == NULL);
7480 if (*is_anonymous)
7481 name = "(anonymous namespace)";
7482
7483 return name;
d9fa45fe
DC
7484}
7485
c906108c
SS
7486/* Extract all information from a DW_TAG_pointer_type DIE and add to
7487 the user defined type vector. */
7488
f792889a 7489static struct type *
e7c27a73 7490read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7491{
5e2b427d 7492 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7493 struct comp_unit_head *cu_header = &cu->header;
c906108c 7494 struct type *type;
8b2dbe47
KB
7495 struct attribute *attr_byte_size;
7496 struct attribute *attr_address_class;
7497 int byte_size, addr_class;
7e314c57
JK
7498 struct type *target_type;
7499
7500 target_type = die_type (die, cu);
c906108c 7501
7e314c57
JK
7502 /* The die_type call above may have already set the type for this DIE. */
7503 type = get_die_type (die, cu);
7504 if (type)
7505 return type;
7506
7507 type = lookup_pointer_type (target_type);
8b2dbe47 7508
e142c38c 7509 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7510 if (attr_byte_size)
7511 byte_size = DW_UNSND (attr_byte_size);
c906108c 7512 else
8b2dbe47
KB
7513 byte_size = cu_header->addr_size;
7514
e142c38c 7515 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7516 if (attr_address_class)
7517 addr_class = DW_UNSND (attr_address_class);
7518 else
7519 addr_class = DW_ADDR_none;
7520
7521 /* If the pointer size or address class is different than the
7522 default, create a type variant marked as such and set the
7523 length accordingly. */
7524 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7525 {
5e2b427d 7526 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7527 {
7528 int type_flags;
7529
849957d9 7530 type_flags = gdbarch_address_class_type_flags
5e2b427d 7531 (gdbarch, byte_size, addr_class);
876cecd0
TT
7532 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7533 == 0);
8b2dbe47
KB
7534 type = make_type_with_address_space (type, type_flags);
7535 }
7536 else if (TYPE_LENGTH (type) != byte_size)
7537 {
e2e0b3e5 7538 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7539 }
6e70227d 7540 else
9a619af0
MS
7541 {
7542 /* Should we also complain about unhandled address classes? */
7543 }
c906108c 7544 }
8b2dbe47
KB
7545
7546 TYPE_LENGTH (type) = byte_size;
f792889a 7547 return set_die_type (die, type, cu);
c906108c
SS
7548}
7549
7550/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7551 the user defined type vector. */
7552
f792889a 7553static struct type *
e7c27a73 7554read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7555{
7556 struct type *type;
7557 struct type *to_type;
7558 struct type *domain;
7559
e7c27a73
DJ
7560 to_type = die_type (die, cu);
7561 domain = die_containing_type (die, cu);
0d5de010 7562
7e314c57
JK
7563 /* The calls above may have already set the type for this DIE. */
7564 type = get_die_type (die, cu);
7565 if (type)
7566 return type;
7567
0d5de010
DJ
7568 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7569 type = lookup_methodptr_type (to_type);
7570 else
7571 type = lookup_memberptr_type (to_type, domain);
c906108c 7572
f792889a 7573 return set_die_type (die, type, cu);
c906108c
SS
7574}
7575
7576/* Extract all information from a DW_TAG_reference_type DIE and add to
7577 the user defined type vector. */
7578
f792889a 7579static struct type *
e7c27a73 7580read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7581{
e7c27a73 7582 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7583 struct type *type, *target_type;
c906108c
SS
7584 struct attribute *attr;
7585
7e314c57
JK
7586 target_type = die_type (die, cu);
7587
7588 /* The die_type call above may have already set the type for this DIE. */
7589 type = get_die_type (die, cu);
7590 if (type)
7591 return type;
7592
7593 type = lookup_reference_type (target_type);
e142c38c 7594 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7595 if (attr)
7596 {
7597 TYPE_LENGTH (type) = DW_UNSND (attr);
7598 }
7599 else
7600 {
107d2387 7601 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7602 }
f792889a 7603 return set_die_type (die, type, cu);
c906108c
SS
7604}
7605
f792889a 7606static struct type *
e7c27a73 7607read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7608{
f792889a 7609 struct type *base_type, *cv_type;
c906108c 7610
e7c27a73 7611 base_type = die_type (die, cu);
7e314c57
JK
7612
7613 /* The die_type call above may have already set the type for this DIE. */
7614 cv_type = get_die_type (die, cu);
7615 if (cv_type)
7616 return cv_type;
7617
2f608a3a
KW
7618 /* In case the const qualifier is applied to an array type, the element type
7619 is so qualified, not the array type (section 6.7.3 of C99). */
7620 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7621 {
7622 struct type *el_type, *inner_array;
7623
7624 base_type = copy_type (base_type);
7625 inner_array = base_type;
7626
7627 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7628 {
7629 TYPE_TARGET_TYPE (inner_array) =
7630 copy_type (TYPE_TARGET_TYPE (inner_array));
7631 inner_array = TYPE_TARGET_TYPE (inner_array);
7632 }
7633
7634 el_type = TYPE_TARGET_TYPE (inner_array);
7635 TYPE_TARGET_TYPE (inner_array) =
7636 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7637
7638 return set_die_type (die, base_type, cu);
7639 }
7640
f792889a
DJ
7641 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7642 return set_die_type (die, cv_type, cu);
c906108c
SS
7643}
7644
f792889a 7645static struct type *
e7c27a73 7646read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7647{
f792889a 7648 struct type *base_type, *cv_type;
c906108c 7649
e7c27a73 7650 base_type = die_type (die, cu);
7e314c57
JK
7651
7652 /* The die_type call above may have already set the type for this DIE. */
7653 cv_type = get_die_type (die, cu);
7654 if (cv_type)
7655 return cv_type;
7656
f792889a
DJ
7657 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7658 return set_die_type (die, cv_type, cu);
c906108c
SS
7659}
7660
7661/* Extract all information from a DW_TAG_string_type DIE and add to
7662 the user defined type vector. It isn't really a user defined type,
7663 but it behaves like one, with other DIE's using an AT_user_def_type
7664 attribute to reference it. */
7665
f792889a 7666static struct type *
e7c27a73 7667read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7668{
e7c27a73 7669 struct objfile *objfile = cu->objfile;
3b7538c0 7670 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7671 struct type *type, *range_type, *index_type, *char_type;
7672 struct attribute *attr;
7673 unsigned int length;
7674
e142c38c 7675 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7676 if (attr)
7677 {
7678 length = DW_UNSND (attr);
7679 }
7680 else
7681 {
b21b22e0 7682 /* check for the DW_AT_byte_size attribute */
e142c38c 7683 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7684 if (attr)
7685 {
7686 length = DW_UNSND (attr);
7687 }
7688 else
7689 {
7690 length = 1;
7691 }
c906108c 7692 }
6ccb9162 7693
46bf5051 7694 index_type = objfile_type (objfile)->builtin_int;
c906108c 7695 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7696 char_type = language_string_char_type (cu->language_defn, gdbarch);
7697 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7698
f792889a 7699 return set_die_type (die, type, cu);
c906108c
SS
7700}
7701
7702/* Handle DIES due to C code like:
7703
7704 struct foo
c5aa993b
JM
7705 {
7706 int (*funcp)(int a, long l);
7707 int b;
7708 };
c906108c
SS
7709
7710 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7711 */
c906108c 7712
f792889a 7713static struct type *
e7c27a73 7714read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7715{
7716 struct type *type; /* Type that this function returns */
7717 struct type *ftype; /* Function that returns above type */
7718 struct attribute *attr;
7719
e7c27a73 7720 type = die_type (die, cu);
7e314c57
JK
7721
7722 /* The die_type call above may have already set the type for this DIE. */
7723 ftype = get_die_type (die, cu);
7724 if (ftype)
7725 return ftype;
7726
0c8b41f1 7727 ftype = lookup_function_type (type);
c906108c 7728
5b8101ae 7729 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7730 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7731 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7732 || cu->language == language_cplus
5b8101ae
PM
7733 || cu->language == language_java
7734 || cu->language == language_pascal)
876cecd0 7735 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7736 else if (producer_is_realview (cu->producer))
7737 /* RealView does not emit DW_AT_prototyped. We can not
7738 distinguish prototyped and unprototyped functions; default to
7739 prototyped, since that is more common in modern code (and
7740 RealView warns about unprototyped functions). */
7741 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7742
c055b101
CV
7743 /* Store the calling convention in the type if it's available in
7744 the subroutine die. Otherwise set the calling convention to
7745 the default value DW_CC_normal. */
7746 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7747 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7748
7749 /* We need to add the subroutine type to the die immediately so
7750 we don't infinitely recurse when dealing with parameters
7751 declared as the same subroutine type. */
7752 set_die_type (die, ftype, cu);
6e70227d 7753
639d11d3 7754 if (die->child != NULL)
c906108c 7755 {
8072405b 7756 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7757 struct die_info *child_die;
8072405b 7758 int nparams, iparams;
c906108c
SS
7759
7760 /* Count the number of parameters.
7761 FIXME: GDB currently ignores vararg functions, but knows about
7762 vararg member functions. */
8072405b 7763 nparams = 0;
639d11d3 7764 child_die = die->child;
c906108c
SS
7765 while (child_die && child_die->tag)
7766 {
7767 if (child_die->tag == DW_TAG_formal_parameter)
7768 nparams++;
7769 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7770 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7771 child_die = sibling_die (child_die);
7772 }
7773
7774 /* Allocate storage for parameters and fill them in. */
7775 TYPE_NFIELDS (ftype) = nparams;
7776 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7777 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7778
8072405b
JK
7779 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7780 even if we error out during the parameters reading below. */
7781 for (iparams = 0; iparams < nparams; iparams++)
7782 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7783
7784 iparams = 0;
639d11d3 7785 child_die = die->child;
c906108c
SS
7786 while (child_die && child_die->tag)
7787 {
7788 if (child_die->tag == DW_TAG_formal_parameter)
7789 {
3ce3b1ba
PA
7790 struct type *arg_type;
7791
7792 /* DWARF version 2 has no clean way to discern C++
7793 static and non-static member functions. G++ helps
7794 GDB by marking the first parameter for non-static
7795 member functions (which is the this pointer) as
7796 artificial. We pass this information to
7797 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7798
7799 DWARF version 3 added DW_AT_object_pointer, which GCC
7800 4.5 does not yet generate. */
e142c38c 7801 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7802 if (attr)
7803 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7804 else
418835cc
KS
7805 {
7806 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7807
7808 /* GCC/43521: In java, the formal parameter
7809 "this" is sometimes not marked with DW_AT_artificial. */
7810 if (cu->language == language_java)
7811 {
7812 const char *name = dwarf2_name (child_die, cu);
9a619af0 7813
418835cc
KS
7814 if (name && !strcmp (name, "this"))
7815 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7816 }
7817 }
3ce3b1ba
PA
7818 arg_type = die_type (child_die, cu);
7819
7820 /* RealView does not mark THIS as const, which the testsuite
7821 expects. GCC marks THIS as const in method definitions,
7822 but not in the class specifications (GCC PR 43053). */
7823 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7824 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7825 {
7826 int is_this = 0;
7827 struct dwarf2_cu *arg_cu = cu;
7828 const char *name = dwarf2_name (child_die, cu);
7829
7830 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7831 if (attr)
7832 {
7833 /* If the compiler emits this, use it. */
7834 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7835 is_this = 1;
7836 }
7837 else if (name && strcmp (name, "this") == 0)
7838 /* Function definitions will have the argument names. */
7839 is_this = 1;
7840 else if (name == NULL && iparams == 0)
7841 /* Declarations may not have the names, so like
7842 elsewhere in GDB, assume an artificial first
7843 argument is "this". */
7844 is_this = 1;
7845
7846 if (is_this)
7847 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7848 arg_type, 0);
7849 }
7850
7851 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7852 iparams++;
7853 }
7854 child_die = sibling_die (child_die);
7855 }
7856 }
7857
76c10ea2 7858 return ftype;
c906108c
SS
7859}
7860
f792889a 7861static struct type *
e7c27a73 7862read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7863{
e7c27a73 7864 struct objfile *objfile = cu->objfile;
0114d602 7865 const char *name = NULL;
f792889a 7866 struct type *this_type;
c906108c 7867
94af9270 7868 name = dwarf2_full_name (NULL, die, cu);
f792889a 7869 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7870 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7871 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7872 set_die_type (die, this_type, cu);
7873 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7874 return this_type;
c906108c
SS
7875}
7876
7877/* Find a representation of a given base type and install
7878 it in the TYPE field of the die. */
7879
f792889a 7880static struct type *
e7c27a73 7881read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7882{
e7c27a73 7883 struct objfile *objfile = cu->objfile;
c906108c
SS
7884 struct type *type;
7885 struct attribute *attr;
7886 int encoding = 0, size = 0;
39cbfefa 7887 char *name;
6ccb9162
UW
7888 enum type_code code = TYPE_CODE_INT;
7889 int type_flags = 0;
7890 struct type *target_type = NULL;
c906108c 7891
e142c38c 7892 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7893 if (attr)
7894 {
7895 encoding = DW_UNSND (attr);
7896 }
e142c38c 7897 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7898 if (attr)
7899 {
7900 size = DW_UNSND (attr);
7901 }
39cbfefa 7902 name = dwarf2_name (die, cu);
6ccb9162 7903 if (!name)
c906108c 7904 {
6ccb9162
UW
7905 complaint (&symfile_complaints,
7906 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7907 }
6ccb9162
UW
7908
7909 switch (encoding)
c906108c 7910 {
6ccb9162
UW
7911 case DW_ATE_address:
7912 /* Turn DW_ATE_address into a void * pointer. */
7913 code = TYPE_CODE_PTR;
7914 type_flags |= TYPE_FLAG_UNSIGNED;
7915 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7916 break;
7917 case DW_ATE_boolean:
7918 code = TYPE_CODE_BOOL;
7919 type_flags |= TYPE_FLAG_UNSIGNED;
7920 break;
7921 case DW_ATE_complex_float:
7922 code = TYPE_CODE_COMPLEX;
7923 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7924 break;
7925 case DW_ATE_decimal_float:
7926 code = TYPE_CODE_DECFLOAT;
7927 break;
7928 case DW_ATE_float:
7929 code = TYPE_CODE_FLT;
7930 break;
7931 case DW_ATE_signed:
7932 break;
7933 case DW_ATE_unsigned:
7934 type_flags |= TYPE_FLAG_UNSIGNED;
7935 break;
7936 case DW_ATE_signed_char:
6e70227d 7937 if (cu->language == language_ada || cu->language == language_m2
868a0084 7938 || cu->language == language_pascal)
6ccb9162
UW
7939 code = TYPE_CODE_CHAR;
7940 break;
7941 case DW_ATE_unsigned_char:
868a0084
PM
7942 if (cu->language == language_ada || cu->language == language_m2
7943 || cu->language == language_pascal)
6ccb9162
UW
7944 code = TYPE_CODE_CHAR;
7945 type_flags |= TYPE_FLAG_UNSIGNED;
7946 break;
75079b2b
TT
7947 case DW_ATE_UTF:
7948 /* We just treat this as an integer and then recognize the
7949 type by name elsewhere. */
7950 break;
7951
6ccb9162
UW
7952 default:
7953 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7954 dwarf_type_encoding_name (encoding));
7955 break;
c906108c 7956 }
6ccb9162 7957
0114d602
DJ
7958 type = init_type (code, size, type_flags, NULL, objfile);
7959 TYPE_NAME (type) = name;
6ccb9162
UW
7960 TYPE_TARGET_TYPE (type) = target_type;
7961
0114d602 7962 if (name && strcmp (name, "char") == 0)
876cecd0 7963 TYPE_NOSIGN (type) = 1;
0114d602 7964
f792889a 7965 return set_die_type (die, type, cu);
c906108c
SS
7966}
7967
a02abb62
JB
7968/* Read the given DW_AT_subrange DIE. */
7969
f792889a 7970static struct type *
a02abb62
JB
7971read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7972{
5e2b427d 7973 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7974 struct type *base_type;
7975 struct type *range_type;
7976 struct attribute *attr;
43bbcdc2
PH
7977 LONGEST low = 0;
7978 LONGEST high = -1;
39cbfefa 7979 char *name;
43bbcdc2 7980 LONGEST negative_mask;
e77813c8 7981
a02abb62 7982 base_type = die_type (die, cu);
953ac07e
JK
7983 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7984 check_typedef (base_type);
a02abb62 7985
7e314c57
JK
7986 /* The die_type call above may have already set the type for this DIE. */
7987 range_type = get_die_type (die, cu);
7988 if (range_type)
7989 return range_type;
7990
e142c38c 7991 if (cu->language == language_fortran)
6e70227d 7992 {
a02abb62
JB
7993 /* FORTRAN implies a lower bound of 1, if not given. */
7994 low = 1;
7995 }
7996
dd5e6932
DJ
7997 /* FIXME: For variable sized arrays either of these could be
7998 a variable rather than a constant value. We'll allow it,
7999 but we don't know how to handle it. */
e142c38c 8000 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8001 if (attr)
8002 low = dwarf2_get_attr_constant_value (attr, 0);
8003
e142c38c 8004 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8005 if (attr)
6e70227d 8006 {
e77813c8 8007 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8008 {
8009 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8010 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8011 FIXME: GDB does not yet know how to handle dynamic
8012 arrays properly, treat them as arrays with unspecified
8013 length for now.
8014
8015 FIXME: jimb/2003-09-22: GDB does not really know
8016 how to handle arrays of unspecified length
8017 either; we just represent them as zero-length
8018 arrays. Choose an appropriate upper bound given
8019 the lower bound we've computed above. */
8020 high = low - 1;
8021 }
8022 else
8023 high = dwarf2_get_attr_constant_value (attr, 1);
8024 }
e77813c8
PM
8025 else
8026 {
8027 attr = dwarf2_attr (die, DW_AT_count, cu);
8028 if (attr)
8029 {
8030 int count = dwarf2_get_attr_constant_value (attr, 1);
8031 high = low + count - 1;
8032 }
8033 }
8034
8035 /* Dwarf-2 specifications explicitly allows to create subrange types
8036 without specifying a base type.
8037 In that case, the base type must be set to the type of
8038 the lower bound, upper bound or count, in that order, if any of these
8039 three attributes references an object that has a type.
8040 If no base type is found, the Dwarf-2 specifications say that
8041 a signed integer type of size equal to the size of an address should
8042 be used.
8043 For the following C code: `extern char gdb_int [];'
8044 GCC produces an empty range DIE.
8045 FIXME: muller/2010-05-28: Possible references to object for low bound,
8046 high bound or count are not yet handled by this code.
8047 */
8048 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8049 {
8050 struct objfile *objfile = cu->objfile;
8051 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8052 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8053 struct type *int_type = objfile_type (objfile)->builtin_int;
8054
8055 /* Test "int", "long int", and "long long int" objfile types,
8056 and select the first one having a size above or equal to the
8057 architecture address size. */
8058 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8059 base_type = int_type;
8060 else
8061 {
8062 int_type = objfile_type (objfile)->builtin_long;
8063 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8064 base_type = int_type;
8065 else
8066 {
8067 int_type = objfile_type (objfile)->builtin_long_long;
8068 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8069 base_type = int_type;
8070 }
8071 }
8072 }
a02abb62 8073
6e70227d 8074 negative_mask =
43bbcdc2
PH
8075 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8076 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8077 low |= negative_mask;
8078 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8079 high |= negative_mask;
8080
a02abb62
JB
8081 range_type = create_range_type (NULL, base_type, low, high);
8082
bbb0eef6
JK
8083 /* Mark arrays with dynamic length at least as an array of unspecified
8084 length. GDB could check the boundary but before it gets implemented at
8085 least allow accessing the array elements. */
8086 if (attr && attr->form == DW_FORM_block1)
8087 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8088
39cbfefa
DJ
8089 name = dwarf2_name (die, cu);
8090 if (name)
8091 TYPE_NAME (range_type) = name;
6e70227d 8092
e142c38c 8093 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8094 if (attr)
8095 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8096
7e314c57
JK
8097 set_die_type (die, range_type, cu);
8098
8099 /* set_die_type should be already done. */
b4ba55a1
JB
8100 set_descriptive_type (range_type, die, cu);
8101
7e314c57 8102 return range_type;
a02abb62 8103}
6e70227d 8104
f792889a 8105static struct type *
81a17f79
JB
8106read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8107{
8108 struct type *type;
81a17f79 8109
81a17f79
JB
8110 /* For now, we only support the C meaning of an unspecified type: void. */
8111
0114d602
DJ
8112 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8113 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8114
f792889a 8115 return set_die_type (die, type, cu);
81a17f79 8116}
a02abb62 8117
51545339
DJ
8118/* Trivial hash function for die_info: the hash value of a DIE
8119 is its offset in .debug_info for this objfile. */
8120
8121static hashval_t
8122die_hash (const void *item)
8123{
8124 const struct die_info *die = item;
9a619af0 8125
51545339
DJ
8126 return die->offset;
8127}
8128
8129/* Trivial comparison function for die_info structures: two DIEs
8130 are equal if they have the same offset. */
8131
8132static int
8133die_eq (const void *item_lhs, const void *item_rhs)
8134{
8135 const struct die_info *die_lhs = item_lhs;
8136 const struct die_info *die_rhs = item_rhs;
9a619af0 8137
51545339
DJ
8138 return die_lhs->offset == die_rhs->offset;
8139}
8140
c906108c
SS
8141/* Read a whole compilation unit into a linked list of dies. */
8142
f9aca02d 8143static struct die_info *
93311388 8144read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8145{
93311388 8146 struct die_reader_specs reader_specs;
98bfdba5 8147 int read_abbrevs = 0;
1d9ec526 8148 struct cleanup *back_to = NULL;
98bfdba5
PA
8149 struct die_info *die;
8150
8151 if (cu->dwarf2_abbrevs == NULL)
8152 {
8153 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8154 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8155 read_abbrevs = 1;
8156 }
93311388 8157
348e048f 8158 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8159 cu->die_hash
8160 = htab_create_alloc_ex (cu->header.length / 12,
8161 die_hash,
8162 die_eq,
8163 NULL,
8164 &cu->comp_unit_obstack,
8165 hashtab_obstack_allocate,
8166 dummy_obstack_deallocate);
8167
93311388
DE
8168 init_cu_die_reader (&reader_specs, cu);
8169
98bfdba5
PA
8170 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8171
8172 if (read_abbrevs)
8173 do_cleanups (back_to);
8174
8175 return die;
639d11d3
DC
8176}
8177
d97bc12b
DE
8178/* Main entry point for reading a DIE and all children.
8179 Read the DIE and dump it if requested. */
8180
8181static struct die_info *
93311388
DE
8182read_die_and_children (const struct die_reader_specs *reader,
8183 gdb_byte *info_ptr,
d97bc12b
DE
8184 gdb_byte **new_info_ptr,
8185 struct die_info *parent)
8186{
93311388 8187 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8188 new_info_ptr, parent);
8189
8190 if (dwarf2_die_debug)
8191 {
348e048f
DE
8192 fprintf_unfiltered (gdb_stdlog,
8193 "\nRead die from %s of %s:\n",
8194 reader->buffer == dwarf2_per_objfile->info.buffer
8195 ? ".debug_info"
8196 : reader->buffer == dwarf2_per_objfile->types.buffer
8197 ? ".debug_types"
8198 : "unknown section",
8199 reader->abfd->filename);
d97bc12b
DE
8200 dump_die (result, dwarf2_die_debug);
8201 }
8202
8203 return result;
8204}
8205
639d11d3
DC
8206/* Read a single die and all its descendents. Set the die's sibling
8207 field to NULL; set other fields in the die correctly, and set all
8208 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8209 location of the info_ptr after reading all of those dies. PARENT
8210 is the parent of the die in question. */
8211
8212static struct die_info *
93311388
DE
8213read_die_and_children_1 (const struct die_reader_specs *reader,
8214 gdb_byte *info_ptr,
d97bc12b
DE
8215 gdb_byte **new_info_ptr,
8216 struct die_info *parent)
639d11d3
DC
8217{
8218 struct die_info *die;
fe1b8b76 8219 gdb_byte *cur_ptr;
639d11d3
DC
8220 int has_children;
8221
93311388 8222 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8223 if (die == NULL)
8224 {
8225 *new_info_ptr = cur_ptr;
8226 return NULL;
8227 }
93311388 8228 store_in_ref_table (die, reader->cu);
639d11d3
DC
8229
8230 if (has_children)
348e048f 8231 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8232 else
8233 {
8234 die->child = NULL;
8235 *new_info_ptr = cur_ptr;
8236 }
8237
8238 die->sibling = NULL;
8239 die->parent = parent;
8240 return die;
8241}
8242
8243/* Read a die, all of its descendents, and all of its siblings; set
8244 all of the fields of all of the dies correctly. Arguments are as
8245 in read_die_and_children. */
8246
8247static struct die_info *
93311388
DE
8248read_die_and_siblings (const struct die_reader_specs *reader,
8249 gdb_byte *info_ptr,
fe1b8b76 8250 gdb_byte **new_info_ptr,
639d11d3
DC
8251 struct die_info *parent)
8252{
8253 struct die_info *first_die, *last_sibling;
fe1b8b76 8254 gdb_byte *cur_ptr;
639d11d3 8255
c906108c 8256 cur_ptr = info_ptr;
639d11d3
DC
8257 first_die = last_sibling = NULL;
8258
8259 while (1)
c906108c 8260 {
639d11d3 8261 struct die_info *die
93311388 8262 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8263
1d325ec1 8264 if (die == NULL)
c906108c 8265 {
639d11d3
DC
8266 *new_info_ptr = cur_ptr;
8267 return first_die;
c906108c 8268 }
1d325ec1
DJ
8269
8270 if (!first_die)
8271 first_die = die;
c906108c 8272 else
1d325ec1
DJ
8273 last_sibling->sibling = die;
8274
8275 last_sibling = die;
c906108c 8276 }
c906108c
SS
8277}
8278
93311388
DE
8279/* Read the die from the .debug_info section buffer. Set DIEP to
8280 point to a newly allocated die with its information, except for its
8281 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8282 whether the die has children or not. */
8283
8284static gdb_byte *
8285read_full_die (const struct die_reader_specs *reader,
8286 struct die_info **diep, gdb_byte *info_ptr,
8287 int *has_children)
8288{
8289 unsigned int abbrev_number, bytes_read, i, offset;
8290 struct abbrev_info *abbrev;
8291 struct die_info *die;
8292 struct dwarf2_cu *cu = reader->cu;
8293 bfd *abfd = reader->abfd;
8294
8295 offset = info_ptr - reader->buffer;
8296 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8297 info_ptr += bytes_read;
8298 if (!abbrev_number)
8299 {
8300 *diep = NULL;
8301 *has_children = 0;
8302 return info_ptr;
8303 }
8304
8305 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8306 if (!abbrev)
348e048f
DE
8307 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8308 abbrev_number,
8309 bfd_get_filename (abfd));
8310
93311388
DE
8311 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8312 die->offset = offset;
8313 die->tag = abbrev->tag;
8314 die->abbrev = abbrev_number;
8315
8316 die->num_attrs = abbrev->num_attrs;
8317
8318 for (i = 0; i < abbrev->num_attrs; ++i)
8319 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8320 abfd, info_ptr, cu);
8321
8322 *diep = die;
8323 *has_children = abbrev->has_children;
8324 return info_ptr;
8325}
8326
c906108c
SS
8327/* In DWARF version 2, the description of the debugging information is
8328 stored in a separate .debug_abbrev section. Before we read any
8329 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8330 in a hash table. This function also sets flags in CU describing
8331 the data found in the abbrev table. */
c906108c
SS
8332
8333static void
e7c27a73 8334dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8335{
e7c27a73 8336 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8337 gdb_byte *abbrev_ptr;
c906108c
SS
8338 struct abbrev_info *cur_abbrev;
8339 unsigned int abbrev_number, bytes_read, abbrev_name;
8340 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8341 struct attr_abbrev *cur_attrs;
8342 unsigned int allocated_attrs;
c906108c 8343
57349743 8344 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8345 obstack_init (&cu->abbrev_obstack);
8346 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8347 (ABBREV_HASH_SIZE
8348 * sizeof (struct abbrev_info *)));
8349 memset (cu->dwarf2_abbrevs, 0,
8350 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8351
be391dca
TT
8352 dwarf2_read_section (dwarf2_per_objfile->objfile,
8353 &dwarf2_per_objfile->abbrev);
dce234bc 8354 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8355 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8356 abbrev_ptr += bytes_read;
8357
f3dd6933
DJ
8358 allocated_attrs = ATTR_ALLOC_CHUNK;
8359 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8360
c906108c
SS
8361 /* loop until we reach an abbrev number of 0 */
8362 while (abbrev_number)
8363 {
f3dd6933 8364 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8365
8366 /* read in abbrev header */
8367 cur_abbrev->number = abbrev_number;
8368 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8369 abbrev_ptr += bytes_read;
8370 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8371 abbrev_ptr += 1;
8372
72bf9492
DJ
8373 if (cur_abbrev->tag == DW_TAG_namespace)
8374 cu->has_namespace_info = 1;
8375
c906108c
SS
8376 /* now read in declarations */
8377 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8378 abbrev_ptr += bytes_read;
8379 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8380 abbrev_ptr += bytes_read;
8381 while (abbrev_name)
8382 {
f3dd6933 8383 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8384 {
f3dd6933
DJ
8385 allocated_attrs += ATTR_ALLOC_CHUNK;
8386 cur_attrs
8387 = xrealloc (cur_attrs, (allocated_attrs
8388 * sizeof (struct attr_abbrev)));
c906108c 8389 }
ae038cb0
DJ
8390
8391 /* Record whether this compilation unit might have
8392 inter-compilation-unit references. If we don't know what form
8393 this attribute will have, then it might potentially be a
8394 DW_FORM_ref_addr, so we conservatively expect inter-CU
8395 references. */
8396
8397 if (abbrev_form == DW_FORM_ref_addr
8398 || abbrev_form == DW_FORM_indirect)
8399 cu->has_form_ref_addr = 1;
8400
f3dd6933
DJ
8401 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8402 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8403 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8404 abbrev_ptr += bytes_read;
8405 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8406 abbrev_ptr += bytes_read;
8407 }
8408
f3dd6933
DJ
8409 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8410 (cur_abbrev->num_attrs
8411 * sizeof (struct attr_abbrev)));
8412 memcpy (cur_abbrev->attrs, cur_attrs,
8413 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8414
c906108c 8415 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8416 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8417 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8418
8419 /* Get next abbreviation.
8420 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8421 always properly terminated with an abbrev number of 0.
8422 Exit loop if we encounter an abbreviation which we have
8423 already read (which means we are about to read the abbreviations
8424 for the next compile unit) or if the end of the abbreviation
8425 table is reached. */
dce234bc
PP
8426 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8427 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8428 break;
8429 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8430 abbrev_ptr += bytes_read;
e7c27a73 8431 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8432 break;
8433 }
f3dd6933
DJ
8434
8435 xfree (cur_attrs);
c906108c
SS
8436}
8437
f3dd6933 8438/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8439
c906108c 8440static void
f3dd6933 8441dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8442{
f3dd6933 8443 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8444
f3dd6933
DJ
8445 obstack_free (&cu->abbrev_obstack, NULL);
8446 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8447}
8448
8449/* Lookup an abbrev_info structure in the abbrev hash table. */
8450
8451static struct abbrev_info *
e7c27a73 8452dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8453{
8454 unsigned int hash_number;
8455 struct abbrev_info *abbrev;
8456
8457 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8458 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8459
8460 while (abbrev)
8461 {
8462 if (abbrev->number == number)
8463 return abbrev;
8464 else
8465 abbrev = abbrev->next;
8466 }
8467 return NULL;
8468}
8469
72bf9492
DJ
8470/* Returns nonzero if TAG represents a type that we might generate a partial
8471 symbol for. */
8472
8473static int
8474is_type_tag_for_partial (int tag)
8475{
8476 switch (tag)
8477 {
8478#if 0
8479 /* Some types that would be reasonable to generate partial symbols for,
8480 that we don't at present. */
8481 case DW_TAG_array_type:
8482 case DW_TAG_file_type:
8483 case DW_TAG_ptr_to_member_type:
8484 case DW_TAG_set_type:
8485 case DW_TAG_string_type:
8486 case DW_TAG_subroutine_type:
8487#endif
8488 case DW_TAG_base_type:
8489 case DW_TAG_class_type:
680b30c7 8490 case DW_TAG_interface_type:
72bf9492
DJ
8491 case DW_TAG_enumeration_type:
8492 case DW_TAG_structure_type:
8493 case DW_TAG_subrange_type:
8494 case DW_TAG_typedef:
8495 case DW_TAG_union_type:
8496 return 1;
8497 default:
8498 return 0;
8499 }
8500}
8501
8502/* Load all DIEs that are interesting for partial symbols into memory. */
8503
8504static struct partial_die_info *
93311388
DE
8505load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8506 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8507{
8508 struct partial_die_info *part_die;
8509 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8510 struct abbrev_info *abbrev;
8511 unsigned int bytes_read;
5afb4e99 8512 unsigned int load_all = 0;
72bf9492
DJ
8513
8514 int nesting_level = 1;
8515
8516 parent_die = NULL;
8517 last_die = NULL;
8518
5afb4e99
DJ
8519 if (cu->per_cu && cu->per_cu->load_all_dies)
8520 load_all = 1;
8521
72bf9492
DJ
8522 cu->partial_dies
8523 = htab_create_alloc_ex (cu->header.length / 12,
8524 partial_die_hash,
8525 partial_die_eq,
8526 NULL,
8527 &cu->comp_unit_obstack,
8528 hashtab_obstack_allocate,
8529 dummy_obstack_deallocate);
8530
8531 part_die = obstack_alloc (&cu->comp_unit_obstack,
8532 sizeof (struct partial_die_info));
8533
8534 while (1)
8535 {
8536 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8537
8538 /* A NULL abbrev means the end of a series of children. */
8539 if (abbrev == NULL)
8540 {
8541 if (--nesting_level == 0)
8542 {
8543 /* PART_DIE was probably the last thing allocated on the
8544 comp_unit_obstack, so we could call obstack_free
8545 here. We don't do that because the waste is small,
8546 and will be cleaned up when we're done with this
8547 compilation unit. This way, we're also more robust
8548 against other users of the comp_unit_obstack. */
8549 return first_die;
8550 }
8551 info_ptr += bytes_read;
8552 last_die = parent_die;
8553 parent_die = parent_die->die_parent;
8554 continue;
8555 }
8556
98bfdba5
PA
8557 /* Check for template arguments. We never save these; if
8558 they're seen, we just mark the parent, and go on our way. */
8559 if (parent_die != NULL
8560 && cu->language == language_cplus
8561 && (abbrev->tag == DW_TAG_template_type_param
8562 || abbrev->tag == DW_TAG_template_value_param))
8563 {
8564 parent_die->has_template_arguments = 1;
8565
8566 if (!load_all)
8567 {
8568 /* We don't need a partial DIE for the template argument. */
8569 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8570 cu);
8571 continue;
8572 }
8573 }
8574
8575 /* We only recurse into subprograms looking for template arguments.
8576 Skip their other children. */
8577 if (!load_all
8578 && cu->language == language_cplus
8579 && parent_die != NULL
8580 && parent_die->tag == DW_TAG_subprogram)
8581 {
8582 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8583 continue;
8584 }
8585
5afb4e99
DJ
8586 /* Check whether this DIE is interesting enough to save. Normally
8587 we would not be interested in members here, but there may be
8588 later variables referencing them via DW_AT_specification (for
8589 static members). */
8590 if (!load_all
8591 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8592 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8593 && abbrev->tag != DW_TAG_enumerator
8594 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8595 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8596 && abbrev->tag != DW_TAG_variable
5afb4e99 8597 && abbrev->tag != DW_TAG_namespace
f55ee35c 8598 && abbrev->tag != DW_TAG_module
5afb4e99 8599 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8600 {
8601 /* Otherwise we skip to the next sibling, if any. */
93311388 8602 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8603 continue;
8604 }
8605
93311388
DE
8606 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8607 buffer, info_ptr, cu);
72bf9492
DJ
8608
8609 /* This two-pass algorithm for processing partial symbols has a
8610 high cost in cache pressure. Thus, handle some simple cases
8611 here which cover the majority of C partial symbols. DIEs
8612 which neither have specification tags in them, nor could have
8613 specification tags elsewhere pointing at them, can simply be
8614 processed and discarded.
8615
8616 This segment is also optional; scan_partial_symbols and
8617 add_partial_symbol will handle these DIEs if we chain
8618 them in normally. When compilers which do not emit large
8619 quantities of duplicate debug information are more common,
8620 this code can probably be removed. */
8621
8622 /* Any complete simple types at the top level (pretty much all
8623 of them, for a language without namespaces), can be processed
8624 directly. */
8625 if (parent_die == NULL
8626 && part_die->has_specification == 0
8627 && part_die->is_declaration == 0
8628 && (part_die->tag == DW_TAG_typedef
8629 || part_die->tag == DW_TAG_base_type
8630 || part_die->tag == DW_TAG_subrange_type))
8631 {
8632 if (building_psymtab && part_die->name != NULL)
04a679b8 8633 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8634 VAR_DOMAIN, LOC_TYPEDEF,
8635 &cu->objfile->static_psymbols,
8636 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8637 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8638 continue;
8639 }
8640
8641 /* If we're at the second level, and we're an enumerator, and
8642 our parent has no specification (meaning possibly lives in a
8643 namespace elsewhere), then we can add the partial symbol now
8644 instead of queueing it. */
8645 if (part_die->tag == DW_TAG_enumerator
8646 && parent_die != NULL
8647 && parent_die->die_parent == NULL
8648 && parent_die->tag == DW_TAG_enumeration_type
8649 && parent_die->has_specification == 0)
8650 {
8651 if (part_die->name == NULL)
e2e0b3e5 8652 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8653 else if (building_psymtab)
04a679b8 8654 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8655 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8656 (cu->language == language_cplus
8657 || cu->language == language_java)
72bf9492
DJ
8658 ? &cu->objfile->global_psymbols
8659 : &cu->objfile->static_psymbols,
8660 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8661
93311388 8662 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8663 continue;
8664 }
8665
8666 /* We'll save this DIE so link it in. */
8667 part_die->die_parent = parent_die;
8668 part_die->die_sibling = NULL;
8669 part_die->die_child = NULL;
8670
8671 if (last_die && last_die == parent_die)
8672 last_die->die_child = part_die;
8673 else if (last_die)
8674 last_die->die_sibling = part_die;
8675
8676 last_die = part_die;
8677
8678 if (first_die == NULL)
8679 first_die = part_die;
8680
8681 /* Maybe add the DIE to the hash table. Not all DIEs that we
8682 find interesting need to be in the hash table, because we
8683 also have the parent/sibling/child chains; only those that we
8684 might refer to by offset later during partial symbol reading.
8685
8686 For now this means things that might have be the target of a
8687 DW_AT_specification, DW_AT_abstract_origin, or
8688 DW_AT_extension. DW_AT_extension will refer only to
8689 namespaces; DW_AT_abstract_origin refers to functions (and
8690 many things under the function DIE, but we do not recurse
8691 into function DIEs during partial symbol reading) and
8692 possibly variables as well; DW_AT_specification refers to
8693 declarations. Declarations ought to have the DW_AT_declaration
8694 flag. It happens that GCC forgets to put it in sometimes, but
8695 only for functions, not for types.
8696
8697 Adding more things than necessary to the hash table is harmless
8698 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8699 wasted time in find_partial_die, when we reread the compilation
8700 unit with load_all_dies set. */
72bf9492 8701
5afb4e99 8702 if (load_all
72929c62 8703 || abbrev->tag == DW_TAG_constant
5afb4e99 8704 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8705 || abbrev->tag == DW_TAG_variable
8706 || abbrev->tag == DW_TAG_namespace
8707 || part_die->is_declaration)
8708 {
8709 void **slot;
8710
8711 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8712 part_die->offset, INSERT);
8713 *slot = part_die;
8714 }
8715
8716 part_die = obstack_alloc (&cu->comp_unit_obstack,
8717 sizeof (struct partial_die_info));
8718
8719 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8720 we have no reason to follow the children of structures; for other
98bfdba5
PA
8721 languages we have to, so that we can get at method physnames
8722 to infer fully qualified class names, for DW_AT_specification,
8723 and for C++ template arguments. For C++, we also look one level
8724 inside functions to find template arguments (if the name of the
8725 function does not already contain the template arguments).
bc30ff58
JB
8726
8727 For Ada, we need to scan the children of subprograms and lexical
8728 blocks as well because Ada allows the definition of nested
8729 entities that could be interesting for the debugger, such as
8730 nested subprograms for instance. */
72bf9492 8731 if (last_die->has_children
5afb4e99
DJ
8732 && (load_all
8733 || last_die->tag == DW_TAG_namespace
f55ee35c 8734 || last_die->tag == DW_TAG_module
72bf9492 8735 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8736 || (cu->language == language_cplus
8737 && last_die->tag == DW_TAG_subprogram
8738 && (last_die->name == NULL
8739 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8740 || (cu->language != language_c
8741 && (last_die->tag == DW_TAG_class_type
680b30c7 8742 || last_die->tag == DW_TAG_interface_type
72bf9492 8743 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8744 || last_die->tag == DW_TAG_union_type))
8745 || (cu->language == language_ada
8746 && (last_die->tag == DW_TAG_subprogram
8747 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8748 {
8749 nesting_level++;
8750 parent_die = last_die;
8751 continue;
8752 }
8753
8754 /* Otherwise we skip to the next sibling, if any. */
93311388 8755 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8756
8757 /* Back to the top, do it again. */
8758 }
8759}
8760
c906108c
SS
8761/* Read a minimal amount of information into the minimal die structure. */
8762
fe1b8b76 8763static gdb_byte *
72bf9492
DJ
8764read_partial_die (struct partial_die_info *part_die,
8765 struct abbrev_info *abbrev,
8766 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8767 gdb_byte *buffer, gdb_byte *info_ptr,
8768 struct dwarf2_cu *cu)
c906108c 8769{
fa238c03 8770 unsigned int i;
c906108c 8771 struct attribute attr;
c5aa993b 8772 int has_low_pc_attr = 0;
c906108c
SS
8773 int has_high_pc_attr = 0;
8774
72bf9492 8775 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8776
93311388 8777 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8778
8779 info_ptr += abbrev_len;
8780
8781 if (abbrev == NULL)
8782 return info_ptr;
8783
c906108c
SS
8784 part_die->tag = abbrev->tag;
8785 part_die->has_children = abbrev->has_children;
c906108c
SS
8786
8787 for (i = 0; i < abbrev->num_attrs; ++i)
8788 {
e7c27a73 8789 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8790
8791 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8792 partial symbol table. */
c906108c
SS
8793 switch (attr.name)
8794 {
8795 case DW_AT_name:
71c25dea
TT
8796 switch (part_die->tag)
8797 {
8798 case DW_TAG_compile_unit:
348e048f 8799 case DW_TAG_type_unit:
71c25dea
TT
8800 /* Compilation units have a DW_AT_name that is a filename, not
8801 a source language identifier. */
8802 case DW_TAG_enumeration_type:
8803 case DW_TAG_enumerator:
8804 /* These tags always have simple identifiers already; no need
8805 to canonicalize them. */
8806 part_die->name = DW_STRING (&attr);
8807 break;
8808 default:
8809 part_die->name
8810 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8811 &cu->objfile->objfile_obstack);
71c25dea
TT
8812 break;
8813 }
c906108c 8814 break;
31ef98ae 8815 case DW_AT_linkage_name:
c906108c 8816 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8817 /* Note that both forms of linkage name might appear. We
8818 assume they will be the same, and we only store the last
8819 one we see. */
94af9270
KS
8820 if (cu->language == language_ada)
8821 part_die->name = DW_STRING (&attr);
abc72ce4 8822 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8823 break;
8824 case DW_AT_low_pc:
8825 has_low_pc_attr = 1;
8826 part_die->lowpc = DW_ADDR (&attr);
8827 break;
8828 case DW_AT_high_pc:
8829 has_high_pc_attr = 1;
8830 part_die->highpc = DW_ADDR (&attr);
8831 break;
8832 case DW_AT_location:
8e19ed76
PS
8833 /* Support the .debug_loc offsets */
8834 if (attr_form_is_block (&attr))
8835 {
8836 part_die->locdesc = DW_BLOCK (&attr);
8837 }
3690dd37 8838 else if (attr_form_is_section_offset (&attr))
8e19ed76 8839 {
4d3c2250 8840 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8841 }
8842 else
8843 {
4d3c2250
KB
8844 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8845 "partial symbol information");
8e19ed76 8846 }
c906108c 8847 break;
c906108c
SS
8848 case DW_AT_external:
8849 part_die->is_external = DW_UNSND (&attr);
8850 break;
8851 case DW_AT_declaration:
8852 part_die->is_declaration = DW_UNSND (&attr);
8853 break;
8854 case DW_AT_type:
8855 part_die->has_type = 1;
8856 break;
8857 case DW_AT_abstract_origin:
8858 case DW_AT_specification:
72bf9492
DJ
8859 case DW_AT_extension:
8860 part_die->has_specification = 1;
c764a876 8861 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8862 break;
8863 case DW_AT_sibling:
8864 /* Ignore absolute siblings, they might point outside of
8865 the current compile unit. */
8866 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8867 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8868 else
93311388 8869 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8870 break;
fa4028e9
JB
8871 case DW_AT_byte_size:
8872 part_die->has_byte_size = 1;
8873 break;
68511cec
CES
8874 case DW_AT_calling_convention:
8875 /* DWARF doesn't provide a way to identify a program's source-level
8876 entry point. DW_AT_calling_convention attributes are only meant
8877 to describe functions' calling conventions.
8878
8879 However, because it's a necessary piece of information in
8880 Fortran, and because DW_CC_program is the only piece of debugging
8881 information whose definition refers to a 'main program' at all,
8882 several compilers have begun marking Fortran main programs with
8883 DW_CC_program --- even when those functions use the standard
8884 calling conventions.
8885
8886 So until DWARF specifies a way to provide this information and
8887 compilers pick up the new representation, we'll support this
8888 practice. */
8889 if (DW_UNSND (&attr) == DW_CC_program
8890 && cu->language == language_fortran)
01f8c46d
JK
8891 {
8892 set_main_name (part_die->name);
8893
8894 /* As this DIE has a static linkage the name would be difficult
8895 to look up later. */
8896 language_of_main = language_fortran;
8897 }
68511cec 8898 break;
c906108c
SS
8899 default:
8900 break;
8901 }
8902 }
8903
c906108c
SS
8904 /* When using the GNU linker, .gnu.linkonce. sections are used to
8905 eliminate duplicate copies of functions and vtables and such.
8906 The linker will arbitrarily choose one and discard the others.
8907 The AT_*_pc values for such functions refer to local labels in
8908 these sections. If the section from that file was discarded, the
8909 labels are not in the output, so the relocs get a value of 0.
8910 If this is a discarded function, mark the pc bounds as invalid,
8911 so that GDB will ignore it. */
8912 if (has_low_pc_attr && has_high_pc_attr
8913 && part_die->lowpc < part_die->highpc
8914 && (part_die->lowpc != 0
72dca2f5 8915 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8916 part_die->has_pc_info = 1;
85cbf3d3 8917
c906108c
SS
8918 return info_ptr;
8919}
8920
72bf9492
DJ
8921/* Find a cached partial DIE at OFFSET in CU. */
8922
8923static struct partial_die_info *
c764a876 8924find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8925{
8926 struct partial_die_info *lookup_die = NULL;
8927 struct partial_die_info part_die;
8928
8929 part_die.offset = offset;
8930 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8931
72bf9492
DJ
8932 return lookup_die;
8933}
8934
348e048f
DE
8935/* Find a partial DIE at OFFSET, which may or may not be in CU,
8936 except in the case of .debug_types DIEs which do not reference
8937 outside their CU (they do however referencing other types via
8938 DW_FORM_sig8). */
72bf9492
DJ
8939
8940static struct partial_die_info *
c764a876 8941find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8942{
5afb4e99
DJ
8943 struct dwarf2_per_cu_data *per_cu = NULL;
8944 struct partial_die_info *pd = NULL;
72bf9492 8945
348e048f
DE
8946 if (cu->per_cu->from_debug_types)
8947 {
8948 pd = find_partial_die_in_comp_unit (offset, cu);
8949 if (pd != NULL)
8950 return pd;
8951 goto not_found;
8952 }
8953
45452591 8954 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8955 {
8956 pd = find_partial_die_in_comp_unit (offset, cu);
8957 if (pd != NULL)
8958 return pd;
8959 }
72bf9492 8960
ae038cb0
DJ
8961 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8962
98bfdba5
PA
8963 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8964 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8965
8966 per_cu->cu->last_used = 0;
5afb4e99
DJ
8967 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8968
8969 if (pd == NULL && per_cu->load_all_dies == 0)
8970 {
8971 struct cleanup *back_to;
8972 struct partial_die_info comp_unit_die;
8973 struct abbrev_info *abbrev;
8974 unsigned int bytes_read;
8975 char *info_ptr;
8976
8977 per_cu->load_all_dies = 1;
8978
8979 /* Re-read the DIEs. */
8980 back_to = make_cleanup (null_cleanup, 0);
8981 if (per_cu->cu->dwarf2_abbrevs == NULL)
8982 {
8983 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8984 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8985 }
dce234bc 8986 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8987 + per_cu->cu->header.offset
8988 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8989 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8990 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8991 per_cu->cu->objfile->obfd,
8992 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8993 per_cu->cu);
8994 if (comp_unit_die.has_children)
93311388
DE
8995 load_partial_dies (per_cu->cu->objfile->obfd,
8996 dwarf2_per_objfile->info.buffer, info_ptr,
8997 0, per_cu->cu);
5afb4e99
DJ
8998 do_cleanups (back_to);
8999
9000 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9001 }
9002
348e048f
DE
9003 not_found:
9004
5afb4e99
DJ
9005 if (pd == NULL)
9006 internal_error (__FILE__, __LINE__,
c764a876 9007 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
9008 offset, bfd_get_filename (cu->objfile->obfd));
9009 return pd;
72bf9492
DJ
9010}
9011
abc72ce4
DE
9012/* See if we can figure out if the class lives in a namespace. We do
9013 this by looking for a member function; its demangled name will
9014 contain namespace info, if there is any. */
9015
9016static void
9017guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9018 struct dwarf2_cu *cu)
9019{
9020 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9021 what template types look like, because the demangler
9022 frequently doesn't give the same name as the debug info. We
9023 could fix this by only using the demangled name to get the
9024 prefix (but see comment in read_structure_type). */
9025
9026 struct partial_die_info *real_pdi;
9027 struct partial_die_info *child_pdi;
9028
9029 /* If this DIE (this DIE's specification, if any) has a parent, then
9030 we should not do this. We'll prepend the parent's fully qualified
9031 name when we create the partial symbol. */
9032
9033 real_pdi = struct_pdi;
9034 while (real_pdi->has_specification)
9035 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9036
9037 if (real_pdi->die_parent != NULL)
9038 return;
9039
9040 for (child_pdi = struct_pdi->die_child;
9041 child_pdi != NULL;
9042 child_pdi = child_pdi->die_sibling)
9043 {
9044 if (child_pdi->tag == DW_TAG_subprogram
9045 && child_pdi->linkage_name != NULL)
9046 {
9047 char *actual_class_name
9048 = language_class_name_from_physname (cu->language_defn,
9049 child_pdi->linkage_name);
9050 if (actual_class_name != NULL)
9051 {
9052 struct_pdi->name
9053 = obsavestring (actual_class_name,
9054 strlen (actual_class_name),
9055 &cu->objfile->objfile_obstack);
9056 xfree (actual_class_name);
9057 }
9058 break;
9059 }
9060 }
9061}
9062
72bf9492
DJ
9063/* Adjust PART_DIE before generating a symbol for it. This function
9064 may set the is_external flag or change the DIE's name. */
9065
9066static void
9067fixup_partial_die (struct partial_die_info *part_die,
9068 struct dwarf2_cu *cu)
9069{
abc72ce4
DE
9070 /* Once we've fixed up a die, there's no point in doing so again.
9071 This also avoids a memory leak if we were to call
9072 guess_partial_die_structure_name multiple times. */
9073 if (part_die->fixup_called)
9074 return;
9075
72bf9492
DJ
9076 /* If we found a reference attribute and the DIE has no name, try
9077 to find a name in the referred to DIE. */
9078
9079 if (part_die->name == NULL && part_die->has_specification)
9080 {
9081 struct partial_die_info *spec_die;
72bf9492 9082
10b3939b 9083 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9084
10b3939b 9085 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9086
9087 if (spec_die->name)
9088 {
9089 part_die->name = spec_die->name;
9090
9091 /* Copy DW_AT_external attribute if it is set. */
9092 if (spec_die->is_external)
9093 part_die->is_external = spec_die->is_external;
9094 }
9095 }
9096
9097 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9098
9099 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9100 part_die->name = "(anonymous namespace)";
9101
abc72ce4
DE
9102 /* If there is no parent die to provide a namespace, and there are
9103 children, see if we can determine the namespace from their linkage
9104 name.
9105 NOTE: We need to do this even if cu->has_namespace_info != 0.
9106 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9107 if (cu->language == language_cplus
9108 && dwarf2_per_objfile->types.asection != NULL
9109 && part_die->die_parent == NULL
9110 && part_die->has_children
9111 && (part_die->tag == DW_TAG_class_type
9112 || part_die->tag == DW_TAG_structure_type
9113 || part_die->tag == DW_TAG_union_type))
9114 guess_partial_die_structure_name (part_die, cu);
9115
9116 part_die->fixup_called = 1;
72bf9492
DJ
9117}
9118
a8329558 9119/* Read an attribute value described by an attribute form. */
c906108c 9120
fe1b8b76 9121static gdb_byte *
a8329558 9122read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9123 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9124 struct dwarf2_cu *cu)
c906108c 9125{
e7c27a73 9126 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9127 unsigned int bytes_read;
9128 struct dwarf_block *blk;
9129
a8329558
KW
9130 attr->form = form;
9131 switch (form)
c906108c 9132 {
c906108c 9133 case DW_FORM_ref_addr:
ae411497
TT
9134 if (cu->header.version == 2)
9135 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9136 else
9137 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9138 info_ptr += bytes_read;
9139 break;
9140 case DW_FORM_addr:
e7c27a73 9141 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9142 info_ptr += bytes_read;
c906108c
SS
9143 break;
9144 case DW_FORM_block2:
7b5a2f43 9145 blk = dwarf_alloc_block (cu);
c906108c
SS
9146 blk->size = read_2_bytes (abfd, info_ptr);
9147 info_ptr += 2;
9148 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9149 info_ptr += blk->size;
9150 DW_BLOCK (attr) = blk;
9151 break;
9152 case DW_FORM_block4:
7b5a2f43 9153 blk = dwarf_alloc_block (cu);
c906108c
SS
9154 blk->size = read_4_bytes (abfd, info_ptr);
9155 info_ptr += 4;
9156 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9157 info_ptr += blk->size;
9158 DW_BLOCK (attr) = blk;
9159 break;
9160 case DW_FORM_data2:
9161 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9162 info_ptr += 2;
9163 break;
9164 case DW_FORM_data4:
9165 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9166 info_ptr += 4;
9167 break;
9168 case DW_FORM_data8:
9169 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9170 info_ptr += 8;
9171 break;
2dc7f7b3
TT
9172 case DW_FORM_sec_offset:
9173 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9174 info_ptr += bytes_read;
9175 break;
c906108c 9176 case DW_FORM_string:
9b1c24c8 9177 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9178 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9179 info_ptr += bytes_read;
9180 break;
4bdf3d34
JJ
9181 case DW_FORM_strp:
9182 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9183 &bytes_read);
8285870a 9184 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9185 info_ptr += bytes_read;
9186 break;
2dc7f7b3 9187 case DW_FORM_exprloc:
c906108c 9188 case DW_FORM_block:
7b5a2f43 9189 blk = dwarf_alloc_block (cu);
c906108c
SS
9190 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9191 info_ptr += bytes_read;
9192 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9193 info_ptr += blk->size;
9194 DW_BLOCK (attr) = blk;
9195 break;
9196 case DW_FORM_block1:
7b5a2f43 9197 blk = dwarf_alloc_block (cu);
c906108c
SS
9198 blk->size = read_1_byte (abfd, info_ptr);
9199 info_ptr += 1;
9200 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9201 info_ptr += blk->size;
9202 DW_BLOCK (attr) = blk;
9203 break;
9204 case DW_FORM_data1:
9205 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9206 info_ptr += 1;
9207 break;
9208 case DW_FORM_flag:
9209 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9210 info_ptr += 1;
9211 break;
2dc7f7b3
TT
9212 case DW_FORM_flag_present:
9213 DW_UNSND (attr) = 1;
9214 break;
c906108c
SS
9215 case DW_FORM_sdata:
9216 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9217 info_ptr += bytes_read;
9218 break;
9219 case DW_FORM_udata:
9220 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9221 info_ptr += bytes_read;
9222 break;
9223 case DW_FORM_ref1:
10b3939b 9224 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9225 info_ptr += 1;
9226 break;
9227 case DW_FORM_ref2:
10b3939b 9228 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9229 info_ptr += 2;
9230 break;
9231 case DW_FORM_ref4:
10b3939b 9232 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9233 info_ptr += 4;
9234 break;
613e1657 9235 case DW_FORM_ref8:
10b3939b 9236 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9237 info_ptr += 8;
9238 break;
348e048f
DE
9239 case DW_FORM_sig8:
9240 /* Convert the signature to something we can record in DW_UNSND
9241 for later lookup.
9242 NOTE: This is NULL if the type wasn't found. */
9243 DW_SIGNATURED_TYPE (attr) =
9244 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9245 info_ptr += 8;
9246 break;
c906108c 9247 case DW_FORM_ref_udata:
10b3939b
DJ
9248 DW_ADDR (attr) = (cu->header.offset
9249 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9250 info_ptr += bytes_read;
9251 break;
c906108c 9252 case DW_FORM_indirect:
a8329558
KW
9253 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9254 info_ptr += bytes_read;
e7c27a73 9255 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9256 break;
c906108c 9257 default:
8a3fe4f8 9258 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9259 dwarf_form_name (form),
9260 bfd_get_filename (abfd));
c906108c 9261 }
28e94949
JB
9262
9263 /* We have seen instances where the compiler tried to emit a byte
9264 size attribute of -1 which ended up being encoded as an unsigned
9265 0xffffffff. Although 0xffffffff is technically a valid size value,
9266 an object of this size seems pretty unlikely so we can relatively
9267 safely treat these cases as if the size attribute was invalid and
9268 treat them as zero by default. */
9269 if (attr->name == DW_AT_byte_size
9270 && form == DW_FORM_data4
9271 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9272 {
9273 complaint
9274 (&symfile_complaints,
43bbcdc2
PH
9275 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9276 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9277 DW_UNSND (attr) = 0;
9278 }
28e94949 9279
c906108c
SS
9280 return info_ptr;
9281}
9282
a8329558
KW
9283/* Read an attribute described by an abbreviated attribute. */
9284
fe1b8b76 9285static gdb_byte *
a8329558 9286read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9287 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9288{
9289 attr->name = abbrev->name;
e7c27a73 9290 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9291}
9292
c906108c
SS
9293/* read dwarf information from a buffer */
9294
9295static unsigned int
fe1b8b76 9296read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9297{
fe1b8b76 9298 return bfd_get_8 (abfd, buf);
c906108c
SS
9299}
9300
9301static int
fe1b8b76 9302read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9303{
fe1b8b76 9304 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9305}
9306
9307static unsigned int
fe1b8b76 9308read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9309{
fe1b8b76 9310 return bfd_get_16 (abfd, buf);
c906108c
SS
9311}
9312
9313static int
fe1b8b76 9314read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9315{
fe1b8b76 9316 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9317}
9318
9319static unsigned int
fe1b8b76 9320read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9321{
fe1b8b76 9322 return bfd_get_32 (abfd, buf);
c906108c
SS
9323}
9324
9325static int
fe1b8b76 9326read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9327{
fe1b8b76 9328 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9329}
9330
93311388 9331static ULONGEST
fe1b8b76 9332read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9333{
fe1b8b76 9334 return bfd_get_64 (abfd, buf);
c906108c
SS
9335}
9336
9337static CORE_ADDR
fe1b8b76 9338read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9339 unsigned int *bytes_read)
c906108c 9340{
e7c27a73 9341 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9342 CORE_ADDR retval = 0;
9343
107d2387 9344 if (cu_header->signed_addr_p)
c906108c 9345 {
107d2387
AC
9346 switch (cu_header->addr_size)
9347 {
9348 case 2:
fe1b8b76 9349 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9350 break;
9351 case 4:
fe1b8b76 9352 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9353 break;
9354 case 8:
fe1b8b76 9355 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9356 break;
9357 default:
8e65ff28 9358 internal_error (__FILE__, __LINE__,
e2e0b3e5 9359 _("read_address: bad switch, signed [in module %s]"),
659b0389 9360 bfd_get_filename (abfd));
107d2387
AC
9361 }
9362 }
9363 else
9364 {
9365 switch (cu_header->addr_size)
9366 {
9367 case 2:
fe1b8b76 9368 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9369 break;
9370 case 4:
fe1b8b76 9371 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9372 break;
9373 case 8:
fe1b8b76 9374 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9375 break;
9376 default:
8e65ff28 9377 internal_error (__FILE__, __LINE__,
e2e0b3e5 9378 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9379 bfd_get_filename (abfd));
107d2387 9380 }
c906108c 9381 }
64367e0a 9382
107d2387
AC
9383 *bytes_read = cu_header->addr_size;
9384 return retval;
c906108c
SS
9385}
9386
f7ef9339 9387/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9388 specification allows the initial length to take up either 4 bytes
9389 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9390 bytes describe the length and all offsets will be 8 bytes in length
9391 instead of 4.
9392
f7ef9339
KB
9393 An older, non-standard 64-bit format is also handled by this
9394 function. The older format in question stores the initial length
9395 as an 8-byte quantity without an escape value. Lengths greater
9396 than 2^32 aren't very common which means that the initial 4 bytes
9397 is almost always zero. Since a length value of zero doesn't make
9398 sense for the 32-bit format, this initial zero can be considered to
9399 be an escape value which indicates the presence of the older 64-bit
9400 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9401 greater than 4GB. If it becomes necessary to handle lengths
9402 somewhat larger than 4GB, we could allow other small values (such
9403 as the non-sensical values of 1, 2, and 3) to also be used as
9404 escape values indicating the presence of the old format.
f7ef9339 9405
917c78fc
MK
9406 The value returned via bytes_read should be used to increment the
9407 relevant pointer after calling read_initial_length().
c764a876 9408
613e1657
KB
9409 [ Note: read_initial_length() and read_offset() are based on the
9410 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9411 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9412 from:
9413
f7ef9339 9414 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9415
613e1657
KB
9416 This document is only a draft and is subject to change. (So beware.)
9417
f7ef9339 9418 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9419 determined empirically by examining 64-bit ELF files produced by
9420 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9421
9422 - Kevin, July 16, 2002
613e1657
KB
9423 ] */
9424
9425static LONGEST
c764a876 9426read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9427{
fe1b8b76 9428 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9429
dd373385 9430 if (length == 0xffffffff)
613e1657 9431 {
fe1b8b76 9432 length = bfd_get_64 (abfd, buf + 4);
613e1657 9433 *bytes_read = 12;
613e1657 9434 }
dd373385 9435 else if (length == 0)
f7ef9339 9436 {
dd373385 9437 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9438 length = bfd_get_64 (abfd, buf);
f7ef9339 9439 *bytes_read = 8;
f7ef9339 9440 }
613e1657
KB
9441 else
9442 {
9443 *bytes_read = 4;
613e1657
KB
9444 }
9445
c764a876
DE
9446 return length;
9447}
dd373385 9448
c764a876
DE
9449/* Cover function for read_initial_length.
9450 Returns the length of the object at BUF, and stores the size of the
9451 initial length in *BYTES_READ and stores the size that offsets will be in
9452 *OFFSET_SIZE.
9453 If the initial length size is not equivalent to that specified in
9454 CU_HEADER then issue a complaint.
9455 This is useful when reading non-comp-unit headers. */
dd373385 9456
c764a876
DE
9457static LONGEST
9458read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9459 const struct comp_unit_head *cu_header,
9460 unsigned int *bytes_read,
9461 unsigned int *offset_size)
9462{
9463 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9464
9465 gdb_assert (cu_header->initial_length_size == 4
9466 || cu_header->initial_length_size == 8
9467 || cu_header->initial_length_size == 12);
9468
9469 if (cu_header->initial_length_size != *bytes_read)
9470 complaint (&symfile_complaints,
9471 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9472
c764a876 9473 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9474 return length;
613e1657
KB
9475}
9476
9477/* Read an offset from the data stream. The size of the offset is
917c78fc 9478 given by cu_header->offset_size. */
613e1657
KB
9479
9480static LONGEST
fe1b8b76 9481read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9482 unsigned int *bytes_read)
c764a876
DE
9483{
9484 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9485
c764a876
DE
9486 *bytes_read = cu_header->offset_size;
9487 return offset;
9488}
9489
9490/* Read an offset from the data stream. */
9491
9492static LONGEST
9493read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9494{
9495 LONGEST retval = 0;
9496
c764a876 9497 switch (offset_size)
613e1657
KB
9498 {
9499 case 4:
fe1b8b76 9500 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9501 break;
9502 case 8:
fe1b8b76 9503 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9504 break;
9505 default:
8e65ff28 9506 internal_error (__FILE__, __LINE__,
c764a876 9507 _("read_offset_1: bad switch [in module %s]"),
659b0389 9508 bfd_get_filename (abfd));
613e1657
KB
9509 }
9510
917c78fc 9511 return retval;
613e1657
KB
9512}
9513
fe1b8b76
JB
9514static gdb_byte *
9515read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9516{
9517 /* If the size of a host char is 8 bits, we can return a pointer
9518 to the buffer, otherwise we have to copy the data to a buffer
9519 allocated on the temporary obstack. */
4bdf3d34 9520 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9521 return buf;
c906108c
SS
9522}
9523
9524static char *
9b1c24c8 9525read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9526{
9527 /* If the size of a host char is 8 bits, we can return a pointer
9528 to the string, otherwise we have to copy the string to a buffer
9529 allocated on the temporary obstack. */
4bdf3d34 9530 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9531 if (*buf == '\0')
9532 {
9533 *bytes_read_ptr = 1;
9534 return NULL;
9535 }
fe1b8b76
JB
9536 *bytes_read_ptr = strlen ((char *) buf) + 1;
9537 return (char *) buf;
4bdf3d34
JJ
9538}
9539
9540static char *
fe1b8b76 9541read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9542 const struct comp_unit_head *cu_header,
9543 unsigned int *bytes_read_ptr)
9544{
c764a876 9545 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9546
be391dca 9547 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9548 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9549 {
8a3fe4f8 9550 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9551 bfd_get_filename (abfd));
4bdf3d34 9552 return NULL;
c906108c 9553 }
dce234bc 9554 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9555 {
8a3fe4f8 9556 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9557 bfd_get_filename (abfd));
c906108c
SS
9558 return NULL;
9559 }
4bdf3d34 9560 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9561 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9562 return NULL;
dce234bc 9563 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9564}
9565
ce5d95e1 9566static unsigned long
fe1b8b76 9567read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9568{
ce5d95e1
JB
9569 unsigned long result;
9570 unsigned int num_read;
c906108c
SS
9571 int i, shift;
9572 unsigned char byte;
9573
9574 result = 0;
9575 shift = 0;
9576 num_read = 0;
9577 i = 0;
9578 while (1)
9579 {
fe1b8b76 9580 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9581 buf++;
9582 num_read++;
ce5d95e1 9583 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9584 if ((byte & 128) == 0)
9585 {
9586 break;
9587 }
9588 shift += 7;
9589 }
9590 *bytes_read_ptr = num_read;
9591 return result;
9592}
9593
ce5d95e1 9594static long
fe1b8b76 9595read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9596{
ce5d95e1 9597 long result;
77e0b926 9598 int i, shift, num_read;
c906108c
SS
9599 unsigned char byte;
9600
9601 result = 0;
9602 shift = 0;
c906108c
SS
9603 num_read = 0;
9604 i = 0;
9605 while (1)
9606 {
fe1b8b76 9607 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9608 buf++;
9609 num_read++;
ce5d95e1 9610 result |= ((long)(byte & 127) << shift);
c906108c
SS
9611 shift += 7;
9612 if ((byte & 128) == 0)
9613 {
9614 break;
9615 }
9616 }
77e0b926
DJ
9617 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9618 result |= -(((long)1) << shift);
c906108c
SS
9619 *bytes_read_ptr = num_read;
9620 return result;
9621}
9622
4bb7a0a7
DJ
9623/* Return a pointer to just past the end of an LEB128 number in BUF. */
9624
fe1b8b76
JB
9625static gdb_byte *
9626skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9627{
9628 int byte;
9629
9630 while (1)
9631 {
fe1b8b76 9632 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9633 buf++;
9634 if ((byte & 128) == 0)
9635 return buf;
9636 }
9637}
9638
c906108c 9639static void
e142c38c 9640set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9641{
9642 switch (lang)
9643 {
9644 case DW_LANG_C89:
76bee0cc 9645 case DW_LANG_C99:
c906108c 9646 case DW_LANG_C:
e142c38c 9647 cu->language = language_c;
c906108c
SS
9648 break;
9649 case DW_LANG_C_plus_plus:
e142c38c 9650 cu->language = language_cplus;
c906108c 9651 break;
6aecb9c2
JB
9652 case DW_LANG_D:
9653 cu->language = language_d;
9654 break;
c906108c
SS
9655 case DW_LANG_Fortran77:
9656 case DW_LANG_Fortran90:
b21b22e0 9657 case DW_LANG_Fortran95:
e142c38c 9658 cu->language = language_fortran;
c906108c
SS
9659 break;
9660 case DW_LANG_Mips_Assembler:
e142c38c 9661 cu->language = language_asm;
c906108c 9662 break;
bebd888e 9663 case DW_LANG_Java:
e142c38c 9664 cu->language = language_java;
bebd888e 9665 break;
c906108c 9666 case DW_LANG_Ada83:
8aaf0b47 9667 case DW_LANG_Ada95:
bc5f45f8
JB
9668 cu->language = language_ada;
9669 break;
72019c9c
GM
9670 case DW_LANG_Modula2:
9671 cu->language = language_m2;
9672 break;
fe8e67fd
PM
9673 case DW_LANG_Pascal83:
9674 cu->language = language_pascal;
9675 break;
22566fbd
DJ
9676 case DW_LANG_ObjC:
9677 cu->language = language_objc;
9678 break;
c906108c
SS
9679 case DW_LANG_Cobol74:
9680 case DW_LANG_Cobol85:
c906108c 9681 default:
e142c38c 9682 cu->language = language_minimal;
c906108c
SS
9683 break;
9684 }
e142c38c 9685 cu->language_defn = language_def (cu->language);
c906108c
SS
9686}
9687
9688/* Return the named attribute or NULL if not there. */
9689
9690static struct attribute *
e142c38c 9691dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9692{
9693 unsigned int i;
9694 struct attribute *spec = NULL;
9695
9696 for (i = 0; i < die->num_attrs; ++i)
9697 {
9698 if (die->attrs[i].name == name)
10b3939b 9699 return &die->attrs[i];
c906108c
SS
9700 if (die->attrs[i].name == DW_AT_specification
9701 || die->attrs[i].name == DW_AT_abstract_origin)
9702 spec = &die->attrs[i];
9703 }
c906108c 9704
10b3939b 9705 if (spec)
f2f0e013
DJ
9706 {
9707 die = follow_die_ref (die, spec, &cu);
9708 return dwarf2_attr (die, name, cu);
9709 }
c5aa993b 9710
c906108c
SS
9711 return NULL;
9712}
9713
348e048f
DE
9714/* Return the named attribute or NULL if not there,
9715 but do not follow DW_AT_specification, etc.
9716 This is for use in contexts where we're reading .debug_types dies.
9717 Following DW_AT_specification, DW_AT_abstract_origin will take us
9718 back up the chain, and we want to go down. */
9719
9720static struct attribute *
9721dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9722 struct dwarf2_cu *cu)
9723{
9724 unsigned int i;
9725
9726 for (i = 0; i < die->num_attrs; ++i)
9727 if (die->attrs[i].name == name)
9728 return &die->attrs[i];
9729
9730 return NULL;
9731}
9732
05cf31d1
JB
9733/* Return non-zero iff the attribute NAME is defined for the given DIE,
9734 and holds a non-zero value. This function should only be used for
2dc7f7b3 9735 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9736
9737static int
9738dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9739{
9740 struct attribute *attr = dwarf2_attr (die, name, cu);
9741
9742 return (attr && DW_UNSND (attr));
9743}
9744
3ca72b44 9745static int
e142c38c 9746die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9747{
05cf31d1
JB
9748 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9749 which value is non-zero. However, we have to be careful with
9750 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9751 (via dwarf2_flag_true_p) follows this attribute. So we may
9752 end up accidently finding a declaration attribute that belongs
9753 to a different DIE referenced by the specification attribute,
9754 even though the given DIE does not have a declaration attribute. */
9755 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9756 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9757}
9758
63d06c5c 9759/* Return the die giving the specification for DIE, if there is
f2f0e013 9760 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9761 containing the return value on output. If there is no
9762 specification, but there is an abstract origin, that is
9763 returned. */
63d06c5c
DC
9764
9765static struct die_info *
f2f0e013 9766die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9767{
f2f0e013
DJ
9768 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9769 *spec_cu);
63d06c5c 9770
edb3359d
DJ
9771 if (spec_attr == NULL)
9772 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9773
63d06c5c
DC
9774 if (spec_attr == NULL)
9775 return NULL;
9776 else
f2f0e013 9777 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9778}
c906108c 9779
debd256d
JB
9780/* Free the line_header structure *LH, and any arrays and strings it
9781 refers to. */
9782static void
9783free_line_header (struct line_header *lh)
9784{
9785 if (lh->standard_opcode_lengths)
a8bc7b56 9786 xfree (lh->standard_opcode_lengths);
debd256d
JB
9787
9788 /* Remember that all the lh->file_names[i].name pointers are
9789 pointers into debug_line_buffer, and don't need to be freed. */
9790 if (lh->file_names)
a8bc7b56 9791 xfree (lh->file_names);
debd256d
JB
9792
9793 /* Similarly for the include directory names. */
9794 if (lh->include_dirs)
a8bc7b56 9795 xfree (lh->include_dirs);
debd256d 9796
a8bc7b56 9797 xfree (lh);
debd256d
JB
9798}
9799
9800
9801/* Add an entry to LH's include directory table. */
9802static void
9803add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9804{
debd256d
JB
9805 /* Grow the array if necessary. */
9806 if (lh->include_dirs_size == 0)
c5aa993b 9807 {
debd256d
JB
9808 lh->include_dirs_size = 1; /* for testing */
9809 lh->include_dirs = xmalloc (lh->include_dirs_size
9810 * sizeof (*lh->include_dirs));
9811 }
9812 else if (lh->num_include_dirs >= lh->include_dirs_size)
9813 {
9814 lh->include_dirs_size *= 2;
9815 lh->include_dirs = xrealloc (lh->include_dirs,
9816 (lh->include_dirs_size
9817 * sizeof (*lh->include_dirs)));
c5aa993b 9818 }
c906108c 9819
debd256d
JB
9820 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9821}
6e70227d 9822
debd256d
JB
9823
9824/* Add an entry to LH's file name table. */
9825static void
9826add_file_name (struct line_header *lh,
9827 char *name,
9828 unsigned int dir_index,
9829 unsigned int mod_time,
9830 unsigned int length)
9831{
9832 struct file_entry *fe;
9833
9834 /* Grow the array if necessary. */
9835 if (lh->file_names_size == 0)
9836 {
9837 lh->file_names_size = 1; /* for testing */
9838 lh->file_names = xmalloc (lh->file_names_size
9839 * sizeof (*lh->file_names));
9840 }
9841 else if (lh->num_file_names >= lh->file_names_size)
9842 {
9843 lh->file_names_size *= 2;
9844 lh->file_names = xrealloc (lh->file_names,
9845 (lh->file_names_size
9846 * sizeof (*lh->file_names)));
9847 }
9848
9849 fe = &lh->file_names[lh->num_file_names++];
9850 fe->name = name;
9851 fe->dir_index = dir_index;
9852 fe->mod_time = mod_time;
9853 fe->length = length;
aaa75496 9854 fe->included_p = 0;
cb1df416 9855 fe->symtab = NULL;
debd256d 9856}
6e70227d 9857
debd256d
JB
9858
9859/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9860 .debug_line, according to the endianness of ABFD. Return a pointer
9861 to a struct line_header, allocated using xmalloc.
debd256d
JB
9862
9863 NOTE: the strings in the include directory and file name tables of
9864 the returned object point into debug_line_buffer, and must not be
9865 freed. */
9866static struct line_header *
9867dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9868 struct dwarf2_cu *cu)
debd256d
JB
9869{
9870 struct cleanup *back_to;
9871 struct line_header *lh;
fe1b8b76 9872 gdb_byte *line_ptr;
c764a876 9873 unsigned int bytes_read, offset_size;
debd256d
JB
9874 int i;
9875 char *cur_dir, *cur_file;
9876
be391dca 9877 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9878 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9879 {
e2e0b3e5 9880 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9881 return 0;
9882 }
9883
a738430d
MK
9884 /* Make sure that at least there's room for the total_length field.
9885 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9886 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9887 {
4d3c2250 9888 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9889 return 0;
9890 }
9891
9892 lh = xmalloc (sizeof (*lh));
9893 memset (lh, 0, sizeof (*lh));
9894 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9895 (void *) lh);
9896
dce234bc 9897 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9898
a738430d 9899 /* Read in the header. */
6e70227d 9900 lh->total_length =
c764a876
DE
9901 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9902 &bytes_read, &offset_size);
debd256d 9903 line_ptr += bytes_read;
dce234bc
PP
9904 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9905 + dwarf2_per_objfile->line.size))
debd256d 9906 {
4d3c2250 9907 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9908 return 0;
9909 }
9910 lh->statement_program_end = line_ptr + lh->total_length;
9911 lh->version = read_2_bytes (abfd, line_ptr);
9912 line_ptr += 2;
c764a876
DE
9913 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9914 line_ptr += offset_size;
debd256d
JB
9915 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9916 line_ptr += 1;
2dc7f7b3
TT
9917 if (lh->version >= 4)
9918 {
9919 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9920 line_ptr += 1;
9921 }
9922 else
9923 lh->maximum_ops_per_instruction = 1;
9924
9925 if (lh->maximum_ops_per_instruction == 0)
9926 {
9927 lh->maximum_ops_per_instruction = 1;
9928 complaint (&symfile_complaints,
9929 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9930 }
9931
debd256d
JB
9932 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9933 line_ptr += 1;
9934 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9935 line_ptr += 1;
9936 lh->line_range = read_1_byte (abfd, line_ptr);
9937 line_ptr += 1;
9938 lh->opcode_base = read_1_byte (abfd, line_ptr);
9939 line_ptr += 1;
9940 lh->standard_opcode_lengths
fe1b8b76 9941 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9942
9943 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9944 for (i = 1; i < lh->opcode_base; ++i)
9945 {
9946 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9947 line_ptr += 1;
9948 }
9949
a738430d 9950 /* Read directory table. */
9b1c24c8 9951 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9952 {
9953 line_ptr += bytes_read;
9954 add_include_dir (lh, cur_dir);
9955 }
9956 line_ptr += bytes_read;
9957
a738430d 9958 /* Read file name table. */
9b1c24c8 9959 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9960 {
9961 unsigned int dir_index, mod_time, length;
9962
9963 line_ptr += bytes_read;
9964 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9965 line_ptr += bytes_read;
9966 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9967 line_ptr += bytes_read;
9968 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9969 line_ptr += bytes_read;
9970
9971 add_file_name (lh, cur_file, dir_index, mod_time, length);
9972 }
9973 line_ptr += bytes_read;
6e70227d 9974 lh->statement_program_start = line_ptr;
debd256d 9975
dce234bc
PP
9976 if (line_ptr > (dwarf2_per_objfile->line.buffer
9977 + dwarf2_per_objfile->line.size))
4d3c2250 9978 complaint (&symfile_complaints,
e2e0b3e5 9979 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9980
9981 discard_cleanups (back_to);
9982 return lh;
9983}
c906108c 9984
5fb290d7
DJ
9985/* This function exists to work around a bug in certain compilers
9986 (particularly GCC 2.95), in which the first line number marker of a
9987 function does not show up until after the prologue, right before
9988 the second line number marker. This function shifts ADDRESS down
9989 to the beginning of the function if necessary, and is called on
9990 addresses passed to record_line. */
9991
9992static CORE_ADDR
e142c38c 9993check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9994{
9995 struct function_range *fn;
9996
9997 /* Find the function_range containing address. */
e142c38c 9998 if (!cu->first_fn)
5fb290d7
DJ
9999 return address;
10000
e142c38c
DJ
10001 if (!cu->cached_fn)
10002 cu->cached_fn = cu->first_fn;
5fb290d7 10003
e142c38c 10004 fn = cu->cached_fn;
5fb290d7
DJ
10005 while (fn)
10006 if (fn->lowpc <= address && fn->highpc > address)
10007 goto found;
10008 else
10009 fn = fn->next;
10010
e142c38c
DJ
10011 fn = cu->first_fn;
10012 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10013 if (fn->lowpc <= address && fn->highpc > address)
10014 goto found;
10015 else
10016 fn = fn->next;
10017
10018 return address;
10019
10020 found:
10021 if (fn->seen_line)
10022 return address;
10023 if (address != fn->lowpc)
4d3c2250 10024 complaint (&symfile_complaints,
e2e0b3e5 10025 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10026 (unsigned long) address, fn->name);
5fb290d7
DJ
10027 fn->seen_line = 1;
10028 return fn->lowpc;
10029}
10030
c6da4cef
DE
10031/* Subroutine of dwarf_decode_lines to simplify it.
10032 Return the file name of the psymtab for included file FILE_INDEX
10033 in line header LH of PST.
10034 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10035 If space for the result is malloc'd, it will be freed by a cleanup.
10036 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10037
10038static char *
10039psymtab_include_file_name (const struct line_header *lh, int file_index,
10040 const struct partial_symtab *pst,
10041 const char *comp_dir)
10042{
10043 const struct file_entry fe = lh->file_names [file_index];
10044 char *include_name = fe.name;
10045 char *include_name_to_compare = include_name;
10046 char *dir_name = NULL;
72b9f47f
TT
10047 const char *pst_filename;
10048 char *copied_name = NULL;
c6da4cef
DE
10049 int file_is_pst;
10050
10051 if (fe.dir_index)
10052 dir_name = lh->include_dirs[fe.dir_index - 1];
10053
10054 if (!IS_ABSOLUTE_PATH (include_name)
10055 && (dir_name != NULL || comp_dir != NULL))
10056 {
10057 /* Avoid creating a duplicate psymtab for PST.
10058 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10059 Before we do the comparison, however, we need to account
10060 for DIR_NAME and COMP_DIR.
10061 First prepend dir_name (if non-NULL). If we still don't
10062 have an absolute path prepend comp_dir (if non-NULL).
10063 However, the directory we record in the include-file's
10064 psymtab does not contain COMP_DIR (to match the
10065 corresponding symtab(s)).
10066
10067 Example:
10068
10069 bash$ cd /tmp
10070 bash$ gcc -g ./hello.c
10071 include_name = "hello.c"
10072 dir_name = "."
10073 DW_AT_comp_dir = comp_dir = "/tmp"
10074 DW_AT_name = "./hello.c" */
10075
10076 if (dir_name != NULL)
10077 {
10078 include_name = concat (dir_name, SLASH_STRING,
10079 include_name, (char *)NULL);
10080 include_name_to_compare = include_name;
10081 make_cleanup (xfree, include_name);
10082 }
10083 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10084 {
10085 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10086 include_name, (char *)NULL);
10087 }
10088 }
10089
10090 pst_filename = pst->filename;
10091 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10092 {
72b9f47f
TT
10093 copied_name = concat (pst->dirname, SLASH_STRING,
10094 pst_filename, (char *)NULL);
10095 pst_filename = copied_name;
c6da4cef
DE
10096 }
10097
1e3fad37 10098 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10099
10100 if (include_name_to_compare != include_name)
10101 xfree (include_name_to_compare);
72b9f47f
TT
10102 if (copied_name != NULL)
10103 xfree (copied_name);
c6da4cef
DE
10104
10105 if (file_is_pst)
10106 return NULL;
10107 return include_name;
10108}
10109
aaa75496
JB
10110/* Decode the Line Number Program (LNP) for the given line_header
10111 structure and CU. The actual information extracted and the type
10112 of structures created from the LNP depends on the value of PST.
10113
10114 1. If PST is NULL, then this procedure uses the data from the program
10115 to create all necessary symbol tables, and their linetables.
6e70227d 10116
aaa75496
JB
10117 2. If PST is not NULL, this procedure reads the program to determine
10118 the list of files included by the unit represented by PST, and
c6da4cef
DE
10119 builds all the associated partial symbol tables.
10120
10121 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10122 It is used for relative paths in the line table.
10123 NOTE: When processing partial symtabs (pst != NULL),
10124 comp_dir == pst->dirname.
10125
10126 NOTE: It is important that psymtabs have the same file name (via strcmp)
10127 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10128 symtab we don't use it in the name of the psymtabs we create.
10129 E.g. expand_line_sal requires this when finding psymtabs to expand.
10130 A good testcase for this is mb-inline.exp. */
debd256d 10131
c906108c 10132static void
72b9f47f 10133dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10134 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10135{
a8c50c1f 10136 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10137 gdb_byte *line_end;
a8c50c1f 10138 unsigned int bytes_read, extended_len;
c906108c 10139 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10140 CORE_ADDR baseaddr;
10141 struct objfile *objfile = cu->objfile;
fbf65064 10142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10143 const int decode_for_pst_p = (pst != NULL);
cb1df416 10144 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10145
10146 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10147
debd256d
JB
10148 line_ptr = lh->statement_program_start;
10149 line_end = lh->statement_program_end;
c906108c
SS
10150
10151 /* Read the statement sequences until there's nothing left. */
10152 while (line_ptr < line_end)
10153 {
10154 /* state machine registers */
10155 CORE_ADDR address = 0;
10156 unsigned int file = 1;
10157 unsigned int line = 1;
10158 unsigned int column = 0;
debd256d 10159 int is_stmt = lh->default_is_stmt;
c906108c
SS
10160 int basic_block = 0;
10161 int end_sequence = 0;
fbf65064 10162 CORE_ADDR addr;
2dc7f7b3 10163 unsigned char op_index = 0;
c906108c 10164
aaa75496 10165 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10166 {
aaa75496 10167 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10168 /* lh->include_dirs and lh->file_names are 0-based, but the
10169 directory and file name numbers in the statement program
10170 are 1-based. */
10171 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10172 char *dir = NULL;
a738430d 10173
debd256d
JB
10174 if (fe->dir_index)
10175 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10176
10177 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10178 }
10179
a738430d 10180 /* Decode the table. */
c5aa993b 10181 while (!end_sequence)
c906108c
SS
10182 {
10183 op_code = read_1_byte (abfd, line_ptr);
10184 line_ptr += 1;
59205f5a
JB
10185 if (line_ptr > line_end)
10186 {
10187 dwarf2_debug_line_missing_end_sequence_complaint ();
10188 break;
10189 }
9aa1fe7e 10190
debd256d 10191 if (op_code >= lh->opcode_base)
6e70227d 10192 {
a738430d 10193 /* Special operand. */
debd256d 10194 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10195 address += (((op_index + (adj_opcode / lh->line_range))
10196 / lh->maximum_ops_per_instruction)
10197 * lh->minimum_instruction_length);
10198 op_index = ((op_index + (adj_opcode / lh->line_range))
10199 % lh->maximum_ops_per_instruction);
debd256d 10200 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10201 if (lh->num_file_names < file || file == 0)
25e43795 10202 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10203 /* For now we ignore lines not starting on an
10204 instruction boundary. */
10205 else if (op_index == 0)
25e43795
DJ
10206 {
10207 lh->file_names[file - 1].included_p = 1;
ca5f395d 10208 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10209 {
10210 if (last_subfile != current_subfile)
10211 {
10212 addr = gdbarch_addr_bits_remove (gdbarch, address);
10213 if (last_subfile)
10214 record_line (last_subfile, 0, addr);
10215 last_subfile = current_subfile;
10216 }
25e43795 10217 /* Append row to matrix using current values. */
fbf65064
UW
10218 addr = check_cu_functions (address, cu);
10219 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10220 record_line (current_subfile, line, addr);
366da635 10221 }
25e43795 10222 }
ca5f395d 10223 basic_block = 0;
9aa1fe7e
GK
10224 }
10225 else switch (op_code)
c906108c
SS
10226 {
10227 case DW_LNS_extended_op:
a8c50c1f 10228 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10229 line_ptr += bytes_read;
a8c50c1f 10230 extended_end = line_ptr + extended_len;
c906108c
SS
10231 extended_op = read_1_byte (abfd, line_ptr);
10232 line_ptr += 1;
10233 switch (extended_op)
10234 {
10235 case DW_LNE_end_sequence:
10236 end_sequence = 1;
c906108c
SS
10237 break;
10238 case DW_LNE_set_address:
e7c27a73 10239 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10240 op_index = 0;
107d2387
AC
10241 line_ptr += bytes_read;
10242 address += baseaddr;
c906108c
SS
10243 break;
10244 case DW_LNE_define_file:
debd256d
JB
10245 {
10246 char *cur_file;
10247 unsigned int dir_index, mod_time, length;
6e70227d 10248
9b1c24c8 10249 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10250 line_ptr += bytes_read;
10251 dir_index =
10252 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10253 line_ptr += bytes_read;
10254 mod_time =
10255 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10256 line_ptr += bytes_read;
10257 length =
10258 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10259 line_ptr += bytes_read;
10260 add_file_name (lh, cur_file, dir_index, mod_time, length);
10261 }
c906108c 10262 break;
d0c6ba3d
CC
10263 case DW_LNE_set_discriminator:
10264 /* The discriminator is not interesting to the debugger;
10265 just ignore it. */
10266 line_ptr = extended_end;
10267 break;
c906108c 10268 default:
4d3c2250 10269 complaint (&symfile_complaints,
e2e0b3e5 10270 _("mangled .debug_line section"));
debd256d 10271 return;
c906108c 10272 }
a8c50c1f
DJ
10273 /* Make sure that we parsed the extended op correctly. If e.g.
10274 we expected a different address size than the producer used,
10275 we may have read the wrong number of bytes. */
10276 if (line_ptr != extended_end)
10277 {
10278 complaint (&symfile_complaints,
10279 _("mangled .debug_line section"));
10280 return;
10281 }
c906108c
SS
10282 break;
10283 case DW_LNS_copy:
59205f5a 10284 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10285 dwarf2_debug_line_missing_file_complaint ();
10286 else
366da635 10287 {
25e43795 10288 lh->file_names[file - 1].included_p = 1;
ca5f395d 10289 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10290 {
10291 if (last_subfile != current_subfile)
10292 {
10293 addr = gdbarch_addr_bits_remove (gdbarch, address);
10294 if (last_subfile)
10295 record_line (last_subfile, 0, addr);
10296 last_subfile = current_subfile;
10297 }
10298 addr = check_cu_functions (address, cu);
10299 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10300 record_line (current_subfile, line, addr);
10301 }
366da635 10302 }
c906108c
SS
10303 basic_block = 0;
10304 break;
10305 case DW_LNS_advance_pc:
2dc7f7b3
TT
10306 {
10307 CORE_ADDR adjust
10308 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10309
10310 address += (((op_index + adjust)
10311 / lh->maximum_ops_per_instruction)
10312 * lh->minimum_instruction_length);
10313 op_index = ((op_index + adjust)
10314 % lh->maximum_ops_per_instruction);
10315 line_ptr += bytes_read;
10316 }
c906108c
SS
10317 break;
10318 case DW_LNS_advance_line:
10319 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10320 line_ptr += bytes_read;
10321 break;
10322 case DW_LNS_set_file:
debd256d 10323 {
a738430d
MK
10324 /* The arrays lh->include_dirs and lh->file_names are
10325 0-based, but the directory and file name numbers in
10326 the statement program are 1-based. */
debd256d 10327 struct file_entry *fe;
4f1520fb 10328 char *dir = NULL;
a738430d 10329
debd256d
JB
10330 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10331 line_ptr += bytes_read;
59205f5a 10332 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10333 dwarf2_debug_line_missing_file_complaint ();
10334 else
10335 {
10336 fe = &lh->file_names[file - 1];
10337 if (fe->dir_index)
10338 dir = lh->include_dirs[fe->dir_index - 1];
10339 if (!decode_for_pst_p)
10340 {
10341 last_subfile = current_subfile;
10342 dwarf2_start_subfile (fe->name, dir, comp_dir);
10343 }
10344 }
debd256d 10345 }
c906108c
SS
10346 break;
10347 case DW_LNS_set_column:
10348 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10349 line_ptr += bytes_read;
10350 break;
10351 case DW_LNS_negate_stmt:
10352 is_stmt = (!is_stmt);
10353 break;
10354 case DW_LNS_set_basic_block:
10355 basic_block = 1;
10356 break;
c2c6d25f
JM
10357 /* Add to the address register of the state machine the
10358 address increment value corresponding to special opcode
a738430d
MK
10359 255. I.e., this value is scaled by the minimum
10360 instruction length since special opcode 255 would have
10361 scaled the the increment. */
c906108c 10362 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10363 {
10364 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10365
10366 address += (((op_index + adjust)
10367 / lh->maximum_ops_per_instruction)
10368 * lh->minimum_instruction_length);
10369 op_index = ((op_index + adjust)
10370 % lh->maximum_ops_per_instruction);
10371 }
c906108c
SS
10372 break;
10373 case DW_LNS_fixed_advance_pc:
10374 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10375 op_index = 0;
c906108c
SS
10376 line_ptr += 2;
10377 break;
9aa1fe7e 10378 default:
a738430d
MK
10379 {
10380 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10381 int i;
a738430d 10382
debd256d 10383 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10384 {
10385 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10386 line_ptr += bytes_read;
10387 }
10388 }
c906108c
SS
10389 }
10390 }
59205f5a
JB
10391 if (lh->num_file_names < file || file == 0)
10392 dwarf2_debug_line_missing_file_complaint ();
10393 else
10394 {
10395 lh->file_names[file - 1].included_p = 1;
10396 if (!decode_for_pst_p)
fbf65064
UW
10397 {
10398 addr = gdbarch_addr_bits_remove (gdbarch, address);
10399 record_line (current_subfile, 0, addr);
10400 }
59205f5a 10401 }
c906108c 10402 }
aaa75496
JB
10403
10404 if (decode_for_pst_p)
10405 {
10406 int file_index;
10407
10408 /* Now that we're done scanning the Line Header Program, we can
10409 create the psymtab of each included file. */
10410 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10411 if (lh->file_names[file_index].included_p == 1)
10412 {
c6da4cef
DE
10413 char *include_name =
10414 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10415 if (include_name != NULL)
aaa75496
JB
10416 dwarf2_create_include_psymtab (include_name, pst, objfile);
10417 }
10418 }
cb1df416
DJ
10419 else
10420 {
10421 /* Make sure a symtab is created for every file, even files
10422 which contain only variables (i.e. no code with associated
10423 line numbers). */
10424
10425 int i;
10426 struct file_entry *fe;
10427
10428 for (i = 0; i < lh->num_file_names; i++)
10429 {
10430 char *dir = NULL;
9a619af0 10431
cb1df416
DJ
10432 fe = &lh->file_names[i];
10433 if (fe->dir_index)
10434 dir = lh->include_dirs[fe->dir_index - 1];
10435 dwarf2_start_subfile (fe->name, dir, comp_dir);
10436
10437 /* Skip the main file; we don't need it, and it must be
10438 allocated last, so that it will show up before the
10439 non-primary symtabs in the objfile's symtab list. */
10440 if (current_subfile == first_subfile)
10441 continue;
10442
10443 if (current_subfile->symtab == NULL)
10444 current_subfile->symtab = allocate_symtab (current_subfile->name,
10445 cu->objfile);
10446 fe->symtab = current_subfile->symtab;
10447 }
10448 }
c906108c
SS
10449}
10450
10451/* Start a subfile for DWARF. FILENAME is the name of the file and
10452 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10453 or NULL if not known. COMP_DIR is the compilation directory for the
10454 linetable's compilation unit or NULL if not known.
c906108c
SS
10455 This routine tries to keep line numbers from identical absolute and
10456 relative file names in a common subfile.
10457
10458 Using the `list' example from the GDB testsuite, which resides in
10459 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10460 of /srcdir/list0.c yields the following debugging information for list0.c:
10461
c5aa993b
JM
10462 DW_AT_name: /srcdir/list0.c
10463 DW_AT_comp_dir: /compdir
357e46e7 10464 files.files[0].name: list0.h
c5aa993b 10465 files.files[0].dir: /srcdir
357e46e7 10466 files.files[1].name: list0.c
c5aa993b 10467 files.files[1].dir: /srcdir
c906108c
SS
10468
10469 The line number information for list0.c has to end up in a single
4f1520fb
FR
10470 subfile, so that `break /srcdir/list0.c:1' works as expected.
10471 start_subfile will ensure that this happens provided that we pass the
10472 concatenation of files.files[1].dir and files.files[1].name as the
10473 subfile's name. */
c906108c
SS
10474
10475static void
72b9f47f 10476dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
c906108c 10477{
4f1520fb
FR
10478 char *fullname;
10479
10480 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10481 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10482 second argument to start_subfile. To be consistent, we do the
10483 same here. In order not to lose the line information directory,
10484 we concatenate it to the filename when it makes sense.
10485 Note that the Dwarf3 standard says (speaking of filenames in line
10486 information): ``The directory index is ignored for file names
10487 that represent full path names''. Thus ignoring dirname in the
10488 `else' branch below isn't an issue. */
c906108c 10489
d5166ae1 10490 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10491 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10492 else
10493 fullname = filename;
c906108c 10494
4f1520fb
FR
10495 start_subfile (fullname, comp_dir);
10496
10497 if (fullname != filename)
10498 xfree (fullname);
c906108c
SS
10499}
10500
4c2df51b
DJ
10501static void
10502var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10503 struct dwarf2_cu *cu)
4c2df51b 10504{
e7c27a73
DJ
10505 struct objfile *objfile = cu->objfile;
10506 struct comp_unit_head *cu_header = &cu->header;
10507
4c2df51b
DJ
10508 /* NOTE drow/2003-01-30: There used to be a comment and some special
10509 code here to turn a symbol with DW_AT_external and a
10510 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10511 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10512 with some versions of binutils) where shared libraries could have
10513 relocations against symbols in their debug information - the
10514 minimal symbol would have the right address, but the debug info
10515 would not. It's no longer necessary, because we will explicitly
10516 apply relocations when we read in the debug information now. */
10517
10518 /* A DW_AT_location attribute with no contents indicates that a
10519 variable has been optimized away. */
10520 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10521 {
10522 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10523 return;
10524 }
10525
10526 /* Handle one degenerate form of location expression specially, to
10527 preserve GDB's previous behavior when section offsets are
10528 specified. If this is just a DW_OP_addr then mark this symbol
10529 as LOC_STATIC. */
10530
10531 if (attr_form_is_block (attr)
10532 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10533 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10534 {
891d2f0b 10535 unsigned int dummy;
4c2df51b
DJ
10536
10537 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10538 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10539 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10540 fixup_symbol_section (sym, objfile);
10541 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10542 SYMBOL_SECTION (sym));
4c2df51b
DJ
10543 return;
10544 }
10545
10546 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10547 expression evaluator, and use LOC_COMPUTED only when necessary
10548 (i.e. when the value of a register or memory location is
10549 referenced, or a thread-local block, etc.). Then again, it might
10550 not be worthwhile. I'm assuming that it isn't unless performance
10551 or memory numbers show me otherwise. */
10552
e7c27a73 10553 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10554 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10555}
10556
c906108c
SS
10557/* Given a pointer to a DWARF information entry, figure out if we need
10558 to make a symbol table entry for it, and if so, create a new entry
10559 and return a pointer to it.
10560 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10561 used the passed type.
10562 If SPACE is not NULL, use it to hold the new symbol. If it is
10563 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10564
10565static struct symbol *
34eaf542
TT
10566new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10567 struct symbol *space)
c906108c 10568{
e7c27a73 10569 struct objfile *objfile = cu->objfile;
c906108c
SS
10570 struct symbol *sym = NULL;
10571 char *name;
10572 struct attribute *attr = NULL;
10573 struct attribute *attr2 = NULL;
e142c38c 10574 CORE_ADDR baseaddr;
e37fd15a
SW
10575 struct pending **list_to_add = NULL;
10576
edb3359d 10577 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10578
10579 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10580
94af9270 10581 name = dwarf2_name (die, cu);
c906108c
SS
10582 if (name)
10583 {
94af9270 10584 const char *linkagename;
34eaf542 10585 int suppress_add = 0;
94af9270 10586
34eaf542
TT
10587 if (space)
10588 sym = space;
10589 else
10590 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10591 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10592
10593 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10594 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10595 linkagename = dwarf2_physname (name, die, cu);
10596 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10597
f55ee35c
JK
10598 /* Fortran does not have mangling standard and the mangling does differ
10599 between gfortran, iFort etc. */
10600 if (cu->language == language_fortran
b250c185 10601 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10602 symbol_set_demangled_name (&(sym->ginfo),
10603 (char *) dwarf2_full_name (name, die, cu),
10604 NULL);
f55ee35c 10605
c906108c 10606 /* Default assumptions.
c5aa993b 10607 Use the passed type or decode it from the die. */
176620f1 10608 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10609 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10610 if (type != NULL)
10611 SYMBOL_TYPE (sym) = type;
10612 else
e7c27a73 10613 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10614 attr = dwarf2_attr (die,
10615 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10616 cu);
c906108c
SS
10617 if (attr)
10618 {
10619 SYMBOL_LINE (sym) = DW_UNSND (attr);
10620 }
cb1df416 10621
edb3359d
DJ
10622 attr = dwarf2_attr (die,
10623 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10624 cu);
cb1df416
DJ
10625 if (attr)
10626 {
10627 int file_index = DW_UNSND (attr);
9a619af0 10628
cb1df416
DJ
10629 if (cu->line_header == NULL
10630 || file_index > cu->line_header->num_file_names)
10631 complaint (&symfile_complaints,
10632 _("file index out of range"));
1c3d648d 10633 else if (file_index > 0)
cb1df416
DJ
10634 {
10635 struct file_entry *fe;
9a619af0 10636
cb1df416
DJ
10637 fe = &cu->line_header->file_names[file_index - 1];
10638 SYMBOL_SYMTAB (sym) = fe->symtab;
10639 }
10640 }
10641
c906108c
SS
10642 switch (die->tag)
10643 {
10644 case DW_TAG_label:
e142c38c 10645 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10646 if (attr)
10647 {
10648 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10649 }
0f5238ed
TT
10650 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10651 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10652 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10653 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10654 break;
10655 case DW_TAG_subprogram:
10656 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10657 finish_block. */
10658 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10659 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10660 if ((attr2 && (DW_UNSND (attr2) != 0))
10661 || cu->language == language_ada)
c906108c 10662 {
2cfa0c8d
JB
10663 /* Subprograms marked external are stored as a global symbol.
10664 Ada subprograms, whether marked external or not, are always
10665 stored as a global symbol, because we want to be able to
10666 access them globally. For instance, we want to be able
10667 to break on a nested subprogram without having to
10668 specify the context. */
e37fd15a 10669 list_to_add = &global_symbols;
c906108c
SS
10670 }
10671 else
10672 {
e37fd15a 10673 list_to_add = cu->list_in_scope;
c906108c
SS
10674 }
10675 break;
edb3359d
DJ
10676 case DW_TAG_inlined_subroutine:
10677 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10678 finish_block. */
10679 SYMBOL_CLASS (sym) = LOC_BLOCK;
10680 SYMBOL_INLINED (sym) = 1;
10681 /* Do not add the symbol to any lists. It will be found via
10682 BLOCK_FUNCTION from the blockvector. */
10683 break;
34eaf542
TT
10684 case DW_TAG_template_value_param:
10685 suppress_add = 1;
10686 /* Fall through. */
72929c62 10687 case DW_TAG_constant:
c906108c 10688 case DW_TAG_variable:
254e6b9e 10689 case DW_TAG_member:
c906108c
SS
10690 /* Compilation with minimal debug info may result in variables
10691 with missing type entries. Change the misleading `void' type
10692 to something sensible. */
10693 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10694 SYMBOL_TYPE (sym)
46bf5051 10695 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10696
e142c38c 10697 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10698 /* In the case of DW_TAG_member, we should only be called for
10699 static const members. */
10700 if (die->tag == DW_TAG_member)
10701 {
3863f96c
DE
10702 /* dwarf2_add_field uses die_is_declaration,
10703 so we do the same. */
254e6b9e
DE
10704 gdb_assert (die_is_declaration (die, cu));
10705 gdb_assert (attr);
10706 }
c906108c
SS
10707 if (attr)
10708 {
e7c27a73 10709 dwarf2_const_value (attr, sym, cu);
e142c38c 10710 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10711 if (!suppress_add)
34eaf542
TT
10712 {
10713 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10714 list_to_add = &global_symbols;
34eaf542 10715 else
e37fd15a 10716 list_to_add = cu->list_in_scope;
34eaf542 10717 }
c906108c
SS
10718 break;
10719 }
e142c38c 10720 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10721 if (attr)
10722 {
e7c27a73 10723 var_decode_location (attr, sym, cu);
e142c38c 10724 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10725 if (SYMBOL_CLASS (sym) == LOC_STATIC
10726 && SYMBOL_VALUE_ADDRESS (sym) == 0
10727 && !dwarf2_per_objfile->has_section_at_zero)
10728 {
10729 /* When a static variable is eliminated by the linker,
10730 the corresponding debug information is not stripped
10731 out, but the variable address is set to null;
10732 do not add such variables into symbol table. */
10733 }
10734 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10735 {
f55ee35c
JK
10736 /* Workaround gfortran PR debug/40040 - it uses
10737 DW_AT_location for variables in -fPIC libraries which may
10738 get overriden by other libraries/executable and get
10739 a different address. Resolve it by the minimal symbol
10740 which may come from inferior's executable using copy
10741 relocation. Make this workaround only for gfortran as for
10742 other compilers GDB cannot guess the minimal symbol
10743 Fortran mangling kind. */
10744 if (cu->language == language_fortran && die->parent
10745 && die->parent->tag == DW_TAG_module
10746 && cu->producer
10747 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10748 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10749
1c809c68
TT
10750 /* A variable with DW_AT_external is never static,
10751 but it may be block-scoped. */
10752 list_to_add = (cu->list_in_scope == &file_symbols
10753 ? &global_symbols : cu->list_in_scope);
1c809c68 10754 }
c906108c 10755 else
e37fd15a 10756 list_to_add = cu->list_in_scope;
c906108c
SS
10757 }
10758 else
10759 {
10760 /* We do not know the address of this symbol.
c5aa993b
JM
10761 If it is an external symbol and we have type information
10762 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10763 The address of the variable will then be determined from
10764 the minimal symbol table whenever the variable is
10765 referenced. */
e142c38c 10766 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10767 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10768 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10769 {
0fe7935b
DJ
10770 /* A variable with DW_AT_external is never static, but it
10771 may be block-scoped. */
10772 list_to_add = (cu->list_in_scope == &file_symbols
10773 ? &global_symbols : cu->list_in_scope);
10774
c906108c 10775 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10776 }
442ddf59
JK
10777 else if (!die_is_declaration (die, cu))
10778 {
10779 /* Use the default LOC_OPTIMIZED_OUT class. */
10780 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10781 if (!suppress_add)
10782 list_to_add = cu->list_in_scope;
442ddf59 10783 }
c906108c
SS
10784 }
10785 break;
10786 case DW_TAG_formal_parameter:
edb3359d
DJ
10787 /* If we are inside a function, mark this as an argument. If
10788 not, we might be looking at an argument to an inlined function
10789 when we do not have enough information to show inlined frames;
10790 pretend it's a local variable in that case so that the user can
10791 still see it. */
10792 if (context_stack_depth > 0
10793 && context_stack[context_stack_depth - 1].name != NULL)
10794 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10795 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10796 if (attr)
10797 {
e7c27a73 10798 var_decode_location (attr, sym, cu);
c906108c 10799 }
e142c38c 10800 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10801 if (attr)
10802 {
e7c27a73 10803 dwarf2_const_value (attr, sym, cu);
c906108c 10804 }
f346a30d
PM
10805 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10806 if (attr && DW_UNSND (attr))
10807 {
10808 struct type *ref_type;
10809
10810 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10811 SYMBOL_TYPE (sym) = ref_type;
10812 }
10813
e37fd15a 10814 list_to_add = cu->list_in_scope;
c906108c
SS
10815 break;
10816 case DW_TAG_unspecified_parameters:
10817 /* From varargs functions; gdb doesn't seem to have any
10818 interest in this information, so just ignore it for now.
10819 (FIXME?) */
10820 break;
34eaf542
TT
10821 case DW_TAG_template_type_param:
10822 suppress_add = 1;
10823 /* Fall through. */
c906108c 10824 case DW_TAG_class_type:
680b30c7 10825 case DW_TAG_interface_type:
c906108c
SS
10826 case DW_TAG_structure_type:
10827 case DW_TAG_union_type:
72019c9c 10828 case DW_TAG_set_type:
c906108c
SS
10829 case DW_TAG_enumeration_type:
10830 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10831 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10832
63d06c5c 10833 {
987504bb 10834 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10835 really ever be static objects: otherwise, if you try
10836 to, say, break of a class's method and you're in a file
10837 which doesn't mention that class, it won't work unless
10838 the check for all static symbols in lookup_symbol_aux
10839 saves you. See the OtherFileClass tests in
10840 gdb.c++/namespace.exp. */
10841
e37fd15a 10842 if (!suppress_add)
34eaf542 10843 {
34eaf542
TT
10844 list_to_add = (cu->list_in_scope == &file_symbols
10845 && (cu->language == language_cplus
10846 || cu->language == language_java)
10847 ? &global_symbols : cu->list_in_scope);
63d06c5c 10848
64382290
TT
10849 /* The semantics of C++ state that "struct foo {
10850 ... }" also defines a typedef for "foo". A Java
10851 class declaration also defines a typedef for the
10852 class. */
10853 if (cu->language == language_cplus
10854 || cu->language == language_java
10855 || cu->language == language_ada)
10856 {
10857 /* The symbol's name is already allocated along
10858 with this objfile, so we don't need to
10859 duplicate it for the type. */
10860 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10861 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10862 }
63d06c5c
DC
10863 }
10864 }
c906108c
SS
10865 break;
10866 case DW_TAG_typedef:
63d06c5c
DC
10867 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10868 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10869 list_to_add = cu->list_in_scope;
63d06c5c 10870 break;
c906108c 10871 case DW_TAG_base_type:
a02abb62 10872 case DW_TAG_subrange_type:
c906108c 10873 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10874 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10875 list_to_add = cu->list_in_scope;
c906108c
SS
10876 break;
10877 case DW_TAG_enumerator:
e142c38c 10878 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10879 if (attr)
10880 {
e7c27a73 10881 dwarf2_const_value (attr, sym, cu);
c906108c 10882 }
63d06c5c
DC
10883 {
10884 /* NOTE: carlton/2003-11-10: See comment above in the
10885 DW_TAG_class_type, etc. block. */
10886
e142c38c 10887 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10888 && (cu->language == language_cplus
10889 || cu->language == language_java)
e142c38c 10890 ? &global_symbols : cu->list_in_scope);
63d06c5c 10891 }
c906108c 10892 break;
5c4e30ca
DC
10893 case DW_TAG_namespace:
10894 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10895 list_to_add = &global_symbols;
5c4e30ca 10896 break;
c906108c
SS
10897 default:
10898 /* Not a tag we recognize. Hopefully we aren't processing
10899 trash data, but since we must specifically ignore things
10900 we don't recognize, there is nothing else we should do at
10901 this point. */
e2e0b3e5 10902 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10903 dwarf_tag_name (die->tag));
c906108c
SS
10904 break;
10905 }
df8a16a1 10906
e37fd15a
SW
10907 if (suppress_add)
10908 {
10909 sym->hash_next = objfile->template_symbols;
10910 objfile->template_symbols = sym;
10911 list_to_add = NULL;
10912 }
10913
10914 if (list_to_add != NULL)
10915 add_symbol_to_list (sym, list_to_add);
10916
df8a16a1
DJ
10917 /* For the benefit of old versions of GCC, check for anonymous
10918 namespaces based on the demangled name. */
10919 if (!processing_has_namespace_info
94af9270 10920 && cu->language == language_cplus)
df8a16a1 10921 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10922 }
10923 return (sym);
10924}
10925
34eaf542
TT
10926/* A wrapper for new_symbol_full that always allocates a new symbol. */
10927
10928static struct symbol *
10929new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10930{
10931 return new_symbol_full (die, type, cu, NULL);
10932}
10933
98bfdba5
PA
10934/* Given an attr with a DW_FORM_dataN value in host byte order,
10935 zero-extend it as appropriate for the symbol's type. The DWARF
10936 standard (v4) is not entirely clear about the meaning of using
10937 DW_FORM_dataN for a constant with a signed type, where the type is
10938 wider than the data. The conclusion of a discussion on the DWARF
10939 list was that this is unspecified. We choose to always zero-extend
10940 because that is the interpretation long in use by GCC. */
c906108c 10941
98bfdba5
PA
10942static gdb_byte *
10943dwarf2_const_value_data (struct attribute *attr, struct type *type,
10944 const char *name, struct obstack *obstack,
10945 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10946{
e7c27a73 10947 struct objfile *objfile = cu->objfile;
e17a4113
UW
10948 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10949 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10950 LONGEST l = DW_UNSND (attr);
10951
10952 if (bits < sizeof (*value) * 8)
10953 {
10954 l &= ((LONGEST) 1 << bits) - 1;
10955 *value = l;
10956 }
10957 else if (bits == sizeof (*value) * 8)
10958 *value = l;
10959 else
10960 {
10961 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10962 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10963 return bytes;
10964 }
10965
10966 return NULL;
10967}
10968
10969/* Read a constant value from an attribute. Either set *VALUE, or if
10970 the value does not fit in *VALUE, set *BYTES - either already
10971 allocated on the objfile obstack, or newly allocated on OBSTACK,
10972 or, set *BATON, if we translated the constant to a location
10973 expression. */
10974
10975static void
10976dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10977 const char *name, struct obstack *obstack,
10978 struct dwarf2_cu *cu,
10979 long *value, gdb_byte **bytes,
10980 struct dwarf2_locexpr_baton **baton)
10981{
10982 struct objfile *objfile = cu->objfile;
10983 struct comp_unit_head *cu_header = &cu->header;
c906108c 10984 struct dwarf_block *blk;
98bfdba5
PA
10985 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10986 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10987
10988 *value = 0;
10989 *bytes = NULL;
10990 *baton = NULL;
c906108c
SS
10991
10992 switch (attr->form)
10993 {
10994 case DW_FORM_addr:
ac56253d 10995 {
ac56253d
TT
10996 gdb_byte *data;
10997
98bfdba5
PA
10998 if (TYPE_LENGTH (type) != cu_header->addr_size)
10999 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11000 cu_header->addr_size,
98bfdba5 11001 TYPE_LENGTH (type));
ac56253d
TT
11002 /* Symbols of this form are reasonably rare, so we just
11003 piggyback on the existing location code rather than writing
11004 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11005 *baton = obstack_alloc (&objfile->objfile_obstack,
11006 sizeof (struct dwarf2_locexpr_baton));
11007 (*baton)->per_cu = cu->per_cu;
11008 gdb_assert ((*baton)->per_cu);
ac56253d 11009
98bfdba5
PA
11010 (*baton)->size = 2 + cu_header->addr_size;
11011 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11012 (*baton)->data = data;
ac56253d
TT
11013
11014 data[0] = DW_OP_addr;
11015 store_unsigned_integer (&data[1], cu_header->addr_size,
11016 byte_order, DW_ADDR (attr));
11017 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11018 }
c906108c 11019 break;
4ac36638 11020 case DW_FORM_string:
93b5768b 11021 case DW_FORM_strp:
98bfdba5
PA
11022 /* DW_STRING is already allocated on the objfile obstack, point
11023 directly to it. */
11024 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11025 break;
c906108c
SS
11026 case DW_FORM_block1:
11027 case DW_FORM_block2:
11028 case DW_FORM_block4:
11029 case DW_FORM_block:
2dc7f7b3 11030 case DW_FORM_exprloc:
c906108c 11031 blk = DW_BLOCK (attr);
98bfdba5
PA
11032 if (TYPE_LENGTH (type) != blk->size)
11033 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11034 TYPE_LENGTH (type));
11035 *bytes = blk->data;
c906108c 11036 break;
2df3850c
JM
11037
11038 /* The DW_AT_const_value attributes are supposed to carry the
11039 symbol's value "represented as it would be on the target
11040 architecture." By the time we get here, it's already been
11041 converted to host endianness, so we just need to sign- or
11042 zero-extend it as appropriate. */
11043 case DW_FORM_data1:
98bfdba5 11044 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 11045 break;
c906108c 11046 case DW_FORM_data2:
98bfdba5 11047 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 11048 break;
c906108c 11049 case DW_FORM_data4:
98bfdba5 11050 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 11051 break;
c906108c 11052 case DW_FORM_data8:
98bfdba5 11053 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11054 break;
11055
c906108c 11056 case DW_FORM_sdata:
98bfdba5 11057 *value = DW_SND (attr);
2df3850c
JM
11058 break;
11059
c906108c 11060 case DW_FORM_udata:
98bfdba5 11061 *value = DW_UNSND (attr);
c906108c 11062 break;
2df3850c 11063
c906108c 11064 default:
4d3c2250 11065 complaint (&symfile_complaints,
e2e0b3e5 11066 _("unsupported const value attribute form: '%s'"),
4d3c2250 11067 dwarf_form_name (attr->form));
98bfdba5 11068 *value = 0;
c906108c
SS
11069 break;
11070 }
11071}
11072
2df3850c 11073
98bfdba5
PA
11074/* Copy constant value from an attribute to a symbol. */
11075
2df3850c 11076static void
98bfdba5
PA
11077dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11078 struct dwarf2_cu *cu)
2df3850c 11079{
98bfdba5
PA
11080 struct objfile *objfile = cu->objfile;
11081 struct comp_unit_head *cu_header = &cu->header;
11082 long value;
11083 gdb_byte *bytes;
11084 struct dwarf2_locexpr_baton *baton;
2df3850c 11085
98bfdba5
PA
11086 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11087 SYMBOL_PRINT_NAME (sym),
11088 &objfile->objfile_obstack, cu,
11089 &value, &bytes, &baton);
2df3850c 11090
98bfdba5
PA
11091 if (baton != NULL)
11092 {
11093 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11094 SYMBOL_LOCATION_BATON (sym) = baton;
11095 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11096 }
11097 else if (bytes != NULL)
11098 {
11099 SYMBOL_VALUE_BYTES (sym) = bytes;
11100 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11101 }
11102 else
11103 {
11104 SYMBOL_VALUE (sym) = value;
11105 SYMBOL_CLASS (sym) = LOC_CONST;
11106 }
2df3850c
JM
11107}
11108
c906108c
SS
11109/* Return the type of the die in question using its DW_AT_type attribute. */
11110
11111static struct type *
e7c27a73 11112die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11113{
c906108c 11114 struct attribute *type_attr;
c906108c 11115
e142c38c 11116 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11117 if (!type_attr)
11118 {
11119 /* A missing DW_AT_type represents a void type. */
46bf5051 11120 return objfile_type (cu->objfile)->builtin_void;
c906108c 11121 }
348e048f 11122
673bfd45 11123 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11124}
11125
b4ba55a1
JB
11126/* True iff CU's producer generates GNAT Ada auxiliary information
11127 that allows to find parallel types through that information instead
11128 of having to do expensive parallel lookups by type name. */
11129
11130static int
11131need_gnat_info (struct dwarf2_cu *cu)
11132{
11133 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11134 of GNAT produces this auxiliary information, without any indication
11135 that it is produced. Part of enhancing the FSF version of GNAT
11136 to produce that information will be to put in place an indicator
11137 that we can use in order to determine whether the descriptive type
11138 info is available or not. One suggestion that has been made is
11139 to use a new attribute, attached to the CU die. For now, assume
11140 that the descriptive type info is not available. */
11141 return 0;
11142}
11143
b4ba55a1
JB
11144/* Return the auxiliary type of the die in question using its
11145 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11146 attribute is not present. */
11147
11148static struct type *
11149die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11150{
b4ba55a1 11151 struct attribute *type_attr;
b4ba55a1
JB
11152
11153 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11154 if (!type_attr)
11155 return NULL;
11156
673bfd45 11157 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11158}
11159
11160/* If DIE has a descriptive_type attribute, then set the TYPE's
11161 descriptive type accordingly. */
11162
11163static void
11164set_descriptive_type (struct type *type, struct die_info *die,
11165 struct dwarf2_cu *cu)
11166{
11167 struct type *descriptive_type = die_descriptive_type (die, cu);
11168
11169 if (descriptive_type)
11170 {
11171 ALLOCATE_GNAT_AUX_TYPE (type);
11172 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11173 }
11174}
11175
c906108c
SS
11176/* Return the containing type of the die in question using its
11177 DW_AT_containing_type attribute. */
11178
11179static struct type *
e7c27a73 11180die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11181{
c906108c 11182 struct attribute *type_attr;
c906108c 11183
e142c38c 11184 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11185 if (!type_attr)
11186 error (_("Dwarf Error: Problem turning containing type into gdb type "
11187 "[in module %s]"), cu->objfile->name);
11188
673bfd45 11189 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11190}
11191
673bfd45
DE
11192/* Look up the type of DIE in CU using its type attribute ATTR.
11193 If there is no type substitute an error marker. */
11194
c906108c 11195static struct type *
673bfd45
DE
11196lookup_die_type (struct die_info *die, struct attribute *attr,
11197 struct dwarf2_cu *cu)
c906108c 11198{
f792889a
DJ
11199 struct type *this_type;
11200
673bfd45
DE
11201 /* First see if we have it cached. */
11202
11203 if (is_ref_attr (attr))
11204 {
11205 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11206
11207 this_type = get_die_type_at_offset (offset, cu->per_cu);
11208 }
11209 else if (attr->form == DW_FORM_sig8)
11210 {
11211 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11212 struct dwarf2_cu *sig_cu;
11213 unsigned int offset;
11214
11215 /* sig_type will be NULL if the signatured type is missing from
11216 the debug info. */
11217 if (sig_type == NULL)
11218 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11219 "at 0x%x [in module %s]"),
11220 die->offset, cu->objfile->name);
11221
11222 gdb_assert (sig_type->per_cu.from_debug_types);
11223 offset = sig_type->offset + sig_type->type_offset;
11224 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11225 }
11226 else
11227 {
11228 dump_die_for_error (die);
11229 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11230 dwarf_attr_name (attr->name), cu->objfile->name);
11231 }
11232
11233 /* If not cached we need to read it in. */
11234
11235 if (this_type == NULL)
11236 {
11237 struct die_info *type_die;
11238 struct dwarf2_cu *type_cu = cu;
11239
11240 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11241 /* If the type is cached, we should have found it above. */
11242 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11243 this_type = read_type_die_1 (type_die, type_cu);
11244 }
11245
11246 /* If we still don't have a type use an error marker. */
11247
11248 if (this_type == NULL)
c906108c 11249 {
b00fdb78
TT
11250 char *message, *saved;
11251
11252 /* read_type_die already issued a complaint. */
11253 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11254 cu->objfile->name,
11255 cu->header.offset,
11256 die->offset);
11257 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11258 message, strlen (message));
11259 xfree (message);
11260
11261 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11262 }
673bfd45 11263
f792889a 11264 return this_type;
c906108c
SS
11265}
11266
673bfd45
DE
11267/* Return the type in DIE, CU.
11268 Returns NULL for invalid types.
11269
11270 This first does a lookup in the appropriate type_hash table,
11271 and only reads the die in if necessary.
11272
11273 NOTE: This can be called when reading in partial or full symbols. */
11274
f792889a 11275static struct type *
e7c27a73 11276read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11277{
f792889a
DJ
11278 struct type *this_type;
11279
11280 this_type = get_die_type (die, cu);
11281 if (this_type)
11282 return this_type;
11283
673bfd45
DE
11284 return read_type_die_1 (die, cu);
11285}
11286
11287/* Read the type in DIE, CU.
11288 Returns NULL for invalid types. */
11289
11290static struct type *
11291read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11292{
11293 struct type *this_type = NULL;
11294
c906108c
SS
11295 switch (die->tag)
11296 {
11297 case DW_TAG_class_type:
680b30c7 11298 case DW_TAG_interface_type:
c906108c
SS
11299 case DW_TAG_structure_type:
11300 case DW_TAG_union_type:
f792889a 11301 this_type = read_structure_type (die, cu);
c906108c
SS
11302 break;
11303 case DW_TAG_enumeration_type:
f792889a 11304 this_type = read_enumeration_type (die, cu);
c906108c
SS
11305 break;
11306 case DW_TAG_subprogram:
11307 case DW_TAG_subroutine_type:
edb3359d 11308 case DW_TAG_inlined_subroutine:
f792889a 11309 this_type = read_subroutine_type (die, cu);
c906108c
SS
11310 break;
11311 case DW_TAG_array_type:
f792889a 11312 this_type = read_array_type (die, cu);
c906108c 11313 break;
72019c9c 11314 case DW_TAG_set_type:
f792889a 11315 this_type = read_set_type (die, cu);
72019c9c 11316 break;
c906108c 11317 case DW_TAG_pointer_type:
f792889a 11318 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11319 break;
11320 case DW_TAG_ptr_to_member_type:
f792889a 11321 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11322 break;
11323 case DW_TAG_reference_type:
f792889a 11324 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11325 break;
11326 case DW_TAG_const_type:
f792889a 11327 this_type = read_tag_const_type (die, cu);
c906108c
SS
11328 break;
11329 case DW_TAG_volatile_type:
f792889a 11330 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11331 break;
11332 case DW_TAG_string_type:
f792889a 11333 this_type = read_tag_string_type (die, cu);
c906108c
SS
11334 break;
11335 case DW_TAG_typedef:
f792889a 11336 this_type = read_typedef (die, cu);
c906108c 11337 break;
a02abb62 11338 case DW_TAG_subrange_type:
f792889a 11339 this_type = read_subrange_type (die, cu);
a02abb62 11340 break;
c906108c 11341 case DW_TAG_base_type:
f792889a 11342 this_type = read_base_type (die, cu);
c906108c 11343 break;
81a17f79 11344 case DW_TAG_unspecified_type:
f792889a 11345 this_type = read_unspecified_type (die, cu);
81a17f79 11346 break;
0114d602
DJ
11347 case DW_TAG_namespace:
11348 this_type = read_namespace_type (die, cu);
11349 break;
f55ee35c
JK
11350 case DW_TAG_module:
11351 this_type = read_module_type (die, cu);
11352 break;
c906108c 11353 default:
a1f5b845 11354 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11355 dwarf_tag_name (die->tag));
c906108c
SS
11356 break;
11357 }
63d06c5c 11358
f792889a 11359 return this_type;
63d06c5c
DC
11360}
11361
abc72ce4
DE
11362/* See if we can figure out if the class lives in a namespace. We do
11363 this by looking for a member function; its demangled name will
11364 contain namespace info, if there is any.
11365 Return the computed name or NULL.
11366 Space for the result is allocated on the objfile's obstack.
11367 This is the full-die version of guess_partial_die_structure_name.
11368 In this case we know DIE has no useful parent. */
11369
11370static char *
11371guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11372{
11373 struct die_info *spec_die;
11374 struct dwarf2_cu *spec_cu;
11375 struct die_info *child;
11376
11377 spec_cu = cu;
11378 spec_die = die_specification (die, &spec_cu);
11379 if (spec_die != NULL)
11380 {
11381 die = spec_die;
11382 cu = spec_cu;
11383 }
11384
11385 for (child = die->child;
11386 child != NULL;
11387 child = child->sibling)
11388 {
11389 if (child->tag == DW_TAG_subprogram)
11390 {
11391 struct attribute *attr;
11392
11393 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11394 if (attr == NULL)
11395 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11396 if (attr != NULL)
11397 {
11398 char *actual_name
11399 = language_class_name_from_physname (cu->language_defn,
11400 DW_STRING (attr));
11401 char *name = NULL;
11402
11403 if (actual_name != NULL)
11404 {
11405 char *die_name = dwarf2_name (die, cu);
11406
11407 if (die_name != NULL
11408 && strcmp (die_name, actual_name) != 0)
11409 {
11410 /* Strip off the class name from the full name.
11411 We want the prefix. */
11412 int die_name_len = strlen (die_name);
11413 int actual_name_len = strlen (actual_name);
11414
11415 /* Test for '::' as a sanity check. */
11416 if (actual_name_len > die_name_len + 2
11417 && actual_name[actual_name_len - die_name_len - 1] == ':')
11418 name =
11419 obsavestring (actual_name,
11420 actual_name_len - die_name_len - 2,
11421 &cu->objfile->objfile_obstack);
11422 }
11423 }
11424 xfree (actual_name);
11425 return name;
11426 }
11427 }
11428 }
11429
11430 return NULL;
11431}
11432
fdde2d81 11433/* Return the name of the namespace/class that DIE is defined within,
0114d602 11434 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11435
0114d602
DJ
11436 For example, if we're within the method foo() in the following
11437 code:
11438
11439 namespace N {
11440 class C {
11441 void foo () {
11442 }
11443 };
11444 }
11445
11446 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11447
11448static char *
e142c38c 11449determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11450{
0114d602
DJ
11451 struct die_info *parent, *spec_die;
11452 struct dwarf2_cu *spec_cu;
11453 struct type *parent_type;
63d06c5c 11454
f55ee35c
JK
11455 if (cu->language != language_cplus && cu->language != language_java
11456 && cu->language != language_fortran)
0114d602
DJ
11457 return "";
11458
11459 /* We have to be careful in the presence of DW_AT_specification.
11460 For example, with GCC 3.4, given the code
11461
11462 namespace N {
11463 void foo() {
11464 // Definition of N::foo.
11465 }
11466 }
11467
11468 then we'll have a tree of DIEs like this:
11469
11470 1: DW_TAG_compile_unit
11471 2: DW_TAG_namespace // N
11472 3: DW_TAG_subprogram // declaration of N::foo
11473 4: DW_TAG_subprogram // definition of N::foo
11474 DW_AT_specification // refers to die #3
11475
11476 Thus, when processing die #4, we have to pretend that we're in
11477 the context of its DW_AT_specification, namely the contex of die
11478 #3. */
11479 spec_cu = cu;
11480 spec_die = die_specification (die, &spec_cu);
11481 if (spec_die == NULL)
11482 parent = die->parent;
11483 else
63d06c5c 11484 {
0114d602
DJ
11485 parent = spec_die->parent;
11486 cu = spec_cu;
63d06c5c 11487 }
0114d602
DJ
11488
11489 if (parent == NULL)
11490 return "";
98bfdba5
PA
11491 else if (parent->building_fullname)
11492 {
11493 const char *name;
11494 const char *parent_name;
11495
11496 /* It has been seen on RealView 2.2 built binaries,
11497 DW_TAG_template_type_param types actually _defined_ as
11498 children of the parent class:
11499
11500 enum E {};
11501 template class <class Enum> Class{};
11502 Class<enum E> class_e;
11503
11504 1: DW_TAG_class_type (Class)
11505 2: DW_TAG_enumeration_type (E)
11506 3: DW_TAG_enumerator (enum1:0)
11507 3: DW_TAG_enumerator (enum2:1)
11508 ...
11509 2: DW_TAG_template_type_param
11510 DW_AT_type DW_FORM_ref_udata (E)
11511
11512 Besides being broken debug info, it can put GDB into an
11513 infinite loop. Consider:
11514
11515 When we're building the full name for Class<E>, we'll start
11516 at Class, and go look over its template type parameters,
11517 finding E. We'll then try to build the full name of E, and
11518 reach here. We're now trying to build the full name of E,
11519 and look over the parent DIE for containing scope. In the
11520 broken case, if we followed the parent DIE of E, we'd again
11521 find Class, and once again go look at its template type
11522 arguments, etc., etc. Simply don't consider such parent die
11523 as source-level parent of this die (it can't be, the language
11524 doesn't allow it), and break the loop here. */
11525 name = dwarf2_name (die, cu);
11526 parent_name = dwarf2_name (parent, cu);
11527 complaint (&symfile_complaints,
11528 _("template param type '%s' defined within parent '%s'"),
11529 name ? name : "<unknown>",
11530 parent_name ? parent_name : "<unknown>");
11531 return "";
11532 }
63d06c5c 11533 else
0114d602
DJ
11534 switch (parent->tag)
11535 {
63d06c5c 11536 case DW_TAG_namespace:
0114d602 11537 parent_type = read_type_die (parent, cu);
acebe513
UW
11538 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11539 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11540 Work around this problem here. */
11541 if (cu->language == language_cplus
11542 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11543 return "";
0114d602
DJ
11544 /* We give a name to even anonymous namespaces. */
11545 return TYPE_TAG_NAME (parent_type);
63d06c5c 11546 case DW_TAG_class_type:
680b30c7 11547 case DW_TAG_interface_type:
63d06c5c 11548 case DW_TAG_structure_type:
0114d602 11549 case DW_TAG_union_type:
f55ee35c 11550 case DW_TAG_module:
0114d602
DJ
11551 parent_type = read_type_die (parent, cu);
11552 if (TYPE_TAG_NAME (parent_type) != NULL)
11553 return TYPE_TAG_NAME (parent_type);
11554 else
11555 /* An anonymous structure is only allowed non-static data
11556 members; no typedefs, no member functions, et cetera.
11557 So it does not need a prefix. */
11558 return "";
abc72ce4
DE
11559 case DW_TAG_compile_unit:
11560 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11561 if (cu->language == language_cplus
11562 && dwarf2_per_objfile->types.asection != NULL
11563 && die->child != NULL
11564 && (die->tag == DW_TAG_class_type
11565 || die->tag == DW_TAG_structure_type
11566 || die->tag == DW_TAG_union_type))
11567 {
11568 char *name = guess_full_die_structure_name (die, cu);
11569 if (name != NULL)
11570 return name;
11571 }
11572 return "";
63d06c5c 11573 default:
8176b9b8 11574 return determine_prefix (parent, cu);
63d06c5c 11575 }
63d06c5c
DC
11576}
11577
987504bb
JJ
11578/* Return a newly-allocated string formed by concatenating PREFIX and
11579 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11580 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11581 perform an obconcat, otherwise allocate storage for the result. The CU argument
11582 is used to determine the language and hence, the appropriate separator. */
11583
f55ee35c 11584#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11585
11586static char *
f55ee35c
JK
11587typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11588 int physname, struct dwarf2_cu *cu)
63d06c5c 11589{
f55ee35c 11590 const char *lead = "";
5c315b68 11591 const char *sep;
63d06c5c 11592
987504bb
JJ
11593 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11594 sep = "";
11595 else if (cu->language == language_java)
11596 sep = ".";
f55ee35c
JK
11597 else if (cu->language == language_fortran && physname)
11598 {
11599 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11600 DW_AT_MIPS_linkage_name is preferred and used instead. */
11601
11602 lead = "__";
11603 sep = "_MOD_";
11604 }
987504bb
JJ
11605 else
11606 sep = "::";
63d06c5c 11607
6dd47d34
DE
11608 if (prefix == NULL)
11609 prefix = "";
11610 if (suffix == NULL)
11611 suffix = "";
11612
987504bb
JJ
11613 if (obs == NULL)
11614 {
11615 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11616
f55ee35c
JK
11617 strcpy (retval, lead);
11618 strcat (retval, prefix);
6dd47d34
DE
11619 strcat (retval, sep);
11620 strcat (retval, suffix);
63d06c5c
DC
11621 return retval;
11622 }
987504bb
JJ
11623 else
11624 {
11625 /* We have an obstack. */
f55ee35c 11626 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11627 }
63d06c5c
DC
11628}
11629
c906108c
SS
11630/* Return sibling of die, NULL if no sibling. */
11631
f9aca02d 11632static struct die_info *
fba45db2 11633sibling_die (struct die_info *die)
c906108c 11634{
639d11d3 11635 return die->sibling;
c906108c
SS
11636}
11637
71c25dea
TT
11638/* Get name of a die, return NULL if not found. */
11639
11640static char *
11641dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11642 struct obstack *obstack)
11643{
11644 if (name && cu->language == language_cplus)
11645 {
11646 char *canon_name = cp_canonicalize_string (name);
11647
11648 if (canon_name != NULL)
11649 {
11650 if (strcmp (canon_name, name) != 0)
11651 name = obsavestring (canon_name, strlen (canon_name),
11652 obstack);
11653 xfree (canon_name);
11654 }
11655 }
11656
11657 return name;
c906108c
SS
11658}
11659
9219021c
DC
11660/* Get name of a die, return NULL if not found. */
11661
11662static char *
e142c38c 11663dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11664{
11665 struct attribute *attr;
11666
e142c38c 11667 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11668 if (!attr || !DW_STRING (attr))
11669 return NULL;
11670
11671 switch (die->tag)
11672 {
11673 case DW_TAG_compile_unit:
11674 /* Compilation units have a DW_AT_name that is a filename, not
11675 a source language identifier. */
11676 case DW_TAG_enumeration_type:
11677 case DW_TAG_enumerator:
11678 /* These tags always have simple identifiers already; no need
11679 to canonicalize them. */
11680 return DW_STRING (attr);
907af001 11681
418835cc
KS
11682 case DW_TAG_subprogram:
11683 /* Java constructors will all be named "<init>", so return
11684 the class name when we see this special case. */
11685 if (cu->language == language_java
11686 && DW_STRING (attr) != NULL
11687 && strcmp (DW_STRING (attr), "<init>") == 0)
11688 {
11689 struct dwarf2_cu *spec_cu = cu;
11690 struct die_info *spec_die;
11691
11692 /* GCJ will output '<init>' for Java constructor names.
11693 For this special case, return the name of the parent class. */
11694
11695 /* GCJ may output suprogram DIEs with AT_specification set.
11696 If so, use the name of the specified DIE. */
11697 spec_die = die_specification (die, &spec_cu);
11698 if (spec_die != NULL)
11699 return dwarf2_name (spec_die, spec_cu);
11700
11701 do
11702 {
11703 die = die->parent;
11704 if (die->tag == DW_TAG_class_type)
11705 return dwarf2_name (die, cu);
11706 }
11707 while (die->tag != DW_TAG_compile_unit);
11708 }
907af001
UW
11709 break;
11710
11711 case DW_TAG_class_type:
11712 case DW_TAG_interface_type:
11713 case DW_TAG_structure_type:
11714 case DW_TAG_union_type:
11715 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11716 structures or unions. These were of the form "._%d" in GCC 4.1,
11717 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11718 and GCC 4.4. We work around this problem by ignoring these. */
11719 if (strncmp (DW_STRING (attr), "._", 2) == 0
11720 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11721 return NULL;
11722 break;
11723
71c25dea 11724 default:
907af001
UW
11725 break;
11726 }
11727
11728 if (!DW_STRING_IS_CANONICAL (attr))
11729 {
11730 DW_STRING (attr)
11731 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11732 &cu->objfile->objfile_obstack);
11733 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11734 }
907af001 11735 return DW_STRING (attr);
9219021c
DC
11736}
11737
11738/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11739 is none. *EXT_CU is the CU containing DIE on input, and the CU
11740 containing the return value on output. */
9219021c
DC
11741
11742static struct die_info *
f2f0e013 11743dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11744{
11745 struct attribute *attr;
9219021c 11746
f2f0e013 11747 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11748 if (attr == NULL)
11749 return NULL;
11750
f2f0e013 11751 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11752}
11753
c906108c
SS
11754/* Convert a DIE tag into its string name. */
11755
11756static char *
aa1ee363 11757dwarf_tag_name (unsigned tag)
c906108c
SS
11758{
11759 switch (tag)
11760 {
11761 case DW_TAG_padding:
11762 return "DW_TAG_padding";
11763 case DW_TAG_array_type:
11764 return "DW_TAG_array_type";
11765 case DW_TAG_class_type:
11766 return "DW_TAG_class_type";
11767 case DW_TAG_entry_point:
11768 return "DW_TAG_entry_point";
11769 case DW_TAG_enumeration_type:
11770 return "DW_TAG_enumeration_type";
11771 case DW_TAG_formal_parameter:
11772 return "DW_TAG_formal_parameter";
11773 case DW_TAG_imported_declaration:
11774 return "DW_TAG_imported_declaration";
11775 case DW_TAG_label:
11776 return "DW_TAG_label";
11777 case DW_TAG_lexical_block:
11778 return "DW_TAG_lexical_block";
11779 case DW_TAG_member:
11780 return "DW_TAG_member";
11781 case DW_TAG_pointer_type:
11782 return "DW_TAG_pointer_type";
11783 case DW_TAG_reference_type:
11784 return "DW_TAG_reference_type";
11785 case DW_TAG_compile_unit:
11786 return "DW_TAG_compile_unit";
11787 case DW_TAG_string_type:
11788 return "DW_TAG_string_type";
11789 case DW_TAG_structure_type:
11790 return "DW_TAG_structure_type";
11791 case DW_TAG_subroutine_type:
11792 return "DW_TAG_subroutine_type";
11793 case DW_TAG_typedef:
11794 return "DW_TAG_typedef";
11795 case DW_TAG_union_type:
11796 return "DW_TAG_union_type";
11797 case DW_TAG_unspecified_parameters:
11798 return "DW_TAG_unspecified_parameters";
11799 case DW_TAG_variant:
11800 return "DW_TAG_variant";
11801 case DW_TAG_common_block:
11802 return "DW_TAG_common_block";
11803 case DW_TAG_common_inclusion:
11804 return "DW_TAG_common_inclusion";
11805 case DW_TAG_inheritance:
11806 return "DW_TAG_inheritance";
11807 case DW_TAG_inlined_subroutine:
11808 return "DW_TAG_inlined_subroutine";
11809 case DW_TAG_module:
11810 return "DW_TAG_module";
11811 case DW_TAG_ptr_to_member_type:
11812 return "DW_TAG_ptr_to_member_type";
11813 case DW_TAG_set_type:
11814 return "DW_TAG_set_type";
11815 case DW_TAG_subrange_type:
11816 return "DW_TAG_subrange_type";
11817 case DW_TAG_with_stmt:
11818 return "DW_TAG_with_stmt";
11819 case DW_TAG_access_declaration:
11820 return "DW_TAG_access_declaration";
11821 case DW_TAG_base_type:
11822 return "DW_TAG_base_type";
11823 case DW_TAG_catch_block:
11824 return "DW_TAG_catch_block";
11825 case DW_TAG_const_type:
11826 return "DW_TAG_const_type";
11827 case DW_TAG_constant:
11828 return "DW_TAG_constant";
11829 case DW_TAG_enumerator:
11830 return "DW_TAG_enumerator";
11831 case DW_TAG_file_type:
11832 return "DW_TAG_file_type";
11833 case DW_TAG_friend:
11834 return "DW_TAG_friend";
11835 case DW_TAG_namelist:
11836 return "DW_TAG_namelist";
11837 case DW_TAG_namelist_item:
11838 return "DW_TAG_namelist_item";
11839 case DW_TAG_packed_type:
11840 return "DW_TAG_packed_type";
11841 case DW_TAG_subprogram:
11842 return "DW_TAG_subprogram";
11843 case DW_TAG_template_type_param:
11844 return "DW_TAG_template_type_param";
11845 case DW_TAG_template_value_param:
11846 return "DW_TAG_template_value_param";
11847 case DW_TAG_thrown_type:
11848 return "DW_TAG_thrown_type";
11849 case DW_TAG_try_block:
11850 return "DW_TAG_try_block";
11851 case DW_TAG_variant_part:
11852 return "DW_TAG_variant_part";
11853 case DW_TAG_variable:
11854 return "DW_TAG_variable";
11855 case DW_TAG_volatile_type:
11856 return "DW_TAG_volatile_type";
d9fa45fe
DC
11857 case DW_TAG_dwarf_procedure:
11858 return "DW_TAG_dwarf_procedure";
11859 case DW_TAG_restrict_type:
11860 return "DW_TAG_restrict_type";
11861 case DW_TAG_interface_type:
11862 return "DW_TAG_interface_type";
11863 case DW_TAG_namespace:
11864 return "DW_TAG_namespace";
11865 case DW_TAG_imported_module:
11866 return "DW_TAG_imported_module";
11867 case DW_TAG_unspecified_type:
11868 return "DW_TAG_unspecified_type";
11869 case DW_TAG_partial_unit:
11870 return "DW_TAG_partial_unit";
11871 case DW_TAG_imported_unit:
11872 return "DW_TAG_imported_unit";
b7619582
GF
11873 case DW_TAG_condition:
11874 return "DW_TAG_condition";
11875 case DW_TAG_shared_type:
11876 return "DW_TAG_shared_type";
348e048f
DE
11877 case DW_TAG_type_unit:
11878 return "DW_TAG_type_unit";
c906108c
SS
11879 case DW_TAG_MIPS_loop:
11880 return "DW_TAG_MIPS_loop";
b7619582
GF
11881 case DW_TAG_HP_array_descriptor:
11882 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11883 case DW_TAG_format_label:
11884 return "DW_TAG_format_label";
11885 case DW_TAG_function_template:
11886 return "DW_TAG_function_template";
11887 case DW_TAG_class_template:
11888 return "DW_TAG_class_template";
b7619582
GF
11889 case DW_TAG_GNU_BINCL:
11890 return "DW_TAG_GNU_BINCL";
11891 case DW_TAG_GNU_EINCL:
11892 return "DW_TAG_GNU_EINCL";
11893 case DW_TAG_upc_shared_type:
11894 return "DW_TAG_upc_shared_type";
11895 case DW_TAG_upc_strict_type:
11896 return "DW_TAG_upc_strict_type";
11897 case DW_TAG_upc_relaxed_type:
11898 return "DW_TAG_upc_relaxed_type";
11899 case DW_TAG_PGI_kanji_type:
11900 return "DW_TAG_PGI_kanji_type";
11901 case DW_TAG_PGI_interface_block:
11902 return "DW_TAG_PGI_interface_block";
c906108c
SS
11903 default:
11904 return "DW_TAG_<unknown>";
11905 }
11906}
11907
11908/* Convert a DWARF attribute code into its string name. */
11909
11910static char *
aa1ee363 11911dwarf_attr_name (unsigned attr)
c906108c
SS
11912{
11913 switch (attr)
11914 {
11915 case DW_AT_sibling:
11916 return "DW_AT_sibling";
11917 case DW_AT_location:
11918 return "DW_AT_location";
11919 case DW_AT_name:
11920 return "DW_AT_name";
11921 case DW_AT_ordering:
11922 return "DW_AT_ordering";
11923 case DW_AT_subscr_data:
11924 return "DW_AT_subscr_data";
11925 case DW_AT_byte_size:
11926 return "DW_AT_byte_size";
11927 case DW_AT_bit_offset:
11928 return "DW_AT_bit_offset";
11929 case DW_AT_bit_size:
11930 return "DW_AT_bit_size";
11931 case DW_AT_element_list:
11932 return "DW_AT_element_list";
11933 case DW_AT_stmt_list:
11934 return "DW_AT_stmt_list";
11935 case DW_AT_low_pc:
11936 return "DW_AT_low_pc";
11937 case DW_AT_high_pc:
11938 return "DW_AT_high_pc";
11939 case DW_AT_language:
11940 return "DW_AT_language";
11941 case DW_AT_member:
11942 return "DW_AT_member";
11943 case DW_AT_discr:
11944 return "DW_AT_discr";
11945 case DW_AT_discr_value:
11946 return "DW_AT_discr_value";
11947 case DW_AT_visibility:
11948 return "DW_AT_visibility";
11949 case DW_AT_import:
11950 return "DW_AT_import";
11951 case DW_AT_string_length:
11952 return "DW_AT_string_length";
11953 case DW_AT_common_reference:
11954 return "DW_AT_common_reference";
11955 case DW_AT_comp_dir:
11956 return "DW_AT_comp_dir";
11957 case DW_AT_const_value:
11958 return "DW_AT_const_value";
11959 case DW_AT_containing_type:
11960 return "DW_AT_containing_type";
11961 case DW_AT_default_value:
11962 return "DW_AT_default_value";
11963 case DW_AT_inline:
11964 return "DW_AT_inline";
11965 case DW_AT_is_optional:
11966 return "DW_AT_is_optional";
11967 case DW_AT_lower_bound:
11968 return "DW_AT_lower_bound";
11969 case DW_AT_producer:
11970 return "DW_AT_producer";
11971 case DW_AT_prototyped:
11972 return "DW_AT_prototyped";
11973 case DW_AT_return_addr:
11974 return "DW_AT_return_addr";
11975 case DW_AT_start_scope:
11976 return "DW_AT_start_scope";
09fa0d7c
JK
11977 case DW_AT_bit_stride:
11978 return "DW_AT_bit_stride";
c906108c
SS
11979 case DW_AT_upper_bound:
11980 return "DW_AT_upper_bound";
11981 case DW_AT_abstract_origin:
11982 return "DW_AT_abstract_origin";
11983 case DW_AT_accessibility:
11984 return "DW_AT_accessibility";
11985 case DW_AT_address_class:
11986 return "DW_AT_address_class";
11987 case DW_AT_artificial:
11988 return "DW_AT_artificial";
11989 case DW_AT_base_types:
11990 return "DW_AT_base_types";
11991 case DW_AT_calling_convention:
11992 return "DW_AT_calling_convention";
11993 case DW_AT_count:
11994 return "DW_AT_count";
11995 case DW_AT_data_member_location:
11996 return "DW_AT_data_member_location";
11997 case DW_AT_decl_column:
11998 return "DW_AT_decl_column";
11999 case DW_AT_decl_file:
12000 return "DW_AT_decl_file";
12001 case DW_AT_decl_line:
12002 return "DW_AT_decl_line";
12003 case DW_AT_declaration:
12004 return "DW_AT_declaration";
12005 case DW_AT_discr_list:
12006 return "DW_AT_discr_list";
12007 case DW_AT_encoding:
12008 return "DW_AT_encoding";
12009 case DW_AT_external:
12010 return "DW_AT_external";
12011 case DW_AT_frame_base:
12012 return "DW_AT_frame_base";
12013 case DW_AT_friend:
12014 return "DW_AT_friend";
12015 case DW_AT_identifier_case:
12016 return "DW_AT_identifier_case";
12017 case DW_AT_macro_info:
12018 return "DW_AT_macro_info";
12019 case DW_AT_namelist_items:
12020 return "DW_AT_namelist_items";
12021 case DW_AT_priority:
12022 return "DW_AT_priority";
12023 case DW_AT_segment:
12024 return "DW_AT_segment";
12025 case DW_AT_specification:
12026 return "DW_AT_specification";
12027 case DW_AT_static_link:
12028 return "DW_AT_static_link";
12029 case DW_AT_type:
12030 return "DW_AT_type";
12031 case DW_AT_use_location:
12032 return "DW_AT_use_location";
12033 case DW_AT_variable_parameter:
12034 return "DW_AT_variable_parameter";
12035 case DW_AT_virtuality:
12036 return "DW_AT_virtuality";
12037 case DW_AT_vtable_elem_location:
12038 return "DW_AT_vtable_elem_location";
b7619582 12039 /* DWARF 3 values. */
d9fa45fe
DC
12040 case DW_AT_allocated:
12041 return "DW_AT_allocated";
12042 case DW_AT_associated:
12043 return "DW_AT_associated";
12044 case DW_AT_data_location:
12045 return "DW_AT_data_location";
09fa0d7c
JK
12046 case DW_AT_byte_stride:
12047 return "DW_AT_byte_stride";
d9fa45fe
DC
12048 case DW_AT_entry_pc:
12049 return "DW_AT_entry_pc";
12050 case DW_AT_use_UTF8:
12051 return "DW_AT_use_UTF8";
12052 case DW_AT_extension:
12053 return "DW_AT_extension";
12054 case DW_AT_ranges:
12055 return "DW_AT_ranges";
12056 case DW_AT_trampoline:
12057 return "DW_AT_trampoline";
12058 case DW_AT_call_column:
12059 return "DW_AT_call_column";
12060 case DW_AT_call_file:
12061 return "DW_AT_call_file";
12062 case DW_AT_call_line:
12063 return "DW_AT_call_line";
b7619582
GF
12064 case DW_AT_description:
12065 return "DW_AT_description";
12066 case DW_AT_binary_scale:
12067 return "DW_AT_binary_scale";
12068 case DW_AT_decimal_scale:
12069 return "DW_AT_decimal_scale";
12070 case DW_AT_small:
12071 return "DW_AT_small";
12072 case DW_AT_decimal_sign:
12073 return "DW_AT_decimal_sign";
12074 case DW_AT_digit_count:
12075 return "DW_AT_digit_count";
12076 case DW_AT_picture_string:
12077 return "DW_AT_picture_string";
12078 case DW_AT_mutable:
12079 return "DW_AT_mutable";
12080 case DW_AT_threads_scaled:
12081 return "DW_AT_threads_scaled";
12082 case DW_AT_explicit:
12083 return "DW_AT_explicit";
12084 case DW_AT_object_pointer:
12085 return "DW_AT_object_pointer";
12086 case DW_AT_endianity:
12087 return "DW_AT_endianity";
12088 case DW_AT_elemental:
12089 return "DW_AT_elemental";
12090 case DW_AT_pure:
12091 return "DW_AT_pure";
12092 case DW_AT_recursive:
12093 return "DW_AT_recursive";
348e048f
DE
12094 /* DWARF 4 values. */
12095 case DW_AT_signature:
12096 return "DW_AT_signature";
31ef98ae
TT
12097 case DW_AT_linkage_name:
12098 return "DW_AT_linkage_name";
b7619582 12099 /* SGI/MIPS extensions. */
c764a876 12100#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12101 case DW_AT_MIPS_fde:
12102 return "DW_AT_MIPS_fde";
c764a876 12103#endif
c906108c
SS
12104 case DW_AT_MIPS_loop_begin:
12105 return "DW_AT_MIPS_loop_begin";
12106 case DW_AT_MIPS_tail_loop_begin:
12107 return "DW_AT_MIPS_tail_loop_begin";
12108 case DW_AT_MIPS_epilog_begin:
12109 return "DW_AT_MIPS_epilog_begin";
12110 case DW_AT_MIPS_loop_unroll_factor:
12111 return "DW_AT_MIPS_loop_unroll_factor";
12112 case DW_AT_MIPS_software_pipeline_depth:
12113 return "DW_AT_MIPS_software_pipeline_depth";
12114 case DW_AT_MIPS_linkage_name:
12115 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12116 case DW_AT_MIPS_stride:
12117 return "DW_AT_MIPS_stride";
12118 case DW_AT_MIPS_abstract_name:
12119 return "DW_AT_MIPS_abstract_name";
12120 case DW_AT_MIPS_clone_origin:
12121 return "DW_AT_MIPS_clone_origin";
12122 case DW_AT_MIPS_has_inlines:
12123 return "DW_AT_MIPS_has_inlines";
b7619582 12124 /* HP extensions. */
c764a876 12125#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12126 case DW_AT_HP_block_index:
12127 return "DW_AT_HP_block_index";
c764a876 12128#endif
b7619582
GF
12129 case DW_AT_HP_unmodifiable:
12130 return "DW_AT_HP_unmodifiable";
12131 case DW_AT_HP_actuals_stmt_list:
12132 return "DW_AT_HP_actuals_stmt_list";
12133 case DW_AT_HP_proc_per_section:
12134 return "DW_AT_HP_proc_per_section";
12135 case DW_AT_HP_raw_data_ptr:
12136 return "DW_AT_HP_raw_data_ptr";
12137 case DW_AT_HP_pass_by_reference:
12138 return "DW_AT_HP_pass_by_reference";
12139 case DW_AT_HP_opt_level:
12140 return "DW_AT_HP_opt_level";
12141 case DW_AT_HP_prof_version_id:
12142 return "DW_AT_HP_prof_version_id";
12143 case DW_AT_HP_opt_flags:
12144 return "DW_AT_HP_opt_flags";
12145 case DW_AT_HP_cold_region_low_pc:
12146 return "DW_AT_HP_cold_region_low_pc";
12147 case DW_AT_HP_cold_region_high_pc:
12148 return "DW_AT_HP_cold_region_high_pc";
12149 case DW_AT_HP_all_variables_modifiable:
12150 return "DW_AT_HP_all_variables_modifiable";
12151 case DW_AT_HP_linkage_name:
12152 return "DW_AT_HP_linkage_name";
12153 case DW_AT_HP_prof_flags:
12154 return "DW_AT_HP_prof_flags";
12155 /* GNU extensions. */
c906108c
SS
12156 case DW_AT_sf_names:
12157 return "DW_AT_sf_names";
12158 case DW_AT_src_info:
12159 return "DW_AT_src_info";
12160 case DW_AT_mac_info:
12161 return "DW_AT_mac_info";
12162 case DW_AT_src_coords:
12163 return "DW_AT_src_coords";
12164 case DW_AT_body_begin:
12165 return "DW_AT_body_begin";
12166 case DW_AT_body_end:
12167 return "DW_AT_body_end";
f5f8a009
EZ
12168 case DW_AT_GNU_vector:
12169 return "DW_AT_GNU_vector";
2de00c64
DE
12170 case DW_AT_GNU_odr_signature:
12171 return "DW_AT_GNU_odr_signature";
b7619582
GF
12172 /* VMS extensions. */
12173 case DW_AT_VMS_rtnbeg_pd_address:
12174 return "DW_AT_VMS_rtnbeg_pd_address";
12175 /* UPC extension. */
12176 case DW_AT_upc_threads_scaled:
12177 return "DW_AT_upc_threads_scaled";
12178 /* PGI (STMicroelectronics) extensions. */
12179 case DW_AT_PGI_lbase:
12180 return "DW_AT_PGI_lbase";
12181 case DW_AT_PGI_soffset:
12182 return "DW_AT_PGI_soffset";
12183 case DW_AT_PGI_lstride:
12184 return "DW_AT_PGI_lstride";
c906108c
SS
12185 default:
12186 return "DW_AT_<unknown>";
12187 }
12188}
12189
12190/* Convert a DWARF value form code into its string name. */
12191
12192static char *
aa1ee363 12193dwarf_form_name (unsigned form)
c906108c
SS
12194{
12195 switch (form)
12196 {
12197 case DW_FORM_addr:
12198 return "DW_FORM_addr";
12199 case DW_FORM_block2:
12200 return "DW_FORM_block2";
12201 case DW_FORM_block4:
12202 return "DW_FORM_block4";
12203 case DW_FORM_data2:
12204 return "DW_FORM_data2";
12205 case DW_FORM_data4:
12206 return "DW_FORM_data4";
12207 case DW_FORM_data8:
12208 return "DW_FORM_data8";
12209 case DW_FORM_string:
12210 return "DW_FORM_string";
12211 case DW_FORM_block:
12212 return "DW_FORM_block";
12213 case DW_FORM_block1:
12214 return "DW_FORM_block1";
12215 case DW_FORM_data1:
12216 return "DW_FORM_data1";
12217 case DW_FORM_flag:
12218 return "DW_FORM_flag";
12219 case DW_FORM_sdata:
12220 return "DW_FORM_sdata";
12221 case DW_FORM_strp:
12222 return "DW_FORM_strp";
12223 case DW_FORM_udata:
12224 return "DW_FORM_udata";
12225 case DW_FORM_ref_addr:
12226 return "DW_FORM_ref_addr";
12227 case DW_FORM_ref1:
12228 return "DW_FORM_ref1";
12229 case DW_FORM_ref2:
12230 return "DW_FORM_ref2";
12231 case DW_FORM_ref4:
12232 return "DW_FORM_ref4";
12233 case DW_FORM_ref8:
12234 return "DW_FORM_ref8";
12235 case DW_FORM_ref_udata:
12236 return "DW_FORM_ref_udata";
12237 case DW_FORM_indirect:
12238 return "DW_FORM_indirect";
348e048f
DE
12239 case DW_FORM_sec_offset:
12240 return "DW_FORM_sec_offset";
12241 case DW_FORM_exprloc:
12242 return "DW_FORM_exprloc";
12243 case DW_FORM_flag_present:
12244 return "DW_FORM_flag_present";
12245 case DW_FORM_sig8:
12246 return "DW_FORM_sig8";
c906108c
SS
12247 default:
12248 return "DW_FORM_<unknown>";
12249 }
12250}
12251
12252/* Convert a DWARF stack opcode into its string name. */
12253
9eae7c52
TT
12254const char *
12255dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12256{
12257 switch (op)
12258 {
12259 case DW_OP_addr:
12260 return "DW_OP_addr";
12261 case DW_OP_deref:
12262 return "DW_OP_deref";
12263 case DW_OP_const1u:
12264 return "DW_OP_const1u";
12265 case DW_OP_const1s:
12266 return "DW_OP_const1s";
12267 case DW_OP_const2u:
12268 return "DW_OP_const2u";
12269 case DW_OP_const2s:
12270 return "DW_OP_const2s";
12271 case DW_OP_const4u:
12272 return "DW_OP_const4u";
12273 case DW_OP_const4s:
12274 return "DW_OP_const4s";
12275 case DW_OP_const8u:
12276 return "DW_OP_const8u";
12277 case DW_OP_const8s:
12278 return "DW_OP_const8s";
12279 case DW_OP_constu:
12280 return "DW_OP_constu";
12281 case DW_OP_consts:
12282 return "DW_OP_consts";
12283 case DW_OP_dup:
12284 return "DW_OP_dup";
12285 case DW_OP_drop:
12286 return "DW_OP_drop";
12287 case DW_OP_over:
12288 return "DW_OP_over";
12289 case DW_OP_pick:
12290 return "DW_OP_pick";
12291 case DW_OP_swap:
12292 return "DW_OP_swap";
12293 case DW_OP_rot:
12294 return "DW_OP_rot";
12295 case DW_OP_xderef:
12296 return "DW_OP_xderef";
12297 case DW_OP_abs:
12298 return "DW_OP_abs";
12299 case DW_OP_and:
12300 return "DW_OP_and";
12301 case DW_OP_div:
12302 return "DW_OP_div";
12303 case DW_OP_minus:
12304 return "DW_OP_minus";
12305 case DW_OP_mod:
12306 return "DW_OP_mod";
12307 case DW_OP_mul:
12308 return "DW_OP_mul";
12309 case DW_OP_neg:
12310 return "DW_OP_neg";
12311 case DW_OP_not:
12312 return "DW_OP_not";
12313 case DW_OP_or:
12314 return "DW_OP_or";
12315 case DW_OP_plus:
12316 return "DW_OP_plus";
12317 case DW_OP_plus_uconst:
12318 return "DW_OP_plus_uconst";
12319 case DW_OP_shl:
12320 return "DW_OP_shl";
12321 case DW_OP_shr:
12322 return "DW_OP_shr";
12323 case DW_OP_shra:
12324 return "DW_OP_shra";
12325 case DW_OP_xor:
12326 return "DW_OP_xor";
12327 case DW_OP_bra:
12328 return "DW_OP_bra";
12329 case DW_OP_eq:
12330 return "DW_OP_eq";
12331 case DW_OP_ge:
12332 return "DW_OP_ge";
12333 case DW_OP_gt:
12334 return "DW_OP_gt";
12335 case DW_OP_le:
12336 return "DW_OP_le";
12337 case DW_OP_lt:
12338 return "DW_OP_lt";
12339 case DW_OP_ne:
12340 return "DW_OP_ne";
12341 case DW_OP_skip:
12342 return "DW_OP_skip";
12343 case DW_OP_lit0:
12344 return "DW_OP_lit0";
12345 case DW_OP_lit1:
12346 return "DW_OP_lit1";
12347 case DW_OP_lit2:
12348 return "DW_OP_lit2";
12349 case DW_OP_lit3:
12350 return "DW_OP_lit3";
12351 case DW_OP_lit4:
12352 return "DW_OP_lit4";
12353 case DW_OP_lit5:
12354 return "DW_OP_lit5";
12355 case DW_OP_lit6:
12356 return "DW_OP_lit6";
12357 case DW_OP_lit7:
12358 return "DW_OP_lit7";
12359 case DW_OP_lit8:
12360 return "DW_OP_lit8";
12361 case DW_OP_lit9:
12362 return "DW_OP_lit9";
12363 case DW_OP_lit10:
12364 return "DW_OP_lit10";
12365 case DW_OP_lit11:
12366 return "DW_OP_lit11";
12367 case DW_OP_lit12:
12368 return "DW_OP_lit12";
12369 case DW_OP_lit13:
12370 return "DW_OP_lit13";
12371 case DW_OP_lit14:
12372 return "DW_OP_lit14";
12373 case DW_OP_lit15:
12374 return "DW_OP_lit15";
12375 case DW_OP_lit16:
12376 return "DW_OP_lit16";
12377 case DW_OP_lit17:
12378 return "DW_OP_lit17";
12379 case DW_OP_lit18:
12380 return "DW_OP_lit18";
12381 case DW_OP_lit19:
12382 return "DW_OP_lit19";
12383 case DW_OP_lit20:
12384 return "DW_OP_lit20";
12385 case DW_OP_lit21:
12386 return "DW_OP_lit21";
12387 case DW_OP_lit22:
12388 return "DW_OP_lit22";
12389 case DW_OP_lit23:
12390 return "DW_OP_lit23";
12391 case DW_OP_lit24:
12392 return "DW_OP_lit24";
12393 case DW_OP_lit25:
12394 return "DW_OP_lit25";
12395 case DW_OP_lit26:
12396 return "DW_OP_lit26";
12397 case DW_OP_lit27:
12398 return "DW_OP_lit27";
12399 case DW_OP_lit28:
12400 return "DW_OP_lit28";
12401 case DW_OP_lit29:
12402 return "DW_OP_lit29";
12403 case DW_OP_lit30:
12404 return "DW_OP_lit30";
12405 case DW_OP_lit31:
12406 return "DW_OP_lit31";
12407 case DW_OP_reg0:
12408 return "DW_OP_reg0";
12409 case DW_OP_reg1:
12410 return "DW_OP_reg1";
12411 case DW_OP_reg2:
12412 return "DW_OP_reg2";
12413 case DW_OP_reg3:
12414 return "DW_OP_reg3";
12415 case DW_OP_reg4:
12416 return "DW_OP_reg4";
12417 case DW_OP_reg5:
12418 return "DW_OP_reg5";
12419 case DW_OP_reg6:
12420 return "DW_OP_reg6";
12421 case DW_OP_reg7:
12422 return "DW_OP_reg7";
12423 case DW_OP_reg8:
12424 return "DW_OP_reg8";
12425 case DW_OP_reg9:
12426 return "DW_OP_reg9";
12427 case DW_OP_reg10:
12428 return "DW_OP_reg10";
12429 case DW_OP_reg11:
12430 return "DW_OP_reg11";
12431 case DW_OP_reg12:
12432 return "DW_OP_reg12";
12433 case DW_OP_reg13:
12434 return "DW_OP_reg13";
12435 case DW_OP_reg14:
12436 return "DW_OP_reg14";
12437 case DW_OP_reg15:
12438 return "DW_OP_reg15";
12439 case DW_OP_reg16:
12440 return "DW_OP_reg16";
12441 case DW_OP_reg17:
12442 return "DW_OP_reg17";
12443 case DW_OP_reg18:
12444 return "DW_OP_reg18";
12445 case DW_OP_reg19:
12446 return "DW_OP_reg19";
12447 case DW_OP_reg20:
12448 return "DW_OP_reg20";
12449 case DW_OP_reg21:
12450 return "DW_OP_reg21";
12451 case DW_OP_reg22:
12452 return "DW_OP_reg22";
12453 case DW_OP_reg23:
12454 return "DW_OP_reg23";
12455 case DW_OP_reg24:
12456 return "DW_OP_reg24";
12457 case DW_OP_reg25:
12458 return "DW_OP_reg25";
12459 case DW_OP_reg26:
12460 return "DW_OP_reg26";
12461 case DW_OP_reg27:
12462 return "DW_OP_reg27";
12463 case DW_OP_reg28:
12464 return "DW_OP_reg28";
12465 case DW_OP_reg29:
12466 return "DW_OP_reg29";
12467 case DW_OP_reg30:
12468 return "DW_OP_reg30";
12469 case DW_OP_reg31:
12470 return "DW_OP_reg31";
12471 case DW_OP_breg0:
12472 return "DW_OP_breg0";
12473 case DW_OP_breg1:
12474 return "DW_OP_breg1";
12475 case DW_OP_breg2:
12476 return "DW_OP_breg2";
12477 case DW_OP_breg3:
12478 return "DW_OP_breg3";
12479 case DW_OP_breg4:
12480 return "DW_OP_breg4";
12481 case DW_OP_breg5:
12482 return "DW_OP_breg5";
12483 case DW_OP_breg6:
12484 return "DW_OP_breg6";
12485 case DW_OP_breg7:
12486 return "DW_OP_breg7";
12487 case DW_OP_breg8:
12488 return "DW_OP_breg8";
12489 case DW_OP_breg9:
12490 return "DW_OP_breg9";
12491 case DW_OP_breg10:
12492 return "DW_OP_breg10";
12493 case DW_OP_breg11:
12494 return "DW_OP_breg11";
12495 case DW_OP_breg12:
12496 return "DW_OP_breg12";
12497 case DW_OP_breg13:
12498 return "DW_OP_breg13";
12499 case DW_OP_breg14:
12500 return "DW_OP_breg14";
12501 case DW_OP_breg15:
12502 return "DW_OP_breg15";
12503 case DW_OP_breg16:
12504 return "DW_OP_breg16";
12505 case DW_OP_breg17:
12506 return "DW_OP_breg17";
12507 case DW_OP_breg18:
12508 return "DW_OP_breg18";
12509 case DW_OP_breg19:
12510 return "DW_OP_breg19";
12511 case DW_OP_breg20:
12512 return "DW_OP_breg20";
12513 case DW_OP_breg21:
12514 return "DW_OP_breg21";
12515 case DW_OP_breg22:
12516 return "DW_OP_breg22";
12517 case DW_OP_breg23:
12518 return "DW_OP_breg23";
12519 case DW_OP_breg24:
12520 return "DW_OP_breg24";
12521 case DW_OP_breg25:
12522 return "DW_OP_breg25";
12523 case DW_OP_breg26:
12524 return "DW_OP_breg26";
12525 case DW_OP_breg27:
12526 return "DW_OP_breg27";
12527 case DW_OP_breg28:
12528 return "DW_OP_breg28";
12529 case DW_OP_breg29:
12530 return "DW_OP_breg29";
12531 case DW_OP_breg30:
12532 return "DW_OP_breg30";
12533 case DW_OP_breg31:
12534 return "DW_OP_breg31";
12535 case DW_OP_regx:
12536 return "DW_OP_regx";
12537 case DW_OP_fbreg:
12538 return "DW_OP_fbreg";
12539 case DW_OP_bregx:
12540 return "DW_OP_bregx";
12541 case DW_OP_piece:
12542 return "DW_OP_piece";
12543 case DW_OP_deref_size:
12544 return "DW_OP_deref_size";
12545 case DW_OP_xderef_size:
12546 return "DW_OP_xderef_size";
12547 case DW_OP_nop:
12548 return "DW_OP_nop";
b7619582 12549 /* DWARF 3 extensions. */
ed348acc
EZ
12550 case DW_OP_push_object_address:
12551 return "DW_OP_push_object_address";
12552 case DW_OP_call2:
12553 return "DW_OP_call2";
12554 case DW_OP_call4:
12555 return "DW_OP_call4";
12556 case DW_OP_call_ref:
12557 return "DW_OP_call_ref";
b7619582
GF
12558 case DW_OP_form_tls_address:
12559 return "DW_OP_form_tls_address";
12560 case DW_OP_call_frame_cfa:
12561 return "DW_OP_call_frame_cfa";
12562 case DW_OP_bit_piece:
12563 return "DW_OP_bit_piece";
9eae7c52
TT
12564 /* DWARF 4 extensions. */
12565 case DW_OP_implicit_value:
12566 return "DW_OP_implicit_value";
12567 case DW_OP_stack_value:
12568 return "DW_OP_stack_value";
12569 /* GNU extensions. */
ed348acc
EZ
12570 case DW_OP_GNU_push_tls_address:
12571 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12572 case DW_OP_GNU_uninit:
12573 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12574 case DW_OP_GNU_implicit_pointer:
12575 return "DW_OP_GNU_implicit_pointer";
c906108c 12576 default:
9eae7c52 12577 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12578 }
12579}
12580
12581static char *
fba45db2 12582dwarf_bool_name (unsigned mybool)
c906108c
SS
12583{
12584 if (mybool)
12585 return "TRUE";
12586 else
12587 return "FALSE";
12588}
12589
12590/* Convert a DWARF type code into its string name. */
12591
12592static char *
aa1ee363 12593dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12594{
12595 switch (enc)
12596 {
b7619582
GF
12597 case DW_ATE_void:
12598 return "DW_ATE_void";
c906108c
SS
12599 case DW_ATE_address:
12600 return "DW_ATE_address";
12601 case DW_ATE_boolean:
12602 return "DW_ATE_boolean";
12603 case DW_ATE_complex_float:
12604 return "DW_ATE_complex_float";
12605 case DW_ATE_float:
12606 return "DW_ATE_float";
12607 case DW_ATE_signed:
12608 return "DW_ATE_signed";
12609 case DW_ATE_signed_char:
12610 return "DW_ATE_signed_char";
12611 case DW_ATE_unsigned:
12612 return "DW_ATE_unsigned";
12613 case DW_ATE_unsigned_char:
12614 return "DW_ATE_unsigned_char";
b7619582 12615 /* DWARF 3. */
d9fa45fe
DC
12616 case DW_ATE_imaginary_float:
12617 return "DW_ATE_imaginary_float";
b7619582
GF
12618 case DW_ATE_packed_decimal:
12619 return "DW_ATE_packed_decimal";
12620 case DW_ATE_numeric_string:
12621 return "DW_ATE_numeric_string";
12622 case DW_ATE_edited:
12623 return "DW_ATE_edited";
12624 case DW_ATE_signed_fixed:
12625 return "DW_ATE_signed_fixed";
12626 case DW_ATE_unsigned_fixed:
12627 return "DW_ATE_unsigned_fixed";
12628 case DW_ATE_decimal_float:
12629 return "DW_ATE_decimal_float";
75079b2b
TT
12630 /* DWARF 4. */
12631 case DW_ATE_UTF:
12632 return "DW_ATE_UTF";
b7619582
GF
12633 /* HP extensions. */
12634 case DW_ATE_HP_float80:
12635 return "DW_ATE_HP_float80";
12636 case DW_ATE_HP_complex_float80:
12637 return "DW_ATE_HP_complex_float80";
12638 case DW_ATE_HP_float128:
12639 return "DW_ATE_HP_float128";
12640 case DW_ATE_HP_complex_float128:
12641 return "DW_ATE_HP_complex_float128";
12642 case DW_ATE_HP_floathpintel:
12643 return "DW_ATE_HP_floathpintel";
12644 case DW_ATE_HP_imaginary_float80:
12645 return "DW_ATE_HP_imaginary_float80";
12646 case DW_ATE_HP_imaginary_float128:
12647 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12648 default:
12649 return "DW_ATE_<unknown>";
12650 }
12651}
12652
12653/* Convert a DWARF call frame info operation to its string name. */
12654
12655#if 0
12656static char *
aa1ee363 12657dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12658{
12659 switch (cfi_opc)
12660 {
12661 case DW_CFA_advance_loc:
12662 return "DW_CFA_advance_loc";
12663 case DW_CFA_offset:
12664 return "DW_CFA_offset";
12665 case DW_CFA_restore:
12666 return "DW_CFA_restore";
12667 case DW_CFA_nop:
12668 return "DW_CFA_nop";
12669 case DW_CFA_set_loc:
12670 return "DW_CFA_set_loc";
12671 case DW_CFA_advance_loc1:
12672 return "DW_CFA_advance_loc1";
12673 case DW_CFA_advance_loc2:
12674 return "DW_CFA_advance_loc2";
12675 case DW_CFA_advance_loc4:
12676 return "DW_CFA_advance_loc4";
12677 case DW_CFA_offset_extended:
12678 return "DW_CFA_offset_extended";
12679 case DW_CFA_restore_extended:
12680 return "DW_CFA_restore_extended";
12681 case DW_CFA_undefined:
12682 return "DW_CFA_undefined";
12683 case DW_CFA_same_value:
12684 return "DW_CFA_same_value";
12685 case DW_CFA_register:
12686 return "DW_CFA_register";
12687 case DW_CFA_remember_state:
12688 return "DW_CFA_remember_state";
12689 case DW_CFA_restore_state:
12690 return "DW_CFA_restore_state";
12691 case DW_CFA_def_cfa:
12692 return "DW_CFA_def_cfa";
12693 case DW_CFA_def_cfa_register:
12694 return "DW_CFA_def_cfa_register";
12695 case DW_CFA_def_cfa_offset:
12696 return "DW_CFA_def_cfa_offset";
b7619582 12697 /* DWARF 3. */
985cb1a3
JM
12698 case DW_CFA_def_cfa_expression:
12699 return "DW_CFA_def_cfa_expression";
12700 case DW_CFA_expression:
12701 return "DW_CFA_expression";
12702 case DW_CFA_offset_extended_sf:
12703 return "DW_CFA_offset_extended_sf";
12704 case DW_CFA_def_cfa_sf:
12705 return "DW_CFA_def_cfa_sf";
12706 case DW_CFA_def_cfa_offset_sf:
12707 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12708 case DW_CFA_val_offset:
12709 return "DW_CFA_val_offset";
12710 case DW_CFA_val_offset_sf:
12711 return "DW_CFA_val_offset_sf";
12712 case DW_CFA_val_expression:
12713 return "DW_CFA_val_expression";
12714 /* SGI/MIPS specific. */
c906108c
SS
12715 case DW_CFA_MIPS_advance_loc8:
12716 return "DW_CFA_MIPS_advance_loc8";
b7619582 12717 /* GNU extensions. */
985cb1a3
JM
12718 case DW_CFA_GNU_window_save:
12719 return "DW_CFA_GNU_window_save";
12720 case DW_CFA_GNU_args_size:
12721 return "DW_CFA_GNU_args_size";
12722 case DW_CFA_GNU_negative_offset_extended:
12723 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12724 default:
12725 return "DW_CFA_<unknown>";
12726 }
12727}
12728#endif
12729
f9aca02d 12730static void
d97bc12b 12731dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12732{
12733 unsigned int i;
12734
d97bc12b
DE
12735 print_spaces (indent, f);
12736 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12737 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12738
12739 if (die->parent != NULL)
12740 {
12741 print_spaces (indent, f);
12742 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12743 die->parent->offset);
12744 }
12745
12746 print_spaces (indent, f);
12747 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12748 dwarf_bool_name (die->child != NULL));
c906108c 12749
d97bc12b
DE
12750 print_spaces (indent, f);
12751 fprintf_unfiltered (f, " attributes:\n");
12752
c906108c
SS
12753 for (i = 0; i < die->num_attrs; ++i)
12754 {
d97bc12b
DE
12755 print_spaces (indent, f);
12756 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12757 dwarf_attr_name (die->attrs[i].name),
12758 dwarf_form_name (die->attrs[i].form));
d97bc12b 12759
c906108c
SS
12760 switch (die->attrs[i].form)
12761 {
12762 case DW_FORM_ref_addr:
12763 case DW_FORM_addr:
d97bc12b 12764 fprintf_unfiltered (f, "address: ");
5af949e3 12765 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12766 break;
12767 case DW_FORM_block2:
12768 case DW_FORM_block4:
12769 case DW_FORM_block:
12770 case DW_FORM_block1:
d97bc12b 12771 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12772 break;
2dc7f7b3
TT
12773 case DW_FORM_exprloc:
12774 fprintf_unfiltered (f, "expression: size %u",
12775 DW_BLOCK (&die->attrs[i])->size);
12776 break;
10b3939b
DJ
12777 case DW_FORM_ref1:
12778 case DW_FORM_ref2:
12779 case DW_FORM_ref4:
d97bc12b 12780 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12781 (long) (DW_ADDR (&die->attrs[i])));
12782 break;
c906108c
SS
12783 case DW_FORM_data1:
12784 case DW_FORM_data2:
12785 case DW_FORM_data4:
ce5d95e1 12786 case DW_FORM_data8:
c906108c
SS
12787 case DW_FORM_udata:
12788 case DW_FORM_sdata:
43bbcdc2
PH
12789 fprintf_unfiltered (f, "constant: %s",
12790 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12791 break;
2dc7f7b3
TT
12792 case DW_FORM_sec_offset:
12793 fprintf_unfiltered (f, "section offset: %s",
12794 pulongest (DW_UNSND (&die->attrs[i])));
12795 break;
348e048f
DE
12796 case DW_FORM_sig8:
12797 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12798 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12799 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12800 else
12801 fprintf_unfiltered (f, "signatured type, offset: unknown");
12802 break;
c906108c 12803 case DW_FORM_string:
4bdf3d34 12804 case DW_FORM_strp:
8285870a 12805 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12806 DW_STRING (&die->attrs[i])
8285870a
JK
12807 ? DW_STRING (&die->attrs[i]) : "",
12808 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12809 break;
12810 case DW_FORM_flag:
12811 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12812 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12813 else
d97bc12b 12814 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12815 break;
2dc7f7b3
TT
12816 case DW_FORM_flag_present:
12817 fprintf_unfiltered (f, "flag: TRUE");
12818 break;
a8329558
KW
12819 case DW_FORM_indirect:
12820 /* the reader will have reduced the indirect form to
12821 the "base form" so this form should not occur */
d97bc12b 12822 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12823 break;
c906108c 12824 default:
d97bc12b 12825 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12826 die->attrs[i].form);
d97bc12b 12827 break;
c906108c 12828 }
d97bc12b 12829 fprintf_unfiltered (f, "\n");
c906108c
SS
12830 }
12831}
12832
f9aca02d 12833static void
d97bc12b 12834dump_die_for_error (struct die_info *die)
c906108c 12835{
d97bc12b
DE
12836 dump_die_shallow (gdb_stderr, 0, die);
12837}
12838
12839static void
12840dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12841{
12842 int indent = level * 4;
12843
12844 gdb_assert (die != NULL);
12845
12846 if (level >= max_level)
12847 return;
12848
12849 dump_die_shallow (f, indent, die);
12850
12851 if (die->child != NULL)
c906108c 12852 {
d97bc12b
DE
12853 print_spaces (indent, f);
12854 fprintf_unfiltered (f, " Children:");
12855 if (level + 1 < max_level)
12856 {
12857 fprintf_unfiltered (f, "\n");
12858 dump_die_1 (f, level + 1, max_level, die->child);
12859 }
12860 else
12861 {
12862 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12863 }
12864 }
12865
12866 if (die->sibling != NULL && level > 0)
12867 {
12868 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12869 }
12870}
12871
d97bc12b
DE
12872/* This is called from the pdie macro in gdbinit.in.
12873 It's not static so gcc will keep a copy callable from gdb. */
12874
12875void
12876dump_die (struct die_info *die, int max_level)
12877{
12878 dump_die_1 (gdb_stdlog, 0, max_level, die);
12879}
12880
f9aca02d 12881static void
51545339 12882store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12883{
51545339 12884 void **slot;
c906108c 12885
51545339
DJ
12886 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12887
12888 *slot = die;
c906108c
SS
12889}
12890
93311388
DE
12891static int
12892is_ref_attr (struct attribute *attr)
c906108c 12893{
c906108c
SS
12894 switch (attr->form)
12895 {
12896 case DW_FORM_ref_addr:
c906108c
SS
12897 case DW_FORM_ref1:
12898 case DW_FORM_ref2:
12899 case DW_FORM_ref4:
613e1657 12900 case DW_FORM_ref8:
c906108c 12901 case DW_FORM_ref_udata:
93311388 12902 return 1;
c906108c 12903 default:
93311388 12904 return 0;
c906108c 12905 }
93311388
DE
12906}
12907
12908static unsigned int
12909dwarf2_get_ref_die_offset (struct attribute *attr)
12910{
12911 if (is_ref_attr (attr))
12912 return DW_ADDR (attr);
12913
12914 complaint (&symfile_complaints,
12915 _("unsupported die ref attribute form: '%s'"),
12916 dwarf_form_name (attr->form));
12917 return 0;
c906108c
SS
12918}
12919
43bbcdc2
PH
12920/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12921 * the value held by the attribute is not constant. */
a02abb62 12922
43bbcdc2 12923static LONGEST
a02abb62
JB
12924dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12925{
12926 if (attr->form == DW_FORM_sdata)
12927 return DW_SND (attr);
12928 else if (attr->form == DW_FORM_udata
12929 || attr->form == DW_FORM_data1
12930 || attr->form == DW_FORM_data2
12931 || attr->form == DW_FORM_data4
12932 || attr->form == DW_FORM_data8)
12933 return DW_UNSND (attr);
12934 else
12935 {
e2e0b3e5 12936 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12937 dwarf_form_name (attr->form));
12938 return default_value;
12939 }
12940}
12941
03dd20cc 12942/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12943 unit and add it to our queue.
12944 The result is non-zero if PER_CU was queued, otherwise the result is zero
12945 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12946
348e048f 12947static int
03dd20cc
DJ
12948maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12949 struct dwarf2_per_cu_data *per_cu)
12950{
98bfdba5
PA
12951 /* We may arrive here during partial symbol reading, if we need full
12952 DIEs to process an unusual case (e.g. template arguments). Do
12953 not queue PER_CU, just tell our caller to load its DIEs. */
12954 if (dwarf2_per_objfile->reading_partial_symbols)
12955 {
12956 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12957 return 1;
12958 return 0;
12959 }
12960
03dd20cc
DJ
12961 /* Mark the dependence relation so that we don't flush PER_CU
12962 too early. */
12963 dwarf2_add_dependence (this_cu, per_cu);
12964
12965 /* If it's already on the queue, we have nothing to do. */
12966 if (per_cu->queued)
348e048f 12967 return 0;
03dd20cc
DJ
12968
12969 /* If the compilation unit is already loaded, just mark it as
12970 used. */
12971 if (per_cu->cu != NULL)
12972 {
12973 per_cu->cu->last_used = 0;
348e048f 12974 return 0;
03dd20cc
DJ
12975 }
12976
12977 /* Add it to the queue. */
12978 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12979
12980 return 1;
12981}
12982
12983/* Follow reference or signature attribute ATTR of SRC_DIE.
12984 On entry *REF_CU is the CU of SRC_DIE.
12985 On exit *REF_CU is the CU of the result. */
12986
12987static struct die_info *
12988follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12989 struct dwarf2_cu **ref_cu)
12990{
12991 struct die_info *die;
12992
12993 if (is_ref_attr (attr))
12994 die = follow_die_ref (src_die, attr, ref_cu);
12995 else if (attr->form == DW_FORM_sig8)
12996 die = follow_die_sig (src_die, attr, ref_cu);
12997 else
12998 {
12999 dump_die_for_error (src_die);
13000 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13001 (*ref_cu)->objfile->name);
13002 }
13003
13004 return die;
03dd20cc
DJ
13005}
13006
5c631832 13007/* Follow reference OFFSET.
673bfd45
DE
13008 On entry *REF_CU is the CU of the source die referencing OFFSET.
13009 On exit *REF_CU is the CU of the result.
13010 Returns NULL if OFFSET is invalid. */
f504f079 13011
f9aca02d 13012static struct die_info *
5c631832 13013follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13014{
10b3939b 13015 struct die_info temp_die;
f2f0e013 13016 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13017
348e048f
DE
13018 gdb_assert (cu->per_cu != NULL);
13019
98bfdba5
PA
13020 target_cu = cu;
13021
348e048f
DE
13022 if (cu->per_cu->from_debug_types)
13023 {
13024 /* .debug_types CUs cannot reference anything outside their CU.
13025 If they need to, they have to reference a signatured type via
13026 DW_FORM_sig8. */
13027 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13028 return NULL;
348e048f
DE
13029 }
13030 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13031 {
13032 struct dwarf2_per_cu_data *per_cu;
9a619af0 13033
45452591 13034 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13035
13036 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13037 if (maybe_queue_comp_unit (cu, per_cu))
13038 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13039
10b3939b
DJ
13040 target_cu = per_cu->cu;
13041 }
98bfdba5
PA
13042 else if (cu->dies == NULL)
13043 {
13044 /* We're loading full DIEs during partial symbol reading. */
13045 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13046 load_full_comp_unit (cu->per_cu, cu->objfile);
13047 }
c906108c 13048
f2f0e013 13049 *ref_cu = target_cu;
51545339 13050 temp_die.offset = offset;
5c631832
JK
13051 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13052}
10b3939b 13053
5c631832
JK
13054/* Follow reference attribute ATTR of SRC_DIE.
13055 On entry *REF_CU is the CU of SRC_DIE.
13056 On exit *REF_CU is the CU of the result. */
13057
13058static struct die_info *
13059follow_die_ref (struct die_info *src_die, struct attribute *attr,
13060 struct dwarf2_cu **ref_cu)
13061{
13062 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13063 struct dwarf2_cu *cu = *ref_cu;
13064 struct die_info *die;
13065
13066 die = follow_die_offset (offset, ref_cu);
13067 if (!die)
13068 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13069 "at 0x%x [in module %s]"),
13070 offset, src_die->offset, cu->objfile->name);
348e048f 13071
5c631832
JK
13072 return die;
13073}
13074
13075/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13076 value is intended for DW_OP_call*. */
13077
13078struct dwarf2_locexpr_baton
13079dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13080 struct dwarf2_per_cu_data *per_cu,
13081 CORE_ADDR (*get_frame_pc) (void *baton),
13082 void *baton)
5c631832
JK
13083{
13084 struct dwarf2_cu *cu = per_cu->cu;
13085 struct die_info *die;
13086 struct attribute *attr;
13087 struct dwarf2_locexpr_baton retval;
13088
8cf6f0b1
TT
13089 dw2_setup (per_cu->objfile);
13090
5c631832
JK
13091 die = follow_die_offset (offset, &cu);
13092 if (!die)
13093 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13094 offset, per_cu->cu->objfile->name);
13095
13096 attr = dwarf2_attr (die, DW_AT_location, cu);
13097 if (!attr)
13098 {
13099 /* DWARF: "If there is no such attribute, then there is no effect.". */
13100
13101 retval.data = NULL;
13102 retval.size = 0;
13103 }
8cf6f0b1
TT
13104 else if (attr_form_is_section_offset (attr))
13105 {
13106 struct dwarf2_loclist_baton loclist_baton;
13107 CORE_ADDR pc = (*get_frame_pc) (baton);
13108 size_t size;
13109
13110 fill_in_loclist_baton (cu, &loclist_baton, attr);
13111
13112 retval.data = dwarf2_find_location_expression (&loclist_baton,
13113 &size, pc);
13114 retval.size = size;
13115 }
5c631832
JK
13116 else
13117 {
13118 if (!attr_form_is_block (attr))
13119 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13120 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13121 offset, per_cu->cu->objfile->name);
13122
13123 retval.data = DW_BLOCK (attr)->data;
13124 retval.size = DW_BLOCK (attr)->size;
13125 }
13126 retval.per_cu = cu->per_cu;
13127 return retval;
348e048f
DE
13128}
13129
13130/* Follow the signature attribute ATTR in SRC_DIE.
13131 On entry *REF_CU is the CU of SRC_DIE.
13132 On exit *REF_CU is the CU of the result. */
13133
13134static struct die_info *
13135follow_die_sig (struct die_info *src_die, struct attribute *attr,
13136 struct dwarf2_cu **ref_cu)
13137{
13138 struct objfile *objfile = (*ref_cu)->objfile;
13139 struct die_info temp_die;
13140 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13141 struct dwarf2_cu *sig_cu;
13142 struct die_info *die;
13143
13144 /* sig_type will be NULL if the signatured type is missing from
13145 the debug info. */
13146 if (sig_type == NULL)
13147 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13148 "at 0x%x [in module %s]"),
13149 src_die->offset, objfile->name);
13150
13151 /* If necessary, add it to the queue and load its DIEs. */
13152
13153 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13154 read_signatured_type (objfile, sig_type);
13155
13156 gdb_assert (sig_type->per_cu.cu != NULL);
13157
13158 sig_cu = sig_type->per_cu.cu;
13159 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13160 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13161 if (die)
13162 {
13163 *ref_cu = sig_cu;
13164 return die;
13165 }
13166
13167 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13168 "at 0x%x [in module %s]"),
13169 sig_type->type_offset, src_die->offset, objfile->name);
13170}
13171
13172/* Given an offset of a signatured type, return its signatured_type. */
13173
13174static struct signatured_type *
13175lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13176{
13177 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13178 unsigned int length, initial_length_size;
13179 unsigned int sig_offset;
13180 struct signatured_type find_entry, *type_sig;
13181
13182 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13183 sig_offset = (initial_length_size
13184 + 2 /*version*/
13185 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13186 + 1 /*address_size*/);
13187 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13188 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13189
13190 /* This is only used to lookup previously recorded types.
13191 If we didn't find it, it's our bug. */
13192 gdb_assert (type_sig != NULL);
13193 gdb_assert (offset == type_sig->offset);
13194
13195 return type_sig;
13196}
13197
13198/* Read in signatured type at OFFSET and build its CU and die(s). */
13199
13200static void
13201read_signatured_type_at_offset (struct objfile *objfile,
13202 unsigned int offset)
13203{
13204 struct signatured_type *type_sig;
13205
be391dca
TT
13206 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13207
348e048f
DE
13208 /* We have the section offset, but we need the signature to do the
13209 hash table lookup. */
13210 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13211
13212 gdb_assert (type_sig->per_cu.cu == NULL);
13213
13214 read_signatured_type (objfile, type_sig);
13215
13216 gdb_assert (type_sig->per_cu.cu != NULL);
13217}
13218
13219/* Read in a signatured type and build its CU and DIEs. */
13220
13221static void
13222read_signatured_type (struct objfile *objfile,
13223 struct signatured_type *type_sig)
13224{
1fd400ff 13225 gdb_byte *types_ptr;
348e048f
DE
13226 struct die_reader_specs reader_specs;
13227 struct dwarf2_cu *cu;
13228 ULONGEST signature;
13229 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13230
1fd400ff
TT
13231 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13232 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13233
348e048f
DE
13234 gdb_assert (type_sig->per_cu.cu == NULL);
13235
9816fde3
JK
13236 cu = xmalloc (sizeof (*cu));
13237 init_one_comp_unit (cu, objfile);
13238
348e048f
DE
13239 type_sig->per_cu.cu = cu;
13240 cu->per_cu = &type_sig->per_cu;
13241
13242 /* If an error occurs while loading, release our storage. */
13243 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13244
13245 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13246 types_ptr, objfile->obfd);
13247 gdb_assert (signature == type_sig->signature);
13248
13249 cu->die_hash
13250 = htab_create_alloc_ex (cu->header.length / 12,
13251 die_hash,
13252 die_eq,
13253 NULL,
13254 &cu->comp_unit_obstack,
13255 hashtab_obstack_allocate,
13256 dummy_obstack_deallocate);
13257
13258 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13259 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13260
13261 init_cu_die_reader (&reader_specs, cu);
13262
13263 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13264 NULL /*parent*/);
13265
13266 /* We try not to read any attributes in this function, because not
13267 all objfiles needed for references have been loaded yet, and symbol
13268 table processing isn't initialized. But we have to set the CU language,
13269 or we won't be able to build types correctly. */
9816fde3 13270 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13271
13272 do_cleanups (back_to);
13273
13274 /* We've successfully allocated this compilation unit. Let our caller
13275 clean it up when finished with it. */
13276 discard_cleanups (free_cu_cleanup);
13277
13278 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13279 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13280}
13281
c906108c
SS
13282/* Decode simple location descriptions.
13283 Given a pointer to a dwarf block that defines a location, compute
13284 the location and return the value.
13285
4cecd739
DJ
13286 NOTE drow/2003-11-18: This function is called in two situations
13287 now: for the address of static or global variables (partial symbols
13288 only) and for offsets into structures which are expected to be
13289 (more or less) constant. The partial symbol case should go away,
13290 and only the constant case should remain. That will let this
13291 function complain more accurately. A few special modes are allowed
13292 without complaint for global variables (for instance, global
13293 register values and thread-local values).
c906108c
SS
13294
13295 A location description containing no operations indicates that the
4cecd739 13296 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13297 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13298 callers will only want a very basic result and this can become a
13299 complaint.
c906108c 13300
d53d4ac5 13301 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13302
13303static CORE_ADDR
e7c27a73 13304decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13305{
e7c27a73 13306 struct objfile *objfile = cu->objfile;
c906108c
SS
13307 int i;
13308 int size = blk->size;
fe1b8b76 13309 gdb_byte *data = blk->data;
c906108c
SS
13310 CORE_ADDR stack[64];
13311 int stacki;
13312 unsigned int bytes_read, unsnd;
fe1b8b76 13313 gdb_byte op;
c906108c
SS
13314
13315 i = 0;
13316 stacki = 0;
13317 stack[stacki] = 0;
d53d4ac5 13318 stack[++stacki] = 0;
c906108c
SS
13319
13320 while (i < size)
13321 {
c906108c
SS
13322 op = data[i++];
13323 switch (op)
13324 {
f1bea926
JM
13325 case DW_OP_lit0:
13326 case DW_OP_lit1:
13327 case DW_OP_lit2:
13328 case DW_OP_lit3:
13329 case DW_OP_lit4:
13330 case DW_OP_lit5:
13331 case DW_OP_lit6:
13332 case DW_OP_lit7:
13333 case DW_OP_lit8:
13334 case DW_OP_lit9:
13335 case DW_OP_lit10:
13336 case DW_OP_lit11:
13337 case DW_OP_lit12:
13338 case DW_OP_lit13:
13339 case DW_OP_lit14:
13340 case DW_OP_lit15:
13341 case DW_OP_lit16:
13342 case DW_OP_lit17:
13343 case DW_OP_lit18:
13344 case DW_OP_lit19:
13345 case DW_OP_lit20:
13346 case DW_OP_lit21:
13347 case DW_OP_lit22:
13348 case DW_OP_lit23:
13349 case DW_OP_lit24:
13350 case DW_OP_lit25:
13351 case DW_OP_lit26:
13352 case DW_OP_lit27:
13353 case DW_OP_lit28:
13354 case DW_OP_lit29:
13355 case DW_OP_lit30:
13356 case DW_OP_lit31:
13357 stack[++stacki] = op - DW_OP_lit0;
13358 break;
13359
c906108c
SS
13360 case DW_OP_reg0:
13361 case DW_OP_reg1:
13362 case DW_OP_reg2:
13363 case DW_OP_reg3:
13364 case DW_OP_reg4:
13365 case DW_OP_reg5:
13366 case DW_OP_reg6:
13367 case DW_OP_reg7:
13368 case DW_OP_reg8:
13369 case DW_OP_reg9:
13370 case DW_OP_reg10:
13371 case DW_OP_reg11:
13372 case DW_OP_reg12:
13373 case DW_OP_reg13:
13374 case DW_OP_reg14:
13375 case DW_OP_reg15:
13376 case DW_OP_reg16:
13377 case DW_OP_reg17:
13378 case DW_OP_reg18:
13379 case DW_OP_reg19:
13380 case DW_OP_reg20:
13381 case DW_OP_reg21:
13382 case DW_OP_reg22:
13383 case DW_OP_reg23:
13384 case DW_OP_reg24:
13385 case DW_OP_reg25:
13386 case DW_OP_reg26:
13387 case DW_OP_reg27:
13388 case DW_OP_reg28:
13389 case DW_OP_reg29:
13390 case DW_OP_reg30:
13391 case DW_OP_reg31:
c906108c 13392 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13393 if (i < size)
13394 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13395 break;
13396
13397 case DW_OP_regx:
c906108c
SS
13398 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13399 i += bytes_read;
c906108c 13400 stack[++stacki] = unsnd;
4cecd739
DJ
13401 if (i < size)
13402 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13403 break;
13404
13405 case DW_OP_addr:
107d2387 13406 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13407 cu, &bytes_read);
107d2387 13408 i += bytes_read;
c906108c
SS
13409 break;
13410
13411 case DW_OP_const1u:
13412 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13413 i += 1;
13414 break;
13415
13416 case DW_OP_const1s:
13417 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13418 i += 1;
13419 break;
13420
13421 case DW_OP_const2u:
13422 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13423 i += 2;
13424 break;
13425
13426 case DW_OP_const2s:
13427 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13428 i += 2;
13429 break;
13430
13431 case DW_OP_const4u:
13432 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13433 i += 4;
13434 break;
13435
13436 case DW_OP_const4s:
13437 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13438 i += 4;
13439 break;
13440
13441 case DW_OP_constu:
13442 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13443 &bytes_read);
c906108c
SS
13444 i += bytes_read;
13445 break;
13446
13447 case DW_OP_consts:
13448 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13449 i += bytes_read;
13450 break;
13451
f1bea926
JM
13452 case DW_OP_dup:
13453 stack[stacki + 1] = stack[stacki];
13454 stacki++;
13455 break;
13456
c906108c
SS
13457 case DW_OP_plus:
13458 stack[stacki - 1] += stack[stacki];
13459 stacki--;
13460 break;
13461
13462 case DW_OP_plus_uconst:
13463 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13464 i += bytes_read;
13465 break;
13466
13467 case DW_OP_minus:
f1bea926 13468 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13469 stacki--;
13470 break;
13471
7a292a7a 13472 case DW_OP_deref:
7a292a7a 13473 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13474 this using GDB's address_class enum. This is valid for partial
13475 global symbols, although the variable's address will be bogus
13476 in the psymtab. */
7a292a7a 13477 if (i < size)
4d3c2250 13478 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13479 break;
13480
9d774e44 13481 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13482 /* The top of the stack has the offset from the beginning
13483 of the thread control block at which the variable is located. */
13484 /* Nothing should follow this operator, so the top of stack would
13485 be returned. */
4cecd739
DJ
13486 /* This is valid for partial global symbols, but the variable's
13487 address will be bogus in the psymtab. */
9d774e44 13488 if (i < size)
4d3c2250 13489 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13490 break;
13491
42be36b3
CT
13492 case DW_OP_GNU_uninit:
13493 break;
13494
c906108c 13495 default:
e2e0b3e5 13496 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13497 dwarf_stack_op_name (op, 1));
c906108c
SS
13498 return (stack[stacki]);
13499 }
d53d4ac5
TT
13500
13501 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13502 outside of the allocated space. Also enforce minimum>0. */
13503 if (stacki >= ARRAY_SIZE (stack) - 1)
13504 {
13505 complaint (&symfile_complaints,
13506 _("location description stack overflow"));
13507 return 0;
13508 }
13509
13510 if (stacki <= 0)
13511 {
13512 complaint (&symfile_complaints,
13513 _("location description stack underflow"));
13514 return 0;
13515 }
c906108c
SS
13516 }
13517 return (stack[stacki]);
13518}
13519
13520/* memory allocation interface */
13521
c906108c 13522static struct dwarf_block *
7b5a2f43 13523dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13524{
13525 struct dwarf_block *blk;
13526
13527 blk = (struct dwarf_block *)
7b5a2f43 13528 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13529 return (blk);
13530}
13531
13532static struct abbrev_info *
f3dd6933 13533dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13534{
13535 struct abbrev_info *abbrev;
13536
f3dd6933
DJ
13537 abbrev = (struct abbrev_info *)
13538 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13539 memset (abbrev, 0, sizeof (struct abbrev_info));
13540 return (abbrev);
13541}
13542
13543static struct die_info *
b60c80d6 13544dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13545{
13546 struct die_info *die;
b60c80d6
DJ
13547 size_t size = sizeof (struct die_info);
13548
13549 if (num_attrs > 1)
13550 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13551
b60c80d6 13552 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13553 memset (die, 0, sizeof (struct die_info));
13554 return (die);
13555}
2e276125
JB
13556
13557\f
13558/* Macro support. */
13559
13560
13561/* Return the full name of file number I in *LH's file name table.
13562 Use COMP_DIR as the name of the current directory of the
13563 compilation. The result is allocated using xmalloc; the caller is
13564 responsible for freeing it. */
13565static char *
13566file_full_name (int file, struct line_header *lh, const char *comp_dir)
13567{
6a83a1e6
EZ
13568 /* Is the file number a valid index into the line header's file name
13569 table? Remember that file numbers start with one, not zero. */
13570 if (1 <= file && file <= lh->num_file_names)
13571 {
13572 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13573
6a83a1e6
EZ
13574 if (IS_ABSOLUTE_PATH (fe->name))
13575 return xstrdup (fe->name);
13576 else
13577 {
13578 const char *dir;
13579 int dir_len;
13580 char *full_name;
13581
13582 if (fe->dir_index)
13583 dir = lh->include_dirs[fe->dir_index - 1];
13584 else
13585 dir = comp_dir;
13586
13587 if (dir)
13588 {
13589 dir_len = strlen (dir);
13590 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13591 strcpy (full_name, dir);
13592 full_name[dir_len] = '/';
13593 strcpy (full_name + dir_len + 1, fe->name);
13594 return full_name;
13595 }
13596 else
13597 return xstrdup (fe->name);
13598 }
13599 }
2e276125
JB
13600 else
13601 {
6a83a1e6
EZ
13602 /* The compiler produced a bogus file number. We can at least
13603 record the macro definitions made in the file, even if we
13604 won't be able to find the file by name. */
13605 char fake_name[80];
9a619af0 13606
6a83a1e6 13607 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13608
6e70227d 13609 complaint (&symfile_complaints,
6a83a1e6
EZ
13610 _("bad file number in macro information (%d)"),
13611 file);
2e276125 13612
6a83a1e6 13613 return xstrdup (fake_name);
2e276125
JB
13614 }
13615}
13616
13617
13618static struct macro_source_file *
13619macro_start_file (int file, int line,
13620 struct macro_source_file *current_file,
13621 const char *comp_dir,
13622 struct line_header *lh, struct objfile *objfile)
13623{
13624 /* The full name of this source file. */
13625 char *full_name = file_full_name (file, lh, comp_dir);
13626
13627 /* We don't create a macro table for this compilation unit
13628 at all until we actually get a filename. */
13629 if (! pending_macros)
4a146b47 13630 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13631 objfile->macro_cache);
2e276125
JB
13632
13633 if (! current_file)
13634 /* If we have no current file, then this must be the start_file
13635 directive for the compilation unit's main source file. */
13636 current_file = macro_set_main (pending_macros, full_name);
13637 else
13638 current_file = macro_include (current_file, line, full_name);
13639
13640 xfree (full_name);
6e70227d 13641
2e276125
JB
13642 return current_file;
13643}
13644
13645
13646/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13647 followed by a null byte. */
13648static char *
13649copy_string (const char *buf, int len)
13650{
13651 char *s = xmalloc (len + 1);
9a619af0 13652
2e276125
JB
13653 memcpy (s, buf, len);
13654 s[len] = '\0';
2e276125
JB
13655 return s;
13656}
13657
13658
13659static const char *
13660consume_improper_spaces (const char *p, const char *body)
13661{
13662 if (*p == ' ')
13663 {
4d3c2250 13664 complaint (&symfile_complaints,
e2e0b3e5 13665 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13666 body);
2e276125
JB
13667
13668 while (*p == ' ')
13669 p++;
13670 }
13671
13672 return p;
13673}
13674
13675
13676static void
13677parse_macro_definition (struct macro_source_file *file, int line,
13678 const char *body)
13679{
13680 const char *p;
13681
13682 /* The body string takes one of two forms. For object-like macro
13683 definitions, it should be:
13684
13685 <macro name> " " <definition>
13686
13687 For function-like macro definitions, it should be:
13688
13689 <macro name> "() " <definition>
13690 or
13691 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13692
13693 Spaces may appear only where explicitly indicated, and in the
13694 <definition>.
13695
13696 The Dwarf 2 spec says that an object-like macro's name is always
13697 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13698 the space when the macro's definition is the empty string.
2e276125
JB
13699
13700 The Dwarf 2 spec says that there should be no spaces between the
13701 formal arguments in a function-like macro's formal argument list,
13702 but versions of GCC around March 2002 include spaces after the
13703 commas. */
13704
13705
13706 /* Find the extent of the macro name. The macro name is terminated
13707 by either a space or null character (for an object-like macro) or
13708 an opening paren (for a function-like macro). */
13709 for (p = body; *p; p++)
13710 if (*p == ' ' || *p == '(')
13711 break;
13712
13713 if (*p == ' ' || *p == '\0')
13714 {
13715 /* It's an object-like macro. */
13716 int name_len = p - body;
13717 char *name = copy_string (body, name_len);
13718 const char *replacement;
13719
13720 if (*p == ' ')
13721 replacement = body + name_len + 1;
13722 else
13723 {
4d3c2250 13724 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13725 replacement = body + name_len;
13726 }
6e70227d 13727
2e276125
JB
13728 macro_define_object (file, line, name, replacement);
13729
13730 xfree (name);
13731 }
13732 else if (*p == '(')
13733 {
13734 /* It's a function-like macro. */
13735 char *name = copy_string (body, p - body);
13736 int argc = 0;
13737 int argv_size = 1;
13738 char **argv = xmalloc (argv_size * sizeof (*argv));
13739
13740 p++;
13741
13742 p = consume_improper_spaces (p, body);
13743
13744 /* Parse the formal argument list. */
13745 while (*p && *p != ')')
13746 {
13747 /* Find the extent of the current argument name. */
13748 const char *arg_start = p;
13749
13750 while (*p && *p != ',' && *p != ')' && *p != ' ')
13751 p++;
13752
13753 if (! *p || p == arg_start)
4d3c2250 13754 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13755 else
13756 {
13757 /* Make sure argv has room for the new argument. */
13758 if (argc >= argv_size)
13759 {
13760 argv_size *= 2;
13761 argv = xrealloc (argv, argv_size * sizeof (*argv));
13762 }
13763
13764 argv[argc++] = copy_string (arg_start, p - arg_start);
13765 }
13766
13767 p = consume_improper_spaces (p, body);
13768
13769 /* Consume the comma, if present. */
13770 if (*p == ',')
13771 {
13772 p++;
13773
13774 p = consume_improper_spaces (p, body);
13775 }
13776 }
13777
13778 if (*p == ')')
13779 {
13780 p++;
13781
13782 if (*p == ' ')
13783 /* Perfectly formed definition, no complaints. */
13784 macro_define_function (file, line, name,
6e70227d 13785 argc, (const char **) argv,
2e276125
JB
13786 p + 1);
13787 else if (*p == '\0')
13788 {
13789 /* Complain, but do define it. */
4d3c2250 13790 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13791 macro_define_function (file, line, name,
6e70227d 13792 argc, (const char **) argv,
2e276125
JB
13793 p);
13794 }
13795 else
13796 /* Just complain. */
4d3c2250 13797 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13798 }
13799 else
13800 /* Just complain. */
4d3c2250 13801 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13802
13803 xfree (name);
13804 {
13805 int i;
13806
13807 for (i = 0; i < argc; i++)
13808 xfree (argv[i]);
13809 }
13810 xfree (argv);
13811 }
13812 else
4d3c2250 13813 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13814}
13815
13816
13817static void
13818dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13819 char *comp_dir, bfd *abfd,
e7c27a73 13820 struct dwarf2_cu *cu)
2e276125 13821{
fe1b8b76 13822 gdb_byte *mac_ptr, *mac_end;
2e276125 13823 struct macro_source_file *current_file = 0;
757a13d0
JK
13824 enum dwarf_macinfo_record_type macinfo_type;
13825 int at_commandline;
2e276125 13826
be391dca
TT
13827 dwarf2_read_section (dwarf2_per_objfile->objfile,
13828 &dwarf2_per_objfile->macinfo);
dce234bc 13829 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13830 {
e2e0b3e5 13831 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13832 return;
13833 }
13834
757a13d0
JK
13835 /* First pass: Find the name of the base filename.
13836 This filename is needed in order to process all macros whose definition
13837 (or undefinition) comes from the command line. These macros are defined
13838 before the first DW_MACINFO_start_file entry, and yet still need to be
13839 associated to the base file.
13840
13841 To determine the base file name, we scan the macro definitions until we
13842 reach the first DW_MACINFO_start_file entry. We then initialize
13843 CURRENT_FILE accordingly so that any macro definition found before the
13844 first DW_MACINFO_start_file can still be associated to the base file. */
13845
dce234bc
PP
13846 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13847 mac_end = dwarf2_per_objfile->macinfo.buffer
13848 + dwarf2_per_objfile->macinfo.size;
2e276125 13849
757a13d0 13850 do
2e276125 13851 {
2e276125
JB
13852 /* Do we at least have room for a macinfo type byte? */
13853 if (mac_ptr >= mac_end)
13854 {
757a13d0
JK
13855 /* Complaint is printed during the second pass as GDB will probably
13856 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13857 break;
2e276125
JB
13858 }
13859
13860 macinfo_type = read_1_byte (abfd, mac_ptr);
13861 mac_ptr++;
13862
13863 switch (macinfo_type)
13864 {
13865 /* A zero macinfo type indicates the end of the macro
13866 information. */
13867 case 0:
757a13d0
JK
13868 break;
13869
13870 case DW_MACINFO_define:
13871 case DW_MACINFO_undef:
13872 /* Only skip the data by MAC_PTR. */
13873 {
13874 unsigned int bytes_read;
13875
13876 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13877 mac_ptr += bytes_read;
9b1c24c8 13878 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13879 mac_ptr += bytes_read;
13880 }
13881 break;
13882
13883 case DW_MACINFO_start_file:
13884 {
13885 unsigned int bytes_read;
13886 int line, file;
13887
13888 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13889 mac_ptr += bytes_read;
13890 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13891 mac_ptr += bytes_read;
13892
13893 current_file = macro_start_file (file, line, current_file, comp_dir,
13894 lh, cu->objfile);
13895 }
13896 break;
13897
13898 case DW_MACINFO_end_file:
13899 /* No data to skip by MAC_PTR. */
13900 break;
13901
13902 case DW_MACINFO_vendor_ext:
13903 /* Only skip the data by MAC_PTR. */
13904 {
13905 unsigned int bytes_read;
13906
13907 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13908 mac_ptr += bytes_read;
9b1c24c8 13909 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13910 mac_ptr += bytes_read;
13911 }
13912 break;
13913
13914 default:
13915 break;
13916 }
13917 } while (macinfo_type != 0 && current_file == NULL);
13918
13919 /* Second pass: Process all entries.
13920
13921 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13922 command-line macro definitions/undefinitions. This flag is unset when we
13923 reach the first DW_MACINFO_start_file entry. */
13924
dce234bc 13925 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13926
13927 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13928 GDB is still reading the definitions from command line. First
13929 DW_MACINFO_start_file will need to be ignored as it was already executed
13930 to create CURRENT_FILE for the main source holding also the command line
13931 definitions. On first met DW_MACINFO_start_file this flag is reset to
13932 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13933
13934 at_commandline = 1;
13935
13936 do
13937 {
13938 /* Do we at least have room for a macinfo type byte? */
13939 if (mac_ptr >= mac_end)
13940 {
13941 dwarf2_macros_too_long_complaint ();
13942 break;
13943 }
13944
13945 macinfo_type = read_1_byte (abfd, mac_ptr);
13946 mac_ptr++;
13947
13948 switch (macinfo_type)
13949 {
13950 /* A zero macinfo type indicates the end of the macro
13951 information. */
13952 case 0:
13953 break;
2e276125
JB
13954
13955 case DW_MACINFO_define:
13956 case DW_MACINFO_undef:
13957 {
891d2f0b 13958 unsigned int bytes_read;
2e276125
JB
13959 int line;
13960 char *body;
13961
13962 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13963 mac_ptr += bytes_read;
9b1c24c8 13964 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13965 mac_ptr += bytes_read;
13966
13967 if (! current_file)
757a13d0
JK
13968 {
13969 /* DWARF violation as no main source is present. */
13970 complaint (&symfile_complaints,
13971 _("debug info with no main source gives macro %s "
13972 "on line %d: %s"),
6e70227d
DE
13973 macinfo_type == DW_MACINFO_define ?
13974 _("definition") :
905e0470
PM
13975 macinfo_type == DW_MACINFO_undef ?
13976 _("undefinition") :
13977 _("something-or-other"), line, body);
757a13d0
JK
13978 break;
13979 }
13980 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13981 complaint (&symfile_complaints,
757a13d0
JK
13982 _("debug info gives %s macro %s with %s line %d: %s"),
13983 at_commandline ? _("command-line") : _("in-file"),
905e0470 13984 macinfo_type == DW_MACINFO_define ?
6e70227d 13985 _("definition") :
905e0470
PM
13986 macinfo_type == DW_MACINFO_undef ?
13987 _("undefinition") :
13988 _("something-or-other"),
757a13d0
JK
13989 line == 0 ? _("zero") : _("non-zero"), line, body);
13990
13991 if (macinfo_type == DW_MACINFO_define)
13992 parse_macro_definition (current_file, line, body);
13993 else if (macinfo_type == DW_MACINFO_undef)
13994 macro_undef (current_file, line, body);
2e276125
JB
13995 }
13996 break;
13997
13998 case DW_MACINFO_start_file:
13999 {
891d2f0b 14000 unsigned int bytes_read;
2e276125
JB
14001 int line, file;
14002
14003 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14004 mac_ptr += bytes_read;
14005 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14006 mac_ptr += bytes_read;
14007
757a13d0
JK
14008 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14009 complaint (&symfile_complaints,
14010 _("debug info gives source %d included "
14011 "from %s at %s line %d"),
14012 file, at_commandline ? _("command-line") : _("file"),
14013 line == 0 ? _("zero") : _("non-zero"), line);
14014
14015 if (at_commandline)
14016 {
14017 /* This DW_MACINFO_start_file was executed in the pass one. */
14018 at_commandline = 0;
14019 }
14020 else
14021 current_file = macro_start_file (file, line,
14022 current_file, comp_dir,
14023 lh, cu->objfile);
2e276125
JB
14024 }
14025 break;
14026
14027 case DW_MACINFO_end_file:
14028 if (! current_file)
4d3c2250 14029 complaint (&symfile_complaints,
e2e0b3e5 14030 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
14031 else
14032 {
14033 current_file = current_file->included_by;
14034 if (! current_file)
14035 {
14036 enum dwarf_macinfo_record_type next_type;
14037
14038 /* GCC circa March 2002 doesn't produce the zero
14039 type byte marking the end of the compilation
14040 unit. Complain if it's not there, but exit no
14041 matter what. */
14042
14043 /* Do we at least have room for a macinfo type byte? */
14044 if (mac_ptr >= mac_end)
14045 {
4d3c2250 14046 dwarf2_macros_too_long_complaint ();
2e276125
JB
14047 return;
14048 }
14049
14050 /* We don't increment mac_ptr here, so this is just
14051 a look-ahead. */
14052 next_type = read_1_byte (abfd, mac_ptr);
14053 if (next_type != 0)
4d3c2250 14054 complaint (&symfile_complaints,
e2e0b3e5 14055 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
14056
14057 return;
14058 }
14059 }
14060 break;
14061
14062 case DW_MACINFO_vendor_ext:
14063 {
891d2f0b 14064 unsigned int bytes_read;
2e276125
JB
14065 int constant;
14066 char *string;
14067
14068 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14069 mac_ptr += bytes_read;
9b1c24c8 14070 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14071 mac_ptr += bytes_read;
14072
14073 /* We don't recognize any vendor extensions. */
14074 }
14075 break;
14076 }
757a13d0 14077 } while (macinfo_type != 0);
2e276125 14078}
8e19ed76
PS
14079
14080/* Check if the attribute's form is a DW_FORM_block*
14081 if so return true else false. */
14082static int
14083attr_form_is_block (struct attribute *attr)
14084{
14085 return (attr == NULL ? 0 :
14086 attr->form == DW_FORM_block1
14087 || attr->form == DW_FORM_block2
14088 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14089 || attr->form == DW_FORM_block
14090 || attr->form == DW_FORM_exprloc);
8e19ed76 14091}
4c2df51b 14092
c6a0999f
JB
14093/* Return non-zero if ATTR's value is a section offset --- classes
14094 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14095 You may use DW_UNSND (attr) to retrieve such offsets.
14096
14097 Section 7.5.4, "Attribute Encodings", explains that no attribute
14098 may have a value that belongs to more than one of these classes; it
14099 would be ambiguous if we did, because we use the same forms for all
14100 of them. */
3690dd37
JB
14101static int
14102attr_form_is_section_offset (struct attribute *attr)
14103{
14104 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14105 || attr->form == DW_FORM_data8
14106 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14107}
14108
14109
14110/* Return non-zero if ATTR's value falls in the 'constant' class, or
14111 zero otherwise. When this function returns true, you can apply
14112 dwarf2_get_attr_constant_value to it.
14113
14114 However, note that for some attributes you must check
14115 attr_form_is_section_offset before using this test. DW_FORM_data4
14116 and DW_FORM_data8 are members of both the constant class, and of
14117 the classes that contain offsets into other debug sections
14118 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14119 that, if an attribute's can be either a constant or one of the
14120 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14121 taken as section offsets, not constants. */
14122static int
14123attr_form_is_constant (struct attribute *attr)
14124{
14125 switch (attr->form)
14126 {
14127 case DW_FORM_sdata:
14128 case DW_FORM_udata:
14129 case DW_FORM_data1:
14130 case DW_FORM_data2:
14131 case DW_FORM_data4:
14132 case DW_FORM_data8:
14133 return 1;
14134 default:
14135 return 0;
14136 }
14137}
14138
8cf6f0b1
TT
14139/* A helper function that fills in a dwarf2_loclist_baton. */
14140
14141static void
14142fill_in_loclist_baton (struct dwarf2_cu *cu,
14143 struct dwarf2_loclist_baton *baton,
14144 struct attribute *attr)
14145{
14146 dwarf2_read_section (dwarf2_per_objfile->objfile,
14147 &dwarf2_per_objfile->loc);
14148
14149 baton->per_cu = cu->per_cu;
14150 gdb_assert (baton->per_cu);
14151 /* We don't know how long the location list is, but make sure we
14152 don't run off the edge of the section. */
14153 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14154 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14155 baton->base_address = cu->base_address;
14156}
14157
4c2df51b
DJ
14158static void
14159dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14160 struct dwarf2_cu *cu)
4c2df51b 14161{
3690dd37 14162 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14163 /* ".debug_loc" may not exist at all, or the offset may be outside
14164 the section. If so, fall through to the complaint in the
14165 other branch. */
dce234bc 14166 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14167 {
0d53c4c4 14168 struct dwarf2_loclist_baton *baton;
4c2df51b 14169
4a146b47 14170 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14171 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14172
8cf6f0b1 14173 fill_in_loclist_baton (cu, baton, attr);
be391dca 14174
d00adf39 14175 if (cu->base_known == 0)
0d53c4c4 14176 complaint (&symfile_complaints,
e2e0b3e5 14177 _("Location list used without specifying the CU base address."));
4c2df51b 14178
768a979c 14179 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14180 SYMBOL_LOCATION_BATON (sym) = baton;
14181 }
14182 else
14183 {
14184 struct dwarf2_locexpr_baton *baton;
14185
4a146b47 14186 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14187 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14188 baton->per_cu = cu->per_cu;
14189 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14190
14191 if (attr_form_is_block (attr))
14192 {
14193 /* Note that we're just copying the block's data pointer
14194 here, not the actual data. We're still pointing into the
6502dd73
DJ
14195 info_buffer for SYM's objfile; right now we never release
14196 that buffer, but when we do clean up properly this may
14197 need to change. */
0d53c4c4
DJ
14198 baton->size = DW_BLOCK (attr)->size;
14199 baton->data = DW_BLOCK (attr)->data;
14200 }
14201 else
14202 {
14203 dwarf2_invalid_attrib_class_complaint ("location description",
14204 SYMBOL_NATURAL_NAME (sym));
14205 baton->size = 0;
14206 baton->data = NULL;
14207 }
6e70227d 14208
768a979c 14209 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14210 SYMBOL_LOCATION_BATON (sym) = baton;
14211 }
4c2df51b 14212}
6502dd73 14213
9aa1f1e3
TT
14214/* Return the OBJFILE associated with the compilation unit CU. If CU
14215 came from a separate debuginfo file, then the master objfile is
14216 returned. */
ae0d2f24
UW
14217
14218struct objfile *
14219dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14220{
9291a0cd 14221 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14222
14223 /* Return the master objfile, so that we can report and look up the
14224 correct file containing this variable. */
14225 if (objfile->separate_debug_objfile_backlink)
14226 objfile = objfile->separate_debug_objfile_backlink;
14227
14228 return objfile;
14229}
14230
14231/* Return the address size given in the compilation unit header for CU. */
14232
14233CORE_ADDR
14234dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14235{
14236 if (per_cu->cu)
14237 return per_cu->cu->header.addr_size;
14238 else
14239 {
14240 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14241 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14242 struct dwarf2_per_objfile *per_objfile
14243 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14244 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14245 struct comp_unit_head cu_header;
9a619af0 14246
ae0d2f24
UW
14247 memset (&cu_header, 0, sizeof cu_header);
14248 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14249 return cu_header.addr_size;
14250 }
14251}
14252
9eae7c52
TT
14253/* Return the offset size given in the compilation unit header for CU. */
14254
14255int
14256dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14257{
14258 if (per_cu->cu)
14259 return per_cu->cu->header.offset_size;
14260 else
14261 {
14262 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14263 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14264 struct dwarf2_per_objfile *per_objfile
14265 = objfile_data (objfile, dwarf2_objfile_data_key);
14266 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14267 struct comp_unit_head cu_header;
14268
14269 memset (&cu_header, 0, sizeof cu_header);
14270 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14271 return cu_header.offset_size;
14272 }
14273}
14274
9aa1f1e3
TT
14275/* Return the text offset of the CU. The returned offset comes from
14276 this CU's objfile. If this objfile came from a separate debuginfo
14277 file, then the offset may be different from the corresponding
14278 offset in the parent objfile. */
14279
14280CORE_ADDR
14281dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14282{
bb3fa9d0 14283 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14284
14285 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14286}
14287
348e048f
DE
14288/* Locate the .debug_info compilation unit from CU's objfile which contains
14289 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14290
14291static struct dwarf2_per_cu_data *
c764a876 14292dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14293 struct objfile *objfile)
14294{
14295 struct dwarf2_per_cu_data *this_cu;
14296 int low, high;
14297
ae038cb0
DJ
14298 low = 0;
14299 high = dwarf2_per_objfile->n_comp_units - 1;
14300 while (high > low)
14301 {
14302 int mid = low + (high - low) / 2;
9a619af0 14303
ae038cb0
DJ
14304 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14305 high = mid;
14306 else
14307 low = mid + 1;
14308 }
14309 gdb_assert (low == high);
14310 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14311 {
10b3939b 14312 if (low == 0)
8a3fe4f8
AC
14313 error (_("Dwarf Error: could not find partial DIE containing "
14314 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14315 (long) offset, bfd_get_filename (objfile->obfd));
14316
ae038cb0
DJ
14317 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14318 return dwarf2_per_objfile->all_comp_units[low-1];
14319 }
14320 else
14321 {
14322 this_cu = dwarf2_per_objfile->all_comp_units[low];
14323 if (low == dwarf2_per_objfile->n_comp_units - 1
14324 && offset >= this_cu->offset + this_cu->length)
c764a876 14325 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14326 gdb_assert (offset < this_cu->offset + this_cu->length);
14327 return this_cu;
14328 }
14329}
14330
10b3939b
DJ
14331/* Locate the compilation unit from OBJFILE which is located at exactly
14332 OFFSET. Raises an error on failure. */
14333
ae038cb0 14334static struct dwarf2_per_cu_data *
c764a876 14335dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14336{
14337 struct dwarf2_per_cu_data *this_cu;
9a619af0 14338
ae038cb0
DJ
14339 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14340 if (this_cu->offset != offset)
c764a876 14341 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14342 return this_cu;
14343}
14344
9816fde3 14345/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14346
9816fde3
JK
14347static void
14348init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14349{
9816fde3 14350 memset (cu, 0, sizeof (*cu));
93311388
DE
14351 cu->objfile = objfile;
14352 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14353}
14354
14355/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14356
14357static void
14358prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14359{
14360 struct attribute *attr;
14361
14362 /* Set the language we're debugging. */
14363 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14364 if (attr)
14365 set_cu_language (DW_UNSND (attr), cu);
14366 else
14367 set_cu_language (language_minimal, cu);
93311388
DE
14368}
14369
ae038cb0
DJ
14370/* Release one cached compilation unit, CU. We unlink it from the tree
14371 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14372 the caller is responsible for that.
14373 NOTE: DATA is a void * because this function is also used as a
14374 cleanup routine. */
ae038cb0
DJ
14375
14376static void
14377free_one_comp_unit (void *data)
14378{
14379 struct dwarf2_cu *cu = data;
14380
14381 if (cu->per_cu != NULL)
14382 cu->per_cu->cu = NULL;
14383 cu->per_cu = NULL;
14384
14385 obstack_free (&cu->comp_unit_obstack, NULL);
14386
14387 xfree (cu);
14388}
14389
72bf9492 14390/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14391 when we're finished with it. We can't free the pointer itself, but be
14392 sure to unlink it from the cache. Also release any associated storage
14393 and perform cache maintenance.
72bf9492
DJ
14394
14395 Only used during partial symbol parsing. */
14396
14397static void
14398free_stack_comp_unit (void *data)
14399{
14400 struct dwarf2_cu *cu = data;
14401
14402 obstack_free (&cu->comp_unit_obstack, NULL);
14403 cu->partial_dies = NULL;
ae038cb0
DJ
14404
14405 if (cu->per_cu != NULL)
14406 {
14407 /* This compilation unit is on the stack in our caller, so we
14408 should not xfree it. Just unlink it. */
14409 cu->per_cu->cu = NULL;
14410 cu->per_cu = NULL;
14411
14412 /* If we had a per-cu pointer, then we may have other compilation
14413 units loaded, so age them now. */
14414 age_cached_comp_units ();
14415 }
14416}
14417
14418/* Free all cached compilation units. */
14419
14420static void
14421free_cached_comp_units (void *data)
14422{
14423 struct dwarf2_per_cu_data *per_cu, **last_chain;
14424
14425 per_cu = dwarf2_per_objfile->read_in_chain;
14426 last_chain = &dwarf2_per_objfile->read_in_chain;
14427 while (per_cu != NULL)
14428 {
14429 struct dwarf2_per_cu_data *next_cu;
14430
14431 next_cu = per_cu->cu->read_in_chain;
14432
14433 free_one_comp_unit (per_cu->cu);
14434 *last_chain = next_cu;
14435
14436 per_cu = next_cu;
14437 }
14438}
14439
14440/* Increase the age counter on each cached compilation unit, and free
14441 any that are too old. */
14442
14443static void
14444age_cached_comp_units (void)
14445{
14446 struct dwarf2_per_cu_data *per_cu, **last_chain;
14447
14448 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14449 per_cu = dwarf2_per_objfile->read_in_chain;
14450 while (per_cu != NULL)
14451 {
14452 per_cu->cu->last_used ++;
14453 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14454 dwarf2_mark (per_cu->cu);
14455 per_cu = per_cu->cu->read_in_chain;
14456 }
14457
14458 per_cu = dwarf2_per_objfile->read_in_chain;
14459 last_chain = &dwarf2_per_objfile->read_in_chain;
14460 while (per_cu != NULL)
14461 {
14462 struct dwarf2_per_cu_data *next_cu;
14463
14464 next_cu = per_cu->cu->read_in_chain;
14465
14466 if (!per_cu->cu->mark)
14467 {
14468 free_one_comp_unit (per_cu->cu);
14469 *last_chain = next_cu;
14470 }
14471 else
14472 last_chain = &per_cu->cu->read_in_chain;
14473
14474 per_cu = next_cu;
14475 }
14476}
14477
14478/* Remove a single compilation unit from the cache. */
14479
14480static void
14481free_one_cached_comp_unit (void *target_cu)
14482{
14483 struct dwarf2_per_cu_data *per_cu, **last_chain;
14484
14485 per_cu = dwarf2_per_objfile->read_in_chain;
14486 last_chain = &dwarf2_per_objfile->read_in_chain;
14487 while (per_cu != NULL)
14488 {
14489 struct dwarf2_per_cu_data *next_cu;
14490
14491 next_cu = per_cu->cu->read_in_chain;
14492
14493 if (per_cu->cu == target_cu)
14494 {
14495 free_one_comp_unit (per_cu->cu);
14496 *last_chain = next_cu;
14497 break;
14498 }
14499 else
14500 last_chain = &per_cu->cu->read_in_chain;
14501
14502 per_cu = next_cu;
14503 }
14504}
14505
fe3e1990
DJ
14506/* Release all extra memory associated with OBJFILE. */
14507
14508void
14509dwarf2_free_objfile (struct objfile *objfile)
14510{
14511 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14512
14513 if (dwarf2_per_objfile == NULL)
14514 return;
14515
14516 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14517 free_cached_comp_units (NULL);
14518
9291a0cd
TT
14519 if (dwarf2_per_objfile->using_index)
14520 {
14521 int i;
14522
14523 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14524 {
14525 int j;
e254ef6a
DE
14526 struct dwarf2_per_cu_data *per_cu =
14527 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14528
e254ef6a 14529 if (!per_cu->v.quick->lines)
9291a0cd
TT
14530 continue;
14531
e254ef6a 14532 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14533 {
e254ef6a
DE
14534 if (per_cu->v.quick->file_names)
14535 xfree ((void *) per_cu->v.quick->file_names[j]);
14536 if (per_cu->v.quick->full_names)
14537 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14538 }
14539
e254ef6a 14540 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14541 }
14542 }
14543
fe3e1990
DJ
14544 /* Everything else should be on the objfile obstack. */
14545}
14546
1c379e20
DJ
14547/* A pair of DIE offset and GDB type pointer. We store these
14548 in a hash table separate from the DIEs, and preserve them
14549 when the DIEs are flushed out of cache. */
14550
14551struct dwarf2_offset_and_type
14552{
14553 unsigned int offset;
14554 struct type *type;
14555};
14556
14557/* Hash function for a dwarf2_offset_and_type. */
14558
14559static hashval_t
14560offset_and_type_hash (const void *item)
14561{
14562 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14563
1c379e20
DJ
14564 return ofs->offset;
14565}
14566
14567/* Equality function for a dwarf2_offset_and_type. */
14568
14569static int
14570offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14571{
14572 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14573 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14574
1c379e20
DJ
14575 return ofs_lhs->offset == ofs_rhs->offset;
14576}
14577
14578/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14579 table if necessary. For convenience, return TYPE.
14580
14581 The DIEs reading must have careful ordering to:
14582 * Not cause infite loops trying to read in DIEs as a prerequisite for
14583 reading current DIE.
14584 * Not trying to dereference contents of still incompletely read in types
14585 while reading in other DIEs.
14586 * Enable referencing still incompletely read in types just by a pointer to
14587 the type without accessing its fields.
14588
14589 Therefore caller should follow these rules:
14590 * Try to fetch any prerequisite types we may need to build this DIE type
14591 before building the type and calling set_die_type.
e71ec853 14592 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14593 possible before fetching more types to complete the current type.
14594 * Make the type as complete as possible before fetching more types. */
1c379e20 14595
f792889a 14596static struct type *
1c379e20
DJ
14597set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14598{
14599 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14600 struct objfile *objfile = cu->objfile;
14601 htab_t *type_hash_ptr;
1c379e20 14602
b4ba55a1
JB
14603 /* For Ada types, make sure that the gnat-specific data is always
14604 initialized (if not already set). There are a few types where
14605 we should not be doing so, because the type-specific area is
14606 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14607 where the type-specific area is used to store the floatformat).
14608 But this is not a problem, because the gnat-specific information
14609 is actually not needed for these types. */
14610 if (need_gnat_info (cu)
14611 && TYPE_CODE (type) != TYPE_CODE_FUNC
14612 && TYPE_CODE (type) != TYPE_CODE_FLT
14613 && !HAVE_GNAT_AUX_INFO (type))
14614 INIT_GNAT_SPECIFIC (type);
14615
673bfd45
DE
14616 if (cu->per_cu->from_debug_types)
14617 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14618 else
14619 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14620
14621 if (*type_hash_ptr == NULL)
f792889a 14622 {
673bfd45
DE
14623 *type_hash_ptr
14624 = htab_create_alloc_ex (127,
f792889a
DJ
14625 offset_and_type_hash,
14626 offset_and_type_eq,
14627 NULL,
673bfd45 14628 &objfile->objfile_obstack,
f792889a
DJ
14629 hashtab_obstack_allocate,
14630 dummy_obstack_deallocate);
f792889a 14631 }
1c379e20
DJ
14632
14633 ofs.offset = die->offset;
14634 ofs.type = type;
14635 slot = (struct dwarf2_offset_and_type **)
673bfd45 14636 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14637 if (*slot)
14638 complaint (&symfile_complaints,
14639 _("A problem internal to GDB: DIE 0x%x has type already set"),
14640 die->offset);
673bfd45 14641 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14642 **slot = ofs;
f792889a 14643 return type;
1c379e20
DJ
14644}
14645
673bfd45
DE
14646/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14647 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14648
14649static struct type *
673bfd45
DE
14650get_die_type_at_offset (unsigned int offset,
14651 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14652{
14653 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14654 htab_t type_hash;
f792889a 14655
673bfd45
DE
14656 if (per_cu->from_debug_types)
14657 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14658 else
14659 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14660 if (type_hash == NULL)
14661 return NULL;
1c379e20 14662
673bfd45 14663 ofs.offset = offset;
1c379e20
DJ
14664 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14665 if (slot)
14666 return slot->type;
14667 else
14668 return NULL;
14669}
14670
673bfd45
DE
14671/* Look up the type for DIE in the appropriate type_hash table,
14672 or return NULL if DIE does not have a saved type. */
14673
14674static struct type *
14675get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14676{
14677 return get_die_type_at_offset (die->offset, cu->per_cu);
14678}
14679
10b3939b
DJ
14680/* Add a dependence relationship from CU to REF_PER_CU. */
14681
14682static void
14683dwarf2_add_dependence (struct dwarf2_cu *cu,
14684 struct dwarf2_per_cu_data *ref_per_cu)
14685{
14686 void **slot;
14687
14688 if (cu->dependencies == NULL)
14689 cu->dependencies
14690 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14691 NULL, &cu->comp_unit_obstack,
14692 hashtab_obstack_allocate,
14693 dummy_obstack_deallocate);
14694
14695 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14696 if (*slot == NULL)
14697 *slot = ref_per_cu;
14698}
1c379e20 14699
f504f079
DE
14700/* Subroutine of dwarf2_mark to pass to htab_traverse.
14701 Set the mark field in every compilation unit in the
ae038cb0
DJ
14702 cache that we must keep because we are keeping CU. */
14703
10b3939b
DJ
14704static int
14705dwarf2_mark_helper (void **slot, void *data)
14706{
14707 struct dwarf2_per_cu_data *per_cu;
14708
14709 per_cu = (struct dwarf2_per_cu_data *) *slot;
14710 if (per_cu->cu->mark)
14711 return 1;
14712 per_cu->cu->mark = 1;
14713
14714 if (per_cu->cu->dependencies != NULL)
14715 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14716
14717 return 1;
14718}
14719
f504f079
DE
14720/* Set the mark field in CU and in every other compilation unit in the
14721 cache that we must keep because we are keeping CU. */
14722
ae038cb0
DJ
14723static void
14724dwarf2_mark (struct dwarf2_cu *cu)
14725{
14726 if (cu->mark)
14727 return;
14728 cu->mark = 1;
10b3939b
DJ
14729 if (cu->dependencies != NULL)
14730 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14731}
14732
14733static void
14734dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14735{
14736 while (per_cu)
14737 {
14738 per_cu->cu->mark = 0;
14739 per_cu = per_cu->cu->read_in_chain;
14740 }
72bf9492
DJ
14741}
14742
72bf9492
DJ
14743/* Trivial hash function for partial_die_info: the hash value of a DIE
14744 is its offset in .debug_info for this objfile. */
14745
14746static hashval_t
14747partial_die_hash (const void *item)
14748{
14749 const struct partial_die_info *part_die = item;
9a619af0 14750
72bf9492
DJ
14751 return part_die->offset;
14752}
14753
14754/* Trivial comparison function for partial_die_info structures: two DIEs
14755 are equal if they have the same offset. */
14756
14757static int
14758partial_die_eq (const void *item_lhs, const void *item_rhs)
14759{
14760 const struct partial_die_info *part_die_lhs = item_lhs;
14761 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14762
72bf9492
DJ
14763 return part_die_lhs->offset == part_die_rhs->offset;
14764}
14765
ae038cb0
DJ
14766static struct cmd_list_element *set_dwarf2_cmdlist;
14767static struct cmd_list_element *show_dwarf2_cmdlist;
14768
14769static void
14770set_dwarf2_cmd (char *args, int from_tty)
14771{
14772 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14773}
14774
14775static void
14776show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14777{
ae038cb0
DJ
14778 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14779}
14780
dce234bc
PP
14781/* If section described by INFO was mmapped, munmap it now. */
14782
14783static void
14784munmap_section_buffer (struct dwarf2_section_info *info)
14785{
14786 if (info->was_mmapped)
14787 {
14788#ifdef HAVE_MMAP
14789 intptr_t begin = (intptr_t) info->buffer;
14790 intptr_t map_begin = begin & ~(pagesize - 1);
14791 size_t map_length = info->size + begin - map_begin;
9a619af0 14792
dce234bc
PP
14793 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14794#else
14795 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14796 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14797#endif
14798 }
14799}
14800
14801/* munmap debug sections for OBJFILE, if necessary. */
14802
14803static void
c1bd65d0 14804dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14805{
14806 struct dwarf2_per_objfile *data = d;
9a619af0 14807
16be1145
DE
14808 /* This is sorted according to the order they're defined in to make it easier
14809 to keep in sync. */
dce234bc
PP
14810 munmap_section_buffer (&data->info);
14811 munmap_section_buffer (&data->abbrev);
14812 munmap_section_buffer (&data->line);
16be1145 14813 munmap_section_buffer (&data->loc);
dce234bc 14814 munmap_section_buffer (&data->macinfo);
16be1145 14815 munmap_section_buffer (&data->str);
dce234bc 14816 munmap_section_buffer (&data->ranges);
16be1145 14817 munmap_section_buffer (&data->types);
dce234bc
PP
14818 munmap_section_buffer (&data->frame);
14819 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14820 munmap_section_buffer (&data->gdb_index);
14821}
14822
14823\f
14824
14825/* The contents of the hash table we create when building the string
14826 table. */
14827struct strtab_entry
14828{
14829 offset_type offset;
14830 const char *str;
14831};
14832
14833/* Hash function for a strtab_entry. */
b89be57b 14834
9291a0cd
TT
14835static hashval_t
14836hash_strtab_entry (const void *e)
14837{
14838 const struct strtab_entry *entry = e;
14839 return mapped_index_string_hash (entry->str);
14840}
14841
14842/* Equality function for a strtab_entry. */
b89be57b 14843
9291a0cd
TT
14844static int
14845eq_strtab_entry (const void *a, const void *b)
14846{
14847 const struct strtab_entry *ea = a;
14848 const struct strtab_entry *eb = b;
14849 return !strcmp (ea->str, eb->str);
14850}
14851
14852/* Create a strtab_entry hash table. */
b89be57b 14853
9291a0cd
TT
14854static htab_t
14855create_strtab (void)
14856{
14857 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14858 xfree, xcalloc, xfree);
14859}
14860
14861/* Add a string to the constant pool. Return the string's offset in
14862 host order. */
b89be57b 14863
9291a0cd
TT
14864static offset_type
14865add_string (htab_t table, struct obstack *cpool, const char *str)
14866{
14867 void **slot;
14868 struct strtab_entry entry;
14869 struct strtab_entry *result;
14870
14871 entry.str = str;
14872 slot = htab_find_slot (table, &entry, INSERT);
14873 if (*slot)
14874 result = *slot;
14875 else
14876 {
14877 result = XNEW (struct strtab_entry);
14878 result->offset = obstack_object_size (cpool);
14879 result->str = str;
14880 obstack_grow_str0 (cpool, str);
14881 *slot = result;
14882 }
14883 return result->offset;
14884}
14885
14886/* An entry in the symbol table. */
14887struct symtab_index_entry
14888{
14889 /* The name of the symbol. */
14890 const char *name;
14891 /* The offset of the name in the constant pool. */
14892 offset_type index_offset;
14893 /* A sorted vector of the indices of all the CUs that hold an object
14894 of this name. */
14895 VEC (offset_type) *cu_indices;
14896};
14897
14898/* The symbol table. This is a power-of-2-sized hash table. */
14899struct mapped_symtab
14900{
14901 offset_type n_elements;
14902 offset_type size;
14903 struct symtab_index_entry **data;
14904};
14905
14906/* Hash function for a symtab_index_entry. */
b89be57b 14907
9291a0cd
TT
14908static hashval_t
14909hash_symtab_entry (const void *e)
14910{
14911 const struct symtab_index_entry *entry = e;
14912 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14913 sizeof (offset_type) * VEC_length (offset_type,
14914 entry->cu_indices),
14915 0);
14916}
14917
14918/* Equality function for a symtab_index_entry. */
b89be57b 14919
9291a0cd
TT
14920static int
14921eq_symtab_entry (const void *a, const void *b)
14922{
14923 const struct symtab_index_entry *ea = a;
14924 const struct symtab_index_entry *eb = b;
14925 int len = VEC_length (offset_type, ea->cu_indices);
14926 if (len != VEC_length (offset_type, eb->cu_indices))
14927 return 0;
14928 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14929 VEC_address (offset_type, eb->cu_indices),
14930 sizeof (offset_type) * len);
14931}
14932
14933/* Destroy a symtab_index_entry. */
b89be57b 14934
9291a0cd
TT
14935static void
14936delete_symtab_entry (void *p)
14937{
14938 struct symtab_index_entry *entry = p;
14939 VEC_free (offset_type, entry->cu_indices);
14940 xfree (entry);
14941}
14942
14943/* Create a hash table holding symtab_index_entry objects. */
b89be57b 14944
9291a0cd 14945static htab_t
3876f04e 14946create_symbol_hash_table (void)
9291a0cd
TT
14947{
14948 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14949 delete_symtab_entry, xcalloc, xfree);
14950}
14951
14952/* Create a new mapped symtab object. */
b89be57b 14953
9291a0cd
TT
14954static struct mapped_symtab *
14955create_mapped_symtab (void)
14956{
14957 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14958 symtab->n_elements = 0;
14959 symtab->size = 1024;
14960 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14961 return symtab;
14962}
14963
14964/* Destroy a mapped_symtab. */
b89be57b 14965
9291a0cd
TT
14966static void
14967cleanup_mapped_symtab (void *p)
14968{
14969 struct mapped_symtab *symtab = p;
14970 /* The contents of the array are freed when the other hash table is
14971 destroyed. */
14972 xfree (symtab->data);
14973 xfree (symtab);
14974}
14975
14976/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14977 the slot. */
b89be57b 14978
9291a0cd
TT
14979static struct symtab_index_entry **
14980find_slot (struct mapped_symtab *symtab, const char *name)
14981{
14982 offset_type index, step, hash = mapped_index_string_hash (name);
14983
14984 index = hash & (symtab->size - 1);
14985 step = ((hash * 17) & (symtab->size - 1)) | 1;
14986
14987 for (;;)
14988 {
14989 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14990 return &symtab->data[index];
14991 index = (index + step) & (symtab->size - 1);
14992 }
14993}
14994
14995/* Expand SYMTAB's hash table. */
b89be57b 14996
9291a0cd
TT
14997static void
14998hash_expand (struct mapped_symtab *symtab)
14999{
15000 offset_type old_size = symtab->size;
15001 offset_type i;
15002 struct symtab_index_entry **old_entries = symtab->data;
15003
15004 symtab->size *= 2;
15005 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15006
15007 for (i = 0; i < old_size; ++i)
15008 {
15009 if (old_entries[i])
15010 {
15011 struct symtab_index_entry **slot = find_slot (symtab,
15012 old_entries[i]->name);
15013 *slot = old_entries[i];
15014 }
15015 }
15016
15017 xfree (old_entries);
15018}
15019
15020/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15021 is the index of the CU in which the symbol appears. */
b89be57b 15022
9291a0cd
TT
15023static void
15024add_index_entry (struct mapped_symtab *symtab, const char *name,
15025 offset_type cu_index)
15026{
15027 struct symtab_index_entry **slot;
15028
15029 ++symtab->n_elements;
15030 if (4 * symtab->n_elements / 3 >= symtab->size)
15031 hash_expand (symtab);
15032
15033 slot = find_slot (symtab, name);
15034 if (!*slot)
15035 {
15036 *slot = XNEW (struct symtab_index_entry);
15037 (*slot)->name = name;
15038 (*slot)->cu_indices = NULL;
15039 }
15040 /* Don't push an index twice. Due to how we add entries we only
15041 have to check the last one. */
15042 if (VEC_empty (offset_type, (*slot)->cu_indices)
15043 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15044 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15045}
15046
15047/* Add a vector of indices to the constant pool. */
b89be57b 15048
9291a0cd 15049static offset_type
3876f04e 15050add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15051 struct symtab_index_entry *entry)
15052{
15053 void **slot;
15054
3876f04e 15055 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15056 if (!*slot)
15057 {
15058 offset_type len = VEC_length (offset_type, entry->cu_indices);
15059 offset_type val = MAYBE_SWAP (len);
15060 offset_type iter;
15061 int i;
15062
15063 *slot = entry;
15064 entry->index_offset = obstack_object_size (cpool);
15065
15066 obstack_grow (cpool, &val, sizeof (val));
15067 for (i = 0;
15068 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15069 ++i)
15070 {
15071 val = MAYBE_SWAP (iter);
15072 obstack_grow (cpool, &val, sizeof (val));
15073 }
15074 }
15075 else
15076 {
15077 struct symtab_index_entry *old_entry = *slot;
15078 entry->index_offset = old_entry->index_offset;
15079 entry = old_entry;
15080 }
15081 return entry->index_offset;
15082}
15083
15084/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15085 constant pool entries going into the obstack CPOOL. */
b89be57b 15086
9291a0cd
TT
15087static void
15088write_hash_table (struct mapped_symtab *symtab,
15089 struct obstack *output, struct obstack *cpool)
15090{
15091 offset_type i;
3876f04e 15092 htab_t symbol_hash_table;
9291a0cd
TT
15093 htab_t str_table;
15094
3876f04e 15095 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15096 str_table = create_strtab ();
3876f04e 15097
9291a0cd
TT
15098 /* We add all the index vectors to the constant pool first, to
15099 ensure alignment is ok. */
15100 for (i = 0; i < symtab->size; ++i)
15101 {
15102 if (symtab->data[i])
3876f04e 15103 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15104 }
15105
15106 /* Now write out the hash table. */
15107 for (i = 0; i < symtab->size; ++i)
15108 {
15109 offset_type str_off, vec_off;
15110
15111 if (symtab->data[i])
15112 {
15113 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15114 vec_off = symtab->data[i]->index_offset;
15115 }
15116 else
15117 {
15118 /* While 0 is a valid constant pool index, it is not valid
15119 to have 0 for both offsets. */
15120 str_off = 0;
15121 vec_off = 0;
15122 }
15123
15124 str_off = MAYBE_SWAP (str_off);
15125 vec_off = MAYBE_SWAP (vec_off);
15126
15127 obstack_grow (output, &str_off, sizeof (str_off));
15128 obstack_grow (output, &vec_off, sizeof (vec_off));
15129 }
15130
15131 htab_delete (str_table);
3876f04e 15132 htab_delete (symbol_hash_table);
9291a0cd
TT
15133}
15134
15135/* Write an address entry to ADDR_OBSTACK. The addresses are taken
15136 from PST; CU_INDEX is the index of the CU in the vector of all
15137 CUs. */
b89be57b 15138
9291a0cd
TT
15139static void
15140add_address_entry (struct objfile *objfile,
15141 struct obstack *addr_obstack, struct partial_symtab *pst,
15142 unsigned int cu_index)
15143{
15144 offset_type offset;
15145 char addr[8];
15146 CORE_ADDR baseaddr;
15147
1fd400ff
TT
15148 /* Don't bother recording empty ranges. */
15149 if (pst->textlow == pst->texthigh)
15150 return;
15151
9291a0cd
TT
15152 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15153
15154 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15155 obstack_grow (addr_obstack, addr, 8);
15156 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15157 obstack_grow (addr_obstack, addr, 8);
15158 offset = MAYBE_SWAP (cu_index);
15159 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15160}
15161
15162/* Add a list of partial symbols to SYMTAB. */
b89be57b 15163
9291a0cd
TT
15164static void
15165write_psymbols (struct mapped_symtab *symtab,
987d643c 15166 htab_t psyms_seen,
9291a0cd
TT
15167 struct partial_symbol **psymp,
15168 int count,
987d643c
TT
15169 offset_type cu_index,
15170 int is_static)
9291a0cd
TT
15171{
15172 for (; count-- > 0; ++psymp)
15173 {
987d643c
TT
15174 void **slot, *lookup;
15175
9291a0cd
TT
15176 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15177 error (_("Ada is not currently supported by the index"));
987d643c
TT
15178
15179 /* We only want to add a given psymbol once. However, we also
15180 want to account for whether it is global or static. So, we
15181 may add it twice, using slightly different values. */
15182 if (is_static)
15183 {
15184 uintptr_t val = 1 | (uintptr_t) *psymp;
15185
15186 lookup = (void *) val;
15187 }
15188 else
15189 lookup = *psymp;
15190
15191 /* Only add a given psymbol once. */
15192 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15193 if (!*slot)
15194 {
15195 *slot = lookup;
15196 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15197 }
9291a0cd
TT
15198 }
15199}
15200
15201/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15202 exception if there is an error. */
b89be57b 15203
9291a0cd
TT
15204static void
15205write_obstack (FILE *file, struct obstack *obstack)
15206{
15207 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15208 file)
15209 != obstack_object_size (obstack))
15210 error (_("couldn't data write to file"));
15211}
15212
15213/* Unlink a file if the argument is not NULL. */
b89be57b 15214
9291a0cd
TT
15215static void
15216unlink_if_set (void *p)
15217{
15218 char **filename = p;
15219 if (*filename)
15220 unlink (*filename);
15221}
15222
1fd400ff
TT
15223/* A helper struct used when iterating over debug_types. */
15224struct signatured_type_index_data
15225{
15226 struct objfile *objfile;
15227 struct mapped_symtab *symtab;
15228 struct obstack *types_list;
987d643c 15229 htab_t psyms_seen;
1fd400ff
TT
15230 int cu_index;
15231};
15232
15233/* A helper function that writes a single signatured_type to an
15234 obstack. */
b89be57b 15235
1fd400ff
TT
15236static int
15237write_one_signatured_type (void **slot, void *d)
15238{
15239 struct signatured_type_index_data *info = d;
15240 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15241 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15242 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15243 gdb_byte val[8];
15244
15245 write_psymbols (info->symtab,
987d643c 15246 info->psyms_seen,
1fd400ff 15247 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15248 psymtab->n_global_syms, info->cu_index,
15249 0);
1fd400ff 15250 write_psymbols (info->symtab,
987d643c 15251 info->psyms_seen,
1fd400ff 15252 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15253 psymtab->n_static_syms, info->cu_index,
15254 1);
1fd400ff
TT
15255
15256 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15257 obstack_grow (info->types_list, val, 8);
15258 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15259 obstack_grow (info->types_list, val, 8);
15260 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15261 obstack_grow (info->types_list, val, 8);
15262
15263 ++info->cu_index;
15264
15265 return 1;
15266}
15267
987d643c
TT
15268/* A cleanup function for an htab_t. */
15269
15270static void
15271cleanup_htab (void *arg)
15272{
15273 htab_delete (arg);
15274}
15275
9291a0cd 15276/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15277
9291a0cd
TT
15278static void
15279write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15280{
15281 struct cleanup *cleanup;
15282 char *filename, *cleanup_filename;
1fd400ff
TT
15283 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15284 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15285 int i;
15286 FILE *out_file;
15287 struct mapped_symtab *symtab;
15288 offset_type val, size_of_contents, total_len;
15289 struct stat st;
15290 char buf[8];
987d643c 15291 htab_t psyms_seen;
9291a0cd
TT
15292
15293 if (!objfile->psymtabs)
15294 return;
15295 if (dwarf2_per_objfile->using_index)
15296 error (_("Cannot use an index to create the index"));
15297
15298 if (stat (objfile->name, &st) < 0)
15299 perror_with_name (_("Could not stat"));
15300
15301 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15302 INDEX_SUFFIX, (char *) NULL);
15303 cleanup = make_cleanup (xfree, filename);
15304
15305 out_file = fopen (filename, "wb");
15306 if (!out_file)
15307 error (_("Can't open `%s' for writing"), filename);
15308
15309 cleanup_filename = filename;
15310 make_cleanup (unlink_if_set, &cleanup_filename);
15311
15312 symtab = create_mapped_symtab ();
15313 make_cleanup (cleanup_mapped_symtab, symtab);
15314
15315 obstack_init (&addr_obstack);
15316 make_cleanup_obstack_free (&addr_obstack);
15317
15318 obstack_init (&cu_list);
15319 make_cleanup_obstack_free (&cu_list);
15320
1fd400ff
TT
15321 obstack_init (&types_cu_list);
15322 make_cleanup_obstack_free (&types_cu_list);
15323
987d643c
TT
15324 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15325 NULL, xcalloc, xfree);
15326 make_cleanup (cleanup_htab, psyms_seen);
15327
1fd400ff
TT
15328 /* The list is already sorted, so we don't need to do additional
15329 work here. Also, the debug_types entries do not appear in
15330 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15331 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15332 {
e254ef6a
DE
15333 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15334 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15335 gdb_byte val[8];
15336
15337 write_psymbols (symtab,
987d643c 15338 psyms_seen,
9291a0cd 15339 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15340 psymtab->n_global_syms, i,
15341 0);
9291a0cd 15342 write_psymbols (symtab,
987d643c 15343 psyms_seen,
9291a0cd 15344 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15345 psymtab->n_static_syms, i,
15346 1);
9291a0cd
TT
15347
15348 add_address_entry (objfile, &addr_obstack, psymtab, i);
15349
e254ef6a 15350 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15351 obstack_grow (&cu_list, val, 8);
e254ef6a 15352 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15353 obstack_grow (&cu_list, val, 8);
15354 }
15355
1fd400ff
TT
15356 /* Write out the .debug_type entries, if any. */
15357 if (dwarf2_per_objfile->signatured_types)
15358 {
15359 struct signatured_type_index_data sig_data;
15360
15361 sig_data.objfile = objfile;
15362 sig_data.symtab = symtab;
15363 sig_data.types_list = &types_cu_list;
987d643c 15364 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15365 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15366 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15367 write_one_signatured_type, &sig_data);
15368 }
15369
9291a0cd
TT
15370 obstack_init (&constant_pool);
15371 make_cleanup_obstack_free (&constant_pool);
15372 obstack_init (&symtab_obstack);
15373 make_cleanup_obstack_free (&symtab_obstack);
15374 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15375
15376 obstack_init (&contents);
15377 make_cleanup_obstack_free (&contents);
1fd400ff 15378 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15379 total_len = size_of_contents;
15380
15381 /* The version number. */
987d643c 15382 val = MAYBE_SWAP (3);
9291a0cd
TT
15383 obstack_grow (&contents, &val, sizeof (val));
15384
15385 /* The offset of the CU list from the start of the file. */
15386 val = MAYBE_SWAP (total_len);
15387 obstack_grow (&contents, &val, sizeof (val));
15388 total_len += obstack_object_size (&cu_list);
15389
1fd400ff
TT
15390 /* The offset of the types CU list from the start of the file. */
15391 val = MAYBE_SWAP (total_len);
15392 obstack_grow (&contents, &val, sizeof (val));
15393 total_len += obstack_object_size (&types_cu_list);
15394
9291a0cd
TT
15395 /* The offset of the address table from the start of the file. */
15396 val = MAYBE_SWAP (total_len);
15397 obstack_grow (&contents, &val, sizeof (val));
15398 total_len += obstack_object_size (&addr_obstack);
15399
15400 /* The offset of the symbol table from the start of the file. */
15401 val = MAYBE_SWAP (total_len);
15402 obstack_grow (&contents, &val, sizeof (val));
15403 total_len += obstack_object_size (&symtab_obstack);
15404
15405 /* The offset of the constant pool from the start of the file. */
15406 val = MAYBE_SWAP (total_len);
15407 obstack_grow (&contents, &val, sizeof (val));
15408 total_len += obstack_object_size (&constant_pool);
15409
15410 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15411
15412 write_obstack (out_file, &contents);
15413 write_obstack (out_file, &cu_list);
1fd400ff 15414 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15415 write_obstack (out_file, &addr_obstack);
15416 write_obstack (out_file, &symtab_obstack);
15417 write_obstack (out_file, &constant_pool);
15418
15419 fclose (out_file);
15420
15421 /* We want to keep the file, so we set cleanup_filename to NULL
15422 here. See unlink_if_set. */
15423 cleanup_filename = NULL;
15424
15425 do_cleanups (cleanup);
15426}
15427
15428/* The mapped index file format is designed to be directly mmap()able
15429 on any architecture. In most cases, a datum is represented using a
15430 little-endian 32-bit integer value, called an offset_type. Big
15431 endian machines must byte-swap the values before using them.
15432 Exceptions to this rule are noted. The data is laid out such that
15433 alignment is always respected.
15434
15435 A mapped index consists of several sections.
15436
15437 1. The file header. This is a sequence of values, of offset_type
15438 unless otherwise noted:
987d643c
TT
15439
15440 [0] The version number, currently 3. Versions 1 and 2 are
15441 obsolete.
9291a0cd 15442 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15443 [2] The offset, from the start of the file, of the types CU list.
15444 Note that this section can be empty, in which case this offset will
15445 be equal to the next offset.
15446 [3] The offset, from the start of the file, of the address section.
15447 [4] The offset, from the start of the file, of the symbol table.
15448 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15449
15450 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15451 little-endian values, sorted by the CU offset. The first element
15452 in each pair is the offset of a CU in the .debug_info section. The
15453 second element in each pair is the length of that CU. References
15454 to a CU elsewhere in the map are done using a CU index, which is
15455 just the 0-based index into this table. Note that if there are
15456 type CUs, then conceptually CUs and type CUs form a single list for
15457 the purposes of CU indices.
15458
987d643c
TT
15459 3. The types CU list. This is a sequence of triplets of 64-bit
15460 little-endian values. In a triplet, the first value is the CU
15461 offset, the second value is the type offset in the CU, and the
15462 third value is the type signature. The types CU list is not
15463 sorted.
9291a0cd 15464
987d643c 15465 4. The address section. The address section consists of a sequence
9291a0cd
TT
15466 of address entries. Each address entry has three elements.
15467 [0] The low address. This is a 64-bit little-endian value.
15468 [1] The high address. This is a 64-bit little-endian value.
148c11bf 15469 Like DW_AT_high_pc, the value is one byte beyond the end.
9291a0cd
TT
15470 [2] The CU index. This is an offset_type value.
15471
987d643c 15472 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15473 table is always a power of 2. The initial hash and the step are
15474 currently defined by the `find_slot' function.
15475
15476 Each slot in the hash table consists of a pair of offset_type
15477 values. The first value is the offset of the symbol's name in the
15478 constant pool. The second value is the offset of the CU vector in
15479 the constant pool.
15480
15481 If both values are 0, then this slot in the hash table is empty.
15482 This is ok because while 0 is a valid constant pool index, it
15483 cannot be a valid index for both a string and a CU vector.
15484
15485 A string in the constant pool is stored as a \0-terminated string,
15486 as you'd expect.
15487
15488 A CU vector in the constant pool is a sequence of offset_type
15489 values. The first value is the number of CU indices in the vector.
15490 Each subsequent value is the index of a CU in the CU list. This
15491 element in the hash table is used to indicate which CUs define the
15492 symbol.
15493
987d643c 15494 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15495 organized so that alignment is correct: CU vectors are stored
15496 first, followed by strings. */
11570e71 15497
9291a0cd
TT
15498static void
15499save_gdb_index_command (char *arg, int from_tty)
15500{
15501 struct objfile *objfile;
15502
15503 if (!arg || !*arg)
96d19272 15504 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15505
15506 ALL_OBJFILES (objfile)
15507 {
15508 struct stat st;
15509
15510 /* If the objfile does not correspond to an actual file, skip it. */
15511 if (stat (objfile->name, &st) < 0)
15512 continue;
15513
15514 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15515 if (dwarf2_per_objfile)
15516 {
15517 volatile struct gdb_exception except;
15518
15519 TRY_CATCH (except, RETURN_MASK_ERROR)
15520 {
15521 write_psymtabs_to_index (objfile, arg);
15522 }
15523 if (except.reason < 0)
15524 exception_fprintf (gdb_stderr, except,
15525 _("Error while writing index for `%s': "),
15526 objfile->name);
15527 }
15528 }
dce234bc
PP
15529}
15530
9291a0cd
TT
15531\f
15532
9eae7c52
TT
15533int dwarf2_always_disassemble;
15534
15535static void
15536show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15537 struct cmd_list_element *c, const char *value)
15538{
15539 fprintf_filtered (file, _("\
15540Whether to always disassemble DWARF expressions is %s.\n"),
15541 value);
15542}
15543
6502dd73
DJ
15544void _initialize_dwarf2_read (void);
15545
15546void
15547_initialize_dwarf2_read (void)
15548{
96d19272
JK
15549 struct cmd_list_element *c;
15550
dce234bc 15551 dwarf2_objfile_data_key
c1bd65d0 15552 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15553
1bedd215
AC
15554 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15555Set DWARF 2 specific variables.\n\
15556Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15557 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15558 0/*allow-unknown*/, &maintenance_set_cmdlist);
15559
1bedd215
AC
15560 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15561Show DWARF 2 specific variables\n\
15562Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15563 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15564 0/*allow-unknown*/, &maintenance_show_cmdlist);
15565
15566 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15567 &dwarf2_max_cache_age, _("\
15568Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15569Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15570A higher limit means that cached compilation units will be stored\n\
15571in memory longer, and more total memory will be used. Zero disables\n\
15572caching, which can slow down startup."),
2c5b56ce 15573 NULL,
920d2a44 15574 show_dwarf2_max_cache_age,
2c5b56ce 15575 &set_dwarf2_cmdlist,
ae038cb0 15576 &show_dwarf2_cmdlist);
d97bc12b 15577
9eae7c52
TT
15578 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15579 &dwarf2_always_disassemble, _("\
15580Set whether `info address' always disassembles DWARF expressions."), _("\
15581Show whether `info address' always disassembles DWARF expressions."), _("\
15582When enabled, DWARF expressions are always printed in an assembly-like\n\
15583syntax. When disabled, expressions will be printed in a more\n\
15584conversational style, when possible."),
15585 NULL,
15586 show_dwarf2_always_disassemble,
15587 &set_dwarf2_cmdlist,
15588 &show_dwarf2_cmdlist);
15589
d97bc12b
DE
15590 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15591Set debugging of the dwarf2 DIE reader."), _("\
15592Show debugging of the dwarf2 DIE reader."), _("\
15593When enabled (non-zero), DIEs are dumped after they are read in.\n\
15594The value is the maximum depth to print."),
15595 NULL,
15596 NULL,
15597 &setdebuglist, &showdebuglist);
9291a0cd 15598
96d19272 15599 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71
DE
15600 _("\
15601Save a .gdb-index file.\n\
15602Usage: save gdb-index DIRECTORY"),
96d19272
JK
15603 &save_cmdlist);
15604 set_cmd_completer (c, filename_completer);
6502dd73 15605}
This page took 2.204542 seconds and 4 git commands to generate.