Allow disabling of gdbserver build (--enable-gdbserver=yes/no/auto).
[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
4950bc1c 672 together via their SIBLING fields. */
639d11d3
DC
673 struct die_info *child; /* Its first child, if any. */
674 struct die_info *sibling; /* Its next sibling, if any. */
675 struct die_info *parent; /* Its parent, if any. */
c906108c 676
b60c80d6
DJ
677 /* An array of attributes, with NUM_ATTRS elements. There may be
678 zero, but it's not common and zero-sized arrays are not
679 sufficiently portable C. */
680 struct attribute attrs[1];
c906108c
SS
681 };
682
5fb290d7
DJ
683struct function_range
684{
685 const char *name;
686 CORE_ADDR lowpc, highpc;
687 int seen_line;
688 struct function_range *next;
689};
690
c906108c
SS
691/* Get at parts of an attribute structure */
692
693#define DW_STRING(attr) ((attr)->u.str)
8285870a 694#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
695#define DW_UNSND(attr) ((attr)->u.unsnd)
696#define DW_BLOCK(attr) ((attr)->u.blk)
697#define DW_SND(attr) ((attr)->u.snd)
698#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 699#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
700
701/* Blocks are a bunch of untyped bytes. */
702struct dwarf_block
703 {
704 unsigned int size;
fe1b8b76 705 gdb_byte *data;
c906108c
SS
706 };
707
c906108c
SS
708#ifndef ATTR_ALLOC_CHUNK
709#define ATTR_ALLOC_CHUNK 4
710#endif
711
c906108c
SS
712/* Allocate fields for structs, unions and enums in this size. */
713#ifndef DW_FIELD_ALLOC_CHUNK
714#define DW_FIELD_ALLOC_CHUNK 4
715#endif
716
c906108c
SS
717/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
718 but this would require a corresponding change in unpack_field_as_long
719 and friends. */
720static int bits_per_byte = 8;
721
722/* The routines that read and process dies for a C struct or C++ class
723 pass lists of data member fields and lists of member function fields
724 in an instance of a field_info structure, as defined below. */
725struct field_info
c5aa993b
JM
726 {
727 /* List of data member and baseclasses fields. */
728 struct nextfield
729 {
730 struct nextfield *next;
731 int accessibility;
732 int virtuality;
733 struct field field;
734 }
7d0ccb61 735 *fields, *baseclasses;
c906108c 736
7d0ccb61 737 /* Number of fields (including baseclasses). */
c5aa993b 738 int nfields;
c906108c 739
c5aa993b
JM
740 /* Number of baseclasses. */
741 int nbaseclasses;
c906108c 742
c5aa993b
JM
743 /* Set if the accesibility of one of the fields is not public. */
744 int non_public_fields;
c906108c 745
c5aa993b
JM
746 /* Member function fields array, entries are allocated in the order they
747 are encountered in the object file. */
748 struct nextfnfield
749 {
750 struct nextfnfield *next;
751 struct fn_field fnfield;
752 }
753 *fnfields;
c906108c 754
c5aa993b
JM
755 /* Member function fieldlist array, contains name of possibly overloaded
756 member function, number of overloaded member functions and a pointer
757 to the head of the member function field chain. */
758 struct fnfieldlist
759 {
760 char *name;
761 int length;
762 struct nextfnfield *head;
763 }
764 *fnfieldlists;
c906108c 765
c5aa993b
JM
766 /* Number of entries in the fnfieldlists array. */
767 int nfnfields;
98751a41
JK
768
769 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
770 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
771 struct typedef_field_list
772 {
773 struct typedef_field field;
774 struct typedef_field_list *next;
775 }
776 *typedef_field_list;
777 unsigned typedef_field_list_count;
c5aa993b 778 };
c906108c 779
10b3939b
DJ
780/* One item on the queue of compilation units to read in full symbols
781 for. */
782struct dwarf2_queue_item
783{
784 struct dwarf2_per_cu_data *per_cu;
785 struct dwarf2_queue_item *next;
786};
787
788/* The current queue. */
789static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
790
ae038cb0
DJ
791/* Loaded secondary compilation units are kept in memory until they
792 have not been referenced for the processing of this many
793 compilation units. Set this to zero to disable caching. Cache
794 sizes of up to at least twenty will improve startup time for
795 typical inter-CU-reference binaries, at an obvious memory cost. */
796static int dwarf2_max_cache_age = 5;
920d2a44
AC
797static void
798show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
799 struct cmd_list_element *c, const char *value)
800{
801 fprintf_filtered (file, _("\
802The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
803 value);
804}
805
ae038cb0 806
c906108c
SS
807/* Various complaints about symbol reading that don't abort the process */
808
4d3c2250
KB
809static void
810dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 811{
4d3c2250 812 complaint (&symfile_complaints,
e2e0b3e5 813 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
814}
815
25e43795
DJ
816static void
817dwarf2_debug_line_missing_file_complaint (void)
818{
819 complaint (&symfile_complaints,
820 _(".debug_line section has line data without a file"));
821}
822
59205f5a
JB
823static void
824dwarf2_debug_line_missing_end_sequence_complaint (void)
825{
826 complaint (&symfile_complaints,
827 _(".debug_line section has line program sequence without an end"));
828}
829
4d3c2250
KB
830static void
831dwarf2_complex_location_expr_complaint (void)
2e276125 832{
e2e0b3e5 833 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
834}
835
4d3c2250
KB
836static void
837dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
838 int arg3)
2e276125 839{
4d3c2250 840 complaint (&symfile_complaints,
e2e0b3e5 841 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
842 arg2, arg3);
843}
844
845static void
846dwarf2_macros_too_long_complaint (void)
2e276125 847{
4d3c2250 848 complaint (&symfile_complaints,
e2e0b3e5 849 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
850}
851
852static void
853dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 854{
4d3c2250 855 complaint (&symfile_complaints,
e2e0b3e5 856 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
857 arg1);
858}
859
860static void
861dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 862{
4d3c2250 863 complaint (&symfile_complaints,
e2e0b3e5 864 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 865}
c906108c 866
c906108c
SS
867/* local function prototypes */
868
4efb68b1 869static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 870
aaa75496
JB
871static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
872 struct objfile *);
873
c67a9c90 874static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 875
72bf9492
DJ
876static void scan_partial_symbols (struct partial_die_info *,
877 CORE_ADDR *, CORE_ADDR *,
5734ee8b 878 int, struct dwarf2_cu *);
c906108c 879
72bf9492
DJ
880static void add_partial_symbol (struct partial_die_info *,
881 struct dwarf2_cu *);
63d06c5c 882
72bf9492
DJ
883static void add_partial_namespace (struct partial_die_info *pdi,
884 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 885 int need_pc, struct dwarf2_cu *cu);
63d06c5c 886
5d7cb8df
JK
887static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
888 CORE_ADDR *highpc, int need_pc,
889 struct dwarf2_cu *cu);
890
72bf9492
DJ
891static void add_partial_enumeration (struct partial_die_info *enum_pdi,
892 struct dwarf2_cu *cu);
91c24f0a 893
bc30ff58
JB
894static void add_partial_subprogram (struct partial_die_info *pdi,
895 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 896 int need_pc, struct dwarf2_cu *cu);
bc30ff58 897
fe1b8b76 898static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
899 gdb_byte *buffer, gdb_byte *info_ptr,
900 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 901
a14ed312 902static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 903
a14ed312 904static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 905
e7c27a73 906static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 907
f3dd6933 908static void dwarf2_free_abbrev_table (void *);
c906108c 909
fe1b8b76 910static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 911 struct dwarf2_cu *);
72bf9492 912
57349743 913static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 914 struct dwarf2_cu *);
c906108c 915
93311388
DE
916static struct partial_die_info *load_partial_dies (bfd *,
917 gdb_byte *, gdb_byte *,
918 int, struct dwarf2_cu *);
72bf9492 919
fe1b8b76 920static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
921 struct abbrev_info *abbrev,
922 unsigned int, bfd *,
923 gdb_byte *, gdb_byte *,
924 struct dwarf2_cu *);
c906108c 925
c764a876 926static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 927 struct dwarf2_cu *);
72bf9492
DJ
928
929static void fixup_partial_die (struct partial_die_info *,
930 struct dwarf2_cu *);
931
fe1b8b76
JB
932static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
933 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 934
fe1b8b76
JB
935static gdb_byte *read_attribute_value (struct attribute *, unsigned,
936 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 937
fe1b8b76 938static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 939
fe1b8b76 940static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 941
fe1b8b76 942static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 943
fe1b8b76 944static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 945
93311388 946static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 947
fe1b8b76 948static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 949 unsigned int *);
c906108c 950
c764a876
DE
951static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
952
953static LONGEST read_checked_initial_length_and_offset
954 (bfd *, gdb_byte *, const struct comp_unit_head *,
955 unsigned int *, unsigned int *);
613e1657 956
fe1b8b76 957static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
958 unsigned int *);
959
960static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 961
fe1b8b76 962static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 963
9b1c24c8 964static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 965
fe1b8b76
JB
966static char *read_indirect_string (bfd *, gdb_byte *,
967 const struct comp_unit_head *,
968 unsigned int *);
4bdf3d34 969
fe1b8b76 970static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 971
fe1b8b76 972static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 973
fe1b8b76 974static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 975
e142c38c 976static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 977
e142c38c
DJ
978static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
979 struct dwarf2_cu *);
c906108c 980
348e048f
DE
981static struct attribute *dwarf2_attr_no_follow (struct die_info *,
982 unsigned int,
983 struct dwarf2_cu *);
984
05cf31d1
JB
985static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
986 struct dwarf2_cu *cu);
987
e142c38c 988static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 989
e142c38c 990static struct die_info *die_specification (struct die_info *die,
f2f0e013 991 struct dwarf2_cu **);
63d06c5c 992
debd256d
JB
993static void free_line_header (struct line_header *lh);
994
aaa75496
JB
995static void add_file_name (struct line_header *, char *, unsigned int,
996 unsigned int, unsigned int);
997
debd256d
JB
998static struct line_header *(dwarf_decode_line_header
999 (unsigned int offset,
e7c27a73 1000 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
1001
1002static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 1003 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1004
4f1520fb 1005static void dwarf2_start_subfile (char *, char *, char *);
c906108c 1006
a14ed312 1007static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1008 struct dwarf2_cu *);
c906108c 1009
34eaf542
TT
1010static struct symbol *new_symbol_full (struct die_info *, struct type *,
1011 struct dwarf2_cu *, struct symbol *);
1012
a14ed312 1013static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1014 struct dwarf2_cu *);
c906108c 1015
98bfdba5
PA
1016static void dwarf2_const_value_attr (struct attribute *attr,
1017 struct type *type,
1018 const char *name,
1019 struct obstack *obstack,
1020 struct dwarf2_cu *cu, long *value,
1021 gdb_byte **bytes,
1022 struct dwarf2_locexpr_baton **baton);
2df3850c 1023
e7c27a73 1024static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1025
b4ba55a1
JB
1026static int need_gnat_info (struct dwarf2_cu *);
1027
1028static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1029
1030static void set_descriptive_type (struct type *, struct die_info *,
1031 struct dwarf2_cu *);
1032
e7c27a73
DJ
1033static struct type *die_containing_type (struct die_info *,
1034 struct dwarf2_cu *);
c906108c 1035
673bfd45
DE
1036static struct type *lookup_die_type (struct die_info *, struct attribute *,
1037 struct dwarf2_cu *);
c906108c 1038
f792889a 1039static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1040
673bfd45
DE
1041static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1042
086ed43d 1043static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1044
6e70227d 1045static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1046 const char *suffix, int physname,
1047 struct dwarf2_cu *cu);
63d06c5c 1048
e7c27a73 1049static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1050
348e048f
DE
1051static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1052
e7c27a73 1053static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1054
e7c27a73 1055static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1056
ff013f42
JK
1057static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1058 struct dwarf2_cu *, struct partial_symtab *);
1059
a14ed312 1060static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1061 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1062 struct partial_symtab *);
c906108c 1063
fae299cd
DC
1064static void get_scope_pc_bounds (struct die_info *,
1065 CORE_ADDR *, CORE_ADDR *,
1066 struct dwarf2_cu *);
1067
801e3a5b
JB
1068static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1069 CORE_ADDR, struct dwarf2_cu *);
1070
a14ed312 1071static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1072 struct dwarf2_cu *);
c906108c 1073
a14ed312 1074static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1075 struct type *, struct dwarf2_cu *);
c906108c 1076
a14ed312 1077static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1078 struct die_info *, struct type *,
e7c27a73 1079 struct dwarf2_cu *);
c906108c 1080
a14ed312 1081static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1082 struct type *, struct dwarf2_cu *);
c906108c 1083
134d01f1 1084static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1085
e7c27a73 1086static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1087
e7c27a73 1088static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1089
5d7cb8df
JK
1090static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1091
27aa8d6a
SW
1092static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1093
f55ee35c
JK
1094static struct type *read_module_type (struct die_info *die,
1095 struct dwarf2_cu *cu);
1096
38d518c9 1097static const char *namespace_name (struct die_info *die,
e142c38c 1098 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1099
134d01f1 1100static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1101
e7c27a73 1102static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1103
6e70227d 1104static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1105 struct dwarf2_cu *);
1106
93311388 1107static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1108
93311388
DE
1109static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1110 gdb_byte *info_ptr,
d97bc12b
DE
1111 gdb_byte **new_info_ptr,
1112 struct die_info *parent);
1113
93311388
DE
1114static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
fe1b8b76 1116 gdb_byte **new_info_ptr,
639d11d3
DC
1117 struct die_info *parent);
1118
93311388
DE
1119static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1120 gdb_byte *info_ptr,
fe1b8b76 1121 gdb_byte **new_info_ptr,
639d11d3
DC
1122 struct die_info *parent);
1123
93311388
DE
1124static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1125 struct die_info **, gdb_byte *,
1126 int *);
1127
e7c27a73 1128static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1129
71c25dea
TT
1130static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1131 struct obstack *);
1132
e142c38c 1133static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1134
98bfdba5
PA
1135static const char *dwarf2_full_name (char *name,
1136 struct die_info *die,
1137 struct dwarf2_cu *cu);
1138
e142c38c 1139static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1140 struct dwarf2_cu **);
9219021c 1141
a14ed312 1142static char *dwarf_tag_name (unsigned int);
c906108c 1143
a14ed312 1144static char *dwarf_attr_name (unsigned int);
c906108c 1145
a14ed312 1146static char *dwarf_form_name (unsigned int);
c906108c 1147
a14ed312 1148static char *dwarf_bool_name (unsigned int);
c906108c 1149
a14ed312 1150static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1151
1152#if 0
a14ed312 1153static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1154#endif
1155
f9aca02d 1156static struct die_info *sibling_die (struct die_info *);
c906108c 1157
d97bc12b
DE
1158static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1159
1160static void dump_die_for_error (struct die_info *);
1161
1162static void dump_die_1 (struct ui_file *, int level, int max_level,
1163 struct die_info *);
c906108c 1164
d97bc12b 1165/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1166
51545339 1167static void store_in_ref_table (struct die_info *,
10b3939b 1168 struct dwarf2_cu *);
c906108c 1169
93311388
DE
1170static int is_ref_attr (struct attribute *);
1171
c764a876 1172static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1173
43bbcdc2 1174static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1175
348e048f
DE
1176static struct die_info *follow_die_ref_or_sig (struct die_info *,
1177 struct attribute *,
1178 struct dwarf2_cu **);
1179
10b3939b
DJ
1180static struct die_info *follow_die_ref (struct die_info *,
1181 struct attribute *,
f2f0e013 1182 struct dwarf2_cu **);
c906108c 1183
348e048f
DE
1184static struct die_info *follow_die_sig (struct die_info *,
1185 struct attribute *,
1186 struct dwarf2_cu **);
1187
1188static void read_signatured_type_at_offset (struct objfile *objfile,
1189 unsigned int offset);
1190
1191static void read_signatured_type (struct objfile *,
1192 struct signatured_type *type_sig);
1193
c906108c
SS
1194/* memory allocation interface */
1195
7b5a2f43 1196static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1197
f3dd6933 1198static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1199
b60c80d6 1200static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1201
e142c38c 1202static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1203
e142c38c
DJ
1204static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1205 struct dwarf2_cu *);
5fb290d7 1206
2e276125 1207static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1208 char *, bfd *, struct dwarf2_cu *);
2e276125 1209
8e19ed76
PS
1210static int attr_form_is_block (struct attribute *);
1211
3690dd37
JB
1212static int attr_form_is_section_offset (struct attribute *);
1213
1214static int attr_form_is_constant (struct attribute *);
1215
93e7bd98
DJ
1216static void dwarf2_symbol_mark_computed (struct attribute *attr,
1217 struct symbol *sym,
1218 struct dwarf2_cu *cu);
4c2df51b 1219
93311388
DE
1220static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1221 struct abbrev_info *abbrev,
1222 struct dwarf2_cu *cu);
4bb7a0a7 1223
72bf9492
DJ
1224static void free_stack_comp_unit (void *);
1225
72bf9492
DJ
1226static hashval_t partial_die_hash (const void *item);
1227
1228static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1229
ae038cb0 1230static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1231 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1232
1233static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1234 (unsigned int offset, struct objfile *objfile);
ae038cb0 1235
93311388
DE
1236static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1237
ae038cb0
DJ
1238static void free_one_comp_unit (void *);
1239
1240static void free_cached_comp_units (void *);
1241
1242static void age_cached_comp_units (void);
1243
1244static void free_one_cached_comp_unit (void *);
1245
f792889a
DJ
1246static struct type *set_die_type (struct die_info *, struct type *,
1247 struct dwarf2_cu *);
1c379e20 1248
ae038cb0
DJ
1249static void create_all_comp_units (struct objfile *);
1250
1fd400ff
TT
1251static int create_debug_types_hash_table (struct objfile *objfile);
1252
93311388
DE
1253static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1254 struct objfile *);
10b3939b
DJ
1255
1256static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1257
1258static void dwarf2_add_dependence (struct dwarf2_cu *,
1259 struct dwarf2_per_cu_data *);
1260
ae038cb0
DJ
1261static void dwarf2_mark (struct dwarf2_cu *);
1262
1263static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1264
673bfd45
DE
1265static struct type *get_die_type_at_offset (unsigned int,
1266 struct dwarf2_per_cu_data *per_cu);
1267
f792889a 1268static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1269
9291a0cd
TT
1270static void dwarf2_release_queue (void *dummy);
1271
1272static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1273 struct objfile *objfile);
1274
1275static void process_queue (struct objfile *objfile);
1276
1277static void find_file_and_directory (struct die_info *die,
1278 struct dwarf2_cu *cu,
1279 char **name, char **comp_dir);
1280
1281static char *file_full_name (int file, struct line_header *lh,
1282 const char *comp_dir);
1283
1284static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1285 gdb_byte *info_ptr,
1286 gdb_byte *buffer,
1287 unsigned int buffer_size,
1288 bfd *abfd);
1289
1290static void init_cu_die_reader (struct die_reader_specs *reader,
1291 struct dwarf2_cu *cu);
1292
673bfd45 1293static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1294
9291a0cd
TT
1295#if WORDS_BIGENDIAN
1296
1297/* Convert VALUE between big- and little-endian. */
1298static offset_type
1299byte_swap (offset_type value)
1300{
1301 offset_type result;
1302
1303 result = (value & 0xff) << 24;
1304 result |= (value & 0xff00) << 8;
1305 result |= (value & 0xff0000) >> 8;
1306 result |= (value & 0xff000000) >> 24;
1307 return result;
1308}
1309
1310#define MAYBE_SWAP(V) byte_swap (V)
1311
1312#else
1313#define MAYBE_SWAP(V) (V)
1314#endif /* WORDS_BIGENDIAN */
1315
1316/* The suffix for an index file. */
1317#define INDEX_SUFFIX ".gdb-index"
1318
3da10d80
KS
1319static const char *dwarf2_physname (char *name, struct die_info *die,
1320 struct dwarf2_cu *cu);
1321
c906108c
SS
1322/* Try to locate the sections we need for DWARF 2 debugging
1323 information and return true if we have enough to do something. */
1324
1325int
6502dd73 1326dwarf2_has_info (struct objfile *objfile)
c906108c 1327{
be391dca
TT
1328 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1329 if (!dwarf2_per_objfile)
1330 {
1331 /* Initialize per-objfile state. */
1332 struct dwarf2_per_objfile *data
1333 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1334
be391dca
TT
1335 memset (data, 0, sizeof (*data));
1336 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1337 dwarf2_per_objfile = data;
6502dd73 1338
be391dca
TT
1339 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1340 dwarf2_per_objfile->objfile = objfile;
1341 }
1342 return (dwarf2_per_objfile->info.asection != NULL
1343 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1344}
1345
233a11ab
CS
1346/* When loading sections, we can either look for ".<name>", or for
1347 * ".z<name>", which indicates a compressed section. */
1348
1349static int
dce234bc 1350section_is_p (const char *section_name, const char *name)
233a11ab 1351{
dce234bc
PP
1352 return (section_name[0] == '.'
1353 && (strcmp (section_name + 1, name) == 0
1354 || (section_name[1] == 'z'
1355 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1356}
1357
c906108c
SS
1358/* This function is mapped across the sections and remembers the
1359 offset and size of each of the debugging sections we are interested
1360 in. */
1361
1362static void
72dca2f5 1363dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1364{
dce234bc 1365 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1366 {
dce234bc
PP
1367 dwarf2_per_objfile->info.asection = sectp;
1368 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1369 }
dce234bc 1370 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1371 {
dce234bc
PP
1372 dwarf2_per_objfile->abbrev.asection = sectp;
1373 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1374 }
dce234bc 1375 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1376 {
dce234bc
PP
1377 dwarf2_per_objfile->line.asection = sectp;
1378 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1379 }
dce234bc 1380 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1381 {
dce234bc
PP
1382 dwarf2_per_objfile->loc.asection = sectp;
1383 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1384 }
dce234bc 1385 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1386 {
dce234bc
PP
1387 dwarf2_per_objfile->macinfo.asection = sectp;
1388 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1389 }
dce234bc 1390 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1391 {
dce234bc
PP
1392 dwarf2_per_objfile->str.asection = sectp;
1393 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1394 }
dce234bc 1395 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1396 {
dce234bc
PP
1397 dwarf2_per_objfile->frame.asection = sectp;
1398 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1399 }
dce234bc 1400 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1401 {
3799ccc6 1402 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1403
3799ccc6
EZ
1404 if (aflag & SEC_HAS_CONTENTS)
1405 {
dce234bc
PP
1406 dwarf2_per_objfile->eh_frame.asection = sectp;
1407 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1408 }
b6af0555 1409 }
dce234bc 1410 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1411 {
dce234bc
PP
1412 dwarf2_per_objfile->ranges.asection = sectp;
1413 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1414 }
348e048f
DE
1415 else if (section_is_p (sectp->name, TYPES_SECTION))
1416 {
1417 dwarf2_per_objfile->types.asection = sectp;
1418 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1419 }
9291a0cd
TT
1420 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1421 {
1422 dwarf2_per_objfile->gdb_index.asection = sectp;
1423 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1424 }
dce234bc 1425
72dca2f5
FR
1426 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1427 && bfd_section_vma (abfd, sectp) == 0)
1428 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1429}
1430
dce234bc
PP
1431/* Decompress a section that was compressed using zlib. Store the
1432 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1433
1434static void
dce234bc
PP
1435zlib_decompress_section (struct objfile *objfile, asection *sectp,
1436 gdb_byte **outbuf, bfd_size_type *outsize)
1437{
1438 bfd *abfd = objfile->obfd;
1439#ifndef HAVE_ZLIB_H
1440 error (_("Support for zlib-compressed DWARF data (from '%s') "
1441 "is disabled in this copy of GDB"),
1442 bfd_get_filename (abfd));
1443#else
1444 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1445 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1446 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1447 bfd_size_type uncompressed_size;
1448 gdb_byte *uncompressed_buffer;
1449 z_stream strm;
1450 int rc;
1451 int header_size = 12;
1452
1453 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1454 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1455 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1456 bfd_get_filename (abfd));
1457
1458 /* Read the zlib header. In this case, it should be "ZLIB" followed
1459 by the uncompressed section size, 8 bytes in big-endian order. */
1460 if (compressed_size < header_size
1461 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1462 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1463 bfd_get_filename (abfd));
1464 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1465 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[11];
1472
1473 /* It is possible the section consists of several compressed
1474 buffers concatenated together, so we uncompress in a loop. */
1475 strm.zalloc = NULL;
1476 strm.zfree = NULL;
1477 strm.opaque = NULL;
1478 strm.avail_in = compressed_size - header_size;
1479 strm.next_in = (Bytef*) compressed_buffer + header_size;
1480 strm.avail_out = uncompressed_size;
1481 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1482 uncompressed_size);
1483 rc = inflateInit (&strm);
1484 while (strm.avail_in > 0)
1485 {
1486 if (rc != Z_OK)
1487 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1488 bfd_get_filename (abfd), rc);
1489 strm.next_out = ((Bytef*) uncompressed_buffer
1490 + (uncompressed_size - strm.avail_out));
1491 rc = inflate (&strm, Z_FINISH);
1492 if (rc != Z_STREAM_END)
1493 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1494 bfd_get_filename (abfd), rc);
1495 rc = inflateReset (&strm);
1496 }
1497 rc = inflateEnd (&strm);
1498 if (rc != Z_OK
1499 || strm.avail_out != 0)
1500 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1501 bfd_get_filename (abfd), rc);
1502
affddf13 1503 do_cleanups (cleanup);
dce234bc
PP
1504 *outbuf = uncompressed_buffer;
1505 *outsize = uncompressed_size;
1506#endif
233a11ab
CS
1507}
1508
dce234bc
PP
1509/* Read the contents of the section SECTP from object file specified by
1510 OBJFILE, store info about the section into INFO.
1511 If the section is compressed, uncompress it before returning. */
c906108c 1512
dce234bc
PP
1513static void
1514dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1515{
dce234bc
PP
1516 bfd *abfd = objfile->obfd;
1517 asection *sectp = info->asection;
1518 gdb_byte *buf, *retbuf;
1519 unsigned char header[4];
c906108c 1520
be391dca
TT
1521 if (info->readin)
1522 return;
dce234bc
PP
1523 info->buffer = NULL;
1524 info->was_mmapped = 0;
be391dca 1525 info->readin = 1;
188dd5d6 1526
dce234bc
PP
1527 if (info->asection == NULL || info->size == 0)
1528 return;
c906108c 1529
dce234bc
PP
1530 /* Check if the file has a 4-byte header indicating compression. */
1531 if (info->size > sizeof (header)
1532 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1533 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1534 {
1535 /* Upon decompression, update the buffer and its size. */
1536 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1537 {
1538 zlib_decompress_section (objfile, sectp, &info->buffer,
1539 &info->size);
1540 return;
1541 }
1542 }
4bdf3d34 1543
dce234bc
PP
1544#ifdef HAVE_MMAP
1545 if (pagesize == 0)
1546 pagesize = getpagesize ();
2e276125 1547
dce234bc
PP
1548 /* Only try to mmap sections which are large enough: we don't want to
1549 waste space due to fragmentation. Also, only try mmap for sections
1550 without relocations. */
1551
1552 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1553 {
1554 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1555 size_t map_length = info->size + sectp->filepos - pg_offset;
1556 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1557 MAP_PRIVATE, pg_offset);
1558
1559 if (retbuf != MAP_FAILED)
1560 {
1561 info->was_mmapped = 1;
1562 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1563#if HAVE_POSIX_MADVISE
1564 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1565#endif
dce234bc
PP
1566 return;
1567 }
1568 }
1569#endif
1570
1571 /* If we get here, we are a normal, not-compressed section. */
1572 info->buffer = buf
1573 = obstack_alloc (&objfile->objfile_obstack, info->size);
1574
1575 /* When debugging .o files, we may need to apply relocations; see
1576 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1577 We never compress sections in .o files, so we only need to
1578 try this when the section is not compressed. */
ac8035ab 1579 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1580 if (retbuf != NULL)
1581 {
1582 info->buffer = retbuf;
1583 return;
1584 }
1585
1586 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1587 || bfd_bread (buf, info->size, abfd) != info->size)
1588 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1589 bfd_get_filename (abfd));
1590}
1591
1592/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1593 SECTION_NAME. */
af34e669 1594
dce234bc
PP
1595void
1596dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1597 asection **sectp, gdb_byte **bufp,
1598 bfd_size_type *sizep)
1599{
1600 struct dwarf2_per_objfile *data
1601 = objfile_data (objfile, dwarf2_objfile_data_key);
1602 struct dwarf2_section_info *info;
a3b2a86b
TT
1603
1604 /* We may see an objfile without any DWARF, in which case we just
1605 return nothing. */
1606 if (data == NULL)
1607 {
1608 *sectp = NULL;
1609 *bufp = NULL;
1610 *sizep = 0;
1611 return;
1612 }
dce234bc
PP
1613 if (section_is_p (section_name, EH_FRAME_SECTION))
1614 info = &data->eh_frame;
1615 else if (section_is_p (section_name, FRAME_SECTION))
1616 info = &data->frame;
0d53c4c4 1617 else
f3574227 1618 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1619
1620 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1621 /* We haven't read this section in yet. Do it now. */
1622 dwarf2_read_section (objfile, info);
1623
1624 *sectp = info->asection;
1625 *bufp = info->buffer;
1626 *sizep = info->size;
1627}
1628
9291a0cd
TT
1629\f
1630
1631/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1632 this CU came. */
1633static void
1634dw2_do_instantiate_symtab (struct objfile *objfile,
1635 struct dwarf2_per_cu_data *per_cu)
1636{
1637 struct cleanup *back_to;
1638
1639 back_to = make_cleanup (dwarf2_release_queue, NULL);
1640
1641 queue_comp_unit (per_cu, objfile);
1642
1643 if (per_cu->from_debug_types)
1644 read_signatured_type_at_offset (objfile, per_cu->offset);
1645 else
1646 load_full_comp_unit (per_cu, objfile);
1647
1648 process_queue (objfile);
1649
1650 /* Age the cache, releasing compilation units that have not
1651 been used recently. */
1652 age_cached_comp_units ();
1653
1654 do_cleanups (back_to);
1655}
1656
1657/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1658 the objfile from which this CU came. Returns the resulting symbol
1659 table. */
1660static struct symtab *
1661dw2_instantiate_symtab (struct objfile *objfile,
1662 struct dwarf2_per_cu_data *per_cu)
1663{
1664 if (!per_cu->v.quick->symtab)
1665 {
1666 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1667 increment_reading_symtab ();
1668 dw2_do_instantiate_symtab (objfile, per_cu);
1669 do_cleanups (back_to);
1670 }
1671 return per_cu->v.quick->symtab;
1672}
1673
1fd400ff
TT
1674/* Return the CU given its index. */
1675static struct dwarf2_per_cu_data *
1676dw2_get_cu (int index)
1677{
1678 if (index >= dwarf2_per_objfile->n_comp_units)
1679 {
1680 index -= dwarf2_per_objfile->n_comp_units;
1681 return dwarf2_per_objfile->type_comp_units[index];
1682 }
1683 return dwarf2_per_objfile->all_comp_units[index];
1684}
1685
9291a0cd
TT
1686/* A helper function that knows how to read a 64-bit value in a way
1687 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1688 otherwise. */
1689static int
1690extract_cu_value (const char *bytes, ULONGEST *result)
1691{
1692 if (sizeof (ULONGEST) < 8)
1693 {
1694 int i;
1695
1696 /* Ignore the upper 4 bytes if they are all zero. */
1697 for (i = 0; i < 4; ++i)
1698 if (bytes[i + 4] != 0)
1699 return 0;
1700
1701 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1702 }
1703 else
1704 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1705 return 1;
1706}
1707
1708/* Read the CU list from the mapped index, and use it to create all
1709 the CU objects for this objfile. Return 0 if something went wrong,
1710 1 if everything went ok. */
1711static int
1fd400ff
TT
1712create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1713 offset_type cu_list_elements)
9291a0cd
TT
1714{
1715 offset_type i;
9291a0cd
TT
1716
1717 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1718 dwarf2_per_objfile->all_comp_units
1719 = obstack_alloc (&objfile->objfile_obstack,
1720 dwarf2_per_objfile->n_comp_units
1721 * sizeof (struct dwarf2_per_cu_data *));
1722
1723 for (i = 0; i < cu_list_elements; i += 2)
1724 {
1725 struct dwarf2_per_cu_data *the_cu;
1726 ULONGEST offset, length;
1727
1728 if (!extract_cu_value (cu_list, &offset)
1729 || !extract_cu_value (cu_list + 8, &length))
1730 return 0;
1731 cu_list += 2 * 8;
1732
1733 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1734 struct dwarf2_per_cu_data);
1735 the_cu->offset = offset;
1736 the_cu->length = length;
1737 the_cu->objfile = objfile;
1738 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1739 struct dwarf2_per_cu_quick_data);
1740 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1741 }
1742
1743 return 1;
1744}
1745
1fd400ff 1746/* Create the signatured type hash table from the index. */
673bfd45 1747
1fd400ff 1748static int
673bfd45
DE
1749create_signatured_type_table_from_index (struct objfile *objfile,
1750 const gdb_byte *bytes,
1751 offset_type elements)
1fd400ff
TT
1752{
1753 offset_type i;
673bfd45 1754 htab_t sig_types_hash;
1fd400ff
TT
1755
1756 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1757 dwarf2_per_objfile->type_comp_units
1758 = obstack_alloc (&objfile->objfile_obstack,
1759 dwarf2_per_objfile->n_type_comp_units
1760 * sizeof (struct dwarf2_per_cu_data *));
1761
673bfd45 1762 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1763
1764 for (i = 0; i < elements; i += 3)
1765 {
1766 struct signatured_type *type_sig;
1767 ULONGEST offset, type_offset, signature;
1768 void **slot;
1769
1770 if (!extract_cu_value (bytes, &offset)
1771 || !extract_cu_value (bytes + 8, &type_offset))
1772 return 0;
1773 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1774 bytes += 3 * 8;
1775
1776 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1777 struct signatured_type);
1778 type_sig->signature = signature;
1779 type_sig->offset = offset;
1780 type_sig->type_offset = type_offset;
1781 type_sig->per_cu.from_debug_types = 1;
1782 type_sig->per_cu.offset = offset;
1783 type_sig->per_cu.objfile = objfile;
1784 type_sig->per_cu.v.quick
1785 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1786 struct dwarf2_per_cu_quick_data);
1787
673bfd45 1788 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1789 *slot = type_sig;
1790
1791 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1792 }
1793
673bfd45 1794 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1795
1796 return 1;
1797}
1798
9291a0cd
TT
1799/* Read the address map data from the mapped index, and use it to
1800 populate the objfile's psymtabs_addrmap. */
1801static void
1802create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1803{
1804 const gdb_byte *iter, *end;
1805 struct obstack temp_obstack;
1806 struct addrmap *mutable_map;
1807 struct cleanup *cleanup;
1808 CORE_ADDR baseaddr;
1809
1810 obstack_init (&temp_obstack);
1811 cleanup = make_cleanup_obstack_free (&temp_obstack);
1812 mutable_map = addrmap_create_mutable (&temp_obstack);
1813
1814 iter = index->address_table;
1815 end = iter + index->address_table_size;
1816
1817 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1818
1819 while (iter < end)
1820 {
1821 ULONGEST hi, lo, cu_index;
1822 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1823 iter += 8;
1824 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1825 iter += 8;
1826 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1827 iter += 4;
1828
1829 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1830 dw2_get_cu (cu_index));
9291a0cd
TT
1831 }
1832
1833 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1834 &objfile->objfile_obstack);
1835 do_cleanups (cleanup);
1836}
1837
1838/* The hash function for strings in the mapped index. This is the
1839 same as the hashtab.c hash function, but we keep a separate copy to
1840 maintain control over the implementation. This is necessary
1841 because the hash function is tied to the format of the mapped index
1842 file. */
1843static hashval_t
1844mapped_index_string_hash (const void *p)
1845{
1846 const unsigned char *str = (const unsigned char *) p;
1847 hashval_t r = 0;
1848 unsigned char c;
1849
1850 while ((c = *str++) != 0)
1851 r = r * 67 + c - 113;
1852
1853 return r;
1854}
1855
1856/* Find a slot in the mapped index INDEX for the object named NAME.
1857 If NAME is found, set *VEC_OUT to point to the CU vector in the
1858 constant pool and return 1. If NAME cannot be found, return 0. */
1859static int
1860find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1861 offset_type **vec_out)
1862{
1863 offset_type hash = mapped_index_string_hash (name);
1864 offset_type slot, step;
1865
1866 slot = hash & (index->index_table_slots - 1);
1867 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1868
1869 for (;;)
1870 {
1871 /* Convert a slot number to an offset into the table. */
1872 offset_type i = 2 * slot;
1873 const char *str;
1874 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1875 return 0;
1876
1877 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1878 if (!strcmp (name, str))
1879 {
1880 *vec_out = (offset_type *) (index->constant_pool
1881 + MAYBE_SWAP (index->index_table[i + 1]));
1882 return 1;
1883 }
1884
1885 slot = (slot + step) & (index->index_table_slots - 1);
1886 }
1887}
1888
1889/* Read the index file. If everything went ok, initialize the "quick"
1890 elements of all the CUs and return 1. Otherwise, return 0. */
1891static int
1892dwarf2_read_index (struct objfile *objfile)
1893{
9291a0cd
TT
1894 char *addr;
1895 struct mapped_index *map;
b3b272e1 1896 offset_type *metadata;
ac0b195c
KW
1897 const gdb_byte *cu_list;
1898 const gdb_byte *types_list = NULL;
1899 offset_type version, cu_list_elements;
1900 offset_type types_list_elements = 0;
1fd400ff 1901 int i;
9291a0cd
TT
1902
1903 if (dwarf2_per_objfile->gdb_index.asection == NULL
1904 || dwarf2_per_objfile->gdb_index.size == 0)
1905 return 0;
82430852
JK
1906
1907 /* Older elfutils strip versions could keep the section in the main
1908 executable while splitting it for the separate debug info file. */
1909 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1910 & SEC_HAS_CONTENTS) == 0)
1911 return 0;
1912
9291a0cd
TT
1913 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1914
1915 addr = dwarf2_per_objfile->gdb_index.buffer;
1916 /* Version check. */
1fd400ff
TT
1917 version = MAYBE_SWAP (*(offset_type *) addr);
1918 if (version == 1)
1919 {
1920 /* Index version 1 neglected to account for .debug_types. So,
1921 if we see .debug_types, we cannot use this index. */
1922 if (dwarf2_per_objfile->types.asection != NULL
1923 && dwarf2_per_objfile->types.size != 0)
1924 return 0;
1925 }
1926 else if (version != 2)
9291a0cd
TT
1927 return 0;
1928
1929 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1930 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1931
1932 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1933
1934 i = 0;
1935 cu_list = addr + MAYBE_SWAP (metadata[i]);
1936 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1937 / 8);
1fd400ff
TT
1938 ++i;
1939
1940 if (version == 2)
1941 {
1942 types_list = addr + MAYBE_SWAP (metadata[i]);
1943 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1944 - MAYBE_SWAP (metadata[i]))
1945 / 8);
1946 ++i;
1947 }
1948
1949 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1950 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1951 - MAYBE_SWAP (metadata[i]));
1952 ++i;
1953
1954 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1955 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1956 - MAYBE_SWAP (metadata[i]))
9291a0cd 1957 / (2 * sizeof (offset_type)));
1fd400ff 1958 ++i;
9291a0cd 1959
1fd400ff
TT
1960 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1961
1962 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1963 return 0;
1964
1965 if (version == 2
1966 && types_list_elements
673bfd45
DE
1967 && !create_signatured_type_table_from_index (objfile, types_list,
1968 types_list_elements))
9291a0cd
TT
1969 return 0;
1970
1971 create_addrmap_from_index (objfile, map);
1972
1973 dwarf2_per_objfile->index_table = map;
1974 dwarf2_per_objfile->using_index = 1;
1975
1976 return 1;
1977}
1978
1979/* A helper for the "quick" functions which sets the global
1980 dwarf2_per_objfile according to OBJFILE. */
1981static void
1982dw2_setup (struct objfile *objfile)
1983{
1984 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1985 gdb_assert (dwarf2_per_objfile);
1986}
1987
1988/* A helper for the "quick" functions which attempts to read the line
1989 table for THIS_CU. */
1990static void
1991dw2_require_line_header (struct objfile *objfile,
1992 struct dwarf2_per_cu_data *this_cu)
1993{
1994 bfd *abfd = objfile->obfd;
1995 struct line_header *lh = NULL;
1996 struct attribute *attr;
1997 struct cleanup *cleanups;
1998 struct die_info *comp_unit_die;
36374493 1999 struct dwarf2_section_info* sec;
9291a0cd
TT
2000 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2001 int has_children, i;
2002 struct dwarf2_cu cu;
2003 unsigned int bytes_read, buffer_size;
2004 struct die_reader_specs reader_specs;
2005 char *name, *comp_dir;
2006
2007 if (this_cu->v.quick->read_lines)
2008 return;
2009 this_cu->v.quick->read_lines = 1;
2010
2011 memset (&cu, 0, sizeof (cu));
2012 cu.objfile = objfile;
2013 obstack_init (&cu.comp_unit_obstack);
2014
2015 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2016
36374493
DE
2017 if (this_cu->from_debug_types)
2018 sec = &dwarf2_per_objfile->types;
2019 else
2020 sec = &dwarf2_per_objfile->info;
2021 dwarf2_read_section (objfile, sec);
2022 buffer_size = sec->size;
2023 buffer = sec->buffer;
9291a0cd
TT
2024 info_ptr = buffer + this_cu->offset;
2025 beg_of_comp_unit = info_ptr;
2026
2027 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2028 buffer, buffer_size,
2029 abfd);
2030
2031 /* Complete the cu_header. */
2032 cu.header.offset = beg_of_comp_unit - buffer;
2033 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2034
2035 this_cu->cu = &cu;
2036 cu.per_cu = this_cu;
2037
2038 dwarf2_read_abbrevs (abfd, &cu);
2039 make_cleanup (dwarf2_free_abbrev_table, &cu);
2040
2041 if (this_cu->from_debug_types)
2042 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2043 init_cu_die_reader (&reader_specs, &cu);
2044 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2045 &has_children);
2046
2047 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2048 if (attr)
2049 {
2050 unsigned int line_offset = DW_UNSND (attr);
2051 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2052 }
2053 if (lh == NULL)
2054 {
2055 do_cleanups (cleanups);
2056 return;
2057 }
2058
2059 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2060
2061 this_cu->v.quick->lines = lh;
2062
2063 this_cu->v.quick->file_names
2064 = obstack_alloc (&objfile->objfile_obstack,
2065 lh->num_file_names * sizeof (char *));
2066 for (i = 0; i < lh->num_file_names; ++i)
2067 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2068
2069 do_cleanups (cleanups);
2070}
2071
2072/* A helper for the "quick" functions which computes and caches the
2073 real path for a given file name from the line table.
2074 dw2_require_line_header must have been called before this is
2075 invoked. */
2076static const char *
2077dw2_require_full_path (struct objfile *objfile,
e254ef6a 2078 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2079 int index)
2080{
e254ef6a
DE
2081 if (!per_cu->v.quick->full_names)
2082 per_cu->v.quick->full_names
9291a0cd 2083 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2084 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2085 sizeof (char *));
2086
e254ef6a
DE
2087 if (!per_cu->v.quick->full_names[index])
2088 per_cu->v.quick->full_names[index]
2089 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2090
e254ef6a 2091 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2092}
2093
2094static struct symtab *
2095dw2_find_last_source_symtab (struct objfile *objfile)
2096{
2097 int index;
2098 dw2_setup (objfile);
2099 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2100 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2101}
2102
2103static void
2104dw2_forget_cached_source_info (struct objfile *objfile)
2105{
2106 int i;
2107
2108 dw2_setup (objfile);
1fd400ff
TT
2109 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2110 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2111 {
e254ef6a 2112 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2113
e254ef6a 2114 if (per_cu->v.quick->full_names)
9291a0cd
TT
2115 {
2116 int j;
2117
e254ef6a
DE
2118 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2119 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
2120 }
2121 }
2122}
2123
2124static int
2125dw2_lookup_symtab (struct objfile *objfile, const char *name,
2126 const char *full_path, const char *real_path,
2127 struct symtab **result)
2128{
2129 int i;
2130 int check_basename = lbasename (name) == name;
2131 struct dwarf2_per_cu_data *base_cu = NULL;
2132
2133 dw2_setup (objfile);
1fd400ff
TT
2134 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2135 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2136 {
2137 int j;
e254ef6a 2138 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2139
e254ef6a 2140 if (per_cu->v.quick->symtab)
9291a0cd
TT
2141 continue;
2142
e254ef6a
DE
2143 dw2_require_line_header (objfile, per_cu);
2144 if (!per_cu->v.quick->lines)
9291a0cd
TT
2145 continue;
2146
e254ef6a 2147 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2148 {
e254ef6a 2149 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2150
2151 if (FILENAME_CMP (name, this_name) == 0)
2152 {
e254ef6a 2153 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2154 return 1;
2155 }
2156
2157 if (check_basename && ! base_cu
2158 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2159 base_cu = per_cu;
9291a0cd
TT
2160
2161 if (full_path != NULL)
2162 {
2163 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2164 per_cu, j);
9291a0cd
TT
2165
2166 if (this_full_name
2167 && FILENAME_CMP (full_path, this_full_name) == 0)
2168 {
e254ef6a 2169 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2170 return 1;
2171 }
2172 }
2173
2174 if (real_path != NULL)
2175 {
2176 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2177 per_cu, j);
9291a0cd
TT
2178
2179 if (this_full_name != NULL)
2180 {
2181 char *rp = gdb_realpath (this_full_name);
2182 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2183 {
2184 xfree (rp);
e254ef6a 2185 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2186 return 1;
2187 }
2188 xfree (rp);
2189 }
2190 }
2191 }
2192 }
2193
2194 if (base_cu)
2195 {
2196 *result = dw2_instantiate_symtab (objfile, base_cu);
2197 return 1;
2198 }
2199
2200 return 0;
2201}
2202
2203static struct symtab *
2204dw2_lookup_symbol (struct objfile *objfile, int block_index,
2205 const char *name, domain_enum domain)
2206{
774b6a14 2207 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2208 instead. */
2209 return NULL;
2210}
2211
2212/* A helper function that expands all symtabs that hold an object
2213 named NAME. */
2214static void
2215dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2216{
2217 dw2_setup (objfile);
2218
2219 if (dwarf2_per_objfile->index_table)
2220 {
2221 offset_type *vec;
2222
2223 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2224 name, &vec))
2225 {
2226 offset_type i, len = MAYBE_SWAP (*vec);
2227 for (i = 0; i < len; ++i)
2228 {
2229 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2230 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2231
e254ef6a 2232 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2233 }
2234 }
2235 }
2236}
2237
774b6a14
TT
2238static void
2239dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2240 int kind, const char *name,
2241 domain_enum domain)
9291a0cd 2242{
774b6a14 2243 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2244}
2245
2246static void
2247dw2_print_stats (struct objfile *objfile)
2248{
2249 int i, count;
2250
2251 dw2_setup (objfile);
2252 count = 0;
1fd400ff
TT
2253 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2254 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2255 {
e254ef6a 2256 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2257
e254ef6a 2258 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2259 ++count;
2260 }
2261 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2262}
2263
2264static void
2265dw2_dump (struct objfile *objfile)
2266{
2267 /* Nothing worth printing. */
2268}
2269
2270static void
2271dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2272 struct section_offsets *delta)
2273{
2274 /* There's nothing to relocate here. */
2275}
2276
2277static void
2278dw2_expand_symtabs_for_function (struct objfile *objfile,
2279 const char *func_name)
2280{
2281 dw2_do_expand_symtabs_matching (objfile, func_name);
2282}
2283
2284static void
2285dw2_expand_all_symtabs (struct objfile *objfile)
2286{
2287 int i;
2288
2289 dw2_setup (objfile);
1fd400ff
TT
2290
2291 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2292 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2293 {
e254ef6a 2294 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2295
e254ef6a 2296 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2297 }
2298}
2299
2300static void
2301dw2_expand_symtabs_with_filename (struct objfile *objfile,
2302 const char *filename)
2303{
2304 int i;
2305
2306 dw2_setup (objfile);
1fd400ff
TT
2307 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2308 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2309 {
2310 int j;
e254ef6a 2311 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2312
e254ef6a 2313 if (per_cu->v.quick->symtab)
9291a0cd
TT
2314 continue;
2315
e254ef6a
DE
2316 dw2_require_line_header (objfile, per_cu);
2317 if (!per_cu->v.quick->lines)
9291a0cd
TT
2318 continue;
2319
e254ef6a 2320 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2321 {
e254ef6a 2322 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2323 if (strcmp (this_name, filename) == 0)
2324 {
e254ef6a 2325 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2326 break;
2327 }
2328 }
2329 }
2330}
2331
dd786858 2332static const char *
9291a0cd
TT
2333dw2_find_symbol_file (struct objfile *objfile, const char *name)
2334{
e254ef6a 2335 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2336 offset_type *vec;
2337
2338 dw2_setup (objfile);
2339
2340 if (!dwarf2_per_objfile->index_table)
2341 return NULL;
2342
2343 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2344 name, &vec))
2345 return NULL;
2346
2347 /* Note that this just looks at the very first one named NAME -- but
2348 actually we are looking for a function. find_main_filename
2349 should be rewritten so that it doesn't require a custom hook. It
2350 could just use the ordinary symbol tables. */
2351 /* vec[0] is the length, which must always be >0. */
e254ef6a 2352 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2353
e254ef6a
DE
2354 dw2_require_line_header (objfile, per_cu);
2355 if (!per_cu->v.quick->lines)
9291a0cd
TT
2356 return NULL;
2357
e254ef6a 2358 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2359}
2360
2361static void
2362dw2_map_ada_symtabs (struct objfile *objfile,
2363 int (*wild_match) (const char *, int, const char *),
2364 int (*is_name_suffix) (const char *),
2365 void (*callback) (struct objfile *,
2366 struct symtab *, void *),
2367 const char *name, int global,
2368 domain_enum namespace, int wild,
2369 void *data)
2370{
a9e6a4bb
JK
2371 /* For now, we don't support Ada. Still the function can be called if the
2372 current language is Ada for a non-Ada objfile using GNU index. As Ada
2373 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2374}
2375
2376static void
2377dw2_expand_symtabs_matching (struct objfile *objfile,
2378 int (*file_matcher) (const char *, void *),
2379 int (*name_matcher) (const char *, void *),
2380 domain_enum kind,
2381 void *data)
2382{
2383 int i;
2384 offset_type iter;
2385
2386 dw2_setup (objfile);
2387 if (!dwarf2_per_objfile->index_table)
2388 return;
2389
1fd400ff
TT
2390 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2391 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2392 {
2393 int j;
e254ef6a 2394 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2395
e254ef6a
DE
2396 per_cu->v.quick->mark = 0;
2397 if (per_cu->v.quick->symtab)
9291a0cd
TT
2398 continue;
2399
e254ef6a
DE
2400 dw2_require_line_header (objfile, per_cu);
2401 if (!per_cu->v.quick->lines)
9291a0cd
TT
2402 continue;
2403
e254ef6a 2404 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2405 {
e254ef6a 2406 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2407 {
e254ef6a 2408 per_cu->v.quick->mark = 1;
9291a0cd
TT
2409 break;
2410 }
2411 }
2412 }
2413
2414 for (iter = 0;
2415 iter < dwarf2_per_objfile->index_table->index_table_slots;
2416 ++iter)
2417 {
2418 offset_type idx = 2 * iter;
2419 const char *name;
2420 offset_type *vec, vec_len, vec_idx;
2421
2422 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2423 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2424 continue;
2425
2426 name = (dwarf2_per_objfile->index_table->constant_pool
2427 + dwarf2_per_objfile->index_table->index_table[idx]);
2428
2429 if (! (*name_matcher) (name, data))
2430 continue;
2431
2432 /* The name was matched, now expand corresponding CUs that were
2433 marked. */
2434 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2435 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2436 vec_len = MAYBE_SWAP (vec[0]);
2437 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2438 {
e254ef6a 2439 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2440
e254ef6a
DE
2441 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2442 if (per_cu->v.quick->mark)
2443 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2444 }
2445 }
2446}
2447
2448static struct symtab *
2449dw2_find_pc_sect_symtab (struct objfile *objfile,
2450 struct minimal_symbol *msymbol,
2451 CORE_ADDR pc,
2452 struct obj_section *section,
2453 int warn_if_readin)
2454{
2455 struct dwarf2_per_cu_data *data;
2456
2457 dw2_setup (objfile);
2458
2459 if (!objfile->psymtabs_addrmap)
2460 return NULL;
2461
2462 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2463 if (!data)
2464 return NULL;
2465
2466 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2467 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2468 paddress (get_objfile_arch (objfile), pc));
2469
2470 return dw2_instantiate_symtab (objfile, data);
2471}
2472
2473static void
2474dw2_map_symbol_names (struct objfile *objfile,
2475 void (*fun) (const char *, void *),
2476 void *data)
2477{
2478 offset_type iter;
2479 dw2_setup (objfile);
2480
2481 if (!dwarf2_per_objfile->index_table)
2482 return;
2483
2484 for (iter = 0;
2485 iter < dwarf2_per_objfile->index_table->index_table_slots;
2486 ++iter)
2487 {
2488 offset_type idx = 2 * iter;
2489 const char *name;
2490 offset_type *vec, vec_len, vec_idx;
2491
2492 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2493 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2494 continue;
2495
2496 name = (dwarf2_per_objfile->index_table->constant_pool
2497 + dwarf2_per_objfile->index_table->index_table[idx]);
2498
2499 (*fun) (name, data);
2500 }
2501}
2502
2503static void
2504dw2_map_symbol_filenames (struct objfile *objfile,
2505 void (*fun) (const char *, const char *, void *),
2506 void *data)
2507{
2508 int i;
2509
2510 dw2_setup (objfile);
1fd400ff
TT
2511 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2512 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2513 {
2514 int j;
e254ef6a 2515 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2516
e254ef6a 2517 if (per_cu->v.quick->symtab)
9291a0cd
TT
2518 continue;
2519
e254ef6a
DE
2520 dw2_require_line_header (objfile, per_cu);
2521 if (!per_cu->v.quick->lines)
9291a0cd
TT
2522 continue;
2523
e254ef6a 2524 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2525 {
e254ef6a
DE
2526 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2527 j);
2528 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2529 }
2530 }
2531}
2532
2533static int
2534dw2_has_symbols (struct objfile *objfile)
2535{
2536 return 1;
2537}
2538
2539const struct quick_symbol_functions dwarf2_gdb_index_functions =
2540{
2541 dw2_has_symbols,
2542 dw2_find_last_source_symtab,
2543 dw2_forget_cached_source_info,
2544 dw2_lookup_symtab,
2545 dw2_lookup_symbol,
774b6a14 2546 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2547 dw2_print_stats,
2548 dw2_dump,
2549 dw2_relocate,
2550 dw2_expand_symtabs_for_function,
2551 dw2_expand_all_symtabs,
2552 dw2_expand_symtabs_with_filename,
2553 dw2_find_symbol_file,
2554 dw2_map_ada_symtabs,
2555 dw2_expand_symtabs_matching,
2556 dw2_find_pc_sect_symtab,
2557 dw2_map_symbol_names,
2558 dw2_map_symbol_filenames
2559};
2560
2561/* Initialize for reading DWARF for this objfile. Return 0 if this
2562 file will use psymtabs, or 1 if using the GNU index. */
2563
2564int
2565dwarf2_initialize_objfile (struct objfile *objfile)
2566{
2567 /* If we're about to read full symbols, don't bother with the
2568 indices. In this case we also don't care if some other debug
2569 format is making psymtabs, because they are all about to be
2570 expanded anyway. */
2571 if ((objfile->flags & OBJF_READNOW))
2572 {
2573 int i;
2574
2575 dwarf2_per_objfile->using_index = 1;
2576 create_all_comp_units (objfile);
1fd400ff 2577 create_debug_types_hash_table (objfile);
9291a0cd 2578
1fd400ff
TT
2579 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2580 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2581 {
e254ef6a 2582 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2583
e254ef6a
DE
2584 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2585 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2586 }
2587
2588 /* Return 1 so that gdb sees the "quick" functions. However,
2589 these functions will be no-ops because we will have expanded
2590 all symtabs. */
2591 return 1;
2592 }
2593
2594 if (dwarf2_read_index (objfile))
2595 return 1;
2596
2597 dwarf2_build_psymtabs (objfile);
2598 return 0;
2599}
2600
2601\f
2602
dce234bc
PP
2603/* Build a partial symbol table. */
2604
2605void
f29dff0a 2606dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2607{
f29dff0a 2608 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2609 {
2610 init_psymbol_list (objfile, 1024);
2611 }
2612
d146bf1e 2613 dwarf2_build_psymtabs_hard (objfile);
c906108c 2614}
c906108c 2615
45452591
DE
2616/* Return TRUE if OFFSET is within CU_HEADER. */
2617
2618static inline int
2619offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2620{
2621 unsigned int bottom = cu_header->offset;
2622 unsigned int top = (cu_header->offset
2623 + cu_header->length
2624 + cu_header->initial_length_size);
9a619af0 2625
45452591
DE
2626 return (offset >= bottom && offset < top);
2627}
2628
93311388
DE
2629/* Read in the comp unit header information from the debug_info at info_ptr.
2630 NOTE: This leaves members offset, first_die_offset to be filled in
2631 by the caller. */
107d2387 2632
fe1b8b76 2633static gdb_byte *
107d2387 2634read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2635 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2636{
2637 int signed_addr;
891d2f0b 2638 unsigned int bytes_read;
c764a876
DE
2639
2640 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2641 cu_header->initial_length_size = bytes_read;
2642 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2643 info_ptr += bytes_read;
107d2387
AC
2644 cu_header->version = read_2_bytes (abfd, info_ptr);
2645 info_ptr += 2;
613e1657 2646 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2647 &bytes_read);
613e1657 2648 info_ptr += bytes_read;
107d2387
AC
2649 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2650 info_ptr += 1;
2651 signed_addr = bfd_get_sign_extend_vma (abfd);
2652 if (signed_addr < 0)
8e65ff28 2653 internal_error (__FILE__, __LINE__,
e2e0b3e5 2654 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2655 cu_header->signed_addr_p = signed_addr;
c764a876 2656
107d2387
AC
2657 return info_ptr;
2658}
2659
fe1b8b76
JB
2660static gdb_byte *
2661partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2662 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2663 bfd *abfd)
2664{
fe1b8b76 2665 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2666
2667 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2668
2dc7f7b3 2669 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2670 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2671 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2672 bfd_get_filename (abfd));
72bf9492 2673
dce234bc 2674 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2675 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2676 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2677 (long) header->abbrev_offset,
93311388 2678 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2679 bfd_get_filename (abfd));
2680
2681 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2682 > buffer + buffer_size)
8a3fe4f8
AC
2683 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2684 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2685 (long) header->length,
93311388 2686 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2687 bfd_get_filename (abfd));
2688
2689 return info_ptr;
2690}
2691
348e048f
DE
2692/* Read in the types comp unit header information from .debug_types entry at
2693 types_ptr. The result is a pointer to one past the end of the header. */
2694
2695static gdb_byte *
2696read_type_comp_unit_head (struct comp_unit_head *cu_header,
2697 ULONGEST *signature,
2698 gdb_byte *types_ptr, bfd *abfd)
2699{
348e048f
DE
2700 gdb_byte *initial_types_ptr = types_ptr;
2701
6e70227d 2702 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2703 &dwarf2_per_objfile->types);
348e048f
DE
2704 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2705
2706 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2707
2708 *signature = read_8_bytes (abfd, types_ptr);
2709 types_ptr += 8;
2710 types_ptr += cu_header->offset_size;
2711 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2712
2713 return types_ptr;
2714}
2715
aaa75496
JB
2716/* Allocate a new partial symtab for file named NAME and mark this new
2717 partial symtab as being an include of PST. */
2718
2719static void
2720dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2721 struct objfile *objfile)
2722{
2723 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2724
2725 subpst->section_offsets = pst->section_offsets;
2726 subpst->textlow = 0;
2727 subpst->texthigh = 0;
2728
2729 subpst->dependencies = (struct partial_symtab **)
2730 obstack_alloc (&objfile->objfile_obstack,
2731 sizeof (struct partial_symtab *));
2732 subpst->dependencies[0] = pst;
2733 subpst->number_of_dependencies = 1;
2734
2735 subpst->globals_offset = 0;
2736 subpst->n_global_syms = 0;
2737 subpst->statics_offset = 0;
2738 subpst->n_static_syms = 0;
2739 subpst->symtab = NULL;
2740 subpst->read_symtab = pst->read_symtab;
2741 subpst->readin = 0;
2742
2743 /* No private part is necessary for include psymtabs. This property
2744 can be used to differentiate between such include psymtabs and
10b3939b 2745 the regular ones. */
58a9656e 2746 subpst->read_symtab_private = NULL;
aaa75496
JB
2747}
2748
2749/* Read the Line Number Program data and extract the list of files
2750 included by the source file represented by PST. Build an include
d85a05f0 2751 partial symtab for each of these included files. */
aaa75496
JB
2752
2753static void
2754dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2755 struct die_info *die,
aaa75496
JB
2756 struct partial_symtab *pst)
2757{
2758 struct objfile *objfile = cu->objfile;
2759 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2760 struct line_header *lh = NULL;
2761 struct attribute *attr;
aaa75496 2762
d85a05f0
DJ
2763 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2764 if (attr)
2765 {
2766 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2767
d85a05f0
DJ
2768 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2769 }
aaa75496
JB
2770 if (lh == NULL)
2771 return; /* No linetable, so no includes. */
2772
c6da4cef
DE
2773 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2774 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2775
2776 free_line_header (lh);
2777}
2778
348e048f
DE
2779static hashval_t
2780hash_type_signature (const void *item)
2781{
2782 const struct signatured_type *type_sig = item;
9a619af0 2783
348e048f
DE
2784 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2785 return type_sig->signature;
2786}
2787
2788static int
2789eq_type_signature (const void *item_lhs, const void *item_rhs)
2790{
2791 const struct signatured_type *lhs = item_lhs;
2792 const struct signatured_type *rhs = item_rhs;
9a619af0 2793
348e048f
DE
2794 return lhs->signature == rhs->signature;
2795}
2796
1fd400ff
TT
2797/* Allocate a hash table for signatured types. */
2798
2799static htab_t
673bfd45 2800allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2801{
2802 return htab_create_alloc_ex (41,
2803 hash_type_signature,
2804 eq_type_signature,
2805 NULL,
2806 &objfile->objfile_obstack,
2807 hashtab_obstack_allocate,
2808 dummy_obstack_deallocate);
2809}
2810
2811/* A helper function to add a signatured type CU to a list. */
2812
2813static int
2814add_signatured_type_cu_to_list (void **slot, void *datum)
2815{
2816 struct signatured_type *sigt = *slot;
2817 struct dwarf2_per_cu_data ***datap = datum;
2818
2819 **datap = &sigt->per_cu;
2820 ++*datap;
2821
2822 return 1;
2823}
2824
348e048f
DE
2825/* Create the hash table of all entries in the .debug_types section.
2826 The result is zero if there is an error (e.g. missing .debug_types section),
2827 otherwise non-zero. */
2828
2829static int
2830create_debug_types_hash_table (struct objfile *objfile)
2831{
be391dca 2832 gdb_byte *info_ptr;
348e048f 2833 htab_t types_htab;
1fd400ff 2834 struct dwarf2_per_cu_data **iter;
348e048f 2835
be391dca
TT
2836 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2837 info_ptr = dwarf2_per_objfile->types.buffer;
2838
348e048f
DE
2839 if (info_ptr == NULL)
2840 {
2841 dwarf2_per_objfile->signatured_types = NULL;
2842 return 0;
2843 }
2844
673bfd45 2845 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2846
2847 if (dwarf2_die_debug)
2848 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2849
2850 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2851 {
2852 unsigned int offset;
2853 unsigned int offset_size;
2854 unsigned int type_offset;
2855 unsigned int length, initial_length_size;
2856 unsigned short version;
2857 ULONGEST signature;
2858 struct signatured_type *type_sig;
2859 void **slot;
2860 gdb_byte *ptr = info_ptr;
2861
2862 offset = ptr - dwarf2_per_objfile->types.buffer;
2863
2864 /* We need to read the type's signature in order to build the hash
2865 table, but we don't need to read anything else just yet. */
2866
2867 /* Sanity check to ensure entire cu is present. */
2868 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2869 if (ptr + length + initial_length_size
2870 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2871 {
2872 complaint (&symfile_complaints,
2873 _("debug type entry runs off end of `.debug_types' section, ignored"));
2874 break;
2875 }
2876
2877 offset_size = initial_length_size == 4 ? 4 : 8;
2878 ptr += initial_length_size;
2879 version = bfd_get_16 (objfile->obfd, ptr);
2880 ptr += 2;
2881 ptr += offset_size; /* abbrev offset */
2882 ptr += 1; /* address size */
2883 signature = bfd_get_64 (objfile->obfd, ptr);
2884 ptr += 8;
2885 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2886
2887 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2888 memset (type_sig, 0, sizeof (*type_sig));
2889 type_sig->signature = signature;
2890 type_sig->offset = offset;
2891 type_sig->type_offset = type_offset;
ca1f3406 2892 type_sig->per_cu.objfile = objfile;
1fd400ff 2893 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2894
2895 slot = htab_find_slot (types_htab, type_sig, INSERT);
2896 gdb_assert (slot != NULL);
2897 *slot = type_sig;
2898
2899 if (dwarf2_die_debug)
2900 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2901 offset, phex (signature, sizeof (signature)));
2902
2903 info_ptr = info_ptr + initial_length_size + length;
2904 }
2905
2906 dwarf2_per_objfile->signatured_types = types_htab;
2907
1fd400ff
TT
2908 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2909 dwarf2_per_objfile->type_comp_units
2910 = obstack_alloc (&objfile->objfile_obstack,
2911 dwarf2_per_objfile->n_type_comp_units
2912 * sizeof (struct dwarf2_per_cu_data *));
2913 iter = &dwarf2_per_objfile->type_comp_units[0];
2914 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2915 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2916 == dwarf2_per_objfile->n_type_comp_units);
2917
348e048f
DE
2918 return 1;
2919}
2920
2921/* Lookup a signature based type.
2922 Returns NULL if SIG is not present in the table. */
2923
2924static struct signatured_type *
2925lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2926{
2927 struct signatured_type find_entry, *entry;
2928
2929 if (dwarf2_per_objfile->signatured_types == NULL)
2930 {
2931 complaint (&symfile_complaints,
2932 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2933 return 0;
2934 }
2935
2936 find_entry.signature = sig;
2937 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2938 return entry;
2939}
2940
d85a05f0
DJ
2941/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2942
2943static void
2944init_cu_die_reader (struct die_reader_specs *reader,
2945 struct dwarf2_cu *cu)
2946{
2947 reader->abfd = cu->objfile->obfd;
2948 reader->cu = cu;
2949 if (cu->per_cu->from_debug_types)
be391dca
TT
2950 {
2951 gdb_assert (dwarf2_per_objfile->types.readin);
2952 reader->buffer = dwarf2_per_objfile->types.buffer;
2953 }
d85a05f0 2954 else
be391dca
TT
2955 {
2956 gdb_assert (dwarf2_per_objfile->info.readin);
2957 reader->buffer = dwarf2_per_objfile->info.buffer;
2958 }
d85a05f0
DJ
2959}
2960
2961/* Find the base address of the compilation unit for range lists and
2962 location lists. It will normally be specified by DW_AT_low_pc.
2963 In DWARF-3 draft 4, the base address could be overridden by
2964 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2965 compilation units with discontinuous ranges. */
2966
2967static void
2968dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2969{
2970 struct attribute *attr;
2971
2972 cu->base_known = 0;
2973 cu->base_address = 0;
2974
2975 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2976 if (attr)
2977 {
2978 cu->base_address = DW_ADDR (attr);
2979 cu->base_known = 1;
2980 }
2981 else
2982 {
2983 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2984 if (attr)
2985 {
2986 cu->base_address = DW_ADDR (attr);
2987 cu->base_known = 1;
2988 }
2989 }
2990}
2991
348e048f
DE
2992/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2993 to combine the common parts.
93311388 2994 Process a compilation unit for a psymtab.
348e048f
DE
2995 BUFFER is a pointer to the beginning of the dwarf section buffer,
2996 either .debug_info or debug_types.
93311388
DE
2997 INFO_PTR is a pointer to the start of the CU.
2998 Returns a pointer to the next CU. */
aaa75496 2999
93311388
DE
3000static gdb_byte *
3001process_psymtab_comp_unit (struct objfile *objfile,
3002 struct dwarf2_per_cu_data *this_cu,
3003 gdb_byte *buffer, gdb_byte *info_ptr,
3004 unsigned int buffer_size)
c906108c 3005{
c906108c 3006 bfd *abfd = objfile->obfd;
93311388 3007 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3008 struct die_info *comp_unit_die;
c906108c 3009 struct partial_symtab *pst;
5734ee8b 3010 CORE_ADDR baseaddr;
93311388
DE
3011 struct cleanup *back_to_inner;
3012 struct dwarf2_cu cu;
d85a05f0
DJ
3013 int has_children, has_pc_info;
3014 struct attribute *attr;
d85a05f0
DJ
3015 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3016 struct die_reader_specs reader_specs;
c906108c 3017
93311388
DE
3018 memset (&cu, 0, sizeof (cu));
3019 cu.objfile = objfile;
3020 obstack_init (&cu.comp_unit_obstack);
c906108c 3021
93311388 3022 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3023
93311388
DE
3024 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3025 buffer, buffer_size,
3026 abfd);
10b3939b 3027
93311388
DE
3028 /* Complete the cu_header. */
3029 cu.header.offset = beg_of_comp_unit - buffer;
3030 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3031
93311388 3032 cu.list_in_scope = &file_symbols;
af703f96 3033
328c9494
DJ
3034 /* If this compilation unit was already read in, free the
3035 cached copy in order to read it in again. This is
3036 necessary because we skipped some symbols when we first
3037 read in the compilation unit (see load_partial_dies).
3038 This problem could be avoided, but the benefit is
3039 unclear. */
3040 if (this_cu->cu != NULL)
3041 free_one_cached_comp_unit (this_cu->cu);
3042
3043 /* Note that this is a pointer to our stack frame, being
3044 added to a global data structure. It will be cleaned up
3045 in free_stack_comp_unit when we finish with this
3046 compilation unit. */
3047 this_cu->cu = &cu;
d85a05f0
DJ
3048 cu.per_cu = this_cu;
3049
93311388
DE
3050 /* Read the abbrevs for this compilation unit into a table. */
3051 dwarf2_read_abbrevs (abfd, &cu);
3052 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3053
93311388 3054 /* Read the compilation unit die. */
348e048f
DE
3055 if (this_cu->from_debug_types)
3056 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3057 init_cu_die_reader (&reader_specs, &cu);
3058 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3059 &has_children);
93311388 3060
348e048f
DE
3061 if (this_cu->from_debug_types)
3062 {
3063 /* offset,length haven't been set yet for type units. */
3064 this_cu->offset = cu.header.offset;
3065 this_cu->length = cu.header.length + cu.header.initial_length_size;
3066 }
d85a05f0 3067 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3068 {
93311388
DE
3069 info_ptr = (beg_of_comp_unit + cu.header.length
3070 + cu.header.initial_length_size);
3071 do_cleanups (back_to_inner);
3072 return info_ptr;
3073 }
72bf9492 3074
93311388 3075 /* Set the language we're debugging. */
d85a05f0
DJ
3076 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3077 if (attr)
3078 set_cu_language (DW_UNSND (attr), &cu);
3079 else
3080 set_cu_language (language_minimal, &cu);
c906108c 3081
93311388 3082 /* Allocate a new partial symbol table structure. */
d85a05f0 3083 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3084 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3085 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3086 /* TEXTLOW and TEXTHIGH are set below. */
3087 0,
3088 objfile->global_psymbols.next,
3089 objfile->static_psymbols.next);
72bf9492 3090
d85a05f0
DJ
3091 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3092 if (attr != NULL)
3093 pst->dirname = DW_STRING (attr);
72bf9492 3094
e38df1d0 3095 pst->read_symtab_private = this_cu;
72bf9492 3096
93311388 3097 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3098
93311388
DE
3099 /* Store the function that reads in the rest of the symbol table */
3100 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3101
9291a0cd 3102 this_cu->v.psymtab = pst;
c906108c 3103
d85a05f0
DJ
3104 dwarf2_find_base_address (comp_unit_die, &cu);
3105
93311388
DE
3106 /* Possibly set the default values of LOWPC and HIGHPC from
3107 `DW_AT_ranges'. */
d85a05f0
DJ
3108 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3109 &best_highpc, &cu, pst);
3110 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3111 /* Store the contiguous range if it is not empty; it can be empty for
3112 CUs with no code. */
3113 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3114 best_lowpc + baseaddr,
3115 best_highpc + baseaddr - 1, pst);
93311388
DE
3116
3117 /* Check if comp unit has_children.
3118 If so, read the rest of the partial symbols from this comp unit.
3119 If not, there's no more debug_info for this comp unit. */
d85a05f0 3120 if (has_children)
93311388
DE
3121 {
3122 struct partial_die_info *first_die;
3123 CORE_ADDR lowpc, highpc;
31ffec48 3124
93311388
DE
3125 lowpc = ((CORE_ADDR) -1);
3126 highpc = ((CORE_ADDR) 0);
c906108c 3127
93311388 3128 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3129
93311388 3130 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3131 ! has_pc_info, &cu);
57c22c6c 3132
93311388
DE
3133 /* If we didn't find a lowpc, set it to highpc to avoid
3134 complaints from `maint check'. */
3135 if (lowpc == ((CORE_ADDR) -1))
3136 lowpc = highpc;
10b3939b 3137
93311388
DE
3138 /* If the compilation unit didn't have an explicit address range,
3139 then use the information extracted from its child dies. */
d85a05f0 3140 if (! has_pc_info)
93311388 3141 {
d85a05f0
DJ
3142 best_lowpc = lowpc;
3143 best_highpc = highpc;
93311388
DE
3144 }
3145 }
d85a05f0
DJ
3146 pst->textlow = best_lowpc + baseaddr;
3147 pst->texthigh = best_highpc + baseaddr;
c906108c 3148
93311388
DE
3149 pst->n_global_syms = objfile->global_psymbols.next -
3150 (objfile->global_psymbols.list + pst->globals_offset);
3151 pst->n_static_syms = objfile->static_psymbols.next -
3152 (objfile->static_psymbols.list + pst->statics_offset);
3153 sort_pst_symbols (pst);
c906108c 3154
93311388
DE
3155 info_ptr = (beg_of_comp_unit + cu.header.length
3156 + cu.header.initial_length_size);
ae038cb0 3157
348e048f
DE
3158 if (this_cu->from_debug_types)
3159 {
3160 /* It's not clear we want to do anything with stmt lists here.
3161 Waiting to see what gcc ultimately does. */
3162 }
d85a05f0 3163 else
93311388
DE
3164 {
3165 /* Get the list of files included in the current compilation unit,
3166 and build a psymtab for each of them. */
d85a05f0 3167 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3168 }
ae038cb0 3169
93311388 3170 do_cleanups (back_to_inner);
ae038cb0 3171
93311388
DE
3172 return info_ptr;
3173}
ff013f42 3174
348e048f
DE
3175/* Traversal function for htab_traverse_noresize.
3176 Process one .debug_types comp-unit. */
3177
3178static int
3179process_type_comp_unit (void **slot, void *info)
3180{
3181 struct signatured_type *entry = (struct signatured_type *) *slot;
3182 struct objfile *objfile = (struct objfile *) info;
3183 struct dwarf2_per_cu_data *this_cu;
3184
3185 this_cu = &entry->per_cu;
348e048f 3186
be391dca 3187 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3188 process_psymtab_comp_unit (objfile, this_cu,
3189 dwarf2_per_objfile->types.buffer,
3190 dwarf2_per_objfile->types.buffer + entry->offset,
3191 dwarf2_per_objfile->types.size);
3192
3193 return 1;
3194}
3195
3196/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3197 Build partial symbol tables for the .debug_types comp-units. */
3198
3199static void
3200build_type_psymtabs (struct objfile *objfile)
3201{
3202 if (! create_debug_types_hash_table (objfile))
3203 return;
3204
3205 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3206 process_type_comp_unit, objfile);
3207}
3208
60606b2c
TT
3209/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3210
3211static void
3212psymtabs_addrmap_cleanup (void *o)
3213{
3214 struct objfile *objfile = o;
ec61707d 3215
60606b2c
TT
3216 objfile->psymtabs_addrmap = NULL;
3217}
3218
93311388
DE
3219/* Build the partial symbol table by doing a quick pass through the
3220 .debug_info and .debug_abbrev sections. */
72bf9492 3221
93311388 3222static void
c67a9c90 3223dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3224{
93311388 3225 gdb_byte *info_ptr;
60606b2c
TT
3226 struct cleanup *back_to, *addrmap_cleanup;
3227 struct obstack temp_obstack;
93311388 3228
98bfdba5
PA
3229 dwarf2_per_objfile->reading_partial_symbols = 1;
3230
be391dca 3231 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3232 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3233
93311388
DE
3234 /* Any cached compilation units will be linked by the per-objfile
3235 read_in_chain. Make sure to free them when we're done. */
3236 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3237
348e048f
DE
3238 build_type_psymtabs (objfile);
3239
93311388 3240 create_all_comp_units (objfile);
c906108c 3241
60606b2c
TT
3242 /* Create a temporary address map on a temporary obstack. We later
3243 copy this to the final obstack. */
3244 obstack_init (&temp_obstack);
3245 make_cleanup_obstack_free (&temp_obstack);
3246 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3247 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3248
93311388
DE
3249 /* Since the objects we're extracting from .debug_info vary in
3250 length, only the individual functions to extract them (like
3251 read_comp_unit_head and load_partial_die) can really know whether
3252 the buffer is large enough to hold another complete object.
c906108c 3253
93311388
DE
3254 At the moment, they don't actually check that. If .debug_info
3255 holds just one extra byte after the last compilation unit's dies,
3256 then read_comp_unit_head will happily read off the end of the
3257 buffer. read_partial_die is similarly casual. Those functions
3258 should be fixed.
c906108c 3259
93311388
DE
3260 For this loop condition, simply checking whether there's any data
3261 left at all should be sufficient. */
c906108c 3262
93311388
DE
3263 while (info_ptr < (dwarf2_per_objfile->info.buffer
3264 + dwarf2_per_objfile->info.size))
3265 {
3266 struct dwarf2_per_cu_data *this_cu;
dd373385 3267
93311388
DE
3268 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3269 objfile);
aaa75496 3270
93311388
DE
3271 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3272 dwarf2_per_objfile->info.buffer,
3273 info_ptr,
3274 dwarf2_per_objfile->info.size);
c906108c 3275 }
ff013f42
JK
3276
3277 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3278 &objfile->objfile_obstack);
60606b2c 3279 discard_cleanups (addrmap_cleanup);
ff013f42 3280
ae038cb0
DJ
3281 do_cleanups (back_to);
3282}
3283
93311388 3284/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3285
3286static void
93311388
DE
3287load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3288 struct objfile *objfile)
ae038cb0
DJ
3289{
3290 bfd *abfd = objfile->obfd;
fe1b8b76 3291 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3292 struct die_info *comp_unit_die;
ae038cb0 3293 struct dwarf2_cu *cu;
1d9ec526 3294 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3295 struct attribute *attr;
3296 int has_children;
3297 struct die_reader_specs reader_specs;
98bfdba5 3298 int read_cu = 0;
ae038cb0 3299
348e048f
DE
3300 gdb_assert (! this_cu->from_debug_types);
3301
be391dca 3302 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3303 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3304 beg_of_comp_unit = info_ptr;
3305
98bfdba5
PA
3306 if (this_cu->cu == NULL)
3307 {
3308 cu = alloc_one_comp_unit (objfile);
ae038cb0 3309
98bfdba5 3310 read_cu = 1;
ae038cb0 3311
98bfdba5
PA
3312 /* If an error occurs while loading, release our storage. */
3313 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3314
98bfdba5
PA
3315 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3316 dwarf2_per_objfile->info.buffer,
3317 dwarf2_per_objfile->info.size,
3318 abfd);
ae038cb0 3319
98bfdba5
PA
3320 /* Complete the cu_header. */
3321 cu->header.offset = this_cu->offset;
3322 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3323
3324 /* Link this compilation unit into the compilation unit tree. */
3325 this_cu->cu = cu;
3326 cu->per_cu = this_cu;
98bfdba5
PA
3327
3328 /* Link this CU into read_in_chain. */
3329 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3330 dwarf2_per_objfile->read_in_chain = this_cu;
3331 }
3332 else
3333 {
3334 cu = this_cu->cu;
3335 info_ptr += cu->header.first_die_offset;
3336 }
ae038cb0
DJ
3337
3338 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3339 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3340 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3341 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3342
3343 /* Read the compilation unit die. */
d85a05f0
DJ
3344 init_cu_die_reader (&reader_specs, cu);
3345 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3346 &has_children);
ae038cb0
DJ
3347
3348 /* Set the language we're debugging. */
d85a05f0
DJ
3349 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3350 if (attr)
3351 set_cu_language (DW_UNSND (attr), cu);
3352 else
3353 set_cu_language (language_minimal, cu);
ae038cb0 3354
ae038cb0
DJ
3355 /* Check if comp unit has_children.
3356 If so, read the rest of the partial symbols from this comp unit.
3357 If not, there's no more debug_info for this comp unit. */
d85a05f0 3358 if (has_children)
93311388 3359 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3360
98bfdba5
PA
3361 do_cleanups (free_abbrevs_cleanup);
3362
3363 if (read_cu)
3364 {
3365 /* We've successfully allocated this compilation unit. Let our
3366 caller clean it up when finished with it. */
3367 discard_cleanups (free_cu_cleanup);
3368 }
ae038cb0
DJ
3369}
3370
3371/* Create a list of all compilation units in OBJFILE. We do this only
3372 if an inter-comp-unit reference is found; presumably if there is one,
3373 there will be many, and one will occur early in the .debug_info section.
3374 So there's no point in building this list incrementally. */
3375
3376static void
3377create_all_comp_units (struct objfile *objfile)
3378{
3379 int n_allocated;
3380 int n_comp_units;
3381 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3382 gdb_byte *info_ptr;
3383
3384 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3385 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3386
3387 n_comp_units = 0;
3388 n_allocated = 10;
3389 all_comp_units = xmalloc (n_allocated
3390 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3391
dce234bc 3392 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3393 {
c764a876 3394 unsigned int length, initial_length_size;
ae038cb0 3395 struct dwarf2_per_cu_data *this_cu;
c764a876 3396 unsigned int offset;
ae038cb0 3397
dce234bc 3398 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3399
3400 /* Read just enough information to find out where the next
3401 compilation unit is. */
c764a876
DE
3402 length = read_initial_length (objfile->obfd, info_ptr,
3403 &initial_length_size);
ae038cb0
DJ
3404
3405 /* Save the compilation unit for later lookup. */
3406 this_cu = obstack_alloc (&objfile->objfile_obstack,
3407 sizeof (struct dwarf2_per_cu_data));
3408 memset (this_cu, 0, sizeof (*this_cu));
3409 this_cu->offset = offset;
c764a876 3410 this_cu->length = length + initial_length_size;
9291a0cd 3411 this_cu->objfile = objfile;
ae038cb0
DJ
3412
3413 if (n_comp_units == n_allocated)
3414 {
3415 n_allocated *= 2;
3416 all_comp_units = xrealloc (all_comp_units,
3417 n_allocated
3418 * sizeof (struct dwarf2_per_cu_data *));
3419 }
3420 all_comp_units[n_comp_units++] = this_cu;
3421
3422 info_ptr = info_ptr + this_cu->length;
3423 }
3424
3425 dwarf2_per_objfile->all_comp_units
3426 = obstack_alloc (&objfile->objfile_obstack,
3427 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3428 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3429 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3430 xfree (all_comp_units);
3431 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3432}
3433
5734ee8b
DJ
3434/* Process all loaded DIEs for compilation unit CU, starting at
3435 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3436 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3437 DW_AT_ranges). If NEED_PC is set, then this function will set
3438 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3439 and record the covered ranges in the addrmap. */
c906108c 3440
72bf9492
DJ
3441static void
3442scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3443 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3444{
72bf9492 3445 struct partial_die_info *pdi;
c906108c 3446
91c24f0a
DC
3447 /* Now, march along the PDI's, descending into ones which have
3448 interesting children but skipping the children of the other ones,
3449 until we reach the end of the compilation unit. */
c906108c 3450
72bf9492 3451 pdi = first_die;
91c24f0a 3452
72bf9492
DJ
3453 while (pdi != NULL)
3454 {
3455 fixup_partial_die (pdi, cu);
c906108c 3456
f55ee35c 3457 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3458 children, so we need to look at them. Ditto for anonymous
3459 enums. */
933c6fe4 3460
72bf9492 3461 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3462 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3463 {
72bf9492 3464 switch (pdi->tag)
c906108c
SS
3465 {
3466 case DW_TAG_subprogram:
5734ee8b 3467 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3468 break;
3469 case DW_TAG_variable:
3470 case DW_TAG_typedef:
91c24f0a 3471 case DW_TAG_union_type:
72bf9492 3472 if (!pdi->is_declaration)
63d06c5c 3473 {
72bf9492 3474 add_partial_symbol (pdi, cu);
63d06c5c
DC
3475 }
3476 break;
c906108c 3477 case DW_TAG_class_type:
680b30c7 3478 case DW_TAG_interface_type:
c906108c 3479 case DW_TAG_structure_type:
72bf9492 3480 if (!pdi->is_declaration)
c906108c 3481 {
72bf9492 3482 add_partial_symbol (pdi, cu);
c906108c
SS
3483 }
3484 break;
91c24f0a 3485 case DW_TAG_enumeration_type:
72bf9492
DJ
3486 if (!pdi->is_declaration)
3487 add_partial_enumeration (pdi, cu);
c906108c
SS
3488 break;
3489 case DW_TAG_base_type:
a02abb62 3490 case DW_TAG_subrange_type:
c906108c 3491 /* File scope base type definitions are added to the partial
c5aa993b 3492 symbol table. */
72bf9492 3493 add_partial_symbol (pdi, cu);
c906108c 3494 break;
d9fa45fe 3495 case DW_TAG_namespace:
5734ee8b 3496 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3497 break;
5d7cb8df
JK
3498 case DW_TAG_module:
3499 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3500 break;
c906108c
SS
3501 default:
3502 break;
3503 }
3504 }
3505
72bf9492
DJ
3506 /* If the die has a sibling, skip to the sibling. */
3507
3508 pdi = pdi->die_sibling;
3509 }
3510}
3511
3512/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3513
72bf9492 3514 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3515 name is concatenated with "::" and the partial DIE's name. For
3516 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3517 Enumerators are an exception; they use the scope of their parent
3518 enumeration type, i.e. the name of the enumeration type is not
3519 prepended to the enumerator.
91c24f0a 3520
72bf9492
DJ
3521 There are two complexities. One is DW_AT_specification; in this
3522 case "parent" means the parent of the target of the specification,
3523 instead of the direct parent of the DIE. The other is compilers
3524 which do not emit DW_TAG_namespace; in this case we try to guess
3525 the fully qualified name of structure types from their members'
3526 linkage names. This must be done using the DIE's children rather
3527 than the children of any DW_AT_specification target. We only need
3528 to do this for structures at the top level, i.e. if the target of
3529 any DW_AT_specification (if any; otherwise the DIE itself) does not
3530 have a parent. */
3531
3532/* Compute the scope prefix associated with PDI's parent, in
3533 compilation unit CU. The result will be allocated on CU's
3534 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3535 field. NULL is returned if no prefix is necessary. */
3536static char *
3537partial_die_parent_scope (struct partial_die_info *pdi,
3538 struct dwarf2_cu *cu)
3539{
3540 char *grandparent_scope;
3541 struct partial_die_info *parent, *real_pdi;
91c24f0a 3542
72bf9492
DJ
3543 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3544 then this means the parent of the specification DIE. */
3545
3546 real_pdi = pdi;
72bf9492 3547 while (real_pdi->has_specification)
10b3939b 3548 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3549
3550 parent = real_pdi->die_parent;
3551 if (parent == NULL)
3552 return NULL;
3553
3554 if (parent->scope_set)
3555 return parent->scope;
3556
3557 fixup_partial_die (parent, cu);
3558
10b3939b 3559 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3560
acebe513
UW
3561 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3562 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3563 Work around this problem here. */
3564 if (cu->language == language_cplus
6e70227d 3565 && parent->tag == DW_TAG_namespace
acebe513
UW
3566 && strcmp (parent->name, "::") == 0
3567 && grandparent_scope == NULL)
3568 {
3569 parent->scope = NULL;
3570 parent->scope_set = 1;
3571 return NULL;
3572 }
3573
72bf9492 3574 if (parent->tag == DW_TAG_namespace
f55ee35c 3575 || parent->tag == DW_TAG_module
72bf9492
DJ
3576 || parent->tag == DW_TAG_structure_type
3577 || parent->tag == DW_TAG_class_type
680b30c7 3578 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3579 || parent->tag == DW_TAG_union_type
3580 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3581 {
3582 if (grandparent_scope == NULL)
3583 parent->scope = parent->name;
3584 else
987504bb 3585 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3586 parent->name, 0, cu);
72bf9492 3587 }
ceeb3d5a 3588 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3589 /* Enumerators should not get the name of the enumeration as a prefix. */
3590 parent->scope = grandparent_scope;
3591 else
3592 {
3593 /* FIXME drow/2004-04-01: What should we be doing with
3594 function-local names? For partial symbols, we should probably be
3595 ignoring them. */
3596 complaint (&symfile_complaints,
e2e0b3e5 3597 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3598 parent->tag, pdi->offset);
3599 parent->scope = grandparent_scope;
c906108c
SS
3600 }
3601
72bf9492
DJ
3602 parent->scope_set = 1;
3603 return parent->scope;
3604}
3605
3606/* Return the fully scoped name associated with PDI, from compilation unit
3607 CU. The result will be allocated with malloc. */
3608static char *
3609partial_die_full_name (struct partial_die_info *pdi,
3610 struct dwarf2_cu *cu)
3611{
3612 char *parent_scope;
3613
98bfdba5
PA
3614 /* If this is a template instantiation, we can not work out the
3615 template arguments from partial DIEs. So, unfortunately, we have
3616 to go through the full DIEs. At least any work we do building
3617 types here will be reused if full symbols are loaded later. */
3618 if (pdi->has_template_arguments)
3619 {
3620 fixup_partial_die (pdi, cu);
3621
3622 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3623 {
3624 struct die_info *die;
3625 struct attribute attr;
3626 struct dwarf2_cu *ref_cu = cu;
3627
3628 attr.name = 0;
3629 attr.form = DW_FORM_ref_addr;
3630 attr.u.addr = pdi->offset;
3631 die = follow_die_ref (NULL, &attr, &ref_cu);
3632
3633 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3634 }
3635 }
3636
72bf9492
DJ
3637 parent_scope = partial_die_parent_scope (pdi, cu);
3638 if (parent_scope == NULL)
3639 return NULL;
3640 else
f55ee35c 3641 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3642}
3643
3644static void
72bf9492 3645add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3646{
e7c27a73 3647 struct objfile *objfile = cu->objfile;
c906108c 3648 CORE_ADDR addr = 0;
decbce07 3649 char *actual_name = NULL;
5c4e30ca 3650 const struct partial_symbol *psym = NULL;
e142c38c 3651 CORE_ADDR baseaddr;
72bf9492 3652 int built_actual_name = 0;
e142c38c
DJ
3653
3654 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3655
94af9270
KS
3656 actual_name = partial_die_full_name (pdi, cu);
3657 if (actual_name)
3658 built_actual_name = 1;
63d06c5c 3659
72bf9492
DJ
3660 if (actual_name == NULL)
3661 actual_name = pdi->name;
3662
c906108c
SS
3663 switch (pdi->tag)
3664 {
3665 case DW_TAG_subprogram:
2cfa0c8d 3666 if (pdi->is_external || cu->language == language_ada)
c906108c 3667 {
2cfa0c8d
JB
3668 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3669 of the global scope. But in Ada, we want to be able to access
3670 nested procedures globally. So all Ada subprograms are stored
3671 in the global scope. */
38d518c9 3672 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3673 mst_text, objfile); */
38d518c9 3674 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3675 built_actual_name,
5c4e30ca
DC
3676 VAR_DOMAIN, LOC_BLOCK,
3677 &objfile->global_psymbols,
3678 0, pdi->lowpc + baseaddr,
e142c38c 3679 cu->language, objfile);
c906108c
SS
3680 }
3681 else
3682 {
38d518c9 3683 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3684 mst_file_text, objfile); */
38d518c9 3685 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3686 built_actual_name,
5c4e30ca
DC
3687 VAR_DOMAIN, LOC_BLOCK,
3688 &objfile->static_psymbols,
3689 0, pdi->lowpc + baseaddr,
e142c38c 3690 cu->language, objfile);
c906108c
SS
3691 }
3692 break;
3693 case DW_TAG_variable:
caac4577
JG
3694 if (pdi->locdesc)
3695 addr = decode_locdesc (pdi->locdesc, cu);
3696
3697 if (pdi->locdesc
3698 && addr == 0
3699 && !dwarf2_per_objfile->has_section_at_zero)
3700 {
3701 /* A global or static variable may also have been stripped
3702 out by the linker if unused, in which case its address
3703 will be nullified; do not add such variables into partial
3704 symbol table then. */
3705 }
3706 else if (pdi->is_external)
c906108c
SS
3707 {
3708 /* Global Variable.
3709 Don't enter into the minimal symbol tables as there is
3710 a minimal symbol table entry from the ELF symbols already.
3711 Enter into partial symbol table if it has a location
3712 descriptor or a type.
3713 If the location descriptor is missing, new_symbol will create
3714 a LOC_UNRESOLVED symbol, the address of the variable will then
3715 be determined from the minimal symbol table whenever the variable
3716 is referenced.
3717 The address for the partial symbol table entry is not
3718 used by GDB, but it comes in handy for debugging partial symbol
3719 table building. */
3720
c906108c 3721 if (pdi->locdesc || pdi->has_type)
38d518c9 3722 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3723 built_actual_name,
5c4e30ca
DC
3724 VAR_DOMAIN, LOC_STATIC,
3725 &objfile->global_psymbols,
3726 0, addr + baseaddr,
e142c38c 3727 cu->language, objfile);
c906108c
SS
3728 }
3729 else
3730 {
3731 /* Static Variable. Skip symbols without location descriptors. */
3732 if (pdi->locdesc == NULL)
decbce07
MS
3733 {
3734 if (built_actual_name)
3735 xfree (actual_name);
3736 return;
3737 }
38d518c9 3738 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3739 mst_file_data, objfile); */
38d518c9 3740 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3741 built_actual_name,
5c4e30ca
DC
3742 VAR_DOMAIN, LOC_STATIC,
3743 &objfile->static_psymbols,
3744 0, addr + baseaddr,
e142c38c 3745 cu->language, objfile);
c906108c
SS
3746 }
3747 break;
3748 case DW_TAG_typedef:
3749 case DW_TAG_base_type:
a02abb62 3750 case DW_TAG_subrange_type:
38d518c9 3751 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3752 built_actual_name,
176620f1 3753 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3754 &objfile->static_psymbols,
e142c38c 3755 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3756 break;
72bf9492
DJ
3757 case DW_TAG_namespace:
3758 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3759 built_actual_name,
72bf9492
DJ
3760 VAR_DOMAIN, LOC_TYPEDEF,
3761 &objfile->global_psymbols,
3762 0, (CORE_ADDR) 0, cu->language, objfile);
3763 break;
c906108c 3764 case DW_TAG_class_type:
680b30c7 3765 case DW_TAG_interface_type:
c906108c
SS
3766 case DW_TAG_structure_type:
3767 case DW_TAG_union_type:
3768 case DW_TAG_enumeration_type:
fa4028e9
JB
3769 /* Skip external references. The DWARF standard says in the section
3770 about "Structure, Union, and Class Type Entries": "An incomplete
3771 structure, union or class type is represented by a structure,
3772 union or class entry that does not have a byte size attribute
3773 and that has a DW_AT_declaration attribute." */
3774 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3775 {
3776 if (built_actual_name)
3777 xfree (actual_name);
3778 return;
3779 }
fa4028e9 3780
63d06c5c
DC
3781 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3782 static vs. global. */
38d518c9 3783 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3784 built_actual_name,
176620f1 3785 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3786 (cu->language == language_cplus
3787 || cu->language == language_java)
63d06c5c
DC
3788 ? &objfile->global_psymbols
3789 : &objfile->static_psymbols,
e142c38c 3790 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3791
c906108c
SS
3792 break;
3793 case DW_TAG_enumerator:
38d518c9 3794 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3795 built_actual_name,
176620f1 3796 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3797 (cu->language == language_cplus
3798 || cu->language == language_java)
f6fe98ef
DJ
3799 ? &objfile->global_psymbols
3800 : &objfile->static_psymbols,
e142c38c 3801 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3802 break;
3803 default:
3804 break;
3805 }
5c4e30ca 3806
72bf9492
DJ
3807 if (built_actual_name)
3808 xfree (actual_name);
c906108c
SS
3809}
3810
5c4e30ca
DC
3811/* Read a partial die corresponding to a namespace; also, add a symbol
3812 corresponding to that namespace to the symbol table. NAMESPACE is
3813 the name of the enclosing namespace. */
91c24f0a 3814
72bf9492
DJ
3815static void
3816add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3817 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3818 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3819{
72bf9492 3820 /* Add a symbol for the namespace. */
e7c27a73 3821
72bf9492 3822 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3823
3824 /* Now scan partial symbols in that namespace. */
3825
91c24f0a 3826 if (pdi->has_children)
5734ee8b 3827 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3828}
3829
5d7cb8df
JK
3830/* Read a partial die corresponding to a Fortran module. */
3831
3832static void
3833add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3834 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3835{
f55ee35c 3836 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3837
3838 if (pdi->has_children)
3839 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3840}
3841
bc30ff58
JB
3842/* Read a partial die corresponding to a subprogram and create a partial
3843 symbol for that subprogram. When the CU language allows it, this
3844 routine also defines a partial symbol for each nested subprogram
3845 that this subprogram contains.
6e70227d 3846
bc30ff58
JB
3847 DIE my also be a lexical block, in which case we simply search
3848 recursively for suprograms defined inside that lexical block.
3849 Again, this is only performed when the CU language allows this
3850 type of definitions. */
3851
3852static void
3853add_partial_subprogram (struct partial_die_info *pdi,
3854 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3855 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3856{
3857 if (pdi->tag == DW_TAG_subprogram)
3858 {
3859 if (pdi->has_pc_info)
3860 {
3861 if (pdi->lowpc < *lowpc)
3862 *lowpc = pdi->lowpc;
3863 if (pdi->highpc > *highpc)
3864 *highpc = pdi->highpc;
5734ee8b
DJ
3865 if (need_pc)
3866 {
3867 CORE_ADDR baseaddr;
3868 struct objfile *objfile = cu->objfile;
3869
3870 baseaddr = ANOFFSET (objfile->section_offsets,
3871 SECT_OFF_TEXT (objfile));
3872 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3873 pdi->lowpc + baseaddr,
3874 pdi->highpc - 1 + baseaddr,
9291a0cd 3875 cu->per_cu->v.psymtab);
5734ee8b 3876 }
bc30ff58 3877 if (!pdi->is_declaration)
e8d05480
JB
3878 /* Ignore subprogram DIEs that do not have a name, they are
3879 illegal. Do not emit a complaint at this point, we will
3880 do so when we convert this psymtab into a symtab. */
3881 if (pdi->name)
3882 add_partial_symbol (pdi, cu);
bc30ff58
JB
3883 }
3884 }
6e70227d 3885
bc30ff58
JB
3886 if (! pdi->has_children)
3887 return;
3888
3889 if (cu->language == language_ada)
3890 {
3891 pdi = pdi->die_child;
3892 while (pdi != NULL)
3893 {
3894 fixup_partial_die (pdi, cu);
3895 if (pdi->tag == DW_TAG_subprogram
3896 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3897 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3898 pdi = pdi->die_sibling;
3899 }
3900 }
3901}
3902
72bf9492
DJ
3903/* See if we can figure out if the class lives in a namespace. We do
3904 this by looking for a member function; its demangled name will
3905 contain namespace info, if there is any. */
63d06c5c 3906
72bf9492
DJ
3907static void
3908guess_structure_name (struct partial_die_info *struct_pdi,
3909 struct dwarf2_cu *cu)
63d06c5c 3910{
987504bb
JJ
3911 if ((cu->language == language_cplus
3912 || cu->language == language_java)
72bf9492 3913 && cu->has_namespace_info == 0
63d06c5c
DC
3914 && struct_pdi->has_children)
3915 {
63d06c5c
DC
3916 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3917 what template types look like, because the demangler
3918 frequently doesn't give the same name as the debug info. We
3919 could fix this by only using the demangled name to get the
134d01f1 3920 prefix (but see comment in read_structure_type). */
63d06c5c 3921
72bf9492 3922 struct partial_die_info *real_pdi;
5d51ca54 3923
72bf9492
DJ
3924 /* If this DIE (this DIE's specification, if any) has a parent, then
3925 we should not do this. We'll prepend the parent's fully qualified
3926 name when we create the partial symbol. */
5d51ca54 3927
72bf9492 3928 real_pdi = struct_pdi;
72bf9492 3929 while (real_pdi->has_specification)
10b3939b 3930 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3931
72bf9492
DJ
3932 if (real_pdi->die_parent != NULL)
3933 return;
63d06c5c 3934 }
63d06c5c
DC
3935}
3936
91c24f0a
DC
3937/* Read a partial die corresponding to an enumeration type. */
3938
72bf9492
DJ
3939static void
3940add_partial_enumeration (struct partial_die_info *enum_pdi,
3941 struct dwarf2_cu *cu)
91c24f0a 3942{
72bf9492 3943 struct partial_die_info *pdi;
91c24f0a
DC
3944
3945 if (enum_pdi->name != NULL)
72bf9492
DJ
3946 add_partial_symbol (enum_pdi, cu);
3947
3948 pdi = enum_pdi->die_child;
3949 while (pdi)
91c24f0a 3950 {
72bf9492 3951 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3952 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3953 else
72bf9492
DJ
3954 add_partial_symbol (pdi, cu);
3955 pdi = pdi->die_sibling;
91c24f0a 3956 }
91c24f0a
DC
3957}
3958
4bb7a0a7
DJ
3959/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3960 Return the corresponding abbrev, or NULL if the number is zero (indicating
3961 an empty DIE). In either case *BYTES_READ will be set to the length of
3962 the initial number. */
3963
3964static struct abbrev_info *
fe1b8b76 3965peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3966 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3967{
3968 bfd *abfd = cu->objfile->obfd;
3969 unsigned int abbrev_number;
3970 struct abbrev_info *abbrev;
3971
3972 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3973
3974 if (abbrev_number == 0)
3975 return NULL;
3976
3977 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3978 if (!abbrev)
3979 {
8a3fe4f8 3980 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3981 bfd_get_filename (abfd));
3982 }
3983
3984 return abbrev;
3985}
3986
93311388
DE
3987/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3988 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3989 DIE. Any children of the skipped DIEs will also be skipped. */
3990
fe1b8b76 3991static gdb_byte *
93311388 3992skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3993{
3994 struct abbrev_info *abbrev;
3995 unsigned int bytes_read;
3996
3997 while (1)
3998 {
3999 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4000 if (abbrev == NULL)
4001 return info_ptr + bytes_read;
4002 else
93311388 4003 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4004 }
4005}
4006
93311388
DE
4007/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4008 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4009 abbrev corresponding to that skipped uleb128 should be passed in
4010 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4011 children. */
4012
fe1b8b76 4013static gdb_byte *
93311388
DE
4014skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4015 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4016{
4017 unsigned int bytes_read;
4018 struct attribute attr;
4019 bfd *abfd = cu->objfile->obfd;
4020 unsigned int form, i;
4021
4022 for (i = 0; i < abbrev->num_attrs; i++)
4023 {
4024 /* The only abbrev we care about is DW_AT_sibling. */
4025 if (abbrev->attrs[i].name == DW_AT_sibling)
4026 {
4027 read_attribute (&attr, &abbrev->attrs[i],
4028 abfd, info_ptr, cu);
4029 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4030 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4031 else
93311388 4032 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4033 }
4034
4035 /* If it isn't DW_AT_sibling, skip this attribute. */
4036 form = abbrev->attrs[i].form;
4037 skip_attribute:
4038 switch (form)
4039 {
4bb7a0a7 4040 case DW_FORM_ref_addr:
ae411497
TT
4041 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4042 and later it is offset sized. */
4043 if (cu->header.version == 2)
4044 info_ptr += cu->header.addr_size;
4045 else
4046 info_ptr += cu->header.offset_size;
4047 break;
4048 case DW_FORM_addr:
4bb7a0a7
DJ
4049 info_ptr += cu->header.addr_size;
4050 break;
4051 case DW_FORM_data1:
4052 case DW_FORM_ref1:
4053 case DW_FORM_flag:
4054 info_ptr += 1;
4055 break;
2dc7f7b3
TT
4056 case DW_FORM_flag_present:
4057 break;
4bb7a0a7
DJ
4058 case DW_FORM_data2:
4059 case DW_FORM_ref2:
4060 info_ptr += 2;
4061 break;
4062 case DW_FORM_data4:
4063 case DW_FORM_ref4:
4064 info_ptr += 4;
4065 break;
4066 case DW_FORM_data8:
4067 case DW_FORM_ref8:
348e048f 4068 case DW_FORM_sig8:
4bb7a0a7
DJ
4069 info_ptr += 8;
4070 break;
4071 case DW_FORM_string:
9b1c24c8 4072 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4073 info_ptr += bytes_read;
4074 break;
2dc7f7b3 4075 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4076 case DW_FORM_strp:
4077 info_ptr += cu->header.offset_size;
4078 break;
2dc7f7b3 4079 case DW_FORM_exprloc:
4bb7a0a7
DJ
4080 case DW_FORM_block:
4081 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4082 info_ptr += bytes_read;
4083 break;
4084 case DW_FORM_block1:
4085 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4086 break;
4087 case DW_FORM_block2:
4088 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4089 break;
4090 case DW_FORM_block4:
4091 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4092 break;
4093 case DW_FORM_sdata:
4094 case DW_FORM_udata:
4095 case DW_FORM_ref_udata:
4096 info_ptr = skip_leb128 (abfd, info_ptr);
4097 break;
4098 case DW_FORM_indirect:
4099 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4100 info_ptr += bytes_read;
4101 /* We need to continue parsing from here, so just go back to
4102 the top. */
4103 goto skip_attribute;
4104
4105 default:
8a3fe4f8 4106 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4107 dwarf_form_name (form),
4108 bfd_get_filename (abfd));
4109 }
4110 }
4111
4112 if (abbrev->has_children)
93311388 4113 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4114 else
4115 return info_ptr;
4116}
4117
93311388
DE
4118/* Locate ORIG_PDI's sibling.
4119 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4120 in BUFFER. */
91c24f0a 4121
fe1b8b76 4122static gdb_byte *
93311388
DE
4123locate_pdi_sibling (struct partial_die_info *orig_pdi,
4124 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4125 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4126{
4127 /* Do we know the sibling already? */
72bf9492 4128
91c24f0a
DC
4129 if (orig_pdi->sibling)
4130 return orig_pdi->sibling;
4131
4132 /* Are there any children to deal with? */
4133
4134 if (!orig_pdi->has_children)
4135 return info_ptr;
4136
4bb7a0a7 4137 /* Skip the children the long way. */
91c24f0a 4138
93311388 4139 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4140}
4141
c906108c
SS
4142/* Expand this partial symbol table into a full symbol table. */
4143
4144static void
fba45db2 4145dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4146{
c906108c
SS
4147 if (pst != NULL)
4148 {
4149 if (pst->readin)
4150 {
8a3fe4f8 4151 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4152 }
4153 else
4154 {
4155 if (info_verbose)
4156 {
a3f17187 4157 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4158 gdb_flush (gdb_stdout);
4159 }
4160
10b3939b
DJ
4161 /* Restore our global data. */
4162 dwarf2_per_objfile = objfile_data (pst->objfile,
4163 dwarf2_objfile_data_key);
4164
b2ab525c
KB
4165 /* If this psymtab is constructed from a debug-only objfile, the
4166 has_section_at_zero flag will not necessarily be correct. We
4167 can get the correct value for this flag by looking at the data
4168 associated with the (presumably stripped) associated objfile. */
4169 if (pst->objfile->separate_debug_objfile_backlink)
4170 {
4171 struct dwarf2_per_objfile *dpo_backlink
4172 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4173 dwarf2_objfile_data_key);
9a619af0 4174
b2ab525c
KB
4175 dwarf2_per_objfile->has_section_at_zero
4176 = dpo_backlink->has_section_at_zero;
4177 }
4178
98bfdba5
PA
4179 dwarf2_per_objfile->reading_partial_symbols = 0;
4180
c906108c
SS
4181 psymtab_to_symtab_1 (pst);
4182
4183 /* Finish up the debug error message. */
4184 if (info_verbose)
a3f17187 4185 printf_filtered (_("done.\n"));
c906108c
SS
4186 }
4187 }
4188}
4189
10b3939b
DJ
4190/* Add PER_CU to the queue. */
4191
4192static void
03dd20cc 4193queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4194{
4195 struct dwarf2_queue_item *item;
4196
4197 per_cu->queued = 1;
4198 item = xmalloc (sizeof (*item));
4199 item->per_cu = per_cu;
4200 item->next = NULL;
4201
4202 if (dwarf2_queue == NULL)
4203 dwarf2_queue = item;
4204 else
4205 dwarf2_queue_tail->next = item;
4206
4207 dwarf2_queue_tail = item;
4208}
4209
4210/* Process the queue. */
4211
4212static void
4213process_queue (struct objfile *objfile)
4214{
4215 struct dwarf2_queue_item *item, *next_item;
4216
03dd20cc
DJ
4217 /* The queue starts out with one item, but following a DIE reference
4218 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4219 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4220 {
9291a0cd
TT
4221 if (dwarf2_per_objfile->using_index
4222 ? !item->per_cu->v.quick->symtab
4223 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4224 process_full_comp_unit (item->per_cu);
4225
4226 item->per_cu->queued = 0;
4227 next_item = item->next;
4228 xfree (item);
4229 }
4230
4231 dwarf2_queue_tail = NULL;
4232}
4233
4234/* Free all allocated queue entries. This function only releases anything if
4235 an error was thrown; if the queue was processed then it would have been
4236 freed as we went along. */
4237
4238static void
4239dwarf2_release_queue (void *dummy)
4240{
4241 struct dwarf2_queue_item *item, *last;
4242
4243 item = dwarf2_queue;
4244 while (item)
4245 {
4246 /* Anything still marked queued is likely to be in an
4247 inconsistent state, so discard it. */
4248 if (item->per_cu->queued)
4249 {
4250 if (item->per_cu->cu != NULL)
4251 free_one_cached_comp_unit (item->per_cu->cu);
4252 item->per_cu->queued = 0;
4253 }
4254
4255 last = item;
4256 item = item->next;
4257 xfree (last);
4258 }
4259
4260 dwarf2_queue = dwarf2_queue_tail = NULL;
4261}
4262
4263/* Read in full symbols for PST, and anything it depends on. */
4264
c906108c 4265static void
fba45db2 4266psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4267{
10b3939b 4268 struct dwarf2_per_cu_data *per_cu;
c906108c 4269 struct cleanup *back_to;
aaa75496
JB
4270 int i;
4271
4272 for (i = 0; i < pst->number_of_dependencies; i++)
4273 if (!pst->dependencies[i]->readin)
4274 {
4275 /* Inform about additional files that need to be read in. */
4276 if (info_verbose)
4277 {
a3f17187 4278 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4279 fputs_filtered (" ", gdb_stdout);
4280 wrap_here ("");
4281 fputs_filtered ("and ", gdb_stdout);
4282 wrap_here ("");
4283 printf_filtered ("%s...", pst->dependencies[i]->filename);
4284 wrap_here (""); /* Flush output */
4285 gdb_flush (gdb_stdout);
4286 }
4287 psymtab_to_symtab_1 (pst->dependencies[i]);
4288 }
4289
e38df1d0 4290 per_cu = pst->read_symtab_private;
10b3939b
DJ
4291
4292 if (per_cu == NULL)
aaa75496
JB
4293 {
4294 /* It's an include file, no symbols to read for it.
4295 Everything is in the parent symtab. */
4296 pst->readin = 1;
4297 return;
4298 }
c906108c 4299
9291a0cd 4300 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4301}
4302
93311388 4303/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4304
93311388 4305static void
31ffec48 4306load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4307{
31ffec48 4308 bfd *abfd = objfile->obfd;
10b3939b 4309 struct dwarf2_cu *cu;
c764a876 4310 unsigned int offset;
93311388 4311 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4312 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4313 struct attribute *attr;
98bfdba5 4314 int read_cu = 0;
6502dd73 4315
348e048f
DE
4316 gdb_assert (! per_cu->from_debug_types);
4317
c906108c 4318 /* Set local variables from the partial symbol table info. */
10b3939b 4319 offset = per_cu->offset;
6502dd73 4320
be391dca 4321 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4322 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4323 beg_of_comp_unit = info_ptr;
63d06c5c 4324
98bfdba5
PA
4325 if (per_cu->cu == NULL)
4326 {
4327 cu = alloc_one_comp_unit (objfile);
4328
4329 read_cu = 1;
c906108c 4330
98bfdba5
PA
4331 /* If an error occurs while loading, release our storage. */
4332 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4333
98bfdba5
PA
4334 /* Read in the comp_unit header. */
4335 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4336
98bfdba5
PA
4337 /* Complete the cu_header. */
4338 cu->header.offset = offset;
4339 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4340
98bfdba5
PA
4341 /* Read the abbrevs for this compilation unit. */
4342 dwarf2_read_abbrevs (abfd, cu);
4343 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4344
98bfdba5
PA
4345 /* Link this compilation unit into the compilation unit tree. */
4346 per_cu->cu = cu;
4347 cu->per_cu = per_cu;
98bfdba5
PA
4348
4349 /* Link this CU into read_in_chain. */
4350 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4351 dwarf2_per_objfile->read_in_chain = per_cu;
4352 }
4353 else
4354 {
4355 cu = per_cu->cu;
4356 info_ptr += cu->header.first_die_offset;
4357 }
e142c38c 4358
93311388 4359 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4360
4361 /* We try not to read any attributes in this function, because not
4362 all objfiles needed for references have been loaded yet, and symbol
4363 table processing isn't initialized. But we have to set the CU language,
4364 or we won't be able to build types correctly. */
4365 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4366 if (attr)
4367 set_cu_language (DW_UNSND (attr), cu);
4368 else
4369 set_cu_language (language_minimal, cu);
4370
a6c727b2
DJ
4371 /* Similarly, if we do not read the producer, we can not apply
4372 producer-specific interpretation. */
4373 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4374 if (attr)
4375 cu->producer = DW_STRING (attr);
4376
98bfdba5
PA
4377 if (read_cu)
4378 {
4379 do_cleanups (free_abbrevs_cleanup);
e142c38c 4380
98bfdba5
PA
4381 /* We've successfully allocated this compilation unit. Let our
4382 caller clean it up when finished with it. */
4383 discard_cleanups (free_cu_cleanup);
4384 }
10b3939b
DJ
4385}
4386
3da10d80
KS
4387/* Add a DIE to the delayed physname list. */
4388
4389static void
4390add_to_method_list (struct type *type, int fnfield_index, int index,
4391 const char *name, struct die_info *die,
4392 struct dwarf2_cu *cu)
4393{
4394 struct delayed_method_info mi;
4395 mi.type = type;
4396 mi.fnfield_index = fnfield_index;
4397 mi.index = index;
4398 mi.name = name;
4399 mi.die = die;
4400 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4401}
4402
4403/* A cleanup for freeing the delayed method list. */
4404
4405static void
4406free_delayed_list (void *ptr)
4407{
4408 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4409 if (cu->method_list != NULL)
4410 {
4411 VEC_free (delayed_method_info, cu->method_list);
4412 cu->method_list = NULL;
4413 }
4414}
4415
4416/* Compute the physnames of any methods on the CU's method list.
4417
4418 The computation of method physnames is delayed in order to avoid the
4419 (bad) condition that one of the method's formal parameters is of an as yet
4420 incomplete type. */
4421
4422static void
4423compute_delayed_physnames (struct dwarf2_cu *cu)
4424{
4425 int i;
4426 struct delayed_method_info *mi;
4427 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4428 {
4429 char *physname;
4430 struct fn_fieldlist *fn_flp
4431 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4432 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4433 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4434 }
4435}
4436
10b3939b
DJ
4437/* Generate full symbol information for PST and CU, whose DIEs have
4438 already been loaded into memory. */
4439
4440static void
4441process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4442{
10b3939b 4443 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4444 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4445 CORE_ADDR lowpc, highpc;
4446 struct symtab *symtab;
3da10d80 4447 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4448 CORE_ADDR baseaddr;
4449
4450 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4451
10b3939b
DJ
4452 buildsym_init ();
4453 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4454 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4455
4456 cu->list_in_scope = &file_symbols;
c906108c 4457
d85a05f0 4458 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4459
c906108c 4460 /* Do line number decoding in read_file_scope () */
10b3939b 4461 process_die (cu->dies, cu);
c906108c 4462
3da10d80
KS
4463 /* Now that we have processed all the DIEs in the CU, all the types
4464 should be complete, and it should now be safe to compute all of the
4465 physnames. */
4466 compute_delayed_physnames (cu);
4467 do_cleanups (delayed_list_cleanup);
4468
fae299cd
DC
4469 /* Some compilers don't define a DW_AT_high_pc attribute for the
4470 compilation unit. If the DW_AT_high_pc is missing, synthesize
4471 it, by scanning the DIE's below the compilation unit. */
10b3939b 4472 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4473
613e1657 4474 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4475
4476 /* Set symtab language to language from DW_AT_language.
4477 If the compilation is from a C file generated by language preprocessors,
4478 do not set the language if it was already deduced by start_subfile. */
4479 if (symtab != NULL
10b3939b 4480 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4481 {
10b3939b 4482 symtab->language = cu->language;
c906108c 4483 }
9291a0cd
TT
4484
4485 if (dwarf2_per_objfile->using_index)
4486 per_cu->v.quick->symtab = symtab;
4487 else
4488 {
4489 struct partial_symtab *pst = per_cu->v.psymtab;
4490 pst->symtab = symtab;
4491 pst->readin = 1;
4492 }
c906108c
SS
4493
4494 do_cleanups (back_to);
4495}
4496
4497/* Process a die and its children. */
4498
4499static void
e7c27a73 4500process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4501{
4502 switch (die->tag)
4503 {
4504 case DW_TAG_padding:
4505 break;
4506 case DW_TAG_compile_unit:
e7c27a73 4507 read_file_scope (die, cu);
c906108c 4508 break;
348e048f
DE
4509 case DW_TAG_type_unit:
4510 read_type_unit_scope (die, cu);
4511 break;
c906108c 4512 case DW_TAG_subprogram:
c906108c 4513 case DW_TAG_inlined_subroutine:
edb3359d 4514 read_func_scope (die, cu);
c906108c
SS
4515 break;
4516 case DW_TAG_lexical_block:
14898363
L
4517 case DW_TAG_try_block:
4518 case DW_TAG_catch_block:
e7c27a73 4519 read_lexical_block_scope (die, cu);
c906108c
SS
4520 break;
4521 case DW_TAG_class_type:
680b30c7 4522 case DW_TAG_interface_type:
c906108c
SS
4523 case DW_TAG_structure_type:
4524 case DW_TAG_union_type:
134d01f1 4525 process_structure_scope (die, cu);
c906108c
SS
4526 break;
4527 case DW_TAG_enumeration_type:
134d01f1 4528 process_enumeration_scope (die, cu);
c906108c 4529 break;
134d01f1 4530
f792889a
DJ
4531 /* These dies have a type, but processing them does not create
4532 a symbol or recurse to process the children. Therefore we can
4533 read them on-demand through read_type_die. */
c906108c 4534 case DW_TAG_subroutine_type:
72019c9c 4535 case DW_TAG_set_type:
c906108c 4536 case DW_TAG_array_type:
c906108c 4537 case DW_TAG_pointer_type:
c906108c 4538 case DW_TAG_ptr_to_member_type:
c906108c 4539 case DW_TAG_reference_type:
c906108c 4540 case DW_TAG_string_type:
c906108c 4541 break;
134d01f1 4542
c906108c 4543 case DW_TAG_base_type:
a02abb62 4544 case DW_TAG_subrange_type:
cb249c71 4545 case DW_TAG_typedef:
134d01f1
DJ
4546 /* Add a typedef symbol for the type definition, if it has a
4547 DW_AT_name. */
f792889a 4548 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4549 break;
c906108c 4550 case DW_TAG_common_block:
e7c27a73 4551 read_common_block (die, cu);
c906108c
SS
4552 break;
4553 case DW_TAG_common_inclusion:
4554 break;
d9fa45fe 4555 case DW_TAG_namespace:
63d06c5c 4556 processing_has_namespace_info = 1;
e7c27a73 4557 read_namespace (die, cu);
d9fa45fe 4558 break;
5d7cb8df 4559 case DW_TAG_module:
f55ee35c 4560 processing_has_namespace_info = 1;
5d7cb8df
JK
4561 read_module (die, cu);
4562 break;
d9fa45fe
DC
4563 case DW_TAG_imported_declaration:
4564 case DW_TAG_imported_module:
63d06c5c 4565 processing_has_namespace_info = 1;
27aa8d6a
SW
4566 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4567 || cu->language != language_fortran))
4568 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4569 dwarf_tag_name (die->tag));
4570 read_import_statement (die, cu);
d9fa45fe 4571 break;
c906108c 4572 default:
e7c27a73 4573 new_symbol (die, NULL, cu);
c906108c
SS
4574 break;
4575 }
4576}
4577
94af9270
KS
4578/* A helper function for dwarf2_compute_name which determines whether DIE
4579 needs to have the name of the scope prepended to the name listed in the
4580 die. */
4581
4582static int
4583die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4584{
1c809c68
TT
4585 struct attribute *attr;
4586
94af9270
KS
4587 switch (die->tag)
4588 {
4589 case DW_TAG_namespace:
4590 case DW_TAG_typedef:
4591 case DW_TAG_class_type:
4592 case DW_TAG_interface_type:
4593 case DW_TAG_structure_type:
4594 case DW_TAG_union_type:
4595 case DW_TAG_enumeration_type:
4596 case DW_TAG_enumerator:
4597 case DW_TAG_subprogram:
4598 case DW_TAG_member:
4599 return 1;
4600
4601 case DW_TAG_variable:
4602 /* We only need to prefix "globally" visible variables. These include
4603 any variable marked with DW_AT_external or any variable that
4604 lives in a namespace. [Variables in anonymous namespaces
4605 require prefixing, but they are not DW_AT_external.] */
4606
4607 if (dwarf2_attr (die, DW_AT_specification, cu))
4608 {
4609 struct dwarf2_cu *spec_cu = cu;
9a619af0 4610
94af9270
KS
4611 return die_needs_namespace (die_specification (die, &spec_cu),
4612 spec_cu);
4613 }
4614
1c809c68 4615 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4616 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4617 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4618 return 0;
4619 /* A variable in a lexical block of some kind does not need a
4620 namespace, even though in C++ such variables may be external
4621 and have a mangled name. */
4622 if (die->parent->tag == DW_TAG_lexical_block
4623 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4624 || die->parent->tag == DW_TAG_catch_block
4625 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4626 return 0;
4627 return 1;
94af9270
KS
4628
4629 default:
4630 return 0;
4631 }
4632}
4633
98bfdba5
PA
4634/* Retrieve the last character from a mem_file. */
4635
4636static void
4637do_ui_file_peek_last (void *object, const char *buffer, long length)
4638{
4639 char *last_char_p = (char *) object;
4640
4641 if (length > 0)
4642 *last_char_p = buffer[length - 1];
4643}
4644
94af9270
KS
4645/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4646 compute the physname for the object, which include a method's
4647 formal parameters (C++/Java) and return type (Java).
4648
af6b7be1
JB
4649 For Ada, return the DIE's linkage name rather than the fully qualified
4650 name. PHYSNAME is ignored..
4651
94af9270
KS
4652 The result is allocated on the objfile_obstack and canonicalized. */
4653
4654static const char *
4655dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4656 int physname)
4657{
4658 if (name == NULL)
4659 name = dwarf2_name (die, cu);
4660
f55ee35c
JK
4661 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4662 compute it by typename_concat inside GDB. */
4663 if (cu->language == language_ada
4664 || (cu->language == language_fortran && physname))
4665 {
4666 /* For Ada unit, we prefer the linkage name over the name, as
4667 the former contains the exported name, which the user expects
4668 to be able to reference. Ideally, we want the user to be able
4669 to reference this entity using either natural or linkage name,
4670 but we haven't started looking at this enhancement yet. */
4671 struct attribute *attr;
4672
4673 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4674 if (attr == NULL)
4675 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4676 if (attr && DW_STRING (attr))
4677 return DW_STRING (attr);
4678 }
4679
94af9270
KS
4680 /* These are the only languages we know how to qualify names in. */
4681 if (name != NULL
f55ee35c
JK
4682 && (cu->language == language_cplus || cu->language == language_java
4683 || cu->language == language_fortran))
94af9270
KS
4684 {
4685 if (die_needs_namespace (die, cu))
4686 {
4687 long length;
4688 char *prefix;
4689 struct ui_file *buf;
4690
4691 prefix = determine_prefix (die, cu);
4692 buf = mem_fileopen ();
4693 if (*prefix != '\0')
4694 {
f55ee35c
JK
4695 char *prefixed_name = typename_concat (NULL, prefix, name,
4696 physname, cu);
9a619af0 4697
94af9270
KS
4698 fputs_unfiltered (prefixed_name, buf);
4699 xfree (prefixed_name);
4700 }
4701 else
4702 fputs_unfiltered (name ? name : "", buf);
4703
98bfdba5
PA
4704 /* Template parameters may be specified in the DIE's DW_AT_name, or
4705 as children with DW_TAG_template_type_param or
4706 DW_TAG_value_type_param. If the latter, add them to the name
4707 here. If the name already has template parameters, then
4708 skip this step; some versions of GCC emit both, and
4709 it is more efficient to use the pre-computed name.
4710
4711 Something to keep in mind about this process: it is very
4712 unlikely, or in some cases downright impossible, to produce
4713 something that will match the mangled name of a function.
4714 If the definition of the function has the same debug info,
4715 we should be able to match up with it anyway. But fallbacks
4716 using the minimal symbol, for instance to find a method
4717 implemented in a stripped copy of libstdc++, will not work.
4718 If we do not have debug info for the definition, we will have to
4719 match them up some other way.
4720
4721 When we do name matching there is a related problem with function
4722 templates; two instantiated function templates are allowed to
4723 differ only by their return types, which we do not add here. */
4724
4725 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4726 {
4727 struct attribute *attr;
4728 struct die_info *child;
4729 int first = 1;
4730
4731 die->building_fullname = 1;
4732
4733 for (child = die->child; child != NULL; child = child->sibling)
4734 {
4735 struct type *type;
4736 long value;
4737 gdb_byte *bytes;
4738 struct dwarf2_locexpr_baton *baton;
4739 struct value *v;
4740
4741 if (child->tag != DW_TAG_template_type_param
4742 && child->tag != DW_TAG_template_value_param)
4743 continue;
4744
4745 if (first)
4746 {
4747 fputs_unfiltered ("<", buf);
4748 first = 0;
4749 }
4750 else
4751 fputs_unfiltered (", ", buf);
4752
4753 attr = dwarf2_attr (child, DW_AT_type, cu);
4754 if (attr == NULL)
4755 {
4756 complaint (&symfile_complaints,
4757 _("template parameter missing DW_AT_type"));
4758 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4759 continue;
4760 }
4761 type = die_type (child, cu);
4762
4763 if (child->tag == DW_TAG_template_type_param)
4764 {
4765 c_print_type (type, "", buf, -1, 0);
4766 continue;
4767 }
4768
4769 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4770 if (attr == NULL)
4771 {
4772 complaint (&symfile_complaints,
4773 _("template parameter missing DW_AT_const_value"));
4774 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4775 continue;
4776 }
4777
4778 dwarf2_const_value_attr (attr, type, name,
4779 &cu->comp_unit_obstack, cu,
4780 &value, &bytes, &baton);
4781
4782 if (TYPE_NOSIGN (type))
4783 /* GDB prints characters as NUMBER 'CHAR'. If that's
4784 changed, this can use value_print instead. */
4785 c_printchar (value, type, buf);
4786 else
4787 {
4788 struct value_print_options opts;
4789
4790 if (baton != NULL)
4791 v = dwarf2_evaluate_loc_desc (type, NULL,
4792 baton->data,
4793 baton->size,
4794 baton->per_cu);
4795 else if (bytes != NULL)
4796 {
4797 v = allocate_value (type);
4798 memcpy (value_contents_writeable (v), bytes,
4799 TYPE_LENGTH (type));
4800 }
4801 else
4802 v = value_from_longest (type, value);
4803
4804 /* Specify decimal so that we do not depend on the radix. */
4805 get_formatted_print_options (&opts, 'd');
4806 opts.raw = 1;
4807 value_print (v, buf, &opts);
4808 release_value (v);
4809 value_free (v);
4810 }
4811 }
4812
4813 die->building_fullname = 0;
4814
4815 if (!first)
4816 {
4817 /* Close the argument list, with a space if necessary
4818 (nested templates). */
4819 char last_char = '\0';
4820 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4821 if (last_char == '>')
4822 fputs_unfiltered (" >", buf);
4823 else
4824 fputs_unfiltered (">", buf);
4825 }
4826 }
4827
94af9270
KS
4828 /* For Java and C++ methods, append formal parameter type
4829 information, if PHYSNAME. */
6e70227d 4830
94af9270
KS
4831 if (physname && die->tag == DW_TAG_subprogram
4832 && (cu->language == language_cplus
4833 || cu->language == language_java))
4834 {
4835 struct type *type = read_type_die (die, cu);
4836
4837 c_type_print_args (type, buf, 0, cu->language);
4838
4839 if (cu->language == language_java)
4840 {
4841 /* For java, we must append the return type to method
4842 names. */
4843 if (die->tag == DW_TAG_subprogram)
4844 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4845 0, 0);
4846 }
4847 else if (cu->language == language_cplus)
4848 {
60430eff
DJ
4849 /* Assume that an artificial first parameter is
4850 "this", but do not crash if it is not. RealView
4851 marks unnamed (and thus unused) parameters as
4852 artificial; there is no way to differentiate
4853 the two cases. */
94af9270
KS
4854 if (TYPE_NFIELDS (type) > 0
4855 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4856 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4857 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4858 fputs_unfiltered (" const", buf);
4859 }
4860 }
4861
4862 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4863 &length);
4864 ui_file_delete (buf);
4865
4866 if (cu->language == language_cplus)
4867 {
4868 char *cname
4869 = dwarf2_canonicalize_name (name, cu,
4870 &cu->objfile->objfile_obstack);
9a619af0 4871
94af9270
KS
4872 if (cname != NULL)
4873 name = cname;
4874 }
4875 }
4876 }
4877
4878 return name;
4879}
4880
0114d602
DJ
4881/* Return the fully qualified name of DIE, based on its DW_AT_name.
4882 If scope qualifiers are appropriate they will be added. The result
4883 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4884 not have a name. NAME may either be from a previous call to
4885 dwarf2_name or NULL.
4886
4887 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4888
4889static const char *
94af9270 4890dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4891{
94af9270
KS
4892 return dwarf2_compute_name (name, die, cu, 0);
4893}
0114d602 4894
94af9270
KS
4895/* Construct a physname for the given DIE in CU. NAME may either be
4896 from a previous call to dwarf2_name or NULL. The result will be
4897 allocated on the objfile_objstack or NULL if the DIE does not have a
4898 name.
0114d602 4899
94af9270 4900 The output string will be canonicalized (if C++/Java). */
0114d602 4901
94af9270
KS
4902static const char *
4903dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4904{
4905 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4906}
4907
27aa8d6a
SW
4908/* Read the import statement specified by the given die and record it. */
4909
4910static void
4911read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4912{
4913 struct attribute *import_attr;
4914 struct die_info *imported_die;
de4affc9 4915 struct dwarf2_cu *imported_cu;
27aa8d6a 4916 const char *imported_name;
794684b6 4917 const char *imported_name_prefix;
13387711
SW
4918 const char *canonical_name;
4919 const char *import_alias;
4920 const char *imported_declaration = NULL;
794684b6 4921 const char *import_prefix;
13387711
SW
4922
4923 char *temp;
27aa8d6a
SW
4924
4925 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4926 if (import_attr == NULL)
4927 {
4928 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4929 dwarf_tag_name (die->tag));
4930 return;
4931 }
4932
de4affc9
CC
4933 imported_cu = cu;
4934 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4935 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4936 if (imported_name == NULL)
4937 {
4938 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4939
4940 The import in the following code:
4941 namespace A
4942 {
4943 typedef int B;
4944 }
4945
4946 int main ()
4947 {
4948 using A::B;
4949 B b;
4950 return b;
4951 }
4952
4953 ...
4954 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4955 <52> DW_AT_decl_file : 1
4956 <53> DW_AT_decl_line : 6
4957 <54> DW_AT_import : <0x75>
4958 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4959 <59> DW_AT_name : B
4960 <5b> DW_AT_decl_file : 1
4961 <5c> DW_AT_decl_line : 2
4962 <5d> DW_AT_type : <0x6e>
4963 ...
4964 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4965 <76> DW_AT_byte_size : 4
4966 <77> DW_AT_encoding : 5 (signed)
4967
4968 imports the wrong die ( 0x75 instead of 0x58 ).
4969 This case will be ignored until the gcc bug is fixed. */
4970 return;
4971 }
4972
82856980
SW
4973 /* Figure out the local name after import. */
4974 import_alias = dwarf2_name (die, cu);
27aa8d6a 4975
794684b6
SW
4976 /* Figure out where the statement is being imported to. */
4977 import_prefix = determine_prefix (die, cu);
4978
4979 /* Figure out what the scope of the imported die is and prepend it
4980 to the name of the imported die. */
de4affc9 4981 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4982
f55ee35c
JK
4983 if (imported_die->tag != DW_TAG_namespace
4984 && imported_die->tag != DW_TAG_module)
794684b6 4985 {
13387711
SW
4986 imported_declaration = imported_name;
4987 canonical_name = imported_name_prefix;
794684b6 4988 }
13387711 4989 else if (strlen (imported_name_prefix) > 0)
794684b6 4990 {
13387711
SW
4991 temp = alloca (strlen (imported_name_prefix)
4992 + 2 + strlen (imported_name) + 1);
4993 strcpy (temp, imported_name_prefix);
4994 strcat (temp, "::");
4995 strcat (temp, imported_name);
4996 canonical_name = temp;
794684b6 4997 }
13387711
SW
4998 else
4999 canonical_name = imported_name;
794684b6 5000
c0cc3a76
SW
5001 cp_add_using_directive (import_prefix,
5002 canonical_name,
5003 import_alias,
13387711 5004 imported_declaration,
c0cc3a76 5005 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5006}
5007
5fb290d7 5008static void
e142c38c 5009initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5010{
e142c38c 5011 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5012}
5013
cb1df416
DJ
5014static void
5015free_cu_line_header (void *arg)
5016{
5017 struct dwarf2_cu *cu = arg;
5018
5019 free_line_header (cu->line_header);
5020 cu->line_header = NULL;
5021}
5022
9291a0cd
TT
5023static void
5024find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5025 char **name, char **comp_dir)
5026{
5027 struct attribute *attr;
5028
5029 *name = NULL;
5030 *comp_dir = NULL;
5031
5032 /* Find the filename. Do not use dwarf2_name here, since the filename
5033 is not a source language identifier. */
5034 attr = dwarf2_attr (die, DW_AT_name, cu);
5035 if (attr)
5036 {
5037 *name = DW_STRING (attr);
5038 }
5039
5040 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5041 if (attr)
5042 *comp_dir = DW_STRING (attr);
5043 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5044 {
5045 *comp_dir = ldirname (*name);
5046 if (*comp_dir != NULL)
5047 make_cleanup (xfree, *comp_dir);
5048 }
5049 if (*comp_dir != NULL)
5050 {
5051 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5052 directory, get rid of it. */
5053 char *cp = strchr (*comp_dir, ':');
5054
5055 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5056 *comp_dir = cp + 1;
5057 }
5058
5059 if (*name == NULL)
5060 *name = "<unknown>";
5061}
5062
c906108c 5063static void
e7c27a73 5064read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5065{
e7c27a73 5066 struct objfile *objfile = cu->objfile;
debd256d 5067 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5068 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5069 CORE_ADDR highpc = ((CORE_ADDR) 0);
5070 struct attribute *attr;
e1024ff1 5071 char *name = NULL;
c906108c
SS
5072 char *comp_dir = NULL;
5073 struct die_info *child_die;
5074 bfd *abfd = objfile->obfd;
debd256d 5075 struct line_header *line_header = 0;
e142c38c 5076 CORE_ADDR baseaddr;
6e70227d 5077
e142c38c 5078 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5079
fae299cd 5080 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5081
5082 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5083 from finish_block. */
2acceee2 5084 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5085 lowpc = highpc;
5086 lowpc += baseaddr;
5087 highpc += baseaddr;
5088
9291a0cd 5089 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5090
e142c38c 5091 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5092 if (attr)
5093 {
e142c38c 5094 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5095 }
5096
b0f35d58 5097 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5098 if (attr)
b0f35d58 5099 cu->producer = DW_STRING (attr);
303b6f5d 5100
c906108c
SS
5101 /* We assume that we're processing GCC output. */
5102 processing_gcc_compilation = 2;
c906108c 5103
df8a16a1
DJ
5104 processing_has_namespace_info = 0;
5105
c906108c
SS
5106 start_symtab (name, comp_dir, lowpc);
5107 record_debugformat ("DWARF 2");
303b6f5d 5108 record_producer (cu->producer);
c906108c 5109
e142c38c 5110 initialize_cu_func_list (cu);
c906108c 5111
cb1df416
DJ
5112 /* Decode line number information if present. We do this before
5113 processing child DIEs, so that the line header table is available
5114 for DW_AT_decl_file. */
e142c38c 5115 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5116 if (attr)
5117 {
debd256d 5118 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5119 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5120 if (line_header)
5121 {
cb1df416
DJ
5122 cu->line_header = line_header;
5123 make_cleanup (free_cu_line_header, cu);
aaa75496 5124 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5125 }
5fb290d7 5126 }
debd256d 5127
cb1df416
DJ
5128 /* Process all dies in compilation unit. */
5129 if (die->child != NULL)
5130 {
5131 child_die = die->child;
5132 while (child_die && child_die->tag)
5133 {
5134 process_die (child_die, cu);
5135 child_die = sibling_die (child_die);
5136 }
5137 }
5138
2e276125
JB
5139 /* Decode macro information, if present. Dwarf 2 macro information
5140 refers to information in the line number info statement program
5141 header, so we can only read it if we've read the header
5142 successfully. */
e142c38c 5143 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5144 if (attr && line_header)
2e276125
JB
5145 {
5146 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5147
2e276125 5148 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5149 comp_dir, abfd, cu);
2e276125 5150 }
debd256d 5151 do_cleanups (back_to);
5fb290d7
DJ
5152}
5153
348e048f
DE
5154/* For TUs we want to skip the first top level sibling if it's not the
5155 actual type being defined by this TU. In this case the first top
5156 level sibling is there to provide context only. */
5157
5158static void
5159read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5160{
5161 struct objfile *objfile = cu->objfile;
5162 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5163 CORE_ADDR lowpc;
5164 struct attribute *attr;
5165 char *name = NULL;
5166 char *comp_dir = NULL;
5167 struct die_info *child_die;
5168 bfd *abfd = objfile->obfd;
348e048f
DE
5169
5170 /* start_symtab needs a low pc, but we don't really have one.
5171 Do what read_file_scope would do in the absence of such info. */
5172 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5173
5174 /* Find the filename. Do not use dwarf2_name here, since the filename
5175 is not a source language identifier. */
5176 attr = dwarf2_attr (die, DW_AT_name, cu);
5177 if (attr)
5178 name = DW_STRING (attr);
5179
5180 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5181 if (attr)
5182 comp_dir = DW_STRING (attr);
5183 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5184 {
5185 comp_dir = ldirname (name);
5186 if (comp_dir != NULL)
5187 make_cleanup (xfree, comp_dir);
5188 }
5189
5190 if (name == NULL)
5191 name = "<unknown>";
5192
5193 attr = dwarf2_attr (die, DW_AT_language, cu);
5194 if (attr)
5195 set_cu_language (DW_UNSND (attr), cu);
5196
5197 /* This isn't technically needed today. It is done for symmetry
5198 with read_file_scope. */
5199 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5200 if (attr)
348e048f
DE
5201 cu->producer = DW_STRING (attr);
5202
5203 /* We assume that we're processing GCC output. */
5204 processing_gcc_compilation = 2;
5205
5206 processing_has_namespace_info = 0;
5207
5208 start_symtab (name, comp_dir, lowpc);
5209 record_debugformat ("DWARF 2");
5210 record_producer (cu->producer);
5211
5212 /* Process the dies in the type unit. */
5213 if (die->child == NULL)
5214 {
5215 dump_die_for_error (die);
5216 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5217 bfd_get_filename (abfd));
5218 }
5219
5220 child_die = die->child;
5221
5222 while (child_die && child_die->tag)
5223 {
5224 process_die (child_die, cu);
5225
5226 child_die = sibling_die (child_die);
5227 }
5228
5229 do_cleanups (back_to);
5230}
5231
5fb290d7 5232static void
e142c38c
DJ
5233add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5234 struct dwarf2_cu *cu)
5fb290d7
DJ
5235{
5236 struct function_range *thisfn;
5237
5238 thisfn = (struct function_range *)
7b5a2f43 5239 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5240 thisfn->name = name;
5241 thisfn->lowpc = lowpc;
5242 thisfn->highpc = highpc;
5243 thisfn->seen_line = 0;
5244 thisfn->next = NULL;
5245
e142c38c
DJ
5246 if (cu->last_fn == NULL)
5247 cu->first_fn = thisfn;
5fb290d7 5248 else
e142c38c 5249 cu->last_fn->next = thisfn;
5fb290d7 5250
e142c38c 5251 cu->last_fn = thisfn;
c906108c
SS
5252}
5253
d389af10
JK
5254/* qsort helper for inherit_abstract_dies. */
5255
5256static int
5257unsigned_int_compar (const void *ap, const void *bp)
5258{
5259 unsigned int a = *(unsigned int *) ap;
5260 unsigned int b = *(unsigned int *) bp;
5261
5262 return (a > b) - (b > a);
5263}
5264
5265/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5266 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5267 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5268
5269static void
5270inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5271{
5272 struct die_info *child_die;
5273 unsigned die_children_count;
5274 /* CU offsets which were referenced by children of the current DIE. */
5275 unsigned *offsets;
5276 unsigned *offsets_end, *offsetp;
5277 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5278 struct die_info *origin_die;
5279 /* Iterator of the ORIGIN_DIE children. */
5280 struct die_info *origin_child_die;
5281 struct cleanup *cleanups;
5282 struct attribute *attr;
cd02d79d
PA
5283 struct dwarf2_cu *origin_cu;
5284 struct pending **origin_previous_list_in_scope;
d389af10
JK
5285
5286 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5287 if (!attr)
5288 return;
5289
cd02d79d
PA
5290 /* Note that following die references may follow to a die in a
5291 different cu. */
5292
5293 origin_cu = cu;
5294 origin_die = follow_die_ref (die, attr, &origin_cu);
5295
5296 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5297 symbols in. */
5298 origin_previous_list_in_scope = origin_cu->list_in_scope;
5299 origin_cu->list_in_scope = cu->list_in_scope;
5300
edb3359d
DJ
5301 if (die->tag != origin_die->tag
5302 && !(die->tag == DW_TAG_inlined_subroutine
5303 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5304 complaint (&symfile_complaints,
5305 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5306 die->offset, origin_die->offset);
5307
5308 child_die = die->child;
5309 die_children_count = 0;
5310 while (child_die && child_die->tag)
5311 {
5312 child_die = sibling_die (child_die);
5313 die_children_count++;
5314 }
5315 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5316 cleanups = make_cleanup (xfree, offsets);
5317
5318 offsets_end = offsets;
5319 child_die = die->child;
5320 while (child_die && child_die->tag)
5321 {
c38f313d
DJ
5322 /* For each CHILD_DIE, find the corresponding child of
5323 ORIGIN_DIE. If there is more than one layer of
5324 DW_AT_abstract_origin, follow them all; there shouldn't be,
5325 but GCC versions at least through 4.4 generate this (GCC PR
5326 40573). */
5327 struct die_info *child_origin_die = child_die;
cd02d79d 5328 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5329
c38f313d
DJ
5330 while (1)
5331 {
cd02d79d
PA
5332 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5333 child_origin_cu);
c38f313d
DJ
5334 if (attr == NULL)
5335 break;
cd02d79d
PA
5336 child_origin_die = follow_die_ref (child_origin_die, attr,
5337 &child_origin_cu);
c38f313d
DJ
5338 }
5339
d389af10
JK
5340 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5341 counterpart may exist. */
c38f313d 5342 if (child_origin_die != child_die)
d389af10 5343 {
edb3359d
DJ
5344 if (child_die->tag != child_origin_die->tag
5345 && !(child_die->tag == DW_TAG_inlined_subroutine
5346 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5347 complaint (&symfile_complaints,
5348 _("Child DIE 0x%x and its abstract origin 0x%x have "
5349 "different tags"), child_die->offset,
5350 child_origin_die->offset);
c38f313d
DJ
5351 if (child_origin_die->parent != origin_die)
5352 complaint (&symfile_complaints,
5353 _("Child DIE 0x%x and its abstract origin 0x%x have "
5354 "different parents"), child_die->offset,
5355 child_origin_die->offset);
5356 else
5357 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5358 }
5359 child_die = sibling_die (child_die);
5360 }
5361 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5362 unsigned_int_compar);
5363 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5364 if (offsetp[-1] == *offsetp)
5365 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5366 "to DIE 0x%x as their abstract origin"),
5367 die->offset, *offsetp);
5368
5369 offsetp = offsets;
5370 origin_child_die = origin_die->child;
5371 while (origin_child_die && origin_child_die->tag)
5372 {
5373 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5374 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5375 offsetp++;
5376 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5377 {
5378 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5379 process_die (origin_child_die, origin_cu);
d389af10
JK
5380 }
5381 origin_child_die = sibling_die (origin_child_die);
5382 }
cd02d79d 5383 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5384
5385 do_cleanups (cleanups);
5386}
5387
c906108c 5388static void
e7c27a73 5389read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5390{
e7c27a73 5391 struct objfile *objfile = cu->objfile;
52f0bd74 5392 struct context_stack *new;
c906108c
SS
5393 CORE_ADDR lowpc;
5394 CORE_ADDR highpc;
5395 struct die_info *child_die;
edb3359d 5396 struct attribute *attr, *call_line, *call_file;
c906108c 5397 char *name;
e142c38c 5398 CORE_ADDR baseaddr;
801e3a5b 5399 struct block *block;
edb3359d 5400 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5401 VEC (symbolp) *template_args = NULL;
5402 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5403
5404 if (inlined_func)
5405 {
5406 /* If we do not have call site information, we can't show the
5407 caller of this inlined function. That's too confusing, so
5408 only use the scope for local variables. */
5409 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5410 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5411 if (call_line == NULL || call_file == NULL)
5412 {
5413 read_lexical_block_scope (die, cu);
5414 return;
5415 }
5416 }
c906108c 5417
e142c38c
DJ
5418 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5419
94af9270 5420 name = dwarf2_name (die, cu);
c906108c 5421
e8d05480
JB
5422 /* Ignore functions with missing or empty names. These are actually
5423 illegal according to the DWARF standard. */
5424 if (name == NULL)
5425 {
5426 complaint (&symfile_complaints,
5427 _("missing name for subprogram DIE at %d"), die->offset);
5428 return;
5429 }
5430
5431 /* Ignore functions with missing or invalid low and high pc attributes. */
5432 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5433 {
ae4d0c03
PM
5434 attr = dwarf2_attr (die, DW_AT_external, cu);
5435 if (!attr || !DW_UNSND (attr))
5436 complaint (&symfile_complaints,
5437 _("cannot get low and high bounds for subprogram DIE at %d"),
5438 die->offset);
e8d05480
JB
5439 return;
5440 }
c906108c
SS
5441
5442 lowpc += baseaddr;
5443 highpc += baseaddr;
5444
5fb290d7 5445 /* Record the function range for dwarf_decode_lines. */
e142c38c 5446 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5447
34eaf542
TT
5448 /* If we have any template arguments, then we must allocate a
5449 different sort of symbol. */
5450 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5451 {
5452 if (child_die->tag == DW_TAG_template_type_param
5453 || child_die->tag == DW_TAG_template_value_param)
5454 {
5455 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5456 struct template_symbol);
5457 templ_func->base.is_cplus_template_function = 1;
5458 break;
5459 }
5460 }
5461
c906108c 5462 new = push_context (0, lowpc);
34eaf542
TT
5463 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5464 (struct symbol *) templ_func);
4c2df51b 5465
4cecd739
DJ
5466 /* If there is a location expression for DW_AT_frame_base, record
5467 it. */
e142c38c 5468 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5469 if (attr)
c034e007
AC
5470 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5471 expression is being recorded directly in the function's symbol
5472 and not in a separate frame-base object. I guess this hack is
5473 to avoid adding some sort of frame-base adjunct/annex to the
5474 function's symbol :-(. The problem with doing this is that it
5475 results in a function symbol with a location expression that
5476 has nothing to do with the location of the function, ouch! The
5477 relationship should be: a function's symbol has-a frame base; a
5478 frame-base has-a location expression. */
e7c27a73 5479 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5480
e142c38c 5481 cu->list_in_scope = &local_symbols;
c906108c 5482
639d11d3 5483 if (die->child != NULL)
c906108c 5484 {
639d11d3 5485 child_die = die->child;
c906108c
SS
5486 while (child_die && child_die->tag)
5487 {
34eaf542
TT
5488 if (child_die->tag == DW_TAG_template_type_param
5489 || child_die->tag == DW_TAG_template_value_param)
5490 {
5491 struct symbol *arg = new_symbol (child_die, NULL, cu);
5492
f1078f66
DJ
5493 if (arg != NULL)
5494 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5495 }
5496 else
5497 process_die (child_die, cu);
c906108c
SS
5498 child_die = sibling_die (child_die);
5499 }
5500 }
5501
d389af10
JK
5502 inherit_abstract_dies (die, cu);
5503
4a811a97
UW
5504 /* If we have a DW_AT_specification, we might need to import using
5505 directives from the context of the specification DIE. See the
5506 comment in determine_prefix. */
5507 if (cu->language == language_cplus
5508 && dwarf2_attr (die, DW_AT_specification, cu))
5509 {
5510 struct dwarf2_cu *spec_cu = cu;
5511 struct die_info *spec_die = die_specification (die, &spec_cu);
5512
5513 while (spec_die)
5514 {
5515 child_die = spec_die->child;
5516 while (child_die && child_die->tag)
5517 {
5518 if (child_die->tag == DW_TAG_imported_module)
5519 process_die (child_die, spec_cu);
5520 child_die = sibling_die (child_die);
5521 }
5522
5523 /* In some cases, GCC generates specification DIEs that
5524 themselves contain DW_AT_specification attributes. */
5525 spec_die = die_specification (spec_die, &spec_cu);
5526 }
5527 }
5528
c906108c
SS
5529 new = pop_context ();
5530 /* Make a block for the local symbols within. */
801e3a5b
JB
5531 block = finish_block (new->name, &local_symbols, new->old_blocks,
5532 lowpc, highpc, objfile);
5533
df8a16a1 5534 /* For C++, set the block's scope. */
f55ee35c 5535 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5536 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5537 determine_prefix (die, cu),
df8a16a1
DJ
5538 processing_has_namespace_info);
5539
801e3a5b
JB
5540 /* If we have address ranges, record them. */
5541 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5542
34eaf542
TT
5543 /* Attach template arguments to function. */
5544 if (! VEC_empty (symbolp, template_args))
5545 {
5546 gdb_assert (templ_func != NULL);
5547
5548 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5549 templ_func->template_arguments
5550 = obstack_alloc (&objfile->objfile_obstack,
5551 (templ_func->n_template_arguments
5552 * sizeof (struct symbol *)));
5553 memcpy (templ_func->template_arguments,
5554 VEC_address (symbolp, template_args),
5555 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5556 VEC_free (symbolp, template_args);
5557 }
5558
208d8187
JB
5559 /* In C++, we can have functions nested inside functions (e.g., when
5560 a function declares a class that has methods). This means that
5561 when we finish processing a function scope, we may need to go
5562 back to building a containing block's symbol lists. */
5563 local_symbols = new->locals;
5564 param_symbols = new->params;
27aa8d6a 5565 using_directives = new->using_directives;
208d8187 5566
921e78cf
JB
5567 /* If we've finished processing a top-level function, subsequent
5568 symbols go in the file symbol list. */
5569 if (outermost_context_p ())
e142c38c 5570 cu->list_in_scope = &file_symbols;
c906108c
SS
5571}
5572
5573/* Process all the DIES contained within a lexical block scope. Start
5574 a new scope, process the dies, and then close the scope. */
5575
5576static void
e7c27a73 5577read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5578{
e7c27a73 5579 struct objfile *objfile = cu->objfile;
52f0bd74 5580 struct context_stack *new;
c906108c
SS
5581 CORE_ADDR lowpc, highpc;
5582 struct die_info *child_die;
e142c38c
DJ
5583 CORE_ADDR baseaddr;
5584
5585 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5586
5587 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5588 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5589 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5590 be nasty. Might be easier to properly extend generic blocks to
af34e669 5591 describe ranges. */
d85a05f0 5592 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5593 return;
5594 lowpc += baseaddr;
5595 highpc += baseaddr;
5596
5597 push_context (0, lowpc);
639d11d3 5598 if (die->child != NULL)
c906108c 5599 {
639d11d3 5600 child_die = die->child;
c906108c
SS
5601 while (child_die && child_die->tag)
5602 {
e7c27a73 5603 process_die (child_die, cu);
c906108c
SS
5604 child_die = sibling_die (child_die);
5605 }
5606 }
5607 new = pop_context ();
5608
8540c487 5609 if (local_symbols != NULL || using_directives != NULL)
c906108c 5610 {
801e3a5b
JB
5611 struct block *block
5612 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5613 highpc, objfile);
5614
5615 /* Note that recording ranges after traversing children, as we
5616 do here, means that recording a parent's ranges entails
5617 walking across all its children's ranges as they appear in
5618 the address map, which is quadratic behavior.
5619
5620 It would be nicer to record the parent's ranges before
5621 traversing its children, simply overriding whatever you find
5622 there. But since we don't even decide whether to create a
5623 block until after we've traversed its children, that's hard
5624 to do. */
5625 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5626 }
5627 local_symbols = new->locals;
27aa8d6a 5628 using_directives = new->using_directives;
c906108c
SS
5629}
5630
43039443 5631/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5632 Return 1 if the attributes are present and valid, otherwise, return 0.
5633 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5634
5635static int
5636dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5637 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5638 struct partial_symtab *ranges_pst)
43039443
JK
5639{
5640 struct objfile *objfile = cu->objfile;
5641 struct comp_unit_head *cu_header = &cu->header;
5642 bfd *obfd = objfile->obfd;
5643 unsigned int addr_size = cu_header->addr_size;
5644 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5645 /* Base address selection entry. */
5646 CORE_ADDR base;
5647 int found_base;
5648 unsigned int dummy;
5649 gdb_byte *buffer;
5650 CORE_ADDR marker;
5651 int low_set;
5652 CORE_ADDR low = 0;
5653 CORE_ADDR high = 0;
ff013f42 5654 CORE_ADDR baseaddr;
43039443 5655
d00adf39
DE
5656 found_base = cu->base_known;
5657 base = cu->base_address;
43039443 5658
be391dca 5659 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5660 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5661 {
5662 complaint (&symfile_complaints,
5663 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5664 offset);
5665 return 0;
5666 }
dce234bc 5667 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5668
5669 /* Read in the largest possible address. */
5670 marker = read_address (obfd, buffer, cu, &dummy);
5671 if ((marker & mask) == mask)
5672 {
5673 /* If we found the largest possible address, then
5674 read the base address. */
5675 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5676 buffer += 2 * addr_size;
5677 offset += 2 * addr_size;
5678 found_base = 1;
5679 }
5680
5681 low_set = 0;
5682
e7030f15 5683 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5684
43039443
JK
5685 while (1)
5686 {
5687 CORE_ADDR range_beginning, range_end;
5688
5689 range_beginning = read_address (obfd, buffer, cu, &dummy);
5690 buffer += addr_size;
5691 range_end = read_address (obfd, buffer, cu, &dummy);
5692 buffer += addr_size;
5693 offset += 2 * addr_size;
5694
5695 /* An end of list marker is a pair of zero addresses. */
5696 if (range_beginning == 0 && range_end == 0)
5697 /* Found the end of list entry. */
5698 break;
5699
5700 /* Each base address selection entry is a pair of 2 values.
5701 The first is the largest possible address, the second is
5702 the base address. Check for a base address here. */
5703 if ((range_beginning & mask) == mask)
5704 {
5705 /* If we found the largest possible address, then
5706 read the base address. */
5707 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5708 found_base = 1;
5709 continue;
5710 }
5711
5712 if (!found_base)
5713 {
5714 /* We have no valid base address for the ranges
5715 data. */
5716 complaint (&symfile_complaints,
5717 _("Invalid .debug_ranges data (no base address)"));
5718 return 0;
5719 }
5720
5721 range_beginning += base;
5722 range_end += base;
5723
ff013f42
JK
5724 if (ranges_pst != NULL && range_beginning < range_end)
5725 addrmap_set_empty (objfile->psymtabs_addrmap,
5726 range_beginning + baseaddr, range_end - 1 + baseaddr,
5727 ranges_pst);
5728
43039443
JK
5729 /* FIXME: This is recording everything as a low-high
5730 segment of consecutive addresses. We should have a
5731 data structure for discontiguous block ranges
5732 instead. */
5733 if (! low_set)
5734 {
5735 low = range_beginning;
5736 high = range_end;
5737 low_set = 1;
5738 }
5739 else
5740 {
5741 if (range_beginning < low)
5742 low = range_beginning;
5743 if (range_end > high)
5744 high = range_end;
5745 }
5746 }
5747
5748 if (! low_set)
5749 /* If the first entry is an end-of-list marker, the range
5750 describes an empty scope, i.e. no instructions. */
5751 return 0;
5752
5753 if (low_return)
5754 *low_return = low;
5755 if (high_return)
5756 *high_return = high;
5757 return 1;
5758}
5759
af34e669
DJ
5760/* Get low and high pc attributes from a die. Return 1 if the attributes
5761 are present and valid, otherwise, return 0. Return -1 if the range is
5762 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5763static int
af34e669 5764dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5765 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5766 struct partial_symtab *pst)
c906108c
SS
5767{
5768 struct attribute *attr;
af34e669
DJ
5769 CORE_ADDR low = 0;
5770 CORE_ADDR high = 0;
5771 int ret = 0;
c906108c 5772
e142c38c 5773 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5774 if (attr)
af34e669
DJ
5775 {
5776 high = DW_ADDR (attr);
e142c38c 5777 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5778 if (attr)
5779 low = DW_ADDR (attr);
5780 else
5781 /* Found high w/o low attribute. */
5782 return 0;
5783
5784 /* Found consecutive range of addresses. */
5785 ret = 1;
5786 }
c906108c 5787 else
af34e669 5788 {
e142c38c 5789 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5790 if (attr != NULL)
5791 {
af34e669 5792 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5793 .debug_ranges section. */
d85a05f0 5794 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5795 return 0;
43039443 5796 /* Found discontinuous range of addresses. */
af34e669
DJ
5797 ret = -1;
5798 }
5799 }
c906108c
SS
5800
5801 if (high < low)
5802 return 0;
5803
5804 /* When using the GNU linker, .gnu.linkonce. sections are used to
5805 eliminate duplicate copies of functions and vtables and such.
5806 The linker will arbitrarily choose one and discard the others.
5807 The AT_*_pc values for such functions refer to local labels in
5808 these sections. If the section from that file was discarded, the
5809 labels are not in the output, so the relocs get a value of 0.
5810 If this is a discarded function, mark the pc bounds as invalid,
5811 so that GDB will ignore it. */
72dca2f5 5812 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5813 return 0;
5814
5815 *lowpc = low;
5816 *highpc = high;
af34e669 5817 return ret;
c906108c
SS
5818}
5819
b084d499
JB
5820/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5821 its low and high PC addresses. Do nothing if these addresses could not
5822 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5823 and HIGHPC to the high address if greater than HIGHPC. */
5824
5825static void
5826dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5827 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5828 struct dwarf2_cu *cu)
5829{
5830 CORE_ADDR low, high;
5831 struct die_info *child = die->child;
5832
d85a05f0 5833 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5834 {
5835 *lowpc = min (*lowpc, low);
5836 *highpc = max (*highpc, high);
5837 }
5838
5839 /* If the language does not allow nested subprograms (either inside
5840 subprograms or lexical blocks), we're done. */
5841 if (cu->language != language_ada)
5842 return;
6e70227d 5843
b084d499
JB
5844 /* Check all the children of the given DIE. If it contains nested
5845 subprograms, then check their pc bounds. Likewise, we need to
5846 check lexical blocks as well, as they may also contain subprogram
5847 definitions. */
5848 while (child && child->tag)
5849 {
5850 if (child->tag == DW_TAG_subprogram
5851 || child->tag == DW_TAG_lexical_block)
5852 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5853 child = sibling_die (child);
5854 }
5855}
5856
fae299cd
DC
5857/* Get the low and high pc's represented by the scope DIE, and store
5858 them in *LOWPC and *HIGHPC. If the correct values can't be
5859 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5860
5861static void
5862get_scope_pc_bounds (struct die_info *die,
5863 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5864 struct dwarf2_cu *cu)
5865{
5866 CORE_ADDR best_low = (CORE_ADDR) -1;
5867 CORE_ADDR best_high = (CORE_ADDR) 0;
5868 CORE_ADDR current_low, current_high;
5869
d85a05f0 5870 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5871 {
5872 best_low = current_low;
5873 best_high = current_high;
5874 }
5875 else
5876 {
5877 struct die_info *child = die->child;
5878
5879 while (child && child->tag)
5880 {
5881 switch (child->tag) {
5882 case DW_TAG_subprogram:
b084d499 5883 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5884 break;
5885 case DW_TAG_namespace:
f55ee35c 5886 case DW_TAG_module:
fae299cd
DC
5887 /* FIXME: carlton/2004-01-16: Should we do this for
5888 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5889 that current GCC's always emit the DIEs corresponding
5890 to definitions of methods of classes as children of a
5891 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5892 the DIEs giving the declarations, which could be
5893 anywhere). But I don't see any reason why the
5894 standards says that they have to be there. */
5895 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5896
5897 if (current_low != ((CORE_ADDR) -1))
5898 {
5899 best_low = min (best_low, current_low);
5900 best_high = max (best_high, current_high);
5901 }
5902 break;
5903 default:
5904 /* Ignore. */
5905 break;
5906 }
5907
5908 child = sibling_die (child);
5909 }
5910 }
5911
5912 *lowpc = best_low;
5913 *highpc = best_high;
5914}
5915
801e3a5b
JB
5916/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5917 in DIE. */
5918static void
5919dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5920 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5921{
5922 struct attribute *attr;
5923
5924 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5925 if (attr)
5926 {
5927 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5928
801e3a5b
JB
5929 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5930 if (attr)
5931 {
5932 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5933
801e3a5b
JB
5934 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5935 }
5936 }
5937
5938 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5939 if (attr)
5940 {
5941 bfd *obfd = cu->objfile->obfd;
5942
5943 /* The value of the DW_AT_ranges attribute is the offset of the
5944 address range list in the .debug_ranges section. */
5945 unsigned long offset = DW_UNSND (attr);
dce234bc 5946 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5947
5948 /* For some target architectures, but not others, the
5949 read_address function sign-extends the addresses it returns.
5950 To recognize base address selection entries, we need a
5951 mask. */
5952 unsigned int addr_size = cu->header.addr_size;
5953 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5954
5955 /* The base address, to which the next pair is relative. Note
5956 that this 'base' is a DWARF concept: most entries in a range
5957 list are relative, to reduce the number of relocs against the
5958 debugging information. This is separate from this function's
5959 'baseaddr' argument, which GDB uses to relocate debugging
5960 information from a shared library based on the address at
5961 which the library was loaded. */
d00adf39
DE
5962 CORE_ADDR base = cu->base_address;
5963 int base_known = cu->base_known;
801e3a5b 5964
be391dca 5965 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5966 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5967 {
5968 complaint (&symfile_complaints,
5969 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5970 offset);
5971 return;
5972 }
5973
5974 for (;;)
5975 {
5976 unsigned int bytes_read;
5977 CORE_ADDR start, end;
5978
5979 start = read_address (obfd, buffer, cu, &bytes_read);
5980 buffer += bytes_read;
5981 end = read_address (obfd, buffer, cu, &bytes_read);
5982 buffer += bytes_read;
5983
5984 /* Did we find the end of the range list? */
5985 if (start == 0 && end == 0)
5986 break;
5987
5988 /* Did we find a base address selection entry? */
5989 else if ((start & base_select_mask) == base_select_mask)
5990 {
5991 base = end;
5992 base_known = 1;
5993 }
5994
5995 /* We found an ordinary address range. */
5996 else
5997 {
5998 if (!base_known)
5999 {
6000 complaint (&symfile_complaints,
6001 _("Invalid .debug_ranges data (no base address)"));
6002 return;
6003 }
6004
6e70227d
DE
6005 record_block_range (block,
6006 baseaddr + base + start,
801e3a5b
JB
6007 baseaddr + base + end - 1);
6008 }
6009 }
6010 }
6011}
6012
c906108c
SS
6013/* Add an aggregate field to the field list. */
6014
6015static void
107d2387 6016dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6017 struct dwarf2_cu *cu)
6e70227d 6018{
e7c27a73 6019 struct objfile *objfile = cu->objfile;
5e2b427d 6020 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6021 struct nextfield *new_field;
6022 struct attribute *attr;
6023 struct field *fp;
6024 char *fieldname = "";
6025
6026 /* Allocate a new field list entry and link it in. */
6027 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6028 make_cleanup (xfree, new_field);
c906108c 6029 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6030
6031 if (die->tag == DW_TAG_inheritance)
6032 {
6033 new_field->next = fip->baseclasses;
6034 fip->baseclasses = new_field;
6035 }
6036 else
6037 {
6038 new_field->next = fip->fields;
6039 fip->fields = new_field;
6040 }
c906108c
SS
6041 fip->nfields++;
6042
6043 /* Handle accessibility and virtuality of field.
6044 The default accessibility for members is public, the default
6045 accessibility for inheritance is private. */
6046 if (die->tag != DW_TAG_inheritance)
6047 new_field->accessibility = DW_ACCESS_public;
6048 else
6049 new_field->accessibility = DW_ACCESS_private;
6050 new_field->virtuality = DW_VIRTUALITY_none;
6051
e142c38c 6052 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6053 if (attr)
6054 new_field->accessibility = DW_UNSND (attr);
6055 if (new_field->accessibility != DW_ACCESS_public)
6056 fip->non_public_fields = 1;
e142c38c 6057 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6058 if (attr)
6059 new_field->virtuality = DW_UNSND (attr);
6060
6061 fp = &new_field->field;
a9a9bd0f 6062
e142c38c 6063 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6064 {
a9a9bd0f 6065 /* Data member other than a C++ static data member. */
6e70227d 6066
c906108c 6067 /* Get type of field. */
e7c27a73 6068 fp->type = die_type (die, cu);
c906108c 6069
d6a843b5 6070 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6071
c906108c 6072 /* Get bit size of field (zero if none). */
e142c38c 6073 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6074 if (attr)
6075 {
6076 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6077 }
6078 else
6079 {
6080 FIELD_BITSIZE (*fp) = 0;
6081 }
6082
6083 /* Get bit offset of field. */
e142c38c 6084 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6085 if (attr)
6086 {
d4b96c9a 6087 int byte_offset = 0;
c6a0999f 6088
3690dd37 6089 if (attr_form_is_section_offset (attr))
d4b96c9a 6090 dwarf2_complex_location_expr_complaint ();
3690dd37 6091 else if (attr_form_is_constant (attr))
c6a0999f 6092 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6093 else if (attr_form_is_block (attr))
c6a0999f 6094 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6095 else
6096 dwarf2_complex_location_expr_complaint ();
c6a0999f 6097
d6a843b5 6098 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6099 }
e142c38c 6100 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6101 if (attr)
6102 {
5e2b427d 6103 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6104 {
6105 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6106 additional bit offset from the MSB of the containing
6107 anonymous object to the MSB of the field. We don't
6108 have to do anything special since we don't need to
6109 know the size of the anonymous object. */
c906108c
SS
6110 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6111 }
6112 else
6113 {
6114 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6115 MSB of the anonymous object, subtract off the number of
6116 bits from the MSB of the field to the MSB of the
6117 object, and then subtract off the number of bits of
6118 the field itself. The result is the bit offset of
6119 the LSB of the field. */
c906108c
SS
6120 int anonymous_size;
6121 int bit_offset = DW_UNSND (attr);
6122
e142c38c 6123 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6124 if (attr)
6125 {
6126 /* The size of the anonymous object containing
6127 the bit field is explicit, so use the
6128 indicated size (in bytes). */
6129 anonymous_size = DW_UNSND (attr);
6130 }
6131 else
6132 {
6133 /* The size of the anonymous object containing
6134 the bit field must be inferred from the type
6135 attribute of the data member containing the
6136 bit field. */
6137 anonymous_size = TYPE_LENGTH (fp->type);
6138 }
6139 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6140 - bit_offset - FIELD_BITSIZE (*fp);
6141 }
6142 }
6143
6144 /* Get name of field. */
39cbfefa
DJ
6145 fieldname = dwarf2_name (die, cu);
6146 if (fieldname == NULL)
6147 fieldname = "";
d8151005
DJ
6148
6149 /* The name is already allocated along with this objfile, so we don't
6150 need to duplicate it for the type. */
6151 fp->name = fieldname;
c906108c
SS
6152
6153 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6154 pointer or virtual base class pointer) to private. */
e142c38c 6155 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6156 {
d48cc9dd 6157 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6158 new_field->accessibility = DW_ACCESS_private;
6159 fip->non_public_fields = 1;
6160 }
6161 }
a9a9bd0f 6162 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6163 {
a9a9bd0f
DC
6164 /* C++ static member. */
6165
6166 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6167 is a declaration, but all versions of G++ as of this writing
6168 (so through at least 3.2.1) incorrectly generate
6169 DW_TAG_variable tags. */
6e70227d 6170
c906108c 6171 char *physname;
c906108c 6172
a9a9bd0f 6173 /* Get name of field. */
39cbfefa
DJ
6174 fieldname = dwarf2_name (die, cu);
6175 if (fieldname == NULL)
c906108c
SS
6176 return;
6177
254e6b9e 6178 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6179 if (attr
6180 /* Only create a symbol if this is an external value.
6181 new_symbol checks this and puts the value in the global symbol
6182 table, which we want. If it is not external, new_symbol
6183 will try to put the value in cu->list_in_scope which is wrong. */
6184 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6185 {
6186 /* A static const member, not much different than an enum as far as
6187 we're concerned, except that we can support more types. */
6188 new_symbol (die, NULL, cu);
6189 }
6190
2df3850c 6191 /* Get physical name. */
94af9270 6192 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6193
d8151005
DJ
6194 /* The name is already allocated along with this objfile, so we don't
6195 need to duplicate it for the type. */
6196 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6197 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6198 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6199 }
6200 else if (die->tag == DW_TAG_inheritance)
6201 {
6202 /* C++ base class field. */
e142c38c 6203 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6204 if (attr)
d4b96c9a
JK
6205 {
6206 int byte_offset = 0;
6207
6208 if (attr_form_is_section_offset (attr))
6209 dwarf2_complex_location_expr_complaint ();
6210 else if (attr_form_is_constant (attr))
6211 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6212 else if (attr_form_is_block (attr))
6213 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6214 else
6215 dwarf2_complex_location_expr_complaint ();
6216
6217 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6218 }
c906108c 6219 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6220 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6221 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6222 fip->nbaseclasses++;
6223 }
6224}
6225
98751a41
JK
6226/* Add a typedef defined in the scope of the FIP's class. */
6227
6228static void
6229dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6230 struct dwarf2_cu *cu)
6e70227d 6231{
98751a41
JK
6232 struct objfile *objfile = cu->objfile;
6233 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6234 struct typedef_field_list *new_field;
6235 struct attribute *attr;
6236 struct typedef_field *fp;
6237 char *fieldname = "";
6238
6239 /* Allocate a new field list entry and link it in. */
6240 new_field = xzalloc (sizeof (*new_field));
6241 make_cleanup (xfree, new_field);
6242
6243 gdb_assert (die->tag == DW_TAG_typedef);
6244
6245 fp = &new_field->field;
6246
6247 /* Get name of field. */
6248 fp->name = dwarf2_name (die, cu);
6249 if (fp->name == NULL)
6250 return;
6251
6252 fp->type = read_type_die (die, cu);
6253
6254 new_field->next = fip->typedef_field_list;
6255 fip->typedef_field_list = new_field;
6256 fip->typedef_field_list_count++;
6257}
6258
c906108c
SS
6259/* Create the vector of fields, and attach it to the type. */
6260
6261static void
fba45db2 6262dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6263 struct dwarf2_cu *cu)
c906108c
SS
6264{
6265 int nfields = fip->nfields;
6266
6267 /* Record the field count, allocate space for the array of fields,
6268 and create blank accessibility bitfields if necessary. */
6269 TYPE_NFIELDS (type) = nfields;
6270 TYPE_FIELDS (type) = (struct field *)
6271 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6272 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6273
b4ba55a1 6274 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6275 {
6276 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6277
6278 TYPE_FIELD_PRIVATE_BITS (type) =
6279 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6280 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6281
6282 TYPE_FIELD_PROTECTED_BITS (type) =
6283 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6284 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6285
774b6a14
TT
6286 TYPE_FIELD_IGNORE_BITS (type) =
6287 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6288 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6289 }
6290
6291 /* If the type has baseclasses, allocate and clear a bit vector for
6292 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6293 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6294 {
6295 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6296 unsigned char *pointer;
c906108c
SS
6297
6298 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6299 pointer = TYPE_ALLOC (type, num_bytes);
6300 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6301 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6302 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6303 }
6304
6305 /* Copy the saved-up fields into the field vector. Start from the head
6306 of the list, adding to the tail of the field array, so that they end
6307 up in the same order in the array in which they were added to the list. */
6308 while (nfields-- > 0)
6309 {
7d0ccb61
DJ
6310 struct nextfield *fieldp;
6311
6312 if (fip->fields)
6313 {
6314 fieldp = fip->fields;
6315 fip->fields = fieldp->next;
6316 }
6317 else
6318 {
6319 fieldp = fip->baseclasses;
6320 fip->baseclasses = fieldp->next;
6321 }
6322
6323 TYPE_FIELD (type, nfields) = fieldp->field;
6324 switch (fieldp->accessibility)
c906108c 6325 {
c5aa993b 6326 case DW_ACCESS_private:
b4ba55a1
JB
6327 if (cu->language != language_ada)
6328 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6329 break;
c906108c 6330
c5aa993b 6331 case DW_ACCESS_protected:
b4ba55a1
JB
6332 if (cu->language != language_ada)
6333 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6334 break;
c906108c 6335
c5aa993b
JM
6336 case DW_ACCESS_public:
6337 break;
c906108c 6338
c5aa993b
JM
6339 default:
6340 /* Unknown accessibility. Complain and treat it as public. */
6341 {
e2e0b3e5 6342 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6343 fieldp->accessibility);
c5aa993b
JM
6344 }
6345 break;
c906108c
SS
6346 }
6347 if (nfields < fip->nbaseclasses)
6348 {
7d0ccb61 6349 switch (fieldp->virtuality)
c906108c 6350 {
c5aa993b
JM
6351 case DW_VIRTUALITY_virtual:
6352 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6353 if (cu->language == language_ada)
6354 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6355 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6356 break;
c906108c
SS
6357 }
6358 }
c906108c
SS
6359 }
6360}
6361
c906108c
SS
6362/* Add a member function to the proper fieldlist. */
6363
6364static void
107d2387 6365dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6366 struct type *type, struct dwarf2_cu *cu)
c906108c 6367{
e7c27a73 6368 struct objfile *objfile = cu->objfile;
c906108c
SS
6369 struct attribute *attr;
6370 struct fnfieldlist *flp;
6371 int i;
6372 struct fn_field *fnp;
6373 char *fieldname;
c906108c 6374 struct nextfnfield *new_fnfield;
f792889a 6375 struct type *this_type;
c906108c 6376
b4ba55a1
JB
6377 if (cu->language == language_ada)
6378 error ("unexpected member function in Ada type");
6379
2df3850c 6380 /* Get name of member function. */
39cbfefa
DJ
6381 fieldname = dwarf2_name (die, cu);
6382 if (fieldname == NULL)
2df3850c 6383 return;
c906108c 6384
c906108c
SS
6385 /* Look up member function name in fieldlist. */
6386 for (i = 0; i < fip->nfnfields; i++)
6387 {
27bfe10e 6388 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6389 break;
6390 }
6391
6392 /* Create new list element if necessary. */
6393 if (i < fip->nfnfields)
6394 flp = &fip->fnfieldlists[i];
6395 else
6396 {
6397 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6398 {
6399 fip->fnfieldlists = (struct fnfieldlist *)
6400 xrealloc (fip->fnfieldlists,
6401 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6402 * sizeof (struct fnfieldlist));
c906108c 6403 if (fip->nfnfields == 0)
c13c43fd 6404 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6405 }
6406 flp = &fip->fnfieldlists[fip->nfnfields];
6407 flp->name = fieldname;
6408 flp->length = 0;
6409 flp->head = NULL;
3da10d80 6410 i = fip->nfnfields++;
c906108c
SS
6411 }
6412
6413 /* Create a new member function field and chain it to the field list
6414 entry. */
6415 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6416 make_cleanup (xfree, new_fnfield);
c906108c
SS
6417 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6418 new_fnfield->next = flp->head;
6419 flp->head = new_fnfield;
6420 flp->length++;
6421
6422 /* Fill in the member function field info. */
6423 fnp = &new_fnfield->fnfield;
3da10d80
KS
6424
6425 /* Delay processing of the physname until later. */
6426 if (cu->language == language_cplus || cu->language == language_java)
6427 {
6428 add_to_method_list (type, i, flp->length - 1, fieldname,
6429 die, cu);
6430 }
6431 else
6432 {
6433 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6434 fnp->physname = physname ? physname : "";
6435 }
6436
c906108c 6437 fnp->type = alloc_type (objfile);
f792889a
DJ
6438 this_type = read_type_die (die, cu);
6439 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6440 {
f792889a 6441 int nparams = TYPE_NFIELDS (this_type);
c906108c 6442
f792889a 6443 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6444 of the method itself (TYPE_CODE_METHOD). */
6445 smash_to_method_type (fnp->type, type,
f792889a
DJ
6446 TYPE_TARGET_TYPE (this_type),
6447 TYPE_FIELDS (this_type),
6448 TYPE_NFIELDS (this_type),
6449 TYPE_VARARGS (this_type));
c906108c
SS
6450
6451 /* Handle static member functions.
c5aa993b
JM
6452 Dwarf2 has no clean way to discern C++ static and non-static
6453 member functions. G++ helps GDB by marking the first
6454 parameter for non-static member functions (which is the
6455 this pointer) as artificial. We obtain this information
6456 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6457 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6458 fnp->voffset = VOFFSET_STATIC;
6459 }
6460 else
e2e0b3e5 6461 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6462 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6463
6464 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6465 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6466 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6467
6468 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6469 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6470
6471 /* Get accessibility. */
e142c38c 6472 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6473 if (attr)
6474 {
6475 switch (DW_UNSND (attr))
6476 {
c5aa993b
JM
6477 case DW_ACCESS_private:
6478 fnp->is_private = 1;
6479 break;
6480 case DW_ACCESS_protected:
6481 fnp->is_protected = 1;
6482 break;
c906108c
SS
6483 }
6484 }
6485
b02dede2 6486 /* Check for artificial methods. */
e142c38c 6487 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6488 if (attr && DW_UNSND (attr) != 0)
6489 fnp->is_artificial = 1;
6490
0d564a31 6491 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6492 function. For older versions of GCC, this is an offset in the
6493 appropriate virtual table, as specified by DW_AT_containing_type.
6494 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6495 to the object address. */
6496
e142c38c 6497 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6498 if (attr)
8e19ed76 6499 {
aec5aa8b 6500 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6501 {
aec5aa8b
TT
6502 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6503 {
6504 /* Old-style GCC. */
6505 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6506 }
6507 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6508 || (DW_BLOCK (attr)->size > 1
6509 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6510 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6511 {
6512 struct dwarf_block blk;
6513 int offset;
6514
6515 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6516 ? 1 : 2);
6517 blk.size = DW_BLOCK (attr)->size - offset;
6518 blk.data = DW_BLOCK (attr)->data + offset;
6519 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6520 if ((fnp->voffset % cu->header.addr_size) != 0)
6521 dwarf2_complex_location_expr_complaint ();
6522 else
6523 fnp->voffset /= cu->header.addr_size;
6524 fnp->voffset += 2;
6525 }
6526 else
6527 dwarf2_complex_location_expr_complaint ();
6528
6529 if (!fnp->fcontext)
6530 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6531 }
3690dd37 6532 else if (attr_form_is_section_offset (attr))
8e19ed76 6533 {
4d3c2250 6534 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6535 }
6536 else
6537 {
4d3c2250
KB
6538 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6539 fieldname);
8e19ed76 6540 }
0d564a31 6541 }
d48cc9dd
DJ
6542 else
6543 {
6544 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6545 if (attr && DW_UNSND (attr))
6546 {
6547 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6548 complaint (&symfile_complaints,
6549 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6550 fieldname, die->offset);
9655fd1a 6551 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6552 TYPE_CPLUS_DYNAMIC (type) = 1;
6553 }
6554 }
c906108c
SS
6555}
6556
6557/* Create the vector of member function fields, and attach it to the type. */
6558
6559static void
fba45db2 6560dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6561 struct dwarf2_cu *cu)
c906108c
SS
6562{
6563 struct fnfieldlist *flp;
6564 int total_length = 0;
6565 int i;
6566
b4ba55a1
JB
6567 if (cu->language == language_ada)
6568 error ("unexpected member functions in Ada type");
6569
c906108c
SS
6570 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6571 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6572 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6573
6574 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6575 {
6576 struct nextfnfield *nfp = flp->head;
6577 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6578 int k;
6579
6580 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6581 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6582 fn_flp->fn_fields = (struct fn_field *)
6583 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6584 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6585 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6586
6587 total_length += flp->length;
6588 }
6589
6590 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6591 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6592}
6593
1168df01
JB
6594/* Returns non-zero if NAME is the name of a vtable member in CU's
6595 language, zero otherwise. */
6596static int
6597is_vtable_name (const char *name, struct dwarf2_cu *cu)
6598{
6599 static const char vptr[] = "_vptr";
987504bb 6600 static const char vtable[] = "vtable";
1168df01 6601
987504bb
JJ
6602 /* Look for the C++ and Java forms of the vtable. */
6603 if ((cu->language == language_java
6604 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6605 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6606 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6607 return 1;
6608
6609 return 0;
6610}
6611
c0dd20ea 6612/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6613 functions, with the ABI-specified layout. If TYPE describes
6614 such a structure, smash it into a member function type.
61049d3b
DJ
6615
6616 GCC shouldn't do this; it should just output pointer to member DIEs.
6617 This is GCC PR debug/28767. */
c0dd20ea 6618
0b92b5bb
TT
6619static void
6620quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6621{
0b92b5bb 6622 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6623
6624 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6625 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6626 return;
c0dd20ea
DJ
6627
6628 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6629 if (TYPE_FIELD_NAME (type, 0) == NULL
6630 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6631 || TYPE_FIELD_NAME (type, 1) == NULL
6632 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6633 return;
c0dd20ea
DJ
6634
6635 /* Find the type of the method. */
0b92b5bb 6636 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6637 if (pfn_type == NULL
6638 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6639 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6640 return;
c0dd20ea
DJ
6641
6642 /* Look for the "this" argument. */
6643 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6644 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6645 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6646 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6647 return;
c0dd20ea
DJ
6648
6649 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6650 new_type = alloc_type (objfile);
6651 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6652 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6653 TYPE_VARARGS (pfn_type));
0b92b5bb 6654 smash_to_methodptr_type (type, new_type);
c0dd20ea 6655}
1168df01 6656
c906108c 6657/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6658 (definition) to create a type for the structure or union. Fill in
6659 the type's name and general properties; the members will not be
6660 processed until process_structure_type.
c906108c 6661
c767944b
DJ
6662 NOTE: we need to call these functions regardless of whether or not the
6663 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6664 structure or union. This gets the type entered into our set of
6665 user defined types.
6666
6667 However, if the structure is incomplete (an opaque struct/union)
6668 then suppress creating a symbol table entry for it since gdb only
6669 wants to find the one with the complete definition. Note that if
6670 it is complete, we just call new_symbol, which does it's own
6671 checking about whether the struct/union is anonymous or not (and
6672 suppresses creating a symbol table entry itself). */
6673
f792889a 6674static struct type *
134d01f1 6675read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6676{
e7c27a73 6677 struct objfile *objfile = cu->objfile;
c906108c
SS
6678 struct type *type;
6679 struct attribute *attr;
39cbfefa 6680 char *name;
c906108c 6681
348e048f
DE
6682 /* If the definition of this type lives in .debug_types, read that type.
6683 Don't follow DW_AT_specification though, that will take us back up
6684 the chain and we want to go down. */
6685 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6686 if (attr)
6687 {
6688 struct dwarf2_cu *type_cu = cu;
6689 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6690
348e048f
DE
6691 /* We could just recurse on read_structure_type, but we need to call
6692 get_die_type to ensure only one type for this DIE is created.
6693 This is important, for example, because for c++ classes we need
6694 TYPE_NAME set which is only done by new_symbol. Blech. */
6695 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6696
6697 /* TYPE_CU may not be the same as CU.
6698 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6699 return set_die_type (die, type, cu);
6700 }
6701
c0dd20ea 6702 type = alloc_type (objfile);
c906108c 6703 INIT_CPLUS_SPECIFIC (type);
93311388 6704
39cbfefa
DJ
6705 name = dwarf2_name (die, cu);
6706 if (name != NULL)
c906108c 6707 {
987504bb
JJ
6708 if (cu->language == language_cplus
6709 || cu->language == language_java)
63d06c5c 6710 {
3da10d80
KS
6711 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6712
6713 /* dwarf2_full_name might have already finished building the DIE's
6714 type. If so, there is no need to continue. */
6715 if (get_die_type (die, cu) != NULL)
6716 return get_die_type (die, cu);
6717
6718 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6719 if (die->tag == DW_TAG_structure_type
6720 || die->tag == DW_TAG_class_type)
6721 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6722 }
6723 else
6724 {
d8151005
DJ
6725 /* The name is already allocated along with this objfile, so
6726 we don't need to duplicate it for the type. */
94af9270
KS
6727 TYPE_TAG_NAME (type) = (char *) name;
6728 if (die->tag == DW_TAG_class_type)
6729 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6730 }
c906108c
SS
6731 }
6732
6733 if (die->tag == DW_TAG_structure_type)
6734 {
6735 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6736 }
6737 else if (die->tag == DW_TAG_union_type)
6738 {
6739 TYPE_CODE (type) = TYPE_CODE_UNION;
6740 }
6741 else
6742 {
c906108c
SS
6743 TYPE_CODE (type) = TYPE_CODE_CLASS;
6744 }
6745
0cc2414c
TT
6746 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6747 TYPE_DECLARED_CLASS (type) = 1;
6748
e142c38c 6749 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6750 if (attr)
6751 {
6752 TYPE_LENGTH (type) = DW_UNSND (attr);
6753 }
6754 else
6755 {
6756 TYPE_LENGTH (type) = 0;
6757 }
6758
876cecd0 6759 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6760 if (die_is_declaration (die, cu))
876cecd0 6761 TYPE_STUB (type) = 1;
a6c727b2
DJ
6762 else if (attr == NULL && die->child == NULL
6763 && producer_is_realview (cu->producer))
6764 /* RealView does not output the required DW_AT_declaration
6765 on incomplete types. */
6766 TYPE_STUB (type) = 1;
dc718098 6767
c906108c
SS
6768 /* We need to add the type field to the die immediately so we don't
6769 infinitely recurse when dealing with pointers to the structure
6770 type within the structure itself. */
1c379e20 6771 set_die_type (die, type, cu);
c906108c 6772
7e314c57
JK
6773 /* set_die_type should be already done. */
6774 set_descriptive_type (type, die, cu);
6775
c767944b
DJ
6776 return type;
6777}
6778
6779/* Finish creating a structure or union type, including filling in
6780 its members and creating a symbol for it. */
6781
6782static void
6783process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6784{
6785 struct objfile *objfile = cu->objfile;
6786 struct die_info *child_die = die->child;
6787 struct type *type;
6788
6789 type = get_die_type (die, cu);
6790 if (type == NULL)
6791 type = read_structure_type (die, cu);
6792
e142c38c 6793 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6794 {
6795 struct field_info fi;
6796 struct die_info *child_die;
34eaf542 6797 VEC (symbolp) *template_args = NULL;
c767944b 6798 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6799
6800 memset (&fi, 0, sizeof (struct field_info));
6801
639d11d3 6802 child_die = die->child;
c906108c
SS
6803
6804 while (child_die && child_die->tag)
6805 {
a9a9bd0f
DC
6806 if (child_die->tag == DW_TAG_member
6807 || child_die->tag == DW_TAG_variable)
c906108c 6808 {
a9a9bd0f
DC
6809 /* NOTE: carlton/2002-11-05: A C++ static data member
6810 should be a DW_TAG_member that is a declaration, but
6811 all versions of G++ as of this writing (so through at
6812 least 3.2.1) incorrectly generate DW_TAG_variable
6813 tags for them instead. */
e7c27a73 6814 dwarf2_add_field (&fi, child_die, cu);
c906108c 6815 }
8713b1b1 6816 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6817 {
6818 /* C++ member function. */
e7c27a73 6819 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6820 }
6821 else if (child_die->tag == DW_TAG_inheritance)
6822 {
6823 /* C++ base class field. */
e7c27a73 6824 dwarf2_add_field (&fi, child_die, cu);
c906108c 6825 }
98751a41
JK
6826 else if (child_die->tag == DW_TAG_typedef)
6827 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6828 else if (child_die->tag == DW_TAG_template_type_param
6829 || child_die->tag == DW_TAG_template_value_param)
6830 {
6831 struct symbol *arg = new_symbol (child_die, NULL, cu);
6832
f1078f66
DJ
6833 if (arg != NULL)
6834 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6835 }
6836
c906108c
SS
6837 child_die = sibling_die (child_die);
6838 }
6839
34eaf542
TT
6840 /* Attach template arguments to type. */
6841 if (! VEC_empty (symbolp, template_args))
6842 {
6843 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6844 TYPE_N_TEMPLATE_ARGUMENTS (type)
6845 = VEC_length (symbolp, template_args);
6846 TYPE_TEMPLATE_ARGUMENTS (type)
6847 = obstack_alloc (&objfile->objfile_obstack,
6848 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6849 * sizeof (struct symbol *)));
6850 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6851 VEC_address (symbolp, template_args),
6852 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6853 * sizeof (struct symbol *)));
6854 VEC_free (symbolp, template_args);
6855 }
6856
c906108c
SS
6857 /* Attach fields and member functions to the type. */
6858 if (fi.nfields)
e7c27a73 6859 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6860 if (fi.nfnfields)
6861 {
e7c27a73 6862 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6863
c5aa993b 6864 /* Get the type which refers to the base class (possibly this
c906108c 6865 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6866 class from the DW_AT_containing_type attribute. This use of
6867 DW_AT_containing_type is a GNU extension. */
c906108c 6868
e142c38c 6869 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6870 {
e7c27a73 6871 struct type *t = die_containing_type (die, cu);
c906108c
SS
6872
6873 TYPE_VPTR_BASETYPE (type) = t;
6874 if (type == t)
6875 {
c906108c
SS
6876 int i;
6877
6878 /* Our own class provides vtbl ptr. */
6879 for (i = TYPE_NFIELDS (t) - 1;
6880 i >= TYPE_N_BASECLASSES (t);
6881 --i)
6882 {
6883 char *fieldname = TYPE_FIELD_NAME (t, i);
6884
1168df01 6885 if (is_vtable_name (fieldname, cu))
c906108c
SS
6886 {
6887 TYPE_VPTR_FIELDNO (type) = i;
6888 break;
6889 }
6890 }
6891
6892 /* Complain if virtual function table field not found. */
6893 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6894 complaint (&symfile_complaints,
e2e0b3e5 6895 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6896 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6897 "");
c906108c
SS
6898 }
6899 else
6900 {
6901 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6902 }
6903 }
f6235d4c
EZ
6904 else if (cu->producer
6905 && strncmp (cu->producer,
6906 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6907 {
6908 /* The IBM XLC compiler does not provide direct indication
6909 of the containing type, but the vtable pointer is
6910 always named __vfp. */
6911
6912 int i;
6913
6914 for (i = TYPE_NFIELDS (type) - 1;
6915 i >= TYPE_N_BASECLASSES (type);
6916 --i)
6917 {
6918 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6919 {
6920 TYPE_VPTR_FIELDNO (type) = i;
6921 TYPE_VPTR_BASETYPE (type) = type;
6922 break;
6923 }
6924 }
6925 }
c906108c 6926 }
98751a41
JK
6927
6928 /* Copy fi.typedef_field_list linked list elements content into the
6929 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6930 if (fi.typedef_field_list)
6931 {
6932 int i = fi.typedef_field_list_count;
6933
a0d7a4ff 6934 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6935 TYPE_TYPEDEF_FIELD_ARRAY (type)
6936 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6937 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6938
6939 /* Reverse the list order to keep the debug info elements order. */
6940 while (--i >= 0)
6941 {
6942 struct typedef_field *dest, *src;
6e70227d 6943
98751a41
JK
6944 dest = &TYPE_TYPEDEF_FIELD (type, i);
6945 src = &fi.typedef_field_list->field;
6946 fi.typedef_field_list = fi.typedef_field_list->next;
6947 *dest = *src;
6948 }
6949 }
c767944b
DJ
6950
6951 do_cleanups (back_to);
c906108c 6952 }
63d06c5c 6953
0b92b5bb
TT
6954 quirk_gcc_member_function_pointer (type, cu->objfile);
6955
90aeadfc
DC
6956 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6957 snapshots) has been known to create a die giving a declaration
6958 for a class that has, as a child, a die giving a definition for a
6959 nested class. So we have to process our children even if the
6960 current die is a declaration. Normally, of course, a declaration
6961 won't have any children at all. */
134d01f1 6962
90aeadfc
DC
6963 while (child_die != NULL && child_die->tag)
6964 {
6965 if (child_die->tag == DW_TAG_member
6966 || child_die->tag == DW_TAG_variable
34eaf542
TT
6967 || child_die->tag == DW_TAG_inheritance
6968 || child_die->tag == DW_TAG_template_value_param
6969 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6970 {
90aeadfc 6971 /* Do nothing. */
134d01f1 6972 }
90aeadfc
DC
6973 else
6974 process_die (child_die, cu);
134d01f1 6975
90aeadfc 6976 child_die = sibling_die (child_die);
134d01f1
DJ
6977 }
6978
fa4028e9
JB
6979 /* Do not consider external references. According to the DWARF standard,
6980 these DIEs are identified by the fact that they have no byte_size
6981 attribute, and a declaration attribute. */
6982 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6983 || !die_is_declaration (die, cu))
c767944b 6984 new_symbol (die, type, cu);
134d01f1
DJ
6985}
6986
6987/* Given a DW_AT_enumeration_type die, set its type. We do not
6988 complete the type's fields yet, or create any symbols. */
c906108c 6989
f792889a 6990static struct type *
134d01f1 6991read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6992{
e7c27a73 6993 struct objfile *objfile = cu->objfile;
c906108c 6994 struct type *type;
c906108c 6995 struct attribute *attr;
0114d602 6996 const char *name;
134d01f1 6997
348e048f
DE
6998 /* If the definition of this type lives in .debug_types, read that type.
6999 Don't follow DW_AT_specification though, that will take us back up
7000 the chain and we want to go down. */
7001 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7002 if (attr)
7003 {
7004 struct dwarf2_cu *type_cu = cu;
7005 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7006
348e048f 7007 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7008
7009 /* TYPE_CU may not be the same as CU.
7010 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7011 return set_die_type (die, type, cu);
7012 }
7013
c906108c
SS
7014 type = alloc_type (objfile);
7015
7016 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7017 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7018 if (name != NULL)
0114d602 7019 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7020
e142c38c 7021 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7022 if (attr)
7023 {
7024 TYPE_LENGTH (type) = DW_UNSND (attr);
7025 }
7026 else
7027 {
7028 TYPE_LENGTH (type) = 0;
7029 }
7030
137033e9
JB
7031 /* The enumeration DIE can be incomplete. In Ada, any type can be
7032 declared as private in the package spec, and then defined only
7033 inside the package body. Such types are known as Taft Amendment
7034 Types. When another package uses such a type, an incomplete DIE
7035 may be generated by the compiler. */
02eb380e 7036 if (die_is_declaration (die, cu))
876cecd0 7037 TYPE_STUB (type) = 1;
02eb380e 7038
f792889a 7039 return set_die_type (die, type, cu);
134d01f1
DJ
7040}
7041
7042/* Given a pointer to a die which begins an enumeration, process all
7043 the dies that define the members of the enumeration, and create the
7044 symbol for the enumeration type.
7045
7046 NOTE: We reverse the order of the element list. */
7047
7048static void
7049process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7050{
f792889a 7051 struct type *this_type;
134d01f1 7052
f792889a
DJ
7053 this_type = get_die_type (die, cu);
7054 if (this_type == NULL)
7055 this_type = read_enumeration_type (die, cu);
9dc481d3 7056
639d11d3 7057 if (die->child != NULL)
c906108c 7058 {
9dc481d3
DE
7059 struct die_info *child_die;
7060 struct symbol *sym;
7061 struct field *fields = NULL;
7062 int num_fields = 0;
7063 int unsigned_enum = 1;
7064 char *name;
7065
639d11d3 7066 child_die = die->child;
c906108c
SS
7067 while (child_die && child_die->tag)
7068 {
7069 if (child_die->tag != DW_TAG_enumerator)
7070 {
e7c27a73 7071 process_die (child_die, cu);
c906108c
SS
7072 }
7073 else
7074 {
39cbfefa
DJ
7075 name = dwarf2_name (child_die, cu);
7076 if (name)
c906108c 7077 {
f792889a 7078 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7079 if (SYMBOL_VALUE (sym) < 0)
7080 unsigned_enum = 0;
7081
7082 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7083 {
7084 fields = (struct field *)
7085 xrealloc (fields,
7086 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7087 * sizeof (struct field));
c906108c
SS
7088 }
7089
3567439c 7090 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7091 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7092 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7093 FIELD_BITSIZE (fields[num_fields]) = 0;
7094
7095 num_fields++;
7096 }
7097 }
7098
7099 child_die = sibling_die (child_die);
7100 }
7101
7102 if (num_fields)
7103 {
f792889a
DJ
7104 TYPE_NFIELDS (this_type) = num_fields;
7105 TYPE_FIELDS (this_type) = (struct field *)
7106 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7107 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7108 sizeof (struct field) * num_fields);
b8c9b27d 7109 xfree (fields);
c906108c
SS
7110 }
7111 if (unsigned_enum)
876cecd0 7112 TYPE_UNSIGNED (this_type) = 1;
c906108c 7113 }
134d01f1 7114
f792889a 7115 new_symbol (die, this_type, cu);
c906108c
SS
7116}
7117
7118/* Extract all information from a DW_TAG_array_type DIE and put it in
7119 the DIE's type field. For now, this only handles one dimensional
7120 arrays. */
7121
f792889a 7122static struct type *
e7c27a73 7123read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7124{
e7c27a73 7125 struct objfile *objfile = cu->objfile;
c906108c 7126 struct die_info *child_die;
7e314c57 7127 struct type *type;
c906108c
SS
7128 struct type *element_type, *range_type, *index_type;
7129 struct type **range_types = NULL;
7130 struct attribute *attr;
7131 int ndim = 0;
7132 struct cleanup *back_to;
39cbfefa 7133 char *name;
c906108c 7134
e7c27a73 7135 element_type = die_type (die, cu);
c906108c 7136
7e314c57
JK
7137 /* The die_type call above may have already set the type for this DIE. */
7138 type = get_die_type (die, cu);
7139 if (type)
7140 return type;
7141
c906108c
SS
7142 /* Irix 6.2 native cc creates array types without children for
7143 arrays with unspecified length. */
639d11d3 7144 if (die->child == NULL)
c906108c 7145 {
46bf5051 7146 index_type = objfile_type (objfile)->builtin_int;
c906108c 7147 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7148 type = create_array_type (NULL, element_type, range_type);
7149 return set_die_type (die, type, cu);
c906108c
SS
7150 }
7151
7152 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7153 child_die = die->child;
c906108c
SS
7154 while (child_die && child_die->tag)
7155 {
7156 if (child_die->tag == DW_TAG_subrange_type)
7157 {
f792889a 7158 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7159
f792889a 7160 if (child_type != NULL)
a02abb62
JB
7161 {
7162 /* The range type was succesfully read. Save it for
7163 the array type creation. */
7164 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7165 {
7166 range_types = (struct type **)
7167 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7168 * sizeof (struct type *));
7169 if (ndim == 0)
7170 make_cleanup (free_current_contents, &range_types);
7171 }
f792889a 7172 range_types[ndim++] = child_type;
a02abb62 7173 }
c906108c
SS
7174 }
7175 child_die = sibling_die (child_die);
7176 }
7177
7178 /* Dwarf2 dimensions are output from left to right, create the
7179 necessary array types in backwards order. */
7ca2d3a3 7180
c906108c 7181 type = element_type;
7ca2d3a3
DL
7182
7183 if (read_array_order (die, cu) == DW_ORD_col_major)
7184 {
7185 int i = 0;
9a619af0 7186
7ca2d3a3
DL
7187 while (i < ndim)
7188 type = create_array_type (NULL, type, range_types[i++]);
7189 }
7190 else
7191 {
7192 while (ndim-- > 0)
7193 type = create_array_type (NULL, type, range_types[ndim]);
7194 }
c906108c 7195
f5f8a009
EZ
7196 /* Understand Dwarf2 support for vector types (like they occur on
7197 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7198 array type. This is not part of the Dwarf2/3 standard yet, but a
7199 custom vendor extension. The main difference between a regular
7200 array and the vector variant is that vectors are passed by value
7201 to functions. */
e142c38c 7202 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7203 if (attr)
ea37ba09 7204 make_vector_type (type);
f5f8a009 7205
39cbfefa
DJ
7206 name = dwarf2_name (die, cu);
7207 if (name)
7208 TYPE_NAME (type) = name;
6e70227d 7209
7e314c57
JK
7210 /* Install the type in the die. */
7211 set_die_type (die, type, cu);
7212
7213 /* set_die_type should be already done. */
b4ba55a1
JB
7214 set_descriptive_type (type, die, cu);
7215
c906108c
SS
7216 do_cleanups (back_to);
7217
7e314c57 7218 return type;
c906108c
SS
7219}
7220
7ca2d3a3 7221static enum dwarf_array_dim_ordering
6e70227d 7222read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7223{
7224 struct attribute *attr;
7225
7226 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7227
7228 if (attr) return DW_SND (attr);
7229
7230 /*
7231 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7232 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7233 laid out as per normal fortran.
7234
6e70227d 7235 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7236 version checking.
7237 */
7238
905e0470
PM
7239 if (cu->language == language_fortran
7240 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7241 {
7242 return DW_ORD_row_major;
7243 }
7244
6e70227d 7245 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7246 {
7247 case array_column_major:
7248 return DW_ORD_col_major;
7249 case array_row_major:
7250 default:
7251 return DW_ORD_row_major;
7252 };
7253}
7254
72019c9c
GM
7255/* Extract all information from a DW_TAG_set_type DIE and put it in
7256 the DIE's type field. */
7257
f792889a 7258static struct type *
72019c9c
GM
7259read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7260{
7e314c57
JK
7261 struct type *domain_type, *set_type;
7262 struct attribute *attr;
f792889a 7263
7e314c57
JK
7264 domain_type = die_type (die, cu);
7265
7266 /* The die_type call above may have already set the type for this DIE. */
7267 set_type = get_die_type (die, cu);
7268 if (set_type)
7269 return set_type;
7270
7271 set_type = create_set_type (NULL, domain_type);
7272
7273 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7274 if (attr)
7275 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7276
f792889a 7277 return set_die_type (die, set_type, cu);
72019c9c 7278}
7ca2d3a3 7279
c906108c
SS
7280/* First cut: install each common block member as a global variable. */
7281
7282static void
e7c27a73 7283read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7284{
7285 struct die_info *child_die;
7286 struct attribute *attr;
7287 struct symbol *sym;
7288 CORE_ADDR base = (CORE_ADDR) 0;
7289
e142c38c 7290 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7291 if (attr)
7292 {
8e19ed76
PS
7293 /* Support the .debug_loc offsets */
7294 if (attr_form_is_block (attr))
7295 {
e7c27a73 7296 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7297 }
3690dd37 7298 else if (attr_form_is_section_offset (attr))
8e19ed76 7299 {
4d3c2250 7300 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7301 }
7302 else
7303 {
4d3c2250
KB
7304 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7305 "common block member");
8e19ed76 7306 }
c906108c 7307 }
639d11d3 7308 if (die->child != NULL)
c906108c 7309 {
639d11d3 7310 child_die = die->child;
c906108c
SS
7311 while (child_die && child_die->tag)
7312 {
e7c27a73 7313 sym = new_symbol (child_die, NULL, cu);
e142c38c 7314 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7315 if (sym != NULL && attr != NULL)
c906108c 7316 {
d4b96c9a
JK
7317 CORE_ADDR byte_offset = 0;
7318
7319 if (attr_form_is_section_offset (attr))
7320 dwarf2_complex_location_expr_complaint ();
7321 else if (attr_form_is_constant (attr))
7322 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7323 else if (attr_form_is_block (attr))
7324 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7325 else
7326 dwarf2_complex_location_expr_complaint ();
7327
7328 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7329 add_symbol_to_list (sym, &global_symbols);
7330 }
7331 child_die = sibling_die (child_die);
7332 }
7333 }
7334}
7335
0114d602 7336/* Create a type for a C++ namespace. */
d9fa45fe 7337
0114d602
DJ
7338static struct type *
7339read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7340{
e7c27a73 7341 struct objfile *objfile = cu->objfile;
0114d602 7342 const char *previous_prefix, *name;
9219021c 7343 int is_anonymous;
0114d602
DJ
7344 struct type *type;
7345
7346 /* For extensions, reuse the type of the original namespace. */
7347 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7348 {
7349 struct die_info *ext_die;
7350 struct dwarf2_cu *ext_cu = cu;
9a619af0 7351
0114d602
DJ
7352 ext_die = dwarf2_extension (die, &ext_cu);
7353 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7354
7355 /* EXT_CU may not be the same as CU.
7356 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7357 return set_die_type (die, type, cu);
7358 }
9219021c 7359
e142c38c 7360 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7361
7362 /* Now build the name of the current namespace. */
7363
0114d602
DJ
7364 previous_prefix = determine_prefix (die, cu);
7365 if (previous_prefix[0] != '\0')
7366 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7367 previous_prefix, name, 0, cu);
0114d602
DJ
7368
7369 /* Create the type. */
7370 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7371 objfile);
7372 TYPE_NAME (type) = (char *) name;
7373 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7374
60531b24 7375 return set_die_type (die, type, cu);
0114d602
DJ
7376}
7377
7378/* Read a C++ namespace. */
7379
7380static void
7381read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7382{
7383 struct objfile *objfile = cu->objfile;
7384 const char *name;
7385 int is_anonymous;
9219021c 7386
5c4e30ca
DC
7387 /* Add a symbol associated to this if we haven't seen the namespace
7388 before. Also, add a using directive if it's an anonymous
7389 namespace. */
9219021c 7390
f2f0e013 7391 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7392 {
7393 struct type *type;
7394
0114d602 7395 type = read_type_die (die, cu);
e7c27a73 7396 new_symbol (die, type, cu);
5c4e30ca 7397
0114d602 7398 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7399 if (is_anonymous)
0114d602
DJ
7400 {
7401 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7402
c0cc3a76 7403 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7404 NULL, &objfile->objfile_obstack);
0114d602 7405 }
5c4e30ca 7406 }
9219021c 7407
639d11d3 7408 if (die->child != NULL)
d9fa45fe 7409 {
639d11d3 7410 struct die_info *child_die = die->child;
6e70227d 7411
d9fa45fe
DC
7412 while (child_die && child_die->tag)
7413 {
e7c27a73 7414 process_die (child_die, cu);
d9fa45fe
DC
7415 child_die = sibling_die (child_die);
7416 }
7417 }
38d518c9
EZ
7418}
7419
f55ee35c
JK
7420/* Read a Fortran module as type. This DIE can be only a declaration used for
7421 imported module. Still we need that type as local Fortran "use ... only"
7422 declaration imports depend on the created type in determine_prefix. */
7423
7424static struct type *
7425read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7426{
7427 struct objfile *objfile = cu->objfile;
7428 char *module_name;
7429 struct type *type;
7430
7431 module_name = dwarf2_name (die, cu);
7432 if (!module_name)
7433 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7434 die->offset);
7435 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7436
7437 /* determine_prefix uses TYPE_TAG_NAME. */
7438 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7439
7440 return set_die_type (die, type, cu);
7441}
7442
5d7cb8df
JK
7443/* Read a Fortran module. */
7444
7445static void
7446read_module (struct die_info *die, struct dwarf2_cu *cu)
7447{
7448 struct die_info *child_die = die->child;
7449
5d7cb8df
JK
7450 while (child_die && child_die->tag)
7451 {
7452 process_die (child_die, cu);
7453 child_die = sibling_die (child_die);
7454 }
7455}
7456
38d518c9
EZ
7457/* Return the name of the namespace represented by DIE. Set
7458 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7459 namespace. */
7460
7461static const char *
e142c38c 7462namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7463{
7464 struct die_info *current_die;
7465 const char *name = NULL;
7466
7467 /* Loop through the extensions until we find a name. */
7468
7469 for (current_die = die;
7470 current_die != NULL;
f2f0e013 7471 current_die = dwarf2_extension (die, &cu))
38d518c9 7472 {
e142c38c 7473 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7474 if (name != NULL)
7475 break;
7476 }
7477
7478 /* Is it an anonymous namespace? */
7479
7480 *is_anonymous = (name == NULL);
7481 if (*is_anonymous)
7482 name = "(anonymous namespace)";
7483
7484 return name;
d9fa45fe
DC
7485}
7486
c906108c
SS
7487/* Extract all information from a DW_TAG_pointer_type DIE and add to
7488 the user defined type vector. */
7489
f792889a 7490static struct type *
e7c27a73 7491read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7492{
5e2b427d 7493 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7494 struct comp_unit_head *cu_header = &cu->header;
c906108c 7495 struct type *type;
8b2dbe47
KB
7496 struct attribute *attr_byte_size;
7497 struct attribute *attr_address_class;
7498 int byte_size, addr_class;
7e314c57
JK
7499 struct type *target_type;
7500
7501 target_type = die_type (die, cu);
c906108c 7502
7e314c57
JK
7503 /* The die_type call above may have already set the type for this DIE. */
7504 type = get_die_type (die, cu);
7505 if (type)
7506 return type;
7507
7508 type = lookup_pointer_type (target_type);
8b2dbe47 7509
e142c38c 7510 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7511 if (attr_byte_size)
7512 byte_size = DW_UNSND (attr_byte_size);
c906108c 7513 else
8b2dbe47
KB
7514 byte_size = cu_header->addr_size;
7515
e142c38c 7516 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7517 if (attr_address_class)
7518 addr_class = DW_UNSND (attr_address_class);
7519 else
7520 addr_class = DW_ADDR_none;
7521
7522 /* If the pointer size or address class is different than the
7523 default, create a type variant marked as such and set the
7524 length accordingly. */
7525 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7526 {
5e2b427d 7527 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7528 {
7529 int type_flags;
7530
849957d9 7531 type_flags = gdbarch_address_class_type_flags
5e2b427d 7532 (gdbarch, byte_size, addr_class);
876cecd0
TT
7533 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7534 == 0);
8b2dbe47
KB
7535 type = make_type_with_address_space (type, type_flags);
7536 }
7537 else if (TYPE_LENGTH (type) != byte_size)
7538 {
e2e0b3e5 7539 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7540 }
6e70227d 7541 else
9a619af0
MS
7542 {
7543 /* Should we also complain about unhandled address classes? */
7544 }
c906108c 7545 }
8b2dbe47
KB
7546
7547 TYPE_LENGTH (type) = byte_size;
f792889a 7548 return set_die_type (die, type, cu);
c906108c
SS
7549}
7550
7551/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7552 the user defined type vector. */
7553
f792889a 7554static struct type *
e7c27a73 7555read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7556{
7557 struct type *type;
7558 struct type *to_type;
7559 struct type *domain;
7560
e7c27a73
DJ
7561 to_type = die_type (die, cu);
7562 domain = die_containing_type (die, cu);
0d5de010 7563
7e314c57
JK
7564 /* The calls above may have already set the type for this DIE. */
7565 type = get_die_type (die, cu);
7566 if (type)
7567 return type;
7568
0d5de010
DJ
7569 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7570 type = lookup_methodptr_type (to_type);
7571 else
7572 type = lookup_memberptr_type (to_type, domain);
c906108c 7573
f792889a 7574 return set_die_type (die, type, cu);
c906108c
SS
7575}
7576
7577/* Extract all information from a DW_TAG_reference_type DIE and add to
7578 the user defined type vector. */
7579
f792889a 7580static struct type *
e7c27a73 7581read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7582{
e7c27a73 7583 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7584 struct type *type, *target_type;
c906108c
SS
7585 struct attribute *attr;
7586
7e314c57
JK
7587 target_type = die_type (die, cu);
7588
7589 /* The die_type call above may have already set the type for this DIE. */
7590 type = get_die_type (die, cu);
7591 if (type)
7592 return type;
7593
7594 type = lookup_reference_type (target_type);
e142c38c 7595 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7596 if (attr)
7597 {
7598 TYPE_LENGTH (type) = DW_UNSND (attr);
7599 }
7600 else
7601 {
107d2387 7602 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7603 }
f792889a 7604 return set_die_type (die, type, cu);
c906108c
SS
7605}
7606
f792889a 7607static struct type *
e7c27a73 7608read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7609{
f792889a 7610 struct type *base_type, *cv_type;
c906108c 7611
e7c27a73 7612 base_type = die_type (die, cu);
7e314c57
JK
7613
7614 /* The die_type call above may have already set the type for this DIE. */
7615 cv_type = get_die_type (die, cu);
7616 if (cv_type)
7617 return cv_type;
7618
f792889a
DJ
7619 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7620 return set_die_type (die, cv_type, cu);
c906108c
SS
7621}
7622
f792889a 7623static struct type *
e7c27a73 7624read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7625{
f792889a 7626 struct type *base_type, *cv_type;
c906108c 7627
e7c27a73 7628 base_type = die_type (die, cu);
7e314c57
JK
7629
7630 /* The die_type call above may have already set the type for this DIE. */
7631 cv_type = get_die_type (die, cu);
7632 if (cv_type)
7633 return cv_type;
7634
f792889a
DJ
7635 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7636 return set_die_type (die, cv_type, cu);
c906108c
SS
7637}
7638
7639/* Extract all information from a DW_TAG_string_type DIE and add to
7640 the user defined type vector. It isn't really a user defined type,
7641 but it behaves like one, with other DIE's using an AT_user_def_type
7642 attribute to reference it. */
7643
f792889a 7644static struct type *
e7c27a73 7645read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7646{
e7c27a73 7647 struct objfile *objfile = cu->objfile;
3b7538c0 7648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7649 struct type *type, *range_type, *index_type, *char_type;
7650 struct attribute *attr;
7651 unsigned int length;
7652
e142c38c 7653 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7654 if (attr)
7655 {
7656 length = DW_UNSND (attr);
7657 }
7658 else
7659 {
b21b22e0 7660 /* check for the DW_AT_byte_size attribute */
e142c38c 7661 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7662 if (attr)
7663 {
7664 length = DW_UNSND (attr);
7665 }
7666 else
7667 {
7668 length = 1;
7669 }
c906108c 7670 }
6ccb9162 7671
46bf5051 7672 index_type = objfile_type (objfile)->builtin_int;
c906108c 7673 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7674 char_type = language_string_char_type (cu->language_defn, gdbarch);
7675 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7676
f792889a 7677 return set_die_type (die, type, cu);
c906108c
SS
7678}
7679
7680/* Handle DIES due to C code like:
7681
7682 struct foo
c5aa993b
JM
7683 {
7684 int (*funcp)(int a, long l);
7685 int b;
7686 };
c906108c
SS
7687
7688 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7689 */
c906108c 7690
f792889a 7691static struct type *
e7c27a73 7692read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7693{
7694 struct type *type; /* Type that this function returns */
7695 struct type *ftype; /* Function that returns above type */
7696 struct attribute *attr;
7697
e7c27a73 7698 type = die_type (die, cu);
7e314c57
JK
7699
7700 /* The die_type call above may have already set the type for this DIE. */
7701 ftype = get_die_type (die, cu);
7702 if (ftype)
7703 return ftype;
7704
0c8b41f1 7705 ftype = lookup_function_type (type);
c906108c 7706
5b8101ae 7707 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7708 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7709 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7710 || cu->language == language_cplus
5b8101ae
PM
7711 || cu->language == language_java
7712 || cu->language == language_pascal)
876cecd0 7713 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7714 else if (producer_is_realview (cu->producer))
7715 /* RealView does not emit DW_AT_prototyped. We can not
7716 distinguish prototyped and unprototyped functions; default to
7717 prototyped, since that is more common in modern code (and
7718 RealView warns about unprototyped functions). */
7719 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7720
c055b101
CV
7721 /* Store the calling convention in the type if it's available in
7722 the subroutine die. Otherwise set the calling convention to
7723 the default value DW_CC_normal. */
7724 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7725 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7726
7727 /* We need to add the subroutine type to the die immediately so
7728 we don't infinitely recurse when dealing with parameters
7729 declared as the same subroutine type. */
7730 set_die_type (die, ftype, cu);
6e70227d 7731
639d11d3 7732 if (die->child != NULL)
c906108c 7733 {
8072405b 7734 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7735 struct die_info *child_die;
8072405b 7736 int nparams, iparams;
c906108c
SS
7737
7738 /* Count the number of parameters.
7739 FIXME: GDB currently ignores vararg functions, but knows about
7740 vararg member functions. */
8072405b 7741 nparams = 0;
639d11d3 7742 child_die = die->child;
c906108c
SS
7743 while (child_die && child_die->tag)
7744 {
7745 if (child_die->tag == DW_TAG_formal_parameter)
7746 nparams++;
7747 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7748 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7749 child_die = sibling_die (child_die);
7750 }
7751
7752 /* Allocate storage for parameters and fill them in. */
7753 TYPE_NFIELDS (ftype) = nparams;
7754 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7755 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7756
8072405b
JK
7757 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7758 even if we error out during the parameters reading below. */
7759 for (iparams = 0; iparams < nparams; iparams++)
7760 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7761
7762 iparams = 0;
639d11d3 7763 child_die = die->child;
c906108c
SS
7764 while (child_die && child_die->tag)
7765 {
7766 if (child_die->tag == DW_TAG_formal_parameter)
7767 {
3ce3b1ba
PA
7768 struct type *arg_type;
7769
7770 /* DWARF version 2 has no clean way to discern C++
7771 static and non-static member functions. G++ helps
7772 GDB by marking the first parameter for non-static
7773 member functions (which is the this pointer) as
7774 artificial. We pass this information to
7775 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7776
7777 DWARF version 3 added DW_AT_object_pointer, which GCC
7778 4.5 does not yet generate. */
e142c38c 7779 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7780 if (attr)
7781 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7782 else
418835cc
KS
7783 {
7784 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7785
7786 /* GCC/43521: In java, the formal parameter
7787 "this" is sometimes not marked with DW_AT_artificial. */
7788 if (cu->language == language_java)
7789 {
7790 const char *name = dwarf2_name (child_die, cu);
9a619af0 7791
418835cc
KS
7792 if (name && !strcmp (name, "this"))
7793 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7794 }
7795 }
3ce3b1ba
PA
7796 arg_type = die_type (child_die, cu);
7797
7798 /* RealView does not mark THIS as const, which the testsuite
7799 expects. GCC marks THIS as const in method definitions,
7800 but not in the class specifications (GCC PR 43053). */
7801 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7802 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7803 {
7804 int is_this = 0;
7805 struct dwarf2_cu *arg_cu = cu;
7806 const char *name = dwarf2_name (child_die, cu);
7807
7808 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7809 if (attr)
7810 {
7811 /* If the compiler emits this, use it. */
7812 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7813 is_this = 1;
7814 }
7815 else if (name && strcmp (name, "this") == 0)
7816 /* Function definitions will have the argument names. */
7817 is_this = 1;
7818 else if (name == NULL && iparams == 0)
7819 /* Declarations may not have the names, so like
7820 elsewhere in GDB, assume an artificial first
7821 argument is "this". */
7822 is_this = 1;
7823
7824 if (is_this)
7825 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7826 arg_type, 0);
7827 }
7828
7829 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7830 iparams++;
7831 }
7832 child_die = sibling_die (child_die);
7833 }
7834 }
7835
76c10ea2 7836 return ftype;
c906108c
SS
7837}
7838
f792889a 7839static struct type *
e7c27a73 7840read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7841{
e7c27a73 7842 struct objfile *objfile = cu->objfile;
0114d602 7843 const char *name = NULL;
f792889a 7844 struct type *this_type;
c906108c 7845
94af9270 7846 name = dwarf2_full_name (NULL, die, cu);
f792889a 7847 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7848 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7849 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7850 set_die_type (die, this_type, cu);
7851 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7852 return this_type;
c906108c
SS
7853}
7854
7855/* Find a representation of a given base type and install
7856 it in the TYPE field of the die. */
7857
f792889a 7858static struct type *
e7c27a73 7859read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7860{
e7c27a73 7861 struct objfile *objfile = cu->objfile;
c906108c
SS
7862 struct type *type;
7863 struct attribute *attr;
7864 int encoding = 0, size = 0;
39cbfefa 7865 char *name;
6ccb9162
UW
7866 enum type_code code = TYPE_CODE_INT;
7867 int type_flags = 0;
7868 struct type *target_type = NULL;
c906108c 7869
e142c38c 7870 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7871 if (attr)
7872 {
7873 encoding = DW_UNSND (attr);
7874 }
e142c38c 7875 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7876 if (attr)
7877 {
7878 size = DW_UNSND (attr);
7879 }
39cbfefa 7880 name = dwarf2_name (die, cu);
6ccb9162 7881 if (!name)
c906108c 7882 {
6ccb9162
UW
7883 complaint (&symfile_complaints,
7884 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7885 }
6ccb9162
UW
7886
7887 switch (encoding)
c906108c 7888 {
6ccb9162
UW
7889 case DW_ATE_address:
7890 /* Turn DW_ATE_address into a void * pointer. */
7891 code = TYPE_CODE_PTR;
7892 type_flags |= TYPE_FLAG_UNSIGNED;
7893 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7894 break;
7895 case DW_ATE_boolean:
7896 code = TYPE_CODE_BOOL;
7897 type_flags |= TYPE_FLAG_UNSIGNED;
7898 break;
7899 case DW_ATE_complex_float:
7900 code = TYPE_CODE_COMPLEX;
7901 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7902 break;
7903 case DW_ATE_decimal_float:
7904 code = TYPE_CODE_DECFLOAT;
7905 break;
7906 case DW_ATE_float:
7907 code = TYPE_CODE_FLT;
7908 break;
7909 case DW_ATE_signed:
7910 break;
7911 case DW_ATE_unsigned:
7912 type_flags |= TYPE_FLAG_UNSIGNED;
7913 break;
7914 case DW_ATE_signed_char:
6e70227d 7915 if (cu->language == language_ada || cu->language == language_m2
868a0084 7916 || cu->language == language_pascal)
6ccb9162
UW
7917 code = TYPE_CODE_CHAR;
7918 break;
7919 case DW_ATE_unsigned_char:
868a0084
PM
7920 if (cu->language == language_ada || cu->language == language_m2
7921 || cu->language == language_pascal)
6ccb9162
UW
7922 code = TYPE_CODE_CHAR;
7923 type_flags |= TYPE_FLAG_UNSIGNED;
7924 break;
75079b2b
TT
7925 case DW_ATE_UTF:
7926 /* We just treat this as an integer and then recognize the
7927 type by name elsewhere. */
7928 break;
7929
6ccb9162
UW
7930 default:
7931 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7932 dwarf_type_encoding_name (encoding));
7933 break;
c906108c 7934 }
6ccb9162 7935
0114d602
DJ
7936 type = init_type (code, size, type_flags, NULL, objfile);
7937 TYPE_NAME (type) = name;
6ccb9162
UW
7938 TYPE_TARGET_TYPE (type) = target_type;
7939
0114d602 7940 if (name && strcmp (name, "char") == 0)
876cecd0 7941 TYPE_NOSIGN (type) = 1;
0114d602 7942
f792889a 7943 return set_die_type (die, type, cu);
c906108c
SS
7944}
7945
a02abb62
JB
7946/* Read the given DW_AT_subrange DIE. */
7947
f792889a 7948static struct type *
a02abb62
JB
7949read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7950{
5e2b427d 7951 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7952 struct type *base_type;
7953 struct type *range_type;
7954 struct attribute *attr;
43bbcdc2
PH
7955 LONGEST low = 0;
7956 LONGEST high = -1;
39cbfefa 7957 char *name;
43bbcdc2 7958 LONGEST negative_mask;
e77813c8 7959
a02abb62 7960 base_type = die_type (die, cu);
953ac07e
JK
7961 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7962 check_typedef (base_type);
a02abb62 7963
7e314c57
JK
7964 /* The die_type call above may have already set the type for this DIE. */
7965 range_type = get_die_type (die, cu);
7966 if (range_type)
7967 return range_type;
7968
e142c38c 7969 if (cu->language == language_fortran)
6e70227d 7970 {
a02abb62
JB
7971 /* FORTRAN implies a lower bound of 1, if not given. */
7972 low = 1;
7973 }
7974
dd5e6932
DJ
7975 /* FIXME: For variable sized arrays either of these could be
7976 a variable rather than a constant value. We'll allow it,
7977 but we don't know how to handle it. */
e142c38c 7978 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7979 if (attr)
7980 low = dwarf2_get_attr_constant_value (attr, 0);
7981
e142c38c 7982 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7983 if (attr)
6e70227d 7984 {
e77813c8 7985 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7986 {
7987 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7988 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7989 FIXME: GDB does not yet know how to handle dynamic
7990 arrays properly, treat them as arrays with unspecified
7991 length for now.
7992
7993 FIXME: jimb/2003-09-22: GDB does not really know
7994 how to handle arrays of unspecified length
7995 either; we just represent them as zero-length
7996 arrays. Choose an appropriate upper bound given
7997 the lower bound we've computed above. */
7998 high = low - 1;
7999 }
8000 else
8001 high = dwarf2_get_attr_constant_value (attr, 1);
8002 }
e77813c8
PM
8003 else
8004 {
8005 attr = dwarf2_attr (die, DW_AT_count, cu);
8006 if (attr)
8007 {
8008 int count = dwarf2_get_attr_constant_value (attr, 1);
8009 high = low + count - 1;
8010 }
8011 }
8012
8013 /* Dwarf-2 specifications explicitly allows to create subrange types
8014 without specifying a base type.
8015 In that case, the base type must be set to the type of
8016 the lower bound, upper bound or count, in that order, if any of these
8017 three attributes references an object that has a type.
8018 If no base type is found, the Dwarf-2 specifications say that
8019 a signed integer type of size equal to the size of an address should
8020 be used.
8021 For the following C code: `extern char gdb_int [];'
8022 GCC produces an empty range DIE.
8023 FIXME: muller/2010-05-28: Possible references to object for low bound,
8024 high bound or count are not yet handled by this code.
8025 */
8026 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8027 {
8028 struct objfile *objfile = cu->objfile;
8029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8030 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8031 struct type *int_type = objfile_type (objfile)->builtin_int;
8032
8033 /* Test "int", "long int", and "long long int" objfile types,
8034 and select the first one having a size above or equal to the
8035 architecture address size. */
8036 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8037 base_type = int_type;
8038 else
8039 {
8040 int_type = objfile_type (objfile)->builtin_long;
8041 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8042 base_type = int_type;
8043 else
8044 {
8045 int_type = objfile_type (objfile)->builtin_long_long;
8046 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8047 base_type = int_type;
8048 }
8049 }
8050 }
a02abb62 8051
6e70227d 8052 negative_mask =
43bbcdc2
PH
8053 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8054 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8055 low |= negative_mask;
8056 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8057 high |= negative_mask;
8058
a02abb62
JB
8059 range_type = create_range_type (NULL, base_type, low, high);
8060
bbb0eef6
JK
8061 /* Mark arrays with dynamic length at least as an array of unspecified
8062 length. GDB could check the boundary but before it gets implemented at
8063 least allow accessing the array elements. */
8064 if (attr && attr->form == DW_FORM_block1)
8065 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8066
39cbfefa
DJ
8067 name = dwarf2_name (die, cu);
8068 if (name)
8069 TYPE_NAME (range_type) = name;
6e70227d 8070
e142c38c 8071 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8072 if (attr)
8073 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8074
7e314c57
JK
8075 set_die_type (die, range_type, cu);
8076
8077 /* set_die_type should be already done. */
b4ba55a1
JB
8078 set_descriptive_type (range_type, die, cu);
8079
7e314c57 8080 return range_type;
a02abb62 8081}
6e70227d 8082
f792889a 8083static struct type *
81a17f79
JB
8084read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8085{
8086 struct type *type;
81a17f79 8087
81a17f79
JB
8088 /* For now, we only support the C meaning of an unspecified type: void. */
8089
0114d602
DJ
8090 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8091 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8092
f792889a 8093 return set_die_type (die, type, cu);
81a17f79 8094}
a02abb62 8095
51545339
DJ
8096/* Trivial hash function for die_info: the hash value of a DIE
8097 is its offset in .debug_info for this objfile. */
8098
8099static hashval_t
8100die_hash (const void *item)
8101{
8102 const struct die_info *die = item;
9a619af0 8103
51545339
DJ
8104 return die->offset;
8105}
8106
8107/* Trivial comparison function for die_info structures: two DIEs
8108 are equal if they have the same offset. */
8109
8110static int
8111die_eq (const void *item_lhs, const void *item_rhs)
8112{
8113 const struct die_info *die_lhs = item_lhs;
8114 const struct die_info *die_rhs = item_rhs;
9a619af0 8115
51545339
DJ
8116 return die_lhs->offset == die_rhs->offset;
8117}
8118
c906108c
SS
8119/* Read a whole compilation unit into a linked list of dies. */
8120
f9aca02d 8121static struct die_info *
93311388 8122read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8123{
93311388 8124 struct die_reader_specs reader_specs;
98bfdba5 8125 int read_abbrevs = 0;
1d9ec526 8126 struct cleanup *back_to = NULL;
98bfdba5
PA
8127 struct die_info *die;
8128
8129 if (cu->dwarf2_abbrevs == NULL)
8130 {
8131 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8132 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8133 read_abbrevs = 1;
8134 }
93311388 8135
348e048f 8136 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8137 cu->die_hash
8138 = htab_create_alloc_ex (cu->header.length / 12,
8139 die_hash,
8140 die_eq,
8141 NULL,
8142 &cu->comp_unit_obstack,
8143 hashtab_obstack_allocate,
8144 dummy_obstack_deallocate);
8145
93311388
DE
8146 init_cu_die_reader (&reader_specs, cu);
8147
98bfdba5
PA
8148 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8149
8150 if (read_abbrevs)
8151 do_cleanups (back_to);
8152
8153 return die;
639d11d3
DC
8154}
8155
d97bc12b
DE
8156/* Main entry point for reading a DIE and all children.
8157 Read the DIE and dump it if requested. */
8158
8159static struct die_info *
93311388
DE
8160read_die_and_children (const struct die_reader_specs *reader,
8161 gdb_byte *info_ptr,
d97bc12b
DE
8162 gdb_byte **new_info_ptr,
8163 struct die_info *parent)
8164{
93311388 8165 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8166 new_info_ptr, parent);
8167
8168 if (dwarf2_die_debug)
8169 {
348e048f
DE
8170 fprintf_unfiltered (gdb_stdlog,
8171 "\nRead die from %s of %s:\n",
8172 reader->buffer == dwarf2_per_objfile->info.buffer
8173 ? ".debug_info"
8174 : reader->buffer == dwarf2_per_objfile->types.buffer
8175 ? ".debug_types"
8176 : "unknown section",
8177 reader->abfd->filename);
d97bc12b
DE
8178 dump_die (result, dwarf2_die_debug);
8179 }
8180
8181 return result;
8182}
8183
639d11d3
DC
8184/* Read a single die and all its descendents. Set the die's sibling
8185 field to NULL; set other fields in the die correctly, and set all
8186 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8187 location of the info_ptr after reading all of those dies. PARENT
8188 is the parent of the die in question. */
8189
8190static struct die_info *
93311388
DE
8191read_die_and_children_1 (const struct die_reader_specs *reader,
8192 gdb_byte *info_ptr,
d97bc12b
DE
8193 gdb_byte **new_info_ptr,
8194 struct die_info *parent)
639d11d3
DC
8195{
8196 struct die_info *die;
fe1b8b76 8197 gdb_byte *cur_ptr;
639d11d3
DC
8198 int has_children;
8199
93311388 8200 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8201 if (die == NULL)
8202 {
8203 *new_info_ptr = cur_ptr;
8204 return NULL;
8205 }
93311388 8206 store_in_ref_table (die, reader->cu);
639d11d3
DC
8207
8208 if (has_children)
348e048f 8209 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8210 else
8211 {
8212 die->child = NULL;
8213 *new_info_ptr = cur_ptr;
8214 }
8215
8216 die->sibling = NULL;
8217 die->parent = parent;
8218 return die;
8219}
8220
8221/* Read a die, all of its descendents, and all of its siblings; set
8222 all of the fields of all of the dies correctly. Arguments are as
8223 in read_die_and_children. */
8224
8225static struct die_info *
93311388
DE
8226read_die_and_siblings (const struct die_reader_specs *reader,
8227 gdb_byte *info_ptr,
fe1b8b76 8228 gdb_byte **new_info_ptr,
639d11d3
DC
8229 struct die_info *parent)
8230{
8231 struct die_info *first_die, *last_sibling;
fe1b8b76 8232 gdb_byte *cur_ptr;
639d11d3 8233
c906108c 8234 cur_ptr = info_ptr;
639d11d3
DC
8235 first_die = last_sibling = NULL;
8236
8237 while (1)
c906108c 8238 {
639d11d3 8239 struct die_info *die
93311388 8240 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8241
1d325ec1 8242 if (die == NULL)
c906108c 8243 {
639d11d3
DC
8244 *new_info_ptr = cur_ptr;
8245 return first_die;
c906108c 8246 }
1d325ec1
DJ
8247
8248 if (!first_die)
8249 first_die = die;
c906108c 8250 else
1d325ec1
DJ
8251 last_sibling->sibling = die;
8252
8253 last_sibling = die;
c906108c 8254 }
c906108c
SS
8255}
8256
93311388
DE
8257/* Read the die from the .debug_info section buffer. Set DIEP to
8258 point to a newly allocated die with its information, except for its
8259 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8260 whether the die has children or not. */
8261
8262static gdb_byte *
8263read_full_die (const struct die_reader_specs *reader,
8264 struct die_info **diep, gdb_byte *info_ptr,
8265 int *has_children)
8266{
8267 unsigned int abbrev_number, bytes_read, i, offset;
8268 struct abbrev_info *abbrev;
8269 struct die_info *die;
8270 struct dwarf2_cu *cu = reader->cu;
8271 bfd *abfd = reader->abfd;
8272
8273 offset = info_ptr - reader->buffer;
8274 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8275 info_ptr += bytes_read;
8276 if (!abbrev_number)
8277 {
8278 *diep = NULL;
8279 *has_children = 0;
8280 return info_ptr;
8281 }
8282
8283 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8284 if (!abbrev)
348e048f
DE
8285 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8286 abbrev_number,
8287 bfd_get_filename (abfd));
8288
93311388
DE
8289 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8290 die->offset = offset;
8291 die->tag = abbrev->tag;
8292 die->abbrev = abbrev_number;
8293
8294 die->num_attrs = abbrev->num_attrs;
8295
8296 for (i = 0; i < abbrev->num_attrs; ++i)
8297 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8298 abfd, info_ptr, cu);
8299
8300 *diep = die;
8301 *has_children = abbrev->has_children;
8302 return info_ptr;
8303}
8304
c906108c
SS
8305/* In DWARF version 2, the description of the debugging information is
8306 stored in a separate .debug_abbrev section. Before we read any
8307 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8308 in a hash table. This function also sets flags in CU describing
8309 the data found in the abbrev table. */
c906108c
SS
8310
8311static void
e7c27a73 8312dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8313{
e7c27a73 8314 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8315 gdb_byte *abbrev_ptr;
c906108c
SS
8316 struct abbrev_info *cur_abbrev;
8317 unsigned int abbrev_number, bytes_read, abbrev_name;
8318 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8319 struct attr_abbrev *cur_attrs;
8320 unsigned int allocated_attrs;
c906108c 8321
57349743 8322 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8323 obstack_init (&cu->abbrev_obstack);
8324 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8325 (ABBREV_HASH_SIZE
8326 * sizeof (struct abbrev_info *)));
8327 memset (cu->dwarf2_abbrevs, 0,
8328 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8329
be391dca
TT
8330 dwarf2_read_section (dwarf2_per_objfile->objfile,
8331 &dwarf2_per_objfile->abbrev);
dce234bc 8332 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8333 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8334 abbrev_ptr += bytes_read;
8335
f3dd6933
DJ
8336 allocated_attrs = ATTR_ALLOC_CHUNK;
8337 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8338
c906108c
SS
8339 /* loop until we reach an abbrev number of 0 */
8340 while (abbrev_number)
8341 {
f3dd6933 8342 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8343
8344 /* read in abbrev header */
8345 cur_abbrev->number = abbrev_number;
8346 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8347 abbrev_ptr += bytes_read;
8348 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8349 abbrev_ptr += 1;
8350
72bf9492
DJ
8351 if (cur_abbrev->tag == DW_TAG_namespace)
8352 cu->has_namespace_info = 1;
8353
c906108c
SS
8354 /* now read in declarations */
8355 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8356 abbrev_ptr += bytes_read;
8357 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8358 abbrev_ptr += bytes_read;
8359 while (abbrev_name)
8360 {
f3dd6933 8361 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8362 {
f3dd6933
DJ
8363 allocated_attrs += ATTR_ALLOC_CHUNK;
8364 cur_attrs
8365 = xrealloc (cur_attrs, (allocated_attrs
8366 * sizeof (struct attr_abbrev)));
c906108c 8367 }
ae038cb0
DJ
8368
8369 /* Record whether this compilation unit might have
8370 inter-compilation-unit references. If we don't know what form
8371 this attribute will have, then it might potentially be a
8372 DW_FORM_ref_addr, so we conservatively expect inter-CU
8373 references. */
8374
8375 if (abbrev_form == DW_FORM_ref_addr
8376 || abbrev_form == DW_FORM_indirect)
8377 cu->has_form_ref_addr = 1;
8378
f3dd6933
DJ
8379 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8380 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8381 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8382 abbrev_ptr += bytes_read;
8383 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8384 abbrev_ptr += bytes_read;
8385 }
8386
f3dd6933
DJ
8387 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8388 (cur_abbrev->num_attrs
8389 * sizeof (struct attr_abbrev)));
8390 memcpy (cur_abbrev->attrs, cur_attrs,
8391 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8392
c906108c 8393 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8394 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8395 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8396
8397 /* Get next abbreviation.
8398 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8399 always properly terminated with an abbrev number of 0.
8400 Exit loop if we encounter an abbreviation which we have
8401 already read (which means we are about to read the abbreviations
8402 for the next compile unit) or if the end of the abbreviation
8403 table is reached. */
dce234bc
PP
8404 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8405 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8406 break;
8407 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8408 abbrev_ptr += bytes_read;
e7c27a73 8409 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8410 break;
8411 }
f3dd6933
DJ
8412
8413 xfree (cur_attrs);
c906108c
SS
8414}
8415
f3dd6933 8416/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8417
c906108c 8418static void
f3dd6933 8419dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8420{
f3dd6933 8421 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8422
f3dd6933
DJ
8423 obstack_free (&cu->abbrev_obstack, NULL);
8424 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8425}
8426
8427/* Lookup an abbrev_info structure in the abbrev hash table. */
8428
8429static struct abbrev_info *
e7c27a73 8430dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8431{
8432 unsigned int hash_number;
8433 struct abbrev_info *abbrev;
8434
8435 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8436 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8437
8438 while (abbrev)
8439 {
8440 if (abbrev->number == number)
8441 return abbrev;
8442 else
8443 abbrev = abbrev->next;
8444 }
8445 return NULL;
8446}
8447
72bf9492
DJ
8448/* Returns nonzero if TAG represents a type that we might generate a partial
8449 symbol for. */
8450
8451static int
8452is_type_tag_for_partial (int tag)
8453{
8454 switch (tag)
8455 {
8456#if 0
8457 /* Some types that would be reasonable to generate partial symbols for,
8458 that we don't at present. */
8459 case DW_TAG_array_type:
8460 case DW_TAG_file_type:
8461 case DW_TAG_ptr_to_member_type:
8462 case DW_TAG_set_type:
8463 case DW_TAG_string_type:
8464 case DW_TAG_subroutine_type:
8465#endif
8466 case DW_TAG_base_type:
8467 case DW_TAG_class_type:
680b30c7 8468 case DW_TAG_interface_type:
72bf9492
DJ
8469 case DW_TAG_enumeration_type:
8470 case DW_TAG_structure_type:
8471 case DW_TAG_subrange_type:
8472 case DW_TAG_typedef:
8473 case DW_TAG_union_type:
8474 return 1;
8475 default:
8476 return 0;
8477 }
8478}
8479
8480/* Load all DIEs that are interesting for partial symbols into memory. */
8481
8482static struct partial_die_info *
93311388
DE
8483load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8484 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8485{
8486 struct partial_die_info *part_die;
8487 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8488 struct abbrev_info *abbrev;
8489 unsigned int bytes_read;
5afb4e99 8490 unsigned int load_all = 0;
72bf9492
DJ
8491
8492 int nesting_level = 1;
8493
8494 parent_die = NULL;
8495 last_die = NULL;
8496
5afb4e99
DJ
8497 if (cu->per_cu && cu->per_cu->load_all_dies)
8498 load_all = 1;
8499
72bf9492
DJ
8500 cu->partial_dies
8501 = htab_create_alloc_ex (cu->header.length / 12,
8502 partial_die_hash,
8503 partial_die_eq,
8504 NULL,
8505 &cu->comp_unit_obstack,
8506 hashtab_obstack_allocate,
8507 dummy_obstack_deallocate);
8508
8509 part_die = obstack_alloc (&cu->comp_unit_obstack,
8510 sizeof (struct partial_die_info));
8511
8512 while (1)
8513 {
8514 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8515
8516 /* A NULL abbrev means the end of a series of children. */
8517 if (abbrev == NULL)
8518 {
8519 if (--nesting_level == 0)
8520 {
8521 /* PART_DIE was probably the last thing allocated on the
8522 comp_unit_obstack, so we could call obstack_free
8523 here. We don't do that because the waste is small,
8524 and will be cleaned up when we're done with this
8525 compilation unit. This way, we're also more robust
8526 against other users of the comp_unit_obstack. */
8527 return first_die;
8528 }
8529 info_ptr += bytes_read;
8530 last_die = parent_die;
8531 parent_die = parent_die->die_parent;
8532 continue;
8533 }
8534
98bfdba5
PA
8535 /* Check for template arguments. We never save these; if
8536 they're seen, we just mark the parent, and go on our way. */
8537 if (parent_die != NULL
8538 && cu->language == language_cplus
8539 && (abbrev->tag == DW_TAG_template_type_param
8540 || abbrev->tag == DW_TAG_template_value_param))
8541 {
8542 parent_die->has_template_arguments = 1;
8543
8544 if (!load_all)
8545 {
8546 /* We don't need a partial DIE for the template argument. */
8547 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8548 cu);
8549 continue;
8550 }
8551 }
8552
8553 /* We only recurse into subprograms looking for template arguments.
8554 Skip their other children. */
8555 if (!load_all
8556 && cu->language == language_cplus
8557 && parent_die != NULL
8558 && parent_die->tag == DW_TAG_subprogram)
8559 {
8560 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8561 continue;
8562 }
8563
5afb4e99
DJ
8564 /* Check whether this DIE is interesting enough to save. Normally
8565 we would not be interested in members here, but there may be
8566 later variables referencing them via DW_AT_specification (for
8567 static members). */
8568 if (!load_all
8569 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8570 && abbrev->tag != DW_TAG_enumerator
8571 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8572 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8573 && abbrev->tag != DW_TAG_variable
5afb4e99 8574 && abbrev->tag != DW_TAG_namespace
f55ee35c 8575 && abbrev->tag != DW_TAG_module
5afb4e99 8576 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8577 {
8578 /* Otherwise we skip to the next sibling, if any. */
93311388 8579 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8580 continue;
8581 }
8582
93311388
DE
8583 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8584 buffer, info_ptr, cu);
72bf9492
DJ
8585
8586 /* This two-pass algorithm for processing partial symbols has a
8587 high cost in cache pressure. Thus, handle some simple cases
8588 here which cover the majority of C partial symbols. DIEs
8589 which neither have specification tags in them, nor could have
8590 specification tags elsewhere pointing at them, can simply be
8591 processed and discarded.
8592
8593 This segment is also optional; scan_partial_symbols and
8594 add_partial_symbol will handle these DIEs if we chain
8595 them in normally. When compilers which do not emit large
8596 quantities of duplicate debug information are more common,
8597 this code can probably be removed. */
8598
8599 /* Any complete simple types at the top level (pretty much all
8600 of them, for a language without namespaces), can be processed
8601 directly. */
8602 if (parent_die == NULL
8603 && part_die->has_specification == 0
8604 && part_die->is_declaration == 0
8605 && (part_die->tag == DW_TAG_typedef
8606 || part_die->tag == DW_TAG_base_type
8607 || part_die->tag == DW_TAG_subrange_type))
8608 {
8609 if (building_psymtab && part_die->name != NULL)
04a679b8 8610 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8611 VAR_DOMAIN, LOC_TYPEDEF,
8612 &cu->objfile->static_psymbols,
8613 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8614 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8615 continue;
8616 }
8617
8618 /* If we're at the second level, and we're an enumerator, and
8619 our parent has no specification (meaning possibly lives in a
8620 namespace elsewhere), then we can add the partial symbol now
8621 instead of queueing it. */
8622 if (part_die->tag == DW_TAG_enumerator
8623 && parent_die != NULL
8624 && parent_die->die_parent == NULL
8625 && parent_die->tag == DW_TAG_enumeration_type
8626 && parent_die->has_specification == 0)
8627 {
8628 if (part_die->name == NULL)
e2e0b3e5 8629 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8630 else if (building_psymtab)
04a679b8 8631 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8632 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8633 (cu->language == language_cplus
8634 || cu->language == language_java)
72bf9492
DJ
8635 ? &cu->objfile->global_psymbols
8636 : &cu->objfile->static_psymbols,
8637 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8638
93311388 8639 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8640 continue;
8641 }
8642
8643 /* We'll save this DIE so link it in. */
8644 part_die->die_parent = parent_die;
8645 part_die->die_sibling = NULL;
8646 part_die->die_child = NULL;
8647
8648 if (last_die && last_die == parent_die)
8649 last_die->die_child = part_die;
8650 else if (last_die)
8651 last_die->die_sibling = part_die;
8652
8653 last_die = part_die;
8654
8655 if (first_die == NULL)
8656 first_die = part_die;
8657
8658 /* Maybe add the DIE to the hash table. Not all DIEs that we
8659 find interesting need to be in the hash table, because we
8660 also have the parent/sibling/child chains; only those that we
8661 might refer to by offset later during partial symbol reading.
8662
8663 For now this means things that might have be the target of a
8664 DW_AT_specification, DW_AT_abstract_origin, or
8665 DW_AT_extension. DW_AT_extension will refer only to
8666 namespaces; DW_AT_abstract_origin refers to functions (and
8667 many things under the function DIE, but we do not recurse
8668 into function DIEs during partial symbol reading) and
8669 possibly variables as well; DW_AT_specification refers to
8670 declarations. Declarations ought to have the DW_AT_declaration
8671 flag. It happens that GCC forgets to put it in sometimes, but
8672 only for functions, not for types.
8673
8674 Adding more things than necessary to the hash table is harmless
8675 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8676 wasted time in find_partial_die, when we reread the compilation
8677 unit with load_all_dies set. */
72bf9492 8678
5afb4e99
DJ
8679 if (load_all
8680 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8681 || abbrev->tag == DW_TAG_variable
8682 || abbrev->tag == DW_TAG_namespace
8683 || part_die->is_declaration)
8684 {
8685 void **slot;
8686
8687 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8688 part_die->offset, INSERT);
8689 *slot = part_die;
8690 }
8691
8692 part_die = obstack_alloc (&cu->comp_unit_obstack,
8693 sizeof (struct partial_die_info));
8694
8695 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8696 we have no reason to follow the children of structures; for other
98bfdba5
PA
8697 languages we have to, so that we can get at method physnames
8698 to infer fully qualified class names, for DW_AT_specification,
8699 and for C++ template arguments. For C++, we also look one level
8700 inside functions to find template arguments (if the name of the
8701 function does not already contain the template arguments).
bc30ff58
JB
8702
8703 For Ada, we need to scan the children of subprograms and lexical
8704 blocks as well because Ada allows the definition of nested
8705 entities that could be interesting for the debugger, such as
8706 nested subprograms for instance. */
72bf9492 8707 if (last_die->has_children
5afb4e99
DJ
8708 && (load_all
8709 || last_die->tag == DW_TAG_namespace
f55ee35c 8710 || last_die->tag == DW_TAG_module
72bf9492 8711 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8712 || (cu->language == language_cplus
8713 && last_die->tag == DW_TAG_subprogram
8714 && (last_die->name == NULL
8715 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8716 || (cu->language != language_c
8717 && (last_die->tag == DW_TAG_class_type
680b30c7 8718 || last_die->tag == DW_TAG_interface_type
72bf9492 8719 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8720 || last_die->tag == DW_TAG_union_type))
8721 || (cu->language == language_ada
8722 && (last_die->tag == DW_TAG_subprogram
8723 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8724 {
8725 nesting_level++;
8726 parent_die = last_die;
8727 continue;
8728 }
8729
8730 /* Otherwise we skip to the next sibling, if any. */
93311388 8731 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8732
8733 /* Back to the top, do it again. */
8734 }
8735}
8736
c906108c
SS
8737/* Read a minimal amount of information into the minimal die structure. */
8738
fe1b8b76 8739static gdb_byte *
72bf9492
DJ
8740read_partial_die (struct partial_die_info *part_die,
8741 struct abbrev_info *abbrev,
8742 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8743 gdb_byte *buffer, gdb_byte *info_ptr,
8744 struct dwarf2_cu *cu)
c906108c 8745{
fa238c03 8746 unsigned int i;
c906108c 8747 struct attribute attr;
c5aa993b 8748 int has_low_pc_attr = 0;
c906108c
SS
8749 int has_high_pc_attr = 0;
8750
72bf9492 8751 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8752
93311388 8753 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8754
8755 info_ptr += abbrev_len;
8756
8757 if (abbrev == NULL)
8758 return info_ptr;
8759
c906108c
SS
8760 part_die->tag = abbrev->tag;
8761 part_die->has_children = abbrev->has_children;
c906108c
SS
8762
8763 for (i = 0; i < abbrev->num_attrs; ++i)
8764 {
e7c27a73 8765 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8766
8767 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8768 partial symbol table. */
c906108c
SS
8769 switch (attr.name)
8770 {
8771 case DW_AT_name:
71c25dea
TT
8772 switch (part_die->tag)
8773 {
8774 case DW_TAG_compile_unit:
348e048f 8775 case DW_TAG_type_unit:
71c25dea
TT
8776 /* Compilation units have a DW_AT_name that is a filename, not
8777 a source language identifier. */
8778 case DW_TAG_enumeration_type:
8779 case DW_TAG_enumerator:
8780 /* These tags always have simple identifiers already; no need
8781 to canonicalize them. */
8782 part_die->name = DW_STRING (&attr);
8783 break;
8784 default:
8785 part_die->name
8786 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8787 &cu->objfile->objfile_obstack);
71c25dea
TT
8788 break;
8789 }
c906108c 8790 break;
31ef98ae 8791 case DW_AT_linkage_name:
c906108c 8792 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8793 /* Note that both forms of linkage name might appear. We
8794 assume they will be the same, and we only store the last
8795 one we see. */
94af9270
KS
8796 if (cu->language == language_ada)
8797 part_die->name = DW_STRING (&attr);
c906108c
SS
8798 break;
8799 case DW_AT_low_pc:
8800 has_low_pc_attr = 1;
8801 part_die->lowpc = DW_ADDR (&attr);
8802 break;
8803 case DW_AT_high_pc:
8804 has_high_pc_attr = 1;
8805 part_die->highpc = DW_ADDR (&attr);
8806 break;
8807 case DW_AT_location:
8e19ed76
PS
8808 /* Support the .debug_loc offsets */
8809 if (attr_form_is_block (&attr))
8810 {
8811 part_die->locdesc = DW_BLOCK (&attr);
8812 }
3690dd37 8813 else if (attr_form_is_section_offset (&attr))
8e19ed76 8814 {
4d3c2250 8815 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8816 }
8817 else
8818 {
4d3c2250
KB
8819 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8820 "partial symbol information");
8e19ed76 8821 }
c906108c 8822 break;
c906108c
SS
8823 case DW_AT_external:
8824 part_die->is_external = DW_UNSND (&attr);
8825 break;
8826 case DW_AT_declaration:
8827 part_die->is_declaration = DW_UNSND (&attr);
8828 break;
8829 case DW_AT_type:
8830 part_die->has_type = 1;
8831 break;
8832 case DW_AT_abstract_origin:
8833 case DW_AT_specification:
72bf9492
DJ
8834 case DW_AT_extension:
8835 part_die->has_specification = 1;
c764a876 8836 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8837 break;
8838 case DW_AT_sibling:
8839 /* Ignore absolute siblings, they might point outside of
8840 the current compile unit. */
8841 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8842 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8843 else
93311388 8844 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8845 break;
fa4028e9
JB
8846 case DW_AT_byte_size:
8847 part_die->has_byte_size = 1;
8848 break;
68511cec
CES
8849 case DW_AT_calling_convention:
8850 /* DWARF doesn't provide a way to identify a program's source-level
8851 entry point. DW_AT_calling_convention attributes are only meant
8852 to describe functions' calling conventions.
8853
8854 However, because it's a necessary piece of information in
8855 Fortran, and because DW_CC_program is the only piece of debugging
8856 information whose definition refers to a 'main program' at all,
8857 several compilers have begun marking Fortran main programs with
8858 DW_CC_program --- even when those functions use the standard
8859 calling conventions.
8860
8861 So until DWARF specifies a way to provide this information and
8862 compilers pick up the new representation, we'll support this
8863 practice. */
8864 if (DW_UNSND (&attr) == DW_CC_program
8865 && cu->language == language_fortran)
8866 set_main_name (part_die->name);
8867 break;
c906108c
SS
8868 default:
8869 break;
8870 }
8871 }
8872
c906108c
SS
8873 /* When using the GNU linker, .gnu.linkonce. sections are used to
8874 eliminate duplicate copies of functions and vtables and such.
8875 The linker will arbitrarily choose one and discard the others.
8876 The AT_*_pc values for such functions refer to local labels in
8877 these sections. If the section from that file was discarded, the
8878 labels are not in the output, so the relocs get a value of 0.
8879 If this is a discarded function, mark the pc bounds as invalid,
8880 so that GDB will ignore it. */
8881 if (has_low_pc_attr && has_high_pc_attr
8882 && part_die->lowpc < part_die->highpc
8883 && (part_die->lowpc != 0
72dca2f5 8884 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8885 part_die->has_pc_info = 1;
85cbf3d3 8886
c906108c
SS
8887 return info_ptr;
8888}
8889
72bf9492
DJ
8890/* Find a cached partial DIE at OFFSET in CU. */
8891
8892static struct partial_die_info *
c764a876 8893find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8894{
8895 struct partial_die_info *lookup_die = NULL;
8896 struct partial_die_info part_die;
8897
8898 part_die.offset = offset;
8899 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8900
72bf9492
DJ
8901 return lookup_die;
8902}
8903
348e048f
DE
8904/* Find a partial DIE at OFFSET, which may or may not be in CU,
8905 except in the case of .debug_types DIEs which do not reference
8906 outside their CU (they do however referencing other types via
8907 DW_FORM_sig8). */
72bf9492
DJ
8908
8909static struct partial_die_info *
c764a876 8910find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8911{
5afb4e99
DJ
8912 struct dwarf2_per_cu_data *per_cu = NULL;
8913 struct partial_die_info *pd = NULL;
72bf9492 8914
348e048f
DE
8915 if (cu->per_cu->from_debug_types)
8916 {
8917 pd = find_partial_die_in_comp_unit (offset, cu);
8918 if (pd != NULL)
8919 return pd;
8920 goto not_found;
8921 }
8922
45452591 8923 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8924 {
8925 pd = find_partial_die_in_comp_unit (offset, cu);
8926 if (pd != NULL)
8927 return pd;
8928 }
72bf9492 8929
ae038cb0
DJ
8930 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8931
98bfdba5
PA
8932 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8933 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8934
8935 per_cu->cu->last_used = 0;
5afb4e99
DJ
8936 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8937
8938 if (pd == NULL && per_cu->load_all_dies == 0)
8939 {
8940 struct cleanup *back_to;
8941 struct partial_die_info comp_unit_die;
8942 struct abbrev_info *abbrev;
8943 unsigned int bytes_read;
8944 char *info_ptr;
8945
8946 per_cu->load_all_dies = 1;
8947
8948 /* Re-read the DIEs. */
8949 back_to = make_cleanup (null_cleanup, 0);
8950 if (per_cu->cu->dwarf2_abbrevs == NULL)
8951 {
8952 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8953 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8954 }
dce234bc 8955 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8956 + per_cu->cu->header.offset
8957 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8958 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8959 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8960 per_cu->cu->objfile->obfd,
8961 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8962 per_cu->cu);
8963 if (comp_unit_die.has_children)
93311388
DE
8964 load_partial_dies (per_cu->cu->objfile->obfd,
8965 dwarf2_per_objfile->info.buffer, info_ptr,
8966 0, per_cu->cu);
5afb4e99
DJ
8967 do_cleanups (back_to);
8968
8969 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8970 }
8971
348e048f
DE
8972 not_found:
8973
5afb4e99
DJ
8974 if (pd == NULL)
8975 internal_error (__FILE__, __LINE__,
c764a876 8976 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8977 offset, bfd_get_filename (cu->objfile->obfd));
8978 return pd;
72bf9492
DJ
8979}
8980
8981/* Adjust PART_DIE before generating a symbol for it. This function
8982 may set the is_external flag or change the DIE's name. */
8983
8984static void
8985fixup_partial_die (struct partial_die_info *part_die,
8986 struct dwarf2_cu *cu)
8987{
8988 /* If we found a reference attribute and the DIE has no name, try
8989 to find a name in the referred to DIE. */
8990
8991 if (part_die->name == NULL && part_die->has_specification)
8992 {
8993 struct partial_die_info *spec_die;
72bf9492 8994
10b3939b 8995 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8996
10b3939b 8997 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8998
8999 if (spec_die->name)
9000 {
9001 part_die->name = spec_die->name;
9002
9003 /* Copy DW_AT_external attribute if it is set. */
9004 if (spec_die->is_external)
9005 part_die->is_external = spec_die->is_external;
9006 }
9007 }
9008
9009 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9010
9011 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9012 part_die->name = "(anonymous namespace)";
9013
9014 if (part_die->tag == DW_TAG_structure_type
9015 || part_die->tag == DW_TAG_class_type
9016 || part_die->tag == DW_TAG_union_type)
9017 guess_structure_name (part_die, cu);
9018}
9019
a8329558 9020/* Read an attribute value described by an attribute form. */
c906108c 9021
fe1b8b76 9022static gdb_byte *
a8329558 9023read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9024 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9025 struct dwarf2_cu *cu)
c906108c 9026{
e7c27a73 9027 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9028 unsigned int bytes_read;
9029 struct dwarf_block *blk;
9030
a8329558
KW
9031 attr->form = form;
9032 switch (form)
c906108c 9033 {
c906108c 9034 case DW_FORM_ref_addr:
ae411497
TT
9035 if (cu->header.version == 2)
9036 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9037 else
9038 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9039 info_ptr += bytes_read;
9040 break;
9041 case DW_FORM_addr:
e7c27a73 9042 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9043 info_ptr += bytes_read;
c906108c
SS
9044 break;
9045 case DW_FORM_block2:
7b5a2f43 9046 blk = dwarf_alloc_block (cu);
c906108c
SS
9047 blk->size = read_2_bytes (abfd, info_ptr);
9048 info_ptr += 2;
9049 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9050 info_ptr += blk->size;
9051 DW_BLOCK (attr) = blk;
9052 break;
9053 case DW_FORM_block4:
7b5a2f43 9054 blk = dwarf_alloc_block (cu);
c906108c
SS
9055 blk->size = read_4_bytes (abfd, info_ptr);
9056 info_ptr += 4;
9057 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9058 info_ptr += blk->size;
9059 DW_BLOCK (attr) = blk;
9060 break;
9061 case DW_FORM_data2:
9062 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9063 info_ptr += 2;
9064 break;
9065 case DW_FORM_data4:
9066 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9067 info_ptr += 4;
9068 break;
9069 case DW_FORM_data8:
9070 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9071 info_ptr += 8;
9072 break;
2dc7f7b3
TT
9073 case DW_FORM_sec_offset:
9074 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9075 info_ptr += bytes_read;
9076 break;
c906108c 9077 case DW_FORM_string:
9b1c24c8 9078 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9079 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9080 info_ptr += bytes_read;
9081 break;
4bdf3d34
JJ
9082 case DW_FORM_strp:
9083 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9084 &bytes_read);
8285870a 9085 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9086 info_ptr += bytes_read;
9087 break;
2dc7f7b3 9088 case DW_FORM_exprloc:
c906108c 9089 case DW_FORM_block:
7b5a2f43 9090 blk = dwarf_alloc_block (cu);
c906108c
SS
9091 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9092 info_ptr += bytes_read;
9093 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9094 info_ptr += blk->size;
9095 DW_BLOCK (attr) = blk;
9096 break;
9097 case DW_FORM_block1:
7b5a2f43 9098 blk = dwarf_alloc_block (cu);
c906108c
SS
9099 blk->size = read_1_byte (abfd, info_ptr);
9100 info_ptr += 1;
9101 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9102 info_ptr += blk->size;
9103 DW_BLOCK (attr) = blk;
9104 break;
9105 case DW_FORM_data1:
9106 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9107 info_ptr += 1;
9108 break;
9109 case DW_FORM_flag:
9110 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9111 info_ptr += 1;
9112 break;
2dc7f7b3
TT
9113 case DW_FORM_flag_present:
9114 DW_UNSND (attr) = 1;
9115 break;
c906108c
SS
9116 case DW_FORM_sdata:
9117 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9118 info_ptr += bytes_read;
9119 break;
9120 case DW_FORM_udata:
9121 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9122 info_ptr += bytes_read;
9123 break;
9124 case DW_FORM_ref1:
10b3939b 9125 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9126 info_ptr += 1;
9127 break;
9128 case DW_FORM_ref2:
10b3939b 9129 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9130 info_ptr += 2;
9131 break;
9132 case DW_FORM_ref4:
10b3939b 9133 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9134 info_ptr += 4;
9135 break;
613e1657 9136 case DW_FORM_ref8:
10b3939b 9137 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9138 info_ptr += 8;
9139 break;
348e048f
DE
9140 case DW_FORM_sig8:
9141 /* Convert the signature to something we can record in DW_UNSND
9142 for later lookup.
9143 NOTE: This is NULL if the type wasn't found. */
9144 DW_SIGNATURED_TYPE (attr) =
9145 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9146 info_ptr += 8;
9147 break;
c906108c 9148 case DW_FORM_ref_udata:
10b3939b
DJ
9149 DW_ADDR (attr) = (cu->header.offset
9150 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9151 info_ptr += bytes_read;
9152 break;
c906108c 9153 case DW_FORM_indirect:
a8329558
KW
9154 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9155 info_ptr += bytes_read;
e7c27a73 9156 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9157 break;
c906108c 9158 default:
8a3fe4f8 9159 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9160 dwarf_form_name (form),
9161 bfd_get_filename (abfd));
c906108c 9162 }
28e94949
JB
9163
9164 /* We have seen instances where the compiler tried to emit a byte
9165 size attribute of -1 which ended up being encoded as an unsigned
9166 0xffffffff. Although 0xffffffff is technically a valid size value,
9167 an object of this size seems pretty unlikely so we can relatively
9168 safely treat these cases as if the size attribute was invalid and
9169 treat them as zero by default. */
9170 if (attr->name == DW_AT_byte_size
9171 && form == DW_FORM_data4
9172 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9173 {
9174 complaint
9175 (&symfile_complaints,
43bbcdc2
PH
9176 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9177 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9178 DW_UNSND (attr) = 0;
9179 }
28e94949 9180
c906108c
SS
9181 return info_ptr;
9182}
9183
a8329558
KW
9184/* Read an attribute described by an abbreviated attribute. */
9185
fe1b8b76 9186static gdb_byte *
a8329558 9187read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9188 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9189{
9190 attr->name = abbrev->name;
e7c27a73 9191 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9192}
9193
c906108c
SS
9194/* read dwarf information from a buffer */
9195
9196static unsigned int
fe1b8b76 9197read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9198{
fe1b8b76 9199 return bfd_get_8 (abfd, buf);
c906108c
SS
9200}
9201
9202static int
fe1b8b76 9203read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9204{
fe1b8b76 9205 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9206}
9207
9208static unsigned int
fe1b8b76 9209read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9210{
fe1b8b76 9211 return bfd_get_16 (abfd, buf);
c906108c
SS
9212}
9213
9214static int
fe1b8b76 9215read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9216{
fe1b8b76 9217 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9218}
9219
9220static unsigned int
fe1b8b76 9221read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9222{
fe1b8b76 9223 return bfd_get_32 (abfd, buf);
c906108c
SS
9224}
9225
9226static int
fe1b8b76 9227read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9228{
fe1b8b76 9229 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9230}
9231
93311388 9232static ULONGEST
fe1b8b76 9233read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9234{
fe1b8b76 9235 return bfd_get_64 (abfd, buf);
c906108c
SS
9236}
9237
9238static CORE_ADDR
fe1b8b76 9239read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9240 unsigned int *bytes_read)
c906108c 9241{
e7c27a73 9242 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9243 CORE_ADDR retval = 0;
9244
107d2387 9245 if (cu_header->signed_addr_p)
c906108c 9246 {
107d2387
AC
9247 switch (cu_header->addr_size)
9248 {
9249 case 2:
fe1b8b76 9250 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9251 break;
9252 case 4:
fe1b8b76 9253 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9254 break;
9255 case 8:
fe1b8b76 9256 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9257 break;
9258 default:
8e65ff28 9259 internal_error (__FILE__, __LINE__,
e2e0b3e5 9260 _("read_address: bad switch, signed [in module %s]"),
659b0389 9261 bfd_get_filename (abfd));
107d2387
AC
9262 }
9263 }
9264 else
9265 {
9266 switch (cu_header->addr_size)
9267 {
9268 case 2:
fe1b8b76 9269 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9270 break;
9271 case 4:
fe1b8b76 9272 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9273 break;
9274 case 8:
fe1b8b76 9275 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9276 break;
9277 default:
8e65ff28 9278 internal_error (__FILE__, __LINE__,
e2e0b3e5 9279 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9280 bfd_get_filename (abfd));
107d2387 9281 }
c906108c 9282 }
64367e0a 9283
107d2387
AC
9284 *bytes_read = cu_header->addr_size;
9285 return retval;
c906108c
SS
9286}
9287
f7ef9339 9288/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9289 specification allows the initial length to take up either 4 bytes
9290 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9291 bytes describe the length and all offsets will be 8 bytes in length
9292 instead of 4.
9293
f7ef9339
KB
9294 An older, non-standard 64-bit format is also handled by this
9295 function. The older format in question stores the initial length
9296 as an 8-byte quantity without an escape value. Lengths greater
9297 than 2^32 aren't very common which means that the initial 4 bytes
9298 is almost always zero. Since a length value of zero doesn't make
9299 sense for the 32-bit format, this initial zero can be considered to
9300 be an escape value which indicates the presence of the older 64-bit
9301 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9302 greater than 4GB. If it becomes necessary to handle lengths
9303 somewhat larger than 4GB, we could allow other small values (such
9304 as the non-sensical values of 1, 2, and 3) to also be used as
9305 escape values indicating the presence of the old format.
f7ef9339 9306
917c78fc
MK
9307 The value returned via bytes_read should be used to increment the
9308 relevant pointer after calling read_initial_length().
c764a876 9309
613e1657
KB
9310 [ Note: read_initial_length() and read_offset() are based on the
9311 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9312 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9313 from:
9314
f7ef9339 9315 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9316
613e1657
KB
9317 This document is only a draft and is subject to change. (So beware.)
9318
f7ef9339 9319 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9320 determined empirically by examining 64-bit ELF files produced by
9321 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9322
9323 - Kevin, July 16, 2002
613e1657
KB
9324 ] */
9325
9326static LONGEST
c764a876 9327read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9328{
fe1b8b76 9329 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9330
dd373385 9331 if (length == 0xffffffff)
613e1657 9332 {
fe1b8b76 9333 length = bfd_get_64 (abfd, buf + 4);
613e1657 9334 *bytes_read = 12;
613e1657 9335 }
dd373385 9336 else if (length == 0)
f7ef9339 9337 {
dd373385 9338 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9339 length = bfd_get_64 (abfd, buf);
f7ef9339 9340 *bytes_read = 8;
f7ef9339 9341 }
613e1657
KB
9342 else
9343 {
9344 *bytes_read = 4;
613e1657
KB
9345 }
9346
c764a876
DE
9347 return length;
9348}
dd373385 9349
c764a876
DE
9350/* Cover function for read_initial_length.
9351 Returns the length of the object at BUF, and stores the size of the
9352 initial length in *BYTES_READ and stores the size that offsets will be in
9353 *OFFSET_SIZE.
9354 If the initial length size is not equivalent to that specified in
9355 CU_HEADER then issue a complaint.
9356 This is useful when reading non-comp-unit headers. */
dd373385 9357
c764a876
DE
9358static LONGEST
9359read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9360 const struct comp_unit_head *cu_header,
9361 unsigned int *bytes_read,
9362 unsigned int *offset_size)
9363{
9364 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9365
9366 gdb_assert (cu_header->initial_length_size == 4
9367 || cu_header->initial_length_size == 8
9368 || cu_header->initial_length_size == 12);
9369
9370 if (cu_header->initial_length_size != *bytes_read)
9371 complaint (&symfile_complaints,
9372 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9373
c764a876 9374 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9375 return length;
613e1657
KB
9376}
9377
9378/* Read an offset from the data stream. The size of the offset is
917c78fc 9379 given by cu_header->offset_size. */
613e1657
KB
9380
9381static LONGEST
fe1b8b76 9382read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9383 unsigned int *bytes_read)
c764a876
DE
9384{
9385 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9386
c764a876
DE
9387 *bytes_read = cu_header->offset_size;
9388 return offset;
9389}
9390
9391/* Read an offset from the data stream. */
9392
9393static LONGEST
9394read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9395{
9396 LONGEST retval = 0;
9397
c764a876 9398 switch (offset_size)
613e1657
KB
9399 {
9400 case 4:
fe1b8b76 9401 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9402 break;
9403 case 8:
fe1b8b76 9404 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9405 break;
9406 default:
8e65ff28 9407 internal_error (__FILE__, __LINE__,
c764a876 9408 _("read_offset_1: bad switch [in module %s]"),
659b0389 9409 bfd_get_filename (abfd));
613e1657
KB
9410 }
9411
917c78fc 9412 return retval;
613e1657
KB
9413}
9414
fe1b8b76
JB
9415static gdb_byte *
9416read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9417{
9418 /* If the size of a host char is 8 bits, we can return a pointer
9419 to the buffer, otherwise we have to copy the data to a buffer
9420 allocated on the temporary obstack. */
4bdf3d34 9421 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9422 return buf;
c906108c
SS
9423}
9424
9425static char *
9b1c24c8 9426read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9427{
9428 /* If the size of a host char is 8 bits, we can return a pointer
9429 to the string, otherwise we have to copy the string to a buffer
9430 allocated on the temporary obstack. */
4bdf3d34 9431 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9432 if (*buf == '\0')
9433 {
9434 *bytes_read_ptr = 1;
9435 return NULL;
9436 }
fe1b8b76
JB
9437 *bytes_read_ptr = strlen ((char *) buf) + 1;
9438 return (char *) buf;
4bdf3d34
JJ
9439}
9440
9441static char *
fe1b8b76 9442read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9443 const struct comp_unit_head *cu_header,
9444 unsigned int *bytes_read_ptr)
9445{
c764a876 9446 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9447
be391dca 9448 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9449 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9450 {
8a3fe4f8 9451 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9452 bfd_get_filename (abfd));
4bdf3d34 9453 return NULL;
c906108c 9454 }
dce234bc 9455 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9456 {
8a3fe4f8 9457 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9458 bfd_get_filename (abfd));
c906108c
SS
9459 return NULL;
9460 }
4bdf3d34 9461 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9462 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9463 return NULL;
dce234bc 9464 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9465}
9466
ce5d95e1 9467static unsigned long
fe1b8b76 9468read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9469{
ce5d95e1
JB
9470 unsigned long result;
9471 unsigned int num_read;
c906108c
SS
9472 int i, shift;
9473 unsigned char byte;
9474
9475 result = 0;
9476 shift = 0;
9477 num_read = 0;
9478 i = 0;
9479 while (1)
9480 {
fe1b8b76 9481 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9482 buf++;
9483 num_read++;
ce5d95e1 9484 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9485 if ((byte & 128) == 0)
9486 {
9487 break;
9488 }
9489 shift += 7;
9490 }
9491 *bytes_read_ptr = num_read;
9492 return result;
9493}
9494
ce5d95e1 9495static long
fe1b8b76 9496read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9497{
ce5d95e1 9498 long result;
77e0b926 9499 int i, shift, num_read;
c906108c
SS
9500 unsigned char byte;
9501
9502 result = 0;
9503 shift = 0;
c906108c
SS
9504 num_read = 0;
9505 i = 0;
9506 while (1)
9507 {
fe1b8b76 9508 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9509 buf++;
9510 num_read++;
ce5d95e1 9511 result |= ((long)(byte & 127) << shift);
c906108c
SS
9512 shift += 7;
9513 if ((byte & 128) == 0)
9514 {
9515 break;
9516 }
9517 }
77e0b926
DJ
9518 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9519 result |= -(((long)1) << shift);
c906108c
SS
9520 *bytes_read_ptr = num_read;
9521 return result;
9522}
9523
4bb7a0a7
DJ
9524/* Return a pointer to just past the end of an LEB128 number in BUF. */
9525
fe1b8b76
JB
9526static gdb_byte *
9527skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9528{
9529 int byte;
9530
9531 while (1)
9532 {
fe1b8b76 9533 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9534 buf++;
9535 if ((byte & 128) == 0)
9536 return buf;
9537 }
9538}
9539
c906108c 9540static void
e142c38c 9541set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9542{
9543 switch (lang)
9544 {
9545 case DW_LANG_C89:
76bee0cc 9546 case DW_LANG_C99:
c906108c 9547 case DW_LANG_C:
e142c38c 9548 cu->language = language_c;
c906108c
SS
9549 break;
9550 case DW_LANG_C_plus_plus:
e142c38c 9551 cu->language = language_cplus;
c906108c 9552 break;
6aecb9c2
JB
9553 case DW_LANG_D:
9554 cu->language = language_d;
9555 break;
c906108c
SS
9556 case DW_LANG_Fortran77:
9557 case DW_LANG_Fortran90:
b21b22e0 9558 case DW_LANG_Fortran95:
e142c38c 9559 cu->language = language_fortran;
c906108c
SS
9560 break;
9561 case DW_LANG_Mips_Assembler:
e142c38c 9562 cu->language = language_asm;
c906108c 9563 break;
bebd888e 9564 case DW_LANG_Java:
e142c38c 9565 cu->language = language_java;
bebd888e 9566 break;
c906108c 9567 case DW_LANG_Ada83:
8aaf0b47 9568 case DW_LANG_Ada95:
bc5f45f8
JB
9569 cu->language = language_ada;
9570 break;
72019c9c
GM
9571 case DW_LANG_Modula2:
9572 cu->language = language_m2;
9573 break;
fe8e67fd
PM
9574 case DW_LANG_Pascal83:
9575 cu->language = language_pascal;
9576 break;
22566fbd
DJ
9577 case DW_LANG_ObjC:
9578 cu->language = language_objc;
9579 break;
c906108c
SS
9580 case DW_LANG_Cobol74:
9581 case DW_LANG_Cobol85:
c906108c 9582 default:
e142c38c 9583 cu->language = language_minimal;
c906108c
SS
9584 break;
9585 }
e142c38c 9586 cu->language_defn = language_def (cu->language);
c906108c
SS
9587}
9588
9589/* Return the named attribute or NULL if not there. */
9590
9591static struct attribute *
e142c38c 9592dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9593{
9594 unsigned int i;
9595 struct attribute *spec = NULL;
9596
9597 for (i = 0; i < die->num_attrs; ++i)
9598 {
9599 if (die->attrs[i].name == name)
10b3939b 9600 return &die->attrs[i];
c906108c
SS
9601 if (die->attrs[i].name == DW_AT_specification
9602 || die->attrs[i].name == DW_AT_abstract_origin)
9603 spec = &die->attrs[i];
9604 }
c906108c 9605
10b3939b 9606 if (spec)
f2f0e013
DJ
9607 {
9608 die = follow_die_ref (die, spec, &cu);
9609 return dwarf2_attr (die, name, cu);
9610 }
c5aa993b 9611
c906108c
SS
9612 return NULL;
9613}
9614
348e048f
DE
9615/* Return the named attribute or NULL if not there,
9616 but do not follow DW_AT_specification, etc.
9617 This is for use in contexts where we're reading .debug_types dies.
9618 Following DW_AT_specification, DW_AT_abstract_origin will take us
9619 back up the chain, and we want to go down. */
9620
9621static struct attribute *
9622dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9623 struct dwarf2_cu *cu)
9624{
9625 unsigned int i;
9626
9627 for (i = 0; i < die->num_attrs; ++i)
9628 if (die->attrs[i].name == name)
9629 return &die->attrs[i];
9630
9631 return NULL;
9632}
9633
05cf31d1
JB
9634/* Return non-zero iff the attribute NAME is defined for the given DIE,
9635 and holds a non-zero value. This function should only be used for
2dc7f7b3 9636 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9637
9638static int
9639dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9640{
9641 struct attribute *attr = dwarf2_attr (die, name, cu);
9642
9643 return (attr && DW_UNSND (attr));
9644}
9645
3ca72b44 9646static int
e142c38c 9647die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9648{
05cf31d1
JB
9649 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9650 which value is non-zero. However, we have to be careful with
9651 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9652 (via dwarf2_flag_true_p) follows this attribute. So we may
9653 end up accidently finding a declaration attribute that belongs
9654 to a different DIE referenced by the specification attribute,
9655 even though the given DIE does not have a declaration attribute. */
9656 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9657 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9658}
9659
63d06c5c 9660/* Return the die giving the specification for DIE, if there is
f2f0e013 9661 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9662 containing the return value on output. If there is no
9663 specification, but there is an abstract origin, that is
9664 returned. */
63d06c5c
DC
9665
9666static struct die_info *
f2f0e013 9667die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9668{
f2f0e013
DJ
9669 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9670 *spec_cu);
63d06c5c 9671
edb3359d
DJ
9672 if (spec_attr == NULL)
9673 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9674
63d06c5c
DC
9675 if (spec_attr == NULL)
9676 return NULL;
9677 else
f2f0e013 9678 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9679}
c906108c 9680
debd256d
JB
9681/* Free the line_header structure *LH, and any arrays and strings it
9682 refers to. */
9683static void
9684free_line_header (struct line_header *lh)
9685{
9686 if (lh->standard_opcode_lengths)
a8bc7b56 9687 xfree (lh->standard_opcode_lengths);
debd256d
JB
9688
9689 /* Remember that all the lh->file_names[i].name pointers are
9690 pointers into debug_line_buffer, and don't need to be freed. */
9691 if (lh->file_names)
a8bc7b56 9692 xfree (lh->file_names);
debd256d
JB
9693
9694 /* Similarly for the include directory names. */
9695 if (lh->include_dirs)
a8bc7b56 9696 xfree (lh->include_dirs);
debd256d 9697
a8bc7b56 9698 xfree (lh);
debd256d
JB
9699}
9700
9701
9702/* Add an entry to LH's include directory table. */
9703static void
9704add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9705{
debd256d
JB
9706 /* Grow the array if necessary. */
9707 if (lh->include_dirs_size == 0)
c5aa993b 9708 {
debd256d
JB
9709 lh->include_dirs_size = 1; /* for testing */
9710 lh->include_dirs = xmalloc (lh->include_dirs_size
9711 * sizeof (*lh->include_dirs));
9712 }
9713 else if (lh->num_include_dirs >= lh->include_dirs_size)
9714 {
9715 lh->include_dirs_size *= 2;
9716 lh->include_dirs = xrealloc (lh->include_dirs,
9717 (lh->include_dirs_size
9718 * sizeof (*lh->include_dirs)));
c5aa993b 9719 }
c906108c 9720
debd256d
JB
9721 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9722}
6e70227d 9723
debd256d
JB
9724
9725/* Add an entry to LH's file name table. */
9726static void
9727add_file_name (struct line_header *lh,
9728 char *name,
9729 unsigned int dir_index,
9730 unsigned int mod_time,
9731 unsigned int length)
9732{
9733 struct file_entry *fe;
9734
9735 /* Grow the array if necessary. */
9736 if (lh->file_names_size == 0)
9737 {
9738 lh->file_names_size = 1; /* for testing */
9739 lh->file_names = xmalloc (lh->file_names_size
9740 * sizeof (*lh->file_names));
9741 }
9742 else if (lh->num_file_names >= lh->file_names_size)
9743 {
9744 lh->file_names_size *= 2;
9745 lh->file_names = xrealloc (lh->file_names,
9746 (lh->file_names_size
9747 * sizeof (*lh->file_names)));
9748 }
9749
9750 fe = &lh->file_names[lh->num_file_names++];
9751 fe->name = name;
9752 fe->dir_index = dir_index;
9753 fe->mod_time = mod_time;
9754 fe->length = length;
aaa75496 9755 fe->included_p = 0;
cb1df416 9756 fe->symtab = NULL;
debd256d 9757}
6e70227d 9758
debd256d
JB
9759
9760/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9761 .debug_line, according to the endianness of ABFD. Return a pointer
9762 to a struct line_header, allocated using xmalloc.
debd256d
JB
9763
9764 NOTE: the strings in the include directory and file name tables of
9765 the returned object point into debug_line_buffer, and must not be
9766 freed. */
9767static struct line_header *
9768dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9769 struct dwarf2_cu *cu)
debd256d
JB
9770{
9771 struct cleanup *back_to;
9772 struct line_header *lh;
fe1b8b76 9773 gdb_byte *line_ptr;
c764a876 9774 unsigned int bytes_read, offset_size;
debd256d
JB
9775 int i;
9776 char *cur_dir, *cur_file;
9777
be391dca 9778 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9779 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9780 {
e2e0b3e5 9781 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9782 return 0;
9783 }
9784
a738430d
MK
9785 /* Make sure that at least there's room for the total_length field.
9786 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9787 if (offset + 4 >= 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
9793 lh = xmalloc (sizeof (*lh));
9794 memset (lh, 0, sizeof (*lh));
9795 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9796 (void *) lh);
9797
dce234bc 9798 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9799
a738430d 9800 /* Read in the header. */
6e70227d 9801 lh->total_length =
c764a876
DE
9802 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9803 &bytes_read, &offset_size);
debd256d 9804 line_ptr += bytes_read;
dce234bc
PP
9805 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9806 + dwarf2_per_objfile->line.size))
debd256d 9807 {
4d3c2250 9808 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9809 return 0;
9810 }
9811 lh->statement_program_end = line_ptr + lh->total_length;
9812 lh->version = read_2_bytes (abfd, line_ptr);
9813 line_ptr += 2;
c764a876
DE
9814 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9815 line_ptr += offset_size;
debd256d
JB
9816 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9817 line_ptr += 1;
2dc7f7b3
TT
9818 if (lh->version >= 4)
9819 {
9820 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9821 line_ptr += 1;
9822 }
9823 else
9824 lh->maximum_ops_per_instruction = 1;
9825
9826 if (lh->maximum_ops_per_instruction == 0)
9827 {
9828 lh->maximum_ops_per_instruction = 1;
9829 complaint (&symfile_complaints,
9830 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9831 }
9832
debd256d
JB
9833 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9834 line_ptr += 1;
9835 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9836 line_ptr += 1;
9837 lh->line_range = read_1_byte (abfd, line_ptr);
9838 line_ptr += 1;
9839 lh->opcode_base = read_1_byte (abfd, line_ptr);
9840 line_ptr += 1;
9841 lh->standard_opcode_lengths
fe1b8b76 9842 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9843
9844 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9845 for (i = 1; i < lh->opcode_base; ++i)
9846 {
9847 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9848 line_ptr += 1;
9849 }
9850
a738430d 9851 /* Read directory table. */
9b1c24c8 9852 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9853 {
9854 line_ptr += bytes_read;
9855 add_include_dir (lh, cur_dir);
9856 }
9857 line_ptr += bytes_read;
9858
a738430d 9859 /* Read file name table. */
9b1c24c8 9860 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9861 {
9862 unsigned int dir_index, mod_time, length;
9863
9864 line_ptr += bytes_read;
9865 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9866 line_ptr += bytes_read;
9867 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9868 line_ptr += bytes_read;
9869 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9870 line_ptr += bytes_read;
9871
9872 add_file_name (lh, cur_file, dir_index, mod_time, length);
9873 }
9874 line_ptr += bytes_read;
6e70227d 9875 lh->statement_program_start = line_ptr;
debd256d 9876
dce234bc
PP
9877 if (line_ptr > (dwarf2_per_objfile->line.buffer
9878 + dwarf2_per_objfile->line.size))
4d3c2250 9879 complaint (&symfile_complaints,
e2e0b3e5 9880 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9881
9882 discard_cleanups (back_to);
9883 return lh;
9884}
c906108c 9885
5fb290d7
DJ
9886/* This function exists to work around a bug in certain compilers
9887 (particularly GCC 2.95), in which the first line number marker of a
9888 function does not show up until after the prologue, right before
9889 the second line number marker. This function shifts ADDRESS down
9890 to the beginning of the function if necessary, and is called on
9891 addresses passed to record_line. */
9892
9893static CORE_ADDR
e142c38c 9894check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9895{
9896 struct function_range *fn;
9897
9898 /* Find the function_range containing address. */
e142c38c 9899 if (!cu->first_fn)
5fb290d7
DJ
9900 return address;
9901
e142c38c
DJ
9902 if (!cu->cached_fn)
9903 cu->cached_fn = cu->first_fn;
5fb290d7 9904
e142c38c 9905 fn = cu->cached_fn;
5fb290d7
DJ
9906 while (fn)
9907 if (fn->lowpc <= address && fn->highpc > address)
9908 goto found;
9909 else
9910 fn = fn->next;
9911
e142c38c
DJ
9912 fn = cu->first_fn;
9913 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9914 if (fn->lowpc <= address && fn->highpc > address)
9915 goto found;
9916 else
9917 fn = fn->next;
9918
9919 return address;
9920
9921 found:
9922 if (fn->seen_line)
9923 return address;
9924 if (address != fn->lowpc)
4d3c2250 9925 complaint (&symfile_complaints,
e2e0b3e5 9926 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9927 (unsigned long) address, fn->name);
5fb290d7
DJ
9928 fn->seen_line = 1;
9929 return fn->lowpc;
9930}
9931
c6da4cef
DE
9932/* Subroutine of dwarf_decode_lines to simplify it.
9933 Return the file name of the psymtab for included file FILE_INDEX
9934 in line header LH of PST.
9935 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9936 If space for the result is malloc'd, it will be freed by a cleanup.
9937 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
9938
9939static char *
9940psymtab_include_file_name (const struct line_header *lh, int file_index,
9941 const struct partial_symtab *pst,
9942 const char *comp_dir)
9943{
9944 const struct file_entry fe = lh->file_names [file_index];
9945 char *include_name = fe.name;
9946 char *include_name_to_compare = include_name;
9947 char *dir_name = NULL;
9948 char *pst_filename;
9949 int file_is_pst;
9950
9951 if (fe.dir_index)
9952 dir_name = lh->include_dirs[fe.dir_index - 1];
9953
9954 if (!IS_ABSOLUTE_PATH (include_name)
9955 && (dir_name != NULL || comp_dir != NULL))
9956 {
9957 /* Avoid creating a duplicate psymtab for PST.
9958 We do this by comparing INCLUDE_NAME and PST_FILENAME.
9959 Before we do the comparison, however, we need to account
9960 for DIR_NAME and COMP_DIR.
9961 First prepend dir_name (if non-NULL). If we still don't
9962 have an absolute path prepend comp_dir (if non-NULL).
9963 However, the directory we record in the include-file's
9964 psymtab does not contain COMP_DIR (to match the
9965 corresponding symtab(s)).
9966
9967 Example:
9968
9969 bash$ cd /tmp
9970 bash$ gcc -g ./hello.c
9971 include_name = "hello.c"
9972 dir_name = "."
9973 DW_AT_comp_dir = comp_dir = "/tmp"
9974 DW_AT_name = "./hello.c" */
9975
9976 if (dir_name != NULL)
9977 {
9978 include_name = concat (dir_name, SLASH_STRING,
9979 include_name, (char *)NULL);
9980 include_name_to_compare = include_name;
9981 make_cleanup (xfree, include_name);
9982 }
9983 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
9984 {
9985 include_name_to_compare = concat (comp_dir, SLASH_STRING,
9986 include_name, (char *)NULL);
9987 }
9988 }
9989
9990 pst_filename = pst->filename;
9991 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9992 {
9993 pst_filename = concat (pst->dirname, SLASH_STRING,
9994 pst_filename, (char *)NULL);
9995 }
9996
9997 file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
9998
9999 if (include_name_to_compare != include_name)
10000 xfree (include_name_to_compare);
10001 if (pst_filename != pst->filename)
10002 xfree (pst_filename);
10003
10004 if (file_is_pst)
10005 return NULL;
10006 return include_name;
10007}
10008
aaa75496
JB
10009/* Decode the Line Number Program (LNP) for the given line_header
10010 structure and CU. The actual information extracted and the type
10011 of structures created from the LNP depends on the value of PST.
10012
10013 1. If PST is NULL, then this procedure uses the data from the program
10014 to create all necessary symbol tables, and their linetables.
6e70227d 10015
aaa75496
JB
10016 2. If PST is not NULL, this procedure reads the program to determine
10017 the list of files included by the unit represented by PST, and
c6da4cef
DE
10018 builds all the associated partial symbol tables.
10019
10020 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10021 It is used for relative paths in the line table.
10022 NOTE: When processing partial symtabs (pst != NULL),
10023 comp_dir == pst->dirname.
10024
10025 NOTE: It is important that psymtabs have the same file name (via strcmp)
10026 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10027 symtab we don't use it in the name of the psymtabs we create.
10028 E.g. expand_line_sal requires this when finding psymtabs to expand.
10029 A good testcase for this is mb-inline.exp. */
debd256d 10030
c906108c 10031static void
debd256d 10032dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 10033 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10034{
a8c50c1f 10035 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10036 gdb_byte *line_end;
a8c50c1f 10037 unsigned int bytes_read, extended_len;
c906108c 10038 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10039 CORE_ADDR baseaddr;
10040 struct objfile *objfile = cu->objfile;
fbf65064 10041 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10042 const int decode_for_pst_p = (pst != NULL);
cb1df416 10043 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10044
10045 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10046
debd256d
JB
10047 line_ptr = lh->statement_program_start;
10048 line_end = lh->statement_program_end;
c906108c
SS
10049
10050 /* Read the statement sequences until there's nothing left. */
10051 while (line_ptr < line_end)
10052 {
10053 /* state machine registers */
10054 CORE_ADDR address = 0;
10055 unsigned int file = 1;
10056 unsigned int line = 1;
10057 unsigned int column = 0;
debd256d 10058 int is_stmt = lh->default_is_stmt;
c906108c
SS
10059 int basic_block = 0;
10060 int end_sequence = 0;
fbf65064 10061 CORE_ADDR addr;
2dc7f7b3 10062 unsigned char op_index = 0;
c906108c 10063
aaa75496 10064 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10065 {
aaa75496 10066 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10067 /* lh->include_dirs and lh->file_names are 0-based, but the
10068 directory and file name numbers in the statement program
10069 are 1-based. */
10070 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10071 char *dir = NULL;
a738430d 10072
debd256d
JB
10073 if (fe->dir_index)
10074 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10075
10076 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10077 }
10078
a738430d 10079 /* Decode the table. */
c5aa993b 10080 while (!end_sequence)
c906108c
SS
10081 {
10082 op_code = read_1_byte (abfd, line_ptr);
10083 line_ptr += 1;
59205f5a
JB
10084 if (line_ptr > line_end)
10085 {
10086 dwarf2_debug_line_missing_end_sequence_complaint ();
10087 break;
10088 }
9aa1fe7e 10089
debd256d 10090 if (op_code >= lh->opcode_base)
6e70227d 10091 {
a738430d 10092 /* Special operand. */
debd256d 10093 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10094 address += (((op_index + (adj_opcode / lh->line_range))
10095 / lh->maximum_ops_per_instruction)
10096 * lh->minimum_instruction_length);
10097 op_index = ((op_index + (adj_opcode / lh->line_range))
10098 % lh->maximum_ops_per_instruction);
debd256d 10099 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10100 if (lh->num_file_names < file || file == 0)
25e43795 10101 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10102 /* For now we ignore lines not starting on an
10103 instruction boundary. */
10104 else if (op_index == 0)
25e43795
DJ
10105 {
10106 lh->file_names[file - 1].included_p = 1;
ca5f395d 10107 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10108 {
10109 if (last_subfile != current_subfile)
10110 {
10111 addr = gdbarch_addr_bits_remove (gdbarch, address);
10112 if (last_subfile)
10113 record_line (last_subfile, 0, addr);
10114 last_subfile = current_subfile;
10115 }
25e43795 10116 /* Append row to matrix using current values. */
fbf65064
UW
10117 addr = check_cu_functions (address, cu);
10118 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10119 record_line (current_subfile, line, addr);
366da635 10120 }
25e43795 10121 }
ca5f395d 10122 basic_block = 0;
9aa1fe7e
GK
10123 }
10124 else switch (op_code)
c906108c
SS
10125 {
10126 case DW_LNS_extended_op:
a8c50c1f 10127 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10128 line_ptr += bytes_read;
a8c50c1f 10129 extended_end = line_ptr + extended_len;
c906108c
SS
10130 extended_op = read_1_byte (abfd, line_ptr);
10131 line_ptr += 1;
10132 switch (extended_op)
10133 {
10134 case DW_LNE_end_sequence:
10135 end_sequence = 1;
c906108c
SS
10136 break;
10137 case DW_LNE_set_address:
e7c27a73 10138 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10139 op_index = 0;
107d2387
AC
10140 line_ptr += bytes_read;
10141 address += baseaddr;
c906108c
SS
10142 break;
10143 case DW_LNE_define_file:
debd256d
JB
10144 {
10145 char *cur_file;
10146 unsigned int dir_index, mod_time, length;
6e70227d 10147
9b1c24c8 10148 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10149 line_ptr += bytes_read;
10150 dir_index =
10151 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10152 line_ptr += bytes_read;
10153 mod_time =
10154 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10155 line_ptr += bytes_read;
10156 length =
10157 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10158 line_ptr += bytes_read;
10159 add_file_name (lh, cur_file, dir_index, mod_time, length);
10160 }
c906108c 10161 break;
d0c6ba3d
CC
10162 case DW_LNE_set_discriminator:
10163 /* The discriminator is not interesting to the debugger;
10164 just ignore it. */
10165 line_ptr = extended_end;
10166 break;
c906108c 10167 default:
4d3c2250 10168 complaint (&symfile_complaints,
e2e0b3e5 10169 _("mangled .debug_line section"));
debd256d 10170 return;
c906108c 10171 }
a8c50c1f
DJ
10172 /* Make sure that we parsed the extended op correctly. If e.g.
10173 we expected a different address size than the producer used,
10174 we may have read the wrong number of bytes. */
10175 if (line_ptr != extended_end)
10176 {
10177 complaint (&symfile_complaints,
10178 _("mangled .debug_line section"));
10179 return;
10180 }
c906108c
SS
10181 break;
10182 case DW_LNS_copy:
59205f5a 10183 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10184 dwarf2_debug_line_missing_file_complaint ();
10185 else
366da635 10186 {
25e43795 10187 lh->file_names[file - 1].included_p = 1;
ca5f395d 10188 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10189 {
10190 if (last_subfile != current_subfile)
10191 {
10192 addr = gdbarch_addr_bits_remove (gdbarch, address);
10193 if (last_subfile)
10194 record_line (last_subfile, 0, addr);
10195 last_subfile = current_subfile;
10196 }
10197 addr = check_cu_functions (address, cu);
10198 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10199 record_line (current_subfile, line, addr);
10200 }
366da635 10201 }
c906108c
SS
10202 basic_block = 0;
10203 break;
10204 case DW_LNS_advance_pc:
2dc7f7b3
TT
10205 {
10206 CORE_ADDR adjust
10207 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10208
10209 address += (((op_index + adjust)
10210 / lh->maximum_ops_per_instruction)
10211 * lh->minimum_instruction_length);
10212 op_index = ((op_index + adjust)
10213 % lh->maximum_ops_per_instruction);
10214 line_ptr += bytes_read;
10215 }
c906108c
SS
10216 break;
10217 case DW_LNS_advance_line:
10218 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10219 line_ptr += bytes_read;
10220 break;
10221 case DW_LNS_set_file:
debd256d 10222 {
a738430d
MK
10223 /* The arrays lh->include_dirs and lh->file_names are
10224 0-based, but the directory and file name numbers in
10225 the statement program are 1-based. */
debd256d 10226 struct file_entry *fe;
4f1520fb 10227 char *dir = NULL;
a738430d 10228
debd256d
JB
10229 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10230 line_ptr += bytes_read;
59205f5a 10231 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10232 dwarf2_debug_line_missing_file_complaint ();
10233 else
10234 {
10235 fe = &lh->file_names[file - 1];
10236 if (fe->dir_index)
10237 dir = lh->include_dirs[fe->dir_index - 1];
10238 if (!decode_for_pst_p)
10239 {
10240 last_subfile = current_subfile;
10241 dwarf2_start_subfile (fe->name, dir, comp_dir);
10242 }
10243 }
debd256d 10244 }
c906108c
SS
10245 break;
10246 case DW_LNS_set_column:
10247 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10248 line_ptr += bytes_read;
10249 break;
10250 case DW_LNS_negate_stmt:
10251 is_stmt = (!is_stmt);
10252 break;
10253 case DW_LNS_set_basic_block:
10254 basic_block = 1;
10255 break;
c2c6d25f
JM
10256 /* Add to the address register of the state machine the
10257 address increment value corresponding to special opcode
a738430d
MK
10258 255. I.e., this value is scaled by the minimum
10259 instruction length since special opcode 255 would have
10260 scaled the the increment. */
c906108c 10261 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10262 {
10263 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10264
10265 address += (((op_index + adjust)
10266 / lh->maximum_ops_per_instruction)
10267 * lh->minimum_instruction_length);
10268 op_index = ((op_index + adjust)
10269 % lh->maximum_ops_per_instruction);
10270 }
c906108c
SS
10271 break;
10272 case DW_LNS_fixed_advance_pc:
10273 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10274 op_index = 0;
c906108c
SS
10275 line_ptr += 2;
10276 break;
9aa1fe7e 10277 default:
a738430d
MK
10278 {
10279 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10280 int i;
a738430d 10281
debd256d 10282 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10283 {
10284 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10285 line_ptr += bytes_read;
10286 }
10287 }
c906108c
SS
10288 }
10289 }
59205f5a
JB
10290 if (lh->num_file_names < file || file == 0)
10291 dwarf2_debug_line_missing_file_complaint ();
10292 else
10293 {
10294 lh->file_names[file - 1].included_p = 1;
10295 if (!decode_for_pst_p)
fbf65064
UW
10296 {
10297 addr = gdbarch_addr_bits_remove (gdbarch, address);
10298 record_line (current_subfile, 0, addr);
10299 }
59205f5a 10300 }
c906108c 10301 }
aaa75496
JB
10302
10303 if (decode_for_pst_p)
10304 {
10305 int file_index;
10306
10307 /* Now that we're done scanning the Line Header Program, we can
10308 create the psymtab of each included file. */
10309 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10310 if (lh->file_names[file_index].included_p == 1)
10311 {
c6da4cef
DE
10312 char *include_name =
10313 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10314 if (include_name != NULL)
aaa75496
JB
10315 dwarf2_create_include_psymtab (include_name, pst, objfile);
10316 }
10317 }
cb1df416
DJ
10318 else
10319 {
10320 /* Make sure a symtab is created for every file, even files
10321 which contain only variables (i.e. no code with associated
10322 line numbers). */
10323
10324 int i;
10325 struct file_entry *fe;
10326
10327 for (i = 0; i < lh->num_file_names; i++)
10328 {
10329 char *dir = NULL;
9a619af0 10330
cb1df416
DJ
10331 fe = &lh->file_names[i];
10332 if (fe->dir_index)
10333 dir = lh->include_dirs[fe->dir_index - 1];
10334 dwarf2_start_subfile (fe->name, dir, comp_dir);
10335
10336 /* Skip the main file; we don't need it, and it must be
10337 allocated last, so that it will show up before the
10338 non-primary symtabs in the objfile's symtab list. */
10339 if (current_subfile == first_subfile)
10340 continue;
10341
10342 if (current_subfile->symtab == NULL)
10343 current_subfile->symtab = allocate_symtab (current_subfile->name,
10344 cu->objfile);
10345 fe->symtab = current_subfile->symtab;
10346 }
10347 }
c906108c
SS
10348}
10349
10350/* Start a subfile for DWARF. FILENAME is the name of the file and
10351 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10352 or NULL if not known. COMP_DIR is the compilation directory for the
10353 linetable's compilation unit or NULL if not known.
c906108c
SS
10354 This routine tries to keep line numbers from identical absolute and
10355 relative file names in a common subfile.
10356
10357 Using the `list' example from the GDB testsuite, which resides in
10358 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10359 of /srcdir/list0.c yields the following debugging information for list0.c:
10360
c5aa993b
JM
10361 DW_AT_name: /srcdir/list0.c
10362 DW_AT_comp_dir: /compdir
357e46e7 10363 files.files[0].name: list0.h
c5aa993b 10364 files.files[0].dir: /srcdir
357e46e7 10365 files.files[1].name: list0.c
c5aa993b 10366 files.files[1].dir: /srcdir
c906108c
SS
10367
10368 The line number information for list0.c has to end up in a single
4f1520fb
FR
10369 subfile, so that `break /srcdir/list0.c:1' works as expected.
10370 start_subfile will ensure that this happens provided that we pass the
10371 concatenation of files.files[1].dir and files.files[1].name as the
10372 subfile's name. */
c906108c
SS
10373
10374static void
4f1520fb 10375dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10376{
4f1520fb
FR
10377 char *fullname;
10378
10379 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10380 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10381 second argument to start_subfile. To be consistent, we do the
10382 same here. In order not to lose the line information directory,
10383 we concatenate it to the filename when it makes sense.
10384 Note that the Dwarf3 standard says (speaking of filenames in line
10385 information): ``The directory index is ignored for file names
10386 that represent full path names''. Thus ignoring dirname in the
10387 `else' branch below isn't an issue. */
c906108c 10388
d5166ae1 10389 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10390 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10391 else
10392 fullname = filename;
c906108c 10393
4f1520fb
FR
10394 start_subfile (fullname, comp_dir);
10395
10396 if (fullname != filename)
10397 xfree (fullname);
c906108c
SS
10398}
10399
4c2df51b
DJ
10400static void
10401var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10402 struct dwarf2_cu *cu)
4c2df51b 10403{
e7c27a73
DJ
10404 struct objfile *objfile = cu->objfile;
10405 struct comp_unit_head *cu_header = &cu->header;
10406
4c2df51b
DJ
10407 /* NOTE drow/2003-01-30: There used to be a comment and some special
10408 code here to turn a symbol with DW_AT_external and a
10409 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10410 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10411 with some versions of binutils) where shared libraries could have
10412 relocations against symbols in their debug information - the
10413 minimal symbol would have the right address, but the debug info
10414 would not. It's no longer necessary, because we will explicitly
10415 apply relocations when we read in the debug information now. */
10416
10417 /* A DW_AT_location attribute with no contents indicates that a
10418 variable has been optimized away. */
10419 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10420 {
10421 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10422 return;
10423 }
10424
10425 /* Handle one degenerate form of location expression specially, to
10426 preserve GDB's previous behavior when section offsets are
10427 specified. If this is just a DW_OP_addr then mark this symbol
10428 as LOC_STATIC. */
10429
10430 if (attr_form_is_block (attr)
10431 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10432 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10433 {
891d2f0b 10434 unsigned int dummy;
4c2df51b
DJ
10435
10436 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10437 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10438 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10439 fixup_symbol_section (sym, objfile);
10440 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10441 SYMBOL_SECTION (sym));
4c2df51b
DJ
10442 return;
10443 }
10444
10445 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10446 expression evaluator, and use LOC_COMPUTED only when necessary
10447 (i.e. when the value of a register or memory location is
10448 referenced, or a thread-local block, etc.). Then again, it might
10449 not be worthwhile. I'm assuming that it isn't unless performance
10450 or memory numbers show me otherwise. */
10451
e7c27a73 10452 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10453 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10454}
10455
c906108c
SS
10456/* Given a pointer to a DWARF information entry, figure out if we need
10457 to make a symbol table entry for it, and if so, create a new entry
10458 and return a pointer to it.
10459 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10460 used the passed type.
10461 If SPACE is not NULL, use it to hold the new symbol. If it is
10462 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10463
10464static struct symbol *
34eaf542
TT
10465new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10466 struct symbol *space)
c906108c 10467{
e7c27a73 10468 struct objfile *objfile = cu->objfile;
c906108c
SS
10469 struct symbol *sym = NULL;
10470 char *name;
10471 struct attribute *attr = NULL;
10472 struct attribute *attr2 = NULL;
e142c38c 10473 CORE_ADDR baseaddr;
e37fd15a
SW
10474 struct pending **list_to_add = NULL;
10475
edb3359d 10476 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10477
10478 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10479
94af9270 10480 name = dwarf2_name (die, cu);
c906108c
SS
10481 if (name)
10482 {
94af9270 10483 const char *linkagename;
34eaf542 10484 int suppress_add = 0;
94af9270 10485
34eaf542
TT
10486 if (space)
10487 sym = space;
10488 else
10489 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10490 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10491
10492 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10493 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10494 linkagename = dwarf2_physname (name, die, cu);
10495 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10496
f55ee35c
JK
10497 /* Fortran does not have mangling standard and the mangling does differ
10498 between gfortran, iFort etc. */
10499 if (cu->language == language_fortran
b250c185 10500 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10501 symbol_set_demangled_name (&(sym->ginfo),
10502 (char *) dwarf2_full_name (name, die, cu),
10503 NULL);
f55ee35c 10504
c906108c 10505 /* Default assumptions.
c5aa993b 10506 Use the passed type or decode it from the die. */
176620f1 10507 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10508 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10509 if (type != NULL)
10510 SYMBOL_TYPE (sym) = type;
10511 else
e7c27a73 10512 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10513 attr = dwarf2_attr (die,
10514 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10515 cu);
c906108c
SS
10516 if (attr)
10517 {
10518 SYMBOL_LINE (sym) = DW_UNSND (attr);
10519 }
cb1df416 10520
edb3359d
DJ
10521 attr = dwarf2_attr (die,
10522 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10523 cu);
cb1df416
DJ
10524 if (attr)
10525 {
10526 int file_index = DW_UNSND (attr);
9a619af0 10527
cb1df416
DJ
10528 if (cu->line_header == NULL
10529 || file_index > cu->line_header->num_file_names)
10530 complaint (&symfile_complaints,
10531 _("file index out of range"));
1c3d648d 10532 else if (file_index > 0)
cb1df416
DJ
10533 {
10534 struct file_entry *fe;
9a619af0 10535
cb1df416
DJ
10536 fe = &cu->line_header->file_names[file_index - 1];
10537 SYMBOL_SYMTAB (sym) = fe->symtab;
10538 }
10539 }
10540
c906108c
SS
10541 switch (die->tag)
10542 {
10543 case DW_TAG_label:
e142c38c 10544 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10545 if (attr)
10546 {
10547 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10548 }
0f5238ed
TT
10549 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10550 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10551 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10552 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10553 break;
10554 case DW_TAG_subprogram:
10555 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10556 finish_block. */
10557 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10558 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10559 if ((attr2 && (DW_UNSND (attr2) != 0))
10560 || cu->language == language_ada)
c906108c 10561 {
2cfa0c8d
JB
10562 /* Subprograms marked external are stored as a global symbol.
10563 Ada subprograms, whether marked external or not, are always
10564 stored as a global symbol, because we want to be able to
10565 access them globally. For instance, we want to be able
10566 to break on a nested subprogram without having to
10567 specify the context. */
e37fd15a 10568 list_to_add = &global_symbols;
c906108c
SS
10569 }
10570 else
10571 {
e37fd15a 10572 list_to_add = cu->list_in_scope;
c906108c
SS
10573 }
10574 break;
edb3359d
DJ
10575 case DW_TAG_inlined_subroutine:
10576 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10577 finish_block. */
10578 SYMBOL_CLASS (sym) = LOC_BLOCK;
10579 SYMBOL_INLINED (sym) = 1;
10580 /* Do not add the symbol to any lists. It will be found via
10581 BLOCK_FUNCTION from the blockvector. */
10582 break;
34eaf542
TT
10583 case DW_TAG_template_value_param:
10584 suppress_add = 1;
10585 /* Fall through. */
c906108c 10586 case DW_TAG_variable:
254e6b9e 10587 case DW_TAG_member:
c906108c
SS
10588 /* Compilation with minimal debug info may result in variables
10589 with missing type entries. Change the misleading `void' type
10590 to something sensible. */
10591 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10592 SYMBOL_TYPE (sym)
46bf5051 10593 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10594
e142c38c 10595 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10596 /* In the case of DW_TAG_member, we should only be called for
10597 static const members. */
10598 if (die->tag == DW_TAG_member)
10599 {
3863f96c
DE
10600 /* dwarf2_add_field uses die_is_declaration,
10601 so we do the same. */
254e6b9e
DE
10602 gdb_assert (die_is_declaration (die, cu));
10603 gdb_assert (attr);
10604 }
c906108c
SS
10605 if (attr)
10606 {
e7c27a73 10607 dwarf2_const_value (attr, sym, cu);
e142c38c 10608 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10609 if (!suppress_add)
34eaf542
TT
10610 {
10611 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10612 list_to_add = &global_symbols;
34eaf542 10613 else
e37fd15a 10614 list_to_add = cu->list_in_scope;
34eaf542 10615 }
c906108c
SS
10616 break;
10617 }
e142c38c 10618 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10619 if (attr)
10620 {
e7c27a73 10621 var_decode_location (attr, sym, cu);
e142c38c 10622 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10623 if (SYMBOL_CLASS (sym) == LOC_STATIC
10624 && SYMBOL_VALUE_ADDRESS (sym) == 0
10625 && !dwarf2_per_objfile->has_section_at_zero)
10626 {
10627 /* When a static variable is eliminated by the linker,
10628 the corresponding debug information is not stripped
10629 out, but the variable address is set to null;
10630 do not add such variables into symbol table. */
10631 }
10632 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10633 {
f55ee35c
JK
10634 /* Workaround gfortran PR debug/40040 - it uses
10635 DW_AT_location for variables in -fPIC libraries which may
10636 get overriden by other libraries/executable and get
10637 a different address. Resolve it by the minimal symbol
10638 which may come from inferior's executable using copy
10639 relocation. Make this workaround only for gfortran as for
10640 other compilers GDB cannot guess the minimal symbol
10641 Fortran mangling kind. */
10642 if (cu->language == language_fortran && die->parent
10643 && die->parent->tag == DW_TAG_module
10644 && cu->producer
10645 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10646 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10647
1c809c68
TT
10648 /* A variable with DW_AT_external is never static,
10649 but it may be block-scoped. */
10650 list_to_add = (cu->list_in_scope == &file_symbols
10651 ? &global_symbols : cu->list_in_scope);
1c809c68 10652 }
c906108c 10653 else
e37fd15a 10654 list_to_add = cu->list_in_scope;
c906108c
SS
10655 }
10656 else
10657 {
10658 /* We do not know the address of this symbol.
c5aa993b
JM
10659 If it is an external symbol and we have type information
10660 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10661 The address of the variable will then be determined from
10662 the minimal symbol table whenever the variable is
10663 referenced. */
e142c38c 10664 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10665 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10666 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10667 {
0fe7935b
DJ
10668 /* A variable with DW_AT_external is never static, but it
10669 may be block-scoped. */
10670 list_to_add = (cu->list_in_scope == &file_symbols
10671 ? &global_symbols : cu->list_in_scope);
10672
c906108c 10673 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10674 }
442ddf59
JK
10675 else if (!die_is_declaration (die, cu))
10676 {
10677 /* Use the default LOC_OPTIMIZED_OUT class. */
10678 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10679 if (!suppress_add)
10680 list_to_add = cu->list_in_scope;
442ddf59 10681 }
c906108c
SS
10682 }
10683 break;
10684 case DW_TAG_formal_parameter:
edb3359d
DJ
10685 /* If we are inside a function, mark this as an argument. If
10686 not, we might be looking at an argument to an inlined function
10687 when we do not have enough information to show inlined frames;
10688 pretend it's a local variable in that case so that the user can
10689 still see it. */
10690 if (context_stack_depth > 0
10691 && context_stack[context_stack_depth - 1].name != NULL)
10692 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10693 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10694 if (attr)
10695 {
e7c27a73 10696 var_decode_location (attr, sym, cu);
c906108c 10697 }
e142c38c 10698 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10699 if (attr)
10700 {
e7c27a73 10701 dwarf2_const_value (attr, sym, cu);
c906108c 10702 }
f346a30d
PM
10703 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10704 if (attr && DW_UNSND (attr))
10705 {
10706 struct type *ref_type;
10707
10708 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10709 SYMBOL_TYPE (sym) = ref_type;
10710 }
10711
e37fd15a 10712 list_to_add = cu->list_in_scope;
c906108c
SS
10713 break;
10714 case DW_TAG_unspecified_parameters:
10715 /* From varargs functions; gdb doesn't seem to have any
10716 interest in this information, so just ignore it for now.
10717 (FIXME?) */
10718 break;
34eaf542
TT
10719 case DW_TAG_template_type_param:
10720 suppress_add = 1;
10721 /* Fall through. */
c906108c 10722 case DW_TAG_class_type:
680b30c7 10723 case DW_TAG_interface_type:
c906108c
SS
10724 case DW_TAG_structure_type:
10725 case DW_TAG_union_type:
72019c9c 10726 case DW_TAG_set_type:
c906108c
SS
10727 case DW_TAG_enumeration_type:
10728 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10729 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10730
63d06c5c 10731 {
987504bb 10732 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10733 really ever be static objects: otherwise, if you try
10734 to, say, break of a class's method and you're in a file
10735 which doesn't mention that class, it won't work unless
10736 the check for all static symbols in lookup_symbol_aux
10737 saves you. See the OtherFileClass tests in
10738 gdb.c++/namespace.exp. */
10739
e37fd15a 10740 if (!suppress_add)
34eaf542 10741 {
34eaf542
TT
10742 list_to_add = (cu->list_in_scope == &file_symbols
10743 && (cu->language == language_cplus
10744 || cu->language == language_java)
10745 ? &global_symbols : cu->list_in_scope);
63d06c5c 10746
64382290
TT
10747 /* The semantics of C++ state that "struct foo {
10748 ... }" also defines a typedef for "foo". A Java
10749 class declaration also defines a typedef for the
10750 class. */
10751 if (cu->language == language_cplus
10752 || cu->language == language_java
10753 || cu->language == language_ada)
10754 {
10755 /* The symbol's name is already allocated along
10756 with this objfile, so we don't need to
10757 duplicate it for the type. */
10758 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10759 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10760 }
63d06c5c
DC
10761 }
10762 }
c906108c
SS
10763 break;
10764 case DW_TAG_typedef:
63d06c5c
DC
10765 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10766 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10767 list_to_add = cu->list_in_scope;
63d06c5c 10768 break;
c906108c 10769 case DW_TAG_base_type:
a02abb62 10770 case DW_TAG_subrange_type:
c906108c 10771 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10772 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10773 list_to_add = cu->list_in_scope;
c906108c
SS
10774 break;
10775 case DW_TAG_enumerator:
e142c38c 10776 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10777 if (attr)
10778 {
e7c27a73 10779 dwarf2_const_value (attr, sym, cu);
c906108c 10780 }
63d06c5c
DC
10781 {
10782 /* NOTE: carlton/2003-11-10: See comment above in the
10783 DW_TAG_class_type, etc. block. */
10784
e142c38c 10785 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10786 && (cu->language == language_cplus
10787 || cu->language == language_java)
e142c38c 10788 ? &global_symbols : cu->list_in_scope);
63d06c5c 10789 }
c906108c 10790 break;
5c4e30ca
DC
10791 case DW_TAG_namespace:
10792 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10793 list_to_add = &global_symbols;
5c4e30ca 10794 break;
c906108c
SS
10795 default:
10796 /* Not a tag we recognize. Hopefully we aren't processing
10797 trash data, but since we must specifically ignore things
10798 we don't recognize, there is nothing else we should do at
10799 this point. */
e2e0b3e5 10800 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10801 dwarf_tag_name (die->tag));
c906108c
SS
10802 break;
10803 }
df8a16a1 10804
e37fd15a
SW
10805 if (suppress_add)
10806 {
10807 sym->hash_next = objfile->template_symbols;
10808 objfile->template_symbols = sym;
10809 list_to_add = NULL;
10810 }
10811
10812 if (list_to_add != NULL)
10813 add_symbol_to_list (sym, list_to_add);
10814
df8a16a1
DJ
10815 /* For the benefit of old versions of GCC, check for anonymous
10816 namespaces based on the demangled name. */
10817 if (!processing_has_namespace_info
94af9270 10818 && cu->language == language_cplus)
df8a16a1 10819 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10820 }
10821 return (sym);
10822}
10823
34eaf542
TT
10824/* A wrapper for new_symbol_full that always allocates a new symbol. */
10825
10826static struct symbol *
10827new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10828{
10829 return new_symbol_full (die, type, cu, NULL);
10830}
10831
98bfdba5
PA
10832/* Given an attr with a DW_FORM_dataN value in host byte order,
10833 zero-extend it as appropriate for the symbol's type. The DWARF
10834 standard (v4) is not entirely clear about the meaning of using
10835 DW_FORM_dataN for a constant with a signed type, where the type is
10836 wider than the data. The conclusion of a discussion on the DWARF
10837 list was that this is unspecified. We choose to always zero-extend
10838 because that is the interpretation long in use by GCC. */
c906108c 10839
98bfdba5
PA
10840static gdb_byte *
10841dwarf2_const_value_data (struct attribute *attr, struct type *type,
10842 const char *name, struct obstack *obstack,
10843 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10844{
e7c27a73 10845 struct objfile *objfile = cu->objfile;
e17a4113
UW
10846 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10847 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10848 LONGEST l = DW_UNSND (attr);
10849
10850 if (bits < sizeof (*value) * 8)
10851 {
10852 l &= ((LONGEST) 1 << bits) - 1;
10853 *value = l;
10854 }
10855 else if (bits == sizeof (*value) * 8)
10856 *value = l;
10857 else
10858 {
10859 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10860 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10861 return bytes;
10862 }
10863
10864 return NULL;
10865}
10866
10867/* Read a constant value from an attribute. Either set *VALUE, or if
10868 the value does not fit in *VALUE, set *BYTES - either already
10869 allocated on the objfile obstack, or newly allocated on OBSTACK,
10870 or, set *BATON, if we translated the constant to a location
10871 expression. */
10872
10873static void
10874dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10875 const char *name, struct obstack *obstack,
10876 struct dwarf2_cu *cu,
10877 long *value, gdb_byte **bytes,
10878 struct dwarf2_locexpr_baton **baton)
10879{
10880 struct objfile *objfile = cu->objfile;
10881 struct comp_unit_head *cu_header = &cu->header;
c906108c 10882 struct dwarf_block *blk;
98bfdba5
PA
10883 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10884 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10885
10886 *value = 0;
10887 *bytes = NULL;
10888 *baton = NULL;
c906108c
SS
10889
10890 switch (attr->form)
10891 {
10892 case DW_FORM_addr:
ac56253d 10893 {
ac56253d
TT
10894 gdb_byte *data;
10895
98bfdba5
PA
10896 if (TYPE_LENGTH (type) != cu_header->addr_size)
10897 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10898 cu_header->addr_size,
98bfdba5 10899 TYPE_LENGTH (type));
ac56253d
TT
10900 /* Symbols of this form are reasonably rare, so we just
10901 piggyback on the existing location code rather than writing
10902 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10903 *baton = obstack_alloc (&objfile->objfile_obstack,
10904 sizeof (struct dwarf2_locexpr_baton));
10905 (*baton)->per_cu = cu->per_cu;
10906 gdb_assert ((*baton)->per_cu);
ac56253d 10907
98bfdba5
PA
10908 (*baton)->size = 2 + cu_header->addr_size;
10909 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10910 (*baton)->data = data;
ac56253d
TT
10911
10912 data[0] = DW_OP_addr;
10913 store_unsigned_integer (&data[1], cu_header->addr_size,
10914 byte_order, DW_ADDR (attr));
10915 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10916 }
c906108c 10917 break;
4ac36638 10918 case DW_FORM_string:
93b5768b 10919 case DW_FORM_strp:
98bfdba5
PA
10920 /* DW_STRING is already allocated on the objfile obstack, point
10921 directly to it. */
10922 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10923 break;
c906108c
SS
10924 case DW_FORM_block1:
10925 case DW_FORM_block2:
10926 case DW_FORM_block4:
10927 case DW_FORM_block:
2dc7f7b3 10928 case DW_FORM_exprloc:
c906108c 10929 blk = DW_BLOCK (attr);
98bfdba5
PA
10930 if (TYPE_LENGTH (type) != blk->size)
10931 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10932 TYPE_LENGTH (type));
10933 *bytes = blk->data;
c906108c 10934 break;
2df3850c
JM
10935
10936 /* The DW_AT_const_value attributes are supposed to carry the
10937 symbol's value "represented as it would be on the target
10938 architecture." By the time we get here, it's already been
10939 converted to host endianness, so we just need to sign- or
10940 zero-extend it as appropriate. */
10941 case DW_FORM_data1:
98bfdba5 10942 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10943 break;
c906108c 10944 case DW_FORM_data2:
98bfdba5 10945 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10946 break;
c906108c 10947 case DW_FORM_data4:
98bfdba5 10948 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10949 break;
c906108c 10950 case DW_FORM_data8:
98bfdba5 10951 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
10952 break;
10953
c906108c 10954 case DW_FORM_sdata:
98bfdba5 10955 *value = DW_SND (attr);
2df3850c
JM
10956 break;
10957
c906108c 10958 case DW_FORM_udata:
98bfdba5 10959 *value = DW_UNSND (attr);
c906108c 10960 break;
2df3850c 10961
c906108c 10962 default:
4d3c2250 10963 complaint (&symfile_complaints,
e2e0b3e5 10964 _("unsupported const value attribute form: '%s'"),
4d3c2250 10965 dwarf_form_name (attr->form));
98bfdba5 10966 *value = 0;
c906108c
SS
10967 break;
10968 }
10969}
10970
2df3850c 10971
98bfdba5
PA
10972/* Copy constant value from an attribute to a symbol. */
10973
2df3850c 10974static void
98bfdba5
PA
10975dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10976 struct dwarf2_cu *cu)
2df3850c 10977{
98bfdba5
PA
10978 struct objfile *objfile = cu->objfile;
10979 struct comp_unit_head *cu_header = &cu->header;
10980 long value;
10981 gdb_byte *bytes;
10982 struct dwarf2_locexpr_baton *baton;
2df3850c 10983
98bfdba5
PA
10984 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10985 SYMBOL_PRINT_NAME (sym),
10986 &objfile->objfile_obstack, cu,
10987 &value, &bytes, &baton);
2df3850c 10988
98bfdba5
PA
10989 if (baton != NULL)
10990 {
10991 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10992 SYMBOL_LOCATION_BATON (sym) = baton;
10993 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10994 }
10995 else if (bytes != NULL)
10996 {
10997 SYMBOL_VALUE_BYTES (sym) = bytes;
10998 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10999 }
11000 else
11001 {
11002 SYMBOL_VALUE (sym) = value;
11003 SYMBOL_CLASS (sym) = LOC_CONST;
11004 }
2df3850c
JM
11005}
11006
c906108c
SS
11007/* Return the type of the die in question using its DW_AT_type attribute. */
11008
11009static struct type *
e7c27a73 11010die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11011{
c906108c 11012 struct attribute *type_attr;
c906108c 11013
e142c38c 11014 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11015 if (!type_attr)
11016 {
11017 /* A missing DW_AT_type represents a void type. */
46bf5051 11018 return objfile_type (cu->objfile)->builtin_void;
c906108c 11019 }
348e048f 11020
673bfd45 11021 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11022}
11023
b4ba55a1
JB
11024/* True iff CU's producer generates GNAT Ada auxiliary information
11025 that allows to find parallel types through that information instead
11026 of having to do expensive parallel lookups by type name. */
11027
11028static int
11029need_gnat_info (struct dwarf2_cu *cu)
11030{
11031 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11032 of GNAT produces this auxiliary information, without any indication
11033 that it is produced. Part of enhancing the FSF version of GNAT
11034 to produce that information will be to put in place an indicator
11035 that we can use in order to determine whether the descriptive type
11036 info is available or not. One suggestion that has been made is
11037 to use a new attribute, attached to the CU die. For now, assume
11038 that the descriptive type info is not available. */
11039 return 0;
11040}
11041
b4ba55a1
JB
11042/* Return the auxiliary type of the die in question using its
11043 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11044 attribute is not present. */
11045
11046static struct type *
11047die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11048{
b4ba55a1 11049 struct attribute *type_attr;
b4ba55a1
JB
11050
11051 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11052 if (!type_attr)
11053 return NULL;
11054
673bfd45 11055 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11056}
11057
11058/* If DIE has a descriptive_type attribute, then set the TYPE's
11059 descriptive type accordingly. */
11060
11061static void
11062set_descriptive_type (struct type *type, struct die_info *die,
11063 struct dwarf2_cu *cu)
11064{
11065 struct type *descriptive_type = die_descriptive_type (die, cu);
11066
11067 if (descriptive_type)
11068 {
11069 ALLOCATE_GNAT_AUX_TYPE (type);
11070 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11071 }
11072}
11073
c906108c
SS
11074/* Return the containing type of the die in question using its
11075 DW_AT_containing_type attribute. */
11076
11077static struct type *
e7c27a73 11078die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11079{
c906108c 11080 struct attribute *type_attr;
c906108c 11081
e142c38c 11082 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11083 if (!type_attr)
11084 error (_("Dwarf Error: Problem turning containing type into gdb type "
11085 "[in module %s]"), cu->objfile->name);
11086
673bfd45 11087 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11088}
11089
673bfd45
DE
11090/* Look up the type of DIE in CU using its type attribute ATTR.
11091 If there is no type substitute an error marker. */
11092
c906108c 11093static struct type *
673bfd45
DE
11094lookup_die_type (struct die_info *die, struct attribute *attr,
11095 struct dwarf2_cu *cu)
c906108c 11096{
f792889a
DJ
11097 struct type *this_type;
11098
673bfd45
DE
11099 /* First see if we have it cached. */
11100
11101 if (is_ref_attr (attr))
11102 {
11103 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11104
11105 this_type = get_die_type_at_offset (offset, cu->per_cu);
11106 }
11107 else if (attr->form == DW_FORM_sig8)
11108 {
11109 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11110 struct dwarf2_cu *sig_cu;
11111 unsigned int offset;
11112
11113 /* sig_type will be NULL if the signatured type is missing from
11114 the debug info. */
11115 if (sig_type == NULL)
11116 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11117 "at 0x%x [in module %s]"),
11118 die->offset, cu->objfile->name);
11119
11120 gdb_assert (sig_type->per_cu.from_debug_types);
11121 offset = sig_type->offset + sig_type->type_offset;
11122 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11123 }
11124 else
11125 {
11126 dump_die_for_error (die);
11127 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11128 dwarf_attr_name (attr->name), cu->objfile->name);
11129 }
11130
11131 /* If not cached we need to read it in. */
11132
11133 if (this_type == NULL)
11134 {
11135 struct die_info *type_die;
11136 struct dwarf2_cu *type_cu = cu;
11137
11138 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11139 /* If the type is cached, we should have found it above. */
11140 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11141 this_type = read_type_die_1 (type_die, type_cu);
11142 }
11143
11144 /* If we still don't have a type use an error marker. */
11145
11146 if (this_type == NULL)
c906108c 11147 {
b00fdb78
TT
11148 char *message, *saved;
11149
11150 /* read_type_die already issued a complaint. */
11151 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11152 cu->objfile->name,
11153 cu->header.offset,
11154 die->offset);
11155 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11156 message, strlen (message));
11157 xfree (message);
11158
11159 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11160 }
673bfd45 11161
f792889a 11162 return this_type;
c906108c
SS
11163}
11164
673bfd45
DE
11165/* Return the type in DIE, CU.
11166 Returns NULL for invalid types.
11167
11168 This first does a lookup in the appropriate type_hash table,
11169 and only reads the die in if necessary.
11170
11171 NOTE: This can be called when reading in partial or full symbols. */
11172
f792889a 11173static struct type *
e7c27a73 11174read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11175{
f792889a
DJ
11176 struct type *this_type;
11177
11178 this_type = get_die_type (die, cu);
11179 if (this_type)
11180 return this_type;
11181
673bfd45
DE
11182 return read_type_die_1 (die, cu);
11183}
11184
11185/* Read the type in DIE, CU.
11186 Returns NULL for invalid types. */
11187
11188static struct type *
11189read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11190{
11191 struct type *this_type = NULL;
11192
c906108c
SS
11193 switch (die->tag)
11194 {
11195 case DW_TAG_class_type:
680b30c7 11196 case DW_TAG_interface_type:
c906108c
SS
11197 case DW_TAG_structure_type:
11198 case DW_TAG_union_type:
f792889a 11199 this_type = read_structure_type (die, cu);
c906108c
SS
11200 break;
11201 case DW_TAG_enumeration_type:
f792889a 11202 this_type = read_enumeration_type (die, cu);
c906108c
SS
11203 break;
11204 case DW_TAG_subprogram:
11205 case DW_TAG_subroutine_type:
edb3359d 11206 case DW_TAG_inlined_subroutine:
f792889a 11207 this_type = read_subroutine_type (die, cu);
c906108c
SS
11208 break;
11209 case DW_TAG_array_type:
f792889a 11210 this_type = read_array_type (die, cu);
c906108c 11211 break;
72019c9c 11212 case DW_TAG_set_type:
f792889a 11213 this_type = read_set_type (die, cu);
72019c9c 11214 break;
c906108c 11215 case DW_TAG_pointer_type:
f792889a 11216 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11217 break;
11218 case DW_TAG_ptr_to_member_type:
f792889a 11219 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11220 break;
11221 case DW_TAG_reference_type:
f792889a 11222 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11223 break;
11224 case DW_TAG_const_type:
f792889a 11225 this_type = read_tag_const_type (die, cu);
c906108c
SS
11226 break;
11227 case DW_TAG_volatile_type:
f792889a 11228 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11229 break;
11230 case DW_TAG_string_type:
f792889a 11231 this_type = read_tag_string_type (die, cu);
c906108c
SS
11232 break;
11233 case DW_TAG_typedef:
f792889a 11234 this_type = read_typedef (die, cu);
c906108c 11235 break;
a02abb62 11236 case DW_TAG_subrange_type:
f792889a 11237 this_type = read_subrange_type (die, cu);
a02abb62 11238 break;
c906108c 11239 case DW_TAG_base_type:
f792889a 11240 this_type = read_base_type (die, cu);
c906108c 11241 break;
81a17f79 11242 case DW_TAG_unspecified_type:
f792889a 11243 this_type = read_unspecified_type (die, cu);
81a17f79 11244 break;
0114d602
DJ
11245 case DW_TAG_namespace:
11246 this_type = read_namespace_type (die, cu);
11247 break;
f55ee35c
JK
11248 case DW_TAG_module:
11249 this_type = read_module_type (die, cu);
11250 break;
c906108c 11251 default:
a1f5b845 11252 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11253 dwarf_tag_name (die->tag));
c906108c
SS
11254 break;
11255 }
63d06c5c 11256
f792889a 11257 return this_type;
63d06c5c
DC
11258}
11259
fdde2d81 11260/* Return the name of the namespace/class that DIE is defined within,
0114d602 11261 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11262
0114d602
DJ
11263 For example, if we're within the method foo() in the following
11264 code:
11265
11266 namespace N {
11267 class C {
11268 void foo () {
11269 }
11270 };
11271 }
11272
11273 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11274
11275static char *
e142c38c 11276determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11277{
0114d602
DJ
11278 struct die_info *parent, *spec_die;
11279 struct dwarf2_cu *spec_cu;
11280 struct type *parent_type;
63d06c5c 11281
f55ee35c
JK
11282 if (cu->language != language_cplus && cu->language != language_java
11283 && cu->language != language_fortran)
0114d602
DJ
11284 return "";
11285
11286 /* We have to be careful in the presence of DW_AT_specification.
11287 For example, with GCC 3.4, given the code
11288
11289 namespace N {
11290 void foo() {
11291 // Definition of N::foo.
11292 }
11293 }
11294
11295 then we'll have a tree of DIEs like this:
11296
11297 1: DW_TAG_compile_unit
11298 2: DW_TAG_namespace // N
11299 3: DW_TAG_subprogram // declaration of N::foo
11300 4: DW_TAG_subprogram // definition of N::foo
11301 DW_AT_specification // refers to die #3
11302
11303 Thus, when processing die #4, we have to pretend that we're in
11304 the context of its DW_AT_specification, namely the contex of die
11305 #3. */
11306 spec_cu = cu;
11307 spec_die = die_specification (die, &spec_cu);
11308 if (spec_die == NULL)
11309 parent = die->parent;
11310 else
63d06c5c 11311 {
0114d602
DJ
11312 parent = spec_die->parent;
11313 cu = spec_cu;
63d06c5c 11314 }
0114d602
DJ
11315
11316 if (parent == NULL)
11317 return "";
98bfdba5
PA
11318 else if (parent->building_fullname)
11319 {
11320 const char *name;
11321 const char *parent_name;
11322
11323 /* It has been seen on RealView 2.2 built binaries,
11324 DW_TAG_template_type_param types actually _defined_ as
11325 children of the parent class:
11326
11327 enum E {};
11328 template class <class Enum> Class{};
11329 Class<enum E> class_e;
11330
11331 1: DW_TAG_class_type (Class)
11332 2: DW_TAG_enumeration_type (E)
11333 3: DW_TAG_enumerator (enum1:0)
11334 3: DW_TAG_enumerator (enum2:1)
11335 ...
11336 2: DW_TAG_template_type_param
11337 DW_AT_type DW_FORM_ref_udata (E)
11338
11339 Besides being broken debug info, it can put GDB into an
11340 infinite loop. Consider:
11341
11342 When we're building the full name for Class<E>, we'll start
11343 at Class, and go look over its template type parameters,
11344 finding E. We'll then try to build the full name of E, and
11345 reach here. We're now trying to build the full name of E,
11346 and look over the parent DIE for containing scope. In the
11347 broken case, if we followed the parent DIE of E, we'd again
11348 find Class, and once again go look at its template type
11349 arguments, etc., etc. Simply don't consider such parent die
11350 as source-level parent of this die (it can't be, the language
11351 doesn't allow it), and break the loop here. */
11352 name = dwarf2_name (die, cu);
11353 parent_name = dwarf2_name (parent, cu);
11354 complaint (&symfile_complaints,
11355 _("template param type '%s' defined within parent '%s'"),
11356 name ? name : "<unknown>",
11357 parent_name ? parent_name : "<unknown>");
11358 return "";
11359 }
63d06c5c 11360 else
0114d602
DJ
11361 switch (parent->tag)
11362 {
63d06c5c 11363 case DW_TAG_namespace:
0114d602 11364 parent_type = read_type_die (parent, cu);
acebe513
UW
11365 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11366 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11367 Work around this problem here. */
11368 if (cu->language == language_cplus
11369 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11370 return "";
0114d602
DJ
11371 /* We give a name to even anonymous namespaces. */
11372 return TYPE_TAG_NAME (parent_type);
63d06c5c 11373 case DW_TAG_class_type:
680b30c7 11374 case DW_TAG_interface_type:
63d06c5c 11375 case DW_TAG_structure_type:
0114d602 11376 case DW_TAG_union_type:
f55ee35c 11377 case DW_TAG_module:
0114d602
DJ
11378 parent_type = read_type_die (parent, cu);
11379 if (TYPE_TAG_NAME (parent_type) != NULL)
11380 return TYPE_TAG_NAME (parent_type);
11381 else
11382 /* An anonymous structure is only allowed non-static data
11383 members; no typedefs, no member functions, et cetera.
11384 So it does not need a prefix. */
11385 return "";
63d06c5c 11386 default:
8176b9b8 11387 return determine_prefix (parent, cu);
63d06c5c 11388 }
63d06c5c
DC
11389}
11390
987504bb
JJ
11391/* Return a newly-allocated string formed by concatenating PREFIX and
11392 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11393 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11394 perform an obconcat, otherwise allocate storage for the result. The CU argument
11395 is used to determine the language and hence, the appropriate separator. */
11396
f55ee35c 11397#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11398
11399static char *
f55ee35c
JK
11400typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11401 int physname, struct dwarf2_cu *cu)
63d06c5c 11402{
f55ee35c 11403 const char *lead = "";
5c315b68 11404 const char *sep;
63d06c5c 11405
987504bb
JJ
11406 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11407 sep = "";
11408 else if (cu->language == language_java)
11409 sep = ".";
f55ee35c
JK
11410 else if (cu->language == language_fortran && physname)
11411 {
11412 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11413 DW_AT_MIPS_linkage_name is preferred and used instead. */
11414
11415 lead = "__";
11416 sep = "_MOD_";
11417 }
987504bb
JJ
11418 else
11419 sep = "::";
63d06c5c 11420
6dd47d34
DE
11421 if (prefix == NULL)
11422 prefix = "";
11423 if (suffix == NULL)
11424 suffix = "";
11425
987504bb
JJ
11426 if (obs == NULL)
11427 {
11428 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11429
f55ee35c
JK
11430 strcpy (retval, lead);
11431 strcat (retval, prefix);
6dd47d34
DE
11432 strcat (retval, sep);
11433 strcat (retval, suffix);
63d06c5c
DC
11434 return retval;
11435 }
987504bb
JJ
11436 else
11437 {
11438 /* We have an obstack. */
f55ee35c 11439 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11440 }
63d06c5c
DC
11441}
11442
c906108c
SS
11443/* Return sibling of die, NULL if no sibling. */
11444
f9aca02d 11445static struct die_info *
fba45db2 11446sibling_die (struct die_info *die)
c906108c 11447{
639d11d3 11448 return die->sibling;
c906108c
SS
11449}
11450
71c25dea
TT
11451/* Get name of a die, return NULL if not found. */
11452
11453static char *
11454dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11455 struct obstack *obstack)
11456{
11457 if (name && cu->language == language_cplus)
11458 {
11459 char *canon_name = cp_canonicalize_string (name);
11460
11461 if (canon_name != NULL)
11462 {
11463 if (strcmp (canon_name, name) != 0)
11464 name = obsavestring (canon_name, strlen (canon_name),
11465 obstack);
11466 xfree (canon_name);
11467 }
11468 }
11469
11470 return name;
c906108c
SS
11471}
11472
9219021c
DC
11473/* Get name of a die, return NULL if not found. */
11474
11475static char *
e142c38c 11476dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11477{
11478 struct attribute *attr;
11479
e142c38c 11480 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11481 if (!attr || !DW_STRING (attr))
11482 return NULL;
11483
11484 switch (die->tag)
11485 {
11486 case DW_TAG_compile_unit:
11487 /* Compilation units have a DW_AT_name that is a filename, not
11488 a source language identifier. */
11489 case DW_TAG_enumeration_type:
11490 case DW_TAG_enumerator:
11491 /* These tags always have simple identifiers already; no need
11492 to canonicalize them. */
11493 return DW_STRING (attr);
907af001 11494
418835cc
KS
11495 case DW_TAG_subprogram:
11496 /* Java constructors will all be named "<init>", so return
11497 the class name when we see this special case. */
11498 if (cu->language == language_java
11499 && DW_STRING (attr) != NULL
11500 && strcmp (DW_STRING (attr), "<init>") == 0)
11501 {
11502 struct dwarf2_cu *spec_cu = cu;
11503 struct die_info *spec_die;
11504
11505 /* GCJ will output '<init>' for Java constructor names.
11506 For this special case, return the name of the parent class. */
11507
11508 /* GCJ may output suprogram DIEs with AT_specification set.
11509 If so, use the name of the specified DIE. */
11510 spec_die = die_specification (die, &spec_cu);
11511 if (spec_die != NULL)
11512 return dwarf2_name (spec_die, spec_cu);
11513
11514 do
11515 {
11516 die = die->parent;
11517 if (die->tag == DW_TAG_class_type)
11518 return dwarf2_name (die, cu);
11519 }
11520 while (die->tag != DW_TAG_compile_unit);
11521 }
907af001
UW
11522 break;
11523
11524 case DW_TAG_class_type:
11525 case DW_TAG_interface_type:
11526 case DW_TAG_structure_type:
11527 case DW_TAG_union_type:
11528 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11529 structures or unions. These were of the form "._%d" in GCC 4.1,
11530 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11531 and GCC 4.4. We work around this problem by ignoring these. */
11532 if (strncmp (DW_STRING (attr), "._", 2) == 0
11533 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11534 return NULL;
11535 break;
11536
71c25dea 11537 default:
907af001
UW
11538 break;
11539 }
11540
11541 if (!DW_STRING_IS_CANONICAL (attr))
11542 {
11543 DW_STRING (attr)
11544 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11545 &cu->objfile->objfile_obstack);
11546 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11547 }
907af001 11548 return DW_STRING (attr);
9219021c
DC
11549}
11550
11551/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11552 is none. *EXT_CU is the CU containing DIE on input, and the CU
11553 containing the return value on output. */
9219021c
DC
11554
11555static struct die_info *
f2f0e013 11556dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11557{
11558 struct attribute *attr;
9219021c 11559
f2f0e013 11560 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11561 if (attr == NULL)
11562 return NULL;
11563
f2f0e013 11564 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11565}
11566
c906108c
SS
11567/* Convert a DIE tag into its string name. */
11568
11569static char *
aa1ee363 11570dwarf_tag_name (unsigned tag)
c906108c
SS
11571{
11572 switch (tag)
11573 {
11574 case DW_TAG_padding:
11575 return "DW_TAG_padding";
11576 case DW_TAG_array_type:
11577 return "DW_TAG_array_type";
11578 case DW_TAG_class_type:
11579 return "DW_TAG_class_type";
11580 case DW_TAG_entry_point:
11581 return "DW_TAG_entry_point";
11582 case DW_TAG_enumeration_type:
11583 return "DW_TAG_enumeration_type";
11584 case DW_TAG_formal_parameter:
11585 return "DW_TAG_formal_parameter";
11586 case DW_TAG_imported_declaration:
11587 return "DW_TAG_imported_declaration";
11588 case DW_TAG_label:
11589 return "DW_TAG_label";
11590 case DW_TAG_lexical_block:
11591 return "DW_TAG_lexical_block";
11592 case DW_TAG_member:
11593 return "DW_TAG_member";
11594 case DW_TAG_pointer_type:
11595 return "DW_TAG_pointer_type";
11596 case DW_TAG_reference_type:
11597 return "DW_TAG_reference_type";
11598 case DW_TAG_compile_unit:
11599 return "DW_TAG_compile_unit";
11600 case DW_TAG_string_type:
11601 return "DW_TAG_string_type";
11602 case DW_TAG_structure_type:
11603 return "DW_TAG_structure_type";
11604 case DW_TAG_subroutine_type:
11605 return "DW_TAG_subroutine_type";
11606 case DW_TAG_typedef:
11607 return "DW_TAG_typedef";
11608 case DW_TAG_union_type:
11609 return "DW_TAG_union_type";
11610 case DW_TAG_unspecified_parameters:
11611 return "DW_TAG_unspecified_parameters";
11612 case DW_TAG_variant:
11613 return "DW_TAG_variant";
11614 case DW_TAG_common_block:
11615 return "DW_TAG_common_block";
11616 case DW_TAG_common_inclusion:
11617 return "DW_TAG_common_inclusion";
11618 case DW_TAG_inheritance:
11619 return "DW_TAG_inheritance";
11620 case DW_TAG_inlined_subroutine:
11621 return "DW_TAG_inlined_subroutine";
11622 case DW_TAG_module:
11623 return "DW_TAG_module";
11624 case DW_TAG_ptr_to_member_type:
11625 return "DW_TAG_ptr_to_member_type";
11626 case DW_TAG_set_type:
11627 return "DW_TAG_set_type";
11628 case DW_TAG_subrange_type:
11629 return "DW_TAG_subrange_type";
11630 case DW_TAG_with_stmt:
11631 return "DW_TAG_with_stmt";
11632 case DW_TAG_access_declaration:
11633 return "DW_TAG_access_declaration";
11634 case DW_TAG_base_type:
11635 return "DW_TAG_base_type";
11636 case DW_TAG_catch_block:
11637 return "DW_TAG_catch_block";
11638 case DW_TAG_const_type:
11639 return "DW_TAG_const_type";
11640 case DW_TAG_constant:
11641 return "DW_TAG_constant";
11642 case DW_TAG_enumerator:
11643 return "DW_TAG_enumerator";
11644 case DW_TAG_file_type:
11645 return "DW_TAG_file_type";
11646 case DW_TAG_friend:
11647 return "DW_TAG_friend";
11648 case DW_TAG_namelist:
11649 return "DW_TAG_namelist";
11650 case DW_TAG_namelist_item:
11651 return "DW_TAG_namelist_item";
11652 case DW_TAG_packed_type:
11653 return "DW_TAG_packed_type";
11654 case DW_TAG_subprogram:
11655 return "DW_TAG_subprogram";
11656 case DW_TAG_template_type_param:
11657 return "DW_TAG_template_type_param";
11658 case DW_TAG_template_value_param:
11659 return "DW_TAG_template_value_param";
11660 case DW_TAG_thrown_type:
11661 return "DW_TAG_thrown_type";
11662 case DW_TAG_try_block:
11663 return "DW_TAG_try_block";
11664 case DW_TAG_variant_part:
11665 return "DW_TAG_variant_part";
11666 case DW_TAG_variable:
11667 return "DW_TAG_variable";
11668 case DW_TAG_volatile_type:
11669 return "DW_TAG_volatile_type";
d9fa45fe
DC
11670 case DW_TAG_dwarf_procedure:
11671 return "DW_TAG_dwarf_procedure";
11672 case DW_TAG_restrict_type:
11673 return "DW_TAG_restrict_type";
11674 case DW_TAG_interface_type:
11675 return "DW_TAG_interface_type";
11676 case DW_TAG_namespace:
11677 return "DW_TAG_namespace";
11678 case DW_TAG_imported_module:
11679 return "DW_TAG_imported_module";
11680 case DW_TAG_unspecified_type:
11681 return "DW_TAG_unspecified_type";
11682 case DW_TAG_partial_unit:
11683 return "DW_TAG_partial_unit";
11684 case DW_TAG_imported_unit:
11685 return "DW_TAG_imported_unit";
b7619582
GF
11686 case DW_TAG_condition:
11687 return "DW_TAG_condition";
11688 case DW_TAG_shared_type:
11689 return "DW_TAG_shared_type";
348e048f
DE
11690 case DW_TAG_type_unit:
11691 return "DW_TAG_type_unit";
c906108c
SS
11692 case DW_TAG_MIPS_loop:
11693 return "DW_TAG_MIPS_loop";
b7619582
GF
11694 case DW_TAG_HP_array_descriptor:
11695 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11696 case DW_TAG_format_label:
11697 return "DW_TAG_format_label";
11698 case DW_TAG_function_template:
11699 return "DW_TAG_function_template";
11700 case DW_TAG_class_template:
11701 return "DW_TAG_class_template";
b7619582
GF
11702 case DW_TAG_GNU_BINCL:
11703 return "DW_TAG_GNU_BINCL";
11704 case DW_TAG_GNU_EINCL:
11705 return "DW_TAG_GNU_EINCL";
11706 case DW_TAG_upc_shared_type:
11707 return "DW_TAG_upc_shared_type";
11708 case DW_TAG_upc_strict_type:
11709 return "DW_TAG_upc_strict_type";
11710 case DW_TAG_upc_relaxed_type:
11711 return "DW_TAG_upc_relaxed_type";
11712 case DW_TAG_PGI_kanji_type:
11713 return "DW_TAG_PGI_kanji_type";
11714 case DW_TAG_PGI_interface_block:
11715 return "DW_TAG_PGI_interface_block";
c906108c
SS
11716 default:
11717 return "DW_TAG_<unknown>";
11718 }
11719}
11720
11721/* Convert a DWARF attribute code into its string name. */
11722
11723static char *
aa1ee363 11724dwarf_attr_name (unsigned attr)
c906108c
SS
11725{
11726 switch (attr)
11727 {
11728 case DW_AT_sibling:
11729 return "DW_AT_sibling";
11730 case DW_AT_location:
11731 return "DW_AT_location";
11732 case DW_AT_name:
11733 return "DW_AT_name";
11734 case DW_AT_ordering:
11735 return "DW_AT_ordering";
11736 case DW_AT_subscr_data:
11737 return "DW_AT_subscr_data";
11738 case DW_AT_byte_size:
11739 return "DW_AT_byte_size";
11740 case DW_AT_bit_offset:
11741 return "DW_AT_bit_offset";
11742 case DW_AT_bit_size:
11743 return "DW_AT_bit_size";
11744 case DW_AT_element_list:
11745 return "DW_AT_element_list";
11746 case DW_AT_stmt_list:
11747 return "DW_AT_stmt_list";
11748 case DW_AT_low_pc:
11749 return "DW_AT_low_pc";
11750 case DW_AT_high_pc:
11751 return "DW_AT_high_pc";
11752 case DW_AT_language:
11753 return "DW_AT_language";
11754 case DW_AT_member:
11755 return "DW_AT_member";
11756 case DW_AT_discr:
11757 return "DW_AT_discr";
11758 case DW_AT_discr_value:
11759 return "DW_AT_discr_value";
11760 case DW_AT_visibility:
11761 return "DW_AT_visibility";
11762 case DW_AT_import:
11763 return "DW_AT_import";
11764 case DW_AT_string_length:
11765 return "DW_AT_string_length";
11766 case DW_AT_common_reference:
11767 return "DW_AT_common_reference";
11768 case DW_AT_comp_dir:
11769 return "DW_AT_comp_dir";
11770 case DW_AT_const_value:
11771 return "DW_AT_const_value";
11772 case DW_AT_containing_type:
11773 return "DW_AT_containing_type";
11774 case DW_AT_default_value:
11775 return "DW_AT_default_value";
11776 case DW_AT_inline:
11777 return "DW_AT_inline";
11778 case DW_AT_is_optional:
11779 return "DW_AT_is_optional";
11780 case DW_AT_lower_bound:
11781 return "DW_AT_lower_bound";
11782 case DW_AT_producer:
11783 return "DW_AT_producer";
11784 case DW_AT_prototyped:
11785 return "DW_AT_prototyped";
11786 case DW_AT_return_addr:
11787 return "DW_AT_return_addr";
11788 case DW_AT_start_scope:
11789 return "DW_AT_start_scope";
09fa0d7c
JK
11790 case DW_AT_bit_stride:
11791 return "DW_AT_bit_stride";
c906108c
SS
11792 case DW_AT_upper_bound:
11793 return "DW_AT_upper_bound";
11794 case DW_AT_abstract_origin:
11795 return "DW_AT_abstract_origin";
11796 case DW_AT_accessibility:
11797 return "DW_AT_accessibility";
11798 case DW_AT_address_class:
11799 return "DW_AT_address_class";
11800 case DW_AT_artificial:
11801 return "DW_AT_artificial";
11802 case DW_AT_base_types:
11803 return "DW_AT_base_types";
11804 case DW_AT_calling_convention:
11805 return "DW_AT_calling_convention";
11806 case DW_AT_count:
11807 return "DW_AT_count";
11808 case DW_AT_data_member_location:
11809 return "DW_AT_data_member_location";
11810 case DW_AT_decl_column:
11811 return "DW_AT_decl_column";
11812 case DW_AT_decl_file:
11813 return "DW_AT_decl_file";
11814 case DW_AT_decl_line:
11815 return "DW_AT_decl_line";
11816 case DW_AT_declaration:
11817 return "DW_AT_declaration";
11818 case DW_AT_discr_list:
11819 return "DW_AT_discr_list";
11820 case DW_AT_encoding:
11821 return "DW_AT_encoding";
11822 case DW_AT_external:
11823 return "DW_AT_external";
11824 case DW_AT_frame_base:
11825 return "DW_AT_frame_base";
11826 case DW_AT_friend:
11827 return "DW_AT_friend";
11828 case DW_AT_identifier_case:
11829 return "DW_AT_identifier_case";
11830 case DW_AT_macro_info:
11831 return "DW_AT_macro_info";
11832 case DW_AT_namelist_items:
11833 return "DW_AT_namelist_items";
11834 case DW_AT_priority:
11835 return "DW_AT_priority";
11836 case DW_AT_segment:
11837 return "DW_AT_segment";
11838 case DW_AT_specification:
11839 return "DW_AT_specification";
11840 case DW_AT_static_link:
11841 return "DW_AT_static_link";
11842 case DW_AT_type:
11843 return "DW_AT_type";
11844 case DW_AT_use_location:
11845 return "DW_AT_use_location";
11846 case DW_AT_variable_parameter:
11847 return "DW_AT_variable_parameter";
11848 case DW_AT_virtuality:
11849 return "DW_AT_virtuality";
11850 case DW_AT_vtable_elem_location:
11851 return "DW_AT_vtable_elem_location";
b7619582 11852 /* DWARF 3 values. */
d9fa45fe
DC
11853 case DW_AT_allocated:
11854 return "DW_AT_allocated";
11855 case DW_AT_associated:
11856 return "DW_AT_associated";
11857 case DW_AT_data_location:
11858 return "DW_AT_data_location";
09fa0d7c
JK
11859 case DW_AT_byte_stride:
11860 return "DW_AT_byte_stride";
d9fa45fe
DC
11861 case DW_AT_entry_pc:
11862 return "DW_AT_entry_pc";
11863 case DW_AT_use_UTF8:
11864 return "DW_AT_use_UTF8";
11865 case DW_AT_extension:
11866 return "DW_AT_extension";
11867 case DW_AT_ranges:
11868 return "DW_AT_ranges";
11869 case DW_AT_trampoline:
11870 return "DW_AT_trampoline";
11871 case DW_AT_call_column:
11872 return "DW_AT_call_column";
11873 case DW_AT_call_file:
11874 return "DW_AT_call_file";
11875 case DW_AT_call_line:
11876 return "DW_AT_call_line";
b7619582
GF
11877 case DW_AT_description:
11878 return "DW_AT_description";
11879 case DW_AT_binary_scale:
11880 return "DW_AT_binary_scale";
11881 case DW_AT_decimal_scale:
11882 return "DW_AT_decimal_scale";
11883 case DW_AT_small:
11884 return "DW_AT_small";
11885 case DW_AT_decimal_sign:
11886 return "DW_AT_decimal_sign";
11887 case DW_AT_digit_count:
11888 return "DW_AT_digit_count";
11889 case DW_AT_picture_string:
11890 return "DW_AT_picture_string";
11891 case DW_AT_mutable:
11892 return "DW_AT_mutable";
11893 case DW_AT_threads_scaled:
11894 return "DW_AT_threads_scaled";
11895 case DW_AT_explicit:
11896 return "DW_AT_explicit";
11897 case DW_AT_object_pointer:
11898 return "DW_AT_object_pointer";
11899 case DW_AT_endianity:
11900 return "DW_AT_endianity";
11901 case DW_AT_elemental:
11902 return "DW_AT_elemental";
11903 case DW_AT_pure:
11904 return "DW_AT_pure";
11905 case DW_AT_recursive:
11906 return "DW_AT_recursive";
348e048f
DE
11907 /* DWARF 4 values. */
11908 case DW_AT_signature:
11909 return "DW_AT_signature";
31ef98ae
TT
11910 case DW_AT_linkage_name:
11911 return "DW_AT_linkage_name";
b7619582 11912 /* SGI/MIPS extensions. */
c764a876 11913#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11914 case DW_AT_MIPS_fde:
11915 return "DW_AT_MIPS_fde";
c764a876 11916#endif
c906108c
SS
11917 case DW_AT_MIPS_loop_begin:
11918 return "DW_AT_MIPS_loop_begin";
11919 case DW_AT_MIPS_tail_loop_begin:
11920 return "DW_AT_MIPS_tail_loop_begin";
11921 case DW_AT_MIPS_epilog_begin:
11922 return "DW_AT_MIPS_epilog_begin";
11923 case DW_AT_MIPS_loop_unroll_factor:
11924 return "DW_AT_MIPS_loop_unroll_factor";
11925 case DW_AT_MIPS_software_pipeline_depth:
11926 return "DW_AT_MIPS_software_pipeline_depth";
11927 case DW_AT_MIPS_linkage_name:
11928 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11929 case DW_AT_MIPS_stride:
11930 return "DW_AT_MIPS_stride";
11931 case DW_AT_MIPS_abstract_name:
11932 return "DW_AT_MIPS_abstract_name";
11933 case DW_AT_MIPS_clone_origin:
11934 return "DW_AT_MIPS_clone_origin";
11935 case DW_AT_MIPS_has_inlines:
11936 return "DW_AT_MIPS_has_inlines";
b7619582 11937 /* HP extensions. */
c764a876 11938#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11939 case DW_AT_HP_block_index:
11940 return "DW_AT_HP_block_index";
c764a876 11941#endif
b7619582
GF
11942 case DW_AT_HP_unmodifiable:
11943 return "DW_AT_HP_unmodifiable";
11944 case DW_AT_HP_actuals_stmt_list:
11945 return "DW_AT_HP_actuals_stmt_list";
11946 case DW_AT_HP_proc_per_section:
11947 return "DW_AT_HP_proc_per_section";
11948 case DW_AT_HP_raw_data_ptr:
11949 return "DW_AT_HP_raw_data_ptr";
11950 case DW_AT_HP_pass_by_reference:
11951 return "DW_AT_HP_pass_by_reference";
11952 case DW_AT_HP_opt_level:
11953 return "DW_AT_HP_opt_level";
11954 case DW_AT_HP_prof_version_id:
11955 return "DW_AT_HP_prof_version_id";
11956 case DW_AT_HP_opt_flags:
11957 return "DW_AT_HP_opt_flags";
11958 case DW_AT_HP_cold_region_low_pc:
11959 return "DW_AT_HP_cold_region_low_pc";
11960 case DW_AT_HP_cold_region_high_pc:
11961 return "DW_AT_HP_cold_region_high_pc";
11962 case DW_AT_HP_all_variables_modifiable:
11963 return "DW_AT_HP_all_variables_modifiable";
11964 case DW_AT_HP_linkage_name:
11965 return "DW_AT_HP_linkage_name";
11966 case DW_AT_HP_prof_flags:
11967 return "DW_AT_HP_prof_flags";
11968 /* GNU extensions. */
c906108c
SS
11969 case DW_AT_sf_names:
11970 return "DW_AT_sf_names";
11971 case DW_AT_src_info:
11972 return "DW_AT_src_info";
11973 case DW_AT_mac_info:
11974 return "DW_AT_mac_info";
11975 case DW_AT_src_coords:
11976 return "DW_AT_src_coords";
11977 case DW_AT_body_begin:
11978 return "DW_AT_body_begin";
11979 case DW_AT_body_end:
11980 return "DW_AT_body_end";
f5f8a009
EZ
11981 case DW_AT_GNU_vector:
11982 return "DW_AT_GNU_vector";
2de00c64
DE
11983 case DW_AT_GNU_odr_signature:
11984 return "DW_AT_GNU_odr_signature";
b7619582
GF
11985 /* VMS extensions. */
11986 case DW_AT_VMS_rtnbeg_pd_address:
11987 return "DW_AT_VMS_rtnbeg_pd_address";
11988 /* UPC extension. */
11989 case DW_AT_upc_threads_scaled:
11990 return "DW_AT_upc_threads_scaled";
11991 /* PGI (STMicroelectronics) extensions. */
11992 case DW_AT_PGI_lbase:
11993 return "DW_AT_PGI_lbase";
11994 case DW_AT_PGI_soffset:
11995 return "DW_AT_PGI_soffset";
11996 case DW_AT_PGI_lstride:
11997 return "DW_AT_PGI_lstride";
c906108c
SS
11998 default:
11999 return "DW_AT_<unknown>";
12000 }
12001}
12002
12003/* Convert a DWARF value form code into its string name. */
12004
12005static char *
aa1ee363 12006dwarf_form_name (unsigned form)
c906108c
SS
12007{
12008 switch (form)
12009 {
12010 case DW_FORM_addr:
12011 return "DW_FORM_addr";
12012 case DW_FORM_block2:
12013 return "DW_FORM_block2";
12014 case DW_FORM_block4:
12015 return "DW_FORM_block4";
12016 case DW_FORM_data2:
12017 return "DW_FORM_data2";
12018 case DW_FORM_data4:
12019 return "DW_FORM_data4";
12020 case DW_FORM_data8:
12021 return "DW_FORM_data8";
12022 case DW_FORM_string:
12023 return "DW_FORM_string";
12024 case DW_FORM_block:
12025 return "DW_FORM_block";
12026 case DW_FORM_block1:
12027 return "DW_FORM_block1";
12028 case DW_FORM_data1:
12029 return "DW_FORM_data1";
12030 case DW_FORM_flag:
12031 return "DW_FORM_flag";
12032 case DW_FORM_sdata:
12033 return "DW_FORM_sdata";
12034 case DW_FORM_strp:
12035 return "DW_FORM_strp";
12036 case DW_FORM_udata:
12037 return "DW_FORM_udata";
12038 case DW_FORM_ref_addr:
12039 return "DW_FORM_ref_addr";
12040 case DW_FORM_ref1:
12041 return "DW_FORM_ref1";
12042 case DW_FORM_ref2:
12043 return "DW_FORM_ref2";
12044 case DW_FORM_ref4:
12045 return "DW_FORM_ref4";
12046 case DW_FORM_ref8:
12047 return "DW_FORM_ref8";
12048 case DW_FORM_ref_udata:
12049 return "DW_FORM_ref_udata";
12050 case DW_FORM_indirect:
12051 return "DW_FORM_indirect";
348e048f
DE
12052 case DW_FORM_sec_offset:
12053 return "DW_FORM_sec_offset";
12054 case DW_FORM_exprloc:
12055 return "DW_FORM_exprloc";
12056 case DW_FORM_flag_present:
12057 return "DW_FORM_flag_present";
12058 case DW_FORM_sig8:
12059 return "DW_FORM_sig8";
c906108c
SS
12060 default:
12061 return "DW_FORM_<unknown>";
12062 }
12063}
12064
12065/* Convert a DWARF stack opcode into its string name. */
12066
9eae7c52
TT
12067const char *
12068dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12069{
12070 switch (op)
12071 {
12072 case DW_OP_addr:
12073 return "DW_OP_addr";
12074 case DW_OP_deref:
12075 return "DW_OP_deref";
12076 case DW_OP_const1u:
12077 return "DW_OP_const1u";
12078 case DW_OP_const1s:
12079 return "DW_OP_const1s";
12080 case DW_OP_const2u:
12081 return "DW_OP_const2u";
12082 case DW_OP_const2s:
12083 return "DW_OP_const2s";
12084 case DW_OP_const4u:
12085 return "DW_OP_const4u";
12086 case DW_OP_const4s:
12087 return "DW_OP_const4s";
12088 case DW_OP_const8u:
12089 return "DW_OP_const8u";
12090 case DW_OP_const8s:
12091 return "DW_OP_const8s";
12092 case DW_OP_constu:
12093 return "DW_OP_constu";
12094 case DW_OP_consts:
12095 return "DW_OP_consts";
12096 case DW_OP_dup:
12097 return "DW_OP_dup";
12098 case DW_OP_drop:
12099 return "DW_OP_drop";
12100 case DW_OP_over:
12101 return "DW_OP_over";
12102 case DW_OP_pick:
12103 return "DW_OP_pick";
12104 case DW_OP_swap:
12105 return "DW_OP_swap";
12106 case DW_OP_rot:
12107 return "DW_OP_rot";
12108 case DW_OP_xderef:
12109 return "DW_OP_xderef";
12110 case DW_OP_abs:
12111 return "DW_OP_abs";
12112 case DW_OP_and:
12113 return "DW_OP_and";
12114 case DW_OP_div:
12115 return "DW_OP_div";
12116 case DW_OP_minus:
12117 return "DW_OP_minus";
12118 case DW_OP_mod:
12119 return "DW_OP_mod";
12120 case DW_OP_mul:
12121 return "DW_OP_mul";
12122 case DW_OP_neg:
12123 return "DW_OP_neg";
12124 case DW_OP_not:
12125 return "DW_OP_not";
12126 case DW_OP_or:
12127 return "DW_OP_or";
12128 case DW_OP_plus:
12129 return "DW_OP_plus";
12130 case DW_OP_plus_uconst:
12131 return "DW_OP_plus_uconst";
12132 case DW_OP_shl:
12133 return "DW_OP_shl";
12134 case DW_OP_shr:
12135 return "DW_OP_shr";
12136 case DW_OP_shra:
12137 return "DW_OP_shra";
12138 case DW_OP_xor:
12139 return "DW_OP_xor";
12140 case DW_OP_bra:
12141 return "DW_OP_bra";
12142 case DW_OP_eq:
12143 return "DW_OP_eq";
12144 case DW_OP_ge:
12145 return "DW_OP_ge";
12146 case DW_OP_gt:
12147 return "DW_OP_gt";
12148 case DW_OP_le:
12149 return "DW_OP_le";
12150 case DW_OP_lt:
12151 return "DW_OP_lt";
12152 case DW_OP_ne:
12153 return "DW_OP_ne";
12154 case DW_OP_skip:
12155 return "DW_OP_skip";
12156 case DW_OP_lit0:
12157 return "DW_OP_lit0";
12158 case DW_OP_lit1:
12159 return "DW_OP_lit1";
12160 case DW_OP_lit2:
12161 return "DW_OP_lit2";
12162 case DW_OP_lit3:
12163 return "DW_OP_lit3";
12164 case DW_OP_lit4:
12165 return "DW_OP_lit4";
12166 case DW_OP_lit5:
12167 return "DW_OP_lit5";
12168 case DW_OP_lit6:
12169 return "DW_OP_lit6";
12170 case DW_OP_lit7:
12171 return "DW_OP_lit7";
12172 case DW_OP_lit8:
12173 return "DW_OP_lit8";
12174 case DW_OP_lit9:
12175 return "DW_OP_lit9";
12176 case DW_OP_lit10:
12177 return "DW_OP_lit10";
12178 case DW_OP_lit11:
12179 return "DW_OP_lit11";
12180 case DW_OP_lit12:
12181 return "DW_OP_lit12";
12182 case DW_OP_lit13:
12183 return "DW_OP_lit13";
12184 case DW_OP_lit14:
12185 return "DW_OP_lit14";
12186 case DW_OP_lit15:
12187 return "DW_OP_lit15";
12188 case DW_OP_lit16:
12189 return "DW_OP_lit16";
12190 case DW_OP_lit17:
12191 return "DW_OP_lit17";
12192 case DW_OP_lit18:
12193 return "DW_OP_lit18";
12194 case DW_OP_lit19:
12195 return "DW_OP_lit19";
12196 case DW_OP_lit20:
12197 return "DW_OP_lit20";
12198 case DW_OP_lit21:
12199 return "DW_OP_lit21";
12200 case DW_OP_lit22:
12201 return "DW_OP_lit22";
12202 case DW_OP_lit23:
12203 return "DW_OP_lit23";
12204 case DW_OP_lit24:
12205 return "DW_OP_lit24";
12206 case DW_OP_lit25:
12207 return "DW_OP_lit25";
12208 case DW_OP_lit26:
12209 return "DW_OP_lit26";
12210 case DW_OP_lit27:
12211 return "DW_OP_lit27";
12212 case DW_OP_lit28:
12213 return "DW_OP_lit28";
12214 case DW_OP_lit29:
12215 return "DW_OP_lit29";
12216 case DW_OP_lit30:
12217 return "DW_OP_lit30";
12218 case DW_OP_lit31:
12219 return "DW_OP_lit31";
12220 case DW_OP_reg0:
12221 return "DW_OP_reg0";
12222 case DW_OP_reg1:
12223 return "DW_OP_reg1";
12224 case DW_OP_reg2:
12225 return "DW_OP_reg2";
12226 case DW_OP_reg3:
12227 return "DW_OP_reg3";
12228 case DW_OP_reg4:
12229 return "DW_OP_reg4";
12230 case DW_OP_reg5:
12231 return "DW_OP_reg5";
12232 case DW_OP_reg6:
12233 return "DW_OP_reg6";
12234 case DW_OP_reg7:
12235 return "DW_OP_reg7";
12236 case DW_OP_reg8:
12237 return "DW_OP_reg8";
12238 case DW_OP_reg9:
12239 return "DW_OP_reg9";
12240 case DW_OP_reg10:
12241 return "DW_OP_reg10";
12242 case DW_OP_reg11:
12243 return "DW_OP_reg11";
12244 case DW_OP_reg12:
12245 return "DW_OP_reg12";
12246 case DW_OP_reg13:
12247 return "DW_OP_reg13";
12248 case DW_OP_reg14:
12249 return "DW_OP_reg14";
12250 case DW_OP_reg15:
12251 return "DW_OP_reg15";
12252 case DW_OP_reg16:
12253 return "DW_OP_reg16";
12254 case DW_OP_reg17:
12255 return "DW_OP_reg17";
12256 case DW_OP_reg18:
12257 return "DW_OP_reg18";
12258 case DW_OP_reg19:
12259 return "DW_OP_reg19";
12260 case DW_OP_reg20:
12261 return "DW_OP_reg20";
12262 case DW_OP_reg21:
12263 return "DW_OP_reg21";
12264 case DW_OP_reg22:
12265 return "DW_OP_reg22";
12266 case DW_OP_reg23:
12267 return "DW_OP_reg23";
12268 case DW_OP_reg24:
12269 return "DW_OP_reg24";
12270 case DW_OP_reg25:
12271 return "DW_OP_reg25";
12272 case DW_OP_reg26:
12273 return "DW_OP_reg26";
12274 case DW_OP_reg27:
12275 return "DW_OP_reg27";
12276 case DW_OP_reg28:
12277 return "DW_OP_reg28";
12278 case DW_OP_reg29:
12279 return "DW_OP_reg29";
12280 case DW_OP_reg30:
12281 return "DW_OP_reg30";
12282 case DW_OP_reg31:
12283 return "DW_OP_reg31";
12284 case DW_OP_breg0:
12285 return "DW_OP_breg0";
12286 case DW_OP_breg1:
12287 return "DW_OP_breg1";
12288 case DW_OP_breg2:
12289 return "DW_OP_breg2";
12290 case DW_OP_breg3:
12291 return "DW_OP_breg3";
12292 case DW_OP_breg4:
12293 return "DW_OP_breg4";
12294 case DW_OP_breg5:
12295 return "DW_OP_breg5";
12296 case DW_OP_breg6:
12297 return "DW_OP_breg6";
12298 case DW_OP_breg7:
12299 return "DW_OP_breg7";
12300 case DW_OP_breg8:
12301 return "DW_OP_breg8";
12302 case DW_OP_breg9:
12303 return "DW_OP_breg9";
12304 case DW_OP_breg10:
12305 return "DW_OP_breg10";
12306 case DW_OP_breg11:
12307 return "DW_OP_breg11";
12308 case DW_OP_breg12:
12309 return "DW_OP_breg12";
12310 case DW_OP_breg13:
12311 return "DW_OP_breg13";
12312 case DW_OP_breg14:
12313 return "DW_OP_breg14";
12314 case DW_OP_breg15:
12315 return "DW_OP_breg15";
12316 case DW_OP_breg16:
12317 return "DW_OP_breg16";
12318 case DW_OP_breg17:
12319 return "DW_OP_breg17";
12320 case DW_OP_breg18:
12321 return "DW_OP_breg18";
12322 case DW_OP_breg19:
12323 return "DW_OP_breg19";
12324 case DW_OP_breg20:
12325 return "DW_OP_breg20";
12326 case DW_OP_breg21:
12327 return "DW_OP_breg21";
12328 case DW_OP_breg22:
12329 return "DW_OP_breg22";
12330 case DW_OP_breg23:
12331 return "DW_OP_breg23";
12332 case DW_OP_breg24:
12333 return "DW_OP_breg24";
12334 case DW_OP_breg25:
12335 return "DW_OP_breg25";
12336 case DW_OP_breg26:
12337 return "DW_OP_breg26";
12338 case DW_OP_breg27:
12339 return "DW_OP_breg27";
12340 case DW_OP_breg28:
12341 return "DW_OP_breg28";
12342 case DW_OP_breg29:
12343 return "DW_OP_breg29";
12344 case DW_OP_breg30:
12345 return "DW_OP_breg30";
12346 case DW_OP_breg31:
12347 return "DW_OP_breg31";
12348 case DW_OP_regx:
12349 return "DW_OP_regx";
12350 case DW_OP_fbreg:
12351 return "DW_OP_fbreg";
12352 case DW_OP_bregx:
12353 return "DW_OP_bregx";
12354 case DW_OP_piece:
12355 return "DW_OP_piece";
12356 case DW_OP_deref_size:
12357 return "DW_OP_deref_size";
12358 case DW_OP_xderef_size:
12359 return "DW_OP_xderef_size";
12360 case DW_OP_nop:
12361 return "DW_OP_nop";
b7619582 12362 /* DWARF 3 extensions. */
ed348acc
EZ
12363 case DW_OP_push_object_address:
12364 return "DW_OP_push_object_address";
12365 case DW_OP_call2:
12366 return "DW_OP_call2";
12367 case DW_OP_call4:
12368 return "DW_OP_call4";
12369 case DW_OP_call_ref:
12370 return "DW_OP_call_ref";
b7619582
GF
12371 case DW_OP_form_tls_address:
12372 return "DW_OP_form_tls_address";
12373 case DW_OP_call_frame_cfa:
12374 return "DW_OP_call_frame_cfa";
12375 case DW_OP_bit_piece:
12376 return "DW_OP_bit_piece";
9eae7c52
TT
12377 /* DWARF 4 extensions. */
12378 case DW_OP_implicit_value:
12379 return "DW_OP_implicit_value";
12380 case DW_OP_stack_value:
12381 return "DW_OP_stack_value";
12382 /* GNU extensions. */
ed348acc
EZ
12383 case DW_OP_GNU_push_tls_address:
12384 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12385 case DW_OP_GNU_uninit:
12386 return "DW_OP_GNU_uninit";
c906108c 12387 default:
9eae7c52 12388 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12389 }
12390}
12391
12392static char *
fba45db2 12393dwarf_bool_name (unsigned mybool)
c906108c
SS
12394{
12395 if (mybool)
12396 return "TRUE";
12397 else
12398 return "FALSE";
12399}
12400
12401/* Convert a DWARF type code into its string name. */
12402
12403static char *
aa1ee363 12404dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12405{
12406 switch (enc)
12407 {
b7619582
GF
12408 case DW_ATE_void:
12409 return "DW_ATE_void";
c906108c
SS
12410 case DW_ATE_address:
12411 return "DW_ATE_address";
12412 case DW_ATE_boolean:
12413 return "DW_ATE_boolean";
12414 case DW_ATE_complex_float:
12415 return "DW_ATE_complex_float";
12416 case DW_ATE_float:
12417 return "DW_ATE_float";
12418 case DW_ATE_signed:
12419 return "DW_ATE_signed";
12420 case DW_ATE_signed_char:
12421 return "DW_ATE_signed_char";
12422 case DW_ATE_unsigned:
12423 return "DW_ATE_unsigned";
12424 case DW_ATE_unsigned_char:
12425 return "DW_ATE_unsigned_char";
b7619582 12426 /* DWARF 3. */
d9fa45fe
DC
12427 case DW_ATE_imaginary_float:
12428 return "DW_ATE_imaginary_float";
b7619582
GF
12429 case DW_ATE_packed_decimal:
12430 return "DW_ATE_packed_decimal";
12431 case DW_ATE_numeric_string:
12432 return "DW_ATE_numeric_string";
12433 case DW_ATE_edited:
12434 return "DW_ATE_edited";
12435 case DW_ATE_signed_fixed:
12436 return "DW_ATE_signed_fixed";
12437 case DW_ATE_unsigned_fixed:
12438 return "DW_ATE_unsigned_fixed";
12439 case DW_ATE_decimal_float:
12440 return "DW_ATE_decimal_float";
75079b2b
TT
12441 /* DWARF 4. */
12442 case DW_ATE_UTF:
12443 return "DW_ATE_UTF";
b7619582
GF
12444 /* HP extensions. */
12445 case DW_ATE_HP_float80:
12446 return "DW_ATE_HP_float80";
12447 case DW_ATE_HP_complex_float80:
12448 return "DW_ATE_HP_complex_float80";
12449 case DW_ATE_HP_float128:
12450 return "DW_ATE_HP_float128";
12451 case DW_ATE_HP_complex_float128:
12452 return "DW_ATE_HP_complex_float128";
12453 case DW_ATE_HP_floathpintel:
12454 return "DW_ATE_HP_floathpintel";
12455 case DW_ATE_HP_imaginary_float80:
12456 return "DW_ATE_HP_imaginary_float80";
12457 case DW_ATE_HP_imaginary_float128:
12458 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12459 default:
12460 return "DW_ATE_<unknown>";
12461 }
12462}
12463
12464/* Convert a DWARF call frame info operation to its string name. */
12465
12466#if 0
12467static char *
aa1ee363 12468dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12469{
12470 switch (cfi_opc)
12471 {
12472 case DW_CFA_advance_loc:
12473 return "DW_CFA_advance_loc";
12474 case DW_CFA_offset:
12475 return "DW_CFA_offset";
12476 case DW_CFA_restore:
12477 return "DW_CFA_restore";
12478 case DW_CFA_nop:
12479 return "DW_CFA_nop";
12480 case DW_CFA_set_loc:
12481 return "DW_CFA_set_loc";
12482 case DW_CFA_advance_loc1:
12483 return "DW_CFA_advance_loc1";
12484 case DW_CFA_advance_loc2:
12485 return "DW_CFA_advance_loc2";
12486 case DW_CFA_advance_loc4:
12487 return "DW_CFA_advance_loc4";
12488 case DW_CFA_offset_extended:
12489 return "DW_CFA_offset_extended";
12490 case DW_CFA_restore_extended:
12491 return "DW_CFA_restore_extended";
12492 case DW_CFA_undefined:
12493 return "DW_CFA_undefined";
12494 case DW_CFA_same_value:
12495 return "DW_CFA_same_value";
12496 case DW_CFA_register:
12497 return "DW_CFA_register";
12498 case DW_CFA_remember_state:
12499 return "DW_CFA_remember_state";
12500 case DW_CFA_restore_state:
12501 return "DW_CFA_restore_state";
12502 case DW_CFA_def_cfa:
12503 return "DW_CFA_def_cfa";
12504 case DW_CFA_def_cfa_register:
12505 return "DW_CFA_def_cfa_register";
12506 case DW_CFA_def_cfa_offset:
12507 return "DW_CFA_def_cfa_offset";
b7619582 12508 /* DWARF 3. */
985cb1a3
JM
12509 case DW_CFA_def_cfa_expression:
12510 return "DW_CFA_def_cfa_expression";
12511 case DW_CFA_expression:
12512 return "DW_CFA_expression";
12513 case DW_CFA_offset_extended_sf:
12514 return "DW_CFA_offset_extended_sf";
12515 case DW_CFA_def_cfa_sf:
12516 return "DW_CFA_def_cfa_sf";
12517 case DW_CFA_def_cfa_offset_sf:
12518 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12519 case DW_CFA_val_offset:
12520 return "DW_CFA_val_offset";
12521 case DW_CFA_val_offset_sf:
12522 return "DW_CFA_val_offset_sf";
12523 case DW_CFA_val_expression:
12524 return "DW_CFA_val_expression";
12525 /* SGI/MIPS specific. */
c906108c
SS
12526 case DW_CFA_MIPS_advance_loc8:
12527 return "DW_CFA_MIPS_advance_loc8";
b7619582 12528 /* GNU extensions. */
985cb1a3
JM
12529 case DW_CFA_GNU_window_save:
12530 return "DW_CFA_GNU_window_save";
12531 case DW_CFA_GNU_args_size:
12532 return "DW_CFA_GNU_args_size";
12533 case DW_CFA_GNU_negative_offset_extended:
12534 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12535 default:
12536 return "DW_CFA_<unknown>";
12537 }
12538}
12539#endif
12540
f9aca02d 12541static void
d97bc12b 12542dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12543{
12544 unsigned int i;
12545
d97bc12b
DE
12546 print_spaces (indent, f);
12547 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12548 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12549
12550 if (die->parent != NULL)
12551 {
12552 print_spaces (indent, f);
12553 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12554 die->parent->offset);
12555 }
12556
12557 print_spaces (indent, f);
12558 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12559 dwarf_bool_name (die->child != NULL));
c906108c 12560
d97bc12b
DE
12561 print_spaces (indent, f);
12562 fprintf_unfiltered (f, " attributes:\n");
12563
c906108c
SS
12564 for (i = 0; i < die->num_attrs; ++i)
12565 {
d97bc12b
DE
12566 print_spaces (indent, f);
12567 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12568 dwarf_attr_name (die->attrs[i].name),
12569 dwarf_form_name (die->attrs[i].form));
d97bc12b 12570
c906108c
SS
12571 switch (die->attrs[i].form)
12572 {
12573 case DW_FORM_ref_addr:
12574 case DW_FORM_addr:
d97bc12b 12575 fprintf_unfiltered (f, "address: ");
5af949e3 12576 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12577 break;
12578 case DW_FORM_block2:
12579 case DW_FORM_block4:
12580 case DW_FORM_block:
12581 case DW_FORM_block1:
d97bc12b 12582 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12583 break;
2dc7f7b3
TT
12584 case DW_FORM_exprloc:
12585 fprintf_unfiltered (f, "expression: size %u",
12586 DW_BLOCK (&die->attrs[i])->size);
12587 break;
10b3939b
DJ
12588 case DW_FORM_ref1:
12589 case DW_FORM_ref2:
12590 case DW_FORM_ref4:
d97bc12b 12591 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12592 (long) (DW_ADDR (&die->attrs[i])));
12593 break;
c906108c
SS
12594 case DW_FORM_data1:
12595 case DW_FORM_data2:
12596 case DW_FORM_data4:
ce5d95e1 12597 case DW_FORM_data8:
c906108c
SS
12598 case DW_FORM_udata:
12599 case DW_FORM_sdata:
43bbcdc2
PH
12600 fprintf_unfiltered (f, "constant: %s",
12601 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12602 break;
2dc7f7b3
TT
12603 case DW_FORM_sec_offset:
12604 fprintf_unfiltered (f, "section offset: %s",
12605 pulongest (DW_UNSND (&die->attrs[i])));
12606 break;
348e048f
DE
12607 case DW_FORM_sig8:
12608 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12609 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12610 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12611 else
12612 fprintf_unfiltered (f, "signatured type, offset: unknown");
12613 break;
c906108c 12614 case DW_FORM_string:
4bdf3d34 12615 case DW_FORM_strp:
8285870a 12616 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12617 DW_STRING (&die->attrs[i])
8285870a
JK
12618 ? DW_STRING (&die->attrs[i]) : "",
12619 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12620 break;
12621 case DW_FORM_flag:
12622 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12623 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12624 else
d97bc12b 12625 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12626 break;
2dc7f7b3
TT
12627 case DW_FORM_flag_present:
12628 fprintf_unfiltered (f, "flag: TRUE");
12629 break;
a8329558
KW
12630 case DW_FORM_indirect:
12631 /* the reader will have reduced the indirect form to
12632 the "base form" so this form should not occur */
d97bc12b 12633 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12634 break;
c906108c 12635 default:
d97bc12b 12636 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12637 die->attrs[i].form);
d97bc12b 12638 break;
c906108c 12639 }
d97bc12b 12640 fprintf_unfiltered (f, "\n");
c906108c
SS
12641 }
12642}
12643
f9aca02d 12644static void
d97bc12b 12645dump_die_for_error (struct die_info *die)
c906108c 12646{
d97bc12b
DE
12647 dump_die_shallow (gdb_stderr, 0, die);
12648}
12649
12650static void
12651dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12652{
12653 int indent = level * 4;
12654
12655 gdb_assert (die != NULL);
12656
12657 if (level >= max_level)
12658 return;
12659
12660 dump_die_shallow (f, indent, die);
12661
12662 if (die->child != NULL)
c906108c 12663 {
d97bc12b
DE
12664 print_spaces (indent, f);
12665 fprintf_unfiltered (f, " Children:");
12666 if (level + 1 < max_level)
12667 {
12668 fprintf_unfiltered (f, "\n");
12669 dump_die_1 (f, level + 1, max_level, die->child);
12670 }
12671 else
12672 {
12673 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12674 }
12675 }
12676
12677 if (die->sibling != NULL && level > 0)
12678 {
12679 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12680 }
12681}
12682
d97bc12b
DE
12683/* This is called from the pdie macro in gdbinit.in.
12684 It's not static so gcc will keep a copy callable from gdb. */
12685
12686void
12687dump_die (struct die_info *die, int max_level)
12688{
12689 dump_die_1 (gdb_stdlog, 0, max_level, die);
12690}
12691
f9aca02d 12692static void
51545339 12693store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12694{
51545339 12695 void **slot;
c906108c 12696
51545339
DJ
12697 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12698
12699 *slot = die;
c906108c
SS
12700}
12701
93311388
DE
12702static int
12703is_ref_attr (struct attribute *attr)
c906108c 12704{
c906108c
SS
12705 switch (attr->form)
12706 {
12707 case DW_FORM_ref_addr:
c906108c
SS
12708 case DW_FORM_ref1:
12709 case DW_FORM_ref2:
12710 case DW_FORM_ref4:
613e1657 12711 case DW_FORM_ref8:
c906108c 12712 case DW_FORM_ref_udata:
93311388 12713 return 1;
c906108c 12714 default:
93311388 12715 return 0;
c906108c 12716 }
93311388
DE
12717}
12718
12719static unsigned int
12720dwarf2_get_ref_die_offset (struct attribute *attr)
12721{
12722 if (is_ref_attr (attr))
12723 return DW_ADDR (attr);
12724
12725 complaint (&symfile_complaints,
12726 _("unsupported die ref attribute form: '%s'"),
12727 dwarf_form_name (attr->form));
12728 return 0;
c906108c
SS
12729}
12730
43bbcdc2
PH
12731/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12732 * the value held by the attribute is not constant. */
a02abb62 12733
43bbcdc2 12734static LONGEST
a02abb62
JB
12735dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12736{
12737 if (attr->form == DW_FORM_sdata)
12738 return DW_SND (attr);
12739 else if (attr->form == DW_FORM_udata
12740 || attr->form == DW_FORM_data1
12741 || attr->form == DW_FORM_data2
12742 || attr->form == DW_FORM_data4
12743 || attr->form == DW_FORM_data8)
12744 return DW_UNSND (attr);
12745 else
12746 {
e2e0b3e5 12747 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12748 dwarf_form_name (attr->form));
12749 return default_value;
12750 }
12751}
12752
03dd20cc 12753/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12754 unit and add it to our queue.
12755 The result is non-zero if PER_CU was queued, otherwise the result is zero
12756 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12757
348e048f 12758static int
03dd20cc
DJ
12759maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12760 struct dwarf2_per_cu_data *per_cu)
12761{
98bfdba5
PA
12762 /* We may arrive here during partial symbol reading, if we need full
12763 DIEs to process an unusual case (e.g. template arguments). Do
12764 not queue PER_CU, just tell our caller to load its DIEs. */
12765 if (dwarf2_per_objfile->reading_partial_symbols)
12766 {
12767 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12768 return 1;
12769 return 0;
12770 }
12771
03dd20cc
DJ
12772 /* Mark the dependence relation so that we don't flush PER_CU
12773 too early. */
12774 dwarf2_add_dependence (this_cu, per_cu);
12775
12776 /* If it's already on the queue, we have nothing to do. */
12777 if (per_cu->queued)
348e048f 12778 return 0;
03dd20cc
DJ
12779
12780 /* If the compilation unit is already loaded, just mark it as
12781 used. */
12782 if (per_cu->cu != NULL)
12783 {
12784 per_cu->cu->last_used = 0;
348e048f 12785 return 0;
03dd20cc
DJ
12786 }
12787
12788 /* Add it to the queue. */
12789 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12790
12791 return 1;
12792}
12793
12794/* Follow reference or signature attribute ATTR of SRC_DIE.
12795 On entry *REF_CU is the CU of SRC_DIE.
12796 On exit *REF_CU is the CU of the result. */
12797
12798static struct die_info *
12799follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12800 struct dwarf2_cu **ref_cu)
12801{
12802 struct die_info *die;
12803
12804 if (is_ref_attr (attr))
12805 die = follow_die_ref (src_die, attr, ref_cu);
12806 else if (attr->form == DW_FORM_sig8)
12807 die = follow_die_sig (src_die, attr, ref_cu);
12808 else
12809 {
12810 dump_die_for_error (src_die);
12811 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12812 (*ref_cu)->objfile->name);
12813 }
12814
12815 return die;
03dd20cc
DJ
12816}
12817
5c631832 12818/* Follow reference OFFSET.
673bfd45
DE
12819 On entry *REF_CU is the CU of the source die referencing OFFSET.
12820 On exit *REF_CU is the CU of the result.
12821 Returns NULL if OFFSET is invalid. */
f504f079 12822
f9aca02d 12823static struct die_info *
5c631832 12824follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12825{
10b3939b 12826 struct die_info temp_die;
f2f0e013 12827 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12828
348e048f
DE
12829 gdb_assert (cu->per_cu != NULL);
12830
98bfdba5
PA
12831 target_cu = cu;
12832
348e048f
DE
12833 if (cu->per_cu->from_debug_types)
12834 {
12835 /* .debug_types CUs cannot reference anything outside their CU.
12836 If they need to, they have to reference a signatured type via
12837 DW_FORM_sig8. */
12838 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12839 return NULL;
348e048f
DE
12840 }
12841 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12842 {
12843 struct dwarf2_per_cu_data *per_cu;
9a619af0 12844
45452591 12845 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12846
12847 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12848 if (maybe_queue_comp_unit (cu, per_cu))
12849 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12850
10b3939b
DJ
12851 target_cu = per_cu->cu;
12852 }
98bfdba5
PA
12853 else if (cu->dies == NULL)
12854 {
12855 /* We're loading full DIEs during partial symbol reading. */
12856 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12857 load_full_comp_unit (cu->per_cu, cu->objfile);
12858 }
c906108c 12859
f2f0e013 12860 *ref_cu = target_cu;
51545339 12861 temp_die.offset = offset;
5c631832
JK
12862 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12863}
10b3939b 12864
5c631832
JK
12865/* Follow reference attribute ATTR of SRC_DIE.
12866 On entry *REF_CU is the CU of SRC_DIE.
12867 On exit *REF_CU is the CU of the result. */
12868
12869static struct die_info *
12870follow_die_ref (struct die_info *src_die, struct attribute *attr,
12871 struct dwarf2_cu **ref_cu)
12872{
12873 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12874 struct dwarf2_cu *cu = *ref_cu;
12875 struct die_info *die;
12876
12877 die = follow_die_offset (offset, ref_cu);
12878 if (!die)
12879 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12880 "at 0x%x [in module %s]"),
12881 offset, src_die->offset, cu->objfile->name);
348e048f 12882
5c631832
JK
12883 return die;
12884}
12885
12886/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12887 value is intended for DW_OP_call*. */
12888
12889struct dwarf2_locexpr_baton
12890dwarf2_fetch_die_location_block (unsigned int offset,
12891 struct dwarf2_per_cu_data *per_cu)
12892{
12893 struct dwarf2_cu *cu = per_cu->cu;
12894 struct die_info *die;
12895 struct attribute *attr;
12896 struct dwarf2_locexpr_baton retval;
12897
12898 die = follow_die_offset (offset, &cu);
12899 if (!die)
12900 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12901 offset, per_cu->cu->objfile->name);
12902
12903 attr = dwarf2_attr (die, DW_AT_location, cu);
12904 if (!attr)
12905 {
12906 /* DWARF: "If there is no such attribute, then there is no effect.". */
12907
12908 retval.data = NULL;
12909 retval.size = 0;
12910 }
12911 else
12912 {
12913 if (!attr_form_is_block (attr))
12914 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12915 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12916 offset, per_cu->cu->objfile->name);
12917
12918 retval.data = DW_BLOCK (attr)->data;
12919 retval.size = DW_BLOCK (attr)->size;
12920 }
12921 retval.per_cu = cu->per_cu;
12922 return retval;
348e048f
DE
12923}
12924
12925/* Follow the signature attribute ATTR in SRC_DIE.
12926 On entry *REF_CU is the CU of SRC_DIE.
12927 On exit *REF_CU is the CU of the result. */
12928
12929static struct die_info *
12930follow_die_sig (struct die_info *src_die, struct attribute *attr,
12931 struct dwarf2_cu **ref_cu)
12932{
12933 struct objfile *objfile = (*ref_cu)->objfile;
12934 struct die_info temp_die;
12935 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12936 struct dwarf2_cu *sig_cu;
12937 struct die_info *die;
12938
12939 /* sig_type will be NULL if the signatured type is missing from
12940 the debug info. */
12941 if (sig_type == NULL)
12942 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12943 "at 0x%x [in module %s]"),
12944 src_die->offset, objfile->name);
12945
12946 /* If necessary, add it to the queue and load its DIEs. */
12947
12948 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12949 read_signatured_type (objfile, sig_type);
12950
12951 gdb_assert (sig_type->per_cu.cu != NULL);
12952
12953 sig_cu = sig_type->per_cu.cu;
12954 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12955 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12956 if (die)
12957 {
12958 *ref_cu = sig_cu;
12959 return die;
12960 }
12961
12962 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12963 "at 0x%x [in module %s]"),
12964 sig_type->type_offset, src_die->offset, objfile->name);
12965}
12966
12967/* Given an offset of a signatured type, return its signatured_type. */
12968
12969static struct signatured_type *
12970lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12971{
12972 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12973 unsigned int length, initial_length_size;
12974 unsigned int sig_offset;
12975 struct signatured_type find_entry, *type_sig;
12976
12977 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12978 sig_offset = (initial_length_size
12979 + 2 /*version*/
12980 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12981 + 1 /*address_size*/);
12982 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12983 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12984
12985 /* This is only used to lookup previously recorded types.
12986 If we didn't find it, it's our bug. */
12987 gdb_assert (type_sig != NULL);
12988 gdb_assert (offset == type_sig->offset);
12989
12990 return type_sig;
12991}
12992
12993/* Read in signatured type at OFFSET and build its CU and die(s). */
12994
12995static void
12996read_signatured_type_at_offset (struct objfile *objfile,
12997 unsigned int offset)
12998{
12999 struct signatured_type *type_sig;
13000
be391dca
TT
13001 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13002
348e048f
DE
13003 /* We have the section offset, but we need the signature to do the
13004 hash table lookup. */
13005 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13006
13007 gdb_assert (type_sig->per_cu.cu == NULL);
13008
13009 read_signatured_type (objfile, type_sig);
13010
13011 gdb_assert (type_sig->per_cu.cu != NULL);
13012}
13013
13014/* Read in a signatured type and build its CU and DIEs. */
13015
13016static void
13017read_signatured_type (struct objfile *objfile,
13018 struct signatured_type *type_sig)
13019{
1fd400ff 13020 gdb_byte *types_ptr;
348e048f
DE
13021 struct die_reader_specs reader_specs;
13022 struct dwarf2_cu *cu;
13023 ULONGEST signature;
13024 struct cleanup *back_to, *free_cu_cleanup;
13025 struct attribute *attr;
13026
1fd400ff
TT
13027 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13028 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13029
348e048f
DE
13030 gdb_assert (type_sig->per_cu.cu == NULL);
13031
13032 cu = xmalloc (sizeof (struct dwarf2_cu));
13033 memset (cu, 0, sizeof (struct dwarf2_cu));
13034 obstack_init (&cu->comp_unit_obstack);
13035 cu->objfile = objfile;
13036 type_sig->per_cu.cu = cu;
13037 cu->per_cu = &type_sig->per_cu;
13038
13039 /* If an error occurs while loading, release our storage. */
13040 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13041
13042 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13043 types_ptr, objfile->obfd);
13044 gdb_assert (signature == type_sig->signature);
13045
13046 cu->die_hash
13047 = htab_create_alloc_ex (cu->header.length / 12,
13048 die_hash,
13049 die_eq,
13050 NULL,
13051 &cu->comp_unit_obstack,
13052 hashtab_obstack_allocate,
13053 dummy_obstack_deallocate);
13054
13055 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13056 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13057
13058 init_cu_die_reader (&reader_specs, cu);
13059
13060 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13061 NULL /*parent*/);
13062
13063 /* We try not to read any attributes in this function, because not
13064 all objfiles needed for references have been loaded yet, and symbol
13065 table processing isn't initialized. But we have to set the CU language,
13066 or we won't be able to build types correctly. */
13067 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13068 if (attr)
13069 set_cu_language (DW_UNSND (attr), cu);
13070 else
13071 set_cu_language (language_minimal, cu);
13072
13073 do_cleanups (back_to);
13074
13075 /* We've successfully allocated this compilation unit. Let our caller
13076 clean it up when finished with it. */
13077 discard_cleanups (free_cu_cleanup);
13078
13079 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13080 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13081}
13082
c906108c
SS
13083/* Decode simple location descriptions.
13084 Given a pointer to a dwarf block that defines a location, compute
13085 the location and return the value.
13086
4cecd739
DJ
13087 NOTE drow/2003-11-18: This function is called in two situations
13088 now: for the address of static or global variables (partial symbols
13089 only) and for offsets into structures which are expected to be
13090 (more or less) constant. The partial symbol case should go away,
13091 and only the constant case should remain. That will let this
13092 function complain more accurately. A few special modes are allowed
13093 without complaint for global variables (for instance, global
13094 register values and thread-local values).
c906108c
SS
13095
13096 A location description containing no operations indicates that the
4cecd739 13097 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13098 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13099 callers will only want a very basic result and this can become a
13100 complaint.
c906108c 13101
c906108c
SS
13102 Note that stack[0] is unused except as a default error return.
13103 Note that stack overflow is not yet handled. */
13104
13105static CORE_ADDR
e7c27a73 13106decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13107{
e7c27a73 13108 struct objfile *objfile = cu->objfile;
c906108c
SS
13109 int i;
13110 int size = blk->size;
fe1b8b76 13111 gdb_byte *data = blk->data;
c906108c
SS
13112 CORE_ADDR stack[64];
13113 int stacki;
13114 unsigned int bytes_read, unsnd;
fe1b8b76 13115 gdb_byte op;
c906108c
SS
13116
13117 i = 0;
13118 stacki = 0;
13119 stack[stacki] = 0;
c906108c
SS
13120
13121 while (i < size)
13122 {
c906108c
SS
13123 op = data[i++];
13124 switch (op)
13125 {
f1bea926
JM
13126 case DW_OP_lit0:
13127 case DW_OP_lit1:
13128 case DW_OP_lit2:
13129 case DW_OP_lit3:
13130 case DW_OP_lit4:
13131 case DW_OP_lit5:
13132 case DW_OP_lit6:
13133 case DW_OP_lit7:
13134 case DW_OP_lit8:
13135 case DW_OP_lit9:
13136 case DW_OP_lit10:
13137 case DW_OP_lit11:
13138 case DW_OP_lit12:
13139 case DW_OP_lit13:
13140 case DW_OP_lit14:
13141 case DW_OP_lit15:
13142 case DW_OP_lit16:
13143 case DW_OP_lit17:
13144 case DW_OP_lit18:
13145 case DW_OP_lit19:
13146 case DW_OP_lit20:
13147 case DW_OP_lit21:
13148 case DW_OP_lit22:
13149 case DW_OP_lit23:
13150 case DW_OP_lit24:
13151 case DW_OP_lit25:
13152 case DW_OP_lit26:
13153 case DW_OP_lit27:
13154 case DW_OP_lit28:
13155 case DW_OP_lit29:
13156 case DW_OP_lit30:
13157 case DW_OP_lit31:
13158 stack[++stacki] = op - DW_OP_lit0;
13159 break;
13160
c906108c
SS
13161 case DW_OP_reg0:
13162 case DW_OP_reg1:
13163 case DW_OP_reg2:
13164 case DW_OP_reg3:
13165 case DW_OP_reg4:
13166 case DW_OP_reg5:
13167 case DW_OP_reg6:
13168 case DW_OP_reg7:
13169 case DW_OP_reg8:
13170 case DW_OP_reg9:
13171 case DW_OP_reg10:
13172 case DW_OP_reg11:
13173 case DW_OP_reg12:
13174 case DW_OP_reg13:
13175 case DW_OP_reg14:
13176 case DW_OP_reg15:
13177 case DW_OP_reg16:
13178 case DW_OP_reg17:
13179 case DW_OP_reg18:
13180 case DW_OP_reg19:
13181 case DW_OP_reg20:
13182 case DW_OP_reg21:
13183 case DW_OP_reg22:
13184 case DW_OP_reg23:
13185 case DW_OP_reg24:
13186 case DW_OP_reg25:
13187 case DW_OP_reg26:
13188 case DW_OP_reg27:
13189 case DW_OP_reg28:
13190 case DW_OP_reg29:
13191 case DW_OP_reg30:
13192 case DW_OP_reg31:
c906108c 13193 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13194 if (i < size)
13195 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13196 break;
13197
13198 case DW_OP_regx:
c906108c
SS
13199 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13200 i += bytes_read;
c906108c 13201 stack[++stacki] = unsnd;
4cecd739
DJ
13202 if (i < size)
13203 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13204 break;
13205
13206 case DW_OP_addr:
107d2387 13207 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13208 cu, &bytes_read);
107d2387 13209 i += bytes_read;
c906108c
SS
13210 break;
13211
13212 case DW_OP_const1u:
13213 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13214 i += 1;
13215 break;
13216
13217 case DW_OP_const1s:
13218 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13219 i += 1;
13220 break;
13221
13222 case DW_OP_const2u:
13223 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13224 i += 2;
13225 break;
13226
13227 case DW_OP_const2s:
13228 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13229 i += 2;
13230 break;
13231
13232 case DW_OP_const4u:
13233 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13234 i += 4;
13235 break;
13236
13237 case DW_OP_const4s:
13238 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13239 i += 4;
13240 break;
13241
13242 case DW_OP_constu:
13243 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13244 &bytes_read);
c906108c
SS
13245 i += bytes_read;
13246 break;
13247
13248 case DW_OP_consts:
13249 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13250 i += bytes_read;
13251 break;
13252
f1bea926
JM
13253 case DW_OP_dup:
13254 stack[stacki + 1] = stack[stacki];
13255 stacki++;
13256 break;
13257
c906108c
SS
13258 case DW_OP_plus:
13259 stack[stacki - 1] += stack[stacki];
13260 stacki--;
13261 break;
13262
13263 case DW_OP_plus_uconst:
13264 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13265 i += bytes_read;
13266 break;
13267
13268 case DW_OP_minus:
f1bea926 13269 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13270 stacki--;
13271 break;
13272
7a292a7a 13273 case DW_OP_deref:
7a292a7a 13274 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13275 this using GDB's address_class enum. This is valid for partial
13276 global symbols, although the variable's address will be bogus
13277 in the psymtab. */
7a292a7a 13278 if (i < size)
4d3c2250 13279 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13280 break;
13281
9d774e44 13282 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13283 /* The top of the stack has the offset from the beginning
13284 of the thread control block at which the variable is located. */
13285 /* Nothing should follow this operator, so the top of stack would
13286 be returned. */
4cecd739
DJ
13287 /* This is valid for partial global symbols, but the variable's
13288 address will be bogus in the psymtab. */
9d774e44 13289 if (i < size)
4d3c2250 13290 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13291 break;
13292
42be36b3
CT
13293 case DW_OP_GNU_uninit:
13294 break;
13295
c906108c 13296 default:
e2e0b3e5 13297 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13298 dwarf_stack_op_name (op, 1));
c906108c
SS
13299 return (stack[stacki]);
13300 }
13301 }
13302 return (stack[stacki]);
13303}
13304
13305/* memory allocation interface */
13306
c906108c 13307static struct dwarf_block *
7b5a2f43 13308dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13309{
13310 struct dwarf_block *blk;
13311
13312 blk = (struct dwarf_block *)
7b5a2f43 13313 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13314 return (blk);
13315}
13316
13317static struct abbrev_info *
f3dd6933 13318dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13319{
13320 struct abbrev_info *abbrev;
13321
f3dd6933
DJ
13322 abbrev = (struct abbrev_info *)
13323 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13324 memset (abbrev, 0, sizeof (struct abbrev_info));
13325 return (abbrev);
13326}
13327
13328static struct die_info *
b60c80d6 13329dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13330{
13331 struct die_info *die;
b60c80d6
DJ
13332 size_t size = sizeof (struct die_info);
13333
13334 if (num_attrs > 1)
13335 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13336
b60c80d6 13337 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13338 memset (die, 0, sizeof (struct die_info));
13339 return (die);
13340}
2e276125
JB
13341
13342\f
13343/* Macro support. */
13344
13345
13346/* Return the full name of file number I in *LH's file name table.
13347 Use COMP_DIR as the name of the current directory of the
13348 compilation. The result is allocated using xmalloc; the caller is
13349 responsible for freeing it. */
13350static char *
13351file_full_name (int file, struct line_header *lh, const char *comp_dir)
13352{
6a83a1e6
EZ
13353 /* Is the file number a valid index into the line header's file name
13354 table? Remember that file numbers start with one, not zero. */
13355 if (1 <= file && file <= lh->num_file_names)
13356 {
13357 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13358
6a83a1e6
EZ
13359 if (IS_ABSOLUTE_PATH (fe->name))
13360 return xstrdup (fe->name);
13361 else
13362 {
13363 const char *dir;
13364 int dir_len;
13365 char *full_name;
13366
13367 if (fe->dir_index)
13368 dir = lh->include_dirs[fe->dir_index - 1];
13369 else
13370 dir = comp_dir;
13371
13372 if (dir)
13373 {
13374 dir_len = strlen (dir);
13375 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13376 strcpy (full_name, dir);
13377 full_name[dir_len] = '/';
13378 strcpy (full_name + dir_len + 1, fe->name);
13379 return full_name;
13380 }
13381 else
13382 return xstrdup (fe->name);
13383 }
13384 }
2e276125
JB
13385 else
13386 {
6a83a1e6
EZ
13387 /* The compiler produced a bogus file number. We can at least
13388 record the macro definitions made in the file, even if we
13389 won't be able to find the file by name. */
13390 char fake_name[80];
9a619af0 13391
6a83a1e6 13392 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13393
6e70227d 13394 complaint (&symfile_complaints,
6a83a1e6
EZ
13395 _("bad file number in macro information (%d)"),
13396 file);
2e276125 13397
6a83a1e6 13398 return xstrdup (fake_name);
2e276125
JB
13399 }
13400}
13401
13402
13403static struct macro_source_file *
13404macro_start_file (int file, int line,
13405 struct macro_source_file *current_file,
13406 const char *comp_dir,
13407 struct line_header *lh, struct objfile *objfile)
13408{
13409 /* The full name of this source file. */
13410 char *full_name = file_full_name (file, lh, comp_dir);
13411
13412 /* We don't create a macro table for this compilation unit
13413 at all until we actually get a filename. */
13414 if (! pending_macros)
4a146b47 13415 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13416 objfile->macro_cache);
2e276125
JB
13417
13418 if (! current_file)
13419 /* If we have no current file, then this must be the start_file
13420 directive for the compilation unit's main source file. */
13421 current_file = macro_set_main (pending_macros, full_name);
13422 else
13423 current_file = macro_include (current_file, line, full_name);
13424
13425 xfree (full_name);
6e70227d 13426
2e276125
JB
13427 return current_file;
13428}
13429
13430
13431/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13432 followed by a null byte. */
13433static char *
13434copy_string (const char *buf, int len)
13435{
13436 char *s = xmalloc (len + 1);
9a619af0 13437
2e276125
JB
13438 memcpy (s, buf, len);
13439 s[len] = '\0';
2e276125
JB
13440 return s;
13441}
13442
13443
13444static const char *
13445consume_improper_spaces (const char *p, const char *body)
13446{
13447 if (*p == ' ')
13448 {
4d3c2250 13449 complaint (&symfile_complaints,
e2e0b3e5 13450 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13451 body);
2e276125
JB
13452
13453 while (*p == ' ')
13454 p++;
13455 }
13456
13457 return p;
13458}
13459
13460
13461static void
13462parse_macro_definition (struct macro_source_file *file, int line,
13463 const char *body)
13464{
13465 const char *p;
13466
13467 /* The body string takes one of two forms. For object-like macro
13468 definitions, it should be:
13469
13470 <macro name> " " <definition>
13471
13472 For function-like macro definitions, it should be:
13473
13474 <macro name> "() " <definition>
13475 or
13476 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13477
13478 Spaces may appear only where explicitly indicated, and in the
13479 <definition>.
13480
13481 The Dwarf 2 spec says that an object-like macro's name is always
13482 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13483 the space when the macro's definition is the empty string.
2e276125
JB
13484
13485 The Dwarf 2 spec says that there should be no spaces between the
13486 formal arguments in a function-like macro's formal argument list,
13487 but versions of GCC around March 2002 include spaces after the
13488 commas. */
13489
13490
13491 /* Find the extent of the macro name. The macro name is terminated
13492 by either a space or null character (for an object-like macro) or
13493 an opening paren (for a function-like macro). */
13494 for (p = body; *p; p++)
13495 if (*p == ' ' || *p == '(')
13496 break;
13497
13498 if (*p == ' ' || *p == '\0')
13499 {
13500 /* It's an object-like macro. */
13501 int name_len = p - body;
13502 char *name = copy_string (body, name_len);
13503 const char *replacement;
13504
13505 if (*p == ' ')
13506 replacement = body + name_len + 1;
13507 else
13508 {
4d3c2250 13509 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13510 replacement = body + name_len;
13511 }
6e70227d 13512
2e276125
JB
13513 macro_define_object (file, line, name, replacement);
13514
13515 xfree (name);
13516 }
13517 else if (*p == '(')
13518 {
13519 /* It's a function-like macro. */
13520 char *name = copy_string (body, p - body);
13521 int argc = 0;
13522 int argv_size = 1;
13523 char **argv = xmalloc (argv_size * sizeof (*argv));
13524
13525 p++;
13526
13527 p = consume_improper_spaces (p, body);
13528
13529 /* Parse the formal argument list. */
13530 while (*p && *p != ')')
13531 {
13532 /* Find the extent of the current argument name. */
13533 const char *arg_start = p;
13534
13535 while (*p && *p != ',' && *p != ')' && *p != ' ')
13536 p++;
13537
13538 if (! *p || p == arg_start)
4d3c2250 13539 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13540 else
13541 {
13542 /* Make sure argv has room for the new argument. */
13543 if (argc >= argv_size)
13544 {
13545 argv_size *= 2;
13546 argv = xrealloc (argv, argv_size * sizeof (*argv));
13547 }
13548
13549 argv[argc++] = copy_string (arg_start, p - arg_start);
13550 }
13551
13552 p = consume_improper_spaces (p, body);
13553
13554 /* Consume the comma, if present. */
13555 if (*p == ',')
13556 {
13557 p++;
13558
13559 p = consume_improper_spaces (p, body);
13560 }
13561 }
13562
13563 if (*p == ')')
13564 {
13565 p++;
13566
13567 if (*p == ' ')
13568 /* Perfectly formed definition, no complaints. */
13569 macro_define_function (file, line, name,
6e70227d 13570 argc, (const char **) argv,
2e276125
JB
13571 p + 1);
13572 else if (*p == '\0')
13573 {
13574 /* Complain, but do define it. */
4d3c2250 13575 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13576 macro_define_function (file, line, name,
6e70227d 13577 argc, (const char **) argv,
2e276125
JB
13578 p);
13579 }
13580 else
13581 /* Just complain. */
4d3c2250 13582 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13583 }
13584 else
13585 /* Just complain. */
4d3c2250 13586 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13587
13588 xfree (name);
13589 {
13590 int i;
13591
13592 for (i = 0; i < argc; i++)
13593 xfree (argv[i]);
13594 }
13595 xfree (argv);
13596 }
13597 else
4d3c2250 13598 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13599}
13600
13601
13602static void
13603dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13604 char *comp_dir, bfd *abfd,
e7c27a73 13605 struct dwarf2_cu *cu)
2e276125 13606{
fe1b8b76 13607 gdb_byte *mac_ptr, *mac_end;
2e276125 13608 struct macro_source_file *current_file = 0;
757a13d0
JK
13609 enum dwarf_macinfo_record_type macinfo_type;
13610 int at_commandline;
2e276125 13611
be391dca
TT
13612 dwarf2_read_section (dwarf2_per_objfile->objfile,
13613 &dwarf2_per_objfile->macinfo);
dce234bc 13614 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13615 {
e2e0b3e5 13616 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13617 return;
13618 }
13619
757a13d0
JK
13620 /* First pass: Find the name of the base filename.
13621 This filename is needed in order to process all macros whose definition
13622 (or undefinition) comes from the command line. These macros are defined
13623 before the first DW_MACINFO_start_file entry, and yet still need to be
13624 associated to the base file.
13625
13626 To determine the base file name, we scan the macro definitions until we
13627 reach the first DW_MACINFO_start_file entry. We then initialize
13628 CURRENT_FILE accordingly so that any macro definition found before the
13629 first DW_MACINFO_start_file can still be associated to the base file. */
13630
dce234bc
PP
13631 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13632 mac_end = dwarf2_per_objfile->macinfo.buffer
13633 + dwarf2_per_objfile->macinfo.size;
2e276125 13634
757a13d0 13635 do
2e276125 13636 {
2e276125
JB
13637 /* Do we at least have room for a macinfo type byte? */
13638 if (mac_ptr >= mac_end)
13639 {
757a13d0
JK
13640 /* Complaint is printed during the second pass as GDB will probably
13641 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13642 break;
2e276125
JB
13643 }
13644
13645 macinfo_type = read_1_byte (abfd, mac_ptr);
13646 mac_ptr++;
13647
13648 switch (macinfo_type)
13649 {
13650 /* A zero macinfo type indicates the end of the macro
13651 information. */
13652 case 0:
757a13d0
JK
13653 break;
13654
13655 case DW_MACINFO_define:
13656 case DW_MACINFO_undef:
13657 /* Only skip the data by MAC_PTR. */
13658 {
13659 unsigned int bytes_read;
13660
13661 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13662 mac_ptr += bytes_read;
9b1c24c8 13663 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13664 mac_ptr += bytes_read;
13665 }
13666 break;
13667
13668 case DW_MACINFO_start_file:
13669 {
13670 unsigned int bytes_read;
13671 int line, file;
13672
13673 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13674 mac_ptr += bytes_read;
13675 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13676 mac_ptr += bytes_read;
13677
13678 current_file = macro_start_file (file, line, current_file, comp_dir,
13679 lh, cu->objfile);
13680 }
13681 break;
13682
13683 case DW_MACINFO_end_file:
13684 /* No data to skip by MAC_PTR. */
13685 break;
13686
13687 case DW_MACINFO_vendor_ext:
13688 /* Only skip the data by MAC_PTR. */
13689 {
13690 unsigned int bytes_read;
13691
13692 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13693 mac_ptr += bytes_read;
9b1c24c8 13694 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13695 mac_ptr += bytes_read;
13696 }
13697 break;
13698
13699 default:
13700 break;
13701 }
13702 } while (macinfo_type != 0 && current_file == NULL);
13703
13704 /* Second pass: Process all entries.
13705
13706 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13707 command-line macro definitions/undefinitions. This flag is unset when we
13708 reach the first DW_MACINFO_start_file entry. */
13709
dce234bc 13710 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13711
13712 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13713 GDB is still reading the definitions from command line. First
13714 DW_MACINFO_start_file will need to be ignored as it was already executed
13715 to create CURRENT_FILE for the main source holding also the command line
13716 definitions. On first met DW_MACINFO_start_file this flag is reset to
13717 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13718
13719 at_commandline = 1;
13720
13721 do
13722 {
13723 /* Do we at least have room for a macinfo type byte? */
13724 if (mac_ptr >= mac_end)
13725 {
13726 dwarf2_macros_too_long_complaint ();
13727 break;
13728 }
13729
13730 macinfo_type = read_1_byte (abfd, mac_ptr);
13731 mac_ptr++;
13732
13733 switch (macinfo_type)
13734 {
13735 /* A zero macinfo type indicates the end of the macro
13736 information. */
13737 case 0:
13738 break;
2e276125
JB
13739
13740 case DW_MACINFO_define:
13741 case DW_MACINFO_undef:
13742 {
891d2f0b 13743 unsigned int bytes_read;
2e276125
JB
13744 int line;
13745 char *body;
13746
13747 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13748 mac_ptr += bytes_read;
9b1c24c8 13749 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13750 mac_ptr += bytes_read;
13751
13752 if (! current_file)
757a13d0
JK
13753 {
13754 /* DWARF violation as no main source is present. */
13755 complaint (&symfile_complaints,
13756 _("debug info with no main source gives macro %s "
13757 "on line %d: %s"),
6e70227d
DE
13758 macinfo_type == DW_MACINFO_define ?
13759 _("definition") :
905e0470
PM
13760 macinfo_type == DW_MACINFO_undef ?
13761 _("undefinition") :
13762 _("something-or-other"), line, body);
757a13d0
JK
13763 break;
13764 }
13765 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13766 complaint (&symfile_complaints,
757a13d0
JK
13767 _("debug info gives %s macro %s with %s line %d: %s"),
13768 at_commandline ? _("command-line") : _("in-file"),
905e0470 13769 macinfo_type == DW_MACINFO_define ?
6e70227d 13770 _("definition") :
905e0470
PM
13771 macinfo_type == DW_MACINFO_undef ?
13772 _("undefinition") :
13773 _("something-or-other"),
757a13d0
JK
13774 line == 0 ? _("zero") : _("non-zero"), line, body);
13775
13776 if (macinfo_type == DW_MACINFO_define)
13777 parse_macro_definition (current_file, line, body);
13778 else if (macinfo_type == DW_MACINFO_undef)
13779 macro_undef (current_file, line, body);
2e276125
JB
13780 }
13781 break;
13782
13783 case DW_MACINFO_start_file:
13784 {
891d2f0b 13785 unsigned int bytes_read;
2e276125
JB
13786 int line, file;
13787
13788 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13789 mac_ptr += bytes_read;
13790 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13791 mac_ptr += bytes_read;
13792
757a13d0
JK
13793 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13794 complaint (&symfile_complaints,
13795 _("debug info gives source %d included "
13796 "from %s at %s line %d"),
13797 file, at_commandline ? _("command-line") : _("file"),
13798 line == 0 ? _("zero") : _("non-zero"), line);
13799
13800 if (at_commandline)
13801 {
13802 /* This DW_MACINFO_start_file was executed in the pass one. */
13803 at_commandline = 0;
13804 }
13805 else
13806 current_file = macro_start_file (file, line,
13807 current_file, comp_dir,
13808 lh, cu->objfile);
2e276125
JB
13809 }
13810 break;
13811
13812 case DW_MACINFO_end_file:
13813 if (! current_file)
4d3c2250 13814 complaint (&symfile_complaints,
e2e0b3e5 13815 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13816 else
13817 {
13818 current_file = current_file->included_by;
13819 if (! current_file)
13820 {
13821 enum dwarf_macinfo_record_type next_type;
13822
13823 /* GCC circa March 2002 doesn't produce the zero
13824 type byte marking the end of the compilation
13825 unit. Complain if it's not there, but exit no
13826 matter what. */
13827
13828 /* Do we at least have room for a macinfo type byte? */
13829 if (mac_ptr >= mac_end)
13830 {
4d3c2250 13831 dwarf2_macros_too_long_complaint ();
2e276125
JB
13832 return;
13833 }
13834
13835 /* We don't increment mac_ptr here, so this is just
13836 a look-ahead. */
13837 next_type = read_1_byte (abfd, mac_ptr);
13838 if (next_type != 0)
4d3c2250 13839 complaint (&symfile_complaints,
e2e0b3e5 13840 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13841
13842 return;
13843 }
13844 }
13845 break;
13846
13847 case DW_MACINFO_vendor_ext:
13848 {
891d2f0b 13849 unsigned int bytes_read;
2e276125
JB
13850 int constant;
13851 char *string;
13852
13853 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13854 mac_ptr += bytes_read;
9b1c24c8 13855 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13856 mac_ptr += bytes_read;
13857
13858 /* We don't recognize any vendor extensions. */
13859 }
13860 break;
13861 }
757a13d0 13862 } while (macinfo_type != 0);
2e276125 13863}
8e19ed76
PS
13864
13865/* Check if the attribute's form is a DW_FORM_block*
13866 if so return true else false. */
13867static int
13868attr_form_is_block (struct attribute *attr)
13869{
13870 return (attr == NULL ? 0 :
13871 attr->form == DW_FORM_block1
13872 || attr->form == DW_FORM_block2
13873 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13874 || attr->form == DW_FORM_block
13875 || attr->form == DW_FORM_exprloc);
8e19ed76 13876}
4c2df51b 13877
c6a0999f
JB
13878/* Return non-zero if ATTR's value is a section offset --- classes
13879 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13880 You may use DW_UNSND (attr) to retrieve such offsets.
13881
13882 Section 7.5.4, "Attribute Encodings", explains that no attribute
13883 may have a value that belongs to more than one of these classes; it
13884 would be ambiguous if we did, because we use the same forms for all
13885 of them. */
3690dd37
JB
13886static int
13887attr_form_is_section_offset (struct attribute *attr)
13888{
13889 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13890 || attr->form == DW_FORM_data8
13891 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13892}
13893
13894
13895/* Return non-zero if ATTR's value falls in the 'constant' class, or
13896 zero otherwise. When this function returns true, you can apply
13897 dwarf2_get_attr_constant_value to it.
13898
13899 However, note that for some attributes you must check
13900 attr_form_is_section_offset before using this test. DW_FORM_data4
13901 and DW_FORM_data8 are members of both the constant class, and of
13902 the classes that contain offsets into other debug sections
13903 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13904 that, if an attribute's can be either a constant or one of the
13905 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13906 taken as section offsets, not constants. */
13907static int
13908attr_form_is_constant (struct attribute *attr)
13909{
13910 switch (attr->form)
13911 {
13912 case DW_FORM_sdata:
13913 case DW_FORM_udata:
13914 case DW_FORM_data1:
13915 case DW_FORM_data2:
13916 case DW_FORM_data4:
13917 case DW_FORM_data8:
13918 return 1;
13919 default:
13920 return 0;
13921 }
13922}
13923
4c2df51b
DJ
13924static void
13925dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13926 struct dwarf2_cu *cu)
4c2df51b 13927{
3690dd37 13928 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13929 /* ".debug_loc" may not exist at all, or the offset may be outside
13930 the section. If so, fall through to the complaint in the
13931 other branch. */
dce234bc 13932 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13933 {
0d53c4c4 13934 struct dwarf2_loclist_baton *baton;
4c2df51b 13935
4a146b47 13936 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13937 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13938 baton->per_cu = cu->per_cu;
13939 gdb_assert (baton->per_cu);
4c2df51b 13940
be391dca
TT
13941 dwarf2_read_section (dwarf2_per_objfile->objfile,
13942 &dwarf2_per_objfile->loc);
13943
0d53c4c4
DJ
13944 /* We don't know how long the location list is, but make sure we
13945 don't run off the edge of the section. */
dce234bc
PP
13946 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13947 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13948 baton->base_address = cu->base_address;
13949 if (cu->base_known == 0)
0d53c4c4 13950 complaint (&symfile_complaints,
e2e0b3e5 13951 _("Location list used without specifying the CU base address."));
4c2df51b 13952
768a979c 13953 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13954 SYMBOL_LOCATION_BATON (sym) = baton;
13955 }
13956 else
13957 {
13958 struct dwarf2_locexpr_baton *baton;
13959
4a146b47 13960 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13961 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13962 baton->per_cu = cu->per_cu;
13963 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13964
13965 if (attr_form_is_block (attr))
13966 {
13967 /* Note that we're just copying the block's data pointer
13968 here, not the actual data. We're still pointing into the
6502dd73
DJ
13969 info_buffer for SYM's objfile; right now we never release
13970 that buffer, but when we do clean up properly this may
13971 need to change. */
0d53c4c4
DJ
13972 baton->size = DW_BLOCK (attr)->size;
13973 baton->data = DW_BLOCK (attr)->data;
13974 }
13975 else
13976 {
13977 dwarf2_invalid_attrib_class_complaint ("location description",
13978 SYMBOL_NATURAL_NAME (sym));
13979 baton->size = 0;
13980 baton->data = NULL;
13981 }
6e70227d 13982
768a979c 13983 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13984 SYMBOL_LOCATION_BATON (sym) = baton;
13985 }
4c2df51b 13986}
6502dd73 13987
9aa1f1e3
TT
13988/* Return the OBJFILE associated with the compilation unit CU. If CU
13989 came from a separate debuginfo file, then the master objfile is
13990 returned. */
ae0d2f24
UW
13991
13992struct objfile *
13993dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13994{
9291a0cd 13995 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13996
13997 /* Return the master objfile, so that we can report and look up the
13998 correct file containing this variable. */
13999 if (objfile->separate_debug_objfile_backlink)
14000 objfile = objfile->separate_debug_objfile_backlink;
14001
14002 return objfile;
14003}
14004
14005/* Return the address size given in the compilation unit header for CU. */
14006
14007CORE_ADDR
14008dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14009{
14010 if (per_cu->cu)
14011 return per_cu->cu->header.addr_size;
14012 else
14013 {
14014 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14015 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14016 struct dwarf2_per_objfile *per_objfile
14017 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14018 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14019 struct comp_unit_head cu_header;
9a619af0 14020
ae0d2f24
UW
14021 memset (&cu_header, 0, sizeof cu_header);
14022 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14023 return cu_header.addr_size;
14024 }
14025}
14026
9eae7c52
TT
14027/* Return the offset size given in the compilation unit header for CU. */
14028
14029int
14030dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14031{
14032 if (per_cu->cu)
14033 return per_cu->cu->header.offset_size;
14034 else
14035 {
14036 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14037 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14038 struct dwarf2_per_objfile *per_objfile
14039 = objfile_data (objfile, dwarf2_objfile_data_key);
14040 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14041 struct comp_unit_head cu_header;
14042
14043 memset (&cu_header, 0, sizeof cu_header);
14044 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14045 return cu_header.offset_size;
14046 }
14047}
14048
9aa1f1e3
TT
14049/* Return the text offset of the CU. The returned offset comes from
14050 this CU's objfile. If this objfile came from a separate debuginfo
14051 file, then the offset may be different from the corresponding
14052 offset in the parent objfile. */
14053
14054CORE_ADDR
14055dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14056{
bb3fa9d0 14057 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14058
14059 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14060}
14061
348e048f
DE
14062/* Locate the .debug_info compilation unit from CU's objfile which contains
14063 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14064
14065static struct dwarf2_per_cu_data *
c764a876 14066dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14067 struct objfile *objfile)
14068{
14069 struct dwarf2_per_cu_data *this_cu;
14070 int low, high;
14071
ae038cb0
DJ
14072 low = 0;
14073 high = dwarf2_per_objfile->n_comp_units - 1;
14074 while (high > low)
14075 {
14076 int mid = low + (high - low) / 2;
9a619af0 14077
ae038cb0
DJ
14078 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14079 high = mid;
14080 else
14081 low = mid + 1;
14082 }
14083 gdb_assert (low == high);
14084 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14085 {
10b3939b 14086 if (low == 0)
8a3fe4f8
AC
14087 error (_("Dwarf Error: could not find partial DIE containing "
14088 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14089 (long) offset, bfd_get_filename (objfile->obfd));
14090
ae038cb0
DJ
14091 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14092 return dwarf2_per_objfile->all_comp_units[low-1];
14093 }
14094 else
14095 {
14096 this_cu = dwarf2_per_objfile->all_comp_units[low];
14097 if (low == dwarf2_per_objfile->n_comp_units - 1
14098 && offset >= this_cu->offset + this_cu->length)
c764a876 14099 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14100 gdb_assert (offset < this_cu->offset + this_cu->length);
14101 return this_cu;
14102 }
14103}
14104
10b3939b
DJ
14105/* Locate the compilation unit from OBJFILE which is located at exactly
14106 OFFSET. Raises an error on failure. */
14107
ae038cb0 14108static struct dwarf2_per_cu_data *
c764a876 14109dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14110{
14111 struct dwarf2_per_cu_data *this_cu;
9a619af0 14112
ae038cb0
DJ
14113 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14114 if (this_cu->offset != offset)
c764a876 14115 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14116 return this_cu;
14117}
14118
93311388
DE
14119/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14120
14121static struct dwarf2_cu *
14122alloc_one_comp_unit (struct objfile *objfile)
14123{
14124 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14125 cu->objfile = objfile;
14126 obstack_init (&cu->comp_unit_obstack);
14127 return cu;
14128}
14129
ae038cb0
DJ
14130/* Release one cached compilation unit, CU. We unlink it from the tree
14131 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14132 the caller is responsible for that.
14133 NOTE: DATA is a void * because this function is also used as a
14134 cleanup routine. */
ae038cb0
DJ
14135
14136static void
14137free_one_comp_unit (void *data)
14138{
14139 struct dwarf2_cu *cu = data;
14140
14141 if (cu->per_cu != NULL)
14142 cu->per_cu->cu = NULL;
14143 cu->per_cu = NULL;
14144
14145 obstack_free (&cu->comp_unit_obstack, NULL);
14146
14147 xfree (cu);
14148}
14149
72bf9492 14150/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14151 when we're finished with it. We can't free the pointer itself, but be
14152 sure to unlink it from the cache. Also release any associated storage
14153 and perform cache maintenance.
72bf9492
DJ
14154
14155 Only used during partial symbol parsing. */
14156
14157static void
14158free_stack_comp_unit (void *data)
14159{
14160 struct dwarf2_cu *cu = data;
14161
14162 obstack_free (&cu->comp_unit_obstack, NULL);
14163 cu->partial_dies = NULL;
ae038cb0
DJ
14164
14165 if (cu->per_cu != NULL)
14166 {
14167 /* This compilation unit is on the stack in our caller, so we
14168 should not xfree it. Just unlink it. */
14169 cu->per_cu->cu = NULL;
14170 cu->per_cu = NULL;
14171
14172 /* If we had a per-cu pointer, then we may have other compilation
14173 units loaded, so age them now. */
14174 age_cached_comp_units ();
14175 }
14176}
14177
14178/* Free all cached compilation units. */
14179
14180static void
14181free_cached_comp_units (void *data)
14182{
14183 struct dwarf2_per_cu_data *per_cu, **last_chain;
14184
14185 per_cu = dwarf2_per_objfile->read_in_chain;
14186 last_chain = &dwarf2_per_objfile->read_in_chain;
14187 while (per_cu != NULL)
14188 {
14189 struct dwarf2_per_cu_data *next_cu;
14190
14191 next_cu = per_cu->cu->read_in_chain;
14192
14193 free_one_comp_unit (per_cu->cu);
14194 *last_chain = next_cu;
14195
14196 per_cu = next_cu;
14197 }
14198}
14199
14200/* Increase the age counter on each cached compilation unit, and free
14201 any that are too old. */
14202
14203static void
14204age_cached_comp_units (void)
14205{
14206 struct dwarf2_per_cu_data *per_cu, **last_chain;
14207
14208 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14209 per_cu = dwarf2_per_objfile->read_in_chain;
14210 while (per_cu != NULL)
14211 {
14212 per_cu->cu->last_used ++;
14213 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14214 dwarf2_mark (per_cu->cu);
14215 per_cu = per_cu->cu->read_in_chain;
14216 }
14217
14218 per_cu = dwarf2_per_objfile->read_in_chain;
14219 last_chain = &dwarf2_per_objfile->read_in_chain;
14220 while (per_cu != NULL)
14221 {
14222 struct dwarf2_per_cu_data *next_cu;
14223
14224 next_cu = per_cu->cu->read_in_chain;
14225
14226 if (!per_cu->cu->mark)
14227 {
14228 free_one_comp_unit (per_cu->cu);
14229 *last_chain = next_cu;
14230 }
14231 else
14232 last_chain = &per_cu->cu->read_in_chain;
14233
14234 per_cu = next_cu;
14235 }
14236}
14237
14238/* Remove a single compilation unit from the cache. */
14239
14240static void
14241free_one_cached_comp_unit (void *target_cu)
14242{
14243 struct dwarf2_per_cu_data *per_cu, **last_chain;
14244
14245 per_cu = dwarf2_per_objfile->read_in_chain;
14246 last_chain = &dwarf2_per_objfile->read_in_chain;
14247 while (per_cu != NULL)
14248 {
14249 struct dwarf2_per_cu_data *next_cu;
14250
14251 next_cu = per_cu->cu->read_in_chain;
14252
14253 if (per_cu->cu == target_cu)
14254 {
14255 free_one_comp_unit (per_cu->cu);
14256 *last_chain = next_cu;
14257 break;
14258 }
14259 else
14260 last_chain = &per_cu->cu->read_in_chain;
14261
14262 per_cu = next_cu;
14263 }
14264}
14265
fe3e1990
DJ
14266/* Release all extra memory associated with OBJFILE. */
14267
14268void
14269dwarf2_free_objfile (struct objfile *objfile)
14270{
14271 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14272
14273 if (dwarf2_per_objfile == NULL)
14274 return;
14275
14276 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14277 free_cached_comp_units (NULL);
14278
9291a0cd
TT
14279 if (dwarf2_per_objfile->using_index)
14280 {
14281 int i;
14282
14283 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14284 {
14285 int j;
e254ef6a
DE
14286 struct dwarf2_per_cu_data *per_cu =
14287 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14288
e254ef6a 14289 if (!per_cu->v.quick->lines)
9291a0cd
TT
14290 continue;
14291
e254ef6a 14292 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14293 {
e254ef6a
DE
14294 if (per_cu->v.quick->file_names)
14295 xfree ((void *) per_cu->v.quick->file_names[j]);
14296 if (per_cu->v.quick->full_names)
14297 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14298 }
14299
e254ef6a 14300 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14301 }
14302 }
14303
fe3e1990
DJ
14304 /* Everything else should be on the objfile obstack. */
14305}
14306
1c379e20
DJ
14307/* A pair of DIE offset and GDB type pointer. We store these
14308 in a hash table separate from the DIEs, and preserve them
14309 when the DIEs are flushed out of cache. */
14310
14311struct dwarf2_offset_and_type
14312{
14313 unsigned int offset;
14314 struct type *type;
14315};
14316
14317/* Hash function for a dwarf2_offset_and_type. */
14318
14319static hashval_t
14320offset_and_type_hash (const void *item)
14321{
14322 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14323
1c379e20
DJ
14324 return ofs->offset;
14325}
14326
14327/* Equality function for a dwarf2_offset_and_type. */
14328
14329static int
14330offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14331{
14332 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14333 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14334
1c379e20
DJ
14335 return ofs_lhs->offset == ofs_rhs->offset;
14336}
14337
14338/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14339 table if necessary. For convenience, return TYPE.
14340
14341 The DIEs reading must have careful ordering to:
14342 * Not cause infite loops trying to read in DIEs as a prerequisite for
14343 reading current DIE.
14344 * Not trying to dereference contents of still incompletely read in types
14345 while reading in other DIEs.
14346 * Enable referencing still incompletely read in types just by a pointer to
14347 the type without accessing its fields.
14348
14349 Therefore caller should follow these rules:
14350 * Try to fetch any prerequisite types we may need to build this DIE type
14351 before building the type and calling set_die_type.
e71ec853 14352 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14353 possible before fetching more types to complete the current type.
14354 * Make the type as complete as possible before fetching more types. */
1c379e20 14355
f792889a 14356static struct type *
1c379e20
DJ
14357set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14358{
14359 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14360 struct objfile *objfile = cu->objfile;
14361 htab_t *type_hash_ptr;
1c379e20 14362
b4ba55a1
JB
14363 /* For Ada types, make sure that the gnat-specific data is always
14364 initialized (if not already set). There are a few types where
14365 we should not be doing so, because the type-specific area is
14366 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14367 where the type-specific area is used to store the floatformat).
14368 But this is not a problem, because the gnat-specific information
14369 is actually not needed for these types. */
14370 if (need_gnat_info (cu)
14371 && TYPE_CODE (type) != TYPE_CODE_FUNC
14372 && TYPE_CODE (type) != TYPE_CODE_FLT
14373 && !HAVE_GNAT_AUX_INFO (type))
14374 INIT_GNAT_SPECIFIC (type);
14375
673bfd45
DE
14376 if (cu->per_cu->from_debug_types)
14377 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14378 else
14379 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14380
14381 if (*type_hash_ptr == NULL)
f792889a 14382 {
673bfd45
DE
14383 *type_hash_ptr
14384 = htab_create_alloc_ex (127,
f792889a
DJ
14385 offset_and_type_hash,
14386 offset_and_type_eq,
14387 NULL,
673bfd45 14388 &objfile->objfile_obstack,
f792889a
DJ
14389 hashtab_obstack_allocate,
14390 dummy_obstack_deallocate);
f792889a 14391 }
1c379e20
DJ
14392
14393 ofs.offset = die->offset;
14394 ofs.type = type;
14395 slot = (struct dwarf2_offset_and_type **)
673bfd45 14396 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14397 if (*slot)
14398 complaint (&symfile_complaints,
14399 _("A problem internal to GDB: DIE 0x%x has type already set"),
14400 die->offset);
673bfd45 14401 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14402 **slot = ofs;
f792889a 14403 return type;
1c379e20
DJ
14404}
14405
673bfd45
DE
14406/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14407 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14408
14409static struct type *
673bfd45
DE
14410get_die_type_at_offset (unsigned int offset,
14411 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14412{
14413 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14414 htab_t type_hash;
f792889a 14415
673bfd45
DE
14416 if (per_cu->from_debug_types)
14417 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14418 else
14419 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14420 if (type_hash == NULL)
14421 return NULL;
1c379e20 14422
673bfd45 14423 ofs.offset = offset;
1c379e20
DJ
14424 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14425 if (slot)
14426 return slot->type;
14427 else
14428 return NULL;
14429}
14430
673bfd45
DE
14431/* Look up the type for DIE in the appropriate type_hash table,
14432 or return NULL if DIE does not have a saved type. */
14433
14434static struct type *
14435get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14436{
14437 return get_die_type_at_offset (die->offset, cu->per_cu);
14438}
14439
10b3939b
DJ
14440/* Add a dependence relationship from CU to REF_PER_CU. */
14441
14442static void
14443dwarf2_add_dependence (struct dwarf2_cu *cu,
14444 struct dwarf2_per_cu_data *ref_per_cu)
14445{
14446 void **slot;
14447
14448 if (cu->dependencies == NULL)
14449 cu->dependencies
14450 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14451 NULL, &cu->comp_unit_obstack,
14452 hashtab_obstack_allocate,
14453 dummy_obstack_deallocate);
14454
14455 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14456 if (*slot == NULL)
14457 *slot = ref_per_cu;
14458}
1c379e20 14459
f504f079
DE
14460/* Subroutine of dwarf2_mark to pass to htab_traverse.
14461 Set the mark field in every compilation unit in the
ae038cb0
DJ
14462 cache that we must keep because we are keeping CU. */
14463
10b3939b
DJ
14464static int
14465dwarf2_mark_helper (void **slot, void *data)
14466{
14467 struct dwarf2_per_cu_data *per_cu;
14468
14469 per_cu = (struct dwarf2_per_cu_data *) *slot;
14470 if (per_cu->cu->mark)
14471 return 1;
14472 per_cu->cu->mark = 1;
14473
14474 if (per_cu->cu->dependencies != NULL)
14475 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14476
14477 return 1;
14478}
14479
f504f079
DE
14480/* Set the mark field in CU and in every other compilation unit in the
14481 cache that we must keep because we are keeping CU. */
14482
ae038cb0
DJ
14483static void
14484dwarf2_mark (struct dwarf2_cu *cu)
14485{
14486 if (cu->mark)
14487 return;
14488 cu->mark = 1;
10b3939b
DJ
14489 if (cu->dependencies != NULL)
14490 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14491}
14492
14493static void
14494dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14495{
14496 while (per_cu)
14497 {
14498 per_cu->cu->mark = 0;
14499 per_cu = per_cu->cu->read_in_chain;
14500 }
72bf9492
DJ
14501}
14502
72bf9492
DJ
14503/* Trivial hash function for partial_die_info: the hash value of a DIE
14504 is its offset in .debug_info for this objfile. */
14505
14506static hashval_t
14507partial_die_hash (const void *item)
14508{
14509 const struct partial_die_info *part_die = item;
9a619af0 14510
72bf9492
DJ
14511 return part_die->offset;
14512}
14513
14514/* Trivial comparison function for partial_die_info structures: two DIEs
14515 are equal if they have the same offset. */
14516
14517static int
14518partial_die_eq (const void *item_lhs, const void *item_rhs)
14519{
14520 const struct partial_die_info *part_die_lhs = item_lhs;
14521 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14522
72bf9492
DJ
14523 return part_die_lhs->offset == part_die_rhs->offset;
14524}
14525
ae038cb0
DJ
14526static struct cmd_list_element *set_dwarf2_cmdlist;
14527static struct cmd_list_element *show_dwarf2_cmdlist;
14528
14529static void
14530set_dwarf2_cmd (char *args, int from_tty)
14531{
14532 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14533}
14534
14535static void
14536show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14537{
ae038cb0
DJ
14538 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14539}
14540
dce234bc
PP
14541/* If section described by INFO was mmapped, munmap it now. */
14542
14543static void
14544munmap_section_buffer (struct dwarf2_section_info *info)
14545{
14546 if (info->was_mmapped)
14547 {
14548#ifdef HAVE_MMAP
14549 intptr_t begin = (intptr_t) info->buffer;
14550 intptr_t map_begin = begin & ~(pagesize - 1);
14551 size_t map_length = info->size + begin - map_begin;
9a619af0 14552
dce234bc
PP
14553 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14554#else
14555 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14556 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14557#endif
14558 }
14559}
14560
14561/* munmap debug sections for OBJFILE, if necessary. */
14562
14563static void
c1bd65d0 14564dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14565{
14566 struct dwarf2_per_objfile *data = d;
9a619af0 14567
16be1145
DE
14568 /* This is sorted according to the order they're defined in to make it easier
14569 to keep in sync. */
dce234bc
PP
14570 munmap_section_buffer (&data->info);
14571 munmap_section_buffer (&data->abbrev);
14572 munmap_section_buffer (&data->line);
16be1145 14573 munmap_section_buffer (&data->loc);
dce234bc 14574 munmap_section_buffer (&data->macinfo);
16be1145 14575 munmap_section_buffer (&data->str);
dce234bc 14576 munmap_section_buffer (&data->ranges);
16be1145 14577 munmap_section_buffer (&data->types);
dce234bc
PP
14578 munmap_section_buffer (&data->frame);
14579 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14580 munmap_section_buffer (&data->gdb_index);
14581}
14582
14583\f
14584
14585/* The contents of the hash table we create when building the string
14586 table. */
14587struct strtab_entry
14588{
14589 offset_type offset;
14590 const char *str;
14591};
14592
14593/* Hash function for a strtab_entry. */
14594static hashval_t
14595hash_strtab_entry (const void *e)
14596{
14597 const struct strtab_entry *entry = e;
14598 return mapped_index_string_hash (entry->str);
14599}
14600
14601/* Equality function for a strtab_entry. */
14602static int
14603eq_strtab_entry (const void *a, const void *b)
14604{
14605 const struct strtab_entry *ea = a;
14606 const struct strtab_entry *eb = b;
14607 return !strcmp (ea->str, eb->str);
14608}
14609
14610/* Create a strtab_entry hash table. */
14611static htab_t
14612create_strtab (void)
14613{
14614 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14615 xfree, xcalloc, xfree);
14616}
14617
14618/* Add a string to the constant pool. Return the string's offset in
14619 host order. */
14620static offset_type
14621add_string (htab_t table, struct obstack *cpool, const char *str)
14622{
14623 void **slot;
14624 struct strtab_entry entry;
14625 struct strtab_entry *result;
14626
14627 entry.str = str;
14628 slot = htab_find_slot (table, &entry, INSERT);
14629 if (*slot)
14630 result = *slot;
14631 else
14632 {
14633 result = XNEW (struct strtab_entry);
14634 result->offset = obstack_object_size (cpool);
14635 result->str = str;
14636 obstack_grow_str0 (cpool, str);
14637 *slot = result;
14638 }
14639 return result->offset;
14640}
14641
14642/* An entry in the symbol table. */
14643struct symtab_index_entry
14644{
14645 /* The name of the symbol. */
14646 const char *name;
14647 /* The offset of the name in the constant pool. */
14648 offset_type index_offset;
14649 /* A sorted vector of the indices of all the CUs that hold an object
14650 of this name. */
14651 VEC (offset_type) *cu_indices;
14652};
14653
14654/* The symbol table. This is a power-of-2-sized hash table. */
14655struct mapped_symtab
14656{
14657 offset_type n_elements;
14658 offset_type size;
14659 struct symtab_index_entry **data;
14660};
14661
14662/* Hash function for a symtab_index_entry. */
14663static hashval_t
14664hash_symtab_entry (const void *e)
14665{
14666 const struct symtab_index_entry *entry = e;
14667 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14668 sizeof (offset_type) * VEC_length (offset_type,
14669 entry->cu_indices),
14670 0);
14671}
14672
14673/* Equality function for a symtab_index_entry. */
14674static int
14675eq_symtab_entry (const void *a, const void *b)
14676{
14677 const struct symtab_index_entry *ea = a;
14678 const struct symtab_index_entry *eb = b;
14679 int len = VEC_length (offset_type, ea->cu_indices);
14680 if (len != VEC_length (offset_type, eb->cu_indices))
14681 return 0;
14682 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14683 VEC_address (offset_type, eb->cu_indices),
14684 sizeof (offset_type) * len);
14685}
14686
14687/* Destroy a symtab_index_entry. */
14688static void
14689delete_symtab_entry (void *p)
14690{
14691 struct symtab_index_entry *entry = p;
14692 VEC_free (offset_type, entry->cu_indices);
14693 xfree (entry);
14694}
14695
14696/* Create a hash table holding symtab_index_entry objects. */
14697static htab_t
14698create_index_table (void)
14699{
14700 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14701 delete_symtab_entry, xcalloc, xfree);
14702}
14703
14704/* Create a new mapped symtab object. */
14705static struct mapped_symtab *
14706create_mapped_symtab (void)
14707{
14708 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14709 symtab->n_elements = 0;
14710 symtab->size = 1024;
14711 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14712 return symtab;
14713}
14714
14715/* Destroy a mapped_symtab. */
14716static void
14717cleanup_mapped_symtab (void *p)
14718{
14719 struct mapped_symtab *symtab = p;
14720 /* The contents of the array are freed when the other hash table is
14721 destroyed. */
14722 xfree (symtab->data);
14723 xfree (symtab);
14724}
14725
14726/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14727 the slot. */
14728static struct symtab_index_entry **
14729find_slot (struct mapped_symtab *symtab, const char *name)
14730{
14731 offset_type index, step, hash = mapped_index_string_hash (name);
14732
14733 index = hash & (symtab->size - 1);
14734 step = ((hash * 17) & (symtab->size - 1)) | 1;
14735
14736 for (;;)
14737 {
14738 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14739 return &symtab->data[index];
14740 index = (index + step) & (symtab->size - 1);
14741 }
14742}
14743
14744/* Expand SYMTAB's hash table. */
14745static void
14746hash_expand (struct mapped_symtab *symtab)
14747{
14748 offset_type old_size = symtab->size;
14749 offset_type i;
14750 struct symtab_index_entry **old_entries = symtab->data;
14751
14752 symtab->size *= 2;
14753 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14754
14755 for (i = 0; i < old_size; ++i)
14756 {
14757 if (old_entries[i])
14758 {
14759 struct symtab_index_entry **slot = find_slot (symtab,
14760 old_entries[i]->name);
14761 *slot = old_entries[i];
14762 }
14763 }
14764
14765 xfree (old_entries);
14766}
14767
14768/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14769 is the index of the CU in which the symbol appears. */
14770static void
14771add_index_entry (struct mapped_symtab *symtab, const char *name,
14772 offset_type cu_index)
14773{
14774 struct symtab_index_entry **slot;
14775
14776 ++symtab->n_elements;
14777 if (4 * symtab->n_elements / 3 >= symtab->size)
14778 hash_expand (symtab);
14779
14780 slot = find_slot (symtab, name);
14781 if (!*slot)
14782 {
14783 *slot = XNEW (struct symtab_index_entry);
14784 (*slot)->name = name;
14785 (*slot)->cu_indices = NULL;
14786 }
14787 /* Don't push an index twice. Due to how we add entries we only
14788 have to check the last one. */
14789 if (VEC_empty (offset_type, (*slot)->cu_indices)
14790 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14791 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14792}
14793
14794/* Add a vector of indices to the constant pool. */
14795static offset_type
14796add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14797 struct symtab_index_entry *entry)
14798{
14799 void **slot;
14800
14801 slot = htab_find_slot (index_table, entry, INSERT);
14802 if (!*slot)
14803 {
14804 offset_type len = VEC_length (offset_type, entry->cu_indices);
14805 offset_type val = MAYBE_SWAP (len);
14806 offset_type iter;
14807 int i;
14808
14809 *slot = entry;
14810 entry->index_offset = obstack_object_size (cpool);
14811
14812 obstack_grow (cpool, &val, sizeof (val));
14813 for (i = 0;
14814 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14815 ++i)
14816 {
14817 val = MAYBE_SWAP (iter);
14818 obstack_grow (cpool, &val, sizeof (val));
14819 }
14820 }
14821 else
14822 {
14823 struct symtab_index_entry *old_entry = *slot;
14824 entry->index_offset = old_entry->index_offset;
14825 entry = old_entry;
14826 }
14827 return entry->index_offset;
14828}
14829
14830/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14831 constant pool entries going into the obstack CPOOL. */
14832static void
14833write_hash_table (struct mapped_symtab *symtab,
14834 struct obstack *output, struct obstack *cpool)
14835{
14836 offset_type i;
14837 htab_t index_table;
14838 htab_t str_table;
14839
14840 index_table = create_index_table ();
14841 str_table = create_strtab ();
14842 /* We add all the index vectors to the constant pool first, to
14843 ensure alignment is ok. */
14844 for (i = 0; i < symtab->size; ++i)
14845 {
14846 if (symtab->data[i])
14847 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14848 }
14849
14850 /* Now write out the hash table. */
14851 for (i = 0; i < symtab->size; ++i)
14852 {
14853 offset_type str_off, vec_off;
14854
14855 if (symtab->data[i])
14856 {
14857 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14858 vec_off = symtab->data[i]->index_offset;
14859 }
14860 else
14861 {
14862 /* While 0 is a valid constant pool index, it is not valid
14863 to have 0 for both offsets. */
14864 str_off = 0;
14865 vec_off = 0;
14866 }
14867
14868 str_off = MAYBE_SWAP (str_off);
14869 vec_off = MAYBE_SWAP (vec_off);
14870
14871 obstack_grow (output, &str_off, sizeof (str_off));
14872 obstack_grow (output, &vec_off, sizeof (vec_off));
14873 }
14874
14875 htab_delete (str_table);
14876 htab_delete (index_table);
14877}
14878
14879/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14880 from PST; CU_INDEX is the index of the CU in the vector of all
14881 CUs. */
14882static void
14883add_address_entry (struct objfile *objfile,
14884 struct obstack *addr_obstack, struct partial_symtab *pst,
14885 unsigned int cu_index)
14886{
14887 offset_type offset;
14888 char addr[8];
14889 CORE_ADDR baseaddr;
14890
1fd400ff
TT
14891 /* Don't bother recording empty ranges. */
14892 if (pst->textlow == pst->texthigh)
14893 return;
14894
9291a0cd
TT
14895 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14896
14897 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14898 obstack_grow (addr_obstack, addr, 8);
14899 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14900 obstack_grow (addr_obstack, addr, 8);
14901 offset = MAYBE_SWAP (cu_index);
14902 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14903}
14904
14905/* Add a list of partial symbols to SYMTAB. */
14906static void
14907write_psymbols (struct mapped_symtab *symtab,
14908 struct partial_symbol **psymp,
14909 int count,
14910 offset_type cu_index)
14911{
14912 for (; count-- > 0; ++psymp)
14913 {
14914 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14915 error (_("Ada is not currently supported by the index"));
14916 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14917 }
14918}
14919
14920/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14921 exception if there is an error. */
14922static void
14923write_obstack (FILE *file, struct obstack *obstack)
14924{
14925 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14926 file)
14927 != obstack_object_size (obstack))
14928 error (_("couldn't data write to file"));
14929}
14930
14931/* Unlink a file if the argument is not NULL. */
14932static void
14933unlink_if_set (void *p)
14934{
14935 char **filename = p;
14936 if (*filename)
14937 unlink (*filename);
14938}
14939
1fd400ff
TT
14940/* A helper struct used when iterating over debug_types. */
14941struct signatured_type_index_data
14942{
14943 struct objfile *objfile;
14944 struct mapped_symtab *symtab;
14945 struct obstack *types_list;
14946 int cu_index;
14947};
14948
14949/* A helper function that writes a single signatured_type to an
14950 obstack. */
14951static int
14952write_one_signatured_type (void **slot, void *d)
14953{
14954 struct signatured_type_index_data *info = d;
14955 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
14956 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
14957 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
14958 gdb_byte val[8];
14959
14960 write_psymbols (info->symtab,
14961 info->objfile->global_psymbols.list + psymtab->globals_offset,
14962 psymtab->n_global_syms, info->cu_index);
14963 write_psymbols (info->symtab,
14964 info->objfile->static_psymbols.list + psymtab->statics_offset,
14965 psymtab->n_static_syms, info->cu_index);
14966
14967 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14968 obstack_grow (info->types_list, val, 8);
14969 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14970 obstack_grow (info->types_list, val, 8);
14971 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14972 obstack_grow (info->types_list, val, 8);
14973
14974 ++info->cu_index;
14975
14976 return 1;
14977}
14978
9291a0cd
TT
14979/* Create an index file for OBJFILE in the directory DIR. */
14980static void
14981write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14982{
14983 struct cleanup *cleanup;
14984 char *filename, *cleanup_filename;
1fd400ff
TT
14985 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14986 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14987 int i;
14988 FILE *out_file;
14989 struct mapped_symtab *symtab;
14990 offset_type val, size_of_contents, total_len;
14991 struct stat st;
14992 char buf[8];
14993
14994 if (!objfile->psymtabs)
14995 return;
14996 if (dwarf2_per_objfile->using_index)
14997 error (_("Cannot use an index to create the index"));
14998
14999 if (stat (objfile->name, &st) < 0)
15000 perror_with_name (_("Could not stat"));
15001
15002 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15003 INDEX_SUFFIX, (char *) NULL);
15004 cleanup = make_cleanup (xfree, filename);
15005
15006 out_file = fopen (filename, "wb");
15007 if (!out_file)
15008 error (_("Can't open `%s' for writing"), filename);
15009
15010 cleanup_filename = filename;
15011 make_cleanup (unlink_if_set, &cleanup_filename);
15012
15013 symtab = create_mapped_symtab ();
15014 make_cleanup (cleanup_mapped_symtab, symtab);
15015
15016 obstack_init (&addr_obstack);
15017 make_cleanup_obstack_free (&addr_obstack);
15018
15019 obstack_init (&cu_list);
15020 make_cleanup_obstack_free (&cu_list);
15021
1fd400ff
TT
15022 obstack_init (&types_cu_list);
15023 make_cleanup_obstack_free (&types_cu_list);
15024
15025 /* The list is already sorted, so we don't need to do additional
15026 work here. Also, the debug_types entries do not appear in
15027 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15028 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15029 {
e254ef6a
DE
15030 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15031 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15032 gdb_byte val[8];
15033
15034 write_psymbols (symtab,
15035 objfile->global_psymbols.list + psymtab->globals_offset,
15036 psymtab->n_global_syms, i);
15037 write_psymbols (symtab,
15038 objfile->static_psymbols.list + psymtab->statics_offset,
15039 psymtab->n_static_syms, i);
15040
15041 add_address_entry (objfile, &addr_obstack, psymtab, i);
15042
e254ef6a 15043 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15044 obstack_grow (&cu_list, val, 8);
e254ef6a 15045 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15046 obstack_grow (&cu_list, val, 8);
15047 }
15048
1fd400ff
TT
15049 /* Write out the .debug_type entries, if any. */
15050 if (dwarf2_per_objfile->signatured_types)
15051 {
15052 struct signatured_type_index_data sig_data;
15053
15054 sig_data.objfile = objfile;
15055 sig_data.symtab = symtab;
15056 sig_data.types_list = &types_cu_list;
15057 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15058 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15059 write_one_signatured_type, &sig_data);
15060 }
15061
9291a0cd
TT
15062 obstack_init (&constant_pool);
15063 make_cleanup_obstack_free (&constant_pool);
15064 obstack_init (&symtab_obstack);
15065 make_cleanup_obstack_free (&symtab_obstack);
15066 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15067
15068 obstack_init (&contents);
15069 make_cleanup_obstack_free (&contents);
1fd400ff 15070 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15071 total_len = size_of_contents;
15072
15073 /* The version number. */
1fd400ff 15074 val = MAYBE_SWAP (2);
9291a0cd
TT
15075 obstack_grow (&contents, &val, sizeof (val));
15076
15077 /* The offset of the CU list 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 (&cu_list);
15081
1fd400ff
TT
15082 /* The offset of the types CU list from the start of the file. */
15083 val = MAYBE_SWAP (total_len);
15084 obstack_grow (&contents, &val, sizeof (val));
15085 total_len += obstack_object_size (&types_cu_list);
15086
9291a0cd
TT
15087 /* The offset of the address table from the start of the file. */
15088 val = MAYBE_SWAP (total_len);
15089 obstack_grow (&contents, &val, sizeof (val));
15090 total_len += obstack_object_size (&addr_obstack);
15091
15092 /* The offset of the symbol table from the start of the file. */
15093 val = MAYBE_SWAP (total_len);
15094 obstack_grow (&contents, &val, sizeof (val));
15095 total_len += obstack_object_size (&symtab_obstack);
15096
15097 /* The offset of the constant pool from the start of the file. */
15098 val = MAYBE_SWAP (total_len);
15099 obstack_grow (&contents, &val, sizeof (val));
15100 total_len += obstack_object_size (&constant_pool);
15101
15102 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15103
15104 write_obstack (out_file, &contents);
15105 write_obstack (out_file, &cu_list);
1fd400ff 15106 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15107 write_obstack (out_file, &addr_obstack);
15108 write_obstack (out_file, &symtab_obstack);
15109 write_obstack (out_file, &constant_pool);
15110
15111 fclose (out_file);
15112
15113 /* We want to keep the file, so we set cleanup_filename to NULL
15114 here. See unlink_if_set. */
15115 cleanup_filename = NULL;
15116
15117 do_cleanups (cleanup);
15118}
15119
15120/* The mapped index file format is designed to be directly mmap()able
15121 on any architecture. In most cases, a datum is represented using a
15122 little-endian 32-bit integer value, called an offset_type. Big
15123 endian machines must byte-swap the values before using them.
15124 Exceptions to this rule are noted. The data is laid out such that
15125 alignment is always respected.
15126
15127 A mapped index consists of several sections.
15128
15129 1. The file header. This is a sequence of values, of offset_type
15130 unless otherwise noted:
1fd400ff
TT
15131 [0] The version number. Currently 1 or 2. The differences are
15132 noted below. Version 1 did not account for .debug_types sections;
15133 the presence of a .debug_types section invalidates any version 1
15134 index that may exist.
9291a0cd 15135 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
15136 [1.5] In version 2, the offset, from the start of the file, of the
15137 types CU list. This offset does not appear in version 1. Note
15138 that this can be empty, in which case this offset will be equal to
15139 the next offset.
9291a0cd
TT
15140 [2] The offset, from the start of the file, of the address section.
15141 [3] The offset, from the start of the file, of the symbol table.
15142 [4] The offset, from the start of the file, of the constant pool.
15143
15144 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15145 little-endian values, sorted by the CU offset. The first element
15146 in each pair is the offset of a CU in the .debug_info section. The
15147 second element in each pair is the length of that CU. References
15148 to a CU elsewhere in the map are done using a CU index, which is
15149 just the 0-based index into this table. Note that if there are
15150 type CUs, then conceptually CUs and type CUs form a single list for
15151 the purposes of CU indices.
15152
15153 2.5 The types CU list. This does not appear in a version 1 index.
15154 This is a sequence of triplets of 64-bit little-endian values. In
15155 a triplet, the first value is the CU offset, the second value is
15156 the type offset in the CU, and the third value is the type
15157 signature. The types CU list is not sorted.
9291a0cd
TT
15158
15159 3. The address section. The address section consists of a sequence
15160 of address entries. Each address entry has three elements.
15161 [0] The low address. This is a 64-bit little-endian value.
15162 [1] The high address. This is a 64-bit little-endian value.
15163 [2] The CU index. This is an offset_type value.
15164
15165 4. The symbol table. This is a hash table. The size of the hash
15166 table is always a power of 2. The initial hash and the step are
15167 currently defined by the `find_slot' function.
15168
15169 Each slot in the hash table consists of a pair of offset_type
15170 values. The first value is the offset of the symbol's name in the
15171 constant pool. The second value is the offset of the CU vector in
15172 the constant pool.
15173
15174 If both values are 0, then this slot in the hash table is empty.
15175 This is ok because while 0 is a valid constant pool index, it
15176 cannot be a valid index for both a string and a CU vector.
15177
15178 A string in the constant pool is stored as a \0-terminated string,
15179 as you'd expect.
15180
15181 A CU vector in the constant pool is a sequence of offset_type
15182 values. The first value is the number of CU indices in the vector.
15183 Each subsequent value is the index of a CU in the CU list. This
15184 element in the hash table is used to indicate which CUs define the
15185 symbol.
15186
15187 5. The constant pool. This is simply a bunch of bytes. It is
15188 organized so that alignment is correct: CU vectors are stored
15189 first, followed by strings. */
15190static void
15191save_gdb_index_command (char *arg, int from_tty)
15192{
15193 struct objfile *objfile;
15194
15195 if (!arg || !*arg)
96d19272 15196 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15197
15198 ALL_OBJFILES (objfile)
15199 {
15200 struct stat st;
15201
15202 /* If the objfile does not correspond to an actual file, skip it. */
15203 if (stat (objfile->name, &st) < 0)
15204 continue;
15205
15206 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15207 if (dwarf2_per_objfile)
15208 {
15209 volatile struct gdb_exception except;
15210
15211 TRY_CATCH (except, RETURN_MASK_ERROR)
15212 {
15213 write_psymtabs_to_index (objfile, arg);
15214 }
15215 if (except.reason < 0)
15216 exception_fprintf (gdb_stderr, except,
15217 _("Error while writing index for `%s': "),
15218 objfile->name);
15219 }
15220 }
dce234bc
PP
15221}
15222
9291a0cd
TT
15223\f
15224
9eae7c52
TT
15225int dwarf2_always_disassemble;
15226
15227static void
15228show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15229 struct cmd_list_element *c, const char *value)
15230{
15231 fprintf_filtered (file, _("\
15232Whether to always disassemble DWARF expressions is %s.\n"),
15233 value);
15234}
15235
6502dd73
DJ
15236void _initialize_dwarf2_read (void);
15237
15238void
15239_initialize_dwarf2_read (void)
15240{
96d19272
JK
15241 struct cmd_list_element *c;
15242
dce234bc 15243 dwarf2_objfile_data_key
c1bd65d0 15244 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15245
1bedd215
AC
15246 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15247Set DWARF 2 specific variables.\n\
15248Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15249 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15250 0/*allow-unknown*/, &maintenance_set_cmdlist);
15251
1bedd215
AC
15252 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15253Show DWARF 2 specific variables\n\
15254Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15255 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15256 0/*allow-unknown*/, &maintenance_show_cmdlist);
15257
15258 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15259 &dwarf2_max_cache_age, _("\
15260Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15261Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15262A higher limit means that cached compilation units will be stored\n\
15263in memory longer, and more total memory will be used. Zero disables\n\
15264caching, which can slow down startup."),
2c5b56ce 15265 NULL,
920d2a44 15266 show_dwarf2_max_cache_age,
2c5b56ce 15267 &set_dwarf2_cmdlist,
ae038cb0 15268 &show_dwarf2_cmdlist);
d97bc12b 15269
9eae7c52
TT
15270 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15271 &dwarf2_always_disassemble, _("\
15272Set whether `info address' always disassembles DWARF expressions."), _("\
15273Show whether `info address' always disassembles DWARF expressions."), _("\
15274When enabled, DWARF expressions are always printed in an assembly-like\n\
15275syntax. When disabled, expressions will be printed in a more\n\
15276conversational style, when possible."),
15277 NULL,
15278 show_dwarf2_always_disassemble,
15279 &set_dwarf2_cmdlist,
15280 &show_dwarf2_cmdlist);
15281
d97bc12b
DE
15282 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15283Set debugging of the dwarf2 DIE reader."), _("\
15284Show debugging of the dwarf2 DIE reader."), _("\
15285When enabled (non-zero), DIEs are dumped after they are read in.\n\
15286The value is the maximum depth to print."),
15287 NULL,
15288 NULL,
15289 &setdebuglist, &showdebuglist);
9291a0cd 15290
96d19272
JK
15291 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15292 _("Save a .gdb-index file"),
15293 &save_cmdlist);
15294 set_cmd_completer (c, filename_completer);
6502dd73 15295}
This page took 1.960424 seconds and 4 git commands to generate.