* dwarf2read.c (dwarf2_attach_fields_to_type): Don't set
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
673bfd45
DE
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
6502dd73
DJ
226};
227
228static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
229
230/* names of the debugging sections */
231
233a11ab
CS
232/* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235#define INFO_SECTION "debug_info"
236#define ABBREV_SECTION "debug_abbrev"
237#define LINE_SECTION "debug_line"
233a11ab
CS
238#define LOC_SECTION "debug_loc"
239#define MACINFO_SECTION "debug_macinfo"
240#define STR_SECTION "debug_str"
241#define RANGES_SECTION "debug_ranges"
348e048f 242#define TYPES_SECTION "debug_types"
233a11ab
CS
243#define FRAME_SECTION "debug_frame"
244#define EH_FRAME_SECTION "eh_frame"
9291a0cd 245#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
246
247/* local data types */
248
57349743
JB
249/* We hold several abbreviation tables in memory at the same time. */
250#ifndef ABBREV_HASH_SIZE
251#define ABBREV_HASH_SIZE 121
252#endif
253
107d2387
AC
254/* The data in a compilation unit header, after target2host
255 translation, looks like this. */
c906108c 256struct comp_unit_head
a738430d 257{
c764a876 258 unsigned int length;
a738430d 259 short version;
a738430d
MK
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
9cbfa09e 262 unsigned int abbrev_offset;
57349743 263
a738430d
MK
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
57349743 266
a738430d
MK
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
57349743 269
a738430d
MK
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
57349743 273
d00adf39
DE
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
a738430d 277};
c906108c 278
3da10d80
KS
279/* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281struct delayed_method_info
282{
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297};
298
299typedef struct delayed_method_info delayed_method_info;
300DEF_VEC_O (delayed_method_info);
301
e7c27a73
DJ
302/* Internal state when decoding a particular compilation unit. */
303struct dwarf2_cu
304{
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
d00adf39 308 /* The header of the compilation unit. */
e7c27a73 309 struct comp_unit_head header;
e142c38c 310
d00adf39
DE
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
e142c38c
DJ
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492
DJ
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
10b3939b 361 /* A hash table of die offsets for following references. */
51545339 362 htab_t die_hash;
10b3939b
DJ
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
cb1df416
DJ
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
3da10d80
KS
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
ae038cb0
DJ
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
72bf9492
DJ
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
e7c27a73
DJ
391};
392
9291a0cd
TT
393/* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396struct dwarf2_per_cu_quick_data
397{
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417};
418
10b3939b
DJ
419/* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
ae038cb0
DJ
425struct dwarf2_per_cu_data
426{
348e048f 427 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 428 bytes should suffice to store the length of any compilation unit
45452591
DE
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
c764a876 432 unsigned int offset;
348e048f 433 unsigned int length : 29;
ae038cb0
DJ
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
c764a876 437 unsigned int queued : 1;
ae038cb0 438
5afb4e99
DJ
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
348e048f
DE
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
17ea53c3
JK
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
ae038cb0 451 struct dwarf2_cu *cu;
1c379e20 452
9291a0cd
TT
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
ae038cb0
DJ
468};
469
348e048f
DE
470/* Entry in the signatured_types hash table. */
471
472struct signatured_type
473{
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484};
485
93311388
DE
486/* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492struct die_reader_specs
493{
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503};
504
debd256d
JB
505/* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508struct line_header
509{
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
2dc7f7b3 514 unsigned char maximum_ops_per_instruction;
debd256d
JB
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
c906108c 538 {
debd256d
JB
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
aaa75496 543 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 544 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
545 } *file_names;
546
547 /* The start and end of the statement program following this
6502dd73 548 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 549 gdb_byte *statement_program_start, *statement_program_end;
debd256d 550};
c906108c
SS
551
552/* When we construct a partial symbol table entry we only
553 need this much information. */
554struct partial_die_info
555 {
72bf9492 556 /* Offset of this DIE. */
c906108c 557 unsigned int offset;
72bf9492
DJ
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
72bf9492
DJ
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
fa4028e9
JB
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
98bfdba5
PA
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
72bf9492 580 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 581 sometimes a default name for unnamed DIEs. */
c906108c 582 char *name;
72bf9492
DJ
583
584 /* The scope to prepend to our children. This is generally
585 allocated on the comp_unit_obstack, so will disappear
586 when this compilation unit leaves the cache. */
587 char *scope;
588
589 /* The location description associated with this DIE, if any. */
590 struct dwarf_block *locdesc;
591
592 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
593 CORE_ADDR lowpc;
594 CORE_ADDR highpc;
72bf9492 595
93311388 596 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 597 DW_AT_sibling, if any. */
fe1b8b76 598 gdb_byte *sibling;
72bf9492
DJ
599
600 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
601 DW_AT_specification (or DW_AT_abstract_origin or
602 DW_AT_extension). */
603 unsigned int spec_offset;
604
605 /* Pointers to this DIE's parent, first child, and next sibling,
606 if any. */
607 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
608 };
609
610/* This data structure holds the information of an abbrev. */
611struct abbrev_info
612 {
613 unsigned int number; /* number identifying abbrev */
614 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
615 unsigned short has_children; /* boolean */
616 unsigned short num_attrs; /* number of attributes */
c906108c
SS
617 struct attr_abbrev *attrs; /* an array of attribute descriptions */
618 struct abbrev_info *next; /* next in chain */
619 };
620
621struct attr_abbrev
622 {
9d25dd43
DE
623 ENUM_BITFIELD(dwarf_attribute) name : 16;
624 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
625 };
626
b60c80d6
DJ
627/* Attributes have a name and a value */
628struct attribute
629 {
9d25dd43 630 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
631 ENUM_BITFIELD(dwarf_form) form : 15;
632
633 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
634 field should be in u.str (existing only for DW_STRING) but it is kept
635 here for better struct attribute alignment. */
636 unsigned int string_is_canonical : 1;
637
b60c80d6
DJ
638 union
639 {
640 char *str;
641 struct dwarf_block *blk;
43bbcdc2
PH
642 ULONGEST unsnd;
643 LONGEST snd;
b60c80d6 644 CORE_ADDR addr;
348e048f 645 struct signatured_type *signatured_type;
b60c80d6
DJ
646 }
647 u;
648 };
649
c906108c
SS
650/* This data structure holds a complete die structure. */
651struct die_info
652 {
76815b17
DE
653 /* DWARF-2 tag for this DIE. */
654 ENUM_BITFIELD(dwarf_tag) tag : 16;
655
656 /* Number of attributes */
98bfdba5
PA
657 unsigned char num_attrs;
658
659 /* True if we're presently building the full type name for the
660 type derived from this DIE. */
661 unsigned char building_fullname : 1;
76815b17
DE
662
663 /* Abbrev number */
664 unsigned int abbrev;
665
93311388 666 /* Offset in .debug_info or .debug_types section. */
76815b17 667 unsigned int offset;
78ba4af6
JB
668
669 /* The dies in a compilation unit form an n-ary tree. PARENT
670 points to this die's parent; CHILD points to the first child of
671 this node; and all the children of a given node are chained
672 together via their SIBLING fields, terminated by a die whose
673 tag is zero. */
639d11d3
DC
674 struct die_info *child; /* Its first child, if any. */
675 struct die_info *sibling; /* Its next sibling, if any. */
676 struct die_info *parent; /* Its parent, if any. */
c906108c 677
b60c80d6
DJ
678 /* An array of attributes, with NUM_ATTRS elements. There may be
679 zero, but it's not common and zero-sized arrays are not
680 sufficiently portable C. */
681 struct attribute attrs[1];
c906108c
SS
682 };
683
5fb290d7
DJ
684struct function_range
685{
686 const char *name;
687 CORE_ADDR lowpc, highpc;
688 int seen_line;
689 struct function_range *next;
690};
691
c906108c
SS
692/* Get at parts of an attribute structure */
693
694#define DW_STRING(attr) ((attr)->u.str)
8285870a 695#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
696#define DW_UNSND(attr) ((attr)->u.unsnd)
697#define DW_BLOCK(attr) ((attr)->u.blk)
698#define DW_SND(attr) ((attr)->u.snd)
699#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 700#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
701
702/* Blocks are a bunch of untyped bytes. */
703struct dwarf_block
704 {
705 unsigned int size;
fe1b8b76 706 gdb_byte *data;
c906108c
SS
707 };
708
c906108c
SS
709#ifndef ATTR_ALLOC_CHUNK
710#define ATTR_ALLOC_CHUNK 4
711#endif
712
c906108c
SS
713/* Allocate fields for structs, unions and enums in this size. */
714#ifndef DW_FIELD_ALLOC_CHUNK
715#define DW_FIELD_ALLOC_CHUNK 4
716#endif
717
c906108c
SS
718/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
719 but this would require a corresponding change in unpack_field_as_long
720 and friends. */
721static int bits_per_byte = 8;
722
723/* The routines that read and process dies for a C struct or C++ class
724 pass lists of data member fields and lists of member function fields
725 in an instance of a field_info structure, as defined below. */
726struct field_info
c5aa993b
JM
727 {
728 /* List of data member and baseclasses fields. */
729 struct nextfield
730 {
731 struct nextfield *next;
732 int accessibility;
733 int virtuality;
734 struct field field;
735 }
7d0ccb61 736 *fields, *baseclasses;
c906108c 737
7d0ccb61 738 /* Number of fields (including baseclasses). */
c5aa993b 739 int nfields;
c906108c 740
c5aa993b
JM
741 /* Number of baseclasses. */
742 int nbaseclasses;
c906108c 743
c5aa993b
JM
744 /* Set if the accesibility of one of the fields is not public. */
745 int non_public_fields;
c906108c 746
c5aa993b
JM
747 /* Member function fields array, entries are allocated in the order they
748 are encountered in the object file. */
749 struct nextfnfield
750 {
751 struct nextfnfield *next;
752 struct fn_field fnfield;
753 }
754 *fnfields;
c906108c 755
c5aa993b
JM
756 /* Member function fieldlist array, contains name of possibly overloaded
757 member function, number of overloaded member functions and a pointer
758 to the head of the member function field chain. */
759 struct fnfieldlist
760 {
761 char *name;
762 int length;
763 struct nextfnfield *head;
764 }
765 *fnfieldlists;
c906108c 766
c5aa993b
JM
767 /* Number of entries in the fnfieldlists array. */
768 int nfnfields;
98751a41
JK
769
770 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
771 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
772 struct typedef_field_list
773 {
774 struct typedef_field field;
775 struct typedef_field_list *next;
776 }
777 *typedef_field_list;
778 unsigned typedef_field_list_count;
c5aa993b 779 };
c906108c 780
10b3939b
DJ
781/* One item on the queue of compilation units to read in full symbols
782 for. */
783struct dwarf2_queue_item
784{
785 struct dwarf2_per_cu_data *per_cu;
786 struct dwarf2_queue_item *next;
787};
788
789/* The current queue. */
790static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
791
ae038cb0
DJ
792/* Loaded secondary compilation units are kept in memory until they
793 have not been referenced for the processing of this many
794 compilation units. Set this to zero to disable caching. Cache
795 sizes of up to at least twenty will improve startup time for
796 typical inter-CU-reference binaries, at an obvious memory cost. */
797static int dwarf2_max_cache_age = 5;
920d2a44
AC
798static void
799show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
800 struct cmd_list_element *c, const char *value)
801{
802 fprintf_filtered (file, _("\
803The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
804 value);
805}
806
ae038cb0 807
c906108c
SS
808/* Various complaints about symbol reading that don't abort the process */
809
4d3c2250
KB
810static void
811dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 812{
4d3c2250 813 complaint (&symfile_complaints,
e2e0b3e5 814 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
815}
816
25e43795
DJ
817static void
818dwarf2_debug_line_missing_file_complaint (void)
819{
820 complaint (&symfile_complaints,
821 _(".debug_line section has line data without a file"));
822}
823
59205f5a
JB
824static void
825dwarf2_debug_line_missing_end_sequence_complaint (void)
826{
827 complaint (&symfile_complaints,
828 _(".debug_line section has line program sequence without an end"));
829}
830
4d3c2250
KB
831static void
832dwarf2_complex_location_expr_complaint (void)
2e276125 833{
e2e0b3e5 834 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
835}
836
4d3c2250
KB
837static void
838dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
839 int arg3)
2e276125 840{
4d3c2250 841 complaint (&symfile_complaints,
e2e0b3e5 842 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
843 arg2, arg3);
844}
845
846static void
847dwarf2_macros_too_long_complaint (void)
2e276125 848{
4d3c2250 849 complaint (&symfile_complaints,
e2e0b3e5 850 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
851}
852
853static void
854dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 855{
4d3c2250 856 complaint (&symfile_complaints,
e2e0b3e5 857 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
858 arg1);
859}
860
861static void
862dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 863{
4d3c2250 864 complaint (&symfile_complaints,
e2e0b3e5 865 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 866}
c906108c 867
c906108c
SS
868/* local function prototypes */
869
4efb68b1 870static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 871
aaa75496
JB
872static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873 struct objfile *);
874
c67a9c90 875static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 876
72bf9492
DJ
877static void scan_partial_symbols (struct partial_die_info *,
878 CORE_ADDR *, CORE_ADDR *,
5734ee8b 879 int, struct dwarf2_cu *);
c906108c 880
72bf9492
DJ
881static void add_partial_symbol (struct partial_die_info *,
882 struct dwarf2_cu *);
63d06c5c 883
72bf9492
DJ
884static void add_partial_namespace (struct partial_die_info *pdi,
885 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 886 int need_pc, struct dwarf2_cu *cu);
63d06c5c 887
5d7cb8df
JK
888static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
889 CORE_ADDR *highpc, int need_pc,
890 struct dwarf2_cu *cu);
891
72bf9492
DJ
892static void add_partial_enumeration (struct partial_die_info *enum_pdi,
893 struct dwarf2_cu *cu);
91c24f0a 894
bc30ff58
JB
895static void add_partial_subprogram (struct partial_die_info *pdi,
896 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 897 int need_pc, struct dwarf2_cu *cu);
bc30ff58 898
fe1b8b76 899static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
900 gdb_byte *buffer, gdb_byte *info_ptr,
901 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 902
a14ed312 903static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 904
a14ed312 905static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 906
e7c27a73 907static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 908
f3dd6933 909static void dwarf2_free_abbrev_table (void *);
c906108c 910
fe1b8b76 911static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 912 struct dwarf2_cu *);
72bf9492 913
57349743 914static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 915 struct dwarf2_cu *);
c906108c 916
93311388
DE
917static struct partial_die_info *load_partial_dies (bfd *,
918 gdb_byte *, gdb_byte *,
919 int, struct dwarf2_cu *);
72bf9492 920
fe1b8b76 921static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
922 struct abbrev_info *abbrev,
923 unsigned int, bfd *,
924 gdb_byte *, gdb_byte *,
925 struct dwarf2_cu *);
c906108c 926
c764a876 927static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 928 struct dwarf2_cu *);
72bf9492
DJ
929
930static void fixup_partial_die (struct partial_die_info *,
931 struct dwarf2_cu *);
932
fe1b8b76
JB
933static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
934 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 935
fe1b8b76
JB
936static gdb_byte *read_attribute_value (struct attribute *, unsigned,
937 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 938
fe1b8b76 939static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 940
fe1b8b76 941static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 942
fe1b8b76 943static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 944
fe1b8b76 945static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 946
93311388 947static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 948
fe1b8b76 949static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 950 unsigned int *);
c906108c 951
c764a876
DE
952static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
953
954static LONGEST read_checked_initial_length_and_offset
955 (bfd *, gdb_byte *, const struct comp_unit_head *,
956 unsigned int *, unsigned int *);
613e1657 957
fe1b8b76 958static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
959 unsigned int *);
960
961static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 962
fe1b8b76 963static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 964
9b1c24c8 965static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 966
fe1b8b76
JB
967static char *read_indirect_string (bfd *, gdb_byte *,
968 const struct comp_unit_head *,
969 unsigned int *);
4bdf3d34 970
fe1b8b76 971static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 972
fe1b8b76 973static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 974
fe1b8b76 975static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 976
e142c38c 977static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 978
e142c38c
DJ
979static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
980 struct dwarf2_cu *);
c906108c 981
348e048f
DE
982static struct attribute *dwarf2_attr_no_follow (struct die_info *,
983 unsigned int,
984 struct dwarf2_cu *);
985
05cf31d1
JB
986static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
987 struct dwarf2_cu *cu);
988
e142c38c 989static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 990
e142c38c 991static struct die_info *die_specification (struct die_info *die,
f2f0e013 992 struct dwarf2_cu **);
63d06c5c 993
debd256d
JB
994static void free_line_header (struct line_header *lh);
995
aaa75496
JB
996static void add_file_name (struct line_header *, char *, unsigned int,
997 unsigned int, unsigned int);
998
debd256d
JB
999static struct line_header *(dwarf_decode_line_header
1000 (unsigned int offset,
e7c27a73 1001 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
1002
1003static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 1004 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1005
4f1520fb 1006static void dwarf2_start_subfile (char *, char *, char *);
c906108c 1007
a14ed312 1008static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1009 struct dwarf2_cu *);
c906108c 1010
34eaf542
TT
1011static struct symbol *new_symbol_full (struct die_info *, struct type *,
1012 struct dwarf2_cu *, struct symbol *);
1013
a14ed312 1014static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1015 struct dwarf2_cu *);
c906108c 1016
98bfdba5
PA
1017static void dwarf2_const_value_attr (struct attribute *attr,
1018 struct type *type,
1019 const char *name,
1020 struct obstack *obstack,
1021 struct dwarf2_cu *cu, long *value,
1022 gdb_byte **bytes,
1023 struct dwarf2_locexpr_baton **baton);
2df3850c 1024
e7c27a73 1025static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1026
b4ba55a1
JB
1027static int need_gnat_info (struct dwarf2_cu *);
1028
1029static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1030
1031static void set_descriptive_type (struct type *, struct die_info *,
1032 struct dwarf2_cu *);
1033
e7c27a73
DJ
1034static struct type *die_containing_type (struct die_info *,
1035 struct dwarf2_cu *);
c906108c 1036
673bfd45
DE
1037static struct type *lookup_die_type (struct die_info *, struct attribute *,
1038 struct dwarf2_cu *);
c906108c 1039
f792889a 1040static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1041
673bfd45
DE
1042static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1043
086ed43d 1044static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1045
6e70227d 1046static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1047 const char *suffix, int physname,
1048 struct dwarf2_cu *cu);
63d06c5c 1049
e7c27a73 1050static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1051
348e048f
DE
1052static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1053
e7c27a73 1054static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1055
e7c27a73 1056static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1057
ff013f42
JK
1058static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1059 struct dwarf2_cu *, struct partial_symtab *);
1060
a14ed312 1061static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1062 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1063 struct partial_symtab *);
c906108c 1064
fae299cd
DC
1065static void get_scope_pc_bounds (struct die_info *,
1066 CORE_ADDR *, CORE_ADDR *,
1067 struct dwarf2_cu *);
1068
801e3a5b
JB
1069static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1070 CORE_ADDR, struct dwarf2_cu *);
1071
a14ed312 1072static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1073 struct dwarf2_cu *);
c906108c 1074
a14ed312 1075static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1076 struct type *, struct dwarf2_cu *);
c906108c 1077
a14ed312 1078static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1079 struct die_info *, struct type *,
e7c27a73 1080 struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1083 struct type *, struct dwarf2_cu *);
c906108c 1084
134d01f1 1085static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1086
e7c27a73 1087static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1088
e7c27a73 1089static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1090
5d7cb8df
JK
1091static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1092
27aa8d6a
SW
1093static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1094
f55ee35c
JK
1095static struct type *read_module_type (struct die_info *die,
1096 struct dwarf2_cu *cu);
1097
38d518c9 1098static const char *namespace_name (struct die_info *die,
e142c38c 1099 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1100
134d01f1 1101static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1102
e7c27a73 1103static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1104
6e70227d 1105static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1106 struct dwarf2_cu *);
1107
93311388 1108static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1109
93311388
DE
1110static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1111 gdb_byte *info_ptr,
d97bc12b
DE
1112 gdb_byte **new_info_ptr,
1113 struct die_info *parent);
1114
93311388
DE
1115static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1116 gdb_byte *info_ptr,
fe1b8b76 1117 gdb_byte **new_info_ptr,
639d11d3
DC
1118 struct die_info *parent);
1119
93311388
DE
1120static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1121 gdb_byte *info_ptr,
fe1b8b76 1122 gdb_byte **new_info_ptr,
639d11d3
DC
1123 struct die_info *parent);
1124
93311388
DE
1125static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1126 struct die_info **, gdb_byte *,
1127 int *);
1128
e7c27a73 1129static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1130
71c25dea
TT
1131static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1132 struct obstack *);
1133
e142c38c 1134static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1135
98bfdba5
PA
1136static const char *dwarf2_full_name (char *name,
1137 struct die_info *die,
1138 struct dwarf2_cu *cu);
1139
e142c38c 1140static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1141 struct dwarf2_cu **);
9219021c 1142
a14ed312 1143static char *dwarf_tag_name (unsigned int);
c906108c 1144
a14ed312 1145static char *dwarf_attr_name (unsigned int);
c906108c 1146
a14ed312 1147static char *dwarf_form_name (unsigned int);
c906108c 1148
a14ed312 1149static char *dwarf_bool_name (unsigned int);
c906108c 1150
a14ed312 1151static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1152
1153#if 0
a14ed312 1154static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1155#endif
1156
f9aca02d 1157static struct die_info *sibling_die (struct die_info *);
c906108c 1158
d97bc12b
DE
1159static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1160
1161static void dump_die_for_error (struct die_info *);
1162
1163static void dump_die_1 (struct ui_file *, int level, int max_level,
1164 struct die_info *);
c906108c 1165
d97bc12b 1166/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1167
51545339 1168static void store_in_ref_table (struct die_info *,
10b3939b 1169 struct dwarf2_cu *);
c906108c 1170
93311388
DE
1171static int is_ref_attr (struct attribute *);
1172
c764a876 1173static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1174
43bbcdc2 1175static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1176
348e048f
DE
1177static struct die_info *follow_die_ref_or_sig (struct die_info *,
1178 struct attribute *,
1179 struct dwarf2_cu **);
1180
10b3939b
DJ
1181static struct die_info *follow_die_ref (struct die_info *,
1182 struct attribute *,
f2f0e013 1183 struct dwarf2_cu **);
c906108c 1184
348e048f
DE
1185static struct die_info *follow_die_sig (struct die_info *,
1186 struct attribute *,
1187 struct dwarf2_cu **);
1188
1189static void read_signatured_type_at_offset (struct objfile *objfile,
1190 unsigned int offset);
1191
1192static void read_signatured_type (struct objfile *,
1193 struct signatured_type *type_sig);
1194
c906108c
SS
1195/* memory allocation interface */
1196
7b5a2f43 1197static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1198
f3dd6933 1199static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1200
b60c80d6 1201static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1202
e142c38c 1203static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1204
e142c38c
DJ
1205static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1206 struct dwarf2_cu *);
5fb290d7 1207
2e276125 1208static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1209 char *, bfd *, struct dwarf2_cu *);
2e276125 1210
8e19ed76
PS
1211static int attr_form_is_block (struct attribute *);
1212
3690dd37
JB
1213static int attr_form_is_section_offset (struct attribute *);
1214
1215static int attr_form_is_constant (struct attribute *);
1216
93e7bd98
DJ
1217static void dwarf2_symbol_mark_computed (struct attribute *attr,
1218 struct symbol *sym,
1219 struct dwarf2_cu *cu);
4c2df51b 1220
93311388
DE
1221static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1222 struct abbrev_info *abbrev,
1223 struct dwarf2_cu *cu);
4bb7a0a7 1224
72bf9492
DJ
1225static void free_stack_comp_unit (void *);
1226
72bf9492
DJ
1227static hashval_t partial_die_hash (const void *item);
1228
1229static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1230
ae038cb0 1231static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1232 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1233
1234static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1235 (unsigned int offset, struct objfile *objfile);
ae038cb0 1236
93311388
DE
1237static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1238
ae038cb0
DJ
1239static void free_one_comp_unit (void *);
1240
1241static void free_cached_comp_units (void *);
1242
1243static void age_cached_comp_units (void);
1244
1245static void free_one_cached_comp_unit (void *);
1246
f792889a
DJ
1247static struct type *set_die_type (struct die_info *, struct type *,
1248 struct dwarf2_cu *);
1c379e20 1249
ae038cb0
DJ
1250static void create_all_comp_units (struct objfile *);
1251
1fd400ff
TT
1252static int create_debug_types_hash_table (struct objfile *objfile);
1253
93311388
DE
1254static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1255 struct objfile *);
10b3939b
DJ
1256
1257static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1258
1259static void dwarf2_add_dependence (struct dwarf2_cu *,
1260 struct dwarf2_per_cu_data *);
1261
ae038cb0
DJ
1262static void dwarf2_mark (struct dwarf2_cu *);
1263
1264static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1265
673bfd45
DE
1266static struct type *get_die_type_at_offset (unsigned int,
1267 struct dwarf2_per_cu_data *per_cu);
1268
f792889a 1269static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1270
9291a0cd
TT
1271static void dwarf2_release_queue (void *dummy);
1272
1273static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1274 struct objfile *objfile);
1275
1276static void process_queue (struct objfile *objfile);
1277
1278static void find_file_and_directory (struct die_info *die,
1279 struct dwarf2_cu *cu,
1280 char **name, char **comp_dir);
1281
1282static char *file_full_name (int file, struct line_header *lh,
1283 const char *comp_dir);
1284
1285static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1286 gdb_byte *info_ptr,
1287 gdb_byte *buffer,
1288 unsigned int buffer_size,
1289 bfd *abfd);
1290
1291static void init_cu_die_reader (struct die_reader_specs *reader,
1292 struct dwarf2_cu *cu);
1293
673bfd45 1294static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1295
9291a0cd
TT
1296#if WORDS_BIGENDIAN
1297
1298/* Convert VALUE between big- and little-endian. */
1299static offset_type
1300byte_swap (offset_type value)
1301{
1302 offset_type result;
1303
1304 result = (value & 0xff) << 24;
1305 result |= (value & 0xff00) << 8;
1306 result |= (value & 0xff0000) >> 8;
1307 result |= (value & 0xff000000) >> 24;
1308 return result;
1309}
1310
1311#define MAYBE_SWAP(V) byte_swap (V)
1312
1313#else
1314#define MAYBE_SWAP(V) (V)
1315#endif /* WORDS_BIGENDIAN */
1316
1317/* The suffix for an index file. */
1318#define INDEX_SUFFIX ".gdb-index"
1319
3da10d80
KS
1320static const char *dwarf2_physname (char *name, struct die_info *die,
1321 struct dwarf2_cu *cu);
1322
c906108c
SS
1323/* Try to locate the sections we need for DWARF 2 debugging
1324 information and return true if we have enough to do something. */
1325
1326int
6502dd73 1327dwarf2_has_info (struct objfile *objfile)
c906108c 1328{
be391dca
TT
1329 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1330 if (!dwarf2_per_objfile)
1331 {
1332 /* Initialize per-objfile state. */
1333 struct dwarf2_per_objfile *data
1334 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1335
be391dca
TT
1336 memset (data, 0, sizeof (*data));
1337 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1338 dwarf2_per_objfile = data;
6502dd73 1339
be391dca
TT
1340 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1341 dwarf2_per_objfile->objfile = objfile;
1342 }
1343 return (dwarf2_per_objfile->info.asection != NULL
1344 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1345}
1346
233a11ab
CS
1347/* When loading sections, we can either look for ".<name>", or for
1348 * ".z<name>", which indicates a compressed section. */
1349
1350static int
dce234bc 1351section_is_p (const char *section_name, const char *name)
233a11ab 1352{
dce234bc
PP
1353 return (section_name[0] == '.'
1354 && (strcmp (section_name + 1, name) == 0
1355 || (section_name[1] == 'z'
1356 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1357}
1358
c906108c
SS
1359/* This function is mapped across the sections and remembers the
1360 offset and size of each of the debugging sections we are interested
1361 in. */
1362
1363static void
72dca2f5 1364dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1365{
dce234bc 1366 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1367 {
dce234bc
PP
1368 dwarf2_per_objfile->info.asection = sectp;
1369 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1370 }
dce234bc 1371 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1372 {
dce234bc
PP
1373 dwarf2_per_objfile->abbrev.asection = sectp;
1374 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1375 }
dce234bc 1376 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1377 {
dce234bc
PP
1378 dwarf2_per_objfile->line.asection = sectp;
1379 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1380 }
dce234bc 1381 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1382 {
dce234bc
PP
1383 dwarf2_per_objfile->loc.asection = sectp;
1384 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1385 }
dce234bc 1386 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1387 {
dce234bc
PP
1388 dwarf2_per_objfile->macinfo.asection = sectp;
1389 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1390 }
dce234bc 1391 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1392 {
dce234bc
PP
1393 dwarf2_per_objfile->str.asection = sectp;
1394 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1395 }
dce234bc 1396 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1397 {
dce234bc
PP
1398 dwarf2_per_objfile->frame.asection = sectp;
1399 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1400 }
dce234bc 1401 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1402 {
3799ccc6 1403 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1404
3799ccc6
EZ
1405 if (aflag & SEC_HAS_CONTENTS)
1406 {
dce234bc
PP
1407 dwarf2_per_objfile->eh_frame.asection = sectp;
1408 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1409 }
b6af0555 1410 }
dce234bc 1411 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1412 {
dce234bc
PP
1413 dwarf2_per_objfile->ranges.asection = sectp;
1414 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1415 }
348e048f
DE
1416 else if (section_is_p (sectp->name, TYPES_SECTION))
1417 {
1418 dwarf2_per_objfile->types.asection = sectp;
1419 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1420 }
9291a0cd
TT
1421 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1422 {
1423 dwarf2_per_objfile->gdb_index.asection = sectp;
1424 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1425 }
dce234bc 1426
72dca2f5
FR
1427 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1428 && bfd_section_vma (abfd, sectp) == 0)
1429 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1430}
1431
dce234bc
PP
1432/* Decompress a section that was compressed using zlib. Store the
1433 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1434
1435static void
dce234bc
PP
1436zlib_decompress_section (struct objfile *objfile, asection *sectp,
1437 gdb_byte **outbuf, bfd_size_type *outsize)
1438{
1439 bfd *abfd = objfile->obfd;
1440#ifndef HAVE_ZLIB_H
1441 error (_("Support for zlib-compressed DWARF data (from '%s') "
1442 "is disabled in this copy of GDB"),
1443 bfd_get_filename (abfd));
1444#else
1445 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1446 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1447 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1448 bfd_size_type uncompressed_size;
1449 gdb_byte *uncompressed_buffer;
1450 z_stream strm;
1451 int rc;
1452 int header_size = 12;
1453
1454 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1455 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1456 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1457 bfd_get_filename (abfd));
1458
1459 /* Read the zlib header. In this case, it should be "ZLIB" followed
1460 by the uncompressed section size, 8 bytes in big-endian order. */
1461 if (compressed_size < header_size
1462 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1463 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1464 bfd_get_filename (abfd));
1465 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[11];
1473
1474 /* It is possible the section consists of several compressed
1475 buffers concatenated together, so we uncompress in a loop. */
1476 strm.zalloc = NULL;
1477 strm.zfree = NULL;
1478 strm.opaque = NULL;
1479 strm.avail_in = compressed_size - header_size;
1480 strm.next_in = (Bytef*) compressed_buffer + header_size;
1481 strm.avail_out = uncompressed_size;
1482 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1483 uncompressed_size);
1484 rc = inflateInit (&strm);
1485 while (strm.avail_in > 0)
1486 {
1487 if (rc != Z_OK)
1488 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1489 bfd_get_filename (abfd), rc);
1490 strm.next_out = ((Bytef*) uncompressed_buffer
1491 + (uncompressed_size - strm.avail_out));
1492 rc = inflate (&strm, Z_FINISH);
1493 if (rc != Z_STREAM_END)
1494 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1495 bfd_get_filename (abfd), rc);
1496 rc = inflateReset (&strm);
1497 }
1498 rc = inflateEnd (&strm);
1499 if (rc != Z_OK
1500 || strm.avail_out != 0)
1501 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1502 bfd_get_filename (abfd), rc);
1503
affddf13 1504 do_cleanups (cleanup);
dce234bc
PP
1505 *outbuf = uncompressed_buffer;
1506 *outsize = uncompressed_size;
1507#endif
233a11ab
CS
1508}
1509
dce234bc
PP
1510/* Read the contents of the section SECTP from object file specified by
1511 OBJFILE, store info about the section into INFO.
1512 If the section is compressed, uncompress it before returning. */
c906108c 1513
dce234bc
PP
1514static void
1515dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1516{
dce234bc
PP
1517 bfd *abfd = objfile->obfd;
1518 asection *sectp = info->asection;
1519 gdb_byte *buf, *retbuf;
1520 unsigned char header[4];
c906108c 1521
be391dca
TT
1522 if (info->readin)
1523 return;
dce234bc
PP
1524 info->buffer = NULL;
1525 info->was_mmapped = 0;
be391dca 1526 info->readin = 1;
188dd5d6 1527
dce234bc
PP
1528 if (info->asection == NULL || info->size == 0)
1529 return;
c906108c 1530
dce234bc
PP
1531 /* Check if the file has a 4-byte header indicating compression. */
1532 if (info->size > sizeof (header)
1533 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1534 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1535 {
1536 /* Upon decompression, update the buffer and its size. */
1537 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1538 {
1539 zlib_decompress_section (objfile, sectp, &info->buffer,
1540 &info->size);
1541 return;
1542 }
1543 }
4bdf3d34 1544
dce234bc
PP
1545#ifdef HAVE_MMAP
1546 if (pagesize == 0)
1547 pagesize = getpagesize ();
2e276125 1548
dce234bc
PP
1549 /* Only try to mmap sections which are large enough: we don't want to
1550 waste space due to fragmentation. Also, only try mmap for sections
1551 without relocations. */
1552
1553 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1554 {
1555 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1556 size_t map_length = info->size + sectp->filepos - pg_offset;
1557 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1558 MAP_PRIVATE, pg_offset);
1559
1560 if (retbuf != MAP_FAILED)
1561 {
1562 info->was_mmapped = 1;
1563 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1564#if HAVE_POSIX_MADVISE
1565 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1566#endif
dce234bc
PP
1567 return;
1568 }
1569 }
1570#endif
1571
1572 /* If we get here, we are a normal, not-compressed section. */
1573 info->buffer = buf
1574 = obstack_alloc (&objfile->objfile_obstack, info->size);
1575
1576 /* When debugging .o files, we may need to apply relocations; see
1577 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1578 We never compress sections in .o files, so we only need to
1579 try this when the section is not compressed. */
ac8035ab 1580 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1581 if (retbuf != NULL)
1582 {
1583 info->buffer = retbuf;
1584 return;
1585 }
1586
1587 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1588 || bfd_bread (buf, info->size, abfd) != info->size)
1589 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1590 bfd_get_filename (abfd));
1591}
1592
1593/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1594 SECTION_NAME. */
af34e669 1595
dce234bc
PP
1596void
1597dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1598 asection **sectp, gdb_byte **bufp,
1599 bfd_size_type *sizep)
1600{
1601 struct dwarf2_per_objfile *data
1602 = objfile_data (objfile, dwarf2_objfile_data_key);
1603 struct dwarf2_section_info *info;
a3b2a86b
TT
1604
1605 /* We may see an objfile without any DWARF, in which case we just
1606 return nothing. */
1607 if (data == NULL)
1608 {
1609 *sectp = NULL;
1610 *bufp = NULL;
1611 *sizep = 0;
1612 return;
1613 }
dce234bc
PP
1614 if (section_is_p (section_name, EH_FRAME_SECTION))
1615 info = &data->eh_frame;
1616 else if (section_is_p (section_name, FRAME_SECTION))
1617 info = &data->frame;
0d53c4c4 1618 else
f3574227 1619 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1620
1621 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1622 /* We haven't read this section in yet. Do it now. */
1623 dwarf2_read_section (objfile, info);
1624
1625 *sectp = info->asection;
1626 *bufp = info->buffer;
1627 *sizep = info->size;
1628}
1629
9291a0cd
TT
1630\f
1631
1632/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1633 this CU came. */
1634static void
1635dw2_do_instantiate_symtab (struct objfile *objfile,
1636 struct dwarf2_per_cu_data *per_cu)
1637{
1638 struct cleanup *back_to;
1639
1640 back_to = make_cleanup (dwarf2_release_queue, NULL);
1641
1642 queue_comp_unit (per_cu, objfile);
1643
1644 if (per_cu->from_debug_types)
1645 read_signatured_type_at_offset (objfile, per_cu->offset);
1646 else
1647 load_full_comp_unit (per_cu, objfile);
1648
1649 process_queue (objfile);
1650
1651 /* Age the cache, releasing compilation units that have not
1652 been used recently. */
1653 age_cached_comp_units ();
1654
1655 do_cleanups (back_to);
1656}
1657
1658/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1659 the objfile from which this CU came. Returns the resulting symbol
1660 table. */
1661static struct symtab *
1662dw2_instantiate_symtab (struct objfile *objfile,
1663 struct dwarf2_per_cu_data *per_cu)
1664{
1665 if (!per_cu->v.quick->symtab)
1666 {
1667 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1668 increment_reading_symtab ();
1669 dw2_do_instantiate_symtab (objfile, per_cu);
1670 do_cleanups (back_to);
1671 }
1672 return per_cu->v.quick->symtab;
1673}
1674
1fd400ff
TT
1675/* Return the CU given its index. */
1676static struct dwarf2_per_cu_data *
1677dw2_get_cu (int index)
1678{
1679 if (index >= dwarf2_per_objfile->n_comp_units)
1680 {
1681 index -= dwarf2_per_objfile->n_comp_units;
1682 return dwarf2_per_objfile->type_comp_units[index];
1683 }
1684 return dwarf2_per_objfile->all_comp_units[index];
1685}
1686
9291a0cd
TT
1687/* A helper function that knows how to read a 64-bit value in a way
1688 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1689 otherwise. */
1690static int
1691extract_cu_value (const char *bytes, ULONGEST *result)
1692{
1693 if (sizeof (ULONGEST) < 8)
1694 {
1695 int i;
1696
1697 /* Ignore the upper 4 bytes if they are all zero. */
1698 for (i = 0; i < 4; ++i)
1699 if (bytes[i + 4] != 0)
1700 return 0;
1701
1702 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1703 }
1704 else
1705 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1706 return 1;
1707}
1708
1709/* Read the CU list from the mapped index, and use it to create all
1710 the CU objects for this objfile. Return 0 if something went wrong,
1711 1 if everything went ok. */
1712static int
1fd400ff
TT
1713create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1714 offset_type cu_list_elements)
9291a0cd
TT
1715{
1716 offset_type i;
9291a0cd
TT
1717
1718 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1719 dwarf2_per_objfile->all_comp_units
1720 = obstack_alloc (&objfile->objfile_obstack,
1721 dwarf2_per_objfile->n_comp_units
1722 * sizeof (struct dwarf2_per_cu_data *));
1723
1724 for (i = 0; i < cu_list_elements; i += 2)
1725 {
1726 struct dwarf2_per_cu_data *the_cu;
1727 ULONGEST offset, length;
1728
1729 if (!extract_cu_value (cu_list, &offset)
1730 || !extract_cu_value (cu_list + 8, &length))
1731 return 0;
1732 cu_list += 2 * 8;
1733
1734 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1735 struct dwarf2_per_cu_data);
1736 the_cu->offset = offset;
1737 the_cu->length = length;
1738 the_cu->objfile = objfile;
1739 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1740 struct dwarf2_per_cu_quick_data);
1741 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1742 }
1743
1744 return 1;
1745}
1746
1fd400ff 1747/* Create the signatured type hash table from the index. */
673bfd45 1748
1fd400ff 1749static int
673bfd45
DE
1750create_signatured_type_table_from_index (struct objfile *objfile,
1751 const gdb_byte *bytes,
1752 offset_type elements)
1fd400ff
TT
1753{
1754 offset_type i;
673bfd45 1755 htab_t sig_types_hash;
1fd400ff
TT
1756
1757 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1758 dwarf2_per_objfile->type_comp_units
1759 = obstack_alloc (&objfile->objfile_obstack,
1760 dwarf2_per_objfile->n_type_comp_units
1761 * sizeof (struct dwarf2_per_cu_data *));
1762
673bfd45 1763 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1764
1765 for (i = 0; i < elements; i += 3)
1766 {
1767 struct signatured_type *type_sig;
1768 ULONGEST offset, type_offset, signature;
1769 void **slot;
1770
1771 if (!extract_cu_value (bytes, &offset)
1772 || !extract_cu_value (bytes + 8, &type_offset))
1773 return 0;
1774 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1775 bytes += 3 * 8;
1776
1777 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1778 struct signatured_type);
1779 type_sig->signature = signature;
1780 type_sig->offset = offset;
1781 type_sig->type_offset = type_offset;
1782 type_sig->per_cu.from_debug_types = 1;
1783 type_sig->per_cu.offset = offset;
1784 type_sig->per_cu.objfile = objfile;
1785 type_sig->per_cu.v.quick
1786 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1787 struct dwarf2_per_cu_quick_data);
1788
673bfd45 1789 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1790 *slot = type_sig;
1791
1792 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1793 }
1794
673bfd45 1795 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1796
1797 return 1;
1798}
1799
9291a0cd
TT
1800/* Read the address map data from the mapped index, and use it to
1801 populate the objfile's psymtabs_addrmap. */
1802static void
1803create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1804{
1805 const gdb_byte *iter, *end;
1806 struct obstack temp_obstack;
1807 struct addrmap *mutable_map;
1808 struct cleanup *cleanup;
1809 CORE_ADDR baseaddr;
1810
1811 obstack_init (&temp_obstack);
1812 cleanup = make_cleanup_obstack_free (&temp_obstack);
1813 mutable_map = addrmap_create_mutable (&temp_obstack);
1814
1815 iter = index->address_table;
1816 end = iter + index->address_table_size;
1817
1818 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1819
1820 while (iter < end)
1821 {
1822 ULONGEST hi, lo, cu_index;
1823 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1824 iter += 8;
1825 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1826 iter += 8;
1827 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1828 iter += 4;
1829
1830 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1831 dw2_get_cu (cu_index));
9291a0cd
TT
1832 }
1833
1834 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1835 &objfile->objfile_obstack);
1836 do_cleanups (cleanup);
1837}
1838
1839/* The hash function for strings in the mapped index. This is the
1840 same as the hashtab.c hash function, but we keep a separate copy to
1841 maintain control over the implementation. This is necessary
1842 because the hash function is tied to the format of the mapped index
1843 file. */
1844static hashval_t
1845mapped_index_string_hash (const void *p)
1846{
1847 const unsigned char *str = (const unsigned char *) p;
1848 hashval_t r = 0;
1849 unsigned char c;
1850
1851 while ((c = *str++) != 0)
1852 r = r * 67 + c - 113;
1853
1854 return r;
1855}
1856
1857/* Find a slot in the mapped index INDEX for the object named NAME.
1858 If NAME is found, set *VEC_OUT to point to the CU vector in the
1859 constant pool and return 1. If NAME cannot be found, return 0. */
1860static int
1861find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1862 offset_type **vec_out)
1863{
1864 offset_type hash = mapped_index_string_hash (name);
1865 offset_type slot, step;
1866
1867 slot = hash & (index->index_table_slots - 1);
1868 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1869
1870 for (;;)
1871 {
1872 /* Convert a slot number to an offset into the table. */
1873 offset_type i = 2 * slot;
1874 const char *str;
1875 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1876 return 0;
1877
1878 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1879 if (!strcmp (name, str))
1880 {
1881 *vec_out = (offset_type *) (index->constant_pool
1882 + MAYBE_SWAP (index->index_table[i + 1]));
1883 return 1;
1884 }
1885
1886 slot = (slot + step) & (index->index_table_slots - 1);
1887 }
1888}
1889
1890/* Read the index file. If everything went ok, initialize the "quick"
1891 elements of all the CUs and return 1. Otherwise, return 0. */
1892static int
1893dwarf2_read_index (struct objfile *objfile)
1894{
9291a0cd
TT
1895 char *addr;
1896 struct mapped_index *map;
b3b272e1 1897 offset_type *metadata;
ac0b195c
KW
1898 const gdb_byte *cu_list;
1899 const gdb_byte *types_list = NULL;
1900 offset_type version, cu_list_elements;
1901 offset_type types_list_elements = 0;
1fd400ff 1902 int i;
9291a0cd
TT
1903
1904 if (dwarf2_per_objfile->gdb_index.asection == NULL
1905 || dwarf2_per_objfile->gdb_index.size == 0)
1906 return 0;
1907 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1908
1909 addr = dwarf2_per_objfile->gdb_index.buffer;
1910 /* Version check. */
1fd400ff
TT
1911 version = MAYBE_SWAP (*(offset_type *) addr);
1912 if (version == 1)
1913 {
1914 /* Index version 1 neglected to account for .debug_types. So,
1915 if we see .debug_types, we cannot use this index. */
1916 if (dwarf2_per_objfile->types.asection != NULL
1917 && dwarf2_per_objfile->types.size != 0)
1918 return 0;
1919 }
1920 else if (version != 2)
9291a0cd
TT
1921 return 0;
1922
1923 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1924 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1925
1926 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1927
1928 i = 0;
1929 cu_list = addr + MAYBE_SWAP (metadata[i]);
1930 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1931 / 8);
1fd400ff
TT
1932 ++i;
1933
1934 if (version == 2)
1935 {
1936 types_list = addr + MAYBE_SWAP (metadata[i]);
1937 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1938 - MAYBE_SWAP (metadata[i]))
1939 / 8);
1940 ++i;
1941 }
1942
1943 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1944 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1945 - MAYBE_SWAP (metadata[i]));
1946 ++i;
1947
1948 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1949 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1950 - MAYBE_SWAP (metadata[i]))
9291a0cd 1951 / (2 * sizeof (offset_type)));
1fd400ff 1952 ++i;
9291a0cd 1953
1fd400ff
TT
1954 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1955
1956 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1957 return 0;
1958
1959 if (version == 2
1960 && types_list_elements
673bfd45
DE
1961 && !create_signatured_type_table_from_index (objfile, types_list,
1962 types_list_elements))
9291a0cd
TT
1963 return 0;
1964
1965 create_addrmap_from_index (objfile, map);
1966
1967 dwarf2_per_objfile->index_table = map;
1968 dwarf2_per_objfile->using_index = 1;
1969
1970 return 1;
1971}
1972
1973/* A helper for the "quick" functions which sets the global
1974 dwarf2_per_objfile according to OBJFILE. */
1975static void
1976dw2_setup (struct objfile *objfile)
1977{
1978 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1979 gdb_assert (dwarf2_per_objfile);
1980}
1981
1982/* A helper for the "quick" functions which attempts to read the line
1983 table for THIS_CU. */
1984static void
1985dw2_require_line_header (struct objfile *objfile,
1986 struct dwarf2_per_cu_data *this_cu)
1987{
1988 bfd *abfd = objfile->obfd;
1989 struct line_header *lh = NULL;
1990 struct attribute *attr;
1991 struct cleanup *cleanups;
1992 struct die_info *comp_unit_die;
1993 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1994 int has_children, i;
1995 struct dwarf2_cu cu;
1996 unsigned int bytes_read, buffer_size;
1997 struct die_reader_specs reader_specs;
1998 char *name, *comp_dir;
1999
2000 if (this_cu->v.quick->read_lines)
2001 return;
2002 this_cu->v.quick->read_lines = 1;
2003
2004 memset (&cu, 0, sizeof (cu));
2005 cu.objfile = objfile;
2006 obstack_init (&cu.comp_unit_obstack);
2007
2008 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2009
2010 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2011 buffer_size = dwarf2_per_objfile->info.size;
2012 buffer = dwarf2_per_objfile->info.buffer;
2013 info_ptr = buffer + this_cu->offset;
2014 beg_of_comp_unit = info_ptr;
2015
2016 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2017 buffer, buffer_size,
2018 abfd);
2019
2020 /* Complete the cu_header. */
2021 cu.header.offset = beg_of_comp_unit - buffer;
2022 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2023
2024 this_cu->cu = &cu;
2025 cu.per_cu = this_cu;
2026
2027 dwarf2_read_abbrevs (abfd, &cu);
2028 make_cleanup (dwarf2_free_abbrev_table, &cu);
2029
2030 if (this_cu->from_debug_types)
2031 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2032 init_cu_die_reader (&reader_specs, &cu);
2033 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2034 &has_children);
2035
2036 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2037 if (attr)
2038 {
2039 unsigned int line_offset = DW_UNSND (attr);
2040 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2041 }
2042 if (lh == NULL)
2043 {
2044 do_cleanups (cleanups);
2045 return;
2046 }
2047
2048 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2049
2050 this_cu->v.quick->lines = lh;
2051
2052 this_cu->v.quick->file_names
2053 = obstack_alloc (&objfile->objfile_obstack,
2054 lh->num_file_names * sizeof (char *));
2055 for (i = 0; i < lh->num_file_names; ++i)
2056 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2057
2058 do_cleanups (cleanups);
2059}
2060
2061/* A helper for the "quick" functions which computes and caches the
2062 real path for a given file name from the line table.
2063 dw2_require_line_header must have been called before this is
2064 invoked. */
2065static const char *
2066dw2_require_full_path (struct objfile *objfile,
e254ef6a 2067 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2068 int index)
2069{
e254ef6a
DE
2070 if (!per_cu->v.quick->full_names)
2071 per_cu->v.quick->full_names
9291a0cd 2072 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2073 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2074 sizeof (char *));
2075
e254ef6a
DE
2076 if (!per_cu->v.quick->full_names[index])
2077 per_cu->v.quick->full_names[index]
2078 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2079
e254ef6a 2080 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2081}
2082
2083static struct symtab *
2084dw2_find_last_source_symtab (struct objfile *objfile)
2085{
2086 int index;
2087 dw2_setup (objfile);
2088 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2089 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2090}
2091
2092static void
2093dw2_forget_cached_source_info (struct objfile *objfile)
2094{
2095 int i;
2096
2097 dw2_setup (objfile);
1fd400ff
TT
2098 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2099 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2100 {
e254ef6a 2101 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2102
e254ef6a 2103 if (per_cu->v.quick->full_names)
9291a0cd
TT
2104 {
2105 int j;
2106
e254ef6a
DE
2107 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2108 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
2109 }
2110 }
2111}
2112
2113static int
2114dw2_lookup_symtab (struct objfile *objfile, const char *name,
2115 const char *full_path, const char *real_path,
2116 struct symtab **result)
2117{
2118 int i;
2119 int check_basename = lbasename (name) == name;
2120 struct dwarf2_per_cu_data *base_cu = NULL;
2121
2122 dw2_setup (objfile);
1fd400ff
TT
2123 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2124 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2125 {
2126 int j;
e254ef6a 2127 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2128
e254ef6a 2129 if (per_cu->v.quick->symtab)
9291a0cd
TT
2130 continue;
2131
e254ef6a
DE
2132 dw2_require_line_header (objfile, per_cu);
2133 if (!per_cu->v.quick->lines)
9291a0cd
TT
2134 continue;
2135
e254ef6a 2136 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2137 {
e254ef6a 2138 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2139
2140 if (FILENAME_CMP (name, this_name) == 0)
2141 {
e254ef6a 2142 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2143 return 1;
2144 }
2145
2146 if (check_basename && ! base_cu
2147 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2148 base_cu = per_cu;
9291a0cd
TT
2149
2150 if (full_path != NULL)
2151 {
2152 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2153 per_cu, j);
9291a0cd
TT
2154
2155 if (this_full_name
2156 && FILENAME_CMP (full_path, this_full_name) == 0)
2157 {
e254ef6a 2158 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2159 return 1;
2160 }
2161 }
2162
2163 if (real_path != NULL)
2164 {
2165 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2166 per_cu, j);
9291a0cd
TT
2167
2168 if (this_full_name != NULL)
2169 {
2170 char *rp = gdb_realpath (this_full_name);
2171 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2172 {
2173 xfree (rp);
e254ef6a 2174 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2175 return 1;
2176 }
2177 xfree (rp);
2178 }
2179 }
2180 }
2181 }
2182
2183 if (base_cu)
2184 {
2185 *result = dw2_instantiate_symtab (objfile, base_cu);
2186 return 1;
2187 }
2188
2189 return 0;
2190}
2191
2192static struct symtab *
2193dw2_lookup_symbol (struct objfile *objfile, int block_index,
2194 const char *name, domain_enum domain)
2195{
2196 /* We do all the work in the pre_expand_symtabs_matching hook
2197 instead. */
2198 return NULL;
2199}
2200
2201/* A helper function that expands all symtabs that hold an object
2202 named NAME. */
2203static void
2204dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2205{
2206 dw2_setup (objfile);
2207
2208 if (dwarf2_per_objfile->index_table)
2209 {
2210 offset_type *vec;
2211
2212 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2213 name, &vec))
2214 {
2215 offset_type i, len = MAYBE_SWAP (*vec);
2216 for (i = 0; i < len; ++i)
2217 {
2218 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2219 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2220
e254ef6a 2221 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2222 }
2223 }
2224 }
2225}
2226
2227static void
2228dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2229 int kind, const char *name,
2230 domain_enum domain)
2231{
2232 dw2_do_expand_symtabs_matching (objfile, name);
2233}
2234
2235static void
2236dw2_print_stats (struct objfile *objfile)
2237{
2238 int i, count;
2239
2240 dw2_setup (objfile);
2241 count = 0;
1fd400ff
TT
2242 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2243 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2244 {
e254ef6a 2245 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2246
e254ef6a 2247 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2248 ++count;
2249 }
2250 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2251}
2252
2253static void
2254dw2_dump (struct objfile *objfile)
2255{
2256 /* Nothing worth printing. */
2257}
2258
2259static void
2260dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2261 struct section_offsets *delta)
2262{
2263 /* There's nothing to relocate here. */
2264}
2265
2266static void
2267dw2_expand_symtabs_for_function (struct objfile *objfile,
2268 const char *func_name)
2269{
2270 dw2_do_expand_symtabs_matching (objfile, func_name);
2271}
2272
2273static void
2274dw2_expand_all_symtabs (struct objfile *objfile)
2275{
2276 int i;
2277
2278 dw2_setup (objfile);
1fd400ff
TT
2279
2280 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2281 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2282 {
e254ef6a 2283 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2284
e254ef6a 2285 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2286 }
2287}
2288
2289static void
2290dw2_expand_symtabs_with_filename (struct objfile *objfile,
2291 const char *filename)
2292{
2293 int i;
2294
2295 dw2_setup (objfile);
1fd400ff
TT
2296 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2297 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2298 {
2299 int j;
e254ef6a 2300 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2301
e254ef6a 2302 if (per_cu->v.quick->symtab)
9291a0cd
TT
2303 continue;
2304
e254ef6a
DE
2305 dw2_require_line_header (objfile, per_cu);
2306 if (!per_cu->v.quick->lines)
9291a0cd
TT
2307 continue;
2308
e254ef6a 2309 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2310 {
e254ef6a 2311 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2312 if (strcmp (this_name, filename) == 0)
2313 {
e254ef6a 2314 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2315 break;
2316 }
2317 }
2318 }
2319}
2320
dd786858 2321static const char *
9291a0cd
TT
2322dw2_find_symbol_file (struct objfile *objfile, const char *name)
2323{
e254ef6a 2324 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2325 offset_type *vec;
2326
2327 dw2_setup (objfile);
2328
2329 if (!dwarf2_per_objfile->index_table)
2330 return NULL;
2331
2332 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2333 name, &vec))
2334 return NULL;
2335
2336 /* Note that this just looks at the very first one named NAME -- but
2337 actually we are looking for a function. find_main_filename
2338 should be rewritten so that it doesn't require a custom hook. It
2339 could just use the ordinary symbol tables. */
2340 /* vec[0] is the length, which must always be >0. */
e254ef6a 2341 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2342
e254ef6a
DE
2343 dw2_require_line_header (objfile, per_cu);
2344 if (!per_cu->v.quick->lines)
9291a0cd
TT
2345 return NULL;
2346
e254ef6a 2347 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2348}
2349
2350static void
2351dw2_map_ada_symtabs (struct objfile *objfile,
2352 int (*wild_match) (const char *, int, const char *),
2353 int (*is_name_suffix) (const char *),
2354 void (*callback) (struct objfile *,
2355 struct symtab *, void *),
2356 const char *name, int global,
2357 domain_enum namespace, int wild,
2358 void *data)
2359{
2360 /* For now, we don't support Ada, so this function can't be
2361 reached. */
2362 internal_error (__FILE__, __LINE__,
2363 _("map_ada_symtabs called via index method"));
2364}
2365
2366static void
2367dw2_expand_symtabs_matching (struct objfile *objfile,
2368 int (*file_matcher) (const char *, void *),
2369 int (*name_matcher) (const char *, void *),
2370 domain_enum kind,
2371 void *data)
2372{
2373 int i;
2374 offset_type iter;
2375
2376 dw2_setup (objfile);
2377 if (!dwarf2_per_objfile->index_table)
2378 return;
2379
1fd400ff
TT
2380 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2381 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2382 {
2383 int j;
e254ef6a 2384 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2385
e254ef6a
DE
2386 per_cu->v.quick->mark = 0;
2387 if (per_cu->v.quick->symtab)
9291a0cd
TT
2388 continue;
2389
e254ef6a
DE
2390 dw2_require_line_header (objfile, per_cu);
2391 if (!per_cu->v.quick->lines)
9291a0cd
TT
2392 continue;
2393
e254ef6a 2394 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2395 {
e254ef6a 2396 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2397 {
e254ef6a 2398 per_cu->v.quick->mark = 1;
9291a0cd
TT
2399 break;
2400 }
2401 }
2402 }
2403
2404 for (iter = 0;
2405 iter < dwarf2_per_objfile->index_table->index_table_slots;
2406 ++iter)
2407 {
2408 offset_type idx = 2 * iter;
2409 const char *name;
2410 offset_type *vec, vec_len, vec_idx;
2411
2412 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2413 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2414 continue;
2415
2416 name = (dwarf2_per_objfile->index_table->constant_pool
2417 + dwarf2_per_objfile->index_table->index_table[idx]);
2418
2419 if (! (*name_matcher) (name, data))
2420 continue;
2421
2422 /* The name was matched, now expand corresponding CUs that were
2423 marked. */
2424 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2425 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2426 vec_len = MAYBE_SWAP (vec[0]);
2427 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2428 {
e254ef6a 2429 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2430
e254ef6a
DE
2431 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2432 if (per_cu->v.quick->mark)
2433 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2434 }
2435 }
2436}
2437
2438static struct symtab *
2439dw2_find_pc_sect_symtab (struct objfile *objfile,
2440 struct minimal_symbol *msymbol,
2441 CORE_ADDR pc,
2442 struct obj_section *section,
2443 int warn_if_readin)
2444{
2445 struct dwarf2_per_cu_data *data;
2446
2447 dw2_setup (objfile);
2448
2449 if (!objfile->psymtabs_addrmap)
2450 return NULL;
2451
2452 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2453 if (!data)
2454 return NULL;
2455
2456 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2457 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2458 paddress (get_objfile_arch (objfile), pc));
2459
2460 return dw2_instantiate_symtab (objfile, data);
2461}
2462
2463static void
2464dw2_map_symbol_names (struct objfile *objfile,
2465 void (*fun) (const char *, void *),
2466 void *data)
2467{
2468 offset_type iter;
2469 dw2_setup (objfile);
2470
2471 if (!dwarf2_per_objfile->index_table)
2472 return;
2473
2474 for (iter = 0;
2475 iter < dwarf2_per_objfile->index_table->index_table_slots;
2476 ++iter)
2477 {
2478 offset_type idx = 2 * iter;
2479 const char *name;
2480 offset_type *vec, vec_len, vec_idx;
2481
2482 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2483 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2484 continue;
2485
2486 name = (dwarf2_per_objfile->index_table->constant_pool
2487 + dwarf2_per_objfile->index_table->index_table[idx]);
2488
2489 (*fun) (name, data);
2490 }
2491}
2492
2493static void
2494dw2_map_symbol_filenames (struct objfile *objfile,
2495 void (*fun) (const char *, const char *, void *),
2496 void *data)
2497{
2498 int i;
2499
2500 dw2_setup (objfile);
1fd400ff
TT
2501 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2502 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2503 {
2504 int j;
e254ef6a 2505 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2506
e254ef6a 2507 if (per_cu->v.quick->symtab)
9291a0cd
TT
2508 continue;
2509
e254ef6a
DE
2510 dw2_require_line_header (objfile, per_cu);
2511 if (!per_cu->v.quick->lines)
9291a0cd
TT
2512 continue;
2513
e254ef6a 2514 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2515 {
e254ef6a
DE
2516 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2517 j);
2518 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2519 }
2520 }
2521}
2522
2523static int
2524dw2_has_symbols (struct objfile *objfile)
2525{
2526 return 1;
2527}
2528
2529const struct quick_symbol_functions dwarf2_gdb_index_functions =
2530{
2531 dw2_has_symbols,
2532 dw2_find_last_source_symtab,
2533 dw2_forget_cached_source_info,
2534 dw2_lookup_symtab,
2535 dw2_lookup_symbol,
2536 dw2_pre_expand_symtabs_matching,
2537 dw2_print_stats,
2538 dw2_dump,
2539 dw2_relocate,
2540 dw2_expand_symtabs_for_function,
2541 dw2_expand_all_symtabs,
2542 dw2_expand_symtabs_with_filename,
2543 dw2_find_symbol_file,
2544 dw2_map_ada_symtabs,
2545 dw2_expand_symtabs_matching,
2546 dw2_find_pc_sect_symtab,
2547 dw2_map_symbol_names,
2548 dw2_map_symbol_filenames
2549};
2550
2551/* Initialize for reading DWARF for this objfile. Return 0 if this
2552 file will use psymtabs, or 1 if using the GNU index. */
2553
2554int
2555dwarf2_initialize_objfile (struct objfile *objfile)
2556{
2557 /* If we're about to read full symbols, don't bother with the
2558 indices. In this case we also don't care if some other debug
2559 format is making psymtabs, because they are all about to be
2560 expanded anyway. */
2561 if ((objfile->flags & OBJF_READNOW))
2562 {
2563 int i;
2564
2565 dwarf2_per_objfile->using_index = 1;
2566 create_all_comp_units (objfile);
1fd400ff 2567 create_debug_types_hash_table (objfile);
9291a0cd 2568
1fd400ff
TT
2569 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2570 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2571 {
e254ef6a 2572 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2573
e254ef6a
DE
2574 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2575 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2576 }
2577
2578 /* Return 1 so that gdb sees the "quick" functions. However,
2579 these functions will be no-ops because we will have expanded
2580 all symtabs. */
2581 return 1;
2582 }
2583
2584 if (dwarf2_read_index (objfile))
2585 return 1;
2586
2587 dwarf2_build_psymtabs (objfile);
2588 return 0;
2589}
2590
2591\f
2592
dce234bc
PP
2593/* Build a partial symbol table. */
2594
2595void
f29dff0a 2596dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2597{
f29dff0a 2598 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2599 {
2600 init_psymbol_list (objfile, 1024);
2601 }
2602
d146bf1e 2603 dwarf2_build_psymtabs_hard (objfile);
c906108c 2604}
c906108c 2605
45452591
DE
2606/* Return TRUE if OFFSET is within CU_HEADER. */
2607
2608static inline int
2609offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2610{
2611 unsigned int bottom = cu_header->offset;
2612 unsigned int top = (cu_header->offset
2613 + cu_header->length
2614 + cu_header->initial_length_size);
9a619af0 2615
45452591
DE
2616 return (offset >= bottom && offset < top);
2617}
2618
93311388
DE
2619/* Read in the comp unit header information from the debug_info at info_ptr.
2620 NOTE: This leaves members offset, first_die_offset to be filled in
2621 by the caller. */
107d2387 2622
fe1b8b76 2623static gdb_byte *
107d2387 2624read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2625 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2626{
2627 int signed_addr;
891d2f0b 2628 unsigned int bytes_read;
c764a876
DE
2629
2630 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2631 cu_header->initial_length_size = bytes_read;
2632 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2633 info_ptr += bytes_read;
107d2387
AC
2634 cu_header->version = read_2_bytes (abfd, info_ptr);
2635 info_ptr += 2;
613e1657 2636 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2637 &bytes_read);
613e1657 2638 info_ptr += bytes_read;
107d2387
AC
2639 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2640 info_ptr += 1;
2641 signed_addr = bfd_get_sign_extend_vma (abfd);
2642 if (signed_addr < 0)
8e65ff28 2643 internal_error (__FILE__, __LINE__,
e2e0b3e5 2644 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2645 cu_header->signed_addr_p = signed_addr;
c764a876 2646
107d2387
AC
2647 return info_ptr;
2648}
2649
fe1b8b76
JB
2650static gdb_byte *
2651partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2652 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2653 bfd *abfd)
2654{
fe1b8b76 2655 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2656
2657 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2658
2dc7f7b3 2659 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2660 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2661 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2662 bfd_get_filename (abfd));
72bf9492 2663
dce234bc 2664 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2665 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2666 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2667 (long) header->abbrev_offset,
93311388 2668 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2669 bfd_get_filename (abfd));
2670
2671 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2672 > buffer + buffer_size)
8a3fe4f8
AC
2673 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2674 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2675 (long) header->length,
93311388 2676 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2677 bfd_get_filename (abfd));
2678
2679 return info_ptr;
2680}
2681
348e048f
DE
2682/* Read in the types comp unit header information from .debug_types entry at
2683 types_ptr. The result is a pointer to one past the end of the header. */
2684
2685static gdb_byte *
2686read_type_comp_unit_head (struct comp_unit_head *cu_header,
2687 ULONGEST *signature,
2688 gdb_byte *types_ptr, bfd *abfd)
2689{
348e048f
DE
2690 gdb_byte *initial_types_ptr = types_ptr;
2691
6e70227d 2692 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2693 &dwarf2_per_objfile->types);
348e048f
DE
2694 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2695
2696 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2697
2698 *signature = read_8_bytes (abfd, types_ptr);
2699 types_ptr += 8;
2700 types_ptr += cu_header->offset_size;
2701 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2702
2703 return types_ptr;
2704}
2705
aaa75496
JB
2706/* Allocate a new partial symtab for file named NAME and mark this new
2707 partial symtab as being an include of PST. */
2708
2709static void
2710dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2711 struct objfile *objfile)
2712{
2713 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2714
2715 subpst->section_offsets = pst->section_offsets;
2716 subpst->textlow = 0;
2717 subpst->texthigh = 0;
2718
2719 subpst->dependencies = (struct partial_symtab **)
2720 obstack_alloc (&objfile->objfile_obstack,
2721 sizeof (struct partial_symtab *));
2722 subpst->dependencies[0] = pst;
2723 subpst->number_of_dependencies = 1;
2724
2725 subpst->globals_offset = 0;
2726 subpst->n_global_syms = 0;
2727 subpst->statics_offset = 0;
2728 subpst->n_static_syms = 0;
2729 subpst->symtab = NULL;
2730 subpst->read_symtab = pst->read_symtab;
2731 subpst->readin = 0;
2732
2733 /* No private part is necessary for include psymtabs. This property
2734 can be used to differentiate between such include psymtabs and
10b3939b 2735 the regular ones. */
58a9656e 2736 subpst->read_symtab_private = NULL;
aaa75496
JB
2737}
2738
2739/* Read the Line Number Program data and extract the list of files
2740 included by the source file represented by PST. Build an include
d85a05f0 2741 partial symtab for each of these included files. */
aaa75496
JB
2742
2743static void
2744dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2745 struct die_info *die,
aaa75496
JB
2746 struct partial_symtab *pst)
2747{
2748 struct objfile *objfile = cu->objfile;
2749 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2750 struct line_header *lh = NULL;
2751 struct attribute *attr;
aaa75496 2752
d85a05f0
DJ
2753 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2754 if (attr)
2755 {
2756 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2757
d85a05f0
DJ
2758 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2759 }
aaa75496
JB
2760 if (lh == NULL)
2761 return; /* No linetable, so no includes. */
2762
c6da4cef
DE
2763 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2764 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2765
2766 free_line_header (lh);
2767}
2768
348e048f
DE
2769static hashval_t
2770hash_type_signature (const void *item)
2771{
2772 const struct signatured_type *type_sig = item;
9a619af0 2773
348e048f
DE
2774 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2775 return type_sig->signature;
2776}
2777
2778static int
2779eq_type_signature (const void *item_lhs, const void *item_rhs)
2780{
2781 const struct signatured_type *lhs = item_lhs;
2782 const struct signatured_type *rhs = item_rhs;
9a619af0 2783
348e048f
DE
2784 return lhs->signature == rhs->signature;
2785}
2786
1fd400ff
TT
2787/* Allocate a hash table for signatured types. */
2788
2789static htab_t
673bfd45 2790allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2791{
2792 return htab_create_alloc_ex (41,
2793 hash_type_signature,
2794 eq_type_signature,
2795 NULL,
2796 &objfile->objfile_obstack,
2797 hashtab_obstack_allocate,
2798 dummy_obstack_deallocate);
2799}
2800
2801/* A helper function to add a signatured type CU to a list. */
2802
2803static int
2804add_signatured_type_cu_to_list (void **slot, void *datum)
2805{
2806 struct signatured_type *sigt = *slot;
2807 struct dwarf2_per_cu_data ***datap = datum;
2808
2809 **datap = &sigt->per_cu;
2810 ++*datap;
2811
2812 return 1;
2813}
2814
348e048f
DE
2815/* Create the hash table of all entries in the .debug_types section.
2816 The result is zero if there is an error (e.g. missing .debug_types section),
2817 otherwise non-zero. */
2818
2819static int
2820create_debug_types_hash_table (struct objfile *objfile)
2821{
be391dca 2822 gdb_byte *info_ptr;
348e048f 2823 htab_t types_htab;
1fd400ff 2824 struct dwarf2_per_cu_data **iter;
348e048f 2825
be391dca
TT
2826 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2827 info_ptr = dwarf2_per_objfile->types.buffer;
2828
348e048f
DE
2829 if (info_ptr == NULL)
2830 {
2831 dwarf2_per_objfile->signatured_types = NULL;
2832 return 0;
2833 }
2834
673bfd45 2835 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2836
2837 if (dwarf2_die_debug)
2838 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2839
2840 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2841 {
2842 unsigned int offset;
2843 unsigned int offset_size;
2844 unsigned int type_offset;
2845 unsigned int length, initial_length_size;
2846 unsigned short version;
2847 ULONGEST signature;
2848 struct signatured_type *type_sig;
2849 void **slot;
2850 gdb_byte *ptr = info_ptr;
2851
2852 offset = ptr - dwarf2_per_objfile->types.buffer;
2853
2854 /* We need to read the type's signature in order to build the hash
2855 table, but we don't need to read anything else just yet. */
2856
2857 /* Sanity check to ensure entire cu is present. */
2858 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2859 if (ptr + length + initial_length_size
2860 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2861 {
2862 complaint (&symfile_complaints,
2863 _("debug type entry runs off end of `.debug_types' section, ignored"));
2864 break;
2865 }
2866
2867 offset_size = initial_length_size == 4 ? 4 : 8;
2868 ptr += initial_length_size;
2869 version = bfd_get_16 (objfile->obfd, ptr);
2870 ptr += 2;
2871 ptr += offset_size; /* abbrev offset */
2872 ptr += 1; /* address size */
2873 signature = bfd_get_64 (objfile->obfd, ptr);
2874 ptr += 8;
2875 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2876
2877 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2878 memset (type_sig, 0, sizeof (*type_sig));
2879 type_sig->signature = signature;
2880 type_sig->offset = offset;
2881 type_sig->type_offset = type_offset;
ca1f3406 2882 type_sig->per_cu.objfile = objfile;
1fd400ff 2883 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2884
2885 slot = htab_find_slot (types_htab, type_sig, INSERT);
2886 gdb_assert (slot != NULL);
2887 *slot = type_sig;
2888
2889 if (dwarf2_die_debug)
2890 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2891 offset, phex (signature, sizeof (signature)));
2892
2893 info_ptr = info_ptr + initial_length_size + length;
2894 }
2895
2896 dwarf2_per_objfile->signatured_types = types_htab;
2897
1fd400ff
TT
2898 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2899 dwarf2_per_objfile->type_comp_units
2900 = obstack_alloc (&objfile->objfile_obstack,
2901 dwarf2_per_objfile->n_type_comp_units
2902 * sizeof (struct dwarf2_per_cu_data *));
2903 iter = &dwarf2_per_objfile->type_comp_units[0];
2904 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2905 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2906 == dwarf2_per_objfile->n_type_comp_units);
2907
348e048f
DE
2908 return 1;
2909}
2910
2911/* Lookup a signature based type.
2912 Returns NULL if SIG is not present in the table. */
2913
2914static struct signatured_type *
2915lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2916{
2917 struct signatured_type find_entry, *entry;
2918
2919 if (dwarf2_per_objfile->signatured_types == NULL)
2920 {
2921 complaint (&symfile_complaints,
2922 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2923 return 0;
2924 }
2925
2926 find_entry.signature = sig;
2927 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2928 return entry;
2929}
2930
d85a05f0
DJ
2931/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2932
2933static void
2934init_cu_die_reader (struct die_reader_specs *reader,
2935 struct dwarf2_cu *cu)
2936{
2937 reader->abfd = cu->objfile->obfd;
2938 reader->cu = cu;
2939 if (cu->per_cu->from_debug_types)
be391dca
TT
2940 {
2941 gdb_assert (dwarf2_per_objfile->types.readin);
2942 reader->buffer = dwarf2_per_objfile->types.buffer;
2943 }
d85a05f0 2944 else
be391dca
TT
2945 {
2946 gdb_assert (dwarf2_per_objfile->info.readin);
2947 reader->buffer = dwarf2_per_objfile->info.buffer;
2948 }
d85a05f0
DJ
2949}
2950
2951/* Find the base address of the compilation unit for range lists and
2952 location lists. It will normally be specified by DW_AT_low_pc.
2953 In DWARF-3 draft 4, the base address could be overridden by
2954 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2955 compilation units with discontinuous ranges. */
2956
2957static void
2958dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2959{
2960 struct attribute *attr;
2961
2962 cu->base_known = 0;
2963 cu->base_address = 0;
2964
2965 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2966 if (attr)
2967 {
2968 cu->base_address = DW_ADDR (attr);
2969 cu->base_known = 1;
2970 }
2971 else
2972 {
2973 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2974 if (attr)
2975 {
2976 cu->base_address = DW_ADDR (attr);
2977 cu->base_known = 1;
2978 }
2979 }
2980}
2981
348e048f
DE
2982/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2983 to combine the common parts.
93311388 2984 Process a compilation unit for a psymtab.
348e048f
DE
2985 BUFFER is a pointer to the beginning of the dwarf section buffer,
2986 either .debug_info or debug_types.
93311388
DE
2987 INFO_PTR is a pointer to the start of the CU.
2988 Returns a pointer to the next CU. */
aaa75496 2989
93311388
DE
2990static gdb_byte *
2991process_psymtab_comp_unit (struct objfile *objfile,
2992 struct dwarf2_per_cu_data *this_cu,
2993 gdb_byte *buffer, gdb_byte *info_ptr,
2994 unsigned int buffer_size)
c906108c 2995{
c906108c 2996 bfd *abfd = objfile->obfd;
93311388 2997 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 2998 struct die_info *comp_unit_die;
c906108c 2999 struct partial_symtab *pst;
5734ee8b 3000 CORE_ADDR baseaddr;
93311388
DE
3001 struct cleanup *back_to_inner;
3002 struct dwarf2_cu cu;
d85a05f0
DJ
3003 int has_children, has_pc_info;
3004 struct attribute *attr;
d85a05f0
DJ
3005 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3006 struct die_reader_specs reader_specs;
c906108c 3007
93311388
DE
3008 memset (&cu, 0, sizeof (cu));
3009 cu.objfile = objfile;
3010 obstack_init (&cu.comp_unit_obstack);
c906108c 3011
93311388 3012 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3013
93311388
DE
3014 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3015 buffer, buffer_size,
3016 abfd);
10b3939b 3017
93311388
DE
3018 /* Complete the cu_header. */
3019 cu.header.offset = beg_of_comp_unit - buffer;
3020 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3021
93311388 3022 cu.list_in_scope = &file_symbols;
af703f96 3023
328c9494
DJ
3024 /* If this compilation unit was already read in, free the
3025 cached copy in order to read it in again. This is
3026 necessary because we skipped some symbols when we first
3027 read in the compilation unit (see load_partial_dies).
3028 This problem could be avoided, but the benefit is
3029 unclear. */
3030 if (this_cu->cu != NULL)
3031 free_one_cached_comp_unit (this_cu->cu);
3032
3033 /* Note that this is a pointer to our stack frame, being
3034 added to a global data structure. It will be cleaned up
3035 in free_stack_comp_unit when we finish with this
3036 compilation unit. */
3037 this_cu->cu = &cu;
d85a05f0
DJ
3038 cu.per_cu = this_cu;
3039
93311388
DE
3040 /* Read the abbrevs for this compilation unit into a table. */
3041 dwarf2_read_abbrevs (abfd, &cu);
3042 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3043
93311388 3044 /* Read the compilation unit die. */
348e048f
DE
3045 if (this_cu->from_debug_types)
3046 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3047 init_cu_die_reader (&reader_specs, &cu);
3048 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3049 &has_children);
93311388 3050
348e048f
DE
3051 if (this_cu->from_debug_types)
3052 {
3053 /* offset,length haven't been set yet for type units. */
3054 this_cu->offset = cu.header.offset;
3055 this_cu->length = cu.header.length + cu.header.initial_length_size;
3056 }
d85a05f0 3057 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3058 {
93311388
DE
3059 info_ptr = (beg_of_comp_unit + cu.header.length
3060 + cu.header.initial_length_size);
3061 do_cleanups (back_to_inner);
3062 return info_ptr;
3063 }
72bf9492 3064
93311388 3065 /* Set the language we're debugging. */
d85a05f0
DJ
3066 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3067 if (attr)
3068 set_cu_language (DW_UNSND (attr), &cu);
3069 else
3070 set_cu_language (language_minimal, &cu);
c906108c 3071
93311388 3072 /* Allocate a new partial symbol table structure. */
d85a05f0 3073 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3074 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3075 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3076 /* TEXTLOW and TEXTHIGH are set below. */
3077 0,
3078 objfile->global_psymbols.next,
3079 objfile->static_psymbols.next);
72bf9492 3080
d85a05f0
DJ
3081 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3082 if (attr != NULL)
3083 pst->dirname = DW_STRING (attr);
72bf9492 3084
e38df1d0 3085 pst->read_symtab_private = this_cu;
72bf9492 3086
93311388 3087 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3088
93311388
DE
3089 /* Store the function that reads in the rest of the symbol table */
3090 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3091
9291a0cd 3092 this_cu->v.psymtab = pst;
c906108c 3093
d85a05f0
DJ
3094 dwarf2_find_base_address (comp_unit_die, &cu);
3095
93311388
DE
3096 /* Possibly set the default values of LOWPC and HIGHPC from
3097 `DW_AT_ranges'. */
d85a05f0
DJ
3098 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3099 &best_highpc, &cu, pst);
3100 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3101 /* Store the contiguous range if it is not empty; it can be empty for
3102 CUs with no code. */
3103 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3104 best_lowpc + baseaddr,
3105 best_highpc + baseaddr - 1, pst);
93311388
DE
3106
3107 /* Check if comp unit has_children.
3108 If so, read the rest of the partial symbols from this comp unit.
3109 If not, there's no more debug_info for this comp unit. */
d85a05f0 3110 if (has_children)
93311388
DE
3111 {
3112 struct partial_die_info *first_die;
3113 CORE_ADDR lowpc, highpc;
31ffec48 3114
93311388
DE
3115 lowpc = ((CORE_ADDR) -1);
3116 highpc = ((CORE_ADDR) 0);
c906108c 3117
93311388 3118 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3119
93311388 3120 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3121 ! has_pc_info, &cu);
57c22c6c 3122
93311388
DE
3123 /* If we didn't find a lowpc, set it to highpc to avoid
3124 complaints from `maint check'. */
3125 if (lowpc == ((CORE_ADDR) -1))
3126 lowpc = highpc;
10b3939b 3127
93311388
DE
3128 /* If the compilation unit didn't have an explicit address range,
3129 then use the information extracted from its child dies. */
d85a05f0 3130 if (! has_pc_info)
93311388 3131 {
d85a05f0
DJ
3132 best_lowpc = lowpc;
3133 best_highpc = highpc;
93311388
DE
3134 }
3135 }
d85a05f0
DJ
3136 pst->textlow = best_lowpc + baseaddr;
3137 pst->texthigh = best_highpc + baseaddr;
c906108c 3138
93311388
DE
3139 pst->n_global_syms = objfile->global_psymbols.next -
3140 (objfile->global_psymbols.list + pst->globals_offset);
3141 pst->n_static_syms = objfile->static_psymbols.next -
3142 (objfile->static_psymbols.list + pst->statics_offset);
3143 sort_pst_symbols (pst);
c906108c 3144
93311388
DE
3145 info_ptr = (beg_of_comp_unit + cu.header.length
3146 + cu.header.initial_length_size);
ae038cb0 3147
348e048f
DE
3148 if (this_cu->from_debug_types)
3149 {
3150 /* It's not clear we want to do anything with stmt lists here.
3151 Waiting to see what gcc ultimately does. */
3152 }
d85a05f0 3153 else
93311388
DE
3154 {
3155 /* Get the list of files included in the current compilation unit,
3156 and build a psymtab for each of them. */
d85a05f0 3157 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3158 }
ae038cb0 3159
93311388 3160 do_cleanups (back_to_inner);
ae038cb0 3161
93311388
DE
3162 return info_ptr;
3163}
ff013f42 3164
348e048f
DE
3165/* Traversal function for htab_traverse_noresize.
3166 Process one .debug_types comp-unit. */
3167
3168static int
3169process_type_comp_unit (void **slot, void *info)
3170{
3171 struct signatured_type *entry = (struct signatured_type *) *slot;
3172 struct objfile *objfile = (struct objfile *) info;
3173 struct dwarf2_per_cu_data *this_cu;
3174
3175 this_cu = &entry->per_cu;
348e048f 3176
be391dca 3177 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3178 process_psymtab_comp_unit (objfile, this_cu,
3179 dwarf2_per_objfile->types.buffer,
3180 dwarf2_per_objfile->types.buffer + entry->offset,
3181 dwarf2_per_objfile->types.size);
3182
3183 return 1;
3184}
3185
3186/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3187 Build partial symbol tables for the .debug_types comp-units. */
3188
3189static void
3190build_type_psymtabs (struct objfile *objfile)
3191{
3192 if (! create_debug_types_hash_table (objfile))
3193 return;
3194
3195 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3196 process_type_comp_unit, objfile);
3197}
3198
60606b2c
TT
3199/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3200
3201static void
3202psymtabs_addrmap_cleanup (void *o)
3203{
3204 struct objfile *objfile = o;
ec61707d 3205
60606b2c
TT
3206 objfile->psymtabs_addrmap = NULL;
3207}
3208
93311388
DE
3209/* Build the partial symbol table by doing a quick pass through the
3210 .debug_info and .debug_abbrev sections. */
72bf9492 3211
93311388 3212static void
c67a9c90 3213dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3214{
93311388 3215 gdb_byte *info_ptr;
60606b2c
TT
3216 struct cleanup *back_to, *addrmap_cleanup;
3217 struct obstack temp_obstack;
93311388 3218
98bfdba5
PA
3219 dwarf2_per_objfile->reading_partial_symbols = 1;
3220
be391dca 3221 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3222 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3223
93311388
DE
3224 /* Any cached compilation units will be linked by the per-objfile
3225 read_in_chain. Make sure to free them when we're done. */
3226 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3227
348e048f
DE
3228 build_type_psymtabs (objfile);
3229
93311388 3230 create_all_comp_units (objfile);
c906108c 3231
60606b2c
TT
3232 /* Create a temporary address map on a temporary obstack. We later
3233 copy this to the final obstack. */
3234 obstack_init (&temp_obstack);
3235 make_cleanup_obstack_free (&temp_obstack);
3236 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3237 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3238
93311388
DE
3239 /* Since the objects we're extracting from .debug_info vary in
3240 length, only the individual functions to extract them (like
3241 read_comp_unit_head and load_partial_die) can really know whether
3242 the buffer is large enough to hold another complete object.
c906108c 3243
93311388
DE
3244 At the moment, they don't actually check that. If .debug_info
3245 holds just one extra byte after the last compilation unit's dies,
3246 then read_comp_unit_head will happily read off the end of the
3247 buffer. read_partial_die is similarly casual. Those functions
3248 should be fixed.
c906108c 3249
93311388
DE
3250 For this loop condition, simply checking whether there's any data
3251 left at all should be sufficient. */
c906108c 3252
93311388
DE
3253 while (info_ptr < (dwarf2_per_objfile->info.buffer
3254 + dwarf2_per_objfile->info.size))
3255 {
3256 struct dwarf2_per_cu_data *this_cu;
dd373385 3257
93311388
DE
3258 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3259 objfile);
aaa75496 3260
93311388
DE
3261 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3262 dwarf2_per_objfile->info.buffer,
3263 info_ptr,
3264 dwarf2_per_objfile->info.size);
c906108c 3265 }
ff013f42
JK
3266
3267 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3268 &objfile->objfile_obstack);
60606b2c 3269 discard_cleanups (addrmap_cleanup);
ff013f42 3270
ae038cb0
DJ
3271 do_cleanups (back_to);
3272}
3273
93311388 3274/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3275
3276static void
93311388
DE
3277load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3278 struct objfile *objfile)
ae038cb0
DJ
3279{
3280 bfd *abfd = objfile->obfd;
fe1b8b76 3281 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3282 struct die_info *comp_unit_die;
ae038cb0 3283 struct dwarf2_cu *cu;
1d9ec526 3284 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3285 struct attribute *attr;
3286 int has_children;
3287 struct die_reader_specs reader_specs;
98bfdba5 3288 int read_cu = 0;
ae038cb0 3289
348e048f
DE
3290 gdb_assert (! this_cu->from_debug_types);
3291
be391dca 3292 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3293 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3294 beg_of_comp_unit = info_ptr;
3295
98bfdba5
PA
3296 if (this_cu->cu == NULL)
3297 {
3298 cu = alloc_one_comp_unit (objfile);
ae038cb0 3299
98bfdba5 3300 read_cu = 1;
ae038cb0 3301
98bfdba5
PA
3302 /* If an error occurs while loading, release our storage. */
3303 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3304
98bfdba5
PA
3305 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3306 dwarf2_per_objfile->info.buffer,
3307 dwarf2_per_objfile->info.size,
3308 abfd);
ae038cb0 3309
98bfdba5
PA
3310 /* Complete the cu_header. */
3311 cu->header.offset = this_cu->offset;
3312 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3313
3314 /* Link this compilation unit into the compilation unit tree. */
3315 this_cu->cu = cu;
3316 cu->per_cu = this_cu;
98bfdba5
PA
3317
3318 /* Link this CU into read_in_chain. */
3319 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3320 dwarf2_per_objfile->read_in_chain = this_cu;
3321 }
3322 else
3323 {
3324 cu = this_cu->cu;
3325 info_ptr += cu->header.first_die_offset;
3326 }
ae038cb0
DJ
3327
3328 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3329 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3330 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3331 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3332
3333 /* Read the compilation unit die. */
d85a05f0
DJ
3334 init_cu_die_reader (&reader_specs, cu);
3335 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3336 &has_children);
ae038cb0
DJ
3337
3338 /* Set the language we're debugging. */
d85a05f0
DJ
3339 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3340 if (attr)
3341 set_cu_language (DW_UNSND (attr), cu);
3342 else
3343 set_cu_language (language_minimal, cu);
ae038cb0 3344
ae038cb0
DJ
3345 /* Check if comp unit has_children.
3346 If so, read the rest of the partial symbols from this comp unit.
3347 If not, there's no more debug_info for this comp unit. */
d85a05f0 3348 if (has_children)
93311388 3349 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3350
98bfdba5
PA
3351 do_cleanups (free_abbrevs_cleanup);
3352
3353 if (read_cu)
3354 {
3355 /* We've successfully allocated this compilation unit. Let our
3356 caller clean it up when finished with it. */
3357 discard_cleanups (free_cu_cleanup);
3358 }
ae038cb0
DJ
3359}
3360
3361/* Create a list of all compilation units in OBJFILE. We do this only
3362 if an inter-comp-unit reference is found; presumably if there is one,
3363 there will be many, and one will occur early in the .debug_info section.
3364 So there's no point in building this list incrementally. */
3365
3366static void
3367create_all_comp_units (struct objfile *objfile)
3368{
3369 int n_allocated;
3370 int n_comp_units;
3371 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3372 gdb_byte *info_ptr;
3373
3374 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3375 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3376
3377 n_comp_units = 0;
3378 n_allocated = 10;
3379 all_comp_units = xmalloc (n_allocated
3380 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3381
dce234bc 3382 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3383 {
c764a876 3384 unsigned int length, initial_length_size;
ae038cb0 3385 struct dwarf2_per_cu_data *this_cu;
c764a876 3386 unsigned int offset;
ae038cb0 3387
dce234bc 3388 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3389
3390 /* Read just enough information to find out where the next
3391 compilation unit is. */
c764a876
DE
3392 length = read_initial_length (objfile->obfd, info_ptr,
3393 &initial_length_size);
ae038cb0
DJ
3394
3395 /* Save the compilation unit for later lookup. */
3396 this_cu = obstack_alloc (&objfile->objfile_obstack,
3397 sizeof (struct dwarf2_per_cu_data));
3398 memset (this_cu, 0, sizeof (*this_cu));
3399 this_cu->offset = offset;
c764a876 3400 this_cu->length = length + initial_length_size;
9291a0cd 3401 this_cu->objfile = objfile;
ae038cb0
DJ
3402
3403 if (n_comp_units == n_allocated)
3404 {
3405 n_allocated *= 2;
3406 all_comp_units = xrealloc (all_comp_units,
3407 n_allocated
3408 * sizeof (struct dwarf2_per_cu_data *));
3409 }
3410 all_comp_units[n_comp_units++] = this_cu;
3411
3412 info_ptr = info_ptr + this_cu->length;
3413 }
3414
3415 dwarf2_per_objfile->all_comp_units
3416 = obstack_alloc (&objfile->objfile_obstack,
3417 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3418 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3419 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3420 xfree (all_comp_units);
3421 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3422}
3423
5734ee8b
DJ
3424/* Process all loaded DIEs for compilation unit CU, starting at
3425 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3426 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3427 DW_AT_ranges). If NEED_PC is set, then this function will set
3428 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3429 and record the covered ranges in the addrmap. */
c906108c 3430
72bf9492
DJ
3431static void
3432scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3433 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3434{
72bf9492 3435 struct partial_die_info *pdi;
c906108c 3436
91c24f0a
DC
3437 /* Now, march along the PDI's, descending into ones which have
3438 interesting children but skipping the children of the other ones,
3439 until we reach the end of the compilation unit. */
c906108c 3440
72bf9492 3441 pdi = first_die;
91c24f0a 3442
72bf9492
DJ
3443 while (pdi != NULL)
3444 {
3445 fixup_partial_die (pdi, cu);
c906108c 3446
f55ee35c 3447 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3448 children, so we need to look at them. Ditto for anonymous
3449 enums. */
933c6fe4 3450
72bf9492 3451 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3452 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3453 {
72bf9492 3454 switch (pdi->tag)
c906108c
SS
3455 {
3456 case DW_TAG_subprogram:
5734ee8b 3457 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3458 break;
3459 case DW_TAG_variable:
3460 case DW_TAG_typedef:
91c24f0a 3461 case DW_TAG_union_type:
72bf9492 3462 if (!pdi->is_declaration)
63d06c5c 3463 {
72bf9492 3464 add_partial_symbol (pdi, cu);
63d06c5c
DC
3465 }
3466 break;
c906108c 3467 case DW_TAG_class_type:
680b30c7 3468 case DW_TAG_interface_type:
c906108c 3469 case DW_TAG_structure_type:
72bf9492 3470 if (!pdi->is_declaration)
c906108c 3471 {
72bf9492 3472 add_partial_symbol (pdi, cu);
c906108c
SS
3473 }
3474 break;
91c24f0a 3475 case DW_TAG_enumeration_type:
72bf9492
DJ
3476 if (!pdi->is_declaration)
3477 add_partial_enumeration (pdi, cu);
c906108c
SS
3478 break;
3479 case DW_TAG_base_type:
a02abb62 3480 case DW_TAG_subrange_type:
c906108c 3481 /* File scope base type definitions are added to the partial
c5aa993b 3482 symbol table. */
72bf9492 3483 add_partial_symbol (pdi, cu);
c906108c 3484 break;
d9fa45fe 3485 case DW_TAG_namespace:
5734ee8b 3486 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3487 break;
5d7cb8df
JK
3488 case DW_TAG_module:
3489 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3490 break;
c906108c
SS
3491 default:
3492 break;
3493 }
3494 }
3495
72bf9492
DJ
3496 /* If the die has a sibling, skip to the sibling. */
3497
3498 pdi = pdi->die_sibling;
3499 }
3500}
3501
3502/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3503
72bf9492 3504 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3505 name is concatenated with "::" and the partial DIE's name. For
3506 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3507 Enumerators are an exception; they use the scope of their parent
3508 enumeration type, i.e. the name of the enumeration type is not
3509 prepended to the enumerator.
91c24f0a 3510
72bf9492
DJ
3511 There are two complexities. One is DW_AT_specification; in this
3512 case "parent" means the parent of the target of the specification,
3513 instead of the direct parent of the DIE. The other is compilers
3514 which do not emit DW_TAG_namespace; in this case we try to guess
3515 the fully qualified name of structure types from their members'
3516 linkage names. This must be done using the DIE's children rather
3517 than the children of any DW_AT_specification target. We only need
3518 to do this for structures at the top level, i.e. if the target of
3519 any DW_AT_specification (if any; otherwise the DIE itself) does not
3520 have a parent. */
3521
3522/* Compute the scope prefix associated with PDI's parent, in
3523 compilation unit CU. The result will be allocated on CU's
3524 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3525 field. NULL is returned if no prefix is necessary. */
3526static char *
3527partial_die_parent_scope (struct partial_die_info *pdi,
3528 struct dwarf2_cu *cu)
3529{
3530 char *grandparent_scope;
3531 struct partial_die_info *parent, *real_pdi;
91c24f0a 3532
72bf9492
DJ
3533 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3534 then this means the parent of the specification DIE. */
3535
3536 real_pdi = pdi;
72bf9492 3537 while (real_pdi->has_specification)
10b3939b 3538 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3539
3540 parent = real_pdi->die_parent;
3541 if (parent == NULL)
3542 return NULL;
3543
3544 if (parent->scope_set)
3545 return parent->scope;
3546
3547 fixup_partial_die (parent, cu);
3548
10b3939b 3549 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3550
acebe513
UW
3551 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3552 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3553 Work around this problem here. */
3554 if (cu->language == language_cplus
6e70227d 3555 && parent->tag == DW_TAG_namespace
acebe513
UW
3556 && strcmp (parent->name, "::") == 0
3557 && grandparent_scope == NULL)
3558 {
3559 parent->scope = NULL;
3560 parent->scope_set = 1;
3561 return NULL;
3562 }
3563
72bf9492 3564 if (parent->tag == DW_TAG_namespace
f55ee35c 3565 || parent->tag == DW_TAG_module
72bf9492
DJ
3566 || parent->tag == DW_TAG_structure_type
3567 || parent->tag == DW_TAG_class_type
680b30c7 3568 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3569 || parent->tag == DW_TAG_union_type
3570 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3571 {
3572 if (grandparent_scope == NULL)
3573 parent->scope = parent->name;
3574 else
987504bb 3575 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3576 parent->name, 0, cu);
72bf9492 3577 }
ceeb3d5a 3578 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3579 /* Enumerators should not get the name of the enumeration as a prefix. */
3580 parent->scope = grandparent_scope;
3581 else
3582 {
3583 /* FIXME drow/2004-04-01: What should we be doing with
3584 function-local names? For partial symbols, we should probably be
3585 ignoring them. */
3586 complaint (&symfile_complaints,
e2e0b3e5 3587 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3588 parent->tag, pdi->offset);
3589 parent->scope = grandparent_scope;
c906108c
SS
3590 }
3591
72bf9492
DJ
3592 parent->scope_set = 1;
3593 return parent->scope;
3594}
3595
3596/* Return the fully scoped name associated with PDI, from compilation unit
3597 CU. The result will be allocated with malloc. */
3598static char *
3599partial_die_full_name (struct partial_die_info *pdi,
3600 struct dwarf2_cu *cu)
3601{
3602 char *parent_scope;
3603
98bfdba5
PA
3604 /* If this is a template instantiation, we can not work out the
3605 template arguments from partial DIEs. So, unfortunately, we have
3606 to go through the full DIEs. At least any work we do building
3607 types here will be reused if full symbols are loaded later. */
3608 if (pdi->has_template_arguments)
3609 {
3610 fixup_partial_die (pdi, cu);
3611
3612 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3613 {
3614 struct die_info *die;
3615 struct attribute attr;
3616 struct dwarf2_cu *ref_cu = cu;
3617
3618 attr.name = 0;
3619 attr.form = DW_FORM_ref_addr;
3620 attr.u.addr = pdi->offset;
3621 die = follow_die_ref (NULL, &attr, &ref_cu);
3622
3623 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3624 }
3625 }
3626
72bf9492
DJ
3627 parent_scope = partial_die_parent_scope (pdi, cu);
3628 if (parent_scope == NULL)
3629 return NULL;
3630 else
f55ee35c 3631 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3632}
3633
3634static void
72bf9492 3635add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3636{
e7c27a73 3637 struct objfile *objfile = cu->objfile;
c906108c 3638 CORE_ADDR addr = 0;
decbce07 3639 char *actual_name = NULL;
5c4e30ca 3640 const struct partial_symbol *psym = NULL;
e142c38c 3641 CORE_ADDR baseaddr;
72bf9492 3642 int built_actual_name = 0;
e142c38c
DJ
3643
3644 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3645
94af9270
KS
3646 actual_name = partial_die_full_name (pdi, cu);
3647 if (actual_name)
3648 built_actual_name = 1;
63d06c5c 3649
72bf9492
DJ
3650 if (actual_name == NULL)
3651 actual_name = pdi->name;
3652
c906108c
SS
3653 switch (pdi->tag)
3654 {
3655 case DW_TAG_subprogram:
2cfa0c8d 3656 if (pdi->is_external || cu->language == language_ada)
c906108c 3657 {
2cfa0c8d
JB
3658 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3659 of the global scope. But in Ada, we want to be able to access
3660 nested procedures globally. So all Ada subprograms are stored
3661 in the global scope. */
38d518c9 3662 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3663 mst_text, objfile); */
38d518c9 3664 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3665 built_actual_name,
5c4e30ca
DC
3666 VAR_DOMAIN, LOC_BLOCK,
3667 &objfile->global_psymbols,
3668 0, pdi->lowpc + baseaddr,
e142c38c 3669 cu->language, objfile);
c906108c
SS
3670 }
3671 else
3672 {
38d518c9 3673 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3674 mst_file_text, objfile); */
38d518c9 3675 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3676 built_actual_name,
5c4e30ca
DC
3677 VAR_DOMAIN, LOC_BLOCK,
3678 &objfile->static_psymbols,
3679 0, pdi->lowpc + baseaddr,
e142c38c 3680 cu->language, objfile);
c906108c
SS
3681 }
3682 break;
3683 case DW_TAG_variable:
caac4577
JG
3684 if (pdi->locdesc)
3685 addr = decode_locdesc (pdi->locdesc, cu);
3686
3687 if (pdi->locdesc
3688 && addr == 0
3689 && !dwarf2_per_objfile->has_section_at_zero)
3690 {
3691 /* A global or static variable may also have been stripped
3692 out by the linker if unused, in which case its address
3693 will be nullified; do not add such variables into partial
3694 symbol table then. */
3695 }
3696 else if (pdi->is_external)
c906108c
SS
3697 {
3698 /* Global Variable.
3699 Don't enter into the minimal symbol tables as there is
3700 a minimal symbol table entry from the ELF symbols already.
3701 Enter into partial symbol table if it has a location
3702 descriptor or a type.
3703 If the location descriptor is missing, new_symbol will create
3704 a LOC_UNRESOLVED symbol, the address of the variable will then
3705 be determined from the minimal symbol table whenever the variable
3706 is referenced.
3707 The address for the partial symbol table entry is not
3708 used by GDB, but it comes in handy for debugging partial symbol
3709 table building. */
3710
c906108c 3711 if (pdi->locdesc || pdi->has_type)
38d518c9 3712 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3713 built_actual_name,
5c4e30ca
DC
3714 VAR_DOMAIN, LOC_STATIC,
3715 &objfile->global_psymbols,
3716 0, addr + baseaddr,
e142c38c 3717 cu->language, objfile);
c906108c
SS
3718 }
3719 else
3720 {
3721 /* Static Variable. Skip symbols without location descriptors. */
3722 if (pdi->locdesc == NULL)
decbce07
MS
3723 {
3724 if (built_actual_name)
3725 xfree (actual_name);
3726 return;
3727 }
38d518c9 3728 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3729 mst_file_data, objfile); */
38d518c9 3730 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3731 built_actual_name,
5c4e30ca
DC
3732 VAR_DOMAIN, LOC_STATIC,
3733 &objfile->static_psymbols,
3734 0, addr + baseaddr,
e142c38c 3735 cu->language, objfile);
c906108c
SS
3736 }
3737 break;
3738 case DW_TAG_typedef:
3739 case DW_TAG_base_type:
a02abb62 3740 case DW_TAG_subrange_type:
38d518c9 3741 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3742 built_actual_name,
176620f1 3743 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3744 &objfile->static_psymbols,
e142c38c 3745 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3746 break;
72bf9492
DJ
3747 case DW_TAG_namespace:
3748 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3749 built_actual_name,
72bf9492
DJ
3750 VAR_DOMAIN, LOC_TYPEDEF,
3751 &objfile->global_psymbols,
3752 0, (CORE_ADDR) 0, cu->language, objfile);
3753 break;
c906108c 3754 case DW_TAG_class_type:
680b30c7 3755 case DW_TAG_interface_type:
c906108c
SS
3756 case DW_TAG_structure_type:
3757 case DW_TAG_union_type:
3758 case DW_TAG_enumeration_type:
fa4028e9
JB
3759 /* Skip external references. The DWARF standard says in the section
3760 about "Structure, Union, and Class Type Entries": "An incomplete
3761 structure, union or class type is represented by a structure,
3762 union or class entry that does not have a byte size attribute
3763 and that has a DW_AT_declaration attribute." */
3764 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3765 {
3766 if (built_actual_name)
3767 xfree (actual_name);
3768 return;
3769 }
fa4028e9 3770
63d06c5c
DC
3771 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3772 static vs. global. */
38d518c9 3773 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3774 built_actual_name,
176620f1 3775 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3776 (cu->language == language_cplus
3777 || cu->language == language_java)
63d06c5c
DC
3778 ? &objfile->global_psymbols
3779 : &objfile->static_psymbols,
e142c38c 3780 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3781
c906108c
SS
3782 break;
3783 case DW_TAG_enumerator:
38d518c9 3784 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3785 built_actual_name,
176620f1 3786 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3787 (cu->language == language_cplus
3788 || cu->language == language_java)
f6fe98ef
DJ
3789 ? &objfile->global_psymbols
3790 : &objfile->static_psymbols,
e142c38c 3791 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3792 break;
3793 default:
3794 break;
3795 }
5c4e30ca 3796
72bf9492
DJ
3797 if (built_actual_name)
3798 xfree (actual_name);
c906108c
SS
3799}
3800
5c4e30ca
DC
3801/* Read a partial die corresponding to a namespace; also, add a symbol
3802 corresponding to that namespace to the symbol table. NAMESPACE is
3803 the name of the enclosing namespace. */
91c24f0a 3804
72bf9492
DJ
3805static void
3806add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3807 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3808 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3809{
72bf9492 3810 /* Add a symbol for the namespace. */
e7c27a73 3811
72bf9492 3812 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3813
3814 /* Now scan partial symbols in that namespace. */
3815
91c24f0a 3816 if (pdi->has_children)
5734ee8b 3817 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3818}
3819
5d7cb8df
JK
3820/* Read a partial die corresponding to a Fortran module. */
3821
3822static void
3823add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3824 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3825{
f55ee35c 3826 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3827
3828 if (pdi->has_children)
3829 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3830}
3831
bc30ff58
JB
3832/* Read a partial die corresponding to a subprogram and create a partial
3833 symbol for that subprogram. When the CU language allows it, this
3834 routine also defines a partial symbol for each nested subprogram
3835 that this subprogram contains.
6e70227d 3836
bc30ff58
JB
3837 DIE my also be a lexical block, in which case we simply search
3838 recursively for suprograms defined inside that lexical block.
3839 Again, this is only performed when the CU language allows this
3840 type of definitions. */
3841
3842static void
3843add_partial_subprogram (struct partial_die_info *pdi,
3844 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3845 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3846{
3847 if (pdi->tag == DW_TAG_subprogram)
3848 {
3849 if (pdi->has_pc_info)
3850 {
3851 if (pdi->lowpc < *lowpc)
3852 *lowpc = pdi->lowpc;
3853 if (pdi->highpc > *highpc)
3854 *highpc = pdi->highpc;
5734ee8b
DJ
3855 if (need_pc)
3856 {
3857 CORE_ADDR baseaddr;
3858 struct objfile *objfile = cu->objfile;
3859
3860 baseaddr = ANOFFSET (objfile->section_offsets,
3861 SECT_OFF_TEXT (objfile));
3862 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3863 pdi->lowpc + baseaddr,
3864 pdi->highpc - 1 + baseaddr,
9291a0cd 3865 cu->per_cu->v.psymtab);
5734ee8b 3866 }
bc30ff58 3867 if (!pdi->is_declaration)
e8d05480
JB
3868 /* Ignore subprogram DIEs that do not have a name, they are
3869 illegal. Do not emit a complaint at this point, we will
3870 do so when we convert this psymtab into a symtab. */
3871 if (pdi->name)
3872 add_partial_symbol (pdi, cu);
bc30ff58
JB
3873 }
3874 }
6e70227d 3875
bc30ff58
JB
3876 if (! pdi->has_children)
3877 return;
3878
3879 if (cu->language == language_ada)
3880 {
3881 pdi = pdi->die_child;
3882 while (pdi != NULL)
3883 {
3884 fixup_partial_die (pdi, cu);
3885 if (pdi->tag == DW_TAG_subprogram
3886 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3887 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3888 pdi = pdi->die_sibling;
3889 }
3890 }
3891}
3892
72bf9492
DJ
3893/* See if we can figure out if the class lives in a namespace. We do
3894 this by looking for a member function; its demangled name will
3895 contain namespace info, if there is any. */
63d06c5c 3896
72bf9492
DJ
3897static void
3898guess_structure_name (struct partial_die_info *struct_pdi,
3899 struct dwarf2_cu *cu)
63d06c5c 3900{
987504bb
JJ
3901 if ((cu->language == language_cplus
3902 || cu->language == language_java)
72bf9492 3903 && cu->has_namespace_info == 0
63d06c5c
DC
3904 && struct_pdi->has_children)
3905 {
63d06c5c
DC
3906 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3907 what template types look like, because the demangler
3908 frequently doesn't give the same name as the debug info. We
3909 could fix this by only using the demangled name to get the
134d01f1 3910 prefix (but see comment in read_structure_type). */
63d06c5c 3911
72bf9492 3912 struct partial_die_info *real_pdi;
5d51ca54 3913
72bf9492
DJ
3914 /* If this DIE (this DIE's specification, if any) has a parent, then
3915 we should not do this. We'll prepend the parent's fully qualified
3916 name when we create the partial symbol. */
5d51ca54 3917
72bf9492 3918 real_pdi = struct_pdi;
72bf9492 3919 while (real_pdi->has_specification)
10b3939b 3920 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3921
72bf9492
DJ
3922 if (real_pdi->die_parent != NULL)
3923 return;
63d06c5c 3924 }
63d06c5c
DC
3925}
3926
91c24f0a
DC
3927/* Read a partial die corresponding to an enumeration type. */
3928
72bf9492
DJ
3929static void
3930add_partial_enumeration (struct partial_die_info *enum_pdi,
3931 struct dwarf2_cu *cu)
91c24f0a 3932{
72bf9492 3933 struct partial_die_info *pdi;
91c24f0a
DC
3934
3935 if (enum_pdi->name != NULL)
72bf9492
DJ
3936 add_partial_symbol (enum_pdi, cu);
3937
3938 pdi = enum_pdi->die_child;
3939 while (pdi)
91c24f0a 3940 {
72bf9492 3941 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3942 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3943 else
72bf9492
DJ
3944 add_partial_symbol (pdi, cu);
3945 pdi = pdi->die_sibling;
91c24f0a 3946 }
91c24f0a
DC
3947}
3948
4bb7a0a7
DJ
3949/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3950 Return the corresponding abbrev, or NULL if the number is zero (indicating
3951 an empty DIE). In either case *BYTES_READ will be set to the length of
3952 the initial number. */
3953
3954static struct abbrev_info *
fe1b8b76 3955peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3956 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3957{
3958 bfd *abfd = cu->objfile->obfd;
3959 unsigned int abbrev_number;
3960 struct abbrev_info *abbrev;
3961
3962 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3963
3964 if (abbrev_number == 0)
3965 return NULL;
3966
3967 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3968 if (!abbrev)
3969 {
8a3fe4f8 3970 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3971 bfd_get_filename (abfd));
3972 }
3973
3974 return abbrev;
3975}
3976
93311388
DE
3977/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3978 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3979 DIE. Any children of the skipped DIEs will also be skipped. */
3980
fe1b8b76 3981static gdb_byte *
93311388 3982skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3983{
3984 struct abbrev_info *abbrev;
3985 unsigned int bytes_read;
3986
3987 while (1)
3988 {
3989 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3990 if (abbrev == NULL)
3991 return info_ptr + bytes_read;
3992 else
93311388 3993 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3994 }
3995}
3996
93311388
DE
3997/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3998 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3999 abbrev corresponding to that skipped uleb128 should be passed in
4000 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4001 children. */
4002
fe1b8b76 4003static gdb_byte *
93311388
DE
4004skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4005 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4006{
4007 unsigned int bytes_read;
4008 struct attribute attr;
4009 bfd *abfd = cu->objfile->obfd;
4010 unsigned int form, i;
4011
4012 for (i = 0; i < abbrev->num_attrs; i++)
4013 {
4014 /* The only abbrev we care about is DW_AT_sibling. */
4015 if (abbrev->attrs[i].name == DW_AT_sibling)
4016 {
4017 read_attribute (&attr, &abbrev->attrs[i],
4018 abfd, info_ptr, cu);
4019 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4020 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4021 else
93311388 4022 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4023 }
4024
4025 /* If it isn't DW_AT_sibling, skip this attribute. */
4026 form = abbrev->attrs[i].form;
4027 skip_attribute:
4028 switch (form)
4029 {
4bb7a0a7 4030 case DW_FORM_ref_addr:
ae411497
TT
4031 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4032 and later it is offset sized. */
4033 if (cu->header.version == 2)
4034 info_ptr += cu->header.addr_size;
4035 else
4036 info_ptr += cu->header.offset_size;
4037 break;
4038 case DW_FORM_addr:
4bb7a0a7
DJ
4039 info_ptr += cu->header.addr_size;
4040 break;
4041 case DW_FORM_data1:
4042 case DW_FORM_ref1:
4043 case DW_FORM_flag:
4044 info_ptr += 1;
4045 break;
2dc7f7b3
TT
4046 case DW_FORM_flag_present:
4047 break;
4bb7a0a7
DJ
4048 case DW_FORM_data2:
4049 case DW_FORM_ref2:
4050 info_ptr += 2;
4051 break;
4052 case DW_FORM_data4:
4053 case DW_FORM_ref4:
4054 info_ptr += 4;
4055 break;
4056 case DW_FORM_data8:
4057 case DW_FORM_ref8:
348e048f 4058 case DW_FORM_sig8:
4bb7a0a7
DJ
4059 info_ptr += 8;
4060 break;
4061 case DW_FORM_string:
9b1c24c8 4062 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4063 info_ptr += bytes_read;
4064 break;
2dc7f7b3 4065 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4066 case DW_FORM_strp:
4067 info_ptr += cu->header.offset_size;
4068 break;
2dc7f7b3 4069 case DW_FORM_exprloc:
4bb7a0a7
DJ
4070 case DW_FORM_block:
4071 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4072 info_ptr += bytes_read;
4073 break;
4074 case DW_FORM_block1:
4075 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4076 break;
4077 case DW_FORM_block2:
4078 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4079 break;
4080 case DW_FORM_block4:
4081 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4082 break;
4083 case DW_FORM_sdata:
4084 case DW_FORM_udata:
4085 case DW_FORM_ref_udata:
4086 info_ptr = skip_leb128 (abfd, info_ptr);
4087 break;
4088 case DW_FORM_indirect:
4089 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4090 info_ptr += bytes_read;
4091 /* We need to continue parsing from here, so just go back to
4092 the top. */
4093 goto skip_attribute;
4094
4095 default:
8a3fe4f8 4096 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4097 dwarf_form_name (form),
4098 bfd_get_filename (abfd));
4099 }
4100 }
4101
4102 if (abbrev->has_children)
93311388 4103 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4104 else
4105 return info_ptr;
4106}
4107
93311388
DE
4108/* Locate ORIG_PDI's sibling.
4109 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4110 in BUFFER. */
91c24f0a 4111
fe1b8b76 4112static gdb_byte *
93311388
DE
4113locate_pdi_sibling (struct partial_die_info *orig_pdi,
4114 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4115 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4116{
4117 /* Do we know the sibling already? */
72bf9492 4118
91c24f0a
DC
4119 if (orig_pdi->sibling)
4120 return orig_pdi->sibling;
4121
4122 /* Are there any children to deal with? */
4123
4124 if (!orig_pdi->has_children)
4125 return info_ptr;
4126
4bb7a0a7 4127 /* Skip the children the long way. */
91c24f0a 4128
93311388 4129 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4130}
4131
c906108c
SS
4132/* Expand this partial symbol table into a full symbol table. */
4133
4134static void
fba45db2 4135dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4136{
c906108c
SS
4137 if (pst != NULL)
4138 {
4139 if (pst->readin)
4140 {
8a3fe4f8 4141 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4142 }
4143 else
4144 {
4145 if (info_verbose)
4146 {
a3f17187 4147 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4148 gdb_flush (gdb_stdout);
4149 }
4150
10b3939b
DJ
4151 /* Restore our global data. */
4152 dwarf2_per_objfile = objfile_data (pst->objfile,
4153 dwarf2_objfile_data_key);
4154
b2ab525c
KB
4155 /* If this psymtab is constructed from a debug-only objfile, the
4156 has_section_at_zero flag will not necessarily be correct. We
4157 can get the correct value for this flag by looking at the data
4158 associated with the (presumably stripped) associated objfile. */
4159 if (pst->objfile->separate_debug_objfile_backlink)
4160 {
4161 struct dwarf2_per_objfile *dpo_backlink
4162 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4163 dwarf2_objfile_data_key);
9a619af0 4164
b2ab525c
KB
4165 dwarf2_per_objfile->has_section_at_zero
4166 = dpo_backlink->has_section_at_zero;
4167 }
4168
98bfdba5
PA
4169 dwarf2_per_objfile->reading_partial_symbols = 0;
4170
c906108c
SS
4171 psymtab_to_symtab_1 (pst);
4172
4173 /* Finish up the debug error message. */
4174 if (info_verbose)
a3f17187 4175 printf_filtered (_("done.\n"));
c906108c
SS
4176 }
4177 }
4178}
4179
10b3939b
DJ
4180/* Add PER_CU to the queue. */
4181
4182static void
03dd20cc 4183queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4184{
4185 struct dwarf2_queue_item *item;
4186
4187 per_cu->queued = 1;
4188 item = xmalloc (sizeof (*item));
4189 item->per_cu = per_cu;
4190 item->next = NULL;
4191
4192 if (dwarf2_queue == NULL)
4193 dwarf2_queue = item;
4194 else
4195 dwarf2_queue_tail->next = item;
4196
4197 dwarf2_queue_tail = item;
4198}
4199
4200/* Process the queue. */
4201
4202static void
4203process_queue (struct objfile *objfile)
4204{
4205 struct dwarf2_queue_item *item, *next_item;
4206
03dd20cc
DJ
4207 /* The queue starts out with one item, but following a DIE reference
4208 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4209 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4210 {
9291a0cd
TT
4211 if (dwarf2_per_objfile->using_index
4212 ? !item->per_cu->v.quick->symtab
4213 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4214 process_full_comp_unit (item->per_cu);
4215
4216 item->per_cu->queued = 0;
4217 next_item = item->next;
4218 xfree (item);
4219 }
4220
4221 dwarf2_queue_tail = NULL;
4222}
4223
4224/* Free all allocated queue entries. This function only releases anything if
4225 an error was thrown; if the queue was processed then it would have been
4226 freed as we went along. */
4227
4228static void
4229dwarf2_release_queue (void *dummy)
4230{
4231 struct dwarf2_queue_item *item, *last;
4232
4233 item = dwarf2_queue;
4234 while (item)
4235 {
4236 /* Anything still marked queued is likely to be in an
4237 inconsistent state, so discard it. */
4238 if (item->per_cu->queued)
4239 {
4240 if (item->per_cu->cu != NULL)
4241 free_one_cached_comp_unit (item->per_cu->cu);
4242 item->per_cu->queued = 0;
4243 }
4244
4245 last = item;
4246 item = item->next;
4247 xfree (last);
4248 }
4249
4250 dwarf2_queue = dwarf2_queue_tail = NULL;
4251}
4252
4253/* Read in full symbols for PST, and anything it depends on. */
4254
c906108c 4255static void
fba45db2 4256psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4257{
10b3939b 4258 struct dwarf2_per_cu_data *per_cu;
c906108c 4259 struct cleanup *back_to;
aaa75496
JB
4260 int i;
4261
4262 for (i = 0; i < pst->number_of_dependencies; i++)
4263 if (!pst->dependencies[i]->readin)
4264 {
4265 /* Inform about additional files that need to be read in. */
4266 if (info_verbose)
4267 {
a3f17187 4268 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4269 fputs_filtered (" ", gdb_stdout);
4270 wrap_here ("");
4271 fputs_filtered ("and ", gdb_stdout);
4272 wrap_here ("");
4273 printf_filtered ("%s...", pst->dependencies[i]->filename);
4274 wrap_here (""); /* Flush output */
4275 gdb_flush (gdb_stdout);
4276 }
4277 psymtab_to_symtab_1 (pst->dependencies[i]);
4278 }
4279
e38df1d0 4280 per_cu = pst->read_symtab_private;
10b3939b
DJ
4281
4282 if (per_cu == NULL)
aaa75496
JB
4283 {
4284 /* It's an include file, no symbols to read for it.
4285 Everything is in the parent symtab. */
4286 pst->readin = 1;
4287 return;
4288 }
c906108c 4289
9291a0cd 4290 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4291}
4292
93311388 4293/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4294
93311388 4295static void
31ffec48 4296load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4297{
31ffec48 4298 bfd *abfd = objfile->obfd;
10b3939b 4299 struct dwarf2_cu *cu;
c764a876 4300 unsigned int offset;
93311388 4301 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4302 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4303 struct attribute *attr;
98bfdba5 4304 int read_cu = 0;
6502dd73 4305
348e048f
DE
4306 gdb_assert (! per_cu->from_debug_types);
4307
c906108c 4308 /* Set local variables from the partial symbol table info. */
10b3939b 4309 offset = per_cu->offset;
6502dd73 4310
be391dca 4311 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4312 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4313 beg_of_comp_unit = info_ptr;
63d06c5c 4314
98bfdba5
PA
4315 if (per_cu->cu == NULL)
4316 {
4317 cu = alloc_one_comp_unit (objfile);
4318
4319 read_cu = 1;
c906108c 4320
98bfdba5
PA
4321 /* If an error occurs while loading, release our storage. */
4322 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4323
98bfdba5
PA
4324 /* Read in the comp_unit header. */
4325 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4326
98bfdba5
PA
4327 /* Complete the cu_header. */
4328 cu->header.offset = offset;
4329 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4330
98bfdba5
PA
4331 /* Read the abbrevs for this compilation unit. */
4332 dwarf2_read_abbrevs (abfd, cu);
4333 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4334
98bfdba5
PA
4335 /* Link this compilation unit into the compilation unit tree. */
4336 per_cu->cu = cu;
4337 cu->per_cu = per_cu;
98bfdba5
PA
4338
4339 /* Link this CU into read_in_chain. */
4340 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4341 dwarf2_per_objfile->read_in_chain = per_cu;
4342 }
4343 else
4344 {
4345 cu = per_cu->cu;
4346 info_ptr += cu->header.first_die_offset;
4347 }
e142c38c 4348
93311388 4349 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4350
4351 /* We try not to read any attributes in this function, because not
4352 all objfiles needed for references have been loaded yet, and symbol
4353 table processing isn't initialized. But we have to set the CU language,
4354 or we won't be able to build types correctly. */
4355 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4356 if (attr)
4357 set_cu_language (DW_UNSND (attr), cu);
4358 else
4359 set_cu_language (language_minimal, cu);
4360
a6c727b2
DJ
4361 /* Similarly, if we do not read the producer, we can not apply
4362 producer-specific interpretation. */
4363 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4364 if (attr)
4365 cu->producer = DW_STRING (attr);
4366
98bfdba5
PA
4367 if (read_cu)
4368 {
4369 do_cleanups (free_abbrevs_cleanup);
e142c38c 4370
98bfdba5
PA
4371 /* We've successfully allocated this compilation unit. Let our
4372 caller clean it up when finished with it. */
4373 discard_cleanups (free_cu_cleanup);
4374 }
10b3939b
DJ
4375}
4376
3da10d80
KS
4377/* Add a DIE to the delayed physname list. */
4378
4379static void
4380add_to_method_list (struct type *type, int fnfield_index, int index,
4381 const char *name, struct die_info *die,
4382 struct dwarf2_cu *cu)
4383{
4384 struct delayed_method_info mi;
4385 mi.type = type;
4386 mi.fnfield_index = fnfield_index;
4387 mi.index = index;
4388 mi.name = name;
4389 mi.die = die;
4390 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4391}
4392
4393/* A cleanup for freeing the delayed method list. */
4394
4395static void
4396free_delayed_list (void *ptr)
4397{
4398 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4399 if (cu->method_list != NULL)
4400 {
4401 VEC_free (delayed_method_info, cu->method_list);
4402 cu->method_list = NULL;
4403 }
4404}
4405
4406/* Compute the physnames of any methods on the CU's method list.
4407
4408 The computation of method physnames is delayed in order to avoid the
4409 (bad) condition that one of the method's formal parameters is of an as yet
4410 incomplete type. */
4411
4412static void
4413compute_delayed_physnames (struct dwarf2_cu *cu)
4414{
4415 int i;
4416 struct delayed_method_info *mi;
4417 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4418 {
4419 char *physname;
4420 struct fn_fieldlist *fn_flp
4421 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4422 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4423 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4424 }
4425}
4426
10b3939b
DJ
4427/* Generate full symbol information for PST and CU, whose DIEs have
4428 already been loaded into memory. */
4429
4430static void
4431process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4432{
10b3939b 4433 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4434 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4435 CORE_ADDR lowpc, highpc;
4436 struct symtab *symtab;
3da10d80 4437 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4438 CORE_ADDR baseaddr;
4439
4440 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4441
10b3939b
DJ
4442 buildsym_init ();
4443 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4444 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4445
4446 cu->list_in_scope = &file_symbols;
c906108c 4447
d85a05f0 4448 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4449
c906108c 4450 /* Do line number decoding in read_file_scope () */
10b3939b 4451 process_die (cu->dies, cu);
c906108c 4452
3da10d80
KS
4453 /* Now that we have processed all the DIEs in the CU, all the types
4454 should be complete, and it should now be safe to compute all of the
4455 physnames. */
4456 compute_delayed_physnames (cu);
4457 do_cleanups (delayed_list_cleanup);
4458
fae299cd
DC
4459 /* Some compilers don't define a DW_AT_high_pc attribute for the
4460 compilation unit. If the DW_AT_high_pc is missing, synthesize
4461 it, by scanning the DIE's below the compilation unit. */
10b3939b 4462 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4463
613e1657 4464 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4465
4466 /* Set symtab language to language from DW_AT_language.
4467 If the compilation is from a C file generated by language preprocessors,
4468 do not set the language if it was already deduced by start_subfile. */
4469 if (symtab != NULL
10b3939b 4470 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4471 {
10b3939b 4472 symtab->language = cu->language;
c906108c 4473 }
9291a0cd
TT
4474
4475 if (dwarf2_per_objfile->using_index)
4476 per_cu->v.quick->symtab = symtab;
4477 else
4478 {
4479 struct partial_symtab *pst = per_cu->v.psymtab;
4480 pst->symtab = symtab;
4481 pst->readin = 1;
4482 }
c906108c
SS
4483
4484 do_cleanups (back_to);
4485}
4486
4487/* Process a die and its children. */
4488
4489static void
e7c27a73 4490process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4491{
4492 switch (die->tag)
4493 {
4494 case DW_TAG_padding:
4495 break;
4496 case DW_TAG_compile_unit:
e7c27a73 4497 read_file_scope (die, cu);
c906108c 4498 break;
348e048f
DE
4499 case DW_TAG_type_unit:
4500 read_type_unit_scope (die, cu);
4501 break;
c906108c 4502 case DW_TAG_subprogram:
c906108c 4503 case DW_TAG_inlined_subroutine:
edb3359d 4504 read_func_scope (die, cu);
c906108c
SS
4505 break;
4506 case DW_TAG_lexical_block:
14898363
L
4507 case DW_TAG_try_block:
4508 case DW_TAG_catch_block:
e7c27a73 4509 read_lexical_block_scope (die, cu);
c906108c
SS
4510 break;
4511 case DW_TAG_class_type:
680b30c7 4512 case DW_TAG_interface_type:
c906108c
SS
4513 case DW_TAG_structure_type:
4514 case DW_TAG_union_type:
134d01f1 4515 process_structure_scope (die, cu);
c906108c
SS
4516 break;
4517 case DW_TAG_enumeration_type:
134d01f1 4518 process_enumeration_scope (die, cu);
c906108c 4519 break;
134d01f1 4520
f792889a
DJ
4521 /* These dies have a type, but processing them does not create
4522 a symbol or recurse to process the children. Therefore we can
4523 read them on-demand through read_type_die. */
c906108c 4524 case DW_TAG_subroutine_type:
72019c9c 4525 case DW_TAG_set_type:
c906108c 4526 case DW_TAG_array_type:
c906108c 4527 case DW_TAG_pointer_type:
c906108c 4528 case DW_TAG_ptr_to_member_type:
c906108c 4529 case DW_TAG_reference_type:
c906108c 4530 case DW_TAG_string_type:
c906108c 4531 break;
134d01f1 4532
c906108c 4533 case DW_TAG_base_type:
a02abb62 4534 case DW_TAG_subrange_type:
cb249c71 4535 case DW_TAG_typedef:
134d01f1
DJ
4536 /* Add a typedef symbol for the type definition, if it has a
4537 DW_AT_name. */
f792889a 4538 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4539 break;
c906108c 4540 case DW_TAG_common_block:
e7c27a73 4541 read_common_block (die, cu);
c906108c
SS
4542 break;
4543 case DW_TAG_common_inclusion:
4544 break;
d9fa45fe 4545 case DW_TAG_namespace:
63d06c5c 4546 processing_has_namespace_info = 1;
e7c27a73 4547 read_namespace (die, cu);
d9fa45fe 4548 break;
5d7cb8df 4549 case DW_TAG_module:
f55ee35c 4550 processing_has_namespace_info = 1;
5d7cb8df
JK
4551 read_module (die, cu);
4552 break;
d9fa45fe
DC
4553 case DW_TAG_imported_declaration:
4554 case DW_TAG_imported_module:
63d06c5c 4555 processing_has_namespace_info = 1;
27aa8d6a
SW
4556 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4557 || cu->language != language_fortran))
4558 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4559 dwarf_tag_name (die->tag));
4560 read_import_statement (die, cu);
d9fa45fe 4561 break;
c906108c 4562 default:
e7c27a73 4563 new_symbol (die, NULL, cu);
c906108c
SS
4564 break;
4565 }
4566}
4567
94af9270
KS
4568/* A helper function for dwarf2_compute_name which determines whether DIE
4569 needs to have the name of the scope prepended to the name listed in the
4570 die. */
4571
4572static int
4573die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4574{
1c809c68
TT
4575 struct attribute *attr;
4576
94af9270
KS
4577 switch (die->tag)
4578 {
4579 case DW_TAG_namespace:
4580 case DW_TAG_typedef:
4581 case DW_TAG_class_type:
4582 case DW_TAG_interface_type:
4583 case DW_TAG_structure_type:
4584 case DW_TAG_union_type:
4585 case DW_TAG_enumeration_type:
4586 case DW_TAG_enumerator:
4587 case DW_TAG_subprogram:
4588 case DW_TAG_member:
4589 return 1;
4590
4591 case DW_TAG_variable:
4592 /* We only need to prefix "globally" visible variables. These include
4593 any variable marked with DW_AT_external or any variable that
4594 lives in a namespace. [Variables in anonymous namespaces
4595 require prefixing, but they are not DW_AT_external.] */
4596
4597 if (dwarf2_attr (die, DW_AT_specification, cu))
4598 {
4599 struct dwarf2_cu *spec_cu = cu;
9a619af0 4600
94af9270
KS
4601 return die_needs_namespace (die_specification (die, &spec_cu),
4602 spec_cu);
4603 }
4604
1c809c68 4605 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4606 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4607 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4608 return 0;
4609 /* A variable in a lexical block of some kind does not need a
4610 namespace, even though in C++ such variables may be external
4611 and have a mangled name. */
4612 if (die->parent->tag == DW_TAG_lexical_block
4613 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4614 || die->parent->tag == DW_TAG_catch_block
4615 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4616 return 0;
4617 return 1;
94af9270
KS
4618
4619 default:
4620 return 0;
4621 }
4622}
4623
98bfdba5
PA
4624/* Retrieve the last character from a mem_file. */
4625
4626static void
4627do_ui_file_peek_last (void *object, const char *buffer, long length)
4628{
4629 char *last_char_p = (char *) object;
4630
4631 if (length > 0)
4632 *last_char_p = buffer[length - 1];
4633}
4634
94af9270
KS
4635/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4636 compute the physname for the object, which include a method's
4637 formal parameters (C++/Java) and return type (Java).
4638
af6b7be1
JB
4639 For Ada, return the DIE's linkage name rather than the fully qualified
4640 name. PHYSNAME is ignored..
4641
94af9270
KS
4642 The result is allocated on the objfile_obstack and canonicalized. */
4643
4644static const char *
4645dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4646 int physname)
4647{
4648 if (name == NULL)
4649 name = dwarf2_name (die, cu);
4650
f55ee35c
JK
4651 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4652 compute it by typename_concat inside GDB. */
4653 if (cu->language == language_ada
4654 || (cu->language == language_fortran && physname))
4655 {
4656 /* For Ada unit, we prefer the linkage name over the name, as
4657 the former contains the exported name, which the user expects
4658 to be able to reference. Ideally, we want the user to be able
4659 to reference this entity using either natural or linkage name,
4660 but we haven't started looking at this enhancement yet. */
4661 struct attribute *attr;
4662
4663 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4664 if (attr == NULL)
4665 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4666 if (attr && DW_STRING (attr))
4667 return DW_STRING (attr);
4668 }
4669
94af9270
KS
4670 /* These are the only languages we know how to qualify names in. */
4671 if (name != NULL
f55ee35c
JK
4672 && (cu->language == language_cplus || cu->language == language_java
4673 || cu->language == language_fortran))
94af9270
KS
4674 {
4675 if (die_needs_namespace (die, cu))
4676 {
4677 long length;
4678 char *prefix;
4679 struct ui_file *buf;
4680
4681 prefix = determine_prefix (die, cu);
4682 buf = mem_fileopen ();
4683 if (*prefix != '\0')
4684 {
f55ee35c
JK
4685 char *prefixed_name = typename_concat (NULL, prefix, name,
4686 physname, cu);
9a619af0 4687
94af9270
KS
4688 fputs_unfiltered (prefixed_name, buf);
4689 xfree (prefixed_name);
4690 }
4691 else
4692 fputs_unfiltered (name ? name : "", buf);
4693
98bfdba5
PA
4694 /* Template parameters may be specified in the DIE's DW_AT_name, or
4695 as children with DW_TAG_template_type_param or
4696 DW_TAG_value_type_param. If the latter, add them to the name
4697 here. If the name already has template parameters, then
4698 skip this step; some versions of GCC emit both, and
4699 it is more efficient to use the pre-computed name.
4700
4701 Something to keep in mind about this process: it is very
4702 unlikely, or in some cases downright impossible, to produce
4703 something that will match the mangled name of a function.
4704 If the definition of the function has the same debug info,
4705 we should be able to match up with it anyway. But fallbacks
4706 using the minimal symbol, for instance to find a method
4707 implemented in a stripped copy of libstdc++, will not work.
4708 If we do not have debug info for the definition, we will have to
4709 match them up some other way.
4710
4711 When we do name matching there is a related problem with function
4712 templates; two instantiated function templates are allowed to
4713 differ only by their return types, which we do not add here. */
4714
4715 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4716 {
4717 struct attribute *attr;
4718 struct die_info *child;
4719 int first = 1;
4720
4721 die->building_fullname = 1;
4722
4723 for (child = die->child; child != NULL; child = child->sibling)
4724 {
4725 struct type *type;
4726 long value;
4727 gdb_byte *bytes;
4728 struct dwarf2_locexpr_baton *baton;
4729 struct value *v;
4730
4731 if (child->tag != DW_TAG_template_type_param
4732 && child->tag != DW_TAG_template_value_param)
4733 continue;
4734
4735 if (first)
4736 {
4737 fputs_unfiltered ("<", buf);
4738 first = 0;
4739 }
4740 else
4741 fputs_unfiltered (", ", buf);
4742
4743 attr = dwarf2_attr (child, DW_AT_type, cu);
4744 if (attr == NULL)
4745 {
4746 complaint (&symfile_complaints,
4747 _("template parameter missing DW_AT_type"));
4748 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4749 continue;
4750 }
4751 type = die_type (child, cu);
4752
4753 if (child->tag == DW_TAG_template_type_param)
4754 {
4755 c_print_type (type, "", buf, -1, 0);
4756 continue;
4757 }
4758
4759 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4760 if (attr == NULL)
4761 {
4762 complaint (&symfile_complaints,
4763 _("template parameter missing DW_AT_const_value"));
4764 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4765 continue;
4766 }
4767
4768 dwarf2_const_value_attr (attr, type, name,
4769 &cu->comp_unit_obstack, cu,
4770 &value, &bytes, &baton);
4771
4772 if (TYPE_NOSIGN (type))
4773 /* GDB prints characters as NUMBER 'CHAR'. If that's
4774 changed, this can use value_print instead. */
4775 c_printchar (value, type, buf);
4776 else
4777 {
4778 struct value_print_options opts;
4779
4780 if (baton != NULL)
4781 v = dwarf2_evaluate_loc_desc (type, NULL,
4782 baton->data,
4783 baton->size,
4784 baton->per_cu);
4785 else if (bytes != NULL)
4786 {
4787 v = allocate_value (type);
4788 memcpy (value_contents_writeable (v), bytes,
4789 TYPE_LENGTH (type));
4790 }
4791 else
4792 v = value_from_longest (type, value);
4793
4794 /* Specify decimal so that we do not depend on the radix. */
4795 get_formatted_print_options (&opts, 'd');
4796 opts.raw = 1;
4797 value_print (v, buf, &opts);
4798 release_value (v);
4799 value_free (v);
4800 }
4801 }
4802
4803 die->building_fullname = 0;
4804
4805 if (!first)
4806 {
4807 /* Close the argument list, with a space if necessary
4808 (nested templates). */
4809 char last_char = '\0';
4810 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4811 if (last_char == '>')
4812 fputs_unfiltered (" >", buf);
4813 else
4814 fputs_unfiltered (">", buf);
4815 }
4816 }
4817
94af9270
KS
4818 /* For Java and C++ methods, append formal parameter type
4819 information, if PHYSNAME. */
6e70227d 4820
94af9270
KS
4821 if (physname && die->tag == DW_TAG_subprogram
4822 && (cu->language == language_cplus
4823 || cu->language == language_java))
4824 {
4825 struct type *type = read_type_die (die, cu);
4826
4827 c_type_print_args (type, buf, 0, cu->language);
4828
4829 if (cu->language == language_java)
4830 {
4831 /* For java, we must append the return type to method
4832 names. */
4833 if (die->tag == DW_TAG_subprogram)
4834 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4835 0, 0);
4836 }
4837 else if (cu->language == language_cplus)
4838 {
4839 if (TYPE_NFIELDS (type) > 0
4840 && TYPE_FIELD_ARTIFICIAL (type, 0)
4841 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4842 fputs_unfiltered (" const", buf);
4843 }
4844 }
4845
4846 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4847 &length);
4848 ui_file_delete (buf);
4849
4850 if (cu->language == language_cplus)
4851 {
4852 char *cname
4853 = dwarf2_canonicalize_name (name, cu,
4854 &cu->objfile->objfile_obstack);
9a619af0 4855
94af9270
KS
4856 if (cname != NULL)
4857 name = cname;
4858 }
4859 }
4860 }
4861
4862 return name;
4863}
4864
0114d602
DJ
4865/* Return the fully qualified name of DIE, based on its DW_AT_name.
4866 If scope qualifiers are appropriate they will be added. The result
4867 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4868 not have a name. NAME may either be from a previous call to
4869 dwarf2_name or NULL.
4870
4871 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4872
4873static const char *
94af9270 4874dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4875{
94af9270
KS
4876 return dwarf2_compute_name (name, die, cu, 0);
4877}
0114d602 4878
94af9270
KS
4879/* Construct a physname for the given DIE in CU. NAME may either be
4880 from a previous call to dwarf2_name or NULL. The result will be
4881 allocated on the objfile_objstack or NULL if the DIE does not have a
4882 name.
0114d602 4883
94af9270 4884 The output string will be canonicalized (if C++/Java). */
0114d602 4885
94af9270
KS
4886static const char *
4887dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4888{
4889 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4890}
4891
27aa8d6a
SW
4892/* Read the import statement specified by the given die and record it. */
4893
4894static void
4895read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4896{
4897 struct attribute *import_attr;
4898 struct die_info *imported_die;
de4affc9 4899 struct dwarf2_cu *imported_cu;
27aa8d6a 4900 const char *imported_name;
794684b6 4901 const char *imported_name_prefix;
13387711
SW
4902 const char *canonical_name;
4903 const char *import_alias;
4904 const char *imported_declaration = NULL;
794684b6 4905 const char *import_prefix;
13387711
SW
4906
4907 char *temp;
27aa8d6a
SW
4908
4909 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4910 if (import_attr == NULL)
4911 {
4912 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4913 dwarf_tag_name (die->tag));
4914 return;
4915 }
4916
de4affc9
CC
4917 imported_cu = cu;
4918 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4919 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4920 if (imported_name == NULL)
4921 {
4922 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4923
4924 The import in the following code:
4925 namespace A
4926 {
4927 typedef int B;
4928 }
4929
4930 int main ()
4931 {
4932 using A::B;
4933 B b;
4934 return b;
4935 }
4936
4937 ...
4938 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4939 <52> DW_AT_decl_file : 1
4940 <53> DW_AT_decl_line : 6
4941 <54> DW_AT_import : <0x75>
4942 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4943 <59> DW_AT_name : B
4944 <5b> DW_AT_decl_file : 1
4945 <5c> DW_AT_decl_line : 2
4946 <5d> DW_AT_type : <0x6e>
4947 ...
4948 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4949 <76> DW_AT_byte_size : 4
4950 <77> DW_AT_encoding : 5 (signed)
4951
4952 imports the wrong die ( 0x75 instead of 0x58 ).
4953 This case will be ignored until the gcc bug is fixed. */
4954 return;
4955 }
4956
82856980
SW
4957 /* Figure out the local name after import. */
4958 import_alias = dwarf2_name (die, cu);
27aa8d6a 4959
794684b6
SW
4960 /* Figure out where the statement is being imported to. */
4961 import_prefix = determine_prefix (die, cu);
4962
4963 /* Figure out what the scope of the imported die is and prepend it
4964 to the name of the imported die. */
de4affc9 4965 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4966
f55ee35c
JK
4967 if (imported_die->tag != DW_TAG_namespace
4968 && imported_die->tag != DW_TAG_module)
794684b6 4969 {
13387711
SW
4970 imported_declaration = imported_name;
4971 canonical_name = imported_name_prefix;
794684b6 4972 }
13387711 4973 else if (strlen (imported_name_prefix) > 0)
794684b6 4974 {
13387711
SW
4975 temp = alloca (strlen (imported_name_prefix)
4976 + 2 + strlen (imported_name) + 1);
4977 strcpy (temp, imported_name_prefix);
4978 strcat (temp, "::");
4979 strcat (temp, imported_name);
4980 canonical_name = temp;
794684b6 4981 }
13387711
SW
4982 else
4983 canonical_name = imported_name;
794684b6 4984
c0cc3a76
SW
4985 cp_add_using_directive (import_prefix,
4986 canonical_name,
4987 import_alias,
13387711 4988 imported_declaration,
c0cc3a76 4989 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4990}
4991
5fb290d7 4992static void
e142c38c 4993initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4994{
e142c38c 4995 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4996}
4997
cb1df416
DJ
4998static void
4999free_cu_line_header (void *arg)
5000{
5001 struct dwarf2_cu *cu = arg;
5002
5003 free_line_header (cu->line_header);
5004 cu->line_header = NULL;
5005}
5006
9291a0cd
TT
5007static void
5008find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5009 char **name, char **comp_dir)
5010{
5011 struct attribute *attr;
5012
5013 *name = NULL;
5014 *comp_dir = NULL;
5015
5016 /* Find the filename. Do not use dwarf2_name here, since the filename
5017 is not a source language identifier. */
5018 attr = dwarf2_attr (die, DW_AT_name, cu);
5019 if (attr)
5020 {
5021 *name = DW_STRING (attr);
5022 }
5023
5024 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5025 if (attr)
5026 *comp_dir = DW_STRING (attr);
5027 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5028 {
5029 *comp_dir = ldirname (*name);
5030 if (*comp_dir != NULL)
5031 make_cleanup (xfree, *comp_dir);
5032 }
5033 if (*comp_dir != NULL)
5034 {
5035 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5036 directory, get rid of it. */
5037 char *cp = strchr (*comp_dir, ':');
5038
5039 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5040 *comp_dir = cp + 1;
5041 }
5042
5043 if (*name == NULL)
5044 *name = "<unknown>";
5045}
5046
c906108c 5047static void
e7c27a73 5048read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5049{
e7c27a73 5050 struct objfile *objfile = cu->objfile;
debd256d 5051 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5052 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5053 CORE_ADDR highpc = ((CORE_ADDR) 0);
5054 struct attribute *attr;
e1024ff1 5055 char *name = NULL;
c906108c
SS
5056 char *comp_dir = NULL;
5057 struct die_info *child_die;
5058 bfd *abfd = objfile->obfd;
debd256d 5059 struct line_header *line_header = 0;
e142c38c 5060 CORE_ADDR baseaddr;
6e70227d 5061
e142c38c 5062 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5063
fae299cd 5064 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5065
5066 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5067 from finish_block. */
2acceee2 5068 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5069 lowpc = highpc;
5070 lowpc += baseaddr;
5071 highpc += baseaddr;
5072
9291a0cd 5073 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5074
e142c38c 5075 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5076 if (attr)
5077 {
e142c38c 5078 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5079 }
5080
b0f35d58 5081 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5082 if (attr)
b0f35d58 5083 cu->producer = DW_STRING (attr);
303b6f5d 5084
c906108c
SS
5085 /* We assume that we're processing GCC output. */
5086 processing_gcc_compilation = 2;
c906108c 5087
df8a16a1
DJ
5088 processing_has_namespace_info = 0;
5089
c906108c
SS
5090 start_symtab (name, comp_dir, lowpc);
5091 record_debugformat ("DWARF 2");
303b6f5d 5092 record_producer (cu->producer);
c906108c 5093
e142c38c 5094 initialize_cu_func_list (cu);
c906108c 5095
cb1df416
DJ
5096 /* Decode line number information if present. We do this before
5097 processing child DIEs, so that the line header table is available
5098 for DW_AT_decl_file. */
e142c38c 5099 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5100 if (attr)
5101 {
debd256d 5102 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5103 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5104 if (line_header)
5105 {
cb1df416
DJ
5106 cu->line_header = line_header;
5107 make_cleanup (free_cu_line_header, cu);
aaa75496 5108 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5109 }
5fb290d7 5110 }
debd256d 5111
cb1df416
DJ
5112 /* Process all dies in compilation unit. */
5113 if (die->child != NULL)
5114 {
5115 child_die = die->child;
5116 while (child_die && child_die->tag)
5117 {
5118 process_die (child_die, cu);
5119 child_die = sibling_die (child_die);
5120 }
5121 }
5122
2e276125
JB
5123 /* Decode macro information, if present. Dwarf 2 macro information
5124 refers to information in the line number info statement program
5125 header, so we can only read it if we've read the header
5126 successfully. */
e142c38c 5127 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5128 if (attr && line_header)
2e276125
JB
5129 {
5130 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5131
2e276125 5132 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5133 comp_dir, abfd, cu);
2e276125 5134 }
debd256d 5135 do_cleanups (back_to);
5fb290d7
DJ
5136}
5137
348e048f
DE
5138/* For TUs we want to skip the first top level sibling if it's not the
5139 actual type being defined by this TU. In this case the first top
5140 level sibling is there to provide context only. */
5141
5142static void
5143read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5144{
5145 struct objfile *objfile = cu->objfile;
5146 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5147 CORE_ADDR lowpc;
5148 struct attribute *attr;
5149 char *name = NULL;
5150 char *comp_dir = NULL;
5151 struct die_info *child_die;
5152 bfd *abfd = objfile->obfd;
348e048f
DE
5153
5154 /* start_symtab needs a low pc, but we don't really have one.
5155 Do what read_file_scope would do in the absence of such info. */
5156 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5157
5158 /* Find the filename. Do not use dwarf2_name here, since the filename
5159 is not a source language identifier. */
5160 attr = dwarf2_attr (die, DW_AT_name, cu);
5161 if (attr)
5162 name = DW_STRING (attr);
5163
5164 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5165 if (attr)
5166 comp_dir = DW_STRING (attr);
5167 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5168 {
5169 comp_dir = ldirname (name);
5170 if (comp_dir != NULL)
5171 make_cleanup (xfree, comp_dir);
5172 }
5173
5174 if (name == NULL)
5175 name = "<unknown>";
5176
5177 attr = dwarf2_attr (die, DW_AT_language, cu);
5178 if (attr)
5179 set_cu_language (DW_UNSND (attr), cu);
5180
5181 /* This isn't technically needed today. It is done for symmetry
5182 with read_file_scope. */
5183 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5184 if (attr)
348e048f
DE
5185 cu->producer = DW_STRING (attr);
5186
5187 /* We assume that we're processing GCC output. */
5188 processing_gcc_compilation = 2;
5189
5190 processing_has_namespace_info = 0;
5191
5192 start_symtab (name, comp_dir, lowpc);
5193 record_debugformat ("DWARF 2");
5194 record_producer (cu->producer);
5195
5196 /* Process the dies in the type unit. */
5197 if (die->child == NULL)
5198 {
5199 dump_die_for_error (die);
5200 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5201 bfd_get_filename (abfd));
5202 }
5203
5204 child_die = die->child;
5205
5206 while (child_die && child_die->tag)
5207 {
5208 process_die (child_die, cu);
5209
5210 child_die = sibling_die (child_die);
5211 }
5212
5213 do_cleanups (back_to);
5214}
5215
5fb290d7 5216static void
e142c38c
DJ
5217add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5218 struct dwarf2_cu *cu)
5fb290d7
DJ
5219{
5220 struct function_range *thisfn;
5221
5222 thisfn = (struct function_range *)
7b5a2f43 5223 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5224 thisfn->name = name;
5225 thisfn->lowpc = lowpc;
5226 thisfn->highpc = highpc;
5227 thisfn->seen_line = 0;
5228 thisfn->next = NULL;
5229
e142c38c
DJ
5230 if (cu->last_fn == NULL)
5231 cu->first_fn = thisfn;
5fb290d7 5232 else
e142c38c 5233 cu->last_fn->next = thisfn;
5fb290d7 5234
e142c38c 5235 cu->last_fn = thisfn;
c906108c
SS
5236}
5237
d389af10
JK
5238/* qsort helper for inherit_abstract_dies. */
5239
5240static int
5241unsigned_int_compar (const void *ap, const void *bp)
5242{
5243 unsigned int a = *(unsigned int *) ap;
5244 unsigned int b = *(unsigned int *) bp;
5245
5246 return (a > b) - (b > a);
5247}
5248
5249/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5250 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5251 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5252
5253static void
5254inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5255{
5256 struct die_info *child_die;
5257 unsigned die_children_count;
5258 /* CU offsets which were referenced by children of the current DIE. */
5259 unsigned *offsets;
5260 unsigned *offsets_end, *offsetp;
5261 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5262 struct die_info *origin_die;
5263 /* Iterator of the ORIGIN_DIE children. */
5264 struct die_info *origin_child_die;
5265 struct cleanup *cleanups;
5266 struct attribute *attr;
cd02d79d
PA
5267 struct dwarf2_cu *origin_cu;
5268 struct pending **origin_previous_list_in_scope;
d389af10
JK
5269
5270 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5271 if (!attr)
5272 return;
5273
cd02d79d
PA
5274 /* Note that following die references may follow to a die in a
5275 different cu. */
5276
5277 origin_cu = cu;
5278 origin_die = follow_die_ref (die, attr, &origin_cu);
5279
5280 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5281 symbols in. */
5282 origin_previous_list_in_scope = origin_cu->list_in_scope;
5283 origin_cu->list_in_scope = cu->list_in_scope;
5284
edb3359d
DJ
5285 if (die->tag != origin_die->tag
5286 && !(die->tag == DW_TAG_inlined_subroutine
5287 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5288 complaint (&symfile_complaints,
5289 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5290 die->offset, origin_die->offset);
5291
5292 child_die = die->child;
5293 die_children_count = 0;
5294 while (child_die && child_die->tag)
5295 {
5296 child_die = sibling_die (child_die);
5297 die_children_count++;
5298 }
5299 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5300 cleanups = make_cleanup (xfree, offsets);
5301
5302 offsets_end = offsets;
5303 child_die = die->child;
5304 while (child_die && child_die->tag)
5305 {
c38f313d
DJ
5306 /* For each CHILD_DIE, find the corresponding child of
5307 ORIGIN_DIE. If there is more than one layer of
5308 DW_AT_abstract_origin, follow them all; there shouldn't be,
5309 but GCC versions at least through 4.4 generate this (GCC PR
5310 40573). */
5311 struct die_info *child_origin_die = child_die;
cd02d79d 5312 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5313
c38f313d
DJ
5314 while (1)
5315 {
cd02d79d
PA
5316 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5317 child_origin_cu);
c38f313d
DJ
5318 if (attr == NULL)
5319 break;
cd02d79d
PA
5320 child_origin_die = follow_die_ref (child_origin_die, attr,
5321 &child_origin_cu);
c38f313d
DJ
5322 }
5323
d389af10
JK
5324 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5325 counterpart may exist. */
c38f313d 5326 if (child_origin_die != child_die)
d389af10 5327 {
edb3359d
DJ
5328 if (child_die->tag != child_origin_die->tag
5329 && !(child_die->tag == DW_TAG_inlined_subroutine
5330 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5331 complaint (&symfile_complaints,
5332 _("Child DIE 0x%x and its abstract origin 0x%x have "
5333 "different tags"), child_die->offset,
5334 child_origin_die->offset);
c38f313d
DJ
5335 if (child_origin_die->parent != origin_die)
5336 complaint (&symfile_complaints,
5337 _("Child DIE 0x%x and its abstract origin 0x%x have "
5338 "different parents"), child_die->offset,
5339 child_origin_die->offset);
5340 else
5341 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5342 }
5343 child_die = sibling_die (child_die);
5344 }
5345 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5346 unsigned_int_compar);
5347 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5348 if (offsetp[-1] == *offsetp)
5349 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5350 "to DIE 0x%x as their abstract origin"),
5351 die->offset, *offsetp);
5352
5353 offsetp = offsets;
5354 origin_child_die = origin_die->child;
5355 while (origin_child_die && origin_child_die->tag)
5356 {
5357 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5358 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5359 offsetp++;
5360 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5361 {
5362 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5363 process_die (origin_child_die, origin_cu);
d389af10
JK
5364 }
5365 origin_child_die = sibling_die (origin_child_die);
5366 }
cd02d79d 5367 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5368
5369 do_cleanups (cleanups);
5370}
5371
c906108c 5372static void
e7c27a73 5373read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5374{
e7c27a73 5375 struct objfile *objfile = cu->objfile;
52f0bd74 5376 struct context_stack *new;
c906108c
SS
5377 CORE_ADDR lowpc;
5378 CORE_ADDR highpc;
5379 struct die_info *child_die;
edb3359d 5380 struct attribute *attr, *call_line, *call_file;
c906108c 5381 char *name;
e142c38c 5382 CORE_ADDR baseaddr;
801e3a5b 5383 struct block *block;
edb3359d 5384 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5385 VEC (symbolp) *template_args = NULL;
5386 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5387
5388 if (inlined_func)
5389 {
5390 /* If we do not have call site information, we can't show the
5391 caller of this inlined function. That's too confusing, so
5392 only use the scope for local variables. */
5393 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5394 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5395 if (call_line == NULL || call_file == NULL)
5396 {
5397 read_lexical_block_scope (die, cu);
5398 return;
5399 }
5400 }
c906108c 5401
e142c38c
DJ
5402 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5403
94af9270 5404 name = dwarf2_name (die, cu);
c906108c 5405
e8d05480
JB
5406 /* Ignore functions with missing or empty names. These are actually
5407 illegal according to the DWARF standard. */
5408 if (name == NULL)
5409 {
5410 complaint (&symfile_complaints,
5411 _("missing name for subprogram DIE at %d"), die->offset);
5412 return;
5413 }
5414
5415 /* Ignore functions with missing or invalid low and high pc attributes. */
5416 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5417 {
ae4d0c03
PM
5418 attr = dwarf2_attr (die, DW_AT_external, cu);
5419 if (!attr || !DW_UNSND (attr))
5420 complaint (&symfile_complaints,
5421 _("cannot get low and high bounds for subprogram DIE at %d"),
5422 die->offset);
e8d05480
JB
5423 return;
5424 }
c906108c
SS
5425
5426 lowpc += baseaddr;
5427 highpc += baseaddr;
5428
5fb290d7 5429 /* Record the function range for dwarf_decode_lines. */
e142c38c 5430 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5431
34eaf542
TT
5432 /* If we have any template arguments, then we must allocate a
5433 different sort of symbol. */
5434 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5435 {
5436 if (child_die->tag == DW_TAG_template_type_param
5437 || child_die->tag == DW_TAG_template_value_param)
5438 {
5439 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5440 struct template_symbol);
5441 templ_func->base.is_cplus_template_function = 1;
5442 break;
5443 }
5444 }
5445
c906108c 5446 new = push_context (0, lowpc);
34eaf542
TT
5447 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5448 (struct symbol *) templ_func);
4c2df51b 5449
4cecd739
DJ
5450 /* If there is a location expression for DW_AT_frame_base, record
5451 it. */
e142c38c 5452 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5453 if (attr)
c034e007
AC
5454 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5455 expression is being recorded directly in the function's symbol
5456 and not in a separate frame-base object. I guess this hack is
5457 to avoid adding some sort of frame-base adjunct/annex to the
5458 function's symbol :-(. The problem with doing this is that it
5459 results in a function symbol with a location expression that
5460 has nothing to do with the location of the function, ouch! The
5461 relationship should be: a function's symbol has-a frame base; a
5462 frame-base has-a location expression. */
e7c27a73 5463 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5464
e142c38c 5465 cu->list_in_scope = &local_symbols;
c906108c 5466
639d11d3 5467 if (die->child != NULL)
c906108c 5468 {
639d11d3 5469 child_die = die->child;
c906108c
SS
5470 while (child_die && child_die->tag)
5471 {
34eaf542
TT
5472 if (child_die->tag == DW_TAG_template_type_param
5473 || child_die->tag == DW_TAG_template_value_param)
5474 {
5475 struct symbol *arg = new_symbol (child_die, NULL, cu);
5476
5477 VEC_safe_push (symbolp, template_args, arg);
5478 }
5479 else
5480 process_die (child_die, cu);
c906108c
SS
5481 child_die = sibling_die (child_die);
5482 }
5483 }
5484
d389af10
JK
5485 inherit_abstract_dies (die, cu);
5486
4a811a97
UW
5487 /* If we have a DW_AT_specification, we might need to import using
5488 directives from the context of the specification DIE. See the
5489 comment in determine_prefix. */
5490 if (cu->language == language_cplus
5491 && dwarf2_attr (die, DW_AT_specification, cu))
5492 {
5493 struct dwarf2_cu *spec_cu = cu;
5494 struct die_info *spec_die = die_specification (die, &spec_cu);
5495
5496 while (spec_die)
5497 {
5498 child_die = spec_die->child;
5499 while (child_die && child_die->tag)
5500 {
5501 if (child_die->tag == DW_TAG_imported_module)
5502 process_die (child_die, spec_cu);
5503 child_die = sibling_die (child_die);
5504 }
5505
5506 /* In some cases, GCC generates specification DIEs that
5507 themselves contain DW_AT_specification attributes. */
5508 spec_die = die_specification (spec_die, &spec_cu);
5509 }
5510 }
5511
c906108c
SS
5512 new = pop_context ();
5513 /* Make a block for the local symbols within. */
801e3a5b
JB
5514 block = finish_block (new->name, &local_symbols, new->old_blocks,
5515 lowpc, highpc, objfile);
5516
df8a16a1 5517 /* For C++, set the block's scope. */
f55ee35c 5518 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5519 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5520 determine_prefix (die, cu),
df8a16a1
DJ
5521 processing_has_namespace_info);
5522
801e3a5b
JB
5523 /* If we have address ranges, record them. */
5524 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5525
34eaf542
TT
5526 /* Attach template arguments to function. */
5527 if (! VEC_empty (symbolp, template_args))
5528 {
5529 gdb_assert (templ_func != NULL);
5530
5531 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5532 templ_func->template_arguments
5533 = obstack_alloc (&objfile->objfile_obstack,
5534 (templ_func->n_template_arguments
5535 * sizeof (struct symbol *)));
5536 memcpy (templ_func->template_arguments,
5537 VEC_address (symbolp, template_args),
5538 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5539 VEC_free (symbolp, template_args);
5540 }
5541
208d8187
JB
5542 /* In C++, we can have functions nested inside functions (e.g., when
5543 a function declares a class that has methods). This means that
5544 when we finish processing a function scope, we may need to go
5545 back to building a containing block's symbol lists. */
5546 local_symbols = new->locals;
5547 param_symbols = new->params;
27aa8d6a 5548 using_directives = new->using_directives;
208d8187 5549
921e78cf
JB
5550 /* If we've finished processing a top-level function, subsequent
5551 symbols go in the file symbol list. */
5552 if (outermost_context_p ())
e142c38c 5553 cu->list_in_scope = &file_symbols;
c906108c
SS
5554}
5555
5556/* Process all the DIES contained within a lexical block scope. Start
5557 a new scope, process the dies, and then close the scope. */
5558
5559static void
e7c27a73 5560read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5561{
e7c27a73 5562 struct objfile *objfile = cu->objfile;
52f0bd74 5563 struct context_stack *new;
c906108c
SS
5564 CORE_ADDR lowpc, highpc;
5565 struct die_info *child_die;
e142c38c
DJ
5566 CORE_ADDR baseaddr;
5567
5568 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5569
5570 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5571 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5572 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5573 be nasty. Might be easier to properly extend generic blocks to
af34e669 5574 describe ranges. */
d85a05f0 5575 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5576 return;
5577 lowpc += baseaddr;
5578 highpc += baseaddr;
5579
5580 push_context (0, lowpc);
639d11d3 5581 if (die->child != NULL)
c906108c 5582 {
639d11d3 5583 child_die = die->child;
c906108c
SS
5584 while (child_die && child_die->tag)
5585 {
e7c27a73 5586 process_die (child_die, cu);
c906108c
SS
5587 child_die = sibling_die (child_die);
5588 }
5589 }
5590 new = pop_context ();
5591
8540c487 5592 if (local_symbols != NULL || using_directives != NULL)
c906108c 5593 {
801e3a5b
JB
5594 struct block *block
5595 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5596 highpc, objfile);
5597
5598 /* Note that recording ranges after traversing children, as we
5599 do here, means that recording a parent's ranges entails
5600 walking across all its children's ranges as they appear in
5601 the address map, which is quadratic behavior.
5602
5603 It would be nicer to record the parent's ranges before
5604 traversing its children, simply overriding whatever you find
5605 there. But since we don't even decide whether to create a
5606 block until after we've traversed its children, that's hard
5607 to do. */
5608 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5609 }
5610 local_symbols = new->locals;
27aa8d6a 5611 using_directives = new->using_directives;
c906108c
SS
5612}
5613
43039443 5614/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5615 Return 1 if the attributes are present and valid, otherwise, return 0.
5616 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5617
5618static int
5619dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5620 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5621 struct partial_symtab *ranges_pst)
43039443
JK
5622{
5623 struct objfile *objfile = cu->objfile;
5624 struct comp_unit_head *cu_header = &cu->header;
5625 bfd *obfd = objfile->obfd;
5626 unsigned int addr_size = cu_header->addr_size;
5627 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5628 /* Base address selection entry. */
5629 CORE_ADDR base;
5630 int found_base;
5631 unsigned int dummy;
5632 gdb_byte *buffer;
5633 CORE_ADDR marker;
5634 int low_set;
5635 CORE_ADDR low = 0;
5636 CORE_ADDR high = 0;
ff013f42 5637 CORE_ADDR baseaddr;
43039443 5638
d00adf39
DE
5639 found_base = cu->base_known;
5640 base = cu->base_address;
43039443 5641
be391dca 5642 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5643 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5644 {
5645 complaint (&symfile_complaints,
5646 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5647 offset);
5648 return 0;
5649 }
dce234bc 5650 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5651
5652 /* Read in the largest possible address. */
5653 marker = read_address (obfd, buffer, cu, &dummy);
5654 if ((marker & mask) == mask)
5655 {
5656 /* If we found the largest possible address, then
5657 read the base address. */
5658 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5659 buffer += 2 * addr_size;
5660 offset += 2 * addr_size;
5661 found_base = 1;
5662 }
5663
5664 low_set = 0;
5665
e7030f15 5666 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5667
43039443
JK
5668 while (1)
5669 {
5670 CORE_ADDR range_beginning, range_end;
5671
5672 range_beginning = read_address (obfd, buffer, cu, &dummy);
5673 buffer += addr_size;
5674 range_end = read_address (obfd, buffer, cu, &dummy);
5675 buffer += addr_size;
5676 offset += 2 * addr_size;
5677
5678 /* An end of list marker is a pair of zero addresses. */
5679 if (range_beginning == 0 && range_end == 0)
5680 /* Found the end of list entry. */
5681 break;
5682
5683 /* Each base address selection entry is a pair of 2 values.
5684 The first is the largest possible address, the second is
5685 the base address. Check for a base address here. */
5686 if ((range_beginning & mask) == mask)
5687 {
5688 /* If we found the largest possible address, then
5689 read the base address. */
5690 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5691 found_base = 1;
5692 continue;
5693 }
5694
5695 if (!found_base)
5696 {
5697 /* We have no valid base address for the ranges
5698 data. */
5699 complaint (&symfile_complaints,
5700 _("Invalid .debug_ranges data (no base address)"));
5701 return 0;
5702 }
5703
5704 range_beginning += base;
5705 range_end += base;
5706
ff013f42
JK
5707 if (ranges_pst != NULL && range_beginning < range_end)
5708 addrmap_set_empty (objfile->psymtabs_addrmap,
5709 range_beginning + baseaddr, range_end - 1 + baseaddr,
5710 ranges_pst);
5711
43039443
JK
5712 /* FIXME: This is recording everything as a low-high
5713 segment of consecutive addresses. We should have a
5714 data structure for discontiguous block ranges
5715 instead. */
5716 if (! low_set)
5717 {
5718 low = range_beginning;
5719 high = range_end;
5720 low_set = 1;
5721 }
5722 else
5723 {
5724 if (range_beginning < low)
5725 low = range_beginning;
5726 if (range_end > high)
5727 high = range_end;
5728 }
5729 }
5730
5731 if (! low_set)
5732 /* If the first entry is an end-of-list marker, the range
5733 describes an empty scope, i.e. no instructions. */
5734 return 0;
5735
5736 if (low_return)
5737 *low_return = low;
5738 if (high_return)
5739 *high_return = high;
5740 return 1;
5741}
5742
af34e669
DJ
5743/* Get low and high pc attributes from a die. Return 1 if the attributes
5744 are present and valid, otherwise, return 0. Return -1 if the range is
5745 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5746static int
af34e669 5747dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5748 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5749 struct partial_symtab *pst)
c906108c
SS
5750{
5751 struct attribute *attr;
af34e669
DJ
5752 CORE_ADDR low = 0;
5753 CORE_ADDR high = 0;
5754 int ret = 0;
c906108c 5755
e142c38c 5756 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5757 if (attr)
af34e669
DJ
5758 {
5759 high = DW_ADDR (attr);
e142c38c 5760 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5761 if (attr)
5762 low = DW_ADDR (attr);
5763 else
5764 /* Found high w/o low attribute. */
5765 return 0;
5766
5767 /* Found consecutive range of addresses. */
5768 ret = 1;
5769 }
c906108c 5770 else
af34e669 5771 {
e142c38c 5772 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5773 if (attr != NULL)
5774 {
af34e669 5775 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5776 .debug_ranges section. */
d85a05f0 5777 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5778 return 0;
43039443 5779 /* Found discontinuous range of addresses. */
af34e669
DJ
5780 ret = -1;
5781 }
5782 }
c906108c
SS
5783
5784 if (high < low)
5785 return 0;
5786
5787 /* When using the GNU linker, .gnu.linkonce. sections are used to
5788 eliminate duplicate copies of functions and vtables and such.
5789 The linker will arbitrarily choose one and discard the others.
5790 The AT_*_pc values for such functions refer to local labels in
5791 these sections. If the section from that file was discarded, the
5792 labels are not in the output, so the relocs get a value of 0.
5793 If this is a discarded function, mark the pc bounds as invalid,
5794 so that GDB will ignore it. */
72dca2f5 5795 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5796 return 0;
5797
5798 *lowpc = low;
5799 *highpc = high;
af34e669 5800 return ret;
c906108c
SS
5801}
5802
b084d499
JB
5803/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5804 its low and high PC addresses. Do nothing if these addresses could not
5805 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5806 and HIGHPC to the high address if greater than HIGHPC. */
5807
5808static void
5809dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5810 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5811 struct dwarf2_cu *cu)
5812{
5813 CORE_ADDR low, high;
5814 struct die_info *child = die->child;
5815
d85a05f0 5816 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5817 {
5818 *lowpc = min (*lowpc, low);
5819 *highpc = max (*highpc, high);
5820 }
5821
5822 /* If the language does not allow nested subprograms (either inside
5823 subprograms or lexical blocks), we're done. */
5824 if (cu->language != language_ada)
5825 return;
6e70227d 5826
b084d499
JB
5827 /* Check all the children of the given DIE. If it contains nested
5828 subprograms, then check their pc bounds. Likewise, we need to
5829 check lexical blocks as well, as they may also contain subprogram
5830 definitions. */
5831 while (child && child->tag)
5832 {
5833 if (child->tag == DW_TAG_subprogram
5834 || child->tag == DW_TAG_lexical_block)
5835 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5836 child = sibling_die (child);
5837 }
5838}
5839
fae299cd
DC
5840/* Get the low and high pc's represented by the scope DIE, and store
5841 them in *LOWPC and *HIGHPC. If the correct values can't be
5842 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5843
5844static void
5845get_scope_pc_bounds (struct die_info *die,
5846 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5847 struct dwarf2_cu *cu)
5848{
5849 CORE_ADDR best_low = (CORE_ADDR) -1;
5850 CORE_ADDR best_high = (CORE_ADDR) 0;
5851 CORE_ADDR current_low, current_high;
5852
d85a05f0 5853 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5854 {
5855 best_low = current_low;
5856 best_high = current_high;
5857 }
5858 else
5859 {
5860 struct die_info *child = die->child;
5861
5862 while (child && child->tag)
5863 {
5864 switch (child->tag) {
5865 case DW_TAG_subprogram:
b084d499 5866 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5867 break;
5868 case DW_TAG_namespace:
f55ee35c 5869 case DW_TAG_module:
fae299cd
DC
5870 /* FIXME: carlton/2004-01-16: Should we do this for
5871 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5872 that current GCC's always emit the DIEs corresponding
5873 to definitions of methods of classes as children of a
5874 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5875 the DIEs giving the declarations, which could be
5876 anywhere). But I don't see any reason why the
5877 standards says that they have to be there. */
5878 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5879
5880 if (current_low != ((CORE_ADDR) -1))
5881 {
5882 best_low = min (best_low, current_low);
5883 best_high = max (best_high, current_high);
5884 }
5885 break;
5886 default:
5887 /* Ignore. */
5888 break;
5889 }
5890
5891 child = sibling_die (child);
5892 }
5893 }
5894
5895 *lowpc = best_low;
5896 *highpc = best_high;
5897}
5898
801e3a5b
JB
5899/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5900 in DIE. */
5901static void
5902dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5903 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5904{
5905 struct attribute *attr;
5906
5907 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5908 if (attr)
5909 {
5910 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5911
801e3a5b
JB
5912 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5913 if (attr)
5914 {
5915 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5916
801e3a5b
JB
5917 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5918 }
5919 }
5920
5921 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5922 if (attr)
5923 {
5924 bfd *obfd = cu->objfile->obfd;
5925
5926 /* The value of the DW_AT_ranges attribute is the offset of the
5927 address range list in the .debug_ranges section. */
5928 unsigned long offset = DW_UNSND (attr);
dce234bc 5929 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5930
5931 /* For some target architectures, but not others, the
5932 read_address function sign-extends the addresses it returns.
5933 To recognize base address selection entries, we need a
5934 mask. */
5935 unsigned int addr_size = cu->header.addr_size;
5936 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5937
5938 /* The base address, to which the next pair is relative. Note
5939 that this 'base' is a DWARF concept: most entries in a range
5940 list are relative, to reduce the number of relocs against the
5941 debugging information. This is separate from this function's
5942 'baseaddr' argument, which GDB uses to relocate debugging
5943 information from a shared library based on the address at
5944 which the library was loaded. */
d00adf39
DE
5945 CORE_ADDR base = cu->base_address;
5946 int base_known = cu->base_known;
801e3a5b 5947
be391dca 5948 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5949 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5950 {
5951 complaint (&symfile_complaints,
5952 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5953 offset);
5954 return;
5955 }
5956
5957 for (;;)
5958 {
5959 unsigned int bytes_read;
5960 CORE_ADDR start, end;
5961
5962 start = read_address (obfd, buffer, cu, &bytes_read);
5963 buffer += bytes_read;
5964 end = read_address (obfd, buffer, cu, &bytes_read);
5965 buffer += bytes_read;
5966
5967 /* Did we find the end of the range list? */
5968 if (start == 0 && end == 0)
5969 break;
5970
5971 /* Did we find a base address selection entry? */
5972 else if ((start & base_select_mask) == base_select_mask)
5973 {
5974 base = end;
5975 base_known = 1;
5976 }
5977
5978 /* We found an ordinary address range. */
5979 else
5980 {
5981 if (!base_known)
5982 {
5983 complaint (&symfile_complaints,
5984 _("Invalid .debug_ranges data (no base address)"));
5985 return;
5986 }
5987
6e70227d
DE
5988 record_block_range (block,
5989 baseaddr + base + start,
801e3a5b
JB
5990 baseaddr + base + end - 1);
5991 }
5992 }
5993 }
5994}
5995
c906108c
SS
5996/* Add an aggregate field to the field list. */
5997
5998static void
107d2387 5999dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6000 struct dwarf2_cu *cu)
6e70227d 6001{
e7c27a73 6002 struct objfile *objfile = cu->objfile;
5e2b427d 6003 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6004 struct nextfield *new_field;
6005 struct attribute *attr;
6006 struct field *fp;
6007 char *fieldname = "";
6008
6009 /* Allocate a new field list entry and link it in. */
6010 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6011 make_cleanup (xfree, new_field);
c906108c 6012 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6013
6014 if (die->tag == DW_TAG_inheritance)
6015 {
6016 new_field->next = fip->baseclasses;
6017 fip->baseclasses = new_field;
6018 }
6019 else
6020 {
6021 new_field->next = fip->fields;
6022 fip->fields = new_field;
6023 }
c906108c
SS
6024 fip->nfields++;
6025
6026 /* Handle accessibility and virtuality of field.
6027 The default accessibility for members is public, the default
6028 accessibility for inheritance is private. */
6029 if (die->tag != DW_TAG_inheritance)
6030 new_field->accessibility = DW_ACCESS_public;
6031 else
6032 new_field->accessibility = DW_ACCESS_private;
6033 new_field->virtuality = DW_VIRTUALITY_none;
6034
e142c38c 6035 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6036 if (attr)
6037 new_field->accessibility = DW_UNSND (attr);
6038 if (new_field->accessibility != DW_ACCESS_public)
6039 fip->non_public_fields = 1;
e142c38c 6040 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6041 if (attr)
6042 new_field->virtuality = DW_UNSND (attr);
6043
6044 fp = &new_field->field;
a9a9bd0f 6045
e142c38c 6046 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6047 {
a9a9bd0f 6048 /* Data member other than a C++ static data member. */
6e70227d 6049
c906108c 6050 /* Get type of field. */
e7c27a73 6051 fp->type = die_type (die, cu);
c906108c 6052
d6a843b5 6053 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6054
c906108c 6055 /* Get bit size of field (zero if none). */
e142c38c 6056 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6057 if (attr)
6058 {
6059 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6060 }
6061 else
6062 {
6063 FIELD_BITSIZE (*fp) = 0;
6064 }
6065
6066 /* Get bit offset of field. */
e142c38c 6067 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6068 if (attr)
6069 {
d4b96c9a 6070 int byte_offset = 0;
c6a0999f 6071
3690dd37 6072 if (attr_form_is_section_offset (attr))
d4b96c9a 6073 dwarf2_complex_location_expr_complaint ();
3690dd37 6074 else if (attr_form_is_constant (attr))
c6a0999f 6075 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6076 else if (attr_form_is_block (attr))
c6a0999f 6077 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6078 else
6079 dwarf2_complex_location_expr_complaint ();
c6a0999f 6080
d6a843b5 6081 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6082 }
e142c38c 6083 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6084 if (attr)
6085 {
5e2b427d 6086 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6087 {
6088 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6089 additional bit offset from the MSB of the containing
6090 anonymous object to the MSB of the field. We don't
6091 have to do anything special since we don't need to
6092 know the size of the anonymous object. */
c906108c
SS
6093 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6094 }
6095 else
6096 {
6097 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6098 MSB of the anonymous object, subtract off the number of
6099 bits from the MSB of the field to the MSB of the
6100 object, and then subtract off the number of bits of
6101 the field itself. The result is the bit offset of
6102 the LSB of the field. */
c906108c
SS
6103 int anonymous_size;
6104 int bit_offset = DW_UNSND (attr);
6105
e142c38c 6106 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6107 if (attr)
6108 {
6109 /* The size of the anonymous object containing
6110 the bit field is explicit, so use the
6111 indicated size (in bytes). */
6112 anonymous_size = DW_UNSND (attr);
6113 }
6114 else
6115 {
6116 /* The size of the anonymous object containing
6117 the bit field must be inferred from the type
6118 attribute of the data member containing the
6119 bit field. */
6120 anonymous_size = TYPE_LENGTH (fp->type);
6121 }
6122 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6123 - bit_offset - FIELD_BITSIZE (*fp);
6124 }
6125 }
6126
6127 /* Get name of field. */
39cbfefa
DJ
6128 fieldname = dwarf2_name (die, cu);
6129 if (fieldname == NULL)
6130 fieldname = "";
d8151005
DJ
6131
6132 /* The name is already allocated along with this objfile, so we don't
6133 need to duplicate it for the type. */
6134 fp->name = fieldname;
c906108c
SS
6135
6136 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6137 pointer or virtual base class pointer) to private. */
e142c38c 6138 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6139 {
d48cc9dd 6140 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6141 new_field->accessibility = DW_ACCESS_private;
6142 fip->non_public_fields = 1;
6143 }
6144 }
a9a9bd0f 6145 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6146 {
a9a9bd0f
DC
6147 /* C++ static member. */
6148
6149 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6150 is a declaration, but all versions of G++ as of this writing
6151 (so through at least 3.2.1) incorrectly generate
6152 DW_TAG_variable tags. */
6e70227d 6153
c906108c 6154 char *physname;
c906108c 6155
a9a9bd0f 6156 /* Get name of field. */
39cbfefa
DJ
6157 fieldname = dwarf2_name (die, cu);
6158 if (fieldname == NULL)
c906108c
SS
6159 return;
6160
254e6b9e 6161 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6162 if (attr
6163 /* Only create a symbol if this is an external value.
6164 new_symbol checks this and puts the value in the global symbol
6165 table, which we want. If it is not external, new_symbol
6166 will try to put the value in cu->list_in_scope which is wrong. */
6167 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6168 {
6169 /* A static const member, not much different than an enum as far as
6170 we're concerned, except that we can support more types. */
6171 new_symbol (die, NULL, cu);
6172 }
6173
2df3850c 6174 /* Get physical name. */
94af9270 6175 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6176
d8151005
DJ
6177 /* The name is already allocated along with this objfile, so we don't
6178 need to duplicate it for the type. */
6179 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6180 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6181 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6182 }
6183 else if (die->tag == DW_TAG_inheritance)
6184 {
6185 /* C++ base class field. */
e142c38c 6186 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6187 if (attr)
d4b96c9a
JK
6188 {
6189 int byte_offset = 0;
6190
6191 if (attr_form_is_section_offset (attr))
6192 dwarf2_complex_location_expr_complaint ();
6193 else if (attr_form_is_constant (attr))
6194 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6195 else if (attr_form_is_block (attr))
6196 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6197 else
6198 dwarf2_complex_location_expr_complaint ();
6199
6200 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6201 }
c906108c 6202 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6203 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6204 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6205 fip->nbaseclasses++;
6206 }
6207}
6208
98751a41
JK
6209/* Add a typedef defined in the scope of the FIP's class. */
6210
6211static void
6212dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6213 struct dwarf2_cu *cu)
6e70227d 6214{
98751a41
JK
6215 struct objfile *objfile = cu->objfile;
6216 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6217 struct typedef_field_list *new_field;
6218 struct attribute *attr;
6219 struct typedef_field *fp;
6220 char *fieldname = "";
6221
6222 /* Allocate a new field list entry and link it in. */
6223 new_field = xzalloc (sizeof (*new_field));
6224 make_cleanup (xfree, new_field);
6225
6226 gdb_assert (die->tag == DW_TAG_typedef);
6227
6228 fp = &new_field->field;
6229
6230 /* Get name of field. */
6231 fp->name = dwarf2_name (die, cu);
6232 if (fp->name == NULL)
6233 return;
6234
6235 fp->type = read_type_die (die, cu);
6236
6237 new_field->next = fip->typedef_field_list;
6238 fip->typedef_field_list = new_field;
6239 fip->typedef_field_list_count++;
6240}
6241
c906108c
SS
6242/* Create the vector of fields, and attach it to the type. */
6243
6244static void
fba45db2 6245dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6246 struct dwarf2_cu *cu)
c906108c
SS
6247{
6248 int nfields = fip->nfields;
6249
6250 /* Record the field count, allocate space for the array of fields,
6251 and create blank accessibility bitfields if necessary. */
6252 TYPE_NFIELDS (type) = nfields;
6253 TYPE_FIELDS (type) = (struct field *)
6254 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6255 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6256
b4ba55a1 6257 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6258 {
6259 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6260
6261 TYPE_FIELD_PRIVATE_BITS (type) =
6262 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6263 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6264
6265 TYPE_FIELD_PROTECTED_BITS (type) =
6266 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6267 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6268
8b93c54c
TT
6269 /* We don't set TYPE_FIELD_IGNORE_BITS here. The DWARF reader
6270 never sets any bits in that array, so leaving it NULL lets us
6271 save a little memory. */
c906108c
SS
6272 }
6273
6274 /* If the type has baseclasses, allocate and clear a bit vector for
6275 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6276 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6277 {
6278 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6279 unsigned char *pointer;
c906108c
SS
6280
6281 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6282 pointer = TYPE_ALLOC (type, num_bytes);
6283 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6284 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6285 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6286 }
6287
6288 /* Copy the saved-up fields into the field vector. Start from the head
6289 of the list, adding to the tail of the field array, so that they end
6290 up in the same order in the array in which they were added to the list. */
6291 while (nfields-- > 0)
6292 {
7d0ccb61
DJ
6293 struct nextfield *fieldp;
6294
6295 if (fip->fields)
6296 {
6297 fieldp = fip->fields;
6298 fip->fields = fieldp->next;
6299 }
6300 else
6301 {
6302 fieldp = fip->baseclasses;
6303 fip->baseclasses = fieldp->next;
6304 }
6305
6306 TYPE_FIELD (type, nfields) = fieldp->field;
6307 switch (fieldp->accessibility)
c906108c 6308 {
c5aa993b 6309 case DW_ACCESS_private:
b4ba55a1
JB
6310 if (cu->language != language_ada)
6311 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6312 break;
c906108c 6313
c5aa993b 6314 case DW_ACCESS_protected:
b4ba55a1
JB
6315 if (cu->language != language_ada)
6316 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6317 break;
c906108c 6318
c5aa993b
JM
6319 case DW_ACCESS_public:
6320 break;
c906108c 6321
c5aa993b
JM
6322 default:
6323 /* Unknown accessibility. Complain and treat it as public. */
6324 {
e2e0b3e5 6325 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6326 fieldp->accessibility);
c5aa993b
JM
6327 }
6328 break;
c906108c
SS
6329 }
6330 if (nfields < fip->nbaseclasses)
6331 {
7d0ccb61 6332 switch (fieldp->virtuality)
c906108c 6333 {
c5aa993b
JM
6334 case DW_VIRTUALITY_virtual:
6335 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6336 if (cu->language == language_ada)
6337 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6338 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6339 break;
c906108c
SS
6340 }
6341 }
c906108c
SS
6342 }
6343}
6344
c906108c
SS
6345/* Add a member function to the proper fieldlist. */
6346
6347static void
107d2387 6348dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6349 struct type *type, struct dwarf2_cu *cu)
c906108c 6350{
e7c27a73 6351 struct objfile *objfile = cu->objfile;
c906108c
SS
6352 struct attribute *attr;
6353 struct fnfieldlist *flp;
6354 int i;
6355 struct fn_field *fnp;
6356 char *fieldname;
c906108c 6357 struct nextfnfield *new_fnfield;
f792889a 6358 struct type *this_type;
c906108c 6359
b4ba55a1
JB
6360 if (cu->language == language_ada)
6361 error ("unexpected member function in Ada type");
6362
2df3850c 6363 /* Get name of member function. */
39cbfefa
DJ
6364 fieldname = dwarf2_name (die, cu);
6365 if (fieldname == NULL)
2df3850c 6366 return;
c906108c 6367
c906108c
SS
6368 /* Look up member function name in fieldlist. */
6369 for (i = 0; i < fip->nfnfields; i++)
6370 {
27bfe10e 6371 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6372 break;
6373 }
6374
6375 /* Create new list element if necessary. */
6376 if (i < fip->nfnfields)
6377 flp = &fip->fnfieldlists[i];
6378 else
6379 {
6380 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6381 {
6382 fip->fnfieldlists = (struct fnfieldlist *)
6383 xrealloc (fip->fnfieldlists,
6384 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6385 * sizeof (struct fnfieldlist));
c906108c 6386 if (fip->nfnfields == 0)
c13c43fd 6387 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6388 }
6389 flp = &fip->fnfieldlists[fip->nfnfields];
6390 flp->name = fieldname;
6391 flp->length = 0;
6392 flp->head = NULL;
3da10d80 6393 i = fip->nfnfields++;
c906108c
SS
6394 }
6395
6396 /* Create a new member function field and chain it to the field list
6397 entry. */
6398 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6399 make_cleanup (xfree, new_fnfield);
c906108c
SS
6400 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6401 new_fnfield->next = flp->head;
6402 flp->head = new_fnfield;
6403 flp->length++;
6404
6405 /* Fill in the member function field info. */
6406 fnp = &new_fnfield->fnfield;
3da10d80
KS
6407
6408 /* Delay processing of the physname until later. */
6409 if (cu->language == language_cplus || cu->language == language_java)
6410 {
6411 add_to_method_list (type, i, flp->length - 1, fieldname,
6412 die, cu);
6413 }
6414 else
6415 {
6416 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6417 fnp->physname = physname ? physname : "";
6418 }
6419
c906108c 6420 fnp->type = alloc_type (objfile);
f792889a
DJ
6421 this_type = read_type_die (die, cu);
6422 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6423 {
f792889a 6424 int nparams = TYPE_NFIELDS (this_type);
c906108c 6425
f792889a 6426 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6427 of the method itself (TYPE_CODE_METHOD). */
6428 smash_to_method_type (fnp->type, type,
f792889a
DJ
6429 TYPE_TARGET_TYPE (this_type),
6430 TYPE_FIELDS (this_type),
6431 TYPE_NFIELDS (this_type),
6432 TYPE_VARARGS (this_type));
c906108c
SS
6433
6434 /* Handle static member functions.
c5aa993b
JM
6435 Dwarf2 has no clean way to discern C++ static and non-static
6436 member functions. G++ helps GDB by marking the first
6437 parameter for non-static member functions (which is the
6438 this pointer) as artificial. We obtain this information
6439 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6440 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6441 fnp->voffset = VOFFSET_STATIC;
6442 }
6443 else
e2e0b3e5 6444 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6445 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6446
6447 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6448 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6449 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6450
6451 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6452 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6453
6454 /* Get accessibility. */
e142c38c 6455 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6456 if (attr)
6457 {
6458 switch (DW_UNSND (attr))
6459 {
c5aa993b
JM
6460 case DW_ACCESS_private:
6461 fnp->is_private = 1;
6462 break;
6463 case DW_ACCESS_protected:
6464 fnp->is_protected = 1;
6465 break;
c906108c
SS
6466 }
6467 }
6468
b02dede2 6469 /* Check for artificial methods. */
e142c38c 6470 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6471 if (attr && DW_UNSND (attr) != 0)
6472 fnp->is_artificial = 1;
6473
0d564a31 6474 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6475 function. For older versions of GCC, this is an offset in the
6476 appropriate virtual table, as specified by DW_AT_containing_type.
6477 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6478 to the object address. */
6479
e142c38c 6480 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6481 if (attr)
8e19ed76 6482 {
aec5aa8b 6483 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6484 {
aec5aa8b
TT
6485 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6486 {
6487 /* Old-style GCC. */
6488 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6489 }
6490 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6491 || (DW_BLOCK (attr)->size > 1
6492 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6493 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6494 {
6495 struct dwarf_block blk;
6496 int offset;
6497
6498 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6499 ? 1 : 2);
6500 blk.size = DW_BLOCK (attr)->size - offset;
6501 blk.data = DW_BLOCK (attr)->data + offset;
6502 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6503 if ((fnp->voffset % cu->header.addr_size) != 0)
6504 dwarf2_complex_location_expr_complaint ();
6505 else
6506 fnp->voffset /= cu->header.addr_size;
6507 fnp->voffset += 2;
6508 }
6509 else
6510 dwarf2_complex_location_expr_complaint ();
6511
6512 if (!fnp->fcontext)
6513 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6514 }
3690dd37 6515 else if (attr_form_is_section_offset (attr))
8e19ed76 6516 {
4d3c2250 6517 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6518 }
6519 else
6520 {
4d3c2250
KB
6521 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6522 fieldname);
8e19ed76 6523 }
0d564a31 6524 }
d48cc9dd
DJ
6525 else
6526 {
6527 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6528 if (attr && DW_UNSND (attr))
6529 {
6530 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6531 complaint (&symfile_complaints,
6532 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6533 fieldname, die->offset);
9655fd1a 6534 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6535 TYPE_CPLUS_DYNAMIC (type) = 1;
6536 }
6537 }
c906108c
SS
6538}
6539
6540/* Create the vector of member function fields, and attach it to the type. */
6541
6542static void
fba45db2 6543dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6544 struct dwarf2_cu *cu)
c906108c
SS
6545{
6546 struct fnfieldlist *flp;
6547 int total_length = 0;
6548 int i;
6549
b4ba55a1
JB
6550 if (cu->language == language_ada)
6551 error ("unexpected member functions in Ada type");
6552
c906108c
SS
6553 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6554 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6555 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6556
6557 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6558 {
6559 struct nextfnfield *nfp = flp->head;
6560 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6561 int k;
6562
6563 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6564 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6565 fn_flp->fn_fields = (struct fn_field *)
6566 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6567 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6568 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6569
6570 total_length += flp->length;
6571 }
6572
6573 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6574 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6575}
6576
1168df01
JB
6577/* Returns non-zero if NAME is the name of a vtable member in CU's
6578 language, zero otherwise. */
6579static int
6580is_vtable_name (const char *name, struct dwarf2_cu *cu)
6581{
6582 static const char vptr[] = "_vptr";
987504bb 6583 static const char vtable[] = "vtable";
1168df01 6584
987504bb
JJ
6585 /* Look for the C++ and Java forms of the vtable. */
6586 if ((cu->language == language_java
6587 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6588 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6589 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6590 return 1;
6591
6592 return 0;
6593}
6594
c0dd20ea 6595/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6596 functions, with the ABI-specified layout. If TYPE describes
6597 such a structure, smash it into a member function type.
61049d3b
DJ
6598
6599 GCC shouldn't do this; it should just output pointer to member DIEs.
6600 This is GCC PR debug/28767. */
c0dd20ea 6601
0b92b5bb
TT
6602static void
6603quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6604{
0b92b5bb 6605 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6606
6607 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6608 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6609 return;
c0dd20ea
DJ
6610
6611 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6612 if (TYPE_FIELD_NAME (type, 0) == NULL
6613 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6614 || TYPE_FIELD_NAME (type, 1) == NULL
6615 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6616 return;
c0dd20ea
DJ
6617
6618 /* Find the type of the method. */
0b92b5bb 6619 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6620 if (pfn_type == NULL
6621 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6622 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6623 return;
c0dd20ea
DJ
6624
6625 /* Look for the "this" argument. */
6626 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6627 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6628 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6629 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6630 return;
c0dd20ea
DJ
6631
6632 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6633 new_type = alloc_type (objfile);
6634 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6635 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6636 TYPE_VARARGS (pfn_type));
0b92b5bb 6637 smash_to_methodptr_type (type, new_type);
c0dd20ea 6638}
1168df01 6639
c906108c
SS
6640/* Called when we find the DIE that starts a structure or union scope
6641 (definition) to process all dies that define the members of the
6642 structure or union.
6643
6644 NOTE: we need to call struct_type regardless of whether or not the
6645 DIE has an at_name attribute, since it might be an anonymous
6646 structure or union. This gets the type entered into our set of
6647 user defined types.
6648
6649 However, if the structure is incomplete (an opaque struct/union)
6650 then suppress creating a symbol table entry for it since gdb only
6651 wants to find the one with the complete definition. Note that if
6652 it is complete, we just call new_symbol, which does it's own
6653 checking about whether the struct/union is anonymous or not (and
6654 suppresses creating a symbol table entry itself). */
6655
f792889a 6656static struct type *
134d01f1 6657read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6658{
e7c27a73 6659 struct objfile *objfile = cu->objfile;
c906108c
SS
6660 struct type *type;
6661 struct attribute *attr;
39cbfefa 6662 char *name;
d3f41bb1 6663 struct cleanup *back_to;
c906108c 6664
348e048f
DE
6665 /* If the definition of this type lives in .debug_types, read that type.
6666 Don't follow DW_AT_specification though, that will take us back up
6667 the chain and we want to go down. */
6668 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6669 if (attr)
6670 {
6671 struct dwarf2_cu *type_cu = cu;
6672 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6673
348e048f
DE
6674 /* We could just recurse on read_structure_type, but we need to call
6675 get_die_type to ensure only one type for this DIE is created.
6676 This is important, for example, because for c++ classes we need
6677 TYPE_NAME set which is only done by new_symbol. Blech. */
6678 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6679
6680 /* TYPE_CU may not be the same as CU.
6681 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6682 return set_die_type (die, type, cu);
6683 }
6684
d3f41bb1
TT
6685 back_to = make_cleanup (null_cleanup, 0);
6686
c0dd20ea 6687 type = alloc_type (objfile);
c906108c 6688 INIT_CPLUS_SPECIFIC (type);
93311388 6689
39cbfefa
DJ
6690 name = dwarf2_name (die, cu);
6691 if (name != NULL)
c906108c 6692 {
987504bb
JJ
6693 if (cu->language == language_cplus
6694 || cu->language == language_java)
63d06c5c 6695 {
3da10d80
KS
6696 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6697
6698 /* dwarf2_full_name might have already finished building the DIE's
6699 type. If so, there is no need to continue. */
6700 if (get_die_type (die, cu) != NULL)
6701 return get_die_type (die, cu);
6702
6703 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6704 if (die->tag == DW_TAG_structure_type
6705 || die->tag == DW_TAG_class_type)
6706 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6707 }
6708 else
6709 {
d8151005
DJ
6710 /* The name is already allocated along with this objfile, so
6711 we don't need to duplicate it for the type. */
94af9270
KS
6712 TYPE_TAG_NAME (type) = (char *) name;
6713 if (die->tag == DW_TAG_class_type)
6714 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6715 }
c906108c
SS
6716 }
6717
6718 if (die->tag == DW_TAG_structure_type)
6719 {
6720 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6721 }
6722 else if (die->tag == DW_TAG_union_type)
6723 {
6724 TYPE_CODE (type) = TYPE_CODE_UNION;
6725 }
6726 else
6727 {
c906108c
SS
6728 TYPE_CODE (type) = TYPE_CODE_CLASS;
6729 }
6730
0cc2414c
TT
6731 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6732 TYPE_DECLARED_CLASS (type) = 1;
6733
e142c38c 6734 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6735 if (attr)
6736 {
6737 TYPE_LENGTH (type) = DW_UNSND (attr);
6738 }
6739 else
6740 {
6741 TYPE_LENGTH (type) = 0;
6742 }
6743
876cecd0 6744 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6745 if (die_is_declaration (die, cu))
876cecd0 6746 TYPE_STUB (type) = 1;
a6c727b2
DJ
6747 else if (attr == NULL && die->child == NULL
6748 && producer_is_realview (cu->producer))
6749 /* RealView does not output the required DW_AT_declaration
6750 on incomplete types. */
6751 TYPE_STUB (type) = 1;
dc718098 6752
c906108c
SS
6753 /* We need to add the type field to the die immediately so we don't
6754 infinitely recurse when dealing with pointers to the structure
6755 type within the structure itself. */
1c379e20 6756 set_die_type (die, type, cu);
c906108c 6757
7e314c57
JK
6758 /* set_die_type should be already done. */
6759 set_descriptive_type (type, die, cu);
6760
e142c38c 6761 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6762 {
6763 struct field_info fi;
6764 struct die_info *child_die;
34eaf542 6765 VEC (symbolp) *template_args = NULL;
c906108c
SS
6766
6767 memset (&fi, 0, sizeof (struct field_info));
6768
639d11d3 6769 child_die = die->child;
c906108c
SS
6770
6771 while (child_die && child_die->tag)
6772 {
a9a9bd0f
DC
6773 if (child_die->tag == DW_TAG_member
6774 || child_die->tag == DW_TAG_variable)
c906108c 6775 {
a9a9bd0f
DC
6776 /* NOTE: carlton/2002-11-05: A C++ static data member
6777 should be a DW_TAG_member that is a declaration, but
6778 all versions of G++ as of this writing (so through at
6779 least 3.2.1) incorrectly generate DW_TAG_variable
6780 tags for them instead. */
e7c27a73 6781 dwarf2_add_field (&fi, child_die, cu);
c906108c 6782 }
8713b1b1 6783 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6784 {
6785 /* C++ member function. */
e7c27a73 6786 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6787 }
6788 else if (child_die->tag == DW_TAG_inheritance)
6789 {
6790 /* C++ base class field. */
e7c27a73 6791 dwarf2_add_field (&fi, child_die, cu);
c906108c 6792 }
98751a41
JK
6793 else if (child_die->tag == DW_TAG_typedef)
6794 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6795 else if (child_die->tag == DW_TAG_template_type_param
6796 || child_die->tag == DW_TAG_template_value_param)
6797 {
6798 struct symbol *arg = new_symbol (child_die, NULL, cu);
6799
6800 VEC_safe_push (symbolp, template_args, arg);
6801 }
6802
c906108c
SS
6803 child_die = sibling_die (child_die);
6804 }
6805
34eaf542
TT
6806 /* Attach template arguments to type. */
6807 if (! VEC_empty (symbolp, template_args))
6808 {
6809 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6810 TYPE_N_TEMPLATE_ARGUMENTS (type)
6811 = VEC_length (symbolp, template_args);
6812 TYPE_TEMPLATE_ARGUMENTS (type)
6813 = obstack_alloc (&objfile->objfile_obstack,
6814 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6815 * sizeof (struct symbol *)));
6816 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6817 VEC_address (symbolp, template_args),
6818 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6819 * sizeof (struct symbol *)));
6820 VEC_free (symbolp, template_args);
6821 }
6822
c906108c
SS
6823 /* Attach fields and member functions to the type. */
6824 if (fi.nfields)
e7c27a73 6825 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6826 if (fi.nfnfields)
6827 {
e7c27a73 6828 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6829
c5aa993b 6830 /* Get the type which refers to the base class (possibly this
c906108c 6831 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6832 class from the DW_AT_containing_type attribute. This use of
6833 DW_AT_containing_type is a GNU extension. */
c906108c 6834
e142c38c 6835 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6836 {
e7c27a73 6837 struct type *t = die_containing_type (die, cu);
c906108c
SS
6838
6839 TYPE_VPTR_BASETYPE (type) = t;
6840 if (type == t)
6841 {
c906108c
SS
6842 int i;
6843
6844 /* Our own class provides vtbl ptr. */
6845 for (i = TYPE_NFIELDS (t) - 1;
6846 i >= TYPE_N_BASECLASSES (t);
6847 --i)
6848 {
6849 char *fieldname = TYPE_FIELD_NAME (t, i);
6850
1168df01 6851 if (is_vtable_name (fieldname, cu))
c906108c
SS
6852 {
6853 TYPE_VPTR_FIELDNO (type) = i;
6854 break;
6855 }
6856 }
6857
6858 /* Complain if virtual function table field not found. */
6859 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6860 complaint (&symfile_complaints,
e2e0b3e5 6861 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6862 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6863 "");
c906108c
SS
6864 }
6865 else
6866 {
6867 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6868 }
6869 }
f6235d4c
EZ
6870 else if (cu->producer
6871 && strncmp (cu->producer,
6872 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6873 {
6874 /* The IBM XLC compiler does not provide direct indication
6875 of the containing type, but the vtable pointer is
6876 always named __vfp. */
6877
6878 int i;
6879
6880 for (i = TYPE_NFIELDS (type) - 1;
6881 i >= TYPE_N_BASECLASSES (type);
6882 --i)
6883 {
6884 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6885 {
6886 TYPE_VPTR_FIELDNO (type) = i;
6887 TYPE_VPTR_BASETYPE (type) = type;
6888 break;
6889 }
6890 }
6891 }
c906108c 6892 }
98751a41
JK
6893
6894 /* Copy fi.typedef_field_list linked list elements content into the
6895 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6896 if (fi.typedef_field_list)
6897 {
6898 int i = fi.typedef_field_list_count;
6899
a0d7a4ff 6900 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6901 TYPE_TYPEDEF_FIELD_ARRAY (type)
6902 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6903 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6904
6905 /* Reverse the list order to keep the debug info elements order. */
6906 while (--i >= 0)
6907 {
6908 struct typedef_field *dest, *src;
6e70227d 6909
98751a41
JK
6910 dest = &TYPE_TYPEDEF_FIELD (type, i);
6911 src = &fi.typedef_field_list->field;
6912 fi.typedef_field_list = fi.typedef_field_list->next;
6913 *dest = *src;
6914 }
6915 }
c906108c 6916 }
63d06c5c 6917
0b92b5bb
TT
6918 quirk_gcc_member_function_pointer (type, cu->objfile);
6919
0114d602 6920 do_cleanups (back_to);
f792889a 6921 return type;
c906108c
SS
6922}
6923
134d01f1
DJ
6924static void
6925process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6926{
90aeadfc 6927 struct die_info *child_die = die->child;
f792889a 6928 struct type *this_type;
c906108c 6929
f792889a
DJ
6930 this_type = get_die_type (die, cu);
6931 if (this_type == NULL)
6932 this_type = read_structure_type (die, cu);
c906108c 6933
90aeadfc
DC
6934 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6935 snapshots) has been known to create a die giving a declaration
6936 for a class that has, as a child, a die giving a definition for a
6937 nested class. So we have to process our children even if the
6938 current die is a declaration. Normally, of course, a declaration
6939 won't have any children at all. */
134d01f1 6940
90aeadfc
DC
6941 while (child_die != NULL && child_die->tag)
6942 {
6943 if (child_die->tag == DW_TAG_member
6944 || child_die->tag == DW_TAG_variable
34eaf542
TT
6945 || child_die->tag == DW_TAG_inheritance
6946 || child_die->tag == DW_TAG_template_value_param
6947 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6948 {
90aeadfc 6949 /* Do nothing. */
134d01f1 6950 }
90aeadfc
DC
6951 else
6952 process_die (child_die, cu);
134d01f1 6953
90aeadfc 6954 child_die = sibling_die (child_die);
134d01f1
DJ
6955 }
6956
fa4028e9
JB
6957 /* Do not consider external references. According to the DWARF standard,
6958 these DIEs are identified by the fact that they have no byte_size
6959 attribute, and a declaration attribute. */
6960 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6961 || !die_is_declaration (die, cu))
f792889a 6962 new_symbol (die, this_type, cu);
134d01f1
DJ
6963}
6964
6965/* Given a DW_AT_enumeration_type die, set its type. We do not
6966 complete the type's fields yet, or create any symbols. */
c906108c 6967
f792889a 6968static struct type *
134d01f1 6969read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6970{
e7c27a73 6971 struct objfile *objfile = cu->objfile;
c906108c 6972 struct type *type;
c906108c 6973 struct attribute *attr;
0114d602 6974 const char *name;
134d01f1 6975
348e048f
DE
6976 /* If the definition of this type lives in .debug_types, read that type.
6977 Don't follow DW_AT_specification though, that will take us back up
6978 the chain and we want to go down. */
6979 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6980 if (attr)
6981 {
6982 struct dwarf2_cu *type_cu = cu;
6983 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6984
348e048f 6985 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6986
6987 /* TYPE_CU may not be the same as CU.
6988 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6989 return set_die_type (die, type, cu);
6990 }
6991
c906108c
SS
6992 type = alloc_type (objfile);
6993
6994 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6995 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6996 if (name != NULL)
0114d602 6997 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6998
e142c38c 6999 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7000 if (attr)
7001 {
7002 TYPE_LENGTH (type) = DW_UNSND (attr);
7003 }
7004 else
7005 {
7006 TYPE_LENGTH (type) = 0;
7007 }
7008
137033e9
JB
7009 /* The enumeration DIE can be incomplete. In Ada, any type can be
7010 declared as private in the package spec, and then defined only
7011 inside the package body. Such types are known as Taft Amendment
7012 Types. When another package uses such a type, an incomplete DIE
7013 may be generated by the compiler. */
02eb380e 7014 if (die_is_declaration (die, cu))
876cecd0 7015 TYPE_STUB (type) = 1;
02eb380e 7016
f792889a 7017 return set_die_type (die, type, cu);
134d01f1
DJ
7018}
7019
7020/* Given a pointer to a die which begins an enumeration, process all
7021 the dies that define the members of the enumeration, and create the
7022 symbol for the enumeration type.
7023
7024 NOTE: We reverse the order of the element list. */
7025
7026static void
7027process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7028{
f792889a 7029 struct type *this_type;
134d01f1 7030
f792889a
DJ
7031 this_type = get_die_type (die, cu);
7032 if (this_type == NULL)
7033 this_type = read_enumeration_type (die, cu);
9dc481d3 7034
639d11d3 7035 if (die->child != NULL)
c906108c 7036 {
9dc481d3
DE
7037 struct die_info *child_die;
7038 struct symbol *sym;
7039 struct field *fields = NULL;
7040 int num_fields = 0;
7041 int unsigned_enum = 1;
7042 char *name;
7043
639d11d3 7044 child_die = die->child;
c906108c
SS
7045 while (child_die && child_die->tag)
7046 {
7047 if (child_die->tag != DW_TAG_enumerator)
7048 {
e7c27a73 7049 process_die (child_die, cu);
c906108c
SS
7050 }
7051 else
7052 {
39cbfefa
DJ
7053 name = dwarf2_name (child_die, cu);
7054 if (name)
c906108c 7055 {
f792889a 7056 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7057 if (SYMBOL_VALUE (sym) < 0)
7058 unsigned_enum = 0;
7059
7060 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7061 {
7062 fields = (struct field *)
7063 xrealloc (fields,
7064 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7065 * sizeof (struct field));
c906108c
SS
7066 }
7067
3567439c 7068 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7069 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7070 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7071 FIELD_BITSIZE (fields[num_fields]) = 0;
7072
7073 num_fields++;
7074 }
7075 }
7076
7077 child_die = sibling_die (child_die);
7078 }
7079
7080 if (num_fields)
7081 {
f792889a
DJ
7082 TYPE_NFIELDS (this_type) = num_fields;
7083 TYPE_FIELDS (this_type) = (struct field *)
7084 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7085 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7086 sizeof (struct field) * num_fields);
b8c9b27d 7087 xfree (fields);
c906108c
SS
7088 }
7089 if (unsigned_enum)
876cecd0 7090 TYPE_UNSIGNED (this_type) = 1;
c906108c 7091 }
134d01f1 7092
f792889a 7093 new_symbol (die, this_type, cu);
c906108c
SS
7094}
7095
7096/* Extract all information from a DW_TAG_array_type DIE and put it in
7097 the DIE's type field. For now, this only handles one dimensional
7098 arrays. */
7099
f792889a 7100static struct type *
e7c27a73 7101read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7102{
e7c27a73 7103 struct objfile *objfile = cu->objfile;
c906108c 7104 struct die_info *child_die;
7e314c57 7105 struct type *type;
c906108c
SS
7106 struct type *element_type, *range_type, *index_type;
7107 struct type **range_types = NULL;
7108 struct attribute *attr;
7109 int ndim = 0;
7110 struct cleanup *back_to;
39cbfefa 7111 char *name;
c906108c 7112
e7c27a73 7113 element_type = die_type (die, cu);
c906108c 7114
7e314c57
JK
7115 /* The die_type call above may have already set the type for this DIE. */
7116 type = get_die_type (die, cu);
7117 if (type)
7118 return type;
7119
c906108c
SS
7120 /* Irix 6.2 native cc creates array types without children for
7121 arrays with unspecified length. */
639d11d3 7122 if (die->child == NULL)
c906108c 7123 {
46bf5051 7124 index_type = objfile_type (objfile)->builtin_int;
c906108c 7125 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7126 type = create_array_type (NULL, element_type, range_type);
7127 return set_die_type (die, type, cu);
c906108c
SS
7128 }
7129
7130 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7131 child_die = die->child;
c906108c
SS
7132 while (child_die && child_die->tag)
7133 {
7134 if (child_die->tag == DW_TAG_subrange_type)
7135 {
f792889a 7136 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7137
f792889a 7138 if (child_type != NULL)
a02abb62
JB
7139 {
7140 /* The range type was succesfully read. Save it for
7141 the array type creation. */
7142 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7143 {
7144 range_types = (struct type **)
7145 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7146 * sizeof (struct type *));
7147 if (ndim == 0)
7148 make_cleanup (free_current_contents, &range_types);
7149 }
f792889a 7150 range_types[ndim++] = child_type;
a02abb62 7151 }
c906108c
SS
7152 }
7153 child_die = sibling_die (child_die);
7154 }
7155
7156 /* Dwarf2 dimensions are output from left to right, create the
7157 necessary array types in backwards order. */
7ca2d3a3 7158
c906108c 7159 type = element_type;
7ca2d3a3
DL
7160
7161 if (read_array_order (die, cu) == DW_ORD_col_major)
7162 {
7163 int i = 0;
9a619af0 7164
7ca2d3a3
DL
7165 while (i < ndim)
7166 type = create_array_type (NULL, type, range_types[i++]);
7167 }
7168 else
7169 {
7170 while (ndim-- > 0)
7171 type = create_array_type (NULL, type, range_types[ndim]);
7172 }
c906108c 7173
f5f8a009
EZ
7174 /* Understand Dwarf2 support for vector types (like they occur on
7175 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7176 array type. This is not part of the Dwarf2/3 standard yet, but a
7177 custom vendor extension. The main difference between a regular
7178 array and the vector variant is that vectors are passed by value
7179 to functions. */
e142c38c 7180 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7181 if (attr)
ea37ba09 7182 make_vector_type (type);
f5f8a009 7183
39cbfefa
DJ
7184 name = dwarf2_name (die, cu);
7185 if (name)
7186 TYPE_NAME (type) = name;
6e70227d 7187
7e314c57
JK
7188 /* Install the type in the die. */
7189 set_die_type (die, type, cu);
7190
7191 /* set_die_type should be already done. */
b4ba55a1
JB
7192 set_descriptive_type (type, die, cu);
7193
c906108c
SS
7194 do_cleanups (back_to);
7195
7e314c57 7196 return type;
c906108c
SS
7197}
7198
7ca2d3a3 7199static enum dwarf_array_dim_ordering
6e70227d 7200read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7201{
7202 struct attribute *attr;
7203
7204 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7205
7206 if (attr) return DW_SND (attr);
7207
7208 /*
7209 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7210 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7211 laid out as per normal fortran.
7212
6e70227d 7213 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7214 version checking.
7215 */
7216
905e0470
PM
7217 if (cu->language == language_fortran
7218 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7219 {
7220 return DW_ORD_row_major;
7221 }
7222
6e70227d 7223 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7224 {
7225 case array_column_major:
7226 return DW_ORD_col_major;
7227 case array_row_major:
7228 default:
7229 return DW_ORD_row_major;
7230 };
7231}
7232
72019c9c
GM
7233/* Extract all information from a DW_TAG_set_type DIE and put it in
7234 the DIE's type field. */
7235
f792889a 7236static struct type *
72019c9c
GM
7237read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7238{
7e314c57
JK
7239 struct type *domain_type, *set_type;
7240 struct attribute *attr;
f792889a 7241
7e314c57
JK
7242 domain_type = die_type (die, cu);
7243
7244 /* The die_type call above may have already set the type for this DIE. */
7245 set_type = get_die_type (die, cu);
7246 if (set_type)
7247 return set_type;
7248
7249 set_type = create_set_type (NULL, domain_type);
7250
7251 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7252 if (attr)
7253 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7254
f792889a 7255 return set_die_type (die, set_type, cu);
72019c9c 7256}
7ca2d3a3 7257
c906108c
SS
7258/* First cut: install each common block member as a global variable. */
7259
7260static void
e7c27a73 7261read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7262{
7263 struct die_info *child_die;
7264 struct attribute *attr;
7265 struct symbol *sym;
7266 CORE_ADDR base = (CORE_ADDR) 0;
7267
e142c38c 7268 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7269 if (attr)
7270 {
8e19ed76
PS
7271 /* Support the .debug_loc offsets */
7272 if (attr_form_is_block (attr))
7273 {
e7c27a73 7274 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7275 }
3690dd37 7276 else if (attr_form_is_section_offset (attr))
8e19ed76 7277 {
4d3c2250 7278 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7279 }
7280 else
7281 {
4d3c2250
KB
7282 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7283 "common block member");
8e19ed76 7284 }
c906108c 7285 }
639d11d3 7286 if (die->child != NULL)
c906108c 7287 {
639d11d3 7288 child_die = die->child;
c906108c
SS
7289 while (child_die && child_die->tag)
7290 {
e7c27a73 7291 sym = new_symbol (child_die, NULL, cu);
e142c38c 7292 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
7293 if (attr)
7294 {
d4b96c9a
JK
7295 CORE_ADDR byte_offset = 0;
7296
7297 if (attr_form_is_section_offset (attr))
7298 dwarf2_complex_location_expr_complaint ();
7299 else if (attr_form_is_constant (attr))
7300 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7301 else if (attr_form_is_block (attr))
7302 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7303 else
7304 dwarf2_complex_location_expr_complaint ();
7305
7306 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7307 add_symbol_to_list (sym, &global_symbols);
7308 }
7309 child_die = sibling_die (child_die);
7310 }
7311 }
7312}
7313
0114d602 7314/* Create a type for a C++ namespace. */
d9fa45fe 7315
0114d602
DJ
7316static struct type *
7317read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7318{
e7c27a73 7319 struct objfile *objfile = cu->objfile;
0114d602 7320 const char *previous_prefix, *name;
9219021c 7321 int is_anonymous;
0114d602
DJ
7322 struct type *type;
7323
7324 /* For extensions, reuse the type of the original namespace. */
7325 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7326 {
7327 struct die_info *ext_die;
7328 struct dwarf2_cu *ext_cu = cu;
9a619af0 7329
0114d602
DJ
7330 ext_die = dwarf2_extension (die, &ext_cu);
7331 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7332
7333 /* EXT_CU may not be the same as CU.
7334 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7335 return set_die_type (die, type, cu);
7336 }
9219021c 7337
e142c38c 7338 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7339
7340 /* Now build the name of the current namespace. */
7341
0114d602
DJ
7342 previous_prefix = determine_prefix (die, cu);
7343 if (previous_prefix[0] != '\0')
7344 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7345 previous_prefix, name, 0, cu);
0114d602
DJ
7346
7347 /* Create the type. */
7348 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7349 objfile);
7350 TYPE_NAME (type) = (char *) name;
7351 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7352
60531b24 7353 return set_die_type (die, type, cu);
0114d602
DJ
7354}
7355
7356/* Read a C++ namespace. */
7357
7358static void
7359read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7360{
7361 struct objfile *objfile = cu->objfile;
7362 const char *name;
7363 int is_anonymous;
9219021c 7364
5c4e30ca
DC
7365 /* Add a symbol associated to this if we haven't seen the namespace
7366 before. Also, add a using directive if it's an anonymous
7367 namespace. */
9219021c 7368
f2f0e013 7369 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7370 {
7371 struct type *type;
7372
0114d602 7373 type = read_type_die (die, cu);
e7c27a73 7374 new_symbol (die, type, cu);
5c4e30ca 7375
0114d602 7376 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7377 if (is_anonymous)
0114d602
DJ
7378 {
7379 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7380
c0cc3a76 7381 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7382 NULL, &objfile->objfile_obstack);
0114d602 7383 }
5c4e30ca 7384 }
9219021c 7385
639d11d3 7386 if (die->child != NULL)
d9fa45fe 7387 {
639d11d3 7388 struct die_info *child_die = die->child;
6e70227d 7389
d9fa45fe
DC
7390 while (child_die && child_die->tag)
7391 {
e7c27a73 7392 process_die (child_die, cu);
d9fa45fe
DC
7393 child_die = sibling_die (child_die);
7394 }
7395 }
38d518c9
EZ
7396}
7397
f55ee35c
JK
7398/* Read a Fortran module as type. This DIE can be only a declaration used for
7399 imported module. Still we need that type as local Fortran "use ... only"
7400 declaration imports depend on the created type in determine_prefix. */
7401
7402static struct type *
7403read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7404{
7405 struct objfile *objfile = cu->objfile;
7406 char *module_name;
7407 struct type *type;
7408
7409 module_name = dwarf2_name (die, cu);
7410 if (!module_name)
7411 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7412 die->offset);
7413 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7414
7415 /* determine_prefix uses TYPE_TAG_NAME. */
7416 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7417
7418 return set_die_type (die, type, cu);
7419}
7420
5d7cb8df
JK
7421/* Read a Fortran module. */
7422
7423static void
7424read_module (struct die_info *die, struct dwarf2_cu *cu)
7425{
7426 struct die_info *child_die = die->child;
7427
5d7cb8df
JK
7428 while (child_die && child_die->tag)
7429 {
7430 process_die (child_die, cu);
7431 child_die = sibling_die (child_die);
7432 }
7433}
7434
38d518c9
EZ
7435/* Return the name of the namespace represented by DIE. Set
7436 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7437 namespace. */
7438
7439static const char *
e142c38c 7440namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7441{
7442 struct die_info *current_die;
7443 const char *name = NULL;
7444
7445 /* Loop through the extensions until we find a name. */
7446
7447 for (current_die = die;
7448 current_die != NULL;
f2f0e013 7449 current_die = dwarf2_extension (die, &cu))
38d518c9 7450 {
e142c38c 7451 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7452 if (name != NULL)
7453 break;
7454 }
7455
7456 /* Is it an anonymous namespace? */
7457
7458 *is_anonymous = (name == NULL);
7459 if (*is_anonymous)
7460 name = "(anonymous namespace)";
7461
7462 return name;
d9fa45fe
DC
7463}
7464
c906108c
SS
7465/* Extract all information from a DW_TAG_pointer_type DIE and add to
7466 the user defined type vector. */
7467
f792889a 7468static struct type *
e7c27a73 7469read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7470{
5e2b427d 7471 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7472 struct comp_unit_head *cu_header = &cu->header;
c906108c 7473 struct type *type;
8b2dbe47
KB
7474 struct attribute *attr_byte_size;
7475 struct attribute *attr_address_class;
7476 int byte_size, addr_class;
7e314c57
JK
7477 struct type *target_type;
7478
7479 target_type = die_type (die, cu);
c906108c 7480
7e314c57
JK
7481 /* The die_type call above may have already set the type for this DIE. */
7482 type = get_die_type (die, cu);
7483 if (type)
7484 return type;
7485
7486 type = lookup_pointer_type (target_type);
8b2dbe47 7487
e142c38c 7488 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7489 if (attr_byte_size)
7490 byte_size = DW_UNSND (attr_byte_size);
c906108c 7491 else
8b2dbe47
KB
7492 byte_size = cu_header->addr_size;
7493
e142c38c 7494 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7495 if (attr_address_class)
7496 addr_class = DW_UNSND (attr_address_class);
7497 else
7498 addr_class = DW_ADDR_none;
7499
7500 /* If the pointer size or address class is different than the
7501 default, create a type variant marked as such and set the
7502 length accordingly. */
7503 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7504 {
5e2b427d 7505 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7506 {
7507 int type_flags;
7508
849957d9 7509 type_flags = gdbarch_address_class_type_flags
5e2b427d 7510 (gdbarch, byte_size, addr_class);
876cecd0
TT
7511 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7512 == 0);
8b2dbe47
KB
7513 type = make_type_with_address_space (type, type_flags);
7514 }
7515 else if (TYPE_LENGTH (type) != byte_size)
7516 {
e2e0b3e5 7517 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7518 }
6e70227d 7519 else
9a619af0
MS
7520 {
7521 /* Should we also complain about unhandled address classes? */
7522 }
c906108c 7523 }
8b2dbe47
KB
7524
7525 TYPE_LENGTH (type) = byte_size;
f792889a 7526 return set_die_type (die, type, cu);
c906108c
SS
7527}
7528
7529/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7530 the user defined type vector. */
7531
f792889a 7532static struct type *
e7c27a73 7533read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7534{
7535 struct type *type;
7536 struct type *to_type;
7537 struct type *domain;
7538
e7c27a73
DJ
7539 to_type = die_type (die, cu);
7540 domain = die_containing_type (die, cu);
0d5de010 7541
7e314c57
JK
7542 /* The calls above may have already set the type for this DIE. */
7543 type = get_die_type (die, cu);
7544 if (type)
7545 return type;
7546
0d5de010
DJ
7547 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7548 type = lookup_methodptr_type (to_type);
7549 else
7550 type = lookup_memberptr_type (to_type, domain);
c906108c 7551
f792889a 7552 return set_die_type (die, type, cu);
c906108c
SS
7553}
7554
7555/* Extract all information from a DW_TAG_reference_type DIE and add to
7556 the user defined type vector. */
7557
f792889a 7558static struct type *
e7c27a73 7559read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7560{
e7c27a73 7561 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7562 struct type *type, *target_type;
c906108c
SS
7563 struct attribute *attr;
7564
7e314c57
JK
7565 target_type = die_type (die, cu);
7566
7567 /* The die_type call above may have already set the type for this DIE. */
7568 type = get_die_type (die, cu);
7569 if (type)
7570 return type;
7571
7572 type = lookup_reference_type (target_type);
e142c38c 7573 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7574 if (attr)
7575 {
7576 TYPE_LENGTH (type) = DW_UNSND (attr);
7577 }
7578 else
7579 {
107d2387 7580 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7581 }
f792889a 7582 return set_die_type (die, type, cu);
c906108c
SS
7583}
7584
f792889a 7585static struct type *
e7c27a73 7586read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7587{
f792889a 7588 struct type *base_type, *cv_type;
c906108c 7589
e7c27a73 7590 base_type = die_type (die, cu);
7e314c57
JK
7591
7592 /* The die_type call above may have already set the type for this DIE. */
7593 cv_type = get_die_type (die, cu);
7594 if (cv_type)
7595 return cv_type;
7596
f792889a
DJ
7597 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7598 return set_die_type (die, cv_type, cu);
c906108c
SS
7599}
7600
f792889a 7601static struct type *
e7c27a73 7602read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7603{
f792889a 7604 struct type *base_type, *cv_type;
c906108c 7605
e7c27a73 7606 base_type = die_type (die, cu);
7e314c57
JK
7607
7608 /* The die_type call above may have already set the type for this DIE. */
7609 cv_type = get_die_type (die, cu);
7610 if (cv_type)
7611 return cv_type;
7612
f792889a
DJ
7613 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7614 return set_die_type (die, cv_type, cu);
c906108c
SS
7615}
7616
7617/* Extract all information from a DW_TAG_string_type DIE and add to
7618 the user defined type vector. It isn't really a user defined type,
7619 but it behaves like one, with other DIE's using an AT_user_def_type
7620 attribute to reference it. */
7621
f792889a 7622static struct type *
e7c27a73 7623read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7624{
e7c27a73 7625 struct objfile *objfile = cu->objfile;
3b7538c0 7626 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7627 struct type *type, *range_type, *index_type, *char_type;
7628 struct attribute *attr;
7629 unsigned int length;
7630
e142c38c 7631 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7632 if (attr)
7633 {
7634 length = DW_UNSND (attr);
7635 }
7636 else
7637 {
b21b22e0 7638 /* check for the DW_AT_byte_size attribute */
e142c38c 7639 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7640 if (attr)
7641 {
7642 length = DW_UNSND (attr);
7643 }
7644 else
7645 {
7646 length = 1;
7647 }
c906108c 7648 }
6ccb9162 7649
46bf5051 7650 index_type = objfile_type (objfile)->builtin_int;
c906108c 7651 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7652 char_type = language_string_char_type (cu->language_defn, gdbarch);
7653 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7654
f792889a 7655 return set_die_type (die, type, cu);
c906108c
SS
7656}
7657
7658/* Handle DIES due to C code like:
7659
7660 struct foo
c5aa993b
JM
7661 {
7662 int (*funcp)(int a, long l);
7663 int b;
7664 };
c906108c
SS
7665
7666 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7667 */
c906108c 7668
f792889a 7669static struct type *
e7c27a73 7670read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7671{
7672 struct type *type; /* Type that this function returns */
7673 struct type *ftype; /* Function that returns above type */
7674 struct attribute *attr;
7675
e7c27a73 7676 type = die_type (die, cu);
7e314c57
JK
7677
7678 /* The die_type call above may have already set the type for this DIE. */
7679 ftype = get_die_type (die, cu);
7680 if (ftype)
7681 return ftype;
7682
0c8b41f1 7683 ftype = lookup_function_type (type);
c906108c 7684
5b8101ae 7685 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7686 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7687 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7688 || cu->language == language_cplus
5b8101ae
PM
7689 || cu->language == language_java
7690 || cu->language == language_pascal)
876cecd0 7691 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7692 else if (producer_is_realview (cu->producer))
7693 /* RealView does not emit DW_AT_prototyped. We can not
7694 distinguish prototyped and unprototyped functions; default to
7695 prototyped, since that is more common in modern code (and
7696 RealView warns about unprototyped functions). */
7697 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7698
c055b101
CV
7699 /* Store the calling convention in the type if it's available in
7700 the subroutine die. Otherwise set the calling convention to
7701 the default value DW_CC_normal. */
7702 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7703 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7704
7705 /* We need to add the subroutine type to the die immediately so
7706 we don't infinitely recurse when dealing with parameters
7707 declared as the same subroutine type. */
7708 set_die_type (die, ftype, cu);
6e70227d 7709
639d11d3 7710 if (die->child != NULL)
c906108c 7711 {
8072405b 7712 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7713 struct die_info *child_die;
8072405b 7714 int nparams, iparams;
c906108c
SS
7715
7716 /* Count the number of parameters.
7717 FIXME: GDB currently ignores vararg functions, but knows about
7718 vararg member functions. */
8072405b 7719 nparams = 0;
639d11d3 7720 child_die = die->child;
c906108c
SS
7721 while (child_die && child_die->tag)
7722 {
7723 if (child_die->tag == DW_TAG_formal_parameter)
7724 nparams++;
7725 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7726 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7727 child_die = sibling_die (child_die);
7728 }
7729
7730 /* Allocate storage for parameters and fill them in. */
7731 TYPE_NFIELDS (ftype) = nparams;
7732 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7733 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7734
8072405b
JK
7735 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7736 even if we error out during the parameters reading below. */
7737 for (iparams = 0; iparams < nparams; iparams++)
7738 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7739
7740 iparams = 0;
639d11d3 7741 child_die = die->child;
c906108c
SS
7742 while (child_die && child_die->tag)
7743 {
7744 if (child_die->tag == DW_TAG_formal_parameter)
7745 {
3ce3b1ba
PA
7746 struct type *arg_type;
7747
7748 /* DWARF version 2 has no clean way to discern C++
7749 static and non-static member functions. G++ helps
7750 GDB by marking the first parameter for non-static
7751 member functions (which is the this pointer) as
7752 artificial. We pass this information to
7753 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7754
7755 DWARF version 3 added DW_AT_object_pointer, which GCC
7756 4.5 does not yet generate. */
e142c38c 7757 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7758 if (attr)
7759 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7760 else
418835cc
KS
7761 {
7762 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7763
7764 /* GCC/43521: In java, the formal parameter
7765 "this" is sometimes not marked with DW_AT_artificial. */
7766 if (cu->language == language_java)
7767 {
7768 const char *name = dwarf2_name (child_die, cu);
9a619af0 7769
418835cc
KS
7770 if (name && !strcmp (name, "this"))
7771 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7772 }
7773 }
3ce3b1ba
PA
7774 arg_type = die_type (child_die, cu);
7775
7776 /* RealView does not mark THIS as const, which the testsuite
7777 expects. GCC marks THIS as const in method definitions,
7778 but not in the class specifications (GCC PR 43053). */
7779 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7780 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7781 {
7782 int is_this = 0;
7783 struct dwarf2_cu *arg_cu = cu;
7784 const char *name = dwarf2_name (child_die, cu);
7785
7786 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7787 if (attr)
7788 {
7789 /* If the compiler emits this, use it. */
7790 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7791 is_this = 1;
7792 }
7793 else if (name && strcmp (name, "this") == 0)
7794 /* Function definitions will have the argument names. */
7795 is_this = 1;
7796 else if (name == NULL && iparams == 0)
7797 /* Declarations may not have the names, so like
7798 elsewhere in GDB, assume an artificial first
7799 argument is "this". */
7800 is_this = 1;
7801
7802 if (is_this)
7803 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7804 arg_type, 0);
7805 }
7806
7807 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7808 iparams++;
7809 }
7810 child_die = sibling_die (child_die);
7811 }
7812 }
7813
76c10ea2 7814 return ftype;
c906108c
SS
7815}
7816
f792889a 7817static struct type *
e7c27a73 7818read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7819{
e7c27a73 7820 struct objfile *objfile = cu->objfile;
0114d602 7821 const char *name = NULL;
f792889a 7822 struct type *this_type;
c906108c 7823
94af9270 7824 name = dwarf2_full_name (NULL, die, cu);
f792889a 7825 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7826 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7827 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7828 set_die_type (die, this_type, cu);
7829 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7830 return this_type;
c906108c
SS
7831}
7832
7833/* Find a representation of a given base type and install
7834 it in the TYPE field of the die. */
7835
f792889a 7836static struct type *
e7c27a73 7837read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7838{
e7c27a73 7839 struct objfile *objfile = cu->objfile;
c906108c
SS
7840 struct type *type;
7841 struct attribute *attr;
7842 int encoding = 0, size = 0;
39cbfefa 7843 char *name;
6ccb9162
UW
7844 enum type_code code = TYPE_CODE_INT;
7845 int type_flags = 0;
7846 struct type *target_type = NULL;
c906108c 7847
e142c38c 7848 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7849 if (attr)
7850 {
7851 encoding = DW_UNSND (attr);
7852 }
e142c38c 7853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7854 if (attr)
7855 {
7856 size = DW_UNSND (attr);
7857 }
39cbfefa 7858 name = dwarf2_name (die, cu);
6ccb9162 7859 if (!name)
c906108c 7860 {
6ccb9162
UW
7861 complaint (&symfile_complaints,
7862 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7863 }
6ccb9162
UW
7864
7865 switch (encoding)
c906108c 7866 {
6ccb9162
UW
7867 case DW_ATE_address:
7868 /* Turn DW_ATE_address into a void * pointer. */
7869 code = TYPE_CODE_PTR;
7870 type_flags |= TYPE_FLAG_UNSIGNED;
7871 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7872 break;
7873 case DW_ATE_boolean:
7874 code = TYPE_CODE_BOOL;
7875 type_flags |= TYPE_FLAG_UNSIGNED;
7876 break;
7877 case DW_ATE_complex_float:
7878 code = TYPE_CODE_COMPLEX;
7879 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7880 break;
7881 case DW_ATE_decimal_float:
7882 code = TYPE_CODE_DECFLOAT;
7883 break;
7884 case DW_ATE_float:
7885 code = TYPE_CODE_FLT;
7886 break;
7887 case DW_ATE_signed:
7888 break;
7889 case DW_ATE_unsigned:
7890 type_flags |= TYPE_FLAG_UNSIGNED;
7891 break;
7892 case DW_ATE_signed_char:
6e70227d 7893 if (cu->language == language_ada || cu->language == language_m2
868a0084 7894 || cu->language == language_pascal)
6ccb9162
UW
7895 code = TYPE_CODE_CHAR;
7896 break;
7897 case DW_ATE_unsigned_char:
868a0084
PM
7898 if (cu->language == language_ada || cu->language == language_m2
7899 || cu->language == language_pascal)
6ccb9162
UW
7900 code = TYPE_CODE_CHAR;
7901 type_flags |= TYPE_FLAG_UNSIGNED;
7902 break;
75079b2b
TT
7903 case DW_ATE_UTF:
7904 /* We just treat this as an integer and then recognize the
7905 type by name elsewhere. */
7906 break;
7907
6ccb9162
UW
7908 default:
7909 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7910 dwarf_type_encoding_name (encoding));
7911 break;
c906108c 7912 }
6ccb9162 7913
0114d602
DJ
7914 type = init_type (code, size, type_flags, NULL, objfile);
7915 TYPE_NAME (type) = name;
6ccb9162
UW
7916 TYPE_TARGET_TYPE (type) = target_type;
7917
0114d602 7918 if (name && strcmp (name, "char") == 0)
876cecd0 7919 TYPE_NOSIGN (type) = 1;
0114d602 7920
f792889a 7921 return set_die_type (die, type, cu);
c906108c
SS
7922}
7923
a02abb62
JB
7924/* Read the given DW_AT_subrange DIE. */
7925
f792889a 7926static struct type *
a02abb62
JB
7927read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7928{
5e2b427d 7929 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7930 struct type *base_type;
7931 struct type *range_type;
7932 struct attribute *attr;
43bbcdc2
PH
7933 LONGEST low = 0;
7934 LONGEST high = -1;
39cbfefa 7935 char *name;
43bbcdc2 7936 LONGEST negative_mask;
e77813c8 7937
a02abb62 7938 base_type = die_type (die, cu);
953ac07e
JK
7939 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7940 check_typedef (base_type);
a02abb62 7941
7e314c57
JK
7942 /* The die_type call above may have already set the type for this DIE. */
7943 range_type = get_die_type (die, cu);
7944 if (range_type)
7945 return range_type;
7946
e142c38c 7947 if (cu->language == language_fortran)
6e70227d 7948 {
a02abb62
JB
7949 /* FORTRAN implies a lower bound of 1, if not given. */
7950 low = 1;
7951 }
7952
dd5e6932
DJ
7953 /* FIXME: For variable sized arrays either of these could be
7954 a variable rather than a constant value. We'll allow it,
7955 but we don't know how to handle it. */
e142c38c 7956 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7957 if (attr)
7958 low = dwarf2_get_attr_constant_value (attr, 0);
7959
e142c38c 7960 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7961 if (attr)
6e70227d 7962 {
e77813c8 7963 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7964 {
7965 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7966 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7967 FIXME: GDB does not yet know how to handle dynamic
7968 arrays properly, treat them as arrays with unspecified
7969 length for now.
7970
7971 FIXME: jimb/2003-09-22: GDB does not really know
7972 how to handle arrays of unspecified length
7973 either; we just represent them as zero-length
7974 arrays. Choose an appropriate upper bound given
7975 the lower bound we've computed above. */
7976 high = low - 1;
7977 }
7978 else
7979 high = dwarf2_get_attr_constant_value (attr, 1);
7980 }
e77813c8
PM
7981 else
7982 {
7983 attr = dwarf2_attr (die, DW_AT_count, cu);
7984 if (attr)
7985 {
7986 int count = dwarf2_get_attr_constant_value (attr, 1);
7987 high = low + count - 1;
7988 }
7989 }
7990
7991 /* Dwarf-2 specifications explicitly allows to create subrange types
7992 without specifying a base type.
7993 In that case, the base type must be set to the type of
7994 the lower bound, upper bound or count, in that order, if any of these
7995 three attributes references an object that has a type.
7996 If no base type is found, the Dwarf-2 specifications say that
7997 a signed integer type of size equal to the size of an address should
7998 be used.
7999 For the following C code: `extern char gdb_int [];'
8000 GCC produces an empty range DIE.
8001 FIXME: muller/2010-05-28: Possible references to object for low bound,
8002 high bound or count are not yet handled by this code.
8003 */
8004 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8005 {
8006 struct objfile *objfile = cu->objfile;
8007 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8008 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8009 struct type *int_type = objfile_type (objfile)->builtin_int;
8010
8011 /* Test "int", "long int", and "long long int" objfile types,
8012 and select the first one having a size above or equal to the
8013 architecture address size. */
8014 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8015 base_type = int_type;
8016 else
8017 {
8018 int_type = objfile_type (objfile)->builtin_long;
8019 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8020 base_type = int_type;
8021 else
8022 {
8023 int_type = objfile_type (objfile)->builtin_long_long;
8024 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8025 base_type = int_type;
8026 }
8027 }
8028 }
a02abb62 8029
6e70227d 8030 negative_mask =
43bbcdc2
PH
8031 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8032 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8033 low |= negative_mask;
8034 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8035 high |= negative_mask;
8036
a02abb62
JB
8037 range_type = create_range_type (NULL, base_type, low, high);
8038
bbb0eef6
JK
8039 /* Mark arrays with dynamic length at least as an array of unspecified
8040 length. GDB could check the boundary but before it gets implemented at
8041 least allow accessing the array elements. */
8042 if (attr && attr->form == DW_FORM_block1)
8043 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8044
39cbfefa
DJ
8045 name = dwarf2_name (die, cu);
8046 if (name)
8047 TYPE_NAME (range_type) = name;
6e70227d 8048
e142c38c 8049 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8050 if (attr)
8051 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8052
7e314c57
JK
8053 set_die_type (die, range_type, cu);
8054
8055 /* set_die_type should be already done. */
b4ba55a1
JB
8056 set_descriptive_type (range_type, die, cu);
8057
7e314c57 8058 return range_type;
a02abb62 8059}
6e70227d 8060
f792889a 8061static struct type *
81a17f79
JB
8062read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8063{
8064 struct type *type;
81a17f79 8065
81a17f79
JB
8066 /* For now, we only support the C meaning of an unspecified type: void. */
8067
0114d602
DJ
8068 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8069 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8070
f792889a 8071 return set_die_type (die, type, cu);
81a17f79 8072}
a02abb62 8073
51545339
DJ
8074/* Trivial hash function for die_info: the hash value of a DIE
8075 is its offset in .debug_info for this objfile. */
8076
8077static hashval_t
8078die_hash (const void *item)
8079{
8080 const struct die_info *die = item;
9a619af0 8081
51545339
DJ
8082 return die->offset;
8083}
8084
8085/* Trivial comparison function for die_info structures: two DIEs
8086 are equal if they have the same offset. */
8087
8088static int
8089die_eq (const void *item_lhs, const void *item_rhs)
8090{
8091 const struct die_info *die_lhs = item_lhs;
8092 const struct die_info *die_rhs = item_rhs;
9a619af0 8093
51545339
DJ
8094 return die_lhs->offset == die_rhs->offset;
8095}
8096
c906108c
SS
8097/* Read a whole compilation unit into a linked list of dies. */
8098
f9aca02d 8099static struct die_info *
93311388 8100read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8101{
93311388 8102 struct die_reader_specs reader_specs;
98bfdba5 8103 int read_abbrevs = 0;
1d9ec526 8104 struct cleanup *back_to = NULL;
98bfdba5
PA
8105 struct die_info *die;
8106
8107 if (cu->dwarf2_abbrevs == NULL)
8108 {
8109 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8110 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8111 read_abbrevs = 1;
8112 }
93311388 8113
348e048f 8114 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8115 cu->die_hash
8116 = htab_create_alloc_ex (cu->header.length / 12,
8117 die_hash,
8118 die_eq,
8119 NULL,
8120 &cu->comp_unit_obstack,
8121 hashtab_obstack_allocate,
8122 dummy_obstack_deallocate);
8123
93311388
DE
8124 init_cu_die_reader (&reader_specs, cu);
8125
98bfdba5
PA
8126 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8127
8128 if (read_abbrevs)
8129 do_cleanups (back_to);
8130
8131 return die;
639d11d3
DC
8132}
8133
d97bc12b
DE
8134/* Main entry point for reading a DIE and all children.
8135 Read the DIE and dump it if requested. */
8136
8137static struct die_info *
93311388
DE
8138read_die_and_children (const struct die_reader_specs *reader,
8139 gdb_byte *info_ptr,
d97bc12b
DE
8140 gdb_byte **new_info_ptr,
8141 struct die_info *parent)
8142{
93311388 8143 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8144 new_info_ptr, parent);
8145
8146 if (dwarf2_die_debug)
8147 {
348e048f
DE
8148 fprintf_unfiltered (gdb_stdlog,
8149 "\nRead die from %s of %s:\n",
8150 reader->buffer == dwarf2_per_objfile->info.buffer
8151 ? ".debug_info"
8152 : reader->buffer == dwarf2_per_objfile->types.buffer
8153 ? ".debug_types"
8154 : "unknown section",
8155 reader->abfd->filename);
d97bc12b
DE
8156 dump_die (result, dwarf2_die_debug);
8157 }
8158
8159 return result;
8160}
8161
639d11d3
DC
8162/* Read a single die and all its descendents. Set the die's sibling
8163 field to NULL; set other fields in the die correctly, and set all
8164 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8165 location of the info_ptr after reading all of those dies. PARENT
8166 is the parent of the die in question. */
8167
8168static struct die_info *
93311388
DE
8169read_die_and_children_1 (const struct die_reader_specs *reader,
8170 gdb_byte *info_ptr,
d97bc12b
DE
8171 gdb_byte **new_info_ptr,
8172 struct die_info *parent)
639d11d3
DC
8173{
8174 struct die_info *die;
fe1b8b76 8175 gdb_byte *cur_ptr;
639d11d3
DC
8176 int has_children;
8177
93311388 8178 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8179 if (die == NULL)
8180 {
8181 *new_info_ptr = cur_ptr;
8182 return NULL;
8183 }
93311388 8184 store_in_ref_table (die, reader->cu);
639d11d3
DC
8185
8186 if (has_children)
348e048f 8187 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8188 else
8189 {
8190 die->child = NULL;
8191 *new_info_ptr = cur_ptr;
8192 }
8193
8194 die->sibling = NULL;
8195 die->parent = parent;
8196 return die;
8197}
8198
8199/* Read a die, all of its descendents, and all of its siblings; set
8200 all of the fields of all of the dies correctly. Arguments are as
8201 in read_die_and_children. */
8202
8203static struct die_info *
93311388
DE
8204read_die_and_siblings (const struct die_reader_specs *reader,
8205 gdb_byte *info_ptr,
fe1b8b76 8206 gdb_byte **new_info_ptr,
639d11d3
DC
8207 struct die_info *parent)
8208{
8209 struct die_info *first_die, *last_sibling;
fe1b8b76 8210 gdb_byte *cur_ptr;
639d11d3 8211
c906108c 8212 cur_ptr = info_ptr;
639d11d3
DC
8213 first_die = last_sibling = NULL;
8214
8215 while (1)
c906108c 8216 {
639d11d3 8217 struct die_info *die
93311388 8218 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8219
1d325ec1 8220 if (die == NULL)
c906108c 8221 {
639d11d3
DC
8222 *new_info_ptr = cur_ptr;
8223 return first_die;
c906108c 8224 }
1d325ec1
DJ
8225
8226 if (!first_die)
8227 first_die = die;
c906108c 8228 else
1d325ec1
DJ
8229 last_sibling->sibling = die;
8230
8231 last_sibling = die;
c906108c 8232 }
c906108c
SS
8233}
8234
93311388
DE
8235/* Read the die from the .debug_info section buffer. Set DIEP to
8236 point to a newly allocated die with its information, except for its
8237 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8238 whether the die has children or not. */
8239
8240static gdb_byte *
8241read_full_die (const struct die_reader_specs *reader,
8242 struct die_info **diep, gdb_byte *info_ptr,
8243 int *has_children)
8244{
8245 unsigned int abbrev_number, bytes_read, i, offset;
8246 struct abbrev_info *abbrev;
8247 struct die_info *die;
8248 struct dwarf2_cu *cu = reader->cu;
8249 bfd *abfd = reader->abfd;
8250
8251 offset = info_ptr - reader->buffer;
8252 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8253 info_ptr += bytes_read;
8254 if (!abbrev_number)
8255 {
8256 *diep = NULL;
8257 *has_children = 0;
8258 return info_ptr;
8259 }
8260
8261 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8262 if (!abbrev)
348e048f
DE
8263 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8264 abbrev_number,
8265 bfd_get_filename (abfd));
8266
93311388
DE
8267 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8268 die->offset = offset;
8269 die->tag = abbrev->tag;
8270 die->abbrev = abbrev_number;
8271
8272 die->num_attrs = abbrev->num_attrs;
8273
8274 for (i = 0; i < abbrev->num_attrs; ++i)
8275 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8276 abfd, info_ptr, cu);
8277
8278 *diep = die;
8279 *has_children = abbrev->has_children;
8280 return info_ptr;
8281}
8282
c906108c
SS
8283/* In DWARF version 2, the description of the debugging information is
8284 stored in a separate .debug_abbrev section. Before we read any
8285 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8286 in a hash table. This function also sets flags in CU describing
8287 the data found in the abbrev table. */
c906108c
SS
8288
8289static void
e7c27a73 8290dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8291{
e7c27a73 8292 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8293 gdb_byte *abbrev_ptr;
c906108c
SS
8294 struct abbrev_info *cur_abbrev;
8295 unsigned int abbrev_number, bytes_read, abbrev_name;
8296 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8297 struct attr_abbrev *cur_attrs;
8298 unsigned int allocated_attrs;
c906108c 8299
57349743 8300 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8301 obstack_init (&cu->abbrev_obstack);
8302 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8303 (ABBREV_HASH_SIZE
8304 * sizeof (struct abbrev_info *)));
8305 memset (cu->dwarf2_abbrevs, 0,
8306 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8307
be391dca
TT
8308 dwarf2_read_section (dwarf2_per_objfile->objfile,
8309 &dwarf2_per_objfile->abbrev);
dce234bc 8310 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8311 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8312 abbrev_ptr += bytes_read;
8313
f3dd6933
DJ
8314 allocated_attrs = ATTR_ALLOC_CHUNK;
8315 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8316
c906108c
SS
8317 /* loop until we reach an abbrev number of 0 */
8318 while (abbrev_number)
8319 {
f3dd6933 8320 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8321
8322 /* read in abbrev header */
8323 cur_abbrev->number = abbrev_number;
8324 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8325 abbrev_ptr += bytes_read;
8326 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8327 abbrev_ptr += 1;
8328
72bf9492
DJ
8329 if (cur_abbrev->tag == DW_TAG_namespace)
8330 cu->has_namespace_info = 1;
8331
c906108c
SS
8332 /* now read in declarations */
8333 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8334 abbrev_ptr += bytes_read;
8335 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8336 abbrev_ptr += bytes_read;
8337 while (abbrev_name)
8338 {
f3dd6933 8339 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8340 {
f3dd6933
DJ
8341 allocated_attrs += ATTR_ALLOC_CHUNK;
8342 cur_attrs
8343 = xrealloc (cur_attrs, (allocated_attrs
8344 * sizeof (struct attr_abbrev)));
c906108c 8345 }
ae038cb0
DJ
8346
8347 /* Record whether this compilation unit might have
8348 inter-compilation-unit references. If we don't know what form
8349 this attribute will have, then it might potentially be a
8350 DW_FORM_ref_addr, so we conservatively expect inter-CU
8351 references. */
8352
8353 if (abbrev_form == DW_FORM_ref_addr
8354 || abbrev_form == DW_FORM_indirect)
8355 cu->has_form_ref_addr = 1;
8356
f3dd6933
DJ
8357 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8358 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8359 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8360 abbrev_ptr += bytes_read;
8361 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8362 abbrev_ptr += bytes_read;
8363 }
8364
f3dd6933
DJ
8365 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8366 (cur_abbrev->num_attrs
8367 * sizeof (struct attr_abbrev)));
8368 memcpy (cur_abbrev->attrs, cur_attrs,
8369 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8370
c906108c 8371 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8372 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8373 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8374
8375 /* Get next abbreviation.
8376 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8377 always properly terminated with an abbrev number of 0.
8378 Exit loop if we encounter an abbreviation which we have
8379 already read (which means we are about to read the abbreviations
8380 for the next compile unit) or if the end of the abbreviation
8381 table is reached. */
dce234bc
PP
8382 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8383 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8384 break;
8385 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8386 abbrev_ptr += bytes_read;
e7c27a73 8387 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8388 break;
8389 }
f3dd6933
DJ
8390
8391 xfree (cur_attrs);
c906108c
SS
8392}
8393
f3dd6933 8394/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8395
c906108c 8396static void
f3dd6933 8397dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8398{
f3dd6933 8399 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8400
f3dd6933
DJ
8401 obstack_free (&cu->abbrev_obstack, NULL);
8402 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8403}
8404
8405/* Lookup an abbrev_info structure in the abbrev hash table. */
8406
8407static struct abbrev_info *
e7c27a73 8408dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8409{
8410 unsigned int hash_number;
8411 struct abbrev_info *abbrev;
8412
8413 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8414 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8415
8416 while (abbrev)
8417 {
8418 if (abbrev->number == number)
8419 return abbrev;
8420 else
8421 abbrev = abbrev->next;
8422 }
8423 return NULL;
8424}
8425
72bf9492
DJ
8426/* Returns nonzero if TAG represents a type that we might generate a partial
8427 symbol for. */
8428
8429static int
8430is_type_tag_for_partial (int tag)
8431{
8432 switch (tag)
8433 {
8434#if 0
8435 /* Some types that would be reasonable to generate partial symbols for,
8436 that we don't at present. */
8437 case DW_TAG_array_type:
8438 case DW_TAG_file_type:
8439 case DW_TAG_ptr_to_member_type:
8440 case DW_TAG_set_type:
8441 case DW_TAG_string_type:
8442 case DW_TAG_subroutine_type:
8443#endif
8444 case DW_TAG_base_type:
8445 case DW_TAG_class_type:
680b30c7 8446 case DW_TAG_interface_type:
72bf9492
DJ
8447 case DW_TAG_enumeration_type:
8448 case DW_TAG_structure_type:
8449 case DW_TAG_subrange_type:
8450 case DW_TAG_typedef:
8451 case DW_TAG_union_type:
8452 return 1;
8453 default:
8454 return 0;
8455 }
8456}
8457
8458/* Load all DIEs that are interesting for partial symbols into memory. */
8459
8460static struct partial_die_info *
93311388
DE
8461load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8462 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8463{
8464 struct partial_die_info *part_die;
8465 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8466 struct abbrev_info *abbrev;
8467 unsigned int bytes_read;
5afb4e99 8468 unsigned int load_all = 0;
72bf9492
DJ
8469
8470 int nesting_level = 1;
8471
8472 parent_die = NULL;
8473 last_die = NULL;
8474
5afb4e99
DJ
8475 if (cu->per_cu && cu->per_cu->load_all_dies)
8476 load_all = 1;
8477
72bf9492
DJ
8478 cu->partial_dies
8479 = htab_create_alloc_ex (cu->header.length / 12,
8480 partial_die_hash,
8481 partial_die_eq,
8482 NULL,
8483 &cu->comp_unit_obstack,
8484 hashtab_obstack_allocate,
8485 dummy_obstack_deallocate);
8486
8487 part_die = obstack_alloc (&cu->comp_unit_obstack,
8488 sizeof (struct partial_die_info));
8489
8490 while (1)
8491 {
8492 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8493
8494 /* A NULL abbrev means the end of a series of children. */
8495 if (abbrev == NULL)
8496 {
8497 if (--nesting_level == 0)
8498 {
8499 /* PART_DIE was probably the last thing allocated on the
8500 comp_unit_obstack, so we could call obstack_free
8501 here. We don't do that because the waste is small,
8502 and will be cleaned up when we're done with this
8503 compilation unit. This way, we're also more robust
8504 against other users of the comp_unit_obstack. */
8505 return first_die;
8506 }
8507 info_ptr += bytes_read;
8508 last_die = parent_die;
8509 parent_die = parent_die->die_parent;
8510 continue;
8511 }
8512
98bfdba5
PA
8513 /* Check for template arguments. We never save these; if
8514 they're seen, we just mark the parent, and go on our way. */
8515 if (parent_die != NULL
8516 && cu->language == language_cplus
8517 && (abbrev->tag == DW_TAG_template_type_param
8518 || abbrev->tag == DW_TAG_template_value_param))
8519 {
8520 parent_die->has_template_arguments = 1;
8521
8522 if (!load_all)
8523 {
8524 /* We don't need a partial DIE for the template argument. */
8525 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8526 cu);
8527 continue;
8528 }
8529 }
8530
8531 /* We only recurse into subprograms looking for template arguments.
8532 Skip their other children. */
8533 if (!load_all
8534 && cu->language == language_cplus
8535 && parent_die != NULL
8536 && parent_die->tag == DW_TAG_subprogram)
8537 {
8538 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8539 continue;
8540 }
8541
5afb4e99
DJ
8542 /* Check whether this DIE is interesting enough to save. Normally
8543 we would not be interested in members here, but there may be
8544 later variables referencing them via DW_AT_specification (for
8545 static members). */
8546 if (!load_all
8547 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8548 && abbrev->tag != DW_TAG_enumerator
8549 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8550 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8551 && abbrev->tag != DW_TAG_variable
5afb4e99 8552 && abbrev->tag != DW_TAG_namespace
f55ee35c 8553 && abbrev->tag != DW_TAG_module
5afb4e99 8554 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8555 {
8556 /* Otherwise we skip to the next sibling, if any. */
93311388 8557 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8558 continue;
8559 }
8560
93311388
DE
8561 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8562 buffer, info_ptr, cu);
72bf9492
DJ
8563
8564 /* This two-pass algorithm for processing partial symbols has a
8565 high cost in cache pressure. Thus, handle some simple cases
8566 here which cover the majority of C partial symbols. DIEs
8567 which neither have specification tags in them, nor could have
8568 specification tags elsewhere pointing at them, can simply be
8569 processed and discarded.
8570
8571 This segment is also optional; scan_partial_symbols and
8572 add_partial_symbol will handle these DIEs if we chain
8573 them in normally. When compilers which do not emit large
8574 quantities of duplicate debug information are more common,
8575 this code can probably be removed. */
8576
8577 /* Any complete simple types at the top level (pretty much all
8578 of them, for a language without namespaces), can be processed
8579 directly. */
8580 if (parent_die == NULL
8581 && part_die->has_specification == 0
8582 && part_die->is_declaration == 0
8583 && (part_die->tag == DW_TAG_typedef
8584 || part_die->tag == DW_TAG_base_type
8585 || part_die->tag == DW_TAG_subrange_type))
8586 {
8587 if (building_psymtab && part_die->name != NULL)
04a679b8 8588 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8589 VAR_DOMAIN, LOC_TYPEDEF,
8590 &cu->objfile->static_psymbols,
8591 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8592 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8593 continue;
8594 }
8595
8596 /* If we're at the second level, and we're an enumerator, and
8597 our parent has no specification (meaning possibly lives in a
8598 namespace elsewhere), then we can add the partial symbol now
8599 instead of queueing it. */
8600 if (part_die->tag == DW_TAG_enumerator
8601 && parent_die != NULL
8602 && parent_die->die_parent == NULL
8603 && parent_die->tag == DW_TAG_enumeration_type
8604 && parent_die->has_specification == 0)
8605 {
8606 if (part_die->name == NULL)
e2e0b3e5 8607 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8608 else if (building_psymtab)
04a679b8 8609 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8610 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8611 (cu->language == language_cplus
8612 || cu->language == language_java)
72bf9492
DJ
8613 ? &cu->objfile->global_psymbols
8614 : &cu->objfile->static_psymbols,
8615 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8616
93311388 8617 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8618 continue;
8619 }
8620
8621 /* We'll save this DIE so link it in. */
8622 part_die->die_parent = parent_die;
8623 part_die->die_sibling = NULL;
8624 part_die->die_child = NULL;
8625
8626 if (last_die && last_die == parent_die)
8627 last_die->die_child = part_die;
8628 else if (last_die)
8629 last_die->die_sibling = part_die;
8630
8631 last_die = part_die;
8632
8633 if (first_die == NULL)
8634 first_die = part_die;
8635
8636 /* Maybe add the DIE to the hash table. Not all DIEs that we
8637 find interesting need to be in the hash table, because we
8638 also have the parent/sibling/child chains; only those that we
8639 might refer to by offset later during partial symbol reading.
8640
8641 For now this means things that might have be the target of a
8642 DW_AT_specification, DW_AT_abstract_origin, or
8643 DW_AT_extension. DW_AT_extension will refer only to
8644 namespaces; DW_AT_abstract_origin refers to functions (and
8645 many things under the function DIE, but we do not recurse
8646 into function DIEs during partial symbol reading) and
8647 possibly variables as well; DW_AT_specification refers to
8648 declarations. Declarations ought to have the DW_AT_declaration
8649 flag. It happens that GCC forgets to put it in sometimes, but
8650 only for functions, not for types.
8651
8652 Adding more things than necessary to the hash table is harmless
8653 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8654 wasted time in find_partial_die, when we reread the compilation
8655 unit with load_all_dies set. */
72bf9492 8656
5afb4e99
DJ
8657 if (load_all
8658 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8659 || abbrev->tag == DW_TAG_variable
8660 || abbrev->tag == DW_TAG_namespace
8661 || part_die->is_declaration)
8662 {
8663 void **slot;
8664
8665 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8666 part_die->offset, INSERT);
8667 *slot = part_die;
8668 }
8669
8670 part_die = obstack_alloc (&cu->comp_unit_obstack,
8671 sizeof (struct partial_die_info));
8672
8673 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8674 we have no reason to follow the children of structures; for other
98bfdba5
PA
8675 languages we have to, so that we can get at method physnames
8676 to infer fully qualified class names, for DW_AT_specification,
8677 and for C++ template arguments. For C++, we also look one level
8678 inside functions to find template arguments (if the name of the
8679 function does not already contain the template arguments).
bc30ff58
JB
8680
8681 For Ada, we need to scan the children of subprograms and lexical
8682 blocks as well because Ada allows the definition of nested
8683 entities that could be interesting for the debugger, such as
8684 nested subprograms for instance. */
72bf9492 8685 if (last_die->has_children
5afb4e99
DJ
8686 && (load_all
8687 || last_die->tag == DW_TAG_namespace
f55ee35c 8688 || last_die->tag == DW_TAG_module
72bf9492 8689 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8690 || (cu->language == language_cplus
8691 && last_die->tag == DW_TAG_subprogram
8692 && (last_die->name == NULL
8693 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8694 || (cu->language != language_c
8695 && (last_die->tag == DW_TAG_class_type
680b30c7 8696 || last_die->tag == DW_TAG_interface_type
72bf9492 8697 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8698 || last_die->tag == DW_TAG_union_type))
8699 || (cu->language == language_ada
8700 && (last_die->tag == DW_TAG_subprogram
8701 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8702 {
8703 nesting_level++;
8704 parent_die = last_die;
8705 continue;
8706 }
8707
8708 /* Otherwise we skip to the next sibling, if any. */
93311388 8709 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8710
8711 /* Back to the top, do it again. */
8712 }
8713}
8714
c906108c
SS
8715/* Read a minimal amount of information into the minimal die structure. */
8716
fe1b8b76 8717static gdb_byte *
72bf9492
DJ
8718read_partial_die (struct partial_die_info *part_die,
8719 struct abbrev_info *abbrev,
8720 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8721 gdb_byte *buffer, gdb_byte *info_ptr,
8722 struct dwarf2_cu *cu)
c906108c 8723{
fa238c03 8724 unsigned int i;
c906108c 8725 struct attribute attr;
c5aa993b 8726 int has_low_pc_attr = 0;
c906108c
SS
8727 int has_high_pc_attr = 0;
8728
72bf9492 8729 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8730
93311388 8731 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8732
8733 info_ptr += abbrev_len;
8734
8735 if (abbrev == NULL)
8736 return info_ptr;
8737
c906108c
SS
8738 part_die->tag = abbrev->tag;
8739 part_die->has_children = abbrev->has_children;
c906108c
SS
8740
8741 for (i = 0; i < abbrev->num_attrs; ++i)
8742 {
e7c27a73 8743 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8744
8745 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8746 partial symbol table. */
c906108c
SS
8747 switch (attr.name)
8748 {
8749 case DW_AT_name:
71c25dea
TT
8750 switch (part_die->tag)
8751 {
8752 case DW_TAG_compile_unit:
348e048f 8753 case DW_TAG_type_unit:
71c25dea
TT
8754 /* Compilation units have a DW_AT_name that is a filename, not
8755 a source language identifier. */
8756 case DW_TAG_enumeration_type:
8757 case DW_TAG_enumerator:
8758 /* These tags always have simple identifiers already; no need
8759 to canonicalize them. */
8760 part_die->name = DW_STRING (&attr);
8761 break;
8762 default:
8763 part_die->name
8764 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8765 &cu->objfile->objfile_obstack);
71c25dea
TT
8766 break;
8767 }
c906108c 8768 break;
31ef98ae 8769 case DW_AT_linkage_name:
c906108c 8770 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8771 /* Note that both forms of linkage name might appear. We
8772 assume they will be the same, and we only store the last
8773 one we see. */
94af9270
KS
8774 if (cu->language == language_ada)
8775 part_die->name = DW_STRING (&attr);
c906108c
SS
8776 break;
8777 case DW_AT_low_pc:
8778 has_low_pc_attr = 1;
8779 part_die->lowpc = DW_ADDR (&attr);
8780 break;
8781 case DW_AT_high_pc:
8782 has_high_pc_attr = 1;
8783 part_die->highpc = DW_ADDR (&attr);
8784 break;
8785 case DW_AT_location:
8e19ed76
PS
8786 /* Support the .debug_loc offsets */
8787 if (attr_form_is_block (&attr))
8788 {
8789 part_die->locdesc = DW_BLOCK (&attr);
8790 }
3690dd37 8791 else if (attr_form_is_section_offset (&attr))
8e19ed76 8792 {
4d3c2250 8793 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8794 }
8795 else
8796 {
4d3c2250
KB
8797 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8798 "partial symbol information");
8e19ed76 8799 }
c906108c 8800 break;
c906108c
SS
8801 case DW_AT_external:
8802 part_die->is_external = DW_UNSND (&attr);
8803 break;
8804 case DW_AT_declaration:
8805 part_die->is_declaration = DW_UNSND (&attr);
8806 break;
8807 case DW_AT_type:
8808 part_die->has_type = 1;
8809 break;
8810 case DW_AT_abstract_origin:
8811 case DW_AT_specification:
72bf9492
DJ
8812 case DW_AT_extension:
8813 part_die->has_specification = 1;
c764a876 8814 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8815 break;
8816 case DW_AT_sibling:
8817 /* Ignore absolute siblings, they might point outside of
8818 the current compile unit. */
8819 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8820 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8821 else
93311388 8822 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8823 break;
fa4028e9
JB
8824 case DW_AT_byte_size:
8825 part_die->has_byte_size = 1;
8826 break;
68511cec
CES
8827 case DW_AT_calling_convention:
8828 /* DWARF doesn't provide a way to identify a program's source-level
8829 entry point. DW_AT_calling_convention attributes are only meant
8830 to describe functions' calling conventions.
8831
8832 However, because it's a necessary piece of information in
8833 Fortran, and because DW_CC_program is the only piece of debugging
8834 information whose definition refers to a 'main program' at all,
8835 several compilers have begun marking Fortran main programs with
8836 DW_CC_program --- even when those functions use the standard
8837 calling conventions.
8838
8839 So until DWARF specifies a way to provide this information and
8840 compilers pick up the new representation, we'll support this
8841 practice. */
8842 if (DW_UNSND (&attr) == DW_CC_program
8843 && cu->language == language_fortran)
8844 set_main_name (part_die->name);
8845 break;
c906108c
SS
8846 default:
8847 break;
8848 }
8849 }
8850
c906108c
SS
8851 /* When using the GNU linker, .gnu.linkonce. sections are used to
8852 eliminate duplicate copies of functions and vtables and such.
8853 The linker will arbitrarily choose one and discard the others.
8854 The AT_*_pc values for such functions refer to local labels in
8855 these sections. If the section from that file was discarded, the
8856 labels are not in the output, so the relocs get a value of 0.
8857 If this is a discarded function, mark the pc bounds as invalid,
8858 so that GDB will ignore it. */
8859 if (has_low_pc_attr && has_high_pc_attr
8860 && part_die->lowpc < part_die->highpc
8861 && (part_die->lowpc != 0
72dca2f5 8862 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8863 part_die->has_pc_info = 1;
85cbf3d3 8864
c906108c
SS
8865 return info_ptr;
8866}
8867
72bf9492
DJ
8868/* Find a cached partial DIE at OFFSET in CU. */
8869
8870static struct partial_die_info *
c764a876 8871find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8872{
8873 struct partial_die_info *lookup_die = NULL;
8874 struct partial_die_info part_die;
8875
8876 part_die.offset = offset;
8877 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8878
72bf9492
DJ
8879 return lookup_die;
8880}
8881
348e048f
DE
8882/* Find a partial DIE at OFFSET, which may or may not be in CU,
8883 except in the case of .debug_types DIEs which do not reference
8884 outside their CU (they do however referencing other types via
8885 DW_FORM_sig8). */
72bf9492
DJ
8886
8887static struct partial_die_info *
c764a876 8888find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8889{
5afb4e99
DJ
8890 struct dwarf2_per_cu_data *per_cu = NULL;
8891 struct partial_die_info *pd = NULL;
72bf9492 8892
348e048f
DE
8893 if (cu->per_cu->from_debug_types)
8894 {
8895 pd = find_partial_die_in_comp_unit (offset, cu);
8896 if (pd != NULL)
8897 return pd;
8898 goto not_found;
8899 }
8900
45452591 8901 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8902 {
8903 pd = find_partial_die_in_comp_unit (offset, cu);
8904 if (pd != NULL)
8905 return pd;
8906 }
72bf9492 8907
ae038cb0
DJ
8908 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8909
98bfdba5
PA
8910 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8911 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8912
8913 per_cu->cu->last_used = 0;
5afb4e99
DJ
8914 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8915
8916 if (pd == NULL && per_cu->load_all_dies == 0)
8917 {
8918 struct cleanup *back_to;
8919 struct partial_die_info comp_unit_die;
8920 struct abbrev_info *abbrev;
8921 unsigned int bytes_read;
8922 char *info_ptr;
8923
8924 per_cu->load_all_dies = 1;
8925
8926 /* Re-read the DIEs. */
8927 back_to = make_cleanup (null_cleanup, 0);
8928 if (per_cu->cu->dwarf2_abbrevs == NULL)
8929 {
8930 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8931 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8932 }
dce234bc 8933 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8934 + per_cu->cu->header.offset
8935 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8936 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8937 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8938 per_cu->cu->objfile->obfd,
8939 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8940 per_cu->cu);
8941 if (comp_unit_die.has_children)
93311388
DE
8942 load_partial_dies (per_cu->cu->objfile->obfd,
8943 dwarf2_per_objfile->info.buffer, info_ptr,
8944 0, per_cu->cu);
5afb4e99
DJ
8945 do_cleanups (back_to);
8946
8947 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8948 }
8949
348e048f
DE
8950 not_found:
8951
5afb4e99
DJ
8952 if (pd == NULL)
8953 internal_error (__FILE__, __LINE__,
c764a876 8954 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8955 offset, bfd_get_filename (cu->objfile->obfd));
8956 return pd;
72bf9492
DJ
8957}
8958
8959/* Adjust PART_DIE before generating a symbol for it. This function
8960 may set the is_external flag or change the DIE's name. */
8961
8962static void
8963fixup_partial_die (struct partial_die_info *part_die,
8964 struct dwarf2_cu *cu)
8965{
8966 /* If we found a reference attribute and the DIE has no name, try
8967 to find a name in the referred to DIE. */
8968
8969 if (part_die->name == NULL && part_die->has_specification)
8970 {
8971 struct partial_die_info *spec_die;
72bf9492 8972
10b3939b 8973 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8974
10b3939b 8975 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8976
8977 if (spec_die->name)
8978 {
8979 part_die->name = spec_die->name;
8980
8981 /* Copy DW_AT_external attribute if it is set. */
8982 if (spec_die->is_external)
8983 part_die->is_external = spec_die->is_external;
8984 }
8985 }
8986
8987 /* Set default names for some unnamed DIEs. */
8988 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8989 || part_die->tag == DW_TAG_class_type))
8990 part_die->name = "(anonymous class)";
8991
8992 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8993 part_die->name = "(anonymous namespace)";
8994
8995 if (part_die->tag == DW_TAG_structure_type
8996 || part_die->tag == DW_TAG_class_type
8997 || part_die->tag == DW_TAG_union_type)
8998 guess_structure_name (part_die, cu);
8999}
9000
a8329558 9001/* Read an attribute value described by an attribute form. */
c906108c 9002
fe1b8b76 9003static gdb_byte *
a8329558 9004read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9005 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9006 struct dwarf2_cu *cu)
c906108c 9007{
e7c27a73 9008 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9009 unsigned int bytes_read;
9010 struct dwarf_block *blk;
9011
a8329558
KW
9012 attr->form = form;
9013 switch (form)
c906108c 9014 {
c906108c 9015 case DW_FORM_ref_addr:
ae411497
TT
9016 if (cu->header.version == 2)
9017 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9018 else
9019 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9020 info_ptr += bytes_read;
9021 break;
9022 case DW_FORM_addr:
e7c27a73 9023 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9024 info_ptr += bytes_read;
c906108c
SS
9025 break;
9026 case DW_FORM_block2:
7b5a2f43 9027 blk = dwarf_alloc_block (cu);
c906108c
SS
9028 blk->size = read_2_bytes (abfd, info_ptr);
9029 info_ptr += 2;
9030 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9031 info_ptr += blk->size;
9032 DW_BLOCK (attr) = blk;
9033 break;
9034 case DW_FORM_block4:
7b5a2f43 9035 blk = dwarf_alloc_block (cu);
c906108c
SS
9036 blk->size = read_4_bytes (abfd, info_ptr);
9037 info_ptr += 4;
9038 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9039 info_ptr += blk->size;
9040 DW_BLOCK (attr) = blk;
9041 break;
9042 case DW_FORM_data2:
9043 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9044 info_ptr += 2;
9045 break;
9046 case DW_FORM_data4:
9047 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9048 info_ptr += 4;
9049 break;
9050 case DW_FORM_data8:
9051 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9052 info_ptr += 8;
9053 break;
2dc7f7b3
TT
9054 case DW_FORM_sec_offset:
9055 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9056 info_ptr += bytes_read;
9057 break;
c906108c 9058 case DW_FORM_string:
9b1c24c8 9059 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9060 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9061 info_ptr += bytes_read;
9062 break;
4bdf3d34
JJ
9063 case DW_FORM_strp:
9064 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9065 &bytes_read);
8285870a 9066 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9067 info_ptr += bytes_read;
9068 break;
2dc7f7b3 9069 case DW_FORM_exprloc:
c906108c 9070 case DW_FORM_block:
7b5a2f43 9071 blk = dwarf_alloc_block (cu);
c906108c
SS
9072 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9073 info_ptr += bytes_read;
9074 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9075 info_ptr += blk->size;
9076 DW_BLOCK (attr) = blk;
9077 break;
9078 case DW_FORM_block1:
7b5a2f43 9079 blk = dwarf_alloc_block (cu);
c906108c
SS
9080 blk->size = read_1_byte (abfd, info_ptr);
9081 info_ptr += 1;
9082 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9083 info_ptr += blk->size;
9084 DW_BLOCK (attr) = blk;
9085 break;
9086 case DW_FORM_data1:
9087 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9088 info_ptr += 1;
9089 break;
9090 case DW_FORM_flag:
9091 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9092 info_ptr += 1;
9093 break;
2dc7f7b3
TT
9094 case DW_FORM_flag_present:
9095 DW_UNSND (attr) = 1;
9096 break;
c906108c
SS
9097 case DW_FORM_sdata:
9098 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9099 info_ptr += bytes_read;
9100 break;
9101 case DW_FORM_udata:
9102 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9103 info_ptr += bytes_read;
9104 break;
9105 case DW_FORM_ref1:
10b3939b 9106 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9107 info_ptr += 1;
9108 break;
9109 case DW_FORM_ref2:
10b3939b 9110 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9111 info_ptr += 2;
9112 break;
9113 case DW_FORM_ref4:
10b3939b 9114 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9115 info_ptr += 4;
9116 break;
613e1657 9117 case DW_FORM_ref8:
10b3939b 9118 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9119 info_ptr += 8;
9120 break;
348e048f
DE
9121 case DW_FORM_sig8:
9122 /* Convert the signature to something we can record in DW_UNSND
9123 for later lookup.
9124 NOTE: This is NULL if the type wasn't found. */
9125 DW_SIGNATURED_TYPE (attr) =
9126 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9127 info_ptr += 8;
9128 break;
c906108c 9129 case DW_FORM_ref_udata:
10b3939b
DJ
9130 DW_ADDR (attr) = (cu->header.offset
9131 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9132 info_ptr += bytes_read;
9133 break;
c906108c 9134 case DW_FORM_indirect:
a8329558
KW
9135 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9136 info_ptr += bytes_read;
e7c27a73 9137 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9138 break;
c906108c 9139 default:
8a3fe4f8 9140 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9141 dwarf_form_name (form),
9142 bfd_get_filename (abfd));
c906108c 9143 }
28e94949
JB
9144
9145 /* We have seen instances where the compiler tried to emit a byte
9146 size attribute of -1 which ended up being encoded as an unsigned
9147 0xffffffff. Although 0xffffffff is technically a valid size value,
9148 an object of this size seems pretty unlikely so we can relatively
9149 safely treat these cases as if the size attribute was invalid and
9150 treat them as zero by default. */
9151 if (attr->name == DW_AT_byte_size
9152 && form == DW_FORM_data4
9153 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9154 {
9155 complaint
9156 (&symfile_complaints,
43bbcdc2
PH
9157 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9158 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9159 DW_UNSND (attr) = 0;
9160 }
28e94949 9161
c906108c
SS
9162 return info_ptr;
9163}
9164
a8329558
KW
9165/* Read an attribute described by an abbreviated attribute. */
9166
fe1b8b76 9167static gdb_byte *
a8329558 9168read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9169 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9170{
9171 attr->name = abbrev->name;
e7c27a73 9172 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9173}
9174
c906108c
SS
9175/* read dwarf information from a buffer */
9176
9177static unsigned int
fe1b8b76 9178read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9179{
fe1b8b76 9180 return bfd_get_8 (abfd, buf);
c906108c
SS
9181}
9182
9183static int
fe1b8b76 9184read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9185{
fe1b8b76 9186 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9187}
9188
9189static unsigned int
fe1b8b76 9190read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9191{
fe1b8b76 9192 return bfd_get_16 (abfd, buf);
c906108c
SS
9193}
9194
9195static int
fe1b8b76 9196read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9197{
fe1b8b76 9198 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9199}
9200
9201static unsigned int
fe1b8b76 9202read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9203{
fe1b8b76 9204 return bfd_get_32 (abfd, buf);
c906108c
SS
9205}
9206
9207static int
fe1b8b76 9208read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9209{
fe1b8b76 9210 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9211}
9212
93311388 9213static ULONGEST
fe1b8b76 9214read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9215{
fe1b8b76 9216 return bfd_get_64 (abfd, buf);
c906108c
SS
9217}
9218
9219static CORE_ADDR
fe1b8b76 9220read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9221 unsigned int *bytes_read)
c906108c 9222{
e7c27a73 9223 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9224 CORE_ADDR retval = 0;
9225
107d2387 9226 if (cu_header->signed_addr_p)
c906108c 9227 {
107d2387
AC
9228 switch (cu_header->addr_size)
9229 {
9230 case 2:
fe1b8b76 9231 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9232 break;
9233 case 4:
fe1b8b76 9234 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9235 break;
9236 case 8:
fe1b8b76 9237 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9238 break;
9239 default:
8e65ff28 9240 internal_error (__FILE__, __LINE__,
e2e0b3e5 9241 _("read_address: bad switch, signed [in module %s]"),
659b0389 9242 bfd_get_filename (abfd));
107d2387
AC
9243 }
9244 }
9245 else
9246 {
9247 switch (cu_header->addr_size)
9248 {
9249 case 2:
fe1b8b76 9250 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9251 break;
9252 case 4:
fe1b8b76 9253 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9254 break;
9255 case 8:
fe1b8b76 9256 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9257 break;
9258 default:
8e65ff28 9259 internal_error (__FILE__, __LINE__,
e2e0b3e5 9260 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9261 bfd_get_filename (abfd));
107d2387 9262 }
c906108c 9263 }
64367e0a 9264
107d2387
AC
9265 *bytes_read = cu_header->addr_size;
9266 return retval;
c906108c
SS
9267}
9268
f7ef9339 9269/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9270 specification allows the initial length to take up either 4 bytes
9271 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9272 bytes describe the length and all offsets will be 8 bytes in length
9273 instead of 4.
9274
f7ef9339
KB
9275 An older, non-standard 64-bit format is also handled by this
9276 function. The older format in question stores the initial length
9277 as an 8-byte quantity without an escape value. Lengths greater
9278 than 2^32 aren't very common which means that the initial 4 bytes
9279 is almost always zero. Since a length value of zero doesn't make
9280 sense for the 32-bit format, this initial zero can be considered to
9281 be an escape value which indicates the presence of the older 64-bit
9282 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9283 greater than 4GB. If it becomes necessary to handle lengths
9284 somewhat larger than 4GB, we could allow other small values (such
9285 as the non-sensical values of 1, 2, and 3) to also be used as
9286 escape values indicating the presence of the old format.
f7ef9339 9287
917c78fc
MK
9288 The value returned via bytes_read should be used to increment the
9289 relevant pointer after calling read_initial_length().
c764a876 9290
613e1657
KB
9291 [ Note: read_initial_length() and read_offset() are based on the
9292 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9293 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9294 from:
9295
f7ef9339 9296 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9297
613e1657
KB
9298 This document is only a draft and is subject to change. (So beware.)
9299
f7ef9339 9300 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9301 determined empirically by examining 64-bit ELF files produced by
9302 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9303
9304 - Kevin, July 16, 2002
613e1657
KB
9305 ] */
9306
9307static LONGEST
c764a876 9308read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9309{
fe1b8b76 9310 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9311
dd373385 9312 if (length == 0xffffffff)
613e1657 9313 {
fe1b8b76 9314 length = bfd_get_64 (abfd, buf + 4);
613e1657 9315 *bytes_read = 12;
613e1657 9316 }
dd373385 9317 else if (length == 0)
f7ef9339 9318 {
dd373385 9319 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9320 length = bfd_get_64 (abfd, buf);
f7ef9339 9321 *bytes_read = 8;
f7ef9339 9322 }
613e1657
KB
9323 else
9324 {
9325 *bytes_read = 4;
613e1657
KB
9326 }
9327
c764a876
DE
9328 return length;
9329}
dd373385 9330
c764a876
DE
9331/* Cover function for read_initial_length.
9332 Returns the length of the object at BUF, and stores the size of the
9333 initial length in *BYTES_READ and stores the size that offsets will be in
9334 *OFFSET_SIZE.
9335 If the initial length size is not equivalent to that specified in
9336 CU_HEADER then issue a complaint.
9337 This is useful when reading non-comp-unit headers. */
dd373385 9338
c764a876
DE
9339static LONGEST
9340read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9341 const struct comp_unit_head *cu_header,
9342 unsigned int *bytes_read,
9343 unsigned int *offset_size)
9344{
9345 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9346
9347 gdb_assert (cu_header->initial_length_size == 4
9348 || cu_header->initial_length_size == 8
9349 || cu_header->initial_length_size == 12);
9350
9351 if (cu_header->initial_length_size != *bytes_read)
9352 complaint (&symfile_complaints,
9353 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9354
c764a876 9355 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9356 return length;
613e1657
KB
9357}
9358
9359/* Read an offset from the data stream. The size of the offset is
917c78fc 9360 given by cu_header->offset_size. */
613e1657
KB
9361
9362static LONGEST
fe1b8b76 9363read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9364 unsigned int *bytes_read)
c764a876
DE
9365{
9366 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9367
c764a876
DE
9368 *bytes_read = cu_header->offset_size;
9369 return offset;
9370}
9371
9372/* Read an offset from the data stream. */
9373
9374static LONGEST
9375read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9376{
9377 LONGEST retval = 0;
9378
c764a876 9379 switch (offset_size)
613e1657
KB
9380 {
9381 case 4:
fe1b8b76 9382 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9383 break;
9384 case 8:
fe1b8b76 9385 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9386 break;
9387 default:
8e65ff28 9388 internal_error (__FILE__, __LINE__,
c764a876 9389 _("read_offset_1: bad switch [in module %s]"),
659b0389 9390 bfd_get_filename (abfd));
613e1657
KB
9391 }
9392
917c78fc 9393 return retval;
613e1657
KB
9394}
9395
fe1b8b76
JB
9396static gdb_byte *
9397read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9398{
9399 /* If the size of a host char is 8 bits, we can return a pointer
9400 to the buffer, otherwise we have to copy the data to a buffer
9401 allocated on the temporary obstack. */
4bdf3d34 9402 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9403 return buf;
c906108c
SS
9404}
9405
9406static char *
9b1c24c8 9407read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9408{
9409 /* If the size of a host char is 8 bits, we can return a pointer
9410 to the string, otherwise we have to copy the string to a buffer
9411 allocated on the temporary obstack. */
4bdf3d34 9412 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9413 if (*buf == '\0')
9414 {
9415 *bytes_read_ptr = 1;
9416 return NULL;
9417 }
fe1b8b76
JB
9418 *bytes_read_ptr = strlen ((char *) buf) + 1;
9419 return (char *) buf;
4bdf3d34
JJ
9420}
9421
9422static char *
fe1b8b76 9423read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9424 const struct comp_unit_head *cu_header,
9425 unsigned int *bytes_read_ptr)
9426{
c764a876 9427 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9428
be391dca 9429 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9430 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9431 {
8a3fe4f8 9432 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9433 bfd_get_filename (abfd));
4bdf3d34 9434 return NULL;
c906108c 9435 }
dce234bc 9436 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9437 {
8a3fe4f8 9438 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9439 bfd_get_filename (abfd));
c906108c
SS
9440 return NULL;
9441 }
4bdf3d34 9442 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9443 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9444 return NULL;
dce234bc 9445 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9446}
9447
ce5d95e1 9448static unsigned long
fe1b8b76 9449read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9450{
ce5d95e1
JB
9451 unsigned long result;
9452 unsigned int num_read;
c906108c
SS
9453 int i, shift;
9454 unsigned char byte;
9455
9456 result = 0;
9457 shift = 0;
9458 num_read = 0;
9459 i = 0;
9460 while (1)
9461 {
fe1b8b76 9462 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9463 buf++;
9464 num_read++;
ce5d95e1 9465 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9466 if ((byte & 128) == 0)
9467 {
9468 break;
9469 }
9470 shift += 7;
9471 }
9472 *bytes_read_ptr = num_read;
9473 return result;
9474}
9475
ce5d95e1 9476static long
fe1b8b76 9477read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9478{
ce5d95e1 9479 long result;
77e0b926 9480 int i, shift, num_read;
c906108c
SS
9481 unsigned char byte;
9482
9483 result = 0;
9484 shift = 0;
c906108c
SS
9485 num_read = 0;
9486 i = 0;
9487 while (1)
9488 {
fe1b8b76 9489 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9490 buf++;
9491 num_read++;
ce5d95e1 9492 result |= ((long)(byte & 127) << shift);
c906108c
SS
9493 shift += 7;
9494 if ((byte & 128) == 0)
9495 {
9496 break;
9497 }
9498 }
77e0b926
DJ
9499 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9500 result |= -(((long)1) << shift);
c906108c
SS
9501 *bytes_read_ptr = num_read;
9502 return result;
9503}
9504
4bb7a0a7
DJ
9505/* Return a pointer to just past the end of an LEB128 number in BUF. */
9506
fe1b8b76
JB
9507static gdb_byte *
9508skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9509{
9510 int byte;
9511
9512 while (1)
9513 {
fe1b8b76 9514 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9515 buf++;
9516 if ((byte & 128) == 0)
9517 return buf;
9518 }
9519}
9520
c906108c 9521static void
e142c38c 9522set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9523{
9524 switch (lang)
9525 {
9526 case DW_LANG_C89:
76bee0cc 9527 case DW_LANG_C99:
c906108c 9528 case DW_LANG_C:
e142c38c 9529 cu->language = language_c;
c906108c
SS
9530 break;
9531 case DW_LANG_C_plus_plus:
e142c38c 9532 cu->language = language_cplus;
c906108c 9533 break;
6aecb9c2
JB
9534 case DW_LANG_D:
9535 cu->language = language_d;
9536 break;
c906108c
SS
9537 case DW_LANG_Fortran77:
9538 case DW_LANG_Fortran90:
b21b22e0 9539 case DW_LANG_Fortran95:
e142c38c 9540 cu->language = language_fortran;
c906108c
SS
9541 break;
9542 case DW_LANG_Mips_Assembler:
e142c38c 9543 cu->language = language_asm;
c906108c 9544 break;
bebd888e 9545 case DW_LANG_Java:
e142c38c 9546 cu->language = language_java;
bebd888e 9547 break;
c906108c 9548 case DW_LANG_Ada83:
8aaf0b47 9549 case DW_LANG_Ada95:
bc5f45f8
JB
9550 cu->language = language_ada;
9551 break;
72019c9c
GM
9552 case DW_LANG_Modula2:
9553 cu->language = language_m2;
9554 break;
fe8e67fd
PM
9555 case DW_LANG_Pascal83:
9556 cu->language = language_pascal;
9557 break;
22566fbd
DJ
9558 case DW_LANG_ObjC:
9559 cu->language = language_objc;
9560 break;
c906108c
SS
9561 case DW_LANG_Cobol74:
9562 case DW_LANG_Cobol85:
c906108c 9563 default:
e142c38c 9564 cu->language = language_minimal;
c906108c
SS
9565 break;
9566 }
e142c38c 9567 cu->language_defn = language_def (cu->language);
c906108c
SS
9568}
9569
9570/* Return the named attribute or NULL if not there. */
9571
9572static struct attribute *
e142c38c 9573dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9574{
9575 unsigned int i;
9576 struct attribute *spec = NULL;
9577
9578 for (i = 0; i < die->num_attrs; ++i)
9579 {
9580 if (die->attrs[i].name == name)
10b3939b 9581 return &die->attrs[i];
c906108c
SS
9582 if (die->attrs[i].name == DW_AT_specification
9583 || die->attrs[i].name == DW_AT_abstract_origin)
9584 spec = &die->attrs[i];
9585 }
c906108c 9586
10b3939b 9587 if (spec)
f2f0e013
DJ
9588 {
9589 die = follow_die_ref (die, spec, &cu);
9590 return dwarf2_attr (die, name, cu);
9591 }
c5aa993b 9592
c906108c
SS
9593 return NULL;
9594}
9595
348e048f
DE
9596/* Return the named attribute or NULL if not there,
9597 but do not follow DW_AT_specification, etc.
9598 This is for use in contexts where we're reading .debug_types dies.
9599 Following DW_AT_specification, DW_AT_abstract_origin will take us
9600 back up the chain, and we want to go down. */
9601
9602static struct attribute *
9603dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9604 struct dwarf2_cu *cu)
9605{
9606 unsigned int i;
9607
9608 for (i = 0; i < die->num_attrs; ++i)
9609 if (die->attrs[i].name == name)
9610 return &die->attrs[i];
9611
9612 return NULL;
9613}
9614
05cf31d1
JB
9615/* Return non-zero iff the attribute NAME is defined for the given DIE,
9616 and holds a non-zero value. This function should only be used for
2dc7f7b3 9617 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9618
9619static int
9620dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9621{
9622 struct attribute *attr = dwarf2_attr (die, name, cu);
9623
9624 return (attr && DW_UNSND (attr));
9625}
9626
3ca72b44 9627static int
e142c38c 9628die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9629{
05cf31d1
JB
9630 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9631 which value is non-zero. However, we have to be careful with
9632 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9633 (via dwarf2_flag_true_p) follows this attribute. So we may
9634 end up accidently finding a declaration attribute that belongs
9635 to a different DIE referenced by the specification attribute,
9636 even though the given DIE does not have a declaration attribute. */
9637 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9638 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9639}
9640
63d06c5c 9641/* Return the die giving the specification for DIE, if there is
f2f0e013 9642 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9643 containing the return value on output. If there is no
9644 specification, but there is an abstract origin, that is
9645 returned. */
63d06c5c
DC
9646
9647static struct die_info *
f2f0e013 9648die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9649{
f2f0e013
DJ
9650 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9651 *spec_cu);
63d06c5c 9652
edb3359d
DJ
9653 if (spec_attr == NULL)
9654 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9655
63d06c5c
DC
9656 if (spec_attr == NULL)
9657 return NULL;
9658 else
f2f0e013 9659 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9660}
c906108c 9661
debd256d
JB
9662/* Free the line_header structure *LH, and any arrays and strings it
9663 refers to. */
9664static void
9665free_line_header (struct line_header *lh)
9666{
9667 if (lh->standard_opcode_lengths)
a8bc7b56 9668 xfree (lh->standard_opcode_lengths);
debd256d
JB
9669
9670 /* Remember that all the lh->file_names[i].name pointers are
9671 pointers into debug_line_buffer, and don't need to be freed. */
9672 if (lh->file_names)
a8bc7b56 9673 xfree (lh->file_names);
debd256d
JB
9674
9675 /* Similarly for the include directory names. */
9676 if (lh->include_dirs)
a8bc7b56 9677 xfree (lh->include_dirs);
debd256d 9678
a8bc7b56 9679 xfree (lh);
debd256d
JB
9680}
9681
9682
9683/* Add an entry to LH's include directory table. */
9684static void
9685add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9686{
debd256d
JB
9687 /* Grow the array if necessary. */
9688 if (lh->include_dirs_size == 0)
c5aa993b 9689 {
debd256d
JB
9690 lh->include_dirs_size = 1; /* for testing */
9691 lh->include_dirs = xmalloc (lh->include_dirs_size
9692 * sizeof (*lh->include_dirs));
9693 }
9694 else if (lh->num_include_dirs >= lh->include_dirs_size)
9695 {
9696 lh->include_dirs_size *= 2;
9697 lh->include_dirs = xrealloc (lh->include_dirs,
9698 (lh->include_dirs_size
9699 * sizeof (*lh->include_dirs)));
c5aa993b 9700 }
c906108c 9701
debd256d
JB
9702 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9703}
6e70227d 9704
debd256d
JB
9705
9706/* Add an entry to LH's file name table. */
9707static void
9708add_file_name (struct line_header *lh,
9709 char *name,
9710 unsigned int dir_index,
9711 unsigned int mod_time,
9712 unsigned int length)
9713{
9714 struct file_entry *fe;
9715
9716 /* Grow the array if necessary. */
9717 if (lh->file_names_size == 0)
9718 {
9719 lh->file_names_size = 1; /* for testing */
9720 lh->file_names = xmalloc (lh->file_names_size
9721 * sizeof (*lh->file_names));
9722 }
9723 else if (lh->num_file_names >= lh->file_names_size)
9724 {
9725 lh->file_names_size *= 2;
9726 lh->file_names = xrealloc (lh->file_names,
9727 (lh->file_names_size
9728 * sizeof (*lh->file_names)));
9729 }
9730
9731 fe = &lh->file_names[lh->num_file_names++];
9732 fe->name = name;
9733 fe->dir_index = dir_index;
9734 fe->mod_time = mod_time;
9735 fe->length = length;
aaa75496 9736 fe->included_p = 0;
cb1df416 9737 fe->symtab = NULL;
debd256d 9738}
6e70227d 9739
debd256d
JB
9740
9741/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9742 .debug_line, according to the endianness of ABFD. Return a pointer
9743 to a struct line_header, allocated using xmalloc.
debd256d
JB
9744
9745 NOTE: the strings in the include directory and file name tables of
9746 the returned object point into debug_line_buffer, and must not be
9747 freed. */
9748static struct line_header *
9749dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9750 struct dwarf2_cu *cu)
debd256d
JB
9751{
9752 struct cleanup *back_to;
9753 struct line_header *lh;
fe1b8b76 9754 gdb_byte *line_ptr;
c764a876 9755 unsigned int bytes_read, offset_size;
debd256d
JB
9756 int i;
9757 char *cur_dir, *cur_file;
9758
be391dca 9759 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9760 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9761 {
e2e0b3e5 9762 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9763 return 0;
9764 }
9765
a738430d
MK
9766 /* Make sure that at least there's room for the total_length field.
9767 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9768 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9769 {
4d3c2250 9770 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9771 return 0;
9772 }
9773
9774 lh = xmalloc (sizeof (*lh));
9775 memset (lh, 0, sizeof (*lh));
9776 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9777 (void *) lh);
9778
dce234bc 9779 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9780
a738430d 9781 /* Read in the header. */
6e70227d 9782 lh->total_length =
c764a876
DE
9783 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9784 &bytes_read, &offset_size);
debd256d 9785 line_ptr += bytes_read;
dce234bc
PP
9786 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9787 + dwarf2_per_objfile->line.size))
debd256d 9788 {
4d3c2250 9789 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9790 return 0;
9791 }
9792 lh->statement_program_end = line_ptr + lh->total_length;
9793 lh->version = read_2_bytes (abfd, line_ptr);
9794 line_ptr += 2;
c764a876
DE
9795 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9796 line_ptr += offset_size;
debd256d
JB
9797 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9798 line_ptr += 1;
2dc7f7b3
TT
9799 if (lh->version >= 4)
9800 {
9801 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9802 line_ptr += 1;
9803 }
9804 else
9805 lh->maximum_ops_per_instruction = 1;
9806
9807 if (lh->maximum_ops_per_instruction == 0)
9808 {
9809 lh->maximum_ops_per_instruction = 1;
9810 complaint (&symfile_complaints,
9811 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9812 }
9813
debd256d
JB
9814 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9815 line_ptr += 1;
9816 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9817 line_ptr += 1;
9818 lh->line_range = read_1_byte (abfd, line_ptr);
9819 line_ptr += 1;
9820 lh->opcode_base = read_1_byte (abfd, line_ptr);
9821 line_ptr += 1;
9822 lh->standard_opcode_lengths
fe1b8b76 9823 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9824
9825 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9826 for (i = 1; i < lh->opcode_base; ++i)
9827 {
9828 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9829 line_ptr += 1;
9830 }
9831
a738430d 9832 /* Read directory table. */
9b1c24c8 9833 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9834 {
9835 line_ptr += bytes_read;
9836 add_include_dir (lh, cur_dir);
9837 }
9838 line_ptr += bytes_read;
9839
a738430d 9840 /* Read file name table. */
9b1c24c8 9841 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9842 {
9843 unsigned int dir_index, mod_time, length;
9844
9845 line_ptr += bytes_read;
9846 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9847 line_ptr += bytes_read;
9848 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9849 line_ptr += bytes_read;
9850 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9851 line_ptr += bytes_read;
9852
9853 add_file_name (lh, cur_file, dir_index, mod_time, length);
9854 }
9855 line_ptr += bytes_read;
6e70227d 9856 lh->statement_program_start = line_ptr;
debd256d 9857
dce234bc
PP
9858 if (line_ptr > (dwarf2_per_objfile->line.buffer
9859 + dwarf2_per_objfile->line.size))
4d3c2250 9860 complaint (&symfile_complaints,
e2e0b3e5 9861 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9862
9863 discard_cleanups (back_to);
9864 return lh;
9865}
c906108c 9866
5fb290d7
DJ
9867/* This function exists to work around a bug in certain compilers
9868 (particularly GCC 2.95), in which the first line number marker of a
9869 function does not show up until after the prologue, right before
9870 the second line number marker. This function shifts ADDRESS down
9871 to the beginning of the function if necessary, and is called on
9872 addresses passed to record_line. */
9873
9874static CORE_ADDR
e142c38c 9875check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9876{
9877 struct function_range *fn;
9878
9879 /* Find the function_range containing address. */
e142c38c 9880 if (!cu->first_fn)
5fb290d7
DJ
9881 return address;
9882
e142c38c
DJ
9883 if (!cu->cached_fn)
9884 cu->cached_fn = cu->first_fn;
5fb290d7 9885
e142c38c 9886 fn = cu->cached_fn;
5fb290d7
DJ
9887 while (fn)
9888 if (fn->lowpc <= address && fn->highpc > address)
9889 goto found;
9890 else
9891 fn = fn->next;
9892
e142c38c
DJ
9893 fn = cu->first_fn;
9894 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9895 if (fn->lowpc <= address && fn->highpc > address)
9896 goto found;
9897 else
9898 fn = fn->next;
9899
9900 return address;
9901
9902 found:
9903 if (fn->seen_line)
9904 return address;
9905 if (address != fn->lowpc)
4d3c2250 9906 complaint (&symfile_complaints,
e2e0b3e5 9907 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9908 (unsigned long) address, fn->name);
5fb290d7
DJ
9909 fn->seen_line = 1;
9910 return fn->lowpc;
9911}
9912
c6da4cef
DE
9913/* Subroutine of dwarf_decode_lines to simplify it.
9914 Return the file name of the psymtab for included file FILE_INDEX
9915 in line header LH of PST.
9916 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9917 If space for the result is malloc'd, it will be freed by a cleanup.
9918 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
9919
9920static char *
9921psymtab_include_file_name (const struct line_header *lh, int file_index,
9922 const struct partial_symtab *pst,
9923 const char *comp_dir)
9924{
9925 const struct file_entry fe = lh->file_names [file_index];
9926 char *include_name = fe.name;
9927 char *include_name_to_compare = include_name;
9928 char *dir_name = NULL;
9929 char *pst_filename;
9930 int file_is_pst;
9931
9932 if (fe.dir_index)
9933 dir_name = lh->include_dirs[fe.dir_index - 1];
9934
9935 if (!IS_ABSOLUTE_PATH (include_name)
9936 && (dir_name != NULL || comp_dir != NULL))
9937 {
9938 /* Avoid creating a duplicate psymtab for PST.
9939 We do this by comparing INCLUDE_NAME and PST_FILENAME.
9940 Before we do the comparison, however, we need to account
9941 for DIR_NAME and COMP_DIR.
9942 First prepend dir_name (if non-NULL). If we still don't
9943 have an absolute path prepend comp_dir (if non-NULL).
9944 However, the directory we record in the include-file's
9945 psymtab does not contain COMP_DIR (to match the
9946 corresponding symtab(s)).
9947
9948 Example:
9949
9950 bash$ cd /tmp
9951 bash$ gcc -g ./hello.c
9952 include_name = "hello.c"
9953 dir_name = "."
9954 DW_AT_comp_dir = comp_dir = "/tmp"
9955 DW_AT_name = "./hello.c" */
9956
9957 if (dir_name != NULL)
9958 {
9959 include_name = concat (dir_name, SLASH_STRING,
9960 include_name, (char *)NULL);
9961 include_name_to_compare = include_name;
9962 make_cleanup (xfree, include_name);
9963 }
9964 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
9965 {
9966 include_name_to_compare = concat (comp_dir, SLASH_STRING,
9967 include_name, (char *)NULL);
9968 }
9969 }
9970
9971 pst_filename = pst->filename;
9972 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9973 {
9974 pst_filename = concat (pst->dirname, SLASH_STRING,
9975 pst_filename, (char *)NULL);
9976 }
9977
9978 file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
9979
9980 if (include_name_to_compare != include_name)
9981 xfree (include_name_to_compare);
9982 if (pst_filename != pst->filename)
9983 xfree (pst_filename);
9984
9985 if (file_is_pst)
9986 return NULL;
9987 return include_name;
9988}
9989
aaa75496
JB
9990/* Decode the Line Number Program (LNP) for the given line_header
9991 structure and CU. The actual information extracted and the type
9992 of structures created from the LNP depends on the value of PST.
9993
9994 1. If PST is NULL, then this procedure uses the data from the program
9995 to create all necessary symbol tables, and their linetables.
6e70227d 9996
aaa75496
JB
9997 2. If PST is not NULL, this procedure reads the program to determine
9998 the list of files included by the unit represented by PST, and
c6da4cef
DE
9999 builds all the associated partial symbol tables.
10000
10001 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10002 It is used for relative paths in the line table.
10003 NOTE: When processing partial symtabs (pst != NULL),
10004 comp_dir == pst->dirname.
10005
10006 NOTE: It is important that psymtabs have the same file name (via strcmp)
10007 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10008 symtab we don't use it in the name of the psymtabs we create.
10009 E.g. expand_line_sal requires this when finding psymtabs to expand.
10010 A good testcase for this is mb-inline.exp. */
debd256d 10011
c906108c 10012static void
debd256d 10013dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 10014 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10015{
a8c50c1f 10016 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10017 gdb_byte *line_end;
a8c50c1f 10018 unsigned int bytes_read, extended_len;
c906108c 10019 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10020 CORE_ADDR baseaddr;
10021 struct objfile *objfile = cu->objfile;
fbf65064 10022 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10023 const int decode_for_pst_p = (pst != NULL);
cb1df416 10024 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10025
10026 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10027
debd256d
JB
10028 line_ptr = lh->statement_program_start;
10029 line_end = lh->statement_program_end;
c906108c
SS
10030
10031 /* Read the statement sequences until there's nothing left. */
10032 while (line_ptr < line_end)
10033 {
10034 /* state machine registers */
10035 CORE_ADDR address = 0;
10036 unsigned int file = 1;
10037 unsigned int line = 1;
10038 unsigned int column = 0;
debd256d 10039 int is_stmt = lh->default_is_stmt;
c906108c
SS
10040 int basic_block = 0;
10041 int end_sequence = 0;
fbf65064 10042 CORE_ADDR addr;
2dc7f7b3 10043 unsigned char op_index = 0;
c906108c 10044
aaa75496 10045 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10046 {
aaa75496 10047 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10048 /* lh->include_dirs and lh->file_names are 0-based, but the
10049 directory and file name numbers in the statement program
10050 are 1-based. */
10051 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10052 char *dir = NULL;
a738430d 10053
debd256d
JB
10054 if (fe->dir_index)
10055 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10056
10057 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10058 }
10059
a738430d 10060 /* Decode the table. */
c5aa993b 10061 while (!end_sequence)
c906108c
SS
10062 {
10063 op_code = read_1_byte (abfd, line_ptr);
10064 line_ptr += 1;
59205f5a
JB
10065 if (line_ptr > line_end)
10066 {
10067 dwarf2_debug_line_missing_end_sequence_complaint ();
10068 break;
10069 }
9aa1fe7e 10070
debd256d 10071 if (op_code >= lh->opcode_base)
6e70227d 10072 {
a738430d 10073 /* Special operand. */
debd256d 10074 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10075 address += (((op_index + (adj_opcode / lh->line_range))
10076 / lh->maximum_ops_per_instruction)
10077 * lh->minimum_instruction_length);
10078 op_index = ((op_index + (adj_opcode / lh->line_range))
10079 % lh->maximum_ops_per_instruction);
debd256d 10080 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10081 if (lh->num_file_names < file || file == 0)
25e43795 10082 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10083 /* For now we ignore lines not starting on an
10084 instruction boundary. */
10085 else if (op_index == 0)
25e43795
DJ
10086 {
10087 lh->file_names[file - 1].included_p = 1;
ca5f395d 10088 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10089 {
10090 if (last_subfile != current_subfile)
10091 {
10092 addr = gdbarch_addr_bits_remove (gdbarch, address);
10093 if (last_subfile)
10094 record_line (last_subfile, 0, addr);
10095 last_subfile = current_subfile;
10096 }
25e43795 10097 /* Append row to matrix using current values. */
fbf65064
UW
10098 addr = check_cu_functions (address, cu);
10099 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10100 record_line (current_subfile, line, addr);
366da635 10101 }
25e43795 10102 }
ca5f395d 10103 basic_block = 0;
9aa1fe7e
GK
10104 }
10105 else switch (op_code)
c906108c
SS
10106 {
10107 case DW_LNS_extended_op:
a8c50c1f 10108 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10109 line_ptr += bytes_read;
a8c50c1f 10110 extended_end = line_ptr + extended_len;
c906108c
SS
10111 extended_op = read_1_byte (abfd, line_ptr);
10112 line_ptr += 1;
10113 switch (extended_op)
10114 {
10115 case DW_LNE_end_sequence:
10116 end_sequence = 1;
c906108c
SS
10117 break;
10118 case DW_LNE_set_address:
e7c27a73 10119 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10120 op_index = 0;
107d2387
AC
10121 line_ptr += bytes_read;
10122 address += baseaddr;
c906108c
SS
10123 break;
10124 case DW_LNE_define_file:
debd256d
JB
10125 {
10126 char *cur_file;
10127 unsigned int dir_index, mod_time, length;
6e70227d 10128
9b1c24c8 10129 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10130 line_ptr += bytes_read;
10131 dir_index =
10132 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10133 line_ptr += bytes_read;
10134 mod_time =
10135 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10136 line_ptr += bytes_read;
10137 length =
10138 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10139 line_ptr += bytes_read;
10140 add_file_name (lh, cur_file, dir_index, mod_time, length);
10141 }
c906108c 10142 break;
d0c6ba3d
CC
10143 case DW_LNE_set_discriminator:
10144 /* The discriminator is not interesting to the debugger;
10145 just ignore it. */
10146 line_ptr = extended_end;
10147 break;
c906108c 10148 default:
4d3c2250 10149 complaint (&symfile_complaints,
e2e0b3e5 10150 _("mangled .debug_line section"));
debd256d 10151 return;
c906108c 10152 }
a8c50c1f
DJ
10153 /* Make sure that we parsed the extended op correctly. If e.g.
10154 we expected a different address size than the producer used,
10155 we may have read the wrong number of bytes. */
10156 if (line_ptr != extended_end)
10157 {
10158 complaint (&symfile_complaints,
10159 _("mangled .debug_line section"));
10160 return;
10161 }
c906108c
SS
10162 break;
10163 case DW_LNS_copy:
59205f5a 10164 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10165 dwarf2_debug_line_missing_file_complaint ();
10166 else
366da635 10167 {
25e43795 10168 lh->file_names[file - 1].included_p = 1;
ca5f395d 10169 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10170 {
10171 if (last_subfile != current_subfile)
10172 {
10173 addr = gdbarch_addr_bits_remove (gdbarch, address);
10174 if (last_subfile)
10175 record_line (last_subfile, 0, addr);
10176 last_subfile = current_subfile;
10177 }
10178 addr = check_cu_functions (address, cu);
10179 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10180 record_line (current_subfile, line, addr);
10181 }
366da635 10182 }
c906108c
SS
10183 basic_block = 0;
10184 break;
10185 case DW_LNS_advance_pc:
2dc7f7b3
TT
10186 {
10187 CORE_ADDR adjust
10188 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10189
10190 address += (((op_index + adjust)
10191 / lh->maximum_ops_per_instruction)
10192 * lh->minimum_instruction_length);
10193 op_index = ((op_index + adjust)
10194 % lh->maximum_ops_per_instruction);
10195 line_ptr += bytes_read;
10196 }
c906108c
SS
10197 break;
10198 case DW_LNS_advance_line:
10199 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10200 line_ptr += bytes_read;
10201 break;
10202 case DW_LNS_set_file:
debd256d 10203 {
a738430d
MK
10204 /* The arrays lh->include_dirs and lh->file_names are
10205 0-based, but the directory and file name numbers in
10206 the statement program are 1-based. */
debd256d 10207 struct file_entry *fe;
4f1520fb 10208 char *dir = NULL;
a738430d 10209
debd256d
JB
10210 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10211 line_ptr += bytes_read;
59205f5a 10212 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10213 dwarf2_debug_line_missing_file_complaint ();
10214 else
10215 {
10216 fe = &lh->file_names[file - 1];
10217 if (fe->dir_index)
10218 dir = lh->include_dirs[fe->dir_index - 1];
10219 if (!decode_for_pst_p)
10220 {
10221 last_subfile = current_subfile;
10222 dwarf2_start_subfile (fe->name, dir, comp_dir);
10223 }
10224 }
debd256d 10225 }
c906108c
SS
10226 break;
10227 case DW_LNS_set_column:
10228 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10229 line_ptr += bytes_read;
10230 break;
10231 case DW_LNS_negate_stmt:
10232 is_stmt = (!is_stmt);
10233 break;
10234 case DW_LNS_set_basic_block:
10235 basic_block = 1;
10236 break;
c2c6d25f
JM
10237 /* Add to the address register of the state machine the
10238 address increment value corresponding to special opcode
a738430d
MK
10239 255. I.e., this value is scaled by the minimum
10240 instruction length since special opcode 255 would have
10241 scaled the the increment. */
c906108c 10242 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10243 {
10244 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10245
10246 address += (((op_index + adjust)
10247 / lh->maximum_ops_per_instruction)
10248 * lh->minimum_instruction_length);
10249 op_index = ((op_index + adjust)
10250 % lh->maximum_ops_per_instruction);
10251 }
c906108c
SS
10252 break;
10253 case DW_LNS_fixed_advance_pc:
10254 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10255 op_index = 0;
c906108c
SS
10256 line_ptr += 2;
10257 break;
9aa1fe7e 10258 default:
a738430d
MK
10259 {
10260 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10261 int i;
a738430d 10262
debd256d 10263 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10264 {
10265 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10266 line_ptr += bytes_read;
10267 }
10268 }
c906108c
SS
10269 }
10270 }
59205f5a
JB
10271 if (lh->num_file_names < file || file == 0)
10272 dwarf2_debug_line_missing_file_complaint ();
10273 else
10274 {
10275 lh->file_names[file - 1].included_p = 1;
10276 if (!decode_for_pst_p)
fbf65064
UW
10277 {
10278 addr = gdbarch_addr_bits_remove (gdbarch, address);
10279 record_line (current_subfile, 0, addr);
10280 }
59205f5a 10281 }
c906108c 10282 }
aaa75496
JB
10283
10284 if (decode_for_pst_p)
10285 {
10286 int file_index;
10287
10288 /* Now that we're done scanning the Line Header Program, we can
10289 create the psymtab of each included file. */
10290 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10291 if (lh->file_names[file_index].included_p == 1)
10292 {
c6da4cef
DE
10293 char *include_name =
10294 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10295 if (include_name != NULL)
aaa75496
JB
10296 dwarf2_create_include_psymtab (include_name, pst, objfile);
10297 }
10298 }
cb1df416
DJ
10299 else
10300 {
10301 /* Make sure a symtab is created for every file, even files
10302 which contain only variables (i.e. no code with associated
10303 line numbers). */
10304
10305 int i;
10306 struct file_entry *fe;
10307
10308 for (i = 0; i < lh->num_file_names; i++)
10309 {
10310 char *dir = NULL;
9a619af0 10311
cb1df416
DJ
10312 fe = &lh->file_names[i];
10313 if (fe->dir_index)
10314 dir = lh->include_dirs[fe->dir_index - 1];
10315 dwarf2_start_subfile (fe->name, dir, comp_dir);
10316
10317 /* Skip the main file; we don't need it, and it must be
10318 allocated last, so that it will show up before the
10319 non-primary symtabs in the objfile's symtab list. */
10320 if (current_subfile == first_subfile)
10321 continue;
10322
10323 if (current_subfile->symtab == NULL)
10324 current_subfile->symtab = allocate_symtab (current_subfile->name,
10325 cu->objfile);
10326 fe->symtab = current_subfile->symtab;
10327 }
10328 }
c906108c
SS
10329}
10330
10331/* Start a subfile for DWARF. FILENAME is the name of the file and
10332 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10333 or NULL if not known. COMP_DIR is the compilation directory for the
10334 linetable's compilation unit or NULL if not known.
c906108c
SS
10335 This routine tries to keep line numbers from identical absolute and
10336 relative file names in a common subfile.
10337
10338 Using the `list' example from the GDB testsuite, which resides in
10339 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10340 of /srcdir/list0.c yields the following debugging information for list0.c:
10341
c5aa993b
JM
10342 DW_AT_name: /srcdir/list0.c
10343 DW_AT_comp_dir: /compdir
357e46e7 10344 files.files[0].name: list0.h
c5aa993b 10345 files.files[0].dir: /srcdir
357e46e7 10346 files.files[1].name: list0.c
c5aa993b 10347 files.files[1].dir: /srcdir
c906108c
SS
10348
10349 The line number information for list0.c has to end up in a single
4f1520fb
FR
10350 subfile, so that `break /srcdir/list0.c:1' works as expected.
10351 start_subfile will ensure that this happens provided that we pass the
10352 concatenation of files.files[1].dir and files.files[1].name as the
10353 subfile's name. */
c906108c
SS
10354
10355static void
4f1520fb 10356dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10357{
4f1520fb
FR
10358 char *fullname;
10359
10360 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10361 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10362 second argument to start_subfile. To be consistent, we do the
10363 same here. In order not to lose the line information directory,
10364 we concatenate it to the filename when it makes sense.
10365 Note that the Dwarf3 standard says (speaking of filenames in line
10366 information): ``The directory index is ignored for file names
10367 that represent full path names''. Thus ignoring dirname in the
10368 `else' branch below isn't an issue. */
c906108c 10369
d5166ae1 10370 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10371 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10372 else
10373 fullname = filename;
c906108c 10374
4f1520fb
FR
10375 start_subfile (fullname, comp_dir);
10376
10377 if (fullname != filename)
10378 xfree (fullname);
c906108c
SS
10379}
10380
4c2df51b
DJ
10381static void
10382var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10383 struct dwarf2_cu *cu)
4c2df51b 10384{
e7c27a73
DJ
10385 struct objfile *objfile = cu->objfile;
10386 struct comp_unit_head *cu_header = &cu->header;
10387
4c2df51b
DJ
10388 /* NOTE drow/2003-01-30: There used to be a comment and some special
10389 code here to turn a symbol with DW_AT_external and a
10390 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10391 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10392 with some versions of binutils) where shared libraries could have
10393 relocations against symbols in their debug information - the
10394 minimal symbol would have the right address, but the debug info
10395 would not. It's no longer necessary, because we will explicitly
10396 apply relocations when we read in the debug information now. */
10397
10398 /* A DW_AT_location attribute with no contents indicates that a
10399 variable has been optimized away. */
10400 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10401 {
10402 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10403 return;
10404 }
10405
10406 /* Handle one degenerate form of location expression specially, to
10407 preserve GDB's previous behavior when section offsets are
10408 specified. If this is just a DW_OP_addr then mark this symbol
10409 as LOC_STATIC. */
10410
10411 if (attr_form_is_block (attr)
10412 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10413 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10414 {
891d2f0b 10415 unsigned int dummy;
4c2df51b
DJ
10416
10417 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10418 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10419 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10420 fixup_symbol_section (sym, objfile);
10421 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10422 SYMBOL_SECTION (sym));
4c2df51b
DJ
10423 return;
10424 }
10425
10426 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10427 expression evaluator, and use LOC_COMPUTED only when necessary
10428 (i.e. when the value of a register or memory location is
10429 referenced, or a thread-local block, etc.). Then again, it might
10430 not be worthwhile. I'm assuming that it isn't unless performance
10431 or memory numbers show me otherwise. */
10432
e7c27a73 10433 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10434 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10435}
10436
c906108c
SS
10437/* Given a pointer to a DWARF information entry, figure out if we need
10438 to make a symbol table entry for it, and if so, create a new entry
10439 and return a pointer to it.
10440 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10441 used the passed type.
10442 If SPACE is not NULL, use it to hold the new symbol. If it is
10443 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10444
10445static struct symbol *
34eaf542
TT
10446new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10447 struct symbol *space)
c906108c 10448{
e7c27a73 10449 struct objfile *objfile = cu->objfile;
c906108c
SS
10450 struct symbol *sym = NULL;
10451 char *name;
10452 struct attribute *attr = NULL;
10453 struct attribute *attr2 = NULL;
e142c38c 10454 CORE_ADDR baseaddr;
e37fd15a
SW
10455 struct pending **list_to_add = NULL;
10456
edb3359d 10457 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10458
10459 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10460
94af9270 10461 name = dwarf2_name (die, cu);
c906108c
SS
10462 if (name)
10463 {
94af9270 10464 const char *linkagename;
34eaf542 10465 int suppress_add = 0;
94af9270 10466
34eaf542
TT
10467 if (space)
10468 sym = space;
10469 else
10470 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10471 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10472
10473 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10474 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10475 linkagename = dwarf2_physname (name, die, cu);
10476 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10477
f55ee35c
JK
10478 /* Fortran does not have mangling standard and the mangling does differ
10479 between gfortran, iFort etc. */
10480 if (cu->language == language_fortran
b250c185 10481 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10482 symbol_set_demangled_name (&(sym->ginfo),
10483 (char *) dwarf2_full_name (name, die, cu),
10484 NULL);
f55ee35c 10485
c906108c 10486 /* Default assumptions.
c5aa993b 10487 Use the passed type or decode it from the die. */
176620f1 10488 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10489 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10490 if (type != NULL)
10491 SYMBOL_TYPE (sym) = type;
10492 else
e7c27a73 10493 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10494 attr = dwarf2_attr (die,
10495 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10496 cu);
c906108c
SS
10497 if (attr)
10498 {
10499 SYMBOL_LINE (sym) = DW_UNSND (attr);
10500 }
cb1df416 10501
edb3359d
DJ
10502 attr = dwarf2_attr (die,
10503 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10504 cu);
cb1df416
DJ
10505 if (attr)
10506 {
10507 int file_index = DW_UNSND (attr);
9a619af0 10508
cb1df416
DJ
10509 if (cu->line_header == NULL
10510 || file_index > cu->line_header->num_file_names)
10511 complaint (&symfile_complaints,
10512 _("file index out of range"));
1c3d648d 10513 else if (file_index > 0)
cb1df416
DJ
10514 {
10515 struct file_entry *fe;
9a619af0 10516
cb1df416
DJ
10517 fe = &cu->line_header->file_names[file_index - 1];
10518 SYMBOL_SYMTAB (sym) = fe->symtab;
10519 }
10520 }
10521
c906108c
SS
10522 switch (die->tag)
10523 {
10524 case DW_TAG_label:
e142c38c 10525 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10526 if (attr)
10527 {
10528 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10529 }
0f5238ed
TT
10530 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10531 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10532 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10533 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10534 break;
10535 case DW_TAG_subprogram:
10536 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10537 finish_block. */
10538 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10539 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10540 if ((attr2 && (DW_UNSND (attr2) != 0))
10541 || cu->language == language_ada)
c906108c 10542 {
2cfa0c8d
JB
10543 /* Subprograms marked external are stored as a global symbol.
10544 Ada subprograms, whether marked external or not, are always
10545 stored as a global symbol, because we want to be able to
10546 access them globally. For instance, we want to be able
10547 to break on a nested subprogram without having to
10548 specify the context. */
e37fd15a 10549 list_to_add = &global_symbols;
c906108c
SS
10550 }
10551 else
10552 {
e37fd15a 10553 list_to_add = cu->list_in_scope;
c906108c
SS
10554 }
10555 break;
edb3359d
DJ
10556 case DW_TAG_inlined_subroutine:
10557 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10558 finish_block. */
10559 SYMBOL_CLASS (sym) = LOC_BLOCK;
10560 SYMBOL_INLINED (sym) = 1;
10561 /* Do not add the symbol to any lists. It will be found via
10562 BLOCK_FUNCTION from the blockvector. */
10563 break;
34eaf542
TT
10564 case DW_TAG_template_value_param:
10565 suppress_add = 1;
10566 /* Fall through. */
c906108c 10567 case DW_TAG_variable:
254e6b9e 10568 case DW_TAG_member:
c906108c
SS
10569 /* Compilation with minimal debug info may result in variables
10570 with missing type entries. Change the misleading `void' type
10571 to something sensible. */
10572 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10573 SYMBOL_TYPE (sym)
46bf5051 10574 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10575
e142c38c 10576 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10577 /* In the case of DW_TAG_member, we should only be called for
10578 static const members. */
10579 if (die->tag == DW_TAG_member)
10580 {
3863f96c
DE
10581 /* dwarf2_add_field uses die_is_declaration,
10582 so we do the same. */
254e6b9e
DE
10583 gdb_assert (die_is_declaration (die, cu));
10584 gdb_assert (attr);
10585 }
c906108c
SS
10586 if (attr)
10587 {
e7c27a73 10588 dwarf2_const_value (attr, sym, cu);
e142c38c 10589 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10590 if (!suppress_add)
34eaf542
TT
10591 {
10592 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10593 list_to_add = &global_symbols;
34eaf542 10594 else
e37fd15a 10595 list_to_add = cu->list_in_scope;
34eaf542 10596 }
c906108c
SS
10597 break;
10598 }
e142c38c 10599 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10600 if (attr)
10601 {
e7c27a73 10602 var_decode_location (attr, sym, cu);
e142c38c 10603 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10604 if (SYMBOL_CLASS (sym) == LOC_STATIC
10605 && SYMBOL_VALUE_ADDRESS (sym) == 0
10606 && !dwarf2_per_objfile->has_section_at_zero)
10607 {
10608 /* When a static variable is eliminated by the linker,
10609 the corresponding debug information is not stripped
10610 out, but the variable address is set to null;
10611 do not add such variables into symbol table. */
10612 }
10613 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10614 {
f55ee35c
JK
10615 /* Workaround gfortran PR debug/40040 - it uses
10616 DW_AT_location for variables in -fPIC libraries which may
10617 get overriden by other libraries/executable and get
10618 a different address. Resolve it by the minimal symbol
10619 which may come from inferior's executable using copy
10620 relocation. Make this workaround only for gfortran as for
10621 other compilers GDB cannot guess the minimal symbol
10622 Fortran mangling kind. */
10623 if (cu->language == language_fortran && die->parent
10624 && die->parent->tag == DW_TAG_module
10625 && cu->producer
10626 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10627 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10628
1c809c68
TT
10629 /* A variable with DW_AT_external is never static,
10630 but it may be block-scoped. */
10631 list_to_add = (cu->list_in_scope == &file_symbols
10632 ? &global_symbols : cu->list_in_scope);
1c809c68 10633 }
c906108c 10634 else
e37fd15a 10635 list_to_add = cu->list_in_scope;
c906108c
SS
10636 }
10637 else
10638 {
10639 /* We do not know the address of this symbol.
c5aa993b
JM
10640 If it is an external symbol and we have type information
10641 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10642 The address of the variable will then be determined from
10643 the minimal symbol table whenever the variable is
10644 referenced. */
e142c38c 10645 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10646 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10647 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10648 {
0fe7935b
DJ
10649 /* A variable with DW_AT_external is never static, but it
10650 may be block-scoped. */
10651 list_to_add = (cu->list_in_scope == &file_symbols
10652 ? &global_symbols : cu->list_in_scope);
10653
c906108c 10654 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10655 }
442ddf59
JK
10656 else if (!die_is_declaration (die, cu))
10657 {
10658 /* Use the default LOC_OPTIMIZED_OUT class. */
10659 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10660 if (!suppress_add)
10661 list_to_add = cu->list_in_scope;
442ddf59 10662 }
c906108c
SS
10663 }
10664 break;
10665 case DW_TAG_formal_parameter:
edb3359d
DJ
10666 /* If we are inside a function, mark this as an argument. If
10667 not, we might be looking at an argument to an inlined function
10668 when we do not have enough information to show inlined frames;
10669 pretend it's a local variable in that case so that the user can
10670 still see it. */
10671 if (context_stack_depth > 0
10672 && context_stack[context_stack_depth - 1].name != NULL)
10673 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10674 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10675 if (attr)
10676 {
e7c27a73 10677 var_decode_location (attr, sym, cu);
c906108c 10678 }
e142c38c 10679 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10680 if (attr)
10681 {
e7c27a73 10682 dwarf2_const_value (attr, sym, cu);
c906108c 10683 }
f346a30d
PM
10684 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10685 if (attr && DW_UNSND (attr))
10686 {
10687 struct type *ref_type;
10688
10689 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10690 SYMBOL_TYPE (sym) = ref_type;
10691 }
10692
e37fd15a 10693 list_to_add = cu->list_in_scope;
c906108c
SS
10694 break;
10695 case DW_TAG_unspecified_parameters:
10696 /* From varargs functions; gdb doesn't seem to have any
10697 interest in this information, so just ignore it for now.
10698 (FIXME?) */
10699 break;
34eaf542
TT
10700 case DW_TAG_template_type_param:
10701 suppress_add = 1;
10702 /* Fall through. */
c906108c 10703 case DW_TAG_class_type:
680b30c7 10704 case DW_TAG_interface_type:
c906108c
SS
10705 case DW_TAG_structure_type:
10706 case DW_TAG_union_type:
72019c9c 10707 case DW_TAG_set_type:
c906108c
SS
10708 case DW_TAG_enumeration_type:
10709 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10710 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10711
63d06c5c 10712 {
987504bb 10713 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10714 really ever be static objects: otherwise, if you try
10715 to, say, break of a class's method and you're in a file
10716 which doesn't mention that class, it won't work unless
10717 the check for all static symbols in lookup_symbol_aux
10718 saves you. See the OtherFileClass tests in
10719 gdb.c++/namespace.exp. */
10720
e37fd15a 10721 if (!suppress_add)
34eaf542 10722 {
34eaf542
TT
10723 list_to_add = (cu->list_in_scope == &file_symbols
10724 && (cu->language == language_cplus
10725 || cu->language == language_java)
10726 ? &global_symbols : cu->list_in_scope);
34eaf542 10727 }
63d06c5c
DC
10728
10729 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 10730 defines a typedef for "foo". A Java class declaration also
5eeb2539 10731 defines a typedef for the class. */
987504bb 10732 if (cu->language == language_cplus
8c6860bb
JB
10733 || cu->language == language_java
10734 || cu->language == language_ada)
63d06c5c 10735 {
d8151005
DJ
10736 /* The symbol's name is already allocated along with
10737 this objfile, so we don't need to duplicate it for
10738 the type. */
63d06c5c 10739 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 10740 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
10741 }
10742 }
c906108c
SS
10743 break;
10744 case DW_TAG_typedef:
63d06c5c
DC
10745 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10746 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10747 list_to_add = cu->list_in_scope;
63d06c5c 10748 break;
c906108c 10749 case DW_TAG_base_type:
a02abb62 10750 case DW_TAG_subrange_type:
c906108c 10751 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10752 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10753 list_to_add = cu->list_in_scope;
c906108c
SS
10754 break;
10755 case DW_TAG_enumerator:
e142c38c 10756 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10757 if (attr)
10758 {
e7c27a73 10759 dwarf2_const_value (attr, sym, cu);
c906108c 10760 }
63d06c5c
DC
10761 {
10762 /* NOTE: carlton/2003-11-10: See comment above in the
10763 DW_TAG_class_type, etc. block. */
10764
e142c38c 10765 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10766 && (cu->language == language_cplus
10767 || cu->language == language_java)
e142c38c 10768 ? &global_symbols : cu->list_in_scope);
63d06c5c 10769 }
c906108c 10770 break;
5c4e30ca
DC
10771 case DW_TAG_namespace:
10772 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10773 list_to_add = &global_symbols;
5c4e30ca 10774 break;
c906108c
SS
10775 default:
10776 /* Not a tag we recognize. Hopefully we aren't processing
10777 trash data, but since we must specifically ignore things
10778 we don't recognize, there is nothing else we should do at
10779 this point. */
e2e0b3e5 10780 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10781 dwarf_tag_name (die->tag));
c906108c
SS
10782 break;
10783 }
df8a16a1 10784
e37fd15a
SW
10785 if (suppress_add)
10786 {
10787 sym->hash_next = objfile->template_symbols;
10788 objfile->template_symbols = sym;
10789 list_to_add = NULL;
10790 }
10791
10792 if (list_to_add != NULL)
10793 add_symbol_to_list (sym, list_to_add);
10794
df8a16a1
DJ
10795 /* For the benefit of old versions of GCC, check for anonymous
10796 namespaces based on the demangled name. */
10797 if (!processing_has_namespace_info
94af9270 10798 && cu->language == language_cplus)
df8a16a1 10799 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10800 }
10801 return (sym);
10802}
10803
34eaf542
TT
10804/* A wrapper for new_symbol_full that always allocates a new symbol. */
10805
10806static struct symbol *
10807new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10808{
10809 return new_symbol_full (die, type, cu, NULL);
10810}
10811
98bfdba5
PA
10812/* Given an attr with a DW_FORM_dataN value in host byte order,
10813 zero-extend it as appropriate for the symbol's type. The DWARF
10814 standard (v4) is not entirely clear about the meaning of using
10815 DW_FORM_dataN for a constant with a signed type, where the type is
10816 wider than the data. The conclusion of a discussion on the DWARF
10817 list was that this is unspecified. We choose to always zero-extend
10818 because that is the interpretation long in use by GCC. */
c906108c 10819
98bfdba5
PA
10820static gdb_byte *
10821dwarf2_const_value_data (struct attribute *attr, struct type *type,
10822 const char *name, struct obstack *obstack,
10823 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10824{
e7c27a73 10825 struct objfile *objfile = cu->objfile;
e17a4113
UW
10826 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10827 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10828 LONGEST l = DW_UNSND (attr);
10829
10830 if (bits < sizeof (*value) * 8)
10831 {
10832 l &= ((LONGEST) 1 << bits) - 1;
10833 *value = l;
10834 }
10835 else if (bits == sizeof (*value) * 8)
10836 *value = l;
10837 else
10838 {
10839 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10840 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10841 return bytes;
10842 }
10843
10844 return NULL;
10845}
10846
10847/* Read a constant value from an attribute. Either set *VALUE, or if
10848 the value does not fit in *VALUE, set *BYTES - either already
10849 allocated on the objfile obstack, or newly allocated on OBSTACK,
10850 or, set *BATON, if we translated the constant to a location
10851 expression. */
10852
10853static void
10854dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10855 const char *name, struct obstack *obstack,
10856 struct dwarf2_cu *cu,
10857 long *value, gdb_byte **bytes,
10858 struct dwarf2_locexpr_baton **baton)
10859{
10860 struct objfile *objfile = cu->objfile;
10861 struct comp_unit_head *cu_header = &cu->header;
c906108c 10862 struct dwarf_block *blk;
98bfdba5
PA
10863 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10864 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10865
10866 *value = 0;
10867 *bytes = NULL;
10868 *baton = NULL;
c906108c
SS
10869
10870 switch (attr->form)
10871 {
10872 case DW_FORM_addr:
ac56253d 10873 {
ac56253d
TT
10874 gdb_byte *data;
10875
98bfdba5
PA
10876 if (TYPE_LENGTH (type) != cu_header->addr_size)
10877 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10878 cu_header->addr_size,
98bfdba5 10879 TYPE_LENGTH (type));
ac56253d
TT
10880 /* Symbols of this form are reasonably rare, so we just
10881 piggyback on the existing location code rather than writing
10882 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10883 *baton = obstack_alloc (&objfile->objfile_obstack,
10884 sizeof (struct dwarf2_locexpr_baton));
10885 (*baton)->per_cu = cu->per_cu;
10886 gdb_assert ((*baton)->per_cu);
ac56253d 10887
98bfdba5
PA
10888 (*baton)->size = 2 + cu_header->addr_size;
10889 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10890 (*baton)->data = data;
ac56253d
TT
10891
10892 data[0] = DW_OP_addr;
10893 store_unsigned_integer (&data[1], cu_header->addr_size,
10894 byte_order, DW_ADDR (attr));
10895 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10896 }
c906108c 10897 break;
4ac36638 10898 case DW_FORM_string:
93b5768b 10899 case DW_FORM_strp:
98bfdba5
PA
10900 /* DW_STRING is already allocated on the objfile obstack, point
10901 directly to it. */
10902 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10903 break;
c906108c
SS
10904 case DW_FORM_block1:
10905 case DW_FORM_block2:
10906 case DW_FORM_block4:
10907 case DW_FORM_block:
2dc7f7b3 10908 case DW_FORM_exprloc:
c906108c 10909 blk = DW_BLOCK (attr);
98bfdba5
PA
10910 if (TYPE_LENGTH (type) != blk->size)
10911 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10912 TYPE_LENGTH (type));
10913 *bytes = blk->data;
c906108c 10914 break;
2df3850c
JM
10915
10916 /* The DW_AT_const_value attributes are supposed to carry the
10917 symbol's value "represented as it would be on the target
10918 architecture." By the time we get here, it's already been
10919 converted to host endianness, so we just need to sign- or
10920 zero-extend it as appropriate. */
10921 case DW_FORM_data1:
98bfdba5 10922 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10923 break;
c906108c 10924 case DW_FORM_data2:
98bfdba5 10925 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10926 break;
c906108c 10927 case DW_FORM_data4:
98bfdba5 10928 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10929 break;
c906108c 10930 case DW_FORM_data8:
98bfdba5 10931 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
10932 break;
10933
c906108c 10934 case DW_FORM_sdata:
98bfdba5 10935 *value = DW_SND (attr);
2df3850c
JM
10936 break;
10937
c906108c 10938 case DW_FORM_udata:
98bfdba5 10939 *value = DW_UNSND (attr);
c906108c 10940 break;
2df3850c 10941
c906108c 10942 default:
4d3c2250 10943 complaint (&symfile_complaints,
e2e0b3e5 10944 _("unsupported const value attribute form: '%s'"),
4d3c2250 10945 dwarf_form_name (attr->form));
98bfdba5 10946 *value = 0;
c906108c
SS
10947 break;
10948 }
10949}
10950
2df3850c 10951
98bfdba5
PA
10952/* Copy constant value from an attribute to a symbol. */
10953
2df3850c 10954static void
98bfdba5
PA
10955dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10956 struct dwarf2_cu *cu)
2df3850c 10957{
98bfdba5
PA
10958 struct objfile *objfile = cu->objfile;
10959 struct comp_unit_head *cu_header = &cu->header;
10960 long value;
10961 gdb_byte *bytes;
10962 struct dwarf2_locexpr_baton *baton;
2df3850c 10963
98bfdba5
PA
10964 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10965 SYMBOL_PRINT_NAME (sym),
10966 &objfile->objfile_obstack, cu,
10967 &value, &bytes, &baton);
2df3850c 10968
98bfdba5
PA
10969 if (baton != NULL)
10970 {
10971 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10972 SYMBOL_LOCATION_BATON (sym) = baton;
10973 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10974 }
10975 else if (bytes != NULL)
10976 {
10977 SYMBOL_VALUE_BYTES (sym) = bytes;
10978 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10979 }
10980 else
10981 {
10982 SYMBOL_VALUE (sym) = value;
10983 SYMBOL_CLASS (sym) = LOC_CONST;
10984 }
2df3850c
JM
10985}
10986
c906108c
SS
10987/* Return the type of the die in question using its DW_AT_type attribute. */
10988
10989static struct type *
e7c27a73 10990die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10991{
c906108c 10992 struct attribute *type_attr;
c906108c 10993
e142c38c 10994 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10995 if (!type_attr)
10996 {
10997 /* A missing DW_AT_type represents a void type. */
46bf5051 10998 return objfile_type (cu->objfile)->builtin_void;
c906108c 10999 }
348e048f 11000
673bfd45 11001 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11002}
11003
b4ba55a1
JB
11004/* True iff CU's producer generates GNAT Ada auxiliary information
11005 that allows to find parallel types through that information instead
11006 of having to do expensive parallel lookups by type name. */
11007
11008static int
11009need_gnat_info (struct dwarf2_cu *cu)
11010{
11011 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11012 of GNAT produces this auxiliary information, without any indication
11013 that it is produced. Part of enhancing the FSF version of GNAT
11014 to produce that information will be to put in place an indicator
11015 that we can use in order to determine whether the descriptive type
11016 info is available or not. One suggestion that has been made is
11017 to use a new attribute, attached to the CU die. For now, assume
11018 that the descriptive type info is not available. */
11019 return 0;
11020}
11021
b4ba55a1
JB
11022/* Return the auxiliary type of the die in question using its
11023 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11024 attribute is not present. */
11025
11026static struct type *
11027die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11028{
b4ba55a1 11029 struct attribute *type_attr;
b4ba55a1
JB
11030
11031 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11032 if (!type_attr)
11033 return NULL;
11034
673bfd45 11035 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11036}
11037
11038/* If DIE has a descriptive_type attribute, then set the TYPE's
11039 descriptive type accordingly. */
11040
11041static void
11042set_descriptive_type (struct type *type, struct die_info *die,
11043 struct dwarf2_cu *cu)
11044{
11045 struct type *descriptive_type = die_descriptive_type (die, cu);
11046
11047 if (descriptive_type)
11048 {
11049 ALLOCATE_GNAT_AUX_TYPE (type);
11050 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11051 }
11052}
11053
c906108c
SS
11054/* Return the containing type of the die in question using its
11055 DW_AT_containing_type attribute. */
11056
11057static struct type *
e7c27a73 11058die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11059{
c906108c 11060 struct attribute *type_attr;
c906108c 11061
e142c38c 11062 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11063 if (!type_attr)
11064 error (_("Dwarf Error: Problem turning containing type into gdb type "
11065 "[in module %s]"), cu->objfile->name);
11066
673bfd45 11067 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11068}
11069
673bfd45
DE
11070/* Look up the type of DIE in CU using its type attribute ATTR.
11071 If there is no type substitute an error marker. */
11072
c906108c 11073static struct type *
673bfd45
DE
11074lookup_die_type (struct die_info *die, struct attribute *attr,
11075 struct dwarf2_cu *cu)
c906108c 11076{
f792889a
DJ
11077 struct type *this_type;
11078
673bfd45
DE
11079 /* First see if we have it cached. */
11080
11081 if (is_ref_attr (attr))
11082 {
11083 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11084
11085 this_type = get_die_type_at_offset (offset, cu->per_cu);
11086 }
11087 else if (attr->form == DW_FORM_sig8)
11088 {
11089 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11090 struct dwarf2_cu *sig_cu;
11091 unsigned int offset;
11092
11093 /* sig_type will be NULL if the signatured type is missing from
11094 the debug info. */
11095 if (sig_type == NULL)
11096 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11097 "at 0x%x [in module %s]"),
11098 die->offset, cu->objfile->name);
11099
11100 gdb_assert (sig_type->per_cu.from_debug_types);
11101 offset = sig_type->offset + sig_type->type_offset;
11102 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11103 }
11104 else
11105 {
11106 dump_die_for_error (die);
11107 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11108 dwarf_attr_name (attr->name), cu->objfile->name);
11109 }
11110
11111 /* If not cached we need to read it in. */
11112
11113 if (this_type == NULL)
11114 {
11115 struct die_info *type_die;
11116 struct dwarf2_cu *type_cu = cu;
11117
11118 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11119 /* If the type is cached, we should have found it above. */
11120 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11121 this_type = read_type_die_1 (type_die, type_cu);
11122 }
11123
11124 /* If we still don't have a type use an error marker. */
11125
11126 if (this_type == NULL)
c906108c 11127 {
b00fdb78
TT
11128 char *message, *saved;
11129
11130 /* read_type_die already issued a complaint. */
11131 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11132 cu->objfile->name,
11133 cu->header.offset,
11134 die->offset);
11135 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11136 message, strlen (message));
11137 xfree (message);
11138
11139 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11140 }
673bfd45 11141
f792889a 11142 return this_type;
c906108c
SS
11143}
11144
673bfd45
DE
11145/* Return the type in DIE, CU.
11146 Returns NULL for invalid types.
11147
11148 This first does a lookup in the appropriate type_hash table,
11149 and only reads the die in if necessary.
11150
11151 NOTE: This can be called when reading in partial or full symbols. */
11152
f792889a 11153static struct type *
e7c27a73 11154read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11155{
f792889a
DJ
11156 struct type *this_type;
11157
11158 this_type = get_die_type (die, cu);
11159 if (this_type)
11160 return this_type;
11161
673bfd45
DE
11162 return read_type_die_1 (die, cu);
11163}
11164
11165/* Read the type in DIE, CU.
11166 Returns NULL for invalid types. */
11167
11168static struct type *
11169read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11170{
11171 struct type *this_type = NULL;
11172
c906108c
SS
11173 switch (die->tag)
11174 {
11175 case DW_TAG_class_type:
680b30c7 11176 case DW_TAG_interface_type:
c906108c
SS
11177 case DW_TAG_structure_type:
11178 case DW_TAG_union_type:
f792889a 11179 this_type = read_structure_type (die, cu);
c906108c
SS
11180 break;
11181 case DW_TAG_enumeration_type:
f792889a 11182 this_type = read_enumeration_type (die, cu);
c906108c
SS
11183 break;
11184 case DW_TAG_subprogram:
11185 case DW_TAG_subroutine_type:
edb3359d 11186 case DW_TAG_inlined_subroutine:
f792889a 11187 this_type = read_subroutine_type (die, cu);
c906108c
SS
11188 break;
11189 case DW_TAG_array_type:
f792889a 11190 this_type = read_array_type (die, cu);
c906108c 11191 break;
72019c9c 11192 case DW_TAG_set_type:
f792889a 11193 this_type = read_set_type (die, cu);
72019c9c 11194 break;
c906108c 11195 case DW_TAG_pointer_type:
f792889a 11196 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11197 break;
11198 case DW_TAG_ptr_to_member_type:
f792889a 11199 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11200 break;
11201 case DW_TAG_reference_type:
f792889a 11202 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11203 break;
11204 case DW_TAG_const_type:
f792889a 11205 this_type = read_tag_const_type (die, cu);
c906108c
SS
11206 break;
11207 case DW_TAG_volatile_type:
f792889a 11208 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11209 break;
11210 case DW_TAG_string_type:
f792889a 11211 this_type = read_tag_string_type (die, cu);
c906108c
SS
11212 break;
11213 case DW_TAG_typedef:
f792889a 11214 this_type = read_typedef (die, cu);
c906108c 11215 break;
a02abb62 11216 case DW_TAG_subrange_type:
f792889a 11217 this_type = read_subrange_type (die, cu);
a02abb62 11218 break;
c906108c 11219 case DW_TAG_base_type:
f792889a 11220 this_type = read_base_type (die, cu);
c906108c 11221 break;
81a17f79 11222 case DW_TAG_unspecified_type:
f792889a 11223 this_type = read_unspecified_type (die, cu);
81a17f79 11224 break;
0114d602
DJ
11225 case DW_TAG_namespace:
11226 this_type = read_namespace_type (die, cu);
11227 break;
f55ee35c
JK
11228 case DW_TAG_module:
11229 this_type = read_module_type (die, cu);
11230 break;
c906108c 11231 default:
a1f5b845 11232 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11233 dwarf_tag_name (die->tag));
c906108c
SS
11234 break;
11235 }
63d06c5c 11236
f792889a 11237 return this_type;
63d06c5c
DC
11238}
11239
fdde2d81 11240/* Return the name of the namespace/class that DIE is defined within,
0114d602 11241 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11242
0114d602
DJ
11243 For example, if we're within the method foo() in the following
11244 code:
11245
11246 namespace N {
11247 class C {
11248 void foo () {
11249 }
11250 };
11251 }
11252
11253 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11254
11255static char *
e142c38c 11256determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11257{
0114d602
DJ
11258 struct die_info *parent, *spec_die;
11259 struct dwarf2_cu *spec_cu;
11260 struct type *parent_type;
63d06c5c 11261
f55ee35c
JK
11262 if (cu->language != language_cplus && cu->language != language_java
11263 && cu->language != language_fortran)
0114d602
DJ
11264 return "";
11265
11266 /* We have to be careful in the presence of DW_AT_specification.
11267 For example, with GCC 3.4, given the code
11268
11269 namespace N {
11270 void foo() {
11271 // Definition of N::foo.
11272 }
11273 }
11274
11275 then we'll have a tree of DIEs like this:
11276
11277 1: DW_TAG_compile_unit
11278 2: DW_TAG_namespace // N
11279 3: DW_TAG_subprogram // declaration of N::foo
11280 4: DW_TAG_subprogram // definition of N::foo
11281 DW_AT_specification // refers to die #3
11282
11283 Thus, when processing die #4, we have to pretend that we're in
11284 the context of its DW_AT_specification, namely the contex of die
11285 #3. */
11286 spec_cu = cu;
11287 spec_die = die_specification (die, &spec_cu);
11288 if (spec_die == NULL)
11289 parent = die->parent;
11290 else
63d06c5c 11291 {
0114d602
DJ
11292 parent = spec_die->parent;
11293 cu = spec_cu;
63d06c5c 11294 }
0114d602
DJ
11295
11296 if (parent == NULL)
11297 return "";
98bfdba5
PA
11298 else if (parent->building_fullname)
11299 {
11300 const char *name;
11301 const char *parent_name;
11302
11303 /* It has been seen on RealView 2.2 built binaries,
11304 DW_TAG_template_type_param types actually _defined_ as
11305 children of the parent class:
11306
11307 enum E {};
11308 template class <class Enum> Class{};
11309 Class<enum E> class_e;
11310
11311 1: DW_TAG_class_type (Class)
11312 2: DW_TAG_enumeration_type (E)
11313 3: DW_TAG_enumerator (enum1:0)
11314 3: DW_TAG_enumerator (enum2:1)
11315 ...
11316 2: DW_TAG_template_type_param
11317 DW_AT_type DW_FORM_ref_udata (E)
11318
11319 Besides being broken debug info, it can put GDB into an
11320 infinite loop. Consider:
11321
11322 When we're building the full name for Class<E>, we'll start
11323 at Class, and go look over its template type parameters,
11324 finding E. We'll then try to build the full name of E, and
11325 reach here. We're now trying to build the full name of E,
11326 and look over the parent DIE for containing scope. In the
11327 broken case, if we followed the parent DIE of E, we'd again
11328 find Class, and once again go look at its template type
11329 arguments, etc., etc. Simply don't consider such parent die
11330 as source-level parent of this die (it can't be, the language
11331 doesn't allow it), and break the loop here. */
11332 name = dwarf2_name (die, cu);
11333 parent_name = dwarf2_name (parent, cu);
11334 complaint (&symfile_complaints,
11335 _("template param type '%s' defined within parent '%s'"),
11336 name ? name : "<unknown>",
11337 parent_name ? parent_name : "<unknown>");
11338 return "";
11339 }
63d06c5c 11340 else
0114d602
DJ
11341 switch (parent->tag)
11342 {
63d06c5c 11343 case DW_TAG_namespace:
0114d602 11344 parent_type = read_type_die (parent, cu);
acebe513
UW
11345 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11346 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11347 Work around this problem here. */
11348 if (cu->language == language_cplus
11349 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11350 return "";
0114d602
DJ
11351 /* We give a name to even anonymous namespaces. */
11352 return TYPE_TAG_NAME (parent_type);
63d06c5c 11353 case DW_TAG_class_type:
680b30c7 11354 case DW_TAG_interface_type:
63d06c5c 11355 case DW_TAG_structure_type:
0114d602 11356 case DW_TAG_union_type:
f55ee35c 11357 case DW_TAG_module:
0114d602
DJ
11358 parent_type = read_type_die (parent, cu);
11359 if (TYPE_TAG_NAME (parent_type) != NULL)
11360 return TYPE_TAG_NAME (parent_type);
11361 else
11362 /* An anonymous structure is only allowed non-static data
11363 members; no typedefs, no member functions, et cetera.
11364 So it does not need a prefix. */
11365 return "";
63d06c5c 11366 default:
8176b9b8 11367 return determine_prefix (parent, cu);
63d06c5c 11368 }
63d06c5c
DC
11369}
11370
987504bb
JJ
11371/* Return a newly-allocated string formed by concatenating PREFIX and
11372 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11373 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11374 perform an obconcat, otherwise allocate storage for the result. The CU argument
11375 is used to determine the language and hence, the appropriate separator. */
11376
f55ee35c 11377#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11378
11379static char *
f55ee35c
JK
11380typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11381 int physname, struct dwarf2_cu *cu)
63d06c5c 11382{
f55ee35c 11383 const char *lead = "";
5c315b68 11384 const char *sep;
63d06c5c 11385
987504bb
JJ
11386 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11387 sep = "";
11388 else if (cu->language == language_java)
11389 sep = ".";
f55ee35c
JK
11390 else if (cu->language == language_fortran && physname)
11391 {
11392 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11393 DW_AT_MIPS_linkage_name is preferred and used instead. */
11394
11395 lead = "__";
11396 sep = "_MOD_";
11397 }
987504bb
JJ
11398 else
11399 sep = "::";
63d06c5c 11400
6dd47d34
DE
11401 if (prefix == NULL)
11402 prefix = "";
11403 if (suffix == NULL)
11404 suffix = "";
11405
987504bb
JJ
11406 if (obs == NULL)
11407 {
11408 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11409
f55ee35c
JK
11410 strcpy (retval, lead);
11411 strcat (retval, prefix);
6dd47d34
DE
11412 strcat (retval, sep);
11413 strcat (retval, suffix);
63d06c5c
DC
11414 return retval;
11415 }
987504bb
JJ
11416 else
11417 {
11418 /* We have an obstack. */
f55ee35c 11419 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11420 }
63d06c5c
DC
11421}
11422
c906108c
SS
11423/* Return sibling of die, NULL if no sibling. */
11424
f9aca02d 11425static struct die_info *
fba45db2 11426sibling_die (struct die_info *die)
c906108c 11427{
639d11d3 11428 return die->sibling;
c906108c
SS
11429}
11430
71c25dea
TT
11431/* Get name of a die, return NULL if not found. */
11432
11433static char *
11434dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11435 struct obstack *obstack)
11436{
11437 if (name && cu->language == language_cplus)
11438 {
11439 char *canon_name = cp_canonicalize_string (name);
11440
11441 if (canon_name != NULL)
11442 {
11443 if (strcmp (canon_name, name) != 0)
11444 name = obsavestring (canon_name, strlen (canon_name),
11445 obstack);
11446 xfree (canon_name);
11447 }
11448 }
11449
11450 return name;
c906108c
SS
11451}
11452
9219021c
DC
11453/* Get name of a die, return NULL if not found. */
11454
11455static char *
e142c38c 11456dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11457{
11458 struct attribute *attr;
11459
e142c38c 11460 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11461 if (!attr || !DW_STRING (attr))
11462 return NULL;
11463
11464 switch (die->tag)
11465 {
11466 case DW_TAG_compile_unit:
11467 /* Compilation units have a DW_AT_name that is a filename, not
11468 a source language identifier. */
11469 case DW_TAG_enumeration_type:
11470 case DW_TAG_enumerator:
11471 /* These tags always have simple identifiers already; no need
11472 to canonicalize them. */
11473 return DW_STRING (attr);
907af001 11474
418835cc
KS
11475 case DW_TAG_subprogram:
11476 /* Java constructors will all be named "<init>", so return
11477 the class name when we see this special case. */
11478 if (cu->language == language_java
11479 && DW_STRING (attr) != NULL
11480 && strcmp (DW_STRING (attr), "<init>") == 0)
11481 {
11482 struct dwarf2_cu *spec_cu = cu;
11483 struct die_info *spec_die;
11484
11485 /* GCJ will output '<init>' for Java constructor names.
11486 For this special case, return the name of the parent class. */
11487
11488 /* GCJ may output suprogram DIEs with AT_specification set.
11489 If so, use the name of the specified DIE. */
11490 spec_die = die_specification (die, &spec_cu);
11491 if (spec_die != NULL)
11492 return dwarf2_name (spec_die, spec_cu);
11493
11494 do
11495 {
11496 die = die->parent;
11497 if (die->tag == DW_TAG_class_type)
11498 return dwarf2_name (die, cu);
11499 }
11500 while (die->tag != DW_TAG_compile_unit);
11501 }
907af001
UW
11502 break;
11503
11504 case DW_TAG_class_type:
11505 case DW_TAG_interface_type:
11506 case DW_TAG_structure_type:
11507 case DW_TAG_union_type:
11508 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11509 structures or unions. These were of the form "._%d" in GCC 4.1,
11510 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11511 and GCC 4.4. We work around this problem by ignoring these. */
11512 if (strncmp (DW_STRING (attr), "._", 2) == 0
11513 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11514 return NULL;
11515 break;
11516
71c25dea 11517 default:
907af001
UW
11518 break;
11519 }
11520
11521 if (!DW_STRING_IS_CANONICAL (attr))
11522 {
11523 DW_STRING (attr)
11524 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11525 &cu->objfile->objfile_obstack);
11526 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11527 }
907af001 11528 return DW_STRING (attr);
9219021c
DC
11529}
11530
11531/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11532 is none. *EXT_CU is the CU containing DIE on input, and the CU
11533 containing the return value on output. */
9219021c
DC
11534
11535static struct die_info *
f2f0e013 11536dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11537{
11538 struct attribute *attr;
9219021c 11539
f2f0e013 11540 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11541 if (attr == NULL)
11542 return NULL;
11543
f2f0e013 11544 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11545}
11546
c906108c
SS
11547/* Convert a DIE tag into its string name. */
11548
11549static char *
aa1ee363 11550dwarf_tag_name (unsigned tag)
c906108c
SS
11551{
11552 switch (tag)
11553 {
11554 case DW_TAG_padding:
11555 return "DW_TAG_padding";
11556 case DW_TAG_array_type:
11557 return "DW_TAG_array_type";
11558 case DW_TAG_class_type:
11559 return "DW_TAG_class_type";
11560 case DW_TAG_entry_point:
11561 return "DW_TAG_entry_point";
11562 case DW_TAG_enumeration_type:
11563 return "DW_TAG_enumeration_type";
11564 case DW_TAG_formal_parameter:
11565 return "DW_TAG_formal_parameter";
11566 case DW_TAG_imported_declaration:
11567 return "DW_TAG_imported_declaration";
11568 case DW_TAG_label:
11569 return "DW_TAG_label";
11570 case DW_TAG_lexical_block:
11571 return "DW_TAG_lexical_block";
11572 case DW_TAG_member:
11573 return "DW_TAG_member";
11574 case DW_TAG_pointer_type:
11575 return "DW_TAG_pointer_type";
11576 case DW_TAG_reference_type:
11577 return "DW_TAG_reference_type";
11578 case DW_TAG_compile_unit:
11579 return "DW_TAG_compile_unit";
11580 case DW_TAG_string_type:
11581 return "DW_TAG_string_type";
11582 case DW_TAG_structure_type:
11583 return "DW_TAG_structure_type";
11584 case DW_TAG_subroutine_type:
11585 return "DW_TAG_subroutine_type";
11586 case DW_TAG_typedef:
11587 return "DW_TAG_typedef";
11588 case DW_TAG_union_type:
11589 return "DW_TAG_union_type";
11590 case DW_TAG_unspecified_parameters:
11591 return "DW_TAG_unspecified_parameters";
11592 case DW_TAG_variant:
11593 return "DW_TAG_variant";
11594 case DW_TAG_common_block:
11595 return "DW_TAG_common_block";
11596 case DW_TAG_common_inclusion:
11597 return "DW_TAG_common_inclusion";
11598 case DW_TAG_inheritance:
11599 return "DW_TAG_inheritance";
11600 case DW_TAG_inlined_subroutine:
11601 return "DW_TAG_inlined_subroutine";
11602 case DW_TAG_module:
11603 return "DW_TAG_module";
11604 case DW_TAG_ptr_to_member_type:
11605 return "DW_TAG_ptr_to_member_type";
11606 case DW_TAG_set_type:
11607 return "DW_TAG_set_type";
11608 case DW_TAG_subrange_type:
11609 return "DW_TAG_subrange_type";
11610 case DW_TAG_with_stmt:
11611 return "DW_TAG_with_stmt";
11612 case DW_TAG_access_declaration:
11613 return "DW_TAG_access_declaration";
11614 case DW_TAG_base_type:
11615 return "DW_TAG_base_type";
11616 case DW_TAG_catch_block:
11617 return "DW_TAG_catch_block";
11618 case DW_TAG_const_type:
11619 return "DW_TAG_const_type";
11620 case DW_TAG_constant:
11621 return "DW_TAG_constant";
11622 case DW_TAG_enumerator:
11623 return "DW_TAG_enumerator";
11624 case DW_TAG_file_type:
11625 return "DW_TAG_file_type";
11626 case DW_TAG_friend:
11627 return "DW_TAG_friend";
11628 case DW_TAG_namelist:
11629 return "DW_TAG_namelist";
11630 case DW_TAG_namelist_item:
11631 return "DW_TAG_namelist_item";
11632 case DW_TAG_packed_type:
11633 return "DW_TAG_packed_type";
11634 case DW_TAG_subprogram:
11635 return "DW_TAG_subprogram";
11636 case DW_TAG_template_type_param:
11637 return "DW_TAG_template_type_param";
11638 case DW_TAG_template_value_param:
11639 return "DW_TAG_template_value_param";
11640 case DW_TAG_thrown_type:
11641 return "DW_TAG_thrown_type";
11642 case DW_TAG_try_block:
11643 return "DW_TAG_try_block";
11644 case DW_TAG_variant_part:
11645 return "DW_TAG_variant_part";
11646 case DW_TAG_variable:
11647 return "DW_TAG_variable";
11648 case DW_TAG_volatile_type:
11649 return "DW_TAG_volatile_type";
d9fa45fe
DC
11650 case DW_TAG_dwarf_procedure:
11651 return "DW_TAG_dwarf_procedure";
11652 case DW_TAG_restrict_type:
11653 return "DW_TAG_restrict_type";
11654 case DW_TAG_interface_type:
11655 return "DW_TAG_interface_type";
11656 case DW_TAG_namespace:
11657 return "DW_TAG_namespace";
11658 case DW_TAG_imported_module:
11659 return "DW_TAG_imported_module";
11660 case DW_TAG_unspecified_type:
11661 return "DW_TAG_unspecified_type";
11662 case DW_TAG_partial_unit:
11663 return "DW_TAG_partial_unit";
11664 case DW_TAG_imported_unit:
11665 return "DW_TAG_imported_unit";
b7619582
GF
11666 case DW_TAG_condition:
11667 return "DW_TAG_condition";
11668 case DW_TAG_shared_type:
11669 return "DW_TAG_shared_type";
348e048f
DE
11670 case DW_TAG_type_unit:
11671 return "DW_TAG_type_unit";
c906108c
SS
11672 case DW_TAG_MIPS_loop:
11673 return "DW_TAG_MIPS_loop";
b7619582
GF
11674 case DW_TAG_HP_array_descriptor:
11675 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11676 case DW_TAG_format_label:
11677 return "DW_TAG_format_label";
11678 case DW_TAG_function_template:
11679 return "DW_TAG_function_template";
11680 case DW_TAG_class_template:
11681 return "DW_TAG_class_template";
b7619582
GF
11682 case DW_TAG_GNU_BINCL:
11683 return "DW_TAG_GNU_BINCL";
11684 case DW_TAG_GNU_EINCL:
11685 return "DW_TAG_GNU_EINCL";
11686 case DW_TAG_upc_shared_type:
11687 return "DW_TAG_upc_shared_type";
11688 case DW_TAG_upc_strict_type:
11689 return "DW_TAG_upc_strict_type";
11690 case DW_TAG_upc_relaxed_type:
11691 return "DW_TAG_upc_relaxed_type";
11692 case DW_TAG_PGI_kanji_type:
11693 return "DW_TAG_PGI_kanji_type";
11694 case DW_TAG_PGI_interface_block:
11695 return "DW_TAG_PGI_interface_block";
c906108c
SS
11696 default:
11697 return "DW_TAG_<unknown>";
11698 }
11699}
11700
11701/* Convert a DWARF attribute code into its string name. */
11702
11703static char *
aa1ee363 11704dwarf_attr_name (unsigned attr)
c906108c
SS
11705{
11706 switch (attr)
11707 {
11708 case DW_AT_sibling:
11709 return "DW_AT_sibling";
11710 case DW_AT_location:
11711 return "DW_AT_location";
11712 case DW_AT_name:
11713 return "DW_AT_name";
11714 case DW_AT_ordering:
11715 return "DW_AT_ordering";
11716 case DW_AT_subscr_data:
11717 return "DW_AT_subscr_data";
11718 case DW_AT_byte_size:
11719 return "DW_AT_byte_size";
11720 case DW_AT_bit_offset:
11721 return "DW_AT_bit_offset";
11722 case DW_AT_bit_size:
11723 return "DW_AT_bit_size";
11724 case DW_AT_element_list:
11725 return "DW_AT_element_list";
11726 case DW_AT_stmt_list:
11727 return "DW_AT_stmt_list";
11728 case DW_AT_low_pc:
11729 return "DW_AT_low_pc";
11730 case DW_AT_high_pc:
11731 return "DW_AT_high_pc";
11732 case DW_AT_language:
11733 return "DW_AT_language";
11734 case DW_AT_member:
11735 return "DW_AT_member";
11736 case DW_AT_discr:
11737 return "DW_AT_discr";
11738 case DW_AT_discr_value:
11739 return "DW_AT_discr_value";
11740 case DW_AT_visibility:
11741 return "DW_AT_visibility";
11742 case DW_AT_import:
11743 return "DW_AT_import";
11744 case DW_AT_string_length:
11745 return "DW_AT_string_length";
11746 case DW_AT_common_reference:
11747 return "DW_AT_common_reference";
11748 case DW_AT_comp_dir:
11749 return "DW_AT_comp_dir";
11750 case DW_AT_const_value:
11751 return "DW_AT_const_value";
11752 case DW_AT_containing_type:
11753 return "DW_AT_containing_type";
11754 case DW_AT_default_value:
11755 return "DW_AT_default_value";
11756 case DW_AT_inline:
11757 return "DW_AT_inline";
11758 case DW_AT_is_optional:
11759 return "DW_AT_is_optional";
11760 case DW_AT_lower_bound:
11761 return "DW_AT_lower_bound";
11762 case DW_AT_producer:
11763 return "DW_AT_producer";
11764 case DW_AT_prototyped:
11765 return "DW_AT_prototyped";
11766 case DW_AT_return_addr:
11767 return "DW_AT_return_addr";
11768 case DW_AT_start_scope:
11769 return "DW_AT_start_scope";
09fa0d7c
JK
11770 case DW_AT_bit_stride:
11771 return "DW_AT_bit_stride";
c906108c
SS
11772 case DW_AT_upper_bound:
11773 return "DW_AT_upper_bound";
11774 case DW_AT_abstract_origin:
11775 return "DW_AT_abstract_origin";
11776 case DW_AT_accessibility:
11777 return "DW_AT_accessibility";
11778 case DW_AT_address_class:
11779 return "DW_AT_address_class";
11780 case DW_AT_artificial:
11781 return "DW_AT_artificial";
11782 case DW_AT_base_types:
11783 return "DW_AT_base_types";
11784 case DW_AT_calling_convention:
11785 return "DW_AT_calling_convention";
11786 case DW_AT_count:
11787 return "DW_AT_count";
11788 case DW_AT_data_member_location:
11789 return "DW_AT_data_member_location";
11790 case DW_AT_decl_column:
11791 return "DW_AT_decl_column";
11792 case DW_AT_decl_file:
11793 return "DW_AT_decl_file";
11794 case DW_AT_decl_line:
11795 return "DW_AT_decl_line";
11796 case DW_AT_declaration:
11797 return "DW_AT_declaration";
11798 case DW_AT_discr_list:
11799 return "DW_AT_discr_list";
11800 case DW_AT_encoding:
11801 return "DW_AT_encoding";
11802 case DW_AT_external:
11803 return "DW_AT_external";
11804 case DW_AT_frame_base:
11805 return "DW_AT_frame_base";
11806 case DW_AT_friend:
11807 return "DW_AT_friend";
11808 case DW_AT_identifier_case:
11809 return "DW_AT_identifier_case";
11810 case DW_AT_macro_info:
11811 return "DW_AT_macro_info";
11812 case DW_AT_namelist_items:
11813 return "DW_AT_namelist_items";
11814 case DW_AT_priority:
11815 return "DW_AT_priority";
11816 case DW_AT_segment:
11817 return "DW_AT_segment";
11818 case DW_AT_specification:
11819 return "DW_AT_specification";
11820 case DW_AT_static_link:
11821 return "DW_AT_static_link";
11822 case DW_AT_type:
11823 return "DW_AT_type";
11824 case DW_AT_use_location:
11825 return "DW_AT_use_location";
11826 case DW_AT_variable_parameter:
11827 return "DW_AT_variable_parameter";
11828 case DW_AT_virtuality:
11829 return "DW_AT_virtuality";
11830 case DW_AT_vtable_elem_location:
11831 return "DW_AT_vtable_elem_location";
b7619582 11832 /* DWARF 3 values. */
d9fa45fe
DC
11833 case DW_AT_allocated:
11834 return "DW_AT_allocated";
11835 case DW_AT_associated:
11836 return "DW_AT_associated";
11837 case DW_AT_data_location:
11838 return "DW_AT_data_location";
09fa0d7c
JK
11839 case DW_AT_byte_stride:
11840 return "DW_AT_byte_stride";
d9fa45fe
DC
11841 case DW_AT_entry_pc:
11842 return "DW_AT_entry_pc";
11843 case DW_AT_use_UTF8:
11844 return "DW_AT_use_UTF8";
11845 case DW_AT_extension:
11846 return "DW_AT_extension";
11847 case DW_AT_ranges:
11848 return "DW_AT_ranges";
11849 case DW_AT_trampoline:
11850 return "DW_AT_trampoline";
11851 case DW_AT_call_column:
11852 return "DW_AT_call_column";
11853 case DW_AT_call_file:
11854 return "DW_AT_call_file";
11855 case DW_AT_call_line:
11856 return "DW_AT_call_line";
b7619582
GF
11857 case DW_AT_description:
11858 return "DW_AT_description";
11859 case DW_AT_binary_scale:
11860 return "DW_AT_binary_scale";
11861 case DW_AT_decimal_scale:
11862 return "DW_AT_decimal_scale";
11863 case DW_AT_small:
11864 return "DW_AT_small";
11865 case DW_AT_decimal_sign:
11866 return "DW_AT_decimal_sign";
11867 case DW_AT_digit_count:
11868 return "DW_AT_digit_count";
11869 case DW_AT_picture_string:
11870 return "DW_AT_picture_string";
11871 case DW_AT_mutable:
11872 return "DW_AT_mutable";
11873 case DW_AT_threads_scaled:
11874 return "DW_AT_threads_scaled";
11875 case DW_AT_explicit:
11876 return "DW_AT_explicit";
11877 case DW_AT_object_pointer:
11878 return "DW_AT_object_pointer";
11879 case DW_AT_endianity:
11880 return "DW_AT_endianity";
11881 case DW_AT_elemental:
11882 return "DW_AT_elemental";
11883 case DW_AT_pure:
11884 return "DW_AT_pure";
11885 case DW_AT_recursive:
11886 return "DW_AT_recursive";
348e048f
DE
11887 /* DWARF 4 values. */
11888 case DW_AT_signature:
11889 return "DW_AT_signature";
31ef98ae
TT
11890 case DW_AT_linkage_name:
11891 return "DW_AT_linkage_name";
b7619582 11892 /* SGI/MIPS extensions. */
c764a876 11893#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11894 case DW_AT_MIPS_fde:
11895 return "DW_AT_MIPS_fde";
c764a876 11896#endif
c906108c
SS
11897 case DW_AT_MIPS_loop_begin:
11898 return "DW_AT_MIPS_loop_begin";
11899 case DW_AT_MIPS_tail_loop_begin:
11900 return "DW_AT_MIPS_tail_loop_begin";
11901 case DW_AT_MIPS_epilog_begin:
11902 return "DW_AT_MIPS_epilog_begin";
11903 case DW_AT_MIPS_loop_unroll_factor:
11904 return "DW_AT_MIPS_loop_unroll_factor";
11905 case DW_AT_MIPS_software_pipeline_depth:
11906 return "DW_AT_MIPS_software_pipeline_depth";
11907 case DW_AT_MIPS_linkage_name:
11908 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11909 case DW_AT_MIPS_stride:
11910 return "DW_AT_MIPS_stride";
11911 case DW_AT_MIPS_abstract_name:
11912 return "DW_AT_MIPS_abstract_name";
11913 case DW_AT_MIPS_clone_origin:
11914 return "DW_AT_MIPS_clone_origin";
11915 case DW_AT_MIPS_has_inlines:
11916 return "DW_AT_MIPS_has_inlines";
b7619582 11917 /* HP extensions. */
c764a876 11918#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11919 case DW_AT_HP_block_index:
11920 return "DW_AT_HP_block_index";
c764a876 11921#endif
b7619582
GF
11922 case DW_AT_HP_unmodifiable:
11923 return "DW_AT_HP_unmodifiable";
11924 case DW_AT_HP_actuals_stmt_list:
11925 return "DW_AT_HP_actuals_stmt_list";
11926 case DW_AT_HP_proc_per_section:
11927 return "DW_AT_HP_proc_per_section";
11928 case DW_AT_HP_raw_data_ptr:
11929 return "DW_AT_HP_raw_data_ptr";
11930 case DW_AT_HP_pass_by_reference:
11931 return "DW_AT_HP_pass_by_reference";
11932 case DW_AT_HP_opt_level:
11933 return "DW_AT_HP_opt_level";
11934 case DW_AT_HP_prof_version_id:
11935 return "DW_AT_HP_prof_version_id";
11936 case DW_AT_HP_opt_flags:
11937 return "DW_AT_HP_opt_flags";
11938 case DW_AT_HP_cold_region_low_pc:
11939 return "DW_AT_HP_cold_region_low_pc";
11940 case DW_AT_HP_cold_region_high_pc:
11941 return "DW_AT_HP_cold_region_high_pc";
11942 case DW_AT_HP_all_variables_modifiable:
11943 return "DW_AT_HP_all_variables_modifiable";
11944 case DW_AT_HP_linkage_name:
11945 return "DW_AT_HP_linkage_name";
11946 case DW_AT_HP_prof_flags:
11947 return "DW_AT_HP_prof_flags";
11948 /* GNU extensions. */
c906108c
SS
11949 case DW_AT_sf_names:
11950 return "DW_AT_sf_names";
11951 case DW_AT_src_info:
11952 return "DW_AT_src_info";
11953 case DW_AT_mac_info:
11954 return "DW_AT_mac_info";
11955 case DW_AT_src_coords:
11956 return "DW_AT_src_coords";
11957 case DW_AT_body_begin:
11958 return "DW_AT_body_begin";
11959 case DW_AT_body_end:
11960 return "DW_AT_body_end";
f5f8a009
EZ
11961 case DW_AT_GNU_vector:
11962 return "DW_AT_GNU_vector";
2de00c64
DE
11963 case DW_AT_GNU_odr_signature:
11964 return "DW_AT_GNU_odr_signature";
b7619582
GF
11965 /* VMS extensions. */
11966 case DW_AT_VMS_rtnbeg_pd_address:
11967 return "DW_AT_VMS_rtnbeg_pd_address";
11968 /* UPC extension. */
11969 case DW_AT_upc_threads_scaled:
11970 return "DW_AT_upc_threads_scaled";
11971 /* PGI (STMicroelectronics) extensions. */
11972 case DW_AT_PGI_lbase:
11973 return "DW_AT_PGI_lbase";
11974 case DW_AT_PGI_soffset:
11975 return "DW_AT_PGI_soffset";
11976 case DW_AT_PGI_lstride:
11977 return "DW_AT_PGI_lstride";
c906108c
SS
11978 default:
11979 return "DW_AT_<unknown>";
11980 }
11981}
11982
11983/* Convert a DWARF value form code into its string name. */
11984
11985static char *
aa1ee363 11986dwarf_form_name (unsigned form)
c906108c
SS
11987{
11988 switch (form)
11989 {
11990 case DW_FORM_addr:
11991 return "DW_FORM_addr";
11992 case DW_FORM_block2:
11993 return "DW_FORM_block2";
11994 case DW_FORM_block4:
11995 return "DW_FORM_block4";
11996 case DW_FORM_data2:
11997 return "DW_FORM_data2";
11998 case DW_FORM_data4:
11999 return "DW_FORM_data4";
12000 case DW_FORM_data8:
12001 return "DW_FORM_data8";
12002 case DW_FORM_string:
12003 return "DW_FORM_string";
12004 case DW_FORM_block:
12005 return "DW_FORM_block";
12006 case DW_FORM_block1:
12007 return "DW_FORM_block1";
12008 case DW_FORM_data1:
12009 return "DW_FORM_data1";
12010 case DW_FORM_flag:
12011 return "DW_FORM_flag";
12012 case DW_FORM_sdata:
12013 return "DW_FORM_sdata";
12014 case DW_FORM_strp:
12015 return "DW_FORM_strp";
12016 case DW_FORM_udata:
12017 return "DW_FORM_udata";
12018 case DW_FORM_ref_addr:
12019 return "DW_FORM_ref_addr";
12020 case DW_FORM_ref1:
12021 return "DW_FORM_ref1";
12022 case DW_FORM_ref2:
12023 return "DW_FORM_ref2";
12024 case DW_FORM_ref4:
12025 return "DW_FORM_ref4";
12026 case DW_FORM_ref8:
12027 return "DW_FORM_ref8";
12028 case DW_FORM_ref_udata:
12029 return "DW_FORM_ref_udata";
12030 case DW_FORM_indirect:
12031 return "DW_FORM_indirect";
348e048f
DE
12032 case DW_FORM_sec_offset:
12033 return "DW_FORM_sec_offset";
12034 case DW_FORM_exprloc:
12035 return "DW_FORM_exprloc";
12036 case DW_FORM_flag_present:
12037 return "DW_FORM_flag_present";
12038 case DW_FORM_sig8:
12039 return "DW_FORM_sig8";
c906108c
SS
12040 default:
12041 return "DW_FORM_<unknown>";
12042 }
12043}
12044
12045/* Convert a DWARF stack opcode into its string name. */
12046
9eae7c52
TT
12047const char *
12048dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12049{
12050 switch (op)
12051 {
12052 case DW_OP_addr:
12053 return "DW_OP_addr";
12054 case DW_OP_deref:
12055 return "DW_OP_deref";
12056 case DW_OP_const1u:
12057 return "DW_OP_const1u";
12058 case DW_OP_const1s:
12059 return "DW_OP_const1s";
12060 case DW_OP_const2u:
12061 return "DW_OP_const2u";
12062 case DW_OP_const2s:
12063 return "DW_OP_const2s";
12064 case DW_OP_const4u:
12065 return "DW_OP_const4u";
12066 case DW_OP_const4s:
12067 return "DW_OP_const4s";
12068 case DW_OP_const8u:
12069 return "DW_OP_const8u";
12070 case DW_OP_const8s:
12071 return "DW_OP_const8s";
12072 case DW_OP_constu:
12073 return "DW_OP_constu";
12074 case DW_OP_consts:
12075 return "DW_OP_consts";
12076 case DW_OP_dup:
12077 return "DW_OP_dup";
12078 case DW_OP_drop:
12079 return "DW_OP_drop";
12080 case DW_OP_over:
12081 return "DW_OP_over";
12082 case DW_OP_pick:
12083 return "DW_OP_pick";
12084 case DW_OP_swap:
12085 return "DW_OP_swap";
12086 case DW_OP_rot:
12087 return "DW_OP_rot";
12088 case DW_OP_xderef:
12089 return "DW_OP_xderef";
12090 case DW_OP_abs:
12091 return "DW_OP_abs";
12092 case DW_OP_and:
12093 return "DW_OP_and";
12094 case DW_OP_div:
12095 return "DW_OP_div";
12096 case DW_OP_minus:
12097 return "DW_OP_minus";
12098 case DW_OP_mod:
12099 return "DW_OP_mod";
12100 case DW_OP_mul:
12101 return "DW_OP_mul";
12102 case DW_OP_neg:
12103 return "DW_OP_neg";
12104 case DW_OP_not:
12105 return "DW_OP_not";
12106 case DW_OP_or:
12107 return "DW_OP_or";
12108 case DW_OP_plus:
12109 return "DW_OP_plus";
12110 case DW_OP_plus_uconst:
12111 return "DW_OP_plus_uconst";
12112 case DW_OP_shl:
12113 return "DW_OP_shl";
12114 case DW_OP_shr:
12115 return "DW_OP_shr";
12116 case DW_OP_shra:
12117 return "DW_OP_shra";
12118 case DW_OP_xor:
12119 return "DW_OP_xor";
12120 case DW_OP_bra:
12121 return "DW_OP_bra";
12122 case DW_OP_eq:
12123 return "DW_OP_eq";
12124 case DW_OP_ge:
12125 return "DW_OP_ge";
12126 case DW_OP_gt:
12127 return "DW_OP_gt";
12128 case DW_OP_le:
12129 return "DW_OP_le";
12130 case DW_OP_lt:
12131 return "DW_OP_lt";
12132 case DW_OP_ne:
12133 return "DW_OP_ne";
12134 case DW_OP_skip:
12135 return "DW_OP_skip";
12136 case DW_OP_lit0:
12137 return "DW_OP_lit0";
12138 case DW_OP_lit1:
12139 return "DW_OP_lit1";
12140 case DW_OP_lit2:
12141 return "DW_OP_lit2";
12142 case DW_OP_lit3:
12143 return "DW_OP_lit3";
12144 case DW_OP_lit4:
12145 return "DW_OP_lit4";
12146 case DW_OP_lit5:
12147 return "DW_OP_lit5";
12148 case DW_OP_lit6:
12149 return "DW_OP_lit6";
12150 case DW_OP_lit7:
12151 return "DW_OP_lit7";
12152 case DW_OP_lit8:
12153 return "DW_OP_lit8";
12154 case DW_OP_lit9:
12155 return "DW_OP_lit9";
12156 case DW_OP_lit10:
12157 return "DW_OP_lit10";
12158 case DW_OP_lit11:
12159 return "DW_OP_lit11";
12160 case DW_OP_lit12:
12161 return "DW_OP_lit12";
12162 case DW_OP_lit13:
12163 return "DW_OP_lit13";
12164 case DW_OP_lit14:
12165 return "DW_OP_lit14";
12166 case DW_OP_lit15:
12167 return "DW_OP_lit15";
12168 case DW_OP_lit16:
12169 return "DW_OP_lit16";
12170 case DW_OP_lit17:
12171 return "DW_OP_lit17";
12172 case DW_OP_lit18:
12173 return "DW_OP_lit18";
12174 case DW_OP_lit19:
12175 return "DW_OP_lit19";
12176 case DW_OP_lit20:
12177 return "DW_OP_lit20";
12178 case DW_OP_lit21:
12179 return "DW_OP_lit21";
12180 case DW_OP_lit22:
12181 return "DW_OP_lit22";
12182 case DW_OP_lit23:
12183 return "DW_OP_lit23";
12184 case DW_OP_lit24:
12185 return "DW_OP_lit24";
12186 case DW_OP_lit25:
12187 return "DW_OP_lit25";
12188 case DW_OP_lit26:
12189 return "DW_OP_lit26";
12190 case DW_OP_lit27:
12191 return "DW_OP_lit27";
12192 case DW_OP_lit28:
12193 return "DW_OP_lit28";
12194 case DW_OP_lit29:
12195 return "DW_OP_lit29";
12196 case DW_OP_lit30:
12197 return "DW_OP_lit30";
12198 case DW_OP_lit31:
12199 return "DW_OP_lit31";
12200 case DW_OP_reg0:
12201 return "DW_OP_reg0";
12202 case DW_OP_reg1:
12203 return "DW_OP_reg1";
12204 case DW_OP_reg2:
12205 return "DW_OP_reg2";
12206 case DW_OP_reg3:
12207 return "DW_OP_reg3";
12208 case DW_OP_reg4:
12209 return "DW_OP_reg4";
12210 case DW_OP_reg5:
12211 return "DW_OP_reg5";
12212 case DW_OP_reg6:
12213 return "DW_OP_reg6";
12214 case DW_OP_reg7:
12215 return "DW_OP_reg7";
12216 case DW_OP_reg8:
12217 return "DW_OP_reg8";
12218 case DW_OP_reg9:
12219 return "DW_OP_reg9";
12220 case DW_OP_reg10:
12221 return "DW_OP_reg10";
12222 case DW_OP_reg11:
12223 return "DW_OP_reg11";
12224 case DW_OP_reg12:
12225 return "DW_OP_reg12";
12226 case DW_OP_reg13:
12227 return "DW_OP_reg13";
12228 case DW_OP_reg14:
12229 return "DW_OP_reg14";
12230 case DW_OP_reg15:
12231 return "DW_OP_reg15";
12232 case DW_OP_reg16:
12233 return "DW_OP_reg16";
12234 case DW_OP_reg17:
12235 return "DW_OP_reg17";
12236 case DW_OP_reg18:
12237 return "DW_OP_reg18";
12238 case DW_OP_reg19:
12239 return "DW_OP_reg19";
12240 case DW_OP_reg20:
12241 return "DW_OP_reg20";
12242 case DW_OP_reg21:
12243 return "DW_OP_reg21";
12244 case DW_OP_reg22:
12245 return "DW_OP_reg22";
12246 case DW_OP_reg23:
12247 return "DW_OP_reg23";
12248 case DW_OP_reg24:
12249 return "DW_OP_reg24";
12250 case DW_OP_reg25:
12251 return "DW_OP_reg25";
12252 case DW_OP_reg26:
12253 return "DW_OP_reg26";
12254 case DW_OP_reg27:
12255 return "DW_OP_reg27";
12256 case DW_OP_reg28:
12257 return "DW_OP_reg28";
12258 case DW_OP_reg29:
12259 return "DW_OP_reg29";
12260 case DW_OP_reg30:
12261 return "DW_OP_reg30";
12262 case DW_OP_reg31:
12263 return "DW_OP_reg31";
12264 case DW_OP_breg0:
12265 return "DW_OP_breg0";
12266 case DW_OP_breg1:
12267 return "DW_OP_breg1";
12268 case DW_OP_breg2:
12269 return "DW_OP_breg2";
12270 case DW_OP_breg3:
12271 return "DW_OP_breg3";
12272 case DW_OP_breg4:
12273 return "DW_OP_breg4";
12274 case DW_OP_breg5:
12275 return "DW_OP_breg5";
12276 case DW_OP_breg6:
12277 return "DW_OP_breg6";
12278 case DW_OP_breg7:
12279 return "DW_OP_breg7";
12280 case DW_OP_breg8:
12281 return "DW_OP_breg8";
12282 case DW_OP_breg9:
12283 return "DW_OP_breg9";
12284 case DW_OP_breg10:
12285 return "DW_OP_breg10";
12286 case DW_OP_breg11:
12287 return "DW_OP_breg11";
12288 case DW_OP_breg12:
12289 return "DW_OP_breg12";
12290 case DW_OP_breg13:
12291 return "DW_OP_breg13";
12292 case DW_OP_breg14:
12293 return "DW_OP_breg14";
12294 case DW_OP_breg15:
12295 return "DW_OP_breg15";
12296 case DW_OP_breg16:
12297 return "DW_OP_breg16";
12298 case DW_OP_breg17:
12299 return "DW_OP_breg17";
12300 case DW_OP_breg18:
12301 return "DW_OP_breg18";
12302 case DW_OP_breg19:
12303 return "DW_OP_breg19";
12304 case DW_OP_breg20:
12305 return "DW_OP_breg20";
12306 case DW_OP_breg21:
12307 return "DW_OP_breg21";
12308 case DW_OP_breg22:
12309 return "DW_OP_breg22";
12310 case DW_OP_breg23:
12311 return "DW_OP_breg23";
12312 case DW_OP_breg24:
12313 return "DW_OP_breg24";
12314 case DW_OP_breg25:
12315 return "DW_OP_breg25";
12316 case DW_OP_breg26:
12317 return "DW_OP_breg26";
12318 case DW_OP_breg27:
12319 return "DW_OP_breg27";
12320 case DW_OP_breg28:
12321 return "DW_OP_breg28";
12322 case DW_OP_breg29:
12323 return "DW_OP_breg29";
12324 case DW_OP_breg30:
12325 return "DW_OP_breg30";
12326 case DW_OP_breg31:
12327 return "DW_OP_breg31";
12328 case DW_OP_regx:
12329 return "DW_OP_regx";
12330 case DW_OP_fbreg:
12331 return "DW_OP_fbreg";
12332 case DW_OP_bregx:
12333 return "DW_OP_bregx";
12334 case DW_OP_piece:
12335 return "DW_OP_piece";
12336 case DW_OP_deref_size:
12337 return "DW_OP_deref_size";
12338 case DW_OP_xderef_size:
12339 return "DW_OP_xderef_size";
12340 case DW_OP_nop:
12341 return "DW_OP_nop";
b7619582 12342 /* DWARF 3 extensions. */
ed348acc
EZ
12343 case DW_OP_push_object_address:
12344 return "DW_OP_push_object_address";
12345 case DW_OP_call2:
12346 return "DW_OP_call2";
12347 case DW_OP_call4:
12348 return "DW_OP_call4";
12349 case DW_OP_call_ref:
12350 return "DW_OP_call_ref";
b7619582
GF
12351 case DW_OP_form_tls_address:
12352 return "DW_OP_form_tls_address";
12353 case DW_OP_call_frame_cfa:
12354 return "DW_OP_call_frame_cfa";
12355 case DW_OP_bit_piece:
12356 return "DW_OP_bit_piece";
9eae7c52
TT
12357 /* DWARF 4 extensions. */
12358 case DW_OP_implicit_value:
12359 return "DW_OP_implicit_value";
12360 case DW_OP_stack_value:
12361 return "DW_OP_stack_value";
12362 /* GNU extensions. */
ed348acc
EZ
12363 case DW_OP_GNU_push_tls_address:
12364 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12365 case DW_OP_GNU_uninit:
12366 return "DW_OP_GNU_uninit";
c906108c 12367 default:
9eae7c52 12368 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12369 }
12370}
12371
12372static char *
fba45db2 12373dwarf_bool_name (unsigned mybool)
c906108c
SS
12374{
12375 if (mybool)
12376 return "TRUE";
12377 else
12378 return "FALSE";
12379}
12380
12381/* Convert a DWARF type code into its string name. */
12382
12383static char *
aa1ee363 12384dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12385{
12386 switch (enc)
12387 {
b7619582
GF
12388 case DW_ATE_void:
12389 return "DW_ATE_void";
c906108c
SS
12390 case DW_ATE_address:
12391 return "DW_ATE_address";
12392 case DW_ATE_boolean:
12393 return "DW_ATE_boolean";
12394 case DW_ATE_complex_float:
12395 return "DW_ATE_complex_float";
12396 case DW_ATE_float:
12397 return "DW_ATE_float";
12398 case DW_ATE_signed:
12399 return "DW_ATE_signed";
12400 case DW_ATE_signed_char:
12401 return "DW_ATE_signed_char";
12402 case DW_ATE_unsigned:
12403 return "DW_ATE_unsigned";
12404 case DW_ATE_unsigned_char:
12405 return "DW_ATE_unsigned_char";
b7619582 12406 /* DWARF 3. */
d9fa45fe
DC
12407 case DW_ATE_imaginary_float:
12408 return "DW_ATE_imaginary_float";
b7619582
GF
12409 case DW_ATE_packed_decimal:
12410 return "DW_ATE_packed_decimal";
12411 case DW_ATE_numeric_string:
12412 return "DW_ATE_numeric_string";
12413 case DW_ATE_edited:
12414 return "DW_ATE_edited";
12415 case DW_ATE_signed_fixed:
12416 return "DW_ATE_signed_fixed";
12417 case DW_ATE_unsigned_fixed:
12418 return "DW_ATE_unsigned_fixed";
12419 case DW_ATE_decimal_float:
12420 return "DW_ATE_decimal_float";
75079b2b
TT
12421 /* DWARF 4. */
12422 case DW_ATE_UTF:
12423 return "DW_ATE_UTF";
b7619582
GF
12424 /* HP extensions. */
12425 case DW_ATE_HP_float80:
12426 return "DW_ATE_HP_float80";
12427 case DW_ATE_HP_complex_float80:
12428 return "DW_ATE_HP_complex_float80";
12429 case DW_ATE_HP_float128:
12430 return "DW_ATE_HP_float128";
12431 case DW_ATE_HP_complex_float128:
12432 return "DW_ATE_HP_complex_float128";
12433 case DW_ATE_HP_floathpintel:
12434 return "DW_ATE_HP_floathpintel";
12435 case DW_ATE_HP_imaginary_float80:
12436 return "DW_ATE_HP_imaginary_float80";
12437 case DW_ATE_HP_imaginary_float128:
12438 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12439 default:
12440 return "DW_ATE_<unknown>";
12441 }
12442}
12443
12444/* Convert a DWARF call frame info operation to its string name. */
12445
12446#if 0
12447static char *
aa1ee363 12448dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12449{
12450 switch (cfi_opc)
12451 {
12452 case DW_CFA_advance_loc:
12453 return "DW_CFA_advance_loc";
12454 case DW_CFA_offset:
12455 return "DW_CFA_offset";
12456 case DW_CFA_restore:
12457 return "DW_CFA_restore";
12458 case DW_CFA_nop:
12459 return "DW_CFA_nop";
12460 case DW_CFA_set_loc:
12461 return "DW_CFA_set_loc";
12462 case DW_CFA_advance_loc1:
12463 return "DW_CFA_advance_loc1";
12464 case DW_CFA_advance_loc2:
12465 return "DW_CFA_advance_loc2";
12466 case DW_CFA_advance_loc4:
12467 return "DW_CFA_advance_loc4";
12468 case DW_CFA_offset_extended:
12469 return "DW_CFA_offset_extended";
12470 case DW_CFA_restore_extended:
12471 return "DW_CFA_restore_extended";
12472 case DW_CFA_undefined:
12473 return "DW_CFA_undefined";
12474 case DW_CFA_same_value:
12475 return "DW_CFA_same_value";
12476 case DW_CFA_register:
12477 return "DW_CFA_register";
12478 case DW_CFA_remember_state:
12479 return "DW_CFA_remember_state";
12480 case DW_CFA_restore_state:
12481 return "DW_CFA_restore_state";
12482 case DW_CFA_def_cfa:
12483 return "DW_CFA_def_cfa";
12484 case DW_CFA_def_cfa_register:
12485 return "DW_CFA_def_cfa_register";
12486 case DW_CFA_def_cfa_offset:
12487 return "DW_CFA_def_cfa_offset";
b7619582 12488 /* DWARF 3. */
985cb1a3
JM
12489 case DW_CFA_def_cfa_expression:
12490 return "DW_CFA_def_cfa_expression";
12491 case DW_CFA_expression:
12492 return "DW_CFA_expression";
12493 case DW_CFA_offset_extended_sf:
12494 return "DW_CFA_offset_extended_sf";
12495 case DW_CFA_def_cfa_sf:
12496 return "DW_CFA_def_cfa_sf";
12497 case DW_CFA_def_cfa_offset_sf:
12498 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12499 case DW_CFA_val_offset:
12500 return "DW_CFA_val_offset";
12501 case DW_CFA_val_offset_sf:
12502 return "DW_CFA_val_offset_sf";
12503 case DW_CFA_val_expression:
12504 return "DW_CFA_val_expression";
12505 /* SGI/MIPS specific. */
c906108c
SS
12506 case DW_CFA_MIPS_advance_loc8:
12507 return "DW_CFA_MIPS_advance_loc8";
b7619582 12508 /* GNU extensions. */
985cb1a3
JM
12509 case DW_CFA_GNU_window_save:
12510 return "DW_CFA_GNU_window_save";
12511 case DW_CFA_GNU_args_size:
12512 return "DW_CFA_GNU_args_size";
12513 case DW_CFA_GNU_negative_offset_extended:
12514 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12515 default:
12516 return "DW_CFA_<unknown>";
12517 }
12518}
12519#endif
12520
f9aca02d 12521static void
d97bc12b 12522dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12523{
12524 unsigned int i;
12525
d97bc12b
DE
12526 print_spaces (indent, f);
12527 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12528 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12529
12530 if (die->parent != NULL)
12531 {
12532 print_spaces (indent, f);
12533 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12534 die->parent->offset);
12535 }
12536
12537 print_spaces (indent, f);
12538 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12539 dwarf_bool_name (die->child != NULL));
c906108c 12540
d97bc12b
DE
12541 print_spaces (indent, f);
12542 fprintf_unfiltered (f, " attributes:\n");
12543
c906108c
SS
12544 for (i = 0; i < die->num_attrs; ++i)
12545 {
d97bc12b
DE
12546 print_spaces (indent, f);
12547 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12548 dwarf_attr_name (die->attrs[i].name),
12549 dwarf_form_name (die->attrs[i].form));
d97bc12b 12550
c906108c
SS
12551 switch (die->attrs[i].form)
12552 {
12553 case DW_FORM_ref_addr:
12554 case DW_FORM_addr:
d97bc12b 12555 fprintf_unfiltered (f, "address: ");
5af949e3 12556 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12557 break;
12558 case DW_FORM_block2:
12559 case DW_FORM_block4:
12560 case DW_FORM_block:
12561 case DW_FORM_block1:
d97bc12b 12562 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12563 break;
2dc7f7b3
TT
12564 case DW_FORM_exprloc:
12565 fprintf_unfiltered (f, "expression: size %u",
12566 DW_BLOCK (&die->attrs[i])->size);
12567 break;
10b3939b
DJ
12568 case DW_FORM_ref1:
12569 case DW_FORM_ref2:
12570 case DW_FORM_ref4:
d97bc12b 12571 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12572 (long) (DW_ADDR (&die->attrs[i])));
12573 break;
c906108c
SS
12574 case DW_FORM_data1:
12575 case DW_FORM_data2:
12576 case DW_FORM_data4:
ce5d95e1 12577 case DW_FORM_data8:
c906108c
SS
12578 case DW_FORM_udata:
12579 case DW_FORM_sdata:
43bbcdc2
PH
12580 fprintf_unfiltered (f, "constant: %s",
12581 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12582 break;
2dc7f7b3
TT
12583 case DW_FORM_sec_offset:
12584 fprintf_unfiltered (f, "section offset: %s",
12585 pulongest (DW_UNSND (&die->attrs[i])));
12586 break;
348e048f
DE
12587 case DW_FORM_sig8:
12588 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12589 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12590 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12591 else
12592 fprintf_unfiltered (f, "signatured type, offset: unknown");
12593 break;
c906108c 12594 case DW_FORM_string:
4bdf3d34 12595 case DW_FORM_strp:
8285870a 12596 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12597 DW_STRING (&die->attrs[i])
8285870a
JK
12598 ? DW_STRING (&die->attrs[i]) : "",
12599 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12600 break;
12601 case DW_FORM_flag:
12602 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12603 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12604 else
d97bc12b 12605 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12606 break;
2dc7f7b3
TT
12607 case DW_FORM_flag_present:
12608 fprintf_unfiltered (f, "flag: TRUE");
12609 break;
a8329558
KW
12610 case DW_FORM_indirect:
12611 /* the reader will have reduced the indirect form to
12612 the "base form" so this form should not occur */
d97bc12b 12613 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12614 break;
c906108c 12615 default:
d97bc12b 12616 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12617 die->attrs[i].form);
d97bc12b 12618 break;
c906108c 12619 }
d97bc12b 12620 fprintf_unfiltered (f, "\n");
c906108c
SS
12621 }
12622}
12623
f9aca02d 12624static void
d97bc12b 12625dump_die_for_error (struct die_info *die)
c906108c 12626{
d97bc12b
DE
12627 dump_die_shallow (gdb_stderr, 0, die);
12628}
12629
12630static void
12631dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12632{
12633 int indent = level * 4;
12634
12635 gdb_assert (die != NULL);
12636
12637 if (level >= max_level)
12638 return;
12639
12640 dump_die_shallow (f, indent, die);
12641
12642 if (die->child != NULL)
c906108c 12643 {
d97bc12b
DE
12644 print_spaces (indent, f);
12645 fprintf_unfiltered (f, " Children:");
12646 if (level + 1 < max_level)
12647 {
12648 fprintf_unfiltered (f, "\n");
12649 dump_die_1 (f, level + 1, max_level, die->child);
12650 }
12651 else
12652 {
12653 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12654 }
12655 }
12656
12657 if (die->sibling != NULL && level > 0)
12658 {
12659 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12660 }
12661}
12662
d97bc12b
DE
12663/* This is called from the pdie macro in gdbinit.in.
12664 It's not static so gcc will keep a copy callable from gdb. */
12665
12666void
12667dump_die (struct die_info *die, int max_level)
12668{
12669 dump_die_1 (gdb_stdlog, 0, max_level, die);
12670}
12671
f9aca02d 12672static void
51545339 12673store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12674{
51545339 12675 void **slot;
c906108c 12676
51545339
DJ
12677 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12678
12679 *slot = die;
c906108c
SS
12680}
12681
93311388
DE
12682static int
12683is_ref_attr (struct attribute *attr)
c906108c 12684{
c906108c
SS
12685 switch (attr->form)
12686 {
12687 case DW_FORM_ref_addr:
c906108c
SS
12688 case DW_FORM_ref1:
12689 case DW_FORM_ref2:
12690 case DW_FORM_ref4:
613e1657 12691 case DW_FORM_ref8:
c906108c 12692 case DW_FORM_ref_udata:
93311388 12693 return 1;
c906108c 12694 default:
93311388 12695 return 0;
c906108c 12696 }
93311388
DE
12697}
12698
12699static unsigned int
12700dwarf2_get_ref_die_offset (struct attribute *attr)
12701{
12702 if (is_ref_attr (attr))
12703 return DW_ADDR (attr);
12704
12705 complaint (&symfile_complaints,
12706 _("unsupported die ref attribute form: '%s'"),
12707 dwarf_form_name (attr->form));
12708 return 0;
c906108c
SS
12709}
12710
43bbcdc2
PH
12711/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12712 * the value held by the attribute is not constant. */
a02abb62 12713
43bbcdc2 12714static LONGEST
a02abb62
JB
12715dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12716{
12717 if (attr->form == DW_FORM_sdata)
12718 return DW_SND (attr);
12719 else if (attr->form == DW_FORM_udata
12720 || attr->form == DW_FORM_data1
12721 || attr->form == DW_FORM_data2
12722 || attr->form == DW_FORM_data4
12723 || attr->form == DW_FORM_data8)
12724 return DW_UNSND (attr);
12725 else
12726 {
e2e0b3e5 12727 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12728 dwarf_form_name (attr->form));
12729 return default_value;
12730 }
12731}
12732
03dd20cc 12733/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12734 unit and add it to our queue.
12735 The result is non-zero if PER_CU was queued, otherwise the result is zero
12736 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12737
348e048f 12738static int
03dd20cc
DJ
12739maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12740 struct dwarf2_per_cu_data *per_cu)
12741{
98bfdba5
PA
12742 /* We may arrive here during partial symbol reading, if we need full
12743 DIEs to process an unusual case (e.g. template arguments). Do
12744 not queue PER_CU, just tell our caller to load its DIEs. */
12745 if (dwarf2_per_objfile->reading_partial_symbols)
12746 {
12747 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12748 return 1;
12749 return 0;
12750 }
12751
03dd20cc
DJ
12752 /* Mark the dependence relation so that we don't flush PER_CU
12753 too early. */
12754 dwarf2_add_dependence (this_cu, per_cu);
12755
12756 /* If it's already on the queue, we have nothing to do. */
12757 if (per_cu->queued)
348e048f 12758 return 0;
03dd20cc
DJ
12759
12760 /* If the compilation unit is already loaded, just mark it as
12761 used. */
12762 if (per_cu->cu != NULL)
12763 {
12764 per_cu->cu->last_used = 0;
348e048f 12765 return 0;
03dd20cc
DJ
12766 }
12767
12768 /* Add it to the queue. */
12769 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12770
12771 return 1;
12772}
12773
12774/* Follow reference or signature attribute ATTR of SRC_DIE.
12775 On entry *REF_CU is the CU of SRC_DIE.
12776 On exit *REF_CU is the CU of the result. */
12777
12778static struct die_info *
12779follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12780 struct dwarf2_cu **ref_cu)
12781{
12782 struct die_info *die;
12783
12784 if (is_ref_attr (attr))
12785 die = follow_die_ref (src_die, attr, ref_cu);
12786 else if (attr->form == DW_FORM_sig8)
12787 die = follow_die_sig (src_die, attr, ref_cu);
12788 else
12789 {
12790 dump_die_for_error (src_die);
12791 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12792 (*ref_cu)->objfile->name);
12793 }
12794
12795 return die;
03dd20cc
DJ
12796}
12797
5c631832 12798/* Follow reference OFFSET.
673bfd45
DE
12799 On entry *REF_CU is the CU of the source die referencing OFFSET.
12800 On exit *REF_CU is the CU of the result.
12801 Returns NULL if OFFSET is invalid. */
f504f079 12802
f9aca02d 12803static struct die_info *
5c631832 12804follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12805{
10b3939b 12806 struct die_info temp_die;
f2f0e013 12807 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12808
348e048f
DE
12809 gdb_assert (cu->per_cu != NULL);
12810
98bfdba5
PA
12811 target_cu = cu;
12812
348e048f
DE
12813 if (cu->per_cu->from_debug_types)
12814 {
12815 /* .debug_types CUs cannot reference anything outside their CU.
12816 If they need to, they have to reference a signatured type via
12817 DW_FORM_sig8. */
12818 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12819 return NULL;
348e048f
DE
12820 }
12821 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12822 {
12823 struct dwarf2_per_cu_data *per_cu;
9a619af0 12824
45452591 12825 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12826
12827 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12828 if (maybe_queue_comp_unit (cu, per_cu))
12829 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12830
10b3939b
DJ
12831 target_cu = per_cu->cu;
12832 }
98bfdba5
PA
12833 else if (cu->dies == NULL)
12834 {
12835 /* We're loading full DIEs during partial symbol reading. */
12836 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12837 load_full_comp_unit (cu->per_cu, cu->objfile);
12838 }
c906108c 12839
f2f0e013 12840 *ref_cu = target_cu;
51545339 12841 temp_die.offset = offset;
5c631832
JK
12842 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12843}
10b3939b 12844
5c631832
JK
12845/* Follow reference attribute ATTR of SRC_DIE.
12846 On entry *REF_CU is the CU of SRC_DIE.
12847 On exit *REF_CU is the CU of the result. */
12848
12849static struct die_info *
12850follow_die_ref (struct die_info *src_die, struct attribute *attr,
12851 struct dwarf2_cu **ref_cu)
12852{
12853 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12854 struct dwarf2_cu *cu = *ref_cu;
12855 struct die_info *die;
12856
12857 die = follow_die_offset (offset, ref_cu);
12858 if (!die)
12859 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12860 "at 0x%x [in module %s]"),
12861 offset, src_die->offset, cu->objfile->name);
348e048f 12862
5c631832
JK
12863 return die;
12864}
12865
12866/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12867 value is intended for DW_OP_call*. */
12868
12869struct dwarf2_locexpr_baton
12870dwarf2_fetch_die_location_block (unsigned int offset,
12871 struct dwarf2_per_cu_data *per_cu)
12872{
12873 struct dwarf2_cu *cu = per_cu->cu;
12874 struct die_info *die;
12875 struct attribute *attr;
12876 struct dwarf2_locexpr_baton retval;
12877
12878 die = follow_die_offset (offset, &cu);
12879 if (!die)
12880 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12881 offset, per_cu->cu->objfile->name);
12882
12883 attr = dwarf2_attr (die, DW_AT_location, cu);
12884 if (!attr)
12885 {
12886 /* DWARF: "If there is no such attribute, then there is no effect.". */
12887
12888 retval.data = NULL;
12889 retval.size = 0;
12890 }
12891 else
12892 {
12893 if (!attr_form_is_block (attr))
12894 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12895 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12896 offset, per_cu->cu->objfile->name);
12897
12898 retval.data = DW_BLOCK (attr)->data;
12899 retval.size = DW_BLOCK (attr)->size;
12900 }
12901 retval.per_cu = cu->per_cu;
12902 return retval;
348e048f
DE
12903}
12904
12905/* Follow the signature attribute ATTR in SRC_DIE.
12906 On entry *REF_CU is the CU of SRC_DIE.
12907 On exit *REF_CU is the CU of the result. */
12908
12909static struct die_info *
12910follow_die_sig (struct die_info *src_die, struct attribute *attr,
12911 struct dwarf2_cu **ref_cu)
12912{
12913 struct objfile *objfile = (*ref_cu)->objfile;
12914 struct die_info temp_die;
12915 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12916 struct dwarf2_cu *sig_cu;
12917 struct die_info *die;
12918
12919 /* sig_type will be NULL if the signatured type is missing from
12920 the debug info. */
12921 if (sig_type == NULL)
12922 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12923 "at 0x%x [in module %s]"),
12924 src_die->offset, objfile->name);
12925
12926 /* If necessary, add it to the queue and load its DIEs. */
12927
12928 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12929 read_signatured_type (objfile, sig_type);
12930
12931 gdb_assert (sig_type->per_cu.cu != NULL);
12932
12933 sig_cu = sig_type->per_cu.cu;
12934 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12935 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12936 if (die)
12937 {
12938 *ref_cu = sig_cu;
12939 return die;
12940 }
12941
12942 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12943 "at 0x%x [in module %s]"),
12944 sig_type->type_offset, src_die->offset, objfile->name);
12945}
12946
12947/* Given an offset of a signatured type, return its signatured_type. */
12948
12949static struct signatured_type *
12950lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12951{
12952 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12953 unsigned int length, initial_length_size;
12954 unsigned int sig_offset;
12955 struct signatured_type find_entry, *type_sig;
12956
12957 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12958 sig_offset = (initial_length_size
12959 + 2 /*version*/
12960 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12961 + 1 /*address_size*/);
12962 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12963 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12964
12965 /* This is only used to lookup previously recorded types.
12966 If we didn't find it, it's our bug. */
12967 gdb_assert (type_sig != NULL);
12968 gdb_assert (offset == type_sig->offset);
12969
12970 return type_sig;
12971}
12972
12973/* Read in signatured type at OFFSET and build its CU and die(s). */
12974
12975static void
12976read_signatured_type_at_offset (struct objfile *objfile,
12977 unsigned int offset)
12978{
12979 struct signatured_type *type_sig;
12980
be391dca
TT
12981 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12982
348e048f
DE
12983 /* We have the section offset, but we need the signature to do the
12984 hash table lookup. */
12985 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12986
12987 gdb_assert (type_sig->per_cu.cu == NULL);
12988
12989 read_signatured_type (objfile, type_sig);
12990
12991 gdb_assert (type_sig->per_cu.cu != NULL);
12992}
12993
12994/* Read in a signatured type and build its CU and DIEs. */
12995
12996static void
12997read_signatured_type (struct objfile *objfile,
12998 struct signatured_type *type_sig)
12999{
1fd400ff 13000 gdb_byte *types_ptr;
348e048f
DE
13001 struct die_reader_specs reader_specs;
13002 struct dwarf2_cu *cu;
13003 ULONGEST signature;
13004 struct cleanup *back_to, *free_cu_cleanup;
13005 struct attribute *attr;
13006
1fd400ff
TT
13007 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13008 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13009
348e048f
DE
13010 gdb_assert (type_sig->per_cu.cu == NULL);
13011
13012 cu = xmalloc (sizeof (struct dwarf2_cu));
13013 memset (cu, 0, sizeof (struct dwarf2_cu));
13014 obstack_init (&cu->comp_unit_obstack);
13015 cu->objfile = objfile;
13016 type_sig->per_cu.cu = cu;
13017 cu->per_cu = &type_sig->per_cu;
13018
13019 /* If an error occurs while loading, release our storage. */
13020 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13021
13022 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13023 types_ptr, objfile->obfd);
13024 gdb_assert (signature == type_sig->signature);
13025
13026 cu->die_hash
13027 = htab_create_alloc_ex (cu->header.length / 12,
13028 die_hash,
13029 die_eq,
13030 NULL,
13031 &cu->comp_unit_obstack,
13032 hashtab_obstack_allocate,
13033 dummy_obstack_deallocate);
13034
13035 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13036 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13037
13038 init_cu_die_reader (&reader_specs, cu);
13039
13040 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13041 NULL /*parent*/);
13042
13043 /* We try not to read any attributes in this function, because not
13044 all objfiles needed for references have been loaded yet, and symbol
13045 table processing isn't initialized. But we have to set the CU language,
13046 or we won't be able to build types correctly. */
13047 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13048 if (attr)
13049 set_cu_language (DW_UNSND (attr), cu);
13050 else
13051 set_cu_language (language_minimal, cu);
13052
13053 do_cleanups (back_to);
13054
13055 /* We've successfully allocated this compilation unit. Let our caller
13056 clean it up when finished with it. */
13057 discard_cleanups (free_cu_cleanup);
13058
13059 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13060 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13061}
13062
c906108c
SS
13063/* Decode simple location descriptions.
13064 Given a pointer to a dwarf block that defines a location, compute
13065 the location and return the value.
13066
4cecd739
DJ
13067 NOTE drow/2003-11-18: This function is called in two situations
13068 now: for the address of static or global variables (partial symbols
13069 only) and for offsets into structures which are expected to be
13070 (more or less) constant. The partial symbol case should go away,
13071 and only the constant case should remain. That will let this
13072 function complain more accurately. A few special modes are allowed
13073 without complaint for global variables (for instance, global
13074 register values and thread-local values).
c906108c
SS
13075
13076 A location description containing no operations indicates that the
4cecd739 13077 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13078 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13079 callers will only want a very basic result and this can become a
13080 complaint.
c906108c 13081
c906108c
SS
13082 Note that stack[0] is unused except as a default error return.
13083 Note that stack overflow is not yet handled. */
13084
13085static CORE_ADDR
e7c27a73 13086decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13087{
e7c27a73 13088 struct objfile *objfile = cu->objfile;
c906108c
SS
13089 int i;
13090 int size = blk->size;
fe1b8b76 13091 gdb_byte *data = blk->data;
c906108c
SS
13092 CORE_ADDR stack[64];
13093 int stacki;
13094 unsigned int bytes_read, unsnd;
fe1b8b76 13095 gdb_byte op;
c906108c
SS
13096
13097 i = 0;
13098 stacki = 0;
13099 stack[stacki] = 0;
c906108c
SS
13100
13101 while (i < size)
13102 {
c906108c
SS
13103 op = data[i++];
13104 switch (op)
13105 {
f1bea926
JM
13106 case DW_OP_lit0:
13107 case DW_OP_lit1:
13108 case DW_OP_lit2:
13109 case DW_OP_lit3:
13110 case DW_OP_lit4:
13111 case DW_OP_lit5:
13112 case DW_OP_lit6:
13113 case DW_OP_lit7:
13114 case DW_OP_lit8:
13115 case DW_OP_lit9:
13116 case DW_OP_lit10:
13117 case DW_OP_lit11:
13118 case DW_OP_lit12:
13119 case DW_OP_lit13:
13120 case DW_OP_lit14:
13121 case DW_OP_lit15:
13122 case DW_OP_lit16:
13123 case DW_OP_lit17:
13124 case DW_OP_lit18:
13125 case DW_OP_lit19:
13126 case DW_OP_lit20:
13127 case DW_OP_lit21:
13128 case DW_OP_lit22:
13129 case DW_OP_lit23:
13130 case DW_OP_lit24:
13131 case DW_OP_lit25:
13132 case DW_OP_lit26:
13133 case DW_OP_lit27:
13134 case DW_OP_lit28:
13135 case DW_OP_lit29:
13136 case DW_OP_lit30:
13137 case DW_OP_lit31:
13138 stack[++stacki] = op - DW_OP_lit0;
13139 break;
13140
c906108c
SS
13141 case DW_OP_reg0:
13142 case DW_OP_reg1:
13143 case DW_OP_reg2:
13144 case DW_OP_reg3:
13145 case DW_OP_reg4:
13146 case DW_OP_reg5:
13147 case DW_OP_reg6:
13148 case DW_OP_reg7:
13149 case DW_OP_reg8:
13150 case DW_OP_reg9:
13151 case DW_OP_reg10:
13152 case DW_OP_reg11:
13153 case DW_OP_reg12:
13154 case DW_OP_reg13:
13155 case DW_OP_reg14:
13156 case DW_OP_reg15:
13157 case DW_OP_reg16:
13158 case DW_OP_reg17:
13159 case DW_OP_reg18:
13160 case DW_OP_reg19:
13161 case DW_OP_reg20:
13162 case DW_OP_reg21:
13163 case DW_OP_reg22:
13164 case DW_OP_reg23:
13165 case DW_OP_reg24:
13166 case DW_OP_reg25:
13167 case DW_OP_reg26:
13168 case DW_OP_reg27:
13169 case DW_OP_reg28:
13170 case DW_OP_reg29:
13171 case DW_OP_reg30:
13172 case DW_OP_reg31:
c906108c 13173 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13174 if (i < size)
13175 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13176 break;
13177
13178 case DW_OP_regx:
c906108c
SS
13179 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13180 i += bytes_read;
c906108c 13181 stack[++stacki] = unsnd;
4cecd739
DJ
13182 if (i < size)
13183 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13184 break;
13185
13186 case DW_OP_addr:
107d2387 13187 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13188 cu, &bytes_read);
107d2387 13189 i += bytes_read;
c906108c
SS
13190 break;
13191
13192 case DW_OP_const1u:
13193 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13194 i += 1;
13195 break;
13196
13197 case DW_OP_const1s:
13198 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13199 i += 1;
13200 break;
13201
13202 case DW_OP_const2u:
13203 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13204 i += 2;
13205 break;
13206
13207 case DW_OP_const2s:
13208 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13209 i += 2;
13210 break;
13211
13212 case DW_OP_const4u:
13213 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13214 i += 4;
13215 break;
13216
13217 case DW_OP_const4s:
13218 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13219 i += 4;
13220 break;
13221
13222 case DW_OP_constu:
13223 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13224 &bytes_read);
c906108c
SS
13225 i += bytes_read;
13226 break;
13227
13228 case DW_OP_consts:
13229 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13230 i += bytes_read;
13231 break;
13232
f1bea926
JM
13233 case DW_OP_dup:
13234 stack[stacki + 1] = stack[stacki];
13235 stacki++;
13236 break;
13237
c906108c
SS
13238 case DW_OP_plus:
13239 stack[stacki - 1] += stack[stacki];
13240 stacki--;
13241 break;
13242
13243 case DW_OP_plus_uconst:
13244 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13245 i += bytes_read;
13246 break;
13247
13248 case DW_OP_minus:
f1bea926 13249 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13250 stacki--;
13251 break;
13252
7a292a7a 13253 case DW_OP_deref:
7a292a7a 13254 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13255 this using GDB's address_class enum. This is valid for partial
13256 global symbols, although the variable's address will be bogus
13257 in the psymtab. */
7a292a7a 13258 if (i < size)
4d3c2250 13259 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13260 break;
13261
9d774e44 13262 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13263 /* The top of the stack has the offset from the beginning
13264 of the thread control block at which the variable is located. */
13265 /* Nothing should follow this operator, so the top of stack would
13266 be returned. */
4cecd739
DJ
13267 /* This is valid for partial global symbols, but the variable's
13268 address will be bogus in the psymtab. */
9d774e44 13269 if (i < size)
4d3c2250 13270 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13271 break;
13272
42be36b3
CT
13273 case DW_OP_GNU_uninit:
13274 break;
13275
c906108c 13276 default:
e2e0b3e5 13277 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13278 dwarf_stack_op_name (op, 1));
c906108c
SS
13279 return (stack[stacki]);
13280 }
13281 }
13282 return (stack[stacki]);
13283}
13284
13285/* memory allocation interface */
13286
c906108c 13287static struct dwarf_block *
7b5a2f43 13288dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13289{
13290 struct dwarf_block *blk;
13291
13292 blk = (struct dwarf_block *)
7b5a2f43 13293 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13294 return (blk);
13295}
13296
13297static struct abbrev_info *
f3dd6933 13298dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13299{
13300 struct abbrev_info *abbrev;
13301
f3dd6933
DJ
13302 abbrev = (struct abbrev_info *)
13303 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13304 memset (abbrev, 0, sizeof (struct abbrev_info));
13305 return (abbrev);
13306}
13307
13308static struct die_info *
b60c80d6 13309dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13310{
13311 struct die_info *die;
b60c80d6
DJ
13312 size_t size = sizeof (struct die_info);
13313
13314 if (num_attrs > 1)
13315 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13316
b60c80d6 13317 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13318 memset (die, 0, sizeof (struct die_info));
13319 return (die);
13320}
2e276125
JB
13321
13322\f
13323/* Macro support. */
13324
13325
13326/* Return the full name of file number I in *LH's file name table.
13327 Use COMP_DIR as the name of the current directory of the
13328 compilation. The result is allocated using xmalloc; the caller is
13329 responsible for freeing it. */
13330static char *
13331file_full_name (int file, struct line_header *lh, const char *comp_dir)
13332{
6a83a1e6
EZ
13333 /* Is the file number a valid index into the line header's file name
13334 table? Remember that file numbers start with one, not zero. */
13335 if (1 <= file && file <= lh->num_file_names)
13336 {
13337 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13338
6a83a1e6
EZ
13339 if (IS_ABSOLUTE_PATH (fe->name))
13340 return xstrdup (fe->name);
13341 else
13342 {
13343 const char *dir;
13344 int dir_len;
13345 char *full_name;
13346
13347 if (fe->dir_index)
13348 dir = lh->include_dirs[fe->dir_index - 1];
13349 else
13350 dir = comp_dir;
13351
13352 if (dir)
13353 {
13354 dir_len = strlen (dir);
13355 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13356 strcpy (full_name, dir);
13357 full_name[dir_len] = '/';
13358 strcpy (full_name + dir_len + 1, fe->name);
13359 return full_name;
13360 }
13361 else
13362 return xstrdup (fe->name);
13363 }
13364 }
2e276125
JB
13365 else
13366 {
6a83a1e6
EZ
13367 /* The compiler produced a bogus file number. We can at least
13368 record the macro definitions made in the file, even if we
13369 won't be able to find the file by name. */
13370 char fake_name[80];
9a619af0 13371
6a83a1e6 13372 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13373
6e70227d 13374 complaint (&symfile_complaints,
6a83a1e6
EZ
13375 _("bad file number in macro information (%d)"),
13376 file);
2e276125 13377
6a83a1e6 13378 return xstrdup (fake_name);
2e276125
JB
13379 }
13380}
13381
13382
13383static struct macro_source_file *
13384macro_start_file (int file, int line,
13385 struct macro_source_file *current_file,
13386 const char *comp_dir,
13387 struct line_header *lh, struct objfile *objfile)
13388{
13389 /* The full name of this source file. */
13390 char *full_name = file_full_name (file, lh, comp_dir);
13391
13392 /* We don't create a macro table for this compilation unit
13393 at all until we actually get a filename. */
13394 if (! pending_macros)
4a146b47 13395 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13396 objfile->macro_cache);
2e276125
JB
13397
13398 if (! current_file)
13399 /* If we have no current file, then this must be the start_file
13400 directive for the compilation unit's main source file. */
13401 current_file = macro_set_main (pending_macros, full_name);
13402 else
13403 current_file = macro_include (current_file, line, full_name);
13404
13405 xfree (full_name);
6e70227d 13406
2e276125
JB
13407 return current_file;
13408}
13409
13410
13411/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13412 followed by a null byte. */
13413static char *
13414copy_string (const char *buf, int len)
13415{
13416 char *s = xmalloc (len + 1);
9a619af0 13417
2e276125
JB
13418 memcpy (s, buf, len);
13419 s[len] = '\0';
2e276125
JB
13420 return s;
13421}
13422
13423
13424static const char *
13425consume_improper_spaces (const char *p, const char *body)
13426{
13427 if (*p == ' ')
13428 {
4d3c2250 13429 complaint (&symfile_complaints,
e2e0b3e5 13430 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13431 body);
2e276125
JB
13432
13433 while (*p == ' ')
13434 p++;
13435 }
13436
13437 return p;
13438}
13439
13440
13441static void
13442parse_macro_definition (struct macro_source_file *file, int line,
13443 const char *body)
13444{
13445 const char *p;
13446
13447 /* The body string takes one of two forms. For object-like macro
13448 definitions, it should be:
13449
13450 <macro name> " " <definition>
13451
13452 For function-like macro definitions, it should be:
13453
13454 <macro name> "() " <definition>
13455 or
13456 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13457
13458 Spaces may appear only where explicitly indicated, and in the
13459 <definition>.
13460
13461 The Dwarf 2 spec says that an object-like macro's name is always
13462 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13463 the space when the macro's definition is the empty string.
2e276125
JB
13464
13465 The Dwarf 2 spec says that there should be no spaces between the
13466 formal arguments in a function-like macro's formal argument list,
13467 but versions of GCC around March 2002 include spaces after the
13468 commas. */
13469
13470
13471 /* Find the extent of the macro name. The macro name is terminated
13472 by either a space or null character (for an object-like macro) or
13473 an opening paren (for a function-like macro). */
13474 for (p = body; *p; p++)
13475 if (*p == ' ' || *p == '(')
13476 break;
13477
13478 if (*p == ' ' || *p == '\0')
13479 {
13480 /* It's an object-like macro. */
13481 int name_len = p - body;
13482 char *name = copy_string (body, name_len);
13483 const char *replacement;
13484
13485 if (*p == ' ')
13486 replacement = body + name_len + 1;
13487 else
13488 {
4d3c2250 13489 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13490 replacement = body + name_len;
13491 }
6e70227d 13492
2e276125
JB
13493 macro_define_object (file, line, name, replacement);
13494
13495 xfree (name);
13496 }
13497 else if (*p == '(')
13498 {
13499 /* It's a function-like macro. */
13500 char *name = copy_string (body, p - body);
13501 int argc = 0;
13502 int argv_size = 1;
13503 char **argv = xmalloc (argv_size * sizeof (*argv));
13504
13505 p++;
13506
13507 p = consume_improper_spaces (p, body);
13508
13509 /* Parse the formal argument list. */
13510 while (*p && *p != ')')
13511 {
13512 /* Find the extent of the current argument name. */
13513 const char *arg_start = p;
13514
13515 while (*p && *p != ',' && *p != ')' && *p != ' ')
13516 p++;
13517
13518 if (! *p || p == arg_start)
4d3c2250 13519 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13520 else
13521 {
13522 /* Make sure argv has room for the new argument. */
13523 if (argc >= argv_size)
13524 {
13525 argv_size *= 2;
13526 argv = xrealloc (argv, argv_size * sizeof (*argv));
13527 }
13528
13529 argv[argc++] = copy_string (arg_start, p - arg_start);
13530 }
13531
13532 p = consume_improper_spaces (p, body);
13533
13534 /* Consume the comma, if present. */
13535 if (*p == ',')
13536 {
13537 p++;
13538
13539 p = consume_improper_spaces (p, body);
13540 }
13541 }
13542
13543 if (*p == ')')
13544 {
13545 p++;
13546
13547 if (*p == ' ')
13548 /* Perfectly formed definition, no complaints. */
13549 macro_define_function (file, line, name,
6e70227d 13550 argc, (const char **) argv,
2e276125
JB
13551 p + 1);
13552 else if (*p == '\0')
13553 {
13554 /* Complain, but do define it. */
4d3c2250 13555 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13556 macro_define_function (file, line, name,
6e70227d 13557 argc, (const char **) argv,
2e276125
JB
13558 p);
13559 }
13560 else
13561 /* Just complain. */
4d3c2250 13562 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13563 }
13564 else
13565 /* Just complain. */
4d3c2250 13566 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13567
13568 xfree (name);
13569 {
13570 int i;
13571
13572 for (i = 0; i < argc; i++)
13573 xfree (argv[i]);
13574 }
13575 xfree (argv);
13576 }
13577 else
4d3c2250 13578 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13579}
13580
13581
13582static void
13583dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13584 char *comp_dir, bfd *abfd,
e7c27a73 13585 struct dwarf2_cu *cu)
2e276125 13586{
fe1b8b76 13587 gdb_byte *mac_ptr, *mac_end;
2e276125 13588 struct macro_source_file *current_file = 0;
757a13d0
JK
13589 enum dwarf_macinfo_record_type macinfo_type;
13590 int at_commandline;
2e276125 13591
be391dca
TT
13592 dwarf2_read_section (dwarf2_per_objfile->objfile,
13593 &dwarf2_per_objfile->macinfo);
dce234bc 13594 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13595 {
e2e0b3e5 13596 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13597 return;
13598 }
13599
757a13d0
JK
13600 /* First pass: Find the name of the base filename.
13601 This filename is needed in order to process all macros whose definition
13602 (or undefinition) comes from the command line. These macros are defined
13603 before the first DW_MACINFO_start_file entry, and yet still need to be
13604 associated to the base file.
13605
13606 To determine the base file name, we scan the macro definitions until we
13607 reach the first DW_MACINFO_start_file entry. We then initialize
13608 CURRENT_FILE accordingly so that any macro definition found before the
13609 first DW_MACINFO_start_file can still be associated to the base file. */
13610
dce234bc
PP
13611 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13612 mac_end = dwarf2_per_objfile->macinfo.buffer
13613 + dwarf2_per_objfile->macinfo.size;
2e276125 13614
757a13d0 13615 do
2e276125 13616 {
2e276125
JB
13617 /* Do we at least have room for a macinfo type byte? */
13618 if (mac_ptr >= mac_end)
13619 {
757a13d0
JK
13620 /* Complaint is printed during the second pass as GDB will probably
13621 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13622 break;
2e276125
JB
13623 }
13624
13625 macinfo_type = read_1_byte (abfd, mac_ptr);
13626 mac_ptr++;
13627
13628 switch (macinfo_type)
13629 {
13630 /* A zero macinfo type indicates the end of the macro
13631 information. */
13632 case 0:
757a13d0
JK
13633 break;
13634
13635 case DW_MACINFO_define:
13636 case DW_MACINFO_undef:
13637 /* Only skip the data by MAC_PTR. */
13638 {
13639 unsigned int bytes_read;
13640
13641 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13642 mac_ptr += bytes_read;
9b1c24c8 13643 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13644 mac_ptr += bytes_read;
13645 }
13646 break;
13647
13648 case DW_MACINFO_start_file:
13649 {
13650 unsigned int bytes_read;
13651 int line, file;
13652
13653 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13654 mac_ptr += bytes_read;
13655 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13656 mac_ptr += bytes_read;
13657
13658 current_file = macro_start_file (file, line, current_file, comp_dir,
13659 lh, cu->objfile);
13660 }
13661 break;
13662
13663 case DW_MACINFO_end_file:
13664 /* No data to skip by MAC_PTR. */
13665 break;
13666
13667 case DW_MACINFO_vendor_ext:
13668 /* Only skip the data by MAC_PTR. */
13669 {
13670 unsigned int bytes_read;
13671
13672 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13673 mac_ptr += bytes_read;
9b1c24c8 13674 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13675 mac_ptr += bytes_read;
13676 }
13677 break;
13678
13679 default:
13680 break;
13681 }
13682 } while (macinfo_type != 0 && current_file == NULL);
13683
13684 /* Second pass: Process all entries.
13685
13686 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13687 command-line macro definitions/undefinitions. This flag is unset when we
13688 reach the first DW_MACINFO_start_file entry. */
13689
dce234bc 13690 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13691
13692 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13693 GDB is still reading the definitions from command line. First
13694 DW_MACINFO_start_file will need to be ignored as it was already executed
13695 to create CURRENT_FILE for the main source holding also the command line
13696 definitions. On first met DW_MACINFO_start_file this flag is reset to
13697 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13698
13699 at_commandline = 1;
13700
13701 do
13702 {
13703 /* Do we at least have room for a macinfo type byte? */
13704 if (mac_ptr >= mac_end)
13705 {
13706 dwarf2_macros_too_long_complaint ();
13707 break;
13708 }
13709
13710 macinfo_type = read_1_byte (abfd, mac_ptr);
13711 mac_ptr++;
13712
13713 switch (macinfo_type)
13714 {
13715 /* A zero macinfo type indicates the end of the macro
13716 information. */
13717 case 0:
13718 break;
2e276125
JB
13719
13720 case DW_MACINFO_define:
13721 case DW_MACINFO_undef:
13722 {
891d2f0b 13723 unsigned int bytes_read;
2e276125
JB
13724 int line;
13725 char *body;
13726
13727 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13728 mac_ptr += bytes_read;
9b1c24c8 13729 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13730 mac_ptr += bytes_read;
13731
13732 if (! current_file)
757a13d0
JK
13733 {
13734 /* DWARF violation as no main source is present. */
13735 complaint (&symfile_complaints,
13736 _("debug info with no main source gives macro %s "
13737 "on line %d: %s"),
6e70227d
DE
13738 macinfo_type == DW_MACINFO_define ?
13739 _("definition") :
905e0470
PM
13740 macinfo_type == DW_MACINFO_undef ?
13741 _("undefinition") :
13742 _("something-or-other"), line, body);
757a13d0
JK
13743 break;
13744 }
13745 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13746 complaint (&symfile_complaints,
757a13d0
JK
13747 _("debug info gives %s macro %s with %s line %d: %s"),
13748 at_commandline ? _("command-line") : _("in-file"),
905e0470 13749 macinfo_type == DW_MACINFO_define ?
6e70227d 13750 _("definition") :
905e0470
PM
13751 macinfo_type == DW_MACINFO_undef ?
13752 _("undefinition") :
13753 _("something-or-other"),
757a13d0
JK
13754 line == 0 ? _("zero") : _("non-zero"), line, body);
13755
13756 if (macinfo_type == DW_MACINFO_define)
13757 parse_macro_definition (current_file, line, body);
13758 else if (macinfo_type == DW_MACINFO_undef)
13759 macro_undef (current_file, line, body);
2e276125
JB
13760 }
13761 break;
13762
13763 case DW_MACINFO_start_file:
13764 {
891d2f0b 13765 unsigned int bytes_read;
2e276125
JB
13766 int line, file;
13767
13768 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13769 mac_ptr += bytes_read;
13770 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13771 mac_ptr += bytes_read;
13772
757a13d0
JK
13773 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13774 complaint (&symfile_complaints,
13775 _("debug info gives source %d included "
13776 "from %s at %s line %d"),
13777 file, at_commandline ? _("command-line") : _("file"),
13778 line == 0 ? _("zero") : _("non-zero"), line);
13779
13780 if (at_commandline)
13781 {
13782 /* This DW_MACINFO_start_file was executed in the pass one. */
13783 at_commandline = 0;
13784 }
13785 else
13786 current_file = macro_start_file (file, line,
13787 current_file, comp_dir,
13788 lh, cu->objfile);
2e276125
JB
13789 }
13790 break;
13791
13792 case DW_MACINFO_end_file:
13793 if (! current_file)
4d3c2250 13794 complaint (&symfile_complaints,
e2e0b3e5 13795 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13796 else
13797 {
13798 current_file = current_file->included_by;
13799 if (! current_file)
13800 {
13801 enum dwarf_macinfo_record_type next_type;
13802
13803 /* GCC circa March 2002 doesn't produce the zero
13804 type byte marking the end of the compilation
13805 unit. Complain if it's not there, but exit no
13806 matter what. */
13807
13808 /* Do we at least have room for a macinfo type byte? */
13809 if (mac_ptr >= mac_end)
13810 {
4d3c2250 13811 dwarf2_macros_too_long_complaint ();
2e276125
JB
13812 return;
13813 }
13814
13815 /* We don't increment mac_ptr here, so this is just
13816 a look-ahead. */
13817 next_type = read_1_byte (abfd, mac_ptr);
13818 if (next_type != 0)
4d3c2250 13819 complaint (&symfile_complaints,
e2e0b3e5 13820 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13821
13822 return;
13823 }
13824 }
13825 break;
13826
13827 case DW_MACINFO_vendor_ext:
13828 {
891d2f0b 13829 unsigned int bytes_read;
2e276125
JB
13830 int constant;
13831 char *string;
13832
13833 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13834 mac_ptr += bytes_read;
9b1c24c8 13835 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13836 mac_ptr += bytes_read;
13837
13838 /* We don't recognize any vendor extensions. */
13839 }
13840 break;
13841 }
757a13d0 13842 } while (macinfo_type != 0);
2e276125 13843}
8e19ed76
PS
13844
13845/* Check if the attribute's form is a DW_FORM_block*
13846 if so return true else false. */
13847static int
13848attr_form_is_block (struct attribute *attr)
13849{
13850 return (attr == NULL ? 0 :
13851 attr->form == DW_FORM_block1
13852 || attr->form == DW_FORM_block2
13853 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13854 || attr->form == DW_FORM_block
13855 || attr->form == DW_FORM_exprloc);
8e19ed76 13856}
4c2df51b 13857
c6a0999f
JB
13858/* Return non-zero if ATTR's value is a section offset --- classes
13859 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13860 You may use DW_UNSND (attr) to retrieve such offsets.
13861
13862 Section 7.5.4, "Attribute Encodings", explains that no attribute
13863 may have a value that belongs to more than one of these classes; it
13864 would be ambiguous if we did, because we use the same forms for all
13865 of them. */
3690dd37
JB
13866static int
13867attr_form_is_section_offset (struct attribute *attr)
13868{
13869 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13870 || attr->form == DW_FORM_data8
13871 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13872}
13873
13874
13875/* Return non-zero if ATTR's value falls in the 'constant' class, or
13876 zero otherwise. When this function returns true, you can apply
13877 dwarf2_get_attr_constant_value to it.
13878
13879 However, note that for some attributes you must check
13880 attr_form_is_section_offset before using this test. DW_FORM_data4
13881 and DW_FORM_data8 are members of both the constant class, and of
13882 the classes that contain offsets into other debug sections
13883 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13884 that, if an attribute's can be either a constant or one of the
13885 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13886 taken as section offsets, not constants. */
13887static int
13888attr_form_is_constant (struct attribute *attr)
13889{
13890 switch (attr->form)
13891 {
13892 case DW_FORM_sdata:
13893 case DW_FORM_udata:
13894 case DW_FORM_data1:
13895 case DW_FORM_data2:
13896 case DW_FORM_data4:
13897 case DW_FORM_data8:
13898 return 1;
13899 default:
13900 return 0;
13901 }
13902}
13903
4c2df51b
DJ
13904static void
13905dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13906 struct dwarf2_cu *cu)
4c2df51b 13907{
3690dd37 13908 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13909 /* ".debug_loc" may not exist at all, or the offset may be outside
13910 the section. If so, fall through to the complaint in the
13911 other branch. */
dce234bc 13912 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13913 {
0d53c4c4 13914 struct dwarf2_loclist_baton *baton;
4c2df51b 13915
4a146b47 13916 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13917 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13918 baton->per_cu = cu->per_cu;
13919 gdb_assert (baton->per_cu);
4c2df51b 13920
be391dca
TT
13921 dwarf2_read_section (dwarf2_per_objfile->objfile,
13922 &dwarf2_per_objfile->loc);
13923
0d53c4c4
DJ
13924 /* We don't know how long the location list is, but make sure we
13925 don't run off the edge of the section. */
dce234bc
PP
13926 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13927 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13928 baton->base_address = cu->base_address;
13929 if (cu->base_known == 0)
0d53c4c4 13930 complaint (&symfile_complaints,
e2e0b3e5 13931 _("Location list used without specifying the CU base address."));
4c2df51b 13932
768a979c 13933 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13934 SYMBOL_LOCATION_BATON (sym) = baton;
13935 }
13936 else
13937 {
13938 struct dwarf2_locexpr_baton *baton;
13939
4a146b47 13940 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13941 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13942 baton->per_cu = cu->per_cu;
13943 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13944
13945 if (attr_form_is_block (attr))
13946 {
13947 /* Note that we're just copying the block's data pointer
13948 here, not the actual data. We're still pointing into the
6502dd73
DJ
13949 info_buffer for SYM's objfile; right now we never release
13950 that buffer, but when we do clean up properly this may
13951 need to change. */
0d53c4c4
DJ
13952 baton->size = DW_BLOCK (attr)->size;
13953 baton->data = DW_BLOCK (attr)->data;
13954 }
13955 else
13956 {
13957 dwarf2_invalid_attrib_class_complaint ("location description",
13958 SYMBOL_NATURAL_NAME (sym));
13959 baton->size = 0;
13960 baton->data = NULL;
13961 }
6e70227d 13962
768a979c 13963 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13964 SYMBOL_LOCATION_BATON (sym) = baton;
13965 }
4c2df51b 13966}
6502dd73 13967
9aa1f1e3
TT
13968/* Return the OBJFILE associated with the compilation unit CU. If CU
13969 came from a separate debuginfo file, then the master objfile is
13970 returned. */
ae0d2f24
UW
13971
13972struct objfile *
13973dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13974{
9291a0cd 13975 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13976
13977 /* Return the master objfile, so that we can report and look up the
13978 correct file containing this variable. */
13979 if (objfile->separate_debug_objfile_backlink)
13980 objfile = objfile->separate_debug_objfile_backlink;
13981
13982 return objfile;
13983}
13984
13985/* Return the address size given in the compilation unit header for CU. */
13986
13987CORE_ADDR
13988dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13989{
13990 if (per_cu->cu)
13991 return per_cu->cu->header.addr_size;
13992 else
13993 {
13994 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13995 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13996 struct dwarf2_per_objfile *per_objfile
13997 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13998 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13999 struct comp_unit_head cu_header;
9a619af0 14000
ae0d2f24
UW
14001 memset (&cu_header, 0, sizeof cu_header);
14002 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14003 return cu_header.addr_size;
14004 }
14005}
14006
9eae7c52
TT
14007/* Return the offset size given in the compilation unit header for CU. */
14008
14009int
14010dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14011{
14012 if (per_cu->cu)
14013 return per_cu->cu->header.offset_size;
14014 else
14015 {
14016 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14017 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14018 struct dwarf2_per_objfile *per_objfile
14019 = objfile_data (objfile, dwarf2_objfile_data_key);
14020 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14021 struct comp_unit_head cu_header;
14022
14023 memset (&cu_header, 0, sizeof cu_header);
14024 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14025 return cu_header.offset_size;
14026 }
14027}
14028
9aa1f1e3
TT
14029/* Return the text offset of the CU. The returned offset comes from
14030 this CU's objfile. If this objfile came from a separate debuginfo
14031 file, then the offset may be different from the corresponding
14032 offset in the parent objfile. */
14033
14034CORE_ADDR
14035dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14036{
bb3fa9d0 14037 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14038
14039 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14040}
14041
348e048f
DE
14042/* Locate the .debug_info compilation unit from CU's objfile which contains
14043 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14044
14045static struct dwarf2_per_cu_data *
c764a876 14046dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14047 struct objfile *objfile)
14048{
14049 struct dwarf2_per_cu_data *this_cu;
14050 int low, high;
14051
ae038cb0
DJ
14052 low = 0;
14053 high = dwarf2_per_objfile->n_comp_units - 1;
14054 while (high > low)
14055 {
14056 int mid = low + (high - low) / 2;
9a619af0 14057
ae038cb0
DJ
14058 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14059 high = mid;
14060 else
14061 low = mid + 1;
14062 }
14063 gdb_assert (low == high);
14064 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14065 {
10b3939b 14066 if (low == 0)
8a3fe4f8
AC
14067 error (_("Dwarf Error: could not find partial DIE containing "
14068 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14069 (long) offset, bfd_get_filename (objfile->obfd));
14070
ae038cb0
DJ
14071 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14072 return dwarf2_per_objfile->all_comp_units[low-1];
14073 }
14074 else
14075 {
14076 this_cu = dwarf2_per_objfile->all_comp_units[low];
14077 if (low == dwarf2_per_objfile->n_comp_units - 1
14078 && offset >= this_cu->offset + this_cu->length)
c764a876 14079 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14080 gdb_assert (offset < this_cu->offset + this_cu->length);
14081 return this_cu;
14082 }
14083}
14084
10b3939b
DJ
14085/* Locate the compilation unit from OBJFILE which is located at exactly
14086 OFFSET. Raises an error on failure. */
14087
ae038cb0 14088static struct dwarf2_per_cu_data *
c764a876 14089dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14090{
14091 struct dwarf2_per_cu_data *this_cu;
9a619af0 14092
ae038cb0
DJ
14093 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14094 if (this_cu->offset != offset)
c764a876 14095 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14096 return this_cu;
14097}
14098
93311388
DE
14099/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14100
14101static struct dwarf2_cu *
14102alloc_one_comp_unit (struct objfile *objfile)
14103{
14104 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14105 cu->objfile = objfile;
14106 obstack_init (&cu->comp_unit_obstack);
14107 return cu;
14108}
14109
ae038cb0
DJ
14110/* Release one cached compilation unit, CU. We unlink it from the tree
14111 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14112 the caller is responsible for that.
14113 NOTE: DATA is a void * because this function is also used as a
14114 cleanup routine. */
ae038cb0
DJ
14115
14116static void
14117free_one_comp_unit (void *data)
14118{
14119 struct dwarf2_cu *cu = data;
14120
14121 if (cu->per_cu != NULL)
14122 cu->per_cu->cu = NULL;
14123 cu->per_cu = NULL;
14124
14125 obstack_free (&cu->comp_unit_obstack, NULL);
14126
14127 xfree (cu);
14128}
14129
72bf9492 14130/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14131 when we're finished with it. We can't free the pointer itself, but be
14132 sure to unlink it from the cache. Also release any associated storage
14133 and perform cache maintenance.
72bf9492
DJ
14134
14135 Only used during partial symbol parsing. */
14136
14137static void
14138free_stack_comp_unit (void *data)
14139{
14140 struct dwarf2_cu *cu = data;
14141
14142 obstack_free (&cu->comp_unit_obstack, NULL);
14143 cu->partial_dies = NULL;
ae038cb0
DJ
14144
14145 if (cu->per_cu != NULL)
14146 {
14147 /* This compilation unit is on the stack in our caller, so we
14148 should not xfree it. Just unlink it. */
14149 cu->per_cu->cu = NULL;
14150 cu->per_cu = NULL;
14151
14152 /* If we had a per-cu pointer, then we may have other compilation
14153 units loaded, so age them now. */
14154 age_cached_comp_units ();
14155 }
14156}
14157
14158/* Free all cached compilation units. */
14159
14160static void
14161free_cached_comp_units (void *data)
14162{
14163 struct dwarf2_per_cu_data *per_cu, **last_chain;
14164
14165 per_cu = dwarf2_per_objfile->read_in_chain;
14166 last_chain = &dwarf2_per_objfile->read_in_chain;
14167 while (per_cu != NULL)
14168 {
14169 struct dwarf2_per_cu_data *next_cu;
14170
14171 next_cu = per_cu->cu->read_in_chain;
14172
14173 free_one_comp_unit (per_cu->cu);
14174 *last_chain = next_cu;
14175
14176 per_cu = next_cu;
14177 }
14178}
14179
14180/* Increase the age counter on each cached compilation unit, and free
14181 any that are too old. */
14182
14183static void
14184age_cached_comp_units (void)
14185{
14186 struct dwarf2_per_cu_data *per_cu, **last_chain;
14187
14188 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14189 per_cu = dwarf2_per_objfile->read_in_chain;
14190 while (per_cu != NULL)
14191 {
14192 per_cu->cu->last_used ++;
14193 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14194 dwarf2_mark (per_cu->cu);
14195 per_cu = per_cu->cu->read_in_chain;
14196 }
14197
14198 per_cu = dwarf2_per_objfile->read_in_chain;
14199 last_chain = &dwarf2_per_objfile->read_in_chain;
14200 while (per_cu != NULL)
14201 {
14202 struct dwarf2_per_cu_data *next_cu;
14203
14204 next_cu = per_cu->cu->read_in_chain;
14205
14206 if (!per_cu->cu->mark)
14207 {
14208 free_one_comp_unit (per_cu->cu);
14209 *last_chain = next_cu;
14210 }
14211 else
14212 last_chain = &per_cu->cu->read_in_chain;
14213
14214 per_cu = next_cu;
14215 }
14216}
14217
14218/* Remove a single compilation unit from the cache. */
14219
14220static void
14221free_one_cached_comp_unit (void *target_cu)
14222{
14223 struct dwarf2_per_cu_data *per_cu, **last_chain;
14224
14225 per_cu = dwarf2_per_objfile->read_in_chain;
14226 last_chain = &dwarf2_per_objfile->read_in_chain;
14227 while (per_cu != NULL)
14228 {
14229 struct dwarf2_per_cu_data *next_cu;
14230
14231 next_cu = per_cu->cu->read_in_chain;
14232
14233 if (per_cu->cu == target_cu)
14234 {
14235 free_one_comp_unit (per_cu->cu);
14236 *last_chain = next_cu;
14237 break;
14238 }
14239 else
14240 last_chain = &per_cu->cu->read_in_chain;
14241
14242 per_cu = next_cu;
14243 }
14244}
14245
fe3e1990
DJ
14246/* Release all extra memory associated with OBJFILE. */
14247
14248void
14249dwarf2_free_objfile (struct objfile *objfile)
14250{
14251 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14252
14253 if (dwarf2_per_objfile == NULL)
14254 return;
14255
14256 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14257 free_cached_comp_units (NULL);
14258
9291a0cd
TT
14259 if (dwarf2_per_objfile->using_index)
14260 {
14261 int i;
14262
14263 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14264 {
14265 int j;
e254ef6a
DE
14266 struct dwarf2_per_cu_data *per_cu =
14267 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14268
e254ef6a 14269 if (!per_cu->v.quick->lines)
9291a0cd
TT
14270 continue;
14271
e254ef6a 14272 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14273 {
e254ef6a
DE
14274 if (per_cu->v.quick->file_names)
14275 xfree ((void *) per_cu->v.quick->file_names[j]);
14276 if (per_cu->v.quick->full_names)
14277 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14278 }
14279
e254ef6a 14280 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14281 }
14282 }
14283
fe3e1990
DJ
14284 /* Everything else should be on the objfile obstack. */
14285}
14286
1c379e20
DJ
14287/* A pair of DIE offset and GDB type pointer. We store these
14288 in a hash table separate from the DIEs, and preserve them
14289 when the DIEs are flushed out of cache. */
14290
14291struct dwarf2_offset_and_type
14292{
14293 unsigned int offset;
14294 struct type *type;
14295};
14296
14297/* Hash function for a dwarf2_offset_and_type. */
14298
14299static hashval_t
14300offset_and_type_hash (const void *item)
14301{
14302 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14303
1c379e20
DJ
14304 return ofs->offset;
14305}
14306
14307/* Equality function for a dwarf2_offset_and_type. */
14308
14309static int
14310offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14311{
14312 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14313 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14314
1c379e20
DJ
14315 return ofs_lhs->offset == ofs_rhs->offset;
14316}
14317
14318/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14319 table if necessary. For convenience, return TYPE.
14320
14321 The DIEs reading must have careful ordering to:
14322 * Not cause infite loops trying to read in DIEs as a prerequisite for
14323 reading current DIE.
14324 * Not trying to dereference contents of still incompletely read in types
14325 while reading in other DIEs.
14326 * Enable referencing still incompletely read in types just by a pointer to
14327 the type without accessing its fields.
14328
14329 Therefore caller should follow these rules:
14330 * Try to fetch any prerequisite types we may need to build this DIE type
14331 before building the type and calling set_die_type.
e71ec853 14332 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14333 possible before fetching more types to complete the current type.
14334 * Make the type as complete as possible before fetching more types. */
1c379e20 14335
f792889a 14336static struct type *
1c379e20
DJ
14337set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14338{
14339 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14340 struct objfile *objfile = cu->objfile;
14341 htab_t *type_hash_ptr;
1c379e20 14342
b4ba55a1
JB
14343 /* For Ada types, make sure that the gnat-specific data is always
14344 initialized (if not already set). There are a few types where
14345 we should not be doing so, because the type-specific area is
14346 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14347 where the type-specific area is used to store the floatformat).
14348 But this is not a problem, because the gnat-specific information
14349 is actually not needed for these types. */
14350 if (need_gnat_info (cu)
14351 && TYPE_CODE (type) != TYPE_CODE_FUNC
14352 && TYPE_CODE (type) != TYPE_CODE_FLT
14353 && !HAVE_GNAT_AUX_INFO (type))
14354 INIT_GNAT_SPECIFIC (type);
14355
673bfd45
DE
14356 if (cu->per_cu->from_debug_types)
14357 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14358 else
14359 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14360
14361 if (*type_hash_ptr == NULL)
f792889a 14362 {
673bfd45
DE
14363 *type_hash_ptr
14364 = htab_create_alloc_ex (127,
f792889a
DJ
14365 offset_and_type_hash,
14366 offset_and_type_eq,
14367 NULL,
673bfd45 14368 &objfile->objfile_obstack,
f792889a
DJ
14369 hashtab_obstack_allocate,
14370 dummy_obstack_deallocate);
f792889a 14371 }
1c379e20
DJ
14372
14373 ofs.offset = die->offset;
14374 ofs.type = type;
14375 slot = (struct dwarf2_offset_and_type **)
673bfd45 14376 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14377 if (*slot)
14378 complaint (&symfile_complaints,
14379 _("A problem internal to GDB: DIE 0x%x has type already set"),
14380 die->offset);
673bfd45 14381 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14382 **slot = ofs;
f792889a 14383 return type;
1c379e20
DJ
14384}
14385
673bfd45
DE
14386/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14387 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14388
14389static struct type *
673bfd45
DE
14390get_die_type_at_offset (unsigned int offset,
14391 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14392{
14393 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14394 htab_t type_hash;
f792889a 14395
673bfd45
DE
14396 if (per_cu->from_debug_types)
14397 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14398 else
14399 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14400 if (type_hash == NULL)
14401 return NULL;
1c379e20 14402
673bfd45 14403 ofs.offset = offset;
1c379e20
DJ
14404 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14405 if (slot)
14406 return slot->type;
14407 else
14408 return NULL;
14409}
14410
673bfd45
DE
14411/* Look up the type for DIE in the appropriate type_hash table,
14412 or return NULL if DIE does not have a saved type. */
14413
14414static struct type *
14415get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14416{
14417 return get_die_type_at_offset (die->offset, cu->per_cu);
14418}
14419
10b3939b
DJ
14420/* Add a dependence relationship from CU to REF_PER_CU. */
14421
14422static void
14423dwarf2_add_dependence (struct dwarf2_cu *cu,
14424 struct dwarf2_per_cu_data *ref_per_cu)
14425{
14426 void **slot;
14427
14428 if (cu->dependencies == NULL)
14429 cu->dependencies
14430 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14431 NULL, &cu->comp_unit_obstack,
14432 hashtab_obstack_allocate,
14433 dummy_obstack_deallocate);
14434
14435 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14436 if (*slot == NULL)
14437 *slot = ref_per_cu;
14438}
1c379e20 14439
f504f079
DE
14440/* Subroutine of dwarf2_mark to pass to htab_traverse.
14441 Set the mark field in every compilation unit in the
ae038cb0
DJ
14442 cache that we must keep because we are keeping CU. */
14443
10b3939b
DJ
14444static int
14445dwarf2_mark_helper (void **slot, void *data)
14446{
14447 struct dwarf2_per_cu_data *per_cu;
14448
14449 per_cu = (struct dwarf2_per_cu_data *) *slot;
14450 if (per_cu->cu->mark)
14451 return 1;
14452 per_cu->cu->mark = 1;
14453
14454 if (per_cu->cu->dependencies != NULL)
14455 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14456
14457 return 1;
14458}
14459
f504f079
DE
14460/* Set the mark field in CU and in every other compilation unit in the
14461 cache that we must keep because we are keeping CU. */
14462
ae038cb0
DJ
14463static void
14464dwarf2_mark (struct dwarf2_cu *cu)
14465{
14466 if (cu->mark)
14467 return;
14468 cu->mark = 1;
10b3939b
DJ
14469 if (cu->dependencies != NULL)
14470 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14471}
14472
14473static void
14474dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14475{
14476 while (per_cu)
14477 {
14478 per_cu->cu->mark = 0;
14479 per_cu = per_cu->cu->read_in_chain;
14480 }
72bf9492
DJ
14481}
14482
72bf9492
DJ
14483/* Trivial hash function for partial_die_info: the hash value of a DIE
14484 is its offset in .debug_info for this objfile. */
14485
14486static hashval_t
14487partial_die_hash (const void *item)
14488{
14489 const struct partial_die_info *part_die = item;
9a619af0 14490
72bf9492
DJ
14491 return part_die->offset;
14492}
14493
14494/* Trivial comparison function for partial_die_info structures: two DIEs
14495 are equal if they have the same offset. */
14496
14497static int
14498partial_die_eq (const void *item_lhs, const void *item_rhs)
14499{
14500 const struct partial_die_info *part_die_lhs = item_lhs;
14501 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14502
72bf9492
DJ
14503 return part_die_lhs->offset == part_die_rhs->offset;
14504}
14505
ae038cb0
DJ
14506static struct cmd_list_element *set_dwarf2_cmdlist;
14507static struct cmd_list_element *show_dwarf2_cmdlist;
14508
14509static void
14510set_dwarf2_cmd (char *args, int from_tty)
14511{
14512 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14513}
14514
14515static void
14516show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14517{
ae038cb0
DJ
14518 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14519}
14520
dce234bc
PP
14521/* If section described by INFO was mmapped, munmap it now. */
14522
14523static void
14524munmap_section_buffer (struct dwarf2_section_info *info)
14525{
14526 if (info->was_mmapped)
14527 {
14528#ifdef HAVE_MMAP
14529 intptr_t begin = (intptr_t) info->buffer;
14530 intptr_t map_begin = begin & ~(pagesize - 1);
14531 size_t map_length = info->size + begin - map_begin;
9a619af0 14532
dce234bc
PP
14533 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14534#else
14535 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14536 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14537#endif
14538 }
14539}
14540
14541/* munmap debug sections for OBJFILE, if necessary. */
14542
14543static void
c1bd65d0 14544dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14545{
14546 struct dwarf2_per_objfile *data = d;
9a619af0 14547
16be1145
DE
14548 /* This is sorted according to the order they're defined in to make it easier
14549 to keep in sync. */
dce234bc
PP
14550 munmap_section_buffer (&data->info);
14551 munmap_section_buffer (&data->abbrev);
14552 munmap_section_buffer (&data->line);
16be1145 14553 munmap_section_buffer (&data->loc);
dce234bc 14554 munmap_section_buffer (&data->macinfo);
16be1145 14555 munmap_section_buffer (&data->str);
dce234bc 14556 munmap_section_buffer (&data->ranges);
16be1145 14557 munmap_section_buffer (&data->types);
dce234bc
PP
14558 munmap_section_buffer (&data->frame);
14559 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14560 munmap_section_buffer (&data->gdb_index);
14561}
14562
14563\f
14564
14565/* The contents of the hash table we create when building the string
14566 table. */
14567struct strtab_entry
14568{
14569 offset_type offset;
14570 const char *str;
14571};
14572
14573/* Hash function for a strtab_entry. */
14574static hashval_t
14575hash_strtab_entry (const void *e)
14576{
14577 const struct strtab_entry *entry = e;
14578 return mapped_index_string_hash (entry->str);
14579}
14580
14581/* Equality function for a strtab_entry. */
14582static int
14583eq_strtab_entry (const void *a, const void *b)
14584{
14585 const struct strtab_entry *ea = a;
14586 const struct strtab_entry *eb = b;
14587 return !strcmp (ea->str, eb->str);
14588}
14589
14590/* Create a strtab_entry hash table. */
14591static htab_t
14592create_strtab (void)
14593{
14594 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14595 xfree, xcalloc, xfree);
14596}
14597
14598/* Add a string to the constant pool. Return the string's offset in
14599 host order. */
14600static offset_type
14601add_string (htab_t table, struct obstack *cpool, const char *str)
14602{
14603 void **slot;
14604 struct strtab_entry entry;
14605 struct strtab_entry *result;
14606
14607 entry.str = str;
14608 slot = htab_find_slot (table, &entry, INSERT);
14609 if (*slot)
14610 result = *slot;
14611 else
14612 {
14613 result = XNEW (struct strtab_entry);
14614 result->offset = obstack_object_size (cpool);
14615 result->str = str;
14616 obstack_grow_str0 (cpool, str);
14617 *slot = result;
14618 }
14619 return result->offset;
14620}
14621
14622/* An entry in the symbol table. */
14623struct symtab_index_entry
14624{
14625 /* The name of the symbol. */
14626 const char *name;
14627 /* The offset of the name in the constant pool. */
14628 offset_type index_offset;
14629 /* A sorted vector of the indices of all the CUs that hold an object
14630 of this name. */
14631 VEC (offset_type) *cu_indices;
14632};
14633
14634/* The symbol table. This is a power-of-2-sized hash table. */
14635struct mapped_symtab
14636{
14637 offset_type n_elements;
14638 offset_type size;
14639 struct symtab_index_entry **data;
14640};
14641
14642/* Hash function for a symtab_index_entry. */
14643static hashval_t
14644hash_symtab_entry (const void *e)
14645{
14646 const struct symtab_index_entry *entry = e;
14647 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14648 sizeof (offset_type) * VEC_length (offset_type,
14649 entry->cu_indices),
14650 0);
14651}
14652
14653/* Equality function for a symtab_index_entry. */
14654static int
14655eq_symtab_entry (const void *a, const void *b)
14656{
14657 const struct symtab_index_entry *ea = a;
14658 const struct symtab_index_entry *eb = b;
14659 int len = VEC_length (offset_type, ea->cu_indices);
14660 if (len != VEC_length (offset_type, eb->cu_indices))
14661 return 0;
14662 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14663 VEC_address (offset_type, eb->cu_indices),
14664 sizeof (offset_type) * len);
14665}
14666
14667/* Destroy a symtab_index_entry. */
14668static void
14669delete_symtab_entry (void *p)
14670{
14671 struct symtab_index_entry *entry = p;
14672 VEC_free (offset_type, entry->cu_indices);
14673 xfree (entry);
14674}
14675
14676/* Create a hash table holding symtab_index_entry objects. */
14677static htab_t
14678create_index_table (void)
14679{
14680 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14681 delete_symtab_entry, xcalloc, xfree);
14682}
14683
14684/* Create a new mapped symtab object. */
14685static struct mapped_symtab *
14686create_mapped_symtab (void)
14687{
14688 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14689 symtab->n_elements = 0;
14690 symtab->size = 1024;
14691 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14692 return symtab;
14693}
14694
14695/* Destroy a mapped_symtab. */
14696static void
14697cleanup_mapped_symtab (void *p)
14698{
14699 struct mapped_symtab *symtab = p;
14700 /* The contents of the array are freed when the other hash table is
14701 destroyed. */
14702 xfree (symtab->data);
14703 xfree (symtab);
14704}
14705
14706/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14707 the slot. */
14708static struct symtab_index_entry **
14709find_slot (struct mapped_symtab *symtab, const char *name)
14710{
14711 offset_type index, step, hash = mapped_index_string_hash (name);
14712
14713 index = hash & (symtab->size - 1);
14714 step = ((hash * 17) & (symtab->size - 1)) | 1;
14715
14716 for (;;)
14717 {
14718 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14719 return &symtab->data[index];
14720 index = (index + step) & (symtab->size - 1);
14721 }
14722}
14723
14724/* Expand SYMTAB's hash table. */
14725static void
14726hash_expand (struct mapped_symtab *symtab)
14727{
14728 offset_type old_size = symtab->size;
14729 offset_type i;
14730 struct symtab_index_entry **old_entries = symtab->data;
14731
14732 symtab->size *= 2;
14733 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14734
14735 for (i = 0; i < old_size; ++i)
14736 {
14737 if (old_entries[i])
14738 {
14739 struct symtab_index_entry **slot = find_slot (symtab,
14740 old_entries[i]->name);
14741 *slot = old_entries[i];
14742 }
14743 }
14744
14745 xfree (old_entries);
14746}
14747
14748/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14749 is the index of the CU in which the symbol appears. */
14750static void
14751add_index_entry (struct mapped_symtab *symtab, const char *name,
14752 offset_type cu_index)
14753{
14754 struct symtab_index_entry **slot;
14755
14756 ++symtab->n_elements;
14757 if (4 * symtab->n_elements / 3 >= symtab->size)
14758 hash_expand (symtab);
14759
14760 slot = find_slot (symtab, name);
14761 if (!*slot)
14762 {
14763 *slot = XNEW (struct symtab_index_entry);
14764 (*slot)->name = name;
14765 (*slot)->cu_indices = NULL;
14766 }
14767 /* Don't push an index twice. Due to how we add entries we only
14768 have to check the last one. */
14769 if (VEC_empty (offset_type, (*slot)->cu_indices)
14770 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14771 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14772}
14773
14774/* Add a vector of indices to the constant pool. */
14775static offset_type
14776add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14777 struct symtab_index_entry *entry)
14778{
14779 void **slot;
14780
14781 slot = htab_find_slot (index_table, entry, INSERT);
14782 if (!*slot)
14783 {
14784 offset_type len = VEC_length (offset_type, entry->cu_indices);
14785 offset_type val = MAYBE_SWAP (len);
14786 offset_type iter;
14787 int i;
14788
14789 *slot = entry;
14790 entry->index_offset = obstack_object_size (cpool);
14791
14792 obstack_grow (cpool, &val, sizeof (val));
14793 for (i = 0;
14794 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14795 ++i)
14796 {
14797 val = MAYBE_SWAP (iter);
14798 obstack_grow (cpool, &val, sizeof (val));
14799 }
14800 }
14801 else
14802 {
14803 struct symtab_index_entry *old_entry = *slot;
14804 entry->index_offset = old_entry->index_offset;
14805 entry = old_entry;
14806 }
14807 return entry->index_offset;
14808}
14809
14810/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14811 constant pool entries going into the obstack CPOOL. */
14812static void
14813write_hash_table (struct mapped_symtab *symtab,
14814 struct obstack *output, struct obstack *cpool)
14815{
14816 offset_type i;
14817 htab_t index_table;
14818 htab_t str_table;
14819
14820 index_table = create_index_table ();
14821 str_table = create_strtab ();
14822 /* We add all the index vectors to the constant pool first, to
14823 ensure alignment is ok. */
14824 for (i = 0; i < symtab->size; ++i)
14825 {
14826 if (symtab->data[i])
14827 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14828 }
14829
14830 /* Now write out the hash table. */
14831 for (i = 0; i < symtab->size; ++i)
14832 {
14833 offset_type str_off, vec_off;
14834
14835 if (symtab->data[i])
14836 {
14837 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14838 vec_off = symtab->data[i]->index_offset;
14839 }
14840 else
14841 {
14842 /* While 0 is a valid constant pool index, it is not valid
14843 to have 0 for both offsets. */
14844 str_off = 0;
14845 vec_off = 0;
14846 }
14847
14848 str_off = MAYBE_SWAP (str_off);
14849 vec_off = MAYBE_SWAP (vec_off);
14850
14851 obstack_grow (output, &str_off, sizeof (str_off));
14852 obstack_grow (output, &vec_off, sizeof (vec_off));
14853 }
14854
14855 htab_delete (str_table);
14856 htab_delete (index_table);
14857}
14858
14859/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14860 from PST; CU_INDEX is the index of the CU in the vector of all
14861 CUs. */
14862static void
14863add_address_entry (struct objfile *objfile,
14864 struct obstack *addr_obstack, struct partial_symtab *pst,
14865 unsigned int cu_index)
14866{
14867 offset_type offset;
14868 char addr[8];
14869 CORE_ADDR baseaddr;
14870
1fd400ff
TT
14871 /* Don't bother recording empty ranges. */
14872 if (pst->textlow == pst->texthigh)
14873 return;
14874
9291a0cd
TT
14875 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14876
14877 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14878 obstack_grow (addr_obstack, addr, 8);
14879 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14880 obstack_grow (addr_obstack, addr, 8);
14881 offset = MAYBE_SWAP (cu_index);
14882 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14883}
14884
14885/* Add a list of partial symbols to SYMTAB. */
14886static void
14887write_psymbols (struct mapped_symtab *symtab,
14888 struct partial_symbol **psymp,
14889 int count,
14890 offset_type cu_index)
14891{
14892 for (; count-- > 0; ++psymp)
14893 {
14894 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14895 error (_("Ada is not currently supported by the index"));
14896 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14897 }
14898}
14899
14900/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14901 exception if there is an error. */
14902static void
14903write_obstack (FILE *file, struct obstack *obstack)
14904{
14905 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14906 file)
14907 != obstack_object_size (obstack))
14908 error (_("couldn't data write to file"));
14909}
14910
14911/* Unlink a file if the argument is not NULL. */
14912static void
14913unlink_if_set (void *p)
14914{
14915 char **filename = p;
14916 if (*filename)
14917 unlink (*filename);
14918}
14919
1fd400ff
TT
14920/* A helper struct used when iterating over debug_types. */
14921struct signatured_type_index_data
14922{
14923 struct objfile *objfile;
14924 struct mapped_symtab *symtab;
14925 struct obstack *types_list;
14926 int cu_index;
14927};
14928
14929/* A helper function that writes a single signatured_type to an
14930 obstack. */
14931static int
14932write_one_signatured_type (void **slot, void *d)
14933{
14934 struct signatured_type_index_data *info = d;
14935 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
14936 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
14937 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
14938 gdb_byte val[8];
14939
14940 write_psymbols (info->symtab,
14941 info->objfile->global_psymbols.list + psymtab->globals_offset,
14942 psymtab->n_global_syms, info->cu_index);
14943 write_psymbols (info->symtab,
14944 info->objfile->static_psymbols.list + psymtab->statics_offset,
14945 psymtab->n_static_syms, info->cu_index);
14946
14947 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14948 obstack_grow (info->types_list, val, 8);
14949 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14950 obstack_grow (info->types_list, val, 8);
14951 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14952 obstack_grow (info->types_list, val, 8);
14953
14954 ++info->cu_index;
14955
14956 return 1;
14957}
14958
9291a0cd
TT
14959/* Create an index file for OBJFILE in the directory DIR. */
14960static void
14961write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14962{
14963 struct cleanup *cleanup;
14964 char *filename, *cleanup_filename;
1fd400ff
TT
14965 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14966 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14967 int i;
14968 FILE *out_file;
14969 struct mapped_symtab *symtab;
14970 offset_type val, size_of_contents, total_len;
14971 struct stat st;
14972 char buf[8];
14973
14974 if (!objfile->psymtabs)
14975 return;
14976 if (dwarf2_per_objfile->using_index)
14977 error (_("Cannot use an index to create the index"));
14978
14979 if (stat (objfile->name, &st) < 0)
14980 perror_with_name (_("Could not stat"));
14981
14982 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14983 INDEX_SUFFIX, (char *) NULL);
14984 cleanup = make_cleanup (xfree, filename);
14985
14986 out_file = fopen (filename, "wb");
14987 if (!out_file)
14988 error (_("Can't open `%s' for writing"), filename);
14989
14990 cleanup_filename = filename;
14991 make_cleanup (unlink_if_set, &cleanup_filename);
14992
14993 symtab = create_mapped_symtab ();
14994 make_cleanup (cleanup_mapped_symtab, symtab);
14995
14996 obstack_init (&addr_obstack);
14997 make_cleanup_obstack_free (&addr_obstack);
14998
14999 obstack_init (&cu_list);
15000 make_cleanup_obstack_free (&cu_list);
15001
1fd400ff
TT
15002 obstack_init (&types_cu_list);
15003 make_cleanup_obstack_free (&types_cu_list);
15004
15005 /* The list is already sorted, so we don't need to do additional
15006 work here. Also, the debug_types entries do not appear in
15007 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15008 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15009 {
e254ef6a
DE
15010 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15011 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15012 gdb_byte val[8];
15013
15014 write_psymbols (symtab,
15015 objfile->global_psymbols.list + psymtab->globals_offset,
15016 psymtab->n_global_syms, i);
15017 write_psymbols (symtab,
15018 objfile->static_psymbols.list + psymtab->statics_offset,
15019 psymtab->n_static_syms, i);
15020
15021 add_address_entry (objfile, &addr_obstack, psymtab, i);
15022
e254ef6a 15023 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15024 obstack_grow (&cu_list, val, 8);
e254ef6a 15025 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15026 obstack_grow (&cu_list, val, 8);
15027 }
15028
1fd400ff
TT
15029 /* Write out the .debug_type entries, if any. */
15030 if (dwarf2_per_objfile->signatured_types)
15031 {
15032 struct signatured_type_index_data sig_data;
15033
15034 sig_data.objfile = objfile;
15035 sig_data.symtab = symtab;
15036 sig_data.types_list = &types_cu_list;
15037 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15038 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15039 write_one_signatured_type, &sig_data);
15040 }
15041
9291a0cd
TT
15042 obstack_init (&constant_pool);
15043 make_cleanup_obstack_free (&constant_pool);
15044 obstack_init (&symtab_obstack);
15045 make_cleanup_obstack_free (&symtab_obstack);
15046 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15047
15048 obstack_init (&contents);
15049 make_cleanup_obstack_free (&contents);
1fd400ff 15050 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15051 total_len = size_of_contents;
15052
15053 /* The version number. */
1fd400ff 15054 val = MAYBE_SWAP (2);
9291a0cd
TT
15055 obstack_grow (&contents, &val, sizeof (val));
15056
15057 /* The offset of the CU list from the start of the file. */
15058 val = MAYBE_SWAP (total_len);
15059 obstack_grow (&contents, &val, sizeof (val));
15060 total_len += obstack_object_size (&cu_list);
15061
1fd400ff
TT
15062 /* The offset of the types CU list from the start of the file. */
15063 val = MAYBE_SWAP (total_len);
15064 obstack_grow (&contents, &val, sizeof (val));
15065 total_len += obstack_object_size (&types_cu_list);
15066
9291a0cd
TT
15067 /* The offset of the address table from the start of the file. */
15068 val = MAYBE_SWAP (total_len);
15069 obstack_grow (&contents, &val, sizeof (val));
15070 total_len += obstack_object_size (&addr_obstack);
15071
15072 /* The offset of the symbol table from the start of the file. */
15073 val = MAYBE_SWAP (total_len);
15074 obstack_grow (&contents, &val, sizeof (val));
15075 total_len += obstack_object_size (&symtab_obstack);
15076
15077 /* The offset of the constant pool from the start of the file. */
15078 val = MAYBE_SWAP (total_len);
15079 obstack_grow (&contents, &val, sizeof (val));
15080 total_len += obstack_object_size (&constant_pool);
15081
15082 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15083
15084 write_obstack (out_file, &contents);
15085 write_obstack (out_file, &cu_list);
1fd400ff 15086 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15087 write_obstack (out_file, &addr_obstack);
15088 write_obstack (out_file, &symtab_obstack);
15089 write_obstack (out_file, &constant_pool);
15090
15091 fclose (out_file);
15092
15093 /* We want to keep the file, so we set cleanup_filename to NULL
15094 here. See unlink_if_set. */
15095 cleanup_filename = NULL;
15096
15097 do_cleanups (cleanup);
15098}
15099
15100/* The mapped index file format is designed to be directly mmap()able
15101 on any architecture. In most cases, a datum is represented using a
15102 little-endian 32-bit integer value, called an offset_type. Big
15103 endian machines must byte-swap the values before using them.
15104 Exceptions to this rule are noted. The data is laid out such that
15105 alignment is always respected.
15106
15107 A mapped index consists of several sections.
15108
15109 1. The file header. This is a sequence of values, of offset_type
15110 unless otherwise noted:
1fd400ff
TT
15111 [0] The version number. Currently 1 or 2. The differences are
15112 noted below. Version 1 did not account for .debug_types sections;
15113 the presence of a .debug_types section invalidates any version 1
15114 index that may exist.
9291a0cd 15115 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
15116 [1.5] In version 2, the offset, from the start of the file, of the
15117 types CU list. This offset does not appear in version 1. Note
15118 that this can be empty, in which case this offset will be equal to
15119 the next offset.
9291a0cd
TT
15120 [2] The offset, from the start of the file, of the address section.
15121 [3] The offset, from the start of the file, of the symbol table.
15122 [4] The offset, from the start of the file, of the constant pool.
15123
15124 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15125 little-endian values, sorted by the CU offset. The first element
15126 in each pair is the offset of a CU in the .debug_info section. The
15127 second element in each pair is the length of that CU. References
15128 to a CU elsewhere in the map are done using a CU index, which is
15129 just the 0-based index into this table. Note that if there are
15130 type CUs, then conceptually CUs and type CUs form a single list for
15131 the purposes of CU indices.
15132
15133 2.5 The types CU list. This does not appear in a version 1 index.
15134 This is a sequence of triplets of 64-bit little-endian values. In
15135 a triplet, the first value is the CU offset, the second value is
15136 the type offset in the CU, and the third value is the type
15137 signature. The types CU list is not sorted.
9291a0cd
TT
15138
15139 3. The address section. The address section consists of a sequence
15140 of address entries. Each address entry has three elements.
15141 [0] The low address. This is a 64-bit little-endian value.
15142 [1] The high address. This is a 64-bit little-endian value.
15143 [2] The CU index. This is an offset_type value.
15144
15145 4. The symbol table. This is a hash table. The size of the hash
15146 table is always a power of 2. The initial hash and the step are
15147 currently defined by the `find_slot' function.
15148
15149 Each slot in the hash table consists of a pair of offset_type
15150 values. The first value is the offset of the symbol's name in the
15151 constant pool. The second value is the offset of the CU vector in
15152 the constant pool.
15153
15154 If both values are 0, then this slot in the hash table is empty.
15155 This is ok because while 0 is a valid constant pool index, it
15156 cannot be a valid index for both a string and a CU vector.
15157
15158 A string in the constant pool is stored as a \0-terminated string,
15159 as you'd expect.
15160
15161 A CU vector in the constant pool is a sequence of offset_type
15162 values. The first value is the number of CU indices in the vector.
15163 Each subsequent value is the index of a CU in the CU list. This
15164 element in the hash table is used to indicate which CUs define the
15165 symbol.
15166
15167 5. The constant pool. This is simply a bunch of bytes. It is
15168 organized so that alignment is correct: CU vectors are stored
15169 first, followed by strings. */
15170static void
15171save_gdb_index_command (char *arg, int from_tty)
15172{
15173 struct objfile *objfile;
15174
15175 if (!arg || !*arg)
96d19272 15176 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15177
15178 ALL_OBJFILES (objfile)
15179 {
15180 struct stat st;
15181
15182 /* If the objfile does not correspond to an actual file, skip it. */
15183 if (stat (objfile->name, &st) < 0)
15184 continue;
15185
15186 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15187 if (dwarf2_per_objfile)
15188 {
15189 volatile struct gdb_exception except;
15190
15191 TRY_CATCH (except, RETURN_MASK_ERROR)
15192 {
15193 write_psymtabs_to_index (objfile, arg);
15194 }
15195 if (except.reason < 0)
15196 exception_fprintf (gdb_stderr, except,
15197 _("Error while writing index for `%s': "),
15198 objfile->name);
15199 }
15200 }
dce234bc
PP
15201}
15202
9291a0cd
TT
15203\f
15204
9eae7c52
TT
15205int dwarf2_always_disassemble;
15206
15207static void
15208show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15209 struct cmd_list_element *c, const char *value)
15210{
15211 fprintf_filtered (file, _("\
15212Whether to always disassemble DWARF expressions is %s.\n"),
15213 value);
15214}
15215
6502dd73
DJ
15216void _initialize_dwarf2_read (void);
15217
15218void
15219_initialize_dwarf2_read (void)
15220{
96d19272
JK
15221 struct cmd_list_element *c;
15222
dce234bc 15223 dwarf2_objfile_data_key
c1bd65d0 15224 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15225
1bedd215
AC
15226 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15227Set DWARF 2 specific variables.\n\
15228Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15229 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15230 0/*allow-unknown*/, &maintenance_set_cmdlist);
15231
1bedd215
AC
15232 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15233Show DWARF 2 specific variables\n\
15234Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15235 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15236 0/*allow-unknown*/, &maintenance_show_cmdlist);
15237
15238 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15239 &dwarf2_max_cache_age, _("\
15240Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15241Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15242A higher limit means that cached compilation units will be stored\n\
15243in memory longer, and more total memory will be used. Zero disables\n\
15244caching, which can slow down startup."),
2c5b56ce 15245 NULL,
920d2a44 15246 show_dwarf2_max_cache_age,
2c5b56ce 15247 &set_dwarf2_cmdlist,
ae038cb0 15248 &show_dwarf2_cmdlist);
d97bc12b 15249
9eae7c52
TT
15250 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15251 &dwarf2_always_disassemble, _("\
15252Set whether `info address' always disassembles DWARF expressions."), _("\
15253Show whether `info address' always disassembles DWARF expressions."), _("\
15254When enabled, DWARF expressions are always printed in an assembly-like\n\
15255syntax. When disabled, expressions will be printed in a more\n\
15256conversational style, when possible."),
15257 NULL,
15258 show_dwarf2_always_disassemble,
15259 &set_dwarf2_cmdlist,
15260 &show_dwarf2_cmdlist);
15261
d97bc12b
DE
15262 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15263Set debugging of the dwarf2 DIE reader."), _("\
15264Show debugging of the dwarf2 DIE reader."), _("\
15265When enabled (non-zero), DIEs are dumped after they are read in.\n\
15266The value is the maximum depth to print."),
15267 NULL,
15268 NULL,
15269 &setdebuglist, &showdebuglist);
9291a0cd 15270
96d19272
JK
15271 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15272 _("Save a .gdb-index file"),
15273 &save_cmdlist);
15274 set_cmd_completer (c, filename_completer);
6502dd73 15275}
This page took 1.950675 seconds and 4 git commands to generate.