Check audit entry only on ELF input.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
673bfd45
DE
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
6502dd73
DJ
226};
227
228static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
229
230/* names of the debugging sections */
231
233a11ab
CS
232/* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235#define INFO_SECTION "debug_info"
236#define ABBREV_SECTION "debug_abbrev"
237#define LINE_SECTION "debug_line"
233a11ab
CS
238#define LOC_SECTION "debug_loc"
239#define MACINFO_SECTION "debug_macinfo"
240#define STR_SECTION "debug_str"
241#define RANGES_SECTION "debug_ranges"
348e048f 242#define TYPES_SECTION "debug_types"
233a11ab
CS
243#define FRAME_SECTION "debug_frame"
244#define EH_FRAME_SECTION "eh_frame"
9291a0cd 245#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
246
247/* local data types */
248
57349743
JB
249/* We hold several abbreviation tables in memory at the same time. */
250#ifndef ABBREV_HASH_SIZE
251#define ABBREV_HASH_SIZE 121
252#endif
253
107d2387
AC
254/* The data in a compilation unit header, after target2host
255 translation, looks like this. */
c906108c 256struct comp_unit_head
a738430d 257{
c764a876 258 unsigned int length;
a738430d 259 short version;
a738430d
MK
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
9cbfa09e 262 unsigned int abbrev_offset;
57349743 263
a738430d
MK
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
57349743 266
a738430d
MK
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
57349743 269
a738430d
MK
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
57349743 273
d00adf39
DE
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
a738430d 277};
c906108c 278
3da10d80
KS
279/* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281struct delayed_method_info
282{
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297};
298
299typedef struct delayed_method_info delayed_method_info;
300DEF_VEC_O (delayed_method_info);
301
e7c27a73
DJ
302/* Internal state when decoding a particular compilation unit. */
303struct dwarf2_cu
304{
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
d00adf39 308 /* The header of the compilation unit. */
e7c27a73 309 struct comp_unit_head header;
e142c38c 310
d00adf39
DE
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
e142c38c
DJ
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492
DJ
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
10b3939b 361 /* A hash table of die offsets for following references. */
51545339 362 htab_t die_hash;
10b3939b
DJ
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
cb1df416
DJ
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
3da10d80
KS
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
ae038cb0
DJ
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
72bf9492
DJ
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
e7c27a73
DJ
391};
392
9291a0cd
TT
393/* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396struct dwarf2_per_cu_quick_data
397{
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417};
418
10b3939b
DJ
419/* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
ae038cb0
DJ
425struct dwarf2_per_cu_data
426{
348e048f 427 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 428 bytes should suffice to store the length of any compilation unit
45452591
DE
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
c764a876 432 unsigned int offset;
348e048f 433 unsigned int length : 29;
ae038cb0
DJ
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
c764a876 437 unsigned int queued : 1;
ae038cb0 438
5afb4e99
DJ
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
348e048f
DE
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
17ea53c3
JK
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
ae038cb0 451 struct dwarf2_cu *cu;
1c379e20 452
9291a0cd
TT
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
ae038cb0
DJ
468};
469
348e048f
DE
470/* Entry in the signatured_types hash table. */
471
472struct signatured_type
473{
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484};
485
93311388
DE
486/* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492struct die_reader_specs
493{
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503};
504
debd256d
JB
505/* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508struct line_header
509{
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
2dc7f7b3 514 unsigned char maximum_ops_per_instruction;
debd256d
JB
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
c906108c 538 {
debd256d
JB
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
aaa75496 543 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 544 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
545 } *file_names;
546
547 /* The start and end of the statement program following this
6502dd73 548 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 549 gdb_byte *statement_program_start, *statement_program_end;
debd256d 550};
c906108c
SS
551
552/* When we construct a partial symbol table entry we only
553 need this much information. */
554struct partial_die_info
555 {
72bf9492 556 /* Offset of this DIE. */
c906108c 557 unsigned int offset;
72bf9492
DJ
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
72bf9492
DJ
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
fa4028e9
JB
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
98bfdba5
PA
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
72bf9492 580 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 581 sometimes a default name for unnamed DIEs. */
c906108c 582 char *name;
72bf9492
DJ
583
584 /* The scope to prepend to our children. This is generally
585 allocated on the comp_unit_obstack, so will disappear
586 when this compilation unit leaves the cache. */
587 char *scope;
588
589 /* The location description associated with this DIE, if any. */
590 struct dwarf_block *locdesc;
591
592 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
593 CORE_ADDR lowpc;
594 CORE_ADDR highpc;
72bf9492 595
93311388 596 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 597 DW_AT_sibling, if any. */
fe1b8b76 598 gdb_byte *sibling;
72bf9492
DJ
599
600 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
601 DW_AT_specification (or DW_AT_abstract_origin or
602 DW_AT_extension). */
603 unsigned int spec_offset;
604
605 /* Pointers to this DIE's parent, first child, and next sibling,
606 if any. */
607 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
608 };
609
610/* This data structure holds the information of an abbrev. */
611struct abbrev_info
612 {
613 unsigned int number; /* number identifying abbrev */
614 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
615 unsigned short has_children; /* boolean */
616 unsigned short num_attrs; /* number of attributes */
c906108c
SS
617 struct attr_abbrev *attrs; /* an array of attribute descriptions */
618 struct abbrev_info *next; /* next in chain */
619 };
620
621struct attr_abbrev
622 {
9d25dd43
DE
623 ENUM_BITFIELD(dwarf_attribute) name : 16;
624 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
625 };
626
b60c80d6
DJ
627/* Attributes have a name and a value */
628struct attribute
629 {
9d25dd43 630 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
631 ENUM_BITFIELD(dwarf_form) form : 15;
632
633 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
634 field should be in u.str (existing only for DW_STRING) but it is kept
635 here for better struct attribute alignment. */
636 unsigned int string_is_canonical : 1;
637
b60c80d6
DJ
638 union
639 {
640 char *str;
641 struct dwarf_block *blk;
43bbcdc2
PH
642 ULONGEST unsnd;
643 LONGEST snd;
b60c80d6 644 CORE_ADDR addr;
348e048f 645 struct signatured_type *signatured_type;
b60c80d6
DJ
646 }
647 u;
648 };
649
c906108c
SS
650/* This data structure holds a complete die structure. */
651struct die_info
652 {
76815b17
DE
653 /* DWARF-2 tag for this DIE. */
654 ENUM_BITFIELD(dwarf_tag) tag : 16;
655
656 /* Number of attributes */
98bfdba5
PA
657 unsigned char num_attrs;
658
659 /* True if we're presently building the full type name for the
660 type derived from this DIE. */
661 unsigned char building_fullname : 1;
76815b17
DE
662
663 /* Abbrev number */
664 unsigned int abbrev;
665
93311388 666 /* Offset in .debug_info or .debug_types section. */
76815b17 667 unsigned int offset;
78ba4af6
JB
668
669 /* The dies in a compilation unit form an n-ary tree. PARENT
670 points to this die's parent; CHILD points to the first child of
671 this node; and all the children of a given node are chained
672 together via their SIBLING fields, terminated by a die whose
673 tag is zero. */
639d11d3
DC
674 struct die_info *child; /* Its first child, if any. */
675 struct die_info *sibling; /* Its next sibling, if any. */
676 struct die_info *parent; /* Its parent, if any. */
c906108c 677
b60c80d6
DJ
678 /* An array of attributes, with NUM_ATTRS elements. There may be
679 zero, but it's not common and zero-sized arrays are not
680 sufficiently portable C. */
681 struct attribute attrs[1];
c906108c
SS
682 };
683
5fb290d7
DJ
684struct function_range
685{
686 const char *name;
687 CORE_ADDR lowpc, highpc;
688 int seen_line;
689 struct function_range *next;
690};
691
c906108c
SS
692/* Get at parts of an attribute structure */
693
694#define DW_STRING(attr) ((attr)->u.str)
8285870a 695#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
696#define DW_UNSND(attr) ((attr)->u.unsnd)
697#define DW_BLOCK(attr) ((attr)->u.blk)
698#define DW_SND(attr) ((attr)->u.snd)
699#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 700#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
701
702/* Blocks are a bunch of untyped bytes. */
703struct dwarf_block
704 {
705 unsigned int size;
fe1b8b76 706 gdb_byte *data;
c906108c
SS
707 };
708
c906108c
SS
709#ifndef ATTR_ALLOC_CHUNK
710#define ATTR_ALLOC_CHUNK 4
711#endif
712
c906108c
SS
713/* Allocate fields for structs, unions and enums in this size. */
714#ifndef DW_FIELD_ALLOC_CHUNK
715#define DW_FIELD_ALLOC_CHUNK 4
716#endif
717
c906108c
SS
718/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
719 but this would require a corresponding change in unpack_field_as_long
720 and friends. */
721static int bits_per_byte = 8;
722
723/* The routines that read and process dies for a C struct or C++ class
724 pass lists of data member fields and lists of member function fields
725 in an instance of a field_info structure, as defined below. */
726struct field_info
c5aa993b
JM
727 {
728 /* List of data member and baseclasses fields. */
729 struct nextfield
730 {
731 struct nextfield *next;
732 int accessibility;
733 int virtuality;
734 struct field field;
735 }
7d0ccb61 736 *fields, *baseclasses;
c906108c 737
7d0ccb61 738 /* Number of fields (including baseclasses). */
c5aa993b 739 int nfields;
c906108c 740
c5aa993b
JM
741 /* Number of baseclasses. */
742 int nbaseclasses;
c906108c 743
c5aa993b
JM
744 /* Set if the accesibility of one of the fields is not public. */
745 int non_public_fields;
c906108c 746
c5aa993b
JM
747 /* Member function fields array, entries are allocated in the order they
748 are encountered in the object file. */
749 struct nextfnfield
750 {
751 struct nextfnfield *next;
752 struct fn_field fnfield;
753 }
754 *fnfields;
c906108c 755
c5aa993b
JM
756 /* Member function fieldlist array, contains name of possibly overloaded
757 member function, number of overloaded member functions and a pointer
758 to the head of the member function field chain. */
759 struct fnfieldlist
760 {
761 char *name;
762 int length;
763 struct nextfnfield *head;
764 }
765 *fnfieldlists;
c906108c 766
c5aa993b
JM
767 /* Number of entries in the fnfieldlists array. */
768 int nfnfields;
98751a41
JK
769
770 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
771 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
772 struct typedef_field_list
773 {
774 struct typedef_field field;
775 struct typedef_field_list *next;
776 }
777 *typedef_field_list;
778 unsigned typedef_field_list_count;
c5aa993b 779 };
c906108c 780
10b3939b
DJ
781/* One item on the queue of compilation units to read in full symbols
782 for. */
783struct dwarf2_queue_item
784{
785 struct dwarf2_per_cu_data *per_cu;
786 struct dwarf2_queue_item *next;
787};
788
789/* The current queue. */
790static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
791
ae038cb0
DJ
792/* Loaded secondary compilation units are kept in memory until they
793 have not been referenced for the processing of this many
794 compilation units. Set this to zero to disable caching. Cache
795 sizes of up to at least twenty will improve startup time for
796 typical inter-CU-reference binaries, at an obvious memory cost. */
797static int dwarf2_max_cache_age = 5;
920d2a44
AC
798static void
799show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
800 struct cmd_list_element *c, const char *value)
801{
802 fprintf_filtered (file, _("\
803The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
804 value);
805}
806
ae038cb0 807
c906108c
SS
808/* Various complaints about symbol reading that don't abort the process */
809
4d3c2250
KB
810static void
811dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 812{
4d3c2250 813 complaint (&symfile_complaints,
e2e0b3e5 814 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
815}
816
25e43795
DJ
817static void
818dwarf2_debug_line_missing_file_complaint (void)
819{
820 complaint (&symfile_complaints,
821 _(".debug_line section has line data without a file"));
822}
823
59205f5a
JB
824static void
825dwarf2_debug_line_missing_end_sequence_complaint (void)
826{
827 complaint (&symfile_complaints,
828 _(".debug_line section has line program sequence without an end"));
829}
830
4d3c2250
KB
831static void
832dwarf2_complex_location_expr_complaint (void)
2e276125 833{
e2e0b3e5 834 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
835}
836
4d3c2250
KB
837static void
838dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
839 int arg3)
2e276125 840{
4d3c2250 841 complaint (&symfile_complaints,
e2e0b3e5 842 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
843 arg2, arg3);
844}
845
846static void
847dwarf2_macros_too_long_complaint (void)
2e276125 848{
4d3c2250 849 complaint (&symfile_complaints,
e2e0b3e5 850 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
851}
852
853static void
854dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 855{
4d3c2250 856 complaint (&symfile_complaints,
e2e0b3e5 857 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
858 arg1);
859}
860
861static void
862dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 863{
4d3c2250 864 complaint (&symfile_complaints,
e2e0b3e5 865 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 866}
c906108c 867
c906108c
SS
868/* local function prototypes */
869
4efb68b1 870static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 871
aaa75496
JB
872static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873 struct objfile *);
874
875static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
d85a05f0 876 struct die_info *,
aaa75496
JB
877 struct partial_symtab *);
878
c67a9c90 879static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 880
72bf9492
DJ
881static void scan_partial_symbols (struct partial_die_info *,
882 CORE_ADDR *, CORE_ADDR *,
5734ee8b 883 int, struct dwarf2_cu *);
c906108c 884
72bf9492
DJ
885static void add_partial_symbol (struct partial_die_info *,
886 struct dwarf2_cu *);
63d06c5c 887
72bf9492
DJ
888static void add_partial_namespace (struct partial_die_info *pdi,
889 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 890 int need_pc, struct dwarf2_cu *cu);
63d06c5c 891
5d7cb8df
JK
892static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
893 CORE_ADDR *highpc, int need_pc,
894 struct dwarf2_cu *cu);
895
72bf9492
DJ
896static void add_partial_enumeration (struct partial_die_info *enum_pdi,
897 struct dwarf2_cu *cu);
91c24f0a 898
bc30ff58
JB
899static void add_partial_subprogram (struct partial_die_info *pdi,
900 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 901 int need_pc, struct dwarf2_cu *cu);
bc30ff58 902
fe1b8b76 903static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
904 gdb_byte *buffer, gdb_byte *info_ptr,
905 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 906
a14ed312 907static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 908
a14ed312 909static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 910
e7c27a73 911static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 912
f3dd6933 913static void dwarf2_free_abbrev_table (void *);
c906108c 914
fe1b8b76 915static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 916 struct dwarf2_cu *);
72bf9492 917
57349743 918static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 919 struct dwarf2_cu *);
c906108c 920
93311388
DE
921static struct partial_die_info *load_partial_dies (bfd *,
922 gdb_byte *, gdb_byte *,
923 int, struct dwarf2_cu *);
72bf9492 924
fe1b8b76 925static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
926 struct abbrev_info *abbrev,
927 unsigned int, bfd *,
928 gdb_byte *, gdb_byte *,
929 struct dwarf2_cu *);
c906108c 930
c764a876 931static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 932 struct dwarf2_cu *);
72bf9492
DJ
933
934static void fixup_partial_die (struct partial_die_info *,
935 struct dwarf2_cu *);
936
fe1b8b76
JB
937static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
938 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 939
fe1b8b76
JB
940static gdb_byte *read_attribute_value (struct attribute *, unsigned,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 942
fe1b8b76 943static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 944
fe1b8b76 945static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 946
fe1b8b76 947static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 948
fe1b8b76 949static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 950
93311388 951static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 952
fe1b8b76 953static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 954 unsigned int *);
c906108c 955
c764a876
DE
956static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
957
958static LONGEST read_checked_initial_length_and_offset
959 (bfd *, gdb_byte *, const struct comp_unit_head *,
960 unsigned int *, unsigned int *);
613e1657 961
fe1b8b76 962static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
963 unsigned int *);
964
965static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 966
fe1b8b76 967static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 968
9b1c24c8 969static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 970
fe1b8b76
JB
971static char *read_indirect_string (bfd *, gdb_byte *,
972 const struct comp_unit_head *,
973 unsigned int *);
4bdf3d34 974
fe1b8b76 975static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 976
fe1b8b76 977static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 978
fe1b8b76 979static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 980
e142c38c 981static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 982
e142c38c
DJ
983static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
984 struct dwarf2_cu *);
c906108c 985
348e048f
DE
986static struct attribute *dwarf2_attr_no_follow (struct die_info *,
987 unsigned int,
988 struct dwarf2_cu *);
989
05cf31d1
JB
990static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
991 struct dwarf2_cu *cu);
992
e142c38c 993static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 994
e142c38c 995static struct die_info *die_specification (struct die_info *die,
f2f0e013 996 struct dwarf2_cu **);
63d06c5c 997
debd256d
JB
998static void free_line_header (struct line_header *lh);
999
aaa75496
JB
1000static void add_file_name (struct line_header *, char *, unsigned int,
1001 unsigned int, unsigned int);
1002
debd256d
JB
1003static struct line_header *(dwarf_decode_line_header
1004 (unsigned int offset,
e7c27a73 1005 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
1006
1007static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 1008 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1009
4f1520fb 1010static void dwarf2_start_subfile (char *, char *, char *);
c906108c 1011
a14ed312 1012static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1013 struct dwarf2_cu *);
c906108c 1014
34eaf542
TT
1015static struct symbol *new_symbol_full (struct die_info *, struct type *,
1016 struct dwarf2_cu *, struct symbol *);
1017
a14ed312 1018static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1019 struct dwarf2_cu *);
c906108c 1020
98bfdba5
PA
1021static void dwarf2_const_value_attr (struct attribute *attr,
1022 struct type *type,
1023 const char *name,
1024 struct obstack *obstack,
1025 struct dwarf2_cu *cu, long *value,
1026 gdb_byte **bytes,
1027 struct dwarf2_locexpr_baton **baton);
2df3850c 1028
e7c27a73 1029static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1030
b4ba55a1
JB
1031static int need_gnat_info (struct dwarf2_cu *);
1032
1033static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1034
1035static void set_descriptive_type (struct type *, struct die_info *,
1036 struct dwarf2_cu *);
1037
e7c27a73
DJ
1038static struct type *die_containing_type (struct die_info *,
1039 struct dwarf2_cu *);
c906108c 1040
673bfd45
DE
1041static struct type *lookup_die_type (struct die_info *, struct attribute *,
1042 struct dwarf2_cu *);
c906108c 1043
f792889a 1044static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1045
673bfd45
DE
1046static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1047
086ed43d 1048static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1049
6e70227d 1050static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1051 const char *suffix, int physname,
1052 struct dwarf2_cu *cu);
63d06c5c 1053
e7c27a73 1054static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1055
348e048f
DE
1056static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1057
e7c27a73 1058static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1059
e7c27a73 1060static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1061
ff013f42
JK
1062static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1063 struct dwarf2_cu *, struct partial_symtab *);
1064
a14ed312 1065static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1066 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1067 struct partial_symtab *);
c906108c 1068
fae299cd
DC
1069static void get_scope_pc_bounds (struct die_info *,
1070 CORE_ADDR *, CORE_ADDR *,
1071 struct dwarf2_cu *);
1072
801e3a5b
JB
1073static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1074 CORE_ADDR, struct dwarf2_cu *);
1075
a14ed312 1076static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1077 struct dwarf2_cu *);
c906108c 1078
a14ed312 1079static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1080 struct type *, struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1083 struct die_info *, struct type *,
e7c27a73 1084 struct dwarf2_cu *);
c906108c 1085
a14ed312 1086static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1087 struct type *, struct dwarf2_cu *);
c906108c 1088
134d01f1 1089static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1090
e7c27a73 1091static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1092
e7c27a73 1093static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1094
5d7cb8df
JK
1095static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1096
27aa8d6a
SW
1097static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1098
f55ee35c
JK
1099static struct type *read_module_type (struct die_info *die,
1100 struct dwarf2_cu *cu);
1101
38d518c9 1102static const char *namespace_name (struct die_info *die,
e142c38c 1103 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1104
134d01f1 1105static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1106
e7c27a73 1107static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1108
6e70227d 1109static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1110 struct dwarf2_cu *);
1111
93311388 1112static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1113
93311388
DE
1114static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
d97bc12b
DE
1116 gdb_byte **new_info_ptr,
1117 struct die_info *parent);
1118
93311388
DE
1119static struct die_info *read_die_and_children (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 struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1125 gdb_byte *info_ptr,
fe1b8b76 1126 gdb_byte **new_info_ptr,
639d11d3
DC
1127 struct die_info *parent);
1128
93311388
DE
1129static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1130 struct die_info **, gdb_byte *,
1131 int *);
1132
e7c27a73 1133static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1134
71c25dea
TT
1135static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1136 struct obstack *);
1137
e142c38c 1138static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1139
98bfdba5
PA
1140static const char *dwarf2_full_name (char *name,
1141 struct die_info *die,
1142 struct dwarf2_cu *cu);
1143
e142c38c 1144static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1145 struct dwarf2_cu **);
9219021c 1146
a14ed312 1147static char *dwarf_tag_name (unsigned int);
c906108c 1148
a14ed312 1149static char *dwarf_attr_name (unsigned int);
c906108c 1150
a14ed312 1151static char *dwarf_form_name (unsigned int);
c906108c 1152
a14ed312 1153static char *dwarf_bool_name (unsigned int);
c906108c 1154
a14ed312 1155static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1156
1157#if 0
a14ed312 1158static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1159#endif
1160
f9aca02d 1161static struct die_info *sibling_die (struct die_info *);
c906108c 1162
d97bc12b
DE
1163static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1164
1165static void dump_die_for_error (struct die_info *);
1166
1167static void dump_die_1 (struct ui_file *, int level, int max_level,
1168 struct die_info *);
c906108c 1169
d97bc12b 1170/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1171
51545339 1172static void store_in_ref_table (struct die_info *,
10b3939b 1173 struct dwarf2_cu *);
c906108c 1174
93311388
DE
1175static int is_ref_attr (struct attribute *);
1176
c764a876 1177static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1178
43bbcdc2 1179static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1180
348e048f
DE
1181static struct die_info *follow_die_ref_or_sig (struct die_info *,
1182 struct attribute *,
1183 struct dwarf2_cu **);
1184
10b3939b
DJ
1185static struct die_info *follow_die_ref (struct die_info *,
1186 struct attribute *,
f2f0e013 1187 struct dwarf2_cu **);
c906108c 1188
348e048f
DE
1189static struct die_info *follow_die_sig (struct die_info *,
1190 struct attribute *,
1191 struct dwarf2_cu **);
1192
1193static void read_signatured_type_at_offset (struct objfile *objfile,
1194 unsigned int offset);
1195
1196static void read_signatured_type (struct objfile *,
1197 struct signatured_type *type_sig);
1198
c906108c
SS
1199/* memory allocation interface */
1200
7b5a2f43 1201static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1202
f3dd6933 1203static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1204
b60c80d6 1205static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1206
e142c38c 1207static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1208
e142c38c
DJ
1209static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1210 struct dwarf2_cu *);
5fb290d7 1211
2e276125 1212static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1213 char *, bfd *, struct dwarf2_cu *);
2e276125 1214
8e19ed76
PS
1215static int attr_form_is_block (struct attribute *);
1216
3690dd37
JB
1217static int attr_form_is_section_offset (struct attribute *);
1218
1219static int attr_form_is_constant (struct attribute *);
1220
93e7bd98
DJ
1221static void dwarf2_symbol_mark_computed (struct attribute *attr,
1222 struct symbol *sym,
1223 struct dwarf2_cu *cu);
4c2df51b 1224
93311388
DE
1225static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1226 struct abbrev_info *abbrev,
1227 struct dwarf2_cu *cu);
4bb7a0a7 1228
72bf9492
DJ
1229static void free_stack_comp_unit (void *);
1230
72bf9492
DJ
1231static hashval_t partial_die_hash (const void *item);
1232
1233static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1234
ae038cb0 1235static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1236 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1237
1238static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1239 (unsigned int offset, struct objfile *objfile);
ae038cb0 1240
93311388
DE
1241static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1242
ae038cb0
DJ
1243static void free_one_comp_unit (void *);
1244
1245static void free_cached_comp_units (void *);
1246
1247static void age_cached_comp_units (void);
1248
1249static void free_one_cached_comp_unit (void *);
1250
f792889a
DJ
1251static struct type *set_die_type (struct die_info *, struct type *,
1252 struct dwarf2_cu *);
1c379e20 1253
ae038cb0
DJ
1254static void create_all_comp_units (struct objfile *);
1255
1fd400ff
TT
1256static int create_debug_types_hash_table (struct objfile *objfile);
1257
93311388
DE
1258static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1259 struct objfile *);
10b3939b
DJ
1260
1261static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1262
1263static void dwarf2_add_dependence (struct dwarf2_cu *,
1264 struct dwarf2_per_cu_data *);
1265
ae038cb0
DJ
1266static void dwarf2_mark (struct dwarf2_cu *);
1267
1268static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1269
673bfd45
DE
1270static struct type *get_die_type_at_offset (unsigned int,
1271 struct dwarf2_per_cu_data *per_cu);
1272
f792889a 1273static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1274
9291a0cd
TT
1275static void dwarf2_release_queue (void *dummy);
1276
1277static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1278 struct objfile *objfile);
1279
1280static void process_queue (struct objfile *objfile);
1281
1282static void find_file_and_directory (struct die_info *die,
1283 struct dwarf2_cu *cu,
1284 char **name, char **comp_dir);
1285
1286static char *file_full_name (int file, struct line_header *lh,
1287 const char *comp_dir);
1288
1289static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1290 gdb_byte *info_ptr,
1291 gdb_byte *buffer,
1292 unsigned int buffer_size,
1293 bfd *abfd);
1294
1295static void init_cu_die_reader (struct die_reader_specs *reader,
1296 struct dwarf2_cu *cu);
1297
673bfd45 1298static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1299
9291a0cd
TT
1300#if WORDS_BIGENDIAN
1301
1302/* Convert VALUE between big- and little-endian. */
1303static offset_type
1304byte_swap (offset_type value)
1305{
1306 offset_type result;
1307
1308 result = (value & 0xff) << 24;
1309 result |= (value & 0xff00) << 8;
1310 result |= (value & 0xff0000) >> 8;
1311 result |= (value & 0xff000000) >> 24;
1312 return result;
1313}
1314
1315#define MAYBE_SWAP(V) byte_swap (V)
1316
1317#else
1318#define MAYBE_SWAP(V) (V)
1319#endif /* WORDS_BIGENDIAN */
1320
1321/* The suffix for an index file. */
1322#define INDEX_SUFFIX ".gdb-index"
1323
3da10d80
KS
1324static const char *dwarf2_physname (char *name, struct die_info *die,
1325 struct dwarf2_cu *cu);
1326
c906108c
SS
1327/* Try to locate the sections we need for DWARF 2 debugging
1328 information and return true if we have enough to do something. */
1329
1330int
6502dd73 1331dwarf2_has_info (struct objfile *objfile)
c906108c 1332{
be391dca
TT
1333 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1334 if (!dwarf2_per_objfile)
1335 {
1336 /* Initialize per-objfile state. */
1337 struct dwarf2_per_objfile *data
1338 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1339
be391dca
TT
1340 memset (data, 0, sizeof (*data));
1341 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1342 dwarf2_per_objfile = data;
6502dd73 1343
be391dca
TT
1344 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1345 dwarf2_per_objfile->objfile = objfile;
1346 }
1347 return (dwarf2_per_objfile->info.asection != NULL
1348 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1349}
1350
233a11ab
CS
1351/* When loading sections, we can either look for ".<name>", or for
1352 * ".z<name>", which indicates a compressed section. */
1353
1354static int
dce234bc 1355section_is_p (const char *section_name, const char *name)
233a11ab 1356{
dce234bc
PP
1357 return (section_name[0] == '.'
1358 && (strcmp (section_name + 1, name) == 0
1359 || (section_name[1] == 'z'
1360 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1361}
1362
c906108c
SS
1363/* This function is mapped across the sections and remembers the
1364 offset and size of each of the debugging sections we are interested
1365 in. */
1366
1367static void
72dca2f5 1368dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1369{
dce234bc 1370 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1371 {
dce234bc
PP
1372 dwarf2_per_objfile->info.asection = sectp;
1373 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1374 }
dce234bc 1375 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1376 {
dce234bc
PP
1377 dwarf2_per_objfile->abbrev.asection = sectp;
1378 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1379 }
dce234bc 1380 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1381 {
dce234bc
PP
1382 dwarf2_per_objfile->line.asection = sectp;
1383 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1384 }
dce234bc 1385 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1386 {
dce234bc
PP
1387 dwarf2_per_objfile->loc.asection = sectp;
1388 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1389 }
dce234bc 1390 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1391 {
dce234bc
PP
1392 dwarf2_per_objfile->macinfo.asection = sectp;
1393 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1394 }
dce234bc 1395 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1396 {
dce234bc
PP
1397 dwarf2_per_objfile->str.asection = sectp;
1398 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1399 }
dce234bc 1400 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1401 {
dce234bc
PP
1402 dwarf2_per_objfile->frame.asection = sectp;
1403 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1404 }
dce234bc 1405 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1406 {
3799ccc6 1407 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1408
3799ccc6
EZ
1409 if (aflag & SEC_HAS_CONTENTS)
1410 {
dce234bc
PP
1411 dwarf2_per_objfile->eh_frame.asection = sectp;
1412 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1413 }
b6af0555 1414 }
dce234bc 1415 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1416 {
dce234bc
PP
1417 dwarf2_per_objfile->ranges.asection = sectp;
1418 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1419 }
348e048f
DE
1420 else if (section_is_p (sectp->name, TYPES_SECTION))
1421 {
1422 dwarf2_per_objfile->types.asection = sectp;
1423 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1424 }
9291a0cd
TT
1425 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1426 {
1427 dwarf2_per_objfile->gdb_index.asection = sectp;
1428 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1429 }
dce234bc 1430
72dca2f5
FR
1431 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1432 && bfd_section_vma (abfd, sectp) == 0)
1433 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1434}
1435
dce234bc
PP
1436/* Decompress a section that was compressed using zlib. Store the
1437 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1438
1439static void
dce234bc
PP
1440zlib_decompress_section (struct objfile *objfile, asection *sectp,
1441 gdb_byte **outbuf, bfd_size_type *outsize)
1442{
1443 bfd *abfd = objfile->obfd;
1444#ifndef HAVE_ZLIB_H
1445 error (_("Support for zlib-compressed DWARF data (from '%s') "
1446 "is disabled in this copy of GDB"),
1447 bfd_get_filename (abfd));
1448#else
1449 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1450 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1451 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1452 bfd_size_type uncompressed_size;
1453 gdb_byte *uncompressed_buffer;
1454 z_stream strm;
1455 int rc;
1456 int header_size = 12;
1457
1458 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1459 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1460 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1461 bfd_get_filename (abfd));
1462
1463 /* Read the zlib header. In this case, it should be "ZLIB" followed
1464 by the uncompressed section size, 8 bytes in big-endian order. */
1465 if (compressed_size < header_size
1466 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1467 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1468 bfd_get_filename (abfd));
1469 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1475 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1476 uncompressed_size += compressed_buffer[11];
1477
1478 /* It is possible the section consists of several compressed
1479 buffers concatenated together, so we uncompress in a loop. */
1480 strm.zalloc = NULL;
1481 strm.zfree = NULL;
1482 strm.opaque = NULL;
1483 strm.avail_in = compressed_size - header_size;
1484 strm.next_in = (Bytef*) compressed_buffer + header_size;
1485 strm.avail_out = uncompressed_size;
1486 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1487 uncompressed_size);
1488 rc = inflateInit (&strm);
1489 while (strm.avail_in > 0)
1490 {
1491 if (rc != Z_OK)
1492 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1493 bfd_get_filename (abfd), rc);
1494 strm.next_out = ((Bytef*) uncompressed_buffer
1495 + (uncompressed_size - strm.avail_out));
1496 rc = inflate (&strm, Z_FINISH);
1497 if (rc != Z_STREAM_END)
1498 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1499 bfd_get_filename (abfd), rc);
1500 rc = inflateReset (&strm);
1501 }
1502 rc = inflateEnd (&strm);
1503 if (rc != Z_OK
1504 || strm.avail_out != 0)
1505 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1506 bfd_get_filename (abfd), rc);
1507
affddf13 1508 do_cleanups (cleanup);
dce234bc
PP
1509 *outbuf = uncompressed_buffer;
1510 *outsize = uncompressed_size;
1511#endif
233a11ab
CS
1512}
1513
dce234bc
PP
1514/* Read the contents of the section SECTP from object file specified by
1515 OBJFILE, store info about the section into INFO.
1516 If the section is compressed, uncompress it before returning. */
c906108c 1517
dce234bc
PP
1518static void
1519dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1520{
dce234bc
PP
1521 bfd *abfd = objfile->obfd;
1522 asection *sectp = info->asection;
1523 gdb_byte *buf, *retbuf;
1524 unsigned char header[4];
c906108c 1525
be391dca
TT
1526 if (info->readin)
1527 return;
dce234bc
PP
1528 info->buffer = NULL;
1529 info->was_mmapped = 0;
be391dca 1530 info->readin = 1;
188dd5d6 1531
dce234bc
PP
1532 if (info->asection == NULL || info->size == 0)
1533 return;
c906108c 1534
dce234bc
PP
1535 /* Check if the file has a 4-byte header indicating compression. */
1536 if (info->size > sizeof (header)
1537 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1538 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1539 {
1540 /* Upon decompression, update the buffer and its size. */
1541 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1542 {
1543 zlib_decompress_section (objfile, sectp, &info->buffer,
1544 &info->size);
1545 return;
1546 }
1547 }
4bdf3d34 1548
dce234bc
PP
1549#ifdef HAVE_MMAP
1550 if (pagesize == 0)
1551 pagesize = getpagesize ();
2e276125 1552
dce234bc
PP
1553 /* Only try to mmap sections which are large enough: we don't want to
1554 waste space due to fragmentation. Also, only try mmap for sections
1555 without relocations. */
1556
1557 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1558 {
1559 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1560 size_t map_length = info->size + sectp->filepos - pg_offset;
1561 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1562 MAP_PRIVATE, pg_offset);
1563
1564 if (retbuf != MAP_FAILED)
1565 {
1566 info->was_mmapped = 1;
1567 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1568#if HAVE_POSIX_MADVISE
1569 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1570#endif
dce234bc
PP
1571 return;
1572 }
1573 }
1574#endif
1575
1576 /* If we get here, we are a normal, not-compressed section. */
1577 info->buffer = buf
1578 = obstack_alloc (&objfile->objfile_obstack, info->size);
1579
1580 /* When debugging .o files, we may need to apply relocations; see
1581 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1582 We never compress sections in .o files, so we only need to
1583 try this when the section is not compressed. */
ac8035ab 1584 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1585 if (retbuf != NULL)
1586 {
1587 info->buffer = retbuf;
1588 return;
1589 }
1590
1591 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1592 || bfd_bread (buf, info->size, abfd) != info->size)
1593 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1594 bfd_get_filename (abfd));
1595}
1596
1597/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1598 SECTION_NAME. */
af34e669 1599
dce234bc
PP
1600void
1601dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1602 asection **sectp, gdb_byte **bufp,
1603 bfd_size_type *sizep)
1604{
1605 struct dwarf2_per_objfile *data
1606 = objfile_data (objfile, dwarf2_objfile_data_key);
1607 struct dwarf2_section_info *info;
a3b2a86b
TT
1608
1609 /* We may see an objfile without any DWARF, in which case we just
1610 return nothing. */
1611 if (data == NULL)
1612 {
1613 *sectp = NULL;
1614 *bufp = NULL;
1615 *sizep = 0;
1616 return;
1617 }
dce234bc
PP
1618 if (section_is_p (section_name, EH_FRAME_SECTION))
1619 info = &data->eh_frame;
1620 else if (section_is_p (section_name, FRAME_SECTION))
1621 info = &data->frame;
0d53c4c4 1622 else
f3574227 1623 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1624
1625 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1626 /* We haven't read this section in yet. Do it now. */
1627 dwarf2_read_section (objfile, info);
1628
1629 *sectp = info->asection;
1630 *bufp = info->buffer;
1631 *sizep = info->size;
1632}
1633
9291a0cd
TT
1634\f
1635
1636/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1637 this CU came. */
1638static void
1639dw2_do_instantiate_symtab (struct objfile *objfile,
1640 struct dwarf2_per_cu_data *per_cu)
1641{
1642 struct cleanup *back_to;
1643
1644 back_to = make_cleanup (dwarf2_release_queue, NULL);
1645
1646 queue_comp_unit (per_cu, objfile);
1647
1648 if (per_cu->from_debug_types)
1649 read_signatured_type_at_offset (objfile, per_cu->offset);
1650 else
1651 load_full_comp_unit (per_cu, objfile);
1652
1653 process_queue (objfile);
1654
1655 /* Age the cache, releasing compilation units that have not
1656 been used recently. */
1657 age_cached_comp_units ();
1658
1659 do_cleanups (back_to);
1660}
1661
1662/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1663 the objfile from which this CU came. Returns the resulting symbol
1664 table. */
1665static struct symtab *
1666dw2_instantiate_symtab (struct objfile *objfile,
1667 struct dwarf2_per_cu_data *per_cu)
1668{
1669 if (!per_cu->v.quick->symtab)
1670 {
1671 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1672 increment_reading_symtab ();
1673 dw2_do_instantiate_symtab (objfile, per_cu);
1674 do_cleanups (back_to);
1675 }
1676 return per_cu->v.quick->symtab;
1677}
1678
1fd400ff
TT
1679/* Return the CU given its index. */
1680static struct dwarf2_per_cu_data *
1681dw2_get_cu (int index)
1682{
1683 if (index >= dwarf2_per_objfile->n_comp_units)
1684 {
1685 index -= dwarf2_per_objfile->n_comp_units;
1686 return dwarf2_per_objfile->type_comp_units[index];
1687 }
1688 return dwarf2_per_objfile->all_comp_units[index];
1689}
1690
9291a0cd
TT
1691/* A helper function that knows how to read a 64-bit value in a way
1692 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1693 otherwise. */
1694static int
1695extract_cu_value (const char *bytes, ULONGEST *result)
1696{
1697 if (sizeof (ULONGEST) < 8)
1698 {
1699 int i;
1700
1701 /* Ignore the upper 4 bytes if they are all zero. */
1702 for (i = 0; i < 4; ++i)
1703 if (bytes[i + 4] != 0)
1704 return 0;
1705
1706 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1707 }
1708 else
1709 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1710 return 1;
1711}
1712
1713/* Read the CU list from the mapped index, and use it to create all
1714 the CU objects for this objfile. Return 0 if something went wrong,
1715 1 if everything went ok. */
1716static int
1fd400ff
TT
1717create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1718 offset_type cu_list_elements)
9291a0cd
TT
1719{
1720 offset_type i;
9291a0cd
TT
1721
1722 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1723 dwarf2_per_objfile->all_comp_units
1724 = obstack_alloc (&objfile->objfile_obstack,
1725 dwarf2_per_objfile->n_comp_units
1726 * sizeof (struct dwarf2_per_cu_data *));
1727
1728 for (i = 0; i < cu_list_elements; i += 2)
1729 {
1730 struct dwarf2_per_cu_data *the_cu;
1731 ULONGEST offset, length;
1732
1733 if (!extract_cu_value (cu_list, &offset)
1734 || !extract_cu_value (cu_list + 8, &length))
1735 return 0;
1736 cu_list += 2 * 8;
1737
1738 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1739 struct dwarf2_per_cu_data);
1740 the_cu->offset = offset;
1741 the_cu->length = length;
1742 the_cu->objfile = objfile;
1743 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1744 struct dwarf2_per_cu_quick_data);
1745 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1746 }
1747
1748 return 1;
1749}
1750
1fd400ff 1751/* Create the signatured type hash table from the index. */
673bfd45 1752
1fd400ff 1753static int
673bfd45
DE
1754create_signatured_type_table_from_index (struct objfile *objfile,
1755 const gdb_byte *bytes,
1756 offset_type elements)
1fd400ff
TT
1757{
1758 offset_type i;
673bfd45 1759 htab_t sig_types_hash;
1fd400ff
TT
1760
1761 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1762 dwarf2_per_objfile->type_comp_units
1763 = obstack_alloc (&objfile->objfile_obstack,
1764 dwarf2_per_objfile->n_type_comp_units
1765 * sizeof (struct dwarf2_per_cu_data *));
1766
673bfd45 1767 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1768
1769 for (i = 0; i < elements; i += 3)
1770 {
1771 struct signatured_type *type_sig;
1772 ULONGEST offset, type_offset, signature;
1773 void **slot;
1774
1775 if (!extract_cu_value (bytes, &offset)
1776 || !extract_cu_value (bytes + 8, &type_offset))
1777 return 0;
1778 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1779 bytes += 3 * 8;
1780
1781 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1782 struct signatured_type);
1783 type_sig->signature = signature;
1784 type_sig->offset = offset;
1785 type_sig->type_offset = type_offset;
1786 type_sig->per_cu.from_debug_types = 1;
1787 type_sig->per_cu.offset = offset;
1788 type_sig->per_cu.objfile = objfile;
1789 type_sig->per_cu.v.quick
1790 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1791 struct dwarf2_per_cu_quick_data);
1792
673bfd45 1793 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1794 *slot = type_sig;
1795
1796 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1797 }
1798
673bfd45 1799 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1800
1801 return 1;
1802}
1803
9291a0cd
TT
1804/* Read the address map data from the mapped index, and use it to
1805 populate the objfile's psymtabs_addrmap. */
1806static void
1807create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1808{
1809 const gdb_byte *iter, *end;
1810 struct obstack temp_obstack;
1811 struct addrmap *mutable_map;
1812 struct cleanup *cleanup;
1813 CORE_ADDR baseaddr;
1814
1815 obstack_init (&temp_obstack);
1816 cleanup = make_cleanup_obstack_free (&temp_obstack);
1817 mutable_map = addrmap_create_mutable (&temp_obstack);
1818
1819 iter = index->address_table;
1820 end = iter + index->address_table_size;
1821
1822 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1823
1824 while (iter < end)
1825 {
1826 ULONGEST hi, lo, cu_index;
1827 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1828 iter += 8;
1829 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1830 iter += 8;
1831 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1832 iter += 4;
1833
1834 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1835 dw2_get_cu (cu_index));
9291a0cd
TT
1836 }
1837
1838 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1839 &objfile->objfile_obstack);
1840 do_cleanups (cleanup);
1841}
1842
1843/* The hash function for strings in the mapped index. This is the
1844 same as the hashtab.c hash function, but we keep a separate copy to
1845 maintain control over the implementation. This is necessary
1846 because the hash function is tied to the format of the mapped index
1847 file. */
1848static hashval_t
1849mapped_index_string_hash (const void *p)
1850{
1851 const unsigned char *str = (const unsigned char *) p;
1852 hashval_t r = 0;
1853 unsigned char c;
1854
1855 while ((c = *str++) != 0)
1856 r = r * 67 + c - 113;
1857
1858 return r;
1859}
1860
1861/* Find a slot in the mapped index INDEX for the object named NAME.
1862 If NAME is found, set *VEC_OUT to point to the CU vector in the
1863 constant pool and return 1. If NAME cannot be found, return 0. */
1864static int
1865find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1866 offset_type **vec_out)
1867{
1868 offset_type hash = mapped_index_string_hash (name);
1869 offset_type slot, step;
1870
1871 slot = hash & (index->index_table_slots - 1);
1872 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1873
1874 for (;;)
1875 {
1876 /* Convert a slot number to an offset into the table. */
1877 offset_type i = 2 * slot;
1878 const char *str;
1879 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1880 return 0;
1881
1882 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1883 if (!strcmp (name, str))
1884 {
1885 *vec_out = (offset_type *) (index->constant_pool
1886 + MAYBE_SWAP (index->index_table[i + 1]));
1887 return 1;
1888 }
1889
1890 slot = (slot + step) & (index->index_table_slots - 1);
1891 }
1892}
1893
1894/* Read the index file. If everything went ok, initialize the "quick"
1895 elements of all the CUs and return 1. Otherwise, return 0. */
1896static int
1897dwarf2_read_index (struct objfile *objfile)
1898{
9291a0cd
TT
1899 char *addr;
1900 struct mapped_index *map;
b3b272e1 1901 offset_type *metadata;
ac0b195c
KW
1902 const gdb_byte *cu_list;
1903 const gdb_byte *types_list = NULL;
1904 offset_type version, cu_list_elements;
1905 offset_type types_list_elements = 0;
1fd400ff 1906 int i;
9291a0cd
TT
1907
1908 if (dwarf2_per_objfile->gdb_index.asection == NULL
1909 || dwarf2_per_objfile->gdb_index.size == 0)
1910 return 0;
1911 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1912
1913 addr = dwarf2_per_objfile->gdb_index.buffer;
1914 /* Version check. */
1fd400ff
TT
1915 version = MAYBE_SWAP (*(offset_type *) addr);
1916 if (version == 1)
1917 {
1918 /* Index version 1 neglected to account for .debug_types. So,
1919 if we see .debug_types, we cannot use this index. */
1920 if (dwarf2_per_objfile->types.asection != NULL
1921 && dwarf2_per_objfile->types.size != 0)
1922 return 0;
1923 }
1924 else if (version != 2)
9291a0cd
TT
1925 return 0;
1926
1927 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1928 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1929
1930 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1931
1932 i = 0;
1933 cu_list = addr + MAYBE_SWAP (metadata[i]);
1934 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1935 / 8);
1fd400ff
TT
1936 ++i;
1937
1938 if (version == 2)
1939 {
1940 types_list = addr + MAYBE_SWAP (metadata[i]);
1941 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1942 - MAYBE_SWAP (metadata[i]))
1943 / 8);
1944 ++i;
1945 }
1946
1947 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1948 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1949 - MAYBE_SWAP (metadata[i]));
1950 ++i;
1951
1952 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1953 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1954 - MAYBE_SWAP (metadata[i]))
9291a0cd 1955 / (2 * sizeof (offset_type)));
1fd400ff 1956 ++i;
9291a0cd 1957
1fd400ff
TT
1958 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1959
1960 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1961 return 0;
1962
1963 if (version == 2
1964 && types_list_elements
673bfd45
DE
1965 && !create_signatured_type_table_from_index (objfile, types_list,
1966 types_list_elements))
9291a0cd
TT
1967 return 0;
1968
1969 create_addrmap_from_index (objfile, map);
1970
1971 dwarf2_per_objfile->index_table = map;
1972 dwarf2_per_objfile->using_index = 1;
1973
1974 return 1;
1975}
1976
1977/* A helper for the "quick" functions which sets the global
1978 dwarf2_per_objfile according to OBJFILE. */
1979static void
1980dw2_setup (struct objfile *objfile)
1981{
1982 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1983 gdb_assert (dwarf2_per_objfile);
1984}
1985
1986/* A helper for the "quick" functions which attempts to read the line
1987 table for THIS_CU. */
1988static void
1989dw2_require_line_header (struct objfile *objfile,
1990 struct dwarf2_per_cu_data *this_cu)
1991{
1992 bfd *abfd = objfile->obfd;
1993 struct line_header *lh = NULL;
1994 struct attribute *attr;
1995 struct cleanup *cleanups;
1996 struct die_info *comp_unit_die;
1997 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1998 int has_children, i;
1999 struct dwarf2_cu cu;
2000 unsigned int bytes_read, buffer_size;
2001 struct die_reader_specs reader_specs;
2002 char *name, *comp_dir;
2003
2004 if (this_cu->v.quick->read_lines)
2005 return;
2006 this_cu->v.quick->read_lines = 1;
2007
2008 memset (&cu, 0, sizeof (cu));
2009 cu.objfile = objfile;
2010 obstack_init (&cu.comp_unit_obstack);
2011
2012 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2013
2014 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2015 buffer_size = dwarf2_per_objfile->info.size;
2016 buffer = dwarf2_per_objfile->info.buffer;
2017 info_ptr = buffer + this_cu->offset;
2018 beg_of_comp_unit = info_ptr;
2019
2020 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2021 buffer, buffer_size,
2022 abfd);
2023
2024 /* Complete the cu_header. */
2025 cu.header.offset = beg_of_comp_unit - buffer;
2026 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2027
2028 this_cu->cu = &cu;
2029 cu.per_cu = this_cu;
2030
2031 dwarf2_read_abbrevs (abfd, &cu);
2032 make_cleanup (dwarf2_free_abbrev_table, &cu);
2033
2034 if (this_cu->from_debug_types)
2035 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2036 init_cu_die_reader (&reader_specs, &cu);
2037 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2038 &has_children);
2039
2040 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2041 if (attr)
2042 {
2043 unsigned int line_offset = DW_UNSND (attr);
2044 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2045 }
2046 if (lh == NULL)
2047 {
2048 do_cleanups (cleanups);
2049 return;
2050 }
2051
2052 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2053
2054 this_cu->v.quick->lines = lh;
2055
2056 this_cu->v.quick->file_names
2057 = obstack_alloc (&objfile->objfile_obstack,
2058 lh->num_file_names * sizeof (char *));
2059 for (i = 0; i < lh->num_file_names; ++i)
2060 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2061
2062 do_cleanups (cleanups);
2063}
2064
2065/* A helper for the "quick" functions which computes and caches the
2066 real path for a given file name from the line table.
2067 dw2_require_line_header must have been called before this is
2068 invoked. */
2069static const char *
2070dw2_require_full_path (struct objfile *objfile,
2071 struct dwarf2_per_cu_data *cu,
2072 int index)
2073{
2074 if (!cu->v.quick->full_names)
2075 cu->v.quick->full_names
2076 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2077 cu->v.quick->lines->num_file_names,
2078 sizeof (char *));
2079
2080 if (!cu->v.quick->full_names[index])
2081 cu->v.quick->full_names[index]
2082 = gdb_realpath (cu->v.quick->file_names[index]);
2083
2084 return cu->v.quick->full_names[index];
2085}
2086
2087static struct symtab *
2088dw2_find_last_source_symtab (struct objfile *objfile)
2089{
2090 int index;
2091 dw2_setup (objfile);
2092 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2093 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2094}
2095
2096static void
2097dw2_forget_cached_source_info (struct objfile *objfile)
2098{
2099 int i;
2100
2101 dw2_setup (objfile);
1fd400ff
TT
2102 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2103 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2104 {
1fd400ff 2105 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2106
2107 if (cu->v.quick->full_names)
2108 {
2109 int j;
2110
2111 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2112 xfree ((void *) cu->v.quick->full_names[j]);
2113 }
2114 }
2115}
2116
2117static int
2118dw2_lookup_symtab (struct objfile *objfile, const char *name,
2119 const char *full_path, const char *real_path,
2120 struct symtab **result)
2121{
2122 int i;
2123 int check_basename = lbasename (name) == name;
2124 struct dwarf2_per_cu_data *base_cu = NULL;
2125
2126 dw2_setup (objfile);
1fd400ff
TT
2127 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2128 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2129 {
2130 int j;
1fd400ff 2131 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2132
2133 if (cu->v.quick->symtab)
2134 continue;
2135
2136 dw2_require_line_header (objfile, cu);
2137 if (!cu->v.quick->lines)
2138 continue;
2139
2140 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2141 {
2142 const char *this_name = cu->v.quick->file_names[j];
2143
2144 if (FILENAME_CMP (name, this_name) == 0)
2145 {
2146 *result = dw2_instantiate_symtab (objfile, cu);
2147 return 1;
2148 }
2149
2150 if (check_basename && ! base_cu
2151 && FILENAME_CMP (lbasename (this_name), name) == 0)
2152 base_cu = cu;
2153
2154 if (full_path != NULL)
2155 {
2156 const char *this_full_name = dw2_require_full_path (objfile,
2157 cu, j);
2158
2159 if (this_full_name
2160 && FILENAME_CMP (full_path, this_full_name) == 0)
2161 {
2162 *result = dw2_instantiate_symtab (objfile, cu);
2163 return 1;
2164 }
2165 }
2166
2167 if (real_path != NULL)
2168 {
2169 const char *this_full_name = dw2_require_full_path (objfile,
2170 cu, j);
2171
2172 if (this_full_name != NULL)
2173 {
2174 char *rp = gdb_realpath (this_full_name);
2175 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2176 {
2177 xfree (rp);
2178 *result = dw2_instantiate_symtab (objfile, cu);
2179 return 1;
2180 }
2181 xfree (rp);
2182 }
2183 }
2184 }
2185 }
2186
2187 if (base_cu)
2188 {
2189 *result = dw2_instantiate_symtab (objfile, base_cu);
2190 return 1;
2191 }
2192
2193 return 0;
2194}
2195
2196static struct symtab *
2197dw2_lookup_symbol (struct objfile *objfile, int block_index,
2198 const char *name, domain_enum domain)
2199{
2200 /* We do all the work in the pre_expand_symtabs_matching hook
2201 instead. */
2202 return NULL;
2203}
2204
2205/* A helper function that expands all symtabs that hold an object
2206 named NAME. */
2207static void
2208dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2209{
2210 dw2_setup (objfile);
2211
2212 if (dwarf2_per_objfile->index_table)
2213 {
2214 offset_type *vec;
2215
2216 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2217 name, &vec))
2218 {
2219 offset_type i, len = MAYBE_SWAP (*vec);
2220 for (i = 0; i < len; ++i)
2221 {
2222 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
1fd400ff
TT
2223 struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2224
9291a0cd
TT
2225 dw2_instantiate_symtab (objfile, cu);
2226 }
2227 }
2228 }
2229}
2230
2231static void
2232dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2233 int kind, const char *name,
2234 domain_enum domain)
2235{
2236 dw2_do_expand_symtabs_matching (objfile, name);
2237}
2238
2239static void
2240dw2_print_stats (struct objfile *objfile)
2241{
2242 int i, count;
2243
2244 dw2_setup (objfile);
2245 count = 0;
1fd400ff
TT
2246 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2247 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2248 {
1fd400ff 2249 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2250
2251 if (!cu->v.quick->symtab)
2252 ++count;
2253 }
2254 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2255}
2256
2257static void
2258dw2_dump (struct objfile *objfile)
2259{
2260 /* Nothing worth printing. */
2261}
2262
2263static void
2264dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2265 struct section_offsets *delta)
2266{
2267 /* There's nothing to relocate here. */
2268}
2269
2270static void
2271dw2_expand_symtabs_for_function (struct objfile *objfile,
2272 const char *func_name)
2273{
2274 dw2_do_expand_symtabs_matching (objfile, func_name);
2275}
2276
2277static void
2278dw2_expand_all_symtabs (struct objfile *objfile)
2279{
2280 int i;
2281
2282 dw2_setup (objfile);
1fd400ff
TT
2283
2284 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2285 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2286 {
1fd400ff 2287 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2288
2289 dw2_instantiate_symtab (objfile, cu);
2290 }
2291}
2292
2293static void
2294dw2_expand_symtabs_with_filename (struct objfile *objfile,
2295 const char *filename)
2296{
2297 int i;
2298
2299 dw2_setup (objfile);
1fd400ff
TT
2300 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2301 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2302 {
2303 int j;
1fd400ff 2304 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2305
2306 if (cu->v.quick->symtab)
2307 continue;
2308
2309 dw2_require_line_header (objfile, cu);
2310 if (!cu->v.quick->lines)
2311 continue;
2312
2313 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2314 {
2315 const char *this_name = cu->v.quick->file_names[j];
2316 if (strcmp (this_name, filename) == 0)
2317 {
2318 dw2_instantiate_symtab (objfile, cu);
2319 break;
2320 }
2321 }
2322 }
2323}
2324
dd786858 2325static const char *
9291a0cd
TT
2326dw2_find_symbol_file (struct objfile *objfile, const char *name)
2327{
2328 struct dwarf2_per_cu_data *cu;
2329 offset_type *vec;
2330
2331 dw2_setup (objfile);
2332
2333 if (!dwarf2_per_objfile->index_table)
2334 return NULL;
2335
2336 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2337 name, &vec))
2338 return NULL;
2339
2340 /* Note that this just looks at the very first one named NAME -- but
2341 actually we are looking for a function. find_main_filename
2342 should be rewritten so that it doesn't require a custom hook. It
2343 could just use the ordinary symbol tables. */
2344 /* vec[0] is the length, which must always be >0. */
1fd400ff 2345 cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd
TT
2346
2347 dw2_require_line_header (objfile, cu);
2348 if (!cu->v.quick->lines)
2349 return NULL;
2350
dd786858 2351 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2352}
2353
2354static void
2355dw2_map_ada_symtabs (struct objfile *objfile,
2356 int (*wild_match) (const char *, int, const char *),
2357 int (*is_name_suffix) (const char *),
2358 void (*callback) (struct objfile *,
2359 struct symtab *, void *),
2360 const char *name, int global,
2361 domain_enum namespace, int wild,
2362 void *data)
2363{
2364 /* For now, we don't support Ada, so this function can't be
2365 reached. */
2366 internal_error (__FILE__, __LINE__,
2367 _("map_ada_symtabs called via index method"));
2368}
2369
2370static void
2371dw2_expand_symtabs_matching (struct objfile *objfile,
2372 int (*file_matcher) (const char *, void *),
2373 int (*name_matcher) (const char *, void *),
2374 domain_enum kind,
2375 void *data)
2376{
2377 int i;
2378 offset_type iter;
2379
2380 dw2_setup (objfile);
2381 if (!dwarf2_per_objfile->index_table)
2382 return;
2383
1fd400ff
TT
2384 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2385 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2386 {
2387 int j;
1fd400ff 2388 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2389
2390 cu->v.quick->mark = 0;
2391 if (cu->v.quick->symtab)
2392 continue;
2393
2394 dw2_require_line_header (objfile, cu);
2395 if (!cu->v.quick->lines)
2396 continue;
2397
2398 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2399 {
2400 if (file_matcher (cu->v.quick->file_names[j], data))
2401 {
2402 cu->v.quick->mark = 1;
2403 break;
2404 }
2405 }
2406 }
2407
2408 for (iter = 0;
2409 iter < dwarf2_per_objfile->index_table->index_table_slots;
2410 ++iter)
2411 {
2412 offset_type idx = 2 * iter;
2413 const char *name;
2414 offset_type *vec, vec_len, vec_idx;
2415
2416 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2417 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2418 continue;
2419
2420 name = (dwarf2_per_objfile->index_table->constant_pool
2421 + dwarf2_per_objfile->index_table->index_table[idx]);
2422
2423 if (! (*name_matcher) (name, data))
2424 continue;
2425
2426 /* The name was matched, now expand corresponding CUs that were
2427 marked. */
2428 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2429 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2430 vec_len = MAYBE_SWAP (vec[0]);
2431 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2432 {
1fd400ff
TT
2433 struct dwarf2_per_cu_data *cu;
2434
2435 cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
9291a0cd
TT
2436 if (cu->v.quick->mark)
2437 dw2_instantiate_symtab (objfile, cu);
2438 }
2439 }
2440}
2441
2442static struct symtab *
2443dw2_find_pc_sect_symtab (struct objfile *objfile,
2444 struct minimal_symbol *msymbol,
2445 CORE_ADDR pc,
2446 struct obj_section *section,
2447 int warn_if_readin)
2448{
2449 struct dwarf2_per_cu_data *data;
2450
2451 dw2_setup (objfile);
2452
2453 if (!objfile->psymtabs_addrmap)
2454 return NULL;
2455
2456 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2457 if (!data)
2458 return NULL;
2459
2460 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2461 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2462 paddress (get_objfile_arch (objfile), pc));
2463
2464 return dw2_instantiate_symtab (objfile, data);
2465}
2466
2467static void
2468dw2_map_symbol_names (struct objfile *objfile,
2469 void (*fun) (const char *, void *),
2470 void *data)
2471{
2472 offset_type iter;
2473 dw2_setup (objfile);
2474
2475 if (!dwarf2_per_objfile->index_table)
2476 return;
2477
2478 for (iter = 0;
2479 iter < dwarf2_per_objfile->index_table->index_table_slots;
2480 ++iter)
2481 {
2482 offset_type idx = 2 * iter;
2483 const char *name;
2484 offset_type *vec, vec_len, vec_idx;
2485
2486 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2487 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2488 continue;
2489
2490 name = (dwarf2_per_objfile->index_table->constant_pool
2491 + dwarf2_per_objfile->index_table->index_table[idx]);
2492
2493 (*fun) (name, data);
2494 }
2495}
2496
2497static void
2498dw2_map_symbol_filenames (struct objfile *objfile,
2499 void (*fun) (const char *, const char *, void *),
2500 void *data)
2501{
2502 int i;
2503
2504 dw2_setup (objfile);
1fd400ff
TT
2505 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2506 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2507 {
2508 int j;
1fd400ff 2509 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2510
2511 if (cu->v.quick->symtab)
2512 continue;
2513
2514 dw2_require_line_header (objfile, cu);
2515 if (!cu->v.quick->lines)
2516 continue;
2517
2518 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2519 {
2520 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2521 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2522 }
2523 }
2524}
2525
2526static int
2527dw2_has_symbols (struct objfile *objfile)
2528{
2529 return 1;
2530}
2531
2532const struct quick_symbol_functions dwarf2_gdb_index_functions =
2533{
2534 dw2_has_symbols,
2535 dw2_find_last_source_symtab,
2536 dw2_forget_cached_source_info,
2537 dw2_lookup_symtab,
2538 dw2_lookup_symbol,
2539 dw2_pre_expand_symtabs_matching,
2540 dw2_print_stats,
2541 dw2_dump,
2542 dw2_relocate,
2543 dw2_expand_symtabs_for_function,
2544 dw2_expand_all_symtabs,
2545 dw2_expand_symtabs_with_filename,
2546 dw2_find_symbol_file,
2547 dw2_map_ada_symtabs,
2548 dw2_expand_symtabs_matching,
2549 dw2_find_pc_sect_symtab,
2550 dw2_map_symbol_names,
2551 dw2_map_symbol_filenames
2552};
2553
2554/* Initialize for reading DWARF for this objfile. Return 0 if this
2555 file will use psymtabs, or 1 if using the GNU index. */
2556
2557int
2558dwarf2_initialize_objfile (struct objfile *objfile)
2559{
2560 /* If we're about to read full symbols, don't bother with the
2561 indices. In this case we also don't care if some other debug
2562 format is making psymtabs, because they are all about to be
2563 expanded anyway. */
2564 if ((objfile->flags & OBJF_READNOW))
2565 {
2566 int i;
2567
2568 dwarf2_per_objfile->using_index = 1;
2569 create_all_comp_units (objfile);
1fd400ff 2570 create_debug_types_hash_table (objfile);
9291a0cd 2571
1fd400ff
TT
2572 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2573 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2574 {
1fd400ff 2575 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2576
2577 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2578 struct dwarf2_per_cu_quick_data);
2579 }
2580
2581 /* Return 1 so that gdb sees the "quick" functions. However,
2582 these functions will be no-ops because we will have expanded
2583 all symtabs. */
2584 return 1;
2585 }
2586
2587 if (dwarf2_read_index (objfile))
2588 return 1;
2589
2590 dwarf2_build_psymtabs (objfile);
2591 return 0;
2592}
2593
2594\f
2595
dce234bc
PP
2596/* Build a partial symbol table. */
2597
2598void
f29dff0a 2599dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2600{
f29dff0a 2601 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2602 {
2603 init_psymbol_list (objfile, 1024);
2604 }
2605
d146bf1e 2606 dwarf2_build_psymtabs_hard (objfile);
c906108c 2607}
c906108c 2608
45452591
DE
2609/* Return TRUE if OFFSET is within CU_HEADER. */
2610
2611static inline int
2612offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2613{
2614 unsigned int bottom = cu_header->offset;
2615 unsigned int top = (cu_header->offset
2616 + cu_header->length
2617 + cu_header->initial_length_size);
9a619af0 2618
45452591
DE
2619 return (offset >= bottom && offset < top);
2620}
2621
93311388
DE
2622/* Read in the comp unit header information from the debug_info at info_ptr.
2623 NOTE: This leaves members offset, first_die_offset to be filled in
2624 by the caller. */
107d2387 2625
fe1b8b76 2626static gdb_byte *
107d2387 2627read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2628 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2629{
2630 int signed_addr;
891d2f0b 2631 unsigned int bytes_read;
c764a876
DE
2632
2633 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2634 cu_header->initial_length_size = bytes_read;
2635 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2636 info_ptr += bytes_read;
107d2387
AC
2637 cu_header->version = read_2_bytes (abfd, info_ptr);
2638 info_ptr += 2;
613e1657 2639 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2640 &bytes_read);
613e1657 2641 info_ptr += bytes_read;
107d2387
AC
2642 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2643 info_ptr += 1;
2644 signed_addr = bfd_get_sign_extend_vma (abfd);
2645 if (signed_addr < 0)
8e65ff28 2646 internal_error (__FILE__, __LINE__,
e2e0b3e5 2647 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2648 cu_header->signed_addr_p = signed_addr;
c764a876 2649
107d2387
AC
2650 return info_ptr;
2651}
2652
fe1b8b76
JB
2653static gdb_byte *
2654partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2655 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2656 bfd *abfd)
2657{
fe1b8b76 2658 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2659
2660 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2661
2dc7f7b3 2662 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2663 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2664 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2665 bfd_get_filename (abfd));
72bf9492 2666
dce234bc 2667 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2668 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2669 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2670 (long) header->abbrev_offset,
93311388 2671 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2672 bfd_get_filename (abfd));
2673
2674 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2675 > buffer + buffer_size)
8a3fe4f8
AC
2676 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2677 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2678 (long) header->length,
93311388 2679 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2680 bfd_get_filename (abfd));
2681
2682 return info_ptr;
2683}
2684
348e048f
DE
2685/* Read in the types comp unit header information from .debug_types entry at
2686 types_ptr. The result is a pointer to one past the end of the header. */
2687
2688static gdb_byte *
2689read_type_comp_unit_head (struct comp_unit_head *cu_header,
2690 ULONGEST *signature,
2691 gdb_byte *types_ptr, bfd *abfd)
2692{
348e048f
DE
2693 gdb_byte *initial_types_ptr = types_ptr;
2694
6e70227d 2695 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2696 &dwarf2_per_objfile->types);
348e048f
DE
2697 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2698
2699 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2700
2701 *signature = read_8_bytes (abfd, types_ptr);
2702 types_ptr += 8;
2703 types_ptr += cu_header->offset_size;
2704 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2705
2706 return types_ptr;
2707}
2708
aaa75496
JB
2709/* Allocate a new partial symtab for file named NAME and mark this new
2710 partial symtab as being an include of PST. */
2711
2712static void
2713dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2714 struct objfile *objfile)
2715{
2716 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2717
2718 subpst->section_offsets = pst->section_offsets;
2719 subpst->textlow = 0;
2720 subpst->texthigh = 0;
2721
2722 subpst->dependencies = (struct partial_symtab **)
2723 obstack_alloc (&objfile->objfile_obstack,
2724 sizeof (struct partial_symtab *));
2725 subpst->dependencies[0] = pst;
2726 subpst->number_of_dependencies = 1;
2727
2728 subpst->globals_offset = 0;
2729 subpst->n_global_syms = 0;
2730 subpst->statics_offset = 0;
2731 subpst->n_static_syms = 0;
2732 subpst->symtab = NULL;
2733 subpst->read_symtab = pst->read_symtab;
2734 subpst->readin = 0;
2735
2736 /* No private part is necessary for include psymtabs. This property
2737 can be used to differentiate between such include psymtabs and
10b3939b 2738 the regular ones. */
58a9656e 2739 subpst->read_symtab_private = NULL;
aaa75496
JB
2740}
2741
2742/* Read the Line Number Program data and extract the list of files
2743 included by the source file represented by PST. Build an include
d85a05f0 2744 partial symtab for each of these included files. */
aaa75496
JB
2745
2746static void
2747dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2748 struct die_info *die,
aaa75496
JB
2749 struct partial_symtab *pst)
2750{
2751 struct objfile *objfile = cu->objfile;
2752 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2753 struct line_header *lh = NULL;
2754 struct attribute *attr;
aaa75496 2755
d85a05f0
DJ
2756 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2757 if (attr)
2758 {
2759 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2760
d85a05f0
DJ
2761 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2762 }
aaa75496
JB
2763 if (lh == NULL)
2764 return; /* No linetable, so no includes. */
2765
2766 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2767
2768 free_line_header (lh);
2769}
2770
348e048f
DE
2771static hashval_t
2772hash_type_signature (const void *item)
2773{
2774 const struct signatured_type *type_sig = item;
9a619af0 2775
348e048f
DE
2776 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2777 return type_sig->signature;
2778}
2779
2780static int
2781eq_type_signature (const void *item_lhs, const void *item_rhs)
2782{
2783 const struct signatured_type *lhs = item_lhs;
2784 const struct signatured_type *rhs = item_rhs;
9a619af0 2785
348e048f
DE
2786 return lhs->signature == rhs->signature;
2787}
2788
1fd400ff
TT
2789/* Allocate a hash table for signatured types. */
2790
2791static htab_t
673bfd45 2792allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2793{
2794 return htab_create_alloc_ex (41,
2795 hash_type_signature,
2796 eq_type_signature,
2797 NULL,
2798 &objfile->objfile_obstack,
2799 hashtab_obstack_allocate,
2800 dummy_obstack_deallocate);
2801}
2802
2803/* A helper function to add a signatured type CU to a list. */
2804
2805static int
2806add_signatured_type_cu_to_list (void **slot, void *datum)
2807{
2808 struct signatured_type *sigt = *slot;
2809 struct dwarf2_per_cu_data ***datap = datum;
2810
2811 **datap = &sigt->per_cu;
2812 ++*datap;
2813
2814 return 1;
2815}
2816
348e048f
DE
2817/* Create the hash table of all entries in the .debug_types section.
2818 The result is zero if there is an error (e.g. missing .debug_types section),
2819 otherwise non-zero. */
2820
2821static int
2822create_debug_types_hash_table (struct objfile *objfile)
2823{
be391dca 2824 gdb_byte *info_ptr;
348e048f 2825 htab_t types_htab;
1fd400ff 2826 struct dwarf2_per_cu_data **iter;
348e048f 2827
be391dca
TT
2828 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2829 info_ptr = dwarf2_per_objfile->types.buffer;
2830
348e048f
DE
2831 if (info_ptr == NULL)
2832 {
2833 dwarf2_per_objfile->signatured_types = NULL;
2834 return 0;
2835 }
2836
673bfd45 2837 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2838
2839 if (dwarf2_die_debug)
2840 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2841
2842 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2843 {
2844 unsigned int offset;
2845 unsigned int offset_size;
2846 unsigned int type_offset;
2847 unsigned int length, initial_length_size;
2848 unsigned short version;
2849 ULONGEST signature;
2850 struct signatured_type *type_sig;
2851 void **slot;
2852 gdb_byte *ptr = info_ptr;
2853
2854 offset = ptr - dwarf2_per_objfile->types.buffer;
2855
2856 /* We need to read the type's signature in order to build the hash
2857 table, but we don't need to read anything else just yet. */
2858
2859 /* Sanity check to ensure entire cu is present. */
2860 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2861 if (ptr + length + initial_length_size
2862 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2863 {
2864 complaint (&symfile_complaints,
2865 _("debug type entry runs off end of `.debug_types' section, ignored"));
2866 break;
2867 }
2868
2869 offset_size = initial_length_size == 4 ? 4 : 8;
2870 ptr += initial_length_size;
2871 version = bfd_get_16 (objfile->obfd, ptr);
2872 ptr += 2;
2873 ptr += offset_size; /* abbrev offset */
2874 ptr += 1; /* address size */
2875 signature = bfd_get_64 (objfile->obfd, ptr);
2876 ptr += 8;
2877 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2878
2879 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2880 memset (type_sig, 0, sizeof (*type_sig));
2881 type_sig->signature = signature;
2882 type_sig->offset = offset;
2883 type_sig->type_offset = type_offset;
ca1f3406 2884 type_sig->per_cu.objfile = objfile;
1fd400ff 2885 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2886
2887 slot = htab_find_slot (types_htab, type_sig, INSERT);
2888 gdb_assert (slot != NULL);
2889 *slot = type_sig;
2890
2891 if (dwarf2_die_debug)
2892 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2893 offset, phex (signature, sizeof (signature)));
2894
2895 info_ptr = info_ptr + initial_length_size + length;
2896 }
2897
2898 dwarf2_per_objfile->signatured_types = types_htab;
2899
1fd400ff
TT
2900 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2901 dwarf2_per_objfile->type_comp_units
2902 = obstack_alloc (&objfile->objfile_obstack,
2903 dwarf2_per_objfile->n_type_comp_units
2904 * sizeof (struct dwarf2_per_cu_data *));
2905 iter = &dwarf2_per_objfile->type_comp_units[0];
2906 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2907 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2908 == dwarf2_per_objfile->n_type_comp_units);
2909
348e048f
DE
2910 return 1;
2911}
2912
2913/* Lookup a signature based type.
2914 Returns NULL if SIG is not present in the table. */
2915
2916static struct signatured_type *
2917lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2918{
2919 struct signatured_type find_entry, *entry;
2920
2921 if (dwarf2_per_objfile->signatured_types == NULL)
2922 {
2923 complaint (&symfile_complaints,
2924 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2925 return 0;
2926 }
2927
2928 find_entry.signature = sig;
2929 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2930 return entry;
2931}
2932
d85a05f0
DJ
2933/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2934
2935static void
2936init_cu_die_reader (struct die_reader_specs *reader,
2937 struct dwarf2_cu *cu)
2938{
2939 reader->abfd = cu->objfile->obfd;
2940 reader->cu = cu;
2941 if (cu->per_cu->from_debug_types)
be391dca
TT
2942 {
2943 gdb_assert (dwarf2_per_objfile->types.readin);
2944 reader->buffer = dwarf2_per_objfile->types.buffer;
2945 }
d85a05f0 2946 else
be391dca
TT
2947 {
2948 gdb_assert (dwarf2_per_objfile->info.readin);
2949 reader->buffer = dwarf2_per_objfile->info.buffer;
2950 }
d85a05f0
DJ
2951}
2952
2953/* Find the base address of the compilation unit for range lists and
2954 location lists. It will normally be specified by DW_AT_low_pc.
2955 In DWARF-3 draft 4, the base address could be overridden by
2956 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2957 compilation units with discontinuous ranges. */
2958
2959static void
2960dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2961{
2962 struct attribute *attr;
2963
2964 cu->base_known = 0;
2965 cu->base_address = 0;
2966
2967 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2968 if (attr)
2969 {
2970 cu->base_address = DW_ADDR (attr);
2971 cu->base_known = 1;
2972 }
2973 else
2974 {
2975 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2976 if (attr)
2977 {
2978 cu->base_address = DW_ADDR (attr);
2979 cu->base_known = 1;
2980 }
2981 }
2982}
2983
348e048f
DE
2984/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2985 to combine the common parts.
93311388 2986 Process a compilation unit for a psymtab.
348e048f
DE
2987 BUFFER is a pointer to the beginning of the dwarf section buffer,
2988 either .debug_info or debug_types.
93311388
DE
2989 INFO_PTR is a pointer to the start of the CU.
2990 Returns a pointer to the next CU. */
aaa75496 2991
93311388
DE
2992static gdb_byte *
2993process_psymtab_comp_unit (struct objfile *objfile,
2994 struct dwarf2_per_cu_data *this_cu,
2995 gdb_byte *buffer, gdb_byte *info_ptr,
2996 unsigned int buffer_size)
c906108c 2997{
c906108c 2998 bfd *abfd = objfile->obfd;
93311388 2999 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3000 struct die_info *comp_unit_die;
c906108c 3001 struct partial_symtab *pst;
5734ee8b 3002 CORE_ADDR baseaddr;
93311388
DE
3003 struct cleanup *back_to_inner;
3004 struct dwarf2_cu cu;
d85a05f0
DJ
3005 int has_children, has_pc_info;
3006 struct attribute *attr;
d85a05f0
DJ
3007 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3008 struct die_reader_specs reader_specs;
c906108c 3009
93311388
DE
3010 memset (&cu, 0, sizeof (cu));
3011 cu.objfile = objfile;
3012 obstack_init (&cu.comp_unit_obstack);
c906108c 3013
93311388 3014 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3015
93311388
DE
3016 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3017 buffer, buffer_size,
3018 abfd);
10b3939b 3019
93311388
DE
3020 /* Complete the cu_header. */
3021 cu.header.offset = beg_of_comp_unit - buffer;
3022 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3023
93311388 3024 cu.list_in_scope = &file_symbols;
af703f96 3025
328c9494
DJ
3026 /* If this compilation unit was already read in, free the
3027 cached copy in order to read it in again. This is
3028 necessary because we skipped some symbols when we first
3029 read in the compilation unit (see load_partial_dies).
3030 This problem could be avoided, but the benefit is
3031 unclear. */
3032 if (this_cu->cu != NULL)
3033 free_one_cached_comp_unit (this_cu->cu);
3034
3035 /* Note that this is a pointer to our stack frame, being
3036 added to a global data structure. It will be cleaned up
3037 in free_stack_comp_unit when we finish with this
3038 compilation unit. */
3039 this_cu->cu = &cu;
d85a05f0
DJ
3040 cu.per_cu = this_cu;
3041
93311388
DE
3042 /* Read the abbrevs for this compilation unit into a table. */
3043 dwarf2_read_abbrevs (abfd, &cu);
3044 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3045
93311388 3046 /* Read the compilation unit die. */
348e048f
DE
3047 if (this_cu->from_debug_types)
3048 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3049 init_cu_die_reader (&reader_specs, &cu);
3050 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3051 &has_children);
93311388 3052
348e048f
DE
3053 if (this_cu->from_debug_types)
3054 {
3055 /* offset,length haven't been set yet for type units. */
3056 this_cu->offset = cu.header.offset;
3057 this_cu->length = cu.header.length + cu.header.initial_length_size;
3058 }
d85a05f0 3059 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3060 {
93311388
DE
3061 info_ptr = (beg_of_comp_unit + cu.header.length
3062 + cu.header.initial_length_size);
3063 do_cleanups (back_to_inner);
3064 return info_ptr;
3065 }
72bf9492 3066
93311388 3067 /* Set the language we're debugging. */
d85a05f0
DJ
3068 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3069 if (attr)
3070 set_cu_language (DW_UNSND (attr), &cu);
3071 else
3072 set_cu_language (language_minimal, &cu);
c906108c 3073
93311388 3074 /* Allocate a new partial symbol table structure. */
d85a05f0 3075 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3076 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3077 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3078 /* TEXTLOW and TEXTHIGH are set below. */
3079 0,
3080 objfile->global_psymbols.next,
3081 objfile->static_psymbols.next);
72bf9492 3082
d85a05f0
DJ
3083 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3084 if (attr != NULL)
3085 pst->dirname = DW_STRING (attr);
72bf9492 3086
e38df1d0 3087 pst->read_symtab_private = this_cu;
72bf9492 3088
93311388 3089 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3090
93311388
DE
3091 /* Store the function that reads in the rest of the symbol table */
3092 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3093
9291a0cd 3094 this_cu->v.psymtab = pst;
c906108c 3095
d85a05f0
DJ
3096 dwarf2_find_base_address (comp_unit_die, &cu);
3097
93311388
DE
3098 /* Possibly set the default values of LOWPC and HIGHPC from
3099 `DW_AT_ranges'. */
d85a05f0
DJ
3100 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3101 &best_highpc, &cu, pst);
3102 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3103 /* Store the contiguous range if it is not empty; it can be empty for
3104 CUs with no code. */
3105 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3106 best_lowpc + baseaddr,
3107 best_highpc + baseaddr - 1, pst);
93311388
DE
3108
3109 /* Check if comp unit has_children.
3110 If so, read the rest of the partial symbols from this comp unit.
3111 If not, there's no more debug_info for this comp unit. */
d85a05f0 3112 if (has_children)
93311388
DE
3113 {
3114 struct partial_die_info *first_die;
3115 CORE_ADDR lowpc, highpc;
31ffec48 3116
93311388
DE
3117 lowpc = ((CORE_ADDR) -1);
3118 highpc = ((CORE_ADDR) 0);
c906108c 3119
93311388 3120 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3121
93311388 3122 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3123 ! has_pc_info, &cu);
57c22c6c 3124
93311388
DE
3125 /* If we didn't find a lowpc, set it to highpc to avoid
3126 complaints from `maint check'. */
3127 if (lowpc == ((CORE_ADDR) -1))
3128 lowpc = highpc;
10b3939b 3129
93311388
DE
3130 /* If the compilation unit didn't have an explicit address range,
3131 then use the information extracted from its child dies. */
d85a05f0 3132 if (! has_pc_info)
93311388 3133 {
d85a05f0
DJ
3134 best_lowpc = lowpc;
3135 best_highpc = highpc;
93311388
DE
3136 }
3137 }
d85a05f0
DJ
3138 pst->textlow = best_lowpc + baseaddr;
3139 pst->texthigh = best_highpc + baseaddr;
c906108c 3140
93311388
DE
3141 pst->n_global_syms = objfile->global_psymbols.next -
3142 (objfile->global_psymbols.list + pst->globals_offset);
3143 pst->n_static_syms = objfile->static_psymbols.next -
3144 (objfile->static_psymbols.list + pst->statics_offset);
3145 sort_pst_symbols (pst);
c906108c 3146
93311388
DE
3147 info_ptr = (beg_of_comp_unit + cu.header.length
3148 + cu.header.initial_length_size);
ae038cb0 3149
348e048f
DE
3150 if (this_cu->from_debug_types)
3151 {
3152 /* It's not clear we want to do anything with stmt lists here.
3153 Waiting to see what gcc ultimately does. */
3154 }
d85a05f0 3155 else
93311388
DE
3156 {
3157 /* Get the list of files included in the current compilation unit,
3158 and build a psymtab for each of them. */
d85a05f0 3159 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3160 }
ae038cb0 3161
93311388 3162 do_cleanups (back_to_inner);
ae038cb0 3163
93311388
DE
3164 return info_ptr;
3165}
ff013f42 3166
348e048f
DE
3167/* Traversal function for htab_traverse_noresize.
3168 Process one .debug_types comp-unit. */
3169
3170static int
3171process_type_comp_unit (void **slot, void *info)
3172{
3173 struct signatured_type *entry = (struct signatured_type *) *slot;
3174 struct objfile *objfile = (struct objfile *) info;
3175 struct dwarf2_per_cu_data *this_cu;
3176
3177 this_cu = &entry->per_cu;
348e048f 3178
be391dca 3179 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3180 process_psymtab_comp_unit (objfile, this_cu,
3181 dwarf2_per_objfile->types.buffer,
3182 dwarf2_per_objfile->types.buffer + entry->offset,
3183 dwarf2_per_objfile->types.size);
3184
3185 return 1;
3186}
3187
3188/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3189 Build partial symbol tables for the .debug_types comp-units. */
3190
3191static void
3192build_type_psymtabs (struct objfile *objfile)
3193{
3194 if (! create_debug_types_hash_table (objfile))
3195 return;
3196
3197 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3198 process_type_comp_unit, objfile);
3199}
3200
60606b2c
TT
3201/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3202
3203static void
3204psymtabs_addrmap_cleanup (void *o)
3205{
3206 struct objfile *objfile = o;
ec61707d 3207
60606b2c
TT
3208 objfile->psymtabs_addrmap = NULL;
3209}
3210
93311388
DE
3211/* Build the partial symbol table by doing a quick pass through the
3212 .debug_info and .debug_abbrev sections. */
72bf9492 3213
93311388 3214static void
c67a9c90 3215dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3216{
93311388 3217 gdb_byte *info_ptr;
60606b2c
TT
3218 struct cleanup *back_to, *addrmap_cleanup;
3219 struct obstack temp_obstack;
93311388 3220
98bfdba5
PA
3221 dwarf2_per_objfile->reading_partial_symbols = 1;
3222
be391dca 3223 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3224 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3225
93311388
DE
3226 /* Any cached compilation units will be linked by the per-objfile
3227 read_in_chain. Make sure to free them when we're done. */
3228 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3229
348e048f
DE
3230 build_type_psymtabs (objfile);
3231
93311388 3232 create_all_comp_units (objfile);
c906108c 3233
60606b2c
TT
3234 /* Create a temporary address map on a temporary obstack. We later
3235 copy this to the final obstack. */
3236 obstack_init (&temp_obstack);
3237 make_cleanup_obstack_free (&temp_obstack);
3238 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3239 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3240
93311388
DE
3241 /* Since the objects we're extracting from .debug_info vary in
3242 length, only the individual functions to extract them (like
3243 read_comp_unit_head and load_partial_die) can really know whether
3244 the buffer is large enough to hold another complete object.
c906108c 3245
93311388
DE
3246 At the moment, they don't actually check that. If .debug_info
3247 holds just one extra byte after the last compilation unit's dies,
3248 then read_comp_unit_head will happily read off the end of the
3249 buffer. read_partial_die is similarly casual. Those functions
3250 should be fixed.
c906108c 3251
93311388
DE
3252 For this loop condition, simply checking whether there's any data
3253 left at all should be sufficient. */
c906108c 3254
93311388
DE
3255 while (info_ptr < (dwarf2_per_objfile->info.buffer
3256 + dwarf2_per_objfile->info.size))
3257 {
3258 struct dwarf2_per_cu_data *this_cu;
dd373385 3259
93311388
DE
3260 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3261 objfile);
aaa75496 3262
93311388
DE
3263 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3264 dwarf2_per_objfile->info.buffer,
3265 info_ptr,
3266 dwarf2_per_objfile->info.size);
c906108c 3267 }
ff013f42
JK
3268
3269 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3270 &objfile->objfile_obstack);
60606b2c 3271 discard_cleanups (addrmap_cleanup);
ff013f42 3272
ae038cb0
DJ
3273 do_cleanups (back_to);
3274}
3275
93311388 3276/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3277
3278static void
93311388
DE
3279load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3280 struct objfile *objfile)
ae038cb0
DJ
3281{
3282 bfd *abfd = objfile->obfd;
fe1b8b76 3283 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3284 struct die_info *comp_unit_die;
ae038cb0 3285 struct dwarf2_cu *cu;
1d9ec526 3286 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3287 struct attribute *attr;
3288 int has_children;
3289 struct die_reader_specs reader_specs;
98bfdba5 3290 int read_cu = 0;
ae038cb0 3291
348e048f
DE
3292 gdb_assert (! this_cu->from_debug_types);
3293
be391dca 3294 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3295 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3296 beg_of_comp_unit = info_ptr;
3297
98bfdba5
PA
3298 if (this_cu->cu == NULL)
3299 {
3300 cu = alloc_one_comp_unit (objfile);
ae038cb0 3301
98bfdba5 3302 read_cu = 1;
ae038cb0 3303
98bfdba5
PA
3304 /* If an error occurs while loading, release our storage. */
3305 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3306
98bfdba5
PA
3307 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3308 dwarf2_per_objfile->info.buffer,
3309 dwarf2_per_objfile->info.size,
3310 abfd);
ae038cb0 3311
98bfdba5
PA
3312 /* Complete the cu_header. */
3313 cu->header.offset = this_cu->offset;
3314 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3315
3316 /* Link this compilation unit into the compilation unit tree. */
3317 this_cu->cu = cu;
3318 cu->per_cu = this_cu;
98bfdba5
PA
3319
3320 /* Link this CU into read_in_chain. */
3321 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3322 dwarf2_per_objfile->read_in_chain = this_cu;
3323 }
3324 else
3325 {
3326 cu = this_cu->cu;
3327 info_ptr += cu->header.first_die_offset;
3328 }
ae038cb0
DJ
3329
3330 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3331 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3332 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3333 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3334
3335 /* Read the compilation unit die. */
d85a05f0
DJ
3336 init_cu_die_reader (&reader_specs, cu);
3337 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3338 &has_children);
ae038cb0
DJ
3339
3340 /* Set the language we're debugging. */
d85a05f0
DJ
3341 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3342 if (attr)
3343 set_cu_language (DW_UNSND (attr), cu);
3344 else
3345 set_cu_language (language_minimal, cu);
ae038cb0 3346
ae038cb0
DJ
3347 /* Check if comp unit has_children.
3348 If so, read the rest of the partial symbols from this comp unit.
3349 If not, there's no more debug_info for this comp unit. */
d85a05f0 3350 if (has_children)
93311388 3351 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3352
98bfdba5
PA
3353 do_cleanups (free_abbrevs_cleanup);
3354
3355 if (read_cu)
3356 {
3357 /* We've successfully allocated this compilation unit. Let our
3358 caller clean it up when finished with it. */
3359 discard_cleanups (free_cu_cleanup);
3360 }
ae038cb0
DJ
3361}
3362
3363/* Create a list of all compilation units in OBJFILE. We do this only
3364 if an inter-comp-unit reference is found; presumably if there is one,
3365 there will be many, and one will occur early in the .debug_info section.
3366 So there's no point in building this list incrementally. */
3367
3368static void
3369create_all_comp_units (struct objfile *objfile)
3370{
3371 int n_allocated;
3372 int n_comp_units;
3373 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3374 gdb_byte *info_ptr;
3375
3376 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3377 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3378
3379 n_comp_units = 0;
3380 n_allocated = 10;
3381 all_comp_units = xmalloc (n_allocated
3382 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3383
dce234bc 3384 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3385 {
c764a876 3386 unsigned int length, initial_length_size;
ae038cb0 3387 struct dwarf2_per_cu_data *this_cu;
c764a876 3388 unsigned int offset;
ae038cb0 3389
dce234bc 3390 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3391
3392 /* Read just enough information to find out where the next
3393 compilation unit is. */
c764a876
DE
3394 length = read_initial_length (objfile->obfd, info_ptr,
3395 &initial_length_size);
ae038cb0
DJ
3396
3397 /* Save the compilation unit for later lookup. */
3398 this_cu = obstack_alloc (&objfile->objfile_obstack,
3399 sizeof (struct dwarf2_per_cu_data));
3400 memset (this_cu, 0, sizeof (*this_cu));
3401 this_cu->offset = offset;
c764a876 3402 this_cu->length = length + initial_length_size;
9291a0cd 3403 this_cu->objfile = objfile;
ae038cb0
DJ
3404
3405 if (n_comp_units == n_allocated)
3406 {
3407 n_allocated *= 2;
3408 all_comp_units = xrealloc (all_comp_units,
3409 n_allocated
3410 * sizeof (struct dwarf2_per_cu_data *));
3411 }
3412 all_comp_units[n_comp_units++] = this_cu;
3413
3414 info_ptr = info_ptr + this_cu->length;
3415 }
3416
3417 dwarf2_per_objfile->all_comp_units
3418 = obstack_alloc (&objfile->objfile_obstack,
3419 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3420 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3421 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3422 xfree (all_comp_units);
3423 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3424}
3425
5734ee8b
DJ
3426/* Process all loaded DIEs for compilation unit CU, starting at
3427 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3428 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3429 DW_AT_ranges). If NEED_PC is set, then this function will set
3430 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3431 and record the covered ranges in the addrmap. */
c906108c 3432
72bf9492
DJ
3433static void
3434scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3435 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3436{
72bf9492 3437 struct partial_die_info *pdi;
c906108c 3438
91c24f0a
DC
3439 /* Now, march along the PDI's, descending into ones which have
3440 interesting children but skipping the children of the other ones,
3441 until we reach the end of the compilation unit. */
c906108c 3442
72bf9492 3443 pdi = first_die;
91c24f0a 3444
72bf9492
DJ
3445 while (pdi != NULL)
3446 {
3447 fixup_partial_die (pdi, cu);
c906108c 3448
f55ee35c 3449 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3450 children, so we need to look at them. Ditto for anonymous
3451 enums. */
933c6fe4 3452
72bf9492 3453 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3454 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3455 {
72bf9492 3456 switch (pdi->tag)
c906108c
SS
3457 {
3458 case DW_TAG_subprogram:
5734ee8b 3459 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3460 break;
3461 case DW_TAG_variable:
3462 case DW_TAG_typedef:
91c24f0a 3463 case DW_TAG_union_type:
72bf9492 3464 if (!pdi->is_declaration)
63d06c5c 3465 {
72bf9492 3466 add_partial_symbol (pdi, cu);
63d06c5c
DC
3467 }
3468 break;
c906108c 3469 case DW_TAG_class_type:
680b30c7 3470 case DW_TAG_interface_type:
c906108c 3471 case DW_TAG_structure_type:
72bf9492 3472 if (!pdi->is_declaration)
c906108c 3473 {
72bf9492 3474 add_partial_symbol (pdi, cu);
c906108c
SS
3475 }
3476 break;
91c24f0a 3477 case DW_TAG_enumeration_type:
72bf9492
DJ
3478 if (!pdi->is_declaration)
3479 add_partial_enumeration (pdi, cu);
c906108c
SS
3480 break;
3481 case DW_TAG_base_type:
a02abb62 3482 case DW_TAG_subrange_type:
c906108c 3483 /* File scope base type definitions are added to the partial
c5aa993b 3484 symbol table. */
72bf9492 3485 add_partial_symbol (pdi, cu);
c906108c 3486 break;
d9fa45fe 3487 case DW_TAG_namespace:
5734ee8b 3488 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3489 break;
5d7cb8df
JK
3490 case DW_TAG_module:
3491 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3492 break;
c906108c
SS
3493 default:
3494 break;
3495 }
3496 }
3497
72bf9492
DJ
3498 /* If the die has a sibling, skip to the sibling. */
3499
3500 pdi = pdi->die_sibling;
3501 }
3502}
3503
3504/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3505
72bf9492 3506 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3507 name is concatenated with "::" and the partial DIE's name. For
3508 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3509 Enumerators are an exception; they use the scope of their parent
3510 enumeration type, i.e. the name of the enumeration type is not
3511 prepended to the enumerator.
91c24f0a 3512
72bf9492
DJ
3513 There are two complexities. One is DW_AT_specification; in this
3514 case "parent" means the parent of the target of the specification,
3515 instead of the direct parent of the DIE. The other is compilers
3516 which do not emit DW_TAG_namespace; in this case we try to guess
3517 the fully qualified name of structure types from their members'
3518 linkage names. This must be done using the DIE's children rather
3519 than the children of any DW_AT_specification target. We only need
3520 to do this for structures at the top level, i.e. if the target of
3521 any DW_AT_specification (if any; otherwise the DIE itself) does not
3522 have a parent. */
3523
3524/* Compute the scope prefix associated with PDI's parent, in
3525 compilation unit CU. The result will be allocated on CU's
3526 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3527 field. NULL is returned if no prefix is necessary. */
3528static char *
3529partial_die_parent_scope (struct partial_die_info *pdi,
3530 struct dwarf2_cu *cu)
3531{
3532 char *grandparent_scope;
3533 struct partial_die_info *parent, *real_pdi;
91c24f0a 3534
72bf9492
DJ
3535 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3536 then this means the parent of the specification DIE. */
3537
3538 real_pdi = pdi;
72bf9492 3539 while (real_pdi->has_specification)
10b3939b 3540 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3541
3542 parent = real_pdi->die_parent;
3543 if (parent == NULL)
3544 return NULL;
3545
3546 if (parent->scope_set)
3547 return parent->scope;
3548
3549 fixup_partial_die (parent, cu);
3550
10b3939b 3551 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3552
acebe513
UW
3553 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3554 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3555 Work around this problem here. */
3556 if (cu->language == language_cplus
6e70227d 3557 && parent->tag == DW_TAG_namespace
acebe513
UW
3558 && strcmp (parent->name, "::") == 0
3559 && grandparent_scope == NULL)
3560 {
3561 parent->scope = NULL;
3562 parent->scope_set = 1;
3563 return NULL;
3564 }
3565
72bf9492 3566 if (parent->tag == DW_TAG_namespace
f55ee35c 3567 || parent->tag == DW_TAG_module
72bf9492
DJ
3568 || parent->tag == DW_TAG_structure_type
3569 || parent->tag == DW_TAG_class_type
680b30c7 3570 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3571 || parent->tag == DW_TAG_union_type
3572 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3573 {
3574 if (grandparent_scope == NULL)
3575 parent->scope = parent->name;
3576 else
987504bb 3577 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3578 parent->name, 0, cu);
72bf9492 3579 }
ceeb3d5a 3580 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3581 /* Enumerators should not get the name of the enumeration as a prefix. */
3582 parent->scope = grandparent_scope;
3583 else
3584 {
3585 /* FIXME drow/2004-04-01: What should we be doing with
3586 function-local names? For partial symbols, we should probably be
3587 ignoring them. */
3588 complaint (&symfile_complaints,
e2e0b3e5 3589 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3590 parent->tag, pdi->offset);
3591 parent->scope = grandparent_scope;
c906108c
SS
3592 }
3593
72bf9492
DJ
3594 parent->scope_set = 1;
3595 return parent->scope;
3596}
3597
3598/* Return the fully scoped name associated with PDI, from compilation unit
3599 CU. The result will be allocated with malloc. */
3600static char *
3601partial_die_full_name (struct partial_die_info *pdi,
3602 struct dwarf2_cu *cu)
3603{
3604 char *parent_scope;
3605
98bfdba5
PA
3606 /* If this is a template instantiation, we can not work out the
3607 template arguments from partial DIEs. So, unfortunately, we have
3608 to go through the full DIEs. At least any work we do building
3609 types here will be reused if full symbols are loaded later. */
3610 if (pdi->has_template_arguments)
3611 {
3612 fixup_partial_die (pdi, cu);
3613
3614 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3615 {
3616 struct die_info *die;
3617 struct attribute attr;
3618 struct dwarf2_cu *ref_cu = cu;
3619
3620 attr.name = 0;
3621 attr.form = DW_FORM_ref_addr;
3622 attr.u.addr = pdi->offset;
3623 die = follow_die_ref (NULL, &attr, &ref_cu);
3624
3625 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3626 }
3627 }
3628
72bf9492
DJ
3629 parent_scope = partial_die_parent_scope (pdi, cu);
3630 if (parent_scope == NULL)
3631 return NULL;
3632 else
f55ee35c 3633 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3634}
3635
3636static void
72bf9492 3637add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3638{
e7c27a73 3639 struct objfile *objfile = cu->objfile;
c906108c 3640 CORE_ADDR addr = 0;
decbce07 3641 char *actual_name = NULL;
5c4e30ca 3642 const struct partial_symbol *psym = NULL;
e142c38c 3643 CORE_ADDR baseaddr;
72bf9492 3644 int built_actual_name = 0;
e142c38c
DJ
3645
3646 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3647
94af9270
KS
3648 actual_name = partial_die_full_name (pdi, cu);
3649 if (actual_name)
3650 built_actual_name = 1;
63d06c5c 3651
72bf9492
DJ
3652 if (actual_name == NULL)
3653 actual_name = pdi->name;
3654
c906108c
SS
3655 switch (pdi->tag)
3656 {
3657 case DW_TAG_subprogram:
2cfa0c8d 3658 if (pdi->is_external || cu->language == language_ada)
c906108c 3659 {
2cfa0c8d
JB
3660 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3661 of the global scope. But in Ada, we want to be able to access
3662 nested procedures globally. So all Ada subprograms are stored
3663 in the global scope. */
38d518c9 3664 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3665 mst_text, objfile); */
38d518c9 3666 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3667 built_actual_name,
5c4e30ca
DC
3668 VAR_DOMAIN, LOC_BLOCK,
3669 &objfile->global_psymbols,
3670 0, pdi->lowpc + baseaddr,
e142c38c 3671 cu->language, objfile);
c906108c
SS
3672 }
3673 else
3674 {
38d518c9 3675 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3676 mst_file_text, objfile); */
38d518c9 3677 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3678 built_actual_name,
5c4e30ca
DC
3679 VAR_DOMAIN, LOC_BLOCK,
3680 &objfile->static_psymbols,
3681 0, pdi->lowpc + baseaddr,
e142c38c 3682 cu->language, objfile);
c906108c
SS
3683 }
3684 break;
3685 case DW_TAG_variable:
caac4577
JG
3686 if (pdi->locdesc)
3687 addr = decode_locdesc (pdi->locdesc, cu);
3688
3689 if (pdi->locdesc
3690 && addr == 0
3691 && !dwarf2_per_objfile->has_section_at_zero)
3692 {
3693 /* A global or static variable may also have been stripped
3694 out by the linker if unused, in which case its address
3695 will be nullified; do not add such variables into partial
3696 symbol table then. */
3697 }
3698 else if (pdi->is_external)
c906108c
SS
3699 {
3700 /* Global Variable.
3701 Don't enter into the minimal symbol tables as there is
3702 a minimal symbol table entry from the ELF symbols already.
3703 Enter into partial symbol table if it has a location
3704 descriptor or a type.
3705 If the location descriptor is missing, new_symbol will create
3706 a LOC_UNRESOLVED symbol, the address of the variable will then
3707 be determined from the minimal symbol table whenever the variable
3708 is referenced.
3709 The address for the partial symbol table entry is not
3710 used by GDB, but it comes in handy for debugging partial symbol
3711 table building. */
3712
c906108c 3713 if (pdi->locdesc || pdi->has_type)
38d518c9 3714 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3715 built_actual_name,
5c4e30ca
DC
3716 VAR_DOMAIN, LOC_STATIC,
3717 &objfile->global_psymbols,
3718 0, addr + baseaddr,
e142c38c 3719 cu->language, objfile);
c906108c
SS
3720 }
3721 else
3722 {
3723 /* Static Variable. Skip symbols without location descriptors. */
3724 if (pdi->locdesc == NULL)
decbce07
MS
3725 {
3726 if (built_actual_name)
3727 xfree (actual_name);
3728 return;
3729 }
38d518c9 3730 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3731 mst_file_data, objfile); */
38d518c9 3732 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3733 built_actual_name,
5c4e30ca
DC
3734 VAR_DOMAIN, LOC_STATIC,
3735 &objfile->static_psymbols,
3736 0, addr + baseaddr,
e142c38c 3737 cu->language, objfile);
c906108c
SS
3738 }
3739 break;
3740 case DW_TAG_typedef:
3741 case DW_TAG_base_type:
a02abb62 3742 case DW_TAG_subrange_type:
38d518c9 3743 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3744 built_actual_name,
176620f1 3745 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3746 &objfile->static_psymbols,
e142c38c 3747 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3748 break;
72bf9492
DJ
3749 case DW_TAG_namespace:
3750 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3751 built_actual_name,
72bf9492
DJ
3752 VAR_DOMAIN, LOC_TYPEDEF,
3753 &objfile->global_psymbols,
3754 0, (CORE_ADDR) 0, cu->language, objfile);
3755 break;
c906108c 3756 case DW_TAG_class_type:
680b30c7 3757 case DW_TAG_interface_type:
c906108c
SS
3758 case DW_TAG_structure_type:
3759 case DW_TAG_union_type:
3760 case DW_TAG_enumeration_type:
fa4028e9
JB
3761 /* Skip external references. The DWARF standard says in the section
3762 about "Structure, Union, and Class Type Entries": "An incomplete
3763 structure, union or class type is represented by a structure,
3764 union or class entry that does not have a byte size attribute
3765 and that has a DW_AT_declaration attribute." */
3766 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3767 {
3768 if (built_actual_name)
3769 xfree (actual_name);
3770 return;
3771 }
fa4028e9 3772
63d06c5c
DC
3773 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3774 static vs. global. */
38d518c9 3775 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3776 built_actual_name,
176620f1 3777 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3778 (cu->language == language_cplus
3779 || cu->language == language_java)
63d06c5c
DC
3780 ? &objfile->global_psymbols
3781 : &objfile->static_psymbols,
e142c38c 3782 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3783
c906108c
SS
3784 break;
3785 case DW_TAG_enumerator:
38d518c9 3786 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3787 built_actual_name,
176620f1 3788 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3789 (cu->language == language_cplus
3790 || cu->language == language_java)
f6fe98ef
DJ
3791 ? &objfile->global_psymbols
3792 : &objfile->static_psymbols,
e142c38c 3793 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3794 break;
3795 default:
3796 break;
3797 }
5c4e30ca 3798
72bf9492
DJ
3799 if (built_actual_name)
3800 xfree (actual_name);
c906108c
SS
3801}
3802
5c4e30ca
DC
3803/* Read a partial die corresponding to a namespace; also, add a symbol
3804 corresponding to that namespace to the symbol table. NAMESPACE is
3805 the name of the enclosing namespace. */
91c24f0a 3806
72bf9492
DJ
3807static void
3808add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3809 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3810 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3811{
72bf9492 3812 /* Add a symbol for the namespace. */
e7c27a73 3813
72bf9492 3814 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3815
3816 /* Now scan partial symbols in that namespace. */
3817
91c24f0a 3818 if (pdi->has_children)
5734ee8b 3819 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3820}
3821
5d7cb8df
JK
3822/* Read a partial die corresponding to a Fortran module. */
3823
3824static void
3825add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3826 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3827{
f55ee35c 3828 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3829
3830 if (pdi->has_children)
3831 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3832}
3833
bc30ff58
JB
3834/* Read a partial die corresponding to a subprogram and create a partial
3835 symbol for that subprogram. When the CU language allows it, this
3836 routine also defines a partial symbol for each nested subprogram
3837 that this subprogram contains.
6e70227d 3838
bc30ff58
JB
3839 DIE my also be a lexical block, in which case we simply search
3840 recursively for suprograms defined inside that lexical block.
3841 Again, this is only performed when the CU language allows this
3842 type of definitions. */
3843
3844static void
3845add_partial_subprogram (struct partial_die_info *pdi,
3846 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3847 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3848{
3849 if (pdi->tag == DW_TAG_subprogram)
3850 {
3851 if (pdi->has_pc_info)
3852 {
3853 if (pdi->lowpc < *lowpc)
3854 *lowpc = pdi->lowpc;
3855 if (pdi->highpc > *highpc)
3856 *highpc = pdi->highpc;
5734ee8b
DJ
3857 if (need_pc)
3858 {
3859 CORE_ADDR baseaddr;
3860 struct objfile *objfile = cu->objfile;
3861
3862 baseaddr = ANOFFSET (objfile->section_offsets,
3863 SECT_OFF_TEXT (objfile));
3864 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3865 pdi->lowpc + baseaddr,
3866 pdi->highpc - 1 + baseaddr,
9291a0cd 3867 cu->per_cu->v.psymtab);
5734ee8b 3868 }
bc30ff58 3869 if (!pdi->is_declaration)
e8d05480
JB
3870 /* Ignore subprogram DIEs that do not have a name, they are
3871 illegal. Do not emit a complaint at this point, we will
3872 do so when we convert this psymtab into a symtab. */
3873 if (pdi->name)
3874 add_partial_symbol (pdi, cu);
bc30ff58
JB
3875 }
3876 }
6e70227d 3877
bc30ff58
JB
3878 if (! pdi->has_children)
3879 return;
3880
3881 if (cu->language == language_ada)
3882 {
3883 pdi = pdi->die_child;
3884 while (pdi != NULL)
3885 {
3886 fixup_partial_die (pdi, cu);
3887 if (pdi->tag == DW_TAG_subprogram
3888 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3889 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3890 pdi = pdi->die_sibling;
3891 }
3892 }
3893}
3894
72bf9492
DJ
3895/* See if we can figure out if the class lives in a namespace. We do
3896 this by looking for a member function; its demangled name will
3897 contain namespace info, if there is any. */
63d06c5c 3898
72bf9492
DJ
3899static void
3900guess_structure_name (struct partial_die_info *struct_pdi,
3901 struct dwarf2_cu *cu)
63d06c5c 3902{
987504bb
JJ
3903 if ((cu->language == language_cplus
3904 || cu->language == language_java)
72bf9492 3905 && cu->has_namespace_info == 0
63d06c5c
DC
3906 && struct_pdi->has_children)
3907 {
63d06c5c
DC
3908 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3909 what template types look like, because the demangler
3910 frequently doesn't give the same name as the debug info. We
3911 could fix this by only using the demangled name to get the
134d01f1 3912 prefix (but see comment in read_structure_type). */
63d06c5c 3913
72bf9492 3914 struct partial_die_info *real_pdi;
5d51ca54 3915
72bf9492
DJ
3916 /* If this DIE (this DIE's specification, if any) has a parent, then
3917 we should not do this. We'll prepend the parent's fully qualified
3918 name when we create the partial symbol. */
5d51ca54 3919
72bf9492 3920 real_pdi = struct_pdi;
72bf9492 3921 while (real_pdi->has_specification)
10b3939b 3922 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3923
72bf9492
DJ
3924 if (real_pdi->die_parent != NULL)
3925 return;
63d06c5c 3926 }
63d06c5c
DC
3927}
3928
91c24f0a
DC
3929/* Read a partial die corresponding to an enumeration type. */
3930
72bf9492
DJ
3931static void
3932add_partial_enumeration (struct partial_die_info *enum_pdi,
3933 struct dwarf2_cu *cu)
91c24f0a 3934{
72bf9492 3935 struct partial_die_info *pdi;
91c24f0a
DC
3936
3937 if (enum_pdi->name != NULL)
72bf9492
DJ
3938 add_partial_symbol (enum_pdi, cu);
3939
3940 pdi = enum_pdi->die_child;
3941 while (pdi)
91c24f0a 3942 {
72bf9492 3943 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3944 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3945 else
72bf9492
DJ
3946 add_partial_symbol (pdi, cu);
3947 pdi = pdi->die_sibling;
91c24f0a 3948 }
91c24f0a
DC
3949}
3950
4bb7a0a7
DJ
3951/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3952 Return the corresponding abbrev, or NULL if the number is zero (indicating
3953 an empty DIE). In either case *BYTES_READ will be set to the length of
3954 the initial number. */
3955
3956static struct abbrev_info *
fe1b8b76 3957peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3958 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3959{
3960 bfd *abfd = cu->objfile->obfd;
3961 unsigned int abbrev_number;
3962 struct abbrev_info *abbrev;
3963
3964 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3965
3966 if (abbrev_number == 0)
3967 return NULL;
3968
3969 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3970 if (!abbrev)
3971 {
8a3fe4f8 3972 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3973 bfd_get_filename (abfd));
3974 }
3975
3976 return abbrev;
3977}
3978
93311388
DE
3979/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3980 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3981 DIE. Any children of the skipped DIEs will also be skipped. */
3982
fe1b8b76 3983static gdb_byte *
93311388 3984skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3985{
3986 struct abbrev_info *abbrev;
3987 unsigned int bytes_read;
3988
3989 while (1)
3990 {
3991 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3992 if (abbrev == NULL)
3993 return info_ptr + bytes_read;
3994 else
93311388 3995 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3996 }
3997}
3998
93311388
DE
3999/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4000 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4001 abbrev corresponding to that skipped uleb128 should be passed in
4002 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4003 children. */
4004
fe1b8b76 4005static gdb_byte *
93311388
DE
4006skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4007 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4008{
4009 unsigned int bytes_read;
4010 struct attribute attr;
4011 bfd *abfd = cu->objfile->obfd;
4012 unsigned int form, i;
4013
4014 for (i = 0; i < abbrev->num_attrs; i++)
4015 {
4016 /* The only abbrev we care about is DW_AT_sibling. */
4017 if (abbrev->attrs[i].name == DW_AT_sibling)
4018 {
4019 read_attribute (&attr, &abbrev->attrs[i],
4020 abfd, info_ptr, cu);
4021 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4022 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4023 else
93311388 4024 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4025 }
4026
4027 /* If it isn't DW_AT_sibling, skip this attribute. */
4028 form = abbrev->attrs[i].form;
4029 skip_attribute:
4030 switch (form)
4031 {
4bb7a0a7 4032 case DW_FORM_ref_addr:
ae411497
TT
4033 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4034 and later it is offset sized. */
4035 if (cu->header.version == 2)
4036 info_ptr += cu->header.addr_size;
4037 else
4038 info_ptr += cu->header.offset_size;
4039 break;
4040 case DW_FORM_addr:
4bb7a0a7
DJ
4041 info_ptr += cu->header.addr_size;
4042 break;
4043 case DW_FORM_data1:
4044 case DW_FORM_ref1:
4045 case DW_FORM_flag:
4046 info_ptr += 1;
4047 break;
2dc7f7b3
TT
4048 case DW_FORM_flag_present:
4049 break;
4bb7a0a7
DJ
4050 case DW_FORM_data2:
4051 case DW_FORM_ref2:
4052 info_ptr += 2;
4053 break;
4054 case DW_FORM_data4:
4055 case DW_FORM_ref4:
4056 info_ptr += 4;
4057 break;
4058 case DW_FORM_data8:
4059 case DW_FORM_ref8:
348e048f 4060 case DW_FORM_sig8:
4bb7a0a7
DJ
4061 info_ptr += 8;
4062 break;
4063 case DW_FORM_string:
9b1c24c8 4064 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4065 info_ptr += bytes_read;
4066 break;
2dc7f7b3 4067 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4068 case DW_FORM_strp:
4069 info_ptr += cu->header.offset_size;
4070 break;
2dc7f7b3 4071 case DW_FORM_exprloc:
4bb7a0a7
DJ
4072 case DW_FORM_block:
4073 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4074 info_ptr += bytes_read;
4075 break;
4076 case DW_FORM_block1:
4077 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4078 break;
4079 case DW_FORM_block2:
4080 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4081 break;
4082 case DW_FORM_block4:
4083 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4084 break;
4085 case DW_FORM_sdata:
4086 case DW_FORM_udata:
4087 case DW_FORM_ref_udata:
4088 info_ptr = skip_leb128 (abfd, info_ptr);
4089 break;
4090 case DW_FORM_indirect:
4091 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4092 info_ptr += bytes_read;
4093 /* We need to continue parsing from here, so just go back to
4094 the top. */
4095 goto skip_attribute;
4096
4097 default:
8a3fe4f8 4098 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4099 dwarf_form_name (form),
4100 bfd_get_filename (abfd));
4101 }
4102 }
4103
4104 if (abbrev->has_children)
93311388 4105 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4106 else
4107 return info_ptr;
4108}
4109
93311388
DE
4110/* Locate ORIG_PDI's sibling.
4111 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4112 in BUFFER. */
91c24f0a 4113
fe1b8b76 4114static gdb_byte *
93311388
DE
4115locate_pdi_sibling (struct partial_die_info *orig_pdi,
4116 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4117 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4118{
4119 /* Do we know the sibling already? */
72bf9492 4120
91c24f0a
DC
4121 if (orig_pdi->sibling)
4122 return orig_pdi->sibling;
4123
4124 /* Are there any children to deal with? */
4125
4126 if (!orig_pdi->has_children)
4127 return info_ptr;
4128
4bb7a0a7 4129 /* Skip the children the long way. */
91c24f0a 4130
93311388 4131 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4132}
4133
c906108c
SS
4134/* Expand this partial symbol table into a full symbol table. */
4135
4136static void
fba45db2 4137dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4138{
c906108c
SS
4139 if (pst != NULL)
4140 {
4141 if (pst->readin)
4142 {
8a3fe4f8 4143 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4144 }
4145 else
4146 {
4147 if (info_verbose)
4148 {
a3f17187 4149 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4150 gdb_flush (gdb_stdout);
4151 }
4152
10b3939b
DJ
4153 /* Restore our global data. */
4154 dwarf2_per_objfile = objfile_data (pst->objfile,
4155 dwarf2_objfile_data_key);
4156
b2ab525c
KB
4157 /* If this psymtab is constructed from a debug-only objfile, the
4158 has_section_at_zero flag will not necessarily be correct. We
4159 can get the correct value for this flag by looking at the data
4160 associated with the (presumably stripped) associated objfile. */
4161 if (pst->objfile->separate_debug_objfile_backlink)
4162 {
4163 struct dwarf2_per_objfile *dpo_backlink
4164 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4165 dwarf2_objfile_data_key);
9a619af0 4166
b2ab525c
KB
4167 dwarf2_per_objfile->has_section_at_zero
4168 = dpo_backlink->has_section_at_zero;
4169 }
4170
98bfdba5
PA
4171 dwarf2_per_objfile->reading_partial_symbols = 0;
4172
c906108c
SS
4173 psymtab_to_symtab_1 (pst);
4174
4175 /* Finish up the debug error message. */
4176 if (info_verbose)
a3f17187 4177 printf_filtered (_("done.\n"));
c906108c
SS
4178 }
4179 }
4180}
4181
10b3939b
DJ
4182/* Add PER_CU to the queue. */
4183
4184static void
03dd20cc 4185queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4186{
4187 struct dwarf2_queue_item *item;
4188
4189 per_cu->queued = 1;
4190 item = xmalloc (sizeof (*item));
4191 item->per_cu = per_cu;
4192 item->next = NULL;
4193
4194 if (dwarf2_queue == NULL)
4195 dwarf2_queue = item;
4196 else
4197 dwarf2_queue_tail->next = item;
4198
4199 dwarf2_queue_tail = item;
4200}
4201
4202/* Process the queue. */
4203
4204static void
4205process_queue (struct objfile *objfile)
4206{
4207 struct dwarf2_queue_item *item, *next_item;
4208
03dd20cc
DJ
4209 /* The queue starts out with one item, but following a DIE reference
4210 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4211 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4212 {
9291a0cd
TT
4213 if (dwarf2_per_objfile->using_index
4214 ? !item->per_cu->v.quick->symtab
4215 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4216 process_full_comp_unit (item->per_cu);
4217
4218 item->per_cu->queued = 0;
4219 next_item = item->next;
4220 xfree (item);
4221 }
4222
4223 dwarf2_queue_tail = NULL;
4224}
4225
4226/* Free all allocated queue entries. This function only releases anything if
4227 an error was thrown; if the queue was processed then it would have been
4228 freed as we went along. */
4229
4230static void
4231dwarf2_release_queue (void *dummy)
4232{
4233 struct dwarf2_queue_item *item, *last;
4234
4235 item = dwarf2_queue;
4236 while (item)
4237 {
4238 /* Anything still marked queued is likely to be in an
4239 inconsistent state, so discard it. */
4240 if (item->per_cu->queued)
4241 {
4242 if (item->per_cu->cu != NULL)
4243 free_one_cached_comp_unit (item->per_cu->cu);
4244 item->per_cu->queued = 0;
4245 }
4246
4247 last = item;
4248 item = item->next;
4249 xfree (last);
4250 }
4251
4252 dwarf2_queue = dwarf2_queue_tail = NULL;
4253}
4254
4255/* Read in full symbols for PST, and anything it depends on. */
4256
c906108c 4257static void
fba45db2 4258psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4259{
10b3939b 4260 struct dwarf2_per_cu_data *per_cu;
c906108c 4261 struct cleanup *back_to;
aaa75496
JB
4262 int i;
4263
4264 for (i = 0; i < pst->number_of_dependencies; i++)
4265 if (!pst->dependencies[i]->readin)
4266 {
4267 /* Inform about additional files that need to be read in. */
4268 if (info_verbose)
4269 {
a3f17187 4270 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4271 fputs_filtered (" ", gdb_stdout);
4272 wrap_here ("");
4273 fputs_filtered ("and ", gdb_stdout);
4274 wrap_here ("");
4275 printf_filtered ("%s...", pst->dependencies[i]->filename);
4276 wrap_here (""); /* Flush output */
4277 gdb_flush (gdb_stdout);
4278 }
4279 psymtab_to_symtab_1 (pst->dependencies[i]);
4280 }
4281
e38df1d0 4282 per_cu = pst->read_symtab_private;
10b3939b
DJ
4283
4284 if (per_cu == NULL)
aaa75496
JB
4285 {
4286 /* It's an include file, no symbols to read for it.
4287 Everything is in the parent symtab. */
4288 pst->readin = 1;
4289 return;
4290 }
c906108c 4291
9291a0cd 4292 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4293}
4294
93311388 4295/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4296
93311388 4297static void
31ffec48 4298load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4299{
31ffec48 4300 bfd *abfd = objfile->obfd;
10b3939b 4301 struct dwarf2_cu *cu;
c764a876 4302 unsigned int offset;
93311388 4303 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4304 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4305 struct attribute *attr;
98bfdba5 4306 int read_cu = 0;
6502dd73 4307
348e048f
DE
4308 gdb_assert (! per_cu->from_debug_types);
4309
c906108c 4310 /* Set local variables from the partial symbol table info. */
10b3939b 4311 offset = per_cu->offset;
6502dd73 4312
be391dca 4313 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4314 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4315 beg_of_comp_unit = info_ptr;
63d06c5c 4316
98bfdba5
PA
4317 if (per_cu->cu == NULL)
4318 {
4319 cu = alloc_one_comp_unit (objfile);
4320
4321 read_cu = 1;
c906108c 4322
98bfdba5
PA
4323 /* If an error occurs while loading, release our storage. */
4324 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4325
98bfdba5
PA
4326 /* Read in the comp_unit header. */
4327 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4328
98bfdba5
PA
4329 /* Complete the cu_header. */
4330 cu->header.offset = offset;
4331 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4332
98bfdba5
PA
4333 /* Read the abbrevs for this compilation unit. */
4334 dwarf2_read_abbrevs (abfd, cu);
4335 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4336
98bfdba5
PA
4337 /* Link this compilation unit into the compilation unit tree. */
4338 per_cu->cu = cu;
4339 cu->per_cu = per_cu;
98bfdba5
PA
4340
4341 /* Link this CU into read_in_chain. */
4342 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4343 dwarf2_per_objfile->read_in_chain = per_cu;
4344 }
4345 else
4346 {
4347 cu = per_cu->cu;
4348 info_ptr += cu->header.first_die_offset;
4349 }
e142c38c 4350
93311388 4351 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4352
4353 /* We try not to read any attributes in this function, because not
4354 all objfiles needed for references have been loaded yet, and symbol
4355 table processing isn't initialized. But we have to set the CU language,
4356 or we won't be able to build types correctly. */
4357 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4358 if (attr)
4359 set_cu_language (DW_UNSND (attr), cu);
4360 else
4361 set_cu_language (language_minimal, cu);
4362
a6c727b2
DJ
4363 /* Similarly, if we do not read the producer, we can not apply
4364 producer-specific interpretation. */
4365 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4366 if (attr)
4367 cu->producer = DW_STRING (attr);
4368
98bfdba5
PA
4369 if (read_cu)
4370 {
4371 do_cleanups (free_abbrevs_cleanup);
e142c38c 4372
98bfdba5
PA
4373 /* We've successfully allocated this compilation unit. Let our
4374 caller clean it up when finished with it. */
4375 discard_cleanups (free_cu_cleanup);
4376 }
10b3939b
DJ
4377}
4378
3da10d80
KS
4379/* Add a DIE to the delayed physname list. */
4380
4381static void
4382add_to_method_list (struct type *type, int fnfield_index, int index,
4383 const char *name, struct die_info *die,
4384 struct dwarf2_cu *cu)
4385{
4386 struct delayed_method_info mi;
4387 mi.type = type;
4388 mi.fnfield_index = fnfield_index;
4389 mi.index = index;
4390 mi.name = name;
4391 mi.die = die;
4392 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4393}
4394
4395/* A cleanup for freeing the delayed method list. */
4396
4397static void
4398free_delayed_list (void *ptr)
4399{
4400 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4401 if (cu->method_list != NULL)
4402 {
4403 VEC_free (delayed_method_info, cu->method_list);
4404 cu->method_list = NULL;
4405 }
4406}
4407
4408/* Compute the physnames of any methods on the CU's method list.
4409
4410 The computation of method physnames is delayed in order to avoid the
4411 (bad) condition that one of the method's formal parameters is of an as yet
4412 incomplete type. */
4413
4414static void
4415compute_delayed_physnames (struct dwarf2_cu *cu)
4416{
4417 int i;
4418 struct delayed_method_info *mi;
4419 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4420 {
4421 char *physname;
4422 struct fn_fieldlist *fn_flp
4423 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4424 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4425 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4426 }
4427}
4428
10b3939b
DJ
4429/* Generate full symbol information for PST and CU, whose DIEs have
4430 already been loaded into memory. */
4431
4432static void
4433process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4434{
10b3939b 4435 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4436 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4437 CORE_ADDR lowpc, highpc;
4438 struct symtab *symtab;
3da10d80 4439 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4440 CORE_ADDR baseaddr;
4441
4442 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4443
10b3939b
DJ
4444 buildsym_init ();
4445 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4446 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4447
4448 cu->list_in_scope = &file_symbols;
c906108c 4449
d85a05f0 4450 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4451
c906108c 4452 /* Do line number decoding in read_file_scope () */
10b3939b 4453 process_die (cu->dies, cu);
c906108c 4454
3da10d80
KS
4455 /* Now that we have processed all the DIEs in the CU, all the types
4456 should be complete, and it should now be safe to compute all of the
4457 physnames. */
4458 compute_delayed_physnames (cu);
4459 do_cleanups (delayed_list_cleanup);
4460
fae299cd
DC
4461 /* Some compilers don't define a DW_AT_high_pc attribute for the
4462 compilation unit. If the DW_AT_high_pc is missing, synthesize
4463 it, by scanning the DIE's below the compilation unit. */
10b3939b 4464 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4465
613e1657 4466 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4467
4468 /* Set symtab language to language from DW_AT_language.
4469 If the compilation is from a C file generated by language preprocessors,
4470 do not set the language if it was already deduced by start_subfile. */
4471 if (symtab != NULL
10b3939b 4472 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4473 {
10b3939b 4474 symtab->language = cu->language;
c906108c 4475 }
9291a0cd
TT
4476
4477 if (dwarf2_per_objfile->using_index)
4478 per_cu->v.quick->symtab = symtab;
4479 else
4480 {
4481 struct partial_symtab *pst = per_cu->v.psymtab;
4482 pst->symtab = symtab;
4483 pst->readin = 1;
4484 }
c906108c
SS
4485
4486 do_cleanups (back_to);
4487}
4488
4489/* Process a die and its children. */
4490
4491static void
e7c27a73 4492process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4493{
4494 switch (die->tag)
4495 {
4496 case DW_TAG_padding:
4497 break;
4498 case DW_TAG_compile_unit:
e7c27a73 4499 read_file_scope (die, cu);
c906108c 4500 break;
348e048f
DE
4501 case DW_TAG_type_unit:
4502 read_type_unit_scope (die, cu);
4503 break;
c906108c 4504 case DW_TAG_subprogram:
c906108c 4505 case DW_TAG_inlined_subroutine:
edb3359d 4506 read_func_scope (die, cu);
c906108c
SS
4507 break;
4508 case DW_TAG_lexical_block:
14898363
L
4509 case DW_TAG_try_block:
4510 case DW_TAG_catch_block:
e7c27a73 4511 read_lexical_block_scope (die, cu);
c906108c
SS
4512 break;
4513 case DW_TAG_class_type:
680b30c7 4514 case DW_TAG_interface_type:
c906108c
SS
4515 case DW_TAG_structure_type:
4516 case DW_TAG_union_type:
134d01f1 4517 process_structure_scope (die, cu);
c906108c
SS
4518 break;
4519 case DW_TAG_enumeration_type:
134d01f1 4520 process_enumeration_scope (die, cu);
c906108c 4521 break;
134d01f1 4522
f792889a
DJ
4523 /* These dies have a type, but processing them does not create
4524 a symbol or recurse to process the children. Therefore we can
4525 read them on-demand through read_type_die. */
c906108c 4526 case DW_TAG_subroutine_type:
72019c9c 4527 case DW_TAG_set_type:
c906108c 4528 case DW_TAG_array_type:
c906108c 4529 case DW_TAG_pointer_type:
c906108c 4530 case DW_TAG_ptr_to_member_type:
c906108c 4531 case DW_TAG_reference_type:
c906108c 4532 case DW_TAG_string_type:
c906108c 4533 break;
134d01f1 4534
c906108c 4535 case DW_TAG_base_type:
a02abb62 4536 case DW_TAG_subrange_type:
cb249c71 4537 case DW_TAG_typedef:
134d01f1
DJ
4538 /* Add a typedef symbol for the type definition, if it has a
4539 DW_AT_name. */
f792889a 4540 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4541 break;
c906108c 4542 case DW_TAG_common_block:
e7c27a73 4543 read_common_block (die, cu);
c906108c
SS
4544 break;
4545 case DW_TAG_common_inclusion:
4546 break;
d9fa45fe 4547 case DW_TAG_namespace:
63d06c5c 4548 processing_has_namespace_info = 1;
e7c27a73 4549 read_namespace (die, cu);
d9fa45fe 4550 break;
5d7cb8df 4551 case DW_TAG_module:
f55ee35c 4552 processing_has_namespace_info = 1;
5d7cb8df
JK
4553 read_module (die, cu);
4554 break;
d9fa45fe
DC
4555 case DW_TAG_imported_declaration:
4556 case DW_TAG_imported_module:
63d06c5c 4557 processing_has_namespace_info = 1;
27aa8d6a
SW
4558 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4559 || cu->language != language_fortran))
4560 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4561 dwarf_tag_name (die->tag));
4562 read_import_statement (die, cu);
d9fa45fe 4563 break;
c906108c 4564 default:
e7c27a73 4565 new_symbol (die, NULL, cu);
c906108c
SS
4566 break;
4567 }
4568}
4569
94af9270
KS
4570/* A helper function for dwarf2_compute_name which determines whether DIE
4571 needs to have the name of the scope prepended to the name listed in the
4572 die. */
4573
4574static int
4575die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4576{
1c809c68
TT
4577 struct attribute *attr;
4578
94af9270
KS
4579 switch (die->tag)
4580 {
4581 case DW_TAG_namespace:
4582 case DW_TAG_typedef:
4583 case DW_TAG_class_type:
4584 case DW_TAG_interface_type:
4585 case DW_TAG_structure_type:
4586 case DW_TAG_union_type:
4587 case DW_TAG_enumeration_type:
4588 case DW_TAG_enumerator:
4589 case DW_TAG_subprogram:
4590 case DW_TAG_member:
4591 return 1;
4592
4593 case DW_TAG_variable:
4594 /* We only need to prefix "globally" visible variables. These include
4595 any variable marked with DW_AT_external or any variable that
4596 lives in a namespace. [Variables in anonymous namespaces
4597 require prefixing, but they are not DW_AT_external.] */
4598
4599 if (dwarf2_attr (die, DW_AT_specification, cu))
4600 {
4601 struct dwarf2_cu *spec_cu = cu;
9a619af0 4602
94af9270
KS
4603 return die_needs_namespace (die_specification (die, &spec_cu),
4604 spec_cu);
4605 }
4606
1c809c68 4607 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4608 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4609 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4610 return 0;
4611 /* A variable in a lexical block of some kind does not need a
4612 namespace, even though in C++ such variables may be external
4613 and have a mangled name. */
4614 if (die->parent->tag == DW_TAG_lexical_block
4615 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4616 || die->parent->tag == DW_TAG_catch_block
4617 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4618 return 0;
4619 return 1;
94af9270
KS
4620
4621 default:
4622 return 0;
4623 }
4624}
4625
98bfdba5
PA
4626/* Retrieve the last character from a mem_file. */
4627
4628static void
4629do_ui_file_peek_last (void *object, const char *buffer, long length)
4630{
4631 char *last_char_p = (char *) object;
4632
4633 if (length > 0)
4634 *last_char_p = buffer[length - 1];
4635}
4636
94af9270
KS
4637/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4638 compute the physname for the object, which include a method's
4639 formal parameters (C++/Java) and return type (Java).
4640
af6b7be1
JB
4641 For Ada, return the DIE's linkage name rather than the fully qualified
4642 name. PHYSNAME is ignored..
4643
94af9270
KS
4644 The result is allocated on the objfile_obstack and canonicalized. */
4645
4646static const char *
4647dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4648 int physname)
4649{
4650 if (name == NULL)
4651 name = dwarf2_name (die, cu);
4652
f55ee35c
JK
4653 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4654 compute it by typename_concat inside GDB. */
4655 if (cu->language == language_ada
4656 || (cu->language == language_fortran && physname))
4657 {
4658 /* For Ada unit, we prefer the linkage name over the name, as
4659 the former contains the exported name, which the user expects
4660 to be able to reference. Ideally, we want the user to be able
4661 to reference this entity using either natural or linkage name,
4662 but we haven't started looking at this enhancement yet. */
4663 struct attribute *attr;
4664
4665 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4666 if (attr == NULL)
4667 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4668 if (attr && DW_STRING (attr))
4669 return DW_STRING (attr);
4670 }
4671
94af9270
KS
4672 /* These are the only languages we know how to qualify names in. */
4673 if (name != NULL
f55ee35c
JK
4674 && (cu->language == language_cplus || cu->language == language_java
4675 || cu->language == language_fortran))
94af9270
KS
4676 {
4677 if (die_needs_namespace (die, cu))
4678 {
4679 long length;
4680 char *prefix;
4681 struct ui_file *buf;
4682
4683 prefix = determine_prefix (die, cu);
4684 buf = mem_fileopen ();
4685 if (*prefix != '\0')
4686 {
f55ee35c
JK
4687 char *prefixed_name = typename_concat (NULL, prefix, name,
4688 physname, cu);
9a619af0 4689
94af9270
KS
4690 fputs_unfiltered (prefixed_name, buf);
4691 xfree (prefixed_name);
4692 }
4693 else
4694 fputs_unfiltered (name ? name : "", buf);
4695
98bfdba5
PA
4696 /* Template parameters may be specified in the DIE's DW_AT_name, or
4697 as children with DW_TAG_template_type_param or
4698 DW_TAG_value_type_param. If the latter, add them to the name
4699 here. If the name already has template parameters, then
4700 skip this step; some versions of GCC emit both, and
4701 it is more efficient to use the pre-computed name.
4702
4703 Something to keep in mind about this process: it is very
4704 unlikely, or in some cases downright impossible, to produce
4705 something that will match the mangled name of a function.
4706 If the definition of the function has the same debug info,
4707 we should be able to match up with it anyway. But fallbacks
4708 using the minimal symbol, for instance to find a method
4709 implemented in a stripped copy of libstdc++, will not work.
4710 If we do not have debug info for the definition, we will have to
4711 match them up some other way.
4712
4713 When we do name matching there is a related problem with function
4714 templates; two instantiated function templates are allowed to
4715 differ only by their return types, which we do not add here. */
4716
4717 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4718 {
4719 struct attribute *attr;
4720 struct die_info *child;
4721 int first = 1;
4722
4723 die->building_fullname = 1;
4724
4725 for (child = die->child; child != NULL; child = child->sibling)
4726 {
4727 struct type *type;
4728 long value;
4729 gdb_byte *bytes;
4730 struct dwarf2_locexpr_baton *baton;
4731 struct value *v;
4732
4733 if (child->tag != DW_TAG_template_type_param
4734 && child->tag != DW_TAG_template_value_param)
4735 continue;
4736
4737 if (first)
4738 {
4739 fputs_unfiltered ("<", buf);
4740 first = 0;
4741 }
4742 else
4743 fputs_unfiltered (", ", buf);
4744
4745 attr = dwarf2_attr (child, DW_AT_type, cu);
4746 if (attr == NULL)
4747 {
4748 complaint (&symfile_complaints,
4749 _("template parameter missing DW_AT_type"));
4750 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4751 continue;
4752 }
4753 type = die_type (child, cu);
4754
4755 if (child->tag == DW_TAG_template_type_param)
4756 {
4757 c_print_type (type, "", buf, -1, 0);
4758 continue;
4759 }
4760
4761 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4762 if (attr == NULL)
4763 {
4764 complaint (&symfile_complaints,
4765 _("template parameter missing DW_AT_const_value"));
4766 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4767 continue;
4768 }
4769
4770 dwarf2_const_value_attr (attr, type, name,
4771 &cu->comp_unit_obstack, cu,
4772 &value, &bytes, &baton);
4773
4774 if (TYPE_NOSIGN (type))
4775 /* GDB prints characters as NUMBER 'CHAR'. If that's
4776 changed, this can use value_print instead. */
4777 c_printchar (value, type, buf);
4778 else
4779 {
4780 struct value_print_options opts;
4781
4782 if (baton != NULL)
4783 v = dwarf2_evaluate_loc_desc (type, NULL,
4784 baton->data,
4785 baton->size,
4786 baton->per_cu);
4787 else if (bytes != NULL)
4788 {
4789 v = allocate_value (type);
4790 memcpy (value_contents_writeable (v), bytes,
4791 TYPE_LENGTH (type));
4792 }
4793 else
4794 v = value_from_longest (type, value);
4795
4796 /* Specify decimal so that we do not depend on the radix. */
4797 get_formatted_print_options (&opts, 'd');
4798 opts.raw = 1;
4799 value_print (v, buf, &opts);
4800 release_value (v);
4801 value_free (v);
4802 }
4803 }
4804
4805 die->building_fullname = 0;
4806
4807 if (!first)
4808 {
4809 /* Close the argument list, with a space if necessary
4810 (nested templates). */
4811 char last_char = '\0';
4812 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4813 if (last_char == '>')
4814 fputs_unfiltered (" >", buf);
4815 else
4816 fputs_unfiltered (">", buf);
4817 }
4818 }
4819
94af9270
KS
4820 /* For Java and C++ methods, append formal parameter type
4821 information, if PHYSNAME. */
6e70227d 4822
94af9270
KS
4823 if (physname && die->tag == DW_TAG_subprogram
4824 && (cu->language == language_cplus
4825 || cu->language == language_java))
4826 {
4827 struct type *type = read_type_die (die, cu);
4828
4829 c_type_print_args (type, buf, 0, cu->language);
4830
4831 if (cu->language == language_java)
4832 {
4833 /* For java, we must append the return type to method
4834 names. */
4835 if (die->tag == DW_TAG_subprogram)
4836 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4837 0, 0);
4838 }
4839 else if (cu->language == language_cplus)
4840 {
4841 if (TYPE_NFIELDS (type) > 0
4842 && TYPE_FIELD_ARTIFICIAL (type, 0)
4843 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4844 fputs_unfiltered (" const", buf);
4845 }
4846 }
4847
4848 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4849 &length);
4850 ui_file_delete (buf);
4851
4852 if (cu->language == language_cplus)
4853 {
4854 char *cname
4855 = dwarf2_canonicalize_name (name, cu,
4856 &cu->objfile->objfile_obstack);
9a619af0 4857
94af9270
KS
4858 if (cname != NULL)
4859 name = cname;
4860 }
4861 }
4862 }
4863
4864 return name;
4865}
4866
0114d602
DJ
4867/* Return the fully qualified name of DIE, based on its DW_AT_name.
4868 If scope qualifiers are appropriate they will be added. The result
4869 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4870 not have a name. NAME may either be from a previous call to
4871 dwarf2_name or NULL.
4872
4873 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4874
4875static const char *
94af9270 4876dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4877{
94af9270
KS
4878 return dwarf2_compute_name (name, die, cu, 0);
4879}
0114d602 4880
94af9270
KS
4881/* Construct a physname for the given DIE in CU. NAME may either be
4882 from a previous call to dwarf2_name or NULL. The result will be
4883 allocated on the objfile_objstack or NULL if the DIE does not have a
4884 name.
0114d602 4885
94af9270 4886 The output string will be canonicalized (if C++/Java). */
0114d602 4887
94af9270
KS
4888static const char *
4889dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4890{
4891 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4892}
4893
27aa8d6a
SW
4894/* Read the import statement specified by the given die and record it. */
4895
4896static void
4897read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4898{
4899 struct attribute *import_attr;
4900 struct die_info *imported_die;
de4affc9 4901 struct dwarf2_cu *imported_cu;
27aa8d6a 4902 const char *imported_name;
794684b6 4903 const char *imported_name_prefix;
13387711
SW
4904 const char *canonical_name;
4905 const char *import_alias;
4906 const char *imported_declaration = NULL;
794684b6 4907 const char *import_prefix;
13387711
SW
4908
4909 char *temp;
27aa8d6a
SW
4910
4911 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4912 if (import_attr == NULL)
4913 {
4914 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4915 dwarf_tag_name (die->tag));
4916 return;
4917 }
4918
de4affc9
CC
4919 imported_cu = cu;
4920 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4921 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4922 if (imported_name == NULL)
4923 {
4924 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4925
4926 The import in the following code:
4927 namespace A
4928 {
4929 typedef int B;
4930 }
4931
4932 int main ()
4933 {
4934 using A::B;
4935 B b;
4936 return b;
4937 }
4938
4939 ...
4940 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4941 <52> DW_AT_decl_file : 1
4942 <53> DW_AT_decl_line : 6
4943 <54> DW_AT_import : <0x75>
4944 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4945 <59> DW_AT_name : B
4946 <5b> DW_AT_decl_file : 1
4947 <5c> DW_AT_decl_line : 2
4948 <5d> DW_AT_type : <0x6e>
4949 ...
4950 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4951 <76> DW_AT_byte_size : 4
4952 <77> DW_AT_encoding : 5 (signed)
4953
4954 imports the wrong die ( 0x75 instead of 0x58 ).
4955 This case will be ignored until the gcc bug is fixed. */
4956 return;
4957 }
4958
82856980
SW
4959 /* Figure out the local name after import. */
4960 import_alias = dwarf2_name (die, cu);
27aa8d6a 4961
794684b6
SW
4962 /* Figure out where the statement is being imported to. */
4963 import_prefix = determine_prefix (die, cu);
4964
4965 /* Figure out what the scope of the imported die is and prepend it
4966 to the name of the imported die. */
de4affc9 4967 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4968
f55ee35c
JK
4969 if (imported_die->tag != DW_TAG_namespace
4970 && imported_die->tag != DW_TAG_module)
794684b6 4971 {
13387711
SW
4972 imported_declaration = imported_name;
4973 canonical_name = imported_name_prefix;
794684b6 4974 }
13387711 4975 else if (strlen (imported_name_prefix) > 0)
794684b6 4976 {
13387711
SW
4977 temp = alloca (strlen (imported_name_prefix)
4978 + 2 + strlen (imported_name) + 1);
4979 strcpy (temp, imported_name_prefix);
4980 strcat (temp, "::");
4981 strcat (temp, imported_name);
4982 canonical_name = temp;
794684b6 4983 }
13387711
SW
4984 else
4985 canonical_name = imported_name;
794684b6 4986
c0cc3a76
SW
4987 cp_add_using_directive (import_prefix,
4988 canonical_name,
4989 import_alias,
13387711 4990 imported_declaration,
c0cc3a76 4991 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4992}
4993
5fb290d7 4994static void
e142c38c 4995initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4996{
e142c38c 4997 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4998}
4999
cb1df416
DJ
5000static void
5001free_cu_line_header (void *arg)
5002{
5003 struct dwarf2_cu *cu = arg;
5004
5005 free_line_header (cu->line_header);
5006 cu->line_header = NULL;
5007}
5008
9291a0cd
TT
5009static void
5010find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5011 char **name, char **comp_dir)
5012{
5013 struct attribute *attr;
5014
5015 *name = NULL;
5016 *comp_dir = NULL;
5017
5018 /* Find the filename. Do not use dwarf2_name here, since the filename
5019 is not a source language identifier. */
5020 attr = dwarf2_attr (die, DW_AT_name, cu);
5021 if (attr)
5022 {
5023 *name = DW_STRING (attr);
5024 }
5025
5026 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5027 if (attr)
5028 *comp_dir = DW_STRING (attr);
5029 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5030 {
5031 *comp_dir = ldirname (*name);
5032 if (*comp_dir != NULL)
5033 make_cleanup (xfree, *comp_dir);
5034 }
5035 if (*comp_dir != NULL)
5036 {
5037 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5038 directory, get rid of it. */
5039 char *cp = strchr (*comp_dir, ':');
5040
5041 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5042 *comp_dir = cp + 1;
5043 }
5044
5045 if (*name == NULL)
5046 *name = "<unknown>";
5047}
5048
c906108c 5049static void
e7c27a73 5050read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5051{
e7c27a73 5052 struct objfile *objfile = cu->objfile;
debd256d 5053 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5054 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5055 CORE_ADDR highpc = ((CORE_ADDR) 0);
5056 struct attribute *attr;
e1024ff1 5057 char *name = NULL;
c906108c
SS
5058 char *comp_dir = NULL;
5059 struct die_info *child_die;
5060 bfd *abfd = objfile->obfd;
debd256d 5061 struct line_header *line_header = 0;
e142c38c 5062 CORE_ADDR baseaddr;
6e70227d 5063
e142c38c 5064 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5065
fae299cd 5066 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5067
5068 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5069 from finish_block. */
2acceee2 5070 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5071 lowpc = highpc;
5072 lowpc += baseaddr;
5073 highpc += baseaddr;
5074
9291a0cd 5075 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5076
e142c38c 5077 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5078 if (attr)
5079 {
e142c38c 5080 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5081 }
5082
b0f35d58 5083 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5084 if (attr)
b0f35d58 5085 cu->producer = DW_STRING (attr);
303b6f5d 5086
c906108c
SS
5087 /* We assume that we're processing GCC output. */
5088 processing_gcc_compilation = 2;
c906108c 5089
df8a16a1
DJ
5090 processing_has_namespace_info = 0;
5091
c906108c
SS
5092 start_symtab (name, comp_dir, lowpc);
5093 record_debugformat ("DWARF 2");
303b6f5d 5094 record_producer (cu->producer);
c906108c 5095
e142c38c 5096 initialize_cu_func_list (cu);
c906108c 5097
cb1df416
DJ
5098 /* Decode line number information if present. We do this before
5099 processing child DIEs, so that the line header table is available
5100 for DW_AT_decl_file. */
e142c38c 5101 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5102 if (attr)
5103 {
debd256d 5104 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5105 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5106 if (line_header)
5107 {
cb1df416
DJ
5108 cu->line_header = line_header;
5109 make_cleanup (free_cu_line_header, cu);
aaa75496 5110 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5111 }
5fb290d7 5112 }
debd256d 5113
cb1df416
DJ
5114 /* Process all dies in compilation unit. */
5115 if (die->child != NULL)
5116 {
5117 child_die = die->child;
5118 while (child_die && child_die->tag)
5119 {
5120 process_die (child_die, cu);
5121 child_die = sibling_die (child_die);
5122 }
5123 }
5124
2e276125
JB
5125 /* Decode macro information, if present. Dwarf 2 macro information
5126 refers to information in the line number info statement program
5127 header, so we can only read it if we've read the header
5128 successfully. */
e142c38c 5129 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5130 if (attr && line_header)
2e276125
JB
5131 {
5132 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5133
2e276125 5134 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5135 comp_dir, abfd, cu);
2e276125 5136 }
debd256d 5137 do_cleanups (back_to);
5fb290d7
DJ
5138}
5139
348e048f
DE
5140/* For TUs we want to skip the first top level sibling if it's not the
5141 actual type being defined by this TU. In this case the first top
5142 level sibling is there to provide context only. */
5143
5144static void
5145read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5146{
5147 struct objfile *objfile = cu->objfile;
5148 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5149 CORE_ADDR lowpc;
5150 struct attribute *attr;
5151 char *name = NULL;
5152 char *comp_dir = NULL;
5153 struct die_info *child_die;
5154 bfd *abfd = objfile->obfd;
348e048f
DE
5155
5156 /* start_symtab needs a low pc, but we don't really have one.
5157 Do what read_file_scope would do in the absence of such info. */
5158 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5159
5160 /* Find the filename. Do not use dwarf2_name here, since the filename
5161 is not a source language identifier. */
5162 attr = dwarf2_attr (die, DW_AT_name, cu);
5163 if (attr)
5164 name = DW_STRING (attr);
5165
5166 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5167 if (attr)
5168 comp_dir = DW_STRING (attr);
5169 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5170 {
5171 comp_dir = ldirname (name);
5172 if (comp_dir != NULL)
5173 make_cleanup (xfree, comp_dir);
5174 }
5175
5176 if (name == NULL)
5177 name = "<unknown>";
5178
5179 attr = dwarf2_attr (die, DW_AT_language, cu);
5180 if (attr)
5181 set_cu_language (DW_UNSND (attr), cu);
5182
5183 /* This isn't technically needed today. It is done for symmetry
5184 with read_file_scope. */
5185 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5186 if (attr)
348e048f
DE
5187 cu->producer = DW_STRING (attr);
5188
5189 /* We assume that we're processing GCC output. */
5190 processing_gcc_compilation = 2;
5191
5192 processing_has_namespace_info = 0;
5193
5194 start_symtab (name, comp_dir, lowpc);
5195 record_debugformat ("DWARF 2");
5196 record_producer (cu->producer);
5197
5198 /* Process the dies in the type unit. */
5199 if (die->child == NULL)
5200 {
5201 dump_die_for_error (die);
5202 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5203 bfd_get_filename (abfd));
5204 }
5205
5206 child_die = die->child;
5207
5208 while (child_die && child_die->tag)
5209 {
5210 process_die (child_die, cu);
5211
5212 child_die = sibling_die (child_die);
5213 }
5214
5215 do_cleanups (back_to);
5216}
5217
5fb290d7 5218static void
e142c38c
DJ
5219add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5220 struct dwarf2_cu *cu)
5fb290d7
DJ
5221{
5222 struct function_range *thisfn;
5223
5224 thisfn = (struct function_range *)
7b5a2f43 5225 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5226 thisfn->name = name;
5227 thisfn->lowpc = lowpc;
5228 thisfn->highpc = highpc;
5229 thisfn->seen_line = 0;
5230 thisfn->next = NULL;
5231
e142c38c
DJ
5232 if (cu->last_fn == NULL)
5233 cu->first_fn = thisfn;
5fb290d7 5234 else
e142c38c 5235 cu->last_fn->next = thisfn;
5fb290d7 5236
e142c38c 5237 cu->last_fn = thisfn;
c906108c
SS
5238}
5239
d389af10
JK
5240/* qsort helper for inherit_abstract_dies. */
5241
5242static int
5243unsigned_int_compar (const void *ap, const void *bp)
5244{
5245 unsigned int a = *(unsigned int *) ap;
5246 unsigned int b = *(unsigned int *) bp;
5247
5248 return (a > b) - (b > a);
5249}
5250
5251/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5252 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5253 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5254
5255static void
5256inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5257{
5258 struct die_info *child_die;
5259 unsigned die_children_count;
5260 /* CU offsets which were referenced by children of the current DIE. */
5261 unsigned *offsets;
5262 unsigned *offsets_end, *offsetp;
5263 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5264 struct die_info *origin_die;
5265 /* Iterator of the ORIGIN_DIE children. */
5266 struct die_info *origin_child_die;
5267 struct cleanup *cleanups;
5268 struct attribute *attr;
5269
5270 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5271 if (!attr)
5272 return;
5273
5274 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
5275 if (die->tag != origin_die->tag
5276 && !(die->tag == DW_TAG_inlined_subroutine
5277 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5278 complaint (&symfile_complaints,
5279 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5280 die->offset, origin_die->offset);
5281
5282 child_die = die->child;
5283 die_children_count = 0;
5284 while (child_die && child_die->tag)
5285 {
5286 child_die = sibling_die (child_die);
5287 die_children_count++;
5288 }
5289 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5290 cleanups = make_cleanup (xfree, offsets);
5291
5292 offsets_end = offsets;
5293 child_die = die->child;
5294 while (child_die && child_die->tag)
5295 {
c38f313d
DJ
5296 /* For each CHILD_DIE, find the corresponding child of
5297 ORIGIN_DIE. If there is more than one layer of
5298 DW_AT_abstract_origin, follow them all; there shouldn't be,
5299 but GCC versions at least through 4.4 generate this (GCC PR
5300 40573). */
5301 struct die_info *child_origin_die = child_die;
9a619af0 5302
c38f313d
DJ
5303 while (1)
5304 {
5305 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
5306 if (attr == NULL)
5307 break;
673bfd45
DE
5308 /* FIXME: cu becomes CU of child_origin_die.
5309 What about the next iteration of the outer loop?
5310 cu might then be bogus (won't be CU for child_die). */
c38f313d
DJ
5311 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
5312 }
5313
d389af10
JK
5314 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5315 counterpart may exist. */
c38f313d 5316 if (child_origin_die != child_die)
d389af10 5317 {
edb3359d
DJ
5318 if (child_die->tag != child_origin_die->tag
5319 && !(child_die->tag == DW_TAG_inlined_subroutine
5320 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5321 complaint (&symfile_complaints,
5322 _("Child DIE 0x%x and its abstract origin 0x%x have "
5323 "different tags"), child_die->offset,
5324 child_origin_die->offset);
c38f313d
DJ
5325 if (child_origin_die->parent != origin_die)
5326 complaint (&symfile_complaints,
5327 _("Child DIE 0x%x and its abstract origin 0x%x have "
5328 "different parents"), child_die->offset,
5329 child_origin_die->offset);
5330 else
5331 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5332 }
5333 child_die = sibling_die (child_die);
5334 }
5335 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5336 unsigned_int_compar);
5337 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5338 if (offsetp[-1] == *offsetp)
5339 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5340 "to DIE 0x%x as their abstract origin"),
5341 die->offset, *offsetp);
5342
5343 offsetp = offsets;
5344 origin_child_die = origin_die->child;
5345 while (origin_child_die && origin_child_die->tag)
5346 {
5347 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5348 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5349 offsetp++;
5350 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5351 {
5352 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5353 process_die (origin_child_die, cu);
5354 }
5355 origin_child_die = sibling_die (origin_child_die);
5356 }
5357
5358 do_cleanups (cleanups);
5359}
5360
c906108c 5361static void
e7c27a73 5362read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5363{
e7c27a73 5364 struct objfile *objfile = cu->objfile;
52f0bd74 5365 struct context_stack *new;
c906108c
SS
5366 CORE_ADDR lowpc;
5367 CORE_ADDR highpc;
5368 struct die_info *child_die;
edb3359d 5369 struct attribute *attr, *call_line, *call_file;
c906108c 5370 char *name;
e142c38c 5371 CORE_ADDR baseaddr;
801e3a5b 5372 struct block *block;
edb3359d 5373 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5374 VEC (symbolp) *template_args = NULL;
5375 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5376
5377 if (inlined_func)
5378 {
5379 /* If we do not have call site information, we can't show the
5380 caller of this inlined function. That's too confusing, so
5381 only use the scope for local variables. */
5382 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5383 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5384 if (call_line == NULL || call_file == NULL)
5385 {
5386 read_lexical_block_scope (die, cu);
5387 return;
5388 }
5389 }
c906108c 5390
e142c38c
DJ
5391 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5392
94af9270 5393 name = dwarf2_name (die, cu);
c906108c 5394
e8d05480
JB
5395 /* Ignore functions with missing or empty names. These are actually
5396 illegal according to the DWARF standard. */
5397 if (name == NULL)
5398 {
5399 complaint (&symfile_complaints,
5400 _("missing name for subprogram DIE at %d"), die->offset);
5401 return;
5402 }
5403
5404 /* Ignore functions with missing or invalid low and high pc attributes. */
5405 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5406 {
ae4d0c03
PM
5407 attr = dwarf2_attr (die, DW_AT_external, cu);
5408 if (!attr || !DW_UNSND (attr))
5409 complaint (&symfile_complaints,
5410 _("cannot get low and high bounds for subprogram DIE at %d"),
5411 die->offset);
e8d05480
JB
5412 return;
5413 }
c906108c
SS
5414
5415 lowpc += baseaddr;
5416 highpc += baseaddr;
5417
5fb290d7 5418 /* Record the function range for dwarf_decode_lines. */
e142c38c 5419 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5420
34eaf542
TT
5421 /* If we have any template arguments, then we must allocate a
5422 different sort of symbol. */
5423 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5424 {
5425 if (child_die->tag == DW_TAG_template_type_param
5426 || child_die->tag == DW_TAG_template_value_param)
5427 {
5428 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5429 struct template_symbol);
5430 templ_func->base.is_cplus_template_function = 1;
5431 break;
5432 }
5433 }
5434
c906108c 5435 new = push_context (0, lowpc);
34eaf542
TT
5436 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5437 (struct symbol *) templ_func);
4c2df51b 5438
4cecd739
DJ
5439 /* If there is a location expression for DW_AT_frame_base, record
5440 it. */
e142c38c 5441 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5442 if (attr)
c034e007
AC
5443 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5444 expression is being recorded directly in the function's symbol
5445 and not in a separate frame-base object. I guess this hack is
5446 to avoid adding some sort of frame-base adjunct/annex to the
5447 function's symbol :-(. The problem with doing this is that it
5448 results in a function symbol with a location expression that
5449 has nothing to do with the location of the function, ouch! The
5450 relationship should be: a function's symbol has-a frame base; a
5451 frame-base has-a location expression. */
e7c27a73 5452 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5453
e142c38c 5454 cu->list_in_scope = &local_symbols;
c906108c 5455
639d11d3 5456 if (die->child != NULL)
c906108c 5457 {
639d11d3 5458 child_die = die->child;
c906108c
SS
5459 while (child_die && child_die->tag)
5460 {
34eaf542
TT
5461 if (child_die->tag == DW_TAG_template_type_param
5462 || child_die->tag == DW_TAG_template_value_param)
5463 {
5464 struct symbol *arg = new_symbol (child_die, NULL, cu);
5465
5466 VEC_safe_push (symbolp, template_args, arg);
5467 }
5468 else
5469 process_die (child_die, cu);
c906108c
SS
5470 child_die = sibling_die (child_die);
5471 }
5472 }
5473
d389af10
JK
5474 inherit_abstract_dies (die, cu);
5475
4a811a97
UW
5476 /* If we have a DW_AT_specification, we might need to import using
5477 directives from the context of the specification DIE. See the
5478 comment in determine_prefix. */
5479 if (cu->language == language_cplus
5480 && dwarf2_attr (die, DW_AT_specification, cu))
5481 {
5482 struct dwarf2_cu *spec_cu = cu;
5483 struct die_info *spec_die = die_specification (die, &spec_cu);
5484
5485 while (spec_die)
5486 {
5487 child_die = spec_die->child;
5488 while (child_die && child_die->tag)
5489 {
5490 if (child_die->tag == DW_TAG_imported_module)
5491 process_die (child_die, spec_cu);
5492 child_die = sibling_die (child_die);
5493 }
5494
5495 /* In some cases, GCC generates specification DIEs that
5496 themselves contain DW_AT_specification attributes. */
5497 spec_die = die_specification (spec_die, &spec_cu);
5498 }
5499 }
5500
c906108c
SS
5501 new = pop_context ();
5502 /* Make a block for the local symbols within. */
801e3a5b
JB
5503 block = finish_block (new->name, &local_symbols, new->old_blocks,
5504 lowpc, highpc, objfile);
5505
df8a16a1 5506 /* For C++, set the block's scope. */
f55ee35c 5507 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5508 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5509 determine_prefix (die, cu),
df8a16a1
DJ
5510 processing_has_namespace_info);
5511
801e3a5b
JB
5512 /* If we have address ranges, record them. */
5513 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5514
34eaf542
TT
5515 /* Attach template arguments to function. */
5516 if (! VEC_empty (symbolp, template_args))
5517 {
5518 gdb_assert (templ_func != NULL);
5519
5520 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5521 templ_func->template_arguments
5522 = obstack_alloc (&objfile->objfile_obstack,
5523 (templ_func->n_template_arguments
5524 * sizeof (struct symbol *)));
5525 memcpy (templ_func->template_arguments,
5526 VEC_address (symbolp, template_args),
5527 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5528 VEC_free (symbolp, template_args);
5529 }
5530
208d8187
JB
5531 /* In C++, we can have functions nested inside functions (e.g., when
5532 a function declares a class that has methods). This means that
5533 when we finish processing a function scope, we may need to go
5534 back to building a containing block's symbol lists. */
5535 local_symbols = new->locals;
5536 param_symbols = new->params;
27aa8d6a 5537 using_directives = new->using_directives;
208d8187 5538
921e78cf
JB
5539 /* If we've finished processing a top-level function, subsequent
5540 symbols go in the file symbol list. */
5541 if (outermost_context_p ())
e142c38c 5542 cu->list_in_scope = &file_symbols;
c906108c
SS
5543}
5544
5545/* Process all the DIES contained within a lexical block scope. Start
5546 a new scope, process the dies, and then close the scope. */
5547
5548static void
e7c27a73 5549read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5550{
e7c27a73 5551 struct objfile *objfile = cu->objfile;
52f0bd74 5552 struct context_stack *new;
c906108c
SS
5553 CORE_ADDR lowpc, highpc;
5554 struct die_info *child_die;
e142c38c
DJ
5555 CORE_ADDR baseaddr;
5556
5557 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5558
5559 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5560 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5561 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5562 be nasty. Might be easier to properly extend generic blocks to
af34e669 5563 describe ranges. */
d85a05f0 5564 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5565 return;
5566 lowpc += baseaddr;
5567 highpc += baseaddr;
5568
5569 push_context (0, lowpc);
639d11d3 5570 if (die->child != NULL)
c906108c 5571 {
639d11d3 5572 child_die = die->child;
c906108c
SS
5573 while (child_die && child_die->tag)
5574 {
e7c27a73 5575 process_die (child_die, cu);
c906108c
SS
5576 child_die = sibling_die (child_die);
5577 }
5578 }
5579 new = pop_context ();
5580
8540c487 5581 if (local_symbols != NULL || using_directives != NULL)
c906108c 5582 {
801e3a5b
JB
5583 struct block *block
5584 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5585 highpc, objfile);
5586
5587 /* Note that recording ranges after traversing children, as we
5588 do here, means that recording a parent's ranges entails
5589 walking across all its children's ranges as they appear in
5590 the address map, which is quadratic behavior.
5591
5592 It would be nicer to record the parent's ranges before
5593 traversing its children, simply overriding whatever you find
5594 there. But since we don't even decide whether to create a
5595 block until after we've traversed its children, that's hard
5596 to do. */
5597 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5598 }
5599 local_symbols = new->locals;
27aa8d6a 5600 using_directives = new->using_directives;
c906108c
SS
5601}
5602
43039443 5603/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5604 Return 1 if the attributes are present and valid, otherwise, return 0.
5605 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5606
5607static int
5608dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5609 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5610 struct partial_symtab *ranges_pst)
43039443
JK
5611{
5612 struct objfile *objfile = cu->objfile;
5613 struct comp_unit_head *cu_header = &cu->header;
5614 bfd *obfd = objfile->obfd;
5615 unsigned int addr_size = cu_header->addr_size;
5616 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5617 /* Base address selection entry. */
5618 CORE_ADDR base;
5619 int found_base;
5620 unsigned int dummy;
5621 gdb_byte *buffer;
5622 CORE_ADDR marker;
5623 int low_set;
5624 CORE_ADDR low = 0;
5625 CORE_ADDR high = 0;
ff013f42 5626 CORE_ADDR baseaddr;
43039443 5627
d00adf39
DE
5628 found_base = cu->base_known;
5629 base = cu->base_address;
43039443 5630
be391dca 5631 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5632 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5633 {
5634 complaint (&symfile_complaints,
5635 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5636 offset);
5637 return 0;
5638 }
dce234bc 5639 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5640
5641 /* Read in the largest possible address. */
5642 marker = read_address (obfd, buffer, cu, &dummy);
5643 if ((marker & mask) == mask)
5644 {
5645 /* If we found the largest possible address, then
5646 read the base address. */
5647 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5648 buffer += 2 * addr_size;
5649 offset += 2 * addr_size;
5650 found_base = 1;
5651 }
5652
5653 low_set = 0;
5654
e7030f15 5655 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5656
43039443
JK
5657 while (1)
5658 {
5659 CORE_ADDR range_beginning, range_end;
5660
5661 range_beginning = read_address (obfd, buffer, cu, &dummy);
5662 buffer += addr_size;
5663 range_end = read_address (obfd, buffer, cu, &dummy);
5664 buffer += addr_size;
5665 offset += 2 * addr_size;
5666
5667 /* An end of list marker is a pair of zero addresses. */
5668 if (range_beginning == 0 && range_end == 0)
5669 /* Found the end of list entry. */
5670 break;
5671
5672 /* Each base address selection entry is a pair of 2 values.
5673 The first is the largest possible address, the second is
5674 the base address. Check for a base address here. */
5675 if ((range_beginning & mask) == mask)
5676 {
5677 /* If we found the largest possible address, then
5678 read the base address. */
5679 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5680 found_base = 1;
5681 continue;
5682 }
5683
5684 if (!found_base)
5685 {
5686 /* We have no valid base address for the ranges
5687 data. */
5688 complaint (&symfile_complaints,
5689 _("Invalid .debug_ranges data (no base address)"));
5690 return 0;
5691 }
5692
5693 range_beginning += base;
5694 range_end += base;
5695
ff013f42
JK
5696 if (ranges_pst != NULL && range_beginning < range_end)
5697 addrmap_set_empty (objfile->psymtabs_addrmap,
5698 range_beginning + baseaddr, range_end - 1 + baseaddr,
5699 ranges_pst);
5700
43039443
JK
5701 /* FIXME: This is recording everything as a low-high
5702 segment of consecutive addresses. We should have a
5703 data structure for discontiguous block ranges
5704 instead. */
5705 if (! low_set)
5706 {
5707 low = range_beginning;
5708 high = range_end;
5709 low_set = 1;
5710 }
5711 else
5712 {
5713 if (range_beginning < low)
5714 low = range_beginning;
5715 if (range_end > high)
5716 high = range_end;
5717 }
5718 }
5719
5720 if (! low_set)
5721 /* If the first entry is an end-of-list marker, the range
5722 describes an empty scope, i.e. no instructions. */
5723 return 0;
5724
5725 if (low_return)
5726 *low_return = low;
5727 if (high_return)
5728 *high_return = high;
5729 return 1;
5730}
5731
af34e669
DJ
5732/* Get low and high pc attributes from a die. Return 1 if the attributes
5733 are present and valid, otherwise, return 0. Return -1 if the range is
5734 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5735static int
af34e669 5736dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5737 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5738 struct partial_symtab *pst)
c906108c
SS
5739{
5740 struct attribute *attr;
af34e669
DJ
5741 CORE_ADDR low = 0;
5742 CORE_ADDR high = 0;
5743 int ret = 0;
c906108c 5744
e142c38c 5745 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5746 if (attr)
af34e669
DJ
5747 {
5748 high = DW_ADDR (attr);
e142c38c 5749 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5750 if (attr)
5751 low = DW_ADDR (attr);
5752 else
5753 /* Found high w/o low attribute. */
5754 return 0;
5755
5756 /* Found consecutive range of addresses. */
5757 ret = 1;
5758 }
c906108c 5759 else
af34e669 5760 {
e142c38c 5761 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5762 if (attr != NULL)
5763 {
af34e669 5764 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5765 .debug_ranges section. */
d85a05f0 5766 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5767 return 0;
43039443 5768 /* Found discontinuous range of addresses. */
af34e669
DJ
5769 ret = -1;
5770 }
5771 }
c906108c
SS
5772
5773 if (high < low)
5774 return 0;
5775
5776 /* When using the GNU linker, .gnu.linkonce. sections are used to
5777 eliminate duplicate copies of functions and vtables and such.
5778 The linker will arbitrarily choose one and discard the others.
5779 The AT_*_pc values for such functions refer to local labels in
5780 these sections. If the section from that file was discarded, the
5781 labels are not in the output, so the relocs get a value of 0.
5782 If this is a discarded function, mark the pc bounds as invalid,
5783 so that GDB will ignore it. */
72dca2f5 5784 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5785 return 0;
5786
5787 *lowpc = low;
5788 *highpc = high;
af34e669 5789 return ret;
c906108c
SS
5790}
5791
b084d499
JB
5792/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5793 its low and high PC addresses. Do nothing if these addresses could not
5794 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5795 and HIGHPC to the high address if greater than HIGHPC. */
5796
5797static void
5798dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5799 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5800 struct dwarf2_cu *cu)
5801{
5802 CORE_ADDR low, high;
5803 struct die_info *child = die->child;
5804
d85a05f0 5805 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5806 {
5807 *lowpc = min (*lowpc, low);
5808 *highpc = max (*highpc, high);
5809 }
5810
5811 /* If the language does not allow nested subprograms (either inside
5812 subprograms or lexical blocks), we're done. */
5813 if (cu->language != language_ada)
5814 return;
6e70227d 5815
b084d499
JB
5816 /* Check all the children of the given DIE. If it contains nested
5817 subprograms, then check their pc bounds. Likewise, we need to
5818 check lexical blocks as well, as they may also contain subprogram
5819 definitions. */
5820 while (child && child->tag)
5821 {
5822 if (child->tag == DW_TAG_subprogram
5823 || child->tag == DW_TAG_lexical_block)
5824 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5825 child = sibling_die (child);
5826 }
5827}
5828
fae299cd
DC
5829/* Get the low and high pc's represented by the scope DIE, and store
5830 them in *LOWPC and *HIGHPC. If the correct values can't be
5831 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5832
5833static void
5834get_scope_pc_bounds (struct die_info *die,
5835 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5836 struct dwarf2_cu *cu)
5837{
5838 CORE_ADDR best_low = (CORE_ADDR) -1;
5839 CORE_ADDR best_high = (CORE_ADDR) 0;
5840 CORE_ADDR current_low, current_high;
5841
d85a05f0 5842 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5843 {
5844 best_low = current_low;
5845 best_high = current_high;
5846 }
5847 else
5848 {
5849 struct die_info *child = die->child;
5850
5851 while (child && child->tag)
5852 {
5853 switch (child->tag) {
5854 case DW_TAG_subprogram:
b084d499 5855 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5856 break;
5857 case DW_TAG_namespace:
f55ee35c 5858 case DW_TAG_module:
fae299cd
DC
5859 /* FIXME: carlton/2004-01-16: Should we do this for
5860 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5861 that current GCC's always emit the DIEs corresponding
5862 to definitions of methods of classes as children of a
5863 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5864 the DIEs giving the declarations, which could be
5865 anywhere). But I don't see any reason why the
5866 standards says that they have to be there. */
5867 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5868
5869 if (current_low != ((CORE_ADDR) -1))
5870 {
5871 best_low = min (best_low, current_low);
5872 best_high = max (best_high, current_high);
5873 }
5874 break;
5875 default:
5876 /* Ignore. */
5877 break;
5878 }
5879
5880 child = sibling_die (child);
5881 }
5882 }
5883
5884 *lowpc = best_low;
5885 *highpc = best_high;
5886}
5887
801e3a5b
JB
5888/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5889 in DIE. */
5890static void
5891dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5892 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5893{
5894 struct attribute *attr;
5895
5896 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5897 if (attr)
5898 {
5899 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5900
801e3a5b
JB
5901 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5902 if (attr)
5903 {
5904 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5905
801e3a5b
JB
5906 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5907 }
5908 }
5909
5910 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5911 if (attr)
5912 {
5913 bfd *obfd = cu->objfile->obfd;
5914
5915 /* The value of the DW_AT_ranges attribute is the offset of the
5916 address range list in the .debug_ranges section. */
5917 unsigned long offset = DW_UNSND (attr);
dce234bc 5918 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5919
5920 /* For some target architectures, but not others, the
5921 read_address function sign-extends the addresses it returns.
5922 To recognize base address selection entries, we need a
5923 mask. */
5924 unsigned int addr_size = cu->header.addr_size;
5925 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5926
5927 /* The base address, to which the next pair is relative. Note
5928 that this 'base' is a DWARF concept: most entries in a range
5929 list are relative, to reduce the number of relocs against the
5930 debugging information. This is separate from this function's
5931 'baseaddr' argument, which GDB uses to relocate debugging
5932 information from a shared library based on the address at
5933 which the library was loaded. */
d00adf39
DE
5934 CORE_ADDR base = cu->base_address;
5935 int base_known = cu->base_known;
801e3a5b 5936
be391dca 5937 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5938 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5939 {
5940 complaint (&symfile_complaints,
5941 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5942 offset);
5943 return;
5944 }
5945
5946 for (;;)
5947 {
5948 unsigned int bytes_read;
5949 CORE_ADDR start, end;
5950
5951 start = read_address (obfd, buffer, cu, &bytes_read);
5952 buffer += bytes_read;
5953 end = read_address (obfd, buffer, cu, &bytes_read);
5954 buffer += bytes_read;
5955
5956 /* Did we find the end of the range list? */
5957 if (start == 0 && end == 0)
5958 break;
5959
5960 /* Did we find a base address selection entry? */
5961 else if ((start & base_select_mask) == base_select_mask)
5962 {
5963 base = end;
5964 base_known = 1;
5965 }
5966
5967 /* We found an ordinary address range. */
5968 else
5969 {
5970 if (!base_known)
5971 {
5972 complaint (&symfile_complaints,
5973 _("Invalid .debug_ranges data (no base address)"));
5974 return;
5975 }
5976
6e70227d
DE
5977 record_block_range (block,
5978 baseaddr + base + start,
801e3a5b
JB
5979 baseaddr + base + end - 1);
5980 }
5981 }
5982 }
5983}
5984
c906108c
SS
5985/* Add an aggregate field to the field list. */
5986
5987static void
107d2387 5988dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 5989 struct dwarf2_cu *cu)
6e70227d 5990{
e7c27a73 5991 struct objfile *objfile = cu->objfile;
5e2b427d 5992 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5993 struct nextfield *new_field;
5994 struct attribute *attr;
5995 struct field *fp;
5996 char *fieldname = "";
5997
5998 /* Allocate a new field list entry and link it in. */
5999 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6000 make_cleanup (xfree, new_field);
c906108c 6001 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6002
6003 if (die->tag == DW_TAG_inheritance)
6004 {
6005 new_field->next = fip->baseclasses;
6006 fip->baseclasses = new_field;
6007 }
6008 else
6009 {
6010 new_field->next = fip->fields;
6011 fip->fields = new_field;
6012 }
c906108c
SS
6013 fip->nfields++;
6014
6015 /* Handle accessibility and virtuality of field.
6016 The default accessibility for members is public, the default
6017 accessibility for inheritance is private. */
6018 if (die->tag != DW_TAG_inheritance)
6019 new_field->accessibility = DW_ACCESS_public;
6020 else
6021 new_field->accessibility = DW_ACCESS_private;
6022 new_field->virtuality = DW_VIRTUALITY_none;
6023
e142c38c 6024 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6025 if (attr)
6026 new_field->accessibility = DW_UNSND (attr);
6027 if (new_field->accessibility != DW_ACCESS_public)
6028 fip->non_public_fields = 1;
e142c38c 6029 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6030 if (attr)
6031 new_field->virtuality = DW_UNSND (attr);
6032
6033 fp = &new_field->field;
a9a9bd0f 6034
e142c38c 6035 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6036 {
a9a9bd0f 6037 /* Data member other than a C++ static data member. */
6e70227d 6038
c906108c 6039 /* Get type of field. */
e7c27a73 6040 fp->type = die_type (die, cu);
c906108c 6041
d6a843b5 6042 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6043
c906108c 6044 /* Get bit size of field (zero if none). */
e142c38c 6045 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6046 if (attr)
6047 {
6048 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6049 }
6050 else
6051 {
6052 FIELD_BITSIZE (*fp) = 0;
6053 }
6054
6055 /* Get bit offset of field. */
e142c38c 6056 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6057 if (attr)
6058 {
d4b96c9a 6059 int byte_offset = 0;
c6a0999f 6060
3690dd37 6061 if (attr_form_is_section_offset (attr))
d4b96c9a 6062 dwarf2_complex_location_expr_complaint ();
3690dd37 6063 else if (attr_form_is_constant (attr))
c6a0999f 6064 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6065 else if (attr_form_is_block (attr))
c6a0999f 6066 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6067 else
6068 dwarf2_complex_location_expr_complaint ();
c6a0999f 6069
d6a843b5 6070 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6071 }
e142c38c 6072 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6073 if (attr)
6074 {
5e2b427d 6075 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6076 {
6077 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6078 additional bit offset from the MSB of the containing
6079 anonymous object to the MSB of the field. We don't
6080 have to do anything special since we don't need to
6081 know the size of the anonymous object. */
c906108c
SS
6082 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6083 }
6084 else
6085 {
6086 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6087 MSB of the anonymous object, subtract off the number of
6088 bits from the MSB of the field to the MSB of the
6089 object, and then subtract off the number of bits of
6090 the field itself. The result is the bit offset of
6091 the LSB of the field. */
c906108c
SS
6092 int anonymous_size;
6093 int bit_offset = DW_UNSND (attr);
6094
e142c38c 6095 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6096 if (attr)
6097 {
6098 /* The size of the anonymous object containing
6099 the bit field is explicit, so use the
6100 indicated size (in bytes). */
6101 anonymous_size = DW_UNSND (attr);
6102 }
6103 else
6104 {
6105 /* The size of the anonymous object containing
6106 the bit field must be inferred from the type
6107 attribute of the data member containing the
6108 bit field. */
6109 anonymous_size = TYPE_LENGTH (fp->type);
6110 }
6111 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6112 - bit_offset - FIELD_BITSIZE (*fp);
6113 }
6114 }
6115
6116 /* Get name of field. */
39cbfefa
DJ
6117 fieldname = dwarf2_name (die, cu);
6118 if (fieldname == NULL)
6119 fieldname = "";
d8151005
DJ
6120
6121 /* The name is already allocated along with this objfile, so we don't
6122 need to duplicate it for the type. */
6123 fp->name = fieldname;
c906108c
SS
6124
6125 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6126 pointer or virtual base class pointer) to private. */
e142c38c 6127 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6128 {
d48cc9dd 6129 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6130 new_field->accessibility = DW_ACCESS_private;
6131 fip->non_public_fields = 1;
6132 }
6133 }
a9a9bd0f 6134 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6135 {
a9a9bd0f
DC
6136 /* C++ static member. */
6137
6138 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6139 is a declaration, but all versions of G++ as of this writing
6140 (so through at least 3.2.1) incorrectly generate
6141 DW_TAG_variable tags. */
6e70227d 6142
c906108c 6143 char *physname;
c906108c 6144
a9a9bd0f 6145 /* Get name of field. */
39cbfefa
DJ
6146 fieldname = dwarf2_name (die, cu);
6147 if (fieldname == NULL)
c906108c
SS
6148 return;
6149
254e6b9e 6150 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6151 if (attr
6152 /* Only create a symbol if this is an external value.
6153 new_symbol checks this and puts the value in the global symbol
6154 table, which we want. If it is not external, new_symbol
6155 will try to put the value in cu->list_in_scope which is wrong. */
6156 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6157 {
6158 /* A static const member, not much different than an enum as far as
6159 we're concerned, except that we can support more types. */
6160 new_symbol (die, NULL, cu);
6161 }
6162
2df3850c 6163 /* Get physical name. */
94af9270 6164 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6165
d8151005
DJ
6166 /* The name is already allocated along with this objfile, so we don't
6167 need to duplicate it for the type. */
6168 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6169 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6170 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6171 }
6172 else if (die->tag == DW_TAG_inheritance)
6173 {
6174 /* C++ base class field. */
e142c38c 6175 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6176 if (attr)
d4b96c9a
JK
6177 {
6178 int byte_offset = 0;
6179
6180 if (attr_form_is_section_offset (attr))
6181 dwarf2_complex_location_expr_complaint ();
6182 else if (attr_form_is_constant (attr))
6183 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6184 else if (attr_form_is_block (attr))
6185 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6186 else
6187 dwarf2_complex_location_expr_complaint ();
6188
6189 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6190 }
c906108c 6191 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6192 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6193 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6194 fip->nbaseclasses++;
6195 }
6196}
6197
98751a41
JK
6198/* Add a typedef defined in the scope of the FIP's class. */
6199
6200static void
6201dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6202 struct dwarf2_cu *cu)
6e70227d 6203{
98751a41
JK
6204 struct objfile *objfile = cu->objfile;
6205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6206 struct typedef_field_list *new_field;
6207 struct attribute *attr;
6208 struct typedef_field *fp;
6209 char *fieldname = "";
6210
6211 /* Allocate a new field list entry and link it in. */
6212 new_field = xzalloc (sizeof (*new_field));
6213 make_cleanup (xfree, new_field);
6214
6215 gdb_assert (die->tag == DW_TAG_typedef);
6216
6217 fp = &new_field->field;
6218
6219 /* Get name of field. */
6220 fp->name = dwarf2_name (die, cu);
6221 if (fp->name == NULL)
6222 return;
6223
6224 fp->type = read_type_die (die, cu);
6225
6226 new_field->next = fip->typedef_field_list;
6227 fip->typedef_field_list = new_field;
6228 fip->typedef_field_list_count++;
6229}
6230
c906108c
SS
6231/* Create the vector of fields, and attach it to the type. */
6232
6233static void
fba45db2 6234dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6235 struct dwarf2_cu *cu)
c906108c
SS
6236{
6237 int nfields = fip->nfields;
6238
6239 /* Record the field count, allocate space for the array of fields,
6240 and create blank accessibility bitfields if necessary. */
6241 TYPE_NFIELDS (type) = nfields;
6242 TYPE_FIELDS (type) = (struct field *)
6243 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6244 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6245
b4ba55a1 6246 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6247 {
6248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6249
6250 TYPE_FIELD_PRIVATE_BITS (type) =
6251 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6252 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6253
6254 TYPE_FIELD_PROTECTED_BITS (type) =
6255 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6256 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6257
6258 TYPE_FIELD_IGNORE_BITS (type) =
6259 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6260 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6261 }
6262
6263 /* If the type has baseclasses, allocate and clear a bit vector for
6264 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6265 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6266 {
6267 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6268 unsigned char *pointer;
c906108c
SS
6269
6270 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6271 pointer = TYPE_ALLOC (type, num_bytes);
6272 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6273 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6274 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6275 }
6276
6277 /* Copy the saved-up fields into the field vector. Start from the head
6278 of the list, adding to the tail of the field array, so that they end
6279 up in the same order in the array in which they were added to the list. */
6280 while (nfields-- > 0)
6281 {
7d0ccb61
DJ
6282 struct nextfield *fieldp;
6283
6284 if (fip->fields)
6285 {
6286 fieldp = fip->fields;
6287 fip->fields = fieldp->next;
6288 }
6289 else
6290 {
6291 fieldp = fip->baseclasses;
6292 fip->baseclasses = fieldp->next;
6293 }
6294
6295 TYPE_FIELD (type, nfields) = fieldp->field;
6296 switch (fieldp->accessibility)
c906108c 6297 {
c5aa993b 6298 case DW_ACCESS_private:
b4ba55a1
JB
6299 if (cu->language != language_ada)
6300 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6301 break;
c906108c 6302
c5aa993b 6303 case DW_ACCESS_protected:
b4ba55a1
JB
6304 if (cu->language != language_ada)
6305 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6306 break;
c906108c 6307
c5aa993b
JM
6308 case DW_ACCESS_public:
6309 break;
c906108c 6310
c5aa993b
JM
6311 default:
6312 /* Unknown accessibility. Complain and treat it as public. */
6313 {
e2e0b3e5 6314 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6315 fieldp->accessibility);
c5aa993b
JM
6316 }
6317 break;
c906108c
SS
6318 }
6319 if (nfields < fip->nbaseclasses)
6320 {
7d0ccb61 6321 switch (fieldp->virtuality)
c906108c 6322 {
c5aa993b
JM
6323 case DW_VIRTUALITY_virtual:
6324 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6325 if (cu->language == language_ada)
6326 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6327 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6328 break;
c906108c
SS
6329 }
6330 }
c906108c
SS
6331 }
6332}
6333
c906108c
SS
6334/* Add a member function to the proper fieldlist. */
6335
6336static void
107d2387 6337dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6338 struct type *type, struct dwarf2_cu *cu)
c906108c 6339{
e7c27a73 6340 struct objfile *objfile = cu->objfile;
c906108c
SS
6341 struct attribute *attr;
6342 struct fnfieldlist *flp;
6343 int i;
6344 struct fn_field *fnp;
6345 char *fieldname;
c906108c 6346 struct nextfnfield *new_fnfield;
f792889a 6347 struct type *this_type;
c906108c 6348
b4ba55a1
JB
6349 if (cu->language == language_ada)
6350 error ("unexpected member function in Ada type");
6351
2df3850c 6352 /* Get name of member function. */
39cbfefa
DJ
6353 fieldname = dwarf2_name (die, cu);
6354 if (fieldname == NULL)
2df3850c 6355 return;
c906108c 6356
c906108c
SS
6357 /* Look up member function name in fieldlist. */
6358 for (i = 0; i < fip->nfnfields; i++)
6359 {
27bfe10e 6360 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6361 break;
6362 }
6363
6364 /* Create new list element if necessary. */
6365 if (i < fip->nfnfields)
6366 flp = &fip->fnfieldlists[i];
6367 else
6368 {
6369 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6370 {
6371 fip->fnfieldlists = (struct fnfieldlist *)
6372 xrealloc (fip->fnfieldlists,
6373 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6374 * sizeof (struct fnfieldlist));
c906108c 6375 if (fip->nfnfields == 0)
c13c43fd 6376 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6377 }
6378 flp = &fip->fnfieldlists[fip->nfnfields];
6379 flp->name = fieldname;
6380 flp->length = 0;
6381 flp->head = NULL;
3da10d80 6382 i = fip->nfnfields++;
c906108c
SS
6383 }
6384
6385 /* Create a new member function field and chain it to the field list
6386 entry. */
6387 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6388 make_cleanup (xfree, new_fnfield);
c906108c
SS
6389 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6390 new_fnfield->next = flp->head;
6391 flp->head = new_fnfield;
6392 flp->length++;
6393
6394 /* Fill in the member function field info. */
6395 fnp = &new_fnfield->fnfield;
3da10d80
KS
6396
6397 /* Delay processing of the physname until later. */
6398 if (cu->language == language_cplus || cu->language == language_java)
6399 {
6400 add_to_method_list (type, i, flp->length - 1, fieldname,
6401 die, cu);
6402 }
6403 else
6404 {
6405 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6406 fnp->physname = physname ? physname : "";
6407 }
6408
c906108c 6409 fnp->type = alloc_type (objfile);
f792889a
DJ
6410 this_type = read_type_die (die, cu);
6411 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6412 {
f792889a 6413 int nparams = TYPE_NFIELDS (this_type);
c906108c 6414
f792889a 6415 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6416 of the method itself (TYPE_CODE_METHOD). */
6417 smash_to_method_type (fnp->type, type,
f792889a
DJ
6418 TYPE_TARGET_TYPE (this_type),
6419 TYPE_FIELDS (this_type),
6420 TYPE_NFIELDS (this_type),
6421 TYPE_VARARGS (this_type));
c906108c
SS
6422
6423 /* Handle static member functions.
c5aa993b
JM
6424 Dwarf2 has no clean way to discern C++ static and non-static
6425 member functions. G++ helps GDB by marking the first
6426 parameter for non-static member functions (which is the
6427 this pointer) as artificial. We obtain this information
6428 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6429 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6430 fnp->voffset = VOFFSET_STATIC;
6431 }
6432 else
e2e0b3e5 6433 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6434 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6435
6436 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6437 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6438 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6439
6440 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6441 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6442
6443 /* Get accessibility. */
e142c38c 6444 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6445 if (attr)
6446 {
6447 switch (DW_UNSND (attr))
6448 {
c5aa993b
JM
6449 case DW_ACCESS_private:
6450 fnp->is_private = 1;
6451 break;
6452 case DW_ACCESS_protected:
6453 fnp->is_protected = 1;
6454 break;
c906108c
SS
6455 }
6456 }
6457
b02dede2 6458 /* Check for artificial methods. */
e142c38c 6459 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6460 if (attr && DW_UNSND (attr) != 0)
6461 fnp->is_artificial = 1;
6462
0d564a31 6463 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6464 function. For older versions of GCC, this is an offset in the
6465 appropriate virtual table, as specified by DW_AT_containing_type.
6466 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6467 to the object address. */
6468
e142c38c 6469 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6470 if (attr)
8e19ed76 6471 {
aec5aa8b 6472 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6473 {
aec5aa8b
TT
6474 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6475 {
6476 /* Old-style GCC. */
6477 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6478 }
6479 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6480 || (DW_BLOCK (attr)->size > 1
6481 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6482 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6483 {
6484 struct dwarf_block blk;
6485 int offset;
6486
6487 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6488 ? 1 : 2);
6489 blk.size = DW_BLOCK (attr)->size - offset;
6490 blk.data = DW_BLOCK (attr)->data + offset;
6491 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6492 if ((fnp->voffset % cu->header.addr_size) != 0)
6493 dwarf2_complex_location_expr_complaint ();
6494 else
6495 fnp->voffset /= cu->header.addr_size;
6496 fnp->voffset += 2;
6497 }
6498 else
6499 dwarf2_complex_location_expr_complaint ();
6500
6501 if (!fnp->fcontext)
6502 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6503 }
3690dd37 6504 else if (attr_form_is_section_offset (attr))
8e19ed76 6505 {
4d3c2250 6506 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6507 }
6508 else
6509 {
4d3c2250
KB
6510 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6511 fieldname);
8e19ed76 6512 }
0d564a31 6513 }
d48cc9dd
DJ
6514 else
6515 {
6516 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6517 if (attr && DW_UNSND (attr))
6518 {
6519 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6520 complaint (&symfile_complaints,
6521 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6522 fieldname, die->offset);
9655fd1a 6523 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6524 TYPE_CPLUS_DYNAMIC (type) = 1;
6525 }
6526 }
c906108c
SS
6527}
6528
6529/* Create the vector of member function fields, and attach it to the type. */
6530
6531static void
fba45db2 6532dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6533 struct dwarf2_cu *cu)
c906108c
SS
6534{
6535 struct fnfieldlist *flp;
6536 int total_length = 0;
6537 int i;
6538
b4ba55a1
JB
6539 if (cu->language == language_ada)
6540 error ("unexpected member functions in Ada type");
6541
c906108c
SS
6542 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6543 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6544 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6545
6546 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6547 {
6548 struct nextfnfield *nfp = flp->head;
6549 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6550 int k;
6551
6552 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6553 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6554 fn_flp->fn_fields = (struct fn_field *)
6555 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6556 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6557 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6558
6559 total_length += flp->length;
6560 }
6561
6562 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6563 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6564}
6565
1168df01
JB
6566/* Returns non-zero if NAME is the name of a vtable member in CU's
6567 language, zero otherwise. */
6568static int
6569is_vtable_name (const char *name, struct dwarf2_cu *cu)
6570{
6571 static const char vptr[] = "_vptr";
987504bb 6572 static const char vtable[] = "vtable";
1168df01 6573
987504bb
JJ
6574 /* Look for the C++ and Java forms of the vtable. */
6575 if ((cu->language == language_java
6576 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6577 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6578 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6579 return 1;
6580
6581 return 0;
6582}
6583
c0dd20ea 6584/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6585 functions, with the ABI-specified layout. If TYPE describes
6586 such a structure, smash it into a member function type.
61049d3b
DJ
6587
6588 GCC shouldn't do this; it should just output pointer to member DIEs.
6589 This is GCC PR debug/28767. */
c0dd20ea 6590
0b92b5bb
TT
6591static void
6592quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6593{
0b92b5bb 6594 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6595
6596 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6597 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6598 return;
c0dd20ea
DJ
6599
6600 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6601 if (TYPE_FIELD_NAME (type, 0) == NULL
6602 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6603 || TYPE_FIELD_NAME (type, 1) == NULL
6604 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6605 return;
c0dd20ea
DJ
6606
6607 /* Find the type of the method. */
0b92b5bb 6608 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6609 if (pfn_type == NULL
6610 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6611 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6612 return;
c0dd20ea
DJ
6613
6614 /* Look for the "this" argument. */
6615 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6616 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6617 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6618 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6619 return;
c0dd20ea
DJ
6620
6621 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6622 new_type = alloc_type (objfile);
6623 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6624 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6625 TYPE_VARARGS (pfn_type));
0b92b5bb 6626 smash_to_methodptr_type (type, new_type);
c0dd20ea 6627}
1168df01 6628
c906108c
SS
6629/* Called when we find the DIE that starts a structure or union scope
6630 (definition) to process all dies that define the members of the
6631 structure or union.
6632
6633 NOTE: we need to call struct_type regardless of whether or not the
6634 DIE has an at_name attribute, since it might be an anonymous
6635 structure or union. This gets the type entered into our set of
6636 user defined types.
6637
6638 However, if the structure is incomplete (an opaque struct/union)
6639 then suppress creating a symbol table entry for it since gdb only
6640 wants to find the one with the complete definition. Note that if
6641 it is complete, we just call new_symbol, which does it's own
6642 checking about whether the struct/union is anonymous or not (and
6643 suppresses creating a symbol table entry itself). */
6644
f792889a 6645static struct type *
134d01f1 6646read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6647{
e7c27a73 6648 struct objfile *objfile = cu->objfile;
c906108c
SS
6649 struct type *type;
6650 struct attribute *attr;
39cbfefa 6651 char *name;
d3f41bb1 6652 struct cleanup *back_to;
c906108c 6653
348e048f
DE
6654 /* If the definition of this type lives in .debug_types, read that type.
6655 Don't follow DW_AT_specification though, that will take us back up
6656 the chain and we want to go down. */
6657 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6658 if (attr)
6659 {
6660 struct dwarf2_cu *type_cu = cu;
6661 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6662
348e048f
DE
6663 /* We could just recurse on read_structure_type, but we need to call
6664 get_die_type to ensure only one type for this DIE is created.
6665 This is important, for example, because for c++ classes we need
6666 TYPE_NAME set which is only done by new_symbol. Blech. */
6667 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6668
6669 /* TYPE_CU may not be the same as CU.
6670 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6671 return set_die_type (die, type, cu);
6672 }
6673
d3f41bb1
TT
6674 back_to = make_cleanup (null_cleanup, 0);
6675
c0dd20ea 6676 type = alloc_type (objfile);
c906108c 6677 INIT_CPLUS_SPECIFIC (type);
93311388 6678
39cbfefa
DJ
6679 name = dwarf2_name (die, cu);
6680 if (name != NULL)
c906108c 6681 {
987504bb
JJ
6682 if (cu->language == language_cplus
6683 || cu->language == language_java)
63d06c5c 6684 {
3da10d80
KS
6685 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6686
6687 /* dwarf2_full_name might have already finished building the DIE's
6688 type. If so, there is no need to continue. */
6689 if (get_die_type (die, cu) != NULL)
6690 return get_die_type (die, cu);
6691
6692 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6693 if (die->tag == DW_TAG_structure_type
6694 || die->tag == DW_TAG_class_type)
6695 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6696 }
6697 else
6698 {
d8151005
DJ
6699 /* The name is already allocated along with this objfile, so
6700 we don't need to duplicate it for the type. */
94af9270
KS
6701 TYPE_TAG_NAME (type) = (char *) name;
6702 if (die->tag == DW_TAG_class_type)
6703 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6704 }
c906108c
SS
6705 }
6706
6707 if (die->tag == DW_TAG_structure_type)
6708 {
6709 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6710 }
6711 else if (die->tag == DW_TAG_union_type)
6712 {
6713 TYPE_CODE (type) = TYPE_CODE_UNION;
6714 }
6715 else
6716 {
c906108c
SS
6717 TYPE_CODE (type) = TYPE_CODE_CLASS;
6718 }
6719
0cc2414c
TT
6720 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6721 TYPE_DECLARED_CLASS (type) = 1;
6722
e142c38c 6723 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6724 if (attr)
6725 {
6726 TYPE_LENGTH (type) = DW_UNSND (attr);
6727 }
6728 else
6729 {
6730 TYPE_LENGTH (type) = 0;
6731 }
6732
876cecd0 6733 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6734 if (die_is_declaration (die, cu))
876cecd0 6735 TYPE_STUB (type) = 1;
a6c727b2
DJ
6736 else if (attr == NULL && die->child == NULL
6737 && producer_is_realview (cu->producer))
6738 /* RealView does not output the required DW_AT_declaration
6739 on incomplete types. */
6740 TYPE_STUB (type) = 1;
dc718098 6741
c906108c
SS
6742 /* We need to add the type field to the die immediately so we don't
6743 infinitely recurse when dealing with pointers to the structure
6744 type within the structure itself. */
1c379e20 6745 set_die_type (die, type, cu);
c906108c 6746
7e314c57
JK
6747 /* set_die_type should be already done. */
6748 set_descriptive_type (type, die, cu);
6749
e142c38c 6750 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6751 {
6752 struct field_info fi;
6753 struct die_info *child_die;
34eaf542 6754 VEC (symbolp) *template_args = NULL;
c906108c
SS
6755
6756 memset (&fi, 0, sizeof (struct field_info));
6757
639d11d3 6758 child_die = die->child;
c906108c
SS
6759
6760 while (child_die && child_die->tag)
6761 {
a9a9bd0f
DC
6762 if (child_die->tag == DW_TAG_member
6763 || child_die->tag == DW_TAG_variable)
c906108c 6764 {
a9a9bd0f
DC
6765 /* NOTE: carlton/2002-11-05: A C++ static data member
6766 should be a DW_TAG_member that is a declaration, but
6767 all versions of G++ as of this writing (so through at
6768 least 3.2.1) incorrectly generate DW_TAG_variable
6769 tags for them instead. */
e7c27a73 6770 dwarf2_add_field (&fi, child_die, cu);
c906108c 6771 }
8713b1b1 6772 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6773 {
6774 /* C++ member function. */
e7c27a73 6775 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6776 }
6777 else if (child_die->tag == DW_TAG_inheritance)
6778 {
6779 /* C++ base class field. */
e7c27a73 6780 dwarf2_add_field (&fi, child_die, cu);
c906108c 6781 }
98751a41
JK
6782 else if (child_die->tag == DW_TAG_typedef)
6783 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6784 else if (child_die->tag == DW_TAG_template_type_param
6785 || child_die->tag == DW_TAG_template_value_param)
6786 {
6787 struct symbol *arg = new_symbol (child_die, NULL, cu);
6788
6789 VEC_safe_push (symbolp, template_args, arg);
6790 }
6791
c906108c
SS
6792 child_die = sibling_die (child_die);
6793 }
6794
34eaf542
TT
6795 /* Attach template arguments to type. */
6796 if (! VEC_empty (symbolp, template_args))
6797 {
6798 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6799 TYPE_N_TEMPLATE_ARGUMENTS (type)
6800 = VEC_length (symbolp, template_args);
6801 TYPE_TEMPLATE_ARGUMENTS (type)
6802 = obstack_alloc (&objfile->objfile_obstack,
6803 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6804 * sizeof (struct symbol *)));
6805 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6806 VEC_address (symbolp, template_args),
6807 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6808 * sizeof (struct symbol *)));
6809 VEC_free (symbolp, template_args);
6810 }
6811
c906108c
SS
6812 /* Attach fields and member functions to the type. */
6813 if (fi.nfields)
e7c27a73 6814 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6815 if (fi.nfnfields)
6816 {
e7c27a73 6817 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6818
c5aa993b 6819 /* Get the type which refers to the base class (possibly this
c906108c 6820 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6821 class from the DW_AT_containing_type attribute. This use of
6822 DW_AT_containing_type is a GNU extension. */
c906108c 6823
e142c38c 6824 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6825 {
e7c27a73 6826 struct type *t = die_containing_type (die, cu);
c906108c
SS
6827
6828 TYPE_VPTR_BASETYPE (type) = t;
6829 if (type == t)
6830 {
c906108c
SS
6831 int i;
6832
6833 /* Our own class provides vtbl ptr. */
6834 for (i = TYPE_NFIELDS (t) - 1;
6835 i >= TYPE_N_BASECLASSES (t);
6836 --i)
6837 {
6838 char *fieldname = TYPE_FIELD_NAME (t, i);
6839
1168df01 6840 if (is_vtable_name (fieldname, cu))
c906108c
SS
6841 {
6842 TYPE_VPTR_FIELDNO (type) = i;
6843 break;
6844 }
6845 }
6846
6847 /* Complain if virtual function table field not found. */
6848 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6849 complaint (&symfile_complaints,
e2e0b3e5 6850 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6851 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6852 "");
c906108c
SS
6853 }
6854 else
6855 {
6856 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6857 }
6858 }
f6235d4c
EZ
6859 else if (cu->producer
6860 && strncmp (cu->producer,
6861 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6862 {
6863 /* The IBM XLC compiler does not provide direct indication
6864 of the containing type, but the vtable pointer is
6865 always named __vfp. */
6866
6867 int i;
6868
6869 for (i = TYPE_NFIELDS (type) - 1;
6870 i >= TYPE_N_BASECLASSES (type);
6871 --i)
6872 {
6873 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6874 {
6875 TYPE_VPTR_FIELDNO (type) = i;
6876 TYPE_VPTR_BASETYPE (type) = type;
6877 break;
6878 }
6879 }
6880 }
c906108c 6881 }
98751a41
JK
6882
6883 /* Copy fi.typedef_field_list linked list elements content into the
6884 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6885 if (fi.typedef_field_list)
6886 {
6887 int i = fi.typedef_field_list_count;
6888
a0d7a4ff 6889 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6890 TYPE_TYPEDEF_FIELD_ARRAY (type)
6891 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6892 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6893
6894 /* Reverse the list order to keep the debug info elements order. */
6895 while (--i >= 0)
6896 {
6897 struct typedef_field *dest, *src;
6e70227d 6898
98751a41
JK
6899 dest = &TYPE_TYPEDEF_FIELD (type, i);
6900 src = &fi.typedef_field_list->field;
6901 fi.typedef_field_list = fi.typedef_field_list->next;
6902 *dest = *src;
6903 }
6904 }
c906108c 6905 }
63d06c5c 6906
0b92b5bb
TT
6907 quirk_gcc_member_function_pointer (type, cu->objfile);
6908
0114d602 6909 do_cleanups (back_to);
f792889a 6910 return type;
c906108c
SS
6911}
6912
134d01f1
DJ
6913static void
6914process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6915{
90aeadfc 6916 struct die_info *child_die = die->child;
f792889a 6917 struct type *this_type;
c906108c 6918
f792889a
DJ
6919 this_type = get_die_type (die, cu);
6920 if (this_type == NULL)
6921 this_type = read_structure_type (die, cu);
c906108c 6922
90aeadfc
DC
6923 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6924 snapshots) has been known to create a die giving a declaration
6925 for a class that has, as a child, a die giving a definition for a
6926 nested class. So we have to process our children even if the
6927 current die is a declaration. Normally, of course, a declaration
6928 won't have any children at all. */
134d01f1 6929
90aeadfc
DC
6930 while (child_die != NULL && child_die->tag)
6931 {
6932 if (child_die->tag == DW_TAG_member
6933 || child_die->tag == DW_TAG_variable
34eaf542
TT
6934 || child_die->tag == DW_TAG_inheritance
6935 || child_die->tag == DW_TAG_template_value_param
6936 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6937 {
90aeadfc 6938 /* Do nothing. */
134d01f1 6939 }
90aeadfc
DC
6940 else
6941 process_die (child_die, cu);
134d01f1 6942
90aeadfc 6943 child_die = sibling_die (child_die);
134d01f1
DJ
6944 }
6945
fa4028e9
JB
6946 /* Do not consider external references. According to the DWARF standard,
6947 these DIEs are identified by the fact that they have no byte_size
6948 attribute, and a declaration attribute. */
6949 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6950 || !die_is_declaration (die, cu))
f792889a 6951 new_symbol (die, this_type, cu);
134d01f1
DJ
6952}
6953
6954/* Given a DW_AT_enumeration_type die, set its type. We do not
6955 complete the type's fields yet, or create any symbols. */
c906108c 6956
f792889a 6957static struct type *
134d01f1 6958read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6959{
e7c27a73 6960 struct objfile *objfile = cu->objfile;
c906108c 6961 struct type *type;
c906108c 6962 struct attribute *attr;
0114d602 6963 const char *name;
134d01f1 6964
348e048f
DE
6965 /* If the definition of this type lives in .debug_types, read that type.
6966 Don't follow DW_AT_specification though, that will take us back up
6967 the chain and we want to go down. */
6968 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6969 if (attr)
6970 {
6971 struct dwarf2_cu *type_cu = cu;
6972 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6973
348e048f 6974 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6975
6976 /* TYPE_CU may not be the same as CU.
6977 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6978 return set_die_type (die, type, cu);
6979 }
6980
c906108c
SS
6981 type = alloc_type (objfile);
6982
6983 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6984 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6985 if (name != NULL)
0114d602 6986 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6987
e142c38c 6988 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6989 if (attr)
6990 {
6991 TYPE_LENGTH (type) = DW_UNSND (attr);
6992 }
6993 else
6994 {
6995 TYPE_LENGTH (type) = 0;
6996 }
6997
137033e9
JB
6998 /* The enumeration DIE can be incomplete. In Ada, any type can be
6999 declared as private in the package spec, and then defined only
7000 inside the package body. Such types are known as Taft Amendment
7001 Types. When another package uses such a type, an incomplete DIE
7002 may be generated by the compiler. */
02eb380e 7003 if (die_is_declaration (die, cu))
876cecd0 7004 TYPE_STUB (type) = 1;
02eb380e 7005
f792889a 7006 return set_die_type (die, type, cu);
134d01f1
DJ
7007}
7008
7009/* Given a pointer to a die which begins an enumeration, process all
7010 the dies that define the members of the enumeration, and create the
7011 symbol for the enumeration type.
7012
7013 NOTE: We reverse the order of the element list. */
7014
7015static void
7016process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7017{
f792889a 7018 struct type *this_type;
134d01f1 7019
f792889a
DJ
7020 this_type = get_die_type (die, cu);
7021 if (this_type == NULL)
7022 this_type = read_enumeration_type (die, cu);
9dc481d3 7023
639d11d3 7024 if (die->child != NULL)
c906108c 7025 {
9dc481d3
DE
7026 struct die_info *child_die;
7027 struct symbol *sym;
7028 struct field *fields = NULL;
7029 int num_fields = 0;
7030 int unsigned_enum = 1;
7031 char *name;
7032
639d11d3 7033 child_die = die->child;
c906108c
SS
7034 while (child_die && child_die->tag)
7035 {
7036 if (child_die->tag != DW_TAG_enumerator)
7037 {
e7c27a73 7038 process_die (child_die, cu);
c906108c
SS
7039 }
7040 else
7041 {
39cbfefa
DJ
7042 name = dwarf2_name (child_die, cu);
7043 if (name)
c906108c 7044 {
f792889a 7045 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7046 if (SYMBOL_VALUE (sym) < 0)
7047 unsigned_enum = 0;
7048
7049 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7050 {
7051 fields = (struct field *)
7052 xrealloc (fields,
7053 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7054 * sizeof (struct field));
c906108c
SS
7055 }
7056
3567439c 7057 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7058 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7059 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7060 FIELD_BITSIZE (fields[num_fields]) = 0;
7061
7062 num_fields++;
7063 }
7064 }
7065
7066 child_die = sibling_die (child_die);
7067 }
7068
7069 if (num_fields)
7070 {
f792889a
DJ
7071 TYPE_NFIELDS (this_type) = num_fields;
7072 TYPE_FIELDS (this_type) = (struct field *)
7073 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7074 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7075 sizeof (struct field) * num_fields);
b8c9b27d 7076 xfree (fields);
c906108c
SS
7077 }
7078 if (unsigned_enum)
876cecd0 7079 TYPE_UNSIGNED (this_type) = 1;
c906108c 7080 }
134d01f1 7081
f792889a 7082 new_symbol (die, this_type, cu);
c906108c
SS
7083}
7084
7085/* Extract all information from a DW_TAG_array_type DIE and put it in
7086 the DIE's type field. For now, this only handles one dimensional
7087 arrays. */
7088
f792889a 7089static struct type *
e7c27a73 7090read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7091{
e7c27a73 7092 struct objfile *objfile = cu->objfile;
c906108c 7093 struct die_info *child_die;
7e314c57 7094 struct type *type;
c906108c
SS
7095 struct type *element_type, *range_type, *index_type;
7096 struct type **range_types = NULL;
7097 struct attribute *attr;
7098 int ndim = 0;
7099 struct cleanup *back_to;
39cbfefa 7100 char *name;
c906108c 7101
e7c27a73 7102 element_type = die_type (die, cu);
c906108c 7103
7e314c57
JK
7104 /* The die_type call above may have already set the type for this DIE. */
7105 type = get_die_type (die, cu);
7106 if (type)
7107 return type;
7108
c906108c
SS
7109 /* Irix 6.2 native cc creates array types without children for
7110 arrays with unspecified length. */
639d11d3 7111 if (die->child == NULL)
c906108c 7112 {
46bf5051 7113 index_type = objfile_type (objfile)->builtin_int;
c906108c 7114 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7115 type = create_array_type (NULL, element_type, range_type);
7116 return set_die_type (die, type, cu);
c906108c
SS
7117 }
7118
7119 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7120 child_die = die->child;
c906108c
SS
7121 while (child_die && child_die->tag)
7122 {
7123 if (child_die->tag == DW_TAG_subrange_type)
7124 {
f792889a 7125 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7126
f792889a 7127 if (child_type != NULL)
a02abb62
JB
7128 {
7129 /* The range type was succesfully read. Save it for
7130 the array type creation. */
7131 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7132 {
7133 range_types = (struct type **)
7134 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7135 * sizeof (struct type *));
7136 if (ndim == 0)
7137 make_cleanup (free_current_contents, &range_types);
7138 }
f792889a 7139 range_types[ndim++] = child_type;
a02abb62 7140 }
c906108c
SS
7141 }
7142 child_die = sibling_die (child_die);
7143 }
7144
7145 /* Dwarf2 dimensions are output from left to right, create the
7146 necessary array types in backwards order. */
7ca2d3a3 7147
c906108c 7148 type = element_type;
7ca2d3a3
DL
7149
7150 if (read_array_order (die, cu) == DW_ORD_col_major)
7151 {
7152 int i = 0;
9a619af0 7153
7ca2d3a3
DL
7154 while (i < ndim)
7155 type = create_array_type (NULL, type, range_types[i++]);
7156 }
7157 else
7158 {
7159 while (ndim-- > 0)
7160 type = create_array_type (NULL, type, range_types[ndim]);
7161 }
c906108c 7162
f5f8a009
EZ
7163 /* Understand Dwarf2 support for vector types (like they occur on
7164 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7165 array type. This is not part of the Dwarf2/3 standard yet, but a
7166 custom vendor extension. The main difference between a regular
7167 array and the vector variant is that vectors are passed by value
7168 to functions. */
e142c38c 7169 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7170 if (attr)
ea37ba09 7171 make_vector_type (type);
f5f8a009 7172
39cbfefa
DJ
7173 name = dwarf2_name (die, cu);
7174 if (name)
7175 TYPE_NAME (type) = name;
6e70227d 7176
7e314c57
JK
7177 /* Install the type in the die. */
7178 set_die_type (die, type, cu);
7179
7180 /* set_die_type should be already done. */
b4ba55a1
JB
7181 set_descriptive_type (type, die, cu);
7182
c906108c
SS
7183 do_cleanups (back_to);
7184
7e314c57 7185 return type;
c906108c
SS
7186}
7187
7ca2d3a3 7188static enum dwarf_array_dim_ordering
6e70227d 7189read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7190{
7191 struct attribute *attr;
7192
7193 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7194
7195 if (attr) return DW_SND (attr);
7196
7197 /*
7198 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7199 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7200 laid out as per normal fortran.
7201
6e70227d 7202 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7203 version checking.
7204 */
7205
905e0470
PM
7206 if (cu->language == language_fortran
7207 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7208 {
7209 return DW_ORD_row_major;
7210 }
7211
6e70227d 7212 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7213 {
7214 case array_column_major:
7215 return DW_ORD_col_major;
7216 case array_row_major:
7217 default:
7218 return DW_ORD_row_major;
7219 };
7220}
7221
72019c9c
GM
7222/* Extract all information from a DW_TAG_set_type DIE and put it in
7223 the DIE's type field. */
7224
f792889a 7225static struct type *
72019c9c
GM
7226read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7227{
7e314c57
JK
7228 struct type *domain_type, *set_type;
7229 struct attribute *attr;
f792889a 7230
7e314c57
JK
7231 domain_type = die_type (die, cu);
7232
7233 /* The die_type call above may have already set the type for this DIE. */
7234 set_type = get_die_type (die, cu);
7235 if (set_type)
7236 return set_type;
7237
7238 set_type = create_set_type (NULL, domain_type);
7239
7240 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7241 if (attr)
7242 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7243
f792889a 7244 return set_die_type (die, set_type, cu);
72019c9c 7245}
7ca2d3a3 7246
c906108c
SS
7247/* First cut: install each common block member as a global variable. */
7248
7249static void
e7c27a73 7250read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7251{
7252 struct die_info *child_die;
7253 struct attribute *attr;
7254 struct symbol *sym;
7255 CORE_ADDR base = (CORE_ADDR) 0;
7256
e142c38c 7257 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7258 if (attr)
7259 {
8e19ed76
PS
7260 /* Support the .debug_loc offsets */
7261 if (attr_form_is_block (attr))
7262 {
e7c27a73 7263 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7264 }
3690dd37 7265 else if (attr_form_is_section_offset (attr))
8e19ed76 7266 {
4d3c2250 7267 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7268 }
7269 else
7270 {
4d3c2250
KB
7271 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7272 "common block member");
8e19ed76 7273 }
c906108c 7274 }
639d11d3 7275 if (die->child != NULL)
c906108c 7276 {
639d11d3 7277 child_die = die->child;
c906108c
SS
7278 while (child_die && child_die->tag)
7279 {
e7c27a73 7280 sym = new_symbol (child_die, NULL, cu);
e142c38c 7281 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
7282 if (attr)
7283 {
d4b96c9a
JK
7284 CORE_ADDR byte_offset = 0;
7285
7286 if (attr_form_is_section_offset (attr))
7287 dwarf2_complex_location_expr_complaint ();
7288 else if (attr_form_is_constant (attr))
7289 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7290 else if (attr_form_is_block (attr))
7291 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7292 else
7293 dwarf2_complex_location_expr_complaint ();
7294
7295 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7296 add_symbol_to_list (sym, &global_symbols);
7297 }
7298 child_die = sibling_die (child_die);
7299 }
7300 }
7301}
7302
0114d602 7303/* Create a type for a C++ namespace. */
d9fa45fe 7304
0114d602
DJ
7305static struct type *
7306read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7307{
e7c27a73 7308 struct objfile *objfile = cu->objfile;
0114d602 7309 const char *previous_prefix, *name;
9219021c 7310 int is_anonymous;
0114d602
DJ
7311 struct type *type;
7312
7313 /* For extensions, reuse the type of the original namespace. */
7314 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7315 {
7316 struct die_info *ext_die;
7317 struct dwarf2_cu *ext_cu = cu;
9a619af0 7318
0114d602
DJ
7319 ext_die = dwarf2_extension (die, &ext_cu);
7320 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7321
7322 /* EXT_CU may not be the same as CU.
7323 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7324 return set_die_type (die, type, cu);
7325 }
9219021c 7326
e142c38c 7327 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7328
7329 /* Now build the name of the current namespace. */
7330
0114d602
DJ
7331 previous_prefix = determine_prefix (die, cu);
7332 if (previous_prefix[0] != '\0')
7333 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7334 previous_prefix, name, 0, cu);
0114d602
DJ
7335
7336 /* Create the type. */
7337 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7338 objfile);
7339 TYPE_NAME (type) = (char *) name;
7340 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7341
60531b24 7342 return set_die_type (die, type, cu);
0114d602
DJ
7343}
7344
7345/* Read a C++ namespace. */
7346
7347static void
7348read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7349{
7350 struct objfile *objfile = cu->objfile;
7351 const char *name;
7352 int is_anonymous;
9219021c 7353
5c4e30ca
DC
7354 /* Add a symbol associated to this if we haven't seen the namespace
7355 before. Also, add a using directive if it's an anonymous
7356 namespace. */
9219021c 7357
f2f0e013 7358 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7359 {
7360 struct type *type;
7361
0114d602 7362 type = read_type_die (die, cu);
e7c27a73 7363 new_symbol (die, type, cu);
5c4e30ca 7364
0114d602 7365 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7366 if (is_anonymous)
0114d602
DJ
7367 {
7368 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7369
c0cc3a76 7370 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7371 NULL, &objfile->objfile_obstack);
0114d602 7372 }
5c4e30ca 7373 }
9219021c 7374
639d11d3 7375 if (die->child != NULL)
d9fa45fe 7376 {
639d11d3 7377 struct die_info *child_die = die->child;
6e70227d 7378
d9fa45fe
DC
7379 while (child_die && child_die->tag)
7380 {
e7c27a73 7381 process_die (child_die, cu);
d9fa45fe
DC
7382 child_die = sibling_die (child_die);
7383 }
7384 }
38d518c9
EZ
7385}
7386
f55ee35c
JK
7387/* Read a Fortran module as type. This DIE can be only a declaration used for
7388 imported module. Still we need that type as local Fortran "use ... only"
7389 declaration imports depend on the created type in determine_prefix. */
7390
7391static struct type *
7392read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7393{
7394 struct objfile *objfile = cu->objfile;
7395 char *module_name;
7396 struct type *type;
7397
7398 module_name = dwarf2_name (die, cu);
7399 if (!module_name)
7400 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7401 die->offset);
7402 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7403
7404 /* determine_prefix uses TYPE_TAG_NAME. */
7405 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7406
7407 return set_die_type (die, type, cu);
7408}
7409
5d7cb8df
JK
7410/* Read a Fortran module. */
7411
7412static void
7413read_module (struct die_info *die, struct dwarf2_cu *cu)
7414{
7415 struct die_info *child_die = die->child;
7416
5d7cb8df
JK
7417 while (child_die && child_die->tag)
7418 {
7419 process_die (child_die, cu);
7420 child_die = sibling_die (child_die);
7421 }
7422}
7423
38d518c9
EZ
7424/* Return the name of the namespace represented by DIE. Set
7425 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7426 namespace. */
7427
7428static const char *
e142c38c 7429namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7430{
7431 struct die_info *current_die;
7432 const char *name = NULL;
7433
7434 /* Loop through the extensions until we find a name. */
7435
7436 for (current_die = die;
7437 current_die != NULL;
f2f0e013 7438 current_die = dwarf2_extension (die, &cu))
38d518c9 7439 {
e142c38c 7440 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7441 if (name != NULL)
7442 break;
7443 }
7444
7445 /* Is it an anonymous namespace? */
7446
7447 *is_anonymous = (name == NULL);
7448 if (*is_anonymous)
7449 name = "(anonymous namespace)";
7450
7451 return name;
d9fa45fe
DC
7452}
7453
c906108c
SS
7454/* Extract all information from a DW_TAG_pointer_type DIE and add to
7455 the user defined type vector. */
7456
f792889a 7457static struct type *
e7c27a73 7458read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7459{
5e2b427d 7460 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7461 struct comp_unit_head *cu_header = &cu->header;
c906108c 7462 struct type *type;
8b2dbe47
KB
7463 struct attribute *attr_byte_size;
7464 struct attribute *attr_address_class;
7465 int byte_size, addr_class;
7e314c57
JK
7466 struct type *target_type;
7467
7468 target_type = die_type (die, cu);
c906108c 7469
7e314c57
JK
7470 /* The die_type call above may have already set the type for this DIE. */
7471 type = get_die_type (die, cu);
7472 if (type)
7473 return type;
7474
7475 type = lookup_pointer_type (target_type);
8b2dbe47 7476
e142c38c 7477 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7478 if (attr_byte_size)
7479 byte_size = DW_UNSND (attr_byte_size);
c906108c 7480 else
8b2dbe47
KB
7481 byte_size = cu_header->addr_size;
7482
e142c38c 7483 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7484 if (attr_address_class)
7485 addr_class = DW_UNSND (attr_address_class);
7486 else
7487 addr_class = DW_ADDR_none;
7488
7489 /* If the pointer size or address class is different than the
7490 default, create a type variant marked as such and set the
7491 length accordingly. */
7492 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7493 {
5e2b427d 7494 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7495 {
7496 int type_flags;
7497
849957d9 7498 type_flags = gdbarch_address_class_type_flags
5e2b427d 7499 (gdbarch, byte_size, addr_class);
876cecd0
TT
7500 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7501 == 0);
8b2dbe47
KB
7502 type = make_type_with_address_space (type, type_flags);
7503 }
7504 else if (TYPE_LENGTH (type) != byte_size)
7505 {
e2e0b3e5 7506 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7507 }
6e70227d 7508 else
9a619af0
MS
7509 {
7510 /* Should we also complain about unhandled address classes? */
7511 }
c906108c 7512 }
8b2dbe47
KB
7513
7514 TYPE_LENGTH (type) = byte_size;
f792889a 7515 return set_die_type (die, type, cu);
c906108c
SS
7516}
7517
7518/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7519 the user defined type vector. */
7520
f792889a 7521static struct type *
e7c27a73 7522read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7523{
7524 struct type *type;
7525 struct type *to_type;
7526 struct type *domain;
7527
e7c27a73
DJ
7528 to_type = die_type (die, cu);
7529 domain = die_containing_type (die, cu);
0d5de010 7530
7e314c57
JK
7531 /* The calls above may have already set the type for this DIE. */
7532 type = get_die_type (die, cu);
7533 if (type)
7534 return type;
7535
0d5de010
DJ
7536 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7537 type = lookup_methodptr_type (to_type);
7538 else
7539 type = lookup_memberptr_type (to_type, domain);
c906108c 7540
f792889a 7541 return set_die_type (die, type, cu);
c906108c
SS
7542}
7543
7544/* Extract all information from a DW_TAG_reference_type DIE and add to
7545 the user defined type vector. */
7546
f792889a 7547static struct type *
e7c27a73 7548read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7549{
e7c27a73 7550 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7551 struct type *type, *target_type;
c906108c
SS
7552 struct attribute *attr;
7553
7e314c57
JK
7554 target_type = die_type (die, cu);
7555
7556 /* The die_type call above may have already set the type for this DIE. */
7557 type = get_die_type (die, cu);
7558 if (type)
7559 return type;
7560
7561 type = lookup_reference_type (target_type);
e142c38c 7562 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7563 if (attr)
7564 {
7565 TYPE_LENGTH (type) = DW_UNSND (attr);
7566 }
7567 else
7568 {
107d2387 7569 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7570 }
f792889a 7571 return set_die_type (die, type, cu);
c906108c
SS
7572}
7573
f792889a 7574static struct type *
e7c27a73 7575read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7576{
f792889a 7577 struct type *base_type, *cv_type;
c906108c 7578
e7c27a73 7579 base_type = die_type (die, cu);
7e314c57
JK
7580
7581 /* The die_type call above may have already set the type for this DIE. */
7582 cv_type = get_die_type (die, cu);
7583 if (cv_type)
7584 return cv_type;
7585
f792889a
DJ
7586 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7587 return set_die_type (die, cv_type, cu);
c906108c
SS
7588}
7589
f792889a 7590static struct type *
e7c27a73 7591read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7592{
f792889a 7593 struct type *base_type, *cv_type;
c906108c 7594
e7c27a73 7595 base_type = die_type (die, cu);
7e314c57
JK
7596
7597 /* The die_type call above may have already set the type for this DIE. */
7598 cv_type = get_die_type (die, cu);
7599 if (cv_type)
7600 return cv_type;
7601
f792889a
DJ
7602 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7603 return set_die_type (die, cv_type, cu);
c906108c
SS
7604}
7605
7606/* Extract all information from a DW_TAG_string_type DIE and add to
7607 the user defined type vector. It isn't really a user defined type,
7608 but it behaves like one, with other DIE's using an AT_user_def_type
7609 attribute to reference it. */
7610
f792889a 7611static struct type *
e7c27a73 7612read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7613{
e7c27a73 7614 struct objfile *objfile = cu->objfile;
3b7538c0 7615 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7616 struct type *type, *range_type, *index_type, *char_type;
7617 struct attribute *attr;
7618 unsigned int length;
7619
e142c38c 7620 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7621 if (attr)
7622 {
7623 length = DW_UNSND (attr);
7624 }
7625 else
7626 {
b21b22e0 7627 /* check for the DW_AT_byte_size attribute */
e142c38c 7628 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7629 if (attr)
7630 {
7631 length = DW_UNSND (attr);
7632 }
7633 else
7634 {
7635 length = 1;
7636 }
c906108c 7637 }
6ccb9162 7638
46bf5051 7639 index_type = objfile_type (objfile)->builtin_int;
c906108c 7640 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7641 char_type = language_string_char_type (cu->language_defn, gdbarch);
7642 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7643
f792889a 7644 return set_die_type (die, type, cu);
c906108c
SS
7645}
7646
7647/* Handle DIES due to C code like:
7648
7649 struct foo
c5aa993b
JM
7650 {
7651 int (*funcp)(int a, long l);
7652 int b;
7653 };
c906108c
SS
7654
7655 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7656 */
c906108c 7657
f792889a 7658static struct type *
e7c27a73 7659read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7660{
7661 struct type *type; /* Type that this function returns */
7662 struct type *ftype; /* Function that returns above type */
7663 struct attribute *attr;
7664
e7c27a73 7665 type = die_type (die, cu);
7e314c57
JK
7666
7667 /* The die_type call above may have already set the type for this DIE. */
7668 ftype = get_die_type (die, cu);
7669 if (ftype)
7670 return ftype;
7671
0c8b41f1 7672 ftype = lookup_function_type (type);
c906108c 7673
5b8101ae 7674 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7675 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7676 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7677 || cu->language == language_cplus
5b8101ae
PM
7678 || cu->language == language_java
7679 || cu->language == language_pascal)
876cecd0 7680 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7681 else if (producer_is_realview (cu->producer))
7682 /* RealView does not emit DW_AT_prototyped. We can not
7683 distinguish prototyped and unprototyped functions; default to
7684 prototyped, since that is more common in modern code (and
7685 RealView warns about unprototyped functions). */
7686 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7687
c055b101
CV
7688 /* Store the calling convention in the type if it's available in
7689 the subroutine die. Otherwise set the calling convention to
7690 the default value DW_CC_normal. */
7691 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7692 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7693
7694 /* We need to add the subroutine type to the die immediately so
7695 we don't infinitely recurse when dealing with parameters
7696 declared as the same subroutine type. */
7697 set_die_type (die, ftype, cu);
6e70227d 7698
639d11d3 7699 if (die->child != NULL)
c906108c 7700 {
8072405b 7701 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7702 struct die_info *child_die;
8072405b 7703 int nparams, iparams;
c906108c
SS
7704
7705 /* Count the number of parameters.
7706 FIXME: GDB currently ignores vararg functions, but knows about
7707 vararg member functions. */
8072405b 7708 nparams = 0;
639d11d3 7709 child_die = die->child;
c906108c
SS
7710 while (child_die && child_die->tag)
7711 {
7712 if (child_die->tag == DW_TAG_formal_parameter)
7713 nparams++;
7714 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7715 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7716 child_die = sibling_die (child_die);
7717 }
7718
7719 /* Allocate storage for parameters and fill them in. */
7720 TYPE_NFIELDS (ftype) = nparams;
7721 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7722 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7723
8072405b
JK
7724 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7725 even if we error out during the parameters reading below. */
7726 for (iparams = 0; iparams < nparams; iparams++)
7727 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7728
7729 iparams = 0;
639d11d3 7730 child_die = die->child;
c906108c
SS
7731 while (child_die && child_die->tag)
7732 {
7733 if (child_die->tag == DW_TAG_formal_parameter)
7734 {
3ce3b1ba
PA
7735 struct type *arg_type;
7736
7737 /* DWARF version 2 has no clean way to discern C++
7738 static and non-static member functions. G++ helps
7739 GDB by marking the first parameter for non-static
7740 member functions (which is the this pointer) as
7741 artificial. We pass this information to
7742 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7743
7744 DWARF version 3 added DW_AT_object_pointer, which GCC
7745 4.5 does not yet generate. */
e142c38c 7746 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7747 if (attr)
7748 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7749 else
418835cc
KS
7750 {
7751 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7752
7753 /* GCC/43521: In java, the formal parameter
7754 "this" is sometimes not marked with DW_AT_artificial. */
7755 if (cu->language == language_java)
7756 {
7757 const char *name = dwarf2_name (child_die, cu);
9a619af0 7758
418835cc
KS
7759 if (name && !strcmp (name, "this"))
7760 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7761 }
7762 }
3ce3b1ba
PA
7763 arg_type = die_type (child_die, cu);
7764
7765 /* RealView does not mark THIS as const, which the testsuite
7766 expects. GCC marks THIS as const in method definitions,
7767 but not in the class specifications (GCC PR 43053). */
7768 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7769 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7770 {
7771 int is_this = 0;
7772 struct dwarf2_cu *arg_cu = cu;
7773 const char *name = dwarf2_name (child_die, cu);
7774
7775 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7776 if (attr)
7777 {
7778 /* If the compiler emits this, use it. */
7779 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7780 is_this = 1;
7781 }
7782 else if (name && strcmp (name, "this") == 0)
7783 /* Function definitions will have the argument names. */
7784 is_this = 1;
7785 else if (name == NULL && iparams == 0)
7786 /* Declarations may not have the names, so like
7787 elsewhere in GDB, assume an artificial first
7788 argument is "this". */
7789 is_this = 1;
7790
7791 if (is_this)
7792 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7793 arg_type, 0);
7794 }
7795
7796 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7797 iparams++;
7798 }
7799 child_die = sibling_die (child_die);
7800 }
7801 }
7802
76c10ea2 7803 return ftype;
c906108c
SS
7804}
7805
f792889a 7806static struct type *
e7c27a73 7807read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7808{
e7c27a73 7809 struct objfile *objfile = cu->objfile;
0114d602 7810 const char *name = NULL;
f792889a 7811 struct type *this_type;
c906108c 7812
94af9270 7813 name = dwarf2_full_name (NULL, die, cu);
f792889a 7814 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7815 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7816 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7817 set_die_type (die, this_type, cu);
7818 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7819 return this_type;
c906108c
SS
7820}
7821
7822/* Find a representation of a given base type and install
7823 it in the TYPE field of the die. */
7824
f792889a 7825static struct type *
e7c27a73 7826read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7827{
e7c27a73 7828 struct objfile *objfile = cu->objfile;
c906108c
SS
7829 struct type *type;
7830 struct attribute *attr;
7831 int encoding = 0, size = 0;
39cbfefa 7832 char *name;
6ccb9162
UW
7833 enum type_code code = TYPE_CODE_INT;
7834 int type_flags = 0;
7835 struct type *target_type = NULL;
c906108c 7836
e142c38c 7837 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7838 if (attr)
7839 {
7840 encoding = DW_UNSND (attr);
7841 }
e142c38c 7842 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7843 if (attr)
7844 {
7845 size = DW_UNSND (attr);
7846 }
39cbfefa 7847 name = dwarf2_name (die, cu);
6ccb9162 7848 if (!name)
c906108c 7849 {
6ccb9162
UW
7850 complaint (&symfile_complaints,
7851 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7852 }
6ccb9162
UW
7853
7854 switch (encoding)
c906108c 7855 {
6ccb9162
UW
7856 case DW_ATE_address:
7857 /* Turn DW_ATE_address into a void * pointer. */
7858 code = TYPE_CODE_PTR;
7859 type_flags |= TYPE_FLAG_UNSIGNED;
7860 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7861 break;
7862 case DW_ATE_boolean:
7863 code = TYPE_CODE_BOOL;
7864 type_flags |= TYPE_FLAG_UNSIGNED;
7865 break;
7866 case DW_ATE_complex_float:
7867 code = TYPE_CODE_COMPLEX;
7868 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7869 break;
7870 case DW_ATE_decimal_float:
7871 code = TYPE_CODE_DECFLOAT;
7872 break;
7873 case DW_ATE_float:
7874 code = TYPE_CODE_FLT;
7875 break;
7876 case DW_ATE_signed:
7877 break;
7878 case DW_ATE_unsigned:
7879 type_flags |= TYPE_FLAG_UNSIGNED;
7880 break;
7881 case DW_ATE_signed_char:
6e70227d 7882 if (cu->language == language_ada || cu->language == language_m2
868a0084 7883 || cu->language == language_pascal)
6ccb9162
UW
7884 code = TYPE_CODE_CHAR;
7885 break;
7886 case DW_ATE_unsigned_char:
868a0084
PM
7887 if (cu->language == language_ada || cu->language == language_m2
7888 || cu->language == language_pascal)
6ccb9162
UW
7889 code = TYPE_CODE_CHAR;
7890 type_flags |= TYPE_FLAG_UNSIGNED;
7891 break;
75079b2b
TT
7892 case DW_ATE_UTF:
7893 /* We just treat this as an integer and then recognize the
7894 type by name elsewhere. */
7895 break;
7896
6ccb9162
UW
7897 default:
7898 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7899 dwarf_type_encoding_name (encoding));
7900 break;
c906108c 7901 }
6ccb9162 7902
0114d602
DJ
7903 type = init_type (code, size, type_flags, NULL, objfile);
7904 TYPE_NAME (type) = name;
6ccb9162
UW
7905 TYPE_TARGET_TYPE (type) = target_type;
7906
0114d602 7907 if (name && strcmp (name, "char") == 0)
876cecd0 7908 TYPE_NOSIGN (type) = 1;
0114d602 7909
f792889a 7910 return set_die_type (die, type, cu);
c906108c
SS
7911}
7912
a02abb62
JB
7913/* Read the given DW_AT_subrange DIE. */
7914
f792889a 7915static struct type *
a02abb62
JB
7916read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7917{
5e2b427d 7918 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7919 struct type *base_type;
7920 struct type *range_type;
7921 struct attribute *attr;
43bbcdc2
PH
7922 LONGEST low = 0;
7923 LONGEST high = -1;
39cbfefa 7924 char *name;
43bbcdc2 7925 LONGEST negative_mask;
e77813c8 7926
a02abb62 7927 base_type = die_type (die, cu);
953ac07e
JK
7928 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7929 check_typedef (base_type);
a02abb62 7930
7e314c57
JK
7931 /* The die_type call above may have already set the type for this DIE. */
7932 range_type = get_die_type (die, cu);
7933 if (range_type)
7934 return range_type;
7935
e142c38c 7936 if (cu->language == language_fortran)
6e70227d 7937 {
a02abb62
JB
7938 /* FORTRAN implies a lower bound of 1, if not given. */
7939 low = 1;
7940 }
7941
dd5e6932
DJ
7942 /* FIXME: For variable sized arrays either of these could be
7943 a variable rather than a constant value. We'll allow it,
7944 but we don't know how to handle it. */
e142c38c 7945 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7946 if (attr)
7947 low = dwarf2_get_attr_constant_value (attr, 0);
7948
e142c38c 7949 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7950 if (attr)
6e70227d 7951 {
e77813c8 7952 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7953 {
7954 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7955 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7956 FIXME: GDB does not yet know how to handle dynamic
7957 arrays properly, treat them as arrays with unspecified
7958 length for now.
7959
7960 FIXME: jimb/2003-09-22: GDB does not really know
7961 how to handle arrays of unspecified length
7962 either; we just represent them as zero-length
7963 arrays. Choose an appropriate upper bound given
7964 the lower bound we've computed above. */
7965 high = low - 1;
7966 }
7967 else
7968 high = dwarf2_get_attr_constant_value (attr, 1);
7969 }
e77813c8
PM
7970 else
7971 {
7972 attr = dwarf2_attr (die, DW_AT_count, cu);
7973 if (attr)
7974 {
7975 int count = dwarf2_get_attr_constant_value (attr, 1);
7976 high = low + count - 1;
7977 }
7978 }
7979
7980 /* Dwarf-2 specifications explicitly allows to create subrange types
7981 without specifying a base type.
7982 In that case, the base type must be set to the type of
7983 the lower bound, upper bound or count, in that order, if any of these
7984 three attributes references an object that has a type.
7985 If no base type is found, the Dwarf-2 specifications say that
7986 a signed integer type of size equal to the size of an address should
7987 be used.
7988 For the following C code: `extern char gdb_int [];'
7989 GCC produces an empty range DIE.
7990 FIXME: muller/2010-05-28: Possible references to object for low bound,
7991 high bound or count are not yet handled by this code.
7992 */
7993 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7994 {
7995 struct objfile *objfile = cu->objfile;
7996 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7997 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7998 struct type *int_type = objfile_type (objfile)->builtin_int;
7999
8000 /* Test "int", "long int", and "long long int" objfile types,
8001 and select the first one having a size above or equal to the
8002 architecture address size. */
8003 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8004 base_type = int_type;
8005 else
8006 {
8007 int_type = objfile_type (objfile)->builtin_long;
8008 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8009 base_type = int_type;
8010 else
8011 {
8012 int_type = objfile_type (objfile)->builtin_long_long;
8013 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8014 base_type = int_type;
8015 }
8016 }
8017 }
a02abb62 8018
6e70227d 8019 negative_mask =
43bbcdc2
PH
8020 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8021 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8022 low |= negative_mask;
8023 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8024 high |= negative_mask;
8025
a02abb62
JB
8026 range_type = create_range_type (NULL, base_type, low, high);
8027
bbb0eef6
JK
8028 /* Mark arrays with dynamic length at least as an array of unspecified
8029 length. GDB could check the boundary but before it gets implemented at
8030 least allow accessing the array elements. */
8031 if (attr && attr->form == DW_FORM_block1)
8032 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8033
39cbfefa
DJ
8034 name = dwarf2_name (die, cu);
8035 if (name)
8036 TYPE_NAME (range_type) = name;
6e70227d 8037
e142c38c 8038 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8039 if (attr)
8040 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8041
7e314c57
JK
8042 set_die_type (die, range_type, cu);
8043
8044 /* set_die_type should be already done. */
b4ba55a1
JB
8045 set_descriptive_type (range_type, die, cu);
8046
7e314c57 8047 return range_type;
a02abb62 8048}
6e70227d 8049
f792889a 8050static struct type *
81a17f79
JB
8051read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8052{
8053 struct type *type;
81a17f79 8054
81a17f79
JB
8055 /* For now, we only support the C meaning of an unspecified type: void. */
8056
0114d602
DJ
8057 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8058 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8059
f792889a 8060 return set_die_type (die, type, cu);
81a17f79 8061}
a02abb62 8062
51545339
DJ
8063/* Trivial hash function for die_info: the hash value of a DIE
8064 is its offset in .debug_info for this objfile. */
8065
8066static hashval_t
8067die_hash (const void *item)
8068{
8069 const struct die_info *die = item;
9a619af0 8070
51545339
DJ
8071 return die->offset;
8072}
8073
8074/* Trivial comparison function for die_info structures: two DIEs
8075 are equal if they have the same offset. */
8076
8077static int
8078die_eq (const void *item_lhs, const void *item_rhs)
8079{
8080 const struct die_info *die_lhs = item_lhs;
8081 const struct die_info *die_rhs = item_rhs;
9a619af0 8082
51545339
DJ
8083 return die_lhs->offset == die_rhs->offset;
8084}
8085
c906108c
SS
8086/* Read a whole compilation unit into a linked list of dies. */
8087
f9aca02d 8088static struct die_info *
93311388 8089read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8090{
93311388 8091 struct die_reader_specs reader_specs;
98bfdba5 8092 int read_abbrevs = 0;
1d9ec526 8093 struct cleanup *back_to = NULL;
98bfdba5
PA
8094 struct die_info *die;
8095
8096 if (cu->dwarf2_abbrevs == NULL)
8097 {
8098 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8099 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8100 read_abbrevs = 1;
8101 }
93311388 8102
348e048f 8103 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8104 cu->die_hash
8105 = htab_create_alloc_ex (cu->header.length / 12,
8106 die_hash,
8107 die_eq,
8108 NULL,
8109 &cu->comp_unit_obstack,
8110 hashtab_obstack_allocate,
8111 dummy_obstack_deallocate);
8112
93311388
DE
8113 init_cu_die_reader (&reader_specs, cu);
8114
98bfdba5
PA
8115 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8116
8117 if (read_abbrevs)
8118 do_cleanups (back_to);
8119
8120 return die;
639d11d3
DC
8121}
8122
d97bc12b
DE
8123/* Main entry point for reading a DIE and all children.
8124 Read the DIE and dump it if requested. */
8125
8126static struct die_info *
93311388
DE
8127read_die_and_children (const struct die_reader_specs *reader,
8128 gdb_byte *info_ptr,
d97bc12b
DE
8129 gdb_byte **new_info_ptr,
8130 struct die_info *parent)
8131{
93311388 8132 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8133 new_info_ptr, parent);
8134
8135 if (dwarf2_die_debug)
8136 {
348e048f
DE
8137 fprintf_unfiltered (gdb_stdlog,
8138 "\nRead die from %s of %s:\n",
8139 reader->buffer == dwarf2_per_objfile->info.buffer
8140 ? ".debug_info"
8141 : reader->buffer == dwarf2_per_objfile->types.buffer
8142 ? ".debug_types"
8143 : "unknown section",
8144 reader->abfd->filename);
d97bc12b
DE
8145 dump_die (result, dwarf2_die_debug);
8146 }
8147
8148 return result;
8149}
8150
639d11d3
DC
8151/* Read a single die and all its descendents. Set the die's sibling
8152 field to NULL; set other fields in the die correctly, and set all
8153 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8154 location of the info_ptr after reading all of those dies. PARENT
8155 is the parent of the die in question. */
8156
8157static struct die_info *
93311388
DE
8158read_die_and_children_1 (const struct die_reader_specs *reader,
8159 gdb_byte *info_ptr,
d97bc12b
DE
8160 gdb_byte **new_info_ptr,
8161 struct die_info *parent)
639d11d3
DC
8162{
8163 struct die_info *die;
fe1b8b76 8164 gdb_byte *cur_ptr;
639d11d3
DC
8165 int has_children;
8166
93311388 8167 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8168 if (die == NULL)
8169 {
8170 *new_info_ptr = cur_ptr;
8171 return NULL;
8172 }
93311388 8173 store_in_ref_table (die, reader->cu);
639d11d3
DC
8174
8175 if (has_children)
348e048f 8176 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8177 else
8178 {
8179 die->child = NULL;
8180 *new_info_ptr = cur_ptr;
8181 }
8182
8183 die->sibling = NULL;
8184 die->parent = parent;
8185 return die;
8186}
8187
8188/* Read a die, all of its descendents, and all of its siblings; set
8189 all of the fields of all of the dies correctly. Arguments are as
8190 in read_die_and_children. */
8191
8192static struct die_info *
93311388
DE
8193read_die_and_siblings (const struct die_reader_specs *reader,
8194 gdb_byte *info_ptr,
fe1b8b76 8195 gdb_byte **new_info_ptr,
639d11d3
DC
8196 struct die_info *parent)
8197{
8198 struct die_info *first_die, *last_sibling;
fe1b8b76 8199 gdb_byte *cur_ptr;
639d11d3 8200
c906108c 8201 cur_ptr = info_ptr;
639d11d3
DC
8202 first_die = last_sibling = NULL;
8203
8204 while (1)
c906108c 8205 {
639d11d3 8206 struct die_info *die
93311388 8207 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8208
1d325ec1 8209 if (die == NULL)
c906108c 8210 {
639d11d3
DC
8211 *new_info_ptr = cur_ptr;
8212 return first_die;
c906108c 8213 }
1d325ec1
DJ
8214
8215 if (!first_die)
8216 first_die = die;
c906108c 8217 else
1d325ec1
DJ
8218 last_sibling->sibling = die;
8219
8220 last_sibling = die;
c906108c 8221 }
c906108c
SS
8222}
8223
93311388
DE
8224/* Read the die from the .debug_info section buffer. Set DIEP to
8225 point to a newly allocated die with its information, except for its
8226 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8227 whether the die has children or not. */
8228
8229static gdb_byte *
8230read_full_die (const struct die_reader_specs *reader,
8231 struct die_info **diep, gdb_byte *info_ptr,
8232 int *has_children)
8233{
8234 unsigned int abbrev_number, bytes_read, i, offset;
8235 struct abbrev_info *abbrev;
8236 struct die_info *die;
8237 struct dwarf2_cu *cu = reader->cu;
8238 bfd *abfd = reader->abfd;
8239
8240 offset = info_ptr - reader->buffer;
8241 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8242 info_ptr += bytes_read;
8243 if (!abbrev_number)
8244 {
8245 *diep = NULL;
8246 *has_children = 0;
8247 return info_ptr;
8248 }
8249
8250 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8251 if (!abbrev)
348e048f
DE
8252 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8253 abbrev_number,
8254 bfd_get_filename (abfd));
8255
93311388
DE
8256 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8257 die->offset = offset;
8258 die->tag = abbrev->tag;
8259 die->abbrev = abbrev_number;
8260
8261 die->num_attrs = abbrev->num_attrs;
8262
8263 for (i = 0; i < abbrev->num_attrs; ++i)
8264 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8265 abfd, info_ptr, cu);
8266
8267 *diep = die;
8268 *has_children = abbrev->has_children;
8269 return info_ptr;
8270}
8271
c906108c
SS
8272/* In DWARF version 2, the description of the debugging information is
8273 stored in a separate .debug_abbrev section. Before we read any
8274 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8275 in a hash table. This function also sets flags in CU describing
8276 the data found in the abbrev table. */
c906108c
SS
8277
8278static void
e7c27a73 8279dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8280{
e7c27a73 8281 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8282 gdb_byte *abbrev_ptr;
c906108c
SS
8283 struct abbrev_info *cur_abbrev;
8284 unsigned int abbrev_number, bytes_read, abbrev_name;
8285 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8286 struct attr_abbrev *cur_attrs;
8287 unsigned int allocated_attrs;
c906108c 8288
57349743 8289 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8290 obstack_init (&cu->abbrev_obstack);
8291 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8292 (ABBREV_HASH_SIZE
8293 * sizeof (struct abbrev_info *)));
8294 memset (cu->dwarf2_abbrevs, 0,
8295 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8296
be391dca
TT
8297 dwarf2_read_section (dwarf2_per_objfile->objfile,
8298 &dwarf2_per_objfile->abbrev);
dce234bc 8299 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8300 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8301 abbrev_ptr += bytes_read;
8302
f3dd6933
DJ
8303 allocated_attrs = ATTR_ALLOC_CHUNK;
8304 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8305
c906108c
SS
8306 /* loop until we reach an abbrev number of 0 */
8307 while (abbrev_number)
8308 {
f3dd6933 8309 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8310
8311 /* read in abbrev header */
8312 cur_abbrev->number = abbrev_number;
8313 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8314 abbrev_ptr += bytes_read;
8315 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8316 abbrev_ptr += 1;
8317
72bf9492
DJ
8318 if (cur_abbrev->tag == DW_TAG_namespace)
8319 cu->has_namespace_info = 1;
8320
c906108c
SS
8321 /* now read in declarations */
8322 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8323 abbrev_ptr += bytes_read;
8324 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8325 abbrev_ptr += bytes_read;
8326 while (abbrev_name)
8327 {
f3dd6933 8328 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8329 {
f3dd6933
DJ
8330 allocated_attrs += ATTR_ALLOC_CHUNK;
8331 cur_attrs
8332 = xrealloc (cur_attrs, (allocated_attrs
8333 * sizeof (struct attr_abbrev)));
c906108c 8334 }
ae038cb0
DJ
8335
8336 /* Record whether this compilation unit might have
8337 inter-compilation-unit references. If we don't know what form
8338 this attribute will have, then it might potentially be a
8339 DW_FORM_ref_addr, so we conservatively expect inter-CU
8340 references. */
8341
8342 if (abbrev_form == DW_FORM_ref_addr
8343 || abbrev_form == DW_FORM_indirect)
8344 cu->has_form_ref_addr = 1;
8345
f3dd6933
DJ
8346 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8347 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8348 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8349 abbrev_ptr += bytes_read;
8350 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8351 abbrev_ptr += bytes_read;
8352 }
8353
f3dd6933
DJ
8354 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8355 (cur_abbrev->num_attrs
8356 * sizeof (struct attr_abbrev)));
8357 memcpy (cur_abbrev->attrs, cur_attrs,
8358 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8359
c906108c 8360 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8361 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8362 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8363
8364 /* Get next abbreviation.
8365 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8366 always properly terminated with an abbrev number of 0.
8367 Exit loop if we encounter an abbreviation which we have
8368 already read (which means we are about to read the abbreviations
8369 for the next compile unit) or if the end of the abbreviation
8370 table is reached. */
dce234bc
PP
8371 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8372 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8373 break;
8374 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8375 abbrev_ptr += bytes_read;
e7c27a73 8376 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8377 break;
8378 }
f3dd6933
DJ
8379
8380 xfree (cur_attrs);
c906108c
SS
8381}
8382
f3dd6933 8383/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8384
c906108c 8385static void
f3dd6933 8386dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8387{
f3dd6933 8388 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8389
f3dd6933
DJ
8390 obstack_free (&cu->abbrev_obstack, NULL);
8391 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8392}
8393
8394/* Lookup an abbrev_info structure in the abbrev hash table. */
8395
8396static struct abbrev_info *
e7c27a73 8397dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8398{
8399 unsigned int hash_number;
8400 struct abbrev_info *abbrev;
8401
8402 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8403 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8404
8405 while (abbrev)
8406 {
8407 if (abbrev->number == number)
8408 return abbrev;
8409 else
8410 abbrev = abbrev->next;
8411 }
8412 return NULL;
8413}
8414
72bf9492
DJ
8415/* Returns nonzero if TAG represents a type that we might generate a partial
8416 symbol for. */
8417
8418static int
8419is_type_tag_for_partial (int tag)
8420{
8421 switch (tag)
8422 {
8423#if 0
8424 /* Some types that would be reasonable to generate partial symbols for,
8425 that we don't at present. */
8426 case DW_TAG_array_type:
8427 case DW_TAG_file_type:
8428 case DW_TAG_ptr_to_member_type:
8429 case DW_TAG_set_type:
8430 case DW_TAG_string_type:
8431 case DW_TAG_subroutine_type:
8432#endif
8433 case DW_TAG_base_type:
8434 case DW_TAG_class_type:
680b30c7 8435 case DW_TAG_interface_type:
72bf9492
DJ
8436 case DW_TAG_enumeration_type:
8437 case DW_TAG_structure_type:
8438 case DW_TAG_subrange_type:
8439 case DW_TAG_typedef:
8440 case DW_TAG_union_type:
8441 return 1;
8442 default:
8443 return 0;
8444 }
8445}
8446
8447/* Load all DIEs that are interesting for partial symbols into memory. */
8448
8449static struct partial_die_info *
93311388
DE
8450load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8451 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8452{
8453 struct partial_die_info *part_die;
8454 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8455 struct abbrev_info *abbrev;
8456 unsigned int bytes_read;
5afb4e99 8457 unsigned int load_all = 0;
72bf9492
DJ
8458
8459 int nesting_level = 1;
8460
8461 parent_die = NULL;
8462 last_die = NULL;
8463
5afb4e99
DJ
8464 if (cu->per_cu && cu->per_cu->load_all_dies)
8465 load_all = 1;
8466
72bf9492
DJ
8467 cu->partial_dies
8468 = htab_create_alloc_ex (cu->header.length / 12,
8469 partial_die_hash,
8470 partial_die_eq,
8471 NULL,
8472 &cu->comp_unit_obstack,
8473 hashtab_obstack_allocate,
8474 dummy_obstack_deallocate);
8475
8476 part_die = obstack_alloc (&cu->comp_unit_obstack,
8477 sizeof (struct partial_die_info));
8478
8479 while (1)
8480 {
8481 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8482
8483 /* A NULL abbrev means the end of a series of children. */
8484 if (abbrev == NULL)
8485 {
8486 if (--nesting_level == 0)
8487 {
8488 /* PART_DIE was probably the last thing allocated on the
8489 comp_unit_obstack, so we could call obstack_free
8490 here. We don't do that because the waste is small,
8491 and will be cleaned up when we're done with this
8492 compilation unit. This way, we're also more robust
8493 against other users of the comp_unit_obstack. */
8494 return first_die;
8495 }
8496 info_ptr += bytes_read;
8497 last_die = parent_die;
8498 parent_die = parent_die->die_parent;
8499 continue;
8500 }
8501
98bfdba5
PA
8502 /* Check for template arguments. We never save these; if
8503 they're seen, we just mark the parent, and go on our way. */
8504 if (parent_die != NULL
8505 && cu->language == language_cplus
8506 && (abbrev->tag == DW_TAG_template_type_param
8507 || abbrev->tag == DW_TAG_template_value_param))
8508 {
8509 parent_die->has_template_arguments = 1;
8510
8511 if (!load_all)
8512 {
8513 /* We don't need a partial DIE for the template argument. */
8514 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8515 cu);
8516 continue;
8517 }
8518 }
8519
8520 /* We only recurse into subprograms looking for template arguments.
8521 Skip their other children. */
8522 if (!load_all
8523 && cu->language == language_cplus
8524 && parent_die != NULL
8525 && parent_die->tag == DW_TAG_subprogram)
8526 {
8527 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8528 continue;
8529 }
8530
5afb4e99
DJ
8531 /* Check whether this DIE is interesting enough to save. Normally
8532 we would not be interested in members here, but there may be
8533 later variables referencing them via DW_AT_specification (for
8534 static members). */
8535 if (!load_all
8536 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8537 && abbrev->tag != DW_TAG_enumerator
8538 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8539 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8540 && abbrev->tag != DW_TAG_variable
5afb4e99 8541 && abbrev->tag != DW_TAG_namespace
f55ee35c 8542 && abbrev->tag != DW_TAG_module
5afb4e99 8543 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8544 {
8545 /* Otherwise we skip to the next sibling, if any. */
93311388 8546 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8547 continue;
8548 }
8549
93311388
DE
8550 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8551 buffer, info_ptr, cu);
72bf9492
DJ
8552
8553 /* This two-pass algorithm for processing partial symbols has a
8554 high cost in cache pressure. Thus, handle some simple cases
8555 here which cover the majority of C partial symbols. DIEs
8556 which neither have specification tags in them, nor could have
8557 specification tags elsewhere pointing at them, can simply be
8558 processed and discarded.
8559
8560 This segment is also optional; scan_partial_symbols and
8561 add_partial_symbol will handle these DIEs if we chain
8562 them in normally. When compilers which do not emit large
8563 quantities of duplicate debug information are more common,
8564 this code can probably be removed. */
8565
8566 /* Any complete simple types at the top level (pretty much all
8567 of them, for a language without namespaces), can be processed
8568 directly. */
8569 if (parent_die == NULL
8570 && part_die->has_specification == 0
8571 && part_die->is_declaration == 0
8572 && (part_die->tag == DW_TAG_typedef
8573 || part_die->tag == DW_TAG_base_type
8574 || part_die->tag == DW_TAG_subrange_type))
8575 {
8576 if (building_psymtab && part_die->name != NULL)
04a679b8 8577 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8578 VAR_DOMAIN, LOC_TYPEDEF,
8579 &cu->objfile->static_psymbols,
8580 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8581 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8582 continue;
8583 }
8584
8585 /* If we're at the second level, and we're an enumerator, and
8586 our parent has no specification (meaning possibly lives in a
8587 namespace elsewhere), then we can add the partial symbol now
8588 instead of queueing it. */
8589 if (part_die->tag == DW_TAG_enumerator
8590 && parent_die != NULL
8591 && parent_die->die_parent == NULL
8592 && parent_die->tag == DW_TAG_enumeration_type
8593 && parent_die->has_specification == 0)
8594 {
8595 if (part_die->name == NULL)
e2e0b3e5 8596 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8597 else if (building_psymtab)
04a679b8 8598 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8599 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8600 (cu->language == language_cplus
8601 || cu->language == language_java)
72bf9492
DJ
8602 ? &cu->objfile->global_psymbols
8603 : &cu->objfile->static_psymbols,
8604 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8605
93311388 8606 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8607 continue;
8608 }
8609
8610 /* We'll save this DIE so link it in. */
8611 part_die->die_parent = parent_die;
8612 part_die->die_sibling = NULL;
8613 part_die->die_child = NULL;
8614
8615 if (last_die && last_die == parent_die)
8616 last_die->die_child = part_die;
8617 else if (last_die)
8618 last_die->die_sibling = part_die;
8619
8620 last_die = part_die;
8621
8622 if (first_die == NULL)
8623 first_die = part_die;
8624
8625 /* Maybe add the DIE to the hash table. Not all DIEs that we
8626 find interesting need to be in the hash table, because we
8627 also have the parent/sibling/child chains; only those that we
8628 might refer to by offset later during partial symbol reading.
8629
8630 For now this means things that might have be the target of a
8631 DW_AT_specification, DW_AT_abstract_origin, or
8632 DW_AT_extension. DW_AT_extension will refer only to
8633 namespaces; DW_AT_abstract_origin refers to functions (and
8634 many things under the function DIE, but we do not recurse
8635 into function DIEs during partial symbol reading) and
8636 possibly variables as well; DW_AT_specification refers to
8637 declarations. Declarations ought to have the DW_AT_declaration
8638 flag. It happens that GCC forgets to put it in sometimes, but
8639 only for functions, not for types.
8640
8641 Adding more things than necessary to the hash table is harmless
8642 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8643 wasted time in find_partial_die, when we reread the compilation
8644 unit with load_all_dies set. */
72bf9492 8645
5afb4e99
DJ
8646 if (load_all
8647 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8648 || abbrev->tag == DW_TAG_variable
8649 || abbrev->tag == DW_TAG_namespace
8650 || part_die->is_declaration)
8651 {
8652 void **slot;
8653
8654 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8655 part_die->offset, INSERT);
8656 *slot = part_die;
8657 }
8658
8659 part_die = obstack_alloc (&cu->comp_unit_obstack,
8660 sizeof (struct partial_die_info));
8661
8662 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8663 we have no reason to follow the children of structures; for other
98bfdba5
PA
8664 languages we have to, so that we can get at method physnames
8665 to infer fully qualified class names, for DW_AT_specification,
8666 and for C++ template arguments. For C++, we also look one level
8667 inside functions to find template arguments (if the name of the
8668 function does not already contain the template arguments).
bc30ff58
JB
8669
8670 For Ada, we need to scan the children of subprograms and lexical
8671 blocks as well because Ada allows the definition of nested
8672 entities that could be interesting for the debugger, such as
8673 nested subprograms for instance. */
72bf9492 8674 if (last_die->has_children
5afb4e99
DJ
8675 && (load_all
8676 || last_die->tag == DW_TAG_namespace
f55ee35c 8677 || last_die->tag == DW_TAG_module
72bf9492 8678 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8679 || (cu->language == language_cplus
8680 && last_die->tag == DW_TAG_subprogram
8681 && (last_die->name == NULL
8682 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8683 || (cu->language != language_c
8684 && (last_die->tag == DW_TAG_class_type
680b30c7 8685 || last_die->tag == DW_TAG_interface_type
72bf9492 8686 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8687 || last_die->tag == DW_TAG_union_type))
8688 || (cu->language == language_ada
8689 && (last_die->tag == DW_TAG_subprogram
8690 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8691 {
8692 nesting_level++;
8693 parent_die = last_die;
8694 continue;
8695 }
8696
8697 /* Otherwise we skip to the next sibling, if any. */
93311388 8698 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8699
8700 /* Back to the top, do it again. */
8701 }
8702}
8703
c906108c
SS
8704/* Read a minimal amount of information into the minimal die structure. */
8705
fe1b8b76 8706static gdb_byte *
72bf9492
DJ
8707read_partial_die (struct partial_die_info *part_die,
8708 struct abbrev_info *abbrev,
8709 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8710 gdb_byte *buffer, gdb_byte *info_ptr,
8711 struct dwarf2_cu *cu)
c906108c 8712{
fa238c03 8713 unsigned int i;
c906108c 8714 struct attribute attr;
c5aa993b 8715 int has_low_pc_attr = 0;
c906108c
SS
8716 int has_high_pc_attr = 0;
8717
72bf9492 8718 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8719
93311388 8720 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8721
8722 info_ptr += abbrev_len;
8723
8724 if (abbrev == NULL)
8725 return info_ptr;
8726
c906108c
SS
8727 part_die->tag = abbrev->tag;
8728 part_die->has_children = abbrev->has_children;
c906108c
SS
8729
8730 for (i = 0; i < abbrev->num_attrs; ++i)
8731 {
e7c27a73 8732 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8733
8734 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8735 partial symbol table. */
c906108c
SS
8736 switch (attr.name)
8737 {
8738 case DW_AT_name:
71c25dea
TT
8739 switch (part_die->tag)
8740 {
8741 case DW_TAG_compile_unit:
348e048f 8742 case DW_TAG_type_unit:
71c25dea
TT
8743 /* Compilation units have a DW_AT_name that is a filename, not
8744 a source language identifier. */
8745 case DW_TAG_enumeration_type:
8746 case DW_TAG_enumerator:
8747 /* These tags always have simple identifiers already; no need
8748 to canonicalize them. */
8749 part_die->name = DW_STRING (&attr);
8750 break;
8751 default:
8752 part_die->name
8753 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8754 &cu->objfile->objfile_obstack);
71c25dea
TT
8755 break;
8756 }
c906108c 8757 break;
31ef98ae 8758 case DW_AT_linkage_name:
c906108c 8759 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8760 /* Note that both forms of linkage name might appear. We
8761 assume they will be the same, and we only store the last
8762 one we see. */
94af9270
KS
8763 if (cu->language == language_ada)
8764 part_die->name = DW_STRING (&attr);
c906108c
SS
8765 break;
8766 case DW_AT_low_pc:
8767 has_low_pc_attr = 1;
8768 part_die->lowpc = DW_ADDR (&attr);
8769 break;
8770 case DW_AT_high_pc:
8771 has_high_pc_attr = 1;
8772 part_die->highpc = DW_ADDR (&attr);
8773 break;
8774 case DW_AT_location:
8e19ed76
PS
8775 /* Support the .debug_loc offsets */
8776 if (attr_form_is_block (&attr))
8777 {
8778 part_die->locdesc = DW_BLOCK (&attr);
8779 }
3690dd37 8780 else if (attr_form_is_section_offset (&attr))
8e19ed76 8781 {
4d3c2250 8782 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8783 }
8784 else
8785 {
4d3c2250
KB
8786 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8787 "partial symbol information");
8e19ed76 8788 }
c906108c 8789 break;
c906108c
SS
8790 case DW_AT_external:
8791 part_die->is_external = DW_UNSND (&attr);
8792 break;
8793 case DW_AT_declaration:
8794 part_die->is_declaration = DW_UNSND (&attr);
8795 break;
8796 case DW_AT_type:
8797 part_die->has_type = 1;
8798 break;
8799 case DW_AT_abstract_origin:
8800 case DW_AT_specification:
72bf9492
DJ
8801 case DW_AT_extension:
8802 part_die->has_specification = 1;
c764a876 8803 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8804 break;
8805 case DW_AT_sibling:
8806 /* Ignore absolute siblings, they might point outside of
8807 the current compile unit. */
8808 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8809 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8810 else
93311388 8811 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8812 break;
fa4028e9
JB
8813 case DW_AT_byte_size:
8814 part_die->has_byte_size = 1;
8815 break;
68511cec
CES
8816 case DW_AT_calling_convention:
8817 /* DWARF doesn't provide a way to identify a program's source-level
8818 entry point. DW_AT_calling_convention attributes are only meant
8819 to describe functions' calling conventions.
8820
8821 However, because it's a necessary piece of information in
8822 Fortran, and because DW_CC_program is the only piece of debugging
8823 information whose definition refers to a 'main program' at all,
8824 several compilers have begun marking Fortran main programs with
8825 DW_CC_program --- even when those functions use the standard
8826 calling conventions.
8827
8828 So until DWARF specifies a way to provide this information and
8829 compilers pick up the new representation, we'll support this
8830 practice. */
8831 if (DW_UNSND (&attr) == DW_CC_program
8832 && cu->language == language_fortran)
8833 set_main_name (part_die->name);
8834 break;
c906108c
SS
8835 default:
8836 break;
8837 }
8838 }
8839
c906108c
SS
8840 /* When using the GNU linker, .gnu.linkonce. sections are used to
8841 eliminate duplicate copies of functions and vtables and such.
8842 The linker will arbitrarily choose one and discard the others.
8843 The AT_*_pc values for such functions refer to local labels in
8844 these sections. If the section from that file was discarded, the
8845 labels are not in the output, so the relocs get a value of 0.
8846 If this is a discarded function, mark the pc bounds as invalid,
8847 so that GDB will ignore it. */
8848 if (has_low_pc_attr && has_high_pc_attr
8849 && part_die->lowpc < part_die->highpc
8850 && (part_die->lowpc != 0
72dca2f5 8851 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8852 part_die->has_pc_info = 1;
85cbf3d3 8853
c906108c
SS
8854 return info_ptr;
8855}
8856
72bf9492
DJ
8857/* Find a cached partial DIE at OFFSET in CU. */
8858
8859static struct partial_die_info *
c764a876 8860find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8861{
8862 struct partial_die_info *lookup_die = NULL;
8863 struct partial_die_info part_die;
8864
8865 part_die.offset = offset;
8866 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8867
72bf9492
DJ
8868 return lookup_die;
8869}
8870
348e048f
DE
8871/* Find a partial DIE at OFFSET, which may or may not be in CU,
8872 except in the case of .debug_types DIEs which do not reference
8873 outside their CU (they do however referencing other types via
8874 DW_FORM_sig8). */
72bf9492
DJ
8875
8876static struct partial_die_info *
c764a876 8877find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8878{
5afb4e99
DJ
8879 struct dwarf2_per_cu_data *per_cu = NULL;
8880 struct partial_die_info *pd = NULL;
72bf9492 8881
348e048f
DE
8882 if (cu->per_cu->from_debug_types)
8883 {
8884 pd = find_partial_die_in_comp_unit (offset, cu);
8885 if (pd != NULL)
8886 return pd;
8887 goto not_found;
8888 }
8889
45452591 8890 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8891 {
8892 pd = find_partial_die_in_comp_unit (offset, cu);
8893 if (pd != NULL)
8894 return pd;
8895 }
72bf9492 8896
ae038cb0
DJ
8897 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8898
98bfdba5
PA
8899 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8900 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8901
8902 per_cu->cu->last_used = 0;
5afb4e99
DJ
8903 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8904
8905 if (pd == NULL && per_cu->load_all_dies == 0)
8906 {
8907 struct cleanup *back_to;
8908 struct partial_die_info comp_unit_die;
8909 struct abbrev_info *abbrev;
8910 unsigned int bytes_read;
8911 char *info_ptr;
8912
8913 per_cu->load_all_dies = 1;
8914
8915 /* Re-read the DIEs. */
8916 back_to = make_cleanup (null_cleanup, 0);
8917 if (per_cu->cu->dwarf2_abbrevs == NULL)
8918 {
8919 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8920 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8921 }
dce234bc 8922 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8923 + per_cu->cu->header.offset
8924 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8925 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8926 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8927 per_cu->cu->objfile->obfd,
8928 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8929 per_cu->cu);
8930 if (comp_unit_die.has_children)
93311388
DE
8931 load_partial_dies (per_cu->cu->objfile->obfd,
8932 dwarf2_per_objfile->info.buffer, info_ptr,
8933 0, per_cu->cu);
5afb4e99
DJ
8934 do_cleanups (back_to);
8935
8936 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8937 }
8938
348e048f
DE
8939 not_found:
8940
5afb4e99
DJ
8941 if (pd == NULL)
8942 internal_error (__FILE__, __LINE__,
c764a876 8943 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8944 offset, bfd_get_filename (cu->objfile->obfd));
8945 return pd;
72bf9492
DJ
8946}
8947
8948/* Adjust PART_DIE before generating a symbol for it. This function
8949 may set the is_external flag or change the DIE's name. */
8950
8951static void
8952fixup_partial_die (struct partial_die_info *part_die,
8953 struct dwarf2_cu *cu)
8954{
8955 /* If we found a reference attribute and the DIE has no name, try
8956 to find a name in the referred to DIE. */
8957
8958 if (part_die->name == NULL && part_die->has_specification)
8959 {
8960 struct partial_die_info *spec_die;
72bf9492 8961
10b3939b 8962 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8963
10b3939b 8964 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8965
8966 if (spec_die->name)
8967 {
8968 part_die->name = spec_die->name;
8969
8970 /* Copy DW_AT_external attribute if it is set. */
8971 if (spec_die->is_external)
8972 part_die->is_external = spec_die->is_external;
8973 }
8974 }
8975
8976 /* Set default names for some unnamed DIEs. */
8977 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8978 || part_die->tag == DW_TAG_class_type))
8979 part_die->name = "(anonymous class)";
8980
8981 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8982 part_die->name = "(anonymous namespace)";
8983
8984 if (part_die->tag == DW_TAG_structure_type
8985 || part_die->tag == DW_TAG_class_type
8986 || part_die->tag == DW_TAG_union_type)
8987 guess_structure_name (part_die, cu);
8988}
8989
a8329558 8990/* Read an attribute value described by an attribute form. */
c906108c 8991
fe1b8b76 8992static gdb_byte *
a8329558 8993read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 8994 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 8995 struct dwarf2_cu *cu)
c906108c 8996{
e7c27a73 8997 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8998 unsigned int bytes_read;
8999 struct dwarf_block *blk;
9000
a8329558
KW
9001 attr->form = form;
9002 switch (form)
c906108c 9003 {
c906108c 9004 case DW_FORM_ref_addr:
ae411497
TT
9005 if (cu->header.version == 2)
9006 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9007 else
9008 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9009 info_ptr += bytes_read;
9010 break;
9011 case DW_FORM_addr:
e7c27a73 9012 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9013 info_ptr += bytes_read;
c906108c
SS
9014 break;
9015 case DW_FORM_block2:
7b5a2f43 9016 blk = dwarf_alloc_block (cu);
c906108c
SS
9017 blk->size = read_2_bytes (abfd, info_ptr);
9018 info_ptr += 2;
9019 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9020 info_ptr += blk->size;
9021 DW_BLOCK (attr) = blk;
9022 break;
9023 case DW_FORM_block4:
7b5a2f43 9024 blk = dwarf_alloc_block (cu);
c906108c
SS
9025 blk->size = read_4_bytes (abfd, info_ptr);
9026 info_ptr += 4;
9027 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9028 info_ptr += blk->size;
9029 DW_BLOCK (attr) = blk;
9030 break;
9031 case DW_FORM_data2:
9032 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9033 info_ptr += 2;
9034 break;
9035 case DW_FORM_data4:
9036 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9037 info_ptr += 4;
9038 break;
9039 case DW_FORM_data8:
9040 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9041 info_ptr += 8;
9042 break;
2dc7f7b3
TT
9043 case DW_FORM_sec_offset:
9044 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9045 info_ptr += bytes_read;
9046 break;
c906108c 9047 case DW_FORM_string:
9b1c24c8 9048 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9049 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9050 info_ptr += bytes_read;
9051 break;
4bdf3d34
JJ
9052 case DW_FORM_strp:
9053 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9054 &bytes_read);
8285870a 9055 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9056 info_ptr += bytes_read;
9057 break;
2dc7f7b3 9058 case DW_FORM_exprloc:
c906108c 9059 case DW_FORM_block:
7b5a2f43 9060 blk = dwarf_alloc_block (cu);
c906108c
SS
9061 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9062 info_ptr += bytes_read;
9063 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9064 info_ptr += blk->size;
9065 DW_BLOCK (attr) = blk;
9066 break;
9067 case DW_FORM_block1:
7b5a2f43 9068 blk = dwarf_alloc_block (cu);
c906108c
SS
9069 blk->size = read_1_byte (abfd, info_ptr);
9070 info_ptr += 1;
9071 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9072 info_ptr += blk->size;
9073 DW_BLOCK (attr) = blk;
9074 break;
9075 case DW_FORM_data1:
9076 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9077 info_ptr += 1;
9078 break;
9079 case DW_FORM_flag:
9080 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9081 info_ptr += 1;
9082 break;
2dc7f7b3
TT
9083 case DW_FORM_flag_present:
9084 DW_UNSND (attr) = 1;
9085 break;
c906108c
SS
9086 case DW_FORM_sdata:
9087 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9088 info_ptr += bytes_read;
9089 break;
9090 case DW_FORM_udata:
9091 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9092 info_ptr += bytes_read;
9093 break;
9094 case DW_FORM_ref1:
10b3939b 9095 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9096 info_ptr += 1;
9097 break;
9098 case DW_FORM_ref2:
10b3939b 9099 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9100 info_ptr += 2;
9101 break;
9102 case DW_FORM_ref4:
10b3939b 9103 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9104 info_ptr += 4;
9105 break;
613e1657 9106 case DW_FORM_ref8:
10b3939b 9107 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9108 info_ptr += 8;
9109 break;
348e048f
DE
9110 case DW_FORM_sig8:
9111 /* Convert the signature to something we can record in DW_UNSND
9112 for later lookup.
9113 NOTE: This is NULL if the type wasn't found. */
9114 DW_SIGNATURED_TYPE (attr) =
9115 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9116 info_ptr += 8;
9117 break;
c906108c 9118 case DW_FORM_ref_udata:
10b3939b
DJ
9119 DW_ADDR (attr) = (cu->header.offset
9120 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9121 info_ptr += bytes_read;
9122 break;
c906108c 9123 case DW_FORM_indirect:
a8329558
KW
9124 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9125 info_ptr += bytes_read;
e7c27a73 9126 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9127 break;
c906108c 9128 default:
8a3fe4f8 9129 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9130 dwarf_form_name (form),
9131 bfd_get_filename (abfd));
c906108c 9132 }
28e94949
JB
9133
9134 /* We have seen instances where the compiler tried to emit a byte
9135 size attribute of -1 which ended up being encoded as an unsigned
9136 0xffffffff. Although 0xffffffff is technically a valid size value,
9137 an object of this size seems pretty unlikely so we can relatively
9138 safely treat these cases as if the size attribute was invalid and
9139 treat them as zero by default. */
9140 if (attr->name == DW_AT_byte_size
9141 && form == DW_FORM_data4
9142 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9143 {
9144 complaint
9145 (&symfile_complaints,
43bbcdc2
PH
9146 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9147 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9148 DW_UNSND (attr) = 0;
9149 }
28e94949 9150
c906108c
SS
9151 return info_ptr;
9152}
9153
a8329558
KW
9154/* Read an attribute described by an abbreviated attribute. */
9155
fe1b8b76 9156static gdb_byte *
a8329558 9157read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9158 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9159{
9160 attr->name = abbrev->name;
e7c27a73 9161 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9162}
9163
c906108c
SS
9164/* read dwarf information from a buffer */
9165
9166static unsigned int
fe1b8b76 9167read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9168{
fe1b8b76 9169 return bfd_get_8 (abfd, buf);
c906108c
SS
9170}
9171
9172static int
fe1b8b76 9173read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9174{
fe1b8b76 9175 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9176}
9177
9178static unsigned int
fe1b8b76 9179read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9180{
fe1b8b76 9181 return bfd_get_16 (abfd, buf);
c906108c
SS
9182}
9183
9184static int
fe1b8b76 9185read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9186{
fe1b8b76 9187 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9188}
9189
9190static unsigned int
fe1b8b76 9191read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9192{
fe1b8b76 9193 return bfd_get_32 (abfd, buf);
c906108c
SS
9194}
9195
9196static int
fe1b8b76 9197read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9198{
fe1b8b76 9199 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9200}
9201
93311388 9202static ULONGEST
fe1b8b76 9203read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9204{
fe1b8b76 9205 return bfd_get_64 (abfd, buf);
c906108c
SS
9206}
9207
9208static CORE_ADDR
fe1b8b76 9209read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9210 unsigned int *bytes_read)
c906108c 9211{
e7c27a73 9212 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9213 CORE_ADDR retval = 0;
9214
107d2387 9215 if (cu_header->signed_addr_p)
c906108c 9216 {
107d2387
AC
9217 switch (cu_header->addr_size)
9218 {
9219 case 2:
fe1b8b76 9220 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9221 break;
9222 case 4:
fe1b8b76 9223 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9224 break;
9225 case 8:
fe1b8b76 9226 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9227 break;
9228 default:
8e65ff28 9229 internal_error (__FILE__, __LINE__,
e2e0b3e5 9230 _("read_address: bad switch, signed [in module %s]"),
659b0389 9231 bfd_get_filename (abfd));
107d2387
AC
9232 }
9233 }
9234 else
9235 {
9236 switch (cu_header->addr_size)
9237 {
9238 case 2:
fe1b8b76 9239 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9240 break;
9241 case 4:
fe1b8b76 9242 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9243 break;
9244 case 8:
fe1b8b76 9245 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9246 break;
9247 default:
8e65ff28 9248 internal_error (__FILE__, __LINE__,
e2e0b3e5 9249 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9250 bfd_get_filename (abfd));
107d2387 9251 }
c906108c 9252 }
64367e0a 9253
107d2387
AC
9254 *bytes_read = cu_header->addr_size;
9255 return retval;
c906108c
SS
9256}
9257
f7ef9339 9258/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9259 specification allows the initial length to take up either 4 bytes
9260 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9261 bytes describe the length and all offsets will be 8 bytes in length
9262 instead of 4.
9263
f7ef9339
KB
9264 An older, non-standard 64-bit format is also handled by this
9265 function. The older format in question stores the initial length
9266 as an 8-byte quantity without an escape value. Lengths greater
9267 than 2^32 aren't very common which means that the initial 4 bytes
9268 is almost always zero. Since a length value of zero doesn't make
9269 sense for the 32-bit format, this initial zero can be considered to
9270 be an escape value which indicates the presence of the older 64-bit
9271 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9272 greater than 4GB. If it becomes necessary to handle lengths
9273 somewhat larger than 4GB, we could allow other small values (such
9274 as the non-sensical values of 1, 2, and 3) to also be used as
9275 escape values indicating the presence of the old format.
f7ef9339 9276
917c78fc
MK
9277 The value returned via bytes_read should be used to increment the
9278 relevant pointer after calling read_initial_length().
c764a876 9279
613e1657
KB
9280 [ Note: read_initial_length() and read_offset() are based on the
9281 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9282 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9283 from:
9284
f7ef9339 9285 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9286
613e1657
KB
9287 This document is only a draft and is subject to change. (So beware.)
9288
f7ef9339 9289 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9290 determined empirically by examining 64-bit ELF files produced by
9291 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9292
9293 - Kevin, July 16, 2002
613e1657
KB
9294 ] */
9295
9296static LONGEST
c764a876 9297read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9298{
fe1b8b76 9299 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9300
dd373385 9301 if (length == 0xffffffff)
613e1657 9302 {
fe1b8b76 9303 length = bfd_get_64 (abfd, buf + 4);
613e1657 9304 *bytes_read = 12;
613e1657 9305 }
dd373385 9306 else if (length == 0)
f7ef9339 9307 {
dd373385 9308 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9309 length = bfd_get_64 (abfd, buf);
f7ef9339 9310 *bytes_read = 8;
f7ef9339 9311 }
613e1657
KB
9312 else
9313 {
9314 *bytes_read = 4;
613e1657
KB
9315 }
9316
c764a876
DE
9317 return length;
9318}
dd373385 9319
c764a876
DE
9320/* Cover function for read_initial_length.
9321 Returns the length of the object at BUF, and stores the size of the
9322 initial length in *BYTES_READ and stores the size that offsets will be in
9323 *OFFSET_SIZE.
9324 If the initial length size is not equivalent to that specified in
9325 CU_HEADER then issue a complaint.
9326 This is useful when reading non-comp-unit headers. */
dd373385 9327
c764a876
DE
9328static LONGEST
9329read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9330 const struct comp_unit_head *cu_header,
9331 unsigned int *bytes_read,
9332 unsigned int *offset_size)
9333{
9334 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9335
9336 gdb_assert (cu_header->initial_length_size == 4
9337 || cu_header->initial_length_size == 8
9338 || cu_header->initial_length_size == 12);
9339
9340 if (cu_header->initial_length_size != *bytes_read)
9341 complaint (&symfile_complaints,
9342 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9343
c764a876 9344 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9345 return length;
613e1657
KB
9346}
9347
9348/* Read an offset from the data stream. The size of the offset is
917c78fc 9349 given by cu_header->offset_size. */
613e1657
KB
9350
9351static LONGEST
fe1b8b76 9352read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9353 unsigned int *bytes_read)
c764a876
DE
9354{
9355 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9356
c764a876
DE
9357 *bytes_read = cu_header->offset_size;
9358 return offset;
9359}
9360
9361/* Read an offset from the data stream. */
9362
9363static LONGEST
9364read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9365{
9366 LONGEST retval = 0;
9367
c764a876 9368 switch (offset_size)
613e1657
KB
9369 {
9370 case 4:
fe1b8b76 9371 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9372 break;
9373 case 8:
fe1b8b76 9374 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9375 break;
9376 default:
8e65ff28 9377 internal_error (__FILE__, __LINE__,
c764a876 9378 _("read_offset_1: bad switch [in module %s]"),
659b0389 9379 bfd_get_filename (abfd));
613e1657
KB
9380 }
9381
917c78fc 9382 return retval;
613e1657
KB
9383}
9384
fe1b8b76
JB
9385static gdb_byte *
9386read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9387{
9388 /* If the size of a host char is 8 bits, we can return a pointer
9389 to the buffer, otherwise we have to copy the data to a buffer
9390 allocated on the temporary obstack. */
4bdf3d34 9391 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9392 return buf;
c906108c
SS
9393}
9394
9395static char *
9b1c24c8 9396read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9397{
9398 /* If the size of a host char is 8 bits, we can return a pointer
9399 to the string, otherwise we have to copy the string to a buffer
9400 allocated on the temporary obstack. */
4bdf3d34 9401 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9402 if (*buf == '\0')
9403 {
9404 *bytes_read_ptr = 1;
9405 return NULL;
9406 }
fe1b8b76
JB
9407 *bytes_read_ptr = strlen ((char *) buf) + 1;
9408 return (char *) buf;
4bdf3d34
JJ
9409}
9410
9411static char *
fe1b8b76 9412read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9413 const struct comp_unit_head *cu_header,
9414 unsigned int *bytes_read_ptr)
9415{
c764a876 9416 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9417
be391dca 9418 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9419 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9420 {
8a3fe4f8 9421 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9422 bfd_get_filename (abfd));
4bdf3d34 9423 return NULL;
c906108c 9424 }
dce234bc 9425 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9426 {
8a3fe4f8 9427 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9428 bfd_get_filename (abfd));
c906108c
SS
9429 return NULL;
9430 }
4bdf3d34 9431 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9432 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9433 return NULL;
dce234bc 9434 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9435}
9436
ce5d95e1 9437static unsigned long
fe1b8b76 9438read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9439{
ce5d95e1
JB
9440 unsigned long result;
9441 unsigned int num_read;
c906108c
SS
9442 int i, shift;
9443 unsigned char byte;
9444
9445 result = 0;
9446 shift = 0;
9447 num_read = 0;
9448 i = 0;
9449 while (1)
9450 {
fe1b8b76 9451 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9452 buf++;
9453 num_read++;
ce5d95e1 9454 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9455 if ((byte & 128) == 0)
9456 {
9457 break;
9458 }
9459 shift += 7;
9460 }
9461 *bytes_read_ptr = num_read;
9462 return result;
9463}
9464
ce5d95e1 9465static long
fe1b8b76 9466read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9467{
ce5d95e1 9468 long result;
77e0b926 9469 int i, shift, num_read;
c906108c
SS
9470 unsigned char byte;
9471
9472 result = 0;
9473 shift = 0;
c906108c
SS
9474 num_read = 0;
9475 i = 0;
9476 while (1)
9477 {
fe1b8b76 9478 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9479 buf++;
9480 num_read++;
ce5d95e1 9481 result |= ((long)(byte & 127) << shift);
c906108c
SS
9482 shift += 7;
9483 if ((byte & 128) == 0)
9484 {
9485 break;
9486 }
9487 }
77e0b926
DJ
9488 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9489 result |= -(((long)1) << shift);
c906108c
SS
9490 *bytes_read_ptr = num_read;
9491 return result;
9492}
9493
4bb7a0a7
DJ
9494/* Return a pointer to just past the end of an LEB128 number in BUF. */
9495
fe1b8b76
JB
9496static gdb_byte *
9497skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9498{
9499 int byte;
9500
9501 while (1)
9502 {
fe1b8b76 9503 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9504 buf++;
9505 if ((byte & 128) == 0)
9506 return buf;
9507 }
9508}
9509
c906108c 9510static void
e142c38c 9511set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9512{
9513 switch (lang)
9514 {
9515 case DW_LANG_C89:
76bee0cc 9516 case DW_LANG_C99:
c906108c 9517 case DW_LANG_C:
e142c38c 9518 cu->language = language_c;
c906108c
SS
9519 break;
9520 case DW_LANG_C_plus_plus:
e142c38c 9521 cu->language = language_cplus;
c906108c 9522 break;
6aecb9c2
JB
9523 case DW_LANG_D:
9524 cu->language = language_d;
9525 break;
c906108c
SS
9526 case DW_LANG_Fortran77:
9527 case DW_LANG_Fortran90:
b21b22e0 9528 case DW_LANG_Fortran95:
e142c38c 9529 cu->language = language_fortran;
c906108c
SS
9530 break;
9531 case DW_LANG_Mips_Assembler:
e142c38c 9532 cu->language = language_asm;
c906108c 9533 break;
bebd888e 9534 case DW_LANG_Java:
e142c38c 9535 cu->language = language_java;
bebd888e 9536 break;
c906108c 9537 case DW_LANG_Ada83:
8aaf0b47 9538 case DW_LANG_Ada95:
bc5f45f8
JB
9539 cu->language = language_ada;
9540 break;
72019c9c
GM
9541 case DW_LANG_Modula2:
9542 cu->language = language_m2;
9543 break;
fe8e67fd
PM
9544 case DW_LANG_Pascal83:
9545 cu->language = language_pascal;
9546 break;
22566fbd
DJ
9547 case DW_LANG_ObjC:
9548 cu->language = language_objc;
9549 break;
c906108c
SS
9550 case DW_LANG_Cobol74:
9551 case DW_LANG_Cobol85:
c906108c 9552 default:
e142c38c 9553 cu->language = language_minimal;
c906108c
SS
9554 break;
9555 }
e142c38c 9556 cu->language_defn = language_def (cu->language);
c906108c
SS
9557}
9558
9559/* Return the named attribute or NULL if not there. */
9560
9561static struct attribute *
e142c38c 9562dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9563{
9564 unsigned int i;
9565 struct attribute *spec = NULL;
9566
9567 for (i = 0; i < die->num_attrs; ++i)
9568 {
9569 if (die->attrs[i].name == name)
10b3939b 9570 return &die->attrs[i];
c906108c
SS
9571 if (die->attrs[i].name == DW_AT_specification
9572 || die->attrs[i].name == DW_AT_abstract_origin)
9573 spec = &die->attrs[i];
9574 }
c906108c 9575
10b3939b 9576 if (spec)
f2f0e013
DJ
9577 {
9578 die = follow_die_ref (die, spec, &cu);
9579 return dwarf2_attr (die, name, cu);
9580 }
c5aa993b 9581
c906108c
SS
9582 return NULL;
9583}
9584
348e048f
DE
9585/* Return the named attribute or NULL if not there,
9586 but do not follow DW_AT_specification, etc.
9587 This is for use in contexts where we're reading .debug_types dies.
9588 Following DW_AT_specification, DW_AT_abstract_origin will take us
9589 back up the chain, and we want to go down. */
9590
9591static struct attribute *
9592dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9593 struct dwarf2_cu *cu)
9594{
9595 unsigned int i;
9596
9597 for (i = 0; i < die->num_attrs; ++i)
9598 if (die->attrs[i].name == name)
9599 return &die->attrs[i];
9600
9601 return NULL;
9602}
9603
05cf31d1
JB
9604/* Return non-zero iff the attribute NAME is defined for the given DIE,
9605 and holds a non-zero value. This function should only be used for
2dc7f7b3 9606 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9607
9608static int
9609dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9610{
9611 struct attribute *attr = dwarf2_attr (die, name, cu);
9612
9613 return (attr && DW_UNSND (attr));
9614}
9615
3ca72b44 9616static int
e142c38c 9617die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9618{
05cf31d1
JB
9619 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9620 which value is non-zero. However, we have to be careful with
9621 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9622 (via dwarf2_flag_true_p) follows this attribute. So we may
9623 end up accidently finding a declaration attribute that belongs
9624 to a different DIE referenced by the specification attribute,
9625 even though the given DIE does not have a declaration attribute. */
9626 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9627 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9628}
9629
63d06c5c 9630/* Return the die giving the specification for DIE, if there is
f2f0e013 9631 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9632 containing the return value on output. If there is no
9633 specification, but there is an abstract origin, that is
9634 returned. */
63d06c5c
DC
9635
9636static struct die_info *
f2f0e013 9637die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9638{
f2f0e013
DJ
9639 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9640 *spec_cu);
63d06c5c 9641
edb3359d
DJ
9642 if (spec_attr == NULL)
9643 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9644
63d06c5c
DC
9645 if (spec_attr == NULL)
9646 return NULL;
9647 else
f2f0e013 9648 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9649}
c906108c 9650
debd256d
JB
9651/* Free the line_header structure *LH, and any arrays and strings it
9652 refers to. */
9653static void
9654free_line_header (struct line_header *lh)
9655{
9656 if (lh->standard_opcode_lengths)
a8bc7b56 9657 xfree (lh->standard_opcode_lengths);
debd256d
JB
9658
9659 /* Remember that all the lh->file_names[i].name pointers are
9660 pointers into debug_line_buffer, and don't need to be freed. */
9661 if (lh->file_names)
a8bc7b56 9662 xfree (lh->file_names);
debd256d
JB
9663
9664 /* Similarly for the include directory names. */
9665 if (lh->include_dirs)
a8bc7b56 9666 xfree (lh->include_dirs);
debd256d 9667
a8bc7b56 9668 xfree (lh);
debd256d
JB
9669}
9670
9671
9672/* Add an entry to LH's include directory table. */
9673static void
9674add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9675{
debd256d
JB
9676 /* Grow the array if necessary. */
9677 if (lh->include_dirs_size == 0)
c5aa993b 9678 {
debd256d
JB
9679 lh->include_dirs_size = 1; /* for testing */
9680 lh->include_dirs = xmalloc (lh->include_dirs_size
9681 * sizeof (*lh->include_dirs));
9682 }
9683 else if (lh->num_include_dirs >= lh->include_dirs_size)
9684 {
9685 lh->include_dirs_size *= 2;
9686 lh->include_dirs = xrealloc (lh->include_dirs,
9687 (lh->include_dirs_size
9688 * sizeof (*lh->include_dirs)));
c5aa993b 9689 }
c906108c 9690
debd256d
JB
9691 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9692}
6e70227d 9693
debd256d
JB
9694
9695/* Add an entry to LH's file name table. */
9696static void
9697add_file_name (struct line_header *lh,
9698 char *name,
9699 unsigned int dir_index,
9700 unsigned int mod_time,
9701 unsigned int length)
9702{
9703 struct file_entry *fe;
9704
9705 /* Grow the array if necessary. */
9706 if (lh->file_names_size == 0)
9707 {
9708 lh->file_names_size = 1; /* for testing */
9709 lh->file_names = xmalloc (lh->file_names_size
9710 * sizeof (*lh->file_names));
9711 }
9712 else if (lh->num_file_names >= lh->file_names_size)
9713 {
9714 lh->file_names_size *= 2;
9715 lh->file_names = xrealloc (lh->file_names,
9716 (lh->file_names_size
9717 * sizeof (*lh->file_names)));
9718 }
9719
9720 fe = &lh->file_names[lh->num_file_names++];
9721 fe->name = name;
9722 fe->dir_index = dir_index;
9723 fe->mod_time = mod_time;
9724 fe->length = length;
aaa75496 9725 fe->included_p = 0;
cb1df416 9726 fe->symtab = NULL;
debd256d 9727}
6e70227d 9728
debd256d
JB
9729
9730/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9731 .debug_line, according to the endianness of ABFD. Return a pointer
9732 to a struct line_header, allocated using xmalloc.
debd256d
JB
9733
9734 NOTE: the strings in the include directory and file name tables of
9735 the returned object point into debug_line_buffer, and must not be
9736 freed. */
9737static struct line_header *
9738dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9739 struct dwarf2_cu *cu)
debd256d
JB
9740{
9741 struct cleanup *back_to;
9742 struct line_header *lh;
fe1b8b76 9743 gdb_byte *line_ptr;
c764a876 9744 unsigned int bytes_read, offset_size;
debd256d
JB
9745 int i;
9746 char *cur_dir, *cur_file;
9747
be391dca 9748 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9749 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9750 {
e2e0b3e5 9751 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9752 return 0;
9753 }
9754
a738430d
MK
9755 /* Make sure that at least there's room for the total_length field.
9756 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9757 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9758 {
4d3c2250 9759 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9760 return 0;
9761 }
9762
9763 lh = xmalloc (sizeof (*lh));
9764 memset (lh, 0, sizeof (*lh));
9765 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9766 (void *) lh);
9767
dce234bc 9768 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9769
a738430d 9770 /* Read in the header. */
6e70227d 9771 lh->total_length =
c764a876
DE
9772 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9773 &bytes_read, &offset_size);
debd256d 9774 line_ptr += bytes_read;
dce234bc
PP
9775 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9776 + dwarf2_per_objfile->line.size))
debd256d 9777 {
4d3c2250 9778 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9779 return 0;
9780 }
9781 lh->statement_program_end = line_ptr + lh->total_length;
9782 lh->version = read_2_bytes (abfd, line_ptr);
9783 line_ptr += 2;
c764a876
DE
9784 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9785 line_ptr += offset_size;
debd256d
JB
9786 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9787 line_ptr += 1;
2dc7f7b3
TT
9788 if (lh->version >= 4)
9789 {
9790 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9791 line_ptr += 1;
9792 }
9793 else
9794 lh->maximum_ops_per_instruction = 1;
9795
9796 if (lh->maximum_ops_per_instruction == 0)
9797 {
9798 lh->maximum_ops_per_instruction = 1;
9799 complaint (&symfile_complaints,
9800 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9801 }
9802
debd256d
JB
9803 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9804 line_ptr += 1;
9805 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9806 line_ptr += 1;
9807 lh->line_range = read_1_byte (abfd, line_ptr);
9808 line_ptr += 1;
9809 lh->opcode_base = read_1_byte (abfd, line_ptr);
9810 line_ptr += 1;
9811 lh->standard_opcode_lengths
fe1b8b76 9812 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9813
9814 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9815 for (i = 1; i < lh->opcode_base; ++i)
9816 {
9817 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9818 line_ptr += 1;
9819 }
9820
a738430d 9821 /* Read directory table. */
9b1c24c8 9822 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9823 {
9824 line_ptr += bytes_read;
9825 add_include_dir (lh, cur_dir);
9826 }
9827 line_ptr += bytes_read;
9828
a738430d 9829 /* Read file name table. */
9b1c24c8 9830 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9831 {
9832 unsigned int dir_index, mod_time, length;
9833
9834 line_ptr += bytes_read;
9835 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9836 line_ptr += bytes_read;
9837 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9838 line_ptr += bytes_read;
9839 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9840 line_ptr += bytes_read;
9841
9842 add_file_name (lh, cur_file, dir_index, mod_time, length);
9843 }
9844 line_ptr += bytes_read;
6e70227d 9845 lh->statement_program_start = line_ptr;
debd256d 9846
dce234bc
PP
9847 if (line_ptr > (dwarf2_per_objfile->line.buffer
9848 + dwarf2_per_objfile->line.size))
4d3c2250 9849 complaint (&symfile_complaints,
e2e0b3e5 9850 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9851
9852 discard_cleanups (back_to);
9853 return lh;
9854}
c906108c 9855
5fb290d7
DJ
9856/* This function exists to work around a bug in certain compilers
9857 (particularly GCC 2.95), in which the first line number marker of a
9858 function does not show up until after the prologue, right before
9859 the second line number marker. This function shifts ADDRESS down
9860 to the beginning of the function if necessary, and is called on
9861 addresses passed to record_line. */
9862
9863static CORE_ADDR
e142c38c 9864check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9865{
9866 struct function_range *fn;
9867
9868 /* Find the function_range containing address. */
e142c38c 9869 if (!cu->first_fn)
5fb290d7
DJ
9870 return address;
9871
e142c38c
DJ
9872 if (!cu->cached_fn)
9873 cu->cached_fn = cu->first_fn;
5fb290d7 9874
e142c38c 9875 fn = cu->cached_fn;
5fb290d7
DJ
9876 while (fn)
9877 if (fn->lowpc <= address && fn->highpc > address)
9878 goto found;
9879 else
9880 fn = fn->next;
9881
e142c38c
DJ
9882 fn = cu->first_fn;
9883 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9884 if (fn->lowpc <= address && fn->highpc > address)
9885 goto found;
9886 else
9887 fn = fn->next;
9888
9889 return address;
9890
9891 found:
9892 if (fn->seen_line)
9893 return address;
9894 if (address != fn->lowpc)
4d3c2250 9895 complaint (&symfile_complaints,
e2e0b3e5 9896 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9897 (unsigned long) address, fn->name);
5fb290d7
DJ
9898 fn->seen_line = 1;
9899 return fn->lowpc;
9900}
9901
aaa75496
JB
9902/* Decode the Line Number Program (LNP) for the given line_header
9903 structure and CU. The actual information extracted and the type
9904 of structures created from the LNP depends on the value of PST.
9905
9906 1. If PST is NULL, then this procedure uses the data from the program
9907 to create all necessary symbol tables, and their linetables.
9908 The compilation directory of the file is passed in COMP_DIR,
9909 and must not be NULL.
6e70227d 9910
aaa75496
JB
9911 2. If PST is not NULL, this procedure reads the program to determine
9912 the list of files included by the unit represented by PST, and
9913 builds all the associated partial symbol tables. In this case,
9914 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9915 is not used to compute the full name of the symtab, and therefore
9916 omitting it when building the partial symtab does not introduce
9917 the potential for inconsistency - a partial symtab and its associated
9918 symbtab having a different fullname -). */
debd256d 9919
c906108c 9920static void
debd256d 9921dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 9922 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 9923{
a8c50c1f 9924 gdb_byte *line_ptr, *extended_end;
fe1b8b76 9925 gdb_byte *line_end;
a8c50c1f 9926 unsigned int bytes_read, extended_len;
c906108c 9927 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
9928 CORE_ADDR baseaddr;
9929 struct objfile *objfile = cu->objfile;
fbf65064 9930 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 9931 const int decode_for_pst_p = (pst != NULL);
cb1df416 9932 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
9933
9934 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9935
debd256d
JB
9936 line_ptr = lh->statement_program_start;
9937 line_end = lh->statement_program_end;
c906108c
SS
9938
9939 /* Read the statement sequences until there's nothing left. */
9940 while (line_ptr < line_end)
9941 {
9942 /* state machine registers */
9943 CORE_ADDR address = 0;
9944 unsigned int file = 1;
9945 unsigned int line = 1;
9946 unsigned int column = 0;
debd256d 9947 int is_stmt = lh->default_is_stmt;
c906108c
SS
9948 int basic_block = 0;
9949 int end_sequence = 0;
fbf65064 9950 CORE_ADDR addr;
2dc7f7b3 9951 unsigned char op_index = 0;
c906108c 9952
aaa75496 9953 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 9954 {
aaa75496 9955 /* Start a subfile for the current file of the state machine. */
debd256d
JB
9956 /* lh->include_dirs and lh->file_names are 0-based, but the
9957 directory and file name numbers in the statement program
9958 are 1-based. */
9959 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 9960 char *dir = NULL;
a738430d 9961
debd256d
JB
9962 if (fe->dir_index)
9963 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
9964
9965 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
9966 }
9967
a738430d 9968 /* Decode the table. */
c5aa993b 9969 while (!end_sequence)
c906108c
SS
9970 {
9971 op_code = read_1_byte (abfd, line_ptr);
9972 line_ptr += 1;
59205f5a
JB
9973 if (line_ptr > line_end)
9974 {
9975 dwarf2_debug_line_missing_end_sequence_complaint ();
9976 break;
9977 }
9aa1fe7e 9978
debd256d 9979 if (op_code >= lh->opcode_base)
6e70227d 9980 {
a738430d 9981 /* Special operand. */
debd256d 9982 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
9983 address += (((op_index + (adj_opcode / lh->line_range))
9984 / lh->maximum_ops_per_instruction)
9985 * lh->minimum_instruction_length);
9986 op_index = ((op_index + (adj_opcode / lh->line_range))
9987 % lh->maximum_ops_per_instruction);
debd256d 9988 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 9989 if (lh->num_file_names < file || file == 0)
25e43795 9990 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
9991 /* For now we ignore lines not starting on an
9992 instruction boundary. */
9993 else if (op_index == 0)
25e43795
DJ
9994 {
9995 lh->file_names[file - 1].included_p = 1;
ca5f395d 9996 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9997 {
9998 if (last_subfile != current_subfile)
9999 {
10000 addr = gdbarch_addr_bits_remove (gdbarch, address);
10001 if (last_subfile)
10002 record_line (last_subfile, 0, addr);
10003 last_subfile = current_subfile;
10004 }
25e43795 10005 /* Append row to matrix using current values. */
fbf65064
UW
10006 addr = check_cu_functions (address, cu);
10007 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10008 record_line (current_subfile, line, addr);
366da635 10009 }
25e43795 10010 }
ca5f395d 10011 basic_block = 0;
9aa1fe7e
GK
10012 }
10013 else switch (op_code)
c906108c
SS
10014 {
10015 case DW_LNS_extended_op:
a8c50c1f 10016 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10017 line_ptr += bytes_read;
a8c50c1f 10018 extended_end = line_ptr + extended_len;
c906108c
SS
10019 extended_op = read_1_byte (abfd, line_ptr);
10020 line_ptr += 1;
10021 switch (extended_op)
10022 {
10023 case DW_LNE_end_sequence:
10024 end_sequence = 1;
c906108c
SS
10025 break;
10026 case DW_LNE_set_address:
e7c27a73 10027 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10028 op_index = 0;
107d2387
AC
10029 line_ptr += bytes_read;
10030 address += baseaddr;
c906108c
SS
10031 break;
10032 case DW_LNE_define_file:
debd256d
JB
10033 {
10034 char *cur_file;
10035 unsigned int dir_index, mod_time, length;
6e70227d 10036
9b1c24c8 10037 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10038 line_ptr += bytes_read;
10039 dir_index =
10040 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10041 line_ptr += bytes_read;
10042 mod_time =
10043 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10044 line_ptr += bytes_read;
10045 length =
10046 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10047 line_ptr += bytes_read;
10048 add_file_name (lh, cur_file, dir_index, mod_time, length);
10049 }
c906108c 10050 break;
d0c6ba3d
CC
10051 case DW_LNE_set_discriminator:
10052 /* The discriminator is not interesting to the debugger;
10053 just ignore it. */
10054 line_ptr = extended_end;
10055 break;
c906108c 10056 default:
4d3c2250 10057 complaint (&symfile_complaints,
e2e0b3e5 10058 _("mangled .debug_line section"));
debd256d 10059 return;
c906108c 10060 }
a8c50c1f
DJ
10061 /* Make sure that we parsed the extended op correctly. If e.g.
10062 we expected a different address size than the producer used,
10063 we may have read the wrong number of bytes. */
10064 if (line_ptr != extended_end)
10065 {
10066 complaint (&symfile_complaints,
10067 _("mangled .debug_line section"));
10068 return;
10069 }
c906108c
SS
10070 break;
10071 case DW_LNS_copy:
59205f5a 10072 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10073 dwarf2_debug_line_missing_file_complaint ();
10074 else
366da635 10075 {
25e43795 10076 lh->file_names[file - 1].included_p = 1;
ca5f395d 10077 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10078 {
10079 if (last_subfile != current_subfile)
10080 {
10081 addr = gdbarch_addr_bits_remove (gdbarch, address);
10082 if (last_subfile)
10083 record_line (last_subfile, 0, addr);
10084 last_subfile = current_subfile;
10085 }
10086 addr = check_cu_functions (address, cu);
10087 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10088 record_line (current_subfile, line, addr);
10089 }
366da635 10090 }
c906108c
SS
10091 basic_block = 0;
10092 break;
10093 case DW_LNS_advance_pc:
2dc7f7b3
TT
10094 {
10095 CORE_ADDR adjust
10096 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10097
10098 address += (((op_index + adjust)
10099 / lh->maximum_ops_per_instruction)
10100 * lh->minimum_instruction_length);
10101 op_index = ((op_index + adjust)
10102 % lh->maximum_ops_per_instruction);
10103 line_ptr += bytes_read;
10104 }
c906108c
SS
10105 break;
10106 case DW_LNS_advance_line:
10107 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10108 line_ptr += bytes_read;
10109 break;
10110 case DW_LNS_set_file:
debd256d 10111 {
a738430d
MK
10112 /* The arrays lh->include_dirs and lh->file_names are
10113 0-based, but the directory and file name numbers in
10114 the statement program are 1-based. */
debd256d 10115 struct file_entry *fe;
4f1520fb 10116 char *dir = NULL;
a738430d 10117
debd256d
JB
10118 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10119 line_ptr += bytes_read;
59205f5a 10120 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10121 dwarf2_debug_line_missing_file_complaint ();
10122 else
10123 {
10124 fe = &lh->file_names[file - 1];
10125 if (fe->dir_index)
10126 dir = lh->include_dirs[fe->dir_index - 1];
10127 if (!decode_for_pst_p)
10128 {
10129 last_subfile = current_subfile;
10130 dwarf2_start_subfile (fe->name, dir, comp_dir);
10131 }
10132 }
debd256d 10133 }
c906108c
SS
10134 break;
10135 case DW_LNS_set_column:
10136 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10137 line_ptr += bytes_read;
10138 break;
10139 case DW_LNS_negate_stmt:
10140 is_stmt = (!is_stmt);
10141 break;
10142 case DW_LNS_set_basic_block:
10143 basic_block = 1;
10144 break;
c2c6d25f
JM
10145 /* Add to the address register of the state machine the
10146 address increment value corresponding to special opcode
a738430d
MK
10147 255. I.e., this value is scaled by the minimum
10148 instruction length since special opcode 255 would have
10149 scaled the the increment. */
c906108c 10150 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10151 {
10152 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10153
10154 address += (((op_index + adjust)
10155 / lh->maximum_ops_per_instruction)
10156 * lh->minimum_instruction_length);
10157 op_index = ((op_index + adjust)
10158 % lh->maximum_ops_per_instruction);
10159 }
c906108c
SS
10160 break;
10161 case DW_LNS_fixed_advance_pc:
10162 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10163 op_index = 0;
c906108c
SS
10164 line_ptr += 2;
10165 break;
9aa1fe7e 10166 default:
a738430d
MK
10167 {
10168 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10169 int i;
a738430d 10170
debd256d 10171 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10172 {
10173 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10174 line_ptr += bytes_read;
10175 }
10176 }
c906108c
SS
10177 }
10178 }
59205f5a
JB
10179 if (lh->num_file_names < file || file == 0)
10180 dwarf2_debug_line_missing_file_complaint ();
10181 else
10182 {
10183 lh->file_names[file - 1].included_p = 1;
10184 if (!decode_for_pst_p)
fbf65064
UW
10185 {
10186 addr = gdbarch_addr_bits_remove (gdbarch, address);
10187 record_line (current_subfile, 0, addr);
10188 }
59205f5a 10189 }
c906108c 10190 }
aaa75496
JB
10191
10192 if (decode_for_pst_p)
10193 {
10194 int file_index;
10195
10196 /* Now that we're done scanning the Line Header Program, we can
10197 create the psymtab of each included file. */
10198 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10199 if (lh->file_names[file_index].included_p == 1)
10200 {
5b5464ad
JB
10201 const struct file_entry fe = lh->file_names [file_index];
10202 char *include_name = fe.name;
10203 char *dir_name = NULL;
10204 char *pst_filename = pst->filename;
10205
10206 if (fe.dir_index)
10207 dir_name = lh->include_dirs[fe.dir_index - 1];
10208
10209 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
10210 {
1754f103
MK
10211 include_name = concat (dir_name, SLASH_STRING,
10212 include_name, (char *)NULL);
5b5464ad
JB
10213 make_cleanup (xfree, include_name);
10214 }
10215
10216 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10217 {
1754f103
MK
10218 pst_filename = concat (pst->dirname, SLASH_STRING,
10219 pst_filename, (char *)NULL);
5b5464ad
JB
10220 make_cleanup (xfree, pst_filename);
10221 }
10222
10223 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
10224 dwarf2_create_include_psymtab (include_name, pst, objfile);
10225 }
10226 }
cb1df416
DJ
10227 else
10228 {
10229 /* Make sure a symtab is created for every file, even files
10230 which contain only variables (i.e. no code with associated
10231 line numbers). */
10232
10233 int i;
10234 struct file_entry *fe;
10235
10236 for (i = 0; i < lh->num_file_names; i++)
10237 {
10238 char *dir = NULL;
9a619af0 10239
cb1df416
DJ
10240 fe = &lh->file_names[i];
10241 if (fe->dir_index)
10242 dir = lh->include_dirs[fe->dir_index - 1];
10243 dwarf2_start_subfile (fe->name, dir, comp_dir);
10244
10245 /* Skip the main file; we don't need it, and it must be
10246 allocated last, so that it will show up before the
10247 non-primary symtabs in the objfile's symtab list. */
10248 if (current_subfile == first_subfile)
10249 continue;
10250
10251 if (current_subfile->symtab == NULL)
10252 current_subfile->symtab = allocate_symtab (current_subfile->name,
10253 cu->objfile);
10254 fe->symtab = current_subfile->symtab;
10255 }
10256 }
c906108c
SS
10257}
10258
10259/* Start a subfile for DWARF. FILENAME is the name of the file and
10260 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10261 or NULL if not known. COMP_DIR is the compilation directory for the
10262 linetable's compilation unit or NULL if not known.
c906108c
SS
10263 This routine tries to keep line numbers from identical absolute and
10264 relative file names in a common subfile.
10265
10266 Using the `list' example from the GDB testsuite, which resides in
10267 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10268 of /srcdir/list0.c yields the following debugging information for list0.c:
10269
c5aa993b
JM
10270 DW_AT_name: /srcdir/list0.c
10271 DW_AT_comp_dir: /compdir
357e46e7 10272 files.files[0].name: list0.h
c5aa993b 10273 files.files[0].dir: /srcdir
357e46e7 10274 files.files[1].name: list0.c
c5aa993b 10275 files.files[1].dir: /srcdir
c906108c
SS
10276
10277 The line number information for list0.c has to end up in a single
4f1520fb
FR
10278 subfile, so that `break /srcdir/list0.c:1' works as expected.
10279 start_subfile will ensure that this happens provided that we pass the
10280 concatenation of files.files[1].dir and files.files[1].name as the
10281 subfile's name. */
c906108c
SS
10282
10283static void
4f1520fb 10284dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10285{
4f1520fb
FR
10286 char *fullname;
10287
10288 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10289 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10290 second argument to start_subfile. To be consistent, we do the
10291 same here. In order not to lose the line information directory,
10292 we concatenate it to the filename when it makes sense.
10293 Note that the Dwarf3 standard says (speaking of filenames in line
10294 information): ``The directory index is ignored for file names
10295 that represent full path names''. Thus ignoring dirname in the
10296 `else' branch below isn't an issue. */
c906108c 10297
d5166ae1 10298 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10299 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10300 else
10301 fullname = filename;
c906108c 10302
4f1520fb
FR
10303 start_subfile (fullname, comp_dir);
10304
10305 if (fullname != filename)
10306 xfree (fullname);
c906108c
SS
10307}
10308
4c2df51b
DJ
10309static void
10310var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10311 struct dwarf2_cu *cu)
4c2df51b 10312{
e7c27a73
DJ
10313 struct objfile *objfile = cu->objfile;
10314 struct comp_unit_head *cu_header = &cu->header;
10315
4c2df51b
DJ
10316 /* NOTE drow/2003-01-30: There used to be a comment and some special
10317 code here to turn a symbol with DW_AT_external and a
10318 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10319 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10320 with some versions of binutils) where shared libraries could have
10321 relocations against symbols in their debug information - the
10322 minimal symbol would have the right address, but the debug info
10323 would not. It's no longer necessary, because we will explicitly
10324 apply relocations when we read in the debug information now. */
10325
10326 /* A DW_AT_location attribute with no contents indicates that a
10327 variable has been optimized away. */
10328 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10329 {
10330 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10331 return;
10332 }
10333
10334 /* Handle one degenerate form of location expression specially, to
10335 preserve GDB's previous behavior when section offsets are
10336 specified. If this is just a DW_OP_addr then mark this symbol
10337 as LOC_STATIC. */
10338
10339 if (attr_form_is_block (attr)
10340 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10341 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10342 {
891d2f0b 10343 unsigned int dummy;
4c2df51b
DJ
10344
10345 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10346 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10347 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10348 fixup_symbol_section (sym, objfile);
10349 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10350 SYMBOL_SECTION (sym));
4c2df51b
DJ
10351 return;
10352 }
10353
10354 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10355 expression evaluator, and use LOC_COMPUTED only when necessary
10356 (i.e. when the value of a register or memory location is
10357 referenced, or a thread-local block, etc.). Then again, it might
10358 not be worthwhile. I'm assuming that it isn't unless performance
10359 or memory numbers show me otherwise. */
10360
e7c27a73 10361 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10362 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10363}
10364
c906108c
SS
10365/* Given a pointer to a DWARF information entry, figure out if we need
10366 to make a symbol table entry for it, and if so, create a new entry
10367 and return a pointer to it.
10368 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10369 used the passed type.
10370 If SPACE is not NULL, use it to hold the new symbol. If it is
10371 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10372
10373static struct symbol *
34eaf542
TT
10374new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10375 struct symbol *space)
c906108c 10376{
e7c27a73 10377 struct objfile *objfile = cu->objfile;
c906108c
SS
10378 struct symbol *sym = NULL;
10379 char *name;
10380 struct attribute *attr = NULL;
10381 struct attribute *attr2 = NULL;
e142c38c 10382 CORE_ADDR baseaddr;
e37fd15a
SW
10383 struct pending **list_to_add = NULL;
10384
edb3359d 10385 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10386
10387 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10388
94af9270 10389 name = dwarf2_name (die, cu);
c906108c
SS
10390 if (name)
10391 {
94af9270 10392 const char *linkagename;
34eaf542 10393 int suppress_add = 0;
94af9270 10394
34eaf542
TT
10395 if (space)
10396 sym = space;
10397 else
10398 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10399 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10400
10401 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10402 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10403 linkagename = dwarf2_physname (name, die, cu);
10404 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10405
f55ee35c
JK
10406 /* Fortran does not have mangling standard and the mangling does differ
10407 between gfortran, iFort etc. */
10408 if (cu->language == language_fortran
b250c185 10409 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10410 symbol_set_demangled_name (&(sym->ginfo),
10411 (char *) dwarf2_full_name (name, die, cu),
10412 NULL);
f55ee35c 10413
c906108c 10414 /* Default assumptions.
c5aa993b 10415 Use the passed type or decode it from the die. */
176620f1 10416 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10417 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10418 if (type != NULL)
10419 SYMBOL_TYPE (sym) = type;
10420 else
e7c27a73 10421 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10422 attr = dwarf2_attr (die,
10423 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10424 cu);
c906108c
SS
10425 if (attr)
10426 {
10427 SYMBOL_LINE (sym) = DW_UNSND (attr);
10428 }
cb1df416 10429
edb3359d
DJ
10430 attr = dwarf2_attr (die,
10431 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10432 cu);
cb1df416
DJ
10433 if (attr)
10434 {
10435 int file_index = DW_UNSND (attr);
9a619af0 10436
cb1df416
DJ
10437 if (cu->line_header == NULL
10438 || file_index > cu->line_header->num_file_names)
10439 complaint (&symfile_complaints,
10440 _("file index out of range"));
1c3d648d 10441 else if (file_index > 0)
cb1df416
DJ
10442 {
10443 struct file_entry *fe;
9a619af0 10444
cb1df416
DJ
10445 fe = &cu->line_header->file_names[file_index - 1];
10446 SYMBOL_SYMTAB (sym) = fe->symtab;
10447 }
10448 }
10449
c906108c
SS
10450 switch (die->tag)
10451 {
10452 case DW_TAG_label:
e142c38c 10453 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10454 if (attr)
10455 {
10456 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10457 }
0f5238ed
TT
10458 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10459 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10460 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10461 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10462 break;
10463 case DW_TAG_subprogram:
10464 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10465 finish_block. */
10466 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10467 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10468 if ((attr2 && (DW_UNSND (attr2) != 0))
10469 || cu->language == language_ada)
c906108c 10470 {
2cfa0c8d
JB
10471 /* Subprograms marked external are stored as a global symbol.
10472 Ada subprograms, whether marked external or not, are always
10473 stored as a global symbol, because we want to be able to
10474 access them globally. For instance, we want to be able
10475 to break on a nested subprogram without having to
10476 specify the context. */
e37fd15a 10477 list_to_add = &global_symbols;
c906108c
SS
10478 }
10479 else
10480 {
e37fd15a 10481 list_to_add = cu->list_in_scope;
c906108c
SS
10482 }
10483 break;
edb3359d
DJ
10484 case DW_TAG_inlined_subroutine:
10485 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10486 finish_block. */
10487 SYMBOL_CLASS (sym) = LOC_BLOCK;
10488 SYMBOL_INLINED (sym) = 1;
10489 /* Do not add the symbol to any lists. It will be found via
10490 BLOCK_FUNCTION from the blockvector. */
10491 break;
34eaf542
TT
10492 case DW_TAG_template_value_param:
10493 suppress_add = 1;
10494 /* Fall through. */
c906108c 10495 case DW_TAG_variable:
254e6b9e 10496 case DW_TAG_member:
c906108c
SS
10497 /* Compilation with minimal debug info may result in variables
10498 with missing type entries. Change the misleading `void' type
10499 to something sensible. */
10500 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10501 SYMBOL_TYPE (sym)
46bf5051 10502 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10503
e142c38c 10504 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10505 /* In the case of DW_TAG_member, we should only be called for
10506 static const members. */
10507 if (die->tag == DW_TAG_member)
10508 {
3863f96c
DE
10509 /* dwarf2_add_field uses die_is_declaration,
10510 so we do the same. */
254e6b9e
DE
10511 gdb_assert (die_is_declaration (die, cu));
10512 gdb_assert (attr);
10513 }
c906108c
SS
10514 if (attr)
10515 {
e7c27a73 10516 dwarf2_const_value (attr, sym, cu);
e142c38c 10517 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10518 if (!suppress_add)
34eaf542
TT
10519 {
10520 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10521 list_to_add = &global_symbols;
34eaf542 10522 else
e37fd15a 10523 list_to_add = cu->list_in_scope;
34eaf542 10524 }
c906108c
SS
10525 break;
10526 }
e142c38c 10527 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10528 if (attr)
10529 {
e7c27a73 10530 var_decode_location (attr, sym, cu);
e142c38c 10531 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10532 if (SYMBOL_CLASS (sym) == LOC_STATIC
10533 && SYMBOL_VALUE_ADDRESS (sym) == 0
10534 && !dwarf2_per_objfile->has_section_at_zero)
10535 {
10536 /* When a static variable is eliminated by the linker,
10537 the corresponding debug information is not stripped
10538 out, but the variable address is set to null;
10539 do not add such variables into symbol table. */
10540 }
10541 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10542 {
f55ee35c
JK
10543 /* Workaround gfortran PR debug/40040 - it uses
10544 DW_AT_location for variables in -fPIC libraries which may
10545 get overriden by other libraries/executable and get
10546 a different address. Resolve it by the minimal symbol
10547 which may come from inferior's executable using copy
10548 relocation. Make this workaround only for gfortran as for
10549 other compilers GDB cannot guess the minimal symbol
10550 Fortran mangling kind. */
10551 if (cu->language == language_fortran && die->parent
10552 && die->parent->tag == DW_TAG_module
10553 && cu->producer
10554 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10555 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10556
1c809c68
TT
10557 /* A variable with DW_AT_external is never static,
10558 but it may be block-scoped. */
10559 list_to_add = (cu->list_in_scope == &file_symbols
10560 ? &global_symbols : cu->list_in_scope);
1c809c68 10561 }
c906108c 10562 else
e37fd15a 10563 list_to_add = cu->list_in_scope;
c906108c
SS
10564 }
10565 else
10566 {
10567 /* We do not know the address of this symbol.
c5aa993b
JM
10568 If it is an external symbol and we have type information
10569 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10570 The address of the variable will then be determined from
10571 the minimal symbol table whenever the variable is
10572 referenced. */
e142c38c 10573 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10574 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10575 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10576 {
0fe7935b
DJ
10577 /* A variable with DW_AT_external is never static, but it
10578 may be block-scoped. */
10579 list_to_add = (cu->list_in_scope == &file_symbols
10580 ? &global_symbols : cu->list_in_scope);
10581
c906108c 10582 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10583 }
442ddf59
JK
10584 else if (!die_is_declaration (die, cu))
10585 {
10586 /* Use the default LOC_OPTIMIZED_OUT class. */
10587 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10588 if (!suppress_add)
10589 list_to_add = cu->list_in_scope;
442ddf59 10590 }
c906108c
SS
10591 }
10592 break;
10593 case DW_TAG_formal_parameter:
edb3359d
DJ
10594 /* If we are inside a function, mark this as an argument. If
10595 not, we might be looking at an argument to an inlined function
10596 when we do not have enough information to show inlined frames;
10597 pretend it's a local variable in that case so that the user can
10598 still see it. */
10599 if (context_stack_depth > 0
10600 && context_stack[context_stack_depth - 1].name != NULL)
10601 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10602 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10603 if (attr)
10604 {
e7c27a73 10605 var_decode_location (attr, sym, cu);
c906108c 10606 }
e142c38c 10607 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10608 if (attr)
10609 {
e7c27a73 10610 dwarf2_const_value (attr, sym, cu);
c906108c 10611 }
f346a30d
PM
10612 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10613 if (attr && DW_UNSND (attr))
10614 {
10615 struct type *ref_type;
10616
10617 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10618 SYMBOL_TYPE (sym) = ref_type;
10619 }
10620
e37fd15a 10621 list_to_add = cu->list_in_scope;
c906108c
SS
10622 break;
10623 case DW_TAG_unspecified_parameters:
10624 /* From varargs functions; gdb doesn't seem to have any
10625 interest in this information, so just ignore it for now.
10626 (FIXME?) */
10627 break;
34eaf542
TT
10628 case DW_TAG_template_type_param:
10629 suppress_add = 1;
10630 /* Fall through. */
c906108c 10631 case DW_TAG_class_type:
680b30c7 10632 case DW_TAG_interface_type:
c906108c
SS
10633 case DW_TAG_structure_type:
10634 case DW_TAG_union_type:
72019c9c 10635 case DW_TAG_set_type:
c906108c
SS
10636 case DW_TAG_enumeration_type:
10637 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10638 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10639
63d06c5c 10640 {
987504bb 10641 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10642 really ever be static objects: otherwise, if you try
10643 to, say, break of a class's method and you're in a file
10644 which doesn't mention that class, it won't work unless
10645 the check for all static symbols in lookup_symbol_aux
10646 saves you. See the OtherFileClass tests in
10647 gdb.c++/namespace.exp. */
10648
e37fd15a 10649 if (!suppress_add)
34eaf542 10650 {
34eaf542
TT
10651 list_to_add = (cu->list_in_scope == &file_symbols
10652 && (cu->language == language_cplus
10653 || cu->language == language_java)
10654 ? &global_symbols : cu->list_in_scope);
34eaf542 10655 }
63d06c5c
DC
10656
10657 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 10658 defines a typedef for "foo". A Java class declaration also
5eeb2539 10659 defines a typedef for the class. */
987504bb 10660 if (cu->language == language_cplus
8c6860bb
JB
10661 || cu->language == language_java
10662 || cu->language == language_ada)
63d06c5c 10663 {
d8151005
DJ
10664 /* The symbol's name is already allocated along with
10665 this objfile, so we don't need to duplicate it for
10666 the type. */
63d06c5c 10667 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 10668 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
10669 }
10670 }
c906108c
SS
10671 break;
10672 case DW_TAG_typedef:
63d06c5c
DC
10673 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10674 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10675 list_to_add = cu->list_in_scope;
63d06c5c 10676 break;
c906108c 10677 case DW_TAG_base_type:
a02abb62 10678 case DW_TAG_subrange_type:
c906108c 10679 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10680 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10681 list_to_add = cu->list_in_scope;
c906108c
SS
10682 break;
10683 case DW_TAG_enumerator:
e142c38c 10684 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10685 if (attr)
10686 {
e7c27a73 10687 dwarf2_const_value (attr, sym, cu);
c906108c 10688 }
63d06c5c
DC
10689 {
10690 /* NOTE: carlton/2003-11-10: See comment above in the
10691 DW_TAG_class_type, etc. block. */
10692
e142c38c 10693 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10694 && (cu->language == language_cplus
10695 || cu->language == language_java)
e142c38c 10696 ? &global_symbols : cu->list_in_scope);
63d06c5c 10697 }
c906108c 10698 break;
5c4e30ca
DC
10699 case DW_TAG_namespace:
10700 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10701 list_to_add = &global_symbols;
5c4e30ca 10702 break;
c906108c
SS
10703 default:
10704 /* Not a tag we recognize. Hopefully we aren't processing
10705 trash data, but since we must specifically ignore things
10706 we don't recognize, there is nothing else we should do at
10707 this point. */
e2e0b3e5 10708 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10709 dwarf_tag_name (die->tag));
c906108c
SS
10710 break;
10711 }
df8a16a1 10712
e37fd15a
SW
10713 if (suppress_add)
10714 {
10715 sym->hash_next = objfile->template_symbols;
10716 objfile->template_symbols = sym;
10717 list_to_add = NULL;
10718 }
10719
10720 if (list_to_add != NULL)
10721 add_symbol_to_list (sym, list_to_add);
10722
df8a16a1
DJ
10723 /* For the benefit of old versions of GCC, check for anonymous
10724 namespaces based on the demangled name. */
10725 if (!processing_has_namespace_info
94af9270 10726 && cu->language == language_cplus)
df8a16a1 10727 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10728 }
10729 return (sym);
10730}
10731
34eaf542
TT
10732/* A wrapper for new_symbol_full that always allocates a new symbol. */
10733
10734static struct symbol *
10735new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10736{
10737 return new_symbol_full (die, type, cu, NULL);
10738}
10739
98bfdba5
PA
10740/* Given an attr with a DW_FORM_dataN value in host byte order,
10741 zero-extend it as appropriate for the symbol's type. The DWARF
10742 standard (v4) is not entirely clear about the meaning of using
10743 DW_FORM_dataN for a constant with a signed type, where the type is
10744 wider than the data. The conclusion of a discussion on the DWARF
10745 list was that this is unspecified. We choose to always zero-extend
10746 because that is the interpretation long in use by GCC. */
c906108c 10747
98bfdba5
PA
10748static gdb_byte *
10749dwarf2_const_value_data (struct attribute *attr, struct type *type,
10750 const char *name, struct obstack *obstack,
10751 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10752{
e7c27a73 10753 struct objfile *objfile = cu->objfile;
e17a4113
UW
10754 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10755 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10756 LONGEST l = DW_UNSND (attr);
10757
10758 if (bits < sizeof (*value) * 8)
10759 {
10760 l &= ((LONGEST) 1 << bits) - 1;
10761 *value = l;
10762 }
10763 else if (bits == sizeof (*value) * 8)
10764 *value = l;
10765 else
10766 {
10767 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10768 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10769 return bytes;
10770 }
10771
10772 return NULL;
10773}
10774
10775/* Read a constant value from an attribute. Either set *VALUE, or if
10776 the value does not fit in *VALUE, set *BYTES - either already
10777 allocated on the objfile obstack, or newly allocated on OBSTACK,
10778 or, set *BATON, if we translated the constant to a location
10779 expression. */
10780
10781static void
10782dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10783 const char *name, struct obstack *obstack,
10784 struct dwarf2_cu *cu,
10785 long *value, gdb_byte **bytes,
10786 struct dwarf2_locexpr_baton **baton)
10787{
10788 struct objfile *objfile = cu->objfile;
10789 struct comp_unit_head *cu_header = &cu->header;
c906108c 10790 struct dwarf_block *blk;
98bfdba5
PA
10791 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10792 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10793
10794 *value = 0;
10795 *bytes = NULL;
10796 *baton = NULL;
c906108c
SS
10797
10798 switch (attr->form)
10799 {
10800 case DW_FORM_addr:
ac56253d 10801 {
ac56253d
TT
10802 gdb_byte *data;
10803
98bfdba5
PA
10804 if (TYPE_LENGTH (type) != cu_header->addr_size)
10805 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10806 cu_header->addr_size,
98bfdba5 10807 TYPE_LENGTH (type));
ac56253d
TT
10808 /* Symbols of this form are reasonably rare, so we just
10809 piggyback on the existing location code rather than writing
10810 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10811 *baton = obstack_alloc (&objfile->objfile_obstack,
10812 sizeof (struct dwarf2_locexpr_baton));
10813 (*baton)->per_cu = cu->per_cu;
10814 gdb_assert ((*baton)->per_cu);
ac56253d 10815
98bfdba5
PA
10816 (*baton)->size = 2 + cu_header->addr_size;
10817 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10818 (*baton)->data = data;
ac56253d
TT
10819
10820 data[0] = DW_OP_addr;
10821 store_unsigned_integer (&data[1], cu_header->addr_size,
10822 byte_order, DW_ADDR (attr));
10823 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10824 }
c906108c 10825 break;
4ac36638 10826 case DW_FORM_string:
93b5768b 10827 case DW_FORM_strp:
98bfdba5
PA
10828 /* DW_STRING is already allocated on the objfile obstack, point
10829 directly to it. */
10830 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10831 break;
c906108c
SS
10832 case DW_FORM_block1:
10833 case DW_FORM_block2:
10834 case DW_FORM_block4:
10835 case DW_FORM_block:
2dc7f7b3 10836 case DW_FORM_exprloc:
c906108c 10837 blk = DW_BLOCK (attr);
98bfdba5
PA
10838 if (TYPE_LENGTH (type) != blk->size)
10839 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10840 TYPE_LENGTH (type));
10841 *bytes = blk->data;
c906108c 10842 break;
2df3850c
JM
10843
10844 /* The DW_AT_const_value attributes are supposed to carry the
10845 symbol's value "represented as it would be on the target
10846 architecture." By the time we get here, it's already been
10847 converted to host endianness, so we just need to sign- or
10848 zero-extend it as appropriate. */
10849 case DW_FORM_data1:
98bfdba5 10850 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10851 break;
c906108c 10852 case DW_FORM_data2:
98bfdba5 10853 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10854 break;
c906108c 10855 case DW_FORM_data4:
98bfdba5 10856 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10857 break;
c906108c 10858 case DW_FORM_data8:
98bfdba5 10859 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
10860 break;
10861
c906108c 10862 case DW_FORM_sdata:
98bfdba5 10863 *value = DW_SND (attr);
2df3850c
JM
10864 break;
10865
c906108c 10866 case DW_FORM_udata:
98bfdba5 10867 *value = DW_UNSND (attr);
c906108c 10868 break;
2df3850c 10869
c906108c 10870 default:
4d3c2250 10871 complaint (&symfile_complaints,
e2e0b3e5 10872 _("unsupported const value attribute form: '%s'"),
4d3c2250 10873 dwarf_form_name (attr->form));
98bfdba5 10874 *value = 0;
c906108c
SS
10875 break;
10876 }
10877}
10878
2df3850c 10879
98bfdba5
PA
10880/* Copy constant value from an attribute to a symbol. */
10881
2df3850c 10882static void
98bfdba5
PA
10883dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10884 struct dwarf2_cu *cu)
2df3850c 10885{
98bfdba5
PA
10886 struct objfile *objfile = cu->objfile;
10887 struct comp_unit_head *cu_header = &cu->header;
10888 long value;
10889 gdb_byte *bytes;
10890 struct dwarf2_locexpr_baton *baton;
2df3850c 10891
98bfdba5
PA
10892 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10893 SYMBOL_PRINT_NAME (sym),
10894 &objfile->objfile_obstack, cu,
10895 &value, &bytes, &baton);
2df3850c 10896
98bfdba5
PA
10897 if (baton != NULL)
10898 {
10899 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10900 SYMBOL_LOCATION_BATON (sym) = baton;
10901 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10902 }
10903 else if (bytes != NULL)
10904 {
10905 SYMBOL_VALUE_BYTES (sym) = bytes;
10906 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10907 }
10908 else
10909 {
10910 SYMBOL_VALUE (sym) = value;
10911 SYMBOL_CLASS (sym) = LOC_CONST;
10912 }
2df3850c
JM
10913}
10914
c906108c
SS
10915/* Return the type of the die in question using its DW_AT_type attribute. */
10916
10917static struct type *
e7c27a73 10918die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10919{
c906108c 10920 struct attribute *type_attr;
c906108c 10921
e142c38c 10922 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10923 if (!type_attr)
10924 {
10925 /* A missing DW_AT_type represents a void type. */
46bf5051 10926 return objfile_type (cu->objfile)->builtin_void;
c906108c 10927 }
348e048f 10928
673bfd45 10929 return lookup_die_type (die, type_attr, cu);
c906108c
SS
10930}
10931
b4ba55a1
JB
10932/* True iff CU's producer generates GNAT Ada auxiliary information
10933 that allows to find parallel types through that information instead
10934 of having to do expensive parallel lookups by type name. */
10935
10936static int
10937need_gnat_info (struct dwarf2_cu *cu)
10938{
10939 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10940 of GNAT produces this auxiliary information, without any indication
10941 that it is produced. Part of enhancing the FSF version of GNAT
10942 to produce that information will be to put in place an indicator
10943 that we can use in order to determine whether the descriptive type
10944 info is available or not. One suggestion that has been made is
10945 to use a new attribute, attached to the CU die. For now, assume
10946 that the descriptive type info is not available. */
10947 return 0;
10948}
10949
b4ba55a1
JB
10950/* Return the auxiliary type of the die in question using its
10951 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10952 attribute is not present. */
10953
10954static struct type *
10955die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10956{
b4ba55a1 10957 struct attribute *type_attr;
b4ba55a1
JB
10958
10959 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10960 if (!type_attr)
10961 return NULL;
10962
673bfd45 10963 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
10964}
10965
10966/* If DIE has a descriptive_type attribute, then set the TYPE's
10967 descriptive type accordingly. */
10968
10969static void
10970set_descriptive_type (struct type *type, struct die_info *die,
10971 struct dwarf2_cu *cu)
10972{
10973 struct type *descriptive_type = die_descriptive_type (die, cu);
10974
10975 if (descriptive_type)
10976 {
10977 ALLOCATE_GNAT_AUX_TYPE (type);
10978 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10979 }
10980}
10981
c906108c
SS
10982/* Return the containing type of the die in question using its
10983 DW_AT_containing_type attribute. */
10984
10985static struct type *
e7c27a73 10986die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10987{
c906108c 10988 struct attribute *type_attr;
c906108c 10989
e142c38c 10990 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
10991 if (!type_attr)
10992 error (_("Dwarf Error: Problem turning containing type into gdb type "
10993 "[in module %s]"), cu->objfile->name);
10994
673bfd45 10995 return lookup_die_type (die, type_attr, cu);
c906108c
SS
10996}
10997
673bfd45
DE
10998/* Look up the type of DIE in CU using its type attribute ATTR.
10999 If there is no type substitute an error marker. */
11000
c906108c 11001static struct type *
673bfd45
DE
11002lookup_die_type (struct die_info *die, struct attribute *attr,
11003 struct dwarf2_cu *cu)
c906108c 11004{
f792889a
DJ
11005 struct type *this_type;
11006
673bfd45
DE
11007 /* First see if we have it cached. */
11008
11009 if (is_ref_attr (attr))
11010 {
11011 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11012
11013 this_type = get_die_type_at_offset (offset, cu->per_cu);
11014 }
11015 else if (attr->form == DW_FORM_sig8)
11016 {
11017 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11018 struct dwarf2_cu *sig_cu;
11019 unsigned int offset;
11020
11021 /* sig_type will be NULL if the signatured type is missing from
11022 the debug info. */
11023 if (sig_type == NULL)
11024 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11025 "at 0x%x [in module %s]"),
11026 die->offset, cu->objfile->name);
11027
11028 gdb_assert (sig_type->per_cu.from_debug_types);
11029 offset = sig_type->offset + sig_type->type_offset;
11030 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11031 }
11032 else
11033 {
11034 dump_die_for_error (die);
11035 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11036 dwarf_attr_name (attr->name), cu->objfile->name);
11037 }
11038
11039 /* If not cached we need to read it in. */
11040
11041 if (this_type == NULL)
11042 {
11043 struct die_info *type_die;
11044 struct dwarf2_cu *type_cu = cu;
11045
11046 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11047 /* If the type is cached, we should have found it above. */
11048 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11049 this_type = read_type_die_1 (type_die, type_cu);
11050 }
11051
11052 /* If we still don't have a type use an error marker. */
11053
11054 if (this_type == NULL)
c906108c 11055 {
b00fdb78
TT
11056 char *message, *saved;
11057
11058 /* read_type_die already issued a complaint. */
11059 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11060 cu->objfile->name,
11061 cu->header.offset,
11062 die->offset);
11063 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11064 message, strlen (message));
11065 xfree (message);
11066
11067 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11068 }
673bfd45 11069
f792889a 11070 return this_type;
c906108c
SS
11071}
11072
673bfd45
DE
11073/* Return the type in DIE, CU.
11074 Returns NULL for invalid types.
11075
11076 This first does a lookup in the appropriate type_hash table,
11077 and only reads the die in if necessary.
11078
11079 NOTE: This can be called when reading in partial or full symbols. */
11080
f792889a 11081static struct type *
e7c27a73 11082read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11083{
f792889a
DJ
11084 struct type *this_type;
11085
11086 this_type = get_die_type (die, cu);
11087 if (this_type)
11088 return this_type;
11089
673bfd45
DE
11090 return read_type_die_1 (die, cu);
11091}
11092
11093/* Read the type in DIE, CU.
11094 Returns NULL for invalid types. */
11095
11096static struct type *
11097read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11098{
11099 struct type *this_type = NULL;
11100
c906108c
SS
11101 switch (die->tag)
11102 {
11103 case DW_TAG_class_type:
680b30c7 11104 case DW_TAG_interface_type:
c906108c
SS
11105 case DW_TAG_structure_type:
11106 case DW_TAG_union_type:
f792889a 11107 this_type = read_structure_type (die, cu);
c906108c
SS
11108 break;
11109 case DW_TAG_enumeration_type:
f792889a 11110 this_type = read_enumeration_type (die, cu);
c906108c
SS
11111 break;
11112 case DW_TAG_subprogram:
11113 case DW_TAG_subroutine_type:
edb3359d 11114 case DW_TAG_inlined_subroutine:
f792889a 11115 this_type = read_subroutine_type (die, cu);
c906108c
SS
11116 break;
11117 case DW_TAG_array_type:
f792889a 11118 this_type = read_array_type (die, cu);
c906108c 11119 break;
72019c9c 11120 case DW_TAG_set_type:
f792889a 11121 this_type = read_set_type (die, cu);
72019c9c 11122 break;
c906108c 11123 case DW_TAG_pointer_type:
f792889a 11124 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11125 break;
11126 case DW_TAG_ptr_to_member_type:
f792889a 11127 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11128 break;
11129 case DW_TAG_reference_type:
f792889a 11130 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11131 break;
11132 case DW_TAG_const_type:
f792889a 11133 this_type = read_tag_const_type (die, cu);
c906108c
SS
11134 break;
11135 case DW_TAG_volatile_type:
f792889a 11136 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11137 break;
11138 case DW_TAG_string_type:
f792889a 11139 this_type = read_tag_string_type (die, cu);
c906108c
SS
11140 break;
11141 case DW_TAG_typedef:
f792889a 11142 this_type = read_typedef (die, cu);
c906108c 11143 break;
a02abb62 11144 case DW_TAG_subrange_type:
f792889a 11145 this_type = read_subrange_type (die, cu);
a02abb62 11146 break;
c906108c 11147 case DW_TAG_base_type:
f792889a 11148 this_type = read_base_type (die, cu);
c906108c 11149 break;
81a17f79 11150 case DW_TAG_unspecified_type:
f792889a 11151 this_type = read_unspecified_type (die, cu);
81a17f79 11152 break;
0114d602
DJ
11153 case DW_TAG_namespace:
11154 this_type = read_namespace_type (die, cu);
11155 break;
f55ee35c
JK
11156 case DW_TAG_module:
11157 this_type = read_module_type (die, cu);
11158 break;
c906108c 11159 default:
a1f5b845 11160 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11161 dwarf_tag_name (die->tag));
c906108c
SS
11162 break;
11163 }
63d06c5c 11164
f792889a 11165 return this_type;
63d06c5c
DC
11166}
11167
fdde2d81 11168/* Return the name of the namespace/class that DIE is defined within,
0114d602 11169 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11170
0114d602
DJ
11171 For example, if we're within the method foo() in the following
11172 code:
11173
11174 namespace N {
11175 class C {
11176 void foo () {
11177 }
11178 };
11179 }
11180
11181 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11182
11183static char *
e142c38c 11184determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11185{
0114d602
DJ
11186 struct die_info *parent, *spec_die;
11187 struct dwarf2_cu *spec_cu;
11188 struct type *parent_type;
63d06c5c 11189
f55ee35c
JK
11190 if (cu->language != language_cplus && cu->language != language_java
11191 && cu->language != language_fortran)
0114d602
DJ
11192 return "";
11193
11194 /* We have to be careful in the presence of DW_AT_specification.
11195 For example, with GCC 3.4, given the code
11196
11197 namespace N {
11198 void foo() {
11199 // Definition of N::foo.
11200 }
11201 }
11202
11203 then we'll have a tree of DIEs like this:
11204
11205 1: DW_TAG_compile_unit
11206 2: DW_TAG_namespace // N
11207 3: DW_TAG_subprogram // declaration of N::foo
11208 4: DW_TAG_subprogram // definition of N::foo
11209 DW_AT_specification // refers to die #3
11210
11211 Thus, when processing die #4, we have to pretend that we're in
11212 the context of its DW_AT_specification, namely the contex of die
11213 #3. */
11214 spec_cu = cu;
11215 spec_die = die_specification (die, &spec_cu);
11216 if (spec_die == NULL)
11217 parent = die->parent;
11218 else
63d06c5c 11219 {
0114d602
DJ
11220 parent = spec_die->parent;
11221 cu = spec_cu;
63d06c5c 11222 }
0114d602
DJ
11223
11224 if (parent == NULL)
11225 return "";
98bfdba5
PA
11226 else if (parent->building_fullname)
11227 {
11228 const char *name;
11229 const char *parent_name;
11230
11231 /* It has been seen on RealView 2.2 built binaries,
11232 DW_TAG_template_type_param types actually _defined_ as
11233 children of the parent class:
11234
11235 enum E {};
11236 template class <class Enum> Class{};
11237 Class<enum E> class_e;
11238
11239 1: DW_TAG_class_type (Class)
11240 2: DW_TAG_enumeration_type (E)
11241 3: DW_TAG_enumerator (enum1:0)
11242 3: DW_TAG_enumerator (enum2:1)
11243 ...
11244 2: DW_TAG_template_type_param
11245 DW_AT_type DW_FORM_ref_udata (E)
11246
11247 Besides being broken debug info, it can put GDB into an
11248 infinite loop. Consider:
11249
11250 When we're building the full name for Class<E>, we'll start
11251 at Class, and go look over its template type parameters,
11252 finding E. We'll then try to build the full name of E, and
11253 reach here. We're now trying to build the full name of E,
11254 and look over the parent DIE for containing scope. In the
11255 broken case, if we followed the parent DIE of E, we'd again
11256 find Class, and once again go look at its template type
11257 arguments, etc., etc. Simply don't consider such parent die
11258 as source-level parent of this die (it can't be, the language
11259 doesn't allow it), and break the loop here. */
11260 name = dwarf2_name (die, cu);
11261 parent_name = dwarf2_name (parent, cu);
11262 complaint (&symfile_complaints,
11263 _("template param type '%s' defined within parent '%s'"),
11264 name ? name : "<unknown>",
11265 parent_name ? parent_name : "<unknown>");
11266 return "";
11267 }
63d06c5c 11268 else
0114d602
DJ
11269 switch (parent->tag)
11270 {
63d06c5c 11271 case DW_TAG_namespace:
0114d602 11272 parent_type = read_type_die (parent, cu);
acebe513
UW
11273 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11274 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11275 Work around this problem here. */
11276 if (cu->language == language_cplus
11277 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11278 return "";
0114d602
DJ
11279 /* We give a name to even anonymous namespaces. */
11280 return TYPE_TAG_NAME (parent_type);
63d06c5c 11281 case DW_TAG_class_type:
680b30c7 11282 case DW_TAG_interface_type:
63d06c5c 11283 case DW_TAG_structure_type:
0114d602 11284 case DW_TAG_union_type:
f55ee35c 11285 case DW_TAG_module:
0114d602
DJ
11286 parent_type = read_type_die (parent, cu);
11287 if (TYPE_TAG_NAME (parent_type) != NULL)
11288 return TYPE_TAG_NAME (parent_type);
11289 else
11290 /* An anonymous structure is only allowed non-static data
11291 members; no typedefs, no member functions, et cetera.
11292 So it does not need a prefix. */
11293 return "";
63d06c5c 11294 default:
8176b9b8 11295 return determine_prefix (parent, cu);
63d06c5c 11296 }
63d06c5c
DC
11297}
11298
987504bb
JJ
11299/* Return a newly-allocated string formed by concatenating PREFIX and
11300 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11301 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11302 perform an obconcat, otherwise allocate storage for the result. The CU argument
11303 is used to determine the language and hence, the appropriate separator. */
11304
f55ee35c 11305#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11306
11307static char *
f55ee35c
JK
11308typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11309 int physname, struct dwarf2_cu *cu)
63d06c5c 11310{
f55ee35c 11311 const char *lead = "";
5c315b68 11312 const char *sep;
63d06c5c 11313
987504bb
JJ
11314 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11315 sep = "";
11316 else if (cu->language == language_java)
11317 sep = ".";
f55ee35c
JK
11318 else if (cu->language == language_fortran && physname)
11319 {
11320 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11321 DW_AT_MIPS_linkage_name is preferred and used instead. */
11322
11323 lead = "__";
11324 sep = "_MOD_";
11325 }
987504bb
JJ
11326 else
11327 sep = "::";
63d06c5c 11328
6dd47d34
DE
11329 if (prefix == NULL)
11330 prefix = "";
11331 if (suffix == NULL)
11332 suffix = "";
11333
987504bb
JJ
11334 if (obs == NULL)
11335 {
11336 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11337
f55ee35c
JK
11338 strcpy (retval, lead);
11339 strcat (retval, prefix);
6dd47d34
DE
11340 strcat (retval, sep);
11341 strcat (retval, suffix);
63d06c5c
DC
11342 return retval;
11343 }
987504bb
JJ
11344 else
11345 {
11346 /* We have an obstack. */
f55ee35c 11347 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11348 }
63d06c5c
DC
11349}
11350
c906108c
SS
11351/* Return sibling of die, NULL if no sibling. */
11352
f9aca02d 11353static struct die_info *
fba45db2 11354sibling_die (struct die_info *die)
c906108c 11355{
639d11d3 11356 return die->sibling;
c906108c
SS
11357}
11358
71c25dea
TT
11359/* Get name of a die, return NULL if not found. */
11360
11361static char *
11362dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11363 struct obstack *obstack)
11364{
11365 if (name && cu->language == language_cplus)
11366 {
11367 char *canon_name = cp_canonicalize_string (name);
11368
11369 if (canon_name != NULL)
11370 {
11371 if (strcmp (canon_name, name) != 0)
11372 name = obsavestring (canon_name, strlen (canon_name),
11373 obstack);
11374 xfree (canon_name);
11375 }
11376 }
11377
11378 return name;
c906108c
SS
11379}
11380
9219021c
DC
11381/* Get name of a die, return NULL if not found. */
11382
11383static char *
e142c38c 11384dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11385{
11386 struct attribute *attr;
11387
e142c38c 11388 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11389 if (!attr || !DW_STRING (attr))
11390 return NULL;
11391
11392 switch (die->tag)
11393 {
11394 case DW_TAG_compile_unit:
11395 /* Compilation units have a DW_AT_name that is a filename, not
11396 a source language identifier. */
11397 case DW_TAG_enumeration_type:
11398 case DW_TAG_enumerator:
11399 /* These tags always have simple identifiers already; no need
11400 to canonicalize them. */
11401 return DW_STRING (attr);
907af001 11402
418835cc
KS
11403 case DW_TAG_subprogram:
11404 /* Java constructors will all be named "<init>", so return
11405 the class name when we see this special case. */
11406 if (cu->language == language_java
11407 && DW_STRING (attr) != NULL
11408 && strcmp (DW_STRING (attr), "<init>") == 0)
11409 {
11410 struct dwarf2_cu *spec_cu = cu;
11411 struct die_info *spec_die;
11412
11413 /* GCJ will output '<init>' for Java constructor names.
11414 For this special case, return the name of the parent class. */
11415
11416 /* GCJ may output suprogram DIEs with AT_specification set.
11417 If so, use the name of the specified DIE. */
11418 spec_die = die_specification (die, &spec_cu);
11419 if (spec_die != NULL)
11420 return dwarf2_name (spec_die, spec_cu);
11421
11422 do
11423 {
11424 die = die->parent;
11425 if (die->tag == DW_TAG_class_type)
11426 return dwarf2_name (die, cu);
11427 }
11428 while (die->tag != DW_TAG_compile_unit);
11429 }
907af001
UW
11430 break;
11431
11432 case DW_TAG_class_type:
11433 case DW_TAG_interface_type:
11434 case DW_TAG_structure_type:
11435 case DW_TAG_union_type:
11436 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11437 structures or unions. These were of the form "._%d" in GCC 4.1,
11438 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11439 and GCC 4.4. We work around this problem by ignoring these. */
11440 if (strncmp (DW_STRING (attr), "._", 2) == 0
11441 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11442 return NULL;
11443 break;
11444
71c25dea 11445 default:
907af001
UW
11446 break;
11447 }
11448
11449 if (!DW_STRING_IS_CANONICAL (attr))
11450 {
11451 DW_STRING (attr)
11452 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11453 &cu->objfile->objfile_obstack);
11454 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11455 }
907af001 11456 return DW_STRING (attr);
9219021c
DC
11457}
11458
11459/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11460 is none. *EXT_CU is the CU containing DIE on input, and the CU
11461 containing the return value on output. */
9219021c
DC
11462
11463static struct die_info *
f2f0e013 11464dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11465{
11466 struct attribute *attr;
9219021c 11467
f2f0e013 11468 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11469 if (attr == NULL)
11470 return NULL;
11471
f2f0e013 11472 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11473}
11474
c906108c
SS
11475/* Convert a DIE tag into its string name. */
11476
11477static char *
aa1ee363 11478dwarf_tag_name (unsigned tag)
c906108c
SS
11479{
11480 switch (tag)
11481 {
11482 case DW_TAG_padding:
11483 return "DW_TAG_padding";
11484 case DW_TAG_array_type:
11485 return "DW_TAG_array_type";
11486 case DW_TAG_class_type:
11487 return "DW_TAG_class_type";
11488 case DW_TAG_entry_point:
11489 return "DW_TAG_entry_point";
11490 case DW_TAG_enumeration_type:
11491 return "DW_TAG_enumeration_type";
11492 case DW_TAG_formal_parameter:
11493 return "DW_TAG_formal_parameter";
11494 case DW_TAG_imported_declaration:
11495 return "DW_TAG_imported_declaration";
11496 case DW_TAG_label:
11497 return "DW_TAG_label";
11498 case DW_TAG_lexical_block:
11499 return "DW_TAG_lexical_block";
11500 case DW_TAG_member:
11501 return "DW_TAG_member";
11502 case DW_TAG_pointer_type:
11503 return "DW_TAG_pointer_type";
11504 case DW_TAG_reference_type:
11505 return "DW_TAG_reference_type";
11506 case DW_TAG_compile_unit:
11507 return "DW_TAG_compile_unit";
11508 case DW_TAG_string_type:
11509 return "DW_TAG_string_type";
11510 case DW_TAG_structure_type:
11511 return "DW_TAG_structure_type";
11512 case DW_TAG_subroutine_type:
11513 return "DW_TAG_subroutine_type";
11514 case DW_TAG_typedef:
11515 return "DW_TAG_typedef";
11516 case DW_TAG_union_type:
11517 return "DW_TAG_union_type";
11518 case DW_TAG_unspecified_parameters:
11519 return "DW_TAG_unspecified_parameters";
11520 case DW_TAG_variant:
11521 return "DW_TAG_variant";
11522 case DW_TAG_common_block:
11523 return "DW_TAG_common_block";
11524 case DW_TAG_common_inclusion:
11525 return "DW_TAG_common_inclusion";
11526 case DW_TAG_inheritance:
11527 return "DW_TAG_inheritance";
11528 case DW_TAG_inlined_subroutine:
11529 return "DW_TAG_inlined_subroutine";
11530 case DW_TAG_module:
11531 return "DW_TAG_module";
11532 case DW_TAG_ptr_to_member_type:
11533 return "DW_TAG_ptr_to_member_type";
11534 case DW_TAG_set_type:
11535 return "DW_TAG_set_type";
11536 case DW_TAG_subrange_type:
11537 return "DW_TAG_subrange_type";
11538 case DW_TAG_with_stmt:
11539 return "DW_TAG_with_stmt";
11540 case DW_TAG_access_declaration:
11541 return "DW_TAG_access_declaration";
11542 case DW_TAG_base_type:
11543 return "DW_TAG_base_type";
11544 case DW_TAG_catch_block:
11545 return "DW_TAG_catch_block";
11546 case DW_TAG_const_type:
11547 return "DW_TAG_const_type";
11548 case DW_TAG_constant:
11549 return "DW_TAG_constant";
11550 case DW_TAG_enumerator:
11551 return "DW_TAG_enumerator";
11552 case DW_TAG_file_type:
11553 return "DW_TAG_file_type";
11554 case DW_TAG_friend:
11555 return "DW_TAG_friend";
11556 case DW_TAG_namelist:
11557 return "DW_TAG_namelist";
11558 case DW_TAG_namelist_item:
11559 return "DW_TAG_namelist_item";
11560 case DW_TAG_packed_type:
11561 return "DW_TAG_packed_type";
11562 case DW_TAG_subprogram:
11563 return "DW_TAG_subprogram";
11564 case DW_TAG_template_type_param:
11565 return "DW_TAG_template_type_param";
11566 case DW_TAG_template_value_param:
11567 return "DW_TAG_template_value_param";
11568 case DW_TAG_thrown_type:
11569 return "DW_TAG_thrown_type";
11570 case DW_TAG_try_block:
11571 return "DW_TAG_try_block";
11572 case DW_TAG_variant_part:
11573 return "DW_TAG_variant_part";
11574 case DW_TAG_variable:
11575 return "DW_TAG_variable";
11576 case DW_TAG_volatile_type:
11577 return "DW_TAG_volatile_type";
d9fa45fe
DC
11578 case DW_TAG_dwarf_procedure:
11579 return "DW_TAG_dwarf_procedure";
11580 case DW_TAG_restrict_type:
11581 return "DW_TAG_restrict_type";
11582 case DW_TAG_interface_type:
11583 return "DW_TAG_interface_type";
11584 case DW_TAG_namespace:
11585 return "DW_TAG_namespace";
11586 case DW_TAG_imported_module:
11587 return "DW_TAG_imported_module";
11588 case DW_TAG_unspecified_type:
11589 return "DW_TAG_unspecified_type";
11590 case DW_TAG_partial_unit:
11591 return "DW_TAG_partial_unit";
11592 case DW_TAG_imported_unit:
11593 return "DW_TAG_imported_unit";
b7619582
GF
11594 case DW_TAG_condition:
11595 return "DW_TAG_condition";
11596 case DW_TAG_shared_type:
11597 return "DW_TAG_shared_type";
348e048f
DE
11598 case DW_TAG_type_unit:
11599 return "DW_TAG_type_unit";
c906108c
SS
11600 case DW_TAG_MIPS_loop:
11601 return "DW_TAG_MIPS_loop";
b7619582
GF
11602 case DW_TAG_HP_array_descriptor:
11603 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11604 case DW_TAG_format_label:
11605 return "DW_TAG_format_label";
11606 case DW_TAG_function_template:
11607 return "DW_TAG_function_template";
11608 case DW_TAG_class_template:
11609 return "DW_TAG_class_template";
b7619582
GF
11610 case DW_TAG_GNU_BINCL:
11611 return "DW_TAG_GNU_BINCL";
11612 case DW_TAG_GNU_EINCL:
11613 return "DW_TAG_GNU_EINCL";
11614 case DW_TAG_upc_shared_type:
11615 return "DW_TAG_upc_shared_type";
11616 case DW_TAG_upc_strict_type:
11617 return "DW_TAG_upc_strict_type";
11618 case DW_TAG_upc_relaxed_type:
11619 return "DW_TAG_upc_relaxed_type";
11620 case DW_TAG_PGI_kanji_type:
11621 return "DW_TAG_PGI_kanji_type";
11622 case DW_TAG_PGI_interface_block:
11623 return "DW_TAG_PGI_interface_block";
c906108c
SS
11624 default:
11625 return "DW_TAG_<unknown>";
11626 }
11627}
11628
11629/* Convert a DWARF attribute code into its string name. */
11630
11631static char *
aa1ee363 11632dwarf_attr_name (unsigned attr)
c906108c
SS
11633{
11634 switch (attr)
11635 {
11636 case DW_AT_sibling:
11637 return "DW_AT_sibling";
11638 case DW_AT_location:
11639 return "DW_AT_location";
11640 case DW_AT_name:
11641 return "DW_AT_name";
11642 case DW_AT_ordering:
11643 return "DW_AT_ordering";
11644 case DW_AT_subscr_data:
11645 return "DW_AT_subscr_data";
11646 case DW_AT_byte_size:
11647 return "DW_AT_byte_size";
11648 case DW_AT_bit_offset:
11649 return "DW_AT_bit_offset";
11650 case DW_AT_bit_size:
11651 return "DW_AT_bit_size";
11652 case DW_AT_element_list:
11653 return "DW_AT_element_list";
11654 case DW_AT_stmt_list:
11655 return "DW_AT_stmt_list";
11656 case DW_AT_low_pc:
11657 return "DW_AT_low_pc";
11658 case DW_AT_high_pc:
11659 return "DW_AT_high_pc";
11660 case DW_AT_language:
11661 return "DW_AT_language";
11662 case DW_AT_member:
11663 return "DW_AT_member";
11664 case DW_AT_discr:
11665 return "DW_AT_discr";
11666 case DW_AT_discr_value:
11667 return "DW_AT_discr_value";
11668 case DW_AT_visibility:
11669 return "DW_AT_visibility";
11670 case DW_AT_import:
11671 return "DW_AT_import";
11672 case DW_AT_string_length:
11673 return "DW_AT_string_length";
11674 case DW_AT_common_reference:
11675 return "DW_AT_common_reference";
11676 case DW_AT_comp_dir:
11677 return "DW_AT_comp_dir";
11678 case DW_AT_const_value:
11679 return "DW_AT_const_value";
11680 case DW_AT_containing_type:
11681 return "DW_AT_containing_type";
11682 case DW_AT_default_value:
11683 return "DW_AT_default_value";
11684 case DW_AT_inline:
11685 return "DW_AT_inline";
11686 case DW_AT_is_optional:
11687 return "DW_AT_is_optional";
11688 case DW_AT_lower_bound:
11689 return "DW_AT_lower_bound";
11690 case DW_AT_producer:
11691 return "DW_AT_producer";
11692 case DW_AT_prototyped:
11693 return "DW_AT_prototyped";
11694 case DW_AT_return_addr:
11695 return "DW_AT_return_addr";
11696 case DW_AT_start_scope:
11697 return "DW_AT_start_scope";
09fa0d7c
JK
11698 case DW_AT_bit_stride:
11699 return "DW_AT_bit_stride";
c906108c
SS
11700 case DW_AT_upper_bound:
11701 return "DW_AT_upper_bound";
11702 case DW_AT_abstract_origin:
11703 return "DW_AT_abstract_origin";
11704 case DW_AT_accessibility:
11705 return "DW_AT_accessibility";
11706 case DW_AT_address_class:
11707 return "DW_AT_address_class";
11708 case DW_AT_artificial:
11709 return "DW_AT_artificial";
11710 case DW_AT_base_types:
11711 return "DW_AT_base_types";
11712 case DW_AT_calling_convention:
11713 return "DW_AT_calling_convention";
11714 case DW_AT_count:
11715 return "DW_AT_count";
11716 case DW_AT_data_member_location:
11717 return "DW_AT_data_member_location";
11718 case DW_AT_decl_column:
11719 return "DW_AT_decl_column";
11720 case DW_AT_decl_file:
11721 return "DW_AT_decl_file";
11722 case DW_AT_decl_line:
11723 return "DW_AT_decl_line";
11724 case DW_AT_declaration:
11725 return "DW_AT_declaration";
11726 case DW_AT_discr_list:
11727 return "DW_AT_discr_list";
11728 case DW_AT_encoding:
11729 return "DW_AT_encoding";
11730 case DW_AT_external:
11731 return "DW_AT_external";
11732 case DW_AT_frame_base:
11733 return "DW_AT_frame_base";
11734 case DW_AT_friend:
11735 return "DW_AT_friend";
11736 case DW_AT_identifier_case:
11737 return "DW_AT_identifier_case";
11738 case DW_AT_macro_info:
11739 return "DW_AT_macro_info";
11740 case DW_AT_namelist_items:
11741 return "DW_AT_namelist_items";
11742 case DW_AT_priority:
11743 return "DW_AT_priority";
11744 case DW_AT_segment:
11745 return "DW_AT_segment";
11746 case DW_AT_specification:
11747 return "DW_AT_specification";
11748 case DW_AT_static_link:
11749 return "DW_AT_static_link";
11750 case DW_AT_type:
11751 return "DW_AT_type";
11752 case DW_AT_use_location:
11753 return "DW_AT_use_location";
11754 case DW_AT_variable_parameter:
11755 return "DW_AT_variable_parameter";
11756 case DW_AT_virtuality:
11757 return "DW_AT_virtuality";
11758 case DW_AT_vtable_elem_location:
11759 return "DW_AT_vtable_elem_location";
b7619582 11760 /* DWARF 3 values. */
d9fa45fe
DC
11761 case DW_AT_allocated:
11762 return "DW_AT_allocated";
11763 case DW_AT_associated:
11764 return "DW_AT_associated";
11765 case DW_AT_data_location:
11766 return "DW_AT_data_location";
09fa0d7c
JK
11767 case DW_AT_byte_stride:
11768 return "DW_AT_byte_stride";
d9fa45fe
DC
11769 case DW_AT_entry_pc:
11770 return "DW_AT_entry_pc";
11771 case DW_AT_use_UTF8:
11772 return "DW_AT_use_UTF8";
11773 case DW_AT_extension:
11774 return "DW_AT_extension";
11775 case DW_AT_ranges:
11776 return "DW_AT_ranges";
11777 case DW_AT_trampoline:
11778 return "DW_AT_trampoline";
11779 case DW_AT_call_column:
11780 return "DW_AT_call_column";
11781 case DW_AT_call_file:
11782 return "DW_AT_call_file";
11783 case DW_AT_call_line:
11784 return "DW_AT_call_line";
b7619582
GF
11785 case DW_AT_description:
11786 return "DW_AT_description";
11787 case DW_AT_binary_scale:
11788 return "DW_AT_binary_scale";
11789 case DW_AT_decimal_scale:
11790 return "DW_AT_decimal_scale";
11791 case DW_AT_small:
11792 return "DW_AT_small";
11793 case DW_AT_decimal_sign:
11794 return "DW_AT_decimal_sign";
11795 case DW_AT_digit_count:
11796 return "DW_AT_digit_count";
11797 case DW_AT_picture_string:
11798 return "DW_AT_picture_string";
11799 case DW_AT_mutable:
11800 return "DW_AT_mutable";
11801 case DW_AT_threads_scaled:
11802 return "DW_AT_threads_scaled";
11803 case DW_AT_explicit:
11804 return "DW_AT_explicit";
11805 case DW_AT_object_pointer:
11806 return "DW_AT_object_pointer";
11807 case DW_AT_endianity:
11808 return "DW_AT_endianity";
11809 case DW_AT_elemental:
11810 return "DW_AT_elemental";
11811 case DW_AT_pure:
11812 return "DW_AT_pure";
11813 case DW_AT_recursive:
11814 return "DW_AT_recursive";
348e048f
DE
11815 /* DWARF 4 values. */
11816 case DW_AT_signature:
11817 return "DW_AT_signature";
31ef98ae
TT
11818 case DW_AT_linkage_name:
11819 return "DW_AT_linkage_name";
b7619582 11820 /* SGI/MIPS extensions. */
c764a876 11821#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11822 case DW_AT_MIPS_fde:
11823 return "DW_AT_MIPS_fde";
c764a876 11824#endif
c906108c
SS
11825 case DW_AT_MIPS_loop_begin:
11826 return "DW_AT_MIPS_loop_begin";
11827 case DW_AT_MIPS_tail_loop_begin:
11828 return "DW_AT_MIPS_tail_loop_begin";
11829 case DW_AT_MIPS_epilog_begin:
11830 return "DW_AT_MIPS_epilog_begin";
11831 case DW_AT_MIPS_loop_unroll_factor:
11832 return "DW_AT_MIPS_loop_unroll_factor";
11833 case DW_AT_MIPS_software_pipeline_depth:
11834 return "DW_AT_MIPS_software_pipeline_depth";
11835 case DW_AT_MIPS_linkage_name:
11836 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11837 case DW_AT_MIPS_stride:
11838 return "DW_AT_MIPS_stride";
11839 case DW_AT_MIPS_abstract_name:
11840 return "DW_AT_MIPS_abstract_name";
11841 case DW_AT_MIPS_clone_origin:
11842 return "DW_AT_MIPS_clone_origin";
11843 case DW_AT_MIPS_has_inlines:
11844 return "DW_AT_MIPS_has_inlines";
b7619582 11845 /* HP extensions. */
c764a876 11846#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11847 case DW_AT_HP_block_index:
11848 return "DW_AT_HP_block_index";
c764a876 11849#endif
b7619582
GF
11850 case DW_AT_HP_unmodifiable:
11851 return "DW_AT_HP_unmodifiable";
11852 case DW_AT_HP_actuals_stmt_list:
11853 return "DW_AT_HP_actuals_stmt_list";
11854 case DW_AT_HP_proc_per_section:
11855 return "DW_AT_HP_proc_per_section";
11856 case DW_AT_HP_raw_data_ptr:
11857 return "DW_AT_HP_raw_data_ptr";
11858 case DW_AT_HP_pass_by_reference:
11859 return "DW_AT_HP_pass_by_reference";
11860 case DW_AT_HP_opt_level:
11861 return "DW_AT_HP_opt_level";
11862 case DW_AT_HP_prof_version_id:
11863 return "DW_AT_HP_prof_version_id";
11864 case DW_AT_HP_opt_flags:
11865 return "DW_AT_HP_opt_flags";
11866 case DW_AT_HP_cold_region_low_pc:
11867 return "DW_AT_HP_cold_region_low_pc";
11868 case DW_AT_HP_cold_region_high_pc:
11869 return "DW_AT_HP_cold_region_high_pc";
11870 case DW_AT_HP_all_variables_modifiable:
11871 return "DW_AT_HP_all_variables_modifiable";
11872 case DW_AT_HP_linkage_name:
11873 return "DW_AT_HP_linkage_name";
11874 case DW_AT_HP_prof_flags:
11875 return "DW_AT_HP_prof_flags";
11876 /* GNU extensions. */
c906108c
SS
11877 case DW_AT_sf_names:
11878 return "DW_AT_sf_names";
11879 case DW_AT_src_info:
11880 return "DW_AT_src_info";
11881 case DW_AT_mac_info:
11882 return "DW_AT_mac_info";
11883 case DW_AT_src_coords:
11884 return "DW_AT_src_coords";
11885 case DW_AT_body_begin:
11886 return "DW_AT_body_begin";
11887 case DW_AT_body_end:
11888 return "DW_AT_body_end";
f5f8a009
EZ
11889 case DW_AT_GNU_vector:
11890 return "DW_AT_GNU_vector";
2de00c64
DE
11891 case DW_AT_GNU_odr_signature:
11892 return "DW_AT_GNU_odr_signature";
b7619582
GF
11893 /* VMS extensions. */
11894 case DW_AT_VMS_rtnbeg_pd_address:
11895 return "DW_AT_VMS_rtnbeg_pd_address";
11896 /* UPC extension. */
11897 case DW_AT_upc_threads_scaled:
11898 return "DW_AT_upc_threads_scaled";
11899 /* PGI (STMicroelectronics) extensions. */
11900 case DW_AT_PGI_lbase:
11901 return "DW_AT_PGI_lbase";
11902 case DW_AT_PGI_soffset:
11903 return "DW_AT_PGI_soffset";
11904 case DW_AT_PGI_lstride:
11905 return "DW_AT_PGI_lstride";
c906108c
SS
11906 default:
11907 return "DW_AT_<unknown>";
11908 }
11909}
11910
11911/* Convert a DWARF value form code into its string name. */
11912
11913static char *
aa1ee363 11914dwarf_form_name (unsigned form)
c906108c
SS
11915{
11916 switch (form)
11917 {
11918 case DW_FORM_addr:
11919 return "DW_FORM_addr";
11920 case DW_FORM_block2:
11921 return "DW_FORM_block2";
11922 case DW_FORM_block4:
11923 return "DW_FORM_block4";
11924 case DW_FORM_data2:
11925 return "DW_FORM_data2";
11926 case DW_FORM_data4:
11927 return "DW_FORM_data4";
11928 case DW_FORM_data8:
11929 return "DW_FORM_data8";
11930 case DW_FORM_string:
11931 return "DW_FORM_string";
11932 case DW_FORM_block:
11933 return "DW_FORM_block";
11934 case DW_FORM_block1:
11935 return "DW_FORM_block1";
11936 case DW_FORM_data1:
11937 return "DW_FORM_data1";
11938 case DW_FORM_flag:
11939 return "DW_FORM_flag";
11940 case DW_FORM_sdata:
11941 return "DW_FORM_sdata";
11942 case DW_FORM_strp:
11943 return "DW_FORM_strp";
11944 case DW_FORM_udata:
11945 return "DW_FORM_udata";
11946 case DW_FORM_ref_addr:
11947 return "DW_FORM_ref_addr";
11948 case DW_FORM_ref1:
11949 return "DW_FORM_ref1";
11950 case DW_FORM_ref2:
11951 return "DW_FORM_ref2";
11952 case DW_FORM_ref4:
11953 return "DW_FORM_ref4";
11954 case DW_FORM_ref8:
11955 return "DW_FORM_ref8";
11956 case DW_FORM_ref_udata:
11957 return "DW_FORM_ref_udata";
11958 case DW_FORM_indirect:
11959 return "DW_FORM_indirect";
348e048f
DE
11960 case DW_FORM_sec_offset:
11961 return "DW_FORM_sec_offset";
11962 case DW_FORM_exprloc:
11963 return "DW_FORM_exprloc";
11964 case DW_FORM_flag_present:
11965 return "DW_FORM_flag_present";
11966 case DW_FORM_sig8:
11967 return "DW_FORM_sig8";
c906108c
SS
11968 default:
11969 return "DW_FORM_<unknown>";
11970 }
11971}
11972
11973/* Convert a DWARF stack opcode into its string name. */
11974
9eae7c52
TT
11975const char *
11976dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
11977{
11978 switch (op)
11979 {
11980 case DW_OP_addr:
11981 return "DW_OP_addr";
11982 case DW_OP_deref:
11983 return "DW_OP_deref";
11984 case DW_OP_const1u:
11985 return "DW_OP_const1u";
11986 case DW_OP_const1s:
11987 return "DW_OP_const1s";
11988 case DW_OP_const2u:
11989 return "DW_OP_const2u";
11990 case DW_OP_const2s:
11991 return "DW_OP_const2s";
11992 case DW_OP_const4u:
11993 return "DW_OP_const4u";
11994 case DW_OP_const4s:
11995 return "DW_OP_const4s";
11996 case DW_OP_const8u:
11997 return "DW_OP_const8u";
11998 case DW_OP_const8s:
11999 return "DW_OP_const8s";
12000 case DW_OP_constu:
12001 return "DW_OP_constu";
12002 case DW_OP_consts:
12003 return "DW_OP_consts";
12004 case DW_OP_dup:
12005 return "DW_OP_dup";
12006 case DW_OP_drop:
12007 return "DW_OP_drop";
12008 case DW_OP_over:
12009 return "DW_OP_over";
12010 case DW_OP_pick:
12011 return "DW_OP_pick";
12012 case DW_OP_swap:
12013 return "DW_OP_swap";
12014 case DW_OP_rot:
12015 return "DW_OP_rot";
12016 case DW_OP_xderef:
12017 return "DW_OP_xderef";
12018 case DW_OP_abs:
12019 return "DW_OP_abs";
12020 case DW_OP_and:
12021 return "DW_OP_and";
12022 case DW_OP_div:
12023 return "DW_OP_div";
12024 case DW_OP_minus:
12025 return "DW_OP_minus";
12026 case DW_OP_mod:
12027 return "DW_OP_mod";
12028 case DW_OP_mul:
12029 return "DW_OP_mul";
12030 case DW_OP_neg:
12031 return "DW_OP_neg";
12032 case DW_OP_not:
12033 return "DW_OP_not";
12034 case DW_OP_or:
12035 return "DW_OP_or";
12036 case DW_OP_plus:
12037 return "DW_OP_plus";
12038 case DW_OP_plus_uconst:
12039 return "DW_OP_plus_uconst";
12040 case DW_OP_shl:
12041 return "DW_OP_shl";
12042 case DW_OP_shr:
12043 return "DW_OP_shr";
12044 case DW_OP_shra:
12045 return "DW_OP_shra";
12046 case DW_OP_xor:
12047 return "DW_OP_xor";
12048 case DW_OP_bra:
12049 return "DW_OP_bra";
12050 case DW_OP_eq:
12051 return "DW_OP_eq";
12052 case DW_OP_ge:
12053 return "DW_OP_ge";
12054 case DW_OP_gt:
12055 return "DW_OP_gt";
12056 case DW_OP_le:
12057 return "DW_OP_le";
12058 case DW_OP_lt:
12059 return "DW_OP_lt";
12060 case DW_OP_ne:
12061 return "DW_OP_ne";
12062 case DW_OP_skip:
12063 return "DW_OP_skip";
12064 case DW_OP_lit0:
12065 return "DW_OP_lit0";
12066 case DW_OP_lit1:
12067 return "DW_OP_lit1";
12068 case DW_OP_lit2:
12069 return "DW_OP_lit2";
12070 case DW_OP_lit3:
12071 return "DW_OP_lit3";
12072 case DW_OP_lit4:
12073 return "DW_OP_lit4";
12074 case DW_OP_lit5:
12075 return "DW_OP_lit5";
12076 case DW_OP_lit6:
12077 return "DW_OP_lit6";
12078 case DW_OP_lit7:
12079 return "DW_OP_lit7";
12080 case DW_OP_lit8:
12081 return "DW_OP_lit8";
12082 case DW_OP_lit9:
12083 return "DW_OP_lit9";
12084 case DW_OP_lit10:
12085 return "DW_OP_lit10";
12086 case DW_OP_lit11:
12087 return "DW_OP_lit11";
12088 case DW_OP_lit12:
12089 return "DW_OP_lit12";
12090 case DW_OP_lit13:
12091 return "DW_OP_lit13";
12092 case DW_OP_lit14:
12093 return "DW_OP_lit14";
12094 case DW_OP_lit15:
12095 return "DW_OP_lit15";
12096 case DW_OP_lit16:
12097 return "DW_OP_lit16";
12098 case DW_OP_lit17:
12099 return "DW_OP_lit17";
12100 case DW_OP_lit18:
12101 return "DW_OP_lit18";
12102 case DW_OP_lit19:
12103 return "DW_OP_lit19";
12104 case DW_OP_lit20:
12105 return "DW_OP_lit20";
12106 case DW_OP_lit21:
12107 return "DW_OP_lit21";
12108 case DW_OP_lit22:
12109 return "DW_OP_lit22";
12110 case DW_OP_lit23:
12111 return "DW_OP_lit23";
12112 case DW_OP_lit24:
12113 return "DW_OP_lit24";
12114 case DW_OP_lit25:
12115 return "DW_OP_lit25";
12116 case DW_OP_lit26:
12117 return "DW_OP_lit26";
12118 case DW_OP_lit27:
12119 return "DW_OP_lit27";
12120 case DW_OP_lit28:
12121 return "DW_OP_lit28";
12122 case DW_OP_lit29:
12123 return "DW_OP_lit29";
12124 case DW_OP_lit30:
12125 return "DW_OP_lit30";
12126 case DW_OP_lit31:
12127 return "DW_OP_lit31";
12128 case DW_OP_reg0:
12129 return "DW_OP_reg0";
12130 case DW_OP_reg1:
12131 return "DW_OP_reg1";
12132 case DW_OP_reg2:
12133 return "DW_OP_reg2";
12134 case DW_OP_reg3:
12135 return "DW_OP_reg3";
12136 case DW_OP_reg4:
12137 return "DW_OP_reg4";
12138 case DW_OP_reg5:
12139 return "DW_OP_reg5";
12140 case DW_OP_reg6:
12141 return "DW_OP_reg6";
12142 case DW_OP_reg7:
12143 return "DW_OP_reg7";
12144 case DW_OP_reg8:
12145 return "DW_OP_reg8";
12146 case DW_OP_reg9:
12147 return "DW_OP_reg9";
12148 case DW_OP_reg10:
12149 return "DW_OP_reg10";
12150 case DW_OP_reg11:
12151 return "DW_OP_reg11";
12152 case DW_OP_reg12:
12153 return "DW_OP_reg12";
12154 case DW_OP_reg13:
12155 return "DW_OP_reg13";
12156 case DW_OP_reg14:
12157 return "DW_OP_reg14";
12158 case DW_OP_reg15:
12159 return "DW_OP_reg15";
12160 case DW_OP_reg16:
12161 return "DW_OP_reg16";
12162 case DW_OP_reg17:
12163 return "DW_OP_reg17";
12164 case DW_OP_reg18:
12165 return "DW_OP_reg18";
12166 case DW_OP_reg19:
12167 return "DW_OP_reg19";
12168 case DW_OP_reg20:
12169 return "DW_OP_reg20";
12170 case DW_OP_reg21:
12171 return "DW_OP_reg21";
12172 case DW_OP_reg22:
12173 return "DW_OP_reg22";
12174 case DW_OP_reg23:
12175 return "DW_OP_reg23";
12176 case DW_OP_reg24:
12177 return "DW_OP_reg24";
12178 case DW_OP_reg25:
12179 return "DW_OP_reg25";
12180 case DW_OP_reg26:
12181 return "DW_OP_reg26";
12182 case DW_OP_reg27:
12183 return "DW_OP_reg27";
12184 case DW_OP_reg28:
12185 return "DW_OP_reg28";
12186 case DW_OP_reg29:
12187 return "DW_OP_reg29";
12188 case DW_OP_reg30:
12189 return "DW_OP_reg30";
12190 case DW_OP_reg31:
12191 return "DW_OP_reg31";
12192 case DW_OP_breg0:
12193 return "DW_OP_breg0";
12194 case DW_OP_breg1:
12195 return "DW_OP_breg1";
12196 case DW_OP_breg2:
12197 return "DW_OP_breg2";
12198 case DW_OP_breg3:
12199 return "DW_OP_breg3";
12200 case DW_OP_breg4:
12201 return "DW_OP_breg4";
12202 case DW_OP_breg5:
12203 return "DW_OP_breg5";
12204 case DW_OP_breg6:
12205 return "DW_OP_breg6";
12206 case DW_OP_breg7:
12207 return "DW_OP_breg7";
12208 case DW_OP_breg8:
12209 return "DW_OP_breg8";
12210 case DW_OP_breg9:
12211 return "DW_OP_breg9";
12212 case DW_OP_breg10:
12213 return "DW_OP_breg10";
12214 case DW_OP_breg11:
12215 return "DW_OP_breg11";
12216 case DW_OP_breg12:
12217 return "DW_OP_breg12";
12218 case DW_OP_breg13:
12219 return "DW_OP_breg13";
12220 case DW_OP_breg14:
12221 return "DW_OP_breg14";
12222 case DW_OP_breg15:
12223 return "DW_OP_breg15";
12224 case DW_OP_breg16:
12225 return "DW_OP_breg16";
12226 case DW_OP_breg17:
12227 return "DW_OP_breg17";
12228 case DW_OP_breg18:
12229 return "DW_OP_breg18";
12230 case DW_OP_breg19:
12231 return "DW_OP_breg19";
12232 case DW_OP_breg20:
12233 return "DW_OP_breg20";
12234 case DW_OP_breg21:
12235 return "DW_OP_breg21";
12236 case DW_OP_breg22:
12237 return "DW_OP_breg22";
12238 case DW_OP_breg23:
12239 return "DW_OP_breg23";
12240 case DW_OP_breg24:
12241 return "DW_OP_breg24";
12242 case DW_OP_breg25:
12243 return "DW_OP_breg25";
12244 case DW_OP_breg26:
12245 return "DW_OP_breg26";
12246 case DW_OP_breg27:
12247 return "DW_OP_breg27";
12248 case DW_OP_breg28:
12249 return "DW_OP_breg28";
12250 case DW_OP_breg29:
12251 return "DW_OP_breg29";
12252 case DW_OP_breg30:
12253 return "DW_OP_breg30";
12254 case DW_OP_breg31:
12255 return "DW_OP_breg31";
12256 case DW_OP_regx:
12257 return "DW_OP_regx";
12258 case DW_OP_fbreg:
12259 return "DW_OP_fbreg";
12260 case DW_OP_bregx:
12261 return "DW_OP_bregx";
12262 case DW_OP_piece:
12263 return "DW_OP_piece";
12264 case DW_OP_deref_size:
12265 return "DW_OP_deref_size";
12266 case DW_OP_xderef_size:
12267 return "DW_OP_xderef_size";
12268 case DW_OP_nop:
12269 return "DW_OP_nop";
b7619582 12270 /* DWARF 3 extensions. */
ed348acc
EZ
12271 case DW_OP_push_object_address:
12272 return "DW_OP_push_object_address";
12273 case DW_OP_call2:
12274 return "DW_OP_call2";
12275 case DW_OP_call4:
12276 return "DW_OP_call4";
12277 case DW_OP_call_ref:
12278 return "DW_OP_call_ref";
b7619582
GF
12279 case DW_OP_form_tls_address:
12280 return "DW_OP_form_tls_address";
12281 case DW_OP_call_frame_cfa:
12282 return "DW_OP_call_frame_cfa";
12283 case DW_OP_bit_piece:
12284 return "DW_OP_bit_piece";
9eae7c52
TT
12285 /* DWARF 4 extensions. */
12286 case DW_OP_implicit_value:
12287 return "DW_OP_implicit_value";
12288 case DW_OP_stack_value:
12289 return "DW_OP_stack_value";
12290 /* GNU extensions. */
ed348acc
EZ
12291 case DW_OP_GNU_push_tls_address:
12292 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12293 case DW_OP_GNU_uninit:
12294 return "DW_OP_GNU_uninit";
c906108c 12295 default:
9eae7c52 12296 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12297 }
12298}
12299
12300static char *
fba45db2 12301dwarf_bool_name (unsigned mybool)
c906108c
SS
12302{
12303 if (mybool)
12304 return "TRUE";
12305 else
12306 return "FALSE";
12307}
12308
12309/* Convert a DWARF type code into its string name. */
12310
12311static char *
aa1ee363 12312dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12313{
12314 switch (enc)
12315 {
b7619582
GF
12316 case DW_ATE_void:
12317 return "DW_ATE_void";
c906108c
SS
12318 case DW_ATE_address:
12319 return "DW_ATE_address";
12320 case DW_ATE_boolean:
12321 return "DW_ATE_boolean";
12322 case DW_ATE_complex_float:
12323 return "DW_ATE_complex_float";
12324 case DW_ATE_float:
12325 return "DW_ATE_float";
12326 case DW_ATE_signed:
12327 return "DW_ATE_signed";
12328 case DW_ATE_signed_char:
12329 return "DW_ATE_signed_char";
12330 case DW_ATE_unsigned:
12331 return "DW_ATE_unsigned";
12332 case DW_ATE_unsigned_char:
12333 return "DW_ATE_unsigned_char";
b7619582 12334 /* DWARF 3. */
d9fa45fe
DC
12335 case DW_ATE_imaginary_float:
12336 return "DW_ATE_imaginary_float";
b7619582
GF
12337 case DW_ATE_packed_decimal:
12338 return "DW_ATE_packed_decimal";
12339 case DW_ATE_numeric_string:
12340 return "DW_ATE_numeric_string";
12341 case DW_ATE_edited:
12342 return "DW_ATE_edited";
12343 case DW_ATE_signed_fixed:
12344 return "DW_ATE_signed_fixed";
12345 case DW_ATE_unsigned_fixed:
12346 return "DW_ATE_unsigned_fixed";
12347 case DW_ATE_decimal_float:
12348 return "DW_ATE_decimal_float";
75079b2b
TT
12349 /* DWARF 4. */
12350 case DW_ATE_UTF:
12351 return "DW_ATE_UTF";
b7619582
GF
12352 /* HP extensions. */
12353 case DW_ATE_HP_float80:
12354 return "DW_ATE_HP_float80";
12355 case DW_ATE_HP_complex_float80:
12356 return "DW_ATE_HP_complex_float80";
12357 case DW_ATE_HP_float128:
12358 return "DW_ATE_HP_float128";
12359 case DW_ATE_HP_complex_float128:
12360 return "DW_ATE_HP_complex_float128";
12361 case DW_ATE_HP_floathpintel:
12362 return "DW_ATE_HP_floathpintel";
12363 case DW_ATE_HP_imaginary_float80:
12364 return "DW_ATE_HP_imaginary_float80";
12365 case DW_ATE_HP_imaginary_float128:
12366 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12367 default:
12368 return "DW_ATE_<unknown>";
12369 }
12370}
12371
12372/* Convert a DWARF call frame info operation to its string name. */
12373
12374#if 0
12375static char *
aa1ee363 12376dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12377{
12378 switch (cfi_opc)
12379 {
12380 case DW_CFA_advance_loc:
12381 return "DW_CFA_advance_loc";
12382 case DW_CFA_offset:
12383 return "DW_CFA_offset";
12384 case DW_CFA_restore:
12385 return "DW_CFA_restore";
12386 case DW_CFA_nop:
12387 return "DW_CFA_nop";
12388 case DW_CFA_set_loc:
12389 return "DW_CFA_set_loc";
12390 case DW_CFA_advance_loc1:
12391 return "DW_CFA_advance_loc1";
12392 case DW_CFA_advance_loc2:
12393 return "DW_CFA_advance_loc2";
12394 case DW_CFA_advance_loc4:
12395 return "DW_CFA_advance_loc4";
12396 case DW_CFA_offset_extended:
12397 return "DW_CFA_offset_extended";
12398 case DW_CFA_restore_extended:
12399 return "DW_CFA_restore_extended";
12400 case DW_CFA_undefined:
12401 return "DW_CFA_undefined";
12402 case DW_CFA_same_value:
12403 return "DW_CFA_same_value";
12404 case DW_CFA_register:
12405 return "DW_CFA_register";
12406 case DW_CFA_remember_state:
12407 return "DW_CFA_remember_state";
12408 case DW_CFA_restore_state:
12409 return "DW_CFA_restore_state";
12410 case DW_CFA_def_cfa:
12411 return "DW_CFA_def_cfa";
12412 case DW_CFA_def_cfa_register:
12413 return "DW_CFA_def_cfa_register";
12414 case DW_CFA_def_cfa_offset:
12415 return "DW_CFA_def_cfa_offset";
b7619582 12416 /* DWARF 3. */
985cb1a3
JM
12417 case DW_CFA_def_cfa_expression:
12418 return "DW_CFA_def_cfa_expression";
12419 case DW_CFA_expression:
12420 return "DW_CFA_expression";
12421 case DW_CFA_offset_extended_sf:
12422 return "DW_CFA_offset_extended_sf";
12423 case DW_CFA_def_cfa_sf:
12424 return "DW_CFA_def_cfa_sf";
12425 case DW_CFA_def_cfa_offset_sf:
12426 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12427 case DW_CFA_val_offset:
12428 return "DW_CFA_val_offset";
12429 case DW_CFA_val_offset_sf:
12430 return "DW_CFA_val_offset_sf";
12431 case DW_CFA_val_expression:
12432 return "DW_CFA_val_expression";
12433 /* SGI/MIPS specific. */
c906108c
SS
12434 case DW_CFA_MIPS_advance_loc8:
12435 return "DW_CFA_MIPS_advance_loc8";
b7619582 12436 /* GNU extensions. */
985cb1a3
JM
12437 case DW_CFA_GNU_window_save:
12438 return "DW_CFA_GNU_window_save";
12439 case DW_CFA_GNU_args_size:
12440 return "DW_CFA_GNU_args_size";
12441 case DW_CFA_GNU_negative_offset_extended:
12442 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12443 default:
12444 return "DW_CFA_<unknown>";
12445 }
12446}
12447#endif
12448
f9aca02d 12449static void
d97bc12b 12450dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12451{
12452 unsigned int i;
12453
d97bc12b
DE
12454 print_spaces (indent, f);
12455 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12456 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12457
12458 if (die->parent != NULL)
12459 {
12460 print_spaces (indent, f);
12461 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12462 die->parent->offset);
12463 }
12464
12465 print_spaces (indent, f);
12466 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12467 dwarf_bool_name (die->child != NULL));
c906108c 12468
d97bc12b
DE
12469 print_spaces (indent, f);
12470 fprintf_unfiltered (f, " attributes:\n");
12471
c906108c
SS
12472 for (i = 0; i < die->num_attrs; ++i)
12473 {
d97bc12b
DE
12474 print_spaces (indent, f);
12475 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12476 dwarf_attr_name (die->attrs[i].name),
12477 dwarf_form_name (die->attrs[i].form));
d97bc12b 12478
c906108c
SS
12479 switch (die->attrs[i].form)
12480 {
12481 case DW_FORM_ref_addr:
12482 case DW_FORM_addr:
d97bc12b 12483 fprintf_unfiltered (f, "address: ");
5af949e3 12484 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12485 break;
12486 case DW_FORM_block2:
12487 case DW_FORM_block4:
12488 case DW_FORM_block:
12489 case DW_FORM_block1:
d97bc12b 12490 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12491 break;
2dc7f7b3
TT
12492 case DW_FORM_exprloc:
12493 fprintf_unfiltered (f, "expression: size %u",
12494 DW_BLOCK (&die->attrs[i])->size);
12495 break;
10b3939b
DJ
12496 case DW_FORM_ref1:
12497 case DW_FORM_ref2:
12498 case DW_FORM_ref4:
d97bc12b 12499 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12500 (long) (DW_ADDR (&die->attrs[i])));
12501 break;
c906108c
SS
12502 case DW_FORM_data1:
12503 case DW_FORM_data2:
12504 case DW_FORM_data4:
ce5d95e1 12505 case DW_FORM_data8:
c906108c
SS
12506 case DW_FORM_udata:
12507 case DW_FORM_sdata:
43bbcdc2
PH
12508 fprintf_unfiltered (f, "constant: %s",
12509 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12510 break;
2dc7f7b3
TT
12511 case DW_FORM_sec_offset:
12512 fprintf_unfiltered (f, "section offset: %s",
12513 pulongest (DW_UNSND (&die->attrs[i])));
12514 break;
348e048f
DE
12515 case DW_FORM_sig8:
12516 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12517 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12518 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12519 else
12520 fprintf_unfiltered (f, "signatured type, offset: unknown");
12521 break;
c906108c 12522 case DW_FORM_string:
4bdf3d34 12523 case DW_FORM_strp:
8285870a 12524 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12525 DW_STRING (&die->attrs[i])
8285870a
JK
12526 ? DW_STRING (&die->attrs[i]) : "",
12527 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12528 break;
12529 case DW_FORM_flag:
12530 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12531 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12532 else
d97bc12b 12533 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12534 break;
2dc7f7b3
TT
12535 case DW_FORM_flag_present:
12536 fprintf_unfiltered (f, "flag: TRUE");
12537 break;
a8329558
KW
12538 case DW_FORM_indirect:
12539 /* the reader will have reduced the indirect form to
12540 the "base form" so this form should not occur */
d97bc12b 12541 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12542 break;
c906108c 12543 default:
d97bc12b 12544 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12545 die->attrs[i].form);
d97bc12b 12546 break;
c906108c 12547 }
d97bc12b 12548 fprintf_unfiltered (f, "\n");
c906108c
SS
12549 }
12550}
12551
f9aca02d 12552static void
d97bc12b 12553dump_die_for_error (struct die_info *die)
c906108c 12554{
d97bc12b
DE
12555 dump_die_shallow (gdb_stderr, 0, die);
12556}
12557
12558static void
12559dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12560{
12561 int indent = level * 4;
12562
12563 gdb_assert (die != NULL);
12564
12565 if (level >= max_level)
12566 return;
12567
12568 dump_die_shallow (f, indent, die);
12569
12570 if (die->child != NULL)
c906108c 12571 {
d97bc12b
DE
12572 print_spaces (indent, f);
12573 fprintf_unfiltered (f, " Children:");
12574 if (level + 1 < max_level)
12575 {
12576 fprintf_unfiltered (f, "\n");
12577 dump_die_1 (f, level + 1, max_level, die->child);
12578 }
12579 else
12580 {
12581 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12582 }
12583 }
12584
12585 if (die->sibling != NULL && level > 0)
12586 {
12587 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12588 }
12589}
12590
d97bc12b
DE
12591/* This is called from the pdie macro in gdbinit.in.
12592 It's not static so gcc will keep a copy callable from gdb. */
12593
12594void
12595dump_die (struct die_info *die, int max_level)
12596{
12597 dump_die_1 (gdb_stdlog, 0, max_level, die);
12598}
12599
f9aca02d 12600static void
51545339 12601store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12602{
51545339 12603 void **slot;
c906108c 12604
51545339
DJ
12605 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12606
12607 *slot = die;
c906108c
SS
12608}
12609
93311388
DE
12610static int
12611is_ref_attr (struct attribute *attr)
c906108c 12612{
c906108c
SS
12613 switch (attr->form)
12614 {
12615 case DW_FORM_ref_addr:
c906108c
SS
12616 case DW_FORM_ref1:
12617 case DW_FORM_ref2:
12618 case DW_FORM_ref4:
613e1657 12619 case DW_FORM_ref8:
c906108c 12620 case DW_FORM_ref_udata:
93311388 12621 return 1;
c906108c 12622 default:
93311388 12623 return 0;
c906108c 12624 }
93311388
DE
12625}
12626
12627static unsigned int
12628dwarf2_get_ref_die_offset (struct attribute *attr)
12629{
12630 if (is_ref_attr (attr))
12631 return DW_ADDR (attr);
12632
12633 complaint (&symfile_complaints,
12634 _("unsupported die ref attribute form: '%s'"),
12635 dwarf_form_name (attr->form));
12636 return 0;
c906108c
SS
12637}
12638
43bbcdc2
PH
12639/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12640 * the value held by the attribute is not constant. */
a02abb62 12641
43bbcdc2 12642static LONGEST
a02abb62
JB
12643dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12644{
12645 if (attr->form == DW_FORM_sdata)
12646 return DW_SND (attr);
12647 else if (attr->form == DW_FORM_udata
12648 || attr->form == DW_FORM_data1
12649 || attr->form == DW_FORM_data2
12650 || attr->form == DW_FORM_data4
12651 || attr->form == DW_FORM_data8)
12652 return DW_UNSND (attr);
12653 else
12654 {
e2e0b3e5 12655 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12656 dwarf_form_name (attr->form));
12657 return default_value;
12658 }
12659}
12660
03dd20cc 12661/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12662 unit and add it to our queue.
12663 The result is non-zero if PER_CU was queued, otherwise the result is zero
12664 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12665
348e048f 12666static int
03dd20cc
DJ
12667maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12668 struct dwarf2_per_cu_data *per_cu)
12669{
98bfdba5
PA
12670 /* We may arrive here during partial symbol reading, if we need full
12671 DIEs to process an unusual case (e.g. template arguments). Do
12672 not queue PER_CU, just tell our caller to load its DIEs. */
12673 if (dwarf2_per_objfile->reading_partial_symbols)
12674 {
12675 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12676 return 1;
12677 return 0;
12678 }
12679
03dd20cc
DJ
12680 /* Mark the dependence relation so that we don't flush PER_CU
12681 too early. */
12682 dwarf2_add_dependence (this_cu, per_cu);
12683
12684 /* If it's already on the queue, we have nothing to do. */
12685 if (per_cu->queued)
348e048f 12686 return 0;
03dd20cc
DJ
12687
12688 /* If the compilation unit is already loaded, just mark it as
12689 used. */
12690 if (per_cu->cu != NULL)
12691 {
12692 per_cu->cu->last_used = 0;
348e048f 12693 return 0;
03dd20cc
DJ
12694 }
12695
12696 /* Add it to the queue. */
12697 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12698
12699 return 1;
12700}
12701
12702/* Follow reference or signature attribute ATTR of SRC_DIE.
12703 On entry *REF_CU is the CU of SRC_DIE.
12704 On exit *REF_CU is the CU of the result. */
12705
12706static struct die_info *
12707follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12708 struct dwarf2_cu **ref_cu)
12709{
12710 struct die_info *die;
12711
12712 if (is_ref_attr (attr))
12713 die = follow_die_ref (src_die, attr, ref_cu);
12714 else if (attr->form == DW_FORM_sig8)
12715 die = follow_die_sig (src_die, attr, ref_cu);
12716 else
12717 {
12718 dump_die_for_error (src_die);
12719 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12720 (*ref_cu)->objfile->name);
12721 }
12722
12723 return die;
03dd20cc
DJ
12724}
12725
5c631832 12726/* Follow reference OFFSET.
673bfd45
DE
12727 On entry *REF_CU is the CU of the source die referencing OFFSET.
12728 On exit *REF_CU is the CU of the result.
12729 Returns NULL if OFFSET is invalid. */
f504f079 12730
f9aca02d 12731static struct die_info *
5c631832 12732follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12733{
10b3939b 12734 struct die_info temp_die;
f2f0e013 12735 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12736
348e048f
DE
12737 gdb_assert (cu->per_cu != NULL);
12738
98bfdba5
PA
12739 target_cu = cu;
12740
348e048f
DE
12741 if (cu->per_cu->from_debug_types)
12742 {
12743 /* .debug_types CUs cannot reference anything outside their CU.
12744 If they need to, they have to reference a signatured type via
12745 DW_FORM_sig8. */
12746 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12747 return NULL;
348e048f
DE
12748 }
12749 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12750 {
12751 struct dwarf2_per_cu_data *per_cu;
9a619af0 12752
45452591 12753 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12754
12755 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12756 if (maybe_queue_comp_unit (cu, per_cu))
12757 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12758
10b3939b
DJ
12759 target_cu = per_cu->cu;
12760 }
98bfdba5
PA
12761 else if (cu->dies == NULL)
12762 {
12763 /* We're loading full DIEs during partial symbol reading. */
12764 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12765 load_full_comp_unit (cu->per_cu, cu->objfile);
12766 }
c906108c 12767
f2f0e013 12768 *ref_cu = target_cu;
51545339 12769 temp_die.offset = offset;
5c631832
JK
12770 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12771}
10b3939b 12772
5c631832
JK
12773/* Follow reference attribute ATTR of SRC_DIE.
12774 On entry *REF_CU is the CU of SRC_DIE.
12775 On exit *REF_CU is the CU of the result. */
12776
12777static struct die_info *
12778follow_die_ref (struct die_info *src_die, struct attribute *attr,
12779 struct dwarf2_cu **ref_cu)
12780{
12781 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12782 struct dwarf2_cu *cu = *ref_cu;
12783 struct die_info *die;
12784
12785 die = follow_die_offset (offset, ref_cu);
12786 if (!die)
12787 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12788 "at 0x%x [in module %s]"),
12789 offset, src_die->offset, cu->objfile->name);
348e048f 12790
5c631832
JK
12791 return die;
12792}
12793
12794/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12795 value is intended for DW_OP_call*. */
12796
12797struct dwarf2_locexpr_baton
12798dwarf2_fetch_die_location_block (unsigned int offset,
12799 struct dwarf2_per_cu_data *per_cu)
12800{
12801 struct dwarf2_cu *cu = per_cu->cu;
12802 struct die_info *die;
12803 struct attribute *attr;
12804 struct dwarf2_locexpr_baton retval;
12805
12806 die = follow_die_offset (offset, &cu);
12807 if (!die)
12808 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12809 offset, per_cu->cu->objfile->name);
12810
12811 attr = dwarf2_attr (die, DW_AT_location, cu);
12812 if (!attr)
12813 {
12814 /* DWARF: "If there is no such attribute, then there is no effect.". */
12815
12816 retval.data = NULL;
12817 retval.size = 0;
12818 }
12819 else
12820 {
12821 if (!attr_form_is_block (attr))
12822 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12823 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12824 offset, per_cu->cu->objfile->name);
12825
12826 retval.data = DW_BLOCK (attr)->data;
12827 retval.size = DW_BLOCK (attr)->size;
12828 }
12829 retval.per_cu = cu->per_cu;
12830 return retval;
348e048f
DE
12831}
12832
12833/* Follow the signature attribute ATTR in SRC_DIE.
12834 On entry *REF_CU is the CU of SRC_DIE.
12835 On exit *REF_CU is the CU of the result. */
12836
12837static struct die_info *
12838follow_die_sig (struct die_info *src_die, struct attribute *attr,
12839 struct dwarf2_cu **ref_cu)
12840{
12841 struct objfile *objfile = (*ref_cu)->objfile;
12842 struct die_info temp_die;
12843 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12844 struct dwarf2_cu *sig_cu;
12845 struct die_info *die;
12846
12847 /* sig_type will be NULL if the signatured type is missing from
12848 the debug info. */
12849 if (sig_type == NULL)
12850 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12851 "at 0x%x [in module %s]"),
12852 src_die->offset, objfile->name);
12853
12854 /* If necessary, add it to the queue and load its DIEs. */
12855
12856 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12857 read_signatured_type (objfile, sig_type);
12858
12859 gdb_assert (sig_type->per_cu.cu != NULL);
12860
12861 sig_cu = sig_type->per_cu.cu;
12862 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12863 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12864 if (die)
12865 {
12866 *ref_cu = sig_cu;
12867 return die;
12868 }
12869
12870 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12871 "at 0x%x [in module %s]"),
12872 sig_type->type_offset, src_die->offset, objfile->name);
12873}
12874
12875/* Given an offset of a signatured type, return its signatured_type. */
12876
12877static struct signatured_type *
12878lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12879{
12880 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12881 unsigned int length, initial_length_size;
12882 unsigned int sig_offset;
12883 struct signatured_type find_entry, *type_sig;
12884
12885 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12886 sig_offset = (initial_length_size
12887 + 2 /*version*/
12888 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12889 + 1 /*address_size*/);
12890 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12891 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12892
12893 /* This is only used to lookup previously recorded types.
12894 If we didn't find it, it's our bug. */
12895 gdb_assert (type_sig != NULL);
12896 gdb_assert (offset == type_sig->offset);
12897
12898 return type_sig;
12899}
12900
12901/* Read in signatured type at OFFSET and build its CU and die(s). */
12902
12903static void
12904read_signatured_type_at_offset (struct objfile *objfile,
12905 unsigned int offset)
12906{
12907 struct signatured_type *type_sig;
12908
be391dca
TT
12909 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12910
348e048f
DE
12911 /* We have the section offset, but we need the signature to do the
12912 hash table lookup. */
12913 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12914
12915 gdb_assert (type_sig->per_cu.cu == NULL);
12916
12917 read_signatured_type (objfile, type_sig);
12918
12919 gdb_assert (type_sig->per_cu.cu != NULL);
12920}
12921
12922/* Read in a signatured type and build its CU and DIEs. */
12923
12924static void
12925read_signatured_type (struct objfile *objfile,
12926 struct signatured_type *type_sig)
12927{
1fd400ff 12928 gdb_byte *types_ptr;
348e048f
DE
12929 struct die_reader_specs reader_specs;
12930 struct dwarf2_cu *cu;
12931 ULONGEST signature;
12932 struct cleanup *back_to, *free_cu_cleanup;
12933 struct attribute *attr;
12934
1fd400ff
TT
12935 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12936 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12937
348e048f
DE
12938 gdb_assert (type_sig->per_cu.cu == NULL);
12939
12940 cu = xmalloc (sizeof (struct dwarf2_cu));
12941 memset (cu, 0, sizeof (struct dwarf2_cu));
12942 obstack_init (&cu->comp_unit_obstack);
12943 cu->objfile = objfile;
12944 type_sig->per_cu.cu = cu;
12945 cu->per_cu = &type_sig->per_cu;
12946
12947 /* If an error occurs while loading, release our storage. */
12948 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12949
12950 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12951 types_ptr, objfile->obfd);
12952 gdb_assert (signature == type_sig->signature);
12953
12954 cu->die_hash
12955 = htab_create_alloc_ex (cu->header.length / 12,
12956 die_hash,
12957 die_eq,
12958 NULL,
12959 &cu->comp_unit_obstack,
12960 hashtab_obstack_allocate,
12961 dummy_obstack_deallocate);
12962
12963 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12964 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12965
12966 init_cu_die_reader (&reader_specs, cu);
12967
12968 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12969 NULL /*parent*/);
12970
12971 /* We try not to read any attributes in this function, because not
12972 all objfiles needed for references have been loaded yet, and symbol
12973 table processing isn't initialized. But we have to set the CU language,
12974 or we won't be able to build types correctly. */
12975 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12976 if (attr)
12977 set_cu_language (DW_UNSND (attr), cu);
12978 else
12979 set_cu_language (language_minimal, cu);
12980
12981 do_cleanups (back_to);
12982
12983 /* We've successfully allocated this compilation unit. Let our caller
12984 clean it up when finished with it. */
12985 discard_cleanups (free_cu_cleanup);
12986
12987 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12988 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
12989}
12990
c906108c
SS
12991/* Decode simple location descriptions.
12992 Given a pointer to a dwarf block that defines a location, compute
12993 the location and return the value.
12994
4cecd739
DJ
12995 NOTE drow/2003-11-18: This function is called in two situations
12996 now: for the address of static or global variables (partial symbols
12997 only) and for offsets into structures which are expected to be
12998 (more or less) constant. The partial symbol case should go away,
12999 and only the constant case should remain. That will let this
13000 function complain more accurately. A few special modes are allowed
13001 without complaint for global variables (for instance, global
13002 register values and thread-local values).
c906108c
SS
13003
13004 A location description containing no operations indicates that the
4cecd739 13005 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13006 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13007 callers will only want a very basic result and this can become a
13008 complaint.
c906108c 13009
c906108c
SS
13010 Note that stack[0] is unused except as a default error return.
13011 Note that stack overflow is not yet handled. */
13012
13013static CORE_ADDR
e7c27a73 13014decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13015{
e7c27a73 13016 struct objfile *objfile = cu->objfile;
c906108c
SS
13017 int i;
13018 int size = blk->size;
fe1b8b76 13019 gdb_byte *data = blk->data;
c906108c
SS
13020 CORE_ADDR stack[64];
13021 int stacki;
13022 unsigned int bytes_read, unsnd;
fe1b8b76 13023 gdb_byte op;
c906108c
SS
13024
13025 i = 0;
13026 stacki = 0;
13027 stack[stacki] = 0;
c906108c
SS
13028
13029 while (i < size)
13030 {
c906108c
SS
13031 op = data[i++];
13032 switch (op)
13033 {
f1bea926
JM
13034 case DW_OP_lit0:
13035 case DW_OP_lit1:
13036 case DW_OP_lit2:
13037 case DW_OP_lit3:
13038 case DW_OP_lit4:
13039 case DW_OP_lit5:
13040 case DW_OP_lit6:
13041 case DW_OP_lit7:
13042 case DW_OP_lit8:
13043 case DW_OP_lit9:
13044 case DW_OP_lit10:
13045 case DW_OP_lit11:
13046 case DW_OP_lit12:
13047 case DW_OP_lit13:
13048 case DW_OP_lit14:
13049 case DW_OP_lit15:
13050 case DW_OP_lit16:
13051 case DW_OP_lit17:
13052 case DW_OP_lit18:
13053 case DW_OP_lit19:
13054 case DW_OP_lit20:
13055 case DW_OP_lit21:
13056 case DW_OP_lit22:
13057 case DW_OP_lit23:
13058 case DW_OP_lit24:
13059 case DW_OP_lit25:
13060 case DW_OP_lit26:
13061 case DW_OP_lit27:
13062 case DW_OP_lit28:
13063 case DW_OP_lit29:
13064 case DW_OP_lit30:
13065 case DW_OP_lit31:
13066 stack[++stacki] = op - DW_OP_lit0;
13067 break;
13068
c906108c
SS
13069 case DW_OP_reg0:
13070 case DW_OP_reg1:
13071 case DW_OP_reg2:
13072 case DW_OP_reg3:
13073 case DW_OP_reg4:
13074 case DW_OP_reg5:
13075 case DW_OP_reg6:
13076 case DW_OP_reg7:
13077 case DW_OP_reg8:
13078 case DW_OP_reg9:
13079 case DW_OP_reg10:
13080 case DW_OP_reg11:
13081 case DW_OP_reg12:
13082 case DW_OP_reg13:
13083 case DW_OP_reg14:
13084 case DW_OP_reg15:
13085 case DW_OP_reg16:
13086 case DW_OP_reg17:
13087 case DW_OP_reg18:
13088 case DW_OP_reg19:
13089 case DW_OP_reg20:
13090 case DW_OP_reg21:
13091 case DW_OP_reg22:
13092 case DW_OP_reg23:
13093 case DW_OP_reg24:
13094 case DW_OP_reg25:
13095 case DW_OP_reg26:
13096 case DW_OP_reg27:
13097 case DW_OP_reg28:
13098 case DW_OP_reg29:
13099 case DW_OP_reg30:
13100 case DW_OP_reg31:
c906108c 13101 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13102 if (i < size)
13103 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13104 break;
13105
13106 case DW_OP_regx:
c906108c
SS
13107 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13108 i += bytes_read;
c906108c 13109 stack[++stacki] = unsnd;
4cecd739
DJ
13110 if (i < size)
13111 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13112 break;
13113
13114 case DW_OP_addr:
107d2387 13115 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13116 cu, &bytes_read);
107d2387 13117 i += bytes_read;
c906108c
SS
13118 break;
13119
13120 case DW_OP_const1u:
13121 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13122 i += 1;
13123 break;
13124
13125 case DW_OP_const1s:
13126 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13127 i += 1;
13128 break;
13129
13130 case DW_OP_const2u:
13131 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13132 i += 2;
13133 break;
13134
13135 case DW_OP_const2s:
13136 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13137 i += 2;
13138 break;
13139
13140 case DW_OP_const4u:
13141 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13142 i += 4;
13143 break;
13144
13145 case DW_OP_const4s:
13146 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13147 i += 4;
13148 break;
13149
13150 case DW_OP_constu:
13151 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13152 &bytes_read);
c906108c
SS
13153 i += bytes_read;
13154 break;
13155
13156 case DW_OP_consts:
13157 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13158 i += bytes_read;
13159 break;
13160
f1bea926
JM
13161 case DW_OP_dup:
13162 stack[stacki + 1] = stack[stacki];
13163 stacki++;
13164 break;
13165
c906108c
SS
13166 case DW_OP_plus:
13167 stack[stacki - 1] += stack[stacki];
13168 stacki--;
13169 break;
13170
13171 case DW_OP_plus_uconst:
13172 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13173 i += bytes_read;
13174 break;
13175
13176 case DW_OP_minus:
f1bea926 13177 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13178 stacki--;
13179 break;
13180
7a292a7a 13181 case DW_OP_deref:
7a292a7a 13182 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13183 this using GDB's address_class enum. This is valid for partial
13184 global symbols, although the variable's address will be bogus
13185 in the psymtab. */
7a292a7a 13186 if (i < size)
4d3c2250 13187 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13188 break;
13189
9d774e44 13190 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13191 /* The top of the stack has the offset from the beginning
13192 of the thread control block at which the variable is located. */
13193 /* Nothing should follow this operator, so the top of stack would
13194 be returned. */
4cecd739
DJ
13195 /* This is valid for partial global symbols, but the variable's
13196 address will be bogus in the psymtab. */
9d774e44 13197 if (i < size)
4d3c2250 13198 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13199 break;
13200
42be36b3
CT
13201 case DW_OP_GNU_uninit:
13202 break;
13203
c906108c 13204 default:
e2e0b3e5 13205 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13206 dwarf_stack_op_name (op, 1));
c906108c
SS
13207 return (stack[stacki]);
13208 }
13209 }
13210 return (stack[stacki]);
13211}
13212
13213/* memory allocation interface */
13214
c906108c 13215static struct dwarf_block *
7b5a2f43 13216dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13217{
13218 struct dwarf_block *blk;
13219
13220 blk = (struct dwarf_block *)
7b5a2f43 13221 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13222 return (blk);
13223}
13224
13225static struct abbrev_info *
f3dd6933 13226dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13227{
13228 struct abbrev_info *abbrev;
13229
f3dd6933
DJ
13230 abbrev = (struct abbrev_info *)
13231 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13232 memset (abbrev, 0, sizeof (struct abbrev_info));
13233 return (abbrev);
13234}
13235
13236static struct die_info *
b60c80d6 13237dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13238{
13239 struct die_info *die;
b60c80d6
DJ
13240 size_t size = sizeof (struct die_info);
13241
13242 if (num_attrs > 1)
13243 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13244
b60c80d6 13245 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13246 memset (die, 0, sizeof (struct die_info));
13247 return (die);
13248}
2e276125
JB
13249
13250\f
13251/* Macro support. */
13252
13253
13254/* Return the full name of file number I in *LH's file name table.
13255 Use COMP_DIR as the name of the current directory of the
13256 compilation. The result is allocated using xmalloc; the caller is
13257 responsible for freeing it. */
13258static char *
13259file_full_name (int file, struct line_header *lh, const char *comp_dir)
13260{
6a83a1e6
EZ
13261 /* Is the file number a valid index into the line header's file name
13262 table? Remember that file numbers start with one, not zero. */
13263 if (1 <= file && file <= lh->num_file_names)
13264 {
13265 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13266
6a83a1e6
EZ
13267 if (IS_ABSOLUTE_PATH (fe->name))
13268 return xstrdup (fe->name);
13269 else
13270 {
13271 const char *dir;
13272 int dir_len;
13273 char *full_name;
13274
13275 if (fe->dir_index)
13276 dir = lh->include_dirs[fe->dir_index - 1];
13277 else
13278 dir = comp_dir;
13279
13280 if (dir)
13281 {
13282 dir_len = strlen (dir);
13283 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13284 strcpy (full_name, dir);
13285 full_name[dir_len] = '/';
13286 strcpy (full_name + dir_len + 1, fe->name);
13287 return full_name;
13288 }
13289 else
13290 return xstrdup (fe->name);
13291 }
13292 }
2e276125
JB
13293 else
13294 {
6a83a1e6
EZ
13295 /* The compiler produced a bogus file number. We can at least
13296 record the macro definitions made in the file, even if we
13297 won't be able to find the file by name. */
13298 char fake_name[80];
9a619af0 13299
6a83a1e6 13300 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13301
6e70227d 13302 complaint (&symfile_complaints,
6a83a1e6
EZ
13303 _("bad file number in macro information (%d)"),
13304 file);
2e276125 13305
6a83a1e6 13306 return xstrdup (fake_name);
2e276125
JB
13307 }
13308}
13309
13310
13311static struct macro_source_file *
13312macro_start_file (int file, int line,
13313 struct macro_source_file *current_file,
13314 const char *comp_dir,
13315 struct line_header *lh, struct objfile *objfile)
13316{
13317 /* The full name of this source file. */
13318 char *full_name = file_full_name (file, lh, comp_dir);
13319
13320 /* We don't create a macro table for this compilation unit
13321 at all until we actually get a filename. */
13322 if (! pending_macros)
4a146b47 13323 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13324 objfile->macro_cache);
2e276125
JB
13325
13326 if (! current_file)
13327 /* If we have no current file, then this must be the start_file
13328 directive for the compilation unit's main source file. */
13329 current_file = macro_set_main (pending_macros, full_name);
13330 else
13331 current_file = macro_include (current_file, line, full_name);
13332
13333 xfree (full_name);
6e70227d 13334
2e276125
JB
13335 return current_file;
13336}
13337
13338
13339/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13340 followed by a null byte. */
13341static char *
13342copy_string (const char *buf, int len)
13343{
13344 char *s = xmalloc (len + 1);
9a619af0 13345
2e276125
JB
13346 memcpy (s, buf, len);
13347 s[len] = '\0';
2e276125
JB
13348 return s;
13349}
13350
13351
13352static const char *
13353consume_improper_spaces (const char *p, const char *body)
13354{
13355 if (*p == ' ')
13356 {
4d3c2250 13357 complaint (&symfile_complaints,
e2e0b3e5 13358 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13359 body);
2e276125
JB
13360
13361 while (*p == ' ')
13362 p++;
13363 }
13364
13365 return p;
13366}
13367
13368
13369static void
13370parse_macro_definition (struct macro_source_file *file, int line,
13371 const char *body)
13372{
13373 const char *p;
13374
13375 /* The body string takes one of two forms. For object-like macro
13376 definitions, it should be:
13377
13378 <macro name> " " <definition>
13379
13380 For function-like macro definitions, it should be:
13381
13382 <macro name> "() " <definition>
13383 or
13384 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13385
13386 Spaces may appear only where explicitly indicated, and in the
13387 <definition>.
13388
13389 The Dwarf 2 spec says that an object-like macro's name is always
13390 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13391 the space when the macro's definition is the empty string.
2e276125
JB
13392
13393 The Dwarf 2 spec says that there should be no spaces between the
13394 formal arguments in a function-like macro's formal argument list,
13395 but versions of GCC around March 2002 include spaces after the
13396 commas. */
13397
13398
13399 /* Find the extent of the macro name. The macro name is terminated
13400 by either a space or null character (for an object-like macro) or
13401 an opening paren (for a function-like macro). */
13402 for (p = body; *p; p++)
13403 if (*p == ' ' || *p == '(')
13404 break;
13405
13406 if (*p == ' ' || *p == '\0')
13407 {
13408 /* It's an object-like macro. */
13409 int name_len = p - body;
13410 char *name = copy_string (body, name_len);
13411 const char *replacement;
13412
13413 if (*p == ' ')
13414 replacement = body + name_len + 1;
13415 else
13416 {
4d3c2250 13417 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13418 replacement = body + name_len;
13419 }
6e70227d 13420
2e276125
JB
13421 macro_define_object (file, line, name, replacement);
13422
13423 xfree (name);
13424 }
13425 else if (*p == '(')
13426 {
13427 /* It's a function-like macro. */
13428 char *name = copy_string (body, p - body);
13429 int argc = 0;
13430 int argv_size = 1;
13431 char **argv = xmalloc (argv_size * sizeof (*argv));
13432
13433 p++;
13434
13435 p = consume_improper_spaces (p, body);
13436
13437 /* Parse the formal argument list. */
13438 while (*p && *p != ')')
13439 {
13440 /* Find the extent of the current argument name. */
13441 const char *arg_start = p;
13442
13443 while (*p && *p != ',' && *p != ')' && *p != ' ')
13444 p++;
13445
13446 if (! *p || p == arg_start)
4d3c2250 13447 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13448 else
13449 {
13450 /* Make sure argv has room for the new argument. */
13451 if (argc >= argv_size)
13452 {
13453 argv_size *= 2;
13454 argv = xrealloc (argv, argv_size * sizeof (*argv));
13455 }
13456
13457 argv[argc++] = copy_string (arg_start, p - arg_start);
13458 }
13459
13460 p = consume_improper_spaces (p, body);
13461
13462 /* Consume the comma, if present. */
13463 if (*p == ',')
13464 {
13465 p++;
13466
13467 p = consume_improper_spaces (p, body);
13468 }
13469 }
13470
13471 if (*p == ')')
13472 {
13473 p++;
13474
13475 if (*p == ' ')
13476 /* Perfectly formed definition, no complaints. */
13477 macro_define_function (file, line, name,
6e70227d 13478 argc, (const char **) argv,
2e276125
JB
13479 p + 1);
13480 else if (*p == '\0')
13481 {
13482 /* Complain, but do define it. */
4d3c2250 13483 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13484 macro_define_function (file, line, name,
6e70227d 13485 argc, (const char **) argv,
2e276125
JB
13486 p);
13487 }
13488 else
13489 /* Just complain. */
4d3c2250 13490 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13491 }
13492 else
13493 /* Just complain. */
4d3c2250 13494 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13495
13496 xfree (name);
13497 {
13498 int i;
13499
13500 for (i = 0; i < argc; i++)
13501 xfree (argv[i]);
13502 }
13503 xfree (argv);
13504 }
13505 else
4d3c2250 13506 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13507}
13508
13509
13510static void
13511dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13512 char *comp_dir, bfd *abfd,
e7c27a73 13513 struct dwarf2_cu *cu)
2e276125 13514{
fe1b8b76 13515 gdb_byte *mac_ptr, *mac_end;
2e276125 13516 struct macro_source_file *current_file = 0;
757a13d0
JK
13517 enum dwarf_macinfo_record_type macinfo_type;
13518 int at_commandline;
2e276125 13519
be391dca
TT
13520 dwarf2_read_section (dwarf2_per_objfile->objfile,
13521 &dwarf2_per_objfile->macinfo);
dce234bc 13522 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13523 {
e2e0b3e5 13524 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13525 return;
13526 }
13527
757a13d0
JK
13528 /* First pass: Find the name of the base filename.
13529 This filename is needed in order to process all macros whose definition
13530 (or undefinition) comes from the command line. These macros are defined
13531 before the first DW_MACINFO_start_file entry, and yet still need to be
13532 associated to the base file.
13533
13534 To determine the base file name, we scan the macro definitions until we
13535 reach the first DW_MACINFO_start_file entry. We then initialize
13536 CURRENT_FILE accordingly so that any macro definition found before the
13537 first DW_MACINFO_start_file can still be associated to the base file. */
13538
dce234bc
PP
13539 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13540 mac_end = dwarf2_per_objfile->macinfo.buffer
13541 + dwarf2_per_objfile->macinfo.size;
2e276125 13542
757a13d0 13543 do
2e276125 13544 {
2e276125
JB
13545 /* Do we at least have room for a macinfo type byte? */
13546 if (mac_ptr >= mac_end)
13547 {
757a13d0
JK
13548 /* Complaint is printed during the second pass as GDB will probably
13549 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13550 break;
2e276125
JB
13551 }
13552
13553 macinfo_type = read_1_byte (abfd, mac_ptr);
13554 mac_ptr++;
13555
13556 switch (macinfo_type)
13557 {
13558 /* A zero macinfo type indicates the end of the macro
13559 information. */
13560 case 0:
757a13d0
JK
13561 break;
13562
13563 case DW_MACINFO_define:
13564 case DW_MACINFO_undef:
13565 /* Only skip the data by MAC_PTR. */
13566 {
13567 unsigned int bytes_read;
13568
13569 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13570 mac_ptr += bytes_read;
9b1c24c8 13571 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13572 mac_ptr += bytes_read;
13573 }
13574 break;
13575
13576 case DW_MACINFO_start_file:
13577 {
13578 unsigned int bytes_read;
13579 int line, file;
13580
13581 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13582 mac_ptr += bytes_read;
13583 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13584 mac_ptr += bytes_read;
13585
13586 current_file = macro_start_file (file, line, current_file, comp_dir,
13587 lh, cu->objfile);
13588 }
13589 break;
13590
13591 case DW_MACINFO_end_file:
13592 /* No data to skip by MAC_PTR. */
13593 break;
13594
13595 case DW_MACINFO_vendor_ext:
13596 /* Only skip the data by MAC_PTR. */
13597 {
13598 unsigned int bytes_read;
13599
13600 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13601 mac_ptr += bytes_read;
9b1c24c8 13602 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13603 mac_ptr += bytes_read;
13604 }
13605 break;
13606
13607 default:
13608 break;
13609 }
13610 } while (macinfo_type != 0 && current_file == NULL);
13611
13612 /* Second pass: Process all entries.
13613
13614 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13615 command-line macro definitions/undefinitions. This flag is unset when we
13616 reach the first DW_MACINFO_start_file entry. */
13617
dce234bc 13618 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13619
13620 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13621 GDB is still reading the definitions from command line. First
13622 DW_MACINFO_start_file will need to be ignored as it was already executed
13623 to create CURRENT_FILE for the main source holding also the command line
13624 definitions. On first met DW_MACINFO_start_file this flag is reset to
13625 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13626
13627 at_commandline = 1;
13628
13629 do
13630 {
13631 /* Do we at least have room for a macinfo type byte? */
13632 if (mac_ptr >= mac_end)
13633 {
13634 dwarf2_macros_too_long_complaint ();
13635 break;
13636 }
13637
13638 macinfo_type = read_1_byte (abfd, mac_ptr);
13639 mac_ptr++;
13640
13641 switch (macinfo_type)
13642 {
13643 /* A zero macinfo type indicates the end of the macro
13644 information. */
13645 case 0:
13646 break;
2e276125
JB
13647
13648 case DW_MACINFO_define:
13649 case DW_MACINFO_undef:
13650 {
891d2f0b 13651 unsigned int bytes_read;
2e276125
JB
13652 int line;
13653 char *body;
13654
13655 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13656 mac_ptr += bytes_read;
9b1c24c8 13657 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13658 mac_ptr += bytes_read;
13659
13660 if (! current_file)
757a13d0
JK
13661 {
13662 /* DWARF violation as no main source is present. */
13663 complaint (&symfile_complaints,
13664 _("debug info with no main source gives macro %s "
13665 "on line %d: %s"),
6e70227d
DE
13666 macinfo_type == DW_MACINFO_define ?
13667 _("definition") :
905e0470
PM
13668 macinfo_type == DW_MACINFO_undef ?
13669 _("undefinition") :
13670 _("something-or-other"), line, body);
757a13d0
JK
13671 break;
13672 }
13673 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13674 complaint (&symfile_complaints,
757a13d0
JK
13675 _("debug info gives %s macro %s with %s line %d: %s"),
13676 at_commandline ? _("command-line") : _("in-file"),
905e0470 13677 macinfo_type == DW_MACINFO_define ?
6e70227d 13678 _("definition") :
905e0470
PM
13679 macinfo_type == DW_MACINFO_undef ?
13680 _("undefinition") :
13681 _("something-or-other"),
757a13d0
JK
13682 line == 0 ? _("zero") : _("non-zero"), line, body);
13683
13684 if (macinfo_type == DW_MACINFO_define)
13685 parse_macro_definition (current_file, line, body);
13686 else if (macinfo_type == DW_MACINFO_undef)
13687 macro_undef (current_file, line, body);
2e276125
JB
13688 }
13689 break;
13690
13691 case DW_MACINFO_start_file:
13692 {
891d2f0b 13693 unsigned int bytes_read;
2e276125
JB
13694 int line, file;
13695
13696 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13697 mac_ptr += bytes_read;
13698 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13699 mac_ptr += bytes_read;
13700
757a13d0
JK
13701 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13702 complaint (&symfile_complaints,
13703 _("debug info gives source %d included "
13704 "from %s at %s line %d"),
13705 file, at_commandline ? _("command-line") : _("file"),
13706 line == 0 ? _("zero") : _("non-zero"), line);
13707
13708 if (at_commandline)
13709 {
13710 /* This DW_MACINFO_start_file was executed in the pass one. */
13711 at_commandline = 0;
13712 }
13713 else
13714 current_file = macro_start_file (file, line,
13715 current_file, comp_dir,
13716 lh, cu->objfile);
2e276125
JB
13717 }
13718 break;
13719
13720 case DW_MACINFO_end_file:
13721 if (! current_file)
4d3c2250 13722 complaint (&symfile_complaints,
e2e0b3e5 13723 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13724 else
13725 {
13726 current_file = current_file->included_by;
13727 if (! current_file)
13728 {
13729 enum dwarf_macinfo_record_type next_type;
13730
13731 /* GCC circa March 2002 doesn't produce the zero
13732 type byte marking the end of the compilation
13733 unit. Complain if it's not there, but exit no
13734 matter what. */
13735
13736 /* Do we at least have room for a macinfo type byte? */
13737 if (mac_ptr >= mac_end)
13738 {
4d3c2250 13739 dwarf2_macros_too_long_complaint ();
2e276125
JB
13740 return;
13741 }
13742
13743 /* We don't increment mac_ptr here, so this is just
13744 a look-ahead. */
13745 next_type = read_1_byte (abfd, mac_ptr);
13746 if (next_type != 0)
4d3c2250 13747 complaint (&symfile_complaints,
e2e0b3e5 13748 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13749
13750 return;
13751 }
13752 }
13753 break;
13754
13755 case DW_MACINFO_vendor_ext:
13756 {
891d2f0b 13757 unsigned int bytes_read;
2e276125
JB
13758 int constant;
13759 char *string;
13760
13761 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13762 mac_ptr += bytes_read;
9b1c24c8 13763 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13764 mac_ptr += bytes_read;
13765
13766 /* We don't recognize any vendor extensions. */
13767 }
13768 break;
13769 }
757a13d0 13770 } while (macinfo_type != 0);
2e276125 13771}
8e19ed76
PS
13772
13773/* Check if the attribute's form is a DW_FORM_block*
13774 if so return true else false. */
13775static int
13776attr_form_is_block (struct attribute *attr)
13777{
13778 return (attr == NULL ? 0 :
13779 attr->form == DW_FORM_block1
13780 || attr->form == DW_FORM_block2
13781 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13782 || attr->form == DW_FORM_block
13783 || attr->form == DW_FORM_exprloc);
8e19ed76 13784}
4c2df51b 13785
c6a0999f
JB
13786/* Return non-zero if ATTR's value is a section offset --- classes
13787 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13788 You may use DW_UNSND (attr) to retrieve such offsets.
13789
13790 Section 7.5.4, "Attribute Encodings", explains that no attribute
13791 may have a value that belongs to more than one of these classes; it
13792 would be ambiguous if we did, because we use the same forms for all
13793 of them. */
3690dd37
JB
13794static int
13795attr_form_is_section_offset (struct attribute *attr)
13796{
13797 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13798 || attr->form == DW_FORM_data8
13799 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13800}
13801
13802
13803/* Return non-zero if ATTR's value falls in the 'constant' class, or
13804 zero otherwise. When this function returns true, you can apply
13805 dwarf2_get_attr_constant_value to it.
13806
13807 However, note that for some attributes you must check
13808 attr_form_is_section_offset before using this test. DW_FORM_data4
13809 and DW_FORM_data8 are members of both the constant class, and of
13810 the classes that contain offsets into other debug sections
13811 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13812 that, if an attribute's can be either a constant or one of the
13813 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13814 taken as section offsets, not constants. */
13815static int
13816attr_form_is_constant (struct attribute *attr)
13817{
13818 switch (attr->form)
13819 {
13820 case DW_FORM_sdata:
13821 case DW_FORM_udata:
13822 case DW_FORM_data1:
13823 case DW_FORM_data2:
13824 case DW_FORM_data4:
13825 case DW_FORM_data8:
13826 return 1;
13827 default:
13828 return 0;
13829 }
13830}
13831
4c2df51b
DJ
13832static void
13833dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13834 struct dwarf2_cu *cu)
4c2df51b 13835{
3690dd37 13836 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13837 /* ".debug_loc" may not exist at all, or the offset may be outside
13838 the section. If so, fall through to the complaint in the
13839 other branch. */
dce234bc 13840 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13841 {
0d53c4c4 13842 struct dwarf2_loclist_baton *baton;
4c2df51b 13843
4a146b47 13844 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13845 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13846 baton->per_cu = cu->per_cu;
13847 gdb_assert (baton->per_cu);
4c2df51b 13848
be391dca
TT
13849 dwarf2_read_section (dwarf2_per_objfile->objfile,
13850 &dwarf2_per_objfile->loc);
13851
0d53c4c4
DJ
13852 /* We don't know how long the location list is, but make sure we
13853 don't run off the edge of the section. */
dce234bc
PP
13854 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13855 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13856 baton->base_address = cu->base_address;
13857 if (cu->base_known == 0)
0d53c4c4 13858 complaint (&symfile_complaints,
e2e0b3e5 13859 _("Location list used without specifying the CU base address."));
4c2df51b 13860
768a979c 13861 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13862 SYMBOL_LOCATION_BATON (sym) = baton;
13863 }
13864 else
13865 {
13866 struct dwarf2_locexpr_baton *baton;
13867
4a146b47 13868 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13869 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13870 baton->per_cu = cu->per_cu;
13871 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13872
13873 if (attr_form_is_block (attr))
13874 {
13875 /* Note that we're just copying the block's data pointer
13876 here, not the actual data. We're still pointing into the
6502dd73
DJ
13877 info_buffer for SYM's objfile; right now we never release
13878 that buffer, but when we do clean up properly this may
13879 need to change. */
0d53c4c4
DJ
13880 baton->size = DW_BLOCK (attr)->size;
13881 baton->data = DW_BLOCK (attr)->data;
13882 }
13883 else
13884 {
13885 dwarf2_invalid_attrib_class_complaint ("location description",
13886 SYMBOL_NATURAL_NAME (sym));
13887 baton->size = 0;
13888 baton->data = NULL;
13889 }
6e70227d 13890
768a979c 13891 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13892 SYMBOL_LOCATION_BATON (sym) = baton;
13893 }
4c2df51b 13894}
6502dd73 13895
9aa1f1e3
TT
13896/* Return the OBJFILE associated with the compilation unit CU. If CU
13897 came from a separate debuginfo file, then the master objfile is
13898 returned. */
ae0d2f24
UW
13899
13900struct objfile *
13901dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13902{
9291a0cd 13903 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13904
13905 /* Return the master objfile, so that we can report and look up the
13906 correct file containing this variable. */
13907 if (objfile->separate_debug_objfile_backlink)
13908 objfile = objfile->separate_debug_objfile_backlink;
13909
13910 return objfile;
13911}
13912
13913/* Return the address size given in the compilation unit header for CU. */
13914
13915CORE_ADDR
13916dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13917{
13918 if (per_cu->cu)
13919 return per_cu->cu->header.addr_size;
13920 else
13921 {
13922 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13923 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13924 struct dwarf2_per_objfile *per_objfile
13925 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13926 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13927 struct comp_unit_head cu_header;
9a619af0 13928
ae0d2f24
UW
13929 memset (&cu_header, 0, sizeof cu_header);
13930 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13931 return cu_header.addr_size;
13932 }
13933}
13934
9eae7c52
TT
13935/* Return the offset size given in the compilation unit header for CU. */
13936
13937int
13938dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13939{
13940 if (per_cu->cu)
13941 return per_cu->cu->header.offset_size;
13942 else
13943 {
13944 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13945 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
13946 struct dwarf2_per_objfile *per_objfile
13947 = objfile_data (objfile, dwarf2_objfile_data_key);
13948 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13949 struct comp_unit_head cu_header;
13950
13951 memset (&cu_header, 0, sizeof cu_header);
13952 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13953 return cu_header.offset_size;
13954 }
13955}
13956
9aa1f1e3
TT
13957/* Return the text offset of the CU. The returned offset comes from
13958 this CU's objfile. If this objfile came from a separate debuginfo
13959 file, then the offset may be different from the corresponding
13960 offset in the parent objfile. */
13961
13962CORE_ADDR
13963dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13964{
bb3fa9d0 13965 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
13966
13967 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13968}
13969
348e048f
DE
13970/* Locate the .debug_info compilation unit from CU's objfile which contains
13971 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
13972
13973static struct dwarf2_per_cu_data *
c764a876 13974dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
13975 struct objfile *objfile)
13976{
13977 struct dwarf2_per_cu_data *this_cu;
13978 int low, high;
13979
ae038cb0
DJ
13980 low = 0;
13981 high = dwarf2_per_objfile->n_comp_units - 1;
13982 while (high > low)
13983 {
13984 int mid = low + (high - low) / 2;
9a619af0 13985
ae038cb0
DJ
13986 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13987 high = mid;
13988 else
13989 low = mid + 1;
13990 }
13991 gdb_assert (low == high);
13992 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13993 {
10b3939b 13994 if (low == 0)
8a3fe4f8
AC
13995 error (_("Dwarf Error: could not find partial DIE containing "
13996 "offset 0x%lx [in module %s]"),
10b3939b
DJ
13997 (long) offset, bfd_get_filename (objfile->obfd));
13998
ae038cb0
DJ
13999 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14000 return dwarf2_per_objfile->all_comp_units[low-1];
14001 }
14002 else
14003 {
14004 this_cu = dwarf2_per_objfile->all_comp_units[low];
14005 if (low == dwarf2_per_objfile->n_comp_units - 1
14006 && offset >= this_cu->offset + this_cu->length)
c764a876 14007 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14008 gdb_assert (offset < this_cu->offset + this_cu->length);
14009 return this_cu;
14010 }
14011}
14012
10b3939b
DJ
14013/* Locate the compilation unit from OBJFILE which is located at exactly
14014 OFFSET. Raises an error on failure. */
14015
ae038cb0 14016static struct dwarf2_per_cu_data *
c764a876 14017dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14018{
14019 struct dwarf2_per_cu_data *this_cu;
9a619af0 14020
ae038cb0
DJ
14021 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14022 if (this_cu->offset != offset)
c764a876 14023 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14024 return this_cu;
14025}
14026
93311388
DE
14027/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14028
14029static struct dwarf2_cu *
14030alloc_one_comp_unit (struct objfile *objfile)
14031{
14032 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14033 cu->objfile = objfile;
14034 obstack_init (&cu->comp_unit_obstack);
14035 return cu;
14036}
14037
ae038cb0
DJ
14038/* Release one cached compilation unit, CU. We unlink it from the tree
14039 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14040 the caller is responsible for that.
14041 NOTE: DATA is a void * because this function is also used as a
14042 cleanup routine. */
ae038cb0
DJ
14043
14044static void
14045free_one_comp_unit (void *data)
14046{
14047 struct dwarf2_cu *cu = data;
14048
14049 if (cu->per_cu != NULL)
14050 cu->per_cu->cu = NULL;
14051 cu->per_cu = NULL;
14052
14053 obstack_free (&cu->comp_unit_obstack, NULL);
14054
14055 xfree (cu);
14056}
14057
72bf9492 14058/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14059 when we're finished with it. We can't free the pointer itself, but be
14060 sure to unlink it from the cache. Also release any associated storage
14061 and perform cache maintenance.
72bf9492
DJ
14062
14063 Only used during partial symbol parsing. */
14064
14065static void
14066free_stack_comp_unit (void *data)
14067{
14068 struct dwarf2_cu *cu = data;
14069
14070 obstack_free (&cu->comp_unit_obstack, NULL);
14071 cu->partial_dies = NULL;
ae038cb0
DJ
14072
14073 if (cu->per_cu != NULL)
14074 {
14075 /* This compilation unit is on the stack in our caller, so we
14076 should not xfree it. Just unlink it. */
14077 cu->per_cu->cu = NULL;
14078 cu->per_cu = NULL;
14079
14080 /* If we had a per-cu pointer, then we may have other compilation
14081 units loaded, so age them now. */
14082 age_cached_comp_units ();
14083 }
14084}
14085
14086/* Free all cached compilation units. */
14087
14088static void
14089free_cached_comp_units (void *data)
14090{
14091 struct dwarf2_per_cu_data *per_cu, **last_chain;
14092
14093 per_cu = dwarf2_per_objfile->read_in_chain;
14094 last_chain = &dwarf2_per_objfile->read_in_chain;
14095 while (per_cu != NULL)
14096 {
14097 struct dwarf2_per_cu_data *next_cu;
14098
14099 next_cu = per_cu->cu->read_in_chain;
14100
14101 free_one_comp_unit (per_cu->cu);
14102 *last_chain = next_cu;
14103
14104 per_cu = next_cu;
14105 }
14106}
14107
14108/* Increase the age counter on each cached compilation unit, and free
14109 any that are too old. */
14110
14111static void
14112age_cached_comp_units (void)
14113{
14114 struct dwarf2_per_cu_data *per_cu, **last_chain;
14115
14116 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14117 per_cu = dwarf2_per_objfile->read_in_chain;
14118 while (per_cu != NULL)
14119 {
14120 per_cu->cu->last_used ++;
14121 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14122 dwarf2_mark (per_cu->cu);
14123 per_cu = per_cu->cu->read_in_chain;
14124 }
14125
14126 per_cu = dwarf2_per_objfile->read_in_chain;
14127 last_chain = &dwarf2_per_objfile->read_in_chain;
14128 while (per_cu != NULL)
14129 {
14130 struct dwarf2_per_cu_data *next_cu;
14131
14132 next_cu = per_cu->cu->read_in_chain;
14133
14134 if (!per_cu->cu->mark)
14135 {
14136 free_one_comp_unit (per_cu->cu);
14137 *last_chain = next_cu;
14138 }
14139 else
14140 last_chain = &per_cu->cu->read_in_chain;
14141
14142 per_cu = next_cu;
14143 }
14144}
14145
14146/* Remove a single compilation unit from the cache. */
14147
14148static void
14149free_one_cached_comp_unit (void *target_cu)
14150{
14151 struct dwarf2_per_cu_data *per_cu, **last_chain;
14152
14153 per_cu = dwarf2_per_objfile->read_in_chain;
14154 last_chain = &dwarf2_per_objfile->read_in_chain;
14155 while (per_cu != NULL)
14156 {
14157 struct dwarf2_per_cu_data *next_cu;
14158
14159 next_cu = per_cu->cu->read_in_chain;
14160
14161 if (per_cu->cu == target_cu)
14162 {
14163 free_one_comp_unit (per_cu->cu);
14164 *last_chain = next_cu;
14165 break;
14166 }
14167 else
14168 last_chain = &per_cu->cu->read_in_chain;
14169
14170 per_cu = next_cu;
14171 }
14172}
14173
fe3e1990
DJ
14174/* Release all extra memory associated with OBJFILE. */
14175
14176void
14177dwarf2_free_objfile (struct objfile *objfile)
14178{
14179 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14180
14181 if (dwarf2_per_objfile == NULL)
14182 return;
14183
14184 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14185 free_cached_comp_units (NULL);
14186
9291a0cd
TT
14187 if (dwarf2_per_objfile->using_index)
14188 {
14189 int i;
14190
14191 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14192 {
14193 int j;
14194 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14195
14196 if (!cu->v.quick->lines)
14197 continue;
14198
14199 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
14200 {
14201 if (cu->v.quick->file_names)
14202 xfree ((void *) cu->v.quick->file_names[j]);
14203 if (cu->v.quick->full_names)
14204 xfree ((void *) cu->v.quick->full_names[j]);
14205 }
14206
14207 free_line_header (cu->v.quick->lines);
14208 }
14209 }
14210
fe3e1990
DJ
14211 /* Everything else should be on the objfile obstack. */
14212}
14213
1c379e20
DJ
14214/* A pair of DIE offset and GDB type pointer. We store these
14215 in a hash table separate from the DIEs, and preserve them
14216 when the DIEs are flushed out of cache. */
14217
14218struct dwarf2_offset_and_type
14219{
14220 unsigned int offset;
14221 struct type *type;
14222};
14223
14224/* Hash function for a dwarf2_offset_and_type. */
14225
14226static hashval_t
14227offset_and_type_hash (const void *item)
14228{
14229 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14230
1c379e20
DJ
14231 return ofs->offset;
14232}
14233
14234/* Equality function for a dwarf2_offset_and_type. */
14235
14236static int
14237offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14238{
14239 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14240 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14241
1c379e20
DJ
14242 return ofs_lhs->offset == ofs_rhs->offset;
14243}
14244
14245/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14246 table if necessary. For convenience, return TYPE.
14247
14248 The DIEs reading must have careful ordering to:
14249 * Not cause infite loops trying to read in DIEs as a prerequisite for
14250 reading current DIE.
14251 * Not trying to dereference contents of still incompletely read in types
14252 while reading in other DIEs.
14253 * Enable referencing still incompletely read in types just by a pointer to
14254 the type without accessing its fields.
14255
14256 Therefore caller should follow these rules:
14257 * Try to fetch any prerequisite types we may need to build this DIE type
14258 before building the type and calling set_die_type.
e71ec853 14259 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14260 possible before fetching more types to complete the current type.
14261 * Make the type as complete as possible before fetching more types. */
1c379e20 14262
f792889a 14263static struct type *
1c379e20
DJ
14264set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14265{
14266 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14267 struct objfile *objfile = cu->objfile;
14268 htab_t *type_hash_ptr;
1c379e20 14269
b4ba55a1
JB
14270 /* For Ada types, make sure that the gnat-specific data is always
14271 initialized (if not already set). There are a few types where
14272 we should not be doing so, because the type-specific area is
14273 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14274 where the type-specific area is used to store the floatformat).
14275 But this is not a problem, because the gnat-specific information
14276 is actually not needed for these types. */
14277 if (need_gnat_info (cu)
14278 && TYPE_CODE (type) != TYPE_CODE_FUNC
14279 && TYPE_CODE (type) != TYPE_CODE_FLT
14280 && !HAVE_GNAT_AUX_INFO (type))
14281 INIT_GNAT_SPECIFIC (type);
14282
673bfd45
DE
14283 if (cu->per_cu->from_debug_types)
14284 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14285 else
14286 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14287
14288 if (*type_hash_ptr == NULL)
f792889a 14289 {
673bfd45
DE
14290 *type_hash_ptr
14291 = htab_create_alloc_ex (127,
f792889a
DJ
14292 offset_and_type_hash,
14293 offset_and_type_eq,
14294 NULL,
673bfd45 14295 &objfile->objfile_obstack,
f792889a
DJ
14296 hashtab_obstack_allocate,
14297 dummy_obstack_deallocate);
f792889a 14298 }
1c379e20
DJ
14299
14300 ofs.offset = die->offset;
14301 ofs.type = type;
14302 slot = (struct dwarf2_offset_and_type **)
673bfd45 14303 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14304 if (*slot)
14305 complaint (&symfile_complaints,
14306 _("A problem internal to GDB: DIE 0x%x has type already set"),
14307 die->offset);
673bfd45 14308 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14309 **slot = ofs;
f792889a 14310 return type;
1c379e20
DJ
14311}
14312
673bfd45
DE
14313/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14314 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14315
14316static struct type *
673bfd45
DE
14317get_die_type_at_offset (unsigned int offset,
14318 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14319{
14320 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14321 htab_t type_hash;
f792889a 14322
673bfd45
DE
14323 if (per_cu->from_debug_types)
14324 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14325 else
14326 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14327 if (type_hash == NULL)
14328 return NULL;
1c379e20 14329
673bfd45 14330 ofs.offset = offset;
1c379e20
DJ
14331 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14332 if (slot)
14333 return slot->type;
14334 else
14335 return NULL;
14336}
14337
673bfd45
DE
14338/* Look up the type for DIE in the appropriate type_hash table,
14339 or return NULL if DIE does not have a saved type. */
14340
14341static struct type *
14342get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14343{
14344 return get_die_type_at_offset (die->offset, cu->per_cu);
14345}
14346
10b3939b
DJ
14347/* Add a dependence relationship from CU to REF_PER_CU. */
14348
14349static void
14350dwarf2_add_dependence (struct dwarf2_cu *cu,
14351 struct dwarf2_per_cu_data *ref_per_cu)
14352{
14353 void **slot;
14354
14355 if (cu->dependencies == NULL)
14356 cu->dependencies
14357 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14358 NULL, &cu->comp_unit_obstack,
14359 hashtab_obstack_allocate,
14360 dummy_obstack_deallocate);
14361
14362 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14363 if (*slot == NULL)
14364 *slot = ref_per_cu;
14365}
1c379e20 14366
f504f079
DE
14367/* Subroutine of dwarf2_mark to pass to htab_traverse.
14368 Set the mark field in every compilation unit in the
ae038cb0
DJ
14369 cache that we must keep because we are keeping CU. */
14370
10b3939b
DJ
14371static int
14372dwarf2_mark_helper (void **slot, void *data)
14373{
14374 struct dwarf2_per_cu_data *per_cu;
14375
14376 per_cu = (struct dwarf2_per_cu_data *) *slot;
14377 if (per_cu->cu->mark)
14378 return 1;
14379 per_cu->cu->mark = 1;
14380
14381 if (per_cu->cu->dependencies != NULL)
14382 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14383
14384 return 1;
14385}
14386
f504f079
DE
14387/* Set the mark field in CU and in every other compilation unit in the
14388 cache that we must keep because we are keeping CU. */
14389
ae038cb0
DJ
14390static void
14391dwarf2_mark (struct dwarf2_cu *cu)
14392{
14393 if (cu->mark)
14394 return;
14395 cu->mark = 1;
10b3939b
DJ
14396 if (cu->dependencies != NULL)
14397 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14398}
14399
14400static void
14401dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14402{
14403 while (per_cu)
14404 {
14405 per_cu->cu->mark = 0;
14406 per_cu = per_cu->cu->read_in_chain;
14407 }
72bf9492
DJ
14408}
14409
72bf9492
DJ
14410/* Trivial hash function for partial_die_info: the hash value of a DIE
14411 is its offset in .debug_info for this objfile. */
14412
14413static hashval_t
14414partial_die_hash (const void *item)
14415{
14416 const struct partial_die_info *part_die = item;
9a619af0 14417
72bf9492
DJ
14418 return part_die->offset;
14419}
14420
14421/* Trivial comparison function for partial_die_info structures: two DIEs
14422 are equal if they have the same offset. */
14423
14424static int
14425partial_die_eq (const void *item_lhs, const void *item_rhs)
14426{
14427 const struct partial_die_info *part_die_lhs = item_lhs;
14428 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14429
72bf9492
DJ
14430 return part_die_lhs->offset == part_die_rhs->offset;
14431}
14432
ae038cb0
DJ
14433static struct cmd_list_element *set_dwarf2_cmdlist;
14434static struct cmd_list_element *show_dwarf2_cmdlist;
14435
14436static void
14437set_dwarf2_cmd (char *args, int from_tty)
14438{
14439 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14440}
14441
14442static void
14443show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14444{
ae038cb0
DJ
14445 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14446}
14447
dce234bc
PP
14448/* If section described by INFO was mmapped, munmap it now. */
14449
14450static void
14451munmap_section_buffer (struct dwarf2_section_info *info)
14452{
14453 if (info->was_mmapped)
14454 {
14455#ifdef HAVE_MMAP
14456 intptr_t begin = (intptr_t) info->buffer;
14457 intptr_t map_begin = begin & ~(pagesize - 1);
14458 size_t map_length = info->size + begin - map_begin;
9a619af0 14459
dce234bc
PP
14460 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14461#else
14462 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14463 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14464#endif
14465 }
14466}
14467
14468/* munmap debug sections for OBJFILE, if necessary. */
14469
14470static void
c1bd65d0 14471dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14472{
14473 struct dwarf2_per_objfile *data = d;
9a619af0 14474
dce234bc
PP
14475 munmap_section_buffer (&data->info);
14476 munmap_section_buffer (&data->abbrev);
14477 munmap_section_buffer (&data->line);
14478 munmap_section_buffer (&data->str);
14479 munmap_section_buffer (&data->macinfo);
14480 munmap_section_buffer (&data->ranges);
14481 munmap_section_buffer (&data->loc);
14482 munmap_section_buffer (&data->frame);
14483 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14484 munmap_section_buffer (&data->gdb_index);
14485}
14486
14487\f
14488
14489/* The contents of the hash table we create when building the string
14490 table. */
14491struct strtab_entry
14492{
14493 offset_type offset;
14494 const char *str;
14495};
14496
14497/* Hash function for a strtab_entry. */
14498static hashval_t
14499hash_strtab_entry (const void *e)
14500{
14501 const struct strtab_entry *entry = e;
14502 return mapped_index_string_hash (entry->str);
14503}
14504
14505/* Equality function for a strtab_entry. */
14506static int
14507eq_strtab_entry (const void *a, const void *b)
14508{
14509 const struct strtab_entry *ea = a;
14510 const struct strtab_entry *eb = b;
14511 return !strcmp (ea->str, eb->str);
14512}
14513
14514/* Create a strtab_entry hash table. */
14515static htab_t
14516create_strtab (void)
14517{
14518 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14519 xfree, xcalloc, xfree);
14520}
14521
14522/* Add a string to the constant pool. Return the string's offset in
14523 host order. */
14524static offset_type
14525add_string (htab_t table, struct obstack *cpool, const char *str)
14526{
14527 void **slot;
14528 struct strtab_entry entry;
14529 struct strtab_entry *result;
14530
14531 entry.str = str;
14532 slot = htab_find_slot (table, &entry, INSERT);
14533 if (*slot)
14534 result = *slot;
14535 else
14536 {
14537 result = XNEW (struct strtab_entry);
14538 result->offset = obstack_object_size (cpool);
14539 result->str = str;
14540 obstack_grow_str0 (cpool, str);
14541 *slot = result;
14542 }
14543 return result->offset;
14544}
14545
14546/* An entry in the symbol table. */
14547struct symtab_index_entry
14548{
14549 /* The name of the symbol. */
14550 const char *name;
14551 /* The offset of the name in the constant pool. */
14552 offset_type index_offset;
14553 /* A sorted vector of the indices of all the CUs that hold an object
14554 of this name. */
14555 VEC (offset_type) *cu_indices;
14556};
14557
14558/* The symbol table. This is a power-of-2-sized hash table. */
14559struct mapped_symtab
14560{
14561 offset_type n_elements;
14562 offset_type size;
14563 struct symtab_index_entry **data;
14564};
14565
14566/* Hash function for a symtab_index_entry. */
14567static hashval_t
14568hash_symtab_entry (const void *e)
14569{
14570 const struct symtab_index_entry *entry = e;
14571 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14572 sizeof (offset_type) * VEC_length (offset_type,
14573 entry->cu_indices),
14574 0);
14575}
14576
14577/* Equality function for a symtab_index_entry. */
14578static int
14579eq_symtab_entry (const void *a, const void *b)
14580{
14581 const struct symtab_index_entry *ea = a;
14582 const struct symtab_index_entry *eb = b;
14583 int len = VEC_length (offset_type, ea->cu_indices);
14584 if (len != VEC_length (offset_type, eb->cu_indices))
14585 return 0;
14586 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14587 VEC_address (offset_type, eb->cu_indices),
14588 sizeof (offset_type) * len);
14589}
14590
14591/* Destroy a symtab_index_entry. */
14592static void
14593delete_symtab_entry (void *p)
14594{
14595 struct symtab_index_entry *entry = p;
14596 VEC_free (offset_type, entry->cu_indices);
14597 xfree (entry);
14598}
14599
14600/* Create a hash table holding symtab_index_entry objects. */
14601static htab_t
14602create_index_table (void)
14603{
14604 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14605 delete_symtab_entry, xcalloc, xfree);
14606}
14607
14608/* Create a new mapped symtab object. */
14609static struct mapped_symtab *
14610create_mapped_symtab (void)
14611{
14612 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14613 symtab->n_elements = 0;
14614 symtab->size = 1024;
14615 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14616 return symtab;
14617}
14618
14619/* Destroy a mapped_symtab. */
14620static void
14621cleanup_mapped_symtab (void *p)
14622{
14623 struct mapped_symtab *symtab = p;
14624 /* The contents of the array are freed when the other hash table is
14625 destroyed. */
14626 xfree (symtab->data);
14627 xfree (symtab);
14628}
14629
14630/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14631 the slot. */
14632static struct symtab_index_entry **
14633find_slot (struct mapped_symtab *symtab, const char *name)
14634{
14635 offset_type index, step, hash = mapped_index_string_hash (name);
14636
14637 index = hash & (symtab->size - 1);
14638 step = ((hash * 17) & (symtab->size - 1)) | 1;
14639
14640 for (;;)
14641 {
14642 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14643 return &symtab->data[index];
14644 index = (index + step) & (symtab->size - 1);
14645 }
14646}
14647
14648/* Expand SYMTAB's hash table. */
14649static void
14650hash_expand (struct mapped_symtab *symtab)
14651{
14652 offset_type old_size = symtab->size;
14653 offset_type i;
14654 struct symtab_index_entry **old_entries = symtab->data;
14655
14656 symtab->size *= 2;
14657 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14658
14659 for (i = 0; i < old_size; ++i)
14660 {
14661 if (old_entries[i])
14662 {
14663 struct symtab_index_entry **slot = find_slot (symtab,
14664 old_entries[i]->name);
14665 *slot = old_entries[i];
14666 }
14667 }
14668
14669 xfree (old_entries);
14670}
14671
14672/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14673 is the index of the CU in which the symbol appears. */
14674static void
14675add_index_entry (struct mapped_symtab *symtab, const char *name,
14676 offset_type cu_index)
14677{
14678 struct symtab_index_entry **slot;
14679
14680 ++symtab->n_elements;
14681 if (4 * symtab->n_elements / 3 >= symtab->size)
14682 hash_expand (symtab);
14683
14684 slot = find_slot (symtab, name);
14685 if (!*slot)
14686 {
14687 *slot = XNEW (struct symtab_index_entry);
14688 (*slot)->name = name;
14689 (*slot)->cu_indices = NULL;
14690 }
14691 /* Don't push an index twice. Due to how we add entries we only
14692 have to check the last one. */
14693 if (VEC_empty (offset_type, (*slot)->cu_indices)
14694 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14695 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14696}
14697
14698/* Add a vector of indices to the constant pool. */
14699static offset_type
14700add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14701 struct symtab_index_entry *entry)
14702{
14703 void **slot;
14704
14705 slot = htab_find_slot (index_table, entry, INSERT);
14706 if (!*slot)
14707 {
14708 offset_type len = VEC_length (offset_type, entry->cu_indices);
14709 offset_type val = MAYBE_SWAP (len);
14710 offset_type iter;
14711 int i;
14712
14713 *slot = entry;
14714 entry->index_offset = obstack_object_size (cpool);
14715
14716 obstack_grow (cpool, &val, sizeof (val));
14717 for (i = 0;
14718 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14719 ++i)
14720 {
14721 val = MAYBE_SWAP (iter);
14722 obstack_grow (cpool, &val, sizeof (val));
14723 }
14724 }
14725 else
14726 {
14727 struct symtab_index_entry *old_entry = *slot;
14728 entry->index_offset = old_entry->index_offset;
14729 entry = old_entry;
14730 }
14731 return entry->index_offset;
14732}
14733
14734/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14735 constant pool entries going into the obstack CPOOL. */
14736static void
14737write_hash_table (struct mapped_symtab *symtab,
14738 struct obstack *output, struct obstack *cpool)
14739{
14740 offset_type i;
14741 htab_t index_table;
14742 htab_t str_table;
14743
14744 index_table = create_index_table ();
14745 str_table = create_strtab ();
14746 /* We add all the index vectors to the constant pool first, to
14747 ensure alignment is ok. */
14748 for (i = 0; i < symtab->size; ++i)
14749 {
14750 if (symtab->data[i])
14751 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14752 }
14753
14754 /* Now write out the hash table. */
14755 for (i = 0; i < symtab->size; ++i)
14756 {
14757 offset_type str_off, vec_off;
14758
14759 if (symtab->data[i])
14760 {
14761 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14762 vec_off = symtab->data[i]->index_offset;
14763 }
14764 else
14765 {
14766 /* While 0 is a valid constant pool index, it is not valid
14767 to have 0 for both offsets. */
14768 str_off = 0;
14769 vec_off = 0;
14770 }
14771
14772 str_off = MAYBE_SWAP (str_off);
14773 vec_off = MAYBE_SWAP (vec_off);
14774
14775 obstack_grow (output, &str_off, sizeof (str_off));
14776 obstack_grow (output, &vec_off, sizeof (vec_off));
14777 }
14778
14779 htab_delete (str_table);
14780 htab_delete (index_table);
14781}
14782
14783/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14784 from PST; CU_INDEX is the index of the CU in the vector of all
14785 CUs. */
14786static void
14787add_address_entry (struct objfile *objfile,
14788 struct obstack *addr_obstack, struct partial_symtab *pst,
14789 unsigned int cu_index)
14790{
14791 offset_type offset;
14792 char addr[8];
14793 CORE_ADDR baseaddr;
14794
1fd400ff
TT
14795 /* Don't bother recording empty ranges. */
14796 if (pst->textlow == pst->texthigh)
14797 return;
14798
9291a0cd
TT
14799 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14800
14801 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14802 obstack_grow (addr_obstack, addr, 8);
14803 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14804 obstack_grow (addr_obstack, addr, 8);
14805 offset = MAYBE_SWAP (cu_index);
14806 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14807}
14808
14809/* Add a list of partial symbols to SYMTAB. */
14810static void
14811write_psymbols (struct mapped_symtab *symtab,
14812 struct partial_symbol **psymp,
14813 int count,
14814 offset_type cu_index)
14815{
14816 for (; count-- > 0; ++psymp)
14817 {
14818 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14819 error (_("Ada is not currently supported by the index"));
14820 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14821 }
14822}
14823
14824/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14825 exception if there is an error. */
14826static void
14827write_obstack (FILE *file, struct obstack *obstack)
14828{
14829 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14830 file)
14831 != obstack_object_size (obstack))
14832 error (_("couldn't data write to file"));
14833}
14834
14835/* Unlink a file if the argument is not NULL. */
14836static void
14837unlink_if_set (void *p)
14838{
14839 char **filename = p;
14840 if (*filename)
14841 unlink (*filename);
14842}
14843
1fd400ff
TT
14844/* A helper struct used when iterating over debug_types. */
14845struct signatured_type_index_data
14846{
14847 struct objfile *objfile;
14848 struct mapped_symtab *symtab;
14849 struct obstack *types_list;
14850 int cu_index;
14851};
14852
14853/* A helper function that writes a single signatured_type to an
14854 obstack. */
14855static int
14856write_one_signatured_type (void **slot, void *d)
14857{
14858 struct signatured_type_index_data *info = d;
14859 struct signatured_type *entry = (struct signatured_type *) *slot;
14860 struct dwarf2_per_cu_data *cu = &entry->per_cu;
14861 struct partial_symtab *psymtab = cu->v.psymtab;
14862 gdb_byte val[8];
14863
14864 write_psymbols (info->symtab,
14865 info->objfile->global_psymbols.list + psymtab->globals_offset,
14866 psymtab->n_global_syms, info->cu_index);
14867 write_psymbols (info->symtab,
14868 info->objfile->static_psymbols.list + psymtab->statics_offset,
14869 psymtab->n_static_syms, info->cu_index);
14870
14871 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14872 obstack_grow (info->types_list, val, 8);
14873 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14874 obstack_grow (info->types_list, val, 8);
14875 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14876 obstack_grow (info->types_list, val, 8);
14877
14878 ++info->cu_index;
14879
14880 return 1;
14881}
14882
9291a0cd
TT
14883/* Create an index file for OBJFILE in the directory DIR. */
14884static void
14885write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14886{
14887 struct cleanup *cleanup;
14888 char *filename, *cleanup_filename;
1fd400ff
TT
14889 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14890 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14891 int i;
14892 FILE *out_file;
14893 struct mapped_symtab *symtab;
14894 offset_type val, size_of_contents, total_len;
14895 struct stat st;
14896 char buf[8];
14897
14898 if (!objfile->psymtabs)
14899 return;
14900 if (dwarf2_per_objfile->using_index)
14901 error (_("Cannot use an index to create the index"));
14902
14903 if (stat (objfile->name, &st) < 0)
14904 perror_with_name (_("Could not stat"));
14905
14906 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14907 INDEX_SUFFIX, (char *) NULL);
14908 cleanup = make_cleanup (xfree, filename);
14909
14910 out_file = fopen (filename, "wb");
14911 if (!out_file)
14912 error (_("Can't open `%s' for writing"), filename);
14913
14914 cleanup_filename = filename;
14915 make_cleanup (unlink_if_set, &cleanup_filename);
14916
14917 symtab = create_mapped_symtab ();
14918 make_cleanup (cleanup_mapped_symtab, symtab);
14919
14920 obstack_init (&addr_obstack);
14921 make_cleanup_obstack_free (&addr_obstack);
14922
14923 obstack_init (&cu_list);
14924 make_cleanup_obstack_free (&cu_list);
14925
1fd400ff
TT
14926 obstack_init (&types_cu_list);
14927 make_cleanup_obstack_free (&types_cu_list);
14928
14929 /* The list is already sorted, so we don't need to do additional
14930 work here. Also, the debug_types entries do not appear in
14931 all_comp_units, but only in their own hash table. */
9291a0cd
TT
14932 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14933 {
14934 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14935 struct partial_symtab *psymtab = cu->v.psymtab;
14936 gdb_byte val[8];
14937
14938 write_psymbols (symtab,
14939 objfile->global_psymbols.list + psymtab->globals_offset,
14940 psymtab->n_global_syms, i);
14941 write_psymbols (symtab,
14942 objfile->static_psymbols.list + psymtab->statics_offset,
14943 psymtab->n_static_syms, i);
14944
14945 add_address_entry (objfile, &addr_obstack, psymtab, i);
14946
14947 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14948 obstack_grow (&cu_list, val, 8);
14949 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14950 obstack_grow (&cu_list, val, 8);
14951 }
14952
1fd400ff
TT
14953 /* Write out the .debug_type entries, if any. */
14954 if (dwarf2_per_objfile->signatured_types)
14955 {
14956 struct signatured_type_index_data sig_data;
14957
14958 sig_data.objfile = objfile;
14959 sig_data.symtab = symtab;
14960 sig_data.types_list = &types_cu_list;
14961 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14962 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14963 write_one_signatured_type, &sig_data);
14964 }
14965
9291a0cd
TT
14966 obstack_init (&constant_pool);
14967 make_cleanup_obstack_free (&constant_pool);
14968 obstack_init (&symtab_obstack);
14969 make_cleanup_obstack_free (&symtab_obstack);
14970 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14971
14972 obstack_init (&contents);
14973 make_cleanup_obstack_free (&contents);
1fd400ff 14974 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
14975 total_len = size_of_contents;
14976
14977 /* The version number. */
1fd400ff 14978 val = MAYBE_SWAP (2);
9291a0cd
TT
14979 obstack_grow (&contents, &val, sizeof (val));
14980
14981 /* The offset of the CU list from the start of the file. */
14982 val = MAYBE_SWAP (total_len);
14983 obstack_grow (&contents, &val, sizeof (val));
14984 total_len += obstack_object_size (&cu_list);
14985
1fd400ff
TT
14986 /* The offset of the types CU list from the start of the file. */
14987 val = MAYBE_SWAP (total_len);
14988 obstack_grow (&contents, &val, sizeof (val));
14989 total_len += obstack_object_size (&types_cu_list);
14990
9291a0cd
TT
14991 /* The offset of the address table from the start of the file. */
14992 val = MAYBE_SWAP (total_len);
14993 obstack_grow (&contents, &val, sizeof (val));
14994 total_len += obstack_object_size (&addr_obstack);
14995
14996 /* The offset of the symbol table from the start of the file. */
14997 val = MAYBE_SWAP (total_len);
14998 obstack_grow (&contents, &val, sizeof (val));
14999 total_len += obstack_object_size (&symtab_obstack);
15000
15001 /* The offset of the constant pool from the start of the file. */
15002 val = MAYBE_SWAP (total_len);
15003 obstack_grow (&contents, &val, sizeof (val));
15004 total_len += obstack_object_size (&constant_pool);
15005
15006 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15007
15008 write_obstack (out_file, &contents);
15009 write_obstack (out_file, &cu_list);
1fd400ff 15010 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15011 write_obstack (out_file, &addr_obstack);
15012 write_obstack (out_file, &symtab_obstack);
15013 write_obstack (out_file, &constant_pool);
15014
15015 fclose (out_file);
15016
15017 /* We want to keep the file, so we set cleanup_filename to NULL
15018 here. See unlink_if_set. */
15019 cleanup_filename = NULL;
15020
15021 do_cleanups (cleanup);
15022}
15023
15024/* The mapped index file format is designed to be directly mmap()able
15025 on any architecture. In most cases, a datum is represented using a
15026 little-endian 32-bit integer value, called an offset_type. Big
15027 endian machines must byte-swap the values before using them.
15028 Exceptions to this rule are noted. The data is laid out such that
15029 alignment is always respected.
15030
15031 A mapped index consists of several sections.
15032
15033 1. The file header. This is a sequence of values, of offset_type
15034 unless otherwise noted:
1fd400ff
TT
15035 [0] The version number. Currently 1 or 2. The differences are
15036 noted below. Version 1 did not account for .debug_types sections;
15037 the presence of a .debug_types section invalidates any version 1
15038 index that may exist.
9291a0cd 15039 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
15040 [1.5] In version 2, the offset, from the start of the file, of the
15041 types CU list. This offset does not appear in version 1. Note
15042 that this can be empty, in which case this offset will be equal to
15043 the next offset.
9291a0cd
TT
15044 [2] The offset, from the start of the file, of the address section.
15045 [3] The offset, from the start of the file, of the symbol table.
15046 [4] The offset, from the start of the file, of the constant pool.
15047
15048 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15049 little-endian values, sorted by the CU offset. The first element
15050 in each pair is the offset of a CU in the .debug_info section. The
15051 second element in each pair is the length of that CU. References
15052 to a CU elsewhere in the map are done using a CU index, which is
15053 just the 0-based index into this table. Note that if there are
15054 type CUs, then conceptually CUs and type CUs form a single list for
15055 the purposes of CU indices.
15056
15057 2.5 The types CU list. This does not appear in a version 1 index.
15058 This is a sequence of triplets of 64-bit little-endian values. In
15059 a triplet, the first value is the CU offset, the second value is
15060 the type offset in the CU, and the third value is the type
15061 signature. The types CU list is not sorted.
9291a0cd
TT
15062
15063 3. The address section. The address section consists of a sequence
15064 of address entries. Each address entry has three elements.
15065 [0] The low address. This is a 64-bit little-endian value.
15066 [1] The high address. This is a 64-bit little-endian value.
15067 [2] The CU index. This is an offset_type value.
15068
15069 4. The symbol table. This is a hash table. The size of the hash
15070 table is always a power of 2. The initial hash and the step are
15071 currently defined by the `find_slot' function.
15072
15073 Each slot in the hash table consists of a pair of offset_type
15074 values. The first value is the offset of the symbol's name in the
15075 constant pool. The second value is the offset of the CU vector in
15076 the constant pool.
15077
15078 If both values are 0, then this slot in the hash table is empty.
15079 This is ok because while 0 is a valid constant pool index, it
15080 cannot be a valid index for both a string and a CU vector.
15081
15082 A string in the constant pool is stored as a \0-terminated string,
15083 as you'd expect.
15084
15085 A CU vector in the constant pool is a sequence of offset_type
15086 values. The first value is the number of CU indices in the vector.
15087 Each subsequent value is the index of a CU in the CU list. This
15088 element in the hash table is used to indicate which CUs define the
15089 symbol.
15090
15091 5. The constant pool. This is simply a bunch of bytes. It is
15092 organized so that alignment is correct: CU vectors are stored
15093 first, followed by strings. */
15094static void
15095save_gdb_index_command (char *arg, int from_tty)
15096{
15097 struct objfile *objfile;
15098
15099 if (!arg || !*arg)
96d19272 15100 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15101
15102 ALL_OBJFILES (objfile)
15103 {
15104 struct stat st;
15105
15106 /* If the objfile does not correspond to an actual file, skip it. */
15107 if (stat (objfile->name, &st) < 0)
15108 continue;
15109
15110 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15111 if (dwarf2_per_objfile)
15112 {
15113 volatile struct gdb_exception except;
15114
15115 TRY_CATCH (except, RETURN_MASK_ERROR)
15116 {
15117 write_psymtabs_to_index (objfile, arg);
15118 }
15119 if (except.reason < 0)
15120 exception_fprintf (gdb_stderr, except,
15121 _("Error while writing index for `%s': "),
15122 objfile->name);
15123 }
15124 }
dce234bc
PP
15125}
15126
9291a0cd
TT
15127\f
15128
9eae7c52
TT
15129int dwarf2_always_disassemble;
15130
15131static void
15132show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15133 struct cmd_list_element *c, const char *value)
15134{
15135 fprintf_filtered (file, _("\
15136Whether to always disassemble DWARF expressions is %s.\n"),
15137 value);
15138}
15139
6502dd73
DJ
15140void _initialize_dwarf2_read (void);
15141
15142void
15143_initialize_dwarf2_read (void)
15144{
96d19272
JK
15145 struct cmd_list_element *c;
15146
dce234bc 15147 dwarf2_objfile_data_key
c1bd65d0 15148 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15149
1bedd215
AC
15150 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15151Set DWARF 2 specific variables.\n\
15152Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15153 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15154 0/*allow-unknown*/, &maintenance_set_cmdlist);
15155
1bedd215
AC
15156 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15157Show DWARF 2 specific variables\n\
15158Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15159 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15160 0/*allow-unknown*/, &maintenance_show_cmdlist);
15161
15162 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15163 &dwarf2_max_cache_age, _("\
15164Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15165Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15166A higher limit means that cached compilation units will be stored\n\
15167in memory longer, and more total memory will be used. Zero disables\n\
15168caching, which can slow down startup."),
2c5b56ce 15169 NULL,
920d2a44 15170 show_dwarf2_max_cache_age,
2c5b56ce 15171 &set_dwarf2_cmdlist,
ae038cb0 15172 &show_dwarf2_cmdlist);
d97bc12b 15173
9eae7c52
TT
15174 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15175 &dwarf2_always_disassemble, _("\
15176Set whether `info address' always disassembles DWARF expressions."), _("\
15177Show whether `info address' always disassembles DWARF expressions."), _("\
15178When enabled, DWARF expressions are always printed in an assembly-like\n\
15179syntax. When disabled, expressions will be printed in a more\n\
15180conversational style, when possible."),
15181 NULL,
15182 show_dwarf2_always_disassemble,
15183 &set_dwarf2_cmdlist,
15184 &show_dwarf2_cmdlist);
15185
d97bc12b
DE
15186 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15187Set debugging of the dwarf2 DIE reader."), _("\
15188Show debugging of the dwarf2 DIE reader."), _("\
15189When enabled (non-zero), DIEs are dumped after they are read in.\n\
15190The value is the maximum depth to print."),
15191 NULL,
15192 NULL,
15193 &setdebuglist, &showdebuglist);
9291a0cd 15194
96d19272
JK
15195 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15196 _("Save a .gdb-index file"),
15197 &save_cmdlist);
15198 set_cmd_completer (c, filename_completer);
6502dd73 15199}
This page took 2.623021 seconds and 4 git commands to generate.