Run dw2-compress-2 only for 32bit.
[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:
90e7c2c5
PM
4195 case DW_TAG_const_type:
4196 case DW_TAG_volatile_type:
134d01f1
DJ
4197 /* Add a typedef symbol for the type definition, if it has a
4198 DW_AT_name. */
f792889a 4199 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4200 break;
c906108c 4201 case DW_TAG_common_block:
e7c27a73 4202 read_common_block (die, cu);
c906108c
SS
4203 break;
4204 case DW_TAG_common_inclusion:
4205 break;
d9fa45fe 4206 case DW_TAG_namespace:
63d06c5c 4207 processing_has_namespace_info = 1;
e7c27a73 4208 read_namespace (die, cu);
d9fa45fe 4209 break;
5d7cb8df 4210 case DW_TAG_module:
f55ee35c 4211 processing_has_namespace_info = 1;
5d7cb8df
JK
4212 read_module (die, cu);
4213 break;
d9fa45fe
DC
4214 case DW_TAG_imported_declaration:
4215 case DW_TAG_imported_module:
63d06c5c 4216 processing_has_namespace_info = 1;
27aa8d6a
SW
4217 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4218 || cu->language != language_fortran))
4219 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4220 dwarf_tag_name (die->tag));
4221 read_import_statement (die, cu);
d9fa45fe 4222 break;
c906108c 4223 default:
e7c27a73 4224 new_symbol (die, NULL, cu);
c906108c
SS
4225 break;
4226 }
4227}
4228
94af9270
KS
4229/* A helper function for dwarf2_compute_name which determines whether DIE
4230 needs to have the name of the scope prepended to the name listed in the
4231 die. */
4232
4233static int
4234die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4235{
1c809c68
TT
4236 struct attribute *attr;
4237
94af9270
KS
4238 switch (die->tag)
4239 {
4240 case DW_TAG_namespace:
4241 case DW_TAG_typedef:
4242 case DW_TAG_class_type:
4243 case DW_TAG_interface_type:
4244 case DW_TAG_structure_type:
4245 case DW_TAG_union_type:
4246 case DW_TAG_enumeration_type:
4247 case DW_TAG_enumerator:
4248 case DW_TAG_subprogram:
4249 case DW_TAG_member:
4250 return 1;
4251
4252 case DW_TAG_variable:
4253 /* We only need to prefix "globally" visible variables. These include
4254 any variable marked with DW_AT_external or any variable that
4255 lives in a namespace. [Variables in anonymous namespaces
4256 require prefixing, but they are not DW_AT_external.] */
4257
4258 if (dwarf2_attr (die, DW_AT_specification, cu))
4259 {
4260 struct dwarf2_cu *spec_cu = cu;
9a619af0 4261
94af9270
KS
4262 return die_needs_namespace (die_specification (die, &spec_cu),
4263 spec_cu);
4264 }
4265
1c809c68 4266 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4267 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4268 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4269 return 0;
4270 /* A variable in a lexical block of some kind does not need a
4271 namespace, even though in C++ such variables may be external
4272 and have a mangled name. */
4273 if (die->parent->tag == DW_TAG_lexical_block
4274 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4275 || die->parent->tag == DW_TAG_catch_block
4276 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4277 return 0;
4278 return 1;
94af9270
KS
4279
4280 default:
4281 return 0;
4282 }
4283}
4284
4285/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4286 compute the physname for the object, which include a method's
4287 formal parameters (C++/Java) and return type (Java).
4288
af6b7be1
JB
4289 For Ada, return the DIE's linkage name rather than the fully qualified
4290 name. PHYSNAME is ignored..
4291
94af9270
KS
4292 The result is allocated on the objfile_obstack and canonicalized. */
4293
4294static const char *
4295dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4296 int physname)
4297{
4298 if (name == NULL)
4299 name = dwarf2_name (die, cu);
4300
f55ee35c
JK
4301 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4302 compute it by typename_concat inside GDB. */
4303 if (cu->language == language_ada
4304 || (cu->language == language_fortran && physname))
4305 {
4306 /* For Ada unit, we prefer the linkage name over the name, as
4307 the former contains the exported name, which the user expects
4308 to be able to reference. Ideally, we want the user to be able
4309 to reference this entity using either natural or linkage name,
4310 but we haven't started looking at this enhancement yet. */
4311 struct attribute *attr;
4312
4313 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4314 if (attr == NULL)
4315 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4316 if (attr && DW_STRING (attr))
4317 return DW_STRING (attr);
4318 }
4319
94af9270
KS
4320 /* These are the only languages we know how to qualify names in. */
4321 if (name != NULL
f55ee35c
JK
4322 && (cu->language == language_cplus || cu->language == language_java
4323 || cu->language == language_fortran))
94af9270
KS
4324 {
4325 if (die_needs_namespace (die, cu))
4326 {
4327 long length;
4328 char *prefix;
4329 struct ui_file *buf;
4330
4331 prefix = determine_prefix (die, cu);
4332 buf = mem_fileopen ();
4333 if (*prefix != '\0')
4334 {
f55ee35c
JK
4335 char *prefixed_name = typename_concat (NULL, prefix, name,
4336 physname, cu);
9a619af0 4337
94af9270
KS
4338 fputs_unfiltered (prefixed_name, buf);
4339 xfree (prefixed_name);
4340 }
4341 else
4342 fputs_unfiltered (name ? name : "", buf);
4343
4344 /* For Java and C++ methods, append formal parameter type
4345 information, if PHYSNAME. */
6e70227d 4346
94af9270
KS
4347 if (physname && die->tag == DW_TAG_subprogram
4348 && (cu->language == language_cplus
4349 || cu->language == language_java))
4350 {
4351 struct type *type = read_type_die (die, cu);
4352
4353 c_type_print_args (type, buf, 0, cu->language);
4354
4355 if (cu->language == language_java)
4356 {
4357 /* For java, we must append the return type to method
4358 names. */
4359 if (die->tag == DW_TAG_subprogram)
4360 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4361 0, 0);
4362 }
4363 else if (cu->language == language_cplus)
4364 {
4365 if (TYPE_NFIELDS (type) > 0
4366 && TYPE_FIELD_ARTIFICIAL (type, 0)
4367 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4368 fputs_unfiltered (" const", buf);
4369 }
4370 }
4371
4372 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4373 &length);
4374 ui_file_delete (buf);
4375
4376 if (cu->language == language_cplus)
4377 {
4378 char *cname
4379 = dwarf2_canonicalize_name (name, cu,
4380 &cu->objfile->objfile_obstack);
9a619af0 4381
94af9270
KS
4382 if (cname != NULL)
4383 name = cname;
4384 }
4385 }
4386 }
4387
4388 return name;
4389}
4390
0114d602
DJ
4391/* Return the fully qualified name of DIE, based on its DW_AT_name.
4392 If scope qualifiers are appropriate they will be added. The result
4393 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4394 not have a name. NAME may either be from a previous call to
4395 dwarf2_name or NULL.
4396
4397 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4398
4399static const char *
94af9270 4400dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4401{
94af9270
KS
4402 return dwarf2_compute_name (name, die, cu, 0);
4403}
0114d602 4404
94af9270
KS
4405/* Construct a physname for the given DIE in CU. NAME may either be
4406 from a previous call to dwarf2_name or NULL. The result will be
4407 allocated on the objfile_objstack or NULL if the DIE does not have a
4408 name.
0114d602 4409
94af9270 4410 The output string will be canonicalized (if C++/Java). */
0114d602 4411
94af9270
KS
4412static const char *
4413dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4414{
4415 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4416}
4417
27aa8d6a
SW
4418/* Read the import statement specified by the given die and record it. */
4419
4420static void
4421read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4422{
4423 struct attribute *import_attr;
4424 struct die_info *imported_die;
de4affc9 4425 struct dwarf2_cu *imported_cu;
27aa8d6a 4426 const char *imported_name;
794684b6 4427 const char *imported_name_prefix;
13387711
SW
4428 const char *canonical_name;
4429 const char *import_alias;
4430 const char *imported_declaration = NULL;
794684b6 4431 const char *import_prefix;
13387711
SW
4432
4433 char *temp;
27aa8d6a
SW
4434
4435 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4436 if (import_attr == NULL)
4437 {
4438 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4439 dwarf_tag_name (die->tag));
4440 return;
4441 }
4442
de4affc9
CC
4443 imported_cu = cu;
4444 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4445 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4446 if (imported_name == NULL)
4447 {
4448 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4449
4450 The import in the following code:
4451 namespace A
4452 {
4453 typedef int B;
4454 }
4455
4456 int main ()
4457 {
4458 using A::B;
4459 B b;
4460 return b;
4461 }
4462
4463 ...
4464 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4465 <52> DW_AT_decl_file : 1
4466 <53> DW_AT_decl_line : 6
4467 <54> DW_AT_import : <0x75>
4468 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4469 <59> DW_AT_name : B
4470 <5b> DW_AT_decl_file : 1
4471 <5c> DW_AT_decl_line : 2
4472 <5d> DW_AT_type : <0x6e>
4473 ...
4474 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4475 <76> DW_AT_byte_size : 4
4476 <77> DW_AT_encoding : 5 (signed)
4477
4478 imports the wrong die ( 0x75 instead of 0x58 ).
4479 This case will be ignored until the gcc bug is fixed. */
4480 return;
4481 }
4482
82856980
SW
4483 /* Figure out the local name after import. */
4484 import_alias = dwarf2_name (die, cu);
27aa8d6a 4485
794684b6
SW
4486 /* Figure out where the statement is being imported to. */
4487 import_prefix = determine_prefix (die, cu);
4488
4489 /* Figure out what the scope of the imported die is and prepend it
4490 to the name of the imported die. */
de4affc9 4491 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4492
f55ee35c
JK
4493 if (imported_die->tag != DW_TAG_namespace
4494 && imported_die->tag != DW_TAG_module)
794684b6 4495 {
13387711
SW
4496 imported_declaration = imported_name;
4497 canonical_name = imported_name_prefix;
794684b6 4498 }
13387711 4499 else if (strlen (imported_name_prefix) > 0)
794684b6 4500 {
13387711
SW
4501 temp = alloca (strlen (imported_name_prefix)
4502 + 2 + strlen (imported_name) + 1);
4503 strcpy (temp, imported_name_prefix);
4504 strcat (temp, "::");
4505 strcat (temp, imported_name);
4506 canonical_name = temp;
794684b6 4507 }
13387711
SW
4508 else
4509 canonical_name = imported_name;
794684b6 4510
c0cc3a76
SW
4511 cp_add_using_directive (import_prefix,
4512 canonical_name,
4513 import_alias,
13387711 4514 imported_declaration,
c0cc3a76 4515 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4516}
4517
5fb290d7 4518static void
e142c38c 4519initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4520{
e142c38c 4521 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4522}
4523
cb1df416
DJ
4524static void
4525free_cu_line_header (void *arg)
4526{
4527 struct dwarf2_cu *cu = arg;
4528
4529 free_line_header (cu->line_header);
4530 cu->line_header = NULL;
4531}
4532
9291a0cd
TT
4533static void
4534find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4535 char **name, char **comp_dir)
4536{
4537 struct attribute *attr;
4538
4539 *name = NULL;
4540 *comp_dir = NULL;
4541
4542 /* Find the filename. Do not use dwarf2_name here, since the filename
4543 is not a source language identifier. */
4544 attr = dwarf2_attr (die, DW_AT_name, cu);
4545 if (attr)
4546 {
4547 *name = DW_STRING (attr);
4548 }
4549
4550 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4551 if (attr)
4552 *comp_dir = DW_STRING (attr);
4553 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4554 {
4555 *comp_dir = ldirname (*name);
4556 if (*comp_dir != NULL)
4557 make_cleanup (xfree, *comp_dir);
4558 }
4559 if (*comp_dir != NULL)
4560 {
4561 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4562 directory, get rid of it. */
4563 char *cp = strchr (*comp_dir, ':');
4564
4565 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4566 *comp_dir = cp + 1;
4567 }
4568
4569 if (*name == NULL)
4570 *name = "<unknown>";
4571}
4572
c906108c 4573static void
e7c27a73 4574read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4575{
e7c27a73 4576 struct objfile *objfile = cu->objfile;
debd256d 4577 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 4578 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
4579 CORE_ADDR highpc = ((CORE_ADDR) 0);
4580 struct attribute *attr;
e1024ff1 4581 char *name = NULL;
c906108c
SS
4582 char *comp_dir = NULL;
4583 struct die_info *child_die;
4584 bfd *abfd = objfile->obfd;
debd256d 4585 struct line_header *line_header = 0;
e142c38c 4586 CORE_ADDR baseaddr;
6e70227d 4587
e142c38c 4588 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4589
fae299cd 4590 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
4591
4592 /* If we didn't find a lowpc, set it to highpc to avoid complaints
4593 from finish_block. */
2acceee2 4594 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
4595 lowpc = highpc;
4596 lowpc += baseaddr;
4597 highpc += baseaddr;
4598
9291a0cd 4599 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 4600
e142c38c 4601 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
4602 if (attr)
4603 {
e142c38c 4604 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
4605 }
4606
b0f35d58 4607 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 4608 if (attr)
b0f35d58 4609 cu->producer = DW_STRING (attr);
303b6f5d 4610
c906108c
SS
4611 /* We assume that we're processing GCC output. */
4612 processing_gcc_compilation = 2;
c906108c 4613
df8a16a1
DJ
4614 processing_has_namespace_info = 0;
4615
c906108c
SS
4616 start_symtab (name, comp_dir, lowpc);
4617 record_debugformat ("DWARF 2");
303b6f5d 4618 record_producer (cu->producer);
c906108c 4619
e142c38c 4620 initialize_cu_func_list (cu);
c906108c 4621
cb1df416
DJ
4622 /* Decode line number information if present. We do this before
4623 processing child DIEs, so that the line header table is available
4624 for DW_AT_decl_file. */
e142c38c 4625 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
4626 if (attr)
4627 {
debd256d 4628 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 4629 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
4630 if (line_header)
4631 {
cb1df416
DJ
4632 cu->line_header = line_header;
4633 make_cleanup (free_cu_line_header, cu);
aaa75496 4634 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 4635 }
5fb290d7 4636 }
debd256d 4637
cb1df416
DJ
4638 /* Process all dies in compilation unit. */
4639 if (die->child != NULL)
4640 {
4641 child_die = die->child;
4642 while (child_die && child_die->tag)
4643 {
4644 process_die (child_die, cu);
4645 child_die = sibling_die (child_die);
4646 }
4647 }
4648
2e276125
JB
4649 /* Decode macro information, if present. Dwarf 2 macro information
4650 refers to information in the line number info statement program
4651 header, so we can only read it if we've read the header
4652 successfully. */
e142c38c 4653 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 4654 if (attr && line_header)
2e276125
JB
4655 {
4656 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 4657
2e276125 4658 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 4659 comp_dir, abfd, cu);
2e276125 4660 }
debd256d 4661 do_cleanups (back_to);
5fb290d7
DJ
4662}
4663
348e048f
DE
4664/* For TUs we want to skip the first top level sibling if it's not the
4665 actual type being defined by this TU. In this case the first top
4666 level sibling is there to provide context only. */
4667
4668static void
4669read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
4670{
4671 struct objfile *objfile = cu->objfile;
4672 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4673 CORE_ADDR lowpc;
4674 struct attribute *attr;
4675 char *name = NULL;
4676 char *comp_dir = NULL;
4677 struct die_info *child_die;
4678 bfd *abfd = objfile->obfd;
348e048f
DE
4679
4680 /* start_symtab needs a low pc, but we don't really have one.
4681 Do what read_file_scope would do in the absence of such info. */
4682 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4683
4684 /* Find the filename. Do not use dwarf2_name here, since the filename
4685 is not a source language identifier. */
4686 attr = dwarf2_attr (die, DW_AT_name, cu);
4687 if (attr)
4688 name = DW_STRING (attr);
4689
4690 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4691 if (attr)
4692 comp_dir = DW_STRING (attr);
4693 else if (name != NULL && IS_ABSOLUTE_PATH (name))
4694 {
4695 comp_dir = ldirname (name);
4696 if (comp_dir != NULL)
4697 make_cleanup (xfree, comp_dir);
4698 }
4699
4700 if (name == NULL)
4701 name = "<unknown>";
4702
4703 attr = dwarf2_attr (die, DW_AT_language, cu);
4704 if (attr)
4705 set_cu_language (DW_UNSND (attr), cu);
4706
4707 /* This isn't technically needed today. It is done for symmetry
4708 with read_file_scope. */
4709 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 4710 if (attr)
348e048f
DE
4711 cu->producer = DW_STRING (attr);
4712
4713 /* We assume that we're processing GCC output. */
4714 processing_gcc_compilation = 2;
4715
4716 processing_has_namespace_info = 0;
4717
4718 start_symtab (name, comp_dir, lowpc);
4719 record_debugformat ("DWARF 2");
4720 record_producer (cu->producer);
4721
4722 /* Process the dies in the type unit. */
4723 if (die->child == NULL)
4724 {
4725 dump_die_for_error (die);
4726 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
4727 bfd_get_filename (abfd));
4728 }
4729
4730 child_die = die->child;
4731
4732 while (child_die && child_die->tag)
4733 {
4734 process_die (child_die, cu);
4735
4736 child_die = sibling_die (child_die);
4737 }
4738
4739 do_cleanups (back_to);
4740}
4741
5fb290d7 4742static void
e142c38c
DJ
4743add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
4744 struct dwarf2_cu *cu)
5fb290d7
DJ
4745{
4746 struct function_range *thisfn;
4747
4748 thisfn = (struct function_range *)
7b5a2f43 4749 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
4750 thisfn->name = name;
4751 thisfn->lowpc = lowpc;
4752 thisfn->highpc = highpc;
4753 thisfn->seen_line = 0;
4754 thisfn->next = NULL;
4755
e142c38c
DJ
4756 if (cu->last_fn == NULL)
4757 cu->first_fn = thisfn;
5fb290d7 4758 else
e142c38c 4759 cu->last_fn->next = thisfn;
5fb290d7 4760
e142c38c 4761 cu->last_fn = thisfn;
c906108c
SS
4762}
4763
d389af10
JK
4764/* qsort helper for inherit_abstract_dies. */
4765
4766static int
4767unsigned_int_compar (const void *ap, const void *bp)
4768{
4769 unsigned int a = *(unsigned int *) ap;
4770 unsigned int b = *(unsigned int *) bp;
4771
4772 return (a > b) - (b > a);
4773}
4774
4775/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
4776 Inherit only the children of the DW_AT_abstract_origin DIE not being already
4777 referenced by DW_AT_abstract_origin from the children of the current DIE. */
4778
4779static void
4780inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
4781{
4782 struct die_info *child_die;
4783 unsigned die_children_count;
4784 /* CU offsets which were referenced by children of the current DIE. */
4785 unsigned *offsets;
4786 unsigned *offsets_end, *offsetp;
4787 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
4788 struct die_info *origin_die;
4789 /* Iterator of the ORIGIN_DIE children. */
4790 struct die_info *origin_child_die;
4791 struct cleanup *cleanups;
4792 struct attribute *attr;
4793
4794 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
4795 if (!attr)
4796 return;
4797
4798 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
4799 if (die->tag != origin_die->tag
4800 && !(die->tag == DW_TAG_inlined_subroutine
4801 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
4802 complaint (&symfile_complaints,
4803 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
4804 die->offset, origin_die->offset);
4805
4806 child_die = die->child;
4807 die_children_count = 0;
4808 while (child_die && child_die->tag)
4809 {
4810 child_die = sibling_die (child_die);
4811 die_children_count++;
4812 }
4813 offsets = xmalloc (sizeof (*offsets) * die_children_count);
4814 cleanups = make_cleanup (xfree, offsets);
4815
4816 offsets_end = offsets;
4817 child_die = die->child;
4818 while (child_die && child_die->tag)
4819 {
c38f313d
DJ
4820 /* For each CHILD_DIE, find the corresponding child of
4821 ORIGIN_DIE. If there is more than one layer of
4822 DW_AT_abstract_origin, follow them all; there shouldn't be,
4823 but GCC versions at least through 4.4 generate this (GCC PR
4824 40573). */
4825 struct die_info *child_origin_die = child_die;
9a619af0 4826
c38f313d
DJ
4827 while (1)
4828 {
4829 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
4830 if (attr == NULL)
4831 break;
4832 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
4833 }
4834
d389af10
JK
4835 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
4836 counterpart may exist. */
c38f313d 4837 if (child_origin_die != child_die)
d389af10 4838 {
edb3359d
DJ
4839 if (child_die->tag != child_origin_die->tag
4840 && !(child_die->tag == DW_TAG_inlined_subroutine
4841 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
4842 complaint (&symfile_complaints,
4843 _("Child DIE 0x%x and its abstract origin 0x%x have "
4844 "different tags"), child_die->offset,
4845 child_origin_die->offset);
c38f313d
DJ
4846 if (child_origin_die->parent != origin_die)
4847 complaint (&symfile_complaints,
4848 _("Child DIE 0x%x and its abstract origin 0x%x have "
4849 "different parents"), child_die->offset,
4850 child_origin_die->offset);
4851 else
4852 *offsets_end++ = child_origin_die->offset;
d389af10
JK
4853 }
4854 child_die = sibling_die (child_die);
4855 }
4856 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
4857 unsigned_int_compar);
4858 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
4859 if (offsetp[-1] == *offsetp)
4860 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
4861 "to DIE 0x%x as their abstract origin"),
4862 die->offset, *offsetp);
4863
4864 offsetp = offsets;
4865 origin_child_die = origin_die->child;
4866 while (origin_child_die && origin_child_die->tag)
4867 {
4868 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
4869 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
4870 offsetp++;
4871 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
4872 {
4873 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
4874 process_die (origin_child_die, cu);
4875 }
4876 origin_child_die = sibling_die (origin_child_die);
4877 }
4878
4879 do_cleanups (cleanups);
4880}
4881
c906108c 4882static void
e7c27a73 4883read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4884{
e7c27a73 4885 struct objfile *objfile = cu->objfile;
52f0bd74 4886 struct context_stack *new;
c906108c
SS
4887 CORE_ADDR lowpc;
4888 CORE_ADDR highpc;
4889 struct die_info *child_die;
edb3359d 4890 struct attribute *attr, *call_line, *call_file;
c906108c 4891 char *name;
e142c38c 4892 CORE_ADDR baseaddr;
801e3a5b 4893 struct block *block;
edb3359d
DJ
4894 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
4895
4896 if (inlined_func)
4897 {
4898 /* If we do not have call site information, we can't show the
4899 caller of this inlined function. That's too confusing, so
4900 only use the scope for local variables. */
4901 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
4902 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
4903 if (call_line == NULL || call_file == NULL)
4904 {
4905 read_lexical_block_scope (die, cu);
4906 return;
4907 }
4908 }
c906108c 4909
e142c38c
DJ
4910 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4911
94af9270 4912 name = dwarf2_name (die, cu);
c906108c 4913
e8d05480
JB
4914 /* Ignore functions with missing or empty names. These are actually
4915 illegal according to the DWARF standard. */
4916 if (name == NULL)
4917 {
4918 complaint (&symfile_complaints,
4919 _("missing name for subprogram DIE at %d"), die->offset);
4920 return;
4921 }
4922
4923 /* Ignore functions with missing or invalid low and high pc attributes. */
4924 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
4925 {
ae4d0c03
PM
4926 attr = dwarf2_attr (die, DW_AT_external, cu);
4927 if (!attr || !DW_UNSND (attr))
4928 complaint (&symfile_complaints,
4929 _("cannot get low and high bounds for subprogram DIE at %d"),
4930 die->offset);
e8d05480
JB
4931 return;
4932 }
c906108c
SS
4933
4934 lowpc += baseaddr;
4935 highpc += baseaddr;
4936
5fb290d7 4937 /* Record the function range for dwarf_decode_lines. */
e142c38c 4938 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 4939
c906108c 4940 new = push_context (0, lowpc);
f792889a 4941 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 4942
4cecd739
DJ
4943 /* If there is a location expression for DW_AT_frame_base, record
4944 it. */
e142c38c 4945 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 4946 if (attr)
c034e007
AC
4947 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
4948 expression is being recorded directly in the function's symbol
4949 and not in a separate frame-base object. I guess this hack is
4950 to avoid adding some sort of frame-base adjunct/annex to the
4951 function's symbol :-(. The problem with doing this is that it
4952 results in a function symbol with a location expression that
4953 has nothing to do with the location of the function, ouch! The
4954 relationship should be: a function's symbol has-a frame base; a
4955 frame-base has-a location expression. */
e7c27a73 4956 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 4957
e142c38c 4958 cu->list_in_scope = &local_symbols;
c906108c 4959
639d11d3 4960 if (die->child != NULL)
c906108c 4961 {
639d11d3 4962 child_die = die->child;
c906108c
SS
4963 while (child_die && child_die->tag)
4964 {
e7c27a73 4965 process_die (child_die, cu);
c906108c
SS
4966 child_die = sibling_die (child_die);
4967 }
4968 }
4969
d389af10
JK
4970 inherit_abstract_dies (die, cu);
4971
4a811a97
UW
4972 /* If we have a DW_AT_specification, we might need to import using
4973 directives from the context of the specification DIE. See the
4974 comment in determine_prefix. */
4975 if (cu->language == language_cplus
4976 && dwarf2_attr (die, DW_AT_specification, cu))
4977 {
4978 struct dwarf2_cu *spec_cu = cu;
4979 struct die_info *spec_die = die_specification (die, &spec_cu);
4980
4981 while (spec_die)
4982 {
4983 child_die = spec_die->child;
4984 while (child_die && child_die->tag)
4985 {
4986 if (child_die->tag == DW_TAG_imported_module)
4987 process_die (child_die, spec_cu);
4988 child_die = sibling_die (child_die);
4989 }
4990
4991 /* In some cases, GCC generates specification DIEs that
4992 themselves contain DW_AT_specification attributes. */
4993 spec_die = die_specification (spec_die, &spec_cu);
4994 }
4995 }
4996
c906108c
SS
4997 new = pop_context ();
4998 /* Make a block for the local symbols within. */
801e3a5b
JB
4999 block = finish_block (new->name, &local_symbols, new->old_blocks,
5000 lowpc, highpc, objfile);
5001
df8a16a1 5002 /* For C++, set the block's scope. */
f55ee35c 5003 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5004 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5005 determine_prefix (die, cu),
df8a16a1
DJ
5006 processing_has_namespace_info);
5007
801e3a5b
JB
5008 /* If we have address ranges, record them. */
5009 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5010
208d8187
JB
5011 /* In C++, we can have functions nested inside functions (e.g., when
5012 a function declares a class that has methods). This means that
5013 when we finish processing a function scope, we may need to go
5014 back to building a containing block's symbol lists. */
5015 local_symbols = new->locals;
5016 param_symbols = new->params;
27aa8d6a 5017 using_directives = new->using_directives;
208d8187 5018
921e78cf
JB
5019 /* If we've finished processing a top-level function, subsequent
5020 symbols go in the file symbol list. */
5021 if (outermost_context_p ())
e142c38c 5022 cu->list_in_scope = &file_symbols;
c906108c
SS
5023}
5024
5025/* Process all the DIES contained within a lexical block scope. Start
5026 a new scope, process the dies, and then close the scope. */
5027
5028static void
e7c27a73 5029read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5030{
e7c27a73 5031 struct objfile *objfile = cu->objfile;
52f0bd74 5032 struct context_stack *new;
c906108c
SS
5033 CORE_ADDR lowpc, highpc;
5034 struct die_info *child_die;
e142c38c
DJ
5035 CORE_ADDR baseaddr;
5036
5037 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5038
5039 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5040 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5041 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5042 be nasty. Might be easier to properly extend generic blocks to
af34e669 5043 describe ranges. */
d85a05f0 5044 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5045 return;
5046 lowpc += baseaddr;
5047 highpc += baseaddr;
5048
5049 push_context (0, lowpc);
639d11d3 5050 if (die->child != NULL)
c906108c 5051 {
639d11d3 5052 child_die = die->child;
c906108c
SS
5053 while (child_die && child_die->tag)
5054 {
e7c27a73 5055 process_die (child_die, cu);
c906108c
SS
5056 child_die = sibling_die (child_die);
5057 }
5058 }
5059 new = pop_context ();
5060
8540c487 5061 if (local_symbols != NULL || using_directives != NULL)
c906108c 5062 {
801e3a5b
JB
5063 struct block *block
5064 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5065 highpc, objfile);
5066
5067 /* Note that recording ranges after traversing children, as we
5068 do here, means that recording a parent's ranges entails
5069 walking across all its children's ranges as they appear in
5070 the address map, which is quadratic behavior.
5071
5072 It would be nicer to record the parent's ranges before
5073 traversing its children, simply overriding whatever you find
5074 there. But since we don't even decide whether to create a
5075 block until after we've traversed its children, that's hard
5076 to do. */
5077 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5078 }
5079 local_symbols = new->locals;
27aa8d6a 5080 using_directives = new->using_directives;
c906108c
SS
5081}
5082
43039443 5083/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5084 Return 1 if the attributes are present and valid, otherwise, return 0.
5085 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5086
5087static int
5088dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5089 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5090 struct partial_symtab *ranges_pst)
43039443
JK
5091{
5092 struct objfile *objfile = cu->objfile;
5093 struct comp_unit_head *cu_header = &cu->header;
5094 bfd *obfd = objfile->obfd;
5095 unsigned int addr_size = cu_header->addr_size;
5096 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5097 /* Base address selection entry. */
5098 CORE_ADDR base;
5099 int found_base;
5100 unsigned int dummy;
5101 gdb_byte *buffer;
5102 CORE_ADDR marker;
5103 int low_set;
5104 CORE_ADDR low = 0;
5105 CORE_ADDR high = 0;
ff013f42 5106 CORE_ADDR baseaddr;
43039443 5107
d00adf39
DE
5108 found_base = cu->base_known;
5109 base = cu->base_address;
43039443 5110
be391dca 5111 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5112 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5113 {
5114 complaint (&symfile_complaints,
5115 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5116 offset);
5117 return 0;
5118 }
dce234bc 5119 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5120
5121 /* Read in the largest possible address. */
5122 marker = read_address (obfd, buffer, cu, &dummy);
5123 if ((marker & mask) == mask)
5124 {
5125 /* If we found the largest possible address, then
5126 read the base address. */
5127 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5128 buffer += 2 * addr_size;
5129 offset += 2 * addr_size;
5130 found_base = 1;
5131 }
5132
5133 low_set = 0;
5134
e7030f15 5135 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5136
43039443
JK
5137 while (1)
5138 {
5139 CORE_ADDR range_beginning, range_end;
5140
5141 range_beginning = read_address (obfd, buffer, cu, &dummy);
5142 buffer += addr_size;
5143 range_end = read_address (obfd, buffer, cu, &dummy);
5144 buffer += addr_size;
5145 offset += 2 * addr_size;
5146
5147 /* An end of list marker is a pair of zero addresses. */
5148 if (range_beginning == 0 && range_end == 0)
5149 /* Found the end of list entry. */
5150 break;
5151
5152 /* Each base address selection entry is a pair of 2 values.
5153 The first is the largest possible address, the second is
5154 the base address. Check for a base address here. */
5155 if ((range_beginning & mask) == mask)
5156 {
5157 /* If we found the largest possible address, then
5158 read the base address. */
5159 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5160 found_base = 1;
5161 continue;
5162 }
5163
5164 if (!found_base)
5165 {
5166 /* We have no valid base address for the ranges
5167 data. */
5168 complaint (&symfile_complaints,
5169 _("Invalid .debug_ranges data (no base address)"));
5170 return 0;
5171 }
5172
5173 range_beginning += base;
5174 range_end += base;
5175
ff013f42
JK
5176 if (ranges_pst != NULL && range_beginning < range_end)
5177 addrmap_set_empty (objfile->psymtabs_addrmap,
5178 range_beginning + baseaddr, range_end - 1 + baseaddr,
5179 ranges_pst);
5180
43039443
JK
5181 /* FIXME: This is recording everything as a low-high
5182 segment of consecutive addresses. We should have a
5183 data structure for discontiguous block ranges
5184 instead. */
5185 if (! low_set)
5186 {
5187 low = range_beginning;
5188 high = range_end;
5189 low_set = 1;
5190 }
5191 else
5192 {
5193 if (range_beginning < low)
5194 low = range_beginning;
5195 if (range_end > high)
5196 high = range_end;
5197 }
5198 }
5199
5200 if (! low_set)
5201 /* If the first entry is an end-of-list marker, the range
5202 describes an empty scope, i.e. no instructions. */
5203 return 0;
5204
5205 if (low_return)
5206 *low_return = low;
5207 if (high_return)
5208 *high_return = high;
5209 return 1;
5210}
5211
af34e669
DJ
5212/* Get low and high pc attributes from a die. Return 1 if the attributes
5213 are present and valid, otherwise, return 0. Return -1 if the range is
5214 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5215static int
af34e669 5216dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5217 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5218 struct partial_symtab *pst)
c906108c
SS
5219{
5220 struct attribute *attr;
af34e669
DJ
5221 CORE_ADDR low = 0;
5222 CORE_ADDR high = 0;
5223 int ret = 0;
c906108c 5224
e142c38c 5225 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5226 if (attr)
af34e669
DJ
5227 {
5228 high = DW_ADDR (attr);
e142c38c 5229 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5230 if (attr)
5231 low = DW_ADDR (attr);
5232 else
5233 /* Found high w/o low attribute. */
5234 return 0;
5235
5236 /* Found consecutive range of addresses. */
5237 ret = 1;
5238 }
c906108c 5239 else
af34e669 5240 {
e142c38c 5241 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5242 if (attr != NULL)
5243 {
af34e669 5244 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5245 .debug_ranges section. */
d85a05f0 5246 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5247 return 0;
43039443 5248 /* Found discontinuous range of addresses. */
af34e669
DJ
5249 ret = -1;
5250 }
5251 }
c906108c
SS
5252
5253 if (high < low)
5254 return 0;
5255
5256 /* When using the GNU linker, .gnu.linkonce. sections are used to
5257 eliminate duplicate copies of functions and vtables and such.
5258 The linker will arbitrarily choose one and discard the others.
5259 The AT_*_pc values for such functions refer to local labels in
5260 these sections. If the section from that file was discarded, the
5261 labels are not in the output, so the relocs get a value of 0.
5262 If this is a discarded function, mark the pc bounds as invalid,
5263 so that GDB will ignore it. */
72dca2f5 5264 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5265 return 0;
5266
5267 *lowpc = low;
5268 *highpc = high;
af34e669 5269 return ret;
c906108c
SS
5270}
5271
b084d499
JB
5272/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5273 its low and high PC addresses. Do nothing if these addresses could not
5274 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5275 and HIGHPC to the high address if greater than HIGHPC. */
5276
5277static void
5278dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5279 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5280 struct dwarf2_cu *cu)
5281{
5282 CORE_ADDR low, high;
5283 struct die_info *child = die->child;
5284
d85a05f0 5285 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5286 {
5287 *lowpc = min (*lowpc, low);
5288 *highpc = max (*highpc, high);
5289 }
5290
5291 /* If the language does not allow nested subprograms (either inside
5292 subprograms or lexical blocks), we're done. */
5293 if (cu->language != language_ada)
5294 return;
6e70227d 5295
b084d499
JB
5296 /* Check all the children of the given DIE. If it contains nested
5297 subprograms, then check their pc bounds. Likewise, we need to
5298 check lexical blocks as well, as they may also contain subprogram
5299 definitions. */
5300 while (child && child->tag)
5301 {
5302 if (child->tag == DW_TAG_subprogram
5303 || child->tag == DW_TAG_lexical_block)
5304 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5305 child = sibling_die (child);
5306 }
5307}
5308
fae299cd
DC
5309/* Get the low and high pc's represented by the scope DIE, and store
5310 them in *LOWPC and *HIGHPC. If the correct values can't be
5311 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5312
5313static void
5314get_scope_pc_bounds (struct die_info *die,
5315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5316 struct dwarf2_cu *cu)
5317{
5318 CORE_ADDR best_low = (CORE_ADDR) -1;
5319 CORE_ADDR best_high = (CORE_ADDR) 0;
5320 CORE_ADDR current_low, current_high;
5321
d85a05f0 5322 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5323 {
5324 best_low = current_low;
5325 best_high = current_high;
5326 }
5327 else
5328 {
5329 struct die_info *child = die->child;
5330
5331 while (child && child->tag)
5332 {
5333 switch (child->tag) {
5334 case DW_TAG_subprogram:
b084d499 5335 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5336 break;
5337 case DW_TAG_namespace:
f55ee35c 5338 case DW_TAG_module:
fae299cd
DC
5339 /* FIXME: carlton/2004-01-16: Should we do this for
5340 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5341 that current GCC's always emit the DIEs corresponding
5342 to definitions of methods of classes as children of a
5343 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5344 the DIEs giving the declarations, which could be
5345 anywhere). But I don't see any reason why the
5346 standards says that they have to be there. */
5347 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5348
5349 if (current_low != ((CORE_ADDR) -1))
5350 {
5351 best_low = min (best_low, current_low);
5352 best_high = max (best_high, current_high);
5353 }
5354 break;
5355 default:
5356 /* Ignore. */
5357 break;
5358 }
5359
5360 child = sibling_die (child);
5361 }
5362 }
5363
5364 *lowpc = best_low;
5365 *highpc = best_high;
5366}
5367
801e3a5b
JB
5368/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5369 in DIE. */
5370static void
5371dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5372 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5373{
5374 struct attribute *attr;
5375
5376 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5377 if (attr)
5378 {
5379 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5380
801e3a5b
JB
5381 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5382 if (attr)
5383 {
5384 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5385
801e3a5b
JB
5386 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5387 }
5388 }
5389
5390 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5391 if (attr)
5392 {
5393 bfd *obfd = cu->objfile->obfd;
5394
5395 /* The value of the DW_AT_ranges attribute is the offset of the
5396 address range list in the .debug_ranges section. */
5397 unsigned long offset = DW_UNSND (attr);
dce234bc 5398 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5399
5400 /* For some target architectures, but not others, the
5401 read_address function sign-extends the addresses it returns.
5402 To recognize base address selection entries, we need a
5403 mask. */
5404 unsigned int addr_size = cu->header.addr_size;
5405 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5406
5407 /* The base address, to which the next pair is relative. Note
5408 that this 'base' is a DWARF concept: most entries in a range
5409 list are relative, to reduce the number of relocs against the
5410 debugging information. This is separate from this function's
5411 'baseaddr' argument, which GDB uses to relocate debugging
5412 information from a shared library based on the address at
5413 which the library was loaded. */
d00adf39
DE
5414 CORE_ADDR base = cu->base_address;
5415 int base_known = cu->base_known;
801e3a5b 5416
be391dca 5417 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5418 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5419 {
5420 complaint (&symfile_complaints,
5421 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5422 offset);
5423 return;
5424 }
5425
5426 for (;;)
5427 {
5428 unsigned int bytes_read;
5429 CORE_ADDR start, end;
5430
5431 start = read_address (obfd, buffer, cu, &bytes_read);
5432 buffer += bytes_read;
5433 end = read_address (obfd, buffer, cu, &bytes_read);
5434 buffer += bytes_read;
5435
5436 /* Did we find the end of the range list? */
5437 if (start == 0 && end == 0)
5438 break;
5439
5440 /* Did we find a base address selection entry? */
5441 else if ((start & base_select_mask) == base_select_mask)
5442 {
5443 base = end;
5444 base_known = 1;
5445 }
5446
5447 /* We found an ordinary address range. */
5448 else
5449 {
5450 if (!base_known)
5451 {
5452 complaint (&symfile_complaints,
5453 _("Invalid .debug_ranges data (no base address)"));
5454 return;
5455 }
5456
6e70227d
DE
5457 record_block_range (block,
5458 baseaddr + base + start,
801e3a5b
JB
5459 baseaddr + base + end - 1);
5460 }
5461 }
5462 }
5463}
5464
c906108c
SS
5465/* Add an aggregate field to the field list. */
5466
5467static void
107d2387 5468dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 5469 struct dwarf2_cu *cu)
6e70227d 5470{
e7c27a73 5471 struct objfile *objfile = cu->objfile;
5e2b427d 5472 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5473 struct nextfield *new_field;
5474 struct attribute *attr;
5475 struct field *fp;
5476 char *fieldname = "";
5477
5478 /* Allocate a new field list entry and link it in. */
5479 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 5480 make_cleanup (xfree, new_field);
c906108c 5481 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
5482
5483 if (die->tag == DW_TAG_inheritance)
5484 {
5485 new_field->next = fip->baseclasses;
5486 fip->baseclasses = new_field;
5487 }
5488 else
5489 {
5490 new_field->next = fip->fields;
5491 fip->fields = new_field;
5492 }
c906108c
SS
5493 fip->nfields++;
5494
5495 /* Handle accessibility and virtuality of field.
5496 The default accessibility for members is public, the default
5497 accessibility for inheritance is private. */
5498 if (die->tag != DW_TAG_inheritance)
5499 new_field->accessibility = DW_ACCESS_public;
5500 else
5501 new_field->accessibility = DW_ACCESS_private;
5502 new_field->virtuality = DW_VIRTUALITY_none;
5503
e142c38c 5504 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
5505 if (attr)
5506 new_field->accessibility = DW_UNSND (attr);
5507 if (new_field->accessibility != DW_ACCESS_public)
5508 fip->non_public_fields = 1;
e142c38c 5509 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
5510 if (attr)
5511 new_field->virtuality = DW_UNSND (attr);
5512
5513 fp = &new_field->field;
a9a9bd0f 5514
e142c38c 5515 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 5516 {
a9a9bd0f 5517 /* Data member other than a C++ static data member. */
6e70227d 5518
c906108c 5519 /* Get type of field. */
e7c27a73 5520 fp->type = die_type (die, cu);
c906108c 5521
d6a843b5 5522 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 5523
c906108c 5524 /* Get bit size of field (zero if none). */
e142c38c 5525 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
5526 if (attr)
5527 {
5528 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5529 }
5530 else
5531 {
5532 FIELD_BITSIZE (*fp) = 0;
5533 }
5534
5535 /* Get bit offset of field. */
e142c38c 5536 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
5537 if (attr)
5538 {
d4b96c9a 5539 int byte_offset = 0;
c6a0999f 5540
3690dd37 5541 if (attr_form_is_section_offset (attr))
d4b96c9a 5542 dwarf2_complex_location_expr_complaint ();
3690dd37 5543 else if (attr_form_is_constant (attr))
c6a0999f 5544 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 5545 else if (attr_form_is_block (attr))
c6a0999f 5546 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
5547 else
5548 dwarf2_complex_location_expr_complaint ();
c6a0999f 5549
d6a843b5 5550 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 5551 }
e142c38c 5552 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
5553 if (attr)
5554 {
5e2b427d 5555 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
5556 {
5557 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
5558 additional bit offset from the MSB of the containing
5559 anonymous object to the MSB of the field. We don't
5560 have to do anything special since we don't need to
5561 know the size of the anonymous object. */
c906108c
SS
5562 FIELD_BITPOS (*fp) += DW_UNSND (attr);
5563 }
5564 else
5565 {
5566 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
5567 MSB of the anonymous object, subtract off the number of
5568 bits from the MSB of the field to the MSB of the
5569 object, and then subtract off the number of bits of
5570 the field itself. The result is the bit offset of
5571 the LSB of the field. */
c906108c
SS
5572 int anonymous_size;
5573 int bit_offset = DW_UNSND (attr);
5574
e142c38c 5575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5576 if (attr)
5577 {
5578 /* The size of the anonymous object containing
5579 the bit field is explicit, so use the
5580 indicated size (in bytes). */
5581 anonymous_size = DW_UNSND (attr);
5582 }
5583 else
5584 {
5585 /* The size of the anonymous object containing
5586 the bit field must be inferred from the type
5587 attribute of the data member containing the
5588 bit field. */
5589 anonymous_size = TYPE_LENGTH (fp->type);
5590 }
5591 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
5592 - bit_offset - FIELD_BITSIZE (*fp);
5593 }
5594 }
5595
5596 /* Get name of field. */
39cbfefa
DJ
5597 fieldname = dwarf2_name (die, cu);
5598 if (fieldname == NULL)
5599 fieldname = "";
d8151005
DJ
5600
5601 /* The name is already allocated along with this objfile, so we don't
5602 need to duplicate it for the type. */
5603 fp->name = fieldname;
c906108c
SS
5604
5605 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 5606 pointer or virtual base class pointer) to private. */
e142c38c 5607 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 5608 {
d48cc9dd 5609 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
5610 new_field->accessibility = DW_ACCESS_private;
5611 fip->non_public_fields = 1;
5612 }
5613 }
a9a9bd0f 5614 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 5615 {
a9a9bd0f
DC
5616 /* C++ static member. */
5617
5618 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
5619 is a declaration, but all versions of G++ as of this writing
5620 (so through at least 3.2.1) incorrectly generate
5621 DW_TAG_variable tags. */
6e70227d 5622
c906108c 5623 char *physname;
c906108c 5624
a9a9bd0f 5625 /* Get name of field. */
39cbfefa
DJ
5626 fieldname = dwarf2_name (die, cu);
5627 if (fieldname == NULL)
c906108c
SS
5628 return;
5629
254e6b9e 5630 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
5631 if (attr
5632 /* Only create a symbol if this is an external value.
5633 new_symbol checks this and puts the value in the global symbol
5634 table, which we want. If it is not external, new_symbol
5635 will try to put the value in cu->list_in_scope which is wrong. */
5636 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
5637 {
5638 /* A static const member, not much different than an enum as far as
5639 we're concerned, except that we can support more types. */
5640 new_symbol (die, NULL, cu);
5641 }
5642
2df3850c 5643 /* Get physical name. */
94af9270 5644 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 5645
d8151005
DJ
5646 /* The name is already allocated along with this objfile, so we don't
5647 need to duplicate it for the type. */
5648 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 5649 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 5650 FIELD_NAME (*fp) = fieldname;
c906108c
SS
5651 }
5652 else if (die->tag == DW_TAG_inheritance)
5653 {
5654 /* C++ base class field. */
e142c38c 5655 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 5656 if (attr)
d4b96c9a
JK
5657 {
5658 int byte_offset = 0;
5659
5660 if (attr_form_is_section_offset (attr))
5661 dwarf2_complex_location_expr_complaint ();
5662 else if (attr_form_is_constant (attr))
5663 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5664 else if (attr_form_is_block (attr))
5665 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5666 else
5667 dwarf2_complex_location_expr_complaint ();
5668
5669 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5670 }
c906108c 5671 FIELD_BITSIZE (*fp) = 0;
e7c27a73 5672 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
5673 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
5674 fip->nbaseclasses++;
5675 }
5676}
5677
98751a41
JK
5678/* Add a typedef defined in the scope of the FIP's class. */
5679
5680static void
5681dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
5682 struct dwarf2_cu *cu)
6e70227d 5683{
98751a41
JK
5684 struct objfile *objfile = cu->objfile;
5685 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5686 struct typedef_field_list *new_field;
5687 struct attribute *attr;
5688 struct typedef_field *fp;
5689 char *fieldname = "";
5690
5691 /* Allocate a new field list entry and link it in. */
5692 new_field = xzalloc (sizeof (*new_field));
5693 make_cleanup (xfree, new_field);
5694
5695 gdb_assert (die->tag == DW_TAG_typedef);
5696
5697 fp = &new_field->field;
5698
5699 /* Get name of field. */
5700 fp->name = dwarf2_name (die, cu);
5701 if (fp->name == NULL)
5702 return;
5703
5704 fp->type = read_type_die (die, cu);
5705
5706 new_field->next = fip->typedef_field_list;
5707 fip->typedef_field_list = new_field;
5708 fip->typedef_field_list_count++;
5709}
5710
c906108c
SS
5711/* Create the vector of fields, and attach it to the type. */
5712
5713static void
fba45db2 5714dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 5715 struct dwarf2_cu *cu)
c906108c
SS
5716{
5717 int nfields = fip->nfields;
5718
5719 /* Record the field count, allocate space for the array of fields,
5720 and create blank accessibility bitfields if necessary. */
5721 TYPE_NFIELDS (type) = nfields;
5722 TYPE_FIELDS (type) = (struct field *)
5723 TYPE_ALLOC (type, sizeof (struct field) * nfields);
5724 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
5725
b4ba55a1 5726 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
5727 {
5728 ALLOCATE_CPLUS_STRUCT_TYPE (type);
5729
5730 TYPE_FIELD_PRIVATE_BITS (type) =
5731 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5732 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
5733
5734 TYPE_FIELD_PROTECTED_BITS (type) =
5735 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5736 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
5737
5738 TYPE_FIELD_IGNORE_BITS (type) =
5739 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5740 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
5741 }
5742
5743 /* If the type has baseclasses, allocate and clear a bit vector for
5744 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 5745 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
5746 {
5747 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 5748 unsigned char *pointer;
c906108c
SS
5749
5750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
5751 pointer = TYPE_ALLOC (type, num_bytes);
5752 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
5753 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
5754 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
5755 }
5756
5757 /* Copy the saved-up fields into the field vector. Start from the head
5758 of the list, adding to the tail of the field array, so that they end
5759 up in the same order in the array in which they were added to the list. */
5760 while (nfields-- > 0)
5761 {
7d0ccb61
DJ
5762 struct nextfield *fieldp;
5763
5764 if (fip->fields)
5765 {
5766 fieldp = fip->fields;
5767 fip->fields = fieldp->next;
5768 }
5769 else
5770 {
5771 fieldp = fip->baseclasses;
5772 fip->baseclasses = fieldp->next;
5773 }
5774
5775 TYPE_FIELD (type, nfields) = fieldp->field;
5776 switch (fieldp->accessibility)
c906108c 5777 {
c5aa993b 5778 case DW_ACCESS_private:
b4ba55a1
JB
5779 if (cu->language != language_ada)
5780 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 5781 break;
c906108c 5782
c5aa993b 5783 case DW_ACCESS_protected:
b4ba55a1
JB
5784 if (cu->language != language_ada)
5785 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 5786 break;
c906108c 5787
c5aa993b
JM
5788 case DW_ACCESS_public:
5789 break;
c906108c 5790
c5aa993b
JM
5791 default:
5792 /* Unknown accessibility. Complain and treat it as public. */
5793 {
e2e0b3e5 5794 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 5795 fieldp->accessibility);
c5aa993b
JM
5796 }
5797 break;
c906108c
SS
5798 }
5799 if (nfields < fip->nbaseclasses)
5800 {
7d0ccb61 5801 switch (fieldp->virtuality)
c906108c 5802 {
c5aa993b
JM
5803 case DW_VIRTUALITY_virtual:
5804 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
5805 if (cu->language == language_ada)
5806 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
5807 SET_TYPE_FIELD_VIRTUAL (type, nfields);
5808 break;
c906108c
SS
5809 }
5810 }
c906108c
SS
5811 }
5812}
5813
c906108c
SS
5814/* Add a member function to the proper fieldlist. */
5815
5816static void
107d2387 5817dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 5818 struct type *type, struct dwarf2_cu *cu)
c906108c 5819{
e7c27a73 5820 struct objfile *objfile = cu->objfile;
c906108c
SS
5821 struct attribute *attr;
5822 struct fnfieldlist *flp;
5823 int i;
5824 struct fn_field *fnp;
5825 char *fieldname;
5826 char *physname;
5827 struct nextfnfield *new_fnfield;
f792889a 5828 struct type *this_type;
c906108c 5829
b4ba55a1
JB
5830 if (cu->language == language_ada)
5831 error ("unexpected member function in Ada type");
5832
2df3850c 5833 /* Get name of member function. */
39cbfefa
DJ
5834 fieldname = dwarf2_name (die, cu);
5835 if (fieldname == NULL)
2df3850c 5836 return;
c906108c 5837
2df3850c 5838 /* Get the mangled name. */
94af9270 5839 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c
SS
5840
5841 /* Look up member function name in fieldlist. */
5842 for (i = 0; i < fip->nfnfields; i++)
5843 {
27bfe10e 5844 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
5845 break;
5846 }
5847
5848 /* Create new list element if necessary. */
5849 if (i < fip->nfnfields)
5850 flp = &fip->fnfieldlists[i];
5851 else
5852 {
5853 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
5854 {
5855 fip->fnfieldlists = (struct fnfieldlist *)
5856 xrealloc (fip->fnfieldlists,
5857 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 5858 * sizeof (struct fnfieldlist));
c906108c 5859 if (fip->nfnfields == 0)
c13c43fd 5860 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
5861 }
5862 flp = &fip->fnfieldlists[fip->nfnfields];
5863 flp->name = fieldname;
5864 flp->length = 0;
5865 flp->head = NULL;
5866 fip->nfnfields++;
5867 }
5868
5869 /* Create a new member function field and chain it to the field list
5870 entry. */
5871 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 5872 make_cleanup (xfree, new_fnfield);
c906108c
SS
5873 memset (new_fnfield, 0, sizeof (struct nextfnfield));
5874 new_fnfield->next = flp->head;
5875 flp->head = new_fnfield;
5876 flp->length++;
5877
5878 /* Fill in the member function field info. */
5879 fnp = &new_fnfield->fnfield;
d8151005
DJ
5880 /* The name is already allocated along with this objfile, so we don't
5881 need to duplicate it for the type. */
5882 fnp->physname = physname ? physname : "";
c906108c 5883 fnp->type = alloc_type (objfile);
f792889a
DJ
5884 this_type = read_type_die (die, cu);
5885 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 5886 {
f792889a 5887 int nparams = TYPE_NFIELDS (this_type);
c906108c 5888
f792889a 5889 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
5890 of the method itself (TYPE_CODE_METHOD). */
5891 smash_to_method_type (fnp->type, type,
f792889a
DJ
5892 TYPE_TARGET_TYPE (this_type),
5893 TYPE_FIELDS (this_type),
5894 TYPE_NFIELDS (this_type),
5895 TYPE_VARARGS (this_type));
c906108c
SS
5896
5897 /* Handle static member functions.
c5aa993b
JM
5898 Dwarf2 has no clean way to discern C++ static and non-static
5899 member functions. G++ helps GDB by marking the first
5900 parameter for non-static member functions (which is the
5901 this pointer) as artificial. We obtain this information
5902 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 5903 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
5904 fnp->voffset = VOFFSET_STATIC;
5905 }
5906 else
e2e0b3e5 5907 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 5908 physname);
c906108c
SS
5909
5910 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 5911 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 5912 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
5913
5914 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
5915 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
5916
5917 /* Get accessibility. */
e142c38c 5918 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
5919 if (attr)
5920 {
5921 switch (DW_UNSND (attr))
5922 {
c5aa993b
JM
5923 case DW_ACCESS_private:
5924 fnp->is_private = 1;
5925 break;
5926 case DW_ACCESS_protected:
5927 fnp->is_protected = 1;
5928 break;
c906108c
SS
5929 }
5930 }
5931
b02dede2 5932 /* Check for artificial methods. */
e142c38c 5933 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
5934 if (attr && DW_UNSND (attr) != 0)
5935 fnp->is_artificial = 1;
5936
0d564a31 5937 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
5938 function. For older versions of GCC, this is an offset in the
5939 appropriate virtual table, as specified by DW_AT_containing_type.
5940 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
5941 to the object address. */
5942
e142c38c 5943 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 5944 if (attr)
8e19ed76 5945 {
aec5aa8b 5946 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 5947 {
aec5aa8b
TT
5948 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
5949 {
5950 /* Old-style GCC. */
5951 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
5952 }
5953 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
5954 || (DW_BLOCK (attr)->size > 1
5955 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
5956 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
5957 {
5958 struct dwarf_block blk;
5959 int offset;
5960
5961 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
5962 ? 1 : 2);
5963 blk.size = DW_BLOCK (attr)->size - offset;
5964 blk.data = DW_BLOCK (attr)->data + offset;
5965 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
5966 if ((fnp->voffset % cu->header.addr_size) != 0)
5967 dwarf2_complex_location_expr_complaint ();
5968 else
5969 fnp->voffset /= cu->header.addr_size;
5970 fnp->voffset += 2;
5971 }
5972 else
5973 dwarf2_complex_location_expr_complaint ();
5974
5975 if (!fnp->fcontext)
5976 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
5977 }
3690dd37 5978 else if (attr_form_is_section_offset (attr))
8e19ed76 5979 {
4d3c2250 5980 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
5981 }
5982 else
5983 {
4d3c2250
KB
5984 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
5985 fieldname);
8e19ed76 5986 }
0d564a31 5987 }
d48cc9dd
DJ
5988 else
5989 {
5990 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5991 if (attr && DW_UNSND (attr))
5992 {
5993 /* GCC does this, as of 2008-08-25; PR debug/37237. */
5994 complaint (&symfile_complaints,
5995 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
5996 fieldname, die->offset);
9655fd1a 5997 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
5998 TYPE_CPLUS_DYNAMIC (type) = 1;
5999 }
6000 }
c906108c
SS
6001}
6002
6003/* Create the vector of member function fields, and attach it to the type. */
6004
6005static void
fba45db2 6006dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6007 struct dwarf2_cu *cu)
c906108c
SS
6008{
6009 struct fnfieldlist *flp;
6010 int total_length = 0;
6011 int i;
6012
b4ba55a1
JB
6013 if (cu->language == language_ada)
6014 error ("unexpected member functions in Ada type");
6015
c906108c
SS
6016 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6017 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6018 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6019
6020 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6021 {
6022 struct nextfnfield *nfp = flp->head;
6023 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6024 int k;
6025
6026 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6027 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6028 fn_flp->fn_fields = (struct fn_field *)
6029 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6030 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6031 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6032
6033 total_length += flp->length;
6034 }
6035
6036 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6037 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6038}
6039
1168df01
JB
6040/* Returns non-zero if NAME is the name of a vtable member in CU's
6041 language, zero otherwise. */
6042static int
6043is_vtable_name (const char *name, struct dwarf2_cu *cu)
6044{
6045 static const char vptr[] = "_vptr";
987504bb 6046 static const char vtable[] = "vtable";
1168df01 6047
987504bb
JJ
6048 /* Look for the C++ and Java forms of the vtable. */
6049 if ((cu->language == language_java
6050 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6051 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6052 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6053 return 1;
6054
6055 return 0;
6056}
6057
c0dd20ea 6058/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6059 functions, with the ABI-specified layout. If TYPE describes
6060 such a structure, smash it into a member function type.
61049d3b
DJ
6061
6062 GCC shouldn't do this; it should just output pointer to member DIEs.
6063 This is GCC PR debug/28767. */
c0dd20ea 6064
0b92b5bb
TT
6065static void
6066quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6067{
0b92b5bb 6068 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6069
6070 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6071 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6072 return;
c0dd20ea
DJ
6073
6074 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6075 if (TYPE_FIELD_NAME (type, 0) == NULL
6076 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6077 || TYPE_FIELD_NAME (type, 1) == NULL
6078 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6079 return;
c0dd20ea
DJ
6080
6081 /* Find the type of the method. */
0b92b5bb 6082 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6083 if (pfn_type == NULL
6084 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6085 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6086 return;
c0dd20ea
DJ
6087
6088 /* Look for the "this" argument. */
6089 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6090 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6091 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6092 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6093 return;
c0dd20ea
DJ
6094
6095 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6096 new_type = alloc_type (objfile);
6097 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6098 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6099 TYPE_VARARGS (pfn_type));
0b92b5bb 6100 smash_to_methodptr_type (type, new_type);
c0dd20ea 6101}
1168df01 6102
c906108c
SS
6103/* Called when we find the DIE that starts a structure or union scope
6104 (definition) to process all dies that define the members of the
6105 structure or union.
6106
6107 NOTE: we need to call struct_type regardless of whether or not the
6108 DIE has an at_name attribute, since it might be an anonymous
6109 structure or union. This gets the type entered into our set of
6110 user defined types.
6111
6112 However, if the structure is incomplete (an opaque struct/union)
6113 then suppress creating a symbol table entry for it since gdb only
6114 wants to find the one with the complete definition. Note that if
6115 it is complete, we just call new_symbol, which does it's own
6116 checking about whether the struct/union is anonymous or not (and
6117 suppresses creating a symbol table entry itself). */
6118
f792889a 6119static struct type *
134d01f1 6120read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6121{
e7c27a73 6122 struct objfile *objfile = cu->objfile;
c906108c
SS
6123 struct type *type;
6124 struct attribute *attr;
39cbfefa 6125 char *name;
d3f41bb1 6126 struct cleanup *back_to;
c906108c 6127
348e048f
DE
6128 /* If the definition of this type lives in .debug_types, read that type.
6129 Don't follow DW_AT_specification though, that will take us back up
6130 the chain and we want to go down. */
6131 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6132 if (attr)
6133 {
6134 struct dwarf2_cu *type_cu = cu;
6135 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6136
348e048f
DE
6137 /* We could just recurse on read_structure_type, but we need to call
6138 get_die_type to ensure only one type for this DIE is created.
6139 This is important, for example, because for c++ classes we need
6140 TYPE_NAME set which is only done by new_symbol. Blech. */
6141 type = read_type_die (type_die, type_cu);
6142 return set_die_type (die, type, cu);
6143 }
6144
d3f41bb1
TT
6145 back_to = make_cleanup (null_cleanup, 0);
6146
c0dd20ea 6147 type = alloc_type (objfile);
c906108c 6148 INIT_CPLUS_SPECIFIC (type);
93311388 6149
39cbfefa
DJ
6150 name = dwarf2_name (die, cu);
6151 if (name != NULL)
c906108c 6152 {
987504bb
JJ
6153 if (cu->language == language_cplus
6154 || cu->language == language_java)
63d06c5c 6155 {
94af9270
KS
6156 TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6157 if (die->tag == DW_TAG_structure_type
6158 || die->tag == DW_TAG_class_type)
6159 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6160 }
6161 else
6162 {
d8151005
DJ
6163 /* The name is already allocated along with this objfile, so
6164 we don't need to duplicate it for the type. */
94af9270
KS
6165 TYPE_TAG_NAME (type) = (char *) name;
6166 if (die->tag == DW_TAG_class_type)
6167 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6168 }
c906108c
SS
6169 }
6170
6171 if (die->tag == DW_TAG_structure_type)
6172 {
6173 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6174 }
6175 else if (die->tag == DW_TAG_union_type)
6176 {
6177 TYPE_CODE (type) = TYPE_CODE_UNION;
6178 }
6179 else
6180 {
c906108c
SS
6181 TYPE_CODE (type) = TYPE_CODE_CLASS;
6182 }
6183
0cc2414c
TT
6184 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6185 TYPE_DECLARED_CLASS (type) = 1;
6186
e142c38c 6187 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6188 if (attr)
6189 {
6190 TYPE_LENGTH (type) = DW_UNSND (attr);
6191 }
6192 else
6193 {
6194 TYPE_LENGTH (type) = 0;
6195 }
6196
876cecd0 6197 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6198 if (die_is_declaration (die, cu))
876cecd0 6199 TYPE_STUB (type) = 1;
a6c727b2
DJ
6200 else if (attr == NULL && die->child == NULL
6201 && producer_is_realview (cu->producer))
6202 /* RealView does not output the required DW_AT_declaration
6203 on incomplete types. */
6204 TYPE_STUB (type) = 1;
dc718098 6205
c906108c
SS
6206 /* We need to add the type field to the die immediately so we don't
6207 infinitely recurse when dealing with pointers to the structure
6208 type within the structure itself. */
1c379e20 6209 set_die_type (die, type, cu);
c906108c 6210
7e314c57
JK
6211 /* set_die_type should be already done. */
6212 set_descriptive_type (type, die, cu);
6213
e142c38c 6214 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6215 {
6216 struct field_info fi;
6217 struct die_info *child_die;
c906108c
SS
6218
6219 memset (&fi, 0, sizeof (struct field_info));
6220
639d11d3 6221 child_die = die->child;
c906108c
SS
6222
6223 while (child_die && child_die->tag)
6224 {
a9a9bd0f
DC
6225 if (child_die->tag == DW_TAG_member
6226 || child_die->tag == DW_TAG_variable)
c906108c 6227 {
a9a9bd0f
DC
6228 /* NOTE: carlton/2002-11-05: A C++ static data member
6229 should be a DW_TAG_member that is a declaration, but
6230 all versions of G++ as of this writing (so through at
6231 least 3.2.1) incorrectly generate DW_TAG_variable
6232 tags for them instead. */
e7c27a73 6233 dwarf2_add_field (&fi, child_die, cu);
c906108c 6234 }
8713b1b1 6235 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6236 {
6237 /* C++ member function. */
e7c27a73 6238 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6239 }
6240 else if (child_die->tag == DW_TAG_inheritance)
6241 {
6242 /* C++ base class field. */
e7c27a73 6243 dwarf2_add_field (&fi, child_die, cu);
c906108c 6244 }
98751a41
JK
6245 else if (child_die->tag == DW_TAG_typedef)
6246 dwarf2_add_typedef (&fi, child_die, cu);
c906108c
SS
6247 child_die = sibling_die (child_die);
6248 }
6249
6250 /* Attach fields and member functions to the type. */
6251 if (fi.nfields)
e7c27a73 6252 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6253 if (fi.nfnfields)
6254 {
e7c27a73 6255 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6256
c5aa993b 6257 /* Get the type which refers to the base class (possibly this
c906108c 6258 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6259 class from the DW_AT_containing_type attribute. This use of
6260 DW_AT_containing_type is a GNU extension. */
c906108c 6261
e142c38c 6262 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6263 {
e7c27a73 6264 struct type *t = die_containing_type (die, cu);
c906108c
SS
6265
6266 TYPE_VPTR_BASETYPE (type) = t;
6267 if (type == t)
6268 {
c906108c
SS
6269 int i;
6270
6271 /* Our own class provides vtbl ptr. */
6272 for (i = TYPE_NFIELDS (t) - 1;
6273 i >= TYPE_N_BASECLASSES (t);
6274 --i)
6275 {
6276 char *fieldname = TYPE_FIELD_NAME (t, i);
6277
1168df01 6278 if (is_vtable_name (fieldname, cu))
c906108c
SS
6279 {
6280 TYPE_VPTR_FIELDNO (type) = i;
6281 break;
6282 }
6283 }
6284
6285 /* Complain if virtual function table field not found. */
6286 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6287 complaint (&symfile_complaints,
e2e0b3e5 6288 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6289 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6290 "");
c906108c
SS
6291 }
6292 else
6293 {
6294 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6295 }
6296 }
f6235d4c
EZ
6297 else if (cu->producer
6298 && strncmp (cu->producer,
6299 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6300 {
6301 /* The IBM XLC compiler does not provide direct indication
6302 of the containing type, but the vtable pointer is
6303 always named __vfp. */
6304
6305 int i;
6306
6307 for (i = TYPE_NFIELDS (type) - 1;
6308 i >= TYPE_N_BASECLASSES (type);
6309 --i)
6310 {
6311 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6312 {
6313 TYPE_VPTR_FIELDNO (type) = i;
6314 TYPE_VPTR_BASETYPE (type) = type;
6315 break;
6316 }
6317 }
6318 }
c906108c 6319 }
98751a41
JK
6320
6321 /* Copy fi.typedef_field_list linked list elements content into the
6322 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6323 if (fi.typedef_field_list)
6324 {
6325 int i = fi.typedef_field_list_count;
6326
a0d7a4ff 6327 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6328 TYPE_TYPEDEF_FIELD_ARRAY (type)
6329 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6330 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6331
6332 /* Reverse the list order to keep the debug info elements order. */
6333 while (--i >= 0)
6334 {
6335 struct typedef_field *dest, *src;
6e70227d 6336
98751a41
JK
6337 dest = &TYPE_TYPEDEF_FIELD (type, i);
6338 src = &fi.typedef_field_list->field;
6339 fi.typedef_field_list = fi.typedef_field_list->next;
6340 *dest = *src;
6341 }
6342 }
c906108c 6343 }
63d06c5c 6344
0b92b5bb
TT
6345 quirk_gcc_member_function_pointer (type, cu->objfile);
6346
0114d602 6347 do_cleanups (back_to);
f792889a 6348 return type;
c906108c
SS
6349}
6350
134d01f1
DJ
6351static void
6352process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6353{
90aeadfc 6354 struct die_info *child_die = die->child;
f792889a 6355 struct type *this_type;
c906108c 6356
f792889a
DJ
6357 this_type = get_die_type (die, cu);
6358 if (this_type == NULL)
6359 this_type = read_structure_type (die, cu);
c906108c 6360
90aeadfc
DC
6361 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6362 snapshots) has been known to create a die giving a declaration
6363 for a class that has, as a child, a die giving a definition for a
6364 nested class. So we have to process our children even if the
6365 current die is a declaration. Normally, of course, a declaration
6366 won't have any children at all. */
134d01f1 6367
90aeadfc
DC
6368 while (child_die != NULL && child_die->tag)
6369 {
6370 if (child_die->tag == DW_TAG_member
6371 || child_die->tag == DW_TAG_variable
6372 || child_die->tag == DW_TAG_inheritance)
134d01f1 6373 {
90aeadfc 6374 /* Do nothing. */
134d01f1 6375 }
90aeadfc
DC
6376 else
6377 process_die (child_die, cu);
134d01f1 6378
90aeadfc 6379 child_die = sibling_die (child_die);
134d01f1
DJ
6380 }
6381
fa4028e9
JB
6382 /* Do not consider external references. According to the DWARF standard,
6383 these DIEs are identified by the fact that they have no byte_size
6384 attribute, and a declaration attribute. */
6385 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6386 || !die_is_declaration (die, cu))
f792889a 6387 new_symbol (die, this_type, cu);
134d01f1
DJ
6388}
6389
6390/* Given a DW_AT_enumeration_type die, set its type. We do not
6391 complete the type's fields yet, or create any symbols. */
c906108c 6392
f792889a 6393static struct type *
134d01f1 6394read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6395{
e7c27a73 6396 struct objfile *objfile = cu->objfile;
c906108c 6397 struct type *type;
c906108c 6398 struct attribute *attr;
0114d602 6399 const char *name;
134d01f1 6400
348e048f
DE
6401 /* If the definition of this type lives in .debug_types, read that type.
6402 Don't follow DW_AT_specification though, that will take us back up
6403 the chain and we want to go down. */
6404 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6405 if (attr)
6406 {
6407 struct dwarf2_cu *type_cu = cu;
6408 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6409
348e048f
DE
6410 type = read_type_die (type_die, type_cu);
6411 return set_die_type (die, type, cu);
6412 }
6413
c906108c
SS
6414 type = alloc_type (objfile);
6415
6416 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6417 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6418 if (name != NULL)
0114d602 6419 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6420
e142c38c 6421 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6422 if (attr)
6423 {
6424 TYPE_LENGTH (type) = DW_UNSND (attr);
6425 }
6426 else
6427 {
6428 TYPE_LENGTH (type) = 0;
6429 }
6430
137033e9
JB
6431 /* The enumeration DIE can be incomplete. In Ada, any type can be
6432 declared as private in the package spec, and then defined only
6433 inside the package body. Such types are known as Taft Amendment
6434 Types. When another package uses such a type, an incomplete DIE
6435 may be generated by the compiler. */
02eb380e 6436 if (die_is_declaration (die, cu))
876cecd0 6437 TYPE_STUB (type) = 1;
02eb380e 6438
f792889a 6439 return set_die_type (die, type, cu);
134d01f1
DJ
6440}
6441
6442/* Given a pointer to a die which begins an enumeration, process all
6443 the dies that define the members of the enumeration, and create the
6444 symbol for the enumeration type.
6445
6446 NOTE: We reverse the order of the element list. */
6447
6448static void
6449process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6450{
134d01f1
DJ
6451 struct die_info *child_die;
6452 struct field *fields;
134d01f1
DJ
6453 struct symbol *sym;
6454 int num_fields;
6455 int unsigned_enum = 1;
39cbfefa 6456 char *name;
f792889a 6457 struct type *this_type;
134d01f1 6458
c906108c
SS
6459 num_fields = 0;
6460 fields = NULL;
f792889a
DJ
6461 this_type = get_die_type (die, cu);
6462 if (this_type == NULL)
6463 this_type = read_enumeration_type (die, cu);
639d11d3 6464 if (die->child != NULL)
c906108c 6465 {
639d11d3 6466 child_die = die->child;
c906108c
SS
6467 while (child_die && child_die->tag)
6468 {
6469 if (child_die->tag != DW_TAG_enumerator)
6470 {
e7c27a73 6471 process_die (child_die, cu);
c906108c
SS
6472 }
6473 else
6474 {
39cbfefa
DJ
6475 name = dwarf2_name (child_die, cu);
6476 if (name)
c906108c 6477 {
f792889a 6478 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
6479 if (SYMBOL_VALUE (sym) < 0)
6480 unsigned_enum = 0;
6481
6482 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6483 {
6484 fields = (struct field *)
6485 xrealloc (fields,
6486 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6487 * sizeof (struct field));
c906108c
SS
6488 }
6489
3567439c 6490 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 6491 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 6492 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
6493 FIELD_BITSIZE (fields[num_fields]) = 0;
6494
6495 num_fields++;
6496 }
6497 }
6498
6499 child_die = sibling_die (child_die);
6500 }
6501
6502 if (num_fields)
6503 {
f792889a
DJ
6504 TYPE_NFIELDS (this_type) = num_fields;
6505 TYPE_FIELDS (this_type) = (struct field *)
6506 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6507 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 6508 sizeof (struct field) * num_fields);
b8c9b27d 6509 xfree (fields);
c906108c
SS
6510 }
6511 if (unsigned_enum)
876cecd0 6512 TYPE_UNSIGNED (this_type) = 1;
c906108c 6513 }
134d01f1 6514
f792889a 6515 new_symbol (die, this_type, cu);
c906108c
SS
6516}
6517
6518/* Extract all information from a DW_TAG_array_type DIE and put it in
6519 the DIE's type field. For now, this only handles one dimensional
6520 arrays. */
6521
f792889a 6522static struct type *
e7c27a73 6523read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6524{
e7c27a73 6525 struct objfile *objfile = cu->objfile;
c906108c 6526 struct die_info *child_die;
7e314c57 6527 struct type *type;
c906108c
SS
6528 struct type *element_type, *range_type, *index_type;
6529 struct type **range_types = NULL;
6530 struct attribute *attr;
6531 int ndim = 0;
6532 struct cleanup *back_to;
39cbfefa 6533 char *name;
c906108c 6534
e7c27a73 6535 element_type = die_type (die, cu);
c906108c 6536
7e314c57
JK
6537 /* The die_type call above may have already set the type for this DIE. */
6538 type = get_die_type (die, cu);
6539 if (type)
6540 return type;
6541
c906108c
SS
6542 /* Irix 6.2 native cc creates array types without children for
6543 arrays with unspecified length. */
639d11d3 6544 if (die->child == NULL)
c906108c 6545 {
46bf5051 6546 index_type = objfile_type (objfile)->builtin_int;
c906108c 6547 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
6548 type = create_array_type (NULL, element_type, range_type);
6549 return set_die_type (die, type, cu);
c906108c
SS
6550 }
6551
6552 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 6553 child_die = die->child;
c906108c
SS
6554 while (child_die && child_die->tag)
6555 {
6556 if (child_die->tag == DW_TAG_subrange_type)
6557 {
f792889a 6558 struct type *child_type = read_type_die (child_die, cu);
9a619af0 6559
f792889a 6560 if (child_type != NULL)
a02abb62
JB
6561 {
6562 /* The range type was succesfully read. Save it for
6563 the array type creation. */
6564 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
6565 {
6566 range_types = (struct type **)
6567 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
6568 * sizeof (struct type *));
6569 if (ndim == 0)
6570 make_cleanup (free_current_contents, &range_types);
6571 }
f792889a 6572 range_types[ndim++] = child_type;
a02abb62 6573 }
c906108c
SS
6574 }
6575 child_die = sibling_die (child_die);
6576 }
6577
6578 /* Dwarf2 dimensions are output from left to right, create the
6579 necessary array types in backwards order. */
7ca2d3a3 6580
c906108c 6581 type = element_type;
7ca2d3a3
DL
6582
6583 if (read_array_order (die, cu) == DW_ORD_col_major)
6584 {
6585 int i = 0;
9a619af0 6586
7ca2d3a3
DL
6587 while (i < ndim)
6588 type = create_array_type (NULL, type, range_types[i++]);
6589 }
6590 else
6591 {
6592 while (ndim-- > 0)
6593 type = create_array_type (NULL, type, range_types[ndim]);
6594 }
c906108c 6595
f5f8a009
EZ
6596 /* Understand Dwarf2 support for vector types (like they occur on
6597 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
6598 array type. This is not part of the Dwarf2/3 standard yet, but a
6599 custom vendor extension. The main difference between a regular
6600 array and the vector variant is that vectors are passed by value
6601 to functions. */
e142c38c 6602 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 6603 if (attr)
ea37ba09 6604 make_vector_type (type);
f5f8a009 6605
39cbfefa
DJ
6606 name = dwarf2_name (die, cu);
6607 if (name)
6608 TYPE_NAME (type) = name;
6e70227d 6609
7e314c57
JK
6610 /* Install the type in the die. */
6611 set_die_type (die, type, cu);
6612
6613 /* set_die_type should be already done. */
b4ba55a1
JB
6614 set_descriptive_type (type, die, cu);
6615
c906108c
SS
6616 do_cleanups (back_to);
6617
7e314c57 6618 return type;
c906108c
SS
6619}
6620
7ca2d3a3 6621static enum dwarf_array_dim_ordering
6e70227d 6622read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
6623{
6624 struct attribute *attr;
6625
6626 attr = dwarf2_attr (die, DW_AT_ordering, cu);
6627
6628 if (attr) return DW_SND (attr);
6629
6630 /*
6631 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 6632 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
6633 laid out as per normal fortran.
6634
6e70227d 6635 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
6636 version checking.
6637 */
6638
905e0470
PM
6639 if (cu->language == language_fortran
6640 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
6641 {
6642 return DW_ORD_row_major;
6643 }
6644
6e70227d 6645 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
6646 {
6647 case array_column_major:
6648 return DW_ORD_col_major;
6649 case array_row_major:
6650 default:
6651 return DW_ORD_row_major;
6652 };
6653}
6654
72019c9c
GM
6655/* Extract all information from a DW_TAG_set_type DIE and put it in
6656 the DIE's type field. */
6657
f792889a 6658static struct type *
72019c9c
GM
6659read_set_type (struct die_info *die, struct dwarf2_cu *cu)
6660{
7e314c57
JK
6661 struct type *domain_type, *set_type;
6662 struct attribute *attr;
f792889a 6663
7e314c57
JK
6664 domain_type = die_type (die, cu);
6665
6666 /* The die_type call above may have already set the type for this DIE. */
6667 set_type = get_die_type (die, cu);
6668 if (set_type)
6669 return set_type;
6670
6671 set_type = create_set_type (NULL, domain_type);
6672
6673 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
6674 if (attr)
6675 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 6676
f792889a 6677 return set_die_type (die, set_type, cu);
72019c9c 6678}
7ca2d3a3 6679
c906108c
SS
6680/* First cut: install each common block member as a global variable. */
6681
6682static void
e7c27a73 6683read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6684{
6685 struct die_info *child_die;
6686 struct attribute *attr;
6687 struct symbol *sym;
6688 CORE_ADDR base = (CORE_ADDR) 0;
6689
e142c38c 6690 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
6691 if (attr)
6692 {
8e19ed76
PS
6693 /* Support the .debug_loc offsets */
6694 if (attr_form_is_block (attr))
6695 {
e7c27a73 6696 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 6697 }
3690dd37 6698 else if (attr_form_is_section_offset (attr))
8e19ed76 6699 {
4d3c2250 6700 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6701 }
6702 else
6703 {
4d3c2250
KB
6704 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6705 "common block member");
8e19ed76 6706 }
c906108c 6707 }
639d11d3 6708 if (die->child != NULL)
c906108c 6709 {
639d11d3 6710 child_die = die->child;
c906108c
SS
6711 while (child_die && child_die->tag)
6712 {
e7c27a73 6713 sym = new_symbol (child_die, NULL, cu);
e142c38c 6714 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
6715 if (attr)
6716 {
d4b96c9a
JK
6717 CORE_ADDR byte_offset = 0;
6718
6719 if (attr_form_is_section_offset (attr))
6720 dwarf2_complex_location_expr_complaint ();
6721 else if (attr_form_is_constant (attr))
6722 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6723 else if (attr_form_is_block (attr))
6724 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6725 else
6726 dwarf2_complex_location_expr_complaint ();
6727
6728 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
6729 add_symbol_to_list (sym, &global_symbols);
6730 }
6731 child_die = sibling_die (child_die);
6732 }
6733 }
6734}
6735
0114d602 6736/* Create a type for a C++ namespace. */
d9fa45fe 6737
0114d602
DJ
6738static struct type *
6739read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 6740{
e7c27a73 6741 struct objfile *objfile = cu->objfile;
0114d602 6742 const char *previous_prefix, *name;
9219021c 6743 int is_anonymous;
0114d602
DJ
6744 struct type *type;
6745
6746 /* For extensions, reuse the type of the original namespace. */
6747 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
6748 {
6749 struct die_info *ext_die;
6750 struct dwarf2_cu *ext_cu = cu;
9a619af0 6751
0114d602
DJ
6752 ext_die = dwarf2_extension (die, &ext_cu);
6753 type = read_type_die (ext_die, ext_cu);
6754 return set_die_type (die, type, cu);
6755 }
9219021c 6756
e142c38c 6757 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
6758
6759 /* Now build the name of the current namespace. */
6760
0114d602
DJ
6761 previous_prefix = determine_prefix (die, cu);
6762 if (previous_prefix[0] != '\0')
6763 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 6764 previous_prefix, name, 0, cu);
0114d602
DJ
6765
6766 /* Create the type. */
6767 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
6768 objfile);
6769 TYPE_NAME (type) = (char *) name;
6770 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6771
60531b24 6772 return set_die_type (die, type, cu);
0114d602
DJ
6773}
6774
6775/* Read a C++ namespace. */
6776
6777static void
6778read_namespace (struct die_info *die, struct dwarf2_cu *cu)
6779{
6780 struct objfile *objfile = cu->objfile;
6781 const char *name;
6782 int is_anonymous;
9219021c 6783
5c4e30ca
DC
6784 /* Add a symbol associated to this if we haven't seen the namespace
6785 before. Also, add a using directive if it's an anonymous
6786 namespace. */
9219021c 6787
f2f0e013 6788 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
6789 {
6790 struct type *type;
6791
0114d602 6792 type = read_type_die (die, cu);
e7c27a73 6793 new_symbol (die, type, cu);
5c4e30ca 6794
0114d602 6795 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 6796 if (is_anonymous)
0114d602
DJ
6797 {
6798 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 6799
c0cc3a76 6800 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 6801 NULL, &objfile->objfile_obstack);
0114d602 6802 }
5c4e30ca 6803 }
9219021c 6804
639d11d3 6805 if (die->child != NULL)
d9fa45fe 6806 {
639d11d3 6807 struct die_info *child_die = die->child;
6e70227d 6808
d9fa45fe
DC
6809 while (child_die && child_die->tag)
6810 {
e7c27a73 6811 process_die (child_die, cu);
d9fa45fe
DC
6812 child_die = sibling_die (child_die);
6813 }
6814 }
38d518c9
EZ
6815}
6816
f55ee35c
JK
6817/* Read a Fortran module as type. This DIE can be only a declaration used for
6818 imported module. Still we need that type as local Fortran "use ... only"
6819 declaration imports depend on the created type in determine_prefix. */
6820
6821static struct type *
6822read_module_type (struct die_info *die, struct dwarf2_cu *cu)
6823{
6824 struct objfile *objfile = cu->objfile;
6825 char *module_name;
6826 struct type *type;
6827
6828 module_name = dwarf2_name (die, cu);
6829 if (!module_name)
6830 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
6831 die->offset);
6832 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
6833
6834 /* determine_prefix uses TYPE_TAG_NAME. */
6835 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6836
6837 return set_die_type (die, type, cu);
6838}
6839
5d7cb8df
JK
6840/* Read a Fortran module. */
6841
6842static void
6843read_module (struct die_info *die, struct dwarf2_cu *cu)
6844{
6845 struct die_info *child_die = die->child;
6846
5d7cb8df
JK
6847 while (child_die && child_die->tag)
6848 {
6849 process_die (child_die, cu);
6850 child_die = sibling_die (child_die);
6851 }
6852}
6853
38d518c9
EZ
6854/* Return the name of the namespace represented by DIE. Set
6855 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
6856 namespace. */
6857
6858static const char *
e142c38c 6859namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
6860{
6861 struct die_info *current_die;
6862 const char *name = NULL;
6863
6864 /* Loop through the extensions until we find a name. */
6865
6866 for (current_die = die;
6867 current_die != NULL;
f2f0e013 6868 current_die = dwarf2_extension (die, &cu))
38d518c9 6869 {
e142c38c 6870 name = dwarf2_name (current_die, cu);
38d518c9
EZ
6871 if (name != NULL)
6872 break;
6873 }
6874
6875 /* Is it an anonymous namespace? */
6876
6877 *is_anonymous = (name == NULL);
6878 if (*is_anonymous)
6879 name = "(anonymous namespace)";
6880
6881 return name;
d9fa45fe
DC
6882}
6883
c906108c
SS
6884/* Extract all information from a DW_TAG_pointer_type DIE and add to
6885 the user defined type vector. */
6886
f792889a 6887static struct type *
e7c27a73 6888read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6889{
5e2b427d 6890 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 6891 struct comp_unit_head *cu_header = &cu->header;
c906108c 6892 struct type *type;
8b2dbe47
KB
6893 struct attribute *attr_byte_size;
6894 struct attribute *attr_address_class;
6895 int byte_size, addr_class;
7e314c57
JK
6896 struct type *target_type;
6897
6898 target_type = die_type (die, cu);
c906108c 6899
7e314c57
JK
6900 /* The die_type call above may have already set the type for this DIE. */
6901 type = get_die_type (die, cu);
6902 if (type)
6903 return type;
6904
6905 type = lookup_pointer_type (target_type);
8b2dbe47 6906
e142c38c 6907 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
6908 if (attr_byte_size)
6909 byte_size = DW_UNSND (attr_byte_size);
c906108c 6910 else
8b2dbe47
KB
6911 byte_size = cu_header->addr_size;
6912
e142c38c 6913 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
6914 if (attr_address_class)
6915 addr_class = DW_UNSND (attr_address_class);
6916 else
6917 addr_class = DW_ADDR_none;
6918
6919 /* If the pointer size or address class is different than the
6920 default, create a type variant marked as such and set the
6921 length accordingly. */
6922 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 6923 {
5e2b427d 6924 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
6925 {
6926 int type_flags;
6927
849957d9 6928 type_flags = gdbarch_address_class_type_flags
5e2b427d 6929 (gdbarch, byte_size, addr_class);
876cecd0
TT
6930 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
6931 == 0);
8b2dbe47
KB
6932 type = make_type_with_address_space (type, type_flags);
6933 }
6934 else if (TYPE_LENGTH (type) != byte_size)
6935 {
e2e0b3e5 6936 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 6937 }
6e70227d 6938 else
9a619af0
MS
6939 {
6940 /* Should we also complain about unhandled address classes? */
6941 }
c906108c 6942 }
8b2dbe47
KB
6943
6944 TYPE_LENGTH (type) = byte_size;
f792889a 6945 return set_die_type (die, type, cu);
c906108c
SS
6946}
6947
6948/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
6949 the user defined type vector. */
6950
f792889a 6951static struct type *
e7c27a73 6952read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6953{
6954 struct type *type;
6955 struct type *to_type;
6956 struct type *domain;
6957
e7c27a73
DJ
6958 to_type = die_type (die, cu);
6959 domain = die_containing_type (die, cu);
0d5de010 6960
7e314c57
JK
6961 /* The calls above may have already set the type for this DIE. */
6962 type = get_die_type (die, cu);
6963 if (type)
6964 return type;
6965
0d5de010
DJ
6966 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
6967 type = lookup_methodptr_type (to_type);
6968 else
6969 type = lookup_memberptr_type (to_type, domain);
c906108c 6970
f792889a 6971 return set_die_type (die, type, cu);
c906108c
SS
6972}
6973
6974/* Extract all information from a DW_TAG_reference_type DIE and add to
6975 the user defined type vector. */
6976
f792889a 6977static struct type *
e7c27a73 6978read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6979{
e7c27a73 6980 struct comp_unit_head *cu_header = &cu->header;
7e314c57 6981 struct type *type, *target_type;
c906108c
SS
6982 struct attribute *attr;
6983
7e314c57
JK
6984 target_type = die_type (die, cu);
6985
6986 /* The die_type call above may have already set the type for this DIE. */
6987 type = get_die_type (die, cu);
6988 if (type)
6989 return type;
6990
6991 type = lookup_reference_type (target_type);
e142c38c 6992 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6993 if (attr)
6994 {
6995 TYPE_LENGTH (type) = DW_UNSND (attr);
6996 }
6997 else
6998 {
107d2387 6999 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7000 }
f792889a 7001 return set_die_type (die, type, cu);
c906108c
SS
7002}
7003
f792889a 7004static struct type *
e7c27a73 7005read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7006{
f792889a 7007 struct type *base_type, *cv_type;
c906108c 7008
e7c27a73 7009 base_type = die_type (die, cu);
7e314c57
JK
7010
7011 /* The die_type call above may have already set the type for this DIE. */
7012 cv_type = get_die_type (die, cu);
7013 if (cv_type)
7014 return cv_type;
7015
f792889a
DJ
7016 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7017 return set_die_type (die, cv_type, cu);
c906108c
SS
7018}
7019
f792889a 7020static struct type *
e7c27a73 7021read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7022{
f792889a 7023 struct type *base_type, *cv_type;
c906108c 7024
e7c27a73 7025 base_type = die_type (die, cu);
7e314c57
JK
7026
7027 /* The die_type call above may have already set the type for this DIE. */
7028 cv_type = get_die_type (die, cu);
7029 if (cv_type)
7030 return cv_type;
7031
f792889a
DJ
7032 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7033 return set_die_type (die, cv_type, cu);
c906108c
SS
7034}
7035
7036/* Extract all information from a DW_TAG_string_type DIE and add to
7037 the user defined type vector. It isn't really a user defined type,
7038 but it behaves like one, with other DIE's using an AT_user_def_type
7039 attribute to reference it. */
7040
f792889a 7041static struct type *
e7c27a73 7042read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7043{
e7c27a73 7044 struct objfile *objfile = cu->objfile;
3b7538c0 7045 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7046 struct type *type, *range_type, *index_type, *char_type;
7047 struct attribute *attr;
7048 unsigned int length;
7049
e142c38c 7050 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7051 if (attr)
7052 {
7053 length = DW_UNSND (attr);
7054 }
7055 else
7056 {
b21b22e0 7057 /* check for the DW_AT_byte_size attribute */
e142c38c 7058 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7059 if (attr)
7060 {
7061 length = DW_UNSND (attr);
7062 }
7063 else
7064 {
7065 length = 1;
7066 }
c906108c 7067 }
6ccb9162 7068
46bf5051 7069 index_type = objfile_type (objfile)->builtin_int;
c906108c 7070 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7071 char_type = language_string_char_type (cu->language_defn, gdbarch);
7072 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7073
f792889a 7074 return set_die_type (die, type, cu);
c906108c
SS
7075}
7076
7077/* Handle DIES due to C code like:
7078
7079 struct foo
c5aa993b
JM
7080 {
7081 int (*funcp)(int a, long l);
7082 int b;
7083 };
c906108c
SS
7084
7085 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7086 */
c906108c 7087
f792889a 7088static struct type *
e7c27a73 7089read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7090{
7091 struct type *type; /* Type that this function returns */
7092 struct type *ftype; /* Function that returns above type */
7093 struct attribute *attr;
7094
e7c27a73 7095 type = die_type (die, cu);
7e314c57
JK
7096
7097 /* The die_type call above may have already set the type for this DIE. */
7098 ftype = get_die_type (die, cu);
7099 if (ftype)
7100 return ftype;
7101
0c8b41f1 7102 ftype = lookup_function_type (type);
c906108c 7103
5b8101ae 7104 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7105 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7106 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7107 || cu->language == language_cplus
5b8101ae
PM
7108 || cu->language == language_java
7109 || cu->language == language_pascal)
876cecd0 7110 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7111 else if (producer_is_realview (cu->producer))
7112 /* RealView does not emit DW_AT_prototyped. We can not
7113 distinguish prototyped and unprototyped functions; default to
7114 prototyped, since that is more common in modern code (and
7115 RealView warns about unprototyped functions). */
7116 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7117
c055b101
CV
7118 /* Store the calling convention in the type if it's available in
7119 the subroutine die. Otherwise set the calling convention to
7120 the default value DW_CC_normal. */
7121 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7122 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7123
7124 /* We need to add the subroutine type to the die immediately so
7125 we don't infinitely recurse when dealing with parameters
7126 declared as the same subroutine type. */
7127 set_die_type (die, ftype, cu);
6e70227d 7128
639d11d3 7129 if (die->child != NULL)
c906108c 7130 {
8072405b 7131 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7132 struct die_info *child_die;
8072405b 7133 int nparams, iparams;
c906108c
SS
7134
7135 /* Count the number of parameters.
7136 FIXME: GDB currently ignores vararg functions, but knows about
7137 vararg member functions. */
8072405b 7138 nparams = 0;
639d11d3 7139 child_die = die->child;
c906108c
SS
7140 while (child_die && child_die->tag)
7141 {
7142 if (child_die->tag == DW_TAG_formal_parameter)
7143 nparams++;
7144 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7145 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7146 child_die = sibling_die (child_die);
7147 }
7148
7149 /* Allocate storage for parameters and fill them in. */
7150 TYPE_NFIELDS (ftype) = nparams;
7151 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7152 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7153
8072405b
JK
7154 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7155 even if we error out during the parameters reading below. */
7156 for (iparams = 0; iparams < nparams; iparams++)
7157 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7158
7159 iparams = 0;
639d11d3 7160 child_die = die->child;
c906108c
SS
7161 while (child_die && child_die->tag)
7162 {
7163 if (child_die->tag == DW_TAG_formal_parameter)
7164 {
7165 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
7166 member functions. G++ helps GDB by marking the first
7167 parameter for non-static member functions (which is the
7168 this pointer) as artificial. We pass this information
7169 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 7170 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7171 if (attr)
7172 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7173 else
418835cc
KS
7174 {
7175 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7176
7177 /* GCC/43521: In java, the formal parameter
7178 "this" is sometimes not marked with DW_AT_artificial. */
7179 if (cu->language == language_java)
7180 {
7181 const char *name = dwarf2_name (child_die, cu);
9a619af0 7182
418835cc
KS
7183 if (name && !strcmp (name, "this"))
7184 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7185 }
7186 }
e7c27a73 7187 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
7188 iparams++;
7189 }
7190 child_die = sibling_die (child_die);
7191 }
7192 }
7193
76c10ea2 7194 return ftype;
c906108c
SS
7195}
7196
f792889a 7197static struct type *
e7c27a73 7198read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7199{
e7c27a73 7200 struct objfile *objfile = cu->objfile;
0114d602 7201 const char *name = NULL;
f792889a 7202 struct type *this_type;
c906108c 7203
94af9270 7204 name = dwarf2_full_name (NULL, die, cu);
f792889a 7205 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7206 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7207 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7208 set_die_type (die, this_type, cu);
7209 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7210 return this_type;
c906108c
SS
7211}
7212
7213/* Find a representation of a given base type and install
7214 it in the TYPE field of the die. */
7215
f792889a 7216static struct type *
e7c27a73 7217read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7218{
e7c27a73 7219 struct objfile *objfile = cu->objfile;
c906108c
SS
7220 struct type *type;
7221 struct attribute *attr;
7222 int encoding = 0, size = 0;
39cbfefa 7223 char *name;
6ccb9162
UW
7224 enum type_code code = TYPE_CODE_INT;
7225 int type_flags = 0;
7226 struct type *target_type = NULL;
c906108c 7227
e142c38c 7228 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7229 if (attr)
7230 {
7231 encoding = DW_UNSND (attr);
7232 }
e142c38c 7233 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7234 if (attr)
7235 {
7236 size = DW_UNSND (attr);
7237 }
39cbfefa 7238 name = dwarf2_name (die, cu);
6ccb9162 7239 if (!name)
c906108c 7240 {
6ccb9162
UW
7241 complaint (&symfile_complaints,
7242 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7243 }
6ccb9162
UW
7244
7245 switch (encoding)
c906108c 7246 {
6ccb9162
UW
7247 case DW_ATE_address:
7248 /* Turn DW_ATE_address into a void * pointer. */
7249 code = TYPE_CODE_PTR;
7250 type_flags |= TYPE_FLAG_UNSIGNED;
7251 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7252 break;
7253 case DW_ATE_boolean:
7254 code = TYPE_CODE_BOOL;
7255 type_flags |= TYPE_FLAG_UNSIGNED;
7256 break;
7257 case DW_ATE_complex_float:
7258 code = TYPE_CODE_COMPLEX;
7259 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7260 break;
7261 case DW_ATE_decimal_float:
7262 code = TYPE_CODE_DECFLOAT;
7263 break;
7264 case DW_ATE_float:
7265 code = TYPE_CODE_FLT;
7266 break;
7267 case DW_ATE_signed:
7268 break;
7269 case DW_ATE_unsigned:
7270 type_flags |= TYPE_FLAG_UNSIGNED;
7271 break;
7272 case DW_ATE_signed_char:
6e70227d 7273 if (cu->language == language_ada || cu->language == language_m2
868a0084 7274 || cu->language == language_pascal)
6ccb9162
UW
7275 code = TYPE_CODE_CHAR;
7276 break;
7277 case DW_ATE_unsigned_char:
868a0084
PM
7278 if (cu->language == language_ada || cu->language == language_m2
7279 || cu->language == language_pascal)
6ccb9162
UW
7280 code = TYPE_CODE_CHAR;
7281 type_flags |= TYPE_FLAG_UNSIGNED;
7282 break;
75079b2b
TT
7283 case DW_ATE_UTF:
7284 /* We just treat this as an integer and then recognize the
7285 type by name elsewhere. */
7286 break;
7287
6ccb9162
UW
7288 default:
7289 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7290 dwarf_type_encoding_name (encoding));
7291 break;
c906108c 7292 }
6ccb9162 7293
0114d602
DJ
7294 type = init_type (code, size, type_flags, NULL, objfile);
7295 TYPE_NAME (type) = name;
6ccb9162
UW
7296 TYPE_TARGET_TYPE (type) = target_type;
7297
0114d602 7298 if (name && strcmp (name, "char") == 0)
876cecd0 7299 TYPE_NOSIGN (type) = 1;
0114d602 7300
f792889a 7301 return set_die_type (die, type, cu);
c906108c
SS
7302}
7303
a02abb62
JB
7304/* Read the given DW_AT_subrange DIE. */
7305
f792889a 7306static struct type *
a02abb62
JB
7307read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7308{
5e2b427d 7309 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7310 struct type *base_type;
7311 struct type *range_type;
7312 struct attribute *attr;
43bbcdc2
PH
7313 LONGEST low = 0;
7314 LONGEST high = -1;
39cbfefa 7315 char *name;
43bbcdc2 7316 LONGEST negative_mask;
e77813c8 7317
a02abb62 7318 base_type = die_type (die, cu);
953ac07e
JK
7319 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7320 check_typedef (base_type);
a02abb62 7321
7e314c57
JK
7322 /* The die_type call above may have already set the type for this DIE. */
7323 range_type = get_die_type (die, cu);
7324 if (range_type)
7325 return range_type;
7326
e142c38c 7327 if (cu->language == language_fortran)
6e70227d 7328 {
a02abb62
JB
7329 /* FORTRAN implies a lower bound of 1, if not given. */
7330 low = 1;
7331 }
7332
dd5e6932
DJ
7333 /* FIXME: For variable sized arrays either of these could be
7334 a variable rather than a constant value. We'll allow it,
7335 but we don't know how to handle it. */
e142c38c 7336 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7337 if (attr)
7338 low = dwarf2_get_attr_constant_value (attr, 0);
7339
e142c38c 7340 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7341 if (attr)
6e70227d 7342 {
e77813c8 7343 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7344 {
7345 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7346 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7347 FIXME: GDB does not yet know how to handle dynamic
7348 arrays properly, treat them as arrays with unspecified
7349 length for now.
7350
7351 FIXME: jimb/2003-09-22: GDB does not really know
7352 how to handle arrays of unspecified length
7353 either; we just represent them as zero-length
7354 arrays. Choose an appropriate upper bound given
7355 the lower bound we've computed above. */
7356 high = low - 1;
7357 }
7358 else
7359 high = dwarf2_get_attr_constant_value (attr, 1);
7360 }
e77813c8
PM
7361 else
7362 {
7363 attr = dwarf2_attr (die, DW_AT_count, cu);
7364 if (attr)
7365 {
7366 int count = dwarf2_get_attr_constant_value (attr, 1);
7367 high = low + count - 1;
7368 }
7369 }
7370
7371 /* Dwarf-2 specifications explicitly allows to create subrange types
7372 without specifying a base type.
7373 In that case, the base type must be set to the type of
7374 the lower bound, upper bound or count, in that order, if any of these
7375 three attributes references an object that has a type.
7376 If no base type is found, the Dwarf-2 specifications say that
7377 a signed integer type of size equal to the size of an address should
7378 be used.
7379 For the following C code: `extern char gdb_int [];'
7380 GCC produces an empty range DIE.
7381 FIXME: muller/2010-05-28: Possible references to object for low bound,
7382 high bound or count are not yet handled by this code.
7383 */
7384 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7385 {
7386 struct objfile *objfile = cu->objfile;
7387 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7388 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7389 struct type *int_type = objfile_type (objfile)->builtin_int;
7390
7391 /* Test "int", "long int", and "long long int" objfile types,
7392 and select the first one having a size above or equal to the
7393 architecture address size. */
7394 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7395 base_type = int_type;
7396 else
7397 {
7398 int_type = objfile_type (objfile)->builtin_long;
7399 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7400 base_type = int_type;
7401 else
7402 {
7403 int_type = objfile_type (objfile)->builtin_long_long;
7404 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7405 base_type = int_type;
7406 }
7407 }
7408 }
a02abb62 7409
6e70227d 7410 negative_mask =
43bbcdc2
PH
7411 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7412 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7413 low |= negative_mask;
7414 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7415 high |= negative_mask;
7416
a02abb62
JB
7417 range_type = create_range_type (NULL, base_type, low, high);
7418
bbb0eef6
JK
7419 /* Mark arrays with dynamic length at least as an array of unspecified
7420 length. GDB could check the boundary but before it gets implemented at
7421 least allow accessing the array elements. */
7422 if (attr && attr->form == DW_FORM_block1)
7423 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7424
39cbfefa
DJ
7425 name = dwarf2_name (die, cu);
7426 if (name)
7427 TYPE_NAME (range_type) = name;
6e70227d 7428
e142c38c 7429 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
7430 if (attr)
7431 TYPE_LENGTH (range_type) = DW_UNSND (attr);
7432
7e314c57
JK
7433 set_die_type (die, range_type, cu);
7434
7435 /* set_die_type should be already done. */
b4ba55a1
JB
7436 set_descriptive_type (range_type, die, cu);
7437
7e314c57 7438 return range_type;
a02abb62 7439}
6e70227d 7440
f792889a 7441static struct type *
81a17f79
JB
7442read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7443{
7444 struct type *type;
81a17f79 7445
81a17f79
JB
7446 /* For now, we only support the C meaning of an unspecified type: void. */
7447
0114d602
DJ
7448 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7449 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 7450
f792889a 7451 return set_die_type (die, type, cu);
81a17f79 7452}
a02abb62 7453
51545339
DJ
7454/* Trivial hash function for die_info: the hash value of a DIE
7455 is its offset in .debug_info for this objfile. */
7456
7457static hashval_t
7458die_hash (const void *item)
7459{
7460 const struct die_info *die = item;
9a619af0 7461
51545339
DJ
7462 return die->offset;
7463}
7464
7465/* Trivial comparison function for die_info structures: two DIEs
7466 are equal if they have the same offset. */
7467
7468static int
7469die_eq (const void *item_lhs, const void *item_rhs)
7470{
7471 const struct die_info *die_lhs = item_lhs;
7472 const struct die_info *die_rhs = item_rhs;
9a619af0 7473
51545339
DJ
7474 return die_lhs->offset == die_rhs->offset;
7475}
7476
c906108c
SS
7477/* Read a whole compilation unit into a linked list of dies. */
7478
f9aca02d 7479static struct die_info *
93311388 7480read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 7481{
93311388
DE
7482 struct die_reader_specs reader_specs;
7483
348e048f 7484 gdb_assert (cu->die_hash == NULL);
51545339
DJ
7485 cu->die_hash
7486 = htab_create_alloc_ex (cu->header.length / 12,
7487 die_hash,
7488 die_eq,
7489 NULL,
7490 &cu->comp_unit_obstack,
7491 hashtab_obstack_allocate,
7492 dummy_obstack_deallocate);
7493
93311388
DE
7494 init_cu_die_reader (&reader_specs, cu);
7495
7496 return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
639d11d3
DC
7497}
7498
d97bc12b
DE
7499/* Main entry point for reading a DIE and all children.
7500 Read the DIE and dump it if requested. */
7501
7502static struct die_info *
93311388
DE
7503read_die_and_children (const struct die_reader_specs *reader,
7504 gdb_byte *info_ptr,
d97bc12b
DE
7505 gdb_byte **new_info_ptr,
7506 struct die_info *parent)
7507{
93311388 7508 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
7509 new_info_ptr, parent);
7510
7511 if (dwarf2_die_debug)
7512 {
348e048f
DE
7513 fprintf_unfiltered (gdb_stdlog,
7514 "\nRead die from %s of %s:\n",
7515 reader->buffer == dwarf2_per_objfile->info.buffer
7516 ? ".debug_info"
7517 : reader->buffer == dwarf2_per_objfile->types.buffer
7518 ? ".debug_types"
7519 : "unknown section",
7520 reader->abfd->filename);
d97bc12b
DE
7521 dump_die (result, dwarf2_die_debug);
7522 }
7523
7524 return result;
7525}
7526
639d11d3
DC
7527/* Read a single die and all its descendents. Set the die's sibling
7528 field to NULL; set other fields in the die correctly, and set all
7529 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
7530 location of the info_ptr after reading all of those dies. PARENT
7531 is the parent of the die in question. */
7532
7533static struct die_info *
93311388
DE
7534read_die_and_children_1 (const struct die_reader_specs *reader,
7535 gdb_byte *info_ptr,
d97bc12b
DE
7536 gdb_byte **new_info_ptr,
7537 struct die_info *parent)
639d11d3
DC
7538{
7539 struct die_info *die;
fe1b8b76 7540 gdb_byte *cur_ptr;
639d11d3
DC
7541 int has_children;
7542
93311388 7543 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
7544 if (die == NULL)
7545 {
7546 *new_info_ptr = cur_ptr;
7547 return NULL;
7548 }
93311388 7549 store_in_ref_table (die, reader->cu);
639d11d3
DC
7550
7551 if (has_children)
348e048f 7552 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
7553 else
7554 {
7555 die->child = NULL;
7556 *new_info_ptr = cur_ptr;
7557 }
7558
7559 die->sibling = NULL;
7560 die->parent = parent;
7561 return die;
7562}
7563
7564/* Read a die, all of its descendents, and all of its siblings; set
7565 all of the fields of all of the dies correctly. Arguments are as
7566 in read_die_and_children. */
7567
7568static struct die_info *
93311388
DE
7569read_die_and_siblings (const struct die_reader_specs *reader,
7570 gdb_byte *info_ptr,
fe1b8b76 7571 gdb_byte **new_info_ptr,
639d11d3
DC
7572 struct die_info *parent)
7573{
7574 struct die_info *first_die, *last_sibling;
fe1b8b76 7575 gdb_byte *cur_ptr;
639d11d3 7576
c906108c 7577 cur_ptr = info_ptr;
639d11d3
DC
7578 first_die = last_sibling = NULL;
7579
7580 while (1)
c906108c 7581 {
639d11d3 7582 struct die_info *die
93311388 7583 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 7584
1d325ec1 7585 if (die == NULL)
c906108c 7586 {
639d11d3
DC
7587 *new_info_ptr = cur_ptr;
7588 return first_die;
c906108c 7589 }
1d325ec1
DJ
7590
7591 if (!first_die)
7592 first_die = die;
c906108c 7593 else
1d325ec1
DJ
7594 last_sibling->sibling = die;
7595
7596 last_sibling = die;
c906108c 7597 }
c906108c
SS
7598}
7599
93311388
DE
7600/* Read the die from the .debug_info section buffer. Set DIEP to
7601 point to a newly allocated die with its information, except for its
7602 child, sibling, and parent fields. Set HAS_CHILDREN to tell
7603 whether the die has children or not. */
7604
7605static gdb_byte *
7606read_full_die (const struct die_reader_specs *reader,
7607 struct die_info **diep, gdb_byte *info_ptr,
7608 int *has_children)
7609{
7610 unsigned int abbrev_number, bytes_read, i, offset;
7611 struct abbrev_info *abbrev;
7612 struct die_info *die;
7613 struct dwarf2_cu *cu = reader->cu;
7614 bfd *abfd = reader->abfd;
7615
7616 offset = info_ptr - reader->buffer;
7617 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7618 info_ptr += bytes_read;
7619 if (!abbrev_number)
7620 {
7621 *diep = NULL;
7622 *has_children = 0;
7623 return info_ptr;
7624 }
7625
7626 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
7627 if (!abbrev)
348e048f
DE
7628 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
7629 abbrev_number,
7630 bfd_get_filename (abfd));
7631
93311388
DE
7632 die = dwarf_alloc_die (cu, abbrev->num_attrs);
7633 die->offset = offset;
7634 die->tag = abbrev->tag;
7635 die->abbrev = abbrev_number;
7636
7637 die->num_attrs = abbrev->num_attrs;
7638
7639 for (i = 0; i < abbrev->num_attrs; ++i)
7640 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
7641 abfd, info_ptr, cu);
7642
7643 *diep = die;
7644 *has_children = abbrev->has_children;
7645 return info_ptr;
7646}
7647
c906108c
SS
7648/* In DWARF version 2, the description of the debugging information is
7649 stored in a separate .debug_abbrev section. Before we read any
7650 dies from a section we read in all abbreviations and install them
72bf9492
DJ
7651 in a hash table. This function also sets flags in CU describing
7652 the data found in the abbrev table. */
c906108c
SS
7653
7654static void
e7c27a73 7655dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 7656{
e7c27a73 7657 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 7658 gdb_byte *abbrev_ptr;
c906108c
SS
7659 struct abbrev_info *cur_abbrev;
7660 unsigned int abbrev_number, bytes_read, abbrev_name;
7661 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
7662 struct attr_abbrev *cur_attrs;
7663 unsigned int allocated_attrs;
c906108c 7664
57349743 7665 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
7666 obstack_init (&cu->abbrev_obstack);
7667 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
7668 (ABBREV_HASH_SIZE
7669 * sizeof (struct abbrev_info *)));
7670 memset (cu->dwarf2_abbrevs, 0,
7671 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 7672
be391dca
TT
7673 dwarf2_read_section (dwarf2_per_objfile->objfile,
7674 &dwarf2_per_objfile->abbrev);
dce234bc 7675 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
7676 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7677 abbrev_ptr += bytes_read;
7678
f3dd6933
DJ
7679 allocated_attrs = ATTR_ALLOC_CHUNK;
7680 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 7681
c906108c
SS
7682 /* loop until we reach an abbrev number of 0 */
7683 while (abbrev_number)
7684 {
f3dd6933 7685 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
7686
7687 /* read in abbrev header */
7688 cur_abbrev->number = abbrev_number;
7689 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7690 abbrev_ptr += bytes_read;
7691 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7692 abbrev_ptr += 1;
7693
72bf9492
DJ
7694 if (cur_abbrev->tag == DW_TAG_namespace)
7695 cu->has_namespace_info = 1;
7696
c906108c
SS
7697 /* now read in declarations */
7698 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7699 abbrev_ptr += bytes_read;
7700 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7701 abbrev_ptr += bytes_read;
7702 while (abbrev_name)
7703 {
f3dd6933 7704 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 7705 {
f3dd6933
DJ
7706 allocated_attrs += ATTR_ALLOC_CHUNK;
7707 cur_attrs
7708 = xrealloc (cur_attrs, (allocated_attrs
7709 * sizeof (struct attr_abbrev)));
c906108c 7710 }
ae038cb0
DJ
7711
7712 /* Record whether this compilation unit might have
7713 inter-compilation-unit references. If we don't know what form
7714 this attribute will have, then it might potentially be a
7715 DW_FORM_ref_addr, so we conservatively expect inter-CU
7716 references. */
7717
7718 if (abbrev_form == DW_FORM_ref_addr
7719 || abbrev_form == DW_FORM_indirect)
7720 cu->has_form_ref_addr = 1;
7721
f3dd6933
DJ
7722 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
7723 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
7724 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7725 abbrev_ptr += bytes_read;
7726 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7727 abbrev_ptr += bytes_read;
7728 }
7729
f3dd6933
DJ
7730 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
7731 (cur_abbrev->num_attrs
7732 * sizeof (struct attr_abbrev)));
7733 memcpy (cur_abbrev->attrs, cur_attrs,
7734 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
7735
c906108c 7736 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
7737 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
7738 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
7739
7740 /* Get next abbreviation.
7741 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
7742 always properly terminated with an abbrev number of 0.
7743 Exit loop if we encounter an abbreviation which we have
7744 already read (which means we are about to read the abbreviations
7745 for the next compile unit) or if the end of the abbreviation
7746 table is reached. */
dce234bc
PP
7747 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
7748 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
7749 break;
7750 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7751 abbrev_ptr += bytes_read;
e7c27a73 7752 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
7753 break;
7754 }
f3dd6933
DJ
7755
7756 xfree (cur_attrs);
c906108c
SS
7757}
7758
f3dd6933 7759/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 7760
c906108c 7761static void
f3dd6933 7762dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 7763{
f3dd6933 7764 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 7765
f3dd6933
DJ
7766 obstack_free (&cu->abbrev_obstack, NULL);
7767 cu->dwarf2_abbrevs = NULL;
c906108c
SS
7768}
7769
7770/* Lookup an abbrev_info structure in the abbrev hash table. */
7771
7772static struct abbrev_info *
e7c27a73 7773dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
7774{
7775 unsigned int hash_number;
7776 struct abbrev_info *abbrev;
7777
7778 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 7779 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
7780
7781 while (abbrev)
7782 {
7783 if (abbrev->number == number)
7784 return abbrev;
7785 else
7786 abbrev = abbrev->next;
7787 }
7788 return NULL;
7789}
7790
72bf9492
DJ
7791/* Returns nonzero if TAG represents a type that we might generate a partial
7792 symbol for. */
7793
7794static int
7795is_type_tag_for_partial (int tag)
7796{
7797 switch (tag)
7798 {
7799#if 0
7800 /* Some types that would be reasonable to generate partial symbols for,
7801 that we don't at present. */
7802 case DW_TAG_array_type:
7803 case DW_TAG_file_type:
7804 case DW_TAG_ptr_to_member_type:
7805 case DW_TAG_set_type:
7806 case DW_TAG_string_type:
7807 case DW_TAG_subroutine_type:
7808#endif
7809 case DW_TAG_base_type:
7810 case DW_TAG_class_type:
680b30c7 7811 case DW_TAG_interface_type:
72bf9492
DJ
7812 case DW_TAG_enumeration_type:
7813 case DW_TAG_structure_type:
7814 case DW_TAG_subrange_type:
7815 case DW_TAG_typedef:
7816 case DW_TAG_union_type:
7817 return 1;
7818 default:
7819 return 0;
7820 }
7821}
7822
7823/* Load all DIEs that are interesting for partial symbols into memory. */
7824
7825static struct partial_die_info *
93311388
DE
7826load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
7827 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
7828{
7829 struct partial_die_info *part_die;
7830 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
7831 struct abbrev_info *abbrev;
7832 unsigned int bytes_read;
5afb4e99 7833 unsigned int load_all = 0;
72bf9492
DJ
7834
7835 int nesting_level = 1;
7836
7837 parent_die = NULL;
7838 last_die = NULL;
7839
5afb4e99
DJ
7840 if (cu->per_cu && cu->per_cu->load_all_dies)
7841 load_all = 1;
7842
72bf9492
DJ
7843 cu->partial_dies
7844 = htab_create_alloc_ex (cu->header.length / 12,
7845 partial_die_hash,
7846 partial_die_eq,
7847 NULL,
7848 &cu->comp_unit_obstack,
7849 hashtab_obstack_allocate,
7850 dummy_obstack_deallocate);
7851
7852 part_die = obstack_alloc (&cu->comp_unit_obstack,
7853 sizeof (struct partial_die_info));
7854
7855 while (1)
7856 {
7857 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7858
7859 /* A NULL abbrev means the end of a series of children. */
7860 if (abbrev == NULL)
7861 {
7862 if (--nesting_level == 0)
7863 {
7864 /* PART_DIE was probably the last thing allocated on the
7865 comp_unit_obstack, so we could call obstack_free
7866 here. We don't do that because the waste is small,
7867 and will be cleaned up when we're done with this
7868 compilation unit. This way, we're also more robust
7869 against other users of the comp_unit_obstack. */
7870 return first_die;
7871 }
7872 info_ptr += bytes_read;
7873 last_die = parent_die;
7874 parent_die = parent_die->die_parent;
7875 continue;
7876 }
7877
5afb4e99
DJ
7878 /* Check whether this DIE is interesting enough to save. Normally
7879 we would not be interested in members here, but there may be
7880 later variables referencing them via DW_AT_specification (for
7881 static members). */
7882 if (!load_all
7883 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
7884 && abbrev->tag != DW_TAG_enumerator
7885 && abbrev->tag != DW_TAG_subprogram
bc30ff58 7886 && abbrev->tag != DW_TAG_lexical_block
72bf9492 7887 && abbrev->tag != DW_TAG_variable
5afb4e99 7888 && abbrev->tag != DW_TAG_namespace
f55ee35c 7889 && abbrev->tag != DW_TAG_module
5afb4e99 7890 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
7891 {
7892 /* Otherwise we skip to the next sibling, if any. */
93311388 7893 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
7894 continue;
7895 }
7896
93311388
DE
7897 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
7898 buffer, info_ptr, cu);
72bf9492
DJ
7899
7900 /* This two-pass algorithm for processing partial symbols has a
7901 high cost in cache pressure. Thus, handle some simple cases
7902 here which cover the majority of C partial symbols. DIEs
7903 which neither have specification tags in them, nor could have
7904 specification tags elsewhere pointing at them, can simply be
7905 processed and discarded.
7906
7907 This segment is also optional; scan_partial_symbols and
7908 add_partial_symbol will handle these DIEs if we chain
7909 them in normally. When compilers which do not emit large
7910 quantities of duplicate debug information are more common,
7911 this code can probably be removed. */
7912
7913 /* Any complete simple types at the top level (pretty much all
7914 of them, for a language without namespaces), can be processed
7915 directly. */
7916 if (parent_die == NULL
7917 && part_die->has_specification == 0
7918 && part_die->is_declaration == 0
7919 && (part_die->tag == DW_TAG_typedef
7920 || part_die->tag == DW_TAG_base_type
7921 || part_die->tag == DW_TAG_subrange_type))
7922 {
7923 if (building_psymtab && part_die->name != NULL)
04a679b8 7924 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
7925 VAR_DOMAIN, LOC_TYPEDEF,
7926 &cu->objfile->static_psymbols,
7927 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 7928 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
7929 continue;
7930 }
7931
7932 /* If we're at the second level, and we're an enumerator, and
7933 our parent has no specification (meaning possibly lives in a
7934 namespace elsewhere), then we can add the partial symbol now
7935 instead of queueing it. */
7936 if (part_die->tag == DW_TAG_enumerator
7937 && parent_die != NULL
7938 && parent_die->die_parent == NULL
7939 && parent_die->tag == DW_TAG_enumeration_type
7940 && parent_die->has_specification == 0)
7941 {
7942 if (part_die->name == NULL)
e2e0b3e5 7943 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 7944 else if (building_psymtab)
04a679b8 7945 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 7946 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
7947 (cu->language == language_cplus
7948 || cu->language == language_java)
72bf9492
DJ
7949 ? &cu->objfile->global_psymbols
7950 : &cu->objfile->static_psymbols,
7951 0, (CORE_ADDR) 0, cu->language, cu->objfile);
7952
93311388 7953 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
7954 continue;
7955 }
7956
7957 /* We'll save this DIE so link it in. */
7958 part_die->die_parent = parent_die;
7959 part_die->die_sibling = NULL;
7960 part_die->die_child = NULL;
7961
7962 if (last_die && last_die == parent_die)
7963 last_die->die_child = part_die;
7964 else if (last_die)
7965 last_die->die_sibling = part_die;
7966
7967 last_die = part_die;
7968
7969 if (first_die == NULL)
7970 first_die = part_die;
7971
7972 /* Maybe add the DIE to the hash table. Not all DIEs that we
7973 find interesting need to be in the hash table, because we
7974 also have the parent/sibling/child chains; only those that we
7975 might refer to by offset later during partial symbol reading.
7976
7977 For now this means things that might have be the target of a
7978 DW_AT_specification, DW_AT_abstract_origin, or
7979 DW_AT_extension. DW_AT_extension will refer only to
7980 namespaces; DW_AT_abstract_origin refers to functions (and
7981 many things under the function DIE, but we do not recurse
7982 into function DIEs during partial symbol reading) and
7983 possibly variables as well; DW_AT_specification refers to
7984 declarations. Declarations ought to have the DW_AT_declaration
7985 flag. It happens that GCC forgets to put it in sometimes, but
7986 only for functions, not for types.
7987
7988 Adding more things than necessary to the hash table is harmless
7989 except for the performance cost. Adding too few will result in
5afb4e99
DJ
7990 wasted time in find_partial_die, when we reread the compilation
7991 unit with load_all_dies set. */
72bf9492 7992
5afb4e99
DJ
7993 if (load_all
7994 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
7995 || abbrev->tag == DW_TAG_variable
7996 || abbrev->tag == DW_TAG_namespace
7997 || part_die->is_declaration)
7998 {
7999 void **slot;
8000
8001 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8002 part_die->offset, INSERT);
8003 *slot = part_die;
8004 }
8005
8006 part_die = obstack_alloc (&cu->comp_unit_obstack,
8007 sizeof (struct partial_die_info));
8008
8009 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8010 we have no reason to follow the children of structures; for other
72bf9492 8011 languages we have to, both so that we can get at method physnames
bc30ff58
JB
8012 to infer fully qualified class names, and for DW_AT_specification.
8013
8014 For Ada, we need to scan the children of subprograms and lexical
8015 blocks as well because Ada allows the definition of nested
8016 entities that could be interesting for the debugger, such as
8017 nested subprograms for instance. */
72bf9492 8018 if (last_die->has_children
5afb4e99
DJ
8019 && (load_all
8020 || last_die->tag == DW_TAG_namespace
f55ee35c 8021 || last_die->tag == DW_TAG_module
72bf9492
DJ
8022 || last_die->tag == DW_TAG_enumeration_type
8023 || (cu->language != language_c
8024 && (last_die->tag == DW_TAG_class_type
680b30c7 8025 || last_die->tag == DW_TAG_interface_type
72bf9492 8026 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8027 || last_die->tag == DW_TAG_union_type))
8028 || (cu->language == language_ada
8029 && (last_die->tag == DW_TAG_subprogram
8030 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8031 {
8032 nesting_level++;
8033 parent_die = last_die;
8034 continue;
8035 }
8036
8037 /* Otherwise we skip to the next sibling, if any. */
93311388 8038 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8039
8040 /* Back to the top, do it again. */
8041 }
8042}
8043
c906108c
SS
8044/* Read a minimal amount of information into the minimal die structure. */
8045
fe1b8b76 8046static gdb_byte *
72bf9492
DJ
8047read_partial_die (struct partial_die_info *part_die,
8048 struct abbrev_info *abbrev,
8049 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8050 gdb_byte *buffer, gdb_byte *info_ptr,
8051 struct dwarf2_cu *cu)
c906108c 8052{
fa238c03 8053 unsigned int i;
c906108c 8054 struct attribute attr;
c5aa993b 8055 int has_low_pc_attr = 0;
c906108c
SS
8056 int has_high_pc_attr = 0;
8057
72bf9492 8058 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8059
93311388 8060 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8061
8062 info_ptr += abbrev_len;
8063
8064 if (abbrev == NULL)
8065 return info_ptr;
8066
c906108c
SS
8067 part_die->tag = abbrev->tag;
8068 part_die->has_children = abbrev->has_children;
c906108c
SS
8069
8070 for (i = 0; i < abbrev->num_attrs; ++i)
8071 {
e7c27a73 8072 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8073
8074 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8075 partial symbol table. */
c906108c
SS
8076 switch (attr.name)
8077 {
8078 case DW_AT_name:
71c25dea
TT
8079 switch (part_die->tag)
8080 {
8081 case DW_TAG_compile_unit:
348e048f 8082 case DW_TAG_type_unit:
71c25dea
TT
8083 /* Compilation units have a DW_AT_name that is a filename, not
8084 a source language identifier. */
8085 case DW_TAG_enumeration_type:
8086 case DW_TAG_enumerator:
8087 /* These tags always have simple identifiers already; no need
8088 to canonicalize them. */
8089 part_die->name = DW_STRING (&attr);
8090 break;
8091 default:
8092 part_die->name
8093 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8094 &cu->objfile->objfile_obstack);
71c25dea
TT
8095 break;
8096 }
c906108c 8097 break;
31ef98ae 8098 case DW_AT_linkage_name:
c906108c 8099 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8100 /* Note that both forms of linkage name might appear. We
8101 assume they will be the same, and we only store the last
8102 one we see. */
94af9270
KS
8103 if (cu->language == language_ada)
8104 part_die->name = DW_STRING (&attr);
c906108c
SS
8105 break;
8106 case DW_AT_low_pc:
8107 has_low_pc_attr = 1;
8108 part_die->lowpc = DW_ADDR (&attr);
8109 break;
8110 case DW_AT_high_pc:
8111 has_high_pc_attr = 1;
8112 part_die->highpc = DW_ADDR (&attr);
8113 break;
8114 case DW_AT_location:
8e19ed76
PS
8115 /* Support the .debug_loc offsets */
8116 if (attr_form_is_block (&attr))
8117 {
8118 part_die->locdesc = DW_BLOCK (&attr);
8119 }
3690dd37 8120 else if (attr_form_is_section_offset (&attr))
8e19ed76 8121 {
4d3c2250 8122 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8123 }
8124 else
8125 {
4d3c2250
KB
8126 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8127 "partial symbol information");
8e19ed76 8128 }
c906108c 8129 break;
c906108c
SS
8130 case DW_AT_external:
8131 part_die->is_external = DW_UNSND (&attr);
8132 break;
8133 case DW_AT_declaration:
8134 part_die->is_declaration = DW_UNSND (&attr);
8135 break;
8136 case DW_AT_type:
8137 part_die->has_type = 1;
8138 break;
8139 case DW_AT_abstract_origin:
8140 case DW_AT_specification:
72bf9492
DJ
8141 case DW_AT_extension:
8142 part_die->has_specification = 1;
c764a876 8143 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8144 break;
8145 case DW_AT_sibling:
8146 /* Ignore absolute siblings, they might point outside of
8147 the current compile unit. */
8148 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8149 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8150 else
93311388 8151 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8152 break;
fa4028e9
JB
8153 case DW_AT_byte_size:
8154 part_die->has_byte_size = 1;
8155 break;
68511cec
CES
8156 case DW_AT_calling_convention:
8157 /* DWARF doesn't provide a way to identify a program's source-level
8158 entry point. DW_AT_calling_convention attributes are only meant
8159 to describe functions' calling conventions.
8160
8161 However, because it's a necessary piece of information in
8162 Fortran, and because DW_CC_program is the only piece of debugging
8163 information whose definition refers to a 'main program' at all,
8164 several compilers have begun marking Fortran main programs with
8165 DW_CC_program --- even when those functions use the standard
8166 calling conventions.
8167
8168 So until DWARF specifies a way to provide this information and
8169 compilers pick up the new representation, we'll support this
8170 practice. */
8171 if (DW_UNSND (&attr) == DW_CC_program
8172 && cu->language == language_fortran)
8173 set_main_name (part_die->name);
8174 break;
c906108c
SS
8175 default:
8176 break;
8177 }
8178 }
8179
c906108c
SS
8180 /* When using the GNU linker, .gnu.linkonce. sections are used to
8181 eliminate duplicate copies of functions and vtables and such.
8182 The linker will arbitrarily choose one and discard the others.
8183 The AT_*_pc values for such functions refer to local labels in
8184 these sections. If the section from that file was discarded, the
8185 labels are not in the output, so the relocs get a value of 0.
8186 If this is a discarded function, mark the pc bounds as invalid,
8187 so that GDB will ignore it. */
8188 if (has_low_pc_attr && has_high_pc_attr
8189 && part_die->lowpc < part_die->highpc
8190 && (part_die->lowpc != 0
72dca2f5 8191 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8192 part_die->has_pc_info = 1;
85cbf3d3 8193
c906108c
SS
8194 return info_ptr;
8195}
8196
72bf9492
DJ
8197/* Find a cached partial DIE at OFFSET in CU. */
8198
8199static struct partial_die_info *
c764a876 8200find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8201{
8202 struct partial_die_info *lookup_die = NULL;
8203 struct partial_die_info part_die;
8204
8205 part_die.offset = offset;
8206 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8207
72bf9492
DJ
8208 return lookup_die;
8209}
8210
348e048f
DE
8211/* Find a partial DIE at OFFSET, which may or may not be in CU,
8212 except in the case of .debug_types DIEs which do not reference
8213 outside their CU (they do however referencing other types via
8214 DW_FORM_sig8). */
72bf9492
DJ
8215
8216static struct partial_die_info *
c764a876 8217find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8218{
5afb4e99
DJ
8219 struct dwarf2_per_cu_data *per_cu = NULL;
8220 struct partial_die_info *pd = NULL;
72bf9492 8221
348e048f
DE
8222 if (cu->per_cu->from_debug_types)
8223 {
8224 pd = find_partial_die_in_comp_unit (offset, cu);
8225 if (pd != NULL)
8226 return pd;
8227 goto not_found;
8228 }
8229
45452591 8230 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8231 {
8232 pd = find_partial_die_in_comp_unit (offset, cu);
8233 if (pd != NULL)
8234 return pd;
8235 }
72bf9492 8236
ae038cb0
DJ
8237 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8238
ae038cb0
DJ
8239 if (per_cu->cu == NULL)
8240 {
93311388 8241 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8242 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8243 dwarf2_per_objfile->read_in_chain = per_cu;
8244 }
8245
8246 per_cu->cu->last_used = 0;
5afb4e99
DJ
8247 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8248
8249 if (pd == NULL && per_cu->load_all_dies == 0)
8250 {
8251 struct cleanup *back_to;
8252 struct partial_die_info comp_unit_die;
8253 struct abbrev_info *abbrev;
8254 unsigned int bytes_read;
8255 char *info_ptr;
8256
8257 per_cu->load_all_dies = 1;
8258
8259 /* Re-read the DIEs. */
8260 back_to = make_cleanup (null_cleanup, 0);
8261 if (per_cu->cu->dwarf2_abbrevs == NULL)
8262 {
8263 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8264 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8265 }
dce234bc 8266 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8267 + per_cu->cu->header.offset
8268 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8269 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8270 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8271 per_cu->cu->objfile->obfd,
8272 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8273 per_cu->cu);
8274 if (comp_unit_die.has_children)
93311388
DE
8275 load_partial_dies (per_cu->cu->objfile->obfd,
8276 dwarf2_per_objfile->info.buffer, info_ptr,
8277 0, per_cu->cu);
5afb4e99
DJ
8278 do_cleanups (back_to);
8279
8280 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8281 }
8282
348e048f
DE
8283 not_found:
8284
5afb4e99
DJ
8285 if (pd == NULL)
8286 internal_error (__FILE__, __LINE__,
c764a876 8287 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8288 offset, bfd_get_filename (cu->objfile->obfd));
8289 return pd;
72bf9492
DJ
8290}
8291
8292/* Adjust PART_DIE before generating a symbol for it. This function
8293 may set the is_external flag or change the DIE's name. */
8294
8295static void
8296fixup_partial_die (struct partial_die_info *part_die,
8297 struct dwarf2_cu *cu)
8298{
8299 /* If we found a reference attribute and the DIE has no name, try
8300 to find a name in the referred to DIE. */
8301
8302 if (part_die->name == NULL && part_die->has_specification)
8303 {
8304 struct partial_die_info *spec_die;
72bf9492 8305
10b3939b 8306 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8307
10b3939b 8308 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8309
8310 if (spec_die->name)
8311 {
8312 part_die->name = spec_die->name;
8313
8314 /* Copy DW_AT_external attribute if it is set. */
8315 if (spec_die->is_external)
8316 part_die->is_external = spec_die->is_external;
8317 }
8318 }
8319
8320 /* Set default names for some unnamed DIEs. */
8321 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8322 || part_die->tag == DW_TAG_class_type))
8323 part_die->name = "(anonymous class)";
8324
8325 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8326 part_die->name = "(anonymous namespace)";
8327
8328 if (part_die->tag == DW_TAG_structure_type
8329 || part_die->tag == DW_TAG_class_type
8330 || part_die->tag == DW_TAG_union_type)
8331 guess_structure_name (part_die, cu);
8332}
8333
a8329558 8334/* Read an attribute value described by an attribute form. */
c906108c 8335
fe1b8b76 8336static gdb_byte *
a8329558 8337read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 8338 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 8339 struct dwarf2_cu *cu)
c906108c 8340{
e7c27a73 8341 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8342 unsigned int bytes_read;
8343 struct dwarf_block *blk;
8344
a8329558
KW
8345 attr->form = form;
8346 switch (form)
c906108c 8347 {
c906108c 8348 case DW_FORM_ref_addr:
ae411497
TT
8349 if (cu->header.version == 2)
8350 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8351 else
8352 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8353 info_ptr += bytes_read;
8354 break;
8355 case DW_FORM_addr:
e7c27a73 8356 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 8357 info_ptr += bytes_read;
c906108c
SS
8358 break;
8359 case DW_FORM_block2:
7b5a2f43 8360 blk = dwarf_alloc_block (cu);
c906108c
SS
8361 blk->size = read_2_bytes (abfd, info_ptr);
8362 info_ptr += 2;
8363 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8364 info_ptr += blk->size;
8365 DW_BLOCK (attr) = blk;
8366 break;
8367 case DW_FORM_block4:
7b5a2f43 8368 blk = dwarf_alloc_block (cu);
c906108c
SS
8369 blk->size = read_4_bytes (abfd, info_ptr);
8370 info_ptr += 4;
8371 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8372 info_ptr += blk->size;
8373 DW_BLOCK (attr) = blk;
8374 break;
8375 case DW_FORM_data2:
8376 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8377 info_ptr += 2;
8378 break;
8379 case DW_FORM_data4:
8380 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8381 info_ptr += 4;
8382 break;
8383 case DW_FORM_data8:
8384 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8385 info_ptr += 8;
8386 break;
2dc7f7b3
TT
8387 case DW_FORM_sec_offset:
8388 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8389 info_ptr += bytes_read;
8390 break;
c906108c
SS
8391 case DW_FORM_string:
8392 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
8285870a 8393 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
8394 info_ptr += bytes_read;
8395 break;
4bdf3d34
JJ
8396 case DW_FORM_strp:
8397 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8398 &bytes_read);
8285870a 8399 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
8400 info_ptr += bytes_read;
8401 break;
2dc7f7b3 8402 case DW_FORM_exprloc:
c906108c 8403 case DW_FORM_block:
7b5a2f43 8404 blk = dwarf_alloc_block (cu);
c906108c
SS
8405 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8406 info_ptr += bytes_read;
8407 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8408 info_ptr += blk->size;
8409 DW_BLOCK (attr) = blk;
8410 break;
8411 case DW_FORM_block1:
7b5a2f43 8412 blk = dwarf_alloc_block (cu);
c906108c
SS
8413 blk->size = read_1_byte (abfd, info_ptr);
8414 info_ptr += 1;
8415 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8416 info_ptr += blk->size;
8417 DW_BLOCK (attr) = blk;
8418 break;
8419 case DW_FORM_data1:
8420 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8421 info_ptr += 1;
8422 break;
8423 case DW_FORM_flag:
8424 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8425 info_ptr += 1;
8426 break;
2dc7f7b3
TT
8427 case DW_FORM_flag_present:
8428 DW_UNSND (attr) = 1;
8429 break;
c906108c
SS
8430 case DW_FORM_sdata:
8431 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8432 info_ptr += bytes_read;
8433 break;
8434 case DW_FORM_udata:
8435 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8436 info_ptr += bytes_read;
8437 break;
8438 case DW_FORM_ref1:
10b3939b 8439 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
8440 info_ptr += 1;
8441 break;
8442 case DW_FORM_ref2:
10b3939b 8443 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
8444 info_ptr += 2;
8445 break;
8446 case DW_FORM_ref4:
10b3939b 8447 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
8448 info_ptr += 4;
8449 break;
613e1657 8450 case DW_FORM_ref8:
10b3939b 8451 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
8452 info_ptr += 8;
8453 break;
348e048f
DE
8454 case DW_FORM_sig8:
8455 /* Convert the signature to something we can record in DW_UNSND
8456 for later lookup.
8457 NOTE: This is NULL if the type wasn't found. */
8458 DW_SIGNATURED_TYPE (attr) =
8459 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
8460 info_ptr += 8;
8461 break;
c906108c 8462 case DW_FORM_ref_udata:
10b3939b
DJ
8463 DW_ADDR (attr) = (cu->header.offset
8464 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
8465 info_ptr += bytes_read;
8466 break;
c906108c 8467 case DW_FORM_indirect:
a8329558
KW
8468 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8469 info_ptr += bytes_read;
e7c27a73 8470 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 8471 break;
c906108c 8472 default:
8a3fe4f8 8473 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
8474 dwarf_form_name (form),
8475 bfd_get_filename (abfd));
c906108c 8476 }
28e94949
JB
8477
8478 /* We have seen instances where the compiler tried to emit a byte
8479 size attribute of -1 which ended up being encoded as an unsigned
8480 0xffffffff. Although 0xffffffff is technically a valid size value,
8481 an object of this size seems pretty unlikely so we can relatively
8482 safely treat these cases as if the size attribute was invalid and
8483 treat them as zero by default. */
8484 if (attr->name == DW_AT_byte_size
8485 && form == DW_FORM_data4
8486 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
8487 {
8488 complaint
8489 (&symfile_complaints,
43bbcdc2
PH
8490 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
8491 hex_string (DW_UNSND (attr)));
01c66ae6
JB
8492 DW_UNSND (attr) = 0;
8493 }
28e94949 8494
c906108c
SS
8495 return info_ptr;
8496}
8497
a8329558
KW
8498/* Read an attribute described by an abbreviated attribute. */
8499
fe1b8b76 8500static gdb_byte *
a8329558 8501read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 8502 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
8503{
8504 attr->name = abbrev->name;
e7c27a73 8505 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
8506}
8507
c906108c
SS
8508/* read dwarf information from a buffer */
8509
8510static unsigned int
fe1b8b76 8511read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 8512{
fe1b8b76 8513 return bfd_get_8 (abfd, buf);
c906108c
SS
8514}
8515
8516static int
fe1b8b76 8517read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 8518{
fe1b8b76 8519 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
8520}
8521
8522static unsigned int
fe1b8b76 8523read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8524{
fe1b8b76 8525 return bfd_get_16 (abfd, buf);
c906108c
SS
8526}
8527
8528static int
fe1b8b76 8529read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8530{
fe1b8b76 8531 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
8532}
8533
8534static unsigned int
fe1b8b76 8535read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8536{
fe1b8b76 8537 return bfd_get_32 (abfd, buf);
c906108c
SS
8538}
8539
8540static int
fe1b8b76 8541read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8542{
fe1b8b76 8543 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
8544}
8545
93311388 8546static ULONGEST
fe1b8b76 8547read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8548{
fe1b8b76 8549 return bfd_get_64 (abfd, buf);
c906108c
SS
8550}
8551
8552static CORE_ADDR
fe1b8b76 8553read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 8554 unsigned int *bytes_read)
c906108c 8555{
e7c27a73 8556 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8557 CORE_ADDR retval = 0;
8558
107d2387 8559 if (cu_header->signed_addr_p)
c906108c 8560 {
107d2387
AC
8561 switch (cu_header->addr_size)
8562 {
8563 case 2:
fe1b8b76 8564 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
8565 break;
8566 case 4:
fe1b8b76 8567 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
8568 break;
8569 case 8:
fe1b8b76 8570 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
8571 break;
8572 default:
8e65ff28 8573 internal_error (__FILE__, __LINE__,
e2e0b3e5 8574 _("read_address: bad switch, signed [in module %s]"),
659b0389 8575 bfd_get_filename (abfd));
107d2387
AC
8576 }
8577 }
8578 else
8579 {
8580 switch (cu_header->addr_size)
8581 {
8582 case 2:
fe1b8b76 8583 retval = bfd_get_16 (abfd, buf);
107d2387
AC
8584 break;
8585 case 4:
fe1b8b76 8586 retval = bfd_get_32 (abfd, buf);
107d2387
AC
8587 break;
8588 case 8:
fe1b8b76 8589 retval = bfd_get_64 (abfd, buf);
107d2387
AC
8590 break;
8591 default:
8e65ff28 8592 internal_error (__FILE__, __LINE__,
e2e0b3e5 8593 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 8594 bfd_get_filename (abfd));
107d2387 8595 }
c906108c 8596 }
64367e0a 8597
107d2387
AC
8598 *bytes_read = cu_header->addr_size;
8599 return retval;
c906108c
SS
8600}
8601
f7ef9339 8602/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
8603 specification allows the initial length to take up either 4 bytes
8604 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
8605 bytes describe the length and all offsets will be 8 bytes in length
8606 instead of 4.
8607
f7ef9339
KB
8608 An older, non-standard 64-bit format is also handled by this
8609 function. The older format in question stores the initial length
8610 as an 8-byte quantity without an escape value. Lengths greater
8611 than 2^32 aren't very common which means that the initial 4 bytes
8612 is almost always zero. Since a length value of zero doesn't make
8613 sense for the 32-bit format, this initial zero can be considered to
8614 be an escape value which indicates the presence of the older 64-bit
8615 format. As written, the code can't detect (old format) lengths
917c78fc
MK
8616 greater than 4GB. If it becomes necessary to handle lengths
8617 somewhat larger than 4GB, we could allow other small values (such
8618 as the non-sensical values of 1, 2, and 3) to also be used as
8619 escape values indicating the presence of the old format.
f7ef9339 8620
917c78fc
MK
8621 The value returned via bytes_read should be used to increment the
8622 relevant pointer after calling read_initial_length().
c764a876 8623
613e1657
KB
8624 [ Note: read_initial_length() and read_offset() are based on the
8625 document entitled "DWARF Debugging Information Format", revision
f7ef9339 8626 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
8627 from:
8628
f7ef9339 8629 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 8630
613e1657
KB
8631 This document is only a draft and is subject to change. (So beware.)
8632
f7ef9339 8633 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
8634 determined empirically by examining 64-bit ELF files produced by
8635 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
8636
8637 - Kevin, July 16, 2002
613e1657
KB
8638 ] */
8639
8640static LONGEST
c764a876 8641read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 8642{
fe1b8b76 8643 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 8644
dd373385 8645 if (length == 0xffffffff)
613e1657 8646 {
fe1b8b76 8647 length = bfd_get_64 (abfd, buf + 4);
613e1657 8648 *bytes_read = 12;
613e1657 8649 }
dd373385 8650 else if (length == 0)
f7ef9339 8651 {
dd373385 8652 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 8653 length = bfd_get_64 (abfd, buf);
f7ef9339 8654 *bytes_read = 8;
f7ef9339 8655 }
613e1657
KB
8656 else
8657 {
8658 *bytes_read = 4;
613e1657
KB
8659 }
8660
c764a876
DE
8661 return length;
8662}
dd373385 8663
c764a876
DE
8664/* Cover function for read_initial_length.
8665 Returns the length of the object at BUF, and stores the size of the
8666 initial length in *BYTES_READ and stores the size that offsets will be in
8667 *OFFSET_SIZE.
8668 If the initial length size is not equivalent to that specified in
8669 CU_HEADER then issue a complaint.
8670 This is useful when reading non-comp-unit headers. */
dd373385 8671
c764a876
DE
8672static LONGEST
8673read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
8674 const struct comp_unit_head *cu_header,
8675 unsigned int *bytes_read,
8676 unsigned int *offset_size)
8677{
8678 LONGEST length = read_initial_length (abfd, buf, bytes_read);
8679
8680 gdb_assert (cu_header->initial_length_size == 4
8681 || cu_header->initial_length_size == 8
8682 || cu_header->initial_length_size == 12);
8683
8684 if (cu_header->initial_length_size != *bytes_read)
8685 complaint (&symfile_complaints,
8686 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 8687
c764a876 8688 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 8689 return length;
613e1657
KB
8690}
8691
8692/* Read an offset from the data stream. The size of the offset is
917c78fc 8693 given by cu_header->offset_size. */
613e1657
KB
8694
8695static LONGEST
fe1b8b76 8696read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 8697 unsigned int *bytes_read)
c764a876
DE
8698{
8699 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 8700
c764a876
DE
8701 *bytes_read = cu_header->offset_size;
8702 return offset;
8703}
8704
8705/* Read an offset from the data stream. */
8706
8707static LONGEST
8708read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
8709{
8710 LONGEST retval = 0;
8711
c764a876 8712 switch (offset_size)
613e1657
KB
8713 {
8714 case 4:
fe1b8b76 8715 retval = bfd_get_32 (abfd, buf);
613e1657
KB
8716 break;
8717 case 8:
fe1b8b76 8718 retval = bfd_get_64 (abfd, buf);
613e1657
KB
8719 break;
8720 default:
8e65ff28 8721 internal_error (__FILE__, __LINE__,
c764a876 8722 _("read_offset_1: bad switch [in module %s]"),
659b0389 8723 bfd_get_filename (abfd));
613e1657
KB
8724 }
8725
917c78fc 8726 return retval;
613e1657
KB
8727}
8728
fe1b8b76
JB
8729static gdb_byte *
8730read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
8731{
8732 /* If the size of a host char is 8 bits, we can return a pointer
8733 to the buffer, otherwise we have to copy the data to a buffer
8734 allocated on the temporary obstack. */
4bdf3d34 8735 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 8736 return buf;
c906108c
SS
8737}
8738
8739static char *
fe1b8b76 8740read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
8741{
8742 /* If the size of a host char is 8 bits, we can return a pointer
8743 to the string, otherwise we have to copy the string to a buffer
8744 allocated on the temporary obstack. */
4bdf3d34 8745 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
8746 if (*buf == '\0')
8747 {
8748 *bytes_read_ptr = 1;
8749 return NULL;
8750 }
fe1b8b76
JB
8751 *bytes_read_ptr = strlen ((char *) buf) + 1;
8752 return (char *) buf;
4bdf3d34
JJ
8753}
8754
8755static char *
fe1b8b76 8756read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
8757 const struct comp_unit_head *cu_header,
8758 unsigned int *bytes_read_ptr)
8759{
c764a876 8760 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 8761
be391dca 8762 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 8763 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 8764 {
8a3fe4f8 8765 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 8766 bfd_get_filename (abfd));
4bdf3d34 8767 return NULL;
c906108c 8768 }
dce234bc 8769 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 8770 {
8a3fe4f8 8771 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 8772 bfd_get_filename (abfd));
c906108c
SS
8773 return NULL;
8774 }
4bdf3d34 8775 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 8776 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 8777 return NULL;
dce234bc 8778 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
8779}
8780
ce5d95e1 8781static unsigned long
fe1b8b76 8782read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 8783{
ce5d95e1
JB
8784 unsigned long result;
8785 unsigned int num_read;
c906108c
SS
8786 int i, shift;
8787 unsigned char byte;
8788
8789 result = 0;
8790 shift = 0;
8791 num_read = 0;
8792 i = 0;
8793 while (1)
8794 {
fe1b8b76 8795 byte = bfd_get_8 (abfd, buf);
c906108c
SS
8796 buf++;
8797 num_read++;
ce5d95e1 8798 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
8799 if ((byte & 128) == 0)
8800 {
8801 break;
8802 }
8803 shift += 7;
8804 }
8805 *bytes_read_ptr = num_read;
8806 return result;
8807}
8808
ce5d95e1 8809static long
fe1b8b76 8810read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 8811{
ce5d95e1 8812 long result;
77e0b926 8813 int i, shift, num_read;
c906108c
SS
8814 unsigned char byte;
8815
8816 result = 0;
8817 shift = 0;
c906108c
SS
8818 num_read = 0;
8819 i = 0;
8820 while (1)
8821 {
fe1b8b76 8822 byte = bfd_get_8 (abfd, buf);
c906108c
SS
8823 buf++;
8824 num_read++;
ce5d95e1 8825 result |= ((long)(byte & 127) << shift);
c906108c
SS
8826 shift += 7;
8827 if ((byte & 128) == 0)
8828 {
8829 break;
8830 }
8831 }
77e0b926
DJ
8832 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
8833 result |= -(((long)1) << shift);
c906108c
SS
8834 *bytes_read_ptr = num_read;
8835 return result;
8836}
8837
4bb7a0a7
DJ
8838/* Return a pointer to just past the end of an LEB128 number in BUF. */
8839
fe1b8b76
JB
8840static gdb_byte *
8841skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
8842{
8843 int byte;
8844
8845 while (1)
8846 {
fe1b8b76 8847 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
8848 buf++;
8849 if ((byte & 128) == 0)
8850 return buf;
8851 }
8852}
8853
c906108c 8854static void
e142c38c 8855set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
8856{
8857 switch (lang)
8858 {
8859 case DW_LANG_C89:
76bee0cc 8860 case DW_LANG_C99:
c906108c 8861 case DW_LANG_C:
e142c38c 8862 cu->language = language_c;
c906108c
SS
8863 break;
8864 case DW_LANG_C_plus_plus:
e142c38c 8865 cu->language = language_cplus;
c906108c 8866 break;
6aecb9c2
JB
8867 case DW_LANG_D:
8868 cu->language = language_d;
8869 break;
c906108c
SS
8870 case DW_LANG_Fortran77:
8871 case DW_LANG_Fortran90:
b21b22e0 8872 case DW_LANG_Fortran95:
e142c38c 8873 cu->language = language_fortran;
c906108c
SS
8874 break;
8875 case DW_LANG_Mips_Assembler:
e142c38c 8876 cu->language = language_asm;
c906108c 8877 break;
bebd888e 8878 case DW_LANG_Java:
e142c38c 8879 cu->language = language_java;
bebd888e 8880 break;
c906108c 8881 case DW_LANG_Ada83:
8aaf0b47 8882 case DW_LANG_Ada95:
bc5f45f8
JB
8883 cu->language = language_ada;
8884 break;
72019c9c
GM
8885 case DW_LANG_Modula2:
8886 cu->language = language_m2;
8887 break;
fe8e67fd
PM
8888 case DW_LANG_Pascal83:
8889 cu->language = language_pascal;
8890 break;
22566fbd
DJ
8891 case DW_LANG_ObjC:
8892 cu->language = language_objc;
8893 break;
c906108c
SS
8894 case DW_LANG_Cobol74:
8895 case DW_LANG_Cobol85:
c906108c 8896 default:
e142c38c 8897 cu->language = language_minimal;
c906108c
SS
8898 break;
8899 }
e142c38c 8900 cu->language_defn = language_def (cu->language);
c906108c
SS
8901}
8902
8903/* Return the named attribute or NULL if not there. */
8904
8905static struct attribute *
e142c38c 8906dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
8907{
8908 unsigned int i;
8909 struct attribute *spec = NULL;
8910
8911 for (i = 0; i < die->num_attrs; ++i)
8912 {
8913 if (die->attrs[i].name == name)
10b3939b 8914 return &die->attrs[i];
c906108c
SS
8915 if (die->attrs[i].name == DW_AT_specification
8916 || die->attrs[i].name == DW_AT_abstract_origin)
8917 spec = &die->attrs[i];
8918 }
c906108c 8919
10b3939b 8920 if (spec)
f2f0e013
DJ
8921 {
8922 die = follow_die_ref (die, spec, &cu);
8923 return dwarf2_attr (die, name, cu);
8924 }
c5aa993b 8925
c906108c
SS
8926 return NULL;
8927}
8928
348e048f
DE
8929/* Return the named attribute or NULL if not there,
8930 but do not follow DW_AT_specification, etc.
8931 This is for use in contexts where we're reading .debug_types dies.
8932 Following DW_AT_specification, DW_AT_abstract_origin will take us
8933 back up the chain, and we want to go down. */
8934
8935static struct attribute *
8936dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
8937 struct dwarf2_cu *cu)
8938{
8939 unsigned int i;
8940
8941 for (i = 0; i < die->num_attrs; ++i)
8942 if (die->attrs[i].name == name)
8943 return &die->attrs[i];
8944
8945 return NULL;
8946}
8947
05cf31d1
JB
8948/* Return non-zero iff the attribute NAME is defined for the given DIE,
8949 and holds a non-zero value. This function should only be used for
2dc7f7b3 8950 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
8951
8952static int
8953dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
8954{
8955 struct attribute *attr = dwarf2_attr (die, name, cu);
8956
8957 return (attr && DW_UNSND (attr));
8958}
8959
3ca72b44 8960static int
e142c38c 8961die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 8962{
05cf31d1
JB
8963 /* A DIE is a declaration if it has a DW_AT_declaration attribute
8964 which value is non-zero. However, we have to be careful with
8965 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
8966 (via dwarf2_flag_true_p) follows this attribute. So we may
8967 end up accidently finding a declaration attribute that belongs
8968 to a different DIE referenced by the specification attribute,
8969 even though the given DIE does not have a declaration attribute. */
8970 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
8971 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
8972}
8973
63d06c5c 8974/* Return the die giving the specification for DIE, if there is
f2f0e013 8975 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
8976 containing the return value on output. If there is no
8977 specification, but there is an abstract origin, that is
8978 returned. */
63d06c5c
DC
8979
8980static struct die_info *
f2f0e013 8981die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 8982{
f2f0e013
DJ
8983 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
8984 *spec_cu);
63d06c5c 8985
edb3359d
DJ
8986 if (spec_attr == NULL)
8987 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
8988
63d06c5c
DC
8989 if (spec_attr == NULL)
8990 return NULL;
8991 else
f2f0e013 8992 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 8993}
c906108c 8994
debd256d
JB
8995/* Free the line_header structure *LH, and any arrays and strings it
8996 refers to. */
8997static void
8998free_line_header (struct line_header *lh)
8999{
9000 if (lh->standard_opcode_lengths)
a8bc7b56 9001 xfree (lh->standard_opcode_lengths);
debd256d
JB
9002
9003 /* Remember that all the lh->file_names[i].name pointers are
9004 pointers into debug_line_buffer, and don't need to be freed. */
9005 if (lh->file_names)
a8bc7b56 9006 xfree (lh->file_names);
debd256d
JB
9007
9008 /* Similarly for the include directory names. */
9009 if (lh->include_dirs)
a8bc7b56 9010 xfree (lh->include_dirs);
debd256d 9011
a8bc7b56 9012 xfree (lh);
debd256d
JB
9013}
9014
9015
9016/* Add an entry to LH's include directory table. */
9017static void
9018add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9019{
debd256d
JB
9020 /* Grow the array if necessary. */
9021 if (lh->include_dirs_size == 0)
c5aa993b 9022 {
debd256d
JB
9023 lh->include_dirs_size = 1; /* for testing */
9024 lh->include_dirs = xmalloc (lh->include_dirs_size
9025 * sizeof (*lh->include_dirs));
9026 }
9027 else if (lh->num_include_dirs >= lh->include_dirs_size)
9028 {
9029 lh->include_dirs_size *= 2;
9030 lh->include_dirs = xrealloc (lh->include_dirs,
9031 (lh->include_dirs_size
9032 * sizeof (*lh->include_dirs)));
c5aa993b 9033 }
c906108c 9034
debd256d
JB
9035 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9036}
6e70227d 9037
debd256d
JB
9038
9039/* Add an entry to LH's file name table. */
9040static void
9041add_file_name (struct line_header *lh,
9042 char *name,
9043 unsigned int dir_index,
9044 unsigned int mod_time,
9045 unsigned int length)
9046{
9047 struct file_entry *fe;
9048
9049 /* Grow the array if necessary. */
9050 if (lh->file_names_size == 0)
9051 {
9052 lh->file_names_size = 1; /* for testing */
9053 lh->file_names = xmalloc (lh->file_names_size
9054 * sizeof (*lh->file_names));
9055 }
9056 else if (lh->num_file_names >= lh->file_names_size)
9057 {
9058 lh->file_names_size *= 2;
9059 lh->file_names = xrealloc (lh->file_names,
9060 (lh->file_names_size
9061 * sizeof (*lh->file_names)));
9062 }
9063
9064 fe = &lh->file_names[lh->num_file_names++];
9065 fe->name = name;
9066 fe->dir_index = dir_index;
9067 fe->mod_time = mod_time;
9068 fe->length = length;
aaa75496 9069 fe->included_p = 0;
cb1df416 9070 fe->symtab = NULL;
debd256d 9071}
6e70227d 9072
debd256d
JB
9073
9074/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9075 .debug_line, according to the endianness of ABFD. Return a pointer
9076 to a struct line_header, allocated using xmalloc.
debd256d
JB
9077
9078 NOTE: the strings in the include directory and file name tables of
9079 the returned object point into debug_line_buffer, and must not be
9080 freed. */
9081static struct line_header *
9082dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9083 struct dwarf2_cu *cu)
debd256d
JB
9084{
9085 struct cleanup *back_to;
9086 struct line_header *lh;
fe1b8b76 9087 gdb_byte *line_ptr;
c764a876 9088 unsigned int bytes_read, offset_size;
debd256d
JB
9089 int i;
9090 char *cur_dir, *cur_file;
9091
be391dca 9092 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9093 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9094 {
e2e0b3e5 9095 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9096 return 0;
9097 }
9098
a738430d
MK
9099 /* Make sure that at least there's room for the total_length field.
9100 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9101 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9102 {
4d3c2250 9103 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9104 return 0;
9105 }
9106
9107 lh = xmalloc (sizeof (*lh));
9108 memset (lh, 0, sizeof (*lh));
9109 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9110 (void *) lh);
9111
dce234bc 9112 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9113
a738430d 9114 /* Read in the header. */
6e70227d 9115 lh->total_length =
c764a876
DE
9116 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9117 &bytes_read, &offset_size);
debd256d 9118 line_ptr += bytes_read;
dce234bc
PP
9119 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9120 + dwarf2_per_objfile->line.size))
debd256d 9121 {
4d3c2250 9122 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9123 return 0;
9124 }
9125 lh->statement_program_end = line_ptr + lh->total_length;
9126 lh->version = read_2_bytes (abfd, line_ptr);
9127 line_ptr += 2;
c764a876
DE
9128 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9129 line_ptr += offset_size;
debd256d
JB
9130 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9131 line_ptr += 1;
2dc7f7b3
TT
9132 if (lh->version >= 4)
9133 {
9134 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9135 line_ptr += 1;
9136 }
9137 else
9138 lh->maximum_ops_per_instruction = 1;
9139
9140 if (lh->maximum_ops_per_instruction == 0)
9141 {
9142 lh->maximum_ops_per_instruction = 1;
9143 complaint (&symfile_complaints,
9144 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9145 }
9146
debd256d
JB
9147 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9148 line_ptr += 1;
9149 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9150 line_ptr += 1;
9151 lh->line_range = read_1_byte (abfd, line_ptr);
9152 line_ptr += 1;
9153 lh->opcode_base = read_1_byte (abfd, line_ptr);
9154 line_ptr += 1;
9155 lh->standard_opcode_lengths
fe1b8b76 9156 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9157
9158 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9159 for (i = 1; i < lh->opcode_base; ++i)
9160 {
9161 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9162 line_ptr += 1;
9163 }
9164
a738430d 9165 /* Read directory table. */
debd256d
JB
9166 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9167 {
9168 line_ptr += bytes_read;
9169 add_include_dir (lh, cur_dir);
9170 }
9171 line_ptr += bytes_read;
9172
a738430d 9173 /* Read file name table. */
debd256d
JB
9174 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9175 {
9176 unsigned int dir_index, mod_time, length;
9177
9178 line_ptr += bytes_read;
9179 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9180 line_ptr += bytes_read;
9181 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9182 line_ptr += bytes_read;
9183 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9184 line_ptr += bytes_read;
9185
9186 add_file_name (lh, cur_file, dir_index, mod_time, length);
9187 }
9188 line_ptr += bytes_read;
6e70227d 9189 lh->statement_program_start = line_ptr;
debd256d 9190
dce234bc
PP
9191 if (line_ptr > (dwarf2_per_objfile->line.buffer
9192 + dwarf2_per_objfile->line.size))
4d3c2250 9193 complaint (&symfile_complaints,
e2e0b3e5 9194 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9195
9196 discard_cleanups (back_to);
9197 return lh;
9198}
c906108c 9199
5fb290d7
DJ
9200/* This function exists to work around a bug in certain compilers
9201 (particularly GCC 2.95), in which the first line number marker of a
9202 function does not show up until after the prologue, right before
9203 the second line number marker. This function shifts ADDRESS down
9204 to the beginning of the function if necessary, and is called on
9205 addresses passed to record_line. */
9206
9207static CORE_ADDR
e142c38c 9208check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9209{
9210 struct function_range *fn;
9211
9212 /* Find the function_range containing address. */
e142c38c 9213 if (!cu->first_fn)
5fb290d7
DJ
9214 return address;
9215
e142c38c
DJ
9216 if (!cu->cached_fn)
9217 cu->cached_fn = cu->first_fn;
5fb290d7 9218
e142c38c 9219 fn = cu->cached_fn;
5fb290d7
DJ
9220 while (fn)
9221 if (fn->lowpc <= address && fn->highpc > address)
9222 goto found;
9223 else
9224 fn = fn->next;
9225
e142c38c
DJ
9226 fn = cu->first_fn;
9227 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9228 if (fn->lowpc <= address && fn->highpc > address)
9229 goto found;
9230 else
9231 fn = fn->next;
9232
9233 return address;
9234
9235 found:
9236 if (fn->seen_line)
9237 return address;
9238 if (address != fn->lowpc)
4d3c2250 9239 complaint (&symfile_complaints,
e2e0b3e5 9240 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9241 (unsigned long) address, fn->name);
5fb290d7
DJ
9242 fn->seen_line = 1;
9243 return fn->lowpc;
9244}
9245
aaa75496
JB
9246/* Decode the Line Number Program (LNP) for the given line_header
9247 structure and CU. The actual information extracted and the type
9248 of structures created from the LNP depends on the value of PST.
9249
9250 1. If PST is NULL, then this procedure uses the data from the program
9251 to create all necessary symbol tables, and their linetables.
9252 The compilation directory of the file is passed in COMP_DIR,
9253 and must not be NULL.
6e70227d 9254
aaa75496
JB
9255 2. If PST is not NULL, this procedure reads the program to determine
9256 the list of files included by the unit represented by PST, and
9257 builds all the associated partial symbol tables. In this case,
9258 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9259 is not used to compute the full name of the symtab, and therefore
9260 omitting it when building the partial symtab does not introduce
9261 the potential for inconsistency - a partial symtab and its associated
9262 symbtab having a different fullname -). */
debd256d 9263
c906108c 9264static void
debd256d 9265dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 9266 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 9267{
a8c50c1f 9268 gdb_byte *line_ptr, *extended_end;
fe1b8b76 9269 gdb_byte *line_end;
a8c50c1f 9270 unsigned int bytes_read, extended_len;
c906108c 9271 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
9272 CORE_ADDR baseaddr;
9273 struct objfile *objfile = cu->objfile;
fbf65064 9274 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 9275 const int decode_for_pst_p = (pst != NULL);
cb1df416 9276 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
9277
9278 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9279
debd256d
JB
9280 line_ptr = lh->statement_program_start;
9281 line_end = lh->statement_program_end;
c906108c
SS
9282
9283 /* Read the statement sequences until there's nothing left. */
9284 while (line_ptr < line_end)
9285 {
9286 /* state machine registers */
9287 CORE_ADDR address = 0;
9288 unsigned int file = 1;
9289 unsigned int line = 1;
9290 unsigned int column = 0;
debd256d 9291 int is_stmt = lh->default_is_stmt;
c906108c
SS
9292 int basic_block = 0;
9293 int end_sequence = 0;
fbf65064 9294 CORE_ADDR addr;
2dc7f7b3 9295 unsigned char op_index = 0;
c906108c 9296
aaa75496 9297 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 9298 {
aaa75496 9299 /* Start a subfile for the current file of the state machine. */
debd256d
JB
9300 /* lh->include_dirs and lh->file_names are 0-based, but the
9301 directory and file name numbers in the statement program
9302 are 1-based. */
9303 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 9304 char *dir = NULL;
a738430d 9305
debd256d
JB
9306 if (fe->dir_index)
9307 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
9308
9309 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
9310 }
9311
a738430d 9312 /* Decode the table. */
c5aa993b 9313 while (!end_sequence)
c906108c
SS
9314 {
9315 op_code = read_1_byte (abfd, line_ptr);
9316 line_ptr += 1;
59205f5a
JB
9317 if (line_ptr > line_end)
9318 {
9319 dwarf2_debug_line_missing_end_sequence_complaint ();
9320 break;
9321 }
9aa1fe7e 9322
debd256d 9323 if (op_code >= lh->opcode_base)
6e70227d 9324 {
a738430d 9325 /* Special operand. */
debd256d 9326 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
9327 address += (((op_index + (adj_opcode / lh->line_range))
9328 / lh->maximum_ops_per_instruction)
9329 * lh->minimum_instruction_length);
9330 op_index = ((op_index + (adj_opcode / lh->line_range))
9331 % lh->maximum_ops_per_instruction);
debd256d 9332 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 9333 if (lh->num_file_names < file || file == 0)
25e43795 9334 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
9335 /* For now we ignore lines not starting on an
9336 instruction boundary. */
9337 else if (op_index == 0)
25e43795
DJ
9338 {
9339 lh->file_names[file - 1].included_p = 1;
ca5f395d 9340 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9341 {
9342 if (last_subfile != current_subfile)
9343 {
9344 addr = gdbarch_addr_bits_remove (gdbarch, address);
9345 if (last_subfile)
9346 record_line (last_subfile, 0, addr);
9347 last_subfile = current_subfile;
9348 }
25e43795 9349 /* Append row to matrix using current values. */
fbf65064
UW
9350 addr = check_cu_functions (address, cu);
9351 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9352 record_line (current_subfile, line, addr);
366da635 9353 }
25e43795 9354 }
ca5f395d 9355 basic_block = 0;
9aa1fe7e
GK
9356 }
9357 else switch (op_code)
c906108c
SS
9358 {
9359 case DW_LNS_extended_op:
a8c50c1f 9360 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 9361 line_ptr += bytes_read;
a8c50c1f 9362 extended_end = line_ptr + extended_len;
c906108c
SS
9363 extended_op = read_1_byte (abfd, line_ptr);
9364 line_ptr += 1;
9365 switch (extended_op)
9366 {
9367 case DW_LNE_end_sequence:
9368 end_sequence = 1;
c906108c
SS
9369 break;
9370 case DW_LNE_set_address:
e7c27a73 9371 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 9372 op_index = 0;
107d2387
AC
9373 line_ptr += bytes_read;
9374 address += baseaddr;
c906108c
SS
9375 break;
9376 case DW_LNE_define_file:
debd256d
JB
9377 {
9378 char *cur_file;
9379 unsigned int dir_index, mod_time, length;
6e70227d 9380
debd256d
JB
9381 cur_file = read_string (abfd, line_ptr, &bytes_read);
9382 line_ptr += bytes_read;
9383 dir_index =
9384 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9385 line_ptr += bytes_read;
9386 mod_time =
9387 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9388 line_ptr += bytes_read;
9389 length =
9390 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9391 line_ptr += bytes_read;
9392 add_file_name (lh, cur_file, dir_index, mod_time, length);
9393 }
c906108c 9394 break;
d0c6ba3d
CC
9395 case DW_LNE_set_discriminator:
9396 /* The discriminator is not interesting to the debugger;
9397 just ignore it. */
9398 line_ptr = extended_end;
9399 break;
c906108c 9400 default:
4d3c2250 9401 complaint (&symfile_complaints,
e2e0b3e5 9402 _("mangled .debug_line section"));
debd256d 9403 return;
c906108c 9404 }
a8c50c1f
DJ
9405 /* Make sure that we parsed the extended op correctly. If e.g.
9406 we expected a different address size than the producer used,
9407 we may have read the wrong number of bytes. */
9408 if (line_ptr != extended_end)
9409 {
9410 complaint (&symfile_complaints,
9411 _("mangled .debug_line section"));
9412 return;
9413 }
c906108c
SS
9414 break;
9415 case DW_LNS_copy:
59205f5a 9416 if (lh->num_file_names < file || file == 0)
25e43795
DJ
9417 dwarf2_debug_line_missing_file_complaint ();
9418 else
366da635 9419 {
25e43795 9420 lh->file_names[file - 1].included_p = 1;
ca5f395d 9421 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9422 {
9423 if (last_subfile != current_subfile)
9424 {
9425 addr = gdbarch_addr_bits_remove (gdbarch, address);
9426 if (last_subfile)
9427 record_line (last_subfile, 0, addr);
9428 last_subfile = current_subfile;
9429 }
9430 addr = check_cu_functions (address, cu);
9431 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9432 record_line (current_subfile, line, addr);
9433 }
366da635 9434 }
c906108c
SS
9435 basic_block = 0;
9436 break;
9437 case DW_LNS_advance_pc:
2dc7f7b3
TT
9438 {
9439 CORE_ADDR adjust
9440 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9441
9442 address += (((op_index + adjust)
9443 / lh->maximum_ops_per_instruction)
9444 * lh->minimum_instruction_length);
9445 op_index = ((op_index + adjust)
9446 % lh->maximum_ops_per_instruction);
9447 line_ptr += bytes_read;
9448 }
c906108c
SS
9449 break;
9450 case DW_LNS_advance_line:
9451 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9452 line_ptr += bytes_read;
9453 break;
9454 case DW_LNS_set_file:
debd256d 9455 {
a738430d
MK
9456 /* The arrays lh->include_dirs and lh->file_names are
9457 0-based, but the directory and file name numbers in
9458 the statement program are 1-based. */
debd256d 9459 struct file_entry *fe;
4f1520fb 9460 char *dir = NULL;
a738430d 9461
debd256d
JB
9462 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9463 line_ptr += bytes_read;
59205f5a 9464 if (lh->num_file_names < file || file == 0)
25e43795
DJ
9465 dwarf2_debug_line_missing_file_complaint ();
9466 else
9467 {
9468 fe = &lh->file_names[file - 1];
9469 if (fe->dir_index)
9470 dir = lh->include_dirs[fe->dir_index - 1];
9471 if (!decode_for_pst_p)
9472 {
9473 last_subfile = current_subfile;
9474 dwarf2_start_subfile (fe->name, dir, comp_dir);
9475 }
9476 }
debd256d 9477 }
c906108c
SS
9478 break;
9479 case DW_LNS_set_column:
9480 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9481 line_ptr += bytes_read;
9482 break;
9483 case DW_LNS_negate_stmt:
9484 is_stmt = (!is_stmt);
9485 break;
9486 case DW_LNS_set_basic_block:
9487 basic_block = 1;
9488 break;
c2c6d25f
JM
9489 /* Add to the address register of the state machine the
9490 address increment value corresponding to special opcode
a738430d
MK
9491 255. I.e., this value is scaled by the minimum
9492 instruction length since special opcode 255 would have
9493 scaled the the increment. */
c906108c 9494 case DW_LNS_const_add_pc:
2dc7f7b3
TT
9495 {
9496 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
9497
9498 address += (((op_index + adjust)
9499 / lh->maximum_ops_per_instruction)
9500 * lh->minimum_instruction_length);
9501 op_index = ((op_index + adjust)
9502 % lh->maximum_ops_per_instruction);
9503 }
c906108c
SS
9504 break;
9505 case DW_LNS_fixed_advance_pc:
9506 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 9507 op_index = 0;
c906108c
SS
9508 line_ptr += 2;
9509 break;
9aa1fe7e 9510 default:
a738430d
MK
9511 {
9512 /* Unknown standard opcode, ignore it. */
9aa1fe7e 9513 int i;
a738430d 9514
debd256d 9515 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
9516 {
9517 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9518 line_ptr += bytes_read;
9519 }
9520 }
c906108c
SS
9521 }
9522 }
59205f5a
JB
9523 if (lh->num_file_names < file || file == 0)
9524 dwarf2_debug_line_missing_file_complaint ();
9525 else
9526 {
9527 lh->file_names[file - 1].included_p = 1;
9528 if (!decode_for_pst_p)
fbf65064
UW
9529 {
9530 addr = gdbarch_addr_bits_remove (gdbarch, address);
9531 record_line (current_subfile, 0, addr);
9532 }
59205f5a 9533 }
c906108c 9534 }
aaa75496
JB
9535
9536 if (decode_for_pst_p)
9537 {
9538 int file_index;
9539
9540 /* Now that we're done scanning the Line Header Program, we can
9541 create the psymtab of each included file. */
9542 for (file_index = 0; file_index < lh->num_file_names; file_index++)
9543 if (lh->file_names[file_index].included_p == 1)
9544 {
5b5464ad
JB
9545 const struct file_entry fe = lh->file_names [file_index];
9546 char *include_name = fe.name;
9547 char *dir_name = NULL;
9548 char *pst_filename = pst->filename;
9549
9550 if (fe.dir_index)
9551 dir_name = lh->include_dirs[fe.dir_index - 1];
9552
9553 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
9554 {
1754f103
MK
9555 include_name = concat (dir_name, SLASH_STRING,
9556 include_name, (char *)NULL);
5b5464ad
JB
9557 make_cleanup (xfree, include_name);
9558 }
9559
9560 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9561 {
1754f103
MK
9562 pst_filename = concat (pst->dirname, SLASH_STRING,
9563 pst_filename, (char *)NULL);
5b5464ad
JB
9564 make_cleanup (xfree, pst_filename);
9565 }
9566
9567 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
9568 dwarf2_create_include_psymtab (include_name, pst, objfile);
9569 }
9570 }
cb1df416
DJ
9571 else
9572 {
9573 /* Make sure a symtab is created for every file, even files
9574 which contain only variables (i.e. no code with associated
9575 line numbers). */
9576
9577 int i;
9578 struct file_entry *fe;
9579
9580 for (i = 0; i < lh->num_file_names; i++)
9581 {
9582 char *dir = NULL;
9a619af0 9583
cb1df416
DJ
9584 fe = &lh->file_names[i];
9585 if (fe->dir_index)
9586 dir = lh->include_dirs[fe->dir_index - 1];
9587 dwarf2_start_subfile (fe->name, dir, comp_dir);
9588
9589 /* Skip the main file; we don't need it, and it must be
9590 allocated last, so that it will show up before the
9591 non-primary symtabs in the objfile's symtab list. */
9592 if (current_subfile == first_subfile)
9593 continue;
9594
9595 if (current_subfile->symtab == NULL)
9596 current_subfile->symtab = allocate_symtab (current_subfile->name,
9597 cu->objfile);
9598 fe->symtab = current_subfile->symtab;
9599 }
9600 }
c906108c
SS
9601}
9602
9603/* Start a subfile for DWARF. FILENAME is the name of the file and
9604 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
9605 or NULL if not known. COMP_DIR is the compilation directory for the
9606 linetable's compilation unit or NULL if not known.
c906108c
SS
9607 This routine tries to keep line numbers from identical absolute and
9608 relative file names in a common subfile.
9609
9610 Using the `list' example from the GDB testsuite, which resides in
9611 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
9612 of /srcdir/list0.c yields the following debugging information for list0.c:
9613
c5aa993b
JM
9614 DW_AT_name: /srcdir/list0.c
9615 DW_AT_comp_dir: /compdir
357e46e7 9616 files.files[0].name: list0.h
c5aa993b 9617 files.files[0].dir: /srcdir
357e46e7 9618 files.files[1].name: list0.c
c5aa993b 9619 files.files[1].dir: /srcdir
c906108c
SS
9620
9621 The line number information for list0.c has to end up in a single
4f1520fb
FR
9622 subfile, so that `break /srcdir/list0.c:1' works as expected.
9623 start_subfile will ensure that this happens provided that we pass the
9624 concatenation of files.files[1].dir and files.files[1].name as the
9625 subfile's name. */
c906108c
SS
9626
9627static void
4f1520fb 9628dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 9629{
4f1520fb
FR
9630 char *fullname;
9631
9632 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
9633 `start_symtab' will always pass the contents of DW_AT_comp_dir as
9634 second argument to start_subfile. To be consistent, we do the
9635 same here. In order not to lose the line information directory,
9636 we concatenate it to the filename when it makes sense.
9637 Note that the Dwarf3 standard says (speaking of filenames in line
9638 information): ``The directory index is ignored for file names
9639 that represent full path names''. Thus ignoring dirname in the
9640 `else' branch below isn't an issue. */
c906108c 9641
d5166ae1 9642 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
9643 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
9644 else
9645 fullname = filename;
c906108c 9646
4f1520fb
FR
9647 start_subfile (fullname, comp_dir);
9648
9649 if (fullname != filename)
9650 xfree (fullname);
c906108c
SS
9651}
9652
4c2df51b
DJ
9653static void
9654var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 9655 struct dwarf2_cu *cu)
4c2df51b 9656{
e7c27a73
DJ
9657 struct objfile *objfile = cu->objfile;
9658 struct comp_unit_head *cu_header = &cu->header;
9659
4c2df51b
DJ
9660 /* NOTE drow/2003-01-30: There used to be a comment and some special
9661 code here to turn a symbol with DW_AT_external and a
9662 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
9663 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
9664 with some versions of binutils) where shared libraries could have
9665 relocations against symbols in their debug information - the
9666 minimal symbol would have the right address, but the debug info
9667 would not. It's no longer necessary, because we will explicitly
9668 apply relocations when we read in the debug information now. */
9669
9670 /* A DW_AT_location attribute with no contents indicates that a
9671 variable has been optimized away. */
9672 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
9673 {
9674 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9675 return;
9676 }
9677
9678 /* Handle one degenerate form of location expression specially, to
9679 preserve GDB's previous behavior when section offsets are
9680 specified. If this is just a DW_OP_addr then mark this symbol
9681 as LOC_STATIC. */
9682
9683 if (attr_form_is_block (attr)
9684 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
9685 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
9686 {
891d2f0b 9687 unsigned int dummy;
4c2df51b
DJ
9688
9689 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 9690 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 9691 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
9692 fixup_symbol_section (sym, objfile);
9693 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
9694 SYMBOL_SECTION (sym));
4c2df51b
DJ
9695 return;
9696 }
9697
9698 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
9699 expression evaluator, and use LOC_COMPUTED only when necessary
9700 (i.e. when the value of a register or memory location is
9701 referenced, or a thread-local block, etc.). Then again, it might
9702 not be worthwhile. I'm assuming that it isn't unless performance
9703 or memory numbers show me otherwise. */
9704
e7c27a73 9705 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
9706 SYMBOL_CLASS (sym) = LOC_COMPUTED;
9707}
9708
c906108c
SS
9709/* Given a pointer to a DWARF information entry, figure out if we need
9710 to make a symbol table entry for it, and if so, create a new entry
9711 and return a pointer to it.
9712 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 9713 used the passed type. */
c906108c
SS
9714
9715static struct symbol *
e7c27a73 9716new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 9717{
e7c27a73 9718 struct objfile *objfile = cu->objfile;
c906108c
SS
9719 struct symbol *sym = NULL;
9720 char *name;
9721 struct attribute *attr = NULL;
9722 struct attribute *attr2 = NULL;
e142c38c 9723 CORE_ADDR baseaddr;
edb3359d 9724 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
9725
9726 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9727
94af9270 9728 name = dwarf2_name (die, cu);
c906108c
SS
9729 if (name)
9730 {
94af9270
KS
9731 const char *linkagename;
9732
4a146b47 9733 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
9734 sizeof (struct symbol));
9735 OBJSTAT (objfile, n_syms++);
9736 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
9737
9738 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 9739 SYMBOL_LANGUAGE (sym) = cu->language;
94af9270
KS
9740 linkagename = dwarf2_physname (name, die, cu);
9741 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 9742
f55ee35c
JK
9743 /* Fortran does not have mangling standard and the mangling does differ
9744 between gfortran, iFort etc. */
9745 if (cu->language == language_fortran
b250c185
SW
9746 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
9747 symbol_set_demangled_name (&(sym->ginfo), (char *) dwarf2_full_name (name, die, cu));
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:
90e7c2c5
PM
10002 case DW_TAG_const_type:
10003 case DW_TAG_volatile_type:
c906108c 10004 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10005 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 10006 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10007 break;
10008 case DW_TAG_enumerator:
e142c38c 10009 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10010 if (attr)
10011 {
e7c27a73 10012 dwarf2_const_value (attr, sym, cu);
c906108c 10013 }
63d06c5c
DC
10014 {
10015 /* NOTE: carlton/2003-11-10: See comment above in the
10016 DW_TAG_class_type, etc. block. */
10017
10018 struct pending **list_to_add;
10019
e142c38c 10020 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10021 && (cu->language == language_cplus
10022 || cu->language == language_java)
e142c38c 10023 ? &global_symbols : cu->list_in_scope);
6e70227d 10024
63d06c5c
DC
10025 add_symbol_to_list (sym, list_to_add);
10026 }
c906108c 10027 break;
5c4e30ca
DC
10028 case DW_TAG_namespace:
10029 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10030 add_symbol_to_list (sym, &global_symbols);
10031 break;
c906108c
SS
10032 default:
10033 /* Not a tag we recognize. Hopefully we aren't processing
10034 trash data, but since we must specifically ignore things
10035 we don't recognize, there is nothing else we should do at
10036 this point. */
e2e0b3e5 10037 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10038 dwarf_tag_name (die->tag));
c906108c
SS
10039 break;
10040 }
df8a16a1
DJ
10041
10042 /* For the benefit of old versions of GCC, check for anonymous
10043 namespaces based on the demangled name. */
10044 if (!processing_has_namespace_info
94af9270 10045 && cu->language == language_cplus)
df8a16a1 10046 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10047 }
10048 return (sym);
10049}
10050
10051/* Copy constant value from an attribute to a symbol. */
10052
10053static void
107d2387 10054dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 10055 struct dwarf2_cu *cu)
c906108c 10056{
e7c27a73
DJ
10057 struct objfile *objfile = cu->objfile;
10058 struct comp_unit_head *cu_header = &cu->header;
e17a4113
UW
10059 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10060 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
c906108c
SS
10061 struct dwarf_block *blk;
10062
10063 switch (attr->form)
10064 {
10065 case DW_FORM_addr:
ac56253d
TT
10066 {
10067 struct dwarf2_locexpr_baton *baton;
10068 gdb_byte *data;
10069
10070 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
10071 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10072 cu_header->addr_size,
10073 TYPE_LENGTH (SYMBOL_TYPE
10074 (sym)));
10075 /* Symbols of this form are reasonably rare, so we just
10076 piggyback on the existing location code rather than writing
10077 a new implementation of symbol_computed_ops. */
10078 baton = obstack_alloc (&objfile->objfile_obstack,
10079 sizeof (struct dwarf2_locexpr_baton));
10080 baton->per_cu = cu->per_cu;
10081 gdb_assert (baton->per_cu);
10082
10083 baton->size = 2 + cu_header->addr_size;
10084 data = obstack_alloc (&objfile->objfile_obstack, baton->size);
10085 baton->data = data;
10086
10087 data[0] = DW_OP_addr;
10088 store_unsigned_integer (&data[1], cu_header->addr_size,
10089 byte_order, DW_ADDR (attr));
10090 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10091
10092 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10093 SYMBOL_LOCATION_BATON (sym) = baton;
10094 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10095 }
c906108c 10096 break;
4ac36638 10097 case DW_FORM_string:
93b5768b
PA
10098 case DW_FORM_strp:
10099 /* DW_STRING is already allocated on the obstack, point directly
10100 to it. */
10101 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
10102 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10103 break;
c906108c
SS
10104 case DW_FORM_block1:
10105 case DW_FORM_block2:
10106 case DW_FORM_block4:
10107 case DW_FORM_block:
2dc7f7b3 10108 case DW_FORM_exprloc:
c906108c
SS
10109 blk = DW_BLOCK (attr);
10110 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 10111 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
10112 blk->size,
10113 TYPE_LENGTH (SYMBOL_TYPE
10114 (sym)));
4e38b386 10115 SYMBOL_VALUE_BYTES (sym) =
4a146b47 10116 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
10117 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
10118 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10119 break;
2df3850c
JM
10120
10121 /* The DW_AT_const_value attributes are supposed to carry the
10122 symbol's value "represented as it would be on the target
10123 architecture." By the time we get here, it's already been
10124 converted to host endianness, so we just need to sign- or
10125 zero-extend it as appropriate. */
10126 case DW_FORM_data1:
10127 dwarf2_const_value_data (attr, sym, 8);
10128 break;
c906108c 10129 case DW_FORM_data2:
2df3850c
JM
10130 dwarf2_const_value_data (attr, sym, 16);
10131 break;
c906108c 10132 case DW_FORM_data4:
2df3850c
JM
10133 dwarf2_const_value_data (attr, sym, 32);
10134 break;
c906108c 10135 case DW_FORM_data8:
2df3850c
JM
10136 dwarf2_const_value_data (attr, sym, 64);
10137 break;
10138
c906108c 10139 case DW_FORM_sdata:
2df3850c
JM
10140 SYMBOL_VALUE (sym) = DW_SND (attr);
10141 SYMBOL_CLASS (sym) = LOC_CONST;
10142 break;
10143
c906108c
SS
10144 case DW_FORM_udata:
10145 SYMBOL_VALUE (sym) = DW_UNSND (attr);
10146 SYMBOL_CLASS (sym) = LOC_CONST;
10147 break;
2df3850c 10148
c906108c 10149 default:
4d3c2250 10150 complaint (&symfile_complaints,
e2e0b3e5 10151 _("unsupported const value attribute form: '%s'"),
4d3c2250 10152 dwarf_form_name (attr->form));
c906108c
SS
10153 SYMBOL_VALUE (sym) = 0;
10154 SYMBOL_CLASS (sym) = LOC_CONST;
10155 break;
10156 }
10157}
10158
2df3850c
JM
10159
10160/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
10161 or zero-extend it as appropriate for the symbol's type. */
10162static void
10163dwarf2_const_value_data (struct attribute *attr,
10164 struct symbol *sym,
10165 int bits)
10166{
10167 LONGEST l = DW_UNSND (attr);
10168
10169 if (bits < sizeof (l) * 8)
10170 {
10171 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
10172 l &= ((LONGEST) 1 << bits) - 1;
10173 else
bf9198f1 10174 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
10175 }
10176
10177 SYMBOL_VALUE (sym) = l;
10178 SYMBOL_CLASS (sym) = LOC_CONST;
10179}
10180
10181
c906108c
SS
10182/* Return the type of the die in question using its DW_AT_type attribute. */
10183
10184static struct type *
e7c27a73 10185die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10186{
c906108c
SS
10187 struct attribute *type_attr;
10188 struct die_info *type_die;
c906108c 10189
e142c38c 10190 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10191 if (!type_attr)
10192 {
10193 /* A missing DW_AT_type represents a void type. */
46bf5051 10194 return objfile_type (cu->objfile)->builtin_void;
c906108c 10195 }
348e048f
DE
10196
10197 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 10198
33ac96f0 10199 return tag_type_to_type (type_die, cu);
c906108c
SS
10200}
10201
b4ba55a1
JB
10202/* True iff CU's producer generates GNAT Ada auxiliary information
10203 that allows to find parallel types through that information instead
10204 of having to do expensive parallel lookups by type name. */
10205
10206static int
10207need_gnat_info (struct dwarf2_cu *cu)
10208{
10209 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10210 of GNAT produces this auxiliary information, without any indication
10211 that it is produced. Part of enhancing the FSF version of GNAT
10212 to produce that information will be to put in place an indicator
10213 that we can use in order to determine whether the descriptive type
10214 info is available or not. One suggestion that has been made is
10215 to use a new attribute, attached to the CU die. For now, assume
10216 that the descriptive type info is not available. */
10217 return 0;
10218}
10219
10220
10221/* Return the auxiliary type of the die in question using its
10222 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10223 attribute is not present. */
10224
10225static struct type *
10226die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10227{
b4ba55a1
JB
10228 struct attribute *type_attr;
10229 struct die_info *type_die;
10230
10231 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10232 if (!type_attr)
10233 return NULL;
10234
10235 type_die = follow_die_ref (die, type_attr, &cu);
33ac96f0 10236 return tag_type_to_type (type_die, cu);
b4ba55a1
JB
10237}
10238
10239/* If DIE has a descriptive_type attribute, then set the TYPE's
10240 descriptive type accordingly. */
10241
10242static void
10243set_descriptive_type (struct type *type, struct die_info *die,
10244 struct dwarf2_cu *cu)
10245{
10246 struct type *descriptive_type = die_descriptive_type (die, cu);
10247
10248 if (descriptive_type)
10249 {
10250 ALLOCATE_GNAT_AUX_TYPE (type);
10251 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10252 }
10253}
10254
c906108c
SS
10255/* Return the containing type of the die in question using its
10256 DW_AT_containing_type attribute. */
10257
10258static struct type *
e7c27a73 10259die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10260{
c906108c 10261 struct attribute *type_attr;
33ac96f0 10262 struct die_info *type_die;
c906108c 10263
e142c38c 10264 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
10265 if (!type_attr)
10266 error (_("Dwarf Error: Problem turning containing type into gdb type "
10267 "[in module %s]"), cu->objfile->name);
10268
10269 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10270 return tag_type_to_type (type_die, cu);
c906108c
SS
10271}
10272
c906108c 10273static struct type *
e7c27a73 10274tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10275{
f792889a
DJ
10276 struct type *this_type;
10277
10278 this_type = read_type_die (die, cu);
10279 if (!this_type)
c906108c 10280 {
b00fdb78
TT
10281 char *message, *saved;
10282
10283 /* read_type_die already issued a complaint. */
10284 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10285 cu->objfile->name,
10286 cu->header.offset,
10287 die->offset);
10288 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10289 message, strlen (message));
10290 xfree (message);
10291
10292 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 10293 }
f792889a 10294 return this_type;
c906108c
SS
10295}
10296
f792889a 10297static struct type *
e7c27a73 10298read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10299{
f792889a
DJ
10300 struct type *this_type;
10301
10302 this_type = get_die_type (die, cu);
10303 if (this_type)
10304 return this_type;
10305
c906108c
SS
10306 switch (die->tag)
10307 {
10308 case DW_TAG_class_type:
680b30c7 10309 case DW_TAG_interface_type:
c906108c
SS
10310 case DW_TAG_structure_type:
10311 case DW_TAG_union_type:
f792889a 10312 this_type = read_structure_type (die, cu);
c906108c
SS
10313 break;
10314 case DW_TAG_enumeration_type:
f792889a 10315 this_type = read_enumeration_type (die, cu);
c906108c
SS
10316 break;
10317 case DW_TAG_subprogram:
10318 case DW_TAG_subroutine_type:
edb3359d 10319 case DW_TAG_inlined_subroutine:
f792889a 10320 this_type = read_subroutine_type (die, cu);
c906108c
SS
10321 break;
10322 case DW_TAG_array_type:
f792889a 10323 this_type = read_array_type (die, cu);
c906108c 10324 break;
72019c9c 10325 case DW_TAG_set_type:
f792889a 10326 this_type = read_set_type (die, cu);
72019c9c 10327 break;
c906108c 10328 case DW_TAG_pointer_type:
f792889a 10329 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
10330 break;
10331 case DW_TAG_ptr_to_member_type:
f792889a 10332 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
10333 break;
10334 case DW_TAG_reference_type:
f792889a 10335 this_type = read_tag_reference_type (die, cu);
c906108c
SS
10336 break;
10337 case DW_TAG_const_type:
f792889a 10338 this_type = read_tag_const_type (die, cu);
c906108c
SS
10339 break;
10340 case DW_TAG_volatile_type:
f792889a 10341 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
10342 break;
10343 case DW_TAG_string_type:
f792889a 10344 this_type = read_tag_string_type (die, cu);
c906108c
SS
10345 break;
10346 case DW_TAG_typedef:
f792889a 10347 this_type = read_typedef (die, cu);
c906108c 10348 break;
a02abb62 10349 case DW_TAG_subrange_type:
f792889a 10350 this_type = read_subrange_type (die, cu);
a02abb62 10351 break;
c906108c 10352 case DW_TAG_base_type:
f792889a 10353 this_type = read_base_type (die, cu);
c906108c 10354 break;
81a17f79 10355 case DW_TAG_unspecified_type:
f792889a 10356 this_type = read_unspecified_type (die, cu);
81a17f79 10357 break;
0114d602
DJ
10358 case DW_TAG_namespace:
10359 this_type = read_namespace_type (die, cu);
10360 break;
f55ee35c
JK
10361 case DW_TAG_module:
10362 this_type = read_module_type (die, cu);
10363 break;
c906108c 10364 default:
a1f5b845 10365 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 10366 dwarf_tag_name (die->tag));
c906108c
SS
10367 break;
10368 }
63d06c5c 10369
f792889a 10370 return this_type;
63d06c5c
DC
10371}
10372
fdde2d81 10373/* Return the name of the namespace/class that DIE is defined within,
0114d602 10374 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 10375
0114d602
DJ
10376 For example, if we're within the method foo() in the following
10377 code:
10378
10379 namespace N {
10380 class C {
10381 void foo () {
10382 }
10383 };
10384 }
10385
10386 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
10387
10388static char *
e142c38c 10389determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 10390{
0114d602
DJ
10391 struct die_info *parent, *spec_die;
10392 struct dwarf2_cu *spec_cu;
10393 struct type *parent_type;
63d06c5c 10394
f55ee35c
JK
10395 if (cu->language != language_cplus && cu->language != language_java
10396 && cu->language != language_fortran)
0114d602
DJ
10397 return "";
10398
10399 /* We have to be careful in the presence of DW_AT_specification.
10400 For example, with GCC 3.4, given the code
10401
10402 namespace N {
10403 void foo() {
10404 // Definition of N::foo.
10405 }
10406 }
10407
10408 then we'll have a tree of DIEs like this:
10409
10410 1: DW_TAG_compile_unit
10411 2: DW_TAG_namespace // N
10412 3: DW_TAG_subprogram // declaration of N::foo
10413 4: DW_TAG_subprogram // definition of N::foo
10414 DW_AT_specification // refers to die #3
10415
10416 Thus, when processing die #4, we have to pretend that we're in
10417 the context of its DW_AT_specification, namely the contex of die
10418 #3. */
10419 spec_cu = cu;
10420 spec_die = die_specification (die, &spec_cu);
10421 if (spec_die == NULL)
10422 parent = die->parent;
10423 else
63d06c5c 10424 {
0114d602
DJ
10425 parent = spec_die->parent;
10426 cu = spec_cu;
63d06c5c 10427 }
0114d602
DJ
10428
10429 if (parent == NULL)
10430 return "";
63d06c5c 10431 else
0114d602
DJ
10432 switch (parent->tag)
10433 {
63d06c5c 10434 case DW_TAG_namespace:
0114d602 10435 parent_type = read_type_die (parent, cu);
acebe513
UW
10436 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
10437 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
10438 Work around this problem here. */
10439 if (cu->language == language_cplus
10440 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
10441 return "";
0114d602
DJ
10442 /* We give a name to even anonymous namespaces. */
10443 return TYPE_TAG_NAME (parent_type);
63d06c5c 10444 case DW_TAG_class_type:
680b30c7 10445 case DW_TAG_interface_type:
63d06c5c 10446 case DW_TAG_structure_type:
0114d602 10447 case DW_TAG_union_type:
f55ee35c 10448 case DW_TAG_module:
0114d602
DJ
10449 parent_type = read_type_die (parent, cu);
10450 if (TYPE_TAG_NAME (parent_type) != NULL)
10451 return TYPE_TAG_NAME (parent_type);
10452 else
10453 /* An anonymous structure is only allowed non-static data
10454 members; no typedefs, no member functions, et cetera.
10455 So it does not need a prefix. */
10456 return "";
63d06c5c 10457 default:
8176b9b8 10458 return determine_prefix (parent, cu);
63d06c5c 10459 }
63d06c5c
DC
10460}
10461
987504bb
JJ
10462/* Return a newly-allocated string formed by concatenating PREFIX and
10463 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
10464 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
10465 perform an obconcat, otherwise allocate storage for the result. The CU argument
10466 is used to determine the language and hence, the appropriate separator. */
10467
f55ee35c 10468#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
10469
10470static char *
f55ee35c
JK
10471typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
10472 int physname, struct dwarf2_cu *cu)
63d06c5c 10473{
f55ee35c 10474 const char *lead = "";
5c315b68 10475 const char *sep;
63d06c5c 10476
987504bb
JJ
10477 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
10478 sep = "";
10479 else if (cu->language == language_java)
10480 sep = ".";
f55ee35c
JK
10481 else if (cu->language == language_fortran && physname)
10482 {
10483 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
10484 DW_AT_MIPS_linkage_name is preferred and used instead. */
10485
10486 lead = "__";
10487 sep = "_MOD_";
10488 }
987504bb
JJ
10489 else
10490 sep = "::";
63d06c5c 10491
6dd47d34
DE
10492 if (prefix == NULL)
10493 prefix = "";
10494 if (suffix == NULL)
10495 suffix = "";
10496
987504bb
JJ
10497 if (obs == NULL)
10498 {
10499 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 10500
f55ee35c
JK
10501 strcpy (retval, lead);
10502 strcat (retval, prefix);
6dd47d34
DE
10503 strcat (retval, sep);
10504 strcat (retval, suffix);
63d06c5c
DC
10505 return retval;
10506 }
987504bb
JJ
10507 else
10508 {
10509 /* We have an obstack. */
f55ee35c 10510 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 10511 }
63d06c5c
DC
10512}
10513
c906108c
SS
10514/* Return sibling of die, NULL if no sibling. */
10515
f9aca02d 10516static struct die_info *
fba45db2 10517sibling_die (struct die_info *die)
c906108c 10518{
639d11d3 10519 return die->sibling;
c906108c
SS
10520}
10521
71c25dea
TT
10522/* Get name of a die, return NULL if not found. */
10523
10524static char *
10525dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
10526 struct obstack *obstack)
10527{
10528 if (name && cu->language == language_cplus)
10529 {
10530 char *canon_name = cp_canonicalize_string (name);
10531
10532 if (canon_name != NULL)
10533 {
10534 if (strcmp (canon_name, name) != 0)
10535 name = obsavestring (canon_name, strlen (canon_name),
10536 obstack);
10537 xfree (canon_name);
10538 }
10539 }
10540
10541 return name;
c906108c
SS
10542}
10543
9219021c
DC
10544/* Get name of a die, return NULL if not found. */
10545
10546static char *
e142c38c 10547dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
10548{
10549 struct attribute *attr;
10550
e142c38c 10551 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
10552 if (!attr || !DW_STRING (attr))
10553 return NULL;
10554
10555 switch (die->tag)
10556 {
10557 case DW_TAG_compile_unit:
10558 /* Compilation units have a DW_AT_name that is a filename, not
10559 a source language identifier. */
10560 case DW_TAG_enumeration_type:
10561 case DW_TAG_enumerator:
10562 /* These tags always have simple identifiers already; no need
10563 to canonicalize them. */
10564 return DW_STRING (attr);
907af001 10565
418835cc
KS
10566 case DW_TAG_subprogram:
10567 /* Java constructors will all be named "<init>", so return
10568 the class name when we see this special case. */
10569 if (cu->language == language_java
10570 && DW_STRING (attr) != NULL
10571 && strcmp (DW_STRING (attr), "<init>") == 0)
10572 {
10573 struct dwarf2_cu *spec_cu = cu;
10574 struct die_info *spec_die;
10575
10576 /* GCJ will output '<init>' for Java constructor names.
10577 For this special case, return the name of the parent class. */
10578
10579 /* GCJ may output suprogram DIEs with AT_specification set.
10580 If so, use the name of the specified DIE. */
10581 spec_die = die_specification (die, &spec_cu);
10582 if (spec_die != NULL)
10583 return dwarf2_name (spec_die, spec_cu);
10584
10585 do
10586 {
10587 die = die->parent;
10588 if (die->tag == DW_TAG_class_type)
10589 return dwarf2_name (die, cu);
10590 }
10591 while (die->tag != DW_TAG_compile_unit);
10592 }
907af001
UW
10593 break;
10594
10595 case DW_TAG_class_type:
10596 case DW_TAG_interface_type:
10597 case DW_TAG_structure_type:
10598 case DW_TAG_union_type:
10599 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
10600 structures or unions. These were of the form "._%d" in GCC 4.1,
10601 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
10602 and GCC 4.4. We work around this problem by ignoring these. */
10603 if (strncmp (DW_STRING (attr), "._", 2) == 0
10604 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
10605 return NULL;
10606 break;
10607
71c25dea 10608 default:
907af001
UW
10609 break;
10610 }
10611
10612 if (!DW_STRING_IS_CANONICAL (attr))
10613 {
10614 DW_STRING (attr)
10615 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
10616 &cu->objfile->objfile_obstack);
10617 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 10618 }
907af001 10619 return DW_STRING (attr);
9219021c
DC
10620}
10621
10622/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
10623 is none. *EXT_CU is the CU containing DIE on input, and the CU
10624 containing the return value on output. */
9219021c
DC
10625
10626static struct die_info *
f2f0e013 10627dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
10628{
10629 struct attribute *attr;
9219021c 10630
f2f0e013 10631 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
10632 if (attr == NULL)
10633 return NULL;
10634
f2f0e013 10635 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
10636}
10637
c906108c
SS
10638/* Convert a DIE tag into its string name. */
10639
10640static char *
aa1ee363 10641dwarf_tag_name (unsigned tag)
c906108c
SS
10642{
10643 switch (tag)
10644 {
10645 case DW_TAG_padding:
10646 return "DW_TAG_padding";
10647 case DW_TAG_array_type:
10648 return "DW_TAG_array_type";
10649 case DW_TAG_class_type:
10650 return "DW_TAG_class_type";
10651 case DW_TAG_entry_point:
10652 return "DW_TAG_entry_point";
10653 case DW_TAG_enumeration_type:
10654 return "DW_TAG_enumeration_type";
10655 case DW_TAG_formal_parameter:
10656 return "DW_TAG_formal_parameter";
10657 case DW_TAG_imported_declaration:
10658 return "DW_TAG_imported_declaration";
10659 case DW_TAG_label:
10660 return "DW_TAG_label";
10661 case DW_TAG_lexical_block:
10662 return "DW_TAG_lexical_block";
10663 case DW_TAG_member:
10664 return "DW_TAG_member";
10665 case DW_TAG_pointer_type:
10666 return "DW_TAG_pointer_type";
10667 case DW_TAG_reference_type:
10668 return "DW_TAG_reference_type";
10669 case DW_TAG_compile_unit:
10670 return "DW_TAG_compile_unit";
10671 case DW_TAG_string_type:
10672 return "DW_TAG_string_type";
10673 case DW_TAG_structure_type:
10674 return "DW_TAG_structure_type";
10675 case DW_TAG_subroutine_type:
10676 return "DW_TAG_subroutine_type";
10677 case DW_TAG_typedef:
10678 return "DW_TAG_typedef";
10679 case DW_TAG_union_type:
10680 return "DW_TAG_union_type";
10681 case DW_TAG_unspecified_parameters:
10682 return "DW_TAG_unspecified_parameters";
10683 case DW_TAG_variant:
10684 return "DW_TAG_variant";
10685 case DW_TAG_common_block:
10686 return "DW_TAG_common_block";
10687 case DW_TAG_common_inclusion:
10688 return "DW_TAG_common_inclusion";
10689 case DW_TAG_inheritance:
10690 return "DW_TAG_inheritance";
10691 case DW_TAG_inlined_subroutine:
10692 return "DW_TAG_inlined_subroutine";
10693 case DW_TAG_module:
10694 return "DW_TAG_module";
10695 case DW_TAG_ptr_to_member_type:
10696 return "DW_TAG_ptr_to_member_type";
10697 case DW_TAG_set_type:
10698 return "DW_TAG_set_type";
10699 case DW_TAG_subrange_type:
10700 return "DW_TAG_subrange_type";
10701 case DW_TAG_with_stmt:
10702 return "DW_TAG_with_stmt";
10703 case DW_TAG_access_declaration:
10704 return "DW_TAG_access_declaration";
10705 case DW_TAG_base_type:
10706 return "DW_TAG_base_type";
10707 case DW_TAG_catch_block:
10708 return "DW_TAG_catch_block";
10709 case DW_TAG_const_type:
10710 return "DW_TAG_const_type";
10711 case DW_TAG_constant:
10712 return "DW_TAG_constant";
10713 case DW_TAG_enumerator:
10714 return "DW_TAG_enumerator";
10715 case DW_TAG_file_type:
10716 return "DW_TAG_file_type";
10717 case DW_TAG_friend:
10718 return "DW_TAG_friend";
10719 case DW_TAG_namelist:
10720 return "DW_TAG_namelist";
10721 case DW_TAG_namelist_item:
10722 return "DW_TAG_namelist_item";
10723 case DW_TAG_packed_type:
10724 return "DW_TAG_packed_type";
10725 case DW_TAG_subprogram:
10726 return "DW_TAG_subprogram";
10727 case DW_TAG_template_type_param:
10728 return "DW_TAG_template_type_param";
10729 case DW_TAG_template_value_param:
10730 return "DW_TAG_template_value_param";
10731 case DW_TAG_thrown_type:
10732 return "DW_TAG_thrown_type";
10733 case DW_TAG_try_block:
10734 return "DW_TAG_try_block";
10735 case DW_TAG_variant_part:
10736 return "DW_TAG_variant_part";
10737 case DW_TAG_variable:
10738 return "DW_TAG_variable";
10739 case DW_TAG_volatile_type:
10740 return "DW_TAG_volatile_type";
d9fa45fe
DC
10741 case DW_TAG_dwarf_procedure:
10742 return "DW_TAG_dwarf_procedure";
10743 case DW_TAG_restrict_type:
10744 return "DW_TAG_restrict_type";
10745 case DW_TAG_interface_type:
10746 return "DW_TAG_interface_type";
10747 case DW_TAG_namespace:
10748 return "DW_TAG_namespace";
10749 case DW_TAG_imported_module:
10750 return "DW_TAG_imported_module";
10751 case DW_TAG_unspecified_type:
10752 return "DW_TAG_unspecified_type";
10753 case DW_TAG_partial_unit:
10754 return "DW_TAG_partial_unit";
10755 case DW_TAG_imported_unit:
10756 return "DW_TAG_imported_unit";
b7619582
GF
10757 case DW_TAG_condition:
10758 return "DW_TAG_condition";
10759 case DW_TAG_shared_type:
10760 return "DW_TAG_shared_type";
348e048f
DE
10761 case DW_TAG_type_unit:
10762 return "DW_TAG_type_unit";
c906108c
SS
10763 case DW_TAG_MIPS_loop:
10764 return "DW_TAG_MIPS_loop";
b7619582
GF
10765 case DW_TAG_HP_array_descriptor:
10766 return "DW_TAG_HP_array_descriptor";
c906108c
SS
10767 case DW_TAG_format_label:
10768 return "DW_TAG_format_label";
10769 case DW_TAG_function_template:
10770 return "DW_TAG_function_template";
10771 case DW_TAG_class_template:
10772 return "DW_TAG_class_template";
b7619582
GF
10773 case DW_TAG_GNU_BINCL:
10774 return "DW_TAG_GNU_BINCL";
10775 case DW_TAG_GNU_EINCL:
10776 return "DW_TAG_GNU_EINCL";
10777 case DW_TAG_upc_shared_type:
10778 return "DW_TAG_upc_shared_type";
10779 case DW_TAG_upc_strict_type:
10780 return "DW_TAG_upc_strict_type";
10781 case DW_TAG_upc_relaxed_type:
10782 return "DW_TAG_upc_relaxed_type";
10783 case DW_TAG_PGI_kanji_type:
10784 return "DW_TAG_PGI_kanji_type";
10785 case DW_TAG_PGI_interface_block:
10786 return "DW_TAG_PGI_interface_block";
c906108c
SS
10787 default:
10788 return "DW_TAG_<unknown>";
10789 }
10790}
10791
10792/* Convert a DWARF attribute code into its string name. */
10793
10794static char *
aa1ee363 10795dwarf_attr_name (unsigned attr)
c906108c
SS
10796{
10797 switch (attr)
10798 {
10799 case DW_AT_sibling:
10800 return "DW_AT_sibling";
10801 case DW_AT_location:
10802 return "DW_AT_location";
10803 case DW_AT_name:
10804 return "DW_AT_name";
10805 case DW_AT_ordering:
10806 return "DW_AT_ordering";
10807 case DW_AT_subscr_data:
10808 return "DW_AT_subscr_data";
10809 case DW_AT_byte_size:
10810 return "DW_AT_byte_size";
10811 case DW_AT_bit_offset:
10812 return "DW_AT_bit_offset";
10813 case DW_AT_bit_size:
10814 return "DW_AT_bit_size";
10815 case DW_AT_element_list:
10816 return "DW_AT_element_list";
10817 case DW_AT_stmt_list:
10818 return "DW_AT_stmt_list";
10819 case DW_AT_low_pc:
10820 return "DW_AT_low_pc";
10821 case DW_AT_high_pc:
10822 return "DW_AT_high_pc";
10823 case DW_AT_language:
10824 return "DW_AT_language";
10825 case DW_AT_member:
10826 return "DW_AT_member";
10827 case DW_AT_discr:
10828 return "DW_AT_discr";
10829 case DW_AT_discr_value:
10830 return "DW_AT_discr_value";
10831 case DW_AT_visibility:
10832 return "DW_AT_visibility";
10833 case DW_AT_import:
10834 return "DW_AT_import";
10835 case DW_AT_string_length:
10836 return "DW_AT_string_length";
10837 case DW_AT_common_reference:
10838 return "DW_AT_common_reference";
10839 case DW_AT_comp_dir:
10840 return "DW_AT_comp_dir";
10841 case DW_AT_const_value:
10842 return "DW_AT_const_value";
10843 case DW_AT_containing_type:
10844 return "DW_AT_containing_type";
10845 case DW_AT_default_value:
10846 return "DW_AT_default_value";
10847 case DW_AT_inline:
10848 return "DW_AT_inline";
10849 case DW_AT_is_optional:
10850 return "DW_AT_is_optional";
10851 case DW_AT_lower_bound:
10852 return "DW_AT_lower_bound";
10853 case DW_AT_producer:
10854 return "DW_AT_producer";
10855 case DW_AT_prototyped:
10856 return "DW_AT_prototyped";
10857 case DW_AT_return_addr:
10858 return "DW_AT_return_addr";
10859 case DW_AT_start_scope:
10860 return "DW_AT_start_scope";
09fa0d7c
JK
10861 case DW_AT_bit_stride:
10862 return "DW_AT_bit_stride";
c906108c
SS
10863 case DW_AT_upper_bound:
10864 return "DW_AT_upper_bound";
10865 case DW_AT_abstract_origin:
10866 return "DW_AT_abstract_origin";
10867 case DW_AT_accessibility:
10868 return "DW_AT_accessibility";
10869 case DW_AT_address_class:
10870 return "DW_AT_address_class";
10871 case DW_AT_artificial:
10872 return "DW_AT_artificial";
10873 case DW_AT_base_types:
10874 return "DW_AT_base_types";
10875 case DW_AT_calling_convention:
10876 return "DW_AT_calling_convention";
10877 case DW_AT_count:
10878 return "DW_AT_count";
10879 case DW_AT_data_member_location:
10880 return "DW_AT_data_member_location";
10881 case DW_AT_decl_column:
10882 return "DW_AT_decl_column";
10883 case DW_AT_decl_file:
10884 return "DW_AT_decl_file";
10885 case DW_AT_decl_line:
10886 return "DW_AT_decl_line";
10887 case DW_AT_declaration:
10888 return "DW_AT_declaration";
10889 case DW_AT_discr_list:
10890 return "DW_AT_discr_list";
10891 case DW_AT_encoding:
10892 return "DW_AT_encoding";
10893 case DW_AT_external:
10894 return "DW_AT_external";
10895 case DW_AT_frame_base:
10896 return "DW_AT_frame_base";
10897 case DW_AT_friend:
10898 return "DW_AT_friend";
10899 case DW_AT_identifier_case:
10900 return "DW_AT_identifier_case";
10901 case DW_AT_macro_info:
10902 return "DW_AT_macro_info";
10903 case DW_AT_namelist_items:
10904 return "DW_AT_namelist_items";
10905 case DW_AT_priority:
10906 return "DW_AT_priority";
10907 case DW_AT_segment:
10908 return "DW_AT_segment";
10909 case DW_AT_specification:
10910 return "DW_AT_specification";
10911 case DW_AT_static_link:
10912 return "DW_AT_static_link";
10913 case DW_AT_type:
10914 return "DW_AT_type";
10915 case DW_AT_use_location:
10916 return "DW_AT_use_location";
10917 case DW_AT_variable_parameter:
10918 return "DW_AT_variable_parameter";
10919 case DW_AT_virtuality:
10920 return "DW_AT_virtuality";
10921 case DW_AT_vtable_elem_location:
10922 return "DW_AT_vtable_elem_location";
b7619582 10923 /* DWARF 3 values. */
d9fa45fe
DC
10924 case DW_AT_allocated:
10925 return "DW_AT_allocated";
10926 case DW_AT_associated:
10927 return "DW_AT_associated";
10928 case DW_AT_data_location:
10929 return "DW_AT_data_location";
09fa0d7c
JK
10930 case DW_AT_byte_stride:
10931 return "DW_AT_byte_stride";
d9fa45fe
DC
10932 case DW_AT_entry_pc:
10933 return "DW_AT_entry_pc";
10934 case DW_AT_use_UTF8:
10935 return "DW_AT_use_UTF8";
10936 case DW_AT_extension:
10937 return "DW_AT_extension";
10938 case DW_AT_ranges:
10939 return "DW_AT_ranges";
10940 case DW_AT_trampoline:
10941 return "DW_AT_trampoline";
10942 case DW_AT_call_column:
10943 return "DW_AT_call_column";
10944 case DW_AT_call_file:
10945 return "DW_AT_call_file";
10946 case DW_AT_call_line:
10947 return "DW_AT_call_line";
b7619582
GF
10948 case DW_AT_description:
10949 return "DW_AT_description";
10950 case DW_AT_binary_scale:
10951 return "DW_AT_binary_scale";
10952 case DW_AT_decimal_scale:
10953 return "DW_AT_decimal_scale";
10954 case DW_AT_small:
10955 return "DW_AT_small";
10956 case DW_AT_decimal_sign:
10957 return "DW_AT_decimal_sign";
10958 case DW_AT_digit_count:
10959 return "DW_AT_digit_count";
10960 case DW_AT_picture_string:
10961 return "DW_AT_picture_string";
10962 case DW_AT_mutable:
10963 return "DW_AT_mutable";
10964 case DW_AT_threads_scaled:
10965 return "DW_AT_threads_scaled";
10966 case DW_AT_explicit:
10967 return "DW_AT_explicit";
10968 case DW_AT_object_pointer:
10969 return "DW_AT_object_pointer";
10970 case DW_AT_endianity:
10971 return "DW_AT_endianity";
10972 case DW_AT_elemental:
10973 return "DW_AT_elemental";
10974 case DW_AT_pure:
10975 return "DW_AT_pure";
10976 case DW_AT_recursive:
10977 return "DW_AT_recursive";
348e048f
DE
10978 /* DWARF 4 values. */
10979 case DW_AT_signature:
10980 return "DW_AT_signature";
31ef98ae
TT
10981 case DW_AT_linkage_name:
10982 return "DW_AT_linkage_name";
b7619582 10983 /* SGI/MIPS extensions. */
c764a876 10984#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
10985 case DW_AT_MIPS_fde:
10986 return "DW_AT_MIPS_fde";
c764a876 10987#endif
c906108c
SS
10988 case DW_AT_MIPS_loop_begin:
10989 return "DW_AT_MIPS_loop_begin";
10990 case DW_AT_MIPS_tail_loop_begin:
10991 return "DW_AT_MIPS_tail_loop_begin";
10992 case DW_AT_MIPS_epilog_begin:
10993 return "DW_AT_MIPS_epilog_begin";
10994 case DW_AT_MIPS_loop_unroll_factor:
10995 return "DW_AT_MIPS_loop_unroll_factor";
10996 case DW_AT_MIPS_software_pipeline_depth:
10997 return "DW_AT_MIPS_software_pipeline_depth";
10998 case DW_AT_MIPS_linkage_name:
10999 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11000 case DW_AT_MIPS_stride:
11001 return "DW_AT_MIPS_stride";
11002 case DW_AT_MIPS_abstract_name:
11003 return "DW_AT_MIPS_abstract_name";
11004 case DW_AT_MIPS_clone_origin:
11005 return "DW_AT_MIPS_clone_origin";
11006 case DW_AT_MIPS_has_inlines:
11007 return "DW_AT_MIPS_has_inlines";
b7619582 11008 /* HP extensions. */
c764a876 11009#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11010 case DW_AT_HP_block_index:
11011 return "DW_AT_HP_block_index";
c764a876 11012#endif
b7619582
GF
11013 case DW_AT_HP_unmodifiable:
11014 return "DW_AT_HP_unmodifiable";
11015 case DW_AT_HP_actuals_stmt_list:
11016 return "DW_AT_HP_actuals_stmt_list";
11017 case DW_AT_HP_proc_per_section:
11018 return "DW_AT_HP_proc_per_section";
11019 case DW_AT_HP_raw_data_ptr:
11020 return "DW_AT_HP_raw_data_ptr";
11021 case DW_AT_HP_pass_by_reference:
11022 return "DW_AT_HP_pass_by_reference";
11023 case DW_AT_HP_opt_level:
11024 return "DW_AT_HP_opt_level";
11025 case DW_AT_HP_prof_version_id:
11026 return "DW_AT_HP_prof_version_id";
11027 case DW_AT_HP_opt_flags:
11028 return "DW_AT_HP_opt_flags";
11029 case DW_AT_HP_cold_region_low_pc:
11030 return "DW_AT_HP_cold_region_low_pc";
11031 case DW_AT_HP_cold_region_high_pc:
11032 return "DW_AT_HP_cold_region_high_pc";
11033 case DW_AT_HP_all_variables_modifiable:
11034 return "DW_AT_HP_all_variables_modifiable";
11035 case DW_AT_HP_linkage_name:
11036 return "DW_AT_HP_linkage_name";
11037 case DW_AT_HP_prof_flags:
11038 return "DW_AT_HP_prof_flags";
11039 /* GNU extensions. */
c906108c
SS
11040 case DW_AT_sf_names:
11041 return "DW_AT_sf_names";
11042 case DW_AT_src_info:
11043 return "DW_AT_src_info";
11044 case DW_AT_mac_info:
11045 return "DW_AT_mac_info";
11046 case DW_AT_src_coords:
11047 return "DW_AT_src_coords";
11048 case DW_AT_body_begin:
11049 return "DW_AT_body_begin";
11050 case DW_AT_body_end:
11051 return "DW_AT_body_end";
f5f8a009
EZ
11052 case DW_AT_GNU_vector:
11053 return "DW_AT_GNU_vector";
b7619582
GF
11054 /* VMS extensions. */
11055 case DW_AT_VMS_rtnbeg_pd_address:
11056 return "DW_AT_VMS_rtnbeg_pd_address";
11057 /* UPC extension. */
11058 case DW_AT_upc_threads_scaled:
11059 return "DW_AT_upc_threads_scaled";
11060 /* PGI (STMicroelectronics) extensions. */
11061 case DW_AT_PGI_lbase:
11062 return "DW_AT_PGI_lbase";
11063 case DW_AT_PGI_soffset:
11064 return "DW_AT_PGI_soffset";
11065 case DW_AT_PGI_lstride:
11066 return "DW_AT_PGI_lstride";
c906108c
SS
11067 default:
11068 return "DW_AT_<unknown>";
11069 }
11070}
11071
11072/* Convert a DWARF value form code into its string name. */
11073
11074static char *
aa1ee363 11075dwarf_form_name (unsigned form)
c906108c
SS
11076{
11077 switch (form)
11078 {
11079 case DW_FORM_addr:
11080 return "DW_FORM_addr";
11081 case DW_FORM_block2:
11082 return "DW_FORM_block2";
11083 case DW_FORM_block4:
11084 return "DW_FORM_block4";
11085 case DW_FORM_data2:
11086 return "DW_FORM_data2";
11087 case DW_FORM_data4:
11088 return "DW_FORM_data4";
11089 case DW_FORM_data8:
11090 return "DW_FORM_data8";
11091 case DW_FORM_string:
11092 return "DW_FORM_string";
11093 case DW_FORM_block:
11094 return "DW_FORM_block";
11095 case DW_FORM_block1:
11096 return "DW_FORM_block1";
11097 case DW_FORM_data1:
11098 return "DW_FORM_data1";
11099 case DW_FORM_flag:
11100 return "DW_FORM_flag";
11101 case DW_FORM_sdata:
11102 return "DW_FORM_sdata";
11103 case DW_FORM_strp:
11104 return "DW_FORM_strp";
11105 case DW_FORM_udata:
11106 return "DW_FORM_udata";
11107 case DW_FORM_ref_addr:
11108 return "DW_FORM_ref_addr";
11109 case DW_FORM_ref1:
11110 return "DW_FORM_ref1";
11111 case DW_FORM_ref2:
11112 return "DW_FORM_ref2";
11113 case DW_FORM_ref4:
11114 return "DW_FORM_ref4";
11115 case DW_FORM_ref8:
11116 return "DW_FORM_ref8";
11117 case DW_FORM_ref_udata:
11118 return "DW_FORM_ref_udata";
11119 case DW_FORM_indirect:
11120 return "DW_FORM_indirect";
348e048f
DE
11121 case DW_FORM_sec_offset:
11122 return "DW_FORM_sec_offset";
11123 case DW_FORM_exprloc:
11124 return "DW_FORM_exprloc";
11125 case DW_FORM_flag_present:
11126 return "DW_FORM_flag_present";
11127 case DW_FORM_sig8:
11128 return "DW_FORM_sig8";
c906108c
SS
11129 default:
11130 return "DW_FORM_<unknown>";
11131 }
11132}
11133
11134/* Convert a DWARF stack opcode into its string name. */
11135
9eae7c52
TT
11136const char *
11137dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
11138{
11139 switch (op)
11140 {
11141 case DW_OP_addr:
11142 return "DW_OP_addr";
11143 case DW_OP_deref:
11144 return "DW_OP_deref";
11145 case DW_OP_const1u:
11146 return "DW_OP_const1u";
11147 case DW_OP_const1s:
11148 return "DW_OP_const1s";
11149 case DW_OP_const2u:
11150 return "DW_OP_const2u";
11151 case DW_OP_const2s:
11152 return "DW_OP_const2s";
11153 case DW_OP_const4u:
11154 return "DW_OP_const4u";
11155 case DW_OP_const4s:
11156 return "DW_OP_const4s";
11157 case DW_OP_const8u:
11158 return "DW_OP_const8u";
11159 case DW_OP_const8s:
11160 return "DW_OP_const8s";
11161 case DW_OP_constu:
11162 return "DW_OP_constu";
11163 case DW_OP_consts:
11164 return "DW_OP_consts";
11165 case DW_OP_dup:
11166 return "DW_OP_dup";
11167 case DW_OP_drop:
11168 return "DW_OP_drop";
11169 case DW_OP_over:
11170 return "DW_OP_over";
11171 case DW_OP_pick:
11172 return "DW_OP_pick";
11173 case DW_OP_swap:
11174 return "DW_OP_swap";
11175 case DW_OP_rot:
11176 return "DW_OP_rot";
11177 case DW_OP_xderef:
11178 return "DW_OP_xderef";
11179 case DW_OP_abs:
11180 return "DW_OP_abs";
11181 case DW_OP_and:
11182 return "DW_OP_and";
11183 case DW_OP_div:
11184 return "DW_OP_div";
11185 case DW_OP_minus:
11186 return "DW_OP_minus";
11187 case DW_OP_mod:
11188 return "DW_OP_mod";
11189 case DW_OP_mul:
11190 return "DW_OP_mul";
11191 case DW_OP_neg:
11192 return "DW_OP_neg";
11193 case DW_OP_not:
11194 return "DW_OP_not";
11195 case DW_OP_or:
11196 return "DW_OP_or";
11197 case DW_OP_plus:
11198 return "DW_OP_plus";
11199 case DW_OP_plus_uconst:
11200 return "DW_OP_plus_uconst";
11201 case DW_OP_shl:
11202 return "DW_OP_shl";
11203 case DW_OP_shr:
11204 return "DW_OP_shr";
11205 case DW_OP_shra:
11206 return "DW_OP_shra";
11207 case DW_OP_xor:
11208 return "DW_OP_xor";
11209 case DW_OP_bra:
11210 return "DW_OP_bra";
11211 case DW_OP_eq:
11212 return "DW_OP_eq";
11213 case DW_OP_ge:
11214 return "DW_OP_ge";
11215 case DW_OP_gt:
11216 return "DW_OP_gt";
11217 case DW_OP_le:
11218 return "DW_OP_le";
11219 case DW_OP_lt:
11220 return "DW_OP_lt";
11221 case DW_OP_ne:
11222 return "DW_OP_ne";
11223 case DW_OP_skip:
11224 return "DW_OP_skip";
11225 case DW_OP_lit0:
11226 return "DW_OP_lit0";
11227 case DW_OP_lit1:
11228 return "DW_OP_lit1";
11229 case DW_OP_lit2:
11230 return "DW_OP_lit2";
11231 case DW_OP_lit3:
11232 return "DW_OP_lit3";
11233 case DW_OP_lit4:
11234 return "DW_OP_lit4";
11235 case DW_OP_lit5:
11236 return "DW_OP_lit5";
11237 case DW_OP_lit6:
11238 return "DW_OP_lit6";
11239 case DW_OP_lit7:
11240 return "DW_OP_lit7";
11241 case DW_OP_lit8:
11242 return "DW_OP_lit8";
11243 case DW_OP_lit9:
11244 return "DW_OP_lit9";
11245 case DW_OP_lit10:
11246 return "DW_OP_lit10";
11247 case DW_OP_lit11:
11248 return "DW_OP_lit11";
11249 case DW_OP_lit12:
11250 return "DW_OP_lit12";
11251 case DW_OP_lit13:
11252 return "DW_OP_lit13";
11253 case DW_OP_lit14:
11254 return "DW_OP_lit14";
11255 case DW_OP_lit15:
11256 return "DW_OP_lit15";
11257 case DW_OP_lit16:
11258 return "DW_OP_lit16";
11259 case DW_OP_lit17:
11260 return "DW_OP_lit17";
11261 case DW_OP_lit18:
11262 return "DW_OP_lit18";
11263 case DW_OP_lit19:
11264 return "DW_OP_lit19";
11265 case DW_OP_lit20:
11266 return "DW_OP_lit20";
11267 case DW_OP_lit21:
11268 return "DW_OP_lit21";
11269 case DW_OP_lit22:
11270 return "DW_OP_lit22";
11271 case DW_OP_lit23:
11272 return "DW_OP_lit23";
11273 case DW_OP_lit24:
11274 return "DW_OP_lit24";
11275 case DW_OP_lit25:
11276 return "DW_OP_lit25";
11277 case DW_OP_lit26:
11278 return "DW_OP_lit26";
11279 case DW_OP_lit27:
11280 return "DW_OP_lit27";
11281 case DW_OP_lit28:
11282 return "DW_OP_lit28";
11283 case DW_OP_lit29:
11284 return "DW_OP_lit29";
11285 case DW_OP_lit30:
11286 return "DW_OP_lit30";
11287 case DW_OP_lit31:
11288 return "DW_OP_lit31";
11289 case DW_OP_reg0:
11290 return "DW_OP_reg0";
11291 case DW_OP_reg1:
11292 return "DW_OP_reg1";
11293 case DW_OP_reg2:
11294 return "DW_OP_reg2";
11295 case DW_OP_reg3:
11296 return "DW_OP_reg3";
11297 case DW_OP_reg4:
11298 return "DW_OP_reg4";
11299 case DW_OP_reg5:
11300 return "DW_OP_reg5";
11301 case DW_OP_reg6:
11302 return "DW_OP_reg6";
11303 case DW_OP_reg7:
11304 return "DW_OP_reg7";
11305 case DW_OP_reg8:
11306 return "DW_OP_reg8";
11307 case DW_OP_reg9:
11308 return "DW_OP_reg9";
11309 case DW_OP_reg10:
11310 return "DW_OP_reg10";
11311 case DW_OP_reg11:
11312 return "DW_OP_reg11";
11313 case DW_OP_reg12:
11314 return "DW_OP_reg12";
11315 case DW_OP_reg13:
11316 return "DW_OP_reg13";
11317 case DW_OP_reg14:
11318 return "DW_OP_reg14";
11319 case DW_OP_reg15:
11320 return "DW_OP_reg15";
11321 case DW_OP_reg16:
11322 return "DW_OP_reg16";
11323 case DW_OP_reg17:
11324 return "DW_OP_reg17";
11325 case DW_OP_reg18:
11326 return "DW_OP_reg18";
11327 case DW_OP_reg19:
11328 return "DW_OP_reg19";
11329 case DW_OP_reg20:
11330 return "DW_OP_reg20";
11331 case DW_OP_reg21:
11332 return "DW_OP_reg21";
11333 case DW_OP_reg22:
11334 return "DW_OP_reg22";
11335 case DW_OP_reg23:
11336 return "DW_OP_reg23";
11337 case DW_OP_reg24:
11338 return "DW_OP_reg24";
11339 case DW_OP_reg25:
11340 return "DW_OP_reg25";
11341 case DW_OP_reg26:
11342 return "DW_OP_reg26";
11343 case DW_OP_reg27:
11344 return "DW_OP_reg27";
11345 case DW_OP_reg28:
11346 return "DW_OP_reg28";
11347 case DW_OP_reg29:
11348 return "DW_OP_reg29";
11349 case DW_OP_reg30:
11350 return "DW_OP_reg30";
11351 case DW_OP_reg31:
11352 return "DW_OP_reg31";
11353 case DW_OP_breg0:
11354 return "DW_OP_breg0";
11355 case DW_OP_breg1:
11356 return "DW_OP_breg1";
11357 case DW_OP_breg2:
11358 return "DW_OP_breg2";
11359 case DW_OP_breg3:
11360 return "DW_OP_breg3";
11361 case DW_OP_breg4:
11362 return "DW_OP_breg4";
11363 case DW_OP_breg5:
11364 return "DW_OP_breg5";
11365 case DW_OP_breg6:
11366 return "DW_OP_breg6";
11367 case DW_OP_breg7:
11368 return "DW_OP_breg7";
11369 case DW_OP_breg8:
11370 return "DW_OP_breg8";
11371 case DW_OP_breg9:
11372 return "DW_OP_breg9";
11373 case DW_OP_breg10:
11374 return "DW_OP_breg10";
11375 case DW_OP_breg11:
11376 return "DW_OP_breg11";
11377 case DW_OP_breg12:
11378 return "DW_OP_breg12";
11379 case DW_OP_breg13:
11380 return "DW_OP_breg13";
11381 case DW_OP_breg14:
11382 return "DW_OP_breg14";
11383 case DW_OP_breg15:
11384 return "DW_OP_breg15";
11385 case DW_OP_breg16:
11386 return "DW_OP_breg16";
11387 case DW_OP_breg17:
11388 return "DW_OP_breg17";
11389 case DW_OP_breg18:
11390 return "DW_OP_breg18";
11391 case DW_OP_breg19:
11392 return "DW_OP_breg19";
11393 case DW_OP_breg20:
11394 return "DW_OP_breg20";
11395 case DW_OP_breg21:
11396 return "DW_OP_breg21";
11397 case DW_OP_breg22:
11398 return "DW_OP_breg22";
11399 case DW_OP_breg23:
11400 return "DW_OP_breg23";
11401 case DW_OP_breg24:
11402 return "DW_OP_breg24";
11403 case DW_OP_breg25:
11404 return "DW_OP_breg25";
11405 case DW_OP_breg26:
11406 return "DW_OP_breg26";
11407 case DW_OP_breg27:
11408 return "DW_OP_breg27";
11409 case DW_OP_breg28:
11410 return "DW_OP_breg28";
11411 case DW_OP_breg29:
11412 return "DW_OP_breg29";
11413 case DW_OP_breg30:
11414 return "DW_OP_breg30";
11415 case DW_OP_breg31:
11416 return "DW_OP_breg31";
11417 case DW_OP_regx:
11418 return "DW_OP_regx";
11419 case DW_OP_fbreg:
11420 return "DW_OP_fbreg";
11421 case DW_OP_bregx:
11422 return "DW_OP_bregx";
11423 case DW_OP_piece:
11424 return "DW_OP_piece";
11425 case DW_OP_deref_size:
11426 return "DW_OP_deref_size";
11427 case DW_OP_xderef_size:
11428 return "DW_OP_xderef_size";
11429 case DW_OP_nop:
11430 return "DW_OP_nop";
b7619582 11431 /* DWARF 3 extensions. */
ed348acc
EZ
11432 case DW_OP_push_object_address:
11433 return "DW_OP_push_object_address";
11434 case DW_OP_call2:
11435 return "DW_OP_call2";
11436 case DW_OP_call4:
11437 return "DW_OP_call4";
11438 case DW_OP_call_ref:
11439 return "DW_OP_call_ref";
b7619582
GF
11440 case DW_OP_form_tls_address:
11441 return "DW_OP_form_tls_address";
11442 case DW_OP_call_frame_cfa:
11443 return "DW_OP_call_frame_cfa";
11444 case DW_OP_bit_piece:
11445 return "DW_OP_bit_piece";
9eae7c52
TT
11446 /* DWARF 4 extensions. */
11447 case DW_OP_implicit_value:
11448 return "DW_OP_implicit_value";
11449 case DW_OP_stack_value:
11450 return "DW_OP_stack_value";
11451 /* GNU extensions. */
ed348acc
EZ
11452 case DW_OP_GNU_push_tls_address:
11453 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
11454 case DW_OP_GNU_uninit:
11455 return "DW_OP_GNU_uninit";
c906108c 11456 default:
9eae7c52 11457 return def ? "OP_<unknown>" : NULL;
c906108c
SS
11458 }
11459}
11460
11461static char *
fba45db2 11462dwarf_bool_name (unsigned mybool)
c906108c
SS
11463{
11464 if (mybool)
11465 return "TRUE";
11466 else
11467 return "FALSE";
11468}
11469
11470/* Convert a DWARF type code into its string name. */
11471
11472static char *
aa1ee363 11473dwarf_type_encoding_name (unsigned enc)
c906108c
SS
11474{
11475 switch (enc)
11476 {
b7619582
GF
11477 case DW_ATE_void:
11478 return "DW_ATE_void";
c906108c
SS
11479 case DW_ATE_address:
11480 return "DW_ATE_address";
11481 case DW_ATE_boolean:
11482 return "DW_ATE_boolean";
11483 case DW_ATE_complex_float:
11484 return "DW_ATE_complex_float";
11485 case DW_ATE_float:
11486 return "DW_ATE_float";
11487 case DW_ATE_signed:
11488 return "DW_ATE_signed";
11489 case DW_ATE_signed_char:
11490 return "DW_ATE_signed_char";
11491 case DW_ATE_unsigned:
11492 return "DW_ATE_unsigned";
11493 case DW_ATE_unsigned_char:
11494 return "DW_ATE_unsigned_char";
b7619582 11495 /* DWARF 3. */
d9fa45fe
DC
11496 case DW_ATE_imaginary_float:
11497 return "DW_ATE_imaginary_float";
b7619582
GF
11498 case DW_ATE_packed_decimal:
11499 return "DW_ATE_packed_decimal";
11500 case DW_ATE_numeric_string:
11501 return "DW_ATE_numeric_string";
11502 case DW_ATE_edited:
11503 return "DW_ATE_edited";
11504 case DW_ATE_signed_fixed:
11505 return "DW_ATE_signed_fixed";
11506 case DW_ATE_unsigned_fixed:
11507 return "DW_ATE_unsigned_fixed";
11508 case DW_ATE_decimal_float:
11509 return "DW_ATE_decimal_float";
75079b2b
TT
11510 /* DWARF 4. */
11511 case DW_ATE_UTF:
11512 return "DW_ATE_UTF";
b7619582
GF
11513 /* HP extensions. */
11514 case DW_ATE_HP_float80:
11515 return "DW_ATE_HP_float80";
11516 case DW_ATE_HP_complex_float80:
11517 return "DW_ATE_HP_complex_float80";
11518 case DW_ATE_HP_float128:
11519 return "DW_ATE_HP_float128";
11520 case DW_ATE_HP_complex_float128:
11521 return "DW_ATE_HP_complex_float128";
11522 case DW_ATE_HP_floathpintel:
11523 return "DW_ATE_HP_floathpintel";
11524 case DW_ATE_HP_imaginary_float80:
11525 return "DW_ATE_HP_imaginary_float80";
11526 case DW_ATE_HP_imaginary_float128:
11527 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
11528 default:
11529 return "DW_ATE_<unknown>";
11530 }
11531}
11532
11533/* Convert a DWARF call frame info operation to its string name. */
11534
11535#if 0
11536static char *
aa1ee363 11537dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
11538{
11539 switch (cfi_opc)
11540 {
11541 case DW_CFA_advance_loc:
11542 return "DW_CFA_advance_loc";
11543 case DW_CFA_offset:
11544 return "DW_CFA_offset";
11545 case DW_CFA_restore:
11546 return "DW_CFA_restore";
11547 case DW_CFA_nop:
11548 return "DW_CFA_nop";
11549 case DW_CFA_set_loc:
11550 return "DW_CFA_set_loc";
11551 case DW_CFA_advance_loc1:
11552 return "DW_CFA_advance_loc1";
11553 case DW_CFA_advance_loc2:
11554 return "DW_CFA_advance_loc2";
11555 case DW_CFA_advance_loc4:
11556 return "DW_CFA_advance_loc4";
11557 case DW_CFA_offset_extended:
11558 return "DW_CFA_offset_extended";
11559 case DW_CFA_restore_extended:
11560 return "DW_CFA_restore_extended";
11561 case DW_CFA_undefined:
11562 return "DW_CFA_undefined";
11563 case DW_CFA_same_value:
11564 return "DW_CFA_same_value";
11565 case DW_CFA_register:
11566 return "DW_CFA_register";
11567 case DW_CFA_remember_state:
11568 return "DW_CFA_remember_state";
11569 case DW_CFA_restore_state:
11570 return "DW_CFA_restore_state";
11571 case DW_CFA_def_cfa:
11572 return "DW_CFA_def_cfa";
11573 case DW_CFA_def_cfa_register:
11574 return "DW_CFA_def_cfa_register";
11575 case DW_CFA_def_cfa_offset:
11576 return "DW_CFA_def_cfa_offset";
b7619582 11577 /* DWARF 3. */
985cb1a3
JM
11578 case DW_CFA_def_cfa_expression:
11579 return "DW_CFA_def_cfa_expression";
11580 case DW_CFA_expression:
11581 return "DW_CFA_expression";
11582 case DW_CFA_offset_extended_sf:
11583 return "DW_CFA_offset_extended_sf";
11584 case DW_CFA_def_cfa_sf:
11585 return "DW_CFA_def_cfa_sf";
11586 case DW_CFA_def_cfa_offset_sf:
11587 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
11588 case DW_CFA_val_offset:
11589 return "DW_CFA_val_offset";
11590 case DW_CFA_val_offset_sf:
11591 return "DW_CFA_val_offset_sf";
11592 case DW_CFA_val_expression:
11593 return "DW_CFA_val_expression";
11594 /* SGI/MIPS specific. */
c906108c
SS
11595 case DW_CFA_MIPS_advance_loc8:
11596 return "DW_CFA_MIPS_advance_loc8";
b7619582 11597 /* GNU extensions. */
985cb1a3
JM
11598 case DW_CFA_GNU_window_save:
11599 return "DW_CFA_GNU_window_save";
11600 case DW_CFA_GNU_args_size:
11601 return "DW_CFA_GNU_args_size";
11602 case DW_CFA_GNU_negative_offset_extended:
11603 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
11604 default:
11605 return "DW_CFA_<unknown>";
11606 }
11607}
11608#endif
11609
f9aca02d 11610static void
d97bc12b 11611dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
11612{
11613 unsigned int i;
11614
d97bc12b
DE
11615 print_spaces (indent, f);
11616 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 11617 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
11618
11619 if (die->parent != NULL)
11620 {
11621 print_spaces (indent, f);
11622 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
11623 die->parent->offset);
11624 }
11625
11626 print_spaces (indent, f);
11627 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 11628 dwarf_bool_name (die->child != NULL));
c906108c 11629
d97bc12b
DE
11630 print_spaces (indent, f);
11631 fprintf_unfiltered (f, " attributes:\n");
11632
c906108c
SS
11633 for (i = 0; i < die->num_attrs; ++i)
11634 {
d97bc12b
DE
11635 print_spaces (indent, f);
11636 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
11637 dwarf_attr_name (die->attrs[i].name),
11638 dwarf_form_name (die->attrs[i].form));
d97bc12b 11639
c906108c
SS
11640 switch (die->attrs[i].form)
11641 {
11642 case DW_FORM_ref_addr:
11643 case DW_FORM_addr:
d97bc12b 11644 fprintf_unfiltered (f, "address: ");
5af949e3 11645 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
11646 break;
11647 case DW_FORM_block2:
11648 case DW_FORM_block4:
11649 case DW_FORM_block:
11650 case DW_FORM_block1:
d97bc12b 11651 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 11652 break;
2dc7f7b3
TT
11653 case DW_FORM_exprloc:
11654 fprintf_unfiltered (f, "expression: size %u",
11655 DW_BLOCK (&die->attrs[i])->size);
11656 break;
10b3939b
DJ
11657 case DW_FORM_ref1:
11658 case DW_FORM_ref2:
11659 case DW_FORM_ref4:
d97bc12b 11660 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
11661 (long) (DW_ADDR (&die->attrs[i])));
11662 break;
c906108c
SS
11663 case DW_FORM_data1:
11664 case DW_FORM_data2:
11665 case DW_FORM_data4:
ce5d95e1 11666 case DW_FORM_data8:
c906108c
SS
11667 case DW_FORM_udata:
11668 case DW_FORM_sdata:
43bbcdc2
PH
11669 fprintf_unfiltered (f, "constant: %s",
11670 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 11671 break;
2dc7f7b3
TT
11672 case DW_FORM_sec_offset:
11673 fprintf_unfiltered (f, "section offset: %s",
11674 pulongest (DW_UNSND (&die->attrs[i])));
11675 break;
348e048f
DE
11676 case DW_FORM_sig8:
11677 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
11678 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
11679 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
11680 else
11681 fprintf_unfiltered (f, "signatured type, offset: unknown");
11682 break;
c906108c 11683 case DW_FORM_string:
4bdf3d34 11684 case DW_FORM_strp:
8285870a 11685 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 11686 DW_STRING (&die->attrs[i])
8285870a
JK
11687 ? DW_STRING (&die->attrs[i]) : "",
11688 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
11689 break;
11690 case DW_FORM_flag:
11691 if (DW_UNSND (&die->attrs[i]))
d97bc12b 11692 fprintf_unfiltered (f, "flag: TRUE");
c906108c 11693 else
d97bc12b 11694 fprintf_unfiltered (f, "flag: FALSE");
c906108c 11695 break;
2dc7f7b3
TT
11696 case DW_FORM_flag_present:
11697 fprintf_unfiltered (f, "flag: TRUE");
11698 break;
a8329558
KW
11699 case DW_FORM_indirect:
11700 /* the reader will have reduced the indirect form to
11701 the "base form" so this form should not occur */
d97bc12b 11702 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 11703 break;
c906108c 11704 default:
d97bc12b 11705 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 11706 die->attrs[i].form);
d97bc12b 11707 break;
c906108c 11708 }
d97bc12b 11709 fprintf_unfiltered (f, "\n");
c906108c
SS
11710 }
11711}
11712
f9aca02d 11713static void
d97bc12b 11714dump_die_for_error (struct die_info *die)
c906108c 11715{
d97bc12b
DE
11716 dump_die_shallow (gdb_stderr, 0, die);
11717}
11718
11719static void
11720dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
11721{
11722 int indent = level * 4;
11723
11724 gdb_assert (die != NULL);
11725
11726 if (level >= max_level)
11727 return;
11728
11729 dump_die_shallow (f, indent, die);
11730
11731 if (die->child != NULL)
c906108c 11732 {
d97bc12b
DE
11733 print_spaces (indent, f);
11734 fprintf_unfiltered (f, " Children:");
11735 if (level + 1 < max_level)
11736 {
11737 fprintf_unfiltered (f, "\n");
11738 dump_die_1 (f, level + 1, max_level, die->child);
11739 }
11740 else
11741 {
11742 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
11743 }
11744 }
11745
11746 if (die->sibling != NULL && level > 0)
11747 {
11748 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
11749 }
11750}
11751
d97bc12b
DE
11752/* This is called from the pdie macro in gdbinit.in.
11753 It's not static so gcc will keep a copy callable from gdb. */
11754
11755void
11756dump_die (struct die_info *die, int max_level)
11757{
11758 dump_die_1 (gdb_stdlog, 0, max_level, die);
11759}
11760
f9aca02d 11761static void
51545339 11762store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11763{
51545339 11764 void **slot;
c906108c 11765
51545339
DJ
11766 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
11767
11768 *slot = die;
c906108c
SS
11769}
11770
93311388
DE
11771static int
11772is_ref_attr (struct attribute *attr)
c906108c 11773{
c906108c
SS
11774 switch (attr->form)
11775 {
11776 case DW_FORM_ref_addr:
c906108c
SS
11777 case DW_FORM_ref1:
11778 case DW_FORM_ref2:
11779 case DW_FORM_ref4:
613e1657 11780 case DW_FORM_ref8:
c906108c 11781 case DW_FORM_ref_udata:
93311388 11782 return 1;
c906108c 11783 default:
93311388 11784 return 0;
c906108c 11785 }
93311388
DE
11786}
11787
11788static unsigned int
11789dwarf2_get_ref_die_offset (struct attribute *attr)
11790{
11791 if (is_ref_attr (attr))
11792 return DW_ADDR (attr);
11793
11794 complaint (&symfile_complaints,
11795 _("unsupported die ref attribute form: '%s'"),
11796 dwarf_form_name (attr->form));
11797 return 0;
c906108c
SS
11798}
11799
43bbcdc2
PH
11800/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
11801 * the value held by the attribute is not constant. */
a02abb62 11802
43bbcdc2 11803static LONGEST
a02abb62
JB
11804dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
11805{
11806 if (attr->form == DW_FORM_sdata)
11807 return DW_SND (attr);
11808 else if (attr->form == DW_FORM_udata
11809 || attr->form == DW_FORM_data1
11810 || attr->form == DW_FORM_data2
11811 || attr->form == DW_FORM_data4
11812 || attr->form == DW_FORM_data8)
11813 return DW_UNSND (attr);
11814 else
11815 {
e2e0b3e5 11816 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
11817 dwarf_form_name (attr->form));
11818 return default_value;
11819 }
11820}
11821
03dd20cc 11822/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
11823 unit and add it to our queue.
11824 The result is non-zero if PER_CU was queued, otherwise the result is zero
11825 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 11826
348e048f 11827static int
03dd20cc
DJ
11828maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
11829 struct dwarf2_per_cu_data *per_cu)
11830{
11831 /* Mark the dependence relation so that we don't flush PER_CU
11832 too early. */
11833 dwarf2_add_dependence (this_cu, per_cu);
11834
11835 /* If it's already on the queue, we have nothing to do. */
11836 if (per_cu->queued)
348e048f 11837 return 0;
03dd20cc
DJ
11838
11839 /* If the compilation unit is already loaded, just mark it as
11840 used. */
11841 if (per_cu->cu != NULL)
11842 {
11843 per_cu->cu->last_used = 0;
348e048f 11844 return 0;
03dd20cc
DJ
11845 }
11846
11847 /* Add it to the queue. */
11848 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
11849
11850 return 1;
11851}
11852
11853/* Follow reference or signature attribute ATTR of SRC_DIE.
11854 On entry *REF_CU is the CU of SRC_DIE.
11855 On exit *REF_CU is the CU of the result. */
11856
11857static struct die_info *
11858follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
11859 struct dwarf2_cu **ref_cu)
11860{
11861 struct die_info *die;
11862
11863 if (is_ref_attr (attr))
11864 die = follow_die_ref (src_die, attr, ref_cu);
11865 else if (attr->form == DW_FORM_sig8)
11866 die = follow_die_sig (src_die, attr, ref_cu);
11867 else
11868 {
11869 dump_die_for_error (src_die);
11870 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
11871 (*ref_cu)->objfile->name);
11872 }
11873
11874 return die;
03dd20cc
DJ
11875}
11876
5c631832
JK
11877/* Follow reference OFFSET.
11878 On entry *REF_CU is the CU of source DIE referencing OFFSET.
f504f079
DE
11879 On exit *REF_CU is the CU of the result. */
11880
f9aca02d 11881static struct die_info *
5c631832 11882follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 11883{
10b3939b 11884 struct die_info temp_die;
f2f0e013 11885 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 11886
348e048f
DE
11887 gdb_assert (cu->per_cu != NULL);
11888
348e048f
DE
11889 if (cu->per_cu->from_debug_types)
11890 {
11891 /* .debug_types CUs cannot reference anything outside their CU.
11892 If they need to, they have to reference a signatured type via
11893 DW_FORM_sig8. */
11894 if (! offset_in_cu_p (&cu->header, offset))
5c631832 11895 return NULL;
348e048f
DE
11896 target_cu = cu;
11897 }
11898 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
11899 {
11900 struct dwarf2_per_cu_data *per_cu;
9a619af0 11901
45452591 11902 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
11903
11904 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
11905 if (maybe_queue_comp_unit (cu, per_cu))
11906 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 11907
10b3939b
DJ
11908 target_cu = per_cu->cu;
11909 }
11910 else
11911 target_cu = cu;
c906108c 11912
f2f0e013 11913 *ref_cu = target_cu;
51545339 11914 temp_die.offset = offset;
5c631832
JK
11915 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
11916}
10b3939b 11917
5c631832
JK
11918/* Follow reference attribute ATTR of SRC_DIE.
11919 On entry *REF_CU is the CU of SRC_DIE.
11920 On exit *REF_CU is the CU of the result. */
11921
11922static struct die_info *
11923follow_die_ref (struct die_info *src_die, struct attribute *attr,
11924 struct dwarf2_cu **ref_cu)
11925{
11926 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11927 struct dwarf2_cu *cu = *ref_cu;
11928 struct die_info *die;
11929
11930 die = follow_die_offset (offset, ref_cu);
11931 if (!die)
11932 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
11933 "at 0x%x [in module %s]"),
11934 offset, src_die->offset, cu->objfile->name);
348e048f 11935
5c631832
JK
11936 return die;
11937}
11938
11939/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
11940 value is intended for DW_OP_call*. */
11941
11942struct dwarf2_locexpr_baton
11943dwarf2_fetch_die_location_block (unsigned int offset,
11944 struct dwarf2_per_cu_data *per_cu)
11945{
11946 struct dwarf2_cu *cu = per_cu->cu;
11947 struct die_info *die;
11948 struct attribute *attr;
11949 struct dwarf2_locexpr_baton retval;
11950
11951 die = follow_die_offset (offset, &cu);
11952 if (!die)
11953 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
11954 offset, per_cu->cu->objfile->name);
11955
11956 attr = dwarf2_attr (die, DW_AT_location, cu);
11957 if (!attr)
11958 {
11959 /* DWARF: "If there is no such attribute, then there is no effect.". */
11960
11961 retval.data = NULL;
11962 retval.size = 0;
11963 }
11964 else
11965 {
11966 if (!attr_form_is_block (attr))
11967 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
11968 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
11969 offset, per_cu->cu->objfile->name);
11970
11971 retval.data = DW_BLOCK (attr)->data;
11972 retval.size = DW_BLOCK (attr)->size;
11973 }
11974 retval.per_cu = cu->per_cu;
11975 return retval;
348e048f
DE
11976}
11977
11978/* Follow the signature attribute ATTR in SRC_DIE.
11979 On entry *REF_CU is the CU of SRC_DIE.
11980 On exit *REF_CU is the CU of the result. */
11981
11982static struct die_info *
11983follow_die_sig (struct die_info *src_die, struct attribute *attr,
11984 struct dwarf2_cu **ref_cu)
11985{
11986 struct objfile *objfile = (*ref_cu)->objfile;
11987 struct die_info temp_die;
11988 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11989 struct dwarf2_cu *sig_cu;
11990 struct die_info *die;
11991
11992 /* sig_type will be NULL if the signatured type is missing from
11993 the debug info. */
11994 if (sig_type == NULL)
11995 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11996 "at 0x%x [in module %s]"),
11997 src_die->offset, objfile->name);
11998
11999 /* If necessary, add it to the queue and load its DIEs. */
12000
12001 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12002 read_signatured_type (objfile, sig_type);
12003
12004 gdb_assert (sig_type->per_cu.cu != NULL);
12005
12006 sig_cu = sig_type->per_cu.cu;
12007 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12008 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12009 if (die)
12010 {
12011 *ref_cu = sig_cu;
12012 return die;
12013 }
12014
12015 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12016 "at 0x%x [in module %s]"),
12017 sig_type->type_offset, src_die->offset, objfile->name);
12018}
12019
12020/* Given an offset of a signatured type, return its signatured_type. */
12021
12022static struct signatured_type *
12023lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12024{
12025 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12026 unsigned int length, initial_length_size;
12027 unsigned int sig_offset;
12028 struct signatured_type find_entry, *type_sig;
12029
12030 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12031 sig_offset = (initial_length_size
12032 + 2 /*version*/
12033 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12034 + 1 /*address_size*/);
12035 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12036 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12037
12038 /* This is only used to lookup previously recorded types.
12039 If we didn't find it, it's our bug. */
12040 gdb_assert (type_sig != NULL);
12041 gdb_assert (offset == type_sig->offset);
12042
12043 return type_sig;
12044}
12045
12046/* Read in signatured type at OFFSET and build its CU and die(s). */
12047
12048static void
12049read_signatured_type_at_offset (struct objfile *objfile,
12050 unsigned int offset)
12051{
12052 struct signatured_type *type_sig;
12053
be391dca
TT
12054 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12055
348e048f
DE
12056 /* We have the section offset, but we need the signature to do the
12057 hash table lookup. */
12058 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12059
12060 gdb_assert (type_sig->per_cu.cu == NULL);
12061
12062 read_signatured_type (objfile, type_sig);
12063
12064 gdb_assert (type_sig->per_cu.cu != NULL);
12065}
12066
12067/* Read in a signatured type and build its CU and DIEs. */
12068
12069static void
12070read_signatured_type (struct objfile *objfile,
12071 struct signatured_type *type_sig)
12072{
12073 gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12074 struct die_reader_specs reader_specs;
12075 struct dwarf2_cu *cu;
12076 ULONGEST signature;
12077 struct cleanup *back_to, *free_cu_cleanup;
12078 struct attribute *attr;
12079
12080 gdb_assert (type_sig->per_cu.cu == NULL);
12081
12082 cu = xmalloc (sizeof (struct dwarf2_cu));
12083 memset (cu, 0, sizeof (struct dwarf2_cu));
12084 obstack_init (&cu->comp_unit_obstack);
12085 cu->objfile = objfile;
12086 type_sig->per_cu.cu = cu;
12087 cu->per_cu = &type_sig->per_cu;
12088
12089 /* If an error occurs while loading, release our storage. */
12090 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12091
12092 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12093 types_ptr, objfile->obfd);
12094 gdb_assert (signature == type_sig->signature);
12095
12096 cu->die_hash
12097 = htab_create_alloc_ex (cu->header.length / 12,
12098 die_hash,
12099 die_eq,
12100 NULL,
12101 &cu->comp_unit_obstack,
12102 hashtab_obstack_allocate,
12103 dummy_obstack_deallocate);
12104
12105 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12106 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12107
12108 init_cu_die_reader (&reader_specs, cu);
12109
12110 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12111 NULL /*parent*/);
12112
12113 /* We try not to read any attributes in this function, because not
12114 all objfiles needed for references have been loaded yet, and symbol
12115 table processing isn't initialized. But we have to set the CU language,
12116 or we won't be able to build types correctly. */
12117 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12118 if (attr)
12119 set_cu_language (DW_UNSND (attr), cu);
12120 else
12121 set_cu_language (language_minimal, cu);
12122
12123 do_cleanups (back_to);
12124
12125 /* We've successfully allocated this compilation unit. Let our caller
12126 clean it up when finished with it. */
12127 discard_cleanups (free_cu_cleanup);
12128
12129 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12130 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
12131}
12132
c906108c
SS
12133/* Decode simple location descriptions.
12134 Given a pointer to a dwarf block that defines a location, compute
12135 the location and return the value.
12136
4cecd739
DJ
12137 NOTE drow/2003-11-18: This function is called in two situations
12138 now: for the address of static or global variables (partial symbols
12139 only) and for offsets into structures which are expected to be
12140 (more or less) constant. The partial symbol case should go away,
12141 and only the constant case should remain. That will let this
12142 function complain more accurately. A few special modes are allowed
12143 without complaint for global variables (for instance, global
12144 register values and thread-local values).
c906108c
SS
12145
12146 A location description containing no operations indicates that the
4cecd739 12147 object is optimized out. The return value is 0 for that case.
6b992462
DJ
12148 FIXME drow/2003-11-16: No callers check for this case any more; soon all
12149 callers will only want a very basic result and this can become a
12150 complaint.
c906108c 12151
c906108c
SS
12152 Note that stack[0] is unused except as a default error return.
12153 Note that stack overflow is not yet handled. */
12154
12155static CORE_ADDR
e7c27a73 12156decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 12157{
e7c27a73 12158 struct objfile *objfile = cu->objfile;
c906108c
SS
12159 int i;
12160 int size = blk->size;
fe1b8b76 12161 gdb_byte *data = blk->data;
c906108c
SS
12162 CORE_ADDR stack[64];
12163 int stacki;
12164 unsigned int bytes_read, unsnd;
fe1b8b76 12165 gdb_byte op;
c906108c
SS
12166
12167 i = 0;
12168 stacki = 0;
12169 stack[stacki] = 0;
c906108c
SS
12170
12171 while (i < size)
12172 {
c906108c
SS
12173 op = data[i++];
12174 switch (op)
12175 {
f1bea926
JM
12176 case DW_OP_lit0:
12177 case DW_OP_lit1:
12178 case DW_OP_lit2:
12179 case DW_OP_lit3:
12180 case DW_OP_lit4:
12181 case DW_OP_lit5:
12182 case DW_OP_lit6:
12183 case DW_OP_lit7:
12184 case DW_OP_lit8:
12185 case DW_OP_lit9:
12186 case DW_OP_lit10:
12187 case DW_OP_lit11:
12188 case DW_OP_lit12:
12189 case DW_OP_lit13:
12190 case DW_OP_lit14:
12191 case DW_OP_lit15:
12192 case DW_OP_lit16:
12193 case DW_OP_lit17:
12194 case DW_OP_lit18:
12195 case DW_OP_lit19:
12196 case DW_OP_lit20:
12197 case DW_OP_lit21:
12198 case DW_OP_lit22:
12199 case DW_OP_lit23:
12200 case DW_OP_lit24:
12201 case DW_OP_lit25:
12202 case DW_OP_lit26:
12203 case DW_OP_lit27:
12204 case DW_OP_lit28:
12205 case DW_OP_lit29:
12206 case DW_OP_lit30:
12207 case DW_OP_lit31:
12208 stack[++stacki] = op - DW_OP_lit0;
12209 break;
12210
c906108c
SS
12211 case DW_OP_reg0:
12212 case DW_OP_reg1:
12213 case DW_OP_reg2:
12214 case DW_OP_reg3:
12215 case DW_OP_reg4:
12216 case DW_OP_reg5:
12217 case DW_OP_reg6:
12218 case DW_OP_reg7:
12219 case DW_OP_reg8:
12220 case DW_OP_reg9:
12221 case DW_OP_reg10:
12222 case DW_OP_reg11:
12223 case DW_OP_reg12:
12224 case DW_OP_reg13:
12225 case DW_OP_reg14:
12226 case DW_OP_reg15:
12227 case DW_OP_reg16:
12228 case DW_OP_reg17:
12229 case DW_OP_reg18:
12230 case DW_OP_reg19:
12231 case DW_OP_reg20:
12232 case DW_OP_reg21:
12233 case DW_OP_reg22:
12234 case DW_OP_reg23:
12235 case DW_OP_reg24:
12236 case DW_OP_reg25:
12237 case DW_OP_reg26:
12238 case DW_OP_reg27:
12239 case DW_OP_reg28:
12240 case DW_OP_reg29:
12241 case DW_OP_reg30:
12242 case DW_OP_reg31:
c906108c 12243 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
12244 if (i < size)
12245 dwarf2_complex_location_expr_complaint ();
c906108c
SS
12246 break;
12247
12248 case DW_OP_regx:
c906108c
SS
12249 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12250 i += bytes_read;
c906108c 12251 stack[++stacki] = unsnd;
4cecd739
DJ
12252 if (i < size)
12253 dwarf2_complex_location_expr_complaint ();
c906108c
SS
12254 break;
12255
12256 case DW_OP_addr:
107d2387 12257 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 12258 cu, &bytes_read);
107d2387 12259 i += bytes_read;
c906108c
SS
12260 break;
12261
12262 case DW_OP_const1u:
12263 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12264 i += 1;
12265 break;
12266
12267 case DW_OP_const1s:
12268 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12269 i += 1;
12270 break;
12271
12272 case DW_OP_const2u:
12273 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12274 i += 2;
12275 break;
12276
12277 case DW_OP_const2s:
12278 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12279 i += 2;
12280 break;
12281
12282 case DW_OP_const4u:
12283 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12284 i += 4;
12285 break;
12286
12287 case DW_OP_const4s:
12288 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12289 i += 4;
12290 break;
12291
12292 case DW_OP_constu:
12293 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 12294 &bytes_read);
c906108c
SS
12295 i += bytes_read;
12296 break;
12297
12298 case DW_OP_consts:
12299 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12300 i += bytes_read;
12301 break;
12302
f1bea926
JM
12303 case DW_OP_dup:
12304 stack[stacki + 1] = stack[stacki];
12305 stacki++;
12306 break;
12307
c906108c
SS
12308 case DW_OP_plus:
12309 stack[stacki - 1] += stack[stacki];
12310 stacki--;
12311 break;
12312
12313 case DW_OP_plus_uconst:
12314 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12315 i += bytes_read;
12316 break;
12317
12318 case DW_OP_minus:
f1bea926 12319 stack[stacki - 1] -= stack[stacki];
c906108c
SS
12320 stacki--;
12321 break;
12322
7a292a7a 12323 case DW_OP_deref:
7a292a7a 12324 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
12325 this using GDB's address_class enum. This is valid for partial
12326 global symbols, although the variable's address will be bogus
12327 in the psymtab. */
7a292a7a 12328 if (i < size)
4d3c2250 12329 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
12330 break;
12331
9d774e44 12332 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
12333 /* The top of the stack has the offset from the beginning
12334 of the thread control block at which the variable is located. */
12335 /* Nothing should follow this operator, so the top of stack would
12336 be returned. */
4cecd739
DJ
12337 /* This is valid for partial global symbols, but the variable's
12338 address will be bogus in the psymtab. */
9d774e44 12339 if (i < size)
4d3c2250 12340 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
12341 break;
12342
42be36b3
CT
12343 case DW_OP_GNU_uninit:
12344 break;
12345
c906108c 12346 default:
e2e0b3e5 12347 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 12348 dwarf_stack_op_name (op, 1));
c906108c
SS
12349 return (stack[stacki]);
12350 }
12351 }
12352 return (stack[stacki]);
12353}
12354
12355/* memory allocation interface */
12356
c906108c 12357static struct dwarf_block *
7b5a2f43 12358dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
12359{
12360 struct dwarf_block *blk;
12361
12362 blk = (struct dwarf_block *)
7b5a2f43 12363 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
12364 return (blk);
12365}
12366
12367static struct abbrev_info *
f3dd6933 12368dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
12369{
12370 struct abbrev_info *abbrev;
12371
f3dd6933
DJ
12372 abbrev = (struct abbrev_info *)
12373 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
12374 memset (abbrev, 0, sizeof (struct abbrev_info));
12375 return (abbrev);
12376}
12377
12378static struct die_info *
b60c80d6 12379dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
12380{
12381 struct die_info *die;
b60c80d6
DJ
12382 size_t size = sizeof (struct die_info);
12383
12384 if (num_attrs > 1)
12385 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 12386
b60c80d6 12387 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
12388 memset (die, 0, sizeof (struct die_info));
12389 return (die);
12390}
2e276125
JB
12391
12392\f
12393/* Macro support. */
12394
12395
12396/* Return the full name of file number I in *LH's file name table.
12397 Use COMP_DIR as the name of the current directory of the
12398 compilation. The result is allocated using xmalloc; the caller is
12399 responsible for freeing it. */
12400static char *
12401file_full_name (int file, struct line_header *lh, const char *comp_dir)
12402{
6a83a1e6
EZ
12403 /* Is the file number a valid index into the line header's file name
12404 table? Remember that file numbers start with one, not zero. */
12405 if (1 <= file && file <= lh->num_file_names)
12406 {
12407 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 12408
6a83a1e6
EZ
12409 if (IS_ABSOLUTE_PATH (fe->name))
12410 return xstrdup (fe->name);
12411 else
12412 {
12413 const char *dir;
12414 int dir_len;
12415 char *full_name;
12416
12417 if (fe->dir_index)
12418 dir = lh->include_dirs[fe->dir_index - 1];
12419 else
12420 dir = comp_dir;
12421
12422 if (dir)
12423 {
12424 dir_len = strlen (dir);
12425 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
12426 strcpy (full_name, dir);
12427 full_name[dir_len] = '/';
12428 strcpy (full_name + dir_len + 1, fe->name);
12429 return full_name;
12430 }
12431 else
12432 return xstrdup (fe->name);
12433 }
12434 }
2e276125
JB
12435 else
12436 {
6a83a1e6
EZ
12437 /* The compiler produced a bogus file number. We can at least
12438 record the macro definitions made in the file, even if we
12439 won't be able to find the file by name. */
12440 char fake_name[80];
9a619af0 12441
6a83a1e6 12442 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 12443
6e70227d 12444 complaint (&symfile_complaints,
6a83a1e6
EZ
12445 _("bad file number in macro information (%d)"),
12446 file);
2e276125 12447
6a83a1e6 12448 return xstrdup (fake_name);
2e276125
JB
12449 }
12450}
12451
12452
12453static struct macro_source_file *
12454macro_start_file (int file, int line,
12455 struct macro_source_file *current_file,
12456 const char *comp_dir,
12457 struct line_header *lh, struct objfile *objfile)
12458{
12459 /* The full name of this source file. */
12460 char *full_name = file_full_name (file, lh, comp_dir);
12461
12462 /* We don't create a macro table for this compilation unit
12463 at all until we actually get a filename. */
12464 if (! pending_macros)
4a146b47 12465 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 12466 objfile->macro_cache);
2e276125
JB
12467
12468 if (! current_file)
12469 /* If we have no current file, then this must be the start_file
12470 directive for the compilation unit's main source file. */
12471 current_file = macro_set_main (pending_macros, full_name);
12472 else
12473 current_file = macro_include (current_file, line, full_name);
12474
12475 xfree (full_name);
6e70227d 12476
2e276125
JB
12477 return current_file;
12478}
12479
12480
12481/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
12482 followed by a null byte. */
12483static char *
12484copy_string (const char *buf, int len)
12485{
12486 char *s = xmalloc (len + 1);
9a619af0 12487
2e276125
JB
12488 memcpy (s, buf, len);
12489 s[len] = '\0';
2e276125
JB
12490 return s;
12491}
12492
12493
12494static const char *
12495consume_improper_spaces (const char *p, const char *body)
12496{
12497 if (*p == ' ')
12498 {
4d3c2250 12499 complaint (&symfile_complaints,
e2e0b3e5 12500 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 12501 body);
2e276125
JB
12502
12503 while (*p == ' ')
12504 p++;
12505 }
12506
12507 return p;
12508}
12509
12510
12511static void
12512parse_macro_definition (struct macro_source_file *file, int line,
12513 const char *body)
12514{
12515 const char *p;
12516
12517 /* The body string takes one of two forms. For object-like macro
12518 definitions, it should be:
12519
12520 <macro name> " " <definition>
12521
12522 For function-like macro definitions, it should be:
12523
12524 <macro name> "() " <definition>
12525 or
12526 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
12527
12528 Spaces may appear only where explicitly indicated, and in the
12529 <definition>.
12530
12531 The Dwarf 2 spec says that an object-like macro's name is always
12532 followed by a space, but versions of GCC around March 2002 omit
6e70227d 12533 the space when the macro's definition is the empty string.
2e276125
JB
12534
12535 The Dwarf 2 spec says that there should be no spaces between the
12536 formal arguments in a function-like macro's formal argument list,
12537 but versions of GCC around March 2002 include spaces after the
12538 commas. */
12539
12540
12541 /* Find the extent of the macro name. The macro name is terminated
12542 by either a space or null character (for an object-like macro) or
12543 an opening paren (for a function-like macro). */
12544 for (p = body; *p; p++)
12545 if (*p == ' ' || *p == '(')
12546 break;
12547
12548 if (*p == ' ' || *p == '\0')
12549 {
12550 /* It's an object-like macro. */
12551 int name_len = p - body;
12552 char *name = copy_string (body, name_len);
12553 const char *replacement;
12554
12555 if (*p == ' ')
12556 replacement = body + name_len + 1;
12557 else
12558 {
4d3c2250 12559 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12560 replacement = body + name_len;
12561 }
6e70227d 12562
2e276125
JB
12563 macro_define_object (file, line, name, replacement);
12564
12565 xfree (name);
12566 }
12567 else if (*p == '(')
12568 {
12569 /* It's a function-like macro. */
12570 char *name = copy_string (body, p - body);
12571 int argc = 0;
12572 int argv_size = 1;
12573 char **argv = xmalloc (argv_size * sizeof (*argv));
12574
12575 p++;
12576
12577 p = consume_improper_spaces (p, body);
12578
12579 /* Parse the formal argument list. */
12580 while (*p && *p != ')')
12581 {
12582 /* Find the extent of the current argument name. */
12583 const char *arg_start = p;
12584
12585 while (*p && *p != ',' && *p != ')' && *p != ' ')
12586 p++;
12587
12588 if (! *p || p == arg_start)
4d3c2250 12589 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12590 else
12591 {
12592 /* Make sure argv has room for the new argument. */
12593 if (argc >= argv_size)
12594 {
12595 argv_size *= 2;
12596 argv = xrealloc (argv, argv_size * sizeof (*argv));
12597 }
12598
12599 argv[argc++] = copy_string (arg_start, p - arg_start);
12600 }
12601
12602 p = consume_improper_spaces (p, body);
12603
12604 /* Consume the comma, if present. */
12605 if (*p == ',')
12606 {
12607 p++;
12608
12609 p = consume_improper_spaces (p, body);
12610 }
12611 }
12612
12613 if (*p == ')')
12614 {
12615 p++;
12616
12617 if (*p == ' ')
12618 /* Perfectly formed definition, no complaints. */
12619 macro_define_function (file, line, name,
6e70227d 12620 argc, (const char **) argv,
2e276125
JB
12621 p + 1);
12622 else if (*p == '\0')
12623 {
12624 /* Complain, but do define it. */
4d3c2250 12625 dwarf2_macro_malformed_definition_complaint (body);
2e276125 12626 macro_define_function (file, line, name,
6e70227d 12627 argc, (const char **) argv,
2e276125
JB
12628 p);
12629 }
12630 else
12631 /* Just complain. */
4d3c2250 12632 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12633 }
12634 else
12635 /* Just complain. */
4d3c2250 12636 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12637
12638 xfree (name);
12639 {
12640 int i;
12641
12642 for (i = 0; i < argc; i++)
12643 xfree (argv[i]);
12644 }
12645 xfree (argv);
12646 }
12647 else
4d3c2250 12648 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12649}
12650
12651
12652static void
12653dwarf_decode_macros (struct line_header *lh, unsigned int offset,
12654 char *comp_dir, bfd *abfd,
e7c27a73 12655 struct dwarf2_cu *cu)
2e276125 12656{
fe1b8b76 12657 gdb_byte *mac_ptr, *mac_end;
2e276125 12658 struct macro_source_file *current_file = 0;
757a13d0
JK
12659 enum dwarf_macinfo_record_type macinfo_type;
12660 int at_commandline;
2e276125 12661
be391dca
TT
12662 dwarf2_read_section (dwarf2_per_objfile->objfile,
12663 &dwarf2_per_objfile->macinfo);
dce234bc 12664 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 12665 {
e2e0b3e5 12666 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
12667 return;
12668 }
12669
757a13d0
JK
12670 /* First pass: Find the name of the base filename.
12671 This filename is needed in order to process all macros whose definition
12672 (or undefinition) comes from the command line. These macros are defined
12673 before the first DW_MACINFO_start_file entry, and yet still need to be
12674 associated to the base file.
12675
12676 To determine the base file name, we scan the macro definitions until we
12677 reach the first DW_MACINFO_start_file entry. We then initialize
12678 CURRENT_FILE accordingly so that any macro definition found before the
12679 first DW_MACINFO_start_file can still be associated to the base file. */
12680
dce234bc
PP
12681 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12682 mac_end = dwarf2_per_objfile->macinfo.buffer
12683 + dwarf2_per_objfile->macinfo.size;
2e276125 12684
757a13d0 12685 do
2e276125 12686 {
2e276125
JB
12687 /* Do we at least have room for a macinfo type byte? */
12688 if (mac_ptr >= mac_end)
12689 {
757a13d0
JK
12690 /* Complaint is printed during the second pass as GDB will probably
12691 stop the first pass earlier upon finding DW_MACINFO_start_file. */
12692 break;
2e276125
JB
12693 }
12694
12695 macinfo_type = read_1_byte (abfd, mac_ptr);
12696 mac_ptr++;
12697
12698 switch (macinfo_type)
12699 {
12700 /* A zero macinfo type indicates the end of the macro
12701 information. */
12702 case 0:
757a13d0
JK
12703 break;
12704
12705 case DW_MACINFO_define:
12706 case DW_MACINFO_undef:
12707 /* Only skip the data by MAC_PTR. */
12708 {
12709 unsigned int bytes_read;
12710
12711 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12712 mac_ptr += bytes_read;
12713 read_string (abfd, mac_ptr, &bytes_read);
12714 mac_ptr += bytes_read;
12715 }
12716 break;
12717
12718 case DW_MACINFO_start_file:
12719 {
12720 unsigned int bytes_read;
12721 int line, file;
12722
12723 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12724 mac_ptr += bytes_read;
12725 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12726 mac_ptr += bytes_read;
12727
12728 current_file = macro_start_file (file, line, current_file, comp_dir,
12729 lh, cu->objfile);
12730 }
12731 break;
12732
12733 case DW_MACINFO_end_file:
12734 /* No data to skip by MAC_PTR. */
12735 break;
12736
12737 case DW_MACINFO_vendor_ext:
12738 /* Only skip the data by MAC_PTR. */
12739 {
12740 unsigned int bytes_read;
12741
12742 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12743 mac_ptr += bytes_read;
12744 read_string (abfd, mac_ptr, &bytes_read);
12745 mac_ptr += bytes_read;
12746 }
12747 break;
12748
12749 default:
12750 break;
12751 }
12752 } while (macinfo_type != 0 && current_file == NULL);
12753
12754 /* Second pass: Process all entries.
12755
12756 Use the AT_COMMAND_LINE flag to determine whether we are still processing
12757 command-line macro definitions/undefinitions. This flag is unset when we
12758 reach the first DW_MACINFO_start_file entry. */
12759
dce234bc 12760 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
12761
12762 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
12763 GDB is still reading the definitions from command line. First
12764 DW_MACINFO_start_file will need to be ignored as it was already executed
12765 to create CURRENT_FILE for the main source holding also the command line
12766 definitions. On first met DW_MACINFO_start_file this flag is reset to
12767 normally execute all the remaining DW_MACINFO_start_file macinfos. */
12768
12769 at_commandline = 1;
12770
12771 do
12772 {
12773 /* Do we at least have room for a macinfo type byte? */
12774 if (mac_ptr >= mac_end)
12775 {
12776 dwarf2_macros_too_long_complaint ();
12777 break;
12778 }
12779
12780 macinfo_type = read_1_byte (abfd, mac_ptr);
12781 mac_ptr++;
12782
12783 switch (macinfo_type)
12784 {
12785 /* A zero macinfo type indicates the end of the macro
12786 information. */
12787 case 0:
12788 break;
2e276125
JB
12789
12790 case DW_MACINFO_define:
12791 case DW_MACINFO_undef:
12792 {
891d2f0b 12793 unsigned int bytes_read;
2e276125
JB
12794 int line;
12795 char *body;
12796
12797 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12798 mac_ptr += bytes_read;
12799 body = read_string (abfd, mac_ptr, &bytes_read);
12800 mac_ptr += bytes_read;
12801
12802 if (! current_file)
757a13d0
JK
12803 {
12804 /* DWARF violation as no main source is present. */
12805 complaint (&symfile_complaints,
12806 _("debug info with no main source gives macro %s "
12807 "on line %d: %s"),
6e70227d
DE
12808 macinfo_type == DW_MACINFO_define ?
12809 _("definition") :
905e0470
PM
12810 macinfo_type == DW_MACINFO_undef ?
12811 _("undefinition") :
12812 _("something-or-other"), line, body);
757a13d0
JK
12813 break;
12814 }
12815 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 12816 complaint (&symfile_complaints,
757a13d0
JK
12817 _("debug info gives %s macro %s with %s line %d: %s"),
12818 at_commandline ? _("command-line") : _("in-file"),
905e0470 12819 macinfo_type == DW_MACINFO_define ?
6e70227d 12820 _("definition") :
905e0470
PM
12821 macinfo_type == DW_MACINFO_undef ?
12822 _("undefinition") :
12823 _("something-or-other"),
757a13d0
JK
12824 line == 0 ? _("zero") : _("non-zero"), line, body);
12825
12826 if (macinfo_type == DW_MACINFO_define)
12827 parse_macro_definition (current_file, line, body);
12828 else if (macinfo_type == DW_MACINFO_undef)
12829 macro_undef (current_file, line, body);
2e276125
JB
12830 }
12831 break;
12832
12833 case DW_MACINFO_start_file:
12834 {
891d2f0b 12835 unsigned int bytes_read;
2e276125
JB
12836 int line, file;
12837
12838 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12839 mac_ptr += bytes_read;
12840 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12841 mac_ptr += bytes_read;
12842
757a13d0
JK
12843 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
12844 complaint (&symfile_complaints,
12845 _("debug info gives source %d included "
12846 "from %s at %s line %d"),
12847 file, at_commandline ? _("command-line") : _("file"),
12848 line == 0 ? _("zero") : _("non-zero"), line);
12849
12850 if (at_commandline)
12851 {
12852 /* This DW_MACINFO_start_file was executed in the pass one. */
12853 at_commandline = 0;
12854 }
12855 else
12856 current_file = macro_start_file (file, line,
12857 current_file, comp_dir,
12858 lh, cu->objfile);
2e276125
JB
12859 }
12860 break;
12861
12862 case DW_MACINFO_end_file:
12863 if (! current_file)
4d3c2250 12864 complaint (&symfile_complaints,
e2e0b3e5 12865 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
12866 else
12867 {
12868 current_file = current_file->included_by;
12869 if (! current_file)
12870 {
12871 enum dwarf_macinfo_record_type next_type;
12872
12873 /* GCC circa March 2002 doesn't produce the zero
12874 type byte marking the end of the compilation
12875 unit. Complain if it's not there, but exit no
12876 matter what. */
12877
12878 /* Do we at least have room for a macinfo type byte? */
12879 if (mac_ptr >= mac_end)
12880 {
4d3c2250 12881 dwarf2_macros_too_long_complaint ();
2e276125
JB
12882 return;
12883 }
12884
12885 /* We don't increment mac_ptr here, so this is just
12886 a look-ahead. */
12887 next_type = read_1_byte (abfd, mac_ptr);
12888 if (next_type != 0)
4d3c2250 12889 complaint (&symfile_complaints,
e2e0b3e5 12890 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
12891
12892 return;
12893 }
12894 }
12895 break;
12896
12897 case DW_MACINFO_vendor_ext:
12898 {
891d2f0b 12899 unsigned int bytes_read;
2e276125
JB
12900 int constant;
12901 char *string;
12902
12903 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12904 mac_ptr += bytes_read;
12905 string = read_string (abfd, mac_ptr, &bytes_read);
12906 mac_ptr += bytes_read;
12907
12908 /* We don't recognize any vendor extensions. */
12909 }
12910 break;
12911 }
757a13d0 12912 } while (macinfo_type != 0);
2e276125 12913}
8e19ed76
PS
12914
12915/* Check if the attribute's form is a DW_FORM_block*
12916 if so return true else false. */
12917static int
12918attr_form_is_block (struct attribute *attr)
12919{
12920 return (attr == NULL ? 0 :
12921 attr->form == DW_FORM_block1
12922 || attr->form == DW_FORM_block2
12923 || attr->form == DW_FORM_block4
2dc7f7b3
TT
12924 || attr->form == DW_FORM_block
12925 || attr->form == DW_FORM_exprloc);
8e19ed76 12926}
4c2df51b 12927
c6a0999f
JB
12928/* Return non-zero if ATTR's value is a section offset --- classes
12929 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
12930 You may use DW_UNSND (attr) to retrieve such offsets.
12931
12932 Section 7.5.4, "Attribute Encodings", explains that no attribute
12933 may have a value that belongs to more than one of these classes; it
12934 would be ambiguous if we did, because we use the same forms for all
12935 of them. */
3690dd37
JB
12936static int
12937attr_form_is_section_offset (struct attribute *attr)
12938{
12939 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
12940 || attr->form == DW_FORM_data8
12941 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
12942}
12943
12944
12945/* Return non-zero if ATTR's value falls in the 'constant' class, or
12946 zero otherwise. When this function returns true, you can apply
12947 dwarf2_get_attr_constant_value to it.
12948
12949 However, note that for some attributes you must check
12950 attr_form_is_section_offset before using this test. DW_FORM_data4
12951 and DW_FORM_data8 are members of both the constant class, and of
12952 the classes that contain offsets into other debug sections
12953 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
12954 that, if an attribute's can be either a constant or one of the
12955 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
12956 taken as section offsets, not constants. */
12957static int
12958attr_form_is_constant (struct attribute *attr)
12959{
12960 switch (attr->form)
12961 {
12962 case DW_FORM_sdata:
12963 case DW_FORM_udata:
12964 case DW_FORM_data1:
12965 case DW_FORM_data2:
12966 case DW_FORM_data4:
12967 case DW_FORM_data8:
12968 return 1;
12969 default:
12970 return 0;
12971 }
12972}
12973
4c2df51b
DJ
12974static void
12975dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 12976 struct dwarf2_cu *cu)
4c2df51b 12977{
3690dd37 12978 if (attr_form_is_section_offset (attr)
99bcc461
DJ
12979 /* ".debug_loc" may not exist at all, or the offset may be outside
12980 the section. If so, fall through to the complaint in the
12981 other branch. */
dce234bc 12982 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 12983 {
0d53c4c4 12984 struct dwarf2_loclist_baton *baton;
4c2df51b 12985
4a146b47 12986 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 12987 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
12988 baton->per_cu = cu->per_cu;
12989 gdb_assert (baton->per_cu);
4c2df51b 12990
be391dca
TT
12991 dwarf2_read_section (dwarf2_per_objfile->objfile,
12992 &dwarf2_per_objfile->loc);
12993
0d53c4c4
DJ
12994 /* We don't know how long the location list is, but make sure we
12995 don't run off the edge of the section. */
dce234bc
PP
12996 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
12997 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
12998 baton->base_address = cu->base_address;
12999 if (cu->base_known == 0)
0d53c4c4 13000 complaint (&symfile_complaints,
e2e0b3e5 13001 _("Location list used without specifying the CU base address."));
4c2df51b 13002
768a979c 13003 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13004 SYMBOL_LOCATION_BATON (sym) = baton;
13005 }
13006 else
13007 {
13008 struct dwarf2_locexpr_baton *baton;
13009
4a146b47 13010 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13011 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13012 baton->per_cu = cu->per_cu;
13013 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13014
13015 if (attr_form_is_block (attr))
13016 {
13017 /* Note that we're just copying the block's data pointer
13018 here, not the actual data. We're still pointing into the
6502dd73
DJ
13019 info_buffer for SYM's objfile; right now we never release
13020 that buffer, but when we do clean up properly this may
13021 need to change. */
0d53c4c4
DJ
13022 baton->size = DW_BLOCK (attr)->size;
13023 baton->data = DW_BLOCK (attr)->data;
13024 }
13025 else
13026 {
13027 dwarf2_invalid_attrib_class_complaint ("location description",
13028 SYMBOL_NATURAL_NAME (sym));
13029 baton->size = 0;
13030 baton->data = NULL;
13031 }
6e70227d 13032
768a979c 13033 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13034 SYMBOL_LOCATION_BATON (sym) = baton;
13035 }
4c2df51b 13036}
6502dd73 13037
9aa1f1e3
TT
13038/* Return the OBJFILE associated with the compilation unit CU. If CU
13039 came from a separate debuginfo file, then the master objfile is
13040 returned. */
ae0d2f24
UW
13041
13042struct objfile *
13043dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13044{
9291a0cd 13045 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13046
13047 /* Return the master objfile, so that we can report and look up the
13048 correct file containing this variable. */
13049 if (objfile->separate_debug_objfile_backlink)
13050 objfile = objfile->separate_debug_objfile_backlink;
13051
13052 return objfile;
13053}
13054
13055/* Return the address size given in the compilation unit header for CU. */
13056
13057CORE_ADDR
13058dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13059{
13060 if (per_cu->cu)
13061 return per_cu->cu->header.addr_size;
13062 else
13063 {
13064 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13065 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13066 struct dwarf2_per_objfile *per_objfile
13067 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13068 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13069 struct comp_unit_head cu_header;
9a619af0 13070
ae0d2f24
UW
13071 memset (&cu_header, 0, sizeof cu_header);
13072 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13073 return cu_header.addr_size;
13074 }
13075}
13076
9eae7c52
TT
13077/* Return the offset size given in the compilation unit header for CU. */
13078
13079int
13080dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13081{
13082 if (per_cu->cu)
13083 return per_cu->cu->header.offset_size;
13084 else
13085 {
13086 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13087 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
13088 struct dwarf2_per_objfile *per_objfile
13089 = objfile_data (objfile, dwarf2_objfile_data_key);
13090 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13091 struct comp_unit_head cu_header;
13092
13093 memset (&cu_header, 0, sizeof cu_header);
13094 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13095 return cu_header.offset_size;
13096 }
13097}
13098
9aa1f1e3
TT
13099/* Return the text offset of the CU. The returned offset comes from
13100 this CU's objfile. If this objfile came from a separate debuginfo
13101 file, then the offset may be different from the corresponding
13102 offset in the parent objfile. */
13103
13104CORE_ADDR
13105dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13106{
bb3fa9d0 13107 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
13108
13109 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13110}
13111
348e048f
DE
13112/* Locate the .debug_info compilation unit from CU's objfile which contains
13113 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
13114
13115static struct dwarf2_per_cu_data *
c764a876 13116dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
13117 struct objfile *objfile)
13118{
13119 struct dwarf2_per_cu_data *this_cu;
13120 int low, high;
13121
ae038cb0
DJ
13122 low = 0;
13123 high = dwarf2_per_objfile->n_comp_units - 1;
13124 while (high > low)
13125 {
13126 int mid = low + (high - low) / 2;
9a619af0 13127
ae038cb0
DJ
13128 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13129 high = mid;
13130 else
13131 low = mid + 1;
13132 }
13133 gdb_assert (low == high);
13134 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13135 {
10b3939b 13136 if (low == 0)
8a3fe4f8
AC
13137 error (_("Dwarf Error: could not find partial DIE containing "
13138 "offset 0x%lx [in module %s]"),
10b3939b
DJ
13139 (long) offset, bfd_get_filename (objfile->obfd));
13140
ae038cb0
DJ
13141 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13142 return dwarf2_per_objfile->all_comp_units[low-1];
13143 }
13144 else
13145 {
13146 this_cu = dwarf2_per_objfile->all_comp_units[low];
13147 if (low == dwarf2_per_objfile->n_comp_units - 1
13148 && offset >= this_cu->offset + this_cu->length)
c764a876 13149 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
13150 gdb_assert (offset < this_cu->offset + this_cu->length);
13151 return this_cu;
13152 }
13153}
13154
10b3939b
DJ
13155/* Locate the compilation unit from OBJFILE which is located at exactly
13156 OFFSET. Raises an error on failure. */
13157
ae038cb0 13158static struct dwarf2_per_cu_data *
c764a876 13159dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
13160{
13161 struct dwarf2_per_cu_data *this_cu;
9a619af0 13162
ae038cb0
DJ
13163 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13164 if (this_cu->offset != offset)
c764a876 13165 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
13166 return this_cu;
13167}
13168
93311388
DE
13169/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
13170
13171static struct dwarf2_cu *
13172alloc_one_comp_unit (struct objfile *objfile)
13173{
13174 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13175 cu->objfile = objfile;
13176 obstack_init (&cu->comp_unit_obstack);
13177 return cu;
13178}
13179
ae038cb0
DJ
13180/* Release one cached compilation unit, CU. We unlink it from the tree
13181 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
13182 the caller is responsible for that.
13183 NOTE: DATA is a void * because this function is also used as a
13184 cleanup routine. */
ae038cb0
DJ
13185
13186static void
13187free_one_comp_unit (void *data)
13188{
13189 struct dwarf2_cu *cu = data;
13190
13191 if (cu->per_cu != NULL)
13192 cu->per_cu->cu = NULL;
13193 cu->per_cu = NULL;
13194
13195 obstack_free (&cu->comp_unit_obstack, NULL);
13196
13197 xfree (cu);
13198}
13199
72bf9492 13200/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
13201 when we're finished with it. We can't free the pointer itself, but be
13202 sure to unlink it from the cache. Also release any associated storage
13203 and perform cache maintenance.
72bf9492
DJ
13204
13205 Only used during partial symbol parsing. */
13206
13207static void
13208free_stack_comp_unit (void *data)
13209{
13210 struct dwarf2_cu *cu = data;
13211
13212 obstack_free (&cu->comp_unit_obstack, NULL);
13213 cu->partial_dies = NULL;
ae038cb0
DJ
13214
13215 if (cu->per_cu != NULL)
13216 {
13217 /* This compilation unit is on the stack in our caller, so we
13218 should not xfree it. Just unlink it. */
13219 cu->per_cu->cu = NULL;
13220 cu->per_cu = NULL;
13221
13222 /* If we had a per-cu pointer, then we may have other compilation
13223 units loaded, so age them now. */
13224 age_cached_comp_units ();
13225 }
13226}
13227
13228/* Free all cached compilation units. */
13229
13230static void
13231free_cached_comp_units (void *data)
13232{
13233 struct dwarf2_per_cu_data *per_cu, **last_chain;
13234
13235 per_cu = dwarf2_per_objfile->read_in_chain;
13236 last_chain = &dwarf2_per_objfile->read_in_chain;
13237 while (per_cu != NULL)
13238 {
13239 struct dwarf2_per_cu_data *next_cu;
13240
13241 next_cu = per_cu->cu->read_in_chain;
13242
13243 free_one_comp_unit (per_cu->cu);
13244 *last_chain = next_cu;
13245
13246 per_cu = next_cu;
13247 }
13248}
13249
13250/* Increase the age counter on each cached compilation unit, and free
13251 any that are too old. */
13252
13253static void
13254age_cached_comp_units (void)
13255{
13256 struct dwarf2_per_cu_data *per_cu, **last_chain;
13257
13258 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13259 per_cu = dwarf2_per_objfile->read_in_chain;
13260 while (per_cu != NULL)
13261 {
13262 per_cu->cu->last_used ++;
13263 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13264 dwarf2_mark (per_cu->cu);
13265 per_cu = per_cu->cu->read_in_chain;
13266 }
13267
13268 per_cu = dwarf2_per_objfile->read_in_chain;
13269 last_chain = &dwarf2_per_objfile->read_in_chain;
13270 while (per_cu != NULL)
13271 {
13272 struct dwarf2_per_cu_data *next_cu;
13273
13274 next_cu = per_cu->cu->read_in_chain;
13275
13276 if (!per_cu->cu->mark)
13277 {
13278 free_one_comp_unit (per_cu->cu);
13279 *last_chain = next_cu;
13280 }
13281 else
13282 last_chain = &per_cu->cu->read_in_chain;
13283
13284 per_cu = next_cu;
13285 }
13286}
13287
13288/* Remove a single compilation unit from the cache. */
13289
13290static void
13291free_one_cached_comp_unit (void *target_cu)
13292{
13293 struct dwarf2_per_cu_data *per_cu, **last_chain;
13294
13295 per_cu = dwarf2_per_objfile->read_in_chain;
13296 last_chain = &dwarf2_per_objfile->read_in_chain;
13297 while (per_cu != NULL)
13298 {
13299 struct dwarf2_per_cu_data *next_cu;
13300
13301 next_cu = per_cu->cu->read_in_chain;
13302
13303 if (per_cu->cu == target_cu)
13304 {
13305 free_one_comp_unit (per_cu->cu);
13306 *last_chain = next_cu;
13307 break;
13308 }
13309 else
13310 last_chain = &per_cu->cu->read_in_chain;
13311
13312 per_cu = next_cu;
13313 }
13314}
13315
fe3e1990
DJ
13316/* Release all extra memory associated with OBJFILE. */
13317
13318void
13319dwarf2_free_objfile (struct objfile *objfile)
13320{
13321 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
13322
13323 if (dwarf2_per_objfile == NULL)
13324 return;
13325
13326 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
13327 free_cached_comp_units (NULL);
13328
9291a0cd
TT
13329 if (dwarf2_per_objfile->using_index)
13330 {
13331 int i;
13332
13333 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
13334 {
13335 int j;
13336 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
13337
13338 if (!cu->v.quick->lines)
13339 continue;
13340
13341 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
13342 {
13343 if (cu->v.quick->file_names)
13344 xfree ((void *) cu->v.quick->file_names[j]);
13345 if (cu->v.quick->full_names)
13346 xfree ((void *) cu->v.quick->full_names[j]);
13347 }
13348
13349 free_line_header (cu->v.quick->lines);
13350 }
13351 }
13352
fe3e1990
DJ
13353 /* Everything else should be on the objfile obstack. */
13354}
13355
1c379e20
DJ
13356/* A pair of DIE offset and GDB type pointer. We store these
13357 in a hash table separate from the DIEs, and preserve them
13358 when the DIEs are flushed out of cache. */
13359
13360struct dwarf2_offset_and_type
13361{
13362 unsigned int offset;
13363 struct type *type;
13364};
13365
13366/* Hash function for a dwarf2_offset_and_type. */
13367
13368static hashval_t
13369offset_and_type_hash (const void *item)
13370{
13371 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 13372
1c379e20
DJ
13373 return ofs->offset;
13374}
13375
13376/* Equality function for a dwarf2_offset_and_type. */
13377
13378static int
13379offset_and_type_eq (const void *item_lhs, const void *item_rhs)
13380{
13381 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
13382 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 13383
1c379e20
DJ
13384 return ofs_lhs->offset == ofs_rhs->offset;
13385}
13386
13387/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
13388 table if necessary. For convenience, return TYPE.
13389
13390 The DIEs reading must have careful ordering to:
13391 * Not cause infite loops trying to read in DIEs as a prerequisite for
13392 reading current DIE.
13393 * Not trying to dereference contents of still incompletely read in types
13394 while reading in other DIEs.
13395 * Enable referencing still incompletely read in types just by a pointer to
13396 the type without accessing its fields.
13397
13398 Therefore caller should follow these rules:
13399 * Try to fetch any prerequisite types we may need to build this DIE type
13400 before building the type and calling set_die_type.
13401 * After building typer call set_die_type for current DIE as soon as
13402 possible before fetching more types to complete the current type.
13403 * Make the type as complete as possible before fetching more types. */
1c379e20 13404
f792889a 13405static struct type *
1c379e20
DJ
13406set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13407{
13408 struct dwarf2_offset_and_type **slot, ofs;
13409
b4ba55a1
JB
13410 /* For Ada types, make sure that the gnat-specific data is always
13411 initialized (if not already set). There are a few types where
13412 we should not be doing so, because the type-specific area is
13413 already used to hold some other piece of info (eg: TYPE_CODE_FLT
13414 where the type-specific area is used to store the floatformat).
13415 But this is not a problem, because the gnat-specific information
13416 is actually not needed for these types. */
13417 if (need_gnat_info (cu)
13418 && TYPE_CODE (type) != TYPE_CODE_FUNC
13419 && TYPE_CODE (type) != TYPE_CODE_FLT
13420 && !HAVE_GNAT_AUX_INFO (type))
13421 INIT_GNAT_SPECIFIC (type);
13422
f792889a
DJ
13423 if (cu->type_hash == NULL)
13424 {
13425 gdb_assert (cu->per_cu != NULL);
13426 cu->per_cu->type_hash
13427 = htab_create_alloc_ex (cu->header.length / 24,
13428 offset_and_type_hash,
13429 offset_and_type_eq,
13430 NULL,
13431 &cu->objfile->objfile_obstack,
13432 hashtab_obstack_allocate,
13433 dummy_obstack_deallocate);
13434 cu->type_hash = cu->per_cu->type_hash;
13435 }
1c379e20
DJ
13436
13437 ofs.offset = die->offset;
13438 ofs.type = type;
13439 slot = (struct dwarf2_offset_and_type **)
f792889a 13440 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
7e314c57
JK
13441 if (*slot)
13442 complaint (&symfile_complaints,
13443 _("A problem internal to GDB: DIE 0x%x has type already set"),
13444 die->offset);
1c379e20
DJ
13445 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
13446 **slot = ofs;
f792889a 13447 return type;
1c379e20
DJ
13448}
13449
f792889a
DJ
13450/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
13451 not have a saved type. */
1c379e20
DJ
13452
13453static struct type *
f792889a 13454get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
13455{
13456 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
13457 htab_t type_hash = cu->type_hash;
13458
13459 if (type_hash == NULL)
13460 return NULL;
1c379e20
DJ
13461
13462 ofs.offset = die->offset;
13463 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
13464 if (slot)
13465 return slot->type;
13466 else
13467 return NULL;
13468}
13469
10b3939b
DJ
13470/* Add a dependence relationship from CU to REF_PER_CU. */
13471
13472static void
13473dwarf2_add_dependence (struct dwarf2_cu *cu,
13474 struct dwarf2_per_cu_data *ref_per_cu)
13475{
13476 void **slot;
13477
13478 if (cu->dependencies == NULL)
13479 cu->dependencies
13480 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
13481 NULL, &cu->comp_unit_obstack,
13482 hashtab_obstack_allocate,
13483 dummy_obstack_deallocate);
13484
13485 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
13486 if (*slot == NULL)
13487 *slot = ref_per_cu;
13488}
1c379e20 13489
f504f079
DE
13490/* Subroutine of dwarf2_mark to pass to htab_traverse.
13491 Set the mark field in every compilation unit in the
ae038cb0
DJ
13492 cache that we must keep because we are keeping CU. */
13493
10b3939b
DJ
13494static int
13495dwarf2_mark_helper (void **slot, void *data)
13496{
13497 struct dwarf2_per_cu_data *per_cu;
13498
13499 per_cu = (struct dwarf2_per_cu_data *) *slot;
13500 if (per_cu->cu->mark)
13501 return 1;
13502 per_cu->cu->mark = 1;
13503
13504 if (per_cu->cu->dependencies != NULL)
13505 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
13506
13507 return 1;
13508}
13509
f504f079
DE
13510/* Set the mark field in CU and in every other compilation unit in the
13511 cache that we must keep because we are keeping CU. */
13512
ae038cb0
DJ
13513static void
13514dwarf2_mark (struct dwarf2_cu *cu)
13515{
13516 if (cu->mark)
13517 return;
13518 cu->mark = 1;
10b3939b
DJ
13519 if (cu->dependencies != NULL)
13520 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
13521}
13522
13523static void
13524dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
13525{
13526 while (per_cu)
13527 {
13528 per_cu->cu->mark = 0;
13529 per_cu = per_cu->cu->read_in_chain;
13530 }
72bf9492
DJ
13531}
13532
72bf9492
DJ
13533/* Trivial hash function for partial_die_info: the hash value of a DIE
13534 is its offset in .debug_info for this objfile. */
13535
13536static hashval_t
13537partial_die_hash (const void *item)
13538{
13539 const struct partial_die_info *part_die = item;
9a619af0 13540
72bf9492
DJ
13541 return part_die->offset;
13542}
13543
13544/* Trivial comparison function for partial_die_info structures: two DIEs
13545 are equal if they have the same offset. */
13546
13547static int
13548partial_die_eq (const void *item_lhs, const void *item_rhs)
13549{
13550 const struct partial_die_info *part_die_lhs = item_lhs;
13551 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 13552
72bf9492
DJ
13553 return part_die_lhs->offset == part_die_rhs->offset;
13554}
13555
ae038cb0
DJ
13556static struct cmd_list_element *set_dwarf2_cmdlist;
13557static struct cmd_list_element *show_dwarf2_cmdlist;
13558
13559static void
13560set_dwarf2_cmd (char *args, int from_tty)
13561{
13562 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
13563}
13564
13565static void
13566show_dwarf2_cmd (char *args, int from_tty)
6e70227d 13567{
ae038cb0
DJ
13568 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
13569}
13570
dce234bc
PP
13571/* If section described by INFO was mmapped, munmap it now. */
13572
13573static void
13574munmap_section_buffer (struct dwarf2_section_info *info)
13575{
13576 if (info->was_mmapped)
13577 {
13578#ifdef HAVE_MMAP
13579 intptr_t begin = (intptr_t) info->buffer;
13580 intptr_t map_begin = begin & ~(pagesize - 1);
13581 size_t map_length = info->size + begin - map_begin;
9a619af0 13582
dce234bc
PP
13583 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
13584#else
13585 /* Without HAVE_MMAP, we should never be here to begin with. */
13586 gdb_assert (0);
13587#endif
13588 }
13589}
13590
13591/* munmap debug sections for OBJFILE, if necessary. */
13592
13593static void
c1bd65d0 13594dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
13595{
13596 struct dwarf2_per_objfile *data = d;
9a619af0 13597
dce234bc
PP
13598 munmap_section_buffer (&data->info);
13599 munmap_section_buffer (&data->abbrev);
13600 munmap_section_buffer (&data->line);
13601 munmap_section_buffer (&data->str);
13602 munmap_section_buffer (&data->macinfo);
13603 munmap_section_buffer (&data->ranges);
13604 munmap_section_buffer (&data->loc);
13605 munmap_section_buffer (&data->frame);
13606 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
13607 munmap_section_buffer (&data->gdb_index);
13608}
13609
13610\f
13611
13612/* The contents of the hash table we create when building the string
13613 table. */
13614struct strtab_entry
13615{
13616 offset_type offset;
13617 const char *str;
13618};
13619
13620/* Hash function for a strtab_entry. */
13621static hashval_t
13622hash_strtab_entry (const void *e)
13623{
13624 const struct strtab_entry *entry = e;
13625 return mapped_index_string_hash (entry->str);
13626}
13627
13628/* Equality function for a strtab_entry. */
13629static int
13630eq_strtab_entry (const void *a, const void *b)
13631{
13632 const struct strtab_entry *ea = a;
13633 const struct strtab_entry *eb = b;
13634 return !strcmp (ea->str, eb->str);
13635}
13636
13637/* Create a strtab_entry hash table. */
13638static htab_t
13639create_strtab (void)
13640{
13641 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
13642 xfree, xcalloc, xfree);
13643}
13644
13645/* Add a string to the constant pool. Return the string's offset in
13646 host order. */
13647static offset_type
13648add_string (htab_t table, struct obstack *cpool, const char *str)
13649{
13650 void **slot;
13651 struct strtab_entry entry;
13652 struct strtab_entry *result;
13653
13654 entry.str = str;
13655 slot = htab_find_slot (table, &entry, INSERT);
13656 if (*slot)
13657 result = *slot;
13658 else
13659 {
13660 result = XNEW (struct strtab_entry);
13661 result->offset = obstack_object_size (cpool);
13662 result->str = str;
13663 obstack_grow_str0 (cpool, str);
13664 *slot = result;
13665 }
13666 return result->offset;
13667}
13668
13669/* An entry in the symbol table. */
13670struct symtab_index_entry
13671{
13672 /* The name of the symbol. */
13673 const char *name;
13674 /* The offset of the name in the constant pool. */
13675 offset_type index_offset;
13676 /* A sorted vector of the indices of all the CUs that hold an object
13677 of this name. */
13678 VEC (offset_type) *cu_indices;
13679};
13680
13681/* The symbol table. This is a power-of-2-sized hash table. */
13682struct mapped_symtab
13683{
13684 offset_type n_elements;
13685 offset_type size;
13686 struct symtab_index_entry **data;
13687};
13688
13689/* Hash function for a symtab_index_entry. */
13690static hashval_t
13691hash_symtab_entry (const void *e)
13692{
13693 const struct symtab_index_entry *entry = e;
13694 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
13695 sizeof (offset_type) * VEC_length (offset_type,
13696 entry->cu_indices),
13697 0);
13698}
13699
13700/* Equality function for a symtab_index_entry. */
13701static int
13702eq_symtab_entry (const void *a, const void *b)
13703{
13704 const struct symtab_index_entry *ea = a;
13705 const struct symtab_index_entry *eb = b;
13706 int len = VEC_length (offset_type, ea->cu_indices);
13707 if (len != VEC_length (offset_type, eb->cu_indices))
13708 return 0;
13709 return !memcmp (VEC_address (offset_type, ea->cu_indices),
13710 VEC_address (offset_type, eb->cu_indices),
13711 sizeof (offset_type) * len);
13712}
13713
13714/* Destroy a symtab_index_entry. */
13715static void
13716delete_symtab_entry (void *p)
13717{
13718 struct symtab_index_entry *entry = p;
13719 VEC_free (offset_type, entry->cu_indices);
13720 xfree (entry);
13721}
13722
13723/* Create a hash table holding symtab_index_entry objects. */
13724static htab_t
13725create_index_table (void)
13726{
13727 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
13728 delete_symtab_entry, xcalloc, xfree);
13729}
13730
13731/* Create a new mapped symtab object. */
13732static struct mapped_symtab *
13733create_mapped_symtab (void)
13734{
13735 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
13736 symtab->n_elements = 0;
13737 symtab->size = 1024;
13738 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13739 return symtab;
13740}
13741
13742/* Destroy a mapped_symtab. */
13743static void
13744cleanup_mapped_symtab (void *p)
13745{
13746 struct mapped_symtab *symtab = p;
13747 /* The contents of the array are freed when the other hash table is
13748 destroyed. */
13749 xfree (symtab->data);
13750 xfree (symtab);
13751}
13752
13753/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
13754 the slot. */
13755static struct symtab_index_entry **
13756find_slot (struct mapped_symtab *symtab, const char *name)
13757{
13758 offset_type index, step, hash = mapped_index_string_hash (name);
13759
13760 index = hash & (symtab->size - 1);
13761 step = ((hash * 17) & (symtab->size - 1)) | 1;
13762
13763 for (;;)
13764 {
13765 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
13766 return &symtab->data[index];
13767 index = (index + step) & (symtab->size - 1);
13768 }
13769}
13770
13771/* Expand SYMTAB's hash table. */
13772static void
13773hash_expand (struct mapped_symtab *symtab)
13774{
13775 offset_type old_size = symtab->size;
13776 offset_type i;
13777 struct symtab_index_entry **old_entries = symtab->data;
13778
13779 symtab->size *= 2;
13780 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13781
13782 for (i = 0; i < old_size; ++i)
13783 {
13784 if (old_entries[i])
13785 {
13786 struct symtab_index_entry **slot = find_slot (symtab,
13787 old_entries[i]->name);
13788 *slot = old_entries[i];
13789 }
13790 }
13791
13792 xfree (old_entries);
13793}
13794
13795/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
13796 is the index of the CU in which the symbol appears. */
13797static void
13798add_index_entry (struct mapped_symtab *symtab, const char *name,
13799 offset_type cu_index)
13800{
13801 struct symtab_index_entry **slot;
13802
13803 ++symtab->n_elements;
13804 if (4 * symtab->n_elements / 3 >= symtab->size)
13805 hash_expand (symtab);
13806
13807 slot = find_slot (symtab, name);
13808 if (!*slot)
13809 {
13810 *slot = XNEW (struct symtab_index_entry);
13811 (*slot)->name = name;
13812 (*slot)->cu_indices = NULL;
13813 }
13814 /* Don't push an index twice. Due to how we add entries we only
13815 have to check the last one. */
13816 if (VEC_empty (offset_type, (*slot)->cu_indices)
13817 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
13818 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
13819}
13820
13821/* Add a vector of indices to the constant pool. */
13822static offset_type
13823add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
13824 struct symtab_index_entry *entry)
13825{
13826 void **slot;
13827
13828 slot = htab_find_slot (index_table, entry, INSERT);
13829 if (!*slot)
13830 {
13831 offset_type len = VEC_length (offset_type, entry->cu_indices);
13832 offset_type val = MAYBE_SWAP (len);
13833 offset_type iter;
13834 int i;
13835
13836 *slot = entry;
13837 entry->index_offset = obstack_object_size (cpool);
13838
13839 obstack_grow (cpool, &val, sizeof (val));
13840 for (i = 0;
13841 VEC_iterate (offset_type, entry->cu_indices, i, iter);
13842 ++i)
13843 {
13844 val = MAYBE_SWAP (iter);
13845 obstack_grow (cpool, &val, sizeof (val));
13846 }
13847 }
13848 else
13849 {
13850 struct symtab_index_entry *old_entry = *slot;
13851 entry->index_offset = old_entry->index_offset;
13852 entry = old_entry;
13853 }
13854 return entry->index_offset;
13855}
13856
13857/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
13858 constant pool entries going into the obstack CPOOL. */
13859static void
13860write_hash_table (struct mapped_symtab *symtab,
13861 struct obstack *output, struct obstack *cpool)
13862{
13863 offset_type i;
13864 htab_t index_table;
13865 htab_t str_table;
13866
13867 index_table = create_index_table ();
13868 str_table = create_strtab ();
13869 /* We add all the index vectors to the constant pool first, to
13870 ensure alignment is ok. */
13871 for (i = 0; i < symtab->size; ++i)
13872 {
13873 if (symtab->data[i])
13874 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
13875 }
13876
13877 /* Now write out the hash table. */
13878 for (i = 0; i < symtab->size; ++i)
13879 {
13880 offset_type str_off, vec_off;
13881
13882 if (symtab->data[i])
13883 {
13884 str_off = add_string (str_table, cpool, symtab->data[i]->name);
13885 vec_off = symtab->data[i]->index_offset;
13886 }
13887 else
13888 {
13889 /* While 0 is a valid constant pool index, it is not valid
13890 to have 0 for both offsets. */
13891 str_off = 0;
13892 vec_off = 0;
13893 }
13894
13895 str_off = MAYBE_SWAP (str_off);
13896 vec_off = MAYBE_SWAP (vec_off);
13897
13898 obstack_grow (output, &str_off, sizeof (str_off));
13899 obstack_grow (output, &vec_off, sizeof (vec_off));
13900 }
13901
13902 htab_delete (str_table);
13903 htab_delete (index_table);
13904}
13905
13906/* Write an address entry to ADDR_OBSTACK. The addresses are taken
13907 from PST; CU_INDEX is the index of the CU in the vector of all
13908 CUs. */
13909static void
13910add_address_entry (struct objfile *objfile,
13911 struct obstack *addr_obstack, struct partial_symtab *pst,
13912 unsigned int cu_index)
13913{
13914 offset_type offset;
13915 char addr[8];
13916 CORE_ADDR baseaddr;
13917
13918 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13919
13920 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
13921 obstack_grow (addr_obstack, addr, 8);
13922 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
13923 obstack_grow (addr_obstack, addr, 8);
13924 offset = MAYBE_SWAP (cu_index);
13925 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
13926}
13927
13928/* Add a list of partial symbols to SYMTAB. */
13929static void
13930write_psymbols (struct mapped_symtab *symtab,
13931 struct partial_symbol **psymp,
13932 int count,
13933 offset_type cu_index)
13934{
13935 for (; count-- > 0; ++psymp)
13936 {
13937 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
13938 error (_("Ada is not currently supported by the index"));
13939 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
13940 }
13941}
13942
13943/* Write the contents of an ("unfinished") obstack to FILE. Throw an
13944 exception if there is an error. */
13945static void
13946write_obstack (FILE *file, struct obstack *obstack)
13947{
13948 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
13949 file)
13950 != obstack_object_size (obstack))
13951 error (_("couldn't data write to file"));
13952}
13953
13954/* Unlink a file if the argument is not NULL. */
13955static void
13956unlink_if_set (void *p)
13957{
13958 char **filename = p;
13959 if (*filename)
13960 unlink (*filename);
13961}
13962
13963/* Create an index file for OBJFILE in the directory DIR. */
13964static void
13965write_psymtabs_to_index (struct objfile *objfile, const char *dir)
13966{
13967 struct cleanup *cleanup;
13968 char *filename, *cleanup_filename;
13969 struct obstack contents, addr_obstack, constant_pool, symtab_obstack, cu_list;
13970 int i;
13971 FILE *out_file;
13972 struct mapped_symtab *symtab;
13973 offset_type val, size_of_contents, total_len;
13974 struct stat st;
13975 char buf[8];
13976
13977 if (!objfile->psymtabs)
13978 return;
13979 if (dwarf2_per_objfile->using_index)
13980 error (_("Cannot use an index to create the index"));
13981
13982 if (stat (objfile->name, &st) < 0)
13983 perror_with_name (_("Could not stat"));
13984
13985 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
13986 INDEX_SUFFIX, (char *) NULL);
13987 cleanup = make_cleanup (xfree, filename);
13988
13989 out_file = fopen (filename, "wb");
13990 if (!out_file)
13991 error (_("Can't open `%s' for writing"), filename);
13992
13993 cleanup_filename = filename;
13994 make_cleanup (unlink_if_set, &cleanup_filename);
13995
13996 symtab = create_mapped_symtab ();
13997 make_cleanup (cleanup_mapped_symtab, symtab);
13998
13999 obstack_init (&addr_obstack);
14000 make_cleanup_obstack_free (&addr_obstack);
14001
14002 obstack_init (&cu_list);
14003 make_cleanup_obstack_free (&cu_list);
14004
14005 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14006 {
14007 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14008 struct partial_symtab *psymtab = cu->v.psymtab;
14009 gdb_byte val[8];
14010
14011 write_psymbols (symtab,
14012 objfile->global_psymbols.list + psymtab->globals_offset,
14013 psymtab->n_global_syms, i);
14014 write_psymbols (symtab,
14015 objfile->static_psymbols.list + psymtab->statics_offset,
14016 psymtab->n_static_syms, i);
14017
14018 add_address_entry (objfile, &addr_obstack, psymtab, i);
14019
14020 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14021 obstack_grow (&cu_list, val, 8);
14022 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14023 obstack_grow (&cu_list, val, 8);
14024 }
14025
14026 obstack_init (&constant_pool);
14027 make_cleanup_obstack_free (&constant_pool);
14028 obstack_init (&symtab_obstack);
14029 make_cleanup_obstack_free (&symtab_obstack);
14030 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14031
14032 obstack_init (&contents);
14033 make_cleanup_obstack_free (&contents);
14034 size_of_contents = 5 * sizeof (offset_type);
14035 total_len = size_of_contents;
14036
14037 /* The version number. */
14038 val = MAYBE_SWAP (1);
14039 obstack_grow (&contents, &val, sizeof (val));
14040
14041 /* The offset of the CU list from the start of the file. */
14042 val = MAYBE_SWAP (total_len);
14043 obstack_grow (&contents, &val, sizeof (val));
14044 total_len += obstack_object_size (&cu_list);
14045
14046 /* The offset of the address table from the start of the file. */
14047 val = MAYBE_SWAP (total_len);
14048 obstack_grow (&contents, &val, sizeof (val));
14049 total_len += obstack_object_size (&addr_obstack);
14050
14051 /* The offset of the symbol table from the start of the file. */
14052 val = MAYBE_SWAP (total_len);
14053 obstack_grow (&contents, &val, sizeof (val));
14054 total_len += obstack_object_size (&symtab_obstack);
14055
14056 /* The offset of the constant pool from the start of the file. */
14057 val = MAYBE_SWAP (total_len);
14058 obstack_grow (&contents, &val, sizeof (val));
14059 total_len += obstack_object_size (&constant_pool);
14060
14061 gdb_assert (obstack_object_size (&contents) == size_of_contents);
14062
14063 write_obstack (out_file, &contents);
14064 write_obstack (out_file, &cu_list);
14065 write_obstack (out_file, &addr_obstack);
14066 write_obstack (out_file, &symtab_obstack);
14067 write_obstack (out_file, &constant_pool);
14068
14069 fclose (out_file);
14070
14071 /* We want to keep the file, so we set cleanup_filename to NULL
14072 here. See unlink_if_set. */
14073 cleanup_filename = NULL;
14074
14075 do_cleanups (cleanup);
14076}
14077
14078/* The mapped index file format is designed to be directly mmap()able
14079 on any architecture. In most cases, a datum is represented using a
14080 little-endian 32-bit integer value, called an offset_type. Big
14081 endian machines must byte-swap the values before using them.
14082 Exceptions to this rule are noted. The data is laid out such that
14083 alignment is always respected.
14084
14085 A mapped index consists of several sections.
14086
14087 1. The file header. This is a sequence of values, of offset_type
14088 unless otherwise noted:
14089 [0] The version number. Currently 1.
14090 [1] The offset, from the start of the file, of the CU list.
14091 [2] The offset, from the start of the file, of the address section.
14092 [3] The offset, from the start of the file, of the symbol table.
14093 [4] The offset, from the start of the file, of the constant pool.
14094
14095 2. The CU list. This is a sequence of pairs of 64-bit
14096 little-endian values. The first element in each pair is the offset
14097 of a CU in the .debug_info section. The second element in each
14098 pair is the length of that CU. References to a CU elsewhere in the
14099 map are done using a CU index, which is just the 0-based index into
14100 this table.
14101
14102 3. The address section. The address section consists of a sequence
14103 of address entries. Each address entry has three elements.
14104 [0] The low address. This is a 64-bit little-endian value.
14105 [1] The high address. This is a 64-bit little-endian value.
14106 [2] The CU index. This is an offset_type value.
14107
14108 4. The symbol table. This is a hash table. The size of the hash
14109 table is always a power of 2. The initial hash and the step are
14110 currently defined by the `find_slot' function.
14111
14112 Each slot in the hash table consists of a pair of offset_type
14113 values. The first value is the offset of the symbol's name in the
14114 constant pool. The second value is the offset of the CU vector in
14115 the constant pool.
14116
14117 If both values are 0, then this slot in the hash table is empty.
14118 This is ok because while 0 is a valid constant pool index, it
14119 cannot be a valid index for both a string and a CU vector.
14120
14121 A string in the constant pool is stored as a \0-terminated string,
14122 as you'd expect.
14123
14124 A CU vector in the constant pool is a sequence of offset_type
14125 values. The first value is the number of CU indices in the vector.
14126 Each subsequent value is the index of a CU in the CU list. This
14127 element in the hash table is used to indicate which CUs define the
14128 symbol.
14129
14130 5. The constant pool. This is simply a bunch of bytes. It is
14131 organized so that alignment is correct: CU vectors are stored
14132 first, followed by strings. */
14133static void
14134save_gdb_index_command (char *arg, int from_tty)
14135{
14136 struct objfile *objfile;
14137
14138 if (!arg || !*arg)
14139 error (_("usage: maintenance save-gdb-index DIRECTORY"));
14140
14141 ALL_OBJFILES (objfile)
14142 {
14143 struct stat st;
14144
14145 /* If the objfile does not correspond to an actual file, skip it. */
14146 if (stat (objfile->name, &st) < 0)
14147 continue;
14148
14149 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14150 if (dwarf2_per_objfile)
14151 {
14152 volatile struct gdb_exception except;
14153
14154 TRY_CATCH (except, RETURN_MASK_ERROR)
14155 {
14156 write_psymtabs_to_index (objfile, arg);
14157 }
14158 if (except.reason < 0)
14159 exception_fprintf (gdb_stderr, except,
14160 _("Error while writing index for `%s': "),
14161 objfile->name);
14162 }
14163 }
dce234bc
PP
14164}
14165
9291a0cd
TT
14166\f
14167
9eae7c52
TT
14168int dwarf2_always_disassemble;
14169
14170static void
14171show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14172 struct cmd_list_element *c, const char *value)
14173{
14174 fprintf_filtered (file, _("\
14175Whether to always disassemble DWARF expressions is %s.\n"),
14176 value);
14177}
14178
6502dd73
DJ
14179void _initialize_dwarf2_read (void);
14180
14181void
14182_initialize_dwarf2_read (void)
14183{
dce234bc 14184 dwarf2_objfile_data_key
c1bd65d0 14185 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 14186
1bedd215
AC
14187 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14188Set DWARF 2 specific variables.\n\
14189Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
14190 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14191 0/*allow-unknown*/, &maintenance_set_cmdlist);
14192
1bedd215
AC
14193 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14194Show DWARF 2 specific variables\n\
14195Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
14196 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14197 0/*allow-unknown*/, &maintenance_show_cmdlist);
14198
14199 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
14200 &dwarf2_max_cache_age, _("\
14201Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14202Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14203A higher limit means that cached compilation units will be stored\n\
14204in memory longer, and more total memory will be used. Zero disables\n\
14205caching, which can slow down startup."),
2c5b56ce 14206 NULL,
920d2a44 14207 show_dwarf2_max_cache_age,
2c5b56ce 14208 &set_dwarf2_cmdlist,
ae038cb0 14209 &show_dwarf2_cmdlist);
d97bc12b 14210
9eae7c52
TT
14211 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14212 &dwarf2_always_disassemble, _("\
14213Set whether `info address' always disassembles DWARF expressions."), _("\
14214Show whether `info address' always disassembles DWARF expressions."), _("\
14215When enabled, DWARF expressions are always printed in an assembly-like\n\
14216syntax. When disabled, expressions will be printed in a more\n\
14217conversational style, when possible."),
14218 NULL,
14219 show_dwarf2_always_disassemble,
14220 &set_dwarf2_cmdlist,
14221 &show_dwarf2_cmdlist);
14222
d97bc12b
DE
14223 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14224Set debugging of the dwarf2 DIE reader."), _("\
14225Show debugging of the dwarf2 DIE reader."), _("\
14226When enabled (non-zero), DIEs are dumped after they are read in.\n\
14227The value is the maximum depth to print."),
14228 NULL,
14229 NULL,
14230 &setdebuglist, &showdebuglist);
9291a0cd
TT
14231
14232 add_cmd ("gdb-index", class_files, save_gdb_index_command,
14233 _("Save a .gdb-index file"),
14234 &save_cmdlist);
6502dd73 14235}
This page took 1.938603 seconds and 4 git commands to generate.