* archive.c (_bfd_archive_bsd44_construct_extended_name_table):
[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"
4c2df51b 56
c906108c
SS
57#include <fcntl.h>
58#include "gdb_string.h"
4bdf3d34 59#include "gdb_assert.h"
c906108c 60#include <sys/types.h>
233a11ab
CS
61#ifdef HAVE_ZLIB_H
62#include <zlib.h>
63#endif
dce234bc
PP
64#ifdef HAVE_MMAP
65#include <sys/mman.h>
85d9bd0e
TT
66#ifndef MAP_FAILED
67#define MAP_FAILED ((void *) -1)
68#endif
dce234bc 69#endif
d8151005 70
107d2387 71#if 0
357e46e7 72/* .debug_info header for a compilation unit
c906108c
SS
73 Because of alignment constraints, this structure has padding and cannot
74 be mapped directly onto the beginning of the .debug_info section. */
75typedef struct comp_unit_header
76 {
77 unsigned int length; /* length of the .debug_info
78 contribution */
79 unsigned short version; /* version number -- 2 for DWARF
80 version 2 */
81 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
82 unsigned char addr_size; /* byte size of an address -- 4 */
83 }
84_COMP_UNIT_HEADER;
85#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 86#endif
c906108c 87
c906108c
SS
88/* .debug_line statement program prologue
89 Because of alignment constraints, this structure has padding and cannot
90 be mapped directly onto the beginning of the .debug_info section. */
91typedef struct statement_prologue
92 {
93 unsigned int total_length; /* byte length of the statement
94 information */
95 unsigned short version; /* version number -- 2 for DWARF
96 version 2 */
97 unsigned int prologue_length; /* # bytes between prologue &
98 stmt program */
99 unsigned char minimum_instruction_length; /* byte size of
100 smallest instr */
101 unsigned char default_is_stmt; /* initial value of is_stmt
102 register */
103 char line_base;
104 unsigned char line_range;
105 unsigned char opcode_base; /* number assigned to first special
106 opcode */
107 unsigned char *standard_opcode_lengths;
108 }
109_STATEMENT_PROLOGUE;
110
d97bc12b
DE
111/* When non-zero, dump DIEs after they are read in. */
112static int dwarf2_die_debug = 0;
113
dce234bc
PP
114static int pagesize;
115
df8a16a1
DJ
116/* When set, the file that we're processing is known to have debugging
117 info for C++ namespaces. GCC 3.3.x did not produce this information,
118 but later versions do. */
119
120static int processing_has_namespace_info;
121
6502dd73
DJ
122static const struct objfile_data *dwarf2_objfile_data_key;
123
dce234bc
PP
124struct dwarf2_section_info
125{
126 asection *asection;
127 gdb_byte *buffer;
128 bfd_size_type size;
129 int was_mmapped;
be391dca
TT
130 /* True if we have tried to read this section. */
131 int readin;
dce234bc
PP
132};
133
9291a0cd
TT
134/* All offsets in the index are of this type. It must be
135 architecture-independent. */
136typedef uint32_t offset_type;
137
138DEF_VEC_I (offset_type);
139
140/* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142struct mapped_index
143{
144 /* The total length of the buffer. */
145 off_t total_size;
146 /* A pointer to the address table data. */
147 const gdb_byte *address_table;
148 /* Size of the address table data in bytes. */
149 offset_type address_table_size;
150 /* The hash table. */
151 const offset_type *index_table;
152 /* Size in slots, each slot is 2 offset_types. */
153 offset_type index_table_slots;
154 /* A pointer to the constant pool. */
155 const char *constant_pool;
156};
157
158typedef struct dwarf2_per_cu_data *dwarf2_per_cu_data_ptr;
159DEF_VEC_P (dwarf2_per_cu_data_ptr);
160
6502dd73
DJ
161struct dwarf2_per_objfile
162{
dce234bc
PP
163 struct dwarf2_section_info info;
164 struct dwarf2_section_info abbrev;
165 struct dwarf2_section_info line;
dce234bc
PP
166 struct dwarf2_section_info loc;
167 struct dwarf2_section_info macinfo;
168 struct dwarf2_section_info str;
169 struct dwarf2_section_info ranges;
348e048f 170 struct dwarf2_section_info types;
dce234bc
PP
171 struct dwarf2_section_info frame;
172 struct dwarf2_section_info eh_frame;
9291a0cd 173 struct dwarf2_section_info gdb_index;
ae038cb0 174
be391dca
TT
175 /* Back link. */
176 struct objfile *objfile;
177
10b3939b
DJ
178 /* A list of all the compilation units. This is used to locate
179 the target compilation unit of a particular reference. */
ae038cb0
DJ
180 struct dwarf2_per_cu_data **all_comp_units;
181
182 /* The number of compilation units in ALL_COMP_UNITS. */
183 int n_comp_units;
184
185 /* A chain of compilation units that are currently read in, so that
186 they can be freed later. */
187 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 188
348e048f
DE
189 /* A table mapping .debug_types signatures to its signatured_type entry.
190 This is NULL if the .debug_types section hasn't been read in yet. */
191 htab_t signatured_types;
192
72dca2f5
FR
193 /* A flag indicating wether this objfile has a section loaded at a
194 VMA of 0. */
195 int has_section_at_zero;
9291a0cd
TT
196
197 /* True if we are using the mapped index. */
198 unsigned char using_index;
199
200 /* The mapped index. */
201 struct mapped_index *index_table;
6502dd73
DJ
202};
203
204static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
205
206/* names of the debugging sections */
207
233a11ab
CS
208/* Note that if the debugging section has been compressed, it might
209 have a name like .zdebug_info. */
210
211#define INFO_SECTION "debug_info"
212#define ABBREV_SECTION "debug_abbrev"
213#define LINE_SECTION "debug_line"
233a11ab
CS
214#define LOC_SECTION "debug_loc"
215#define MACINFO_SECTION "debug_macinfo"
216#define STR_SECTION "debug_str"
217#define RANGES_SECTION "debug_ranges"
348e048f 218#define TYPES_SECTION "debug_types"
233a11ab
CS
219#define FRAME_SECTION "debug_frame"
220#define EH_FRAME_SECTION "eh_frame"
9291a0cd 221#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
222
223/* local data types */
224
57349743
JB
225/* We hold several abbreviation tables in memory at the same time. */
226#ifndef ABBREV_HASH_SIZE
227#define ABBREV_HASH_SIZE 121
228#endif
229
107d2387
AC
230/* The data in a compilation unit header, after target2host
231 translation, looks like this. */
c906108c 232struct comp_unit_head
a738430d 233{
c764a876 234 unsigned int length;
a738430d 235 short version;
a738430d
MK
236 unsigned char addr_size;
237 unsigned char signed_addr_p;
9cbfa09e 238 unsigned int abbrev_offset;
57349743 239
a738430d
MK
240 /* Size of file offsets; either 4 or 8. */
241 unsigned int offset_size;
57349743 242
a738430d
MK
243 /* Size of the length field; either 4 or 12. */
244 unsigned int initial_length_size;
57349743 245
a738430d
MK
246 /* Offset to the first byte of this compilation unit header in the
247 .debug_info section, for resolving relative reference dies. */
248 unsigned int offset;
57349743 249
d00adf39
DE
250 /* Offset to first die in this cu from the start of the cu.
251 This will be the first byte following the compilation unit header. */
252 unsigned int first_die_offset;
a738430d 253};
c906108c 254
e7c27a73
DJ
255/* Internal state when decoding a particular compilation unit. */
256struct dwarf2_cu
257{
258 /* The objfile containing this compilation unit. */
259 struct objfile *objfile;
260
d00adf39 261 /* The header of the compilation unit. */
e7c27a73 262 struct comp_unit_head header;
e142c38c 263
d00adf39
DE
264 /* Base address of this compilation unit. */
265 CORE_ADDR base_address;
266
267 /* Non-zero if base_address has been set. */
268 int base_known;
269
e142c38c
DJ
270 struct function_range *first_fn, *last_fn, *cached_fn;
271
272 /* The language we are debugging. */
273 enum language language;
274 const struct language_defn *language_defn;
275
b0f35d58
DL
276 const char *producer;
277
e142c38c
DJ
278 /* The generic symbol table building routines have separate lists for
279 file scope symbols and all all other scopes (local scopes). So
280 we need to select the right one to pass to add_symbol_to_list().
281 We do it by keeping a pointer to the correct list in list_in_scope.
282
283 FIXME: The original dwarf code just treated the file scope as the
284 first local scope, and all other local scopes as nested local
285 scopes, and worked fine. Check to see if we really need to
286 distinguish these in buildsym.c. */
287 struct pending **list_in_scope;
288
f3dd6933
DJ
289 /* DWARF abbreviation table associated with this compilation unit. */
290 struct abbrev_info **dwarf2_abbrevs;
291
292 /* Storage for the abbrev table. */
293 struct obstack abbrev_obstack;
72bf9492
DJ
294
295 /* Hash table holding all the loaded partial DIEs. */
296 htab_t partial_dies;
297
298 /* Storage for things with the same lifetime as this read-in compilation
299 unit, including partial DIEs. */
300 struct obstack comp_unit_obstack;
301
ae038cb0
DJ
302 /* When multiple dwarf2_cu structures are living in memory, this field
303 chains them all together, so that they can be released efficiently.
304 We will probably also want a generation counter so that most-recently-used
305 compilation units are cached... */
306 struct dwarf2_per_cu_data *read_in_chain;
307
308 /* Backchain to our per_cu entry if the tree has been built. */
309 struct dwarf2_per_cu_data *per_cu;
310
f792889a
DJ
311 /* Pointer to the die -> type map. Although it is stored
312 permanently in per_cu, we copy it here to avoid double
313 indirection. */
314 htab_t type_hash;
315
ae038cb0
DJ
316 /* How many compilation units ago was this CU last referenced? */
317 int last_used;
318
10b3939b 319 /* A hash table of die offsets for following references. */
51545339 320 htab_t die_hash;
10b3939b
DJ
321
322 /* Full DIEs if read in. */
323 struct die_info *dies;
324
325 /* A set of pointers to dwarf2_per_cu_data objects for compilation
326 units referenced by this one. Only set during full symbol processing;
327 partial symbol tables do not have dependencies. */
328 htab_t dependencies;
329
cb1df416
DJ
330 /* Header data from the line table, during full symbol processing. */
331 struct line_header *line_header;
332
ae038cb0
DJ
333 /* Mark used when releasing cached dies. */
334 unsigned int mark : 1;
335
336 /* This flag will be set if this compilation unit might include
337 inter-compilation-unit references. */
338 unsigned int has_form_ref_addr : 1;
339
72bf9492
DJ
340 /* This flag will be set if this compilation unit includes any
341 DW_TAG_namespace DIEs. If we know that there are explicit
342 DIEs for namespaces, we don't need to try to infer them
343 from mangled names. */
344 unsigned int has_namespace_info : 1;
e7c27a73
DJ
345};
346
9291a0cd
TT
347/* When using the index (and thus not using psymtabs), each CU has an
348 object of this type. This is used to hold information needed by
349 the various "quick" methods. */
350struct dwarf2_per_cu_quick_data
351{
352 /* The line table. This can be NULL if there was no line table. */
353 struct line_header *lines;
354
355 /* The file names from the line table. */
356 const char **file_names;
357 /* The file names from the line table after being run through
358 gdb_realpath. */
359 const char **full_names;
360
361 /* The corresponding symbol table. This is NULL if symbols for this
362 CU have not yet been read. */
363 struct symtab *symtab;
364
365 /* A temporary mark bit used when iterating over all CUs in
366 expand_symtabs_matching. */
367 unsigned int mark : 1;
368
369 /* True if we've tried to read the line table. */
370 unsigned int read_lines : 1;
371};
372
10b3939b
DJ
373/* Persistent data held for a compilation unit, even when not
374 processing it. We put a pointer to this structure in the
375 read_symtab_private field of the psymtab. If we encounter
376 inter-compilation-unit references, we also maintain a sorted
377 list of all compilation units. */
378
ae038cb0
DJ
379struct dwarf2_per_cu_data
380{
348e048f 381 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 382 bytes should suffice to store the length of any compilation unit
45452591
DE
383 - if it doesn't, GDB will fall over anyway.
384 NOTE: Unlike comp_unit_head.length, this length includes
385 initial_length_size. */
c764a876 386 unsigned int offset;
348e048f 387 unsigned int length : 29;
ae038cb0
DJ
388
389 /* Flag indicating this compilation unit will be read in before
390 any of the current compilation units are processed. */
c764a876 391 unsigned int queued : 1;
ae038cb0 392
5afb4e99
DJ
393 /* This flag will be set if we need to load absolutely all DIEs
394 for this compilation unit, instead of just the ones we think
395 are interesting. It gets set if we look for a DIE in the
396 hash table and don't find it. */
397 unsigned int load_all_dies : 1;
398
348e048f
DE
399 /* Non-zero if this CU is from .debug_types.
400 Otherwise it's from .debug_info. */
401 unsigned int from_debug_types : 1;
402
17ea53c3
JK
403 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
404 of the CU cache it gets reset to NULL again. */
ae038cb0 405 struct dwarf2_cu *cu;
1c379e20
DJ
406
407 /* If full symbols for this CU have been read in, then this field
408 holds a map of DIE offsets to types. It isn't always possible
409 to reconstruct this information later, so we have to preserve
410 it. */
1c379e20 411 htab_t type_hash;
10b3939b 412
9291a0cd
TT
413 /* The corresponding objfile. */
414 struct objfile *objfile;
415
416 /* When using partial symbol tables, the 'psymtab' field is active.
417 Otherwise the 'quick' field is active. */
418 union
419 {
420 /* The partial symbol table associated with this compilation unit,
421 or NULL for partial units (which do not have an associated
422 symtab). */
423 struct partial_symtab *psymtab;
424
425 /* Data needed by the "quick" functions. */
426 struct dwarf2_per_cu_quick_data *quick;
427 } v;
ae038cb0
DJ
428};
429
348e048f
DE
430/* Entry in the signatured_types hash table. */
431
432struct signatured_type
433{
434 ULONGEST signature;
435
436 /* Offset in .debug_types of the TU (type_unit) for this type. */
437 unsigned int offset;
438
439 /* Offset in .debug_types of the type defined by this TU. */
440 unsigned int type_offset;
441
442 /* The CU(/TU) of this type. */
443 struct dwarf2_per_cu_data per_cu;
444};
445
93311388
DE
446/* Struct used to pass misc. parameters to read_die_and_children, et. al.
447 which are used for both .debug_info and .debug_types dies.
448 All parameters here are unchanging for the life of the call.
449 This struct exists to abstract away the constant parameters of
450 die reading. */
451
452struct die_reader_specs
453{
454 /* The bfd of this objfile. */
455 bfd* abfd;
456
457 /* The CU of the DIE we are parsing. */
458 struct dwarf2_cu *cu;
459
460 /* Pointer to start of section buffer.
461 This is either the start of .debug_info or .debug_types. */
462 const gdb_byte *buffer;
463};
464
debd256d
JB
465/* The line number information for a compilation unit (found in the
466 .debug_line section) begins with a "statement program header",
467 which contains the following information. */
468struct line_header
469{
470 unsigned int total_length;
471 unsigned short version;
472 unsigned int header_length;
473 unsigned char minimum_instruction_length;
2dc7f7b3 474 unsigned char maximum_ops_per_instruction;
debd256d
JB
475 unsigned char default_is_stmt;
476 int line_base;
477 unsigned char line_range;
478 unsigned char opcode_base;
479
480 /* standard_opcode_lengths[i] is the number of operands for the
481 standard opcode whose value is i. This means that
482 standard_opcode_lengths[0] is unused, and the last meaningful
483 element is standard_opcode_lengths[opcode_base - 1]. */
484 unsigned char *standard_opcode_lengths;
485
486 /* The include_directories table. NOTE! These strings are not
487 allocated with xmalloc; instead, they are pointers into
488 debug_line_buffer. If you try to free them, `free' will get
489 indigestion. */
490 unsigned int num_include_dirs, include_dirs_size;
491 char **include_dirs;
492
493 /* The file_names table. NOTE! These strings are not allocated
494 with xmalloc; instead, they are pointers into debug_line_buffer.
495 Don't try to free them directly. */
496 unsigned int num_file_names, file_names_size;
497 struct file_entry
c906108c 498 {
debd256d
JB
499 char *name;
500 unsigned int dir_index;
501 unsigned int mod_time;
502 unsigned int length;
aaa75496 503 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 504 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
505 } *file_names;
506
507 /* The start and end of the statement program following this
6502dd73 508 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 509 gdb_byte *statement_program_start, *statement_program_end;
debd256d 510};
c906108c
SS
511
512/* When we construct a partial symbol table entry we only
513 need this much information. */
514struct partial_die_info
515 {
72bf9492 516 /* Offset of this DIE. */
c906108c 517 unsigned int offset;
72bf9492
DJ
518
519 /* DWARF-2 tag for this DIE. */
520 ENUM_BITFIELD(dwarf_tag) tag : 16;
521
72bf9492
DJ
522 /* Assorted flags describing the data found in this DIE. */
523 unsigned int has_children : 1;
524 unsigned int is_external : 1;
525 unsigned int is_declaration : 1;
526 unsigned int has_type : 1;
527 unsigned int has_specification : 1;
528 unsigned int has_pc_info : 1;
529
530 /* Flag set if the SCOPE field of this structure has been
531 computed. */
532 unsigned int scope_set : 1;
533
fa4028e9
JB
534 /* Flag set if the DIE has a byte_size attribute. */
535 unsigned int has_byte_size : 1;
536
72bf9492 537 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 538 sometimes a default name for unnamed DIEs. */
c906108c 539 char *name;
72bf9492
DJ
540
541 /* The scope to prepend to our children. This is generally
542 allocated on the comp_unit_obstack, so will disappear
543 when this compilation unit leaves the cache. */
544 char *scope;
545
546 /* The location description associated with this DIE, if any. */
547 struct dwarf_block *locdesc;
548
549 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
550 CORE_ADDR lowpc;
551 CORE_ADDR highpc;
72bf9492 552
93311388 553 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 554 DW_AT_sibling, if any. */
fe1b8b76 555 gdb_byte *sibling;
72bf9492
DJ
556
557 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
558 DW_AT_specification (or DW_AT_abstract_origin or
559 DW_AT_extension). */
560 unsigned int spec_offset;
561
562 /* Pointers to this DIE's parent, first child, and next sibling,
563 if any. */
564 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
565 };
566
567/* This data structure holds the information of an abbrev. */
568struct abbrev_info
569 {
570 unsigned int number; /* number identifying abbrev */
571 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
572 unsigned short has_children; /* boolean */
573 unsigned short num_attrs; /* number of attributes */
c906108c
SS
574 struct attr_abbrev *attrs; /* an array of attribute descriptions */
575 struct abbrev_info *next; /* next in chain */
576 };
577
578struct attr_abbrev
579 {
9d25dd43
DE
580 ENUM_BITFIELD(dwarf_attribute) name : 16;
581 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
582 };
583
b60c80d6
DJ
584/* Attributes have a name and a value */
585struct attribute
586 {
9d25dd43 587 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
588 ENUM_BITFIELD(dwarf_form) form : 15;
589
590 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
591 field should be in u.str (existing only for DW_STRING) but it is kept
592 here for better struct attribute alignment. */
593 unsigned int string_is_canonical : 1;
594
b60c80d6
DJ
595 union
596 {
597 char *str;
598 struct dwarf_block *blk;
43bbcdc2
PH
599 ULONGEST unsnd;
600 LONGEST snd;
b60c80d6 601 CORE_ADDR addr;
348e048f 602 struct signatured_type *signatured_type;
b60c80d6
DJ
603 }
604 u;
605 };
606
c906108c
SS
607/* This data structure holds a complete die structure. */
608struct die_info
609 {
76815b17
DE
610 /* DWARF-2 tag for this DIE. */
611 ENUM_BITFIELD(dwarf_tag) tag : 16;
612
613 /* Number of attributes */
614 unsigned short num_attrs;
615
616 /* Abbrev number */
617 unsigned int abbrev;
618
93311388 619 /* Offset in .debug_info or .debug_types section. */
76815b17 620 unsigned int offset;
78ba4af6
JB
621
622 /* The dies in a compilation unit form an n-ary tree. PARENT
623 points to this die's parent; CHILD points to the first child of
624 this node; and all the children of a given node are chained
625 together via their SIBLING fields, terminated by a die whose
626 tag is zero. */
639d11d3
DC
627 struct die_info *child; /* Its first child, if any. */
628 struct die_info *sibling; /* Its next sibling, if any. */
629 struct die_info *parent; /* Its parent, if any. */
c906108c 630
b60c80d6
DJ
631 /* An array of attributes, with NUM_ATTRS elements. There may be
632 zero, but it's not common and zero-sized arrays are not
633 sufficiently portable C. */
634 struct attribute attrs[1];
c906108c
SS
635 };
636
5fb290d7
DJ
637struct function_range
638{
639 const char *name;
640 CORE_ADDR lowpc, highpc;
641 int seen_line;
642 struct function_range *next;
643};
644
c906108c
SS
645/* Get at parts of an attribute structure */
646
647#define DW_STRING(attr) ((attr)->u.str)
8285870a 648#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
649#define DW_UNSND(attr) ((attr)->u.unsnd)
650#define DW_BLOCK(attr) ((attr)->u.blk)
651#define DW_SND(attr) ((attr)->u.snd)
652#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 653#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
654
655/* Blocks are a bunch of untyped bytes. */
656struct dwarf_block
657 {
658 unsigned int size;
fe1b8b76 659 gdb_byte *data;
c906108c
SS
660 };
661
c906108c
SS
662#ifndef ATTR_ALLOC_CHUNK
663#define ATTR_ALLOC_CHUNK 4
664#endif
665
c906108c
SS
666/* Allocate fields for structs, unions and enums in this size. */
667#ifndef DW_FIELD_ALLOC_CHUNK
668#define DW_FIELD_ALLOC_CHUNK 4
669#endif
670
c906108c
SS
671/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
672 but this would require a corresponding change in unpack_field_as_long
673 and friends. */
674static int bits_per_byte = 8;
675
676/* The routines that read and process dies for a C struct or C++ class
677 pass lists of data member fields and lists of member function fields
678 in an instance of a field_info structure, as defined below. */
679struct field_info
c5aa993b
JM
680 {
681 /* List of data member and baseclasses fields. */
682 struct nextfield
683 {
684 struct nextfield *next;
685 int accessibility;
686 int virtuality;
687 struct field field;
688 }
7d0ccb61 689 *fields, *baseclasses;
c906108c 690
7d0ccb61 691 /* Number of fields (including baseclasses). */
c5aa993b 692 int nfields;
c906108c 693
c5aa993b
JM
694 /* Number of baseclasses. */
695 int nbaseclasses;
c906108c 696
c5aa993b
JM
697 /* Set if the accesibility of one of the fields is not public. */
698 int non_public_fields;
c906108c 699
c5aa993b
JM
700 /* Member function fields array, entries are allocated in the order they
701 are encountered in the object file. */
702 struct nextfnfield
703 {
704 struct nextfnfield *next;
705 struct fn_field fnfield;
706 }
707 *fnfields;
c906108c 708
c5aa993b
JM
709 /* Member function fieldlist array, contains name of possibly overloaded
710 member function, number of overloaded member functions and a pointer
711 to the head of the member function field chain. */
712 struct fnfieldlist
713 {
714 char *name;
715 int length;
716 struct nextfnfield *head;
717 }
718 *fnfieldlists;
c906108c 719
c5aa993b
JM
720 /* Number of entries in the fnfieldlists array. */
721 int nfnfields;
98751a41
JK
722
723 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
724 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
725 struct typedef_field_list
726 {
727 struct typedef_field field;
728 struct typedef_field_list *next;
729 }
730 *typedef_field_list;
731 unsigned typedef_field_list_count;
c5aa993b 732 };
c906108c 733
10b3939b
DJ
734/* One item on the queue of compilation units to read in full symbols
735 for. */
736struct dwarf2_queue_item
737{
738 struct dwarf2_per_cu_data *per_cu;
739 struct dwarf2_queue_item *next;
740};
741
742/* The current queue. */
743static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
744
ae038cb0
DJ
745/* Loaded secondary compilation units are kept in memory until they
746 have not been referenced for the processing of this many
747 compilation units. Set this to zero to disable caching. Cache
748 sizes of up to at least twenty will improve startup time for
749 typical inter-CU-reference binaries, at an obvious memory cost. */
750static int dwarf2_max_cache_age = 5;
920d2a44
AC
751static void
752show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
753 struct cmd_list_element *c, const char *value)
754{
755 fprintf_filtered (file, _("\
756The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
757 value);
758}
759
ae038cb0 760
c906108c
SS
761/* Various complaints about symbol reading that don't abort the process */
762
4d3c2250
KB
763static void
764dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 765{
4d3c2250 766 complaint (&symfile_complaints,
e2e0b3e5 767 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
768}
769
25e43795
DJ
770static void
771dwarf2_debug_line_missing_file_complaint (void)
772{
773 complaint (&symfile_complaints,
774 _(".debug_line section has line data without a file"));
775}
776
59205f5a
JB
777static void
778dwarf2_debug_line_missing_end_sequence_complaint (void)
779{
780 complaint (&symfile_complaints,
781 _(".debug_line section has line program sequence without an end"));
782}
783
4d3c2250
KB
784static void
785dwarf2_complex_location_expr_complaint (void)
2e276125 786{
e2e0b3e5 787 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
788}
789
4d3c2250
KB
790static void
791dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
792 int arg3)
2e276125 793{
4d3c2250 794 complaint (&symfile_complaints,
e2e0b3e5 795 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
796 arg2, arg3);
797}
798
799static void
800dwarf2_macros_too_long_complaint (void)
2e276125 801{
4d3c2250 802 complaint (&symfile_complaints,
e2e0b3e5 803 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
804}
805
806static void
807dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 808{
4d3c2250 809 complaint (&symfile_complaints,
e2e0b3e5 810 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
811 arg1);
812}
813
814static void
815dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 816{
4d3c2250 817 complaint (&symfile_complaints,
e2e0b3e5 818 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 819}
c906108c 820
c906108c
SS
821/* local function prototypes */
822
4efb68b1 823static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 824
aaa75496
JB
825static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
826 struct objfile *);
827
828static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
d85a05f0 829 struct die_info *,
aaa75496
JB
830 struct partial_symtab *);
831
c67a9c90 832static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 833
72bf9492
DJ
834static void scan_partial_symbols (struct partial_die_info *,
835 CORE_ADDR *, CORE_ADDR *,
5734ee8b 836 int, struct dwarf2_cu *);
c906108c 837
72bf9492
DJ
838static void add_partial_symbol (struct partial_die_info *,
839 struct dwarf2_cu *);
63d06c5c 840
72bf9492
DJ
841static void add_partial_namespace (struct partial_die_info *pdi,
842 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 843 int need_pc, struct dwarf2_cu *cu);
63d06c5c 844
5d7cb8df
JK
845static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
846 CORE_ADDR *highpc, int need_pc,
847 struct dwarf2_cu *cu);
848
72bf9492
DJ
849static void add_partial_enumeration (struct partial_die_info *enum_pdi,
850 struct dwarf2_cu *cu);
91c24f0a 851
bc30ff58
JB
852static void add_partial_subprogram (struct partial_die_info *pdi,
853 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 854 int need_pc, struct dwarf2_cu *cu);
bc30ff58 855
fe1b8b76 856static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
857 gdb_byte *buffer, gdb_byte *info_ptr,
858 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 859
a14ed312 860static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 861
a14ed312 862static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 863
e7c27a73 864static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 865
f3dd6933 866static void dwarf2_free_abbrev_table (void *);
c906108c 867
fe1b8b76 868static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 869 struct dwarf2_cu *);
72bf9492 870
57349743 871static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 872 struct dwarf2_cu *);
c906108c 873
93311388
DE
874static struct partial_die_info *load_partial_dies (bfd *,
875 gdb_byte *, gdb_byte *,
876 int, struct dwarf2_cu *);
72bf9492 877
fe1b8b76 878static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
879 struct abbrev_info *abbrev,
880 unsigned int, bfd *,
881 gdb_byte *, gdb_byte *,
882 struct dwarf2_cu *);
c906108c 883
c764a876 884static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 885 struct dwarf2_cu *);
72bf9492
DJ
886
887static void fixup_partial_die (struct partial_die_info *,
888 struct dwarf2_cu *);
889
fe1b8b76
JB
890static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
891 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 892
fe1b8b76
JB
893static gdb_byte *read_attribute_value (struct attribute *, unsigned,
894 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 895
fe1b8b76 896static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 897
fe1b8b76 898static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 899
fe1b8b76 900static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 901
fe1b8b76 902static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 903
93311388 904static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 905
fe1b8b76 906static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 907 unsigned int *);
c906108c 908
c764a876
DE
909static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
910
911static LONGEST read_checked_initial_length_and_offset
912 (bfd *, gdb_byte *, const struct comp_unit_head *,
913 unsigned int *, unsigned int *);
613e1657 914
fe1b8b76 915static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
916 unsigned int *);
917
918static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 919
fe1b8b76 920static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 921
fe1b8b76 922static char *read_string (bfd *, gdb_byte *, unsigned int *);
c906108c 923
fe1b8b76
JB
924static char *read_indirect_string (bfd *, gdb_byte *,
925 const struct comp_unit_head *,
926 unsigned int *);
4bdf3d34 927
fe1b8b76 928static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 929
fe1b8b76 930static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 931
fe1b8b76 932static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 933
e142c38c 934static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 935
e142c38c
DJ
936static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
937 struct dwarf2_cu *);
c906108c 938
348e048f
DE
939static struct attribute *dwarf2_attr_no_follow (struct die_info *,
940 unsigned int,
941 struct dwarf2_cu *);
942
05cf31d1
JB
943static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
944 struct dwarf2_cu *cu);
945
e142c38c 946static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 947
e142c38c 948static struct die_info *die_specification (struct die_info *die,
f2f0e013 949 struct dwarf2_cu **);
63d06c5c 950
debd256d
JB
951static void free_line_header (struct line_header *lh);
952
aaa75496
JB
953static void add_file_name (struct line_header *, char *, unsigned int,
954 unsigned int, unsigned int);
955
debd256d
JB
956static struct line_header *(dwarf_decode_line_header
957 (unsigned int offset,
e7c27a73 958 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
959
960static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 961 struct dwarf2_cu *, struct partial_symtab *);
c906108c 962
4f1520fb 963static void dwarf2_start_subfile (char *, char *, char *);
c906108c 964
a14ed312 965static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 966 struct dwarf2_cu *);
c906108c 967
a14ed312 968static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 969 struct dwarf2_cu *);
c906108c 970
2df3850c
JM
971static void dwarf2_const_value_data (struct attribute *attr,
972 struct symbol *sym,
973 int bits);
974
e7c27a73 975static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 976
b4ba55a1
JB
977static int need_gnat_info (struct dwarf2_cu *);
978
979static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
980
981static void set_descriptive_type (struct type *, struct die_info *,
982 struct dwarf2_cu *);
983
e7c27a73
DJ
984static struct type *die_containing_type (struct die_info *,
985 struct dwarf2_cu *);
c906108c 986
e7c27a73 987static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
c906108c 988
f792889a 989static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 990
086ed43d 991static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 992
6e70227d 993static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
994 const char *suffix, int physname,
995 struct dwarf2_cu *cu);
63d06c5c 996
e7c27a73 997static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 998
348e048f
DE
999static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1000
e7c27a73 1001static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1002
e7c27a73 1003static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1004
ff013f42
JK
1005static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1006 struct dwarf2_cu *, struct partial_symtab *);
1007
a14ed312 1008static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1009 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1010 struct partial_symtab *);
c906108c 1011
fae299cd
DC
1012static void get_scope_pc_bounds (struct die_info *,
1013 CORE_ADDR *, CORE_ADDR *,
1014 struct dwarf2_cu *);
1015
801e3a5b
JB
1016static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1017 CORE_ADDR, struct dwarf2_cu *);
1018
a14ed312 1019static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1020 struct dwarf2_cu *);
c906108c 1021
a14ed312 1022static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1023 struct type *, struct dwarf2_cu *);
c906108c 1024
a14ed312 1025static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1026 struct die_info *, struct type *,
e7c27a73 1027 struct dwarf2_cu *);
c906108c 1028
a14ed312 1029static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1030 struct type *, struct dwarf2_cu *);
c906108c 1031
134d01f1 1032static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1033
e7c27a73 1034static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1035
e7c27a73 1036static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1037
5d7cb8df
JK
1038static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1039
27aa8d6a
SW
1040static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1041
f55ee35c
JK
1042static struct type *read_module_type (struct die_info *die,
1043 struct dwarf2_cu *cu);
1044
38d518c9 1045static const char *namespace_name (struct die_info *die,
e142c38c 1046 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1047
134d01f1 1048static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1049
e7c27a73 1050static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1051
6e70227d 1052static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1053 struct dwarf2_cu *);
1054
93311388 1055static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1056
93311388
DE
1057static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1058 gdb_byte *info_ptr,
d97bc12b
DE
1059 gdb_byte **new_info_ptr,
1060 struct die_info *parent);
1061
93311388
DE
1062static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1063 gdb_byte *info_ptr,
fe1b8b76 1064 gdb_byte **new_info_ptr,
639d11d3
DC
1065 struct die_info *parent);
1066
93311388
DE
1067static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1068 gdb_byte *info_ptr,
fe1b8b76 1069 gdb_byte **new_info_ptr,
639d11d3
DC
1070 struct die_info *parent);
1071
93311388
DE
1072static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1073 struct die_info **, gdb_byte *,
1074 int *);
1075
e7c27a73 1076static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1077
71c25dea
TT
1078static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1079 struct obstack *);
1080
e142c38c 1081static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1082
e142c38c 1083static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1084 struct dwarf2_cu **);
9219021c 1085
a14ed312 1086static char *dwarf_tag_name (unsigned int);
c906108c 1087
a14ed312 1088static char *dwarf_attr_name (unsigned int);
c906108c 1089
a14ed312 1090static char *dwarf_form_name (unsigned int);
c906108c 1091
a14ed312 1092static char *dwarf_bool_name (unsigned int);
c906108c 1093
a14ed312 1094static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1095
1096#if 0
a14ed312 1097static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1098#endif
1099
f9aca02d 1100static struct die_info *sibling_die (struct die_info *);
c906108c 1101
d97bc12b
DE
1102static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1103
1104static void dump_die_for_error (struct die_info *);
1105
1106static void dump_die_1 (struct ui_file *, int level, int max_level,
1107 struct die_info *);
c906108c 1108
d97bc12b 1109/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1110
51545339 1111static void store_in_ref_table (struct die_info *,
10b3939b 1112 struct dwarf2_cu *);
c906108c 1113
93311388
DE
1114static int is_ref_attr (struct attribute *);
1115
c764a876 1116static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1117
43bbcdc2 1118static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1119
348e048f
DE
1120static struct die_info *follow_die_ref_or_sig (struct die_info *,
1121 struct attribute *,
1122 struct dwarf2_cu **);
1123
10b3939b
DJ
1124static struct die_info *follow_die_ref (struct die_info *,
1125 struct attribute *,
f2f0e013 1126 struct dwarf2_cu **);
c906108c 1127
348e048f
DE
1128static struct die_info *follow_die_sig (struct die_info *,
1129 struct attribute *,
1130 struct dwarf2_cu **);
1131
1132static void read_signatured_type_at_offset (struct objfile *objfile,
1133 unsigned int offset);
1134
1135static void read_signatured_type (struct objfile *,
1136 struct signatured_type *type_sig);
1137
c906108c
SS
1138/* memory allocation interface */
1139
7b5a2f43 1140static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1141
f3dd6933 1142static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1143
b60c80d6 1144static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1145
e142c38c 1146static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1147
e142c38c
DJ
1148static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1149 struct dwarf2_cu *);
5fb290d7 1150
2e276125 1151static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1152 char *, bfd *, struct dwarf2_cu *);
2e276125 1153
8e19ed76
PS
1154static int attr_form_is_block (struct attribute *);
1155
3690dd37
JB
1156static int attr_form_is_section_offset (struct attribute *);
1157
1158static int attr_form_is_constant (struct attribute *);
1159
93e7bd98
DJ
1160static void dwarf2_symbol_mark_computed (struct attribute *attr,
1161 struct symbol *sym,
1162 struct dwarf2_cu *cu);
4c2df51b 1163
93311388
DE
1164static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1165 struct abbrev_info *abbrev,
1166 struct dwarf2_cu *cu);
4bb7a0a7 1167
72bf9492
DJ
1168static void free_stack_comp_unit (void *);
1169
72bf9492
DJ
1170static hashval_t partial_die_hash (const void *item);
1171
1172static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1173
ae038cb0 1174static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1175 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1176
1177static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1178 (unsigned int offset, struct objfile *objfile);
ae038cb0 1179
93311388
DE
1180static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1181
ae038cb0
DJ
1182static void free_one_comp_unit (void *);
1183
1184static void free_cached_comp_units (void *);
1185
1186static void age_cached_comp_units (void);
1187
1188static void free_one_cached_comp_unit (void *);
1189
f792889a
DJ
1190static struct type *set_die_type (struct die_info *, struct type *,
1191 struct dwarf2_cu *);
1c379e20 1192
ae038cb0
DJ
1193static void create_all_comp_units (struct objfile *);
1194
93311388
DE
1195static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1196 struct objfile *);
10b3939b
DJ
1197
1198static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1199
1200static void dwarf2_add_dependence (struct dwarf2_cu *,
1201 struct dwarf2_per_cu_data *);
1202
ae038cb0
DJ
1203static void dwarf2_mark (struct dwarf2_cu *);
1204
1205static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1206
f792889a 1207static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1208
9291a0cd
TT
1209static void dwarf2_release_queue (void *dummy);
1210
1211static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1212 struct objfile *objfile);
1213
1214static void process_queue (struct objfile *objfile);
1215
1216static void find_file_and_directory (struct die_info *die,
1217 struct dwarf2_cu *cu,
1218 char **name, char **comp_dir);
1219
1220static char *file_full_name (int file, struct line_header *lh,
1221 const char *comp_dir);
1222
1223static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1224 gdb_byte *info_ptr,
1225 gdb_byte *buffer,
1226 unsigned int buffer_size,
1227 bfd *abfd);
1228
1229static void init_cu_die_reader (struct die_reader_specs *reader,
1230 struct dwarf2_cu *cu);
1231
1232#if WORDS_BIGENDIAN
1233
1234/* Convert VALUE between big- and little-endian. */
1235static offset_type
1236byte_swap (offset_type value)
1237{
1238 offset_type result;
1239
1240 result = (value & 0xff) << 24;
1241 result |= (value & 0xff00) << 8;
1242 result |= (value & 0xff0000) >> 8;
1243 result |= (value & 0xff000000) >> 24;
1244 return result;
1245}
1246
1247#define MAYBE_SWAP(V) byte_swap (V)
1248
1249#else
1250#define MAYBE_SWAP(V) (V)
1251#endif /* WORDS_BIGENDIAN */
1252
1253/* The suffix for an index file. */
1254#define INDEX_SUFFIX ".gdb-index"
1255
c906108c
SS
1256/* Try to locate the sections we need for DWARF 2 debugging
1257 information and return true if we have enough to do something. */
1258
1259int
6502dd73 1260dwarf2_has_info (struct objfile *objfile)
c906108c 1261{
be391dca
TT
1262 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1263 if (!dwarf2_per_objfile)
1264 {
1265 /* Initialize per-objfile state. */
1266 struct dwarf2_per_objfile *data
1267 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1268
be391dca
TT
1269 memset (data, 0, sizeof (*data));
1270 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1271 dwarf2_per_objfile = data;
6502dd73 1272
be391dca
TT
1273 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1274 dwarf2_per_objfile->objfile = objfile;
1275 }
1276 return (dwarf2_per_objfile->info.asection != NULL
1277 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1278}
1279
233a11ab
CS
1280/* When loading sections, we can either look for ".<name>", or for
1281 * ".z<name>", which indicates a compressed section. */
1282
1283static int
dce234bc 1284section_is_p (const char *section_name, const char *name)
233a11ab 1285{
dce234bc
PP
1286 return (section_name[0] == '.'
1287 && (strcmp (section_name + 1, name) == 0
1288 || (section_name[1] == 'z'
1289 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1290}
1291
c906108c
SS
1292/* This function is mapped across the sections and remembers the
1293 offset and size of each of the debugging sections we are interested
1294 in. */
1295
1296static void
72dca2f5 1297dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1298{
dce234bc 1299 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1300 {
dce234bc
PP
1301 dwarf2_per_objfile->info.asection = sectp;
1302 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1303 }
dce234bc 1304 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1305 {
dce234bc
PP
1306 dwarf2_per_objfile->abbrev.asection = sectp;
1307 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1308 }
dce234bc 1309 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1310 {
dce234bc
PP
1311 dwarf2_per_objfile->line.asection = sectp;
1312 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1313 }
dce234bc 1314 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1315 {
dce234bc
PP
1316 dwarf2_per_objfile->loc.asection = sectp;
1317 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1318 }
dce234bc 1319 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1320 {
dce234bc
PP
1321 dwarf2_per_objfile->macinfo.asection = sectp;
1322 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1323 }
dce234bc 1324 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1325 {
dce234bc
PP
1326 dwarf2_per_objfile->str.asection = sectp;
1327 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1328 }
dce234bc 1329 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1330 {
dce234bc
PP
1331 dwarf2_per_objfile->frame.asection = sectp;
1332 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1333 }
dce234bc 1334 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1335 {
3799ccc6 1336 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1337
3799ccc6
EZ
1338 if (aflag & SEC_HAS_CONTENTS)
1339 {
dce234bc
PP
1340 dwarf2_per_objfile->eh_frame.asection = sectp;
1341 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1342 }
b6af0555 1343 }
dce234bc 1344 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1345 {
dce234bc
PP
1346 dwarf2_per_objfile->ranges.asection = sectp;
1347 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1348 }
348e048f
DE
1349 else if (section_is_p (sectp->name, TYPES_SECTION))
1350 {
1351 dwarf2_per_objfile->types.asection = sectp;
1352 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1353 }
9291a0cd
TT
1354 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1355 {
1356 dwarf2_per_objfile->gdb_index.asection = sectp;
1357 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1358 }
dce234bc 1359
72dca2f5
FR
1360 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1361 && bfd_section_vma (abfd, sectp) == 0)
1362 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1363}
1364
dce234bc
PP
1365/* Decompress a section that was compressed using zlib. Store the
1366 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1367
1368static void
dce234bc
PP
1369zlib_decompress_section (struct objfile *objfile, asection *sectp,
1370 gdb_byte **outbuf, bfd_size_type *outsize)
1371{
1372 bfd *abfd = objfile->obfd;
1373#ifndef HAVE_ZLIB_H
1374 error (_("Support for zlib-compressed DWARF data (from '%s') "
1375 "is disabled in this copy of GDB"),
1376 bfd_get_filename (abfd));
1377#else
1378 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1379 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1380 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1381 bfd_size_type uncompressed_size;
1382 gdb_byte *uncompressed_buffer;
1383 z_stream strm;
1384 int rc;
1385 int header_size = 12;
1386
1387 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1388 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1389 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1390 bfd_get_filename (abfd));
1391
1392 /* Read the zlib header. In this case, it should be "ZLIB" followed
1393 by the uncompressed section size, 8 bytes in big-endian order. */
1394 if (compressed_size < header_size
1395 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1396 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1397 bfd_get_filename (abfd));
1398 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1399 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1400 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1401 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1402 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1403 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1404 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1405 uncompressed_size += compressed_buffer[11];
1406
1407 /* It is possible the section consists of several compressed
1408 buffers concatenated together, so we uncompress in a loop. */
1409 strm.zalloc = NULL;
1410 strm.zfree = NULL;
1411 strm.opaque = NULL;
1412 strm.avail_in = compressed_size - header_size;
1413 strm.next_in = (Bytef*) compressed_buffer + header_size;
1414 strm.avail_out = uncompressed_size;
1415 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1416 uncompressed_size);
1417 rc = inflateInit (&strm);
1418 while (strm.avail_in > 0)
1419 {
1420 if (rc != Z_OK)
1421 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1422 bfd_get_filename (abfd), rc);
1423 strm.next_out = ((Bytef*) uncompressed_buffer
1424 + (uncompressed_size - strm.avail_out));
1425 rc = inflate (&strm, Z_FINISH);
1426 if (rc != Z_STREAM_END)
1427 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1428 bfd_get_filename (abfd), rc);
1429 rc = inflateReset (&strm);
1430 }
1431 rc = inflateEnd (&strm);
1432 if (rc != Z_OK
1433 || strm.avail_out != 0)
1434 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1435 bfd_get_filename (abfd), rc);
1436
affddf13 1437 do_cleanups (cleanup);
dce234bc
PP
1438 *outbuf = uncompressed_buffer;
1439 *outsize = uncompressed_size;
1440#endif
233a11ab
CS
1441}
1442
dce234bc
PP
1443/* Read the contents of the section SECTP from object file specified by
1444 OBJFILE, store info about the section into INFO.
1445 If the section is compressed, uncompress it before returning. */
c906108c 1446
dce234bc
PP
1447static void
1448dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1449{
dce234bc
PP
1450 bfd *abfd = objfile->obfd;
1451 asection *sectp = info->asection;
1452 gdb_byte *buf, *retbuf;
1453 unsigned char header[4];
c906108c 1454
be391dca
TT
1455 if (info->readin)
1456 return;
dce234bc
PP
1457 info->buffer = NULL;
1458 info->was_mmapped = 0;
be391dca 1459 info->readin = 1;
188dd5d6 1460
dce234bc
PP
1461 if (info->asection == NULL || info->size == 0)
1462 return;
c906108c 1463
dce234bc
PP
1464 /* Check if the file has a 4-byte header indicating compression. */
1465 if (info->size > sizeof (header)
1466 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1467 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1468 {
1469 /* Upon decompression, update the buffer and its size. */
1470 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1471 {
1472 zlib_decompress_section (objfile, sectp, &info->buffer,
1473 &info->size);
1474 return;
1475 }
1476 }
4bdf3d34 1477
dce234bc
PP
1478#ifdef HAVE_MMAP
1479 if (pagesize == 0)
1480 pagesize = getpagesize ();
2e276125 1481
dce234bc
PP
1482 /* Only try to mmap sections which are large enough: we don't want to
1483 waste space due to fragmentation. Also, only try mmap for sections
1484 without relocations. */
1485
1486 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1487 {
1488 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1489 size_t map_length = info->size + sectp->filepos - pg_offset;
1490 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1491 MAP_PRIVATE, pg_offset);
1492
1493 if (retbuf != MAP_FAILED)
1494 {
1495 info->was_mmapped = 1;
1496 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1497#if HAVE_POSIX_MADVISE
1498 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1499#endif
dce234bc
PP
1500 return;
1501 }
1502 }
1503#endif
1504
1505 /* If we get here, we are a normal, not-compressed section. */
1506 info->buffer = buf
1507 = obstack_alloc (&objfile->objfile_obstack, info->size);
1508
1509 /* When debugging .o files, we may need to apply relocations; see
1510 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1511 We never compress sections in .o files, so we only need to
1512 try this when the section is not compressed. */
ac8035ab 1513 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1514 if (retbuf != NULL)
1515 {
1516 info->buffer = retbuf;
1517 return;
1518 }
1519
1520 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1521 || bfd_bread (buf, info->size, abfd) != info->size)
1522 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1523 bfd_get_filename (abfd));
1524}
1525
1526/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1527 SECTION_NAME. */
af34e669 1528
dce234bc
PP
1529void
1530dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1531 asection **sectp, gdb_byte **bufp,
1532 bfd_size_type *sizep)
1533{
1534 struct dwarf2_per_objfile *data
1535 = objfile_data (objfile, dwarf2_objfile_data_key);
1536 struct dwarf2_section_info *info;
a3b2a86b
TT
1537
1538 /* We may see an objfile without any DWARF, in which case we just
1539 return nothing. */
1540 if (data == NULL)
1541 {
1542 *sectp = NULL;
1543 *bufp = NULL;
1544 *sizep = 0;
1545 return;
1546 }
dce234bc
PP
1547 if (section_is_p (section_name, EH_FRAME_SECTION))
1548 info = &data->eh_frame;
1549 else if (section_is_p (section_name, FRAME_SECTION))
1550 info = &data->frame;
0d53c4c4 1551 else
dce234bc
PP
1552 gdb_assert (0);
1553
1554 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1555 /* We haven't read this section in yet. Do it now. */
1556 dwarf2_read_section (objfile, info);
1557
1558 *sectp = info->asection;
1559 *bufp = info->buffer;
1560 *sizep = info->size;
1561}
1562
9291a0cd
TT
1563\f
1564
1565/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1566 this CU came. */
1567static void
1568dw2_do_instantiate_symtab (struct objfile *objfile,
1569 struct dwarf2_per_cu_data *per_cu)
1570{
1571 struct cleanup *back_to;
1572
1573 back_to = make_cleanup (dwarf2_release_queue, NULL);
1574
1575 queue_comp_unit (per_cu, objfile);
1576
1577 if (per_cu->from_debug_types)
1578 read_signatured_type_at_offset (objfile, per_cu->offset);
1579 else
1580 load_full_comp_unit (per_cu, objfile);
1581
1582 process_queue (objfile);
1583
1584 /* Age the cache, releasing compilation units that have not
1585 been used recently. */
1586 age_cached_comp_units ();
1587
1588 do_cleanups (back_to);
1589}
1590
1591/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1592 the objfile from which this CU came. Returns the resulting symbol
1593 table. */
1594static struct symtab *
1595dw2_instantiate_symtab (struct objfile *objfile,
1596 struct dwarf2_per_cu_data *per_cu)
1597{
1598 if (!per_cu->v.quick->symtab)
1599 {
1600 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1601 increment_reading_symtab ();
1602 dw2_do_instantiate_symtab (objfile, per_cu);
1603 do_cleanups (back_to);
1604 }
1605 return per_cu->v.quick->symtab;
1606}
1607
1608/* A helper function that knows how to read a 64-bit value in a way
1609 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1610 otherwise. */
1611static int
1612extract_cu_value (const char *bytes, ULONGEST *result)
1613{
1614 if (sizeof (ULONGEST) < 8)
1615 {
1616 int i;
1617
1618 /* Ignore the upper 4 bytes if they are all zero. */
1619 for (i = 0; i < 4; ++i)
1620 if (bytes[i + 4] != 0)
1621 return 0;
1622
1623 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1624 }
1625 else
1626 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1627 return 1;
1628}
1629
1630/* Read the CU list from the mapped index, and use it to create all
1631 the CU objects for this objfile. Return 0 if something went wrong,
1632 1 if everything went ok. */
1633static int
1634create_cus_from_index (struct objfile *objfile, struct mapped_index *index,
1635 const gdb_byte *cu_list, offset_type cu_list_elements)
1636{
1637 offset_type i;
1638 const char *entry;
1639
1640 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1641 dwarf2_per_objfile->all_comp_units
1642 = obstack_alloc (&objfile->objfile_obstack,
1643 dwarf2_per_objfile->n_comp_units
1644 * sizeof (struct dwarf2_per_cu_data *));
1645
1646 for (i = 0; i < cu_list_elements; i += 2)
1647 {
1648 struct dwarf2_per_cu_data *the_cu;
1649 ULONGEST offset, length;
1650
1651 if (!extract_cu_value (cu_list, &offset)
1652 || !extract_cu_value (cu_list + 8, &length))
1653 return 0;
1654 cu_list += 2 * 8;
1655
1656 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1657 struct dwarf2_per_cu_data);
1658 the_cu->offset = offset;
1659 the_cu->length = length;
1660 the_cu->objfile = objfile;
1661 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1662 struct dwarf2_per_cu_quick_data);
1663 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1664 }
1665
1666 return 1;
1667}
1668
1669/* Read the address map data from the mapped index, and use it to
1670 populate the objfile's psymtabs_addrmap. */
1671static void
1672create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1673{
1674 const gdb_byte *iter, *end;
1675 struct obstack temp_obstack;
1676 struct addrmap *mutable_map;
1677 struct cleanup *cleanup;
1678 CORE_ADDR baseaddr;
1679
1680 obstack_init (&temp_obstack);
1681 cleanup = make_cleanup_obstack_free (&temp_obstack);
1682 mutable_map = addrmap_create_mutable (&temp_obstack);
1683
1684 iter = index->address_table;
1685 end = iter + index->address_table_size;
1686
1687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1688
1689 while (iter < end)
1690 {
1691 ULONGEST hi, lo, cu_index;
1692 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1693 iter += 8;
1694 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1695 iter += 8;
1696 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1697 iter += 4;
1698
1699 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1700 dwarf2_per_objfile->all_comp_units[cu_index]);
1701 }
1702
1703 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1704 &objfile->objfile_obstack);
1705 do_cleanups (cleanup);
1706}
1707
1708/* The hash function for strings in the mapped index. This is the
1709 same as the hashtab.c hash function, but we keep a separate copy to
1710 maintain control over the implementation. This is necessary
1711 because the hash function is tied to the format of the mapped index
1712 file. */
1713static hashval_t
1714mapped_index_string_hash (const void *p)
1715{
1716 const unsigned char *str = (const unsigned char *) p;
1717 hashval_t r = 0;
1718 unsigned char c;
1719
1720 while ((c = *str++) != 0)
1721 r = r * 67 + c - 113;
1722
1723 return r;
1724}
1725
1726/* Find a slot in the mapped index INDEX for the object named NAME.
1727 If NAME is found, set *VEC_OUT to point to the CU vector in the
1728 constant pool and return 1. If NAME cannot be found, return 0. */
1729static int
1730find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1731 offset_type **vec_out)
1732{
1733 offset_type hash = mapped_index_string_hash (name);
1734 offset_type slot, step;
1735
1736 slot = hash & (index->index_table_slots - 1);
1737 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1738
1739 for (;;)
1740 {
1741 /* Convert a slot number to an offset into the table. */
1742 offset_type i = 2 * slot;
1743 const char *str;
1744 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1745 return 0;
1746
1747 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1748 if (!strcmp (name, str))
1749 {
1750 *vec_out = (offset_type *) (index->constant_pool
1751 + MAYBE_SWAP (index->index_table[i + 1]));
1752 return 1;
1753 }
1754
1755 slot = (slot + step) & (index->index_table_slots - 1);
1756 }
1757}
1758
1759/* Read the index file. If everything went ok, initialize the "quick"
1760 elements of all the CUs and return 1. Otherwise, return 0. */
1761static int
1762dwarf2_read_index (struct objfile *objfile)
1763{
9291a0cd
TT
1764 char *addr;
1765 struct mapped_index *map;
b3b272e1 1766 offset_type *metadata;
9291a0cd
TT
1767 const gdb_byte *cu_list;
1768 offset_type cu_list_elements;
1769
1770 if (dwarf2_per_objfile->gdb_index.asection == NULL
1771 || dwarf2_per_objfile->gdb_index.size == 0)
1772 return 0;
1773 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1774
1775 addr = dwarf2_per_objfile->gdb_index.buffer;
1776 /* Version check. */
1777 if (MAYBE_SWAP (*(offset_type *) addr) != 1)
1778 return 0;
1779
1780 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1781 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1782
1783 metadata = (offset_type *) (addr + sizeof (offset_type));
1784 cu_list = addr + MAYBE_SWAP (metadata[0]);
1785 cu_list_elements = ((MAYBE_SWAP (metadata[1]) - MAYBE_SWAP (metadata[0]))
1786 / 8);
1787 map->address_table = addr + MAYBE_SWAP (metadata[1]);
1788 map->address_table_size = (MAYBE_SWAP (metadata[2])
1789 - MAYBE_SWAP (metadata[1]));
1790 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[2]));
1791 map->index_table_slots = ((MAYBE_SWAP (metadata[3])
1792 - MAYBE_SWAP (metadata[2]))
1793 / (2 * sizeof (offset_type)));
1794 map->constant_pool = addr + MAYBE_SWAP (metadata[3]);
1795
1796 if (!create_cus_from_index (objfile, map, cu_list, cu_list_elements))
1797 return 0;
1798
1799 create_addrmap_from_index (objfile, map);
1800
1801 dwarf2_per_objfile->index_table = map;
1802 dwarf2_per_objfile->using_index = 1;
1803
1804 return 1;
1805}
1806
1807/* A helper for the "quick" functions which sets the global
1808 dwarf2_per_objfile according to OBJFILE. */
1809static void
1810dw2_setup (struct objfile *objfile)
1811{
1812 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1813 gdb_assert (dwarf2_per_objfile);
1814}
1815
1816/* A helper for the "quick" functions which attempts to read the line
1817 table for THIS_CU. */
1818static void
1819dw2_require_line_header (struct objfile *objfile,
1820 struct dwarf2_per_cu_data *this_cu)
1821{
1822 bfd *abfd = objfile->obfd;
1823 struct line_header *lh = NULL;
1824 struct attribute *attr;
1825 struct cleanup *cleanups;
1826 struct die_info *comp_unit_die;
1827 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1828 int has_children, i;
1829 struct dwarf2_cu cu;
1830 unsigned int bytes_read, buffer_size;
1831 struct die_reader_specs reader_specs;
1832 char *name, *comp_dir;
1833
1834 if (this_cu->v.quick->read_lines)
1835 return;
1836 this_cu->v.quick->read_lines = 1;
1837
1838 memset (&cu, 0, sizeof (cu));
1839 cu.objfile = objfile;
1840 obstack_init (&cu.comp_unit_obstack);
1841
1842 cleanups = make_cleanup (free_stack_comp_unit, &cu);
1843
1844 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1845 buffer_size = dwarf2_per_objfile->info.size;
1846 buffer = dwarf2_per_objfile->info.buffer;
1847 info_ptr = buffer + this_cu->offset;
1848 beg_of_comp_unit = info_ptr;
1849
1850 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1851 buffer, buffer_size,
1852 abfd);
1853
1854 /* Complete the cu_header. */
1855 cu.header.offset = beg_of_comp_unit - buffer;
1856 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1857
1858 this_cu->cu = &cu;
1859 cu.per_cu = this_cu;
1860
1861 dwarf2_read_abbrevs (abfd, &cu);
1862 make_cleanup (dwarf2_free_abbrev_table, &cu);
1863
1864 if (this_cu->from_debug_types)
1865 info_ptr += 8 /*signature*/ + cu.header.offset_size;
1866 init_cu_die_reader (&reader_specs, &cu);
1867 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1868 &has_children);
1869
1870 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
1871 if (attr)
1872 {
1873 unsigned int line_offset = DW_UNSND (attr);
1874 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
1875 }
1876 if (lh == NULL)
1877 {
1878 do_cleanups (cleanups);
1879 return;
1880 }
1881
1882 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
1883
1884 this_cu->v.quick->lines = lh;
1885
1886 this_cu->v.quick->file_names
1887 = obstack_alloc (&objfile->objfile_obstack,
1888 lh->num_file_names * sizeof (char *));
1889 for (i = 0; i < lh->num_file_names; ++i)
1890 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
1891
1892 do_cleanups (cleanups);
1893}
1894
1895/* A helper for the "quick" functions which computes and caches the
1896 real path for a given file name from the line table.
1897 dw2_require_line_header must have been called before this is
1898 invoked. */
1899static const char *
1900dw2_require_full_path (struct objfile *objfile,
1901 struct dwarf2_per_cu_data *cu,
1902 int index)
1903{
1904 if (!cu->v.quick->full_names)
1905 cu->v.quick->full_names
1906 = OBSTACK_CALLOC (&objfile->objfile_obstack,
1907 cu->v.quick->lines->num_file_names,
1908 sizeof (char *));
1909
1910 if (!cu->v.quick->full_names[index])
1911 cu->v.quick->full_names[index]
1912 = gdb_realpath (cu->v.quick->file_names[index]);
1913
1914 return cu->v.quick->full_names[index];
1915}
1916
1917static struct symtab *
1918dw2_find_last_source_symtab (struct objfile *objfile)
1919{
1920 int index;
1921 dw2_setup (objfile);
1922 index = dwarf2_per_objfile->n_comp_units - 1;
1923 return dw2_instantiate_symtab (objfile,
1924 dwarf2_per_objfile->all_comp_units[index]);
1925}
1926
1927static void
1928dw2_forget_cached_source_info (struct objfile *objfile)
1929{
1930 int i;
1931
1932 dw2_setup (objfile);
1933 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1934 {
1935 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
1936
1937 if (cu->v.quick->full_names)
1938 {
1939 int j;
1940
1941 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
1942 xfree ((void *) cu->v.quick->full_names[j]);
1943 }
1944 }
1945}
1946
1947static int
1948dw2_lookup_symtab (struct objfile *objfile, const char *name,
1949 const char *full_path, const char *real_path,
1950 struct symtab **result)
1951{
1952 int i;
1953 int check_basename = lbasename (name) == name;
1954 struct dwarf2_per_cu_data *base_cu = NULL;
1955
1956 dw2_setup (objfile);
1957 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1958 {
1959 int j;
1960 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
1961
1962 if (cu->v.quick->symtab)
1963 continue;
1964
1965 dw2_require_line_header (objfile, cu);
1966 if (!cu->v.quick->lines)
1967 continue;
1968
1969 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
1970 {
1971 const char *this_name = cu->v.quick->file_names[j];
1972
1973 if (FILENAME_CMP (name, this_name) == 0)
1974 {
1975 *result = dw2_instantiate_symtab (objfile, cu);
1976 return 1;
1977 }
1978
1979 if (check_basename && ! base_cu
1980 && FILENAME_CMP (lbasename (this_name), name) == 0)
1981 base_cu = cu;
1982
1983 if (full_path != NULL)
1984 {
1985 const char *this_full_name = dw2_require_full_path (objfile,
1986 cu, j);
1987
1988 if (this_full_name
1989 && FILENAME_CMP (full_path, this_full_name) == 0)
1990 {
1991 *result = dw2_instantiate_symtab (objfile, cu);
1992 return 1;
1993 }
1994 }
1995
1996 if (real_path != NULL)
1997 {
1998 const char *this_full_name = dw2_require_full_path (objfile,
1999 cu, j);
2000
2001 if (this_full_name != NULL)
2002 {
2003 char *rp = gdb_realpath (this_full_name);
2004 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2005 {
2006 xfree (rp);
2007 *result = dw2_instantiate_symtab (objfile, cu);
2008 return 1;
2009 }
2010 xfree (rp);
2011 }
2012 }
2013 }
2014 }
2015
2016 if (base_cu)
2017 {
2018 *result = dw2_instantiate_symtab (objfile, base_cu);
2019 return 1;
2020 }
2021
2022 return 0;
2023}
2024
2025static struct symtab *
2026dw2_lookup_symbol (struct objfile *objfile, int block_index,
2027 const char *name, domain_enum domain)
2028{
2029 /* We do all the work in the pre_expand_symtabs_matching hook
2030 instead. */
2031 return NULL;
2032}
2033
2034/* A helper function that expands all symtabs that hold an object
2035 named NAME. */
2036static void
2037dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2038{
2039 dw2_setup (objfile);
2040
2041 if (dwarf2_per_objfile->index_table)
2042 {
2043 offset_type *vec;
2044
2045 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2046 name, &vec))
2047 {
2048 offset_type i, len = MAYBE_SWAP (*vec);
2049 for (i = 0; i < len; ++i)
2050 {
2051 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2052 struct dwarf2_per_cu_data *cu;
2053 cu = dwarf2_per_objfile->all_comp_units[cu_index];
2054 dw2_instantiate_symtab (objfile, cu);
2055 }
2056 }
2057 }
2058}
2059
2060static void
2061dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2062 int kind, const char *name,
2063 domain_enum domain)
2064{
2065 dw2_do_expand_symtabs_matching (objfile, name);
2066}
2067
2068static void
2069dw2_print_stats (struct objfile *objfile)
2070{
2071 int i, count;
2072
2073 dw2_setup (objfile);
2074 count = 0;
2075 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2076 {
2077 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2078
2079 if (!cu->v.quick->symtab)
2080 ++count;
2081 }
2082 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2083}
2084
2085static void
2086dw2_dump (struct objfile *objfile)
2087{
2088 /* Nothing worth printing. */
2089}
2090
2091static void
2092dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2093 struct section_offsets *delta)
2094{
2095 /* There's nothing to relocate here. */
2096}
2097
2098static void
2099dw2_expand_symtabs_for_function (struct objfile *objfile,
2100 const char *func_name)
2101{
2102 dw2_do_expand_symtabs_matching (objfile, func_name);
2103}
2104
2105static void
2106dw2_expand_all_symtabs (struct objfile *objfile)
2107{
2108 int i;
2109
2110 dw2_setup (objfile);
2111 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2112 {
2113 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2114
2115 dw2_instantiate_symtab (objfile, cu);
2116 }
2117}
2118
2119static void
2120dw2_expand_symtabs_with_filename (struct objfile *objfile,
2121 const char *filename)
2122{
2123 int i;
2124
2125 dw2_setup (objfile);
2126 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2127 {
2128 int j;
2129 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2130
2131 if (cu->v.quick->symtab)
2132 continue;
2133
2134 dw2_require_line_header (objfile, cu);
2135 if (!cu->v.quick->lines)
2136 continue;
2137
2138 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2139 {
2140 const char *this_name = cu->v.quick->file_names[j];
2141 if (strcmp (this_name, filename) == 0)
2142 {
2143 dw2_instantiate_symtab (objfile, cu);
2144 break;
2145 }
2146 }
2147 }
2148}
2149
dd786858 2150static const char *
9291a0cd
TT
2151dw2_find_symbol_file (struct objfile *objfile, const char *name)
2152{
2153 struct dwarf2_per_cu_data *cu;
2154 offset_type *vec;
2155
2156 dw2_setup (objfile);
2157
2158 if (!dwarf2_per_objfile->index_table)
2159 return NULL;
2160
2161 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2162 name, &vec))
2163 return NULL;
2164
2165 /* Note that this just looks at the very first one named NAME -- but
2166 actually we are looking for a function. find_main_filename
2167 should be rewritten so that it doesn't require a custom hook. It
2168 could just use the ordinary symbol tables. */
2169 /* vec[0] is the length, which must always be >0. */
2170 cu = dwarf2_per_objfile->all_comp_units[MAYBE_SWAP (vec[1])];
2171
2172 dw2_require_line_header (objfile, cu);
2173 if (!cu->v.quick->lines)
2174 return NULL;
2175
dd786858 2176 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2177}
2178
2179static void
2180dw2_map_ada_symtabs (struct objfile *objfile,
2181 int (*wild_match) (const char *, int, const char *),
2182 int (*is_name_suffix) (const char *),
2183 void (*callback) (struct objfile *,
2184 struct symtab *, void *),
2185 const char *name, int global,
2186 domain_enum namespace, int wild,
2187 void *data)
2188{
2189 /* For now, we don't support Ada, so this function can't be
2190 reached. */
2191 internal_error (__FILE__, __LINE__,
2192 _("map_ada_symtabs called via index method"));
2193}
2194
2195static void
2196dw2_expand_symtabs_matching (struct objfile *objfile,
2197 int (*file_matcher) (const char *, void *),
2198 int (*name_matcher) (const char *, void *),
2199 domain_enum kind,
2200 void *data)
2201{
2202 int i;
2203 offset_type iter;
2204
2205 dw2_setup (objfile);
2206 if (!dwarf2_per_objfile->index_table)
2207 return;
2208
2209 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2210 {
2211 int j;
2212 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2213
2214 cu->v.quick->mark = 0;
2215 if (cu->v.quick->symtab)
2216 continue;
2217
2218 dw2_require_line_header (objfile, cu);
2219 if (!cu->v.quick->lines)
2220 continue;
2221
2222 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2223 {
2224 if (file_matcher (cu->v.quick->file_names[j], data))
2225 {
2226 cu->v.quick->mark = 1;
2227 break;
2228 }
2229 }
2230 }
2231
2232 for (iter = 0;
2233 iter < dwarf2_per_objfile->index_table->index_table_slots;
2234 ++iter)
2235 {
2236 offset_type idx = 2 * iter;
2237 const char *name;
2238 offset_type *vec, vec_len, vec_idx;
2239
2240 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2241 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2242 continue;
2243
2244 name = (dwarf2_per_objfile->index_table->constant_pool
2245 + dwarf2_per_objfile->index_table->index_table[idx]);
2246
2247 if (! (*name_matcher) (name, data))
2248 continue;
2249
2250 /* The name was matched, now expand corresponding CUs that were
2251 marked. */
2252 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2253 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2254 vec_len = MAYBE_SWAP (vec[0]);
2255 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2256 {
2257 struct dwarf2_per_cu_data *cu
2258 = dwarf2_per_objfile->all_comp_units[MAYBE_SWAP (vec[vec_idx + 1])];
2259 if (cu->v.quick->mark)
2260 dw2_instantiate_symtab (objfile, cu);
2261 }
2262 }
2263}
2264
2265static struct symtab *
2266dw2_find_pc_sect_symtab (struct objfile *objfile,
2267 struct minimal_symbol *msymbol,
2268 CORE_ADDR pc,
2269 struct obj_section *section,
2270 int warn_if_readin)
2271{
2272 struct dwarf2_per_cu_data *data;
2273
2274 dw2_setup (objfile);
2275
2276 if (!objfile->psymtabs_addrmap)
2277 return NULL;
2278
2279 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2280 if (!data)
2281 return NULL;
2282
2283 if (warn_if_readin && data->v.quick->symtab)
2284 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)\n"),
2285 paddress (get_objfile_arch (objfile), pc));
2286
2287 return dw2_instantiate_symtab (objfile, data);
2288}
2289
2290static void
2291dw2_map_symbol_names (struct objfile *objfile,
2292 void (*fun) (const char *, void *),
2293 void *data)
2294{
2295 offset_type iter;
2296 dw2_setup (objfile);
2297
2298 if (!dwarf2_per_objfile->index_table)
2299 return;
2300
2301 for (iter = 0;
2302 iter < dwarf2_per_objfile->index_table->index_table_slots;
2303 ++iter)
2304 {
2305 offset_type idx = 2 * iter;
2306 const char *name;
2307 offset_type *vec, vec_len, vec_idx;
2308
2309 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2310 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2311 continue;
2312
2313 name = (dwarf2_per_objfile->index_table->constant_pool
2314 + dwarf2_per_objfile->index_table->index_table[idx]);
2315
2316 (*fun) (name, data);
2317 }
2318}
2319
2320static void
2321dw2_map_symbol_filenames (struct objfile *objfile,
2322 void (*fun) (const char *, const char *, void *),
2323 void *data)
2324{
2325 int i;
2326
2327 dw2_setup (objfile);
2328 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2329 {
2330 int j;
2331 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2332
2333 if (cu->v.quick->symtab)
2334 continue;
2335
2336 dw2_require_line_header (objfile, cu);
2337 if (!cu->v.quick->lines)
2338 continue;
2339
2340 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2341 {
2342 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2343 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2344 }
2345 }
2346}
2347
2348static int
2349dw2_has_symbols (struct objfile *objfile)
2350{
2351 return 1;
2352}
2353
2354const struct quick_symbol_functions dwarf2_gdb_index_functions =
2355{
2356 dw2_has_symbols,
2357 dw2_find_last_source_symtab,
2358 dw2_forget_cached_source_info,
2359 dw2_lookup_symtab,
2360 dw2_lookup_symbol,
2361 dw2_pre_expand_symtabs_matching,
2362 dw2_print_stats,
2363 dw2_dump,
2364 dw2_relocate,
2365 dw2_expand_symtabs_for_function,
2366 dw2_expand_all_symtabs,
2367 dw2_expand_symtabs_with_filename,
2368 dw2_find_symbol_file,
2369 dw2_map_ada_symtabs,
2370 dw2_expand_symtabs_matching,
2371 dw2_find_pc_sect_symtab,
2372 dw2_map_symbol_names,
2373 dw2_map_symbol_filenames
2374};
2375
2376/* Initialize for reading DWARF for this objfile. Return 0 if this
2377 file will use psymtabs, or 1 if using the GNU index. */
2378
2379int
2380dwarf2_initialize_objfile (struct objfile *objfile)
2381{
2382 /* If we're about to read full symbols, don't bother with the
2383 indices. In this case we also don't care if some other debug
2384 format is making psymtabs, because they are all about to be
2385 expanded anyway. */
2386 if ((objfile->flags & OBJF_READNOW))
2387 {
2388 int i;
2389
2390 dwarf2_per_objfile->using_index = 1;
2391 create_all_comp_units (objfile);
2392
2393 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2394 {
2395 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2396
2397 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2398 struct dwarf2_per_cu_quick_data);
2399 }
2400
2401 /* Return 1 so that gdb sees the "quick" functions. However,
2402 these functions will be no-ops because we will have expanded
2403 all symtabs. */
2404 return 1;
2405 }
2406
2407 if (dwarf2_read_index (objfile))
2408 return 1;
2409
2410 dwarf2_build_psymtabs (objfile);
2411 return 0;
2412}
2413
2414\f
2415
dce234bc
PP
2416/* Build a partial symbol table. */
2417
2418void
f29dff0a 2419dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2420{
f29dff0a 2421 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2422 {
2423 init_psymbol_list (objfile, 1024);
2424 }
2425
d146bf1e 2426 dwarf2_build_psymtabs_hard (objfile);
c906108c 2427}
c906108c 2428
45452591
DE
2429/* Return TRUE if OFFSET is within CU_HEADER. */
2430
2431static inline int
2432offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2433{
2434 unsigned int bottom = cu_header->offset;
2435 unsigned int top = (cu_header->offset
2436 + cu_header->length
2437 + cu_header->initial_length_size);
9a619af0 2438
45452591
DE
2439 return (offset >= bottom && offset < top);
2440}
2441
93311388
DE
2442/* Read in the comp unit header information from the debug_info at info_ptr.
2443 NOTE: This leaves members offset, first_die_offset to be filled in
2444 by the caller. */
107d2387 2445
fe1b8b76 2446static gdb_byte *
107d2387 2447read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2448 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2449{
2450 int signed_addr;
891d2f0b 2451 unsigned int bytes_read;
c764a876
DE
2452
2453 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2454 cu_header->initial_length_size = bytes_read;
2455 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2456 info_ptr += bytes_read;
107d2387
AC
2457 cu_header->version = read_2_bytes (abfd, info_ptr);
2458 info_ptr += 2;
613e1657 2459 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2460 &bytes_read);
613e1657 2461 info_ptr += bytes_read;
107d2387
AC
2462 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2463 info_ptr += 1;
2464 signed_addr = bfd_get_sign_extend_vma (abfd);
2465 if (signed_addr < 0)
8e65ff28 2466 internal_error (__FILE__, __LINE__,
e2e0b3e5 2467 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2468 cu_header->signed_addr_p = signed_addr;
c764a876 2469
107d2387
AC
2470 return info_ptr;
2471}
2472
fe1b8b76
JB
2473static gdb_byte *
2474partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2475 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2476 bfd *abfd)
2477{
fe1b8b76 2478 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2479
2480 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2481
2dc7f7b3 2482 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2483 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2484 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2485 bfd_get_filename (abfd));
72bf9492 2486
dce234bc 2487 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2488 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2489 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2490 (long) header->abbrev_offset,
93311388 2491 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2492 bfd_get_filename (abfd));
2493
2494 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2495 > buffer + buffer_size)
8a3fe4f8
AC
2496 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2497 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2498 (long) header->length,
93311388 2499 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2500 bfd_get_filename (abfd));
2501
2502 return info_ptr;
2503}
2504
348e048f
DE
2505/* Read in the types comp unit header information from .debug_types entry at
2506 types_ptr. The result is a pointer to one past the end of the header. */
2507
2508static gdb_byte *
2509read_type_comp_unit_head (struct comp_unit_head *cu_header,
2510 ULONGEST *signature,
2511 gdb_byte *types_ptr, bfd *abfd)
2512{
348e048f
DE
2513 gdb_byte *initial_types_ptr = types_ptr;
2514
6e70227d 2515 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2516 &dwarf2_per_objfile->types);
348e048f
DE
2517 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2518
2519 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2520
2521 *signature = read_8_bytes (abfd, types_ptr);
2522 types_ptr += 8;
2523 types_ptr += cu_header->offset_size;
2524 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2525
2526 return types_ptr;
2527}
2528
aaa75496
JB
2529/* Allocate a new partial symtab for file named NAME and mark this new
2530 partial symtab as being an include of PST. */
2531
2532static void
2533dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2534 struct objfile *objfile)
2535{
2536 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2537
2538 subpst->section_offsets = pst->section_offsets;
2539 subpst->textlow = 0;
2540 subpst->texthigh = 0;
2541
2542 subpst->dependencies = (struct partial_symtab **)
2543 obstack_alloc (&objfile->objfile_obstack,
2544 sizeof (struct partial_symtab *));
2545 subpst->dependencies[0] = pst;
2546 subpst->number_of_dependencies = 1;
2547
2548 subpst->globals_offset = 0;
2549 subpst->n_global_syms = 0;
2550 subpst->statics_offset = 0;
2551 subpst->n_static_syms = 0;
2552 subpst->symtab = NULL;
2553 subpst->read_symtab = pst->read_symtab;
2554 subpst->readin = 0;
2555
2556 /* No private part is necessary for include psymtabs. This property
2557 can be used to differentiate between such include psymtabs and
10b3939b 2558 the regular ones. */
58a9656e 2559 subpst->read_symtab_private = NULL;
aaa75496
JB
2560}
2561
2562/* Read the Line Number Program data and extract the list of files
2563 included by the source file represented by PST. Build an include
d85a05f0 2564 partial symtab for each of these included files. */
aaa75496
JB
2565
2566static void
2567dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2568 struct die_info *die,
aaa75496
JB
2569 struct partial_symtab *pst)
2570{
2571 struct objfile *objfile = cu->objfile;
2572 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2573 struct line_header *lh = NULL;
2574 struct attribute *attr;
aaa75496 2575
d85a05f0
DJ
2576 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2577 if (attr)
2578 {
2579 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2580
d85a05f0
DJ
2581 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2582 }
aaa75496
JB
2583 if (lh == NULL)
2584 return; /* No linetable, so no includes. */
2585
2586 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2587
2588 free_line_header (lh);
2589}
2590
348e048f
DE
2591static hashval_t
2592hash_type_signature (const void *item)
2593{
2594 const struct signatured_type *type_sig = item;
9a619af0 2595
348e048f
DE
2596 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2597 return type_sig->signature;
2598}
2599
2600static int
2601eq_type_signature (const void *item_lhs, const void *item_rhs)
2602{
2603 const struct signatured_type *lhs = item_lhs;
2604 const struct signatured_type *rhs = item_rhs;
9a619af0 2605
348e048f
DE
2606 return lhs->signature == rhs->signature;
2607}
2608
2609/* Create the hash table of all entries in the .debug_types section.
2610 The result is zero if there is an error (e.g. missing .debug_types section),
2611 otherwise non-zero. */
2612
2613static int
2614create_debug_types_hash_table (struct objfile *objfile)
2615{
be391dca 2616 gdb_byte *info_ptr;
348e048f
DE
2617 htab_t types_htab;
2618
be391dca
TT
2619 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2620 info_ptr = dwarf2_per_objfile->types.buffer;
2621
348e048f
DE
2622 if (info_ptr == NULL)
2623 {
2624 dwarf2_per_objfile->signatured_types = NULL;
2625 return 0;
2626 }
2627
2628 types_htab = htab_create_alloc_ex (41,
2629 hash_type_signature,
2630 eq_type_signature,
2631 NULL,
2632 &objfile->objfile_obstack,
2633 hashtab_obstack_allocate,
2634 dummy_obstack_deallocate);
2635
2636 if (dwarf2_die_debug)
2637 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2638
2639 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2640 {
2641 unsigned int offset;
2642 unsigned int offset_size;
2643 unsigned int type_offset;
2644 unsigned int length, initial_length_size;
2645 unsigned short version;
2646 ULONGEST signature;
2647 struct signatured_type *type_sig;
2648 void **slot;
2649 gdb_byte *ptr = info_ptr;
2650
2651 offset = ptr - dwarf2_per_objfile->types.buffer;
2652
2653 /* We need to read the type's signature in order to build the hash
2654 table, but we don't need to read anything else just yet. */
2655
2656 /* Sanity check to ensure entire cu is present. */
2657 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2658 if (ptr + length + initial_length_size
2659 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2660 {
2661 complaint (&symfile_complaints,
2662 _("debug type entry runs off end of `.debug_types' section, ignored"));
2663 break;
2664 }
2665
2666 offset_size = initial_length_size == 4 ? 4 : 8;
2667 ptr += initial_length_size;
2668 version = bfd_get_16 (objfile->obfd, ptr);
2669 ptr += 2;
2670 ptr += offset_size; /* abbrev offset */
2671 ptr += 1; /* address size */
2672 signature = bfd_get_64 (objfile->obfd, ptr);
2673 ptr += 8;
2674 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2675
2676 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2677 memset (type_sig, 0, sizeof (*type_sig));
2678 type_sig->signature = signature;
2679 type_sig->offset = offset;
2680 type_sig->type_offset = type_offset;
2681
2682 slot = htab_find_slot (types_htab, type_sig, INSERT);
2683 gdb_assert (slot != NULL);
2684 *slot = type_sig;
2685
2686 if (dwarf2_die_debug)
2687 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2688 offset, phex (signature, sizeof (signature)));
2689
2690 info_ptr = info_ptr + initial_length_size + length;
2691 }
2692
2693 dwarf2_per_objfile->signatured_types = types_htab;
2694
2695 return 1;
2696}
2697
2698/* Lookup a signature based type.
2699 Returns NULL if SIG is not present in the table. */
2700
2701static struct signatured_type *
2702lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2703{
2704 struct signatured_type find_entry, *entry;
2705
2706 if (dwarf2_per_objfile->signatured_types == NULL)
2707 {
2708 complaint (&symfile_complaints,
2709 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2710 return 0;
2711 }
2712
2713 find_entry.signature = sig;
2714 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2715 return entry;
2716}
2717
d85a05f0
DJ
2718/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2719
2720static void
2721init_cu_die_reader (struct die_reader_specs *reader,
2722 struct dwarf2_cu *cu)
2723{
2724 reader->abfd = cu->objfile->obfd;
2725 reader->cu = cu;
2726 if (cu->per_cu->from_debug_types)
be391dca
TT
2727 {
2728 gdb_assert (dwarf2_per_objfile->types.readin);
2729 reader->buffer = dwarf2_per_objfile->types.buffer;
2730 }
d85a05f0 2731 else
be391dca
TT
2732 {
2733 gdb_assert (dwarf2_per_objfile->info.readin);
2734 reader->buffer = dwarf2_per_objfile->info.buffer;
2735 }
d85a05f0
DJ
2736}
2737
2738/* Find the base address of the compilation unit for range lists and
2739 location lists. It will normally be specified by DW_AT_low_pc.
2740 In DWARF-3 draft 4, the base address could be overridden by
2741 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2742 compilation units with discontinuous ranges. */
2743
2744static void
2745dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2746{
2747 struct attribute *attr;
2748
2749 cu->base_known = 0;
2750 cu->base_address = 0;
2751
2752 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2753 if (attr)
2754 {
2755 cu->base_address = DW_ADDR (attr);
2756 cu->base_known = 1;
2757 }
2758 else
2759 {
2760 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2761 if (attr)
2762 {
2763 cu->base_address = DW_ADDR (attr);
2764 cu->base_known = 1;
2765 }
2766 }
2767}
2768
348e048f
DE
2769/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2770 to combine the common parts.
93311388 2771 Process a compilation unit for a psymtab.
348e048f
DE
2772 BUFFER is a pointer to the beginning of the dwarf section buffer,
2773 either .debug_info or debug_types.
93311388
DE
2774 INFO_PTR is a pointer to the start of the CU.
2775 Returns a pointer to the next CU. */
aaa75496 2776
93311388
DE
2777static gdb_byte *
2778process_psymtab_comp_unit (struct objfile *objfile,
2779 struct dwarf2_per_cu_data *this_cu,
2780 gdb_byte *buffer, gdb_byte *info_ptr,
2781 unsigned int buffer_size)
c906108c 2782{
c906108c 2783 bfd *abfd = objfile->obfd;
93311388 2784 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 2785 struct die_info *comp_unit_die;
c906108c 2786 struct partial_symtab *pst;
5734ee8b 2787 CORE_ADDR baseaddr;
93311388
DE
2788 struct cleanup *back_to_inner;
2789 struct dwarf2_cu cu;
d85a05f0
DJ
2790 int has_children, has_pc_info;
2791 struct attribute *attr;
d85a05f0
DJ
2792 CORE_ADDR best_lowpc = 0, best_highpc = 0;
2793 struct die_reader_specs reader_specs;
c906108c 2794
93311388
DE
2795 memset (&cu, 0, sizeof (cu));
2796 cu.objfile = objfile;
2797 obstack_init (&cu.comp_unit_obstack);
c906108c 2798
93311388 2799 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 2800
93311388
DE
2801 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2802 buffer, buffer_size,
2803 abfd);
10b3939b 2804
93311388
DE
2805 /* Complete the cu_header. */
2806 cu.header.offset = beg_of_comp_unit - buffer;
2807 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 2808
93311388 2809 cu.list_in_scope = &file_symbols;
af703f96 2810
328c9494
DJ
2811 /* If this compilation unit was already read in, free the
2812 cached copy in order to read it in again. This is
2813 necessary because we skipped some symbols when we first
2814 read in the compilation unit (see load_partial_dies).
2815 This problem could be avoided, but the benefit is
2816 unclear. */
2817 if (this_cu->cu != NULL)
2818 free_one_cached_comp_unit (this_cu->cu);
2819
2820 /* Note that this is a pointer to our stack frame, being
2821 added to a global data structure. It will be cleaned up
2822 in free_stack_comp_unit when we finish with this
2823 compilation unit. */
2824 this_cu->cu = &cu;
d85a05f0
DJ
2825 cu.per_cu = this_cu;
2826
93311388
DE
2827 /* Read the abbrevs for this compilation unit into a table. */
2828 dwarf2_read_abbrevs (abfd, &cu);
2829 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 2830
93311388 2831 /* Read the compilation unit die. */
348e048f
DE
2832 if (this_cu->from_debug_types)
2833 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
2834 init_cu_die_reader (&reader_specs, &cu);
2835 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2836 &has_children);
93311388 2837
348e048f
DE
2838 if (this_cu->from_debug_types)
2839 {
2840 /* offset,length haven't been set yet for type units. */
2841 this_cu->offset = cu.header.offset;
2842 this_cu->length = cu.header.length + cu.header.initial_length_size;
2843 }
d85a05f0 2844 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 2845 {
93311388
DE
2846 info_ptr = (beg_of_comp_unit + cu.header.length
2847 + cu.header.initial_length_size);
2848 do_cleanups (back_to_inner);
2849 return info_ptr;
2850 }
72bf9492 2851
93311388 2852 /* Set the language we're debugging. */
d85a05f0
DJ
2853 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
2854 if (attr)
2855 set_cu_language (DW_UNSND (attr), &cu);
2856 else
2857 set_cu_language (language_minimal, &cu);
c906108c 2858
93311388 2859 /* Allocate a new partial symbol table structure. */
d85a05f0 2860 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 2861 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 2862 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
2863 /* TEXTLOW and TEXTHIGH are set below. */
2864 0,
2865 objfile->global_psymbols.next,
2866 objfile->static_psymbols.next);
72bf9492 2867
d85a05f0
DJ
2868 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
2869 if (attr != NULL)
2870 pst->dirname = DW_STRING (attr);
72bf9492 2871
e38df1d0 2872 pst->read_symtab_private = this_cu;
72bf9492 2873
93311388 2874 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 2875
93311388
DE
2876 /* Store the function that reads in the rest of the symbol table */
2877 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 2878
9291a0cd 2879 this_cu->v.psymtab = pst;
c906108c 2880
d85a05f0
DJ
2881 dwarf2_find_base_address (comp_unit_die, &cu);
2882
93311388
DE
2883 /* Possibly set the default values of LOWPC and HIGHPC from
2884 `DW_AT_ranges'. */
d85a05f0
DJ
2885 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
2886 &best_highpc, &cu, pst);
2887 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
2888 /* Store the contiguous range if it is not empty; it can be empty for
2889 CUs with no code. */
2890 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
2891 best_lowpc + baseaddr,
2892 best_highpc + baseaddr - 1, pst);
93311388
DE
2893
2894 /* Check if comp unit has_children.
2895 If so, read the rest of the partial symbols from this comp unit.
2896 If not, there's no more debug_info for this comp unit. */
d85a05f0 2897 if (has_children)
93311388
DE
2898 {
2899 struct partial_die_info *first_die;
2900 CORE_ADDR lowpc, highpc;
31ffec48 2901
93311388
DE
2902 lowpc = ((CORE_ADDR) -1);
2903 highpc = ((CORE_ADDR) 0);
c906108c 2904
93311388 2905 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 2906
93311388 2907 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 2908 ! has_pc_info, &cu);
57c22c6c 2909
93311388
DE
2910 /* If we didn't find a lowpc, set it to highpc to avoid
2911 complaints from `maint check'. */
2912 if (lowpc == ((CORE_ADDR) -1))
2913 lowpc = highpc;
10b3939b 2914
93311388
DE
2915 /* If the compilation unit didn't have an explicit address range,
2916 then use the information extracted from its child dies. */
d85a05f0 2917 if (! has_pc_info)
93311388 2918 {
d85a05f0
DJ
2919 best_lowpc = lowpc;
2920 best_highpc = highpc;
93311388
DE
2921 }
2922 }
d85a05f0
DJ
2923 pst->textlow = best_lowpc + baseaddr;
2924 pst->texthigh = best_highpc + baseaddr;
c906108c 2925
93311388
DE
2926 pst->n_global_syms = objfile->global_psymbols.next -
2927 (objfile->global_psymbols.list + pst->globals_offset);
2928 pst->n_static_syms = objfile->static_psymbols.next -
2929 (objfile->static_psymbols.list + pst->statics_offset);
2930 sort_pst_symbols (pst);
c906108c 2931
93311388
DE
2932 info_ptr = (beg_of_comp_unit + cu.header.length
2933 + cu.header.initial_length_size);
ae038cb0 2934
348e048f
DE
2935 if (this_cu->from_debug_types)
2936 {
2937 /* It's not clear we want to do anything with stmt lists here.
2938 Waiting to see what gcc ultimately does. */
2939 }
d85a05f0 2940 else
93311388
DE
2941 {
2942 /* Get the list of files included in the current compilation unit,
2943 and build a psymtab for each of them. */
d85a05f0 2944 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 2945 }
ae038cb0 2946
93311388 2947 do_cleanups (back_to_inner);
ae038cb0 2948
93311388
DE
2949 return info_ptr;
2950}
ff013f42 2951
348e048f
DE
2952/* Traversal function for htab_traverse_noresize.
2953 Process one .debug_types comp-unit. */
2954
2955static int
2956process_type_comp_unit (void **slot, void *info)
2957{
2958 struct signatured_type *entry = (struct signatured_type *) *slot;
2959 struct objfile *objfile = (struct objfile *) info;
2960 struct dwarf2_per_cu_data *this_cu;
2961
2962 this_cu = &entry->per_cu;
2963 this_cu->from_debug_types = 1;
2964
be391dca 2965 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
2966 process_psymtab_comp_unit (objfile, this_cu,
2967 dwarf2_per_objfile->types.buffer,
2968 dwarf2_per_objfile->types.buffer + entry->offset,
2969 dwarf2_per_objfile->types.size);
2970
2971 return 1;
2972}
2973
2974/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
2975 Build partial symbol tables for the .debug_types comp-units. */
2976
2977static void
2978build_type_psymtabs (struct objfile *objfile)
2979{
2980 if (! create_debug_types_hash_table (objfile))
2981 return;
2982
2983 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
2984 process_type_comp_unit, objfile);
2985}
2986
60606b2c
TT
2987/* A cleanup function that clears objfile's psymtabs_addrmap field. */
2988
2989static void
2990psymtabs_addrmap_cleanup (void *o)
2991{
2992 struct objfile *objfile = o;
ec61707d 2993
60606b2c
TT
2994 objfile->psymtabs_addrmap = NULL;
2995}
2996
93311388
DE
2997/* Build the partial symbol table by doing a quick pass through the
2998 .debug_info and .debug_abbrev sections. */
72bf9492 2999
93311388 3000static void
c67a9c90 3001dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3002{
93311388 3003 gdb_byte *info_ptr;
60606b2c
TT
3004 struct cleanup *back_to, *addrmap_cleanup;
3005 struct obstack temp_obstack;
93311388 3006
be391dca 3007 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3008 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3009
93311388
DE
3010 /* Any cached compilation units will be linked by the per-objfile
3011 read_in_chain. Make sure to free them when we're done. */
3012 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3013
348e048f
DE
3014 build_type_psymtabs (objfile);
3015
93311388 3016 create_all_comp_units (objfile);
c906108c 3017
60606b2c
TT
3018 /* Create a temporary address map on a temporary obstack. We later
3019 copy this to the final obstack. */
3020 obstack_init (&temp_obstack);
3021 make_cleanup_obstack_free (&temp_obstack);
3022 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3023 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3024
93311388
DE
3025 /* Since the objects we're extracting from .debug_info vary in
3026 length, only the individual functions to extract them (like
3027 read_comp_unit_head and load_partial_die) can really know whether
3028 the buffer is large enough to hold another complete object.
c906108c 3029
93311388
DE
3030 At the moment, they don't actually check that. If .debug_info
3031 holds just one extra byte after the last compilation unit's dies,
3032 then read_comp_unit_head will happily read off the end of the
3033 buffer. read_partial_die is similarly casual. Those functions
3034 should be fixed.
c906108c 3035
93311388
DE
3036 For this loop condition, simply checking whether there's any data
3037 left at all should be sufficient. */
c906108c 3038
93311388
DE
3039 while (info_ptr < (dwarf2_per_objfile->info.buffer
3040 + dwarf2_per_objfile->info.size))
3041 {
3042 struct dwarf2_per_cu_data *this_cu;
dd373385 3043
93311388
DE
3044 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3045 objfile);
aaa75496 3046
93311388
DE
3047 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3048 dwarf2_per_objfile->info.buffer,
3049 info_ptr,
3050 dwarf2_per_objfile->info.size);
c906108c 3051 }
ff013f42
JK
3052
3053 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3054 &objfile->objfile_obstack);
60606b2c 3055 discard_cleanups (addrmap_cleanup);
ff013f42 3056
ae038cb0
DJ
3057 do_cleanups (back_to);
3058}
3059
93311388 3060/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3061
3062static void
93311388
DE
3063load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3064 struct objfile *objfile)
ae038cb0
DJ
3065{
3066 bfd *abfd = objfile->obfd;
fe1b8b76 3067 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3068 struct die_info *comp_unit_die;
ae038cb0 3069 struct dwarf2_cu *cu;
ae038cb0 3070 struct cleanup *back_to;
d85a05f0
DJ
3071 struct attribute *attr;
3072 int has_children;
3073 struct die_reader_specs reader_specs;
ae038cb0 3074
348e048f
DE
3075 gdb_assert (! this_cu->from_debug_types);
3076
be391dca 3077 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3078 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3079 beg_of_comp_unit = info_ptr;
3080
93311388 3081 cu = alloc_one_comp_unit (objfile);
ae038cb0 3082
93311388 3083 /* ??? Missing cleanup for CU? */
ae038cb0 3084
328c9494
DJ
3085 /* Link this compilation unit into the compilation unit tree. */
3086 this_cu->cu = cu;
3087 cu->per_cu = this_cu;
3088 cu->type_hash = this_cu->type_hash;
3089
93311388
DE
3090 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3091 dwarf2_per_objfile->info.buffer,
3092 dwarf2_per_objfile->info.size,
3093 abfd);
ae038cb0
DJ
3094
3095 /* Complete the cu_header. */
93311388 3096 cu->header.offset = this_cu->offset;
d00adf39 3097 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
ae038cb0
DJ
3098
3099 /* Read the abbrevs for this compilation unit into a table. */
3100 dwarf2_read_abbrevs (abfd, cu);
3101 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3102
3103 /* Read the compilation unit die. */
d85a05f0
DJ
3104 init_cu_die_reader (&reader_specs, cu);
3105 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3106 &has_children);
ae038cb0
DJ
3107
3108 /* Set the language we're debugging. */
d85a05f0
DJ
3109 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3110 if (attr)
3111 set_cu_language (DW_UNSND (attr), cu);
3112 else
3113 set_cu_language (language_minimal, cu);
ae038cb0 3114
ae038cb0
DJ
3115 /* Check if comp unit has_children.
3116 If so, read the rest of the partial symbols from this comp unit.
3117 If not, there's no more debug_info for this comp unit. */
d85a05f0 3118 if (has_children)
93311388 3119 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0
DJ
3120
3121 do_cleanups (back_to);
3122}
3123
3124/* Create a list of all compilation units in OBJFILE. We do this only
3125 if an inter-comp-unit reference is found; presumably if there is one,
3126 there will be many, and one will occur early in the .debug_info section.
3127 So there's no point in building this list incrementally. */
3128
3129static void
3130create_all_comp_units (struct objfile *objfile)
3131{
3132 int n_allocated;
3133 int n_comp_units;
3134 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3135 gdb_byte *info_ptr;
3136
3137 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3138 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3139
3140 n_comp_units = 0;
3141 n_allocated = 10;
3142 all_comp_units = xmalloc (n_allocated
3143 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3144
dce234bc 3145 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3146 {
c764a876 3147 unsigned int length, initial_length_size;
ae038cb0 3148 struct dwarf2_per_cu_data *this_cu;
c764a876 3149 unsigned int offset;
ae038cb0 3150
dce234bc 3151 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3152
3153 /* Read just enough information to find out where the next
3154 compilation unit is. */
c764a876
DE
3155 length = read_initial_length (objfile->obfd, info_ptr,
3156 &initial_length_size);
ae038cb0
DJ
3157
3158 /* Save the compilation unit for later lookup. */
3159 this_cu = obstack_alloc (&objfile->objfile_obstack,
3160 sizeof (struct dwarf2_per_cu_data));
3161 memset (this_cu, 0, sizeof (*this_cu));
3162 this_cu->offset = offset;
c764a876 3163 this_cu->length = length + initial_length_size;
9291a0cd 3164 this_cu->objfile = objfile;
ae038cb0
DJ
3165
3166 if (n_comp_units == n_allocated)
3167 {
3168 n_allocated *= 2;
3169 all_comp_units = xrealloc (all_comp_units,
3170 n_allocated
3171 * sizeof (struct dwarf2_per_cu_data *));
3172 }
3173 all_comp_units[n_comp_units++] = this_cu;
3174
3175 info_ptr = info_ptr + this_cu->length;
3176 }
3177
3178 dwarf2_per_objfile->all_comp_units
3179 = obstack_alloc (&objfile->objfile_obstack,
3180 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3181 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3182 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3183 xfree (all_comp_units);
3184 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3185}
3186
5734ee8b
DJ
3187/* Process all loaded DIEs for compilation unit CU, starting at
3188 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3189 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3190 DW_AT_ranges). If NEED_PC is set, then this function will set
3191 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3192 and record the covered ranges in the addrmap. */
c906108c 3193
72bf9492
DJ
3194static void
3195scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3196 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3197{
72bf9492 3198 struct partial_die_info *pdi;
c906108c 3199
91c24f0a
DC
3200 /* Now, march along the PDI's, descending into ones which have
3201 interesting children but skipping the children of the other ones,
3202 until we reach the end of the compilation unit. */
c906108c 3203
72bf9492 3204 pdi = first_die;
91c24f0a 3205
72bf9492
DJ
3206 while (pdi != NULL)
3207 {
3208 fixup_partial_die (pdi, cu);
c906108c 3209
f55ee35c 3210 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3211 children, so we need to look at them. Ditto for anonymous
3212 enums. */
933c6fe4 3213
72bf9492 3214 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3215 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3216 {
72bf9492 3217 switch (pdi->tag)
c906108c
SS
3218 {
3219 case DW_TAG_subprogram:
5734ee8b 3220 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3221 break;
3222 case DW_TAG_variable:
3223 case DW_TAG_typedef:
91c24f0a 3224 case DW_TAG_union_type:
72bf9492 3225 if (!pdi->is_declaration)
63d06c5c 3226 {
72bf9492 3227 add_partial_symbol (pdi, cu);
63d06c5c
DC
3228 }
3229 break;
c906108c 3230 case DW_TAG_class_type:
680b30c7 3231 case DW_TAG_interface_type:
c906108c 3232 case DW_TAG_structure_type:
72bf9492 3233 if (!pdi->is_declaration)
c906108c 3234 {
72bf9492 3235 add_partial_symbol (pdi, cu);
c906108c
SS
3236 }
3237 break;
91c24f0a 3238 case DW_TAG_enumeration_type:
72bf9492
DJ
3239 if (!pdi->is_declaration)
3240 add_partial_enumeration (pdi, cu);
c906108c
SS
3241 break;
3242 case DW_TAG_base_type:
a02abb62 3243 case DW_TAG_subrange_type:
c906108c 3244 /* File scope base type definitions are added to the partial
c5aa993b 3245 symbol table. */
72bf9492 3246 add_partial_symbol (pdi, cu);
c906108c 3247 break;
d9fa45fe 3248 case DW_TAG_namespace:
5734ee8b 3249 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3250 break;
5d7cb8df
JK
3251 case DW_TAG_module:
3252 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3253 break;
c906108c
SS
3254 default:
3255 break;
3256 }
3257 }
3258
72bf9492
DJ
3259 /* If the die has a sibling, skip to the sibling. */
3260
3261 pdi = pdi->die_sibling;
3262 }
3263}
3264
3265/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3266
72bf9492 3267 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3268 name is concatenated with "::" and the partial DIE's name. For
3269 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3270 Enumerators are an exception; they use the scope of their parent
3271 enumeration type, i.e. the name of the enumeration type is not
3272 prepended to the enumerator.
91c24f0a 3273
72bf9492
DJ
3274 There are two complexities. One is DW_AT_specification; in this
3275 case "parent" means the parent of the target of the specification,
3276 instead of the direct parent of the DIE. The other is compilers
3277 which do not emit DW_TAG_namespace; in this case we try to guess
3278 the fully qualified name of structure types from their members'
3279 linkage names. This must be done using the DIE's children rather
3280 than the children of any DW_AT_specification target. We only need
3281 to do this for structures at the top level, i.e. if the target of
3282 any DW_AT_specification (if any; otherwise the DIE itself) does not
3283 have a parent. */
3284
3285/* Compute the scope prefix associated with PDI's parent, in
3286 compilation unit CU. The result will be allocated on CU's
3287 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3288 field. NULL is returned if no prefix is necessary. */
3289static char *
3290partial_die_parent_scope (struct partial_die_info *pdi,
3291 struct dwarf2_cu *cu)
3292{
3293 char *grandparent_scope;
3294 struct partial_die_info *parent, *real_pdi;
91c24f0a 3295
72bf9492
DJ
3296 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3297 then this means the parent of the specification DIE. */
3298
3299 real_pdi = pdi;
72bf9492 3300 while (real_pdi->has_specification)
10b3939b 3301 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3302
3303 parent = real_pdi->die_parent;
3304 if (parent == NULL)
3305 return NULL;
3306
3307 if (parent->scope_set)
3308 return parent->scope;
3309
3310 fixup_partial_die (parent, cu);
3311
10b3939b 3312 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3313
acebe513
UW
3314 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3315 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3316 Work around this problem here. */
3317 if (cu->language == language_cplus
6e70227d 3318 && parent->tag == DW_TAG_namespace
acebe513
UW
3319 && strcmp (parent->name, "::") == 0
3320 && grandparent_scope == NULL)
3321 {
3322 parent->scope = NULL;
3323 parent->scope_set = 1;
3324 return NULL;
3325 }
3326
72bf9492 3327 if (parent->tag == DW_TAG_namespace
f55ee35c 3328 || parent->tag == DW_TAG_module
72bf9492
DJ
3329 || parent->tag == DW_TAG_structure_type
3330 || parent->tag == DW_TAG_class_type
680b30c7 3331 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3332 || parent->tag == DW_TAG_union_type
3333 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3334 {
3335 if (grandparent_scope == NULL)
3336 parent->scope = parent->name;
3337 else
987504bb 3338 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3339 parent->name, 0, cu);
72bf9492 3340 }
ceeb3d5a 3341 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3342 /* Enumerators should not get the name of the enumeration as a prefix. */
3343 parent->scope = grandparent_scope;
3344 else
3345 {
3346 /* FIXME drow/2004-04-01: What should we be doing with
3347 function-local names? For partial symbols, we should probably be
3348 ignoring them. */
3349 complaint (&symfile_complaints,
e2e0b3e5 3350 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3351 parent->tag, pdi->offset);
3352 parent->scope = grandparent_scope;
c906108c
SS
3353 }
3354
72bf9492
DJ
3355 parent->scope_set = 1;
3356 return parent->scope;
3357}
3358
3359/* Return the fully scoped name associated with PDI, from compilation unit
3360 CU. The result will be allocated with malloc. */
3361static char *
3362partial_die_full_name (struct partial_die_info *pdi,
3363 struct dwarf2_cu *cu)
3364{
3365 char *parent_scope;
3366
3367 parent_scope = partial_die_parent_scope (pdi, cu);
3368 if (parent_scope == NULL)
3369 return NULL;
3370 else
f55ee35c 3371 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3372}
3373
3374static void
72bf9492 3375add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3376{
e7c27a73 3377 struct objfile *objfile = cu->objfile;
c906108c 3378 CORE_ADDR addr = 0;
decbce07 3379 char *actual_name = NULL;
5c4e30ca 3380 const struct partial_symbol *psym = NULL;
e142c38c 3381 CORE_ADDR baseaddr;
72bf9492 3382 int built_actual_name = 0;
e142c38c
DJ
3383
3384 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3385
94af9270
KS
3386 actual_name = partial_die_full_name (pdi, cu);
3387 if (actual_name)
3388 built_actual_name = 1;
63d06c5c 3389
72bf9492
DJ
3390 if (actual_name == NULL)
3391 actual_name = pdi->name;
3392
c906108c
SS
3393 switch (pdi->tag)
3394 {
3395 case DW_TAG_subprogram:
2cfa0c8d 3396 if (pdi->is_external || cu->language == language_ada)
c906108c 3397 {
2cfa0c8d
JB
3398 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3399 of the global scope. But in Ada, we want to be able to access
3400 nested procedures globally. So all Ada subprograms are stored
3401 in the global scope. */
38d518c9 3402 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3403 mst_text, objfile); */
38d518c9 3404 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3405 built_actual_name,
5c4e30ca
DC
3406 VAR_DOMAIN, LOC_BLOCK,
3407 &objfile->global_psymbols,
3408 0, pdi->lowpc + baseaddr,
e142c38c 3409 cu->language, objfile);
c906108c
SS
3410 }
3411 else
3412 {
38d518c9 3413 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3414 mst_file_text, objfile); */
38d518c9 3415 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3416 built_actual_name,
5c4e30ca
DC
3417 VAR_DOMAIN, LOC_BLOCK,
3418 &objfile->static_psymbols,
3419 0, pdi->lowpc + baseaddr,
e142c38c 3420 cu->language, objfile);
c906108c
SS
3421 }
3422 break;
3423 case DW_TAG_variable:
3424 if (pdi->is_external)
3425 {
3426 /* Global Variable.
3427 Don't enter into the minimal symbol tables as there is
3428 a minimal symbol table entry from the ELF symbols already.
3429 Enter into partial symbol table if it has a location
3430 descriptor or a type.
3431 If the location descriptor is missing, new_symbol will create
3432 a LOC_UNRESOLVED symbol, the address of the variable will then
3433 be determined from the minimal symbol table whenever the variable
3434 is referenced.
3435 The address for the partial symbol table entry is not
3436 used by GDB, but it comes in handy for debugging partial symbol
3437 table building. */
3438
3439 if (pdi->locdesc)
e7c27a73 3440 addr = decode_locdesc (pdi->locdesc, cu);
c906108c 3441 if (pdi->locdesc || pdi->has_type)
38d518c9 3442 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3443 built_actual_name,
5c4e30ca
DC
3444 VAR_DOMAIN, LOC_STATIC,
3445 &objfile->global_psymbols,
3446 0, addr + baseaddr,
e142c38c 3447 cu->language, objfile);
c906108c
SS
3448 }
3449 else
3450 {
3451 /* Static Variable. Skip symbols without location descriptors. */
3452 if (pdi->locdesc == NULL)
decbce07
MS
3453 {
3454 if (built_actual_name)
3455 xfree (actual_name);
3456 return;
3457 }
e7c27a73 3458 addr = decode_locdesc (pdi->locdesc, cu);
38d518c9 3459 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3460 mst_file_data, objfile); */
38d518c9 3461 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3462 built_actual_name,
5c4e30ca
DC
3463 VAR_DOMAIN, LOC_STATIC,
3464 &objfile->static_psymbols,
3465 0, addr + baseaddr,
e142c38c 3466 cu->language, objfile);
c906108c
SS
3467 }
3468 break;
3469 case DW_TAG_typedef:
3470 case DW_TAG_base_type:
a02abb62 3471 case DW_TAG_subrange_type:
38d518c9 3472 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3473 built_actual_name,
176620f1 3474 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3475 &objfile->static_psymbols,
e142c38c 3476 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3477 break;
72bf9492
DJ
3478 case DW_TAG_namespace:
3479 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3480 built_actual_name,
72bf9492
DJ
3481 VAR_DOMAIN, LOC_TYPEDEF,
3482 &objfile->global_psymbols,
3483 0, (CORE_ADDR) 0, cu->language, objfile);
3484 break;
c906108c 3485 case DW_TAG_class_type:
680b30c7 3486 case DW_TAG_interface_type:
c906108c
SS
3487 case DW_TAG_structure_type:
3488 case DW_TAG_union_type:
3489 case DW_TAG_enumeration_type:
fa4028e9
JB
3490 /* Skip external references. The DWARF standard says in the section
3491 about "Structure, Union, and Class Type Entries": "An incomplete
3492 structure, union or class type is represented by a structure,
3493 union or class entry that does not have a byte size attribute
3494 and that has a DW_AT_declaration attribute." */
3495 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3496 {
3497 if (built_actual_name)
3498 xfree (actual_name);
3499 return;
3500 }
fa4028e9 3501
63d06c5c
DC
3502 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3503 static vs. global. */
38d518c9 3504 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3505 built_actual_name,
176620f1 3506 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3507 (cu->language == language_cplus
3508 || cu->language == language_java)
63d06c5c
DC
3509 ? &objfile->global_psymbols
3510 : &objfile->static_psymbols,
e142c38c 3511 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3512
c906108c
SS
3513 break;
3514 case DW_TAG_enumerator:
38d518c9 3515 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3516 built_actual_name,
176620f1 3517 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3518 (cu->language == language_cplus
3519 || cu->language == language_java)
f6fe98ef
DJ
3520 ? &objfile->global_psymbols
3521 : &objfile->static_psymbols,
e142c38c 3522 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3523 break;
3524 default:
3525 break;
3526 }
5c4e30ca 3527
72bf9492
DJ
3528 if (built_actual_name)
3529 xfree (actual_name);
c906108c
SS
3530}
3531
5c4e30ca
DC
3532/* Read a partial die corresponding to a namespace; also, add a symbol
3533 corresponding to that namespace to the symbol table. NAMESPACE is
3534 the name of the enclosing namespace. */
91c24f0a 3535
72bf9492
DJ
3536static void
3537add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3538 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3539 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3540{
72bf9492 3541 /* Add a symbol for the namespace. */
e7c27a73 3542
72bf9492 3543 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3544
3545 /* Now scan partial symbols in that namespace. */
3546
91c24f0a 3547 if (pdi->has_children)
5734ee8b 3548 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3549}
3550
5d7cb8df
JK
3551/* Read a partial die corresponding to a Fortran module. */
3552
3553static void
3554add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3555 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3556{
f55ee35c 3557 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3558
3559 if (pdi->has_children)
3560 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3561}
3562
bc30ff58
JB
3563/* Read a partial die corresponding to a subprogram and create a partial
3564 symbol for that subprogram. When the CU language allows it, this
3565 routine also defines a partial symbol for each nested subprogram
3566 that this subprogram contains.
6e70227d 3567
bc30ff58
JB
3568 DIE my also be a lexical block, in which case we simply search
3569 recursively for suprograms defined inside that lexical block.
3570 Again, this is only performed when the CU language allows this
3571 type of definitions. */
3572
3573static void
3574add_partial_subprogram (struct partial_die_info *pdi,
3575 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3576 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3577{
3578 if (pdi->tag == DW_TAG_subprogram)
3579 {
3580 if (pdi->has_pc_info)
3581 {
3582 if (pdi->lowpc < *lowpc)
3583 *lowpc = pdi->lowpc;
3584 if (pdi->highpc > *highpc)
3585 *highpc = pdi->highpc;
5734ee8b
DJ
3586 if (need_pc)
3587 {
3588 CORE_ADDR baseaddr;
3589 struct objfile *objfile = cu->objfile;
3590
3591 baseaddr = ANOFFSET (objfile->section_offsets,
3592 SECT_OFF_TEXT (objfile));
3593 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3594 pdi->lowpc + baseaddr,
3595 pdi->highpc - 1 + baseaddr,
9291a0cd 3596 cu->per_cu->v.psymtab);
5734ee8b 3597 }
bc30ff58 3598 if (!pdi->is_declaration)
e8d05480
JB
3599 /* Ignore subprogram DIEs that do not have a name, they are
3600 illegal. Do not emit a complaint at this point, we will
3601 do so when we convert this psymtab into a symtab. */
3602 if (pdi->name)
3603 add_partial_symbol (pdi, cu);
bc30ff58
JB
3604 }
3605 }
6e70227d 3606
bc30ff58
JB
3607 if (! pdi->has_children)
3608 return;
3609
3610 if (cu->language == language_ada)
3611 {
3612 pdi = pdi->die_child;
3613 while (pdi != NULL)
3614 {
3615 fixup_partial_die (pdi, cu);
3616 if (pdi->tag == DW_TAG_subprogram
3617 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3618 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3619 pdi = pdi->die_sibling;
3620 }
3621 }
3622}
3623
72bf9492
DJ
3624/* See if we can figure out if the class lives in a namespace. We do
3625 this by looking for a member function; its demangled name will
3626 contain namespace info, if there is any. */
63d06c5c 3627
72bf9492
DJ
3628static void
3629guess_structure_name (struct partial_die_info *struct_pdi,
3630 struct dwarf2_cu *cu)
63d06c5c 3631{
987504bb
JJ
3632 if ((cu->language == language_cplus
3633 || cu->language == language_java)
72bf9492 3634 && cu->has_namespace_info == 0
63d06c5c
DC
3635 && struct_pdi->has_children)
3636 {
63d06c5c
DC
3637 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3638 what template types look like, because the demangler
3639 frequently doesn't give the same name as the debug info. We
3640 could fix this by only using the demangled name to get the
134d01f1 3641 prefix (but see comment in read_structure_type). */
63d06c5c 3642
72bf9492 3643 struct partial_die_info *real_pdi;
5d51ca54 3644
72bf9492
DJ
3645 /* If this DIE (this DIE's specification, if any) has a parent, then
3646 we should not do this. We'll prepend the parent's fully qualified
3647 name when we create the partial symbol. */
5d51ca54 3648
72bf9492 3649 real_pdi = struct_pdi;
72bf9492 3650 while (real_pdi->has_specification)
10b3939b 3651 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3652
72bf9492
DJ
3653 if (real_pdi->die_parent != NULL)
3654 return;
63d06c5c 3655 }
63d06c5c
DC
3656}
3657
91c24f0a
DC
3658/* Read a partial die corresponding to an enumeration type. */
3659
72bf9492
DJ
3660static void
3661add_partial_enumeration (struct partial_die_info *enum_pdi,
3662 struct dwarf2_cu *cu)
91c24f0a 3663{
72bf9492 3664 struct partial_die_info *pdi;
91c24f0a
DC
3665
3666 if (enum_pdi->name != NULL)
72bf9492
DJ
3667 add_partial_symbol (enum_pdi, cu);
3668
3669 pdi = enum_pdi->die_child;
3670 while (pdi)
91c24f0a 3671 {
72bf9492 3672 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3673 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3674 else
72bf9492
DJ
3675 add_partial_symbol (pdi, cu);
3676 pdi = pdi->die_sibling;
91c24f0a 3677 }
91c24f0a
DC
3678}
3679
4bb7a0a7
DJ
3680/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3681 Return the corresponding abbrev, or NULL if the number is zero (indicating
3682 an empty DIE). In either case *BYTES_READ will be set to the length of
3683 the initial number. */
3684
3685static struct abbrev_info *
fe1b8b76 3686peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3687 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3688{
3689 bfd *abfd = cu->objfile->obfd;
3690 unsigned int abbrev_number;
3691 struct abbrev_info *abbrev;
3692
3693 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3694
3695 if (abbrev_number == 0)
3696 return NULL;
3697
3698 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3699 if (!abbrev)
3700 {
8a3fe4f8 3701 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3702 bfd_get_filename (abfd));
3703 }
3704
3705 return abbrev;
3706}
3707
93311388
DE
3708/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3709 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3710 DIE. Any children of the skipped DIEs will also be skipped. */
3711
fe1b8b76 3712static gdb_byte *
93311388 3713skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3714{
3715 struct abbrev_info *abbrev;
3716 unsigned int bytes_read;
3717
3718 while (1)
3719 {
3720 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3721 if (abbrev == NULL)
3722 return info_ptr + bytes_read;
3723 else
93311388 3724 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3725 }
3726}
3727
93311388
DE
3728/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3729 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3730 abbrev corresponding to that skipped uleb128 should be passed in
3731 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3732 children. */
3733
fe1b8b76 3734static gdb_byte *
93311388
DE
3735skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3736 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3737{
3738 unsigned int bytes_read;
3739 struct attribute attr;
3740 bfd *abfd = cu->objfile->obfd;
3741 unsigned int form, i;
3742
3743 for (i = 0; i < abbrev->num_attrs; i++)
3744 {
3745 /* The only abbrev we care about is DW_AT_sibling. */
3746 if (abbrev->attrs[i].name == DW_AT_sibling)
3747 {
3748 read_attribute (&attr, &abbrev->attrs[i],
3749 abfd, info_ptr, cu);
3750 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 3751 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 3752 else
93311388 3753 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
3754 }
3755
3756 /* If it isn't DW_AT_sibling, skip this attribute. */
3757 form = abbrev->attrs[i].form;
3758 skip_attribute:
3759 switch (form)
3760 {
4bb7a0a7 3761 case DW_FORM_ref_addr:
ae411497
TT
3762 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
3763 and later it is offset sized. */
3764 if (cu->header.version == 2)
3765 info_ptr += cu->header.addr_size;
3766 else
3767 info_ptr += cu->header.offset_size;
3768 break;
3769 case DW_FORM_addr:
4bb7a0a7
DJ
3770 info_ptr += cu->header.addr_size;
3771 break;
3772 case DW_FORM_data1:
3773 case DW_FORM_ref1:
3774 case DW_FORM_flag:
3775 info_ptr += 1;
3776 break;
2dc7f7b3
TT
3777 case DW_FORM_flag_present:
3778 break;
4bb7a0a7
DJ
3779 case DW_FORM_data2:
3780 case DW_FORM_ref2:
3781 info_ptr += 2;
3782 break;
3783 case DW_FORM_data4:
3784 case DW_FORM_ref4:
3785 info_ptr += 4;
3786 break;
3787 case DW_FORM_data8:
3788 case DW_FORM_ref8:
348e048f 3789 case DW_FORM_sig8:
4bb7a0a7
DJ
3790 info_ptr += 8;
3791 break;
3792 case DW_FORM_string:
3793 read_string (abfd, info_ptr, &bytes_read);
3794 info_ptr += bytes_read;
3795 break;
2dc7f7b3 3796 case DW_FORM_sec_offset:
4bb7a0a7
DJ
3797 case DW_FORM_strp:
3798 info_ptr += cu->header.offset_size;
3799 break;
2dc7f7b3 3800 case DW_FORM_exprloc:
4bb7a0a7
DJ
3801 case DW_FORM_block:
3802 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3803 info_ptr += bytes_read;
3804 break;
3805 case DW_FORM_block1:
3806 info_ptr += 1 + read_1_byte (abfd, info_ptr);
3807 break;
3808 case DW_FORM_block2:
3809 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
3810 break;
3811 case DW_FORM_block4:
3812 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
3813 break;
3814 case DW_FORM_sdata:
3815 case DW_FORM_udata:
3816 case DW_FORM_ref_udata:
3817 info_ptr = skip_leb128 (abfd, info_ptr);
3818 break;
3819 case DW_FORM_indirect:
3820 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3821 info_ptr += bytes_read;
3822 /* We need to continue parsing from here, so just go back to
3823 the top. */
3824 goto skip_attribute;
3825
3826 default:
8a3fe4f8 3827 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
3828 dwarf_form_name (form),
3829 bfd_get_filename (abfd));
3830 }
3831 }
3832
3833 if (abbrev->has_children)
93311388 3834 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
3835 else
3836 return info_ptr;
3837}
3838
93311388
DE
3839/* Locate ORIG_PDI's sibling.
3840 INFO_PTR should point to the start of the next DIE after ORIG_PDI
3841 in BUFFER. */
91c24f0a 3842
fe1b8b76 3843static gdb_byte *
93311388
DE
3844locate_pdi_sibling (struct partial_die_info *orig_pdi,
3845 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 3846 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
3847{
3848 /* Do we know the sibling already? */
72bf9492 3849
91c24f0a
DC
3850 if (orig_pdi->sibling)
3851 return orig_pdi->sibling;
3852
3853 /* Are there any children to deal with? */
3854
3855 if (!orig_pdi->has_children)
3856 return info_ptr;
3857
4bb7a0a7 3858 /* Skip the children the long way. */
91c24f0a 3859
93311388 3860 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
3861}
3862
c906108c
SS
3863/* Expand this partial symbol table into a full symbol table. */
3864
3865static void
fba45db2 3866dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 3867{
c906108c
SS
3868 if (pst != NULL)
3869 {
3870 if (pst->readin)
3871 {
8a3fe4f8 3872 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
3873 }
3874 else
3875 {
3876 if (info_verbose)
3877 {
a3f17187 3878 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
3879 gdb_flush (gdb_stdout);
3880 }
3881
10b3939b
DJ
3882 /* Restore our global data. */
3883 dwarf2_per_objfile = objfile_data (pst->objfile,
3884 dwarf2_objfile_data_key);
3885
b2ab525c
KB
3886 /* If this psymtab is constructed from a debug-only objfile, the
3887 has_section_at_zero flag will not necessarily be correct. We
3888 can get the correct value for this flag by looking at the data
3889 associated with the (presumably stripped) associated objfile. */
3890 if (pst->objfile->separate_debug_objfile_backlink)
3891 {
3892 struct dwarf2_per_objfile *dpo_backlink
3893 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
3894 dwarf2_objfile_data_key);
9a619af0 3895
b2ab525c
KB
3896 dwarf2_per_objfile->has_section_at_zero
3897 = dpo_backlink->has_section_at_zero;
3898 }
3899
c906108c
SS
3900 psymtab_to_symtab_1 (pst);
3901
3902 /* Finish up the debug error message. */
3903 if (info_verbose)
a3f17187 3904 printf_filtered (_("done.\n"));
c906108c
SS
3905 }
3906 }
3907}
3908
10b3939b
DJ
3909/* Add PER_CU to the queue. */
3910
3911static void
03dd20cc 3912queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
3913{
3914 struct dwarf2_queue_item *item;
3915
3916 per_cu->queued = 1;
3917 item = xmalloc (sizeof (*item));
3918 item->per_cu = per_cu;
3919 item->next = NULL;
3920
3921 if (dwarf2_queue == NULL)
3922 dwarf2_queue = item;
3923 else
3924 dwarf2_queue_tail->next = item;
3925
3926 dwarf2_queue_tail = item;
3927}
3928
3929/* Process the queue. */
3930
3931static void
3932process_queue (struct objfile *objfile)
3933{
3934 struct dwarf2_queue_item *item, *next_item;
3935
03dd20cc
DJ
3936 /* The queue starts out with one item, but following a DIE reference
3937 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
3938 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
3939 {
9291a0cd
TT
3940 if (dwarf2_per_objfile->using_index
3941 ? !item->per_cu->v.quick->symtab
3942 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
3943 process_full_comp_unit (item->per_cu);
3944
3945 item->per_cu->queued = 0;
3946 next_item = item->next;
3947 xfree (item);
3948 }
3949
3950 dwarf2_queue_tail = NULL;
3951}
3952
3953/* Free all allocated queue entries. This function only releases anything if
3954 an error was thrown; if the queue was processed then it would have been
3955 freed as we went along. */
3956
3957static void
3958dwarf2_release_queue (void *dummy)
3959{
3960 struct dwarf2_queue_item *item, *last;
3961
3962 item = dwarf2_queue;
3963 while (item)
3964 {
3965 /* Anything still marked queued is likely to be in an
3966 inconsistent state, so discard it. */
3967 if (item->per_cu->queued)
3968 {
3969 if (item->per_cu->cu != NULL)
3970 free_one_cached_comp_unit (item->per_cu->cu);
3971 item->per_cu->queued = 0;
3972 }
3973
3974 last = item;
3975 item = item->next;
3976 xfree (last);
3977 }
3978
3979 dwarf2_queue = dwarf2_queue_tail = NULL;
3980}
3981
3982/* Read in full symbols for PST, and anything it depends on. */
3983
c906108c 3984static void
fba45db2 3985psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 3986{
10b3939b 3987 struct dwarf2_per_cu_data *per_cu;
c906108c 3988 struct cleanup *back_to;
aaa75496
JB
3989 int i;
3990
3991 for (i = 0; i < pst->number_of_dependencies; i++)
3992 if (!pst->dependencies[i]->readin)
3993 {
3994 /* Inform about additional files that need to be read in. */
3995 if (info_verbose)
3996 {
a3f17187 3997 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
3998 fputs_filtered (" ", gdb_stdout);
3999 wrap_here ("");
4000 fputs_filtered ("and ", gdb_stdout);
4001 wrap_here ("");
4002 printf_filtered ("%s...", pst->dependencies[i]->filename);
4003 wrap_here (""); /* Flush output */
4004 gdb_flush (gdb_stdout);
4005 }
4006 psymtab_to_symtab_1 (pst->dependencies[i]);
4007 }
4008
e38df1d0 4009 per_cu = pst->read_symtab_private;
10b3939b
DJ
4010
4011 if (per_cu == NULL)
aaa75496
JB
4012 {
4013 /* It's an include file, no symbols to read for it.
4014 Everything is in the parent symtab. */
4015 pst->readin = 1;
4016 return;
4017 }
c906108c 4018
9291a0cd 4019 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4020}
4021
93311388 4022/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4023
93311388 4024static void
31ffec48 4025load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4026{
31ffec48 4027 bfd *abfd = objfile->obfd;
10b3939b 4028 struct dwarf2_cu *cu;
c764a876 4029 unsigned int offset;
93311388 4030 gdb_byte *info_ptr, *beg_of_comp_unit;
10b3939b
DJ
4031 struct cleanup *back_to, *free_cu_cleanup;
4032 struct attribute *attr;
6502dd73 4033
348e048f
DE
4034 gdb_assert (! per_cu->from_debug_types);
4035
c906108c 4036 /* Set local variables from the partial symbol table info. */
10b3939b 4037 offset = per_cu->offset;
6502dd73 4038
be391dca 4039 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4040 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4041 beg_of_comp_unit = info_ptr;
63d06c5c 4042
93311388 4043 cu = alloc_one_comp_unit (objfile);
c906108c 4044
10b3939b
DJ
4045 /* If an error occurs while loading, release our storage. */
4046 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4047
93311388 4048 /* Read in the comp_unit header. */
10b3939b 4049 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4050
93311388
DE
4051 /* Complete the cu_header. */
4052 cu->header.offset = offset;
4053 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4054
4055 /* Read the abbrevs for this compilation unit. */
10b3939b
DJ
4056 dwarf2_read_abbrevs (abfd, cu);
4057 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
4058
93311388 4059 /* Link this compilation unit into the compilation unit tree. */
10b3939b 4060 per_cu->cu = cu;
93311388 4061 cu->per_cu = per_cu;
f792889a 4062 cu->type_hash = per_cu->type_hash;
e142c38c 4063
93311388 4064 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4065
4066 /* We try not to read any attributes in this function, because not
4067 all objfiles needed for references have been loaded yet, and symbol
4068 table processing isn't initialized. But we have to set the CU language,
4069 or we won't be able to build types correctly. */
4070 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4071 if (attr)
4072 set_cu_language (DW_UNSND (attr), cu);
4073 else
4074 set_cu_language (language_minimal, cu);
4075
a6c727b2
DJ
4076 /* Similarly, if we do not read the producer, we can not apply
4077 producer-specific interpretation. */
4078 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4079 if (attr)
4080 cu->producer = DW_STRING (attr);
4081
348e048f
DE
4082 /* Link this CU into read_in_chain. */
4083 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4084 dwarf2_per_objfile->read_in_chain = per_cu;
4085
10b3939b 4086 do_cleanups (back_to);
e142c38c 4087
10b3939b
DJ
4088 /* We've successfully allocated this compilation unit. Let our caller
4089 clean it up when finished with it. */
4090 discard_cleanups (free_cu_cleanup);
10b3939b
DJ
4091}
4092
4093/* Generate full symbol information for PST and CU, whose DIEs have
4094 already been loaded into memory. */
4095
4096static void
4097process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4098{
10b3939b 4099 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4100 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4101 CORE_ADDR lowpc, highpc;
4102 struct symtab *symtab;
4103 struct cleanup *back_to;
10b3939b
DJ
4104 CORE_ADDR baseaddr;
4105
4106 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4107
10b3939b
DJ
4108 buildsym_init ();
4109 back_to = make_cleanup (really_free_pendings, NULL);
4110
4111 cu->list_in_scope = &file_symbols;
c906108c 4112
d85a05f0 4113 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4114
c906108c 4115 /* Do line number decoding in read_file_scope () */
10b3939b 4116 process_die (cu->dies, cu);
c906108c 4117
fae299cd
DC
4118 /* Some compilers don't define a DW_AT_high_pc attribute for the
4119 compilation unit. If the DW_AT_high_pc is missing, synthesize
4120 it, by scanning the DIE's below the compilation unit. */
10b3939b 4121 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4122
613e1657 4123 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4124
4125 /* Set symtab language to language from DW_AT_language.
4126 If the compilation is from a C file generated by language preprocessors,
4127 do not set the language if it was already deduced by start_subfile. */
4128 if (symtab != NULL
10b3939b 4129 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4130 {
10b3939b 4131 symtab->language = cu->language;
c906108c 4132 }
9291a0cd
TT
4133
4134 if (dwarf2_per_objfile->using_index)
4135 per_cu->v.quick->symtab = symtab;
4136 else
4137 {
4138 struct partial_symtab *pst = per_cu->v.psymtab;
4139 pst->symtab = symtab;
4140 pst->readin = 1;
4141 }
c906108c
SS
4142
4143 do_cleanups (back_to);
4144}
4145
4146/* Process a die and its children. */
4147
4148static void
e7c27a73 4149process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4150{
4151 switch (die->tag)
4152 {
4153 case DW_TAG_padding:
4154 break;
4155 case DW_TAG_compile_unit:
e7c27a73 4156 read_file_scope (die, cu);
c906108c 4157 break;
348e048f
DE
4158 case DW_TAG_type_unit:
4159 read_type_unit_scope (die, cu);
4160 break;
c906108c 4161 case DW_TAG_subprogram:
c906108c 4162 case DW_TAG_inlined_subroutine:
edb3359d 4163 read_func_scope (die, cu);
c906108c
SS
4164 break;
4165 case DW_TAG_lexical_block:
14898363
L
4166 case DW_TAG_try_block:
4167 case DW_TAG_catch_block:
e7c27a73 4168 read_lexical_block_scope (die, cu);
c906108c
SS
4169 break;
4170 case DW_TAG_class_type:
680b30c7 4171 case DW_TAG_interface_type:
c906108c
SS
4172 case DW_TAG_structure_type:
4173 case DW_TAG_union_type:
134d01f1 4174 process_structure_scope (die, cu);
c906108c
SS
4175 break;
4176 case DW_TAG_enumeration_type:
134d01f1 4177 process_enumeration_scope (die, cu);
c906108c 4178 break;
134d01f1 4179
f792889a
DJ
4180 /* These dies have a type, but processing them does not create
4181 a symbol or recurse to process the children. Therefore we can
4182 read them on-demand through read_type_die. */
c906108c 4183 case DW_TAG_subroutine_type:
72019c9c 4184 case DW_TAG_set_type:
c906108c 4185 case DW_TAG_array_type:
c906108c 4186 case DW_TAG_pointer_type:
c906108c 4187 case DW_TAG_ptr_to_member_type:
c906108c 4188 case DW_TAG_reference_type:
c906108c 4189 case DW_TAG_string_type:
c906108c 4190 break;
134d01f1 4191
c906108c 4192 case DW_TAG_base_type:
a02abb62 4193 case DW_TAG_subrange_type:
cb249c71 4194 case DW_TAG_typedef:
134d01f1
DJ
4195 /* Add a typedef symbol for the type definition, if it has a
4196 DW_AT_name. */
f792889a 4197 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4198 break;
c906108c 4199 case DW_TAG_common_block:
e7c27a73 4200 read_common_block (die, cu);
c906108c
SS
4201 break;
4202 case DW_TAG_common_inclusion:
4203 break;
d9fa45fe 4204 case DW_TAG_namespace:
63d06c5c 4205 processing_has_namespace_info = 1;
e7c27a73 4206 read_namespace (die, cu);
d9fa45fe 4207 break;
5d7cb8df 4208 case DW_TAG_module:
f55ee35c 4209 processing_has_namespace_info = 1;
5d7cb8df
JK
4210 read_module (die, cu);
4211 break;
d9fa45fe
DC
4212 case DW_TAG_imported_declaration:
4213 case DW_TAG_imported_module:
63d06c5c 4214 processing_has_namespace_info = 1;
27aa8d6a
SW
4215 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4216 || cu->language != language_fortran))
4217 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4218 dwarf_tag_name (die->tag));
4219 read_import_statement (die, cu);
d9fa45fe 4220 break;
c906108c 4221 default:
e7c27a73 4222 new_symbol (die, NULL, cu);
c906108c
SS
4223 break;
4224 }
4225}
4226
94af9270
KS
4227/* A helper function for dwarf2_compute_name which determines whether DIE
4228 needs to have the name of the scope prepended to the name listed in the
4229 die. */
4230
4231static int
4232die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4233{
1c809c68
TT
4234 struct attribute *attr;
4235
94af9270
KS
4236 switch (die->tag)
4237 {
4238 case DW_TAG_namespace:
4239 case DW_TAG_typedef:
4240 case DW_TAG_class_type:
4241 case DW_TAG_interface_type:
4242 case DW_TAG_structure_type:
4243 case DW_TAG_union_type:
4244 case DW_TAG_enumeration_type:
4245 case DW_TAG_enumerator:
4246 case DW_TAG_subprogram:
4247 case DW_TAG_member:
4248 return 1;
4249
4250 case DW_TAG_variable:
4251 /* We only need to prefix "globally" visible variables. These include
4252 any variable marked with DW_AT_external or any variable that
4253 lives in a namespace. [Variables in anonymous namespaces
4254 require prefixing, but they are not DW_AT_external.] */
4255
4256 if (dwarf2_attr (die, DW_AT_specification, cu))
4257 {
4258 struct dwarf2_cu *spec_cu = cu;
9a619af0 4259
94af9270
KS
4260 return die_needs_namespace (die_specification (die, &spec_cu),
4261 spec_cu);
4262 }
4263
1c809c68 4264 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4265 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4266 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4267 return 0;
4268 /* A variable in a lexical block of some kind does not need a
4269 namespace, even though in C++ such variables may be external
4270 and have a mangled name. */
4271 if (die->parent->tag == DW_TAG_lexical_block
4272 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4273 || die->parent->tag == DW_TAG_catch_block
4274 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4275 return 0;
4276 return 1;
94af9270
KS
4277
4278 default:
4279 return 0;
4280 }
4281}
4282
4283/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4284 compute the physname for the object, which include a method's
4285 formal parameters (C++/Java) and return type (Java).
4286
af6b7be1
JB
4287 For Ada, return the DIE's linkage name rather than the fully qualified
4288 name. PHYSNAME is ignored..
4289
94af9270
KS
4290 The result is allocated on the objfile_obstack and canonicalized. */
4291
4292static const char *
4293dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4294 int physname)
4295{
4296 if (name == NULL)
4297 name = dwarf2_name (die, cu);
4298
f55ee35c
JK
4299 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4300 compute it by typename_concat inside GDB. */
4301 if (cu->language == language_ada
4302 || (cu->language == language_fortran && physname))
4303 {
4304 /* For Ada unit, we prefer the linkage name over the name, as
4305 the former contains the exported name, which the user expects
4306 to be able to reference. Ideally, we want the user to be able
4307 to reference this entity using either natural or linkage name,
4308 but we haven't started looking at this enhancement yet. */
4309 struct attribute *attr;
4310
4311 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4312 if (attr == NULL)
4313 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4314 if (attr && DW_STRING (attr))
4315 return DW_STRING (attr);
4316 }
4317
94af9270
KS
4318 /* These are the only languages we know how to qualify names in. */
4319 if (name != NULL
f55ee35c
JK
4320 && (cu->language == language_cplus || cu->language == language_java
4321 || cu->language == language_fortran))
94af9270
KS
4322 {
4323 if (die_needs_namespace (die, cu))
4324 {
4325 long length;
4326 char *prefix;
4327 struct ui_file *buf;
4328
4329 prefix = determine_prefix (die, cu);
4330 buf = mem_fileopen ();
4331 if (*prefix != '\0')
4332 {
f55ee35c
JK
4333 char *prefixed_name = typename_concat (NULL, prefix, name,
4334 physname, cu);
9a619af0 4335
94af9270
KS
4336 fputs_unfiltered (prefixed_name, buf);
4337 xfree (prefixed_name);
4338 }
4339 else
4340 fputs_unfiltered (name ? name : "", buf);
4341
4342 /* For Java and C++ methods, append formal parameter type
4343 information, if PHYSNAME. */
6e70227d 4344
94af9270
KS
4345 if (physname && die->tag == DW_TAG_subprogram
4346 && (cu->language == language_cplus
4347 || cu->language == language_java))
4348 {
4349 struct type *type = read_type_die (die, cu);
4350
4351 c_type_print_args (type, buf, 0, cu->language);
4352
4353 if (cu->language == language_java)
4354 {
4355 /* For java, we must append the return type to method
4356 names. */
4357 if (die->tag == DW_TAG_subprogram)
4358 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4359 0, 0);
4360 }
4361 else if (cu->language == language_cplus)
4362 {
4363 if (TYPE_NFIELDS (type) > 0
4364 && TYPE_FIELD_ARTIFICIAL (type, 0)
4365 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4366 fputs_unfiltered (" const", buf);
4367 }
4368 }
4369
4370 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4371 &length);
4372 ui_file_delete (buf);
4373
4374 if (cu->language == language_cplus)
4375 {
4376 char *cname
4377 = dwarf2_canonicalize_name (name, cu,
4378 &cu->objfile->objfile_obstack);
9a619af0 4379
94af9270
KS
4380 if (cname != NULL)
4381 name = cname;
4382 }
4383 }
4384 }
4385
4386 return name;
4387}
4388
0114d602
DJ
4389/* Return the fully qualified name of DIE, based on its DW_AT_name.
4390 If scope qualifiers are appropriate they will be added. The result
4391 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4392 not have a name. NAME may either be from a previous call to
4393 dwarf2_name or NULL.
4394
4395 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4396
4397static const char *
94af9270 4398dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4399{
94af9270
KS
4400 return dwarf2_compute_name (name, die, cu, 0);
4401}
0114d602 4402
94af9270
KS
4403/* Construct a physname for the given DIE in CU. NAME may either be
4404 from a previous call to dwarf2_name or NULL. The result will be
4405 allocated on the objfile_objstack or NULL if the DIE does not have a
4406 name.
0114d602 4407
94af9270 4408 The output string will be canonicalized (if C++/Java). */
0114d602 4409
94af9270
KS
4410static const char *
4411dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4412{
4413 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4414}
4415
27aa8d6a
SW
4416/* Read the import statement specified by the given die and record it. */
4417
4418static void
4419read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4420{
4421 struct attribute *import_attr;
4422 struct die_info *imported_die;
de4affc9 4423 struct dwarf2_cu *imported_cu;
27aa8d6a 4424 const char *imported_name;
794684b6 4425 const char *imported_name_prefix;
13387711
SW
4426 const char *canonical_name;
4427 const char *import_alias;
4428 const char *imported_declaration = NULL;
794684b6 4429 const char *import_prefix;
13387711
SW
4430
4431 char *temp;
27aa8d6a
SW
4432
4433 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4434 if (import_attr == NULL)
4435 {
4436 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4437 dwarf_tag_name (die->tag));
4438 return;
4439 }
4440
de4affc9
CC
4441 imported_cu = cu;
4442 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4443 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4444 if (imported_name == NULL)
4445 {
4446 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4447
4448 The import in the following code:
4449 namespace A
4450 {
4451 typedef int B;
4452 }
4453
4454 int main ()
4455 {
4456 using A::B;
4457 B b;
4458 return b;
4459 }
4460
4461 ...
4462 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4463 <52> DW_AT_decl_file : 1
4464 <53> DW_AT_decl_line : 6
4465 <54> DW_AT_import : <0x75>
4466 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4467 <59> DW_AT_name : B
4468 <5b> DW_AT_decl_file : 1
4469 <5c> DW_AT_decl_line : 2
4470 <5d> DW_AT_type : <0x6e>
4471 ...
4472 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4473 <76> DW_AT_byte_size : 4
4474 <77> DW_AT_encoding : 5 (signed)
4475
4476 imports the wrong die ( 0x75 instead of 0x58 ).
4477 This case will be ignored until the gcc bug is fixed. */
4478 return;
4479 }
4480
82856980
SW
4481 /* Figure out the local name after import. */
4482 import_alias = dwarf2_name (die, cu);
27aa8d6a 4483
794684b6
SW
4484 /* Figure out where the statement is being imported to. */
4485 import_prefix = determine_prefix (die, cu);
4486
4487 /* Figure out what the scope of the imported die is and prepend it
4488 to the name of the imported die. */
de4affc9 4489 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4490
f55ee35c
JK
4491 if (imported_die->tag != DW_TAG_namespace
4492 && imported_die->tag != DW_TAG_module)
794684b6 4493 {
13387711
SW
4494 imported_declaration = imported_name;
4495 canonical_name = imported_name_prefix;
794684b6 4496 }
13387711 4497 else if (strlen (imported_name_prefix) > 0)
794684b6 4498 {
13387711
SW
4499 temp = alloca (strlen (imported_name_prefix)
4500 + 2 + strlen (imported_name) + 1);
4501 strcpy (temp, imported_name_prefix);
4502 strcat (temp, "::");
4503 strcat (temp, imported_name);
4504 canonical_name = temp;
794684b6 4505 }
13387711
SW
4506 else
4507 canonical_name = imported_name;
794684b6 4508
c0cc3a76
SW
4509 cp_add_using_directive (import_prefix,
4510 canonical_name,
4511 import_alias,
13387711 4512 imported_declaration,
c0cc3a76 4513 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4514}
4515
5fb290d7 4516static void
e142c38c 4517initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4518{
e142c38c 4519 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4520}
4521
cb1df416
DJ
4522static void
4523free_cu_line_header (void *arg)
4524{
4525 struct dwarf2_cu *cu = arg;
4526
4527 free_line_header (cu->line_header);
4528 cu->line_header = NULL;
4529}
4530
9291a0cd
TT
4531static void
4532find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4533 char **name, char **comp_dir)
4534{
4535 struct attribute *attr;
4536
4537 *name = NULL;
4538 *comp_dir = NULL;
4539
4540 /* Find the filename. Do not use dwarf2_name here, since the filename
4541 is not a source language identifier. */
4542 attr = dwarf2_attr (die, DW_AT_name, cu);
4543 if (attr)
4544 {
4545 *name = DW_STRING (attr);
4546 }
4547
4548 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4549 if (attr)
4550 *comp_dir = DW_STRING (attr);
4551 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4552 {
4553 *comp_dir = ldirname (*name);
4554 if (*comp_dir != NULL)
4555 make_cleanup (xfree, *comp_dir);
4556 }
4557 if (*comp_dir != NULL)
4558 {
4559 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4560 directory, get rid of it. */
4561 char *cp = strchr (*comp_dir, ':');
4562
4563 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4564 *comp_dir = cp + 1;
4565 }
4566
4567 if (*name == NULL)
4568 *name = "<unknown>";
4569}
4570
c906108c 4571static void
e7c27a73 4572read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4573{
e7c27a73 4574 struct objfile *objfile = cu->objfile;
debd256d 4575 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 4576 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
4577 CORE_ADDR highpc = ((CORE_ADDR) 0);
4578 struct attribute *attr;
e1024ff1 4579 char *name = NULL;
c906108c
SS
4580 char *comp_dir = NULL;
4581 struct die_info *child_die;
4582 bfd *abfd = objfile->obfd;
debd256d 4583 struct line_header *line_header = 0;
e142c38c 4584 CORE_ADDR baseaddr;
6e70227d 4585
e142c38c 4586 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4587
fae299cd 4588 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
4589
4590 /* If we didn't find a lowpc, set it to highpc to avoid complaints
4591 from finish_block. */
2acceee2 4592 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
4593 lowpc = highpc;
4594 lowpc += baseaddr;
4595 highpc += baseaddr;
4596
9291a0cd 4597 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 4598
e142c38c 4599 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
4600 if (attr)
4601 {
e142c38c 4602 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
4603 }
4604
b0f35d58 4605 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 4606 if (attr)
b0f35d58 4607 cu->producer = DW_STRING (attr);
303b6f5d 4608
c906108c
SS
4609 /* We assume that we're processing GCC output. */
4610 processing_gcc_compilation = 2;
c906108c 4611
df8a16a1
DJ
4612 processing_has_namespace_info = 0;
4613
c906108c
SS
4614 start_symtab (name, comp_dir, lowpc);
4615 record_debugformat ("DWARF 2");
303b6f5d 4616 record_producer (cu->producer);
c906108c 4617
e142c38c 4618 initialize_cu_func_list (cu);
c906108c 4619
cb1df416
DJ
4620 /* Decode line number information if present. We do this before
4621 processing child DIEs, so that the line header table is available
4622 for DW_AT_decl_file. */
e142c38c 4623 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
4624 if (attr)
4625 {
debd256d 4626 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 4627 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
4628 if (line_header)
4629 {
cb1df416
DJ
4630 cu->line_header = line_header;
4631 make_cleanup (free_cu_line_header, cu);
aaa75496 4632 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 4633 }
5fb290d7 4634 }
debd256d 4635
cb1df416
DJ
4636 /* Process all dies in compilation unit. */
4637 if (die->child != NULL)
4638 {
4639 child_die = die->child;
4640 while (child_die && child_die->tag)
4641 {
4642 process_die (child_die, cu);
4643 child_die = sibling_die (child_die);
4644 }
4645 }
4646
2e276125
JB
4647 /* Decode macro information, if present. Dwarf 2 macro information
4648 refers to information in the line number info statement program
4649 header, so we can only read it if we've read the header
4650 successfully. */
e142c38c 4651 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 4652 if (attr && line_header)
2e276125
JB
4653 {
4654 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 4655
2e276125 4656 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 4657 comp_dir, abfd, cu);
2e276125 4658 }
debd256d 4659 do_cleanups (back_to);
5fb290d7
DJ
4660}
4661
348e048f
DE
4662/* For TUs we want to skip the first top level sibling if it's not the
4663 actual type being defined by this TU. In this case the first top
4664 level sibling is there to provide context only. */
4665
4666static void
4667read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
4668{
4669 struct objfile *objfile = cu->objfile;
4670 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4671 CORE_ADDR lowpc;
4672 struct attribute *attr;
4673 char *name = NULL;
4674 char *comp_dir = NULL;
4675 struct die_info *child_die;
4676 bfd *abfd = objfile->obfd;
348e048f
DE
4677
4678 /* start_symtab needs a low pc, but we don't really have one.
4679 Do what read_file_scope would do in the absence of such info. */
4680 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4681
4682 /* Find the filename. Do not use dwarf2_name here, since the filename
4683 is not a source language identifier. */
4684 attr = dwarf2_attr (die, DW_AT_name, cu);
4685 if (attr)
4686 name = DW_STRING (attr);
4687
4688 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4689 if (attr)
4690 comp_dir = DW_STRING (attr);
4691 else if (name != NULL && IS_ABSOLUTE_PATH (name))
4692 {
4693 comp_dir = ldirname (name);
4694 if (comp_dir != NULL)
4695 make_cleanup (xfree, comp_dir);
4696 }
4697
4698 if (name == NULL)
4699 name = "<unknown>";
4700
4701 attr = dwarf2_attr (die, DW_AT_language, cu);
4702 if (attr)
4703 set_cu_language (DW_UNSND (attr), cu);
4704
4705 /* This isn't technically needed today. It is done for symmetry
4706 with read_file_scope. */
4707 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 4708 if (attr)
348e048f
DE
4709 cu->producer = DW_STRING (attr);
4710
4711 /* We assume that we're processing GCC output. */
4712 processing_gcc_compilation = 2;
4713
4714 processing_has_namespace_info = 0;
4715
4716 start_symtab (name, comp_dir, lowpc);
4717 record_debugformat ("DWARF 2");
4718 record_producer (cu->producer);
4719
4720 /* Process the dies in the type unit. */
4721 if (die->child == NULL)
4722 {
4723 dump_die_for_error (die);
4724 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
4725 bfd_get_filename (abfd));
4726 }
4727
4728 child_die = die->child;
4729
4730 while (child_die && child_die->tag)
4731 {
4732 process_die (child_die, cu);
4733
4734 child_die = sibling_die (child_die);
4735 }
4736
4737 do_cleanups (back_to);
4738}
4739
5fb290d7 4740static void
e142c38c
DJ
4741add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
4742 struct dwarf2_cu *cu)
5fb290d7
DJ
4743{
4744 struct function_range *thisfn;
4745
4746 thisfn = (struct function_range *)
7b5a2f43 4747 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
4748 thisfn->name = name;
4749 thisfn->lowpc = lowpc;
4750 thisfn->highpc = highpc;
4751 thisfn->seen_line = 0;
4752 thisfn->next = NULL;
4753
e142c38c
DJ
4754 if (cu->last_fn == NULL)
4755 cu->first_fn = thisfn;
5fb290d7 4756 else
e142c38c 4757 cu->last_fn->next = thisfn;
5fb290d7 4758
e142c38c 4759 cu->last_fn = thisfn;
c906108c
SS
4760}
4761
d389af10
JK
4762/* qsort helper for inherit_abstract_dies. */
4763
4764static int
4765unsigned_int_compar (const void *ap, const void *bp)
4766{
4767 unsigned int a = *(unsigned int *) ap;
4768 unsigned int b = *(unsigned int *) bp;
4769
4770 return (a > b) - (b > a);
4771}
4772
4773/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
4774 Inherit only the children of the DW_AT_abstract_origin DIE not being already
4775 referenced by DW_AT_abstract_origin from the children of the current DIE. */
4776
4777static void
4778inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
4779{
4780 struct die_info *child_die;
4781 unsigned die_children_count;
4782 /* CU offsets which were referenced by children of the current DIE. */
4783 unsigned *offsets;
4784 unsigned *offsets_end, *offsetp;
4785 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
4786 struct die_info *origin_die;
4787 /* Iterator of the ORIGIN_DIE children. */
4788 struct die_info *origin_child_die;
4789 struct cleanup *cleanups;
4790 struct attribute *attr;
4791
4792 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
4793 if (!attr)
4794 return;
4795
4796 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
4797 if (die->tag != origin_die->tag
4798 && !(die->tag == DW_TAG_inlined_subroutine
4799 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
4800 complaint (&symfile_complaints,
4801 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
4802 die->offset, origin_die->offset);
4803
4804 child_die = die->child;
4805 die_children_count = 0;
4806 while (child_die && child_die->tag)
4807 {
4808 child_die = sibling_die (child_die);
4809 die_children_count++;
4810 }
4811 offsets = xmalloc (sizeof (*offsets) * die_children_count);
4812 cleanups = make_cleanup (xfree, offsets);
4813
4814 offsets_end = offsets;
4815 child_die = die->child;
4816 while (child_die && child_die->tag)
4817 {
c38f313d
DJ
4818 /* For each CHILD_DIE, find the corresponding child of
4819 ORIGIN_DIE. If there is more than one layer of
4820 DW_AT_abstract_origin, follow them all; there shouldn't be,
4821 but GCC versions at least through 4.4 generate this (GCC PR
4822 40573). */
4823 struct die_info *child_origin_die = child_die;
9a619af0 4824
c38f313d
DJ
4825 while (1)
4826 {
4827 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
4828 if (attr == NULL)
4829 break;
4830 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
4831 }
4832
d389af10
JK
4833 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
4834 counterpart may exist. */
c38f313d 4835 if (child_origin_die != child_die)
d389af10 4836 {
edb3359d
DJ
4837 if (child_die->tag != child_origin_die->tag
4838 && !(child_die->tag == DW_TAG_inlined_subroutine
4839 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
4840 complaint (&symfile_complaints,
4841 _("Child DIE 0x%x and its abstract origin 0x%x have "
4842 "different tags"), child_die->offset,
4843 child_origin_die->offset);
c38f313d
DJ
4844 if (child_origin_die->parent != origin_die)
4845 complaint (&symfile_complaints,
4846 _("Child DIE 0x%x and its abstract origin 0x%x have "
4847 "different parents"), child_die->offset,
4848 child_origin_die->offset);
4849 else
4850 *offsets_end++ = child_origin_die->offset;
d389af10
JK
4851 }
4852 child_die = sibling_die (child_die);
4853 }
4854 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
4855 unsigned_int_compar);
4856 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
4857 if (offsetp[-1] == *offsetp)
4858 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
4859 "to DIE 0x%x as their abstract origin"),
4860 die->offset, *offsetp);
4861
4862 offsetp = offsets;
4863 origin_child_die = origin_die->child;
4864 while (origin_child_die && origin_child_die->tag)
4865 {
4866 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
4867 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
4868 offsetp++;
4869 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
4870 {
4871 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
4872 process_die (origin_child_die, cu);
4873 }
4874 origin_child_die = sibling_die (origin_child_die);
4875 }
4876
4877 do_cleanups (cleanups);
4878}
4879
c906108c 4880static void
e7c27a73 4881read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4882{
e7c27a73 4883 struct objfile *objfile = cu->objfile;
52f0bd74 4884 struct context_stack *new;
c906108c
SS
4885 CORE_ADDR lowpc;
4886 CORE_ADDR highpc;
4887 struct die_info *child_die;
edb3359d 4888 struct attribute *attr, *call_line, *call_file;
c906108c 4889 char *name;
e142c38c 4890 CORE_ADDR baseaddr;
801e3a5b 4891 struct block *block;
edb3359d
DJ
4892 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
4893
4894 if (inlined_func)
4895 {
4896 /* If we do not have call site information, we can't show the
4897 caller of this inlined function. That's too confusing, so
4898 only use the scope for local variables. */
4899 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
4900 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
4901 if (call_line == NULL || call_file == NULL)
4902 {
4903 read_lexical_block_scope (die, cu);
4904 return;
4905 }
4906 }
c906108c 4907
e142c38c
DJ
4908 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4909
94af9270 4910 name = dwarf2_name (die, cu);
c906108c 4911
e8d05480
JB
4912 /* Ignore functions with missing or empty names. These are actually
4913 illegal according to the DWARF standard. */
4914 if (name == NULL)
4915 {
4916 complaint (&symfile_complaints,
4917 _("missing name for subprogram DIE at %d"), die->offset);
4918 return;
4919 }
4920
4921 /* Ignore functions with missing or invalid low and high pc attributes. */
4922 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
4923 {
ae4d0c03
PM
4924 attr = dwarf2_attr (die, DW_AT_external, cu);
4925 if (!attr || !DW_UNSND (attr))
4926 complaint (&symfile_complaints,
4927 _("cannot get low and high bounds for subprogram DIE at %d"),
4928 die->offset);
e8d05480
JB
4929 return;
4930 }
c906108c
SS
4931
4932 lowpc += baseaddr;
4933 highpc += baseaddr;
4934
5fb290d7 4935 /* Record the function range for dwarf_decode_lines. */
e142c38c 4936 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 4937
c906108c 4938 new = push_context (0, lowpc);
f792889a 4939 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 4940
4cecd739
DJ
4941 /* If there is a location expression for DW_AT_frame_base, record
4942 it. */
e142c38c 4943 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 4944 if (attr)
c034e007
AC
4945 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
4946 expression is being recorded directly in the function's symbol
4947 and not in a separate frame-base object. I guess this hack is
4948 to avoid adding some sort of frame-base adjunct/annex to the
4949 function's symbol :-(. The problem with doing this is that it
4950 results in a function symbol with a location expression that
4951 has nothing to do with the location of the function, ouch! The
4952 relationship should be: a function's symbol has-a frame base; a
4953 frame-base has-a location expression. */
e7c27a73 4954 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 4955
e142c38c 4956 cu->list_in_scope = &local_symbols;
c906108c 4957
639d11d3 4958 if (die->child != NULL)
c906108c 4959 {
639d11d3 4960 child_die = die->child;
c906108c
SS
4961 while (child_die && child_die->tag)
4962 {
e7c27a73 4963 process_die (child_die, cu);
c906108c
SS
4964 child_die = sibling_die (child_die);
4965 }
4966 }
4967
d389af10
JK
4968 inherit_abstract_dies (die, cu);
4969
4a811a97
UW
4970 /* If we have a DW_AT_specification, we might need to import using
4971 directives from the context of the specification DIE. See the
4972 comment in determine_prefix. */
4973 if (cu->language == language_cplus
4974 && dwarf2_attr (die, DW_AT_specification, cu))
4975 {
4976 struct dwarf2_cu *spec_cu = cu;
4977 struct die_info *spec_die = die_specification (die, &spec_cu);
4978
4979 while (spec_die)
4980 {
4981 child_die = spec_die->child;
4982 while (child_die && child_die->tag)
4983 {
4984 if (child_die->tag == DW_TAG_imported_module)
4985 process_die (child_die, spec_cu);
4986 child_die = sibling_die (child_die);
4987 }
4988
4989 /* In some cases, GCC generates specification DIEs that
4990 themselves contain DW_AT_specification attributes. */
4991 spec_die = die_specification (spec_die, &spec_cu);
4992 }
4993 }
4994
c906108c
SS
4995 new = pop_context ();
4996 /* Make a block for the local symbols within. */
801e3a5b
JB
4997 block = finish_block (new->name, &local_symbols, new->old_blocks,
4998 lowpc, highpc, objfile);
4999
df8a16a1 5000 /* For C++, set the block's scope. */
f55ee35c 5001 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5002 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5003 determine_prefix (die, cu),
df8a16a1
DJ
5004 processing_has_namespace_info);
5005
801e3a5b
JB
5006 /* If we have address ranges, record them. */
5007 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5008
208d8187
JB
5009 /* In C++, we can have functions nested inside functions (e.g., when
5010 a function declares a class that has methods). This means that
5011 when we finish processing a function scope, we may need to go
5012 back to building a containing block's symbol lists. */
5013 local_symbols = new->locals;
5014 param_symbols = new->params;
27aa8d6a 5015 using_directives = new->using_directives;
208d8187 5016
921e78cf
JB
5017 /* If we've finished processing a top-level function, subsequent
5018 symbols go in the file symbol list. */
5019 if (outermost_context_p ())
e142c38c 5020 cu->list_in_scope = &file_symbols;
c906108c
SS
5021}
5022
5023/* Process all the DIES contained within a lexical block scope. Start
5024 a new scope, process the dies, and then close the scope. */
5025
5026static void
e7c27a73 5027read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5028{
e7c27a73 5029 struct objfile *objfile = cu->objfile;
52f0bd74 5030 struct context_stack *new;
c906108c
SS
5031 CORE_ADDR lowpc, highpc;
5032 struct die_info *child_die;
e142c38c
DJ
5033 CORE_ADDR baseaddr;
5034
5035 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5036
5037 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5038 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5039 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5040 be nasty. Might be easier to properly extend generic blocks to
af34e669 5041 describe ranges. */
d85a05f0 5042 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5043 return;
5044 lowpc += baseaddr;
5045 highpc += baseaddr;
5046
5047 push_context (0, lowpc);
639d11d3 5048 if (die->child != NULL)
c906108c 5049 {
639d11d3 5050 child_die = die->child;
c906108c
SS
5051 while (child_die && child_die->tag)
5052 {
e7c27a73 5053 process_die (child_die, cu);
c906108c
SS
5054 child_die = sibling_die (child_die);
5055 }
5056 }
5057 new = pop_context ();
5058
8540c487 5059 if (local_symbols != NULL || using_directives != NULL)
c906108c 5060 {
801e3a5b
JB
5061 struct block *block
5062 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5063 highpc, objfile);
5064
5065 /* Note that recording ranges after traversing children, as we
5066 do here, means that recording a parent's ranges entails
5067 walking across all its children's ranges as they appear in
5068 the address map, which is quadratic behavior.
5069
5070 It would be nicer to record the parent's ranges before
5071 traversing its children, simply overriding whatever you find
5072 there. But since we don't even decide whether to create a
5073 block until after we've traversed its children, that's hard
5074 to do. */
5075 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5076 }
5077 local_symbols = new->locals;
27aa8d6a 5078 using_directives = new->using_directives;
c906108c
SS
5079}
5080
43039443 5081/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5082 Return 1 if the attributes are present and valid, otherwise, return 0.
5083 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5084
5085static int
5086dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5087 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5088 struct partial_symtab *ranges_pst)
43039443
JK
5089{
5090 struct objfile *objfile = cu->objfile;
5091 struct comp_unit_head *cu_header = &cu->header;
5092 bfd *obfd = objfile->obfd;
5093 unsigned int addr_size = cu_header->addr_size;
5094 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5095 /* Base address selection entry. */
5096 CORE_ADDR base;
5097 int found_base;
5098 unsigned int dummy;
5099 gdb_byte *buffer;
5100 CORE_ADDR marker;
5101 int low_set;
5102 CORE_ADDR low = 0;
5103 CORE_ADDR high = 0;
ff013f42 5104 CORE_ADDR baseaddr;
43039443 5105
d00adf39
DE
5106 found_base = cu->base_known;
5107 base = cu->base_address;
43039443 5108
be391dca 5109 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5110 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5111 {
5112 complaint (&symfile_complaints,
5113 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5114 offset);
5115 return 0;
5116 }
dce234bc 5117 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5118
5119 /* Read in the largest possible address. */
5120 marker = read_address (obfd, buffer, cu, &dummy);
5121 if ((marker & mask) == mask)
5122 {
5123 /* If we found the largest possible address, then
5124 read the base address. */
5125 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5126 buffer += 2 * addr_size;
5127 offset += 2 * addr_size;
5128 found_base = 1;
5129 }
5130
5131 low_set = 0;
5132
e7030f15 5133 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5134
43039443
JK
5135 while (1)
5136 {
5137 CORE_ADDR range_beginning, range_end;
5138
5139 range_beginning = read_address (obfd, buffer, cu, &dummy);
5140 buffer += addr_size;
5141 range_end = read_address (obfd, buffer, cu, &dummy);
5142 buffer += addr_size;
5143 offset += 2 * addr_size;
5144
5145 /* An end of list marker is a pair of zero addresses. */
5146 if (range_beginning == 0 && range_end == 0)
5147 /* Found the end of list entry. */
5148 break;
5149
5150 /* Each base address selection entry is a pair of 2 values.
5151 The first is the largest possible address, the second is
5152 the base address. Check for a base address here. */
5153 if ((range_beginning & mask) == mask)
5154 {
5155 /* If we found the largest possible address, then
5156 read the base address. */
5157 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5158 found_base = 1;
5159 continue;
5160 }
5161
5162 if (!found_base)
5163 {
5164 /* We have no valid base address for the ranges
5165 data. */
5166 complaint (&symfile_complaints,
5167 _("Invalid .debug_ranges data (no base address)"));
5168 return 0;
5169 }
5170
5171 range_beginning += base;
5172 range_end += base;
5173
ff013f42
JK
5174 if (ranges_pst != NULL && range_beginning < range_end)
5175 addrmap_set_empty (objfile->psymtabs_addrmap,
5176 range_beginning + baseaddr, range_end - 1 + baseaddr,
5177 ranges_pst);
5178
43039443
JK
5179 /* FIXME: This is recording everything as a low-high
5180 segment of consecutive addresses. We should have a
5181 data structure for discontiguous block ranges
5182 instead. */
5183 if (! low_set)
5184 {
5185 low = range_beginning;
5186 high = range_end;
5187 low_set = 1;
5188 }
5189 else
5190 {
5191 if (range_beginning < low)
5192 low = range_beginning;
5193 if (range_end > high)
5194 high = range_end;
5195 }
5196 }
5197
5198 if (! low_set)
5199 /* If the first entry is an end-of-list marker, the range
5200 describes an empty scope, i.e. no instructions. */
5201 return 0;
5202
5203 if (low_return)
5204 *low_return = low;
5205 if (high_return)
5206 *high_return = high;
5207 return 1;
5208}
5209
af34e669
DJ
5210/* Get low and high pc attributes from a die. Return 1 if the attributes
5211 are present and valid, otherwise, return 0. Return -1 if the range is
5212 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5213static int
af34e669 5214dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5215 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5216 struct partial_symtab *pst)
c906108c
SS
5217{
5218 struct attribute *attr;
af34e669
DJ
5219 CORE_ADDR low = 0;
5220 CORE_ADDR high = 0;
5221 int ret = 0;
c906108c 5222
e142c38c 5223 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5224 if (attr)
af34e669
DJ
5225 {
5226 high = DW_ADDR (attr);
e142c38c 5227 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5228 if (attr)
5229 low = DW_ADDR (attr);
5230 else
5231 /* Found high w/o low attribute. */
5232 return 0;
5233
5234 /* Found consecutive range of addresses. */
5235 ret = 1;
5236 }
c906108c 5237 else
af34e669 5238 {
e142c38c 5239 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5240 if (attr != NULL)
5241 {
af34e669 5242 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5243 .debug_ranges section. */
d85a05f0 5244 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5245 return 0;
43039443 5246 /* Found discontinuous range of addresses. */
af34e669
DJ
5247 ret = -1;
5248 }
5249 }
c906108c
SS
5250
5251 if (high < low)
5252 return 0;
5253
5254 /* When using the GNU linker, .gnu.linkonce. sections are used to
5255 eliminate duplicate copies of functions and vtables and such.
5256 The linker will arbitrarily choose one and discard the others.
5257 The AT_*_pc values for such functions refer to local labels in
5258 these sections. If the section from that file was discarded, the
5259 labels are not in the output, so the relocs get a value of 0.
5260 If this is a discarded function, mark the pc bounds as invalid,
5261 so that GDB will ignore it. */
72dca2f5 5262 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5263 return 0;
5264
5265 *lowpc = low;
5266 *highpc = high;
af34e669 5267 return ret;
c906108c
SS
5268}
5269
b084d499
JB
5270/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5271 its low and high PC addresses. Do nothing if these addresses could not
5272 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5273 and HIGHPC to the high address if greater than HIGHPC. */
5274
5275static void
5276dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5277 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5278 struct dwarf2_cu *cu)
5279{
5280 CORE_ADDR low, high;
5281 struct die_info *child = die->child;
5282
d85a05f0 5283 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5284 {
5285 *lowpc = min (*lowpc, low);
5286 *highpc = max (*highpc, high);
5287 }
5288
5289 /* If the language does not allow nested subprograms (either inside
5290 subprograms or lexical blocks), we're done. */
5291 if (cu->language != language_ada)
5292 return;
6e70227d 5293
b084d499
JB
5294 /* Check all the children of the given DIE. If it contains nested
5295 subprograms, then check their pc bounds. Likewise, we need to
5296 check lexical blocks as well, as they may also contain subprogram
5297 definitions. */
5298 while (child && child->tag)
5299 {
5300 if (child->tag == DW_TAG_subprogram
5301 || child->tag == DW_TAG_lexical_block)
5302 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5303 child = sibling_die (child);
5304 }
5305}
5306
fae299cd
DC
5307/* Get the low and high pc's represented by the scope DIE, and store
5308 them in *LOWPC and *HIGHPC. If the correct values can't be
5309 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5310
5311static void
5312get_scope_pc_bounds (struct die_info *die,
5313 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5314 struct dwarf2_cu *cu)
5315{
5316 CORE_ADDR best_low = (CORE_ADDR) -1;
5317 CORE_ADDR best_high = (CORE_ADDR) 0;
5318 CORE_ADDR current_low, current_high;
5319
d85a05f0 5320 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5321 {
5322 best_low = current_low;
5323 best_high = current_high;
5324 }
5325 else
5326 {
5327 struct die_info *child = die->child;
5328
5329 while (child && child->tag)
5330 {
5331 switch (child->tag) {
5332 case DW_TAG_subprogram:
b084d499 5333 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5334 break;
5335 case DW_TAG_namespace:
f55ee35c 5336 case DW_TAG_module:
fae299cd
DC
5337 /* FIXME: carlton/2004-01-16: Should we do this for
5338 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5339 that current GCC's always emit the DIEs corresponding
5340 to definitions of methods of classes as children of a
5341 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5342 the DIEs giving the declarations, which could be
5343 anywhere). But I don't see any reason why the
5344 standards says that they have to be there. */
5345 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5346
5347 if (current_low != ((CORE_ADDR) -1))
5348 {
5349 best_low = min (best_low, current_low);
5350 best_high = max (best_high, current_high);
5351 }
5352 break;
5353 default:
5354 /* Ignore. */
5355 break;
5356 }
5357
5358 child = sibling_die (child);
5359 }
5360 }
5361
5362 *lowpc = best_low;
5363 *highpc = best_high;
5364}
5365
801e3a5b
JB
5366/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5367 in DIE. */
5368static void
5369dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5370 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5371{
5372 struct attribute *attr;
5373
5374 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5375 if (attr)
5376 {
5377 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5378
801e3a5b
JB
5379 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5380 if (attr)
5381 {
5382 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5383
801e3a5b
JB
5384 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5385 }
5386 }
5387
5388 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5389 if (attr)
5390 {
5391 bfd *obfd = cu->objfile->obfd;
5392
5393 /* The value of the DW_AT_ranges attribute is the offset of the
5394 address range list in the .debug_ranges section. */
5395 unsigned long offset = DW_UNSND (attr);
dce234bc 5396 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5397
5398 /* For some target architectures, but not others, the
5399 read_address function sign-extends the addresses it returns.
5400 To recognize base address selection entries, we need a
5401 mask. */
5402 unsigned int addr_size = cu->header.addr_size;
5403 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5404
5405 /* The base address, to which the next pair is relative. Note
5406 that this 'base' is a DWARF concept: most entries in a range
5407 list are relative, to reduce the number of relocs against the
5408 debugging information. This is separate from this function's
5409 'baseaddr' argument, which GDB uses to relocate debugging
5410 information from a shared library based on the address at
5411 which the library was loaded. */
d00adf39
DE
5412 CORE_ADDR base = cu->base_address;
5413 int base_known = cu->base_known;
801e3a5b 5414
be391dca 5415 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5416 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5417 {
5418 complaint (&symfile_complaints,
5419 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5420 offset);
5421 return;
5422 }
5423
5424 for (;;)
5425 {
5426 unsigned int bytes_read;
5427 CORE_ADDR start, end;
5428
5429 start = read_address (obfd, buffer, cu, &bytes_read);
5430 buffer += bytes_read;
5431 end = read_address (obfd, buffer, cu, &bytes_read);
5432 buffer += bytes_read;
5433
5434 /* Did we find the end of the range list? */
5435 if (start == 0 && end == 0)
5436 break;
5437
5438 /* Did we find a base address selection entry? */
5439 else if ((start & base_select_mask) == base_select_mask)
5440 {
5441 base = end;
5442 base_known = 1;
5443 }
5444
5445 /* We found an ordinary address range. */
5446 else
5447 {
5448 if (!base_known)
5449 {
5450 complaint (&symfile_complaints,
5451 _("Invalid .debug_ranges data (no base address)"));
5452 return;
5453 }
5454
6e70227d
DE
5455 record_block_range (block,
5456 baseaddr + base + start,
801e3a5b
JB
5457 baseaddr + base + end - 1);
5458 }
5459 }
5460 }
5461}
5462
c906108c
SS
5463/* Add an aggregate field to the field list. */
5464
5465static void
107d2387 5466dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 5467 struct dwarf2_cu *cu)
6e70227d 5468{
e7c27a73 5469 struct objfile *objfile = cu->objfile;
5e2b427d 5470 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5471 struct nextfield *new_field;
5472 struct attribute *attr;
5473 struct field *fp;
5474 char *fieldname = "";
5475
5476 /* Allocate a new field list entry and link it in. */
5477 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 5478 make_cleanup (xfree, new_field);
c906108c 5479 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
5480
5481 if (die->tag == DW_TAG_inheritance)
5482 {
5483 new_field->next = fip->baseclasses;
5484 fip->baseclasses = new_field;
5485 }
5486 else
5487 {
5488 new_field->next = fip->fields;
5489 fip->fields = new_field;
5490 }
c906108c
SS
5491 fip->nfields++;
5492
5493 /* Handle accessibility and virtuality of field.
5494 The default accessibility for members is public, the default
5495 accessibility for inheritance is private. */
5496 if (die->tag != DW_TAG_inheritance)
5497 new_field->accessibility = DW_ACCESS_public;
5498 else
5499 new_field->accessibility = DW_ACCESS_private;
5500 new_field->virtuality = DW_VIRTUALITY_none;
5501
e142c38c 5502 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
5503 if (attr)
5504 new_field->accessibility = DW_UNSND (attr);
5505 if (new_field->accessibility != DW_ACCESS_public)
5506 fip->non_public_fields = 1;
e142c38c 5507 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
5508 if (attr)
5509 new_field->virtuality = DW_UNSND (attr);
5510
5511 fp = &new_field->field;
a9a9bd0f 5512
e142c38c 5513 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 5514 {
a9a9bd0f 5515 /* Data member other than a C++ static data member. */
6e70227d 5516
c906108c 5517 /* Get type of field. */
e7c27a73 5518 fp->type = die_type (die, cu);
c906108c 5519
d6a843b5 5520 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 5521
c906108c 5522 /* Get bit size of field (zero if none). */
e142c38c 5523 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
5524 if (attr)
5525 {
5526 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5527 }
5528 else
5529 {
5530 FIELD_BITSIZE (*fp) = 0;
5531 }
5532
5533 /* Get bit offset of field. */
e142c38c 5534 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
5535 if (attr)
5536 {
d4b96c9a 5537 int byte_offset = 0;
c6a0999f 5538
3690dd37 5539 if (attr_form_is_section_offset (attr))
d4b96c9a 5540 dwarf2_complex_location_expr_complaint ();
3690dd37 5541 else if (attr_form_is_constant (attr))
c6a0999f 5542 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 5543 else if (attr_form_is_block (attr))
c6a0999f 5544 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
5545 else
5546 dwarf2_complex_location_expr_complaint ();
c6a0999f 5547
d6a843b5 5548 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 5549 }
e142c38c 5550 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
5551 if (attr)
5552 {
5e2b427d 5553 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
5554 {
5555 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
5556 additional bit offset from the MSB of the containing
5557 anonymous object to the MSB of the field. We don't
5558 have to do anything special since we don't need to
5559 know the size of the anonymous object. */
c906108c
SS
5560 FIELD_BITPOS (*fp) += DW_UNSND (attr);
5561 }
5562 else
5563 {
5564 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
5565 MSB of the anonymous object, subtract off the number of
5566 bits from the MSB of the field to the MSB of the
5567 object, and then subtract off the number of bits of
5568 the field itself. The result is the bit offset of
5569 the LSB of the field. */
c906108c
SS
5570 int anonymous_size;
5571 int bit_offset = DW_UNSND (attr);
5572
e142c38c 5573 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5574 if (attr)
5575 {
5576 /* The size of the anonymous object containing
5577 the bit field is explicit, so use the
5578 indicated size (in bytes). */
5579 anonymous_size = DW_UNSND (attr);
5580 }
5581 else
5582 {
5583 /* The size of the anonymous object containing
5584 the bit field must be inferred from the type
5585 attribute of the data member containing the
5586 bit field. */
5587 anonymous_size = TYPE_LENGTH (fp->type);
5588 }
5589 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
5590 - bit_offset - FIELD_BITSIZE (*fp);
5591 }
5592 }
5593
5594 /* Get name of field. */
39cbfefa
DJ
5595 fieldname = dwarf2_name (die, cu);
5596 if (fieldname == NULL)
5597 fieldname = "";
d8151005
DJ
5598
5599 /* The name is already allocated along with this objfile, so we don't
5600 need to duplicate it for the type. */
5601 fp->name = fieldname;
c906108c
SS
5602
5603 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 5604 pointer or virtual base class pointer) to private. */
e142c38c 5605 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 5606 {
d48cc9dd 5607 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
5608 new_field->accessibility = DW_ACCESS_private;
5609 fip->non_public_fields = 1;
5610 }
5611 }
a9a9bd0f 5612 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 5613 {
a9a9bd0f
DC
5614 /* C++ static member. */
5615
5616 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
5617 is a declaration, but all versions of G++ as of this writing
5618 (so through at least 3.2.1) incorrectly generate
5619 DW_TAG_variable tags. */
6e70227d 5620
c906108c 5621 char *physname;
c906108c 5622
a9a9bd0f 5623 /* Get name of field. */
39cbfefa
DJ
5624 fieldname = dwarf2_name (die, cu);
5625 if (fieldname == NULL)
c906108c
SS
5626 return;
5627
254e6b9e 5628 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
5629 if (attr
5630 /* Only create a symbol if this is an external value.
5631 new_symbol checks this and puts the value in the global symbol
5632 table, which we want. If it is not external, new_symbol
5633 will try to put the value in cu->list_in_scope which is wrong. */
5634 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
5635 {
5636 /* A static const member, not much different than an enum as far as
5637 we're concerned, except that we can support more types. */
5638 new_symbol (die, NULL, cu);
5639 }
5640
2df3850c 5641 /* Get physical name. */
94af9270 5642 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 5643
d8151005
DJ
5644 /* The name is already allocated along with this objfile, so we don't
5645 need to duplicate it for the type. */
5646 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 5647 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 5648 FIELD_NAME (*fp) = fieldname;
c906108c
SS
5649 }
5650 else if (die->tag == DW_TAG_inheritance)
5651 {
5652 /* C++ base class field. */
e142c38c 5653 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 5654 if (attr)
d4b96c9a
JK
5655 {
5656 int byte_offset = 0;
5657
5658 if (attr_form_is_section_offset (attr))
5659 dwarf2_complex_location_expr_complaint ();
5660 else if (attr_form_is_constant (attr))
5661 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5662 else if (attr_form_is_block (attr))
5663 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5664 else
5665 dwarf2_complex_location_expr_complaint ();
5666
5667 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5668 }
c906108c 5669 FIELD_BITSIZE (*fp) = 0;
e7c27a73 5670 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
5671 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
5672 fip->nbaseclasses++;
5673 }
5674}
5675
98751a41
JK
5676/* Add a typedef defined in the scope of the FIP's class. */
5677
5678static void
5679dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
5680 struct dwarf2_cu *cu)
6e70227d 5681{
98751a41
JK
5682 struct objfile *objfile = cu->objfile;
5683 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5684 struct typedef_field_list *new_field;
5685 struct attribute *attr;
5686 struct typedef_field *fp;
5687 char *fieldname = "";
5688
5689 /* Allocate a new field list entry and link it in. */
5690 new_field = xzalloc (sizeof (*new_field));
5691 make_cleanup (xfree, new_field);
5692
5693 gdb_assert (die->tag == DW_TAG_typedef);
5694
5695 fp = &new_field->field;
5696
5697 /* Get name of field. */
5698 fp->name = dwarf2_name (die, cu);
5699 if (fp->name == NULL)
5700 return;
5701
5702 fp->type = read_type_die (die, cu);
5703
5704 new_field->next = fip->typedef_field_list;
5705 fip->typedef_field_list = new_field;
5706 fip->typedef_field_list_count++;
5707}
5708
c906108c
SS
5709/* Create the vector of fields, and attach it to the type. */
5710
5711static void
fba45db2 5712dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 5713 struct dwarf2_cu *cu)
c906108c
SS
5714{
5715 int nfields = fip->nfields;
5716
5717 /* Record the field count, allocate space for the array of fields,
5718 and create blank accessibility bitfields if necessary. */
5719 TYPE_NFIELDS (type) = nfields;
5720 TYPE_FIELDS (type) = (struct field *)
5721 TYPE_ALLOC (type, sizeof (struct field) * nfields);
5722 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
5723
b4ba55a1 5724 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
5725 {
5726 ALLOCATE_CPLUS_STRUCT_TYPE (type);
5727
5728 TYPE_FIELD_PRIVATE_BITS (type) =
5729 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5730 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
5731
5732 TYPE_FIELD_PROTECTED_BITS (type) =
5733 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5734 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
5735
5736 TYPE_FIELD_IGNORE_BITS (type) =
5737 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5738 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
5739 }
5740
5741 /* If the type has baseclasses, allocate and clear a bit vector for
5742 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 5743 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
5744 {
5745 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 5746 unsigned char *pointer;
c906108c
SS
5747
5748 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
5749 pointer = TYPE_ALLOC (type, num_bytes);
5750 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
5751 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
5752 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
5753 }
5754
5755 /* Copy the saved-up fields into the field vector. Start from the head
5756 of the list, adding to the tail of the field array, so that they end
5757 up in the same order in the array in which they were added to the list. */
5758 while (nfields-- > 0)
5759 {
7d0ccb61
DJ
5760 struct nextfield *fieldp;
5761
5762 if (fip->fields)
5763 {
5764 fieldp = fip->fields;
5765 fip->fields = fieldp->next;
5766 }
5767 else
5768 {
5769 fieldp = fip->baseclasses;
5770 fip->baseclasses = fieldp->next;
5771 }
5772
5773 TYPE_FIELD (type, nfields) = fieldp->field;
5774 switch (fieldp->accessibility)
c906108c 5775 {
c5aa993b 5776 case DW_ACCESS_private:
b4ba55a1
JB
5777 if (cu->language != language_ada)
5778 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 5779 break;
c906108c 5780
c5aa993b 5781 case DW_ACCESS_protected:
b4ba55a1
JB
5782 if (cu->language != language_ada)
5783 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 5784 break;
c906108c 5785
c5aa993b
JM
5786 case DW_ACCESS_public:
5787 break;
c906108c 5788
c5aa993b
JM
5789 default:
5790 /* Unknown accessibility. Complain and treat it as public. */
5791 {
e2e0b3e5 5792 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 5793 fieldp->accessibility);
c5aa993b
JM
5794 }
5795 break;
c906108c
SS
5796 }
5797 if (nfields < fip->nbaseclasses)
5798 {
7d0ccb61 5799 switch (fieldp->virtuality)
c906108c 5800 {
c5aa993b
JM
5801 case DW_VIRTUALITY_virtual:
5802 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
5803 if (cu->language == language_ada)
5804 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
5805 SET_TYPE_FIELD_VIRTUAL (type, nfields);
5806 break;
c906108c
SS
5807 }
5808 }
c906108c
SS
5809 }
5810}
5811
c906108c
SS
5812/* Add a member function to the proper fieldlist. */
5813
5814static void
107d2387 5815dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 5816 struct type *type, struct dwarf2_cu *cu)
c906108c 5817{
e7c27a73 5818 struct objfile *objfile = cu->objfile;
c906108c
SS
5819 struct attribute *attr;
5820 struct fnfieldlist *flp;
5821 int i;
5822 struct fn_field *fnp;
5823 char *fieldname;
5824 char *physname;
5825 struct nextfnfield *new_fnfield;
f792889a 5826 struct type *this_type;
c906108c 5827
b4ba55a1
JB
5828 if (cu->language == language_ada)
5829 error ("unexpected member function in Ada type");
5830
2df3850c 5831 /* Get name of member function. */
39cbfefa
DJ
5832 fieldname = dwarf2_name (die, cu);
5833 if (fieldname == NULL)
2df3850c 5834 return;
c906108c 5835
2df3850c 5836 /* Get the mangled name. */
94af9270 5837 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c
SS
5838
5839 /* Look up member function name in fieldlist. */
5840 for (i = 0; i < fip->nfnfields; i++)
5841 {
27bfe10e 5842 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
5843 break;
5844 }
5845
5846 /* Create new list element if necessary. */
5847 if (i < fip->nfnfields)
5848 flp = &fip->fnfieldlists[i];
5849 else
5850 {
5851 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
5852 {
5853 fip->fnfieldlists = (struct fnfieldlist *)
5854 xrealloc (fip->fnfieldlists,
5855 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 5856 * sizeof (struct fnfieldlist));
c906108c 5857 if (fip->nfnfields == 0)
c13c43fd 5858 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
5859 }
5860 flp = &fip->fnfieldlists[fip->nfnfields];
5861 flp->name = fieldname;
5862 flp->length = 0;
5863 flp->head = NULL;
5864 fip->nfnfields++;
5865 }
5866
5867 /* Create a new member function field and chain it to the field list
5868 entry. */
5869 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 5870 make_cleanup (xfree, new_fnfield);
c906108c
SS
5871 memset (new_fnfield, 0, sizeof (struct nextfnfield));
5872 new_fnfield->next = flp->head;
5873 flp->head = new_fnfield;
5874 flp->length++;
5875
5876 /* Fill in the member function field info. */
5877 fnp = &new_fnfield->fnfield;
d8151005
DJ
5878 /* The name is already allocated along with this objfile, so we don't
5879 need to duplicate it for the type. */
5880 fnp->physname = physname ? physname : "";
c906108c 5881 fnp->type = alloc_type (objfile);
f792889a
DJ
5882 this_type = read_type_die (die, cu);
5883 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 5884 {
f792889a 5885 int nparams = TYPE_NFIELDS (this_type);
c906108c 5886
f792889a 5887 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
5888 of the method itself (TYPE_CODE_METHOD). */
5889 smash_to_method_type (fnp->type, type,
f792889a
DJ
5890 TYPE_TARGET_TYPE (this_type),
5891 TYPE_FIELDS (this_type),
5892 TYPE_NFIELDS (this_type),
5893 TYPE_VARARGS (this_type));
c906108c
SS
5894
5895 /* Handle static member functions.
c5aa993b
JM
5896 Dwarf2 has no clean way to discern C++ static and non-static
5897 member functions. G++ helps GDB by marking the first
5898 parameter for non-static member functions (which is the
5899 this pointer) as artificial. We obtain this information
5900 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 5901 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
5902 fnp->voffset = VOFFSET_STATIC;
5903 }
5904 else
e2e0b3e5 5905 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 5906 physname);
c906108c
SS
5907
5908 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 5909 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 5910 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
5911
5912 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
5913 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
5914
5915 /* Get accessibility. */
e142c38c 5916 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
5917 if (attr)
5918 {
5919 switch (DW_UNSND (attr))
5920 {
c5aa993b
JM
5921 case DW_ACCESS_private:
5922 fnp->is_private = 1;
5923 break;
5924 case DW_ACCESS_protected:
5925 fnp->is_protected = 1;
5926 break;
c906108c
SS
5927 }
5928 }
5929
b02dede2 5930 /* Check for artificial methods. */
e142c38c 5931 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
5932 if (attr && DW_UNSND (attr) != 0)
5933 fnp->is_artificial = 1;
5934
0d564a31 5935 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
5936 function. For older versions of GCC, this is an offset in the
5937 appropriate virtual table, as specified by DW_AT_containing_type.
5938 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
5939 to the object address. */
5940
e142c38c 5941 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 5942 if (attr)
8e19ed76 5943 {
aec5aa8b 5944 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 5945 {
aec5aa8b
TT
5946 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
5947 {
5948 /* Old-style GCC. */
5949 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
5950 }
5951 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
5952 || (DW_BLOCK (attr)->size > 1
5953 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
5954 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
5955 {
5956 struct dwarf_block blk;
5957 int offset;
5958
5959 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
5960 ? 1 : 2);
5961 blk.size = DW_BLOCK (attr)->size - offset;
5962 blk.data = DW_BLOCK (attr)->data + offset;
5963 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
5964 if ((fnp->voffset % cu->header.addr_size) != 0)
5965 dwarf2_complex_location_expr_complaint ();
5966 else
5967 fnp->voffset /= cu->header.addr_size;
5968 fnp->voffset += 2;
5969 }
5970 else
5971 dwarf2_complex_location_expr_complaint ();
5972
5973 if (!fnp->fcontext)
5974 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
5975 }
3690dd37 5976 else if (attr_form_is_section_offset (attr))
8e19ed76 5977 {
4d3c2250 5978 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
5979 }
5980 else
5981 {
4d3c2250
KB
5982 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
5983 fieldname);
8e19ed76 5984 }
0d564a31 5985 }
d48cc9dd
DJ
5986 else
5987 {
5988 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5989 if (attr && DW_UNSND (attr))
5990 {
5991 /* GCC does this, as of 2008-08-25; PR debug/37237. */
5992 complaint (&symfile_complaints,
5993 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
5994 fieldname, die->offset);
9655fd1a 5995 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
5996 TYPE_CPLUS_DYNAMIC (type) = 1;
5997 }
5998 }
c906108c
SS
5999}
6000
6001/* Create the vector of member function fields, and attach it to the type. */
6002
6003static void
fba45db2 6004dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6005 struct dwarf2_cu *cu)
c906108c
SS
6006{
6007 struct fnfieldlist *flp;
6008 int total_length = 0;
6009 int i;
6010
b4ba55a1
JB
6011 if (cu->language == language_ada)
6012 error ("unexpected member functions in Ada type");
6013
c906108c
SS
6014 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6015 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6016 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6017
6018 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6019 {
6020 struct nextfnfield *nfp = flp->head;
6021 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6022 int k;
6023
6024 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6025 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6026 fn_flp->fn_fields = (struct fn_field *)
6027 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6028 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6029 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6030
6031 total_length += flp->length;
6032 }
6033
6034 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6035 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6036}
6037
1168df01
JB
6038/* Returns non-zero if NAME is the name of a vtable member in CU's
6039 language, zero otherwise. */
6040static int
6041is_vtable_name (const char *name, struct dwarf2_cu *cu)
6042{
6043 static const char vptr[] = "_vptr";
987504bb 6044 static const char vtable[] = "vtable";
1168df01 6045
987504bb
JJ
6046 /* Look for the C++ and Java forms of the vtable. */
6047 if ((cu->language == language_java
6048 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6049 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6050 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6051 return 1;
6052
6053 return 0;
6054}
6055
c0dd20ea 6056/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6057 functions, with the ABI-specified layout. If TYPE describes
6058 such a structure, smash it into a member function type.
61049d3b
DJ
6059
6060 GCC shouldn't do this; it should just output pointer to member DIEs.
6061 This is GCC PR debug/28767. */
c0dd20ea 6062
0b92b5bb
TT
6063static void
6064quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6065{
0b92b5bb 6066 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6067
6068 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6069 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6070 return;
c0dd20ea
DJ
6071
6072 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6073 if (TYPE_FIELD_NAME (type, 0) == NULL
6074 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6075 || TYPE_FIELD_NAME (type, 1) == NULL
6076 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6077 return;
c0dd20ea
DJ
6078
6079 /* Find the type of the method. */
0b92b5bb 6080 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6081 if (pfn_type == NULL
6082 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6083 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6084 return;
c0dd20ea
DJ
6085
6086 /* Look for the "this" argument. */
6087 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6088 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6089 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6090 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6091 return;
c0dd20ea
DJ
6092
6093 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6094 new_type = alloc_type (objfile);
6095 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6096 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6097 TYPE_VARARGS (pfn_type));
0b92b5bb 6098 smash_to_methodptr_type (type, new_type);
c0dd20ea 6099}
1168df01 6100
c906108c
SS
6101/* Called when we find the DIE that starts a structure or union scope
6102 (definition) to process all dies that define the members of the
6103 structure or union.
6104
6105 NOTE: we need to call struct_type regardless of whether or not the
6106 DIE has an at_name attribute, since it might be an anonymous
6107 structure or union. This gets the type entered into our set of
6108 user defined types.
6109
6110 However, if the structure is incomplete (an opaque struct/union)
6111 then suppress creating a symbol table entry for it since gdb only
6112 wants to find the one with the complete definition. Note that if
6113 it is complete, we just call new_symbol, which does it's own
6114 checking about whether the struct/union is anonymous or not (and
6115 suppresses creating a symbol table entry itself). */
6116
f792889a 6117static struct type *
134d01f1 6118read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6119{
e7c27a73 6120 struct objfile *objfile = cu->objfile;
c906108c
SS
6121 struct type *type;
6122 struct attribute *attr;
39cbfefa 6123 char *name;
d3f41bb1 6124 struct cleanup *back_to;
c906108c 6125
348e048f
DE
6126 /* If the definition of this type lives in .debug_types, read that type.
6127 Don't follow DW_AT_specification though, that will take us back up
6128 the chain and we want to go down. */
6129 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6130 if (attr)
6131 {
6132 struct dwarf2_cu *type_cu = cu;
6133 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6134
348e048f
DE
6135 /* We could just recurse on read_structure_type, but we need to call
6136 get_die_type to ensure only one type for this DIE is created.
6137 This is important, for example, because for c++ classes we need
6138 TYPE_NAME set which is only done by new_symbol. Blech. */
6139 type = read_type_die (type_die, type_cu);
6140 return set_die_type (die, type, cu);
6141 }
6142
d3f41bb1
TT
6143 back_to = make_cleanup (null_cleanup, 0);
6144
c0dd20ea 6145 type = alloc_type (objfile);
c906108c 6146 INIT_CPLUS_SPECIFIC (type);
93311388 6147
39cbfefa
DJ
6148 name = dwarf2_name (die, cu);
6149 if (name != NULL)
c906108c 6150 {
987504bb
JJ
6151 if (cu->language == language_cplus
6152 || cu->language == language_java)
63d06c5c 6153 {
94af9270
KS
6154 TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6155 if (die->tag == DW_TAG_structure_type
6156 || die->tag == DW_TAG_class_type)
6157 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6158 }
6159 else
6160 {
d8151005
DJ
6161 /* The name is already allocated along with this objfile, so
6162 we don't need to duplicate it for the type. */
94af9270
KS
6163 TYPE_TAG_NAME (type) = (char *) name;
6164 if (die->tag == DW_TAG_class_type)
6165 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6166 }
c906108c
SS
6167 }
6168
6169 if (die->tag == DW_TAG_structure_type)
6170 {
6171 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6172 }
6173 else if (die->tag == DW_TAG_union_type)
6174 {
6175 TYPE_CODE (type) = TYPE_CODE_UNION;
6176 }
6177 else
6178 {
c906108c
SS
6179 TYPE_CODE (type) = TYPE_CODE_CLASS;
6180 }
6181
0cc2414c
TT
6182 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6183 TYPE_DECLARED_CLASS (type) = 1;
6184
e142c38c 6185 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6186 if (attr)
6187 {
6188 TYPE_LENGTH (type) = DW_UNSND (attr);
6189 }
6190 else
6191 {
6192 TYPE_LENGTH (type) = 0;
6193 }
6194
876cecd0 6195 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6196 if (die_is_declaration (die, cu))
876cecd0 6197 TYPE_STUB (type) = 1;
a6c727b2
DJ
6198 else if (attr == NULL && die->child == NULL
6199 && producer_is_realview (cu->producer))
6200 /* RealView does not output the required DW_AT_declaration
6201 on incomplete types. */
6202 TYPE_STUB (type) = 1;
dc718098 6203
c906108c
SS
6204 /* We need to add the type field to the die immediately so we don't
6205 infinitely recurse when dealing with pointers to the structure
6206 type within the structure itself. */
1c379e20 6207 set_die_type (die, type, cu);
c906108c 6208
7e314c57
JK
6209 /* set_die_type should be already done. */
6210 set_descriptive_type (type, die, cu);
6211
e142c38c 6212 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6213 {
6214 struct field_info fi;
6215 struct die_info *child_die;
c906108c
SS
6216
6217 memset (&fi, 0, sizeof (struct field_info));
6218
639d11d3 6219 child_die = die->child;
c906108c
SS
6220
6221 while (child_die && child_die->tag)
6222 {
a9a9bd0f
DC
6223 if (child_die->tag == DW_TAG_member
6224 || child_die->tag == DW_TAG_variable)
c906108c 6225 {
a9a9bd0f
DC
6226 /* NOTE: carlton/2002-11-05: A C++ static data member
6227 should be a DW_TAG_member that is a declaration, but
6228 all versions of G++ as of this writing (so through at
6229 least 3.2.1) incorrectly generate DW_TAG_variable
6230 tags for them instead. */
e7c27a73 6231 dwarf2_add_field (&fi, child_die, cu);
c906108c 6232 }
8713b1b1 6233 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6234 {
6235 /* C++ member function. */
e7c27a73 6236 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6237 }
6238 else if (child_die->tag == DW_TAG_inheritance)
6239 {
6240 /* C++ base class field. */
e7c27a73 6241 dwarf2_add_field (&fi, child_die, cu);
c906108c 6242 }
98751a41
JK
6243 else if (child_die->tag == DW_TAG_typedef)
6244 dwarf2_add_typedef (&fi, child_die, cu);
c906108c
SS
6245 child_die = sibling_die (child_die);
6246 }
6247
6248 /* Attach fields and member functions to the type. */
6249 if (fi.nfields)
e7c27a73 6250 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6251 if (fi.nfnfields)
6252 {
e7c27a73 6253 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6254
c5aa993b 6255 /* Get the type which refers to the base class (possibly this
c906108c 6256 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6257 class from the DW_AT_containing_type attribute. This use of
6258 DW_AT_containing_type is a GNU extension. */
c906108c 6259
e142c38c 6260 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6261 {
e7c27a73 6262 struct type *t = die_containing_type (die, cu);
c906108c
SS
6263
6264 TYPE_VPTR_BASETYPE (type) = t;
6265 if (type == t)
6266 {
c906108c
SS
6267 int i;
6268
6269 /* Our own class provides vtbl ptr. */
6270 for (i = TYPE_NFIELDS (t) - 1;
6271 i >= TYPE_N_BASECLASSES (t);
6272 --i)
6273 {
6274 char *fieldname = TYPE_FIELD_NAME (t, i);
6275
1168df01 6276 if (is_vtable_name (fieldname, cu))
c906108c
SS
6277 {
6278 TYPE_VPTR_FIELDNO (type) = i;
6279 break;
6280 }
6281 }
6282
6283 /* Complain if virtual function table field not found. */
6284 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6285 complaint (&symfile_complaints,
e2e0b3e5 6286 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6287 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6288 "");
c906108c
SS
6289 }
6290 else
6291 {
6292 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6293 }
6294 }
f6235d4c
EZ
6295 else if (cu->producer
6296 && strncmp (cu->producer,
6297 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6298 {
6299 /* The IBM XLC compiler does not provide direct indication
6300 of the containing type, but the vtable pointer is
6301 always named __vfp. */
6302
6303 int i;
6304
6305 for (i = TYPE_NFIELDS (type) - 1;
6306 i >= TYPE_N_BASECLASSES (type);
6307 --i)
6308 {
6309 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6310 {
6311 TYPE_VPTR_FIELDNO (type) = i;
6312 TYPE_VPTR_BASETYPE (type) = type;
6313 break;
6314 }
6315 }
6316 }
c906108c 6317 }
98751a41
JK
6318
6319 /* Copy fi.typedef_field_list linked list elements content into the
6320 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6321 if (fi.typedef_field_list)
6322 {
6323 int i = fi.typedef_field_list_count;
6324
a0d7a4ff 6325 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6326 TYPE_TYPEDEF_FIELD_ARRAY (type)
6327 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6328 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6329
6330 /* Reverse the list order to keep the debug info elements order. */
6331 while (--i >= 0)
6332 {
6333 struct typedef_field *dest, *src;
6e70227d 6334
98751a41
JK
6335 dest = &TYPE_TYPEDEF_FIELD (type, i);
6336 src = &fi.typedef_field_list->field;
6337 fi.typedef_field_list = fi.typedef_field_list->next;
6338 *dest = *src;
6339 }
6340 }
c906108c 6341 }
63d06c5c 6342
0b92b5bb
TT
6343 quirk_gcc_member_function_pointer (type, cu->objfile);
6344
0114d602 6345 do_cleanups (back_to);
f792889a 6346 return type;
c906108c
SS
6347}
6348
134d01f1
DJ
6349static void
6350process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6351{
90aeadfc 6352 struct die_info *child_die = die->child;
f792889a 6353 struct type *this_type;
c906108c 6354
f792889a
DJ
6355 this_type = get_die_type (die, cu);
6356 if (this_type == NULL)
6357 this_type = read_structure_type (die, cu);
c906108c 6358
90aeadfc
DC
6359 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6360 snapshots) has been known to create a die giving a declaration
6361 for a class that has, as a child, a die giving a definition for a
6362 nested class. So we have to process our children even if the
6363 current die is a declaration. Normally, of course, a declaration
6364 won't have any children at all. */
134d01f1 6365
90aeadfc
DC
6366 while (child_die != NULL && child_die->tag)
6367 {
6368 if (child_die->tag == DW_TAG_member
6369 || child_die->tag == DW_TAG_variable
6370 || child_die->tag == DW_TAG_inheritance)
134d01f1 6371 {
90aeadfc 6372 /* Do nothing. */
134d01f1 6373 }
90aeadfc
DC
6374 else
6375 process_die (child_die, cu);
134d01f1 6376
90aeadfc 6377 child_die = sibling_die (child_die);
134d01f1
DJ
6378 }
6379
fa4028e9
JB
6380 /* Do not consider external references. According to the DWARF standard,
6381 these DIEs are identified by the fact that they have no byte_size
6382 attribute, and a declaration attribute. */
6383 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6384 || !die_is_declaration (die, cu))
f792889a 6385 new_symbol (die, this_type, cu);
134d01f1
DJ
6386}
6387
6388/* Given a DW_AT_enumeration_type die, set its type. We do not
6389 complete the type's fields yet, or create any symbols. */
c906108c 6390
f792889a 6391static struct type *
134d01f1 6392read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6393{
e7c27a73 6394 struct objfile *objfile = cu->objfile;
c906108c 6395 struct type *type;
c906108c 6396 struct attribute *attr;
0114d602 6397 const char *name;
134d01f1 6398
348e048f
DE
6399 /* If the definition of this type lives in .debug_types, read that type.
6400 Don't follow DW_AT_specification though, that will take us back up
6401 the chain and we want to go down. */
6402 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6403 if (attr)
6404 {
6405 struct dwarf2_cu *type_cu = cu;
6406 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6407
348e048f
DE
6408 type = read_type_die (type_die, type_cu);
6409 return set_die_type (die, type, cu);
6410 }
6411
c906108c
SS
6412 type = alloc_type (objfile);
6413
6414 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6415 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6416 if (name != NULL)
0114d602 6417 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6418
e142c38c 6419 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6420 if (attr)
6421 {
6422 TYPE_LENGTH (type) = DW_UNSND (attr);
6423 }
6424 else
6425 {
6426 TYPE_LENGTH (type) = 0;
6427 }
6428
137033e9
JB
6429 /* The enumeration DIE can be incomplete. In Ada, any type can be
6430 declared as private in the package spec, and then defined only
6431 inside the package body. Such types are known as Taft Amendment
6432 Types. When another package uses such a type, an incomplete DIE
6433 may be generated by the compiler. */
02eb380e 6434 if (die_is_declaration (die, cu))
876cecd0 6435 TYPE_STUB (type) = 1;
02eb380e 6436
f792889a 6437 return set_die_type (die, type, cu);
134d01f1
DJ
6438}
6439
6440/* Given a pointer to a die which begins an enumeration, process all
6441 the dies that define the members of the enumeration, and create the
6442 symbol for the enumeration type.
6443
6444 NOTE: We reverse the order of the element list. */
6445
6446static void
6447process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6448{
134d01f1
DJ
6449 struct die_info *child_die;
6450 struct field *fields;
134d01f1
DJ
6451 struct symbol *sym;
6452 int num_fields;
6453 int unsigned_enum = 1;
39cbfefa 6454 char *name;
f792889a 6455 struct type *this_type;
134d01f1 6456
c906108c
SS
6457 num_fields = 0;
6458 fields = NULL;
f792889a
DJ
6459 this_type = get_die_type (die, cu);
6460 if (this_type == NULL)
6461 this_type = read_enumeration_type (die, cu);
639d11d3 6462 if (die->child != NULL)
c906108c 6463 {
639d11d3 6464 child_die = die->child;
c906108c
SS
6465 while (child_die && child_die->tag)
6466 {
6467 if (child_die->tag != DW_TAG_enumerator)
6468 {
e7c27a73 6469 process_die (child_die, cu);
c906108c
SS
6470 }
6471 else
6472 {
39cbfefa
DJ
6473 name = dwarf2_name (child_die, cu);
6474 if (name)
c906108c 6475 {
f792889a 6476 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
6477 if (SYMBOL_VALUE (sym) < 0)
6478 unsigned_enum = 0;
6479
6480 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6481 {
6482 fields = (struct field *)
6483 xrealloc (fields,
6484 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6485 * sizeof (struct field));
c906108c
SS
6486 }
6487
3567439c 6488 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 6489 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 6490 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
6491 FIELD_BITSIZE (fields[num_fields]) = 0;
6492
6493 num_fields++;
6494 }
6495 }
6496
6497 child_die = sibling_die (child_die);
6498 }
6499
6500 if (num_fields)
6501 {
f792889a
DJ
6502 TYPE_NFIELDS (this_type) = num_fields;
6503 TYPE_FIELDS (this_type) = (struct field *)
6504 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6505 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 6506 sizeof (struct field) * num_fields);
b8c9b27d 6507 xfree (fields);
c906108c
SS
6508 }
6509 if (unsigned_enum)
876cecd0 6510 TYPE_UNSIGNED (this_type) = 1;
c906108c 6511 }
134d01f1 6512
f792889a 6513 new_symbol (die, this_type, cu);
c906108c
SS
6514}
6515
6516/* Extract all information from a DW_TAG_array_type DIE and put it in
6517 the DIE's type field. For now, this only handles one dimensional
6518 arrays. */
6519
f792889a 6520static struct type *
e7c27a73 6521read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6522{
e7c27a73 6523 struct objfile *objfile = cu->objfile;
c906108c 6524 struct die_info *child_die;
7e314c57 6525 struct type *type;
c906108c
SS
6526 struct type *element_type, *range_type, *index_type;
6527 struct type **range_types = NULL;
6528 struct attribute *attr;
6529 int ndim = 0;
6530 struct cleanup *back_to;
39cbfefa 6531 char *name;
c906108c 6532
e7c27a73 6533 element_type = die_type (die, cu);
c906108c 6534
7e314c57
JK
6535 /* The die_type call above may have already set the type for this DIE. */
6536 type = get_die_type (die, cu);
6537 if (type)
6538 return type;
6539
c906108c
SS
6540 /* Irix 6.2 native cc creates array types without children for
6541 arrays with unspecified length. */
639d11d3 6542 if (die->child == NULL)
c906108c 6543 {
46bf5051 6544 index_type = objfile_type (objfile)->builtin_int;
c906108c 6545 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
6546 type = create_array_type (NULL, element_type, range_type);
6547 return set_die_type (die, type, cu);
c906108c
SS
6548 }
6549
6550 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 6551 child_die = die->child;
c906108c
SS
6552 while (child_die && child_die->tag)
6553 {
6554 if (child_die->tag == DW_TAG_subrange_type)
6555 {
f792889a 6556 struct type *child_type = read_type_die (child_die, cu);
9a619af0 6557
f792889a 6558 if (child_type != NULL)
a02abb62
JB
6559 {
6560 /* The range type was succesfully read. Save it for
6561 the array type creation. */
6562 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
6563 {
6564 range_types = (struct type **)
6565 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
6566 * sizeof (struct type *));
6567 if (ndim == 0)
6568 make_cleanup (free_current_contents, &range_types);
6569 }
f792889a 6570 range_types[ndim++] = child_type;
a02abb62 6571 }
c906108c
SS
6572 }
6573 child_die = sibling_die (child_die);
6574 }
6575
6576 /* Dwarf2 dimensions are output from left to right, create the
6577 necessary array types in backwards order. */
7ca2d3a3 6578
c906108c 6579 type = element_type;
7ca2d3a3
DL
6580
6581 if (read_array_order (die, cu) == DW_ORD_col_major)
6582 {
6583 int i = 0;
9a619af0 6584
7ca2d3a3
DL
6585 while (i < ndim)
6586 type = create_array_type (NULL, type, range_types[i++]);
6587 }
6588 else
6589 {
6590 while (ndim-- > 0)
6591 type = create_array_type (NULL, type, range_types[ndim]);
6592 }
c906108c 6593
f5f8a009
EZ
6594 /* Understand Dwarf2 support for vector types (like they occur on
6595 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
6596 array type. This is not part of the Dwarf2/3 standard yet, but a
6597 custom vendor extension. The main difference between a regular
6598 array and the vector variant is that vectors are passed by value
6599 to functions. */
e142c38c 6600 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 6601 if (attr)
ea37ba09 6602 make_vector_type (type);
f5f8a009 6603
39cbfefa
DJ
6604 name = dwarf2_name (die, cu);
6605 if (name)
6606 TYPE_NAME (type) = name;
6e70227d 6607
7e314c57
JK
6608 /* Install the type in the die. */
6609 set_die_type (die, type, cu);
6610
6611 /* set_die_type should be already done. */
b4ba55a1
JB
6612 set_descriptive_type (type, die, cu);
6613
c906108c
SS
6614 do_cleanups (back_to);
6615
7e314c57 6616 return type;
c906108c
SS
6617}
6618
7ca2d3a3 6619static enum dwarf_array_dim_ordering
6e70227d 6620read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
6621{
6622 struct attribute *attr;
6623
6624 attr = dwarf2_attr (die, DW_AT_ordering, cu);
6625
6626 if (attr) return DW_SND (attr);
6627
6628 /*
6629 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 6630 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
6631 laid out as per normal fortran.
6632
6e70227d 6633 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
6634 version checking.
6635 */
6636
905e0470
PM
6637 if (cu->language == language_fortran
6638 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
6639 {
6640 return DW_ORD_row_major;
6641 }
6642
6e70227d 6643 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
6644 {
6645 case array_column_major:
6646 return DW_ORD_col_major;
6647 case array_row_major:
6648 default:
6649 return DW_ORD_row_major;
6650 };
6651}
6652
72019c9c
GM
6653/* Extract all information from a DW_TAG_set_type DIE and put it in
6654 the DIE's type field. */
6655
f792889a 6656static struct type *
72019c9c
GM
6657read_set_type (struct die_info *die, struct dwarf2_cu *cu)
6658{
7e314c57
JK
6659 struct type *domain_type, *set_type;
6660 struct attribute *attr;
f792889a 6661
7e314c57
JK
6662 domain_type = die_type (die, cu);
6663
6664 /* The die_type call above may have already set the type for this DIE. */
6665 set_type = get_die_type (die, cu);
6666 if (set_type)
6667 return set_type;
6668
6669 set_type = create_set_type (NULL, domain_type);
6670
6671 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
6672 if (attr)
6673 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 6674
f792889a 6675 return set_die_type (die, set_type, cu);
72019c9c 6676}
7ca2d3a3 6677
c906108c
SS
6678/* First cut: install each common block member as a global variable. */
6679
6680static void
e7c27a73 6681read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6682{
6683 struct die_info *child_die;
6684 struct attribute *attr;
6685 struct symbol *sym;
6686 CORE_ADDR base = (CORE_ADDR) 0;
6687
e142c38c 6688 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
6689 if (attr)
6690 {
8e19ed76
PS
6691 /* Support the .debug_loc offsets */
6692 if (attr_form_is_block (attr))
6693 {
e7c27a73 6694 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 6695 }
3690dd37 6696 else if (attr_form_is_section_offset (attr))
8e19ed76 6697 {
4d3c2250 6698 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6699 }
6700 else
6701 {
4d3c2250
KB
6702 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6703 "common block member");
8e19ed76 6704 }
c906108c 6705 }
639d11d3 6706 if (die->child != NULL)
c906108c 6707 {
639d11d3 6708 child_die = die->child;
c906108c
SS
6709 while (child_die && child_die->tag)
6710 {
e7c27a73 6711 sym = new_symbol (child_die, NULL, cu);
e142c38c 6712 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
6713 if (attr)
6714 {
d4b96c9a
JK
6715 CORE_ADDR byte_offset = 0;
6716
6717 if (attr_form_is_section_offset (attr))
6718 dwarf2_complex_location_expr_complaint ();
6719 else if (attr_form_is_constant (attr))
6720 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6721 else if (attr_form_is_block (attr))
6722 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6723 else
6724 dwarf2_complex_location_expr_complaint ();
6725
6726 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
6727 add_symbol_to_list (sym, &global_symbols);
6728 }
6729 child_die = sibling_die (child_die);
6730 }
6731 }
6732}
6733
0114d602 6734/* Create a type for a C++ namespace. */
d9fa45fe 6735
0114d602
DJ
6736static struct type *
6737read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 6738{
e7c27a73 6739 struct objfile *objfile = cu->objfile;
0114d602 6740 const char *previous_prefix, *name;
9219021c 6741 int is_anonymous;
0114d602
DJ
6742 struct type *type;
6743
6744 /* For extensions, reuse the type of the original namespace. */
6745 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
6746 {
6747 struct die_info *ext_die;
6748 struct dwarf2_cu *ext_cu = cu;
9a619af0 6749
0114d602
DJ
6750 ext_die = dwarf2_extension (die, &ext_cu);
6751 type = read_type_die (ext_die, ext_cu);
6752 return set_die_type (die, type, cu);
6753 }
9219021c 6754
e142c38c 6755 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
6756
6757 /* Now build the name of the current namespace. */
6758
0114d602
DJ
6759 previous_prefix = determine_prefix (die, cu);
6760 if (previous_prefix[0] != '\0')
6761 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 6762 previous_prefix, name, 0, cu);
0114d602
DJ
6763
6764 /* Create the type. */
6765 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
6766 objfile);
6767 TYPE_NAME (type) = (char *) name;
6768 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6769
60531b24 6770 return set_die_type (die, type, cu);
0114d602
DJ
6771}
6772
6773/* Read a C++ namespace. */
6774
6775static void
6776read_namespace (struct die_info *die, struct dwarf2_cu *cu)
6777{
6778 struct objfile *objfile = cu->objfile;
6779 const char *name;
6780 int is_anonymous;
9219021c 6781
5c4e30ca
DC
6782 /* Add a symbol associated to this if we haven't seen the namespace
6783 before. Also, add a using directive if it's an anonymous
6784 namespace. */
9219021c 6785
f2f0e013 6786 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
6787 {
6788 struct type *type;
6789
0114d602 6790 type = read_type_die (die, cu);
e7c27a73 6791 new_symbol (die, type, cu);
5c4e30ca 6792
0114d602 6793 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 6794 if (is_anonymous)
0114d602
DJ
6795 {
6796 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 6797
c0cc3a76 6798 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 6799 NULL, &objfile->objfile_obstack);
0114d602 6800 }
5c4e30ca 6801 }
9219021c 6802
639d11d3 6803 if (die->child != NULL)
d9fa45fe 6804 {
639d11d3 6805 struct die_info *child_die = die->child;
6e70227d 6806
d9fa45fe
DC
6807 while (child_die && child_die->tag)
6808 {
e7c27a73 6809 process_die (child_die, cu);
d9fa45fe
DC
6810 child_die = sibling_die (child_die);
6811 }
6812 }
38d518c9
EZ
6813}
6814
f55ee35c
JK
6815/* Read a Fortran module as type. This DIE can be only a declaration used for
6816 imported module. Still we need that type as local Fortran "use ... only"
6817 declaration imports depend on the created type in determine_prefix. */
6818
6819static struct type *
6820read_module_type (struct die_info *die, struct dwarf2_cu *cu)
6821{
6822 struct objfile *objfile = cu->objfile;
6823 char *module_name;
6824 struct type *type;
6825
6826 module_name = dwarf2_name (die, cu);
6827 if (!module_name)
6828 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
6829 die->offset);
6830 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
6831
6832 /* determine_prefix uses TYPE_TAG_NAME. */
6833 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6834
6835 return set_die_type (die, type, cu);
6836}
6837
5d7cb8df
JK
6838/* Read a Fortran module. */
6839
6840static void
6841read_module (struct die_info *die, struct dwarf2_cu *cu)
6842{
6843 struct die_info *child_die = die->child;
6844
5d7cb8df
JK
6845 while (child_die && child_die->tag)
6846 {
6847 process_die (child_die, cu);
6848 child_die = sibling_die (child_die);
6849 }
6850}
6851
38d518c9
EZ
6852/* Return the name of the namespace represented by DIE. Set
6853 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
6854 namespace. */
6855
6856static const char *
e142c38c 6857namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
6858{
6859 struct die_info *current_die;
6860 const char *name = NULL;
6861
6862 /* Loop through the extensions until we find a name. */
6863
6864 for (current_die = die;
6865 current_die != NULL;
f2f0e013 6866 current_die = dwarf2_extension (die, &cu))
38d518c9 6867 {
e142c38c 6868 name = dwarf2_name (current_die, cu);
38d518c9
EZ
6869 if (name != NULL)
6870 break;
6871 }
6872
6873 /* Is it an anonymous namespace? */
6874
6875 *is_anonymous = (name == NULL);
6876 if (*is_anonymous)
6877 name = "(anonymous namespace)";
6878
6879 return name;
d9fa45fe
DC
6880}
6881
c906108c
SS
6882/* Extract all information from a DW_TAG_pointer_type DIE and add to
6883 the user defined type vector. */
6884
f792889a 6885static struct type *
e7c27a73 6886read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6887{
5e2b427d 6888 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 6889 struct comp_unit_head *cu_header = &cu->header;
c906108c 6890 struct type *type;
8b2dbe47
KB
6891 struct attribute *attr_byte_size;
6892 struct attribute *attr_address_class;
6893 int byte_size, addr_class;
7e314c57
JK
6894 struct type *target_type;
6895
6896 target_type = die_type (die, cu);
c906108c 6897
7e314c57
JK
6898 /* The die_type call above may have already set the type for this DIE. */
6899 type = get_die_type (die, cu);
6900 if (type)
6901 return type;
6902
6903 type = lookup_pointer_type (target_type);
8b2dbe47 6904
e142c38c 6905 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
6906 if (attr_byte_size)
6907 byte_size = DW_UNSND (attr_byte_size);
c906108c 6908 else
8b2dbe47
KB
6909 byte_size = cu_header->addr_size;
6910
e142c38c 6911 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
6912 if (attr_address_class)
6913 addr_class = DW_UNSND (attr_address_class);
6914 else
6915 addr_class = DW_ADDR_none;
6916
6917 /* If the pointer size or address class is different than the
6918 default, create a type variant marked as such and set the
6919 length accordingly. */
6920 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 6921 {
5e2b427d 6922 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
6923 {
6924 int type_flags;
6925
849957d9 6926 type_flags = gdbarch_address_class_type_flags
5e2b427d 6927 (gdbarch, byte_size, addr_class);
876cecd0
TT
6928 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
6929 == 0);
8b2dbe47
KB
6930 type = make_type_with_address_space (type, type_flags);
6931 }
6932 else if (TYPE_LENGTH (type) != byte_size)
6933 {
e2e0b3e5 6934 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 6935 }
6e70227d 6936 else
9a619af0
MS
6937 {
6938 /* Should we also complain about unhandled address classes? */
6939 }
c906108c 6940 }
8b2dbe47
KB
6941
6942 TYPE_LENGTH (type) = byte_size;
f792889a 6943 return set_die_type (die, type, cu);
c906108c
SS
6944}
6945
6946/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
6947 the user defined type vector. */
6948
f792889a 6949static struct type *
e7c27a73 6950read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6951{
6952 struct type *type;
6953 struct type *to_type;
6954 struct type *domain;
6955
e7c27a73
DJ
6956 to_type = die_type (die, cu);
6957 domain = die_containing_type (die, cu);
0d5de010 6958
7e314c57
JK
6959 /* The calls above may have already set the type for this DIE. */
6960 type = get_die_type (die, cu);
6961 if (type)
6962 return type;
6963
0d5de010
DJ
6964 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
6965 type = lookup_methodptr_type (to_type);
6966 else
6967 type = lookup_memberptr_type (to_type, domain);
c906108c 6968
f792889a 6969 return set_die_type (die, type, cu);
c906108c
SS
6970}
6971
6972/* Extract all information from a DW_TAG_reference_type DIE and add to
6973 the user defined type vector. */
6974
f792889a 6975static struct type *
e7c27a73 6976read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6977{
e7c27a73 6978 struct comp_unit_head *cu_header = &cu->header;
7e314c57 6979 struct type *type, *target_type;
c906108c
SS
6980 struct attribute *attr;
6981
7e314c57
JK
6982 target_type = die_type (die, cu);
6983
6984 /* The die_type call above may have already set the type for this DIE. */
6985 type = get_die_type (die, cu);
6986 if (type)
6987 return type;
6988
6989 type = lookup_reference_type (target_type);
e142c38c 6990 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6991 if (attr)
6992 {
6993 TYPE_LENGTH (type) = DW_UNSND (attr);
6994 }
6995 else
6996 {
107d2387 6997 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 6998 }
f792889a 6999 return set_die_type (die, type, cu);
c906108c
SS
7000}
7001
f792889a 7002static struct type *
e7c27a73 7003read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7004{
f792889a 7005 struct type *base_type, *cv_type;
c906108c 7006
e7c27a73 7007 base_type = die_type (die, cu);
7e314c57
JK
7008
7009 /* The die_type call above may have already set the type for this DIE. */
7010 cv_type = get_die_type (die, cu);
7011 if (cv_type)
7012 return cv_type;
7013
f792889a
DJ
7014 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7015 return set_die_type (die, cv_type, cu);
c906108c
SS
7016}
7017
f792889a 7018static struct type *
e7c27a73 7019read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7020{
f792889a 7021 struct type *base_type, *cv_type;
c906108c 7022
e7c27a73 7023 base_type = die_type (die, cu);
7e314c57
JK
7024
7025 /* The die_type call above may have already set the type for this DIE. */
7026 cv_type = get_die_type (die, cu);
7027 if (cv_type)
7028 return cv_type;
7029
f792889a
DJ
7030 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7031 return set_die_type (die, cv_type, cu);
c906108c
SS
7032}
7033
7034/* Extract all information from a DW_TAG_string_type DIE and add to
7035 the user defined type vector. It isn't really a user defined type,
7036 but it behaves like one, with other DIE's using an AT_user_def_type
7037 attribute to reference it. */
7038
f792889a 7039static struct type *
e7c27a73 7040read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7041{
e7c27a73 7042 struct objfile *objfile = cu->objfile;
3b7538c0 7043 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7044 struct type *type, *range_type, *index_type, *char_type;
7045 struct attribute *attr;
7046 unsigned int length;
7047
e142c38c 7048 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7049 if (attr)
7050 {
7051 length = DW_UNSND (attr);
7052 }
7053 else
7054 {
b21b22e0 7055 /* check for the DW_AT_byte_size attribute */
e142c38c 7056 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7057 if (attr)
7058 {
7059 length = DW_UNSND (attr);
7060 }
7061 else
7062 {
7063 length = 1;
7064 }
c906108c 7065 }
6ccb9162 7066
46bf5051 7067 index_type = objfile_type (objfile)->builtin_int;
c906108c 7068 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7069 char_type = language_string_char_type (cu->language_defn, gdbarch);
7070 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7071
f792889a 7072 return set_die_type (die, type, cu);
c906108c
SS
7073}
7074
7075/* Handle DIES due to C code like:
7076
7077 struct foo
c5aa993b
JM
7078 {
7079 int (*funcp)(int a, long l);
7080 int b;
7081 };
c906108c
SS
7082
7083 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7084 */
c906108c 7085
f792889a 7086static struct type *
e7c27a73 7087read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7088{
7089 struct type *type; /* Type that this function returns */
7090 struct type *ftype; /* Function that returns above type */
7091 struct attribute *attr;
7092
e7c27a73 7093 type = die_type (die, cu);
7e314c57
JK
7094
7095 /* The die_type call above may have already set the type for this DIE. */
7096 ftype = get_die_type (die, cu);
7097 if (ftype)
7098 return ftype;
7099
0c8b41f1 7100 ftype = lookup_function_type (type);
c906108c 7101
5b8101ae 7102 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7103 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7104 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7105 || cu->language == language_cplus
5b8101ae
PM
7106 || cu->language == language_java
7107 || cu->language == language_pascal)
876cecd0 7108 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7109 else if (producer_is_realview (cu->producer))
7110 /* RealView does not emit DW_AT_prototyped. We can not
7111 distinguish prototyped and unprototyped functions; default to
7112 prototyped, since that is more common in modern code (and
7113 RealView warns about unprototyped functions). */
7114 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7115
c055b101
CV
7116 /* Store the calling convention in the type if it's available in
7117 the subroutine die. Otherwise set the calling convention to
7118 the default value DW_CC_normal. */
7119 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7120 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7121
7122 /* We need to add the subroutine type to the die immediately so
7123 we don't infinitely recurse when dealing with parameters
7124 declared as the same subroutine type. */
7125 set_die_type (die, ftype, cu);
6e70227d 7126
639d11d3 7127 if (die->child != NULL)
c906108c 7128 {
8072405b 7129 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7130 struct die_info *child_die;
8072405b 7131 int nparams, iparams;
c906108c
SS
7132
7133 /* Count the number of parameters.
7134 FIXME: GDB currently ignores vararg functions, but knows about
7135 vararg member functions. */
8072405b 7136 nparams = 0;
639d11d3 7137 child_die = die->child;
c906108c
SS
7138 while (child_die && child_die->tag)
7139 {
7140 if (child_die->tag == DW_TAG_formal_parameter)
7141 nparams++;
7142 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7143 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7144 child_die = sibling_die (child_die);
7145 }
7146
7147 /* Allocate storage for parameters and fill them in. */
7148 TYPE_NFIELDS (ftype) = nparams;
7149 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7150 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7151
8072405b
JK
7152 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7153 even if we error out during the parameters reading below. */
7154 for (iparams = 0; iparams < nparams; iparams++)
7155 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7156
7157 iparams = 0;
639d11d3 7158 child_die = die->child;
c906108c
SS
7159 while (child_die && child_die->tag)
7160 {
7161 if (child_die->tag == DW_TAG_formal_parameter)
7162 {
7163 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
7164 member functions. G++ helps GDB by marking the first
7165 parameter for non-static member functions (which is the
7166 this pointer) as artificial. We pass this information
7167 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 7168 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7169 if (attr)
7170 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7171 else
418835cc
KS
7172 {
7173 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7174
7175 /* GCC/43521: In java, the formal parameter
7176 "this" is sometimes not marked with DW_AT_artificial. */
7177 if (cu->language == language_java)
7178 {
7179 const char *name = dwarf2_name (child_die, cu);
9a619af0 7180
418835cc
KS
7181 if (name && !strcmp (name, "this"))
7182 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7183 }
7184 }
e7c27a73 7185 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
7186 iparams++;
7187 }
7188 child_die = sibling_die (child_die);
7189 }
7190 }
7191
76c10ea2 7192 return ftype;
c906108c
SS
7193}
7194
f792889a 7195static struct type *
e7c27a73 7196read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7197{
e7c27a73 7198 struct objfile *objfile = cu->objfile;
0114d602 7199 const char *name = NULL;
f792889a 7200 struct type *this_type;
c906108c 7201
94af9270 7202 name = dwarf2_full_name (NULL, die, cu);
f792889a 7203 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7204 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7205 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7206 set_die_type (die, this_type, cu);
7207 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7208 return this_type;
c906108c
SS
7209}
7210
7211/* Find a representation of a given base type and install
7212 it in the TYPE field of the die. */
7213
f792889a 7214static struct type *
e7c27a73 7215read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7216{
e7c27a73 7217 struct objfile *objfile = cu->objfile;
c906108c
SS
7218 struct type *type;
7219 struct attribute *attr;
7220 int encoding = 0, size = 0;
39cbfefa 7221 char *name;
6ccb9162
UW
7222 enum type_code code = TYPE_CODE_INT;
7223 int type_flags = 0;
7224 struct type *target_type = NULL;
c906108c 7225
e142c38c 7226 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7227 if (attr)
7228 {
7229 encoding = DW_UNSND (attr);
7230 }
e142c38c 7231 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7232 if (attr)
7233 {
7234 size = DW_UNSND (attr);
7235 }
39cbfefa 7236 name = dwarf2_name (die, cu);
6ccb9162 7237 if (!name)
c906108c 7238 {
6ccb9162
UW
7239 complaint (&symfile_complaints,
7240 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7241 }
6ccb9162
UW
7242
7243 switch (encoding)
c906108c 7244 {
6ccb9162
UW
7245 case DW_ATE_address:
7246 /* Turn DW_ATE_address into a void * pointer. */
7247 code = TYPE_CODE_PTR;
7248 type_flags |= TYPE_FLAG_UNSIGNED;
7249 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7250 break;
7251 case DW_ATE_boolean:
7252 code = TYPE_CODE_BOOL;
7253 type_flags |= TYPE_FLAG_UNSIGNED;
7254 break;
7255 case DW_ATE_complex_float:
7256 code = TYPE_CODE_COMPLEX;
7257 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7258 break;
7259 case DW_ATE_decimal_float:
7260 code = TYPE_CODE_DECFLOAT;
7261 break;
7262 case DW_ATE_float:
7263 code = TYPE_CODE_FLT;
7264 break;
7265 case DW_ATE_signed:
7266 break;
7267 case DW_ATE_unsigned:
7268 type_flags |= TYPE_FLAG_UNSIGNED;
7269 break;
7270 case DW_ATE_signed_char:
6e70227d 7271 if (cu->language == language_ada || cu->language == language_m2
868a0084 7272 || cu->language == language_pascal)
6ccb9162
UW
7273 code = TYPE_CODE_CHAR;
7274 break;
7275 case DW_ATE_unsigned_char:
868a0084
PM
7276 if (cu->language == language_ada || cu->language == language_m2
7277 || cu->language == language_pascal)
6ccb9162
UW
7278 code = TYPE_CODE_CHAR;
7279 type_flags |= TYPE_FLAG_UNSIGNED;
7280 break;
75079b2b
TT
7281 case DW_ATE_UTF:
7282 /* We just treat this as an integer and then recognize the
7283 type by name elsewhere. */
7284 break;
7285
6ccb9162
UW
7286 default:
7287 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7288 dwarf_type_encoding_name (encoding));
7289 break;
c906108c 7290 }
6ccb9162 7291
0114d602
DJ
7292 type = init_type (code, size, type_flags, NULL, objfile);
7293 TYPE_NAME (type) = name;
6ccb9162
UW
7294 TYPE_TARGET_TYPE (type) = target_type;
7295
0114d602 7296 if (name && strcmp (name, "char") == 0)
876cecd0 7297 TYPE_NOSIGN (type) = 1;
0114d602 7298
f792889a 7299 return set_die_type (die, type, cu);
c906108c
SS
7300}
7301
a02abb62
JB
7302/* Read the given DW_AT_subrange DIE. */
7303
f792889a 7304static struct type *
a02abb62
JB
7305read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7306{
5e2b427d 7307 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7308 struct type *base_type;
7309 struct type *range_type;
7310 struct attribute *attr;
43bbcdc2
PH
7311 LONGEST low = 0;
7312 LONGEST high = -1;
39cbfefa 7313 char *name;
43bbcdc2 7314 LONGEST negative_mask;
e77813c8 7315
a02abb62 7316 base_type = die_type (die, cu);
953ac07e
JK
7317 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7318 check_typedef (base_type);
a02abb62 7319
7e314c57
JK
7320 /* The die_type call above may have already set the type for this DIE. */
7321 range_type = get_die_type (die, cu);
7322 if (range_type)
7323 return range_type;
7324
e142c38c 7325 if (cu->language == language_fortran)
6e70227d 7326 {
a02abb62
JB
7327 /* FORTRAN implies a lower bound of 1, if not given. */
7328 low = 1;
7329 }
7330
dd5e6932
DJ
7331 /* FIXME: For variable sized arrays either of these could be
7332 a variable rather than a constant value. We'll allow it,
7333 but we don't know how to handle it. */
e142c38c 7334 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7335 if (attr)
7336 low = dwarf2_get_attr_constant_value (attr, 0);
7337
e142c38c 7338 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7339 if (attr)
6e70227d 7340 {
e77813c8 7341 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7342 {
7343 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7344 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7345 FIXME: GDB does not yet know how to handle dynamic
7346 arrays properly, treat them as arrays with unspecified
7347 length for now.
7348
7349 FIXME: jimb/2003-09-22: GDB does not really know
7350 how to handle arrays of unspecified length
7351 either; we just represent them as zero-length
7352 arrays. Choose an appropriate upper bound given
7353 the lower bound we've computed above. */
7354 high = low - 1;
7355 }
7356 else
7357 high = dwarf2_get_attr_constant_value (attr, 1);
7358 }
e77813c8
PM
7359 else
7360 {
7361 attr = dwarf2_attr (die, DW_AT_count, cu);
7362 if (attr)
7363 {
7364 int count = dwarf2_get_attr_constant_value (attr, 1);
7365 high = low + count - 1;
7366 }
7367 }
7368
7369 /* Dwarf-2 specifications explicitly allows to create subrange types
7370 without specifying a base type.
7371 In that case, the base type must be set to the type of
7372 the lower bound, upper bound or count, in that order, if any of these
7373 three attributes references an object that has a type.
7374 If no base type is found, the Dwarf-2 specifications say that
7375 a signed integer type of size equal to the size of an address should
7376 be used.
7377 For the following C code: `extern char gdb_int [];'
7378 GCC produces an empty range DIE.
7379 FIXME: muller/2010-05-28: Possible references to object for low bound,
7380 high bound or count are not yet handled by this code.
7381 */
7382 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7383 {
7384 struct objfile *objfile = cu->objfile;
7385 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7386 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7387 struct type *int_type = objfile_type (objfile)->builtin_int;
7388
7389 /* Test "int", "long int", and "long long int" objfile types,
7390 and select the first one having a size above or equal to the
7391 architecture address size. */
7392 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7393 base_type = int_type;
7394 else
7395 {
7396 int_type = objfile_type (objfile)->builtin_long;
7397 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7398 base_type = int_type;
7399 else
7400 {
7401 int_type = objfile_type (objfile)->builtin_long_long;
7402 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7403 base_type = int_type;
7404 }
7405 }
7406 }
a02abb62 7407
6e70227d 7408 negative_mask =
43bbcdc2
PH
7409 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7410 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7411 low |= negative_mask;
7412 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7413 high |= negative_mask;
7414
a02abb62
JB
7415 range_type = create_range_type (NULL, base_type, low, high);
7416
bbb0eef6
JK
7417 /* Mark arrays with dynamic length at least as an array of unspecified
7418 length. GDB could check the boundary but before it gets implemented at
7419 least allow accessing the array elements. */
7420 if (attr && attr->form == DW_FORM_block1)
7421 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7422
39cbfefa
DJ
7423 name = dwarf2_name (die, cu);
7424 if (name)
7425 TYPE_NAME (range_type) = name;
6e70227d 7426
e142c38c 7427 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
7428 if (attr)
7429 TYPE_LENGTH (range_type) = DW_UNSND (attr);
7430
7e314c57
JK
7431 set_die_type (die, range_type, cu);
7432
7433 /* set_die_type should be already done. */
b4ba55a1
JB
7434 set_descriptive_type (range_type, die, cu);
7435
7e314c57 7436 return range_type;
a02abb62 7437}
6e70227d 7438
f792889a 7439static struct type *
81a17f79
JB
7440read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7441{
7442 struct type *type;
81a17f79 7443
81a17f79
JB
7444 /* For now, we only support the C meaning of an unspecified type: void. */
7445
0114d602
DJ
7446 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7447 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 7448
f792889a 7449 return set_die_type (die, type, cu);
81a17f79 7450}
a02abb62 7451
51545339
DJ
7452/* Trivial hash function for die_info: the hash value of a DIE
7453 is its offset in .debug_info for this objfile. */
7454
7455static hashval_t
7456die_hash (const void *item)
7457{
7458 const struct die_info *die = item;
9a619af0 7459
51545339
DJ
7460 return die->offset;
7461}
7462
7463/* Trivial comparison function for die_info structures: two DIEs
7464 are equal if they have the same offset. */
7465
7466static int
7467die_eq (const void *item_lhs, const void *item_rhs)
7468{
7469 const struct die_info *die_lhs = item_lhs;
7470 const struct die_info *die_rhs = item_rhs;
9a619af0 7471
51545339
DJ
7472 return die_lhs->offset == die_rhs->offset;
7473}
7474
c906108c
SS
7475/* Read a whole compilation unit into a linked list of dies. */
7476
f9aca02d 7477static struct die_info *
93311388 7478read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 7479{
93311388
DE
7480 struct die_reader_specs reader_specs;
7481
348e048f 7482 gdb_assert (cu->die_hash == NULL);
51545339
DJ
7483 cu->die_hash
7484 = htab_create_alloc_ex (cu->header.length / 12,
7485 die_hash,
7486 die_eq,
7487 NULL,
7488 &cu->comp_unit_obstack,
7489 hashtab_obstack_allocate,
7490 dummy_obstack_deallocate);
7491
93311388
DE
7492 init_cu_die_reader (&reader_specs, cu);
7493
7494 return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
639d11d3
DC
7495}
7496
d97bc12b
DE
7497/* Main entry point for reading a DIE and all children.
7498 Read the DIE and dump it if requested. */
7499
7500static struct die_info *
93311388
DE
7501read_die_and_children (const struct die_reader_specs *reader,
7502 gdb_byte *info_ptr,
d97bc12b
DE
7503 gdb_byte **new_info_ptr,
7504 struct die_info *parent)
7505{
93311388 7506 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
7507 new_info_ptr, parent);
7508
7509 if (dwarf2_die_debug)
7510 {
348e048f
DE
7511 fprintf_unfiltered (gdb_stdlog,
7512 "\nRead die from %s of %s:\n",
7513 reader->buffer == dwarf2_per_objfile->info.buffer
7514 ? ".debug_info"
7515 : reader->buffer == dwarf2_per_objfile->types.buffer
7516 ? ".debug_types"
7517 : "unknown section",
7518 reader->abfd->filename);
d97bc12b
DE
7519 dump_die (result, dwarf2_die_debug);
7520 }
7521
7522 return result;
7523}
7524
639d11d3
DC
7525/* Read a single die and all its descendents. Set the die's sibling
7526 field to NULL; set other fields in the die correctly, and set all
7527 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
7528 location of the info_ptr after reading all of those dies. PARENT
7529 is the parent of the die in question. */
7530
7531static struct die_info *
93311388
DE
7532read_die_and_children_1 (const struct die_reader_specs *reader,
7533 gdb_byte *info_ptr,
d97bc12b
DE
7534 gdb_byte **new_info_ptr,
7535 struct die_info *parent)
639d11d3
DC
7536{
7537 struct die_info *die;
fe1b8b76 7538 gdb_byte *cur_ptr;
639d11d3
DC
7539 int has_children;
7540
93311388 7541 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
7542 if (die == NULL)
7543 {
7544 *new_info_ptr = cur_ptr;
7545 return NULL;
7546 }
93311388 7547 store_in_ref_table (die, reader->cu);
639d11d3
DC
7548
7549 if (has_children)
348e048f 7550 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
7551 else
7552 {
7553 die->child = NULL;
7554 *new_info_ptr = cur_ptr;
7555 }
7556
7557 die->sibling = NULL;
7558 die->parent = parent;
7559 return die;
7560}
7561
7562/* Read a die, all of its descendents, and all of its siblings; set
7563 all of the fields of all of the dies correctly. Arguments are as
7564 in read_die_and_children. */
7565
7566static struct die_info *
93311388
DE
7567read_die_and_siblings (const struct die_reader_specs *reader,
7568 gdb_byte *info_ptr,
fe1b8b76 7569 gdb_byte **new_info_ptr,
639d11d3
DC
7570 struct die_info *parent)
7571{
7572 struct die_info *first_die, *last_sibling;
fe1b8b76 7573 gdb_byte *cur_ptr;
639d11d3 7574
c906108c 7575 cur_ptr = info_ptr;
639d11d3
DC
7576 first_die = last_sibling = NULL;
7577
7578 while (1)
c906108c 7579 {
639d11d3 7580 struct die_info *die
93311388 7581 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 7582
1d325ec1 7583 if (die == NULL)
c906108c 7584 {
639d11d3
DC
7585 *new_info_ptr = cur_ptr;
7586 return first_die;
c906108c 7587 }
1d325ec1
DJ
7588
7589 if (!first_die)
7590 first_die = die;
c906108c 7591 else
1d325ec1
DJ
7592 last_sibling->sibling = die;
7593
7594 last_sibling = die;
c906108c 7595 }
c906108c
SS
7596}
7597
93311388
DE
7598/* Read the die from the .debug_info section buffer. Set DIEP to
7599 point to a newly allocated die with its information, except for its
7600 child, sibling, and parent fields. Set HAS_CHILDREN to tell
7601 whether the die has children or not. */
7602
7603static gdb_byte *
7604read_full_die (const struct die_reader_specs *reader,
7605 struct die_info **diep, gdb_byte *info_ptr,
7606 int *has_children)
7607{
7608 unsigned int abbrev_number, bytes_read, i, offset;
7609 struct abbrev_info *abbrev;
7610 struct die_info *die;
7611 struct dwarf2_cu *cu = reader->cu;
7612 bfd *abfd = reader->abfd;
7613
7614 offset = info_ptr - reader->buffer;
7615 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7616 info_ptr += bytes_read;
7617 if (!abbrev_number)
7618 {
7619 *diep = NULL;
7620 *has_children = 0;
7621 return info_ptr;
7622 }
7623
7624 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
7625 if (!abbrev)
348e048f
DE
7626 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
7627 abbrev_number,
7628 bfd_get_filename (abfd));
7629
93311388
DE
7630 die = dwarf_alloc_die (cu, abbrev->num_attrs);
7631 die->offset = offset;
7632 die->tag = abbrev->tag;
7633 die->abbrev = abbrev_number;
7634
7635 die->num_attrs = abbrev->num_attrs;
7636
7637 for (i = 0; i < abbrev->num_attrs; ++i)
7638 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
7639 abfd, info_ptr, cu);
7640
7641 *diep = die;
7642 *has_children = abbrev->has_children;
7643 return info_ptr;
7644}
7645
c906108c
SS
7646/* In DWARF version 2, the description of the debugging information is
7647 stored in a separate .debug_abbrev section. Before we read any
7648 dies from a section we read in all abbreviations and install them
72bf9492
DJ
7649 in a hash table. This function also sets flags in CU describing
7650 the data found in the abbrev table. */
c906108c
SS
7651
7652static void
e7c27a73 7653dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 7654{
e7c27a73 7655 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 7656 gdb_byte *abbrev_ptr;
c906108c
SS
7657 struct abbrev_info *cur_abbrev;
7658 unsigned int abbrev_number, bytes_read, abbrev_name;
7659 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
7660 struct attr_abbrev *cur_attrs;
7661 unsigned int allocated_attrs;
c906108c 7662
57349743 7663 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
7664 obstack_init (&cu->abbrev_obstack);
7665 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
7666 (ABBREV_HASH_SIZE
7667 * sizeof (struct abbrev_info *)));
7668 memset (cu->dwarf2_abbrevs, 0,
7669 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 7670
be391dca
TT
7671 dwarf2_read_section (dwarf2_per_objfile->objfile,
7672 &dwarf2_per_objfile->abbrev);
dce234bc 7673 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
7674 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7675 abbrev_ptr += bytes_read;
7676
f3dd6933
DJ
7677 allocated_attrs = ATTR_ALLOC_CHUNK;
7678 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 7679
c906108c
SS
7680 /* loop until we reach an abbrev number of 0 */
7681 while (abbrev_number)
7682 {
f3dd6933 7683 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
7684
7685 /* read in abbrev header */
7686 cur_abbrev->number = abbrev_number;
7687 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7688 abbrev_ptr += bytes_read;
7689 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7690 abbrev_ptr += 1;
7691
72bf9492
DJ
7692 if (cur_abbrev->tag == DW_TAG_namespace)
7693 cu->has_namespace_info = 1;
7694
c906108c
SS
7695 /* now read in declarations */
7696 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7697 abbrev_ptr += bytes_read;
7698 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7699 abbrev_ptr += bytes_read;
7700 while (abbrev_name)
7701 {
f3dd6933 7702 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 7703 {
f3dd6933
DJ
7704 allocated_attrs += ATTR_ALLOC_CHUNK;
7705 cur_attrs
7706 = xrealloc (cur_attrs, (allocated_attrs
7707 * sizeof (struct attr_abbrev)));
c906108c 7708 }
ae038cb0
DJ
7709
7710 /* Record whether this compilation unit might have
7711 inter-compilation-unit references. If we don't know what form
7712 this attribute will have, then it might potentially be a
7713 DW_FORM_ref_addr, so we conservatively expect inter-CU
7714 references. */
7715
7716 if (abbrev_form == DW_FORM_ref_addr
7717 || abbrev_form == DW_FORM_indirect)
7718 cu->has_form_ref_addr = 1;
7719
f3dd6933
DJ
7720 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
7721 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
7722 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7723 abbrev_ptr += bytes_read;
7724 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7725 abbrev_ptr += bytes_read;
7726 }
7727
f3dd6933
DJ
7728 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
7729 (cur_abbrev->num_attrs
7730 * sizeof (struct attr_abbrev)));
7731 memcpy (cur_abbrev->attrs, cur_attrs,
7732 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
7733
c906108c 7734 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
7735 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
7736 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
7737
7738 /* Get next abbreviation.
7739 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
7740 always properly terminated with an abbrev number of 0.
7741 Exit loop if we encounter an abbreviation which we have
7742 already read (which means we are about to read the abbreviations
7743 for the next compile unit) or if the end of the abbreviation
7744 table is reached. */
dce234bc
PP
7745 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
7746 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
7747 break;
7748 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7749 abbrev_ptr += bytes_read;
e7c27a73 7750 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
7751 break;
7752 }
f3dd6933
DJ
7753
7754 xfree (cur_attrs);
c906108c
SS
7755}
7756
f3dd6933 7757/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 7758
c906108c 7759static void
f3dd6933 7760dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 7761{
f3dd6933 7762 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 7763
f3dd6933
DJ
7764 obstack_free (&cu->abbrev_obstack, NULL);
7765 cu->dwarf2_abbrevs = NULL;
c906108c
SS
7766}
7767
7768/* Lookup an abbrev_info structure in the abbrev hash table. */
7769
7770static struct abbrev_info *
e7c27a73 7771dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
7772{
7773 unsigned int hash_number;
7774 struct abbrev_info *abbrev;
7775
7776 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 7777 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
7778
7779 while (abbrev)
7780 {
7781 if (abbrev->number == number)
7782 return abbrev;
7783 else
7784 abbrev = abbrev->next;
7785 }
7786 return NULL;
7787}
7788
72bf9492
DJ
7789/* Returns nonzero if TAG represents a type that we might generate a partial
7790 symbol for. */
7791
7792static int
7793is_type_tag_for_partial (int tag)
7794{
7795 switch (tag)
7796 {
7797#if 0
7798 /* Some types that would be reasonable to generate partial symbols for,
7799 that we don't at present. */
7800 case DW_TAG_array_type:
7801 case DW_TAG_file_type:
7802 case DW_TAG_ptr_to_member_type:
7803 case DW_TAG_set_type:
7804 case DW_TAG_string_type:
7805 case DW_TAG_subroutine_type:
7806#endif
7807 case DW_TAG_base_type:
7808 case DW_TAG_class_type:
680b30c7 7809 case DW_TAG_interface_type:
72bf9492
DJ
7810 case DW_TAG_enumeration_type:
7811 case DW_TAG_structure_type:
7812 case DW_TAG_subrange_type:
7813 case DW_TAG_typedef:
7814 case DW_TAG_union_type:
7815 return 1;
7816 default:
7817 return 0;
7818 }
7819}
7820
7821/* Load all DIEs that are interesting for partial symbols into memory. */
7822
7823static struct partial_die_info *
93311388
DE
7824load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
7825 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
7826{
7827 struct partial_die_info *part_die;
7828 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
7829 struct abbrev_info *abbrev;
7830 unsigned int bytes_read;
5afb4e99 7831 unsigned int load_all = 0;
72bf9492
DJ
7832
7833 int nesting_level = 1;
7834
7835 parent_die = NULL;
7836 last_die = NULL;
7837
5afb4e99
DJ
7838 if (cu->per_cu && cu->per_cu->load_all_dies)
7839 load_all = 1;
7840
72bf9492
DJ
7841 cu->partial_dies
7842 = htab_create_alloc_ex (cu->header.length / 12,
7843 partial_die_hash,
7844 partial_die_eq,
7845 NULL,
7846 &cu->comp_unit_obstack,
7847 hashtab_obstack_allocate,
7848 dummy_obstack_deallocate);
7849
7850 part_die = obstack_alloc (&cu->comp_unit_obstack,
7851 sizeof (struct partial_die_info));
7852
7853 while (1)
7854 {
7855 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7856
7857 /* A NULL abbrev means the end of a series of children. */
7858 if (abbrev == NULL)
7859 {
7860 if (--nesting_level == 0)
7861 {
7862 /* PART_DIE was probably the last thing allocated on the
7863 comp_unit_obstack, so we could call obstack_free
7864 here. We don't do that because the waste is small,
7865 and will be cleaned up when we're done with this
7866 compilation unit. This way, we're also more robust
7867 against other users of the comp_unit_obstack. */
7868 return first_die;
7869 }
7870 info_ptr += bytes_read;
7871 last_die = parent_die;
7872 parent_die = parent_die->die_parent;
7873 continue;
7874 }
7875
5afb4e99
DJ
7876 /* Check whether this DIE is interesting enough to save. Normally
7877 we would not be interested in members here, but there may be
7878 later variables referencing them via DW_AT_specification (for
7879 static members). */
7880 if (!load_all
7881 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
7882 && abbrev->tag != DW_TAG_enumerator
7883 && abbrev->tag != DW_TAG_subprogram
bc30ff58 7884 && abbrev->tag != DW_TAG_lexical_block
72bf9492 7885 && abbrev->tag != DW_TAG_variable
5afb4e99 7886 && abbrev->tag != DW_TAG_namespace
f55ee35c 7887 && abbrev->tag != DW_TAG_module
5afb4e99 7888 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
7889 {
7890 /* Otherwise we skip to the next sibling, if any. */
93311388 7891 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
7892 continue;
7893 }
7894
93311388
DE
7895 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
7896 buffer, info_ptr, cu);
72bf9492
DJ
7897
7898 /* This two-pass algorithm for processing partial symbols has a
7899 high cost in cache pressure. Thus, handle some simple cases
7900 here which cover the majority of C partial symbols. DIEs
7901 which neither have specification tags in them, nor could have
7902 specification tags elsewhere pointing at them, can simply be
7903 processed and discarded.
7904
7905 This segment is also optional; scan_partial_symbols and
7906 add_partial_symbol will handle these DIEs if we chain
7907 them in normally. When compilers which do not emit large
7908 quantities of duplicate debug information are more common,
7909 this code can probably be removed. */
7910
7911 /* Any complete simple types at the top level (pretty much all
7912 of them, for a language without namespaces), can be processed
7913 directly. */
7914 if (parent_die == NULL
7915 && part_die->has_specification == 0
7916 && part_die->is_declaration == 0
7917 && (part_die->tag == DW_TAG_typedef
7918 || part_die->tag == DW_TAG_base_type
7919 || part_die->tag == DW_TAG_subrange_type))
7920 {
7921 if (building_psymtab && part_die->name != NULL)
04a679b8 7922 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
7923 VAR_DOMAIN, LOC_TYPEDEF,
7924 &cu->objfile->static_psymbols,
7925 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 7926 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
7927 continue;
7928 }
7929
7930 /* If we're at the second level, and we're an enumerator, and
7931 our parent has no specification (meaning possibly lives in a
7932 namespace elsewhere), then we can add the partial symbol now
7933 instead of queueing it. */
7934 if (part_die->tag == DW_TAG_enumerator
7935 && parent_die != NULL
7936 && parent_die->die_parent == NULL
7937 && parent_die->tag == DW_TAG_enumeration_type
7938 && parent_die->has_specification == 0)
7939 {
7940 if (part_die->name == NULL)
e2e0b3e5 7941 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 7942 else if (building_psymtab)
04a679b8 7943 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 7944 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
7945 (cu->language == language_cplus
7946 || cu->language == language_java)
72bf9492
DJ
7947 ? &cu->objfile->global_psymbols
7948 : &cu->objfile->static_psymbols,
7949 0, (CORE_ADDR) 0, cu->language, cu->objfile);
7950
93311388 7951 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
7952 continue;
7953 }
7954
7955 /* We'll save this DIE so link it in. */
7956 part_die->die_parent = parent_die;
7957 part_die->die_sibling = NULL;
7958 part_die->die_child = NULL;
7959
7960 if (last_die && last_die == parent_die)
7961 last_die->die_child = part_die;
7962 else if (last_die)
7963 last_die->die_sibling = part_die;
7964
7965 last_die = part_die;
7966
7967 if (first_die == NULL)
7968 first_die = part_die;
7969
7970 /* Maybe add the DIE to the hash table. Not all DIEs that we
7971 find interesting need to be in the hash table, because we
7972 also have the parent/sibling/child chains; only those that we
7973 might refer to by offset later during partial symbol reading.
7974
7975 For now this means things that might have be the target of a
7976 DW_AT_specification, DW_AT_abstract_origin, or
7977 DW_AT_extension. DW_AT_extension will refer only to
7978 namespaces; DW_AT_abstract_origin refers to functions (and
7979 many things under the function DIE, but we do not recurse
7980 into function DIEs during partial symbol reading) and
7981 possibly variables as well; DW_AT_specification refers to
7982 declarations. Declarations ought to have the DW_AT_declaration
7983 flag. It happens that GCC forgets to put it in sometimes, but
7984 only for functions, not for types.
7985
7986 Adding more things than necessary to the hash table is harmless
7987 except for the performance cost. Adding too few will result in
5afb4e99
DJ
7988 wasted time in find_partial_die, when we reread the compilation
7989 unit with load_all_dies set. */
72bf9492 7990
5afb4e99
DJ
7991 if (load_all
7992 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
7993 || abbrev->tag == DW_TAG_variable
7994 || abbrev->tag == DW_TAG_namespace
7995 || part_die->is_declaration)
7996 {
7997 void **slot;
7998
7999 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8000 part_die->offset, INSERT);
8001 *slot = part_die;
8002 }
8003
8004 part_die = obstack_alloc (&cu->comp_unit_obstack,
8005 sizeof (struct partial_die_info));
8006
8007 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8008 we have no reason to follow the children of structures; for other
72bf9492 8009 languages we have to, both so that we can get at method physnames
bc30ff58
JB
8010 to infer fully qualified class names, and for DW_AT_specification.
8011
8012 For Ada, we need to scan the children of subprograms and lexical
8013 blocks as well because Ada allows the definition of nested
8014 entities that could be interesting for the debugger, such as
8015 nested subprograms for instance. */
72bf9492 8016 if (last_die->has_children
5afb4e99
DJ
8017 && (load_all
8018 || last_die->tag == DW_TAG_namespace
f55ee35c 8019 || last_die->tag == DW_TAG_module
72bf9492
DJ
8020 || last_die->tag == DW_TAG_enumeration_type
8021 || (cu->language != language_c
8022 && (last_die->tag == DW_TAG_class_type
680b30c7 8023 || last_die->tag == DW_TAG_interface_type
72bf9492 8024 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8025 || last_die->tag == DW_TAG_union_type))
8026 || (cu->language == language_ada
8027 && (last_die->tag == DW_TAG_subprogram
8028 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8029 {
8030 nesting_level++;
8031 parent_die = last_die;
8032 continue;
8033 }
8034
8035 /* Otherwise we skip to the next sibling, if any. */
93311388 8036 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8037
8038 /* Back to the top, do it again. */
8039 }
8040}
8041
c906108c
SS
8042/* Read a minimal amount of information into the minimal die structure. */
8043
fe1b8b76 8044static gdb_byte *
72bf9492
DJ
8045read_partial_die (struct partial_die_info *part_die,
8046 struct abbrev_info *abbrev,
8047 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8048 gdb_byte *buffer, gdb_byte *info_ptr,
8049 struct dwarf2_cu *cu)
c906108c 8050{
fa238c03 8051 unsigned int i;
c906108c 8052 struct attribute attr;
c5aa993b 8053 int has_low_pc_attr = 0;
c906108c
SS
8054 int has_high_pc_attr = 0;
8055
72bf9492 8056 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8057
93311388 8058 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8059
8060 info_ptr += abbrev_len;
8061
8062 if (abbrev == NULL)
8063 return info_ptr;
8064
c906108c
SS
8065 part_die->tag = abbrev->tag;
8066 part_die->has_children = abbrev->has_children;
c906108c
SS
8067
8068 for (i = 0; i < abbrev->num_attrs; ++i)
8069 {
e7c27a73 8070 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8071
8072 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8073 partial symbol table. */
c906108c
SS
8074 switch (attr.name)
8075 {
8076 case DW_AT_name:
71c25dea
TT
8077 switch (part_die->tag)
8078 {
8079 case DW_TAG_compile_unit:
348e048f 8080 case DW_TAG_type_unit:
71c25dea
TT
8081 /* Compilation units have a DW_AT_name that is a filename, not
8082 a source language identifier. */
8083 case DW_TAG_enumeration_type:
8084 case DW_TAG_enumerator:
8085 /* These tags always have simple identifiers already; no need
8086 to canonicalize them. */
8087 part_die->name = DW_STRING (&attr);
8088 break;
8089 default:
8090 part_die->name
8091 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8092 &cu->objfile->objfile_obstack);
71c25dea
TT
8093 break;
8094 }
c906108c 8095 break;
31ef98ae 8096 case DW_AT_linkage_name:
c906108c 8097 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8098 /* Note that both forms of linkage name might appear. We
8099 assume they will be the same, and we only store the last
8100 one we see. */
94af9270
KS
8101 if (cu->language == language_ada)
8102 part_die->name = DW_STRING (&attr);
c906108c
SS
8103 break;
8104 case DW_AT_low_pc:
8105 has_low_pc_attr = 1;
8106 part_die->lowpc = DW_ADDR (&attr);
8107 break;
8108 case DW_AT_high_pc:
8109 has_high_pc_attr = 1;
8110 part_die->highpc = DW_ADDR (&attr);
8111 break;
8112 case DW_AT_location:
8e19ed76
PS
8113 /* Support the .debug_loc offsets */
8114 if (attr_form_is_block (&attr))
8115 {
8116 part_die->locdesc = DW_BLOCK (&attr);
8117 }
3690dd37 8118 else if (attr_form_is_section_offset (&attr))
8e19ed76 8119 {
4d3c2250 8120 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8121 }
8122 else
8123 {
4d3c2250
KB
8124 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8125 "partial symbol information");
8e19ed76 8126 }
c906108c 8127 break;
c906108c
SS
8128 case DW_AT_external:
8129 part_die->is_external = DW_UNSND (&attr);
8130 break;
8131 case DW_AT_declaration:
8132 part_die->is_declaration = DW_UNSND (&attr);
8133 break;
8134 case DW_AT_type:
8135 part_die->has_type = 1;
8136 break;
8137 case DW_AT_abstract_origin:
8138 case DW_AT_specification:
72bf9492
DJ
8139 case DW_AT_extension:
8140 part_die->has_specification = 1;
c764a876 8141 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8142 break;
8143 case DW_AT_sibling:
8144 /* Ignore absolute siblings, they might point outside of
8145 the current compile unit. */
8146 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8147 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8148 else
93311388 8149 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8150 break;
fa4028e9
JB
8151 case DW_AT_byte_size:
8152 part_die->has_byte_size = 1;
8153 break;
68511cec
CES
8154 case DW_AT_calling_convention:
8155 /* DWARF doesn't provide a way to identify a program's source-level
8156 entry point. DW_AT_calling_convention attributes are only meant
8157 to describe functions' calling conventions.
8158
8159 However, because it's a necessary piece of information in
8160 Fortran, and because DW_CC_program is the only piece of debugging
8161 information whose definition refers to a 'main program' at all,
8162 several compilers have begun marking Fortran main programs with
8163 DW_CC_program --- even when those functions use the standard
8164 calling conventions.
8165
8166 So until DWARF specifies a way to provide this information and
8167 compilers pick up the new representation, we'll support this
8168 practice. */
8169 if (DW_UNSND (&attr) == DW_CC_program
8170 && cu->language == language_fortran)
8171 set_main_name (part_die->name);
8172 break;
c906108c
SS
8173 default:
8174 break;
8175 }
8176 }
8177
c906108c
SS
8178 /* When using the GNU linker, .gnu.linkonce. sections are used to
8179 eliminate duplicate copies of functions and vtables and such.
8180 The linker will arbitrarily choose one and discard the others.
8181 The AT_*_pc values for such functions refer to local labels in
8182 these sections. If the section from that file was discarded, the
8183 labels are not in the output, so the relocs get a value of 0.
8184 If this is a discarded function, mark the pc bounds as invalid,
8185 so that GDB will ignore it. */
8186 if (has_low_pc_attr && has_high_pc_attr
8187 && part_die->lowpc < part_die->highpc
8188 && (part_die->lowpc != 0
72dca2f5 8189 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8190 part_die->has_pc_info = 1;
85cbf3d3 8191
c906108c
SS
8192 return info_ptr;
8193}
8194
72bf9492
DJ
8195/* Find a cached partial DIE at OFFSET in CU. */
8196
8197static struct partial_die_info *
c764a876 8198find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8199{
8200 struct partial_die_info *lookup_die = NULL;
8201 struct partial_die_info part_die;
8202
8203 part_die.offset = offset;
8204 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8205
72bf9492
DJ
8206 return lookup_die;
8207}
8208
348e048f
DE
8209/* Find a partial DIE at OFFSET, which may or may not be in CU,
8210 except in the case of .debug_types DIEs which do not reference
8211 outside their CU (they do however referencing other types via
8212 DW_FORM_sig8). */
72bf9492
DJ
8213
8214static struct partial_die_info *
c764a876 8215find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8216{
5afb4e99
DJ
8217 struct dwarf2_per_cu_data *per_cu = NULL;
8218 struct partial_die_info *pd = NULL;
72bf9492 8219
348e048f
DE
8220 if (cu->per_cu->from_debug_types)
8221 {
8222 pd = find_partial_die_in_comp_unit (offset, cu);
8223 if (pd != NULL)
8224 return pd;
8225 goto not_found;
8226 }
8227
45452591 8228 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8229 {
8230 pd = find_partial_die_in_comp_unit (offset, cu);
8231 if (pd != NULL)
8232 return pd;
8233 }
72bf9492 8234
ae038cb0
DJ
8235 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8236
ae038cb0
DJ
8237 if (per_cu->cu == NULL)
8238 {
93311388 8239 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8240 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8241 dwarf2_per_objfile->read_in_chain = per_cu;
8242 }
8243
8244 per_cu->cu->last_used = 0;
5afb4e99
DJ
8245 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8246
8247 if (pd == NULL && per_cu->load_all_dies == 0)
8248 {
8249 struct cleanup *back_to;
8250 struct partial_die_info comp_unit_die;
8251 struct abbrev_info *abbrev;
8252 unsigned int bytes_read;
8253 char *info_ptr;
8254
8255 per_cu->load_all_dies = 1;
8256
8257 /* Re-read the DIEs. */
8258 back_to = make_cleanup (null_cleanup, 0);
8259 if (per_cu->cu->dwarf2_abbrevs == NULL)
8260 {
8261 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8262 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8263 }
dce234bc 8264 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8265 + per_cu->cu->header.offset
8266 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8267 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8268 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8269 per_cu->cu->objfile->obfd,
8270 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8271 per_cu->cu);
8272 if (comp_unit_die.has_children)
93311388
DE
8273 load_partial_dies (per_cu->cu->objfile->obfd,
8274 dwarf2_per_objfile->info.buffer, info_ptr,
8275 0, per_cu->cu);
5afb4e99
DJ
8276 do_cleanups (back_to);
8277
8278 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8279 }
8280
348e048f
DE
8281 not_found:
8282
5afb4e99
DJ
8283 if (pd == NULL)
8284 internal_error (__FILE__, __LINE__,
c764a876 8285 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8286 offset, bfd_get_filename (cu->objfile->obfd));
8287 return pd;
72bf9492
DJ
8288}
8289
8290/* Adjust PART_DIE before generating a symbol for it. This function
8291 may set the is_external flag or change the DIE's name. */
8292
8293static void
8294fixup_partial_die (struct partial_die_info *part_die,
8295 struct dwarf2_cu *cu)
8296{
8297 /* If we found a reference attribute and the DIE has no name, try
8298 to find a name in the referred to DIE. */
8299
8300 if (part_die->name == NULL && part_die->has_specification)
8301 {
8302 struct partial_die_info *spec_die;
72bf9492 8303
10b3939b 8304 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8305
10b3939b 8306 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8307
8308 if (spec_die->name)
8309 {
8310 part_die->name = spec_die->name;
8311
8312 /* Copy DW_AT_external attribute if it is set. */
8313 if (spec_die->is_external)
8314 part_die->is_external = spec_die->is_external;
8315 }
8316 }
8317
8318 /* Set default names for some unnamed DIEs. */
8319 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8320 || part_die->tag == DW_TAG_class_type))
8321 part_die->name = "(anonymous class)";
8322
8323 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8324 part_die->name = "(anonymous namespace)";
8325
8326 if (part_die->tag == DW_TAG_structure_type
8327 || part_die->tag == DW_TAG_class_type
8328 || part_die->tag == DW_TAG_union_type)
8329 guess_structure_name (part_die, cu);
8330}
8331
a8329558 8332/* Read an attribute value described by an attribute form. */
c906108c 8333
fe1b8b76 8334static gdb_byte *
a8329558 8335read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 8336 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 8337 struct dwarf2_cu *cu)
c906108c 8338{
e7c27a73 8339 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8340 unsigned int bytes_read;
8341 struct dwarf_block *blk;
8342
a8329558
KW
8343 attr->form = form;
8344 switch (form)
c906108c 8345 {
c906108c 8346 case DW_FORM_ref_addr:
ae411497
TT
8347 if (cu->header.version == 2)
8348 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8349 else
8350 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8351 info_ptr += bytes_read;
8352 break;
8353 case DW_FORM_addr:
e7c27a73 8354 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 8355 info_ptr += bytes_read;
c906108c
SS
8356 break;
8357 case DW_FORM_block2:
7b5a2f43 8358 blk = dwarf_alloc_block (cu);
c906108c
SS
8359 blk->size = read_2_bytes (abfd, info_ptr);
8360 info_ptr += 2;
8361 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8362 info_ptr += blk->size;
8363 DW_BLOCK (attr) = blk;
8364 break;
8365 case DW_FORM_block4:
7b5a2f43 8366 blk = dwarf_alloc_block (cu);
c906108c
SS
8367 blk->size = read_4_bytes (abfd, info_ptr);
8368 info_ptr += 4;
8369 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8370 info_ptr += blk->size;
8371 DW_BLOCK (attr) = blk;
8372 break;
8373 case DW_FORM_data2:
8374 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8375 info_ptr += 2;
8376 break;
8377 case DW_FORM_data4:
8378 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8379 info_ptr += 4;
8380 break;
8381 case DW_FORM_data8:
8382 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8383 info_ptr += 8;
8384 break;
2dc7f7b3
TT
8385 case DW_FORM_sec_offset:
8386 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8387 info_ptr += bytes_read;
8388 break;
c906108c
SS
8389 case DW_FORM_string:
8390 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
8285870a 8391 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
8392 info_ptr += bytes_read;
8393 break;
4bdf3d34
JJ
8394 case DW_FORM_strp:
8395 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8396 &bytes_read);
8285870a 8397 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
8398 info_ptr += bytes_read;
8399 break;
2dc7f7b3 8400 case DW_FORM_exprloc:
c906108c 8401 case DW_FORM_block:
7b5a2f43 8402 blk = dwarf_alloc_block (cu);
c906108c
SS
8403 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8404 info_ptr += bytes_read;
8405 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8406 info_ptr += blk->size;
8407 DW_BLOCK (attr) = blk;
8408 break;
8409 case DW_FORM_block1:
7b5a2f43 8410 blk = dwarf_alloc_block (cu);
c906108c
SS
8411 blk->size = read_1_byte (abfd, info_ptr);
8412 info_ptr += 1;
8413 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8414 info_ptr += blk->size;
8415 DW_BLOCK (attr) = blk;
8416 break;
8417 case DW_FORM_data1:
8418 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8419 info_ptr += 1;
8420 break;
8421 case DW_FORM_flag:
8422 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8423 info_ptr += 1;
8424 break;
2dc7f7b3
TT
8425 case DW_FORM_flag_present:
8426 DW_UNSND (attr) = 1;
8427 break;
c906108c
SS
8428 case DW_FORM_sdata:
8429 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8430 info_ptr += bytes_read;
8431 break;
8432 case DW_FORM_udata:
8433 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8434 info_ptr += bytes_read;
8435 break;
8436 case DW_FORM_ref1:
10b3939b 8437 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
8438 info_ptr += 1;
8439 break;
8440 case DW_FORM_ref2:
10b3939b 8441 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
8442 info_ptr += 2;
8443 break;
8444 case DW_FORM_ref4:
10b3939b 8445 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
8446 info_ptr += 4;
8447 break;
613e1657 8448 case DW_FORM_ref8:
10b3939b 8449 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
8450 info_ptr += 8;
8451 break;
348e048f
DE
8452 case DW_FORM_sig8:
8453 /* Convert the signature to something we can record in DW_UNSND
8454 for later lookup.
8455 NOTE: This is NULL if the type wasn't found. */
8456 DW_SIGNATURED_TYPE (attr) =
8457 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
8458 info_ptr += 8;
8459 break;
c906108c 8460 case DW_FORM_ref_udata:
10b3939b
DJ
8461 DW_ADDR (attr) = (cu->header.offset
8462 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
8463 info_ptr += bytes_read;
8464 break;
c906108c 8465 case DW_FORM_indirect:
a8329558
KW
8466 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8467 info_ptr += bytes_read;
e7c27a73 8468 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 8469 break;
c906108c 8470 default:
8a3fe4f8 8471 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
8472 dwarf_form_name (form),
8473 bfd_get_filename (abfd));
c906108c 8474 }
28e94949
JB
8475
8476 /* We have seen instances where the compiler tried to emit a byte
8477 size attribute of -1 which ended up being encoded as an unsigned
8478 0xffffffff. Although 0xffffffff is technically a valid size value,
8479 an object of this size seems pretty unlikely so we can relatively
8480 safely treat these cases as if the size attribute was invalid and
8481 treat them as zero by default. */
8482 if (attr->name == DW_AT_byte_size
8483 && form == DW_FORM_data4
8484 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
8485 {
8486 complaint
8487 (&symfile_complaints,
43bbcdc2
PH
8488 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
8489 hex_string (DW_UNSND (attr)));
01c66ae6
JB
8490 DW_UNSND (attr) = 0;
8491 }
28e94949 8492
c906108c
SS
8493 return info_ptr;
8494}
8495
a8329558
KW
8496/* Read an attribute described by an abbreviated attribute. */
8497
fe1b8b76 8498static gdb_byte *
a8329558 8499read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 8500 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
8501{
8502 attr->name = abbrev->name;
e7c27a73 8503 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
8504}
8505
c906108c
SS
8506/* read dwarf information from a buffer */
8507
8508static unsigned int
fe1b8b76 8509read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 8510{
fe1b8b76 8511 return bfd_get_8 (abfd, buf);
c906108c
SS
8512}
8513
8514static int
fe1b8b76 8515read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 8516{
fe1b8b76 8517 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
8518}
8519
8520static unsigned int
fe1b8b76 8521read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8522{
fe1b8b76 8523 return bfd_get_16 (abfd, buf);
c906108c
SS
8524}
8525
8526static int
fe1b8b76 8527read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8528{
fe1b8b76 8529 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
8530}
8531
8532static unsigned int
fe1b8b76 8533read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8534{
fe1b8b76 8535 return bfd_get_32 (abfd, buf);
c906108c
SS
8536}
8537
8538static int
fe1b8b76 8539read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8540{
fe1b8b76 8541 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
8542}
8543
93311388 8544static ULONGEST
fe1b8b76 8545read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8546{
fe1b8b76 8547 return bfd_get_64 (abfd, buf);
c906108c
SS
8548}
8549
8550static CORE_ADDR
fe1b8b76 8551read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 8552 unsigned int *bytes_read)
c906108c 8553{
e7c27a73 8554 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8555 CORE_ADDR retval = 0;
8556
107d2387 8557 if (cu_header->signed_addr_p)
c906108c 8558 {
107d2387
AC
8559 switch (cu_header->addr_size)
8560 {
8561 case 2:
fe1b8b76 8562 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
8563 break;
8564 case 4:
fe1b8b76 8565 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
8566 break;
8567 case 8:
fe1b8b76 8568 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
8569 break;
8570 default:
8e65ff28 8571 internal_error (__FILE__, __LINE__,
e2e0b3e5 8572 _("read_address: bad switch, signed [in module %s]"),
659b0389 8573 bfd_get_filename (abfd));
107d2387
AC
8574 }
8575 }
8576 else
8577 {
8578 switch (cu_header->addr_size)
8579 {
8580 case 2:
fe1b8b76 8581 retval = bfd_get_16 (abfd, buf);
107d2387
AC
8582 break;
8583 case 4:
fe1b8b76 8584 retval = bfd_get_32 (abfd, buf);
107d2387
AC
8585 break;
8586 case 8:
fe1b8b76 8587 retval = bfd_get_64 (abfd, buf);
107d2387
AC
8588 break;
8589 default:
8e65ff28 8590 internal_error (__FILE__, __LINE__,
e2e0b3e5 8591 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 8592 bfd_get_filename (abfd));
107d2387 8593 }
c906108c 8594 }
64367e0a 8595
107d2387
AC
8596 *bytes_read = cu_header->addr_size;
8597 return retval;
c906108c
SS
8598}
8599
f7ef9339 8600/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
8601 specification allows the initial length to take up either 4 bytes
8602 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
8603 bytes describe the length and all offsets will be 8 bytes in length
8604 instead of 4.
8605
f7ef9339
KB
8606 An older, non-standard 64-bit format is also handled by this
8607 function. The older format in question stores the initial length
8608 as an 8-byte quantity without an escape value. Lengths greater
8609 than 2^32 aren't very common which means that the initial 4 bytes
8610 is almost always zero. Since a length value of zero doesn't make
8611 sense for the 32-bit format, this initial zero can be considered to
8612 be an escape value which indicates the presence of the older 64-bit
8613 format. As written, the code can't detect (old format) lengths
917c78fc
MK
8614 greater than 4GB. If it becomes necessary to handle lengths
8615 somewhat larger than 4GB, we could allow other small values (such
8616 as the non-sensical values of 1, 2, and 3) to also be used as
8617 escape values indicating the presence of the old format.
f7ef9339 8618
917c78fc
MK
8619 The value returned via bytes_read should be used to increment the
8620 relevant pointer after calling read_initial_length().
c764a876 8621
613e1657
KB
8622 [ Note: read_initial_length() and read_offset() are based on the
8623 document entitled "DWARF Debugging Information Format", revision
f7ef9339 8624 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
8625 from:
8626
f7ef9339 8627 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 8628
613e1657
KB
8629 This document is only a draft and is subject to change. (So beware.)
8630
f7ef9339 8631 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
8632 determined empirically by examining 64-bit ELF files produced by
8633 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
8634
8635 - Kevin, July 16, 2002
613e1657
KB
8636 ] */
8637
8638static LONGEST
c764a876 8639read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 8640{
fe1b8b76 8641 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 8642
dd373385 8643 if (length == 0xffffffff)
613e1657 8644 {
fe1b8b76 8645 length = bfd_get_64 (abfd, buf + 4);
613e1657 8646 *bytes_read = 12;
613e1657 8647 }
dd373385 8648 else if (length == 0)
f7ef9339 8649 {
dd373385 8650 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 8651 length = bfd_get_64 (abfd, buf);
f7ef9339 8652 *bytes_read = 8;
f7ef9339 8653 }
613e1657
KB
8654 else
8655 {
8656 *bytes_read = 4;
613e1657
KB
8657 }
8658
c764a876
DE
8659 return length;
8660}
dd373385 8661
c764a876
DE
8662/* Cover function for read_initial_length.
8663 Returns the length of the object at BUF, and stores the size of the
8664 initial length in *BYTES_READ and stores the size that offsets will be in
8665 *OFFSET_SIZE.
8666 If the initial length size is not equivalent to that specified in
8667 CU_HEADER then issue a complaint.
8668 This is useful when reading non-comp-unit headers. */
dd373385 8669
c764a876
DE
8670static LONGEST
8671read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
8672 const struct comp_unit_head *cu_header,
8673 unsigned int *bytes_read,
8674 unsigned int *offset_size)
8675{
8676 LONGEST length = read_initial_length (abfd, buf, bytes_read);
8677
8678 gdb_assert (cu_header->initial_length_size == 4
8679 || cu_header->initial_length_size == 8
8680 || cu_header->initial_length_size == 12);
8681
8682 if (cu_header->initial_length_size != *bytes_read)
8683 complaint (&symfile_complaints,
8684 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 8685
c764a876 8686 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 8687 return length;
613e1657
KB
8688}
8689
8690/* Read an offset from the data stream. The size of the offset is
917c78fc 8691 given by cu_header->offset_size. */
613e1657
KB
8692
8693static LONGEST
fe1b8b76 8694read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 8695 unsigned int *bytes_read)
c764a876
DE
8696{
8697 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 8698
c764a876
DE
8699 *bytes_read = cu_header->offset_size;
8700 return offset;
8701}
8702
8703/* Read an offset from the data stream. */
8704
8705static LONGEST
8706read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
8707{
8708 LONGEST retval = 0;
8709
c764a876 8710 switch (offset_size)
613e1657
KB
8711 {
8712 case 4:
fe1b8b76 8713 retval = bfd_get_32 (abfd, buf);
613e1657
KB
8714 break;
8715 case 8:
fe1b8b76 8716 retval = bfd_get_64 (abfd, buf);
613e1657
KB
8717 break;
8718 default:
8e65ff28 8719 internal_error (__FILE__, __LINE__,
c764a876 8720 _("read_offset_1: bad switch [in module %s]"),
659b0389 8721 bfd_get_filename (abfd));
613e1657
KB
8722 }
8723
917c78fc 8724 return retval;
613e1657
KB
8725}
8726
fe1b8b76
JB
8727static gdb_byte *
8728read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
8729{
8730 /* If the size of a host char is 8 bits, we can return a pointer
8731 to the buffer, otherwise we have to copy the data to a buffer
8732 allocated on the temporary obstack. */
4bdf3d34 8733 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 8734 return buf;
c906108c
SS
8735}
8736
8737static char *
fe1b8b76 8738read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
8739{
8740 /* If the size of a host char is 8 bits, we can return a pointer
8741 to the string, otherwise we have to copy the string to a buffer
8742 allocated on the temporary obstack. */
4bdf3d34 8743 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
8744 if (*buf == '\0')
8745 {
8746 *bytes_read_ptr = 1;
8747 return NULL;
8748 }
fe1b8b76
JB
8749 *bytes_read_ptr = strlen ((char *) buf) + 1;
8750 return (char *) buf;
4bdf3d34
JJ
8751}
8752
8753static char *
fe1b8b76 8754read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
8755 const struct comp_unit_head *cu_header,
8756 unsigned int *bytes_read_ptr)
8757{
c764a876 8758 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 8759
be391dca 8760 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 8761 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 8762 {
8a3fe4f8 8763 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 8764 bfd_get_filename (abfd));
4bdf3d34 8765 return NULL;
c906108c 8766 }
dce234bc 8767 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 8768 {
8a3fe4f8 8769 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 8770 bfd_get_filename (abfd));
c906108c
SS
8771 return NULL;
8772 }
4bdf3d34 8773 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 8774 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 8775 return NULL;
dce234bc 8776 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
8777}
8778
ce5d95e1 8779static unsigned long
fe1b8b76 8780read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 8781{
ce5d95e1
JB
8782 unsigned long result;
8783 unsigned int num_read;
c906108c
SS
8784 int i, shift;
8785 unsigned char byte;
8786
8787 result = 0;
8788 shift = 0;
8789 num_read = 0;
8790 i = 0;
8791 while (1)
8792 {
fe1b8b76 8793 byte = bfd_get_8 (abfd, buf);
c906108c
SS
8794 buf++;
8795 num_read++;
ce5d95e1 8796 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
8797 if ((byte & 128) == 0)
8798 {
8799 break;
8800 }
8801 shift += 7;
8802 }
8803 *bytes_read_ptr = num_read;
8804 return result;
8805}
8806
ce5d95e1 8807static long
fe1b8b76 8808read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 8809{
ce5d95e1 8810 long result;
77e0b926 8811 int i, shift, num_read;
c906108c
SS
8812 unsigned char byte;
8813
8814 result = 0;
8815 shift = 0;
c906108c
SS
8816 num_read = 0;
8817 i = 0;
8818 while (1)
8819 {
fe1b8b76 8820 byte = bfd_get_8 (abfd, buf);
c906108c
SS
8821 buf++;
8822 num_read++;
ce5d95e1 8823 result |= ((long)(byte & 127) << shift);
c906108c
SS
8824 shift += 7;
8825 if ((byte & 128) == 0)
8826 {
8827 break;
8828 }
8829 }
77e0b926
DJ
8830 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
8831 result |= -(((long)1) << shift);
c906108c
SS
8832 *bytes_read_ptr = num_read;
8833 return result;
8834}
8835
4bb7a0a7
DJ
8836/* Return a pointer to just past the end of an LEB128 number in BUF. */
8837
fe1b8b76
JB
8838static gdb_byte *
8839skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
8840{
8841 int byte;
8842
8843 while (1)
8844 {
fe1b8b76 8845 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
8846 buf++;
8847 if ((byte & 128) == 0)
8848 return buf;
8849 }
8850}
8851
c906108c 8852static void
e142c38c 8853set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
8854{
8855 switch (lang)
8856 {
8857 case DW_LANG_C89:
76bee0cc 8858 case DW_LANG_C99:
c906108c 8859 case DW_LANG_C:
e142c38c 8860 cu->language = language_c;
c906108c
SS
8861 break;
8862 case DW_LANG_C_plus_plus:
e142c38c 8863 cu->language = language_cplus;
c906108c 8864 break;
6aecb9c2
JB
8865 case DW_LANG_D:
8866 cu->language = language_d;
8867 break;
c906108c
SS
8868 case DW_LANG_Fortran77:
8869 case DW_LANG_Fortran90:
b21b22e0 8870 case DW_LANG_Fortran95:
e142c38c 8871 cu->language = language_fortran;
c906108c
SS
8872 break;
8873 case DW_LANG_Mips_Assembler:
e142c38c 8874 cu->language = language_asm;
c906108c 8875 break;
bebd888e 8876 case DW_LANG_Java:
e142c38c 8877 cu->language = language_java;
bebd888e 8878 break;
c906108c 8879 case DW_LANG_Ada83:
8aaf0b47 8880 case DW_LANG_Ada95:
bc5f45f8
JB
8881 cu->language = language_ada;
8882 break;
72019c9c
GM
8883 case DW_LANG_Modula2:
8884 cu->language = language_m2;
8885 break;
fe8e67fd
PM
8886 case DW_LANG_Pascal83:
8887 cu->language = language_pascal;
8888 break;
22566fbd
DJ
8889 case DW_LANG_ObjC:
8890 cu->language = language_objc;
8891 break;
c906108c
SS
8892 case DW_LANG_Cobol74:
8893 case DW_LANG_Cobol85:
c906108c 8894 default:
e142c38c 8895 cu->language = language_minimal;
c906108c
SS
8896 break;
8897 }
e142c38c 8898 cu->language_defn = language_def (cu->language);
c906108c
SS
8899}
8900
8901/* Return the named attribute or NULL if not there. */
8902
8903static struct attribute *
e142c38c 8904dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
8905{
8906 unsigned int i;
8907 struct attribute *spec = NULL;
8908
8909 for (i = 0; i < die->num_attrs; ++i)
8910 {
8911 if (die->attrs[i].name == name)
10b3939b 8912 return &die->attrs[i];
c906108c
SS
8913 if (die->attrs[i].name == DW_AT_specification
8914 || die->attrs[i].name == DW_AT_abstract_origin)
8915 spec = &die->attrs[i];
8916 }
c906108c 8917
10b3939b 8918 if (spec)
f2f0e013
DJ
8919 {
8920 die = follow_die_ref (die, spec, &cu);
8921 return dwarf2_attr (die, name, cu);
8922 }
c5aa993b 8923
c906108c
SS
8924 return NULL;
8925}
8926
348e048f
DE
8927/* Return the named attribute or NULL if not there,
8928 but do not follow DW_AT_specification, etc.
8929 This is for use in contexts where we're reading .debug_types dies.
8930 Following DW_AT_specification, DW_AT_abstract_origin will take us
8931 back up the chain, and we want to go down. */
8932
8933static struct attribute *
8934dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
8935 struct dwarf2_cu *cu)
8936{
8937 unsigned int i;
8938
8939 for (i = 0; i < die->num_attrs; ++i)
8940 if (die->attrs[i].name == name)
8941 return &die->attrs[i];
8942
8943 return NULL;
8944}
8945
05cf31d1
JB
8946/* Return non-zero iff the attribute NAME is defined for the given DIE,
8947 and holds a non-zero value. This function should only be used for
2dc7f7b3 8948 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
8949
8950static int
8951dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
8952{
8953 struct attribute *attr = dwarf2_attr (die, name, cu);
8954
8955 return (attr && DW_UNSND (attr));
8956}
8957
3ca72b44 8958static int
e142c38c 8959die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 8960{
05cf31d1
JB
8961 /* A DIE is a declaration if it has a DW_AT_declaration attribute
8962 which value is non-zero. However, we have to be careful with
8963 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
8964 (via dwarf2_flag_true_p) follows this attribute. So we may
8965 end up accidently finding a declaration attribute that belongs
8966 to a different DIE referenced by the specification attribute,
8967 even though the given DIE does not have a declaration attribute. */
8968 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
8969 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
8970}
8971
63d06c5c 8972/* Return the die giving the specification for DIE, if there is
f2f0e013 8973 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
8974 containing the return value on output. If there is no
8975 specification, but there is an abstract origin, that is
8976 returned. */
63d06c5c
DC
8977
8978static struct die_info *
f2f0e013 8979die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 8980{
f2f0e013
DJ
8981 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
8982 *spec_cu);
63d06c5c 8983
edb3359d
DJ
8984 if (spec_attr == NULL)
8985 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
8986
63d06c5c
DC
8987 if (spec_attr == NULL)
8988 return NULL;
8989 else
f2f0e013 8990 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 8991}
c906108c 8992
debd256d
JB
8993/* Free the line_header structure *LH, and any arrays and strings it
8994 refers to. */
8995static void
8996free_line_header (struct line_header *lh)
8997{
8998 if (lh->standard_opcode_lengths)
a8bc7b56 8999 xfree (lh->standard_opcode_lengths);
debd256d
JB
9000
9001 /* Remember that all the lh->file_names[i].name pointers are
9002 pointers into debug_line_buffer, and don't need to be freed. */
9003 if (lh->file_names)
a8bc7b56 9004 xfree (lh->file_names);
debd256d
JB
9005
9006 /* Similarly for the include directory names. */
9007 if (lh->include_dirs)
a8bc7b56 9008 xfree (lh->include_dirs);
debd256d 9009
a8bc7b56 9010 xfree (lh);
debd256d
JB
9011}
9012
9013
9014/* Add an entry to LH's include directory table. */
9015static void
9016add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9017{
debd256d
JB
9018 /* Grow the array if necessary. */
9019 if (lh->include_dirs_size == 0)
c5aa993b 9020 {
debd256d
JB
9021 lh->include_dirs_size = 1; /* for testing */
9022 lh->include_dirs = xmalloc (lh->include_dirs_size
9023 * sizeof (*lh->include_dirs));
9024 }
9025 else if (lh->num_include_dirs >= lh->include_dirs_size)
9026 {
9027 lh->include_dirs_size *= 2;
9028 lh->include_dirs = xrealloc (lh->include_dirs,
9029 (lh->include_dirs_size
9030 * sizeof (*lh->include_dirs)));
c5aa993b 9031 }
c906108c 9032
debd256d
JB
9033 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9034}
6e70227d 9035
debd256d
JB
9036
9037/* Add an entry to LH's file name table. */
9038static void
9039add_file_name (struct line_header *lh,
9040 char *name,
9041 unsigned int dir_index,
9042 unsigned int mod_time,
9043 unsigned int length)
9044{
9045 struct file_entry *fe;
9046
9047 /* Grow the array if necessary. */
9048 if (lh->file_names_size == 0)
9049 {
9050 lh->file_names_size = 1; /* for testing */
9051 lh->file_names = xmalloc (lh->file_names_size
9052 * sizeof (*lh->file_names));
9053 }
9054 else if (lh->num_file_names >= lh->file_names_size)
9055 {
9056 lh->file_names_size *= 2;
9057 lh->file_names = xrealloc (lh->file_names,
9058 (lh->file_names_size
9059 * sizeof (*lh->file_names)));
9060 }
9061
9062 fe = &lh->file_names[lh->num_file_names++];
9063 fe->name = name;
9064 fe->dir_index = dir_index;
9065 fe->mod_time = mod_time;
9066 fe->length = length;
aaa75496 9067 fe->included_p = 0;
cb1df416 9068 fe->symtab = NULL;
debd256d 9069}
6e70227d 9070
debd256d
JB
9071
9072/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9073 .debug_line, according to the endianness of ABFD. Return a pointer
9074 to a struct line_header, allocated using xmalloc.
debd256d
JB
9075
9076 NOTE: the strings in the include directory and file name tables of
9077 the returned object point into debug_line_buffer, and must not be
9078 freed. */
9079static struct line_header *
9080dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9081 struct dwarf2_cu *cu)
debd256d
JB
9082{
9083 struct cleanup *back_to;
9084 struct line_header *lh;
fe1b8b76 9085 gdb_byte *line_ptr;
c764a876 9086 unsigned int bytes_read, offset_size;
debd256d
JB
9087 int i;
9088 char *cur_dir, *cur_file;
9089
be391dca 9090 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9091 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9092 {
e2e0b3e5 9093 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9094 return 0;
9095 }
9096
a738430d
MK
9097 /* Make sure that at least there's room for the total_length field.
9098 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9099 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9100 {
4d3c2250 9101 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9102 return 0;
9103 }
9104
9105 lh = xmalloc (sizeof (*lh));
9106 memset (lh, 0, sizeof (*lh));
9107 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9108 (void *) lh);
9109
dce234bc 9110 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9111
a738430d 9112 /* Read in the header. */
6e70227d 9113 lh->total_length =
c764a876
DE
9114 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9115 &bytes_read, &offset_size);
debd256d 9116 line_ptr += bytes_read;
dce234bc
PP
9117 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9118 + dwarf2_per_objfile->line.size))
debd256d 9119 {
4d3c2250 9120 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9121 return 0;
9122 }
9123 lh->statement_program_end = line_ptr + lh->total_length;
9124 lh->version = read_2_bytes (abfd, line_ptr);
9125 line_ptr += 2;
c764a876
DE
9126 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9127 line_ptr += offset_size;
debd256d
JB
9128 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9129 line_ptr += 1;
2dc7f7b3
TT
9130 if (lh->version >= 4)
9131 {
9132 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9133 line_ptr += 1;
9134 }
9135 else
9136 lh->maximum_ops_per_instruction = 1;
9137
9138 if (lh->maximum_ops_per_instruction == 0)
9139 {
9140 lh->maximum_ops_per_instruction = 1;
9141 complaint (&symfile_complaints,
9142 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9143 }
9144
debd256d
JB
9145 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9146 line_ptr += 1;
9147 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9148 line_ptr += 1;
9149 lh->line_range = read_1_byte (abfd, line_ptr);
9150 line_ptr += 1;
9151 lh->opcode_base = read_1_byte (abfd, line_ptr);
9152 line_ptr += 1;
9153 lh->standard_opcode_lengths
fe1b8b76 9154 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9155
9156 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9157 for (i = 1; i < lh->opcode_base; ++i)
9158 {
9159 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9160 line_ptr += 1;
9161 }
9162
a738430d 9163 /* Read directory table. */
debd256d
JB
9164 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9165 {
9166 line_ptr += bytes_read;
9167 add_include_dir (lh, cur_dir);
9168 }
9169 line_ptr += bytes_read;
9170
a738430d 9171 /* Read file name table. */
debd256d
JB
9172 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9173 {
9174 unsigned int dir_index, mod_time, length;
9175
9176 line_ptr += bytes_read;
9177 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9178 line_ptr += bytes_read;
9179 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9180 line_ptr += bytes_read;
9181 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9182 line_ptr += bytes_read;
9183
9184 add_file_name (lh, cur_file, dir_index, mod_time, length);
9185 }
9186 line_ptr += bytes_read;
6e70227d 9187 lh->statement_program_start = line_ptr;
debd256d 9188
dce234bc
PP
9189 if (line_ptr > (dwarf2_per_objfile->line.buffer
9190 + dwarf2_per_objfile->line.size))
4d3c2250 9191 complaint (&symfile_complaints,
e2e0b3e5 9192 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9193
9194 discard_cleanups (back_to);
9195 return lh;
9196}
c906108c 9197
5fb290d7
DJ
9198/* This function exists to work around a bug in certain compilers
9199 (particularly GCC 2.95), in which the first line number marker of a
9200 function does not show up until after the prologue, right before
9201 the second line number marker. This function shifts ADDRESS down
9202 to the beginning of the function if necessary, and is called on
9203 addresses passed to record_line. */
9204
9205static CORE_ADDR
e142c38c 9206check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9207{
9208 struct function_range *fn;
9209
9210 /* Find the function_range containing address. */
e142c38c 9211 if (!cu->first_fn)
5fb290d7
DJ
9212 return address;
9213
e142c38c
DJ
9214 if (!cu->cached_fn)
9215 cu->cached_fn = cu->first_fn;
5fb290d7 9216
e142c38c 9217 fn = cu->cached_fn;
5fb290d7
DJ
9218 while (fn)
9219 if (fn->lowpc <= address && fn->highpc > address)
9220 goto found;
9221 else
9222 fn = fn->next;
9223
e142c38c
DJ
9224 fn = cu->first_fn;
9225 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9226 if (fn->lowpc <= address && fn->highpc > address)
9227 goto found;
9228 else
9229 fn = fn->next;
9230
9231 return address;
9232
9233 found:
9234 if (fn->seen_line)
9235 return address;
9236 if (address != fn->lowpc)
4d3c2250 9237 complaint (&symfile_complaints,
e2e0b3e5 9238 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9239 (unsigned long) address, fn->name);
5fb290d7
DJ
9240 fn->seen_line = 1;
9241 return fn->lowpc;
9242}
9243
aaa75496
JB
9244/* Decode the Line Number Program (LNP) for the given line_header
9245 structure and CU. The actual information extracted and the type
9246 of structures created from the LNP depends on the value of PST.
9247
9248 1. If PST is NULL, then this procedure uses the data from the program
9249 to create all necessary symbol tables, and their linetables.
9250 The compilation directory of the file is passed in COMP_DIR,
9251 and must not be NULL.
6e70227d 9252
aaa75496
JB
9253 2. If PST is not NULL, this procedure reads the program to determine
9254 the list of files included by the unit represented by PST, and
9255 builds all the associated partial symbol tables. In this case,
9256 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9257 is not used to compute the full name of the symtab, and therefore
9258 omitting it when building the partial symtab does not introduce
9259 the potential for inconsistency - a partial symtab and its associated
9260 symbtab having a different fullname -). */
debd256d 9261
c906108c 9262static void
debd256d 9263dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 9264 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 9265{
a8c50c1f 9266 gdb_byte *line_ptr, *extended_end;
fe1b8b76 9267 gdb_byte *line_end;
a8c50c1f 9268 unsigned int bytes_read, extended_len;
c906108c 9269 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
9270 CORE_ADDR baseaddr;
9271 struct objfile *objfile = cu->objfile;
fbf65064 9272 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 9273 const int decode_for_pst_p = (pst != NULL);
cb1df416 9274 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
9275
9276 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9277
debd256d
JB
9278 line_ptr = lh->statement_program_start;
9279 line_end = lh->statement_program_end;
c906108c
SS
9280
9281 /* Read the statement sequences until there's nothing left. */
9282 while (line_ptr < line_end)
9283 {
9284 /* state machine registers */
9285 CORE_ADDR address = 0;
9286 unsigned int file = 1;
9287 unsigned int line = 1;
9288 unsigned int column = 0;
debd256d 9289 int is_stmt = lh->default_is_stmt;
c906108c
SS
9290 int basic_block = 0;
9291 int end_sequence = 0;
fbf65064 9292 CORE_ADDR addr;
2dc7f7b3 9293 unsigned char op_index = 0;
c906108c 9294
aaa75496 9295 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 9296 {
aaa75496 9297 /* Start a subfile for the current file of the state machine. */
debd256d
JB
9298 /* lh->include_dirs and lh->file_names are 0-based, but the
9299 directory and file name numbers in the statement program
9300 are 1-based. */
9301 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 9302 char *dir = NULL;
a738430d 9303
debd256d
JB
9304 if (fe->dir_index)
9305 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
9306
9307 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
9308 }
9309
a738430d 9310 /* Decode the table. */
c5aa993b 9311 while (!end_sequence)
c906108c
SS
9312 {
9313 op_code = read_1_byte (abfd, line_ptr);
9314 line_ptr += 1;
59205f5a
JB
9315 if (line_ptr > line_end)
9316 {
9317 dwarf2_debug_line_missing_end_sequence_complaint ();
9318 break;
9319 }
9aa1fe7e 9320
debd256d 9321 if (op_code >= lh->opcode_base)
6e70227d 9322 {
a738430d 9323 /* Special operand. */
debd256d 9324 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
9325 address += (((op_index + (adj_opcode / lh->line_range))
9326 / lh->maximum_ops_per_instruction)
9327 * lh->minimum_instruction_length);
9328 op_index = ((op_index + (adj_opcode / lh->line_range))
9329 % lh->maximum_ops_per_instruction);
debd256d 9330 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 9331 if (lh->num_file_names < file || file == 0)
25e43795 9332 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
9333 /* For now we ignore lines not starting on an
9334 instruction boundary. */
9335 else if (op_index == 0)
25e43795
DJ
9336 {
9337 lh->file_names[file - 1].included_p = 1;
ca5f395d 9338 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9339 {
9340 if (last_subfile != current_subfile)
9341 {
9342 addr = gdbarch_addr_bits_remove (gdbarch, address);
9343 if (last_subfile)
9344 record_line (last_subfile, 0, addr);
9345 last_subfile = current_subfile;
9346 }
25e43795 9347 /* Append row to matrix using current values. */
fbf65064
UW
9348 addr = check_cu_functions (address, cu);
9349 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9350 record_line (current_subfile, line, addr);
366da635 9351 }
25e43795 9352 }
ca5f395d 9353 basic_block = 0;
9aa1fe7e
GK
9354 }
9355 else switch (op_code)
c906108c
SS
9356 {
9357 case DW_LNS_extended_op:
a8c50c1f 9358 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 9359 line_ptr += bytes_read;
a8c50c1f 9360 extended_end = line_ptr + extended_len;
c906108c
SS
9361 extended_op = read_1_byte (abfd, line_ptr);
9362 line_ptr += 1;
9363 switch (extended_op)
9364 {
9365 case DW_LNE_end_sequence:
9366 end_sequence = 1;
c906108c
SS
9367 break;
9368 case DW_LNE_set_address:
e7c27a73 9369 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 9370 op_index = 0;
107d2387
AC
9371 line_ptr += bytes_read;
9372 address += baseaddr;
c906108c
SS
9373 break;
9374 case DW_LNE_define_file:
debd256d
JB
9375 {
9376 char *cur_file;
9377 unsigned int dir_index, mod_time, length;
6e70227d 9378
debd256d
JB
9379 cur_file = read_string (abfd, line_ptr, &bytes_read);
9380 line_ptr += bytes_read;
9381 dir_index =
9382 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9383 line_ptr += bytes_read;
9384 mod_time =
9385 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9386 line_ptr += bytes_read;
9387 length =
9388 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9389 line_ptr += bytes_read;
9390 add_file_name (lh, cur_file, dir_index, mod_time, length);
9391 }
c906108c 9392 break;
d0c6ba3d
CC
9393 case DW_LNE_set_discriminator:
9394 /* The discriminator is not interesting to the debugger;
9395 just ignore it. */
9396 line_ptr = extended_end;
9397 break;
c906108c 9398 default:
4d3c2250 9399 complaint (&symfile_complaints,
e2e0b3e5 9400 _("mangled .debug_line section"));
debd256d 9401 return;
c906108c 9402 }
a8c50c1f
DJ
9403 /* Make sure that we parsed the extended op correctly. If e.g.
9404 we expected a different address size than the producer used,
9405 we may have read the wrong number of bytes. */
9406 if (line_ptr != extended_end)
9407 {
9408 complaint (&symfile_complaints,
9409 _("mangled .debug_line section"));
9410 return;
9411 }
c906108c
SS
9412 break;
9413 case DW_LNS_copy:
59205f5a 9414 if (lh->num_file_names < file || file == 0)
25e43795
DJ
9415 dwarf2_debug_line_missing_file_complaint ();
9416 else
366da635 9417 {
25e43795 9418 lh->file_names[file - 1].included_p = 1;
ca5f395d 9419 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9420 {
9421 if (last_subfile != current_subfile)
9422 {
9423 addr = gdbarch_addr_bits_remove (gdbarch, address);
9424 if (last_subfile)
9425 record_line (last_subfile, 0, addr);
9426 last_subfile = current_subfile;
9427 }
9428 addr = check_cu_functions (address, cu);
9429 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9430 record_line (current_subfile, line, addr);
9431 }
366da635 9432 }
c906108c
SS
9433 basic_block = 0;
9434 break;
9435 case DW_LNS_advance_pc:
2dc7f7b3
TT
9436 {
9437 CORE_ADDR adjust
9438 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9439
9440 address += (((op_index + adjust)
9441 / lh->maximum_ops_per_instruction)
9442 * lh->minimum_instruction_length);
9443 op_index = ((op_index + adjust)
9444 % lh->maximum_ops_per_instruction);
9445 line_ptr += bytes_read;
9446 }
c906108c
SS
9447 break;
9448 case DW_LNS_advance_line:
9449 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9450 line_ptr += bytes_read;
9451 break;
9452 case DW_LNS_set_file:
debd256d 9453 {
a738430d
MK
9454 /* The arrays lh->include_dirs and lh->file_names are
9455 0-based, but the directory and file name numbers in
9456 the statement program are 1-based. */
debd256d 9457 struct file_entry *fe;
4f1520fb 9458 char *dir = NULL;
a738430d 9459
debd256d
JB
9460 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9461 line_ptr += bytes_read;
59205f5a 9462 if (lh->num_file_names < file || file == 0)
25e43795
DJ
9463 dwarf2_debug_line_missing_file_complaint ();
9464 else
9465 {
9466 fe = &lh->file_names[file - 1];
9467 if (fe->dir_index)
9468 dir = lh->include_dirs[fe->dir_index - 1];
9469 if (!decode_for_pst_p)
9470 {
9471 last_subfile = current_subfile;
9472 dwarf2_start_subfile (fe->name, dir, comp_dir);
9473 }
9474 }
debd256d 9475 }
c906108c
SS
9476 break;
9477 case DW_LNS_set_column:
9478 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9479 line_ptr += bytes_read;
9480 break;
9481 case DW_LNS_negate_stmt:
9482 is_stmt = (!is_stmt);
9483 break;
9484 case DW_LNS_set_basic_block:
9485 basic_block = 1;
9486 break;
c2c6d25f
JM
9487 /* Add to the address register of the state machine the
9488 address increment value corresponding to special opcode
a738430d
MK
9489 255. I.e., this value is scaled by the minimum
9490 instruction length since special opcode 255 would have
9491 scaled the the increment. */
c906108c 9492 case DW_LNS_const_add_pc:
2dc7f7b3
TT
9493 {
9494 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
9495
9496 address += (((op_index + adjust)
9497 / lh->maximum_ops_per_instruction)
9498 * lh->minimum_instruction_length);
9499 op_index = ((op_index + adjust)
9500 % lh->maximum_ops_per_instruction);
9501 }
c906108c
SS
9502 break;
9503 case DW_LNS_fixed_advance_pc:
9504 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 9505 op_index = 0;
c906108c
SS
9506 line_ptr += 2;
9507 break;
9aa1fe7e 9508 default:
a738430d
MK
9509 {
9510 /* Unknown standard opcode, ignore it. */
9aa1fe7e 9511 int i;
a738430d 9512
debd256d 9513 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
9514 {
9515 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9516 line_ptr += bytes_read;
9517 }
9518 }
c906108c
SS
9519 }
9520 }
59205f5a
JB
9521 if (lh->num_file_names < file || file == 0)
9522 dwarf2_debug_line_missing_file_complaint ();
9523 else
9524 {
9525 lh->file_names[file - 1].included_p = 1;
9526 if (!decode_for_pst_p)
fbf65064
UW
9527 {
9528 addr = gdbarch_addr_bits_remove (gdbarch, address);
9529 record_line (current_subfile, 0, addr);
9530 }
59205f5a 9531 }
c906108c 9532 }
aaa75496
JB
9533
9534 if (decode_for_pst_p)
9535 {
9536 int file_index;
9537
9538 /* Now that we're done scanning the Line Header Program, we can
9539 create the psymtab of each included file. */
9540 for (file_index = 0; file_index < lh->num_file_names; file_index++)
9541 if (lh->file_names[file_index].included_p == 1)
9542 {
5b5464ad
JB
9543 const struct file_entry fe = lh->file_names [file_index];
9544 char *include_name = fe.name;
9545 char *dir_name = NULL;
9546 char *pst_filename = pst->filename;
9547
9548 if (fe.dir_index)
9549 dir_name = lh->include_dirs[fe.dir_index - 1];
9550
9551 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
9552 {
1754f103
MK
9553 include_name = concat (dir_name, SLASH_STRING,
9554 include_name, (char *)NULL);
5b5464ad
JB
9555 make_cleanup (xfree, include_name);
9556 }
9557
9558 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9559 {
1754f103
MK
9560 pst_filename = concat (pst->dirname, SLASH_STRING,
9561 pst_filename, (char *)NULL);
5b5464ad
JB
9562 make_cleanup (xfree, pst_filename);
9563 }
9564
9565 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
9566 dwarf2_create_include_psymtab (include_name, pst, objfile);
9567 }
9568 }
cb1df416
DJ
9569 else
9570 {
9571 /* Make sure a symtab is created for every file, even files
9572 which contain only variables (i.e. no code with associated
9573 line numbers). */
9574
9575 int i;
9576 struct file_entry *fe;
9577
9578 for (i = 0; i < lh->num_file_names; i++)
9579 {
9580 char *dir = NULL;
9a619af0 9581
cb1df416
DJ
9582 fe = &lh->file_names[i];
9583 if (fe->dir_index)
9584 dir = lh->include_dirs[fe->dir_index - 1];
9585 dwarf2_start_subfile (fe->name, dir, comp_dir);
9586
9587 /* Skip the main file; we don't need it, and it must be
9588 allocated last, so that it will show up before the
9589 non-primary symtabs in the objfile's symtab list. */
9590 if (current_subfile == first_subfile)
9591 continue;
9592
9593 if (current_subfile->symtab == NULL)
9594 current_subfile->symtab = allocate_symtab (current_subfile->name,
9595 cu->objfile);
9596 fe->symtab = current_subfile->symtab;
9597 }
9598 }
c906108c
SS
9599}
9600
9601/* Start a subfile for DWARF. FILENAME is the name of the file and
9602 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
9603 or NULL if not known. COMP_DIR is the compilation directory for the
9604 linetable's compilation unit or NULL if not known.
c906108c
SS
9605 This routine tries to keep line numbers from identical absolute and
9606 relative file names in a common subfile.
9607
9608 Using the `list' example from the GDB testsuite, which resides in
9609 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
9610 of /srcdir/list0.c yields the following debugging information for list0.c:
9611
c5aa993b
JM
9612 DW_AT_name: /srcdir/list0.c
9613 DW_AT_comp_dir: /compdir
357e46e7 9614 files.files[0].name: list0.h
c5aa993b 9615 files.files[0].dir: /srcdir
357e46e7 9616 files.files[1].name: list0.c
c5aa993b 9617 files.files[1].dir: /srcdir
c906108c
SS
9618
9619 The line number information for list0.c has to end up in a single
4f1520fb
FR
9620 subfile, so that `break /srcdir/list0.c:1' works as expected.
9621 start_subfile will ensure that this happens provided that we pass the
9622 concatenation of files.files[1].dir and files.files[1].name as the
9623 subfile's name. */
c906108c
SS
9624
9625static void
4f1520fb 9626dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 9627{
4f1520fb
FR
9628 char *fullname;
9629
9630 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
9631 `start_symtab' will always pass the contents of DW_AT_comp_dir as
9632 second argument to start_subfile. To be consistent, we do the
9633 same here. In order not to lose the line information directory,
9634 we concatenate it to the filename when it makes sense.
9635 Note that the Dwarf3 standard says (speaking of filenames in line
9636 information): ``The directory index is ignored for file names
9637 that represent full path names''. Thus ignoring dirname in the
9638 `else' branch below isn't an issue. */
c906108c 9639
d5166ae1 9640 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
9641 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
9642 else
9643 fullname = filename;
c906108c 9644
4f1520fb
FR
9645 start_subfile (fullname, comp_dir);
9646
9647 if (fullname != filename)
9648 xfree (fullname);
c906108c
SS
9649}
9650
4c2df51b
DJ
9651static void
9652var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 9653 struct dwarf2_cu *cu)
4c2df51b 9654{
e7c27a73
DJ
9655 struct objfile *objfile = cu->objfile;
9656 struct comp_unit_head *cu_header = &cu->header;
9657
4c2df51b
DJ
9658 /* NOTE drow/2003-01-30: There used to be a comment and some special
9659 code here to turn a symbol with DW_AT_external and a
9660 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
9661 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
9662 with some versions of binutils) where shared libraries could have
9663 relocations against symbols in their debug information - the
9664 minimal symbol would have the right address, but the debug info
9665 would not. It's no longer necessary, because we will explicitly
9666 apply relocations when we read in the debug information now. */
9667
9668 /* A DW_AT_location attribute with no contents indicates that a
9669 variable has been optimized away. */
9670 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
9671 {
9672 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9673 return;
9674 }
9675
9676 /* Handle one degenerate form of location expression specially, to
9677 preserve GDB's previous behavior when section offsets are
9678 specified. If this is just a DW_OP_addr then mark this symbol
9679 as LOC_STATIC. */
9680
9681 if (attr_form_is_block (attr)
9682 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
9683 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
9684 {
891d2f0b 9685 unsigned int dummy;
4c2df51b
DJ
9686
9687 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 9688 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 9689 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
9690 fixup_symbol_section (sym, objfile);
9691 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
9692 SYMBOL_SECTION (sym));
4c2df51b
DJ
9693 return;
9694 }
9695
9696 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
9697 expression evaluator, and use LOC_COMPUTED only when necessary
9698 (i.e. when the value of a register or memory location is
9699 referenced, or a thread-local block, etc.). Then again, it might
9700 not be worthwhile. I'm assuming that it isn't unless performance
9701 or memory numbers show me otherwise. */
9702
e7c27a73 9703 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
9704 SYMBOL_CLASS (sym) = LOC_COMPUTED;
9705}
9706
c906108c
SS
9707/* Given a pointer to a DWARF information entry, figure out if we need
9708 to make a symbol table entry for it, and if so, create a new entry
9709 and return a pointer to it.
9710 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 9711 used the passed type. */
c906108c
SS
9712
9713static struct symbol *
e7c27a73 9714new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 9715{
e7c27a73 9716 struct objfile *objfile = cu->objfile;
c906108c
SS
9717 struct symbol *sym = NULL;
9718 char *name;
9719 struct attribute *attr = NULL;
9720 struct attribute *attr2 = NULL;
e142c38c 9721 CORE_ADDR baseaddr;
edb3359d 9722 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
9723
9724 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9725
94af9270 9726 name = dwarf2_name (die, cu);
c906108c
SS
9727 if (name)
9728 {
94af9270
KS
9729 const char *linkagename;
9730
4a146b47 9731 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
9732 sizeof (struct symbol));
9733 OBJSTAT (objfile, n_syms++);
9734 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
9735
9736 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 9737 SYMBOL_LANGUAGE (sym) = cu->language;
94af9270
KS
9738 linkagename = dwarf2_physname (name, die, cu);
9739 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 9740
f55ee35c
JK
9741 /* Fortran does not have mangling standard and the mangling does differ
9742 between gfortran, iFort etc. */
9743 if (cu->language == language_fortran
b250c185 9744 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
9745 symbol_set_demangled_name (&(sym->ginfo),
9746 (char *) dwarf2_full_name (name, die, cu),
9747 NULL);
f55ee35c 9748
c906108c 9749 /* Default assumptions.
c5aa993b 9750 Use the passed type or decode it from the die. */
176620f1 9751 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 9752 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
9753 if (type != NULL)
9754 SYMBOL_TYPE (sym) = type;
9755 else
e7c27a73 9756 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
9757 attr = dwarf2_attr (die,
9758 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
9759 cu);
c906108c
SS
9760 if (attr)
9761 {
9762 SYMBOL_LINE (sym) = DW_UNSND (attr);
9763 }
cb1df416 9764
edb3359d
DJ
9765 attr = dwarf2_attr (die,
9766 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
9767 cu);
cb1df416
DJ
9768 if (attr)
9769 {
9770 int file_index = DW_UNSND (attr);
9a619af0 9771
cb1df416
DJ
9772 if (cu->line_header == NULL
9773 || file_index > cu->line_header->num_file_names)
9774 complaint (&symfile_complaints,
9775 _("file index out of range"));
1c3d648d 9776 else if (file_index > 0)
cb1df416
DJ
9777 {
9778 struct file_entry *fe;
9a619af0 9779
cb1df416
DJ
9780 fe = &cu->line_header->file_names[file_index - 1];
9781 SYMBOL_SYMTAB (sym) = fe->symtab;
9782 }
9783 }
9784
c906108c
SS
9785 switch (die->tag)
9786 {
9787 case DW_TAG_label:
e142c38c 9788 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
9789 if (attr)
9790 {
9791 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
9792 }
0f5238ed
TT
9793 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
9794 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 9795 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 9796 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
9797 break;
9798 case DW_TAG_subprogram:
9799 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9800 finish_block. */
9801 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 9802 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
9803 if ((attr2 && (DW_UNSND (attr2) != 0))
9804 || cu->language == language_ada)
c906108c 9805 {
2cfa0c8d
JB
9806 /* Subprograms marked external are stored as a global symbol.
9807 Ada subprograms, whether marked external or not, are always
9808 stored as a global symbol, because we want to be able to
9809 access them globally. For instance, we want to be able
9810 to break on a nested subprogram without having to
9811 specify the context. */
c906108c
SS
9812 add_symbol_to_list (sym, &global_symbols);
9813 }
9814 else
9815 {
e142c38c 9816 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
9817 }
9818 break;
edb3359d
DJ
9819 case DW_TAG_inlined_subroutine:
9820 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9821 finish_block. */
9822 SYMBOL_CLASS (sym) = LOC_BLOCK;
9823 SYMBOL_INLINED (sym) = 1;
9824 /* Do not add the symbol to any lists. It will be found via
9825 BLOCK_FUNCTION from the blockvector. */
9826 break;
c906108c 9827 case DW_TAG_variable:
254e6b9e 9828 case DW_TAG_member:
c906108c
SS
9829 /* Compilation with minimal debug info may result in variables
9830 with missing type entries. Change the misleading `void' type
9831 to something sensible. */
9832 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 9833 SYMBOL_TYPE (sym)
46bf5051 9834 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 9835
e142c38c 9836 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
9837 /* In the case of DW_TAG_member, we should only be called for
9838 static const members. */
9839 if (die->tag == DW_TAG_member)
9840 {
3863f96c
DE
9841 /* dwarf2_add_field uses die_is_declaration,
9842 so we do the same. */
254e6b9e
DE
9843 gdb_assert (die_is_declaration (die, cu));
9844 gdb_assert (attr);
9845 }
c906108c
SS
9846 if (attr)
9847 {
e7c27a73 9848 dwarf2_const_value (attr, sym, cu);
e142c38c 9849 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
9850 if (attr2 && (DW_UNSND (attr2) != 0))
9851 add_symbol_to_list (sym, &global_symbols);
9852 else
e142c38c 9853 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
9854 break;
9855 }
e142c38c 9856 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9857 if (attr)
9858 {
e7c27a73 9859 var_decode_location (attr, sym, cu);
e142c38c 9860 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 9861 if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68
TT
9862 {
9863 struct pending **list_to_add;
9864
f55ee35c
JK
9865 /* Workaround gfortran PR debug/40040 - it uses
9866 DW_AT_location for variables in -fPIC libraries which may
9867 get overriden by other libraries/executable and get
9868 a different address. Resolve it by the minimal symbol
9869 which may come from inferior's executable using copy
9870 relocation. Make this workaround only for gfortran as for
9871 other compilers GDB cannot guess the minimal symbol
9872 Fortran mangling kind. */
9873 if (cu->language == language_fortran && die->parent
9874 && die->parent->tag == DW_TAG_module
9875 && cu->producer
9876 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
9877 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
9878
1c809c68
TT
9879 /* A variable with DW_AT_external is never static,
9880 but it may be block-scoped. */
9881 list_to_add = (cu->list_in_scope == &file_symbols
9882 ? &global_symbols : cu->list_in_scope);
9883 add_symbol_to_list (sym, list_to_add);
9884 }
c906108c 9885 else
e142c38c 9886 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
9887 }
9888 else
9889 {
9890 /* We do not know the address of this symbol.
c5aa993b
JM
9891 If it is an external symbol and we have type information
9892 for it, enter the symbol as a LOC_UNRESOLVED symbol.
9893 The address of the variable will then be determined from
9894 the minimal symbol table whenever the variable is
9895 referenced. */
e142c38c 9896 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 9897 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 9898 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 9899 {
0fe7935b
DJ
9900 struct pending **list_to_add;
9901
9902 /* A variable with DW_AT_external is never static, but it
9903 may be block-scoped. */
9904 list_to_add = (cu->list_in_scope == &file_symbols
9905 ? &global_symbols : cu->list_in_scope);
9906
c906108c 9907 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
0fe7935b 9908 add_symbol_to_list (sym, list_to_add);
c906108c 9909 }
442ddf59
JK
9910 else if (!die_is_declaration (die, cu))
9911 {
9912 /* Use the default LOC_OPTIMIZED_OUT class. */
9913 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
9914 add_symbol_to_list (sym, cu->list_in_scope);
9915 }
c906108c
SS
9916 }
9917 break;
9918 case DW_TAG_formal_parameter:
edb3359d
DJ
9919 /* If we are inside a function, mark this as an argument. If
9920 not, we might be looking at an argument to an inlined function
9921 when we do not have enough information to show inlined frames;
9922 pretend it's a local variable in that case so that the user can
9923 still see it. */
9924 if (context_stack_depth > 0
9925 && context_stack[context_stack_depth - 1].name != NULL)
9926 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 9927 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9928 if (attr)
9929 {
e7c27a73 9930 var_decode_location (attr, sym, cu);
c906108c 9931 }
e142c38c 9932 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
9933 if (attr)
9934 {
e7c27a73 9935 dwarf2_const_value (attr, sym, cu);
c906108c 9936 }
f346a30d
PM
9937 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
9938 if (attr && DW_UNSND (attr))
9939 {
9940 struct type *ref_type;
9941
9942 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
9943 SYMBOL_TYPE (sym) = ref_type;
9944 }
9945
e142c38c 9946 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
9947 break;
9948 case DW_TAG_unspecified_parameters:
9949 /* From varargs functions; gdb doesn't seem to have any
9950 interest in this information, so just ignore it for now.
9951 (FIXME?) */
9952 break;
9953 case DW_TAG_class_type:
680b30c7 9954 case DW_TAG_interface_type:
c906108c
SS
9955 case DW_TAG_structure_type:
9956 case DW_TAG_union_type:
72019c9c 9957 case DW_TAG_set_type:
c906108c
SS
9958 case DW_TAG_enumeration_type:
9959 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 9960 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 9961
63d06c5c 9962 {
987504bb 9963 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
9964 really ever be static objects: otherwise, if you try
9965 to, say, break of a class's method and you're in a file
9966 which doesn't mention that class, it won't work unless
9967 the check for all static symbols in lookup_symbol_aux
9968 saves you. See the OtherFileClass tests in
9969 gdb.c++/namespace.exp. */
9970
9971 struct pending **list_to_add;
9972
e142c38c 9973 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
9974 && (cu->language == language_cplus
9975 || cu->language == language_java)
e142c38c 9976 ? &global_symbols : cu->list_in_scope);
6e70227d 9977
63d06c5c
DC
9978 add_symbol_to_list (sym, list_to_add);
9979
9980 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 9981 defines a typedef for "foo". A Java class declaration also
5eeb2539 9982 defines a typedef for the class. */
987504bb 9983 if (cu->language == language_cplus
8c6860bb
JB
9984 || cu->language == language_java
9985 || cu->language == language_ada)
63d06c5c 9986 {
d8151005
DJ
9987 /* The symbol's name is already allocated along with
9988 this objfile, so we don't need to duplicate it for
9989 the type. */
63d06c5c 9990 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 9991 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
9992 }
9993 }
c906108c
SS
9994 break;
9995 case DW_TAG_typedef:
63d06c5c
DC
9996 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
9997 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 9998 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 9999 break;
c906108c 10000 case DW_TAG_base_type:
a02abb62 10001 case DW_TAG_subrange_type:
c906108c 10002 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10003 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 10004 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10005 break;
10006 case DW_TAG_enumerator:
e142c38c 10007 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10008 if (attr)
10009 {
e7c27a73 10010 dwarf2_const_value (attr, sym, cu);
c906108c 10011 }
63d06c5c
DC
10012 {
10013 /* NOTE: carlton/2003-11-10: See comment above in the
10014 DW_TAG_class_type, etc. block. */
10015
10016 struct pending **list_to_add;
10017
e142c38c 10018 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10019 && (cu->language == language_cplus
10020 || cu->language == language_java)
e142c38c 10021 ? &global_symbols : cu->list_in_scope);
6e70227d 10022
63d06c5c
DC
10023 add_symbol_to_list (sym, list_to_add);
10024 }
c906108c 10025 break;
5c4e30ca
DC
10026 case DW_TAG_namespace:
10027 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10028 add_symbol_to_list (sym, &global_symbols);
10029 break;
c906108c
SS
10030 default:
10031 /* Not a tag we recognize. Hopefully we aren't processing
10032 trash data, but since we must specifically ignore things
10033 we don't recognize, there is nothing else we should do at
10034 this point. */
e2e0b3e5 10035 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10036 dwarf_tag_name (die->tag));
c906108c
SS
10037 break;
10038 }
df8a16a1
DJ
10039
10040 /* For the benefit of old versions of GCC, check for anonymous
10041 namespaces based on the demangled name. */
10042 if (!processing_has_namespace_info
94af9270 10043 && cu->language == language_cplus)
df8a16a1 10044 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10045 }
10046 return (sym);
10047}
10048
10049/* Copy constant value from an attribute to a symbol. */
10050
10051static void
107d2387 10052dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 10053 struct dwarf2_cu *cu)
c906108c 10054{
e7c27a73
DJ
10055 struct objfile *objfile = cu->objfile;
10056 struct comp_unit_head *cu_header = &cu->header;
e17a4113
UW
10057 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10058 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
c906108c
SS
10059 struct dwarf_block *blk;
10060
10061 switch (attr->form)
10062 {
10063 case DW_FORM_addr:
ac56253d
TT
10064 {
10065 struct dwarf2_locexpr_baton *baton;
10066 gdb_byte *data;
10067
10068 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
10069 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10070 cu_header->addr_size,
10071 TYPE_LENGTH (SYMBOL_TYPE
10072 (sym)));
10073 /* Symbols of this form are reasonably rare, so we just
10074 piggyback on the existing location code rather than writing
10075 a new implementation of symbol_computed_ops. */
10076 baton = obstack_alloc (&objfile->objfile_obstack,
10077 sizeof (struct dwarf2_locexpr_baton));
10078 baton->per_cu = cu->per_cu;
10079 gdb_assert (baton->per_cu);
10080
10081 baton->size = 2 + cu_header->addr_size;
10082 data = obstack_alloc (&objfile->objfile_obstack, baton->size);
10083 baton->data = data;
10084
10085 data[0] = DW_OP_addr;
10086 store_unsigned_integer (&data[1], cu_header->addr_size,
10087 byte_order, DW_ADDR (attr));
10088 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10089
10090 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10091 SYMBOL_LOCATION_BATON (sym) = baton;
10092 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10093 }
c906108c 10094 break;
4ac36638 10095 case DW_FORM_string:
93b5768b
PA
10096 case DW_FORM_strp:
10097 /* DW_STRING is already allocated on the obstack, point directly
10098 to it. */
10099 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
10100 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10101 break;
c906108c
SS
10102 case DW_FORM_block1:
10103 case DW_FORM_block2:
10104 case DW_FORM_block4:
10105 case DW_FORM_block:
2dc7f7b3 10106 case DW_FORM_exprloc:
c906108c
SS
10107 blk = DW_BLOCK (attr);
10108 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 10109 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
10110 blk->size,
10111 TYPE_LENGTH (SYMBOL_TYPE
10112 (sym)));
4e38b386 10113 SYMBOL_VALUE_BYTES (sym) =
4a146b47 10114 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
10115 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
10116 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10117 break;
2df3850c
JM
10118
10119 /* The DW_AT_const_value attributes are supposed to carry the
10120 symbol's value "represented as it would be on the target
10121 architecture." By the time we get here, it's already been
10122 converted to host endianness, so we just need to sign- or
10123 zero-extend it as appropriate. */
10124 case DW_FORM_data1:
10125 dwarf2_const_value_data (attr, sym, 8);
10126 break;
c906108c 10127 case DW_FORM_data2:
2df3850c
JM
10128 dwarf2_const_value_data (attr, sym, 16);
10129 break;
c906108c 10130 case DW_FORM_data4:
2df3850c
JM
10131 dwarf2_const_value_data (attr, sym, 32);
10132 break;
c906108c 10133 case DW_FORM_data8:
2df3850c
JM
10134 dwarf2_const_value_data (attr, sym, 64);
10135 break;
10136
c906108c 10137 case DW_FORM_sdata:
2df3850c
JM
10138 SYMBOL_VALUE (sym) = DW_SND (attr);
10139 SYMBOL_CLASS (sym) = LOC_CONST;
10140 break;
10141
c906108c
SS
10142 case DW_FORM_udata:
10143 SYMBOL_VALUE (sym) = DW_UNSND (attr);
10144 SYMBOL_CLASS (sym) = LOC_CONST;
10145 break;
2df3850c 10146
c906108c 10147 default:
4d3c2250 10148 complaint (&symfile_complaints,
e2e0b3e5 10149 _("unsupported const value attribute form: '%s'"),
4d3c2250 10150 dwarf_form_name (attr->form));
c906108c
SS
10151 SYMBOL_VALUE (sym) = 0;
10152 SYMBOL_CLASS (sym) = LOC_CONST;
10153 break;
10154 }
10155}
10156
2df3850c
JM
10157
10158/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
10159 or zero-extend it as appropriate for the symbol's type. */
10160static void
10161dwarf2_const_value_data (struct attribute *attr,
10162 struct symbol *sym,
10163 int bits)
10164{
10165 LONGEST l = DW_UNSND (attr);
10166
10167 if (bits < sizeof (l) * 8)
10168 {
10169 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
10170 l &= ((LONGEST) 1 << bits) - 1;
10171 else
bf9198f1 10172 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
10173 }
10174
10175 SYMBOL_VALUE (sym) = l;
10176 SYMBOL_CLASS (sym) = LOC_CONST;
10177}
10178
10179
c906108c
SS
10180/* Return the type of the die in question using its DW_AT_type attribute. */
10181
10182static struct type *
e7c27a73 10183die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10184{
c906108c
SS
10185 struct attribute *type_attr;
10186 struct die_info *type_die;
c906108c 10187
e142c38c 10188 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10189 if (!type_attr)
10190 {
10191 /* A missing DW_AT_type represents a void type. */
46bf5051 10192 return objfile_type (cu->objfile)->builtin_void;
c906108c 10193 }
348e048f
DE
10194
10195 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 10196
33ac96f0 10197 return tag_type_to_type (type_die, cu);
c906108c
SS
10198}
10199
b4ba55a1
JB
10200/* True iff CU's producer generates GNAT Ada auxiliary information
10201 that allows to find parallel types through that information instead
10202 of having to do expensive parallel lookups by type name. */
10203
10204static int
10205need_gnat_info (struct dwarf2_cu *cu)
10206{
10207 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10208 of GNAT produces this auxiliary information, without any indication
10209 that it is produced. Part of enhancing the FSF version of GNAT
10210 to produce that information will be to put in place an indicator
10211 that we can use in order to determine whether the descriptive type
10212 info is available or not. One suggestion that has been made is
10213 to use a new attribute, attached to the CU die. For now, assume
10214 that the descriptive type info is not available. */
10215 return 0;
10216}
10217
10218
10219/* Return the auxiliary type of the die in question using its
10220 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10221 attribute is not present. */
10222
10223static struct type *
10224die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10225{
b4ba55a1
JB
10226 struct attribute *type_attr;
10227 struct die_info *type_die;
10228
10229 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10230 if (!type_attr)
10231 return NULL;
10232
10233 type_die = follow_die_ref (die, type_attr, &cu);
33ac96f0 10234 return tag_type_to_type (type_die, cu);
b4ba55a1
JB
10235}
10236
10237/* If DIE has a descriptive_type attribute, then set the TYPE's
10238 descriptive type accordingly. */
10239
10240static void
10241set_descriptive_type (struct type *type, struct die_info *die,
10242 struct dwarf2_cu *cu)
10243{
10244 struct type *descriptive_type = die_descriptive_type (die, cu);
10245
10246 if (descriptive_type)
10247 {
10248 ALLOCATE_GNAT_AUX_TYPE (type);
10249 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10250 }
10251}
10252
c906108c
SS
10253/* Return the containing type of the die in question using its
10254 DW_AT_containing_type attribute. */
10255
10256static struct type *
e7c27a73 10257die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10258{
c906108c 10259 struct attribute *type_attr;
33ac96f0 10260 struct die_info *type_die;
c906108c 10261
e142c38c 10262 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
10263 if (!type_attr)
10264 error (_("Dwarf Error: Problem turning containing type into gdb type "
10265 "[in module %s]"), cu->objfile->name);
10266
10267 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10268 return tag_type_to_type (type_die, cu);
c906108c
SS
10269}
10270
c906108c 10271static struct type *
e7c27a73 10272tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10273{
f792889a
DJ
10274 struct type *this_type;
10275
10276 this_type = read_type_die (die, cu);
10277 if (!this_type)
c906108c 10278 {
b00fdb78
TT
10279 char *message, *saved;
10280
10281 /* read_type_die already issued a complaint. */
10282 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10283 cu->objfile->name,
10284 cu->header.offset,
10285 die->offset);
10286 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10287 message, strlen (message));
10288 xfree (message);
10289
10290 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 10291 }
f792889a 10292 return this_type;
c906108c
SS
10293}
10294
f792889a 10295static struct type *
e7c27a73 10296read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10297{
f792889a
DJ
10298 struct type *this_type;
10299
10300 this_type = get_die_type (die, cu);
10301 if (this_type)
10302 return this_type;
10303
c906108c
SS
10304 switch (die->tag)
10305 {
10306 case DW_TAG_class_type:
680b30c7 10307 case DW_TAG_interface_type:
c906108c
SS
10308 case DW_TAG_structure_type:
10309 case DW_TAG_union_type:
f792889a 10310 this_type = read_structure_type (die, cu);
c906108c
SS
10311 break;
10312 case DW_TAG_enumeration_type:
f792889a 10313 this_type = read_enumeration_type (die, cu);
c906108c
SS
10314 break;
10315 case DW_TAG_subprogram:
10316 case DW_TAG_subroutine_type:
edb3359d 10317 case DW_TAG_inlined_subroutine:
f792889a 10318 this_type = read_subroutine_type (die, cu);
c906108c
SS
10319 break;
10320 case DW_TAG_array_type:
f792889a 10321 this_type = read_array_type (die, cu);
c906108c 10322 break;
72019c9c 10323 case DW_TAG_set_type:
f792889a 10324 this_type = read_set_type (die, cu);
72019c9c 10325 break;
c906108c 10326 case DW_TAG_pointer_type:
f792889a 10327 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
10328 break;
10329 case DW_TAG_ptr_to_member_type:
f792889a 10330 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
10331 break;
10332 case DW_TAG_reference_type:
f792889a 10333 this_type = read_tag_reference_type (die, cu);
c906108c
SS
10334 break;
10335 case DW_TAG_const_type:
f792889a 10336 this_type = read_tag_const_type (die, cu);
c906108c
SS
10337 break;
10338 case DW_TAG_volatile_type:
f792889a 10339 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
10340 break;
10341 case DW_TAG_string_type:
f792889a 10342 this_type = read_tag_string_type (die, cu);
c906108c
SS
10343 break;
10344 case DW_TAG_typedef:
f792889a 10345 this_type = read_typedef (die, cu);
c906108c 10346 break;
a02abb62 10347 case DW_TAG_subrange_type:
f792889a 10348 this_type = read_subrange_type (die, cu);
a02abb62 10349 break;
c906108c 10350 case DW_TAG_base_type:
f792889a 10351 this_type = read_base_type (die, cu);
c906108c 10352 break;
81a17f79 10353 case DW_TAG_unspecified_type:
f792889a 10354 this_type = read_unspecified_type (die, cu);
81a17f79 10355 break;
0114d602
DJ
10356 case DW_TAG_namespace:
10357 this_type = read_namespace_type (die, cu);
10358 break;
f55ee35c
JK
10359 case DW_TAG_module:
10360 this_type = read_module_type (die, cu);
10361 break;
c906108c 10362 default:
a1f5b845 10363 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 10364 dwarf_tag_name (die->tag));
c906108c
SS
10365 break;
10366 }
63d06c5c 10367
f792889a 10368 return this_type;
63d06c5c
DC
10369}
10370
fdde2d81 10371/* Return the name of the namespace/class that DIE is defined within,
0114d602 10372 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 10373
0114d602
DJ
10374 For example, if we're within the method foo() in the following
10375 code:
10376
10377 namespace N {
10378 class C {
10379 void foo () {
10380 }
10381 };
10382 }
10383
10384 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
10385
10386static char *
e142c38c 10387determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 10388{
0114d602
DJ
10389 struct die_info *parent, *spec_die;
10390 struct dwarf2_cu *spec_cu;
10391 struct type *parent_type;
63d06c5c 10392
f55ee35c
JK
10393 if (cu->language != language_cplus && cu->language != language_java
10394 && cu->language != language_fortran)
0114d602
DJ
10395 return "";
10396
10397 /* We have to be careful in the presence of DW_AT_specification.
10398 For example, with GCC 3.4, given the code
10399
10400 namespace N {
10401 void foo() {
10402 // Definition of N::foo.
10403 }
10404 }
10405
10406 then we'll have a tree of DIEs like this:
10407
10408 1: DW_TAG_compile_unit
10409 2: DW_TAG_namespace // N
10410 3: DW_TAG_subprogram // declaration of N::foo
10411 4: DW_TAG_subprogram // definition of N::foo
10412 DW_AT_specification // refers to die #3
10413
10414 Thus, when processing die #4, we have to pretend that we're in
10415 the context of its DW_AT_specification, namely the contex of die
10416 #3. */
10417 spec_cu = cu;
10418 spec_die = die_specification (die, &spec_cu);
10419 if (spec_die == NULL)
10420 parent = die->parent;
10421 else
63d06c5c 10422 {
0114d602
DJ
10423 parent = spec_die->parent;
10424 cu = spec_cu;
63d06c5c 10425 }
0114d602
DJ
10426
10427 if (parent == NULL)
10428 return "";
63d06c5c 10429 else
0114d602
DJ
10430 switch (parent->tag)
10431 {
63d06c5c 10432 case DW_TAG_namespace:
0114d602 10433 parent_type = read_type_die (parent, cu);
acebe513
UW
10434 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
10435 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
10436 Work around this problem here. */
10437 if (cu->language == language_cplus
10438 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
10439 return "";
0114d602
DJ
10440 /* We give a name to even anonymous namespaces. */
10441 return TYPE_TAG_NAME (parent_type);
63d06c5c 10442 case DW_TAG_class_type:
680b30c7 10443 case DW_TAG_interface_type:
63d06c5c 10444 case DW_TAG_structure_type:
0114d602 10445 case DW_TAG_union_type:
f55ee35c 10446 case DW_TAG_module:
0114d602
DJ
10447 parent_type = read_type_die (parent, cu);
10448 if (TYPE_TAG_NAME (parent_type) != NULL)
10449 return TYPE_TAG_NAME (parent_type);
10450 else
10451 /* An anonymous structure is only allowed non-static data
10452 members; no typedefs, no member functions, et cetera.
10453 So it does not need a prefix. */
10454 return "";
63d06c5c 10455 default:
8176b9b8 10456 return determine_prefix (parent, cu);
63d06c5c 10457 }
63d06c5c
DC
10458}
10459
987504bb
JJ
10460/* Return a newly-allocated string formed by concatenating PREFIX and
10461 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
10462 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
10463 perform an obconcat, otherwise allocate storage for the result. The CU argument
10464 is used to determine the language and hence, the appropriate separator. */
10465
f55ee35c 10466#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
10467
10468static char *
f55ee35c
JK
10469typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
10470 int physname, struct dwarf2_cu *cu)
63d06c5c 10471{
f55ee35c 10472 const char *lead = "";
5c315b68 10473 const char *sep;
63d06c5c 10474
987504bb
JJ
10475 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
10476 sep = "";
10477 else if (cu->language == language_java)
10478 sep = ".";
f55ee35c
JK
10479 else if (cu->language == language_fortran && physname)
10480 {
10481 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
10482 DW_AT_MIPS_linkage_name is preferred and used instead. */
10483
10484 lead = "__";
10485 sep = "_MOD_";
10486 }
987504bb
JJ
10487 else
10488 sep = "::";
63d06c5c 10489
6dd47d34
DE
10490 if (prefix == NULL)
10491 prefix = "";
10492 if (suffix == NULL)
10493 suffix = "";
10494
987504bb
JJ
10495 if (obs == NULL)
10496 {
10497 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 10498
f55ee35c
JK
10499 strcpy (retval, lead);
10500 strcat (retval, prefix);
6dd47d34
DE
10501 strcat (retval, sep);
10502 strcat (retval, suffix);
63d06c5c
DC
10503 return retval;
10504 }
987504bb
JJ
10505 else
10506 {
10507 /* We have an obstack. */
f55ee35c 10508 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 10509 }
63d06c5c
DC
10510}
10511
c906108c
SS
10512/* Return sibling of die, NULL if no sibling. */
10513
f9aca02d 10514static struct die_info *
fba45db2 10515sibling_die (struct die_info *die)
c906108c 10516{
639d11d3 10517 return die->sibling;
c906108c
SS
10518}
10519
71c25dea
TT
10520/* Get name of a die, return NULL if not found. */
10521
10522static char *
10523dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
10524 struct obstack *obstack)
10525{
10526 if (name && cu->language == language_cplus)
10527 {
10528 char *canon_name = cp_canonicalize_string (name);
10529
10530 if (canon_name != NULL)
10531 {
10532 if (strcmp (canon_name, name) != 0)
10533 name = obsavestring (canon_name, strlen (canon_name),
10534 obstack);
10535 xfree (canon_name);
10536 }
10537 }
10538
10539 return name;
c906108c
SS
10540}
10541
9219021c
DC
10542/* Get name of a die, return NULL if not found. */
10543
10544static char *
e142c38c 10545dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
10546{
10547 struct attribute *attr;
10548
e142c38c 10549 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
10550 if (!attr || !DW_STRING (attr))
10551 return NULL;
10552
10553 switch (die->tag)
10554 {
10555 case DW_TAG_compile_unit:
10556 /* Compilation units have a DW_AT_name that is a filename, not
10557 a source language identifier. */
10558 case DW_TAG_enumeration_type:
10559 case DW_TAG_enumerator:
10560 /* These tags always have simple identifiers already; no need
10561 to canonicalize them. */
10562 return DW_STRING (attr);
907af001 10563
418835cc
KS
10564 case DW_TAG_subprogram:
10565 /* Java constructors will all be named "<init>", so return
10566 the class name when we see this special case. */
10567 if (cu->language == language_java
10568 && DW_STRING (attr) != NULL
10569 && strcmp (DW_STRING (attr), "<init>") == 0)
10570 {
10571 struct dwarf2_cu *spec_cu = cu;
10572 struct die_info *spec_die;
10573
10574 /* GCJ will output '<init>' for Java constructor names.
10575 For this special case, return the name of the parent class. */
10576
10577 /* GCJ may output suprogram DIEs with AT_specification set.
10578 If so, use the name of the specified DIE. */
10579 spec_die = die_specification (die, &spec_cu);
10580 if (spec_die != NULL)
10581 return dwarf2_name (spec_die, spec_cu);
10582
10583 do
10584 {
10585 die = die->parent;
10586 if (die->tag == DW_TAG_class_type)
10587 return dwarf2_name (die, cu);
10588 }
10589 while (die->tag != DW_TAG_compile_unit);
10590 }
907af001
UW
10591 break;
10592
10593 case DW_TAG_class_type:
10594 case DW_TAG_interface_type:
10595 case DW_TAG_structure_type:
10596 case DW_TAG_union_type:
10597 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
10598 structures or unions. These were of the form "._%d" in GCC 4.1,
10599 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
10600 and GCC 4.4. We work around this problem by ignoring these. */
10601 if (strncmp (DW_STRING (attr), "._", 2) == 0
10602 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
10603 return NULL;
10604 break;
10605
71c25dea 10606 default:
907af001
UW
10607 break;
10608 }
10609
10610 if (!DW_STRING_IS_CANONICAL (attr))
10611 {
10612 DW_STRING (attr)
10613 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
10614 &cu->objfile->objfile_obstack);
10615 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 10616 }
907af001 10617 return DW_STRING (attr);
9219021c
DC
10618}
10619
10620/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
10621 is none. *EXT_CU is the CU containing DIE on input, and the CU
10622 containing the return value on output. */
9219021c
DC
10623
10624static struct die_info *
f2f0e013 10625dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
10626{
10627 struct attribute *attr;
9219021c 10628
f2f0e013 10629 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
10630 if (attr == NULL)
10631 return NULL;
10632
f2f0e013 10633 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
10634}
10635
c906108c
SS
10636/* Convert a DIE tag into its string name. */
10637
10638static char *
aa1ee363 10639dwarf_tag_name (unsigned tag)
c906108c
SS
10640{
10641 switch (tag)
10642 {
10643 case DW_TAG_padding:
10644 return "DW_TAG_padding";
10645 case DW_TAG_array_type:
10646 return "DW_TAG_array_type";
10647 case DW_TAG_class_type:
10648 return "DW_TAG_class_type";
10649 case DW_TAG_entry_point:
10650 return "DW_TAG_entry_point";
10651 case DW_TAG_enumeration_type:
10652 return "DW_TAG_enumeration_type";
10653 case DW_TAG_formal_parameter:
10654 return "DW_TAG_formal_parameter";
10655 case DW_TAG_imported_declaration:
10656 return "DW_TAG_imported_declaration";
10657 case DW_TAG_label:
10658 return "DW_TAG_label";
10659 case DW_TAG_lexical_block:
10660 return "DW_TAG_lexical_block";
10661 case DW_TAG_member:
10662 return "DW_TAG_member";
10663 case DW_TAG_pointer_type:
10664 return "DW_TAG_pointer_type";
10665 case DW_TAG_reference_type:
10666 return "DW_TAG_reference_type";
10667 case DW_TAG_compile_unit:
10668 return "DW_TAG_compile_unit";
10669 case DW_TAG_string_type:
10670 return "DW_TAG_string_type";
10671 case DW_TAG_structure_type:
10672 return "DW_TAG_structure_type";
10673 case DW_TAG_subroutine_type:
10674 return "DW_TAG_subroutine_type";
10675 case DW_TAG_typedef:
10676 return "DW_TAG_typedef";
10677 case DW_TAG_union_type:
10678 return "DW_TAG_union_type";
10679 case DW_TAG_unspecified_parameters:
10680 return "DW_TAG_unspecified_parameters";
10681 case DW_TAG_variant:
10682 return "DW_TAG_variant";
10683 case DW_TAG_common_block:
10684 return "DW_TAG_common_block";
10685 case DW_TAG_common_inclusion:
10686 return "DW_TAG_common_inclusion";
10687 case DW_TAG_inheritance:
10688 return "DW_TAG_inheritance";
10689 case DW_TAG_inlined_subroutine:
10690 return "DW_TAG_inlined_subroutine";
10691 case DW_TAG_module:
10692 return "DW_TAG_module";
10693 case DW_TAG_ptr_to_member_type:
10694 return "DW_TAG_ptr_to_member_type";
10695 case DW_TAG_set_type:
10696 return "DW_TAG_set_type";
10697 case DW_TAG_subrange_type:
10698 return "DW_TAG_subrange_type";
10699 case DW_TAG_with_stmt:
10700 return "DW_TAG_with_stmt";
10701 case DW_TAG_access_declaration:
10702 return "DW_TAG_access_declaration";
10703 case DW_TAG_base_type:
10704 return "DW_TAG_base_type";
10705 case DW_TAG_catch_block:
10706 return "DW_TAG_catch_block";
10707 case DW_TAG_const_type:
10708 return "DW_TAG_const_type";
10709 case DW_TAG_constant:
10710 return "DW_TAG_constant";
10711 case DW_TAG_enumerator:
10712 return "DW_TAG_enumerator";
10713 case DW_TAG_file_type:
10714 return "DW_TAG_file_type";
10715 case DW_TAG_friend:
10716 return "DW_TAG_friend";
10717 case DW_TAG_namelist:
10718 return "DW_TAG_namelist";
10719 case DW_TAG_namelist_item:
10720 return "DW_TAG_namelist_item";
10721 case DW_TAG_packed_type:
10722 return "DW_TAG_packed_type";
10723 case DW_TAG_subprogram:
10724 return "DW_TAG_subprogram";
10725 case DW_TAG_template_type_param:
10726 return "DW_TAG_template_type_param";
10727 case DW_TAG_template_value_param:
10728 return "DW_TAG_template_value_param";
10729 case DW_TAG_thrown_type:
10730 return "DW_TAG_thrown_type";
10731 case DW_TAG_try_block:
10732 return "DW_TAG_try_block";
10733 case DW_TAG_variant_part:
10734 return "DW_TAG_variant_part";
10735 case DW_TAG_variable:
10736 return "DW_TAG_variable";
10737 case DW_TAG_volatile_type:
10738 return "DW_TAG_volatile_type";
d9fa45fe
DC
10739 case DW_TAG_dwarf_procedure:
10740 return "DW_TAG_dwarf_procedure";
10741 case DW_TAG_restrict_type:
10742 return "DW_TAG_restrict_type";
10743 case DW_TAG_interface_type:
10744 return "DW_TAG_interface_type";
10745 case DW_TAG_namespace:
10746 return "DW_TAG_namespace";
10747 case DW_TAG_imported_module:
10748 return "DW_TAG_imported_module";
10749 case DW_TAG_unspecified_type:
10750 return "DW_TAG_unspecified_type";
10751 case DW_TAG_partial_unit:
10752 return "DW_TAG_partial_unit";
10753 case DW_TAG_imported_unit:
10754 return "DW_TAG_imported_unit";
b7619582
GF
10755 case DW_TAG_condition:
10756 return "DW_TAG_condition";
10757 case DW_TAG_shared_type:
10758 return "DW_TAG_shared_type";
348e048f
DE
10759 case DW_TAG_type_unit:
10760 return "DW_TAG_type_unit";
c906108c
SS
10761 case DW_TAG_MIPS_loop:
10762 return "DW_TAG_MIPS_loop";
b7619582
GF
10763 case DW_TAG_HP_array_descriptor:
10764 return "DW_TAG_HP_array_descriptor";
c906108c
SS
10765 case DW_TAG_format_label:
10766 return "DW_TAG_format_label";
10767 case DW_TAG_function_template:
10768 return "DW_TAG_function_template";
10769 case DW_TAG_class_template:
10770 return "DW_TAG_class_template";
b7619582
GF
10771 case DW_TAG_GNU_BINCL:
10772 return "DW_TAG_GNU_BINCL";
10773 case DW_TAG_GNU_EINCL:
10774 return "DW_TAG_GNU_EINCL";
10775 case DW_TAG_upc_shared_type:
10776 return "DW_TAG_upc_shared_type";
10777 case DW_TAG_upc_strict_type:
10778 return "DW_TAG_upc_strict_type";
10779 case DW_TAG_upc_relaxed_type:
10780 return "DW_TAG_upc_relaxed_type";
10781 case DW_TAG_PGI_kanji_type:
10782 return "DW_TAG_PGI_kanji_type";
10783 case DW_TAG_PGI_interface_block:
10784 return "DW_TAG_PGI_interface_block";
c906108c
SS
10785 default:
10786 return "DW_TAG_<unknown>";
10787 }
10788}
10789
10790/* Convert a DWARF attribute code into its string name. */
10791
10792static char *
aa1ee363 10793dwarf_attr_name (unsigned attr)
c906108c
SS
10794{
10795 switch (attr)
10796 {
10797 case DW_AT_sibling:
10798 return "DW_AT_sibling";
10799 case DW_AT_location:
10800 return "DW_AT_location";
10801 case DW_AT_name:
10802 return "DW_AT_name";
10803 case DW_AT_ordering:
10804 return "DW_AT_ordering";
10805 case DW_AT_subscr_data:
10806 return "DW_AT_subscr_data";
10807 case DW_AT_byte_size:
10808 return "DW_AT_byte_size";
10809 case DW_AT_bit_offset:
10810 return "DW_AT_bit_offset";
10811 case DW_AT_bit_size:
10812 return "DW_AT_bit_size";
10813 case DW_AT_element_list:
10814 return "DW_AT_element_list";
10815 case DW_AT_stmt_list:
10816 return "DW_AT_stmt_list";
10817 case DW_AT_low_pc:
10818 return "DW_AT_low_pc";
10819 case DW_AT_high_pc:
10820 return "DW_AT_high_pc";
10821 case DW_AT_language:
10822 return "DW_AT_language";
10823 case DW_AT_member:
10824 return "DW_AT_member";
10825 case DW_AT_discr:
10826 return "DW_AT_discr";
10827 case DW_AT_discr_value:
10828 return "DW_AT_discr_value";
10829 case DW_AT_visibility:
10830 return "DW_AT_visibility";
10831 case DW_AT_import:
10832 return "DW_AT_import";
10833 case DW_AT_string_length:
10834 return "DW_AT_string_length";
10835 case DW_AT_common_reference:
10836 return "DW_AT_common_reference";
10837 case DW_AT_comp_dir:
10838 return "DW_AT_comp_dir";
10839 case DW_AT_const_value:
10840 return "DW_AT_const_value";
10841 case DW_AT_containing_type:
10842 return "DW_AT_containing_type";
10843 case DW_AT_default_value:
10844 return "DW_AT_default_value";
10845 case DW_AT_inline:
10846 return "DW_AT_inline";
10847 case DW_AT_is_optional:
10848 return "DW_AT_is_optional";
10849 case DW_AT_lower_bound:
10850 return "DW_AT_lower_bound";
10851 case DW_AT_producer:
10852 return "DW_AT_producer";
10853 case DW_AT_prototyped:
10854 return "DW_AT_prototyped";
10855 case DW_AT_return_addr:
10856 return "DW_AT_return_addr";
10857 case DW_AT_start_scope:
10858 return "DW_AT_start_scope";
09fa0d7c
JK
10859 case DW_AT_bit_stride:
10860 return "DW_AT_bit_stride";
c906108c
SS
10861 case DW_AT_upper_bound:
10862 return "DW_AT_upper_bound";
10863 case DW_AT_abstract_origin:
10864 return "DW_AT_abstract_origin";
10865 case DW_AT_accessibility:
10866 return "DW_AT_accessibility";
10867 case DW_AT_address_class:
10868 return "DW_AT_address_class";
10869 case DW_AT_artificial:
10870 return "DW_AT_artificial";
10871 case DW_AT_base_types:
10872 return "DW_AT_base_types";
10873 case DW_AT_calling_convention:
10874 return "DW_AT_calling_convention";
10875 case DW_AT_count:
10876 return "DW_AT_count";
10877 case DW_AT_data_member_location:
10878 return "DW_AT_data_member_location";
10879 case DW_AT_decl_column:
10880 return "DW_AT_decl_column";
10881 case DW_AT_decl_file:
10882 return "DW_AT_decl_file";
10883 case DW_AT_decl_line:
10884 return "DW_AT_decl_line";
10885 case DW_AT_declaration:
10886 return "DW_AT_declaration";
10887 case DW_AT_discr_list:
10888 return "DW_AT_discr_list";
10889 case DW_AT_encoding:
10890 return "DW_AT_encoding";
10891 case DW_AT_external:
10892 return "DW_AT_external";
10893 case DW_AT_frame_base:
10894 return "DW_AT_frame_base";
10895 case DW_AT_friend:
10896 return "DW_AT_friend";
10897 case DW_AT_identifier_case:
10898 return "DW_AT_identifier_case";
10899 case DW_AT_macro_info:
10900 return "DW_AT_macro_info";
10901 case DW_AT_namelist_items:
10902 return "DW_AT_namelist_items";
10903 case DW_AT_priority:
10904 return "DW_AT_priority";
10905 case DW_AT_segment:
10906 return "DW_AT_segment";
10907 case DW_AT_specification:
10908 return "DW_AT_specification";
10909 case DW_AT_static_link:
10910 return "DW_AT_static_link";
10911 case DW_AT_type:
10912 return "DW_AT_type";
10913 case DW_AT_use_location:
10914 return "DW_AT_use_location";
10915 case DW_AT_variable_parameter:
10916 return "DW_AT_variable_parameter";
10917 case DW_AT_virtuality:
10918 return "DW_AT_virtuality";
10919 case DW_AT_vtable_elem_location:
10920 return "DW_AT_vtable_elem_location";
b7619582 10921 /* DWARF 3 values. */
d9fa45fe
DC
10922 case DW_AT_allocated:
10923 return "DW_AT_allocated";
10924 case DW_AT_associated:
10925 return "DW_AT_associated";
10926 case DW_AT_data_location:
10927 return "DW_AT_data_location";
09fa0d7c
JK
10928 case DW_AT_byte_stride:
10929 return "DW_AT_byte_stride";
d9fa45fe
DC
10930 case DW_AT_entry_pc:
10931 return "DW_AT_entry_pc";
10932 case DW_AT_use_UTF8:
10933 return "DW_AT_use_UTF8";
10934 case DW_AT_extension:
10935 return "DW_AT_extension";
10936 case DW_AT_ranges:
10937 return "DW_AT_ranges";
10938 case DW_AT_trampoline:
10939 return "DW_AT_trampoline";
10940 case DW_AT_call_column:
10941 return "DW_AT_call_column";
10942 case DW_AT_call_file:
10943 return "DW_AT_call_file";
10944 case DW_AT_call_line:
10945 return "DW_AT_call_line";
b7619582
GF
10946 case DW_AT_description:
10947 return "DW_AT_description";
10948 case DW_AT_binary_scale:
10949 return "DW_AT_binary_scale";
10950 case DW_AT_decimal_scale:
10951 return "DW_AT_decimal_scale";
10952 case DW_AT_small:
10953 return "DW_AT_small";
10954 case DW_AT_decimal_sign:
10955 return "DW_AT_decimal_sign";
10956 case DW_AT_digit_count:
10957 return "DW_AT_digit_count";
10958 case DW_AT_picture_string:
10959 return "DW_AT_picture_string";
10960 case DW_AT_mutable:
10961 return "DW_AT_mutable";
10962 case DW_AT_threads_scaled:
10963 return "DW_AT_threads_scaled";
10964 case DW_AT_explicit:
10965 return "DW_AT_explicit";
10966 case DW_AT_object_pointer:
10967 return "DW_AT_object_pointer";
10968 case DW_AT_endianity:
10969 return "DW_AT_endianity";
10970 case DW_AT_elemental:
10971 return "DW_AT_elemental";
10972 case DW_AT_pure:
10973 return "DW_AT_pure";
10974 case DW_AT_recursive:
10975 return "DW_AT_recursive";
348e048f
DE
10976 /* DWARF 4 values. */
10977 case DW_AT_signature:
10978 return "DW_AT_signature";
31ef98ae
TT
10979 case DW_AT_linkage_name:
10980 return "DW_AT_linkage_name";
b7619582 10981 /* SGI/MIPS extensions. */
c764a876 10982#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
10983 case DW_AT_MIPS_fde:
10984 return "DW_AT_MIPS_fde";
c764a876 10985#endif
c906108c
SS
10986 case DW_AT_MIPS_loop_begin:
10987 return "DW_AT_MIPS_loop_begin";
10988 case DW_AT_MIPS_tail_loop_begin:
10989 return "DW_AT_MIPS_tail_loop_begin";
10990 case DW_AT_MIPS_epilog_begin:
10991 return "DW_AT_MIPS_epilog_begin";
10992 case DW_AT_MIPS_loop_unroll_factor:
10993 return "DW_AT_MIPS_loop_unroll_factor";
10994 case DW_AT_MIPS_software_pipeline_depth:
10995 return "DW_AT_MIPS_software_pipeline_depth";
10996 case DW_AT_MIPS_linkage_name:
10997 return "DW_AT_MIPS_linkage_name";
b7619582
GF
10998 case DW_AT_MIPS_stride:
10999 return "DW_AT_MIPS_stride";
11000 case DW_AT_MIPS_abstract_name:
11001 return "DW_AT_MIPS_abstract_name";
11002 case DW_AT_MIPS_clone_origin:
11003 return "DW_AT_MIPS_clone_origin";
11004 case DW_AT_MIPS_has_inlines:
11005 return "DW_AT_MIPS_has_inlines";
b7619582 11006 /* HP extensions. */
c764a876 11007#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11008 case DW_AT_HP_block_index:
11009 return "DW_AT_HP_block_index";
c764a876 11010#endif
b7619582
GF
11011 case DW_AT_HP_unmodifiable:
11012 return "DW_AT_HP_unmodifiable";
11013 case DW_AT_HP_actuals_stmt_list:
11014 return "DW_AT_HP_actuals_stmt_list";
11015 case DW_AT_HP_proc_per_section:
11016 return "DW_AT_HP_proc_per_section";
11017 case DW_AT_HP_raw_data_ptr:
11018 return "DW_AT_HP_raw_data_ptr";
11019 case DW_AT_HP_pass_by_reference:
11020 return "DW_AT_HP_pass_by_reference";
11021 case DW_AT_HP_opt_level:
11022 return "DW_AT_HP_opt_level";
11023 case DW_AT_HP_prof_version_id:
11024 return "DW_AT_HP_prof_version_id";
11025 case DW_AT_HP_opt_flags:
11026 return "DW_AT_HP_opt_flags";
11027 case DW_AT_HP_cold_region_low_pc:
11028 return "DW_AT_HP_cold_region_low_pc";
11029 case DW_AT_HP_cold_region_high_pc:
11030 return "DW_AT_HP_cold_region_high_pc";
11031 case DW_AT_HP_all_variables_modifiable:
11032 return "DW_AT_HP_all_variables_modifiable";
11033 case DW_AT_HP_linkage_name:
11034 return "DW_AT_HP_linkage_name";
11035 case DW_AT_HP_prof_flags:
11036 return "DW_AT_HP_prof_flags";
11037 /* GNU extensions. */
c906108c
SS
11038 case DW_AT_sf_names:
11039 return "DW_AT_sf_names";
11040 case DW_AT_src_info:
11041 return "DW_AT_src_info";
11042 case DW_AT_mac_info:
11043 return "DW_AT_mac_info";
11044 case DW_AT_src_coords:
11045 return "DW_AT_src_coords";
11046 case DW_AT_body_begin:
11047 return "DW_AT_body_begin";
11048 case DW_AT_body_end:
11049 return "DW_AT_body_end";
f5f8a009
EZ
11050 case DW_AT_GNU_vector:
11051 return "DW_AT_GNU_vector";
b7619582
GF
11052 /* VMS extensions. */
11053 case DW_AT_VMS_rtnbeg_pd_address:
11054 return "DW_AT_VMS_rtnbeg_pd_address";
11055 /* UPC extension. */
11056 case DW_AT_upc_threads_scaled:
11057 return "DW_AT_upc_threads_scaled";
11058 /* PGI (STMicroelectronics) extensions. */
11059 case DW_AT_PGI_lbase:
11060 return "DW_AT_PGI_lbase";
11061 case DW_AT_PGI_soffset:
11062 return "DW_AT_PGI_soffset";
11063 case DW_AT_PGI_lstride:
11064 return "DW_AT_PGI_lstride";
c906108c
SS
11065 default:
11066 return "DW_AT_<unknown>";
11067 }
11068}
11069
11070/* Convert a DWARF value form code into its string name. */
11071
11072static char *
aa1ee363 11073dwarf_form_name (unsigned form)
c906108c
SS
11074{
11075 switch (form)
11076 {
11077 case DW_FORM_addr:
11078 return "DW_FORM_addr";
11079 case DW_FORM_block2:
11080 return "DW_FORM_block2";
11081 case DW_FORM_block4:
11082 return "DW_FORM_block4";
11083 case DW_FORM_data2:
11084 return "DW_FORM_data2";
11085 case DW_FORM_data4:
11086 return "DW_FORM_data4";
11087 case DW_FORM_data8:
11088 return "DW_FORM_data8";
11089 case DW_FORM_string:
11090 return "DW_FORM_string";
11091 case DW_FORM_block:
11092 return "DW_FORM_block";
11093 case DW_FORM_block1:
11094 return "DW_FORM_block1";
11095 case DW_FORM_data1:
11096 return "DW_FORM_data1";
11097 case DW_FORM_flag:
11098 return "DW_FORM_flag";
11099 case DW_FORM_sdata:
11100 return "DW_FORM_sdata";
11101 case DW_FORM_strp:
11102 return "DW_FORM_strp";
11103 case DW_FORM_udata:
11104 return "DW_FORM_udata";
11105 case DW_FORM_ref_addr:
11106 return "DW_FORM_ref_addr";
11107 case DW_FORM_ref1:
11108 return "DW_FORM_ref1";
11109 case DW_FORM_ref2:
11110 return "DW_FORM_ref2";
11111 case DW_FORM_ref4:
11112 return "DW_FORM_ref4";
11113 case DW_FORM_ref8:
11114 return "DW_FORM_ref8";
11115 case DW_FORM_ref_udata:
11116 return "DW_FORM_ref_udata";
11117 case DW_FORM_indirect:
11118 return "DW_FORM_indirect";
348e048f
DE
11119 case DW_FORM_sec_offset:
11120 return "DW_FORM_sec_offset";
11121 case DW_FORM_exprloc:
11122 return "DW_FORM_exprloc";
11123 case DW_FORM_flag_present:
11124 return "DW_FORM_flag_present";
11125 case DW_FORM_sig8:
11126 return "DW_FORM_sig8";
c906108c
SS
11127 default:
11128 return "DW_FORM_<unknown>";
11129 }
11130}
11131
11132/* Convert a DWARF stack opcode into its string name. */
11133
9eae7c52
TT
11134const char *
11135dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
11136{
11137 switch (op)
11138 {
11139 case DW_OP_addr:
11140 return "DW_OP_addr";
11141 case DW_OP_deref:
11142 return "DW_OP_deref";
11143 case DW_OP_const1u:
11144 return "DW_OP_const1u";
11145 case DW_OP_const1s:
11146 return "DW_OP_const1s";
11147 case DW_OP_const2u:
11148 return "DW_OP_const2u";
11149 case DW_OP_const2s:
11150 return "DW_OP_const2s";
11151 case DW_OP_const4u:
11152 return "DW_OP_const4u";
11153 case DW_OP_const4s:
11154 return "DW_OP_const4s";
11155 case DW_OP_const8u:
11156 return "DW_OP_const8u";
11157 case DW_OP_const8s:
11158 return "DW_OP_const8s";
11159 case DW_OP_constu:
11160 return "DW_OP_constu";
11161 case DW_OP_consts:
11162 return "DW_OP_consts";
11163 case DW_OP_dup:
11164 return "DW_OP_dup";
11165 case DW_OP_drop:
11166 return "DW_OP_drop";
11167 case DW_OP_over:
11168 return "DW_OP_over";
11169 case DW_OP_pick:
11170 return "DW_OP_pick";
11171 case DW_OP_swap:
11172 return "DW_OP_swap";
11173 case DW_OP_rot:
11174 return "DW_OP_rot";
11175 case DW_OP_xderef:
11176 return "DW_OP_xderef";
11177 case DW_OP_abs:
11178 return "DW_OP_abs";
11179 case DW_OP_and:
11180 return "DW_OP_and";
11181 case DW_OP_div:
11182 return "DW_OP_div";
11183 case DW_OP_minus:
11184 return "DW_OP_minus";
11185 case DW_OP_mod:
11186 return "DW_OP_mod";
11187 case DW_OP_mul:
11188 return "DW_OP_mul";
11189 case DW_OP_neg:
11190 return "DW_OP_neg";
11191 case DW_OP_not:
11192 return "DW_OP_not";
11193 case DW_OP_or:
11194 return "DW_OP_or";
11195 case DW_OP_plus:
11196 return "DW_OP_plus";
11197 case DW_OP_plus_uconst:
11198 return "DW_OP_plus_uconst";
11199 case DW_OP_shl:
11200 return "DW_OP_shl";
11201 case DW_OP_shr:
11202 return "DW_OP_shr";
11203 case DW_OP_shra:
11204 return "DW_OP_shra";
11205 case DW_OP_xor:
11206 return "DW_OP_xor";
11207 case DW_OP_bra:
11208 return "DW_OP_bra";
11209 case DW_OP_eq:
11210 return "DW_OP_eq";
11211 case DW_OP_ge:
11212 return "DW_OP_ge";
11213 case DW_OP_gt:
11214 return "DW_OP_gt";
11215 case DW_OP_le:
11216 return "DW_OP_le";
11217 case DW_OP_lt:
11218 return "DW_OP_lt";
11219 case DW_OP_ne:
11220 return "DW_OP_ne";
11221 case DW_OP_skip:
11222 return "DW_OP_skip";
11223 case DW_OP_lit0:
11224 return "DW_OP_lit0";
11225 case DW_OP_lit1:
11226 return "DW_OP_lit1";
11227 case DW_OP_lit2:
11228 return "DW_OP_lit2";
11229 case DW_OP_lit3:
11230 return "DW_OP_lit3";
11231 case DW_OP_lit4:
11232 return "DW_OP_lit4";
11233 case DW_OP_lit5:
11234 return "DW_OP_lit5";
11235 case DW_OP_lit6:
11236 return "DW_OP_lit6";
11237 case DW_OP_lit7:
11238 return "DW_OP_lit7";
11239 case DW_OP_lit8:
11240 return "DW_OP_lit8";
11241 case DW_OP_lit9:
11242 return "DW_OP_lit9";
11243 case DW_OP_lit10:
11244 return "DW_OP_lit10";
11245 case DW_OP_lit11:
11246 return "DW_OP_lit11";
11247 case DW_OP_lit12:
11248 return "DW_OP_lit12";
11249 case DW_OP_lit13:
11250 return "DW_OP_lit13";
11251 case DW_OP_lit14:
11252 return "DW_OP_lit14";
11253 case DW_OP_lit15:
11254 return "DW_OP_lit15";
11255 case DW_OP_lit16:
11256 return "DW_OP_lit16";
11257 case DW_OP_lit17:
11258 return "DW_OP_lit17";
11259 case DW_OP_lit18:
11260 return "DW_OP_lit18";
11261 case DW_OP_lit19:
11262 return "DW_OP_lit19";
11263 case DW_OP_lit20:
11264 return "DW_OP_lit20";
11265 case DW_OP_lit21:
11266 return "DW_OP_lit21";
11267 case DW_OP_lit22:
11268 return "DW_OP_lit22";
11269 case DW_OP_lit23:
11270 return "DW_OP_lit23";
11271 case DW_OP_lit24:
11272 return "DW_OP_lit24";
11273 case DW_OP_lit25:
11274 return "DW_OP_lit25";
11275 case DW_OP_lit26:
11276 return "DW_OP_lit26";
11277 case DW_OP_lit27:
11278 return "DW_OP_lit27";
11279 case DW_OP_lit28:
11280 return "DW_OP_lit28";
11281 case DW_OP_lit29:
11282 return "DW_OP_lit29";
11283 case DW_OP_lit30:
11284 return "DW_OP_lit30";
11285 case DW_OP_lit31:
11286 return "DW_OP_lit31";
11287 case DW_OP_reg0:
11288 return "DW_OP_reg0";
11289 case DW_OP_reg1:
11290 return "DW_OP_reg1";
11291 case DW_OP_reg2:
11292 return "DW_OP_reg2";
11293 case DW_OP_reg3:
11294 return "DW_OP_reg3";
11295 case DW_OP_reg4:
11296 return "DW_OP_reg4";
11297 case DW_OP_reg5:
11298 return "DW_OP_reg5";
11299 case DW_OP_reg6:
11300 return "DW_OP_reg6";
11301 case DW_OP_reg7:
11302 return "DW_OP_reg7";
11303 case DW_OP_reg8:
11304 return "DW_OP_reg8";
11305 case DW_OP_reg9:
11306 return "DW_OP_reg9";
11307 case DW_OP_reg10:
11308 return "DW_OP_reg10";
11309 case DW_OP_reg11:
11310 return "DW_OP_reg11";
11311 case DW_OP_reg12:
11312 return "DW_OP_reg12";
11313 case DW_OP_reg13:
11314 return "DW_OP_reg13";
11315 case DW_OP_reg14:
11316 return "DW_OP_reg14";
11317 case DW_OP_reg15:
11318 return "DW_OP_reg15";
11319 case DW_OP_reg16:
11320 return "DW_OP_reg16";
11321 case DW_OP_reg17:
11322 return "DW_OP_reg17";
11323 case DW_OP_reg18:
11324 return "DW_OP_reg18";
11325 case DW_OP_reg19:
11326 return "DW_OP_reg19";
11327 case DW_OP_reg20:
11328 return "DW_OP_reg20";
11329 case DW_OP_reg21:
11330 return "DW_OP_reg21";
11331 case DW_OP_reg22:
11332 return "DW_OP_reg22";
11333 case DW_OP_reg23:
11334 return "DW_OP_reg23";
11335 case DW_OP_reg24:
11336 return "DW_OP_reg24";
11337 case DW_OP_reg25:
11338 return "DW_OP_reg25";
11339 case DW_OP_reg26:
11340 return "DW_OP_reg26";
11341 case DW_OP_reg27:
11342 return "DW_OP_reg27";
11343 case DW_OP_reg28:
11344 return "DW_OP_reg28";
11345 case DW_OP_reg29:
11346 return "DW_OP_reg29";
11347 case DW_OP_reg30:
11348 return "DW_OP_reg30";
11349 case DW_OP_reg31:
11350 return "DW_OP_reg31";
11351 case DW_OP_breg0:
11352 return "DW_OP_breg0";
11353 case DW_OP_breg1:
11354 return "DW_OP_breg1";
11355 case DW_OP_breg2:
11356 return "DW_OP_breg2";
11357 case DW_OP_breg3:
11358 return "DW_OP_breg3";
11359 case DW_OP_breg4:
11360 return "DW_OP_breg4";
11361 case DW_OP_breg5:
11362 return "DW_OP_breg5";
11363 case DW_OP_breg6:
11364 return "DW_OP_breg6";
11365 case DW_OP_breg7:
11366 return "DW_OP_breg7";
11367 case DW_OP_breg8:
11368 return "DW_OP_breg8";
11369 case DW_OP_breg9:
11370 return "DW_OP_breg9";
11371 case DW_OP_breg10:
11372 return "DW_OP_breg10";
11373 case DW_OP_breg11:
11374 return "DW_OP_breg11";
11375 case DW_OP_breg12:
11376 return "DW_OP_breg12";
11377 case DW_OP_breg13:
11378 return "DW_OP_breg13";
11379 case DW_OP_breg14:
11380 return "DW_OP_breg14";
11381 case DW_OP_breg15:
11382 return "DW_OP_breg15";
11383 case DW_OP_breg16:
11384 return "DW_OP_breg16";
11385 case DW_OP_breg17:
11386 return "DW_OP_breg17";
11387 case DW_OP_breg18:
11388 return "DW_OP_breg18";
11389 case DW_OP_breg19:
11390 return "DW_OP_breg19";
11391 case DW_OP_breg20:
11392 return "DW_OP_breg20";
11393 case DW_OP_breg21:
11394 return "DW_OP_breg21";
11395 case DW_OP_breg22:
11396 return "DW_OP_breg22";
11397 case DW_OP_breg23:
11398 return "DW_OP_breg23";
11399 case DW_OP_breg24:
11400 return "DW_OP_breg24";
11401 case DW_OP_breg25:
11402 return "DW_OP_breg25";
11403 case DW_OP_breg26:
11404 return "DW_OP_breg26";
11405 case DW_OP_breg27:
11406 return "DW_OP_breg27";
11407 case DW_OP_breg28:
11408 return "DW_OP_breg28";
11409 case DW_OP_breg29:
11410 return "DW_OP_breg29";
11411 case DW_OP_breg30:
11412 return "DW_OP_breg30";
11413 case DW_OP_breg31:
11414 return "DW_OP_breg31";
11415 case DW_OP_regx:
11416 return "DW_OP_regx";
11417 case DW_OP_fbreg:
11418 return "DW_OP_fbreg";
11419 case DW_OP_bregx:
11420 return "DW_OP_bregx";
11421 case DW_OP_piece:
11422 return "DW_OP_piece";
11423 case DW_OP_deref_size:
11424 return "DW_OP_deref_size";
11425 case DW_OP_xderef_size:
11426 return "DW_OP_xderef_size";
11427 case DW_OP_nop:
11428 return "DW_OP_nop";
b7619582 11429 /* DWARF 3 extensions. */
ed348acc
EZ
11430 case DW_OP_push_object_address:
11431 return "DW_OP_push_object_address";
11432 case DW_OP_call2:
11433 return "DW_OP_call2";
11434 case DW_OP_call4:
11435 return "DW_OP_call4";
11436 case DW_OP_call_ref:
11437 return "DW_OP_call_ref";
b7619582
GF
11438 case DW_OP_form_tls_address:
11439 return "DW_OP_form_tls_address";
11440 case DW_OP_call_frame_cfa:
11441 return "DW_OP_call_frame_cfa";
11442 case DW_OP_bit_piece:
11443 return "DW_OP_bit_piece";
9eae7c52
TT
11444 /* DWARF 4 extensions. */
11445 case DW_OP_implicit_value:
11446 return "DW_OP_implicit_value";
11447 case DW_OP_stack_value:
11448 return "DW_OP_stack_value";
11449 /* GNU extensions. */
ed348acc
EZ
11450 case DW_OP_GNU_push_tls_address:
11451 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
11452 case DW_OP_GNU_uninit:
11453 return "DW_OP_GNU_uninit";
c906108c 11454 default:
9eae7c52 11455 return def ? "OP_<unknown>" : NULL;
c906108c
SS
11456 }
11457}
11458
11459static char *
fba45db2 11460dwarf_bool_name (unsigned mybool)
c906108c
SS
11461{
11462 if (mybool)
11463 return "TRUE";
11464 else
11465 return "FALSE";
11466}
11467
11468/* Convert a DWARF type code into its string name. */
11469
11470static char *
aa1ee363 11471dwarf_type_encoding_name (unsigned enc)
c906108c
SS
11472{
11473 switch (enc)
11474 {
b7619582
GF
11475 case DW_ATE_void:
11476 return "DW_ATE_void";
c906108c
SS
11477 case DW_ATE_address:
11478 return "DW_ATE_address";
11479 case DW_ATE_boolean:
11480 return "DW_ATE_boolean";
11481 case DW_ATE_complex_float:
11482 return "DW_ATE_complex_float";
11483 case DW_ATE_float:
11484 return "DW_ATE_float";
11485 case DW_ATE_signed:
11486 return "DW_ATE_signed";
11487 case DW_ATE_signed_char:
11488 return "DW_ATE_signed_char";
11489 case DW_ATE_unsigned:
11490 return "DW_ATE_unsigned";
11491 case DW_ATE_unsigned_char:
11492 return "DW_ATE_unsigned_char";
b7619582 11493 /* DWARF 3. */
d9fa45fe
DC
11494 case DW_ATE_imaginary_float:
11495 return "DW_ATE_imaginary_float";
b7619582
GF
11496 case DW_ATE_packed_decimal:
11497 return "DW_ATE_packed_decimal";
11498 case DW_ATE_numeric_string:
11499 return "DW_ATE_numeric_string";
11500 case DW_ATE_edited:
11501 return "DW_ATE_edited";
11502 case DW_ATE_signed_fixed:
11503 return "DW_ATE_signed_fixed";
11504 case DW_ATE_unsigned_fixed:
11505 return "DW_ATE_unsigned_fixed";
11506 case DW_ATE_decimal_float:
11507 return "DW_ATE_decimal_float";
75079b2b
TT
11508 /* DWARF 4. */
11509 case DW_ATE_UTF:
11510 return "DW_ATE_UTF";
b7619582
GF
11511 /* HP extensions. */
11512 case DW_ATE_HP_float80:
11513 return "DW_ATE_HP_float80";
11514 case DW_ATE_HP_complex_float80:
11515 return "DW_ATE_HP_complex_float80";
11516 case DW_ATE_HP_float128:
11517 return "DW_ATE_HP_float128";
11518 case DW_ATE_HP_complex_float128:
11519 return "DW_ATE_HP_complex_float128";
11520 case DW_ATE_HP_floathpintel:
11521 return "DW_ATE_HP_floathpintel";
11522 case DW_ATE_HP_imaginary_float80:
11523 return "DW_ATE_HP_imaginary_float80";
11524 case DW_ATE_HP_imaginary_float128:
11525 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
11526 default:
11527 return "DW_ATE_<unknown>";
11528 }
11529}
11530
11531/* Convert a DWARF call frame info operation to its string name. */
11532
11533#if 0
11534static char *
aa1ee363 11535dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
11536{
11537 switch (cfi_opc)
11538 {
11539 case DW_CFA_advance_loc:
11540 return "DW_CFA_advance_loc";
11541 case DW_CFA_offset:
11542 return "DW_CFA_offset";
11543 case DW_CFA_restore:
11544 return "DW_CFA_restore";
11545 case DW_CFA_nop:
11546 return "DW_CFA_nop";
11547 case DW_CFA_set_loc:
11548 return "DW_CFA_set_loc";
11549 case DW_CFA_advance_loc1:
11550 return "DW_CFA_advance_loc1";
11551 case DW_CFA_advance_loc2:
11552 return "DW_CFA_advance_loc2";
11553 case DW_CFA_advance_loc4:
11554 return "DW_CFA_advance_loc4";
11555 case DW_CFA_offset_extended:
11556 return "DW_CFA_offset_extended";
11557 case DW_CFA_restore_extended:
11558 return "DW_CFA_restore_extended";
11559 case DW_CFA_undefined:
11560 return "DW_CFA_undefined";
11561 case DW_CFA_same_value:
11562 return "DW_CFA_same_value";
11563 case DW_CFA_register:
11564 return "DW_CFA_register";
11565 case DW_CFA_remember_state:
11566 return "DW_CFA_remember_state";
11567 case DW_CFA_restore_state:
11568 return "DW_CFA_restore_state";
11569 case DW_CFA_def_cfa:
11570 return "DW_CFA_def_cfa";
11571 case DW_CFA_def_cfa_register:
11572 return "DW_CFA_def_cfa_register";
11573 case DW_CFA_def_cfa_offset:
11574 return "DW_CFA_def_cfa_offset";
b7619582 11575 /* DWARF 3. */
985cb1a3
JM
11576 case DW_CFA_def_cfa_expression:
11577 return "DW_CFA_def_cfa_expression";
11578 case DW_CFA_expression:
11579 return "DW_CFA_expression";
11580 case DW_CFA_offset_extended_sf:
11581 return "DW_CFA_offset_extended_sf";
11582 case DW_CFA_def_cfa_sf:
11583 return "DW_CFA_def_cfa_sf";
11584 case DW_CFA_def_cfa_offset_sf:
11585 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
11586 case DW_CFA_val_offset:
11587 return "DW_CFA_val_offset";
11588 case DW_CFA_val_offset_sf:
11589 return "DW_CFA_val_offset_sf";
11590 case DW_CFA_val_expression:
11591 return "DW_CFA_val_expression";
11592 /* SGI/MIPS specific. */
c906108c
SS
11593 case DW_CFA_MIPS_advance_loc8:
11594 return "DW_CFA_MIPS_advance_loc8";
b7619582 11595 /* GNU extensions. */
985cb1a3
JM
11596 case DW_CFA_GNU_window_save:
11597 return "DW_CFA_GNU_window_save";
11598 case DW_CFA_GNU_args_size:
11599 return "DW_CFA_GNU_args_size";
11600 case DW_CFA_GNU_negative_offset_extended:
11601 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
11602 default:
11603 return "DW_CFA_<unknown>";
11604 }
11605}
11606#endif
11607
f9aca02d 11608static void
d97bc12b 11609dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
11610{
11611 unsigned int i;
11612
d97bc12b
DE
11613 print_spaces (indent, f);
11614 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 11615 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
11616
11617 if (die->parent != NULL)
11618 {
11619 print_spaces (indent, f);
11620 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
11621 die->parent->offset);
11622 }
11623
11624 print_spaces (indent, f);
11625 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 11626 dwarf_bool_name (die->child != NULL));
c906108c 11627
d97bc12b
DE
11628 print_spaces (indent, f);
11629 fprintf_unfiltered (f, " attributes:\n");
11630
c906108c
SS
11631 for (i = 0; i < die->num_attrs; ++i)
11632 {
d97bc12b
DE
11633 print_spaces (indent, f);
11634 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
11635 dwarf_attr_name (die->attrs[i].name),
11636 dwarf_form_name (die->attrs[i].form));
d97bc12b 11637
c906108c
SS
11638 switch (die->attrs[i].form)
11639 {
11640 case DW_FORM_ref_addr:
11641 case DW_FORM_addr:
d97bc12b 11642 fprintf_unfiltered (f, "address: ");
5af949e3 11643 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
11644 break;
11645 case DW_FORM_block2:
11646 case DW_FORM_block4:
11647 case DW_FORM_block:
11648 case DW_FORM_block1:
d97bc12b 11649 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 11650 break;
2dc7f7b3
TT
11651 case DW_FORM_exprloc:
11652 fprintf_unfiltered (f, "expression: size %u",
11653 DW_BLOCK (&die->attrs[i])->size);
11654 break;
10b3939b
DJ
11655 case DW_FORM_ref1:
11656 case DW_FORM_ref2:
11657 case DW_FORM_ref4:
d97bc12b 11658 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
11659 (long) (DW_ADDR (&die->attrs[i])));
11660 break;
c906108c
SS
11661 case DW_FORM_data1:
11662 case DW_FORM_data2:
11663 case DW_FORM_data4:
ce5d95e1 11664 case DW_FORM_data8:
c906108c
SS
11665 case DW_FORM_udata:
11666 case DW_FORM_sdata:
43bbcdc2
PH
11667 fprintf_unfiltered (f, "constant: %s",
11668 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 11669 break;
2dc7f7b3
TT
11670 case DW_FORM_sec_offset:
11671 fprintf_unfiltered (f, "section offset: %s",
11672 pulongest (DW_UNSND (&die->attrs[i])));
11673 break;
348e048f
DE
11674 case DW_FORM_sig8:
11675 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
11676 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
11677 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
11678 else
11679 fprintf_unfiltered (f, "signatured type, offset: unknown");
11680 break;
c906108c 11681 case DW_FORM_string:
4bdf3d34 11682 case DW_FORM_strp:
8285870a 11683 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 11684 DW_STRING (&die->attrs[i])
8285870a
JK
11685 ? DW_STRING (&die->attrs[i]) : "",
11686 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
11687 break;
11688 case DW_FORM_flag:
11689 if (DW_UNSND (&die->attrs[i]))
d97bc12b 11690 fprintf_unfiltered (f, "flag: TRUE");
c906108c 11691 else
d97bc12b 11692 fprintf_unfiltered (f, "flag: FALSE");
c906108c 11693 break;
2dc7f7b3
TT
11694 case DW_FORM_flag_present:
11695 fprintf_unfiltered (f, "flag: TRUE");
11696 break;
a8329558
KW
11697 case DW_FORM_indirect:
11698 /* the reader will have reduced the indirect form to
11699 the "base form" so this form should not occur */
d97bc12b 11700 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 11701 break;
c906108c 11702 default:
d97bc12b 11703 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 11704 die->attrs[i].form);
d97bc12b 11705 break;
c906108c 11706 }
d97bc12b 11707 fprintf_unfiltered (f, "\n");
c906108c
SS
11708 }
11709}
11710
f9aca02d 11711static void
d97bc12b 11712dump_die_for_error (struct die_info *die)
c906108c 11713{
d97bc12b
DE
11714 dump_die_shallow (gdb_stderr, 0, die);
11715}
11716
11717static void
11718dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
11719{
11720 int indent = level * 4;
11721
11722 gdb_assert (die != NULL);
11723
11724 if (level >= max_level)
11725 return;
11726
11727 dump_die_shallow (f, indent, die);
11728
11729 if (die->child != NULL)
c906108c 11730 {
d97bc12b
DE
11731 print_spaces (indent, f);
11732 fprintf_unfiltered (f, " Children:");
11733 if (level + 1 < max_level)
11734 {
11735 fprintf_unfiltered (f, "\n");
11736 dump_die_1 (f, level + 1, max_level, die->child);
11737 }
11738 else
11739 {
11740 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
11741 }
11742 }
11743
11744 if (die->sibling != NULL && level > 0)
11745 {
11746 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
11747 }
11748}
11749
d97bc12b
DE
11750/* This is called from the pdie macro in gdbinit.in.
11751 It's not static so gcc will keep a copy callable from gdb. */
11752
11753void
11754dump_die (struct die_info *die, int max_level)
11755{
11756 dump_die_1 (gdb_stdlog, 0, max_level, die);
11757}
11758
f9aca02d 11759static void
51545339 11760store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11761{
51545339 11762 void **slot;
c906108c 11763
51545339
DJ
11764 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
11765
11766 *slot = die;
c906108c
SS
11767}
11768
93311388
DE
11769static int
11770is_ref_attr (struct attribute *attr)
c906108c 11771{
c906108c
SS
11772 switch (attr->form)
11773 {
11774 case DW_FORM_ref_addr:
c906108c
SS
11775 case DW_FORM_ref1:
11776 case DW_FORM_ref2:
11777 case DW_FORM_ref4:
613e1657 11778 case DW_FORM_ref8:
c906108c 11779 case DW_FORM_ref_udata:
93311388 11780 return 1;
c906108c 11781 default:
93311388 11782 return 0;
c906108c 11783 }
93311388
DE
11784}
11785
11786static unsigned int
11787dwarf2_get_ref_die_offset (struct attribute *attr)
11788{
11789 if (is_ref_attr (attr))
11790 return DW_ADDR (attr);
11791
11792 complaint (&symfile_complaints,
11793 _("unsupported die ref attribute form: '%s'"),
11794 dwarf_form_name (attr->form));
11795 return 0;
c906108c
SS
11796}
11797
43bbcdc2
PH
11798/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
11799 * the value held by the attribute is not constant. */
a02abb62 11800
43bbcdc2 11801static LONGEST
a02abb62
JB
11802dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
11803{
11804 if (attr->form == DW_FORM_sdata)
11805 return DW_SND (attr);
11806 else if (attr->form == DW_FORM_udata
11807 || attr->form == DW_FORM_data1
11808 || attr->form == DW_FORM_data2
11809 || attr->form == DW_FORM_data4
11810 || attr->form == DW_FORM_data8)
11811 return DW_UNSND (attr);
11812 else
11813 {
e2e0b3e5 11814 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
11815 dwarf_form_name (attr->form));
11816 return default_value;
11817 }
11818}
11819
03dd20cc 11820/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
11821 unit and add it to our queue.
11822 The result is non-zero if PER_CU was queued, otherwise the result is zero
11823 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 11824
348e048f 11825static int
03dd20cc
DJ
11826maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
11827 struct dwarf2_per_cu_data *per_cu)
11828{
11829 /* Mark the dependence relation so that we don't flush PER_CU
11830 too early. */
11831 dwarf2_add_dependence (this_cu, per_cu);
11832
11833 /* If it's already on the queue, we have nothing to do. */
11834 if (per_cu->queued)
348e048f 11835 return 0;
03dd20cc
DJ
11836
11837 /* If the compilation unit is already loaded, just mark it as
11838 used. */
11839 if (per_cu->cu != NULL)
11840 {
11841 per_cu->cu->last_used = 0;
348e048f 11842 return 0;
03dd20cc
DJ
11843 }
11844
11845 /* Add it to the queue. */
11846 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
11847
11848 return 1;
11849}
11850
11851/* Follow reference or signature attribute ATTR of SRC_DIE.
11852 On entry *REF_CU is the CU of SRC_DIE.
11853 On exit *REF_CU is the CU of the result. */
11854
11855static struct die_info *
11856follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
11857 struct dwarf2_cu **ref_cu)
11858{
11859 struct die_info *die;
11860
11861 if (is_ref_attr (attr))
11862 die = follow_die_ref (src_die, attr, ref_cu);
11863 else if (attr->form == DW_FORM_sig8)
11864 die = follow_die_sig (src_die, attr, ref_cu);
11865 else
11866 {
11867 dump_die_for_error (src_die);
11868 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
11869 (*ref_cu)->objfile->name);
11870 }
11871
11872 return die;
03dd20cc
DJ
11873}
11874
5c631832
JK
11875/* Follow reference OFFSET.
11876 On entry *REF_CU is the CU of source DIE referencing OFFSET.
f504f079
DE
11877 On exit *REF_CU is the CU of the result. */
11878
f9aca02d 11879static struct die_info *
5c631832 11880follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 11881{
10b3939b 11882 struct die_info temp_die;
f2f0e013 11883 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 11884
348e048f
DE
11885 gdb_assert (cu->per_cu != NULL);
11886
348e048f
DE
11887 if (cu->per_cu->from_debug_types)
11888 {
11889 /* .debug_types CUs cannot reference anything outside their CU.
11890 If they need to, they have to reference a signatured type via
11891 DW_FORM_sig8. */
11892 if (! offset_in_cu_p (&cu->header, offset))
5c631832 11893 return NULL;
348e048f
DE
11894 target_cu = cu;
11895 }
11896 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
11897 {
11898 struct dwarf2_per_cu_data *per_cu;
9a619af0 11899
45452591 11900 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
11901
11902 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
11903 if (maybe_queue_comp_unit (cu, per_cu))
11904 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 11905
10b3939b
DJ
11906 target_cu = per_cu->cu;
11907 }
11908 else
11909 target_cu = cu;
c906108c 11910
f2f0e013 11911 *ref_cu = target_cu;
51545339 11912 temp_die.offset = offset;
5c631832
JK
11913 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
11914}
10b3939b 11915
5c631832
JK
11916/* Follow reference attribute ATTR of SRC_DIE.
11917 On entry *REF_CU is the CU of SRC_DIE.
11918 On exit *REF_CU is the CU of the result. */
11919
11920static struct die_info *
11921follow_die_ref (struct die_info *src_die, struct attribute *attr,
11922 struct dwarf2_cu **ref_cu)
11923{
11924 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11925 struct dwarf2_cu *cu = *ref_cu;
11926 struct die_info *die;
11927
11928 die = follow_die_offset (offset, ref_cu);
11929 if (!die)
11930 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
11931 "at 0x%x [in module %s]"),
11932 offset, src_die->offset, cu->objfile->name);
348e048f 11933
5c631832
JK
11934 return die;
11935}
11936
11937/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
11938 value is intended for DW_OP_call*. */
11939
11940struct dwarf2_locexpr_baton
11941dwarf2_fetch_die_location_block (unsigned int offset,
11942 struct dwarf2_per_cu_data *per_cu)
11943{
11944 struct dwarf2_cu *cu = per_cu->cu;
11945 struct die_info *die;
11946 struct attribute *attr;
11947 struct dwarf2_locexpr_baton retval;
11948
11949 die = follow_die_offset (offset, &cu);
11950 if (!die)
11951 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
11952 offset, per_cu->cu->objfile->name);
11953
11954 attr = dwarf2_attr (die, DW_AT_location, cu);
11955 if (!attr)
11956 {
11957 /* DWARF: "If there is no such attribute, then there is no effect.". */
11958
11959 retval.data = NULL;
11960 retval.size = 0;
11961 }
11962 else
11963 {
11964 if (!attr_form_is_block (attr))
11965 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
11966 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
11967 offset, per_cu->cu->objfile->name);
11968
11969 retval.data = DW_BLOCK (attr)->data;
11970 retval.size = DW_BLOCK (attr)->size;
11971 }
11972 retval.per_cu = cu->per_cu;
11973 return retval;
348e048f
DE
11974}
11975
11976/* Follow the signature attribute ATTR in SRC_DIE.
11977 On entry *REF_CU is the CU of SRC_DIE.
11978 On exit *REF_CU is the CU of the result. */
11979
11980static struct die_info *
11981follow_die_sig (struct die_info *src_die, struct attribute *attr,
11982 struct dwarf2_cu **ref_cu)
11983{
11984 struct objfile *objfile = (*ref_cu)->objfile;
11985 struct die_info temp_die;
11986 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11987 struct dwarf2_cu *sig_cu;
11988 struct die_info *die;
11989
11990 /* sig_type will be NULL if the signatured type is missing from
11991 the debug info. */
11992 if (sig_type == NULL)
11993 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11994 "at 0x%x [in module %s]"),
11995 src_die->offset, objfile->name);
11996
11997 /* If necessary, add it to the queue and load its DIEs. */
11998
11999 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12000 read_signatured_type (objfile, sig_type);
12001
12002 gdb_assert (sig_type->per_cu.cu != NULL);
12003
12004 sig_cu = sig_type->per_cu.cu;
12005 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12006 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12007 if (die)
12008 {
12009 *ref_cu = sig_cu;
12010 return die;
12011 }
12012
12013 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12014 "at 0x%x [in module %s]"),
12015 sig_type->type_offset, src_die->offset, objfile->name);
12016}
12017
12018/* Given an offset of a signatured type, return its signatured_type. */
12019
12020static struct signatured_type *
12021lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12022{
12023 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12024 unsigned int length, initial_length_size;
12025 unsigned int sig_offset;
12026 struct signatured_type find_entry, *type_sig;
12027
12028 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12029 sig_offset = (initial_length_size
12030 + 2 /*version*/
12031 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12032 + 1 /*address_size*/);
12033 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12034 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12035
12036 /* This is only used to lookup previously recorded types.
12037 If we didn't find it, it's our bug. */
12038 gdb_assert (type_sig != NULL);
12039 gdb_assert (offset == type_sig->offset);
12040
12041 return type_sig;
12042}
12043
12044/* Read in signatured type at OFFSET and build its CU and die(s). */
12045
12046static void
12047read_signatured_type_at_offset (struct objfile *objfile,
12048 unsigned int offset)
12049{
12050 struct signatured_type *type_sig;
12051
be391dca
TT
12052 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12053
348e048f
DE
12054 /* We have the section offset, but we need the signature to do the
12055 hash table lookup. */
12056 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12057
12058 gdb_assert (type_sig->per_cu.cu == NULL);
12059
12060 read_signatured_type (objfile, type_sig);
12061
12062 gdb_assert (type_sig->per_cu.cu != NULL);
12063}
12064
12065/* Read in a signatured type and build its CU and DIEs. */
12066
12067static void
12068read_signatured_type (struct objfile *objfile,
12069 struct signatured_type *type_sig)
12070{
12071 gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12072 struct die_reader_specs reader_specs;
12073 struct dwarf2_cu *cu;
12074 ULONGEST signature;
12075 struct cleanup *back_to, *free_cu_cleanup;
12076 struct attribute *attr;
12077
12078 gdb_assert (type_sig->per_cu.cu == NULL);
12079
12080 cu = xmalloc (sizeof (struct dwarf2_cu));
12081 memset (cu, 0, sizeof (struct dwarf2_cu));
12082 obstack_init (&cu->comp_unit_obstack);
12083 cu->objfile = objfile;
12084 type_sig->per_cu.cu = cu;
12085 cu->per_cu = &type_sig->per_cu;
12086
12087 /* If an error occurs while loading, release our storage. */
12088 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12089
12090 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12091 types_ptr, objfile->obfd);
12092 gdb_assert (signature == type_sig->signature);
12093
12094 cu->die_hash
12095 = htab_create_alloc_ex (cu->header.length / 12,
12096 die_hash,
12097 die_eq,
12098 NULL,
12099 &cu->comp_unit_obstack,
12100 hashtab_obstack_allocate,
12101 dummy_obstack_deallocate);
12102
12103 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12104 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12105
12106 init_cu_die_reader (&reader_specs, cu);
12107
12108 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12109 NULL /*parent*/);
12110
12111 /* We try not to read any attributes in this function, because not
12112 all objfiles needed for references have been loaded yet, and symbol
12113 table processing isn't initialized. But we have to set the CU language,
12114 or we won't be able to build types correctly. */
12115 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12116 if (attr)
12117 set_cu_language (DW_UNSND (attr), cu);
12118 else
12119 set_cu_language (language_minimal, cu);
12120
12121 do_cleanups (back_to);
12122
12123 /* We've successfully allocated this compilation unit. Let our caller
12124 clean it up when finished with it. */
12125 discard_cleanups (free_cu_cleanup);
12126
12127 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12128 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
12129}
12130
c906108c
SS
12131/* Decode simple location descriptions.
12132 Given a pointer to a dwarf block that defines a location, compute
12133 the location and return the value.
12134
4cecd739
DJ
12135 NOTE drow/2003-11-18: This function is called in two situations
12136 now: for the address of static or global variables (partial symbols
12137 only) and for offsets into structures which are expected to be
12138 (more or less) constant. The partial symbol case should go away,
12139 and only the constant case should remain. That will let this
12140 function complain more accurately. A few special modes are allowed
12141 without complaint for global variables (for instance, global
12142 register values and thread-local values).
c906108c
SS
12143
12144 A location description containing no operations indicates that the
4cecd739 12145 object is optimized out. The return value is 0 for that case.
6b992462
DJ
12146 FIXME drow/2003-11-16: No callers check for this case any more; soon all
12147 callers will only want a very basic result and this can become a
12148 complaint.
c906108c 12149
c906108c
SS
12150 Note that stack[0] is unused except as a default error return.
12151 Note that stack overflow is not yet handled. */
12152
12153static CORE_ADDR
e7c27a73 12154decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 12155{
e7c27a73 12156 struct objfile *objfile = cu->objfile;
c906108c
SS
12157 int i;
12158 int size = blk->size;
fe1b8b76 12159 gdb_byte *data = blk->data;
c906108c
SS
12160 CORE_ADDR stack[64];
12161 int stacki;
12162 unsigned int bytes_read, unsnd;
fe1b8b76 12163 gdb_byte op;
c906108c
SS
12164
12165 i = 0;
12166 stacki = 0;
12167 stack[stacki] = 0;
c906108c
SS
12168
12169 while (i < size)
12170 {
c906108c
SS
12171 op = data[i++];
12172 switch (op)
12173 {
f1bea926
JM
12174 case DW_OP_lit0:
12175 case DW_OP_lit1:
12176 case DW_OP_lit2:
12177 case DW_OP_lit3:
12178 case DW_OP_lit4:
12179 case DW_OP_lit5:
12180 case DW_OP_lit6:
12181 case DW_OP_lit7:
12182 case DW_OP_lit8:
12183 case DW_OP_lit9:
12184 case DW_OP_lit10:
12185 case DW_OP_lit11:
12186 case DW_OP_lit12:
12187 case DW_OP_lit13:
12188 case DW_OP_lit14:
12189 case DW_OP_lit15:
12190 case DW_OP_lit16:
12191 case DW_OP_lit17:
12192 case DW_OP_lit18:
12193 case DW_OP_lit19:
12194 case DW_OP_lit20:
12195 case DW_OP_lit21:
12196 case DW_OP_lit22:
12197 case DW_OP_lit23:
12198 case DW_OP_lit24:
12199 case DW_OP_lit25:
12200 case DW_OP_lit26:
12201 case DW_OP_lit27:
12202 case DW_OP_lit28:
12203 case DW_OP_lit29:
12204 case DW_OP_lit30:
12205 case DW_OP_lit31:
12206 stack[++stacki] = op - DW_OP_lit0;
12207 break;
12208
c906108c
SS
12209 case DW_OP_reg0:
12210 case DW_OP_reg1:
12211 case DW_OP_reg2:
12212 case DW_OP_reg3:
12213 case DW_OP_reg4:
12214 case DW_OP_reg5:
12215 case DW_OP_reg6:
12216 case DW_OP_reg7:
12217 case DW_OP_reg8:
12218 case DW_OP_reg9:
12219 case DW_OP_reg10:
12220 case DW_OP_reg11:
12221 case DW_OP_reg12:
12222 case DW_OP_reg13:
12223 case DW_OP_reg14:
12224 case DW_OP_reg15:
12225 case DW_OP_reg16:
12226 case DW_OP_reg17:
12227 case DW_OP_reg18:
12228 case DW_OP_reg19:
12229 case DW_OP_reg20:
12230 case DW_OP_reg21:
12231 case DW_OP_reg22:
12232 case DW_OP_reg23:
12233 case DW_OP_reg24:
12234 case DW_OP_reg25:
12235 case DW_OP_reg26:
12236 case DW_OP_reg27:
12237 case DW_OP_reg28:
12238 case DW_OP_reg29:
12239 case DW_OP_reg30:
12240 case DW_OP_reg31:
c906108c 12241 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
12242 if (i < size)
12243 dwarf2_complex_location_expr_complaint ();
c906108c
SS
12244 break;
12245
12246 case DW_OP_regx:
c906108c
SS
12247 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12248 i += bytes_read;
c906108c 12249 stack[++stacki] = unsnd;
4cecd739
DJ
12250 if (i < size)
12251 dwarf2_complex_location_expr_complaint ();
c906108c
SS
12252 break;
12253
12254 case DW_OP_addr:
107d2387 12255 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 12256 cu, &bytes_read);
107d2387 12257 i += bytes_read;
c906108c
SS
12258 break;
12259
12260 case DW_OP_const1u:
12261 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12262 i += 1;
12263 break;
12264
12265 case DW_OP_const1s:
12266 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12267 i += 1;
12268 break;
12269
12270 case DW_OP_const2u:
12271 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12272 i += 2;
12273 break;
12274
12275 case DW_OP_const2s:
12276 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12277 i += 2;
12278 break;
12279
12280 case DW_OP_const4u:
12281 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12282 i += 4;
12283 break;
12284
12285 case DW_OP_const4s:
12286 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12287 i += 4;
12288 break;
12289
12290 case DW_OP_constu:
12291 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 12292 &bytes_read);
c906108c
SS
12293 i += bytes_read;
12294 break;
12295
12296 case DW_OP_consts:
12297 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12298 i += bytes_read;
12299 break;
12300
f1bea926
JM
12301 case DW_OP_dup:
12302 stack[stacki + 1] = stack[stacki];
12303 stacki++;
12304 break;
12305
c906108c
SS
12306 case DW_OP_plus:
12307 stack[stacki - 1] += stack[stacki];
12308 stacki--;
12309 break;
12310
12311 case DW_OP_plus_uconst:
12312 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12313 i += bytes_read;
12314 break;
12315
12316 case DW_OP_minus:
f1bea926 12317 stack[stacki - 1] -= stack[stacki];
c906108c
SS
12318 stacki--;
12319 break;
12320
7a292a7a 12321 case DW_OP_deref:
7a292a7a 12322 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
12323 this using GDB's address_class enum. This is valid for partial
12324 global symbols, although the variable's address will be bogus
12325 in the psymtab. */
7a292a7a 12326 if (i < size)
4d3c2250 12327 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
12328 break;
12329
9d774e44 12330 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
12331 /* The top of the stack has the offset from the beginning
12332 of the thread control block at which the variable is located. */
12333 /* Nothing should follow this operator, so the top of stack would
12334 be returned. */
4cecd739
DJ
12335 /* This is valid for partial global symbols, but the variable's
12336 address will be bogus in the psymtab. */
9d774e44 12337 if (i < size)
4d3c2250 12338 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
12339 break;
12340
42be36b3
CT
12341 case DW_OP_GNU_uninit:
12342 break;
12343
c906108c 12344 default:
e2e0b3e5 12345 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 12346 dwarf_stack_op_name (op, 1));
c906108c
SS
12347 return (stack[stacki]);
12348 }
12349 }
12350 return (stack[stacki]);
12351}
12352
12353/* memory allocation interface */
12354
c906108c 12355static struct dwarf_block *
7b5a2f43 12356dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
12357{
12358 struct dwarf_block *blk;
12359
12360 blk = (struct dwarf_block *)
7b5a2f43 12361 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
12362 return (blk);
12363}
12364
12365static struct abbrev_info *
f3dd6933 12366dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
12367{
12368 struct abbrev_info *abbrev;
12369
f3dd6933
DJ
12370 abbrev = (struct abbrev_info *)
12371 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
12372 memset (abbrev, 0, sizeof (struct abbrev_info));
12373 return (abbrev);
12374}
12375
12376static struct die_info *
b60c80d6 12377dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
12378{
12379 struct die_info *die;
b60c80d6
DJ
12380 size_t size = sizeof (struct die_info);
12381
12382 if (num_attrs > 1)
12383 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 12384
b60c80d6 12385 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
12386 memset (die, 0, sizeof (struct die_info));
12387 return (die);
12388}
2e276125
JB
12389
12390\f
12391/* Macro support. */
12392
12393
12394/* Return the full name of file number I in *LH's file name table.
12395 Use COMP_DIR as the name of the current directory of the
12396 compilation. The result is allocated using xmalloc; the caller is
12397 responsible for freeing it. */
12398static char *
12399file_full_name (int file, struct line_header *lh, const char *comp_dir)
12400{
6a83a1e6
EZ
12401 /* Is the file number a valid index into the line header's file name
12402 table? Remember that file numbers start with one, not zero. */
12403 if (1 <= file && file <= lh->num_file_names)
12404 {
12405 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 12406
6a83a1e6
EZ
12407 if (IS_ABSOLUTE_PATH (fe->name))
12408 return xstrdup (fe->name);
12409 else
12410 {
12411 const char *dir;
12412 int dir_len;
12413 char *full_name;
12414
12415 if (fe->dir_index)
12416 dir = lh->include_dirs[fe->dir_index - 1];
12417 else
12418 dir = comp_dir;
12419
12420 if (dir)
12421 {
12422 dir_len = strlen (dir);
12423 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
12424 strcpy (full_name, dir);
12425 full_name[dir_len] = '/';
12426 strcpy (full_name + dir_len + 1, fe->name);
12427 return full_name;
12428 }
12429 else
12430 return xstrdup (fe->name);
12431 }
12432 }
2e276125
JB
12433 else
12434 {
6a83a1e6
EZ
12435 /* The compiler produced a bogus file number. We can at least
12436 record the macro definitions made in the file, even if we
12437 won't be able to find the file by name. */
12438 char fake_name[80];
9a619af0 12439
6a83a1e6 12440 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 12441
6e70227d 12442 complaint (&symfile_complaints,
6a83a1e6
EZ
12443 _("bad file number in macro information (%d)"),
12444 file);
2e276125 12445
6a83a1e6 12446 return xstrdup (fake_name);
2e276125
JB
12447 }
12448}
12449
12450
12451static struct macro_source_file *
12452macro_start_file (int file, int line,
12453 struct macro_source_file *current_file,
12454 const char *comp_dir,
12455 struct line_header *lh, struct objfile *objfile)
12456{
12457 /* The full name of this source file. */
12458 char *full_name = file_full_name (file, lh, comp_dir);
12459
12460 /* We don't create a macro table for this compilation unit
12461 at all until we actually get a filename. */
12462 if (! pending_macros)
4a146b47 12463 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 12464 objfile->macro_cache);
2e276125
JB
12465
12466 if (! current_file)
12467 /* If we have no current file, then this must be the start_file
12468 directive for the compilation unit's main source file. */
12469 current_file = macro_set_main (pending_macros, full_name);
12470 else
12471 current_file = macro_include (current_file, line, full_name);
12472
12473 xfree (full_name);
6e70227d 12474
2e276125
JB
12475 return current_file;
12476}
12477
12478
12479/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
12480 followed by a null byte. */
12481static char *
12482copy_string (const char *buf, int len)
12483{
12484 char *s = xmalloc (len + 1);
9a619af0 12485
2e276125
JB
12486 memcpy (s, buf, len);
12487 s[len] = '\0';
2e276125
JB
12488 return s;
12489}
12490
12491
12492static const char *
12493consume_improper_spaces (const char *p, const char *body)
12494{
12495 if (*p == ' ')
12496 {
4d3c2250 12497 complaint (&symfile_complaints,
e2e0b3e5 12498 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 12499 body);
2e276125
JB
12500
12501 while (*p == ' ')
12502 p++;
12503 }
12504
12505 return p;
12506}
12507
12508
12509static void
12510parse_macro_definition (struct macro_source_file *file, int line,
12511 const char *body)
12512{
12513 const char *p;
12514
12515 /* The body string takes one of two forms. For object-like macro
12516 definitions, it should be:
12517
12518 <macro name> " " <definition>
12519
12520 For function-like macro definitions, it should be:
12521
12522 <macro name> "() " <definition>
12523 or
12524 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
12525
12526 Spaces may appear only where explicitly indicated, and in the
12527 <definition>.
12528
12529 The Dwarf 2 spec says that an object-like macro's name is always
12530 followed by a space, but versions of GCC around March 2002 omit
6e70227d 12531 the space when the macro's definition is the empty string.
2e276125
JB
12532
12533 The Dwarf 2 spec says that there should be no spaces between the
12534 formal arguments in a function-like macro's formal argument list,
12535 but versions of GCC around March 2002 include spaces after the
12536 commas. */
12537
12538
12539 /* Find the extent of the macro name. The macro name is terminated
12540 by either a space or null character (for an object-like macro) or
12541 an opening paren (for a function-like macro). */
12542 for (p = body; *p; p++)
12543 if (*p == ' ' || *p == '(')
12544 break;
12545
12546 if (*p == ' ' || *p == '\0')
12547 {
12548 /* It's an object-like macro. */
12549 int name_len = p - body;
12550 char *name = copy_string (body, name_len);
12551 const char *replacement;
12552
12553 if (*p == ' ')
12554 replacement = body + name_len + 1;
12555 else
12556 {
4d3c2250 12557 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12558 replacement = body + name_len;
12559 }
6e70227d 12560
2e276125
JB
12561 macro_define_object (file, line, name, replacement);
12562
12563 xfree (name);
12564 }
12565 else if (*p == '(')
12566 {
12567 /* It's a function-like macro. */
12568 char *name = copy_string (body, p - body);
12569 int argc = 0;
12570 int argv_size = 1;
12571 char **argv = xmalloc (argv_size * sizeof (*argv));
12572
12573 p++;
12574
12575 p = consume_improper_spaces (p, body);
12576
12577 /* Parse the formal argument list. */
12578 while (*p && *p != ')')
12579 {
12580 /* Find the extent of the current argument name. */
12581 const char *arg_start = p;
12582
12583 while (*p && *p != ',' && *p != ')' && *p != ' ')
12584 p++;
12585
12586 if (! *p || p == arg_start)
4d3c2250 12587 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12588 else
12589 {
12590 /* Make sure argv has room for the new argument. */
12591 if (argc >= argv_size)
12592 {
12593 argv_size *= 2;
12594 argv = xrealloc (argv, argv_size * sizeof (*argv));
12595 }
12596
12597 argv[argc++] = copy_string (arg_start, p - arg_start);
12598 }
12599
12600 p = consume_improper_spaces (p, body);
12601
12602 /* Consume the comma, if present. */
12603 if (*p == ',')
12604 {
12605 p++;
12606
12607 p = consume_improper_spaces (p, body);
12608 }
12609 }
12610
12611 if (*p == ')')
12612 {
12613 p++;
12614
12615 if (*p == ' ')
12616 /* Perfectly formed definition, no complaints. */
12617 macro_define_function (file, line, name,
6e70227d 12618 argc, (const char **) argv,
2e276125
JB
12619 p + 1);
12620 else if (*p == '\0')
12621 {
12622 /* Complain, but do define it. */
4d3c2250 12623 dwarf2_macro_malformed_definition_complaint (body);
2e276125 12624 macro_define_function (file, line, name,
6e70227d 12625 argc, (const char **) argv,
2e276125
JB
12626 p);
12627 }
12628 else
12629 /* Just complain. */
4d3c2250 12630 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12631 }
12632 else
12633 /* Just complain. */
4d3c2250 12634 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12635
12636 xfree (name);
12637 {
12638 int i;
12639
12640 for (i = 0; i < argc; i++)
12641 xfree (argv[i]);
12642 }
12643 xfree (argv);
12644 }
12645 else
4d3c2250 12646 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12647}
12648
12649
12650static void
12651dwarf_decode_macros (struct line_header *lh, unsigned int offset,
12652 char *comp_dir, bfd *abfd,
e7c27a73 12653 struct dwarf2_cu *cu)
2e276125 12654{
fe1b8b76 12655 gdb_byte *mac_ptr, *mac_end;
2e276125 12656 struct macro_source_file *current_file = 0;
757a13d0
JK
12657 enum dwarf_macinfo_record_type macinfo_type;
12658 int at_commandline;
2e276125 12659
be391dca
TT
12660 dwarf2_read_section (dwarf2_per_objfile->objfile,
12661 &dwarf2_per_objfile->macinfo);
dce234bc 12662 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 12663 {
e2e0b3e5 12664 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
12665 return;
12666 }
12667
757a13d0
JK
12668 /* First pass: Find the name of the base filename.
12669 This filename is needed in order to process all macros whose definition
12670 (or undefinition) comes from the command line. These macros are defined
12671 before the first DW_MACINFO_start_file entry, and yet still need to be
12672 associated to the base file.
12673
12674 To determine the base file name, we scan the macro definitions until we
12675 reach the first DW_MACINFO_start_file entry. We then initialize
12676 CURRENT_FILE accordingly so that any macro definition found before the
12677 first DW_MACINFO_start_file can still be associated to the base file. */
12678
dce234bc
PP
12679 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12680 mac_end = dwarf2_per_objfile->macinfo.buffer
12681 + dwarf2_per_objfile->macinfo.size;
2e276125 12682
757a13d0 12683 do
2e276125 12684 {
2e276125
JB
12685 /* Do we at least have room for a macinfo type byte? */
12686 if (mac_ptr >= mac_end)
12687 {
757a13d0
JK
12688 /* Complaint is printed during the second pass as GDB will probably
12689 stop the first pass earlier upon finding DW_MACINFO_start_file. */
12690 break;
2e276125
JB
12691 }
12692
12693 macinfo_type = read_1_byte (abfd, mac_ptr);
12694 mac_ptr++;
12695
12696 switch (macinfo_type)
12697 {
12698 /* A zero macinfo type indicates the end of the macro
12699 information. */
12700 case 0:
757a13d0
JK
12701 break;
12702
12703 case DW_MACINFO_define:
12704 case DW_MACINFO_undef:
12705 /* Only skip the data by MAC_PTR. */
12706 {
12707 unsigned int bytes_read;
12708
12709 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12710 mac_ptr += bytes_read;
12711 read_string (abfd, mac_ptr, &bytes_read);
12712 mac_ptr += bytes_read;
12713 }
12714 break;
12715
12716 case DW_MACINFO_start_file:
12717 {
12718 unsigned int bytes_read;
12719 int line, file;
12720
12721 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12722 mac_ptr += bytes_read;
12723 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12724 mac_ptr += bytes_read;
12725
12726 current_file = macro_start_file (file, line, current_file, comp_dir,
12727 lh, cu->objfile);
12728 }
12729 break;
12730
12731 case DW_MACINFO_end_file:
12732 /* No data to skip by MAC_PTR. */
12733 break;
12734
12735 case DW_MACINFO_vendor_ext:
12736 /* Only skip the data by MAC_PTR. */
12737 {
12738 unsigned int bytes_read;
12739
12740 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12741 mac_ptr += bytes_read;
12742 read_string (abfd, mac_ptr, &bytes_read);
12743 mac_ptr += bytes_read;
12744 }
12745 break;
12746
12747 default:
12748 break;
12749 }
12750 } while (macinfo_type != 0 && current_file == NULL);
12751
12752 /* Second pass: Process all entries.
12753
12754 Use the AT_COMMAND_LINE flag to determine whether we are still processing
12755 command-line macro definitions/undefinitions. This flag is unset when we
12756 reach the first DW_MACINFO_start_file entry. */
12757
dce234bc 12758 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
12759
12760 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
12761 GDB is still reading the definitions from command line. First
12762 DW_MACINFO_start_file will need to be ignored as it was already executed
12763 to create CURRENT_FILE for the main source holding also the command line
12764 definitions. On first met DW_MACINFO_start_file this flag is reset to
12765 normally execute all the remaining DW_MACINFO_start_file macinfos. */
12766
12767 at_commandline = 1;
12768
12769 do
12770 {
12771 /* Do we at least have room for a macinfo type byte? */
12772 if (mac_ptr >= mac_end)
12773 {
12774 dwarf2_macros_too_long_complaint ();
12775 break;
12776 }
12777
12778 macinfo_type = read_1_byte (abfd, mac_ptr);
12779 mac_ptr++;
12780
12781 switch (macinfo_type)
12782 {
12783 /* A zero macinfo type indicates the end of the macro
12784 information. */
12785 case 0:
12786 break;
2e276125
JB
12787
12788 case DW_MACINFO_define:
12789 case DW_MACINFO_undef:
12790 {
891d2f0b 12791 unsigned int bytes_read;
2e276125
JB
12792 int line;
12793 char *body;
12794
12795 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12796 mac_ptr += bytes_read;
12797 body = read_string (abfd, mac_ptr, &bytes_read);
12798 mac_ptr += bytes_read;
12799
12800 if (! current_file)
757a13d0
JK
12801 {
12802 /* DWARF violation as no main source is present. */
12803 complaint (&symfile_complaints,
12804 _("debug info with no main source gives macro %s "
12805 "on line %d: %s"),
6e70227d
DE
12806 macinfo_type == DW_MACINFO_define ?
12807 _("definition") :
905e0470
PM
12808 macinfo_type == DW_MACINFO_undef ?
12809 _("undefinition") :
12810 _("something-or-other"), line, body);
757a13d0
JK
12811 break;
12812 }
12813 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 12814 complaint (&symfile_complaints,
757a13d0
JK
12815 _("debug info gives %s macro %s with %s line %d: %s"),
12816 at_commandline ? _("command-line") : _("in-file"),
905e0470 12817 macinfo_type == DW_MACINFO_define ?
6e70227d 12818 _("definition") :
905e0470
PM
12819 macinfo_type == DW_MACINFO_undef ?
12820 _("undefinition") :
12821 _("something-or-other"),
757a13d0
JK
12822 line == 0 ? _("zero") : _("non-zero"), line, body);
12823
12824 if (macinfo_type == DW_MACINFO_define)
12825 parse_macro_definition (current_file, line, body);
12826 else if (macinfo_type == DW_MACINFO_undef)
12827 macro_undef (current_file, line, body);
2e276125
JB
12828 }
12829 break;
12830
12831 case DW_MACINFO_start_file:
12832 {
891d2f0b 12833 unsigned int bytes_read;
2e276125
JB
12834 int line, file;
12835
12836 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12837 mac_ptr += bytes_read;
12838 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12839 mac_ptr += bytes_read;
12840
757a13d0
JK
12841 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
12842 complaint (&symfile_complaints,
12843 _("debug info gives source %d included "
12844 "from %s at %s line %d"),
12845 file, at_commandline ? _("command-line") : _("file"),
12846 line == 0 ? _("zero") : _("non-zero"), line);
12847
12848 if (at_commandline)
12849 {
12850 /* This DW_MACINFO_start_file was executed in the pass one. */
12851 at_commandline = 0;
12852 }
12853 else
12854 current_file = macro_start_file (file, line,
12855 current_file, comp_dir,
12856 lh, cu->objfile);
2e276125
JB
12857 }
12858 break;
12859
12860 case DW_MACINFO_end_file:
12861 if (! current_file)
4d3c2250 12862 complaint (&symfile_complaints,
e2e0b3e5 12863 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
12864 else
12865 {
12866 current_file = current_file->included_by;
12867 if (! current_file)
12868 {
12869 enum dwarf_macinfo_record_type next_type;
12870
12871 /* GCC circa March 2002 doesn't produce the zero
12872 type byte marking the end of the compilation
12873 unit. Complain if it's not there, but exit no
12874 matter what. */
12875
12876 /* Do we at least have room for a macinfo type byte? */
12877 if (mac_ptr >= mac_end)
12878 {
4d3c2250 12879 dwarf2_macros_too_long_complaint ();
2e276125
JB
12880 return;
12881 }
12882
12883 /* We don't increment mac_ptr here, so this is just
12884 a look-ahead. */
12885 next_type = read_1_byte (abfd, mac_ptr);
12886 if (next_type != 0)
4d3c2250 12887 complaint (&symfile_complaints,
e2e0b3e5 12888 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
12889
12890 return;
12891 }
12892 }
12893 break;
12894
12895 case DW_MACINFO_vendor_ext:
12896 {
891d2f0b 12897 unsigned int bytes_read;
2e276125
JB
12898 int constant;
12899 char *string;
12900
12901 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12902 mac_ptr += bytes_read;
12903 string = read_string (abfd, mac_ptr, &bytes_read);
12904 mac_ptr += bytes_read;
12905
12906 /* We don't recognize any vendor extensions. */
12907 }
12908 break;
12909 }
757a13d0 12910 } while (macinfo_type != 0);
2e276125 12911}
8e19ed76
PS
12912
12913/* Check if the attribute's form is a DW_FORM_block*
12914 if so return true else false. */
12915static int
12916attr_form_is_block (struct attribute *attr)
12917{
12918 return (attr == NULL ? 0 :
12919 attr->form == DW_FORM_block1
12920 || attr->form == DW_FORM_block2
12921 || attr->form == DW_FORM_block4
2dc7f7b3
TT
12922 || attr->form == DW_FORM_block
12923 || attr->form == DW_FORM_exprloc);
8e19ed76 12924}
4c2df51b 12925
c6a0999f
JB
12926/* Return non-zero if ATTR's value is a section offset --- classes
12927 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
12928 You may use DW_UNSND (attr) to retrieve such offsets.
12929
12930 Section 7.5.4, "Attribute Encodings", explains that no attribute
12931 may have a value that belongs to more than one of these classes; it
12932 would be ambiguous if we did, because we use the same forms for all
12933 of them. */
3690dd37
JB
12934static int
12935attr_form_is_section_offset (struct attribute *attr)
12936{
12937 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
12938 || attr->form == DW_FORM_data8
12939 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
12940}
12941
12942
12943/* Return non-zero if ATTR's value falls in the 'constant' class, or
12944 zero otherwise. When this function returns true, you can apply
12945 dwarf2_get_attr_constant_value to it.
12946
12947 However, note that for some attributes you must check
12948 attr_form_is_section_offset before using this test. DW_FORM_data4
12949 and DW_FORM_data8 are members of both the constant class, and of
12950 the classes that contain offsets into other debug sections
12951 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
12952 that, if an attribute's can be either a constant or one of the
12953 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
12954 taken as section offsets, not constants. */
12955static int
12956attr_form_is_constant (struct attribute *attr)
12957{
12958 switch (attr->form)
12959 {
12960 case DW_FORM_sdata:
12961 case DW_FORM_udata:
12962 case DW_FORM_data1:
12963 case DW_FORM_data2:
12964 case DW_FORM_data4:
12965 case DW_FORM_data8:
12966 return 1;
12967 default:
12968 return 0;
12969 }
12970}
12971
4c2df51b
DJ
12972static void
12973dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 12974 struct dwarf2_cu *cu)
4c2df51b 12975{
3690dd37 12976 if (attr_form_is_section_offset (attr)
99bcc461
DJ
12977 /* ".debug_loc" may not exist at all, or the offset may be outside
12978 the section. If so, fall through to the complaint in the
12979 other branch. */
dce234bc 12980 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 12981 {
0d53c4c4 12982 struct dwarf2_loclist_baton *baton;
4c2df51b 12983
4a146b47 12984 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 12985 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
12986 baton->per_cu = cu->per_cu;
12987 gdb_assert (baton->per_cu);
4c2df51b 12988
be391dca
TT
12989 dwarf2_read_section (dwarf2_per_objfile->objfile,
12990 &dwarf2_per_objfile->loc);
12991
0d53c4c4
DJ
12992 /* We don't know how long the location list is, but make sure we
12993 don't run off the edge of the section. */
dce234bc
PP
12994 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
12995 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
12996 baton->base_address = cu->base_address;
12997 if (cu->base_known == 0)
0d53c4c4 12998 complaint (&symfile_complaints,
e2e0b3e5 12999 _("Location list used without specifying the CU base address."));
4c2df51b 13000
768a979c 13001 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13002 SYMBOL_LOCATION_BATON (sym) = baton;
13003 }
13004 else
13005 {
13006 struct dwarf2_locexpr_baton *baton;
13007
4a146b47 13008 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13009 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13010 baton->per_cu = cu->per_cu;
13011 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13012
13013 if (attr_form_is_block (attr))
13014 {
13015 /* Note that we're just copying the block's data pointer
13016 here, not the actual data. We're still pointing into the
6502dd73
DJ
13017 info_buffer for SYM's objfile; right now we never release
13018 that buffer, but when we do clean up properly this may
13019 need to change. */
0d53c4c4
DJ
13020 baton->size = DW_BLOCK (attr)->size;
13021 baton->data = DW_BLOCK (attr)->data;
13022 }
13023 else
13024 {
13025 dwarf2_invalid_attrib_class_complaint ("location description",
13026 SYMBOL_NATURAL_NAME (sym));
13027 baton->size = 0;
13028 baton->data = NULL;
13029 }
6e70227d 13030
768a979c 13031 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13032 SYMBOL_LOCATION_BATON (sym) = baton;
13033 }
4c2df51b 13034}
6502dd73 13035
9aa1f1e3
TT
13036/* Return the OBJFILE associated with the compilation unit CU. If CU
13037 came from a separate debuginfo file, then the master objfile is
13038 returned. */
ae0d2f24
UW
13039
13040struct objfile *
13041dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13042{
9291a0cd 13043 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13044
13045 /* Return the master objfile, so that we can report and look up the
13046 correct file containing this variable. */
13047 if (objfile->separate_debug_objfile_backlink)
13048 objfile = objfile->separate_debug_objfile_backlink;
13049
13050 return objfile;
13051}
13052
13053/* Return the address size given in the compilation unit header for CU. */
13054
13055CORE_ADDR
13056dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13057{
13058 if (per_cu->cu)
13059 return per_cu->cu->header.addr_size;
13060 else
13061 {
13062 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13063 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13064 struct dwarf2_per_objfile *per_objfile
13065 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13066 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13067 struct comp_unit_head cu_header;
9a619af0 13068
ae0d2f24
UW
13069 memset (&cu_header, 0, sizeof cu_header);
13070 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13071 return cu_header.addr_size;
13072 }
13073}
13074
9eae7c52
TT
13075/* Return the offset size given in the compilation unit header for CU. */
13076
13077int
13078dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13079{
13080 if (per_cu->cu)
13081 return per_cu->cu->header.offset_size;
13082 else
13083 {
13084 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13085 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
13086 struct dwarf2_per_objfile *per_objfile
13087 = objfile_data (objfile, dwarf2_objfile_data_key);
13088 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13089 struct comp_unit_head cu_header;
13090
13091 memset (&cu_header, 0, sizeof cu_header);
13092 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13093 return cu_header.offset_size;
13094 }
13095}
13096
9aa1f1e3
TT
13097/* Return the text offset of the CU. The returned offset comes from
13098 this CU's objfile. If this objfile came from a separate debuginfo
13099 file, then the offset may be different from the corresponding
13100 offset in the parent objfile. */
13101
13102CORE_ADDR
13103dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13104{
bb3fa9d0 13105 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
13106
13107 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13108}
13109
348e048f
DE
13110/* Locate the .debug_info compilation unit from CU's objfile which contains
13111 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
13112
13113static struct dwarf2_per_cu_data *
c764a876 13114dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
13115 struct objfile *objfile)
13116{
13117 struct dwarf2_per_cu_data *this_cu;
13118 int low, high;
13119
ae038cb0
DJ
13120 low = 0;
13121 high = dwarf2_per_objfile->n_comp_units - 1;
13122 while (high > low)
13123 {
13124 int mid = low + (high - low) / 2;
9a619af0 13125
ae038cb0
DJ
13126 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13127 high = mid;
13128 else
13129 low = mid + 1;
13130 }
13131 gdb_assert (low == high);
13132 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13133 {
10b3939b 13134 if (low == 0)
8a3fe4f8
AC
13135 error (_("Dwarf Error: could not find partial DIE containing "
13136 "offset 0x%lx [in module %s]"),
10b3939b
DJ
13137 (long) offset, bfd_get_filename (objfile->obfd));
13138
ae038cb0
DJ
13139 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13140 return dwarf2_per_objfile->all_comp_units[low-1];
13141 }
13142 else
13143 {
13144 this_cu = dwarf2_per_objfile->all_comp_units[low];
13145 if (low == dwarf2_per_objfile->n_comp_units - 1
13146 && offset >= this_cu->offset + this_cu->length)
c764a876 13147 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
13148 gdb_assert (offset < this_cu->offset + this_cu->length);
13149 return this_cu;
13150 }
13151}
13152
10b3939b
DJ
13153/* Locate the compilation unit from OBJFILE which is located at exactly
13154 OFFSET. Raises an error on failure. */
13155
ae038cb0 13156static struct dwarf2_per_cu_data *
c764a876 13157dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
13158{
13159 struct dwarf2_per_cu_data *this_cu;
9a619af0 13160
ae038cb0
DJ
13161 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13162 if (this_cu->offset != offset)
c764a876 13163 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
13164 return this_cu;
13165}
13166
93311388
DE
13167/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
13168
13169static struct dwarf2_cu *
13170alloc_one_comp_unit (struct objfile *objfile)
13171{
13172 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13173 cu->objfile = objfile;
13174 obstack_init (&cu->comp_unit_obstack);
13175 return cu;
13176}
13177
ae038cb0
DJ
13178/* Release one cached compilation unit, CU. We unlink it from the tree
13179 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
13180 the caller is responsible for that.
13181 NOTE: DATA is a void * because this function is also used as a
13182 cleanup routine. */
ae038cb0
DJ
13183
13184static void
13185free_one_comp_unit (void *data)
13186{
13187 struct dwarf2_cu *cu = data;
13188
13189 if (cu->per_cu != NULL)
13190 cu->per_cu->cu = NULL;
13191 cu->per_cu = NULL;
13192
13193 obstack_free (&cu->comp_unit_obstack, NULL);
13194
13195 xfree (cu);
13196}
13197
72bf9492 13198/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
13199 when we're finished with it. We can't free the pointer itself, but be
13200 sure to unlink it from the cache. Also release any associated storage
13201 and perform cache maintenance.
72bf9492
DJ
13202
13203 Only used during partial symbol parsing. */
13204
13205static void
13206free_stack_comp_unit (void *data)
13207{
13208 struct dwarf2_cu *cu = data;
13209
13210 obstack_free (&cu->comp_unit_obstack, NULL);
13211 cu->partial_dies = NULL;
ae038cb0
DJ
13212
13213 if (cu->per_cu != NULL)
13214 {
13215 /* This compilation unit is on the stack in our caller, so we
13216 should not xfree it. Just unlink it. */
13217 cu->per_cu->cu = NULL;
13218 cu->per_cu = NULL;
13219
13220 /* If we had a per-cu pointer, then we may have other compilation
13221 units loaded, so age them now. */
13222 age_cached_comp_units ();
13223 }
13224}
13225
13226/* Free all cached compilation units. */
13227
13228static void
13229free_cached_comp_units (void *data)
13230{
13231 struct dwarf2_per_cu_data *per_cu, **last_chain;
13232
13233 per_cu = dwarf2_per_objfile->read_in_chain;
13234 last_chain = &dwarf2_per_objfile->read_in_chain;
13235 while (per_cu != NULL)
13236 {
13237 struct dwarf2_per_cu_data *next_cu;
13238
13239 next_cu = per_cu->cu->read_in_chain;
13240
13241 free_one_comp_unit (per_cu->cu);
13242 *last_chain = next_cu;
13243
13244 per_cu = next_cu;
13245 }
13246}
13247
13248/* Increase the age counter on each cached compilation unit, and free
13249 any that are too old. */
13250
13251static void
13252age_cached_comp_units (void)
13253{
13254 struct dwarf2_per_cu_data *per_cu, **last_chain;
13255
13256 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13257 per_cu = dwarf2_per_objfile->read_in_chain;
13258 while (per_cu != NULL)
13259 {
13260 per_cu->cu->last_used ++;
13261 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13262 dwarf2_mark (per_cu->cu);
13263 per_cu = per_cu->cu->read_in_chain;
13264 }
13265
13266 per_cu = dwarf2_per_objfile->read_in_chain;
13267 last_chain = &dwarf2_per_objfile->read_in_chain;
13268 while (per_cu != NULL)
13269 {
13270 struct dwarf2_per_cu_data *next_cu;
13271
13272 next_cu = per_cu->cu->read_in_chain;
13273
13274 if (!per_cu->cu->mark)
13275 {
13276 free_one_comp_unit (per_cu->cu);
13277 *last_chain = next_cu;
13278 }
13279 else
13280 last_chain = &per_cu->cu->read_in_chain;
13281
13282 per_cu = next_cu;
13283 }
13284}
13285
13286/* Remove a single compilation unit from the cache. */
13287
13288static void
13289free_one_cached_comp_unit (void *target_cu)
13290{
13291 struct dwarf2_per_cu_data *per_cu, **last_chain;
13292
13293 per_cu = dwarf2_per_objfile->read_in_chain;
13294 last_chain = &dwarf2_per_objfile->read_in_chain;
13295 while (per_cu != NULL)
13296 {
13297 struct dwarf2_per_cu_data *next_cu;
13298
13299 next_cu = per_cu->cu->read_in_chain;
13300
13301 if (per_cu->cu == target_cu)
13302 {
13303 free_one_comp_unit (per_cu->cu);
13304 *last_chain = next_cu;
13305 break;
13306 }
13307 else
13308 last_chain = &per_cu->cu->read_in_chain;
13309
13310 per_cu = next_cu;
13311 }
13312}
13313
fe3e1990
DJ
13314/* Release all extra memory associated with OBJFILE. */
13315
13316void
13317dwarf2_free_objfile (struct objfile *objfile)
13318{
13319 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
13320
13321 if (dwarf2_per_objfile == NULL)
13322 return;
13323
13324 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
13325 free_cached_comp_units (NULL);
13326
9291a0cd
TT
13327 if (dwarf2_per_objfile->using_index)
13328 {
13329 int i;
13330
13331 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
13332 {
13333 int j;
13334 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
13335
13336 if (!cu->v.quick->lines)
13337 continue;
13338
13339 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
13340 {
13341 if (cu->v.quick->file_names)
13342 xfree ((void *) cu->v.quick->file_names[j]);
13343 if (cu->v.quick->full_names)
13344 xfree ((void *) cu->v.quick->full_names[j]);
13345 }
13346
13347 free_line_header (cu->v.quick->lines);
13348 }
13349 }
13350
fe3e1990
DJ
13351 /* Everything else should be on the objfile obstack. */
13352}
13353
1c379e20
DJ
13354/* A pair of DIE offset and GDB type pointer. We store these
13355 in a hash table separate from the DIEs, and preserve them
13356 when the DIEs are flushed out of cache. */
13357
13358struct dwarf2_offset_and_type
13359{
13360 unsigned int offset;
13361 struct type *type;
13362};
13363
13364/* Hash function for a dwarf2_offset_and_type. */
13365
13366static hashval_t
13367offset_and_type_hash (const void *item)
13368{
13369 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 13370
1c379e20
DJ
13371 return ofs->offset;
13372}
13373
13374/* Equality function for a dwarf2_offset_and_type. */
13375
13376static int
13377offset_and_type_eq (const void *item_lhs, const void *item_rhs)
13378{
13379 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
13380 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 13381
1c379e20
DJ
13382 return ofs_lhs->offset == ofs_rhs->offset;
13383}
13384
13385/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
13386 table if necessary. For convenience, return TYPE.
13387
13388 The DIEs reading must have careful ordering to:
13389 * Not cause infite loops trying to read in DIEs as a prerequisite for
13390 reading current DIE.
13391 * Not trying to dereference contents of still incompletely read in types
13392 while reading in other DIEs.
13393 * Enable referencing still incompletely read in types just by a pointer to
13394 the type without accessing its fields.
13395
13396 Therefore caller should follow these rules:
13397 * Try to fetch any prerequisite types we may need to build this DIE type
13398 before building the type and calling set_die_type.
13399 * After building typer call set_die_type for current DIE as soon as
13400 possible before fetching more types to complete the current type.
13401 * Make the type as complete as possible before fetching more types. */
1c379e20 13402
f792889a 13403static struct type *
1c379e20
DJ
13404set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13405{
13406 struct dwarf2_offset_and_type **slot, ofs;
13407
b4ba55a1
JB
13408 /* For Ada types, make sure that the gnat-specific data is always
13409 initialized (if not already set). There are a few types where
13410 we should not be doing so, because the type-specific area is
13411 already used to hold some other piece of info (eg: TYPE_CODE_FLT
13412 where the type-specific area is used to store the floatformat).
13413 But this is not a problem, because the gnat-specific information
13414 is actually not needed for these types. */
13415 if (need_gnat_info (cu)
13416 && TYPE_CODE (type) != TYPE_CODE_FUNC
13417 && TYPE_CODE (type) != TYPE_CODE_FLT
13418 && !HAVE_GNAT_AUX_INFO (type))
13419 INIT_GNAT_SPECIFIC (type);
13420
f792889a
DJ
13421 if (cu->type_hash == NULL)
13422 {
13423 gdb_assert (cu->per_cu != NULL);
13424 cu->per_cu->type_hash
13425 = htab_create_alloc_ex (cu->header.length / 24,
13426 offset_and_type_hash,
13427 offset_and_type_eq,
13428 NULL,
13429 &cu->objfile->objfile_obstack,
13430 hashtab_obstack_allocate,
13431 dummy_obstack_deallocate);
13432 cu->type_hash = cu->per_cu->type_hash;
13433 }
1c379e20
DJ
13434
13435 ofs.offset = die->offset;
13436 ofs.type = type;
13437 slot = (struct dwarf2_offset_and_type **)
f792889a 13438 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
7e314c57
JK
13439 if (*slot)
13440 complaint (&symfile_complaints,
13441 _("A problem internal to GDB: DIE 0x%x has type already set"),
13442 die->offset);
1c379e20
DJ
13443 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
13444 **slot = ofs;
f792889a 13445 return type;
1c379e20
DJ
13446}
13447
f792889a
DJ
13448/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
13449 not have a saved type. */
1c379e20
DJ
13450
13451static struct type *
f792889a 13452get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
13453{
13454 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
13455 htab_t type_hash = cu->type_hash;
13456
13457 if (type_hash == NULL)
13458 return NULL;
1c379e20
DJ
13459
13460 ofs.offset = die->offset;
13461 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
13462 if (slot)
13463 return slot->type;
13464 else
13465 return NULL;
13466}
13467
10b3939b
DJ
13468/* Add a dependence relationship from CU to REF_PER_CU. */
13469
13470static void
13471dwarf2_add_dependence (struct dwarf2_cu *cu,
13472 struct dwarf2_per_cu_data *ref_per_cu)
13473{
13474 void **slot;
13475
13476 if (cu->dependencies == NULL)
13477 cu->dependencies
13478 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
13479 NULL, &cu->comp_unit_obstack,
13480 hashtab_obstack_allocate,
13481 dummy_obstack_deallocate);
13482
13483 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
13484 if (*slot == NULL)
13485 *slot = ref_per_cu;
13486}
1c379e20 13487
f504f079
DE
13488/* Subroutine of dwarf2_mark to pass to htab_traverse.
13489 Set the mark field in every compilation unit in the
ae038cb0
DJ
13490 cache that we must keep because we are keeping CU. */
13491
10b3939b
DJ
13492static int
13493dwarf2_mark_helper (void **slot, void *data)
13494{
13495 struct dwarf2_per_cu_data *per_cu;
13496
13497 per_cu = (struct dwarf2_per_cu_data *) *slot;
13498 if (per_cu->cu->mark)
13499 return 1;
13500 per_cu->cu->mark = 1;
13501
13502 if (per_cu->cu->dependencies != NULL)
13503 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
13504
13505 return 1;
13506}
13507
f504f079
DE
13508/* Set the mark field in CU and in every other compilation unit in the
13509 cache that we must keep because we are keeping CU. */
13510
ae038cb0
DJ
13511static void
13512dwarf2_mark (struct dwarf2_cu *cu)
13513{
13514 if (cu->mark)
13515 return;
13516 cu->mark = 1;
10b3939b
DJ
13517 if (cu->dependencies != NULL)
13518 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
13519}
13520
13521static void
13522dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
13523{
13524 while (per_cu)
13525 {
13526 per_cu->cu->mark = 0;
13527 per_cu = per_cu->cu->read_in_chain;
13528 }
72bf9492
DJ
13529}
13530
72bf9492
DJ
13531/* Trivial hash function for partial_die_info: the hash value of a DIE
13532 is its offset in .debug_info for this objfile. */
13533
13534static hashval_t
13535partial_die_hash (const void *item)
13536{
13537 const struct partial_die_info *part_die = item;
9a619af0 13538
72bf9492
DJ
13539 return part_die->offset;
13540}
13541
13542/* Trivial comparison function for partial_die_info structures: two DIEs
13543 are equal if they have the same offset. */
13544
13545static int
13546partial_die_eq (const void *item_lhs, const void *item_rhs)
13547{
13548 const struct partial_die_info *part_die_lhs = item_lhs;
13549 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 13550
72bf9492
DJ
13551 return part_die_lhs->offset == part_die_rhs->offset;
13552}
13553
ae038cb0
DJ
13554static struct cmd_list_element *set_dwarf2_cmdlist;
13555static struct cmd_list_element *show_dwarf2_cmdlist;
13556
13557static void
13558set_dwarf2_cmd (char *args, int from_tty)
13559{
13560 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
13561}
13562
13563static void
13564show_dwarf2_cmd (char *args, int from_tty)
6e70227d 13565{
ae038cb0
DJ
13566 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
13567}
13568
dce234bc
PP
13569/* If section described by INFO was mmapped, munmap it now. */
13570
13571static void
13572munmap_section_buffer (struct dwarf2_section_info *info)
13573{
13574 if (info->was_mmapped)
13575 {
13576#ifdef HAVE_MMAP
13577 intptr_t begin = (intptr_t) info->buffer;
13578 intptr_t map_begin = begin & ~(pagesize - 1);
13579 size_t map_length = info->size + begin - map_begin;
9a619af0 13580
dce234bc
PP
13581 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
13582#else
13583 /* Without HAVE_MMAP, we should never be here to begin with. */
13584 gdb_assert (0);
13585#endif
13586 }
13587}
13588
13589/* munmap debug sections for OBJFILE, if necessary. */
13590
13591static void
c1bd65d0 13592dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
13593{
13594 struct dwarf2_per_objfile *data = d;
9a619af0 13595
dce234bc
PP
13596 munmap_section_buffer (&data->info);
13597 munmap_section_buffer (&data->abbrev);
13598 munmap_section_buffer (&data->line);
13599 munmap_section_buffer (&data->str);
13600 munmap_section_buffer (&data->macinfo);
13601 munmap_section_buffer (&data->ranges);
13602 munmap_section_buffer (&data->loc);
13603 munmap_section_buffer (&data->frame);
13604 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
13605 munmap_section_buffer (&data->gdb_index);
13606}
13607
13608\f
13609
13610/* The contents of the hash table we create when building the string
13611 table. */
13612struct strtab_entry
13613{
13614 offset_type offset;
13615 const char *str;
13616};
13617
13618/* Hash function for a strtab_entry. */
13619static hashval_t
13620hash_strtab_entry (const void *e)
13621{
13622 const struct strtab_entry *entry = e;
13623 return mapped_index_string_hash (entry->str);
13624}
13625
13626/* Equality function for a strtab_entry. */
13627static int
13628eq_strtab_entry (const void *a, const void *b)
13629{
13630 const struct strtab_entry *ea = a;
13631 const struct strtab_entry *eb = b;
13632 return !strcmp (ea->str, eb->str);
13633}
13634
13635/* Create a strtab_entry hash table. */
13636static htab_t
13637create_strtab (void)
13638{
13639 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
13640 xfree, xcalloc, xfree);
13641}
13642
13643/* Add a string to the constant pool. Return the string's offset in
13644 host order. */
13645static offset_type
13646add_string (htab_t table, struct obstack *cpool, const char *str)
13647{
13648 void **slot;
13649 struct strtab_entry entry;
13650 struct strtab_entry *result;
13651
13652 entry.str = str;
13653 slot = htab_find_slot (table, &entry, INSERT);
13654 if (*slot)
13655 result = *slot;
13656 else
13657 {
13658 result = XNEW (struct strtab_entry);
13659 result->offset = obstack_object_size (cpool);
13660 result->str = str;
13661 obstack_grow_str0 (cpool, str);
13662 *slot = result;
13663 }
13664 return result->offset;
13665}
13666
13667/* An entry in the symbol table. */
13668struct symtab_index_entry
13669{
13670 /* The name of the symbol. */
13671 const char *name;
13672 /* The offset of the name in the constant pool. */
13673 offset_type index_offset;
13674 /* A sorted vector of the indices of all the CUs that hold an object
13675 of this name. */
13676 VEC (offset_type) *cu_indices;
13677};
13678
13679/* The symbol table. This is a power-of-2-sized hash table. */
13680struct mapped_symtab
13681{
13682 offset_type n_elements;
13683 offset_type size;
13684 struct symtab_index_entry **data;
13685};
13686
13687/* Hash function for a symtab_index_entry. */
13688static hashval_t
13689hash_symtab_entry (const void *e)
13690{
13691 const struct symtab_index_entry *entry = e;
13692 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
13693 sizeof (offset_type) * VEC_length (offset_type,
13694 entry->cu_indices),
13695 0);
13696}
13697
13698/* Equality function for a symtab_index_entry. */
13699static int
13700eq_symtab_entry (const void *a, const void *b)
13701{
13702 const struct symtab_index_entry *ea = a;
13703 const struct symtab_index_entry *eb = b;
13704 int len = VEC_length (offset_type, ea->cu_indices);
13705 if (len != VEC_length (offset_type, eb->cu_indices))
13706 return 0;
13707 return !memcmp (VEC_address (offset_type, ea->cu_indices),
13708 VEC_address (offset_type, eb->cu_indices),
13709 sizeof (offset_type) * len);
13710}
13711
13712/* Destroy a symtab_index_entry. */
13713static void
13714delete_symtab_entry (void *p)
13715{
13716 struct symtab_index_entry *entry = p;
13717 VEC_free (offset_type, entry->cu_indices);
13718 xfree (entry);
13719}
13720
13721/* Create a hash table holding symtab_index_entry objects. */
13722static htab_t
13723create_index_table (void)
13724{
13725 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
13726 delete_symtab_entry, xcalloc, xfree);
13727}
13728
13729/* Create a new mapped symtab object. */
13730static struct mapped_symtab *
13731create_mapped_symtab (void)
13732{
13733 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
13734 symtab->n_elements = 0;
13735 symtab->size = 1024;
13736 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13737 return symtab;
13738}
13739
13740/* Destroy a mapped_symtab. */
13741static void
13742cleanup_mapped_symtab (void *p)
13743{
13744 struct mapped_symtab *symtab = p;
13745 /* The contents of the array are freed when the other hash table is
13746 destroyed. */
13747 xfree (symtab->data);
13748 xfree (symtab);
13749}
13750
13751/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
13752 the slot. */
13753static struct symtab_index_entry **
13754find_slot (struct mapped_symtab *symtab, const char *name)
13755{
13756 offset_type index, step, hash = mapped_index_string_hash (name);
13757
13758 index = hash & (symtab->size - 1);
13759 step = ((hash * 17) & (symtab->size - 1)) | 1;
13760
13761 for (;;)
13762 {
13763 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
13764 return &symtab->data[index];
13765 index = (index + step) & (symtab->size - 1);
13766 }
13767}
13768
13769/* Expand SYMTAB's hash table. */
13770static void
13771hash_expand (struct mapped_symtab *symtab)
13772{
13773 offset_type old_size = symtab->size;
13774 offset_type i;
13775 struct symtab_index_entry **old_entries = symtab->data;
13776
13777 symtab->size *= 2;
13778 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13779
13780 for (i = 0; i < old_size; ++i)
13781 {
13782 if (old_entries[i])
13783 {
13784 struct symtab_index_entry **slot = find_slot (symtab,
13785 old_entries[i]->name);
13786 *slot = old_entries[i];
13787 }
13788 }
13789
13790 xfree (old_entries);
13791}
13792
13793/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
13794 is the index of the CU in which the symbol appears. */
13795static void
13796add_index_entry (struct mapped_symtab *symtab, const char *name,
13797 offset_type cu_index)
13798{
13799 struct symtab_index_entry **slot;
13800
13801 ++symtab->n_elements;
13802 if (4 * symtab->n_elements / 3 >= symtab->size)
13803 hash_expand (symtab);
13804
13805 slot = find_slot (symtab, name);
13806 if (!*slot)
13807 {
13808 *slot = XNEW (struct symtab_index_entry);
13809 (*slot)->name = name;
13810 (*slot)->cu_indices = NULL;
13811 }
13812 /* Don't push an index twice. Due to how we add entries we only
13813 have to check the last one. */
13814 if (VEC_empty (offset_type, (*slot)->cu_indices)
13815 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
13816 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
13817}
13818
13819/* Add a vector of indices to the constant pool. */
13820static offset_type
13821add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
13822 struct symtab_index_entry *entry)
13823{
13824 void **slot;
13825
13826 slot = htab_find_slot (index_table, entry, INSERT);
13827 if (!*slot)
13828 {
13829 offset_type len = VEC_length (offset_type, entry->cu_indices);
13830 offset_type val = MAYBE_SWAP (len);
13831 offset_type iter;
13832 int i;
13833
13834 *slot = entry;
13835 entry->index_offset = obstack_object_size (cpool);
13836
13837 obstack_grow (cpool, &val, sizeof (val));
13838 for (i = 0;
13839 VEC_iterate (offset_type, entry->cu_indices, i, iter);
13840 ++i)
13841 {
13842 val = MAYBE_SWAP (iter);
13843 obstack_grow (cpool, &val, sizeof (val));
13844 }
13845 }
13846 else
13847 {
13848 struct symtab_index_entry *old_entry = *slot;
13849 entry->index_offset = old_entry->index_offset;
13850 entry = old_entry;
13851 }
13852 return entry->index_offset;
13853}
13854
13855/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
13856 constant pool entries going into the obstack CPOOL. */
13857static void
13858write_hash_table (struct mapped_symtab *symtab,
13859 struct obstack *output, struct obstack *cpool)
13860{
13861 offset_type i;
13862 htab_t index_table;
13863 htab_t str_table;
13864
13865 index_table = create_index_table ();
13866 str_table = create_strtab ();
13867 /* We add all the index vectors to the constant pool first, to
13868 ensure alignment is ok. */
13869 for (i = 0; i < symtab->size; ++i)
13870 {
13871 if (symtab->data[i])
13872 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
13873 }
13874
13875 /* Now write out the hash table. */
13876 for (i = 0; i < symtab->size; ++i)
13877 {
13878 offset_type str_off, vec_off;
13879
13880 if (symtab->data[i])
13881 {
13882 str_off = add_string (str_table, cpool, symtab->data[i]->name);
13883 vec_off = symtab->data[i]->index_offset;
13884 }
13885 else
13886 {
13887 /* While 0 is a valid constant pool index, it is not valid
13888 to have 0 for both offsets. */
13889 str_off = 0;
13890 vec_off = 0;
13891 }
13892
13893 str_off = MAYBE_SWAP (str_off);
13894 vec_off = MAYBE_SWAP (vec_off);
13895
13896 obstack_grow (output, &str_off, sizeof (str_off));
13897 obstack_grow (output, &vec_off, sizeof (vec_off));
13898 }
13899
13900 htab_delete (str_table);
13901 htab_delete (index_table);
13902}
13903
13904/* Write an address entry to ADDR_OBSTACK. The addresses are taken
13905 from PST; CU_INDEX is the index of the CU in the vector of all
13906 CUs. */
13907static void
13908add_address_entry (struct objfile *objfile,
13909 struct obstack *addr_obstack, struct partial_symtab *pst,
13910 unsigned int cu_index)
13911{
13912 offset_type offset;
13913 char addr[8];
13914 CORE_ADDR baseaddr;
13915
13916 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13917
13918 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
13919 obstack_grow (addr_obstack, addr, 8);
13920 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
13921 obstack_grow (addr_obstack, addr, 8);
13922 offset = MAYBE_SWAP (cu_index);
13923 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
13924}
13925
13926/* Add a list of partial symbols to SYMTAB. */
13927static void
13928write_psymbols (struct mapped_symtab *symtab,
13929 struct partial_symbol **psymp,
13930 int count,
13931 offset_type cu_index)
13932{
13933 for (; count-- > 0; ++psymp)
13934 {
13935 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
13936 error (_("Ada is not currently supported by the index"));
13937 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
13938 }
13939}
13940
13941/* Write the contents of an ("unfinished") obstack to FILE. Throw an
13942 exception if there is an error. */
13943static void
13944write_obstack (FILE *file, struct obstack *obstack)
13945{
13946 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
13947 file)
13948 != obstack_object_size (obstack))
13949 error (_("couldn't data write to file"));
13950}
13951
13952/* Unlink a file if the argument is not NULL. */
13953static void
13954unlink_if_set (void *p)
13955{
13956 char **filename = p;
13957 if (*filename)
13958 unlink (*filename);
13959}
13960
13961/* Create an index file for OBJFILE in the directory DIR. */
13962static void
13963write_psymtabs_to_index (struct objfile *objfile, const char *dir)
13964{
13965 struct cleanup *cleanup;
13966 char *filename, *cleanup_filename;
13967 struct obstack contents, addr_obstack, constant_pool, symtab_obstack, cu_list;
13968 int i;
13969 FILE *out_file;
13970 struct mapped_symtab *symtab;
13971 offset_type val, size_of_contents, total_len;
13972 struct stat st;
13973 char buf[8];
13974
13975 if (!objfile->psymtabs)
13976 return;
13977 if (dwarf2_per_objfile->using_index)
13978 error (_("Cannot use an index to create the index"));
13979
13980 if (stat (objfile->name, &st) < 0)
13981 perror_with_name (_("Could not stat"));
13982
13983 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
13984 INDEX_SUFFIX, (char *) NULL);
13985 cleanup = make_cleanup (xfree, filename);
13986
13987 out_file = fopen (filename, "wb");
13988 if (!out_file)
13989 error (_("Can't open `%s' for writing"), filename);
13990
13991 cleanup_filename = filename;
13992 make_cleanup (unlink_if_set, &cleanup_filename);
13993
13994 symtab = create_mapped_symtab ();
13995 make_cleanup (cleanup_mapped_symtab, symtab);
13996
13997 obstack_init (&addr_obstack);
13998 make_cleanup_obstack_free (&addr_obstack);
13999
14000 obstack_init (&cu_list);
14001 make_cleanup_obstack_free (&cu_list);
14002
14003 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14004 {
14005 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14006 struct partial_symtab *psymtab = cu->v.psymtab;
14007 gdb_byte val[8];
14008
14009 write_psymbols (symtab,
14010 objfile->global_psymbols.list + psymtab->globals_offset,
14011 psymtab->n_global_syms, i);
14012 write_psymbols (symtab,
14013 objfile->static_psymbols.list + psymtab->statics_offset,
14014 psymtab->n_static_syms, i);
14015
14016 add_address_entry (objfile, &addr_obstack, psymtab, i);
14017
14018 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14019 obstack_grow (&cu_list, val, 8);
14020 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14021 obstack_grow (&cu_list, val, 8);
14022 }
14023
14024 obstack_init (&constant_pool);
14025 make_cleanup_obstack_free (&constant_pool);
14026 obstack_init (&symtab_obstack);
14027 make_cleanup_obstack_free (&symtab_obstack);
14028 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14029
14030 obstack_init (&contents);
14031 make_cleanup_obstack_free (&contents);
14032 size_of_contents = 5 * sizeof (offset_type);
14033 total_len = size_of_contents;
14034
14035 /* The version number. */
14036 val = MAYBE_SWAP (1);
14037 obstack_grow (&contents, &val, sizeof (val));
14038
14039 /* The offset of the CU list from the start of the file. */
14040 val = MAYBE_SWAP (total_len);
14041 obstack_grow (&contents, &val, sizeof (val));
14042 total_len += obstack_object_size (&cu_list);
14043
14044 /* The offset of the address table from the start of the file. */
14045 val = MAYBE_SWAP (total_len);
14046 obstack_grow (&contents, &val, sizeof (val));
14047 total_len += obstack_object_size (&addr_obstack);
14048
14049 /* The offset of the symbol table from the start of the file. */
14050 val = MAYBE_SWAP (total_len);
14051 obstack_grow (&contents, &val, sizeof (val));
14052 total_len += obstack_object_size (&symtab_obstack);
14053
14054 /* The offset of the constant pool from the start of the file. */
14055 val = MAYBE_SWAP (total_len);
14056 obstack_grow (&contents, &val, sizeof (val));
14057 total_len += obstack_object_size (&constant_pool);
14058
14059 gdb_assert (obstack_object_size (&contents) == size_of_contents);
14060
14061 write_obstack (out_file, &contents);
14062 write_obstack (out_file, &cu_list);
14063 write_obstack (out_file, &addr_obstack);
14064 write_obstack (out_file, &symtab_obstack);
14065 write_obstack (out_file, &constant_pool);
14066
14067 fclose (out_file);
14068
14069 /* We want to keep the file, so we set cleanup_filename to NULL
14070 here. See unlink_if_set. */
14071 cleanup_filename = NULL;
14072
14073 do_cleanups (cleanup);
14074}
14075
14076/* The mapped index file format is designed to be directly mmap()able
14077 on any architecture. In most cases, a datum is represented using a
14078 little-endian 32-bit integer value, called an offset_type. Big
14079 endian machines must byte-swap the values before using them.
14080 Exceptions to this rule are noted. The data is laid out such that
14081 alignment is always respected.
14082
14083 A mapped index consists of several sections.
14084
14085 1. The file header. This is a sequence of values, of offset_type
14086 unless otherwise noted:
14087 [0] The version number. Currently 1.
14088 [1] The offset, from the start of the file, of the CU list.
14089 [2] The offset, from the start of the file, of the address section.
14090 [3] The offset, from the start of the file, of the symbol table.
14091 [4] The offset, from the start of the file, of the constant pool.
14092
14093 2. The CU list. This is a sequence of pairs of 64-bit
14094 little-endian values. The first element in each pair is the offset
14095 of a CU in the .debug_info section. The second element in each
14096 pair is the length of that CU. References to a CU elsewhere in the
14097 map are done using a CU index, which is just the 0-based index into
14098 this table.
14099
14100 3. The address section. The address section consists of a sequence
14101 of address entries. Each address entry has three elements.
14102 [0] The low address. This is a 64-bit little-endian value.
14103 [1] The high address. This is a 64-bit little-endian value.
14104 [2] The CU index. This is an offset_type value.
14105
14106 4. The symbol table. This is a hash table. The size of the hash
14107 table is always a power of 2. The initial hash and the step are
14108 currently defined by the `find_slot' function.
14109
14110 Each slot in the hash table consists of a pair of offset_type
14111 values. The first value is the offset of the symbol's name in the
14112 constant pool. The second value is the offset of the CU vector in
14113 the constant pool.
14114
14115 If both values are 0, then this slot in the hash table is empty.
14116 This is ok because while 0 is a valid constant pool index, it
14117 cannot be a valid index for both a string and a CU vector.
14118
14119 A string in the constant pool is stored as a \0-terminated string,
14120 as you'd expect.
14121
14122 A CU vector in the constant pool is a sequence of offset_type
14123 values. The first value is the number of CU indices in the vector.
14124 Each subsequent value is the index of a CU in the CU list. This
14125 element in the hash table is used to indicate which CUs define the
14126 symbol.
14127
14128 5. The constant pool. This is simply a bunch of bytes. It is
14129 organized so that alignment is correct: CU vectors are stored
14130 first, followed by strings. */
14131static void
14132save_gdb_index_command (char *arg, int from_tty)
14133{
14134 struct objfile *objfile;
14135
14136 if (!arg || !*arg)
14137 error (_("usage: maintenance save-gdb-index DIRECTORY"));
14138
14139 ALL_OBJFILES (objfile)
14140 {
14141 struct stat st;
14142
14143 /* If the objfile does not correspond to an actual file, skip it. */
14144 if (stat (objfile->name, &st) < 0)
14145 continue;
14146
14147 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14148 if (dwarf2_per_objfile)
14149 {
14150 volatile struct gdb_exception except;
14151
14152 TRY_CATCH (except, RETURN_MASK_ERROR)
14153 {
14154 write_psymtabs_to_index (objfile, arg);
14155 }
14156 if (except.reason < 0)
14157 exception_fprintf (gdb_stderr, except,
14158 _("Error while writing index for `%s': "),
14159 objfile->name);
14160 }
14161 }
dce234bc
PP
14162}
14163
9291a0cd
TT
14164\f
14165
9eae7c52
TT
14166int dwarf2_always_disassemble;
14167
14168static void
14169show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14170 struct cmd_list_element *c, const char *value)
14171{
14172 fprintf_filtered (file, _("\
14173Whether to always disassemble DWARF expressions is %s.\n"),
14174 value);
14175}
14176
6502dd73
DJ
14177void _initialize_dwarf2_read (void);
14178
14179void
14180_initialize_dwarf2_read (void)
14181{
dce234bc 14182 dwarf2_objfile_data_key
c1bd65d0 14183 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 14184
1bedd215
AC
14185 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14186Set DWARF 2 specific variables.\n\
14187Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
14188 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14189 0/*allow-unknown*/, &maintenance_set_cmdlist);
14190
1bedd215
AC
14191 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14192Show DWARF 2 specific variables\n\
14193Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
14194 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14195 0/*allow-unknown*/, &maintenance_show_cmdlist);
14196
14197 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
14198 &dwarf2_max_cache_age, _("\
14199Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14200Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14201A higher limit means that cached compilation units will be stored\n\
14202in memory longer, and more total memory will be used. Zero disables\n\
14203caching, which can slow down startup."),
2c5b56ce 14204 NULL,
920d2a44 14205 show_dwarf2_max_cache_age,
2c5b56ce 14206 &set_dwarf2_cmdlist,
ae038cb0 14207 &show_dwarf2_cmdlist);
d97bc12b 14208
9eae7c52
TT
14209 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14210 &dwarf2_always_disassemble, _("\
14211Set whether `info address' always disassembles DWARF expressions."), _("\
14212Show whether `info address' always disassembles DWARF expressions."), _("\
14213When enabled, DWARF expressions are always printed in an assembly-like\n\
14214syntax. When disabled, expressions will be printed in a more\n\
14215conversational style, when possible."),
14216 NULL,
14217 show_dwarf2_always_disassemble,
14218 &set_dwarf2_cmdlist,
14219 &show_dwarf2_cmdlist);
14220
d97bc12b
DE
14221 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14222Set debugging of the dwarf2 DIE reader."), _("\
14223Show debugging of the dwarf2 DIE reader."), _("\
14224When enabled (non-zero), DIEs are dumped after they are read in.\n\
14225The value is the maximum depth to print."),
14226 NULL,
14227 NULL,
14228 &setdebuglist, &showdebuglist);
9291a0cd
TT
14229
14230 add_cmd ("gdb-index", class_files, save_gdb_index_command,
14231 _("Save a .gdb-index file"),
14232 &save_cmdlist);
6502dd73 14233}
This page took 1.836823 seconds and 4 git commands to generate.