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