*** empty log message ***
[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,
9b254dd1 4 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
c906108c
SS
5
6 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 11 support.
c906108c 12
c5aa993b 13 This file is part of GDB.
c906108c 14
c5aa993b
JM
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
a9762ec7
JB
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
c906108c 19
a9762ec7
JB
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
c906108c 24
c5aa993b 25 You should have received a copy of the GNU General Public License
a9762ec7 26 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
27
28#include "defs.h"
29#include "bfd.h"
c906108c
SS
30#include "symtab.h"
31#include "gdbtypes.h"
c906108c
SS
32#include "objfiles.h"
33#include "elf/dwarf2.h"
34#include "buildsym.h"
35#include "demangle.h"
36#include "expression.h"
d5166ae1 37#include "filenames.h" /* for DOSish file names */
2e276125 38#include "macrotab.h"
c906108c
SS
39#include "language.h"
40#include "complaints.h"
357e46e7 41#include "bcache.h"
4c2df51b
DJ
42#include "dwarf2expr.h"
43#include "dwarf2loc.h"
9219021c 44#include "cp-support.h"
72bf9492 45#include "hashtab.h"
ae038cb0
DJ
46#include "command.h"
47#include "gdbcmd.h"
ff013f42 48#include "addrmap.h"
4c2df51b 49
c906108c
SS
50#include <fcntl.h>
51#include "gdb_string.h"
4bdf3d34 52#include "gdb_assert.h"
c906108c 53#include <sys/types.h>
233a11ab
CS
54#ifdef HAVE_ZLIB_H
55#include <zlib.h>
56#endif
c906108c 57
d8151005
DJ
58/* A note on memory usage for this file.
59
60 At the present time, this code reads the debug info sections into
61 the objfile's objfile_obstack. A definite improvement for startup
62 time, on platforms which do not emit relocations for debug
63 sections, would be to use mmap instead. The object's complete
64 debug information is loaded into memory, partly to simplify
65 absolute DIE references.
66
67 Whether using obstacks or mmap, the sections should remain loaded
68 until the objfile is released, and pointers into the section data
69 can be used for any other data associated to the objfile (symbol
70 names, type names, location expressions to name a few). */
71
107d2387 72#if 0
357e46e7 73/* .debug_info header for a compilation unit
c906108c
SS
74 Because of alignment constraints, this structure has padding and cannot
75 be mapped directly onto the beginning of the .debug_info section. */
76typedef struct comp_unit_header
77 {
78 unsigned int length; /* length of the .debug_info
79 contribution */
80 unsigned short version; /* version number -- 2 for DWARF
81 version 2 */
82 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
83 unsigned char addr_size; /* byte size of an address -- 4 */
84 }
85_COMP_UNIT_HEADER;
86#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 87#endif
c906108c
SS
88
89/* .debug_pubnames header
90 Because of alignment constraints, this structure has padding and cannot
91 be mapped directly onto the beginning of the .debug_info section. */
92typedef struct pubnames_header
93 {
94 unsigned int length; /* length of the .debug_pubnames
95 contribution */
96 unsigned char version; /* version number -- 2 for DWARF
97 version 2 */
98 unsigned int info_offset; /* offset into .debug_info section */
99 unsigned int info_size; /* byte size of .debug_info section
100 portion */
101 }
102_PUBNAMES_HEADER;
103#define _ACTUAL_PUBNAMES_HEADER_SIZE 13
104
105/* .debug_pubnames header
106 Because of alignment constraints, this structure has padding and cannot
107 be mapped directly onto the beginning of the .debug_info section. */
108typedef struct aranges_header
109 {
110 unsigned int length; /* byte len of the .debug_aranges
111 contribution */
112 unsigned short version; /* version number -- 2 for DWARF
113 version 2 */
114 unsigned int info_offset; /* offset into .debug_info section */
115 unsigned char addr_size; /* byte size of an address */
116 unsigned char seg_size; /* byte size of segment descriptor */
117 }
118_ARANGES_HEADER;
119#define _ACTUAL_ARANGES_HEADER_SIZE 12
120
121/* .debug_line statement program prologue
122 Because of alignment constraints, this structure has padding and cannot
123 be mapped directly onto the beginning of the .debug_info section. */
124typedef struct statement_prologue
125 {
126 unsigned int total_length; /* byte length of the statement
127 information */
128 unsigned short version; /* version number -- 2 for DWARF
129 version 2 */
130 unsigned int prologue_length; /* # bytes between prologue &
131 stmt program */
132 unsigned char minimum_instruction_length; /* byte size of
133 smallest instr */
134 unsigned char default_is_stmt; /* initial value of is_stmt
135 register */
136 char line_base;
137 unsigned char line_range;
138 unsigned char opcode_base; /* number assigned to first special
139 opcode */
140 unsigned char *standard_opcode_lengths;
141 }
142_STATEMENT_PROLOGUE;
143
df8a16a1
DJ
144/* When set, the file that we're processing is known to have debugging
145 info for C++ namespaces. GCC 3.3.x did not produce this information,
146 but later versions do. */
147
148static int processing_has_namespace_info;
149
6502dd73
DJ
150static const struct objfile_data *dwarf2_objfile_data_key;
151
152struct dwarf2_per_objfile
153{
154 /* Sizes of debugging sections. */
155 unsigned int info_size;
156 unsigned int abbrev_size;
157 unsigned int line_size;
158 unsigned int pubnames_size;
159 unsigned int aranges_size;
160 unsigned int loc_size;
161 unsigned int macinfo_size;
162 unsigned int str_size;
163 unsigned int ranges_size;
164 unsigned int frame_size;
165 unsigned int eh_frame_size;
166
167 /* Loaded data from the sections. */
fe1b8b76
JB
168 gdb_byte *info_buffer;
169 gdb_byte *abbrev_buffer;
170 gdb_byte *line_buffer;
171 gdb_byte *str_buffer;
172 gdb_byte *macinfo_buffer;
173 gdb_byte *ranges_buffer;
174 gdb_byte *loc_buffer;
ae038cb0 175
10b3939b
DJ
176 /* A list of all the compilation units. This is used to locate
177 the target compilation unit of a particular reference. */
ae038cb0
DJ
178 struct dwarf2_per_cu_data **all_comp_units;
179
180 /* The number of compilation units in ALL_COMP_UNITS. */
181 int n_comp_units;
182
183 /* A chain of compilation units that are currently read in, so that
184 they can be freed later. */
185 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5
FR
186
187 /* A flag indicating wether this objfile has a section loaded at a
188 VMA of 0. */
189 int has_section_at_zero;
6502dd73
DJ
190};
191
192static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 193
086df311
DJ
194static asection *dwarf_info_section;
195static asection *dwarf_abbrev_section;
196static asection *dwarf_line_section;
197static asection *dwarf_pubnames_section;
198static asection *dwarf_aranges_section;
199static asection *dwarf_loc_section;
200static asection *dwarf_macinfo_section;
201static asection *dwarf_str_section;
202static asection *dwarf_ranges_section;
203asection *dwarf_frame_section;
204asection *dwarf_eh_frame_section;
205
c906108c
SS
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"
214#define PUBNAMES_SECTION "debug_pubnames"
215#define ARANGES_SECTION "debug_aranges"
216#define LOC_SECTION "debug_loc"
217#define MACINFO_SECTION "debug_macinfo"
218#define STR_SECTION "debug_str"
219#define RANGES_SECTION "debug_ranges"
220#define FRAME_SECTION "debug_frame"
221#define EH_FRAME_SECTION "eh_frame"
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
MK
233{
234 unsigned long length;
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
ff013f42
JK
298 /* `.debug_ranges' offset for this `DW_TAG_compile_unit' DIE. */
299 unsigned long ranges_offset;
300
72bf9492
DJ
301 /* Storage for things with the same lifetime as this read-in compilation
302 unit, including partial DIEs. */
303 struct obstack comp_unit_obstack;
304
ae038cb0
DJ
305 /* When multiple dwarf2_cu structures are living in memory, this field
306 chains them all together, so that they can be released efficiently.
307 We will probably also want a generation counter so that most-recently-used
308 compilation units are cached... */
309 struct dwarf2_per_cu_data *read_in_chain;
310
311 /* Backchain to our per_cu entry if the tree has been built. */
312 struct dwarf2_per_cu_data *per_cu;
313
f792889a
DJ
314 /* Pointer to the die -> type map. Although it is stored
315 permanently in per_cu, we copy it here to avoid double
316 indirection. */
317 htab_t type_hash;
318
ae038cb0
DJ
319 /* How many compilation units ago was this CU last referenced? */
320 int last_used;
321
10b3939b 322 /* A hash table of die offsets for following references. */
51545339 323 htab_t die_hash;
10b3939b
DJ
324
325 /* Full DIEs if read in. */
326 struct die_info *dies;
327
328 /* A set of pointers to dwarf2_per_cu_data objects for compilation
329 units referenced by this one. Only set during full symbol processing;
330 partial symbol tables do not have dependencies. */
331 htab_t dependencies;
332
cb1df416
DJ
333 /* Header data from the line table, during full symbol processing. */
334 struct line_header *line_header;
335
ae038cb0
DJ
336 /* Mark used when releasing cached dies. */
337 unsigned int mark : 1;
338
339 /* This flag will be set if this compilation unit might include
340 inter-compilation-unit references. */
341 unsigned int has_form_ref_addr : 1;
342
72bf9492
DJ
343 /* This flag will be set if this compilation unit includes any
344 DW_TAG_namespace DIEs. If we know that there are explicit
345 DIEs for namespaces, we don't need to try to infer them
346 from mangled names. */
347 unsigned int has_namespace_info : 1;
ff013f42
JK
348
349 /* Field `ranges_offset' is filled in; flag as the value may be zero. */
350 unsigned int has_ranges_offset : 1;
e7c27a73
DJ
351};
352
10b3939b
DJ
353/* Persistent data held for a compilation unit, even when not
354 processing it. We put a pointer to this structure in the
355 read_symtab_private field of the psymtab. If we encounter
356 inter-compilation-unit references, we also maintain a sorted
357 list of all compilation units. */
358
ae038cb0
DJ
359struct dwarf2_per_cu_data
360{
5afb4e99 361 /* The start offset and length of this compilation unit. 2**30-1
ae038cb0
DJ
362 bytes should suffice to store the length of any compilation unit
363 - if it doesn't, GDB will fall over anyway. */
364 unsigned long offset;
5afb4e99 365 unsigned long length : 30;
ae038cb0
DJ
366
367 /* Flag indicating this compilation unit will be read in before
368 any of the current compilation units are processed. */
369 unsigned long queued : 1;
370
5afb4e99
DJ
371 /* This flag will be set if we need to load absolutely all DIEs
372 for this compilation unit, instead of just the ones we think
373 are interesting. It gets set if we look for a DIE in the
374 hash table and don't find it. */
375 unsigned int load_all_dies : 1;
376
ae038cb0
DJ
377 /* Set iff currently read in. */
378 struct dwarf2_cu *cu;
1c379e20
DJ
379
380 /* If full symbols for this CU have been read in, then this field
381 holds a map of DIE offsets to types. It isn't always possible
382 to reconstruct this information later, so we have to preserve
383 it. */
1c379e20 384 htab_t type_hash;
10b3939b 385
31ffec48
DJ
386 /* The partial symbol table associated with this compilation unit,
387 or NULL for partial units (which do not have an associated
388 symtab). */
10b3939b 389 struct partial_symtab *psymtab;
ae038cb0
DJ
390};
391
debd256d
JB
392/* The line number information for a compilation unit (found in the
393 .debug_line section) begins with a "statement program header",
394 which contains the following information. */
395struct line_header
396{
397 unsigned int total_length;
398 unsigned short version;
399 unsigned int header_length;
400 unsigned char minimum_instruction_length;
401 unsigned char default_is_stmt;
402 int line_base;
403 unsigned char line_range;
404 unsigned char opcode_base;
405
406 /* standard_opcode_lengths[i] is the number of operands for the
407 standard opcode whose value is i. This means that
408 standard_opcode_lengths[0] is unused, and the last meaningful
409 element is standard_opcode_lengths[opcode_base - 1]. */
410 unsigned char *standard_opcode_lengths;
411
412 /* The include_directories table. NOTE! These strings are not
413 allocated with xmalloc; instead, they are pointers into
414 debug_line_buffer. If you try to free them, `free' will get
415 indigestion. */
416 unsigned int num_include_dirs, include_dirs_size;
417 char **include_dirs;
418
419 /* The file_names table. NOTE! These strings are not allocated
420 with xmalloc; instead, they are pointers into debug_line_buffer.
421 Don't try to free them directly. */
422 unsigned int num_file_names, file_names_size;
423 struct file_entry
c906108c 424 {
debd256d
JB
425 char *name;
426 unsigned int dir_index;
427 unsigned int mod_time;
428 unsigned int length;
aaa75496 429 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 430 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
431 } *file_names;
432
433 /* The start and end of the statement program following this
6502dd73 434 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 435 gdb_byte *statement_program_start, *statement_program_end;
debd256d 436};
c906108c
SS
437
438/* When we construct a partial symbol table entry we only
439 need this much information. */
440struct partial_die_info
441 {
72bf9492 442 /* Offset of this DIE. */
c906108c 443 unsigned int offset;
72bf9492
DJ
444
445 /* DWARF-2 tag for this DIE. */
446 ENUM_BITFIELD(dwarf_tag) tag : 16;
447
448 /* Language code associated with this DIE. This is only used
449 for the compilation unit DIE. */
450 unsigned int language : 8;
451
452 /* Assorted flags describing the data found in this DIE. */
453 unsigned int has_children : 1;
454 unsigned int is_external : 1;
455 unsigned int is_declaration : 1;
456 unsigned int has_type : 1;
457 unsigned int has_specification : 1;
aaa75496 458 unsigned int has_stmt_list : 1;
72bf9492
DJ
459 unsigned int has_pc_info : 1;
460
461 /* Flag set if the SCOPE field of this structure has been
462 computed. */
463 unsigned int scope_set : 1;
464
fa4028e9
JB
465 /* Flag set if the DIE has a byte_size attribute. */
466 unsigned int has_byte_size : 1;
467
72bf9492
DJ
468 /* The name of this DIE. Normally the value of DW_AT_name, but
469 sometimes DW_TAG_MIPS_linkage_name or a string computed in some
470 other fashion. */
c906108c 471 char *name;
57c22c6c 472 char *dirname;
72bf9492
DJ
473
474 /* The scope to prepend to our children. This is generally
475 allocated on the comp_unit_obstack, so will disappear
476 when this compilation unit leaves the cache. */
477 char *scope;
478
479 /* The location description associated with this DIE, if any. */
480 struct dwarf_block *locdesc;
481
482 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
483 CORE_ADDR lowpc;
484 CORE_ADDR highpc;
72bf9492
DJ
485
486 /* Pointer into the info_buffer pointing at the target of
487 DW_AT_sibling, if any. */
fe1b8b76 488 gdb_byte *sibling;
72bf9492
DJ
489
490 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
491 DW_AT_specification (or DW_AT_abstract_origin or
492 DW_AT_extension). */
493 unsigned int spec_offset;
494
aaa75496
JB
495 /* If HAS_STMT_LIST, the offset of the Line Number Information data. */
496 unsigned int line_offset;
497
72bf9492
DJ
498 /* Pointers to this DIE's parent, first child, and next sibling,
499 if any. */
500 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
501 };
502
503/* This data structure holds the information of an abbrev. */
504struct abbrev_info
505 {
506 unsigned int number; /* number identifying abbrev */
507 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
508 unsigned short has_children; /* boolean */
509 unsigned short num_attrs; /* number of attributes */
c906108c
SS
510 struct attr_abbrev *attrs; /* an array of attribute descriptions */
511 struct abbrev_info *next; /* next in chain */
512 };
513
514struct attr_abbrev
515 {
9d25dd43
DE
516 ENUM_BITFIELD(dwarf_attribute) name : 16;
517 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
518 };
519
b60c80d6
DJ
520/* Attributes have a name and a value */
521struct attribute
522 {
9d25dd43
DE
523 ENUM_BITFIELD(dwarf_attribute) name : 16;
524 ENUM_BITFIELD(dwarf_form) form : 16;
b60c80d6
DJ
525 union
526 {
527 char *str;
528 struct dwarf_block *blk;
529 unsigned long unsnd;
530 long int snd;
531 CORE_ADDR addr;
532 }
533 u;
534 };
535
c906108c
SS
536/* This data structure holds a complete die structure. */
537struct die_info
538 {
76815b17
DE
539 /* DWARF-2 tag for this DIE. */
540 ENUM_BITFIELD(dwarf_tag) tag : 16;
541
542 /* Number of attributes */
543 unsigned short num_attrs;
544
545 /* Abbrev number */
546 unsigned int abbrev;
547
548 /* Offset in .debug_info section */
549 unsigned int offset;
78ba4af6
JB
550
551 /* The dies in a compilation unit form an n-ary tree. PARENT
552 points to this die's parent; CHILD points to the first child of
553 this node; and all the children of a given node are chained
554 together via their SIBLING fields, terminated by a die whose
555 tag is zero. */
639d11d3
DC
556 struct die_info *child; /* Its first child, if any. */
557 struct die_info *sibling; /* Its next sibling, if any. */
558 struct die_info *parent; /* Its parent, if any. */
c906108c 559
b60c80d6
DJ
560 /* An array of attributes, with NUM_ATTRS elements. There may be
561 zero, but it's not common and zero-sized arrays are not
562 sufficiently portable C. */
563 struct attribute attrs[1];
c906108c
SS
564 };
565
5fb290d7
DJ
566struct function_range
567{
568 const char *name;
569 CORE_ADDR lowpc, highpc;
570 int seen_line;
571 struct function_range *next;
572};
573
c906108c
SS
574/* Get at parts of an attribute structure */
575
576#define DW_STRING(attr) ((attr)->u.str)
577#define DW_UNSND(attr) ((attr)->u.unsnd)
578#define DW_BLOCK(attr) ((attr)->u.blk)
579#define DW_SND(attr) ((attr)->u.snd)
580#define DW_ADDR(attr) ((attr)->u.addr)
581
582/* Blocks are a bunch of untyped bytes. */
583struct dwarf_block
584 {
585 unsigned int size;
fe1b8b76 586 gdb_byte *data;
c906108c
SS
587 };
588
c906108c
SS
589#ifndef ATTR_ALLOC_CHUNK
590#define ATTR_ALLOC_CHUNK 4
591#endif
592
c906108c
SS
593/* Allocate fields for structs, unions and enums in this size. */
594#ifndef DW_FIELD_ALLOC_CHUNK
595#define DW_FIELD_ALLOC_CHUNK 4
596#endif
597
c906108c
SS
598/* A zeroed version of a partial die for initialization purposes. */
599static struct partial_die_info zeroed_partial_die;
600
c906108c
SS
601/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
602 but this would require a corresponding change in unpack_field_as_long
603 and friends. */
604static int bits_per_byte = 8;
605
606/* The routines that read and process dies for a C struct or C++ class
607 pass lists of data member fields and lists of member function fields
608 in an instance of a field_info structure, as defined below. */
609struct field_info
c5aa993b
JM
610 {
611 /* List of data member and baseclasses fields. */
612 struct nextfield
613 {
614 struct nextfield *next;
615 int accessibility;
616 int virtuality;
617 struct field field;
618 }
619 *fields;
c906108c 620
c5aa993b
JM
621 /* Number of fields. */
622 int nfields;
c906108c 623
c5aa993b
JM
624 /* Number of baseclasses. */
625 int nbaseclasses;
c906108c 626
c5aa993b
JM
627 /* Set if the accesibility of one of the fields is not public. */
628 int non_public_fields;
c906108c 629
c5aa993b
JM
630 /* Member function fields array, entries are allocated in the order they
631 are encountered in the object file. */
632 struct nextfnfield
633 {
634 struct nextfnfield *next;
635 struct fn_field fnfield;
636 }
637 *fnfields;
c906108c 638
c5aa993b
JM
639 /* Member function fieldlist array, contains name of possibly overloaded
640 member function, number of overloaded member functions and a pointer
641 to the head of the member function field chain. */
642 struct fnfieldlist
643 {
644 char *name;
645 int length;
646 struct nextfnfield *head;
647 }
648 *fnfieldlists;
c906108c 649
c5aa993b
JM
650 /* Number of entries in the fnfieldlists array. */
651 int nfnfields;
652 };
c906108c 653
10b3939b
DJ
654/* One item on the queue of compilation units to read in full symbols
655 for. */
656struct dwarf2_queue_item
657{
658 struct dwarf2_per_cu_data *per_cu;
659 struct dwarf2_queue_item *next;
660};
661
662/* The current queue. */
663static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
664
ae038cb0
DJ
665/* Loaded secondary compilation units are kept in memory until they
666 have not been referenced for the processing of this many
667 compilation units. Set this to zero to disable caching. Cache
668 sizes of up to at least twenty will improve startup time for
669 typical inter-CU-reference binaries, at an obvious memory cost. */
670static int dwarf2_max_cache_age = 5;
920d2a44
AC
671static void
672show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
673 struct cmd_list_element *c, const char *value)
674{
675 fprintf_filtered (file, _("\
676The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
677 value);
678}
679
ae038cb0 680
c906108c
SS
681/* Various complaints about symbol reading that don't abort the process */
682
4d3c2250
KB
683static void
684dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 685{
4d3c2250 686 complaint (&symfile_complaints,
e2e0b3e5 687 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
688}
689
25e43795
DJ
690static void
691dwarf2_debug_line_missing_file_complaint (void)
692{
693 complaint (&symfile_complaints,
694 _(".debug_line section has line data without a file"));
695}
696
4d3c2250
KB
697static void
698dwarf2_complex_location_expr_complaint (void)
2e276125 699{
e2e0b3e5 700 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
701}
702
4d3c2250
KB
703static void
704dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
705 int arg3)
2e276125 706{
4d3c2250 707 complaint (&symfile_complaints,
e2e0b3e5 708 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
709 arg2, arg3);
710}
711
712static void
713dwarf2_macros_too_long_complaint (void)
2e276125 714{
4d3c2250 715 complaint (&symfile_complaints,
e2e0b3e5 716 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
717}
718
719static void
720dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 721{
4d3c2250 722 complaint (&symfile_complaints,
e2e0b3e5 723 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
724 arg1);
725}
726
727static void
728dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 729{
4d3c2250 730 complaint (&symfile_complaints,
e2e0b3e5 731 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 732}
c906108c 733
c906108c
SS
734/* local function prototypes */
735
4efb68b1 736static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c
SS
737
738#if 0
a14ed312 739static void dwarf2_build_psymtabs_easy (struct objfile *, int);
c906108c
SS
740#endif
741
aaa75496
JB
742static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
743 struct objfile *);
744
745static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
746 struct partial_die_info *,
747 struct partial_symtab *);
748
a14ed312 749static void dwarf2_build_psymtabs_hard (struct objfile *, int);
c906108c 750
72bf9492
DJ
751static void scan_partial_symbols (struct partial_die_info *,
752 CORE_ADDR *, CORE_ADDR *,
753 struct dwarf2_cu *);
c906108c 754
72bf9492
DJ
755static void add_partial_symbol (struct partial_die_info *,
756 struct dwarf2_cu *);
63d06c5c 757
72bf9492 758static int pdi_needs_namespace (enum dwarf_tag tag);
91c24f0a 759
72bf9492
DJ
760static void add_partial_namespace (struct partial_die_info *pdi,
761 CORE_ADDR *lowpc, CORE_ADDR *highpc,
762 struct dwarf2_cu *cu);
63d06c5c 763
72bf9492
DJ
764static void add_partial_enumeration (struct partial_die_info *enum_pdi,
765 struct dwarf2_cu *cu);
91c24f0a 766
bc30ff58
JB
767static void add_partial_subprogram (struct partial_die_info *pdi,
768 CORE_ADDR *lowpc, CORE_ADDR *highpc,
769 struct dwarf2_cu *cu);
770
fe1b8b76
JB
771static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
772 gdb_byte *info_ptr,
773 bfd *abfd,
774 struct dwarf2_cu *cu);
91c24f0a 775
a14ed312 776static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 777
a14ed312 778static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 779
fe1b8b76 780gdb_byte *dwarf2_read_section (struct objfile *, asection *);
c906108c 781
e7c27a73 782static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 783
f3dd6933 784static void dwarf2_free_abbrev_table (void *);
c906108c 785
fe1b8b76 786static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 787 struct dwarf2_cu *);
72bf9492 788
57349743 789static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 790 struct dwarf2_cu *);
c906108c 791
fe1b8b76 792static struct partial_die_info *load_partial_dies (bfd *, gdb_byte *, int,
72bf9492
DJ
793 struct dwarf2_cu *);
794
fe1b8b76
JB
795static gdb_byte *read_partial_die (struct partial_die_info *,
796 struct abbrev_info *abbrev, unsigned int,
797 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 798
72bf9492 799static struct partial_die_info *find_partial_die (unsigned long,
10b3939b 800 struct dwarf2_cu *);
72bf9492
DJ
801
802static void fixup_partial_die (struct partial_die_info *,
803 struct dwarf2_cu *);
804
fe1b8b76
JB
805static gdb_byte *read_full_die (struct die_info **, bfd *, gdb_byte *,
806 struct dwarf2_cu *, int *);
c906108c 807
fe1b8b76
JB
808static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
809 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 810
fe1b8b76
JB
811static gdb_byte *read_attribute_value (struct attribute *, unsigned,
812 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 813
fe1b8b76 814static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 815
fe1b8b76 816static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 817
fe1b8b76 818static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 819
fe1b8b76 820static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 821
fe1b8b76 822static unsigned long read_8_bytes (bfd *, gdb_byte *);
c906108c 823
fe1b8b76 824static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 825 unsigned int *);
c906108c 826
fe1b8b76 827static LONGEST read_initial_length (bfd *, gdb_byte *,
891d2f0b 828 struct comp_unit_head *, unsigned int *);
613e1657 829
fe1b8b76 830static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
891d2f0b 831 unsigned int *);
613e1657 832
fe1b8b76 833static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 834
fe1b8b76 835static char *read_string (bfd *, gdb_byte *, unsigned int *);
c906108c 836
fe1b8b76
JB
837static char *read_indirect_string (bfd *, gdb_byte *,
838 const struct comp_unit_head *,
839 unsigned int *);
4bdf3d34 840
fe1b8b76 841static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 842
fe1b8b76 843static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 844
fe1b8b76 845static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 846
e142c38c 847static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 848
e142c38c
DJ
849static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
850 struct dwarf2_cu *);
c906108c 851
05cf31d1
JB
852static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
853 struct dwarf2_cu *cu);
854
e142c38c 855static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 856
e142c38c 857static struct die_info *die_specification (struct die_info *die,
f2f0e013 858 struct dwarf2_cu **);
63d06c5c 859
debd256d
JB
860static void free_line_header (struct line_header *lh);
861
aaa75496
JB
862static void add_file_name (struct line_header *, char *, unsigned int,
863 unsigned int, unsigned int);
864
debd256d
JB
865static struct line_header *(dwarf_decode_line_header
866 (unsigned int offset,
e7c27a73 867 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
868
869static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 870 struct dwarf2_cu *, struct partial_symtab *);
c906108c 871
4f1520fb 872static void dwarf2_start_subfile (char *, char *, char *);
c906108c 873
a14ed312 874static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 875 struct dwarf2_cu *);
c906108c 876
a14ed312 877static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 878 struct dwarf2_cu *);
c906108c 879
2df3850c
JM
880static void dwarf2_const_value_data (struct attribute *attr,
881 struct symbol *sym,
882 int bits);
883
e7c27a73 884static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 885
e7c27a73
DJ
886static struct type *die_containing_type (struct die_info *,
887 struct dwarf2_cu *);
c906108c 888
e7c27a73 889static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
c906108c 890
f792889a 891static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 892
086ed43d 893static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 894
fe1b8b76
JB
895static char *typename_concat (struct obstack *,
896 const char *prefix,
897 const char *suffix,
987504bb 898 struct dwarf2_cu *);
63d06c5c 899
e7c27a73 900static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 901
e7c27a73 902static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 903
e7c27a73 904static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 905
ff013f42
JK
906static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
907 struct dwarf2_cu *, struct partial_symtab *);
908
a14ed312 909static int dwarf2_get_pc_bounds (struct die_info *,
e7c27a73 910 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *);
c906108c 911
fae299cd
DC
912static void get_scope_pc_bounds (struct die_info *,
913 CORE_ADDR *, CORE_ADDR *,
914 struct dwarf2_cu *);
915
801e3a5b
JB
916static void dwarf2_record_block_ranges (struct die_info *, struct block *,
917 CORE_ADDR, struct dwarf2_cu *);
918
a14ed312 919static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 920 struct dwarf2_cu *);
c906108c 921
a14ed312 922static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 923 struct type *, struct dwarf2_cu *);
c906108c 924
a14ed312 925static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 926 struct die_info *, struct type *,
e7c27a73 927 struct dwarf2_cu *);
c906108c 928
a14ed312 929static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 930 struct type *, struct dwarf2_cu *);
c906108c 931
134d01f1 932static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 933
0114d602
DJ
934static const char *determine_class_name (struct die_info *die,
935 struct dwarf2_cu *cu);
8176b9b8 936
e7c27a73 937static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 938
e7c27a73 939static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 940
38d518c9 941static const char *namespace_name (struct die_info *die,
e142c38c 942 int *is_anonymous, struct dwarf2_cu *);
38d518c9 943
134d01f1 944static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 945
e7c27a73 946static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 947
7ca2d3a3
DL
948static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
949 struct dwarf2_cu *);
950
fe1b8b76 951static struct die_info *read_comp_unit (gdb_byte *, bfd *, struct dwarf2_cu *);
c906108c 952
fe1b8b76 953static struct die_info *read_die_and_children (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 954 struct dwarf2_cu *,
fe1b8b76 955 gdb_byte **new_info_ptr,
639d11d3
DC
956 struct die_info *parent);
957
fe1b8b76 958static struct die_info *read_die_and_siblings (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 959 struct dwarf2_cu *,
fe1b8b76 960 gdb_byte **new_info_ptr,
639d11d3
DC
961 struct die_info *parent);
962
e7c27a73 963static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 964
e142c38c 965static char *dwarf2_linkage_name (struct die_info *, struct dwarf2_cu *);
c906108c 966
e142c38c 967static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 968
e142c38c 969static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 970 struct dwarf2_cu **);
9219021c 971
a14ed312 972static char *dwarf_tag_name (unsigned int);
c906108c 973
a14ed312 974static char *dwarf_attr_name (unsigned int);
c906108c 975
a14ed312 976static char *dwarf_form_name (unsigned int);
c906108c 977
a14ed312 978static char *dwarf_stack_op_name (unsigned int);
c906108c 979
a14ed312 980static char *dwarf_bool_name (unsigned int);
c906108c 981
a14ed312 982static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
983
984#if 0
a14ed312 985static char *dwarf_cfi_name (unsigned int);
c906108c
SS
986#endif
987
f9aca02d 988static struct die_info *sibling_die (struct die_info *);
c906108c 989
f9aca02d 990static void dump_die (struct die_info *);
c906108c 991
f9aca02d 992static void dump_die_list (struct die_info *);
c906108c 993
51545339 994static void store_in_ref_table (struct die_info *,
10b3939b 995 struct dwarf2_cu *);
c906108c 996
e142c38c
DJ
997static unsigned int dwarf2_get_ref_die_offset (struct attribute *,
998 struct dwarf2_cu *);
c906108c 999
a02abb62
JB
1000static int dwarf2_get_attr_constant_value (struct attribute *, int);
1001
10b3939b
DJ
1002static struct die_info *follow_die_ref (struct die_info *,
1003 struct attribute *,
f2f0e013 1004 struct dwarf2_cu **);
c906108c 1005
c906108c
SS
1006/* memory allocation interface */
1007
7b5a2f43 1008static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1009
f3dd6933 1010static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1011
b60c80d6 1012static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1013
e142c38c 1014static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1015
e142c38c
DJ
1016static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1017 struct dwarf2_cu *);
5fb290d7 1018
2e276125 1019static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1020 char *, bfd *, struct dwarf2_cu *);
2e276125 1021
8e19ed76
PS
1022static int attr_form_is_block (struct attribute *);
1023
3690dd37
JB
1024static int attr_form_is_section_offset (struct attribute *);
1025
1026static int attr_form_is_constant (struct attribute *);
1027
93e7bd98
DJ
1028static void dwarf2_symbol_mark_computed (struct attribute *attr,
1029 struct symbol *sym,
1030 struct dwarf2_cu *cu);
4c2df51b 1031
fe1b8b76
JB
1032static gdb_byte *skip_one_die (gdb_byte *info_ptr, struct abbrev_info *abbrev,
1033 struct dwarf2_cu *cu);
4bb7a0a7 1034
72bf9492
DJ
1035static void free_stack_comp_unit (void *);
1036
72bf9492
DJ
1037static hashval_t partial_die_hash (const void *item);
1038
1039static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1040
ae038cb0
DJ
1041static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1042 (unsigned long offset, struct objfile *objfile);
1043
1044static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1045 (unsigned long offset, struct objfile *objfile);
1046
1047static void free_one_comp_unit (void *);
1048
1049static void free_cached_comp_units (void *);
1050
1051static void age_cached_comp_units (void);
1052
1053static void free_one_cached_comp_unit (void *);
1054
f792889a
DJ
1055static struct type *set_die_type (struct die_info *, struct type *,
1056 struct dwarf2_cu *);
1c379e20 1057
ae038cb0
DJ
1058static void create_all_comp_units (struct objfile *);
1059
31ffec48
DJ
1060static struct dwarf2_cu *load_full_comp_unit (struct dwarf2_per_cu_data *,
1061 struct objfile *);
10b3939b
DJ
1062
1063static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1064
1065static void dwarf2_add_dependence (struct dwarf2_cu *,
1066 struct dwarf2_per_cu_data *);
1067
ae038cb0
DJ
1068static void dwarf2_mark (struct dwarf2_cu *);
1069
1070static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1071
f792889a 1072static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1073
c906108c
SS
1074/* Try to locate the sections we need for DWARF 2 debugging
1075 information and return true if we have enough to do something. */
1076
1077int
6502dd73 1078dwarf2_has_info (struct objfile *objfile)
c906108c 1079{
6502dd73
DJ
1080 struct dwarf2_per_objfile *data;
1081
1082 /* Initialize per-objfile state. */
1083 data = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1084 memset (data, 0, sizeof (*data));
1085 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1086 dwarf2_per_objfile = data;
1087
188dd5d6
DJ
1088 dwarf_info_section = 0;
1089 dwarf_abbrev_section = 0;
1090 dwarf_line_section = 0;
1091 dwarf_str_section = 0;
1092 dwarf_macinfo_section = 0;
1093 dwarf_frame_section = 0;
1094 dwarf_eh_frame_section = 0;
1095 dwarf_ranges_section = 0;
1096 dwarf_loc_section = 0;
af34e669 1097
6502dd73 1098 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
188dd5d6 1099 return (dwarf_info_section != NULL && dwarf_abbrev_section != NULL);
c906108c
SS
1100}
1101
233a11ab
CS
1102/* When loading sections, we can either look for ".<name>", or for
1103 * ".z<name>", which indicates a compressed section. */
1104
1105static int
1106section_is_p (asection *sectp, const char *name)
1107{
1108 return ((sectp->name[0] == '.'
1109 && strcmp (sectp->name + 1, name) == 0)
1110 || (sectp->name[0] == '.' && sectp->name[1] == 'z'
1111 && strcmp (sectp->name + 2, name) == 0));
1112}
1113
c906108c
SS
1114/* This function is mapped across the sections and remembers the
1115 offset and size of each of the debugging sections we are interested
1116 in. */
1117
1118static void
72dca2f5 1119dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1120{
233a11ab 1121 if (section_is_p (sectp, INFO_SECTION))
c906108c 1122 {
2c500098 1123 dwarf2_per_objfile->info_size = bfd_get_section_size (sectp);
086df311 1124 dwarf_info_section = sectp;
c906108c 1125 }
233a11ab 1126 else if (section_is_p (sectp, ABBREV_SECTION))
c906108c 1127 {
2c500098 1128 dwarf2_per_objfile->abbrev_size = bfd_get_section_size (sectp);
086df311 1129 dwarf_abbrev_section = sectp;
c906108c 1130 }
233a11ab 1131 else if (section_is_p (sectp, LINE_SECTION))
c906108c 1132 {
2c500098 1133 dwarf2_per_objfile->line_size = bfd_get_section_size (sectp);
086df311 1134 dwarf_line_section = sectp;
c906108c 1135 }
233a11ab 1136 else if (section_is_p (sectp, PUBNAMES_SECTION))
c906108c 1137 {
2c500098 1138 dwarf2_per_objfile->pubnames_size = bfd_get_section_size (sectp);
086df311 1139 dwarf_pubnames_section = sectp;
c906108c 1140 }
233a11ab 1141 else if (section_is_p (sectp, ARANGES_SECTION))
c906108c 1142 {
2c500098 1143 dwarf2_per_objfile->aranges_size = bfd_get_section_size (sectp);
086df311 1144 dwarf_aranges_section = sectp;
c906108c 1145 }
233a11ab 1146 else if (section_is_p (sectp, LOC_SECTION))
c906108c 1147 {
2c500098 1148 dwarf2_per_objfile->loc_size = bfd_get_section_size (sectp);
086df311 1149 dwarf_loc_section = sectp;
c906108c 1150 }
233a11ab 1151 else if (section_is_p (sectp, MACINFO_SECTION))
c906108c 1152 {
2c500098 1153 dwarf2_per_objfile->macinfo_size = bfd_get_section_size (sectp);
0cf824c9 1154 dwarf_macinfo_section = sectp;
c906108c 1155 }
233a11ab 1156 else if (section_is_p (sectp, STR_SECTION))
c906108c 1157 {
2c500098 1158 dwarf2_per_objfile->str_size = bfd_get_section_size (sectp);
086df311 1159 dwarf_str_section = sectp;
c906108c 1160 }
233a11ab 1161 else if (section_is_p (sectp, FRAME_SECTION))
b6af0555 1162 {
2c500098 1163 dwarf2_per_objfile->frame_size = bfd_get_section_size (sectp);
086df311 1164 dwarf_frame_section = sectp;
b6af0555 1165 }
233a11ab 1166 else if (section_is_p (sectp, EH_FRAME_SECTION))
b6af0555 1167 {
3799ccc6
EZ
1168 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1169 if (aflag & SEC_HAS_CONTENTS)
1170 {
2c500098 1171 dwarf2_per_objfile->eh_frame_size = bfd_get_section_size (sectp);
3799ccc6
EZ
1172 dwarf_eh_frame_section = sectp;
1173 }
b6af0555 1174 }
233a11ab 1175 else if (section_is_p (sectp, RANGES_SECTION))
af34e669 1176 {
2c500098 1177 dwarf2_per_objfile->ranges_size = bfd_get_section_size (sectp);
6f10aeb1 1178 dwarf_ranges_section = sectp;
af34e669 1179 }
72dca2f5
FR
1180
1181 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1182 && bfd_section_vma (abfd, sectp) == 0)
1183 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1184}
1185
233a11ab
CS
1186/* This function is called after decompressing a section, so
1187 dwarf2_per_objfile can record its new, uncompressed size. */
1188
1189static void
1190dwarf2_resize_section (asection *sectp, bfd_size_type new_size)
1191{
1192 if (section_is_p (sectp, INFO_SECTION))
1193 dwarf2_per_objfile->info_size = new_size;
1194 else if (section_is_p (sectp, ABBREV_SECTION))
1195 dwarf2_per_objfile->abbrev_size = new_size;
1196 else if (section_is_p (sectp, LINE_SECTION))
1197 dwarf2_per_objfile->line_size = new_size;
1198 else if (section_is_p (sectp, PUBNAMES_SECTION))
1199 dwarf2_per_objfile->pubnames_size = new_size;
1200 else if (section_is_p (sectp, ARANGES_SECTION))
1201 dwarf2_per_objfile->aranges_size = new_size;
1202 else if (section_is_p (sectp, LOC_SECTION))
1203 dwarf2_per_objfile->loc_size = new_size;
1204 else if (section_is_p (sectp, MACINFO_SECTION))
1205 dwarf2_per_objfile->macinfo_size = new_size;
1206 else if (section_is_p (sectp, STR_SECTION))
1207 dwarf2_per_objfile->str_size = new_size;
1208 else if (section_is_p (sectp, FRAME_SECTION))
1209 dwarf2_per_objfile->frame_size = new_size;
1210 else if (section_is_p (sectp, EH_FRAME_SECTION))
1211 dwarf2_per_objfile->eh_frame_size = new_size;
1212 else if (section_is_p (sectp, RANGES_SECTION))
1213 dwarf2_per_objfile->ranges_size = new_size;
1214 else
1215 internal_error (__FILE__, __LINE__,
1216 _("dwarf2_resize_section: missing section_is_p check: %s"),
1217 sectp->name);
1218}
1219
c906108c
SS
1220/* Build a partial symbol table. */
1221
1222void
fba45db2 1223dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
c906108c 1224{
c906108c
SS
1225 /* We definitely need the .debug_info and .debug_abbrev sections */
1226
6502dd73
DJ
1227 dwarf2_per_objfile->info_buffer = dwarf2_read_section (objfile, dwarf_info_section);
1228 dwarf2_per_objfile->abbrev_buffer = dwarf2_read_section (objfile, dwarf_abbrev_section);
188dd5d6
DJ
1229
1230 if (dwarf_line_section)
6502dd73 1231 dwarf2_per_objfile->line_buffer = dwarf2_read_section (objfile, dwarf_line_section);
41ff2da1 1232 else
6502dd73 1233 dwarf2_per_objfile->line_buffer = NULL;
c906108c 1234
188dd5d6 1235 if (dwarf_str_section)
6502dd73 1236 dwarf2_per_objfile->str_buffer = dwarf2_read_section (objfile, dwarf_str_section);
4bdf3d34 1237 else
6502dd73 1238 dwarf2_per_objfile->str_buffer = NULL;
4bdf3d34 1239
188dd5d6 1240 if (dwarf_macinfo_section)
6502dd73 1241 dwarf2_per_objfile->macinfo_buffer = dwarf2_read_section (objfile,
086df311 1242 dwarf_macinfo_section);
2e276125 1243 else
6502dd73 1244 dwarf2_per_objfile->macinfo_buffer = NULL;
2e276125 1245
188dd5d6 1246 if (dwarf_ranges_section)
6502dd73 1247 dwarf2_per_objfile->ranges_buffer = dwarf2_read_section (objfile, dwarf_ranges_section);
af34e669 1248 else
6502dd73 1249 dwarf2_per_objfile->ranges_buffer = NULL;
af34e669 1250
188dd5d6 1251 if (dwarf_loc_section)
6502dd73 1252 dwarf2_per_objfile->loc_buffer = dwarf2_read_section (objfile, dwarf_loc_section);
0d53c4c4 1253 else
6502dd73 1254 dwarf2_per_objfile->loc_buffer = NULL;
0d53c4c4 1255
ef96bde8
EZ
1256 if (mainline
1257 || (objfile->global_psymbols.size == 0
1258 && objfile->static_psymbols.size == 0))
c906108c
SS
1259 {
1260 init_psymbol_list (objfile, 1024);
1261 }
1262
1263#if 0
1264 if (dwarf_aranges_offset && dwarf_pubnames_offset)
1265 {
d4f3574e 1266 /* Things are significantly easier if we have .debug_aranges and
c906108c
SS
1267 .debug_pubnames sections */
1268
d4f3574e 1269 dwarf2_build_psymtabs_easy (objfile, mainline);
c906108c
SS
1270 }
1271 else
1272#endif
1273 /* only test this case for now */
c5aa993b 1274 {
c906108c 1275 /* In this case we have to work a bit harder */
d4f3574e 1276 dwarf2_build_psymtabs_hard (objfile, mainline);
c906108c
SS
1277 }
1278}
1279
1280#if 0
1281/* Build the partial symbol table from the information in the
1282 .debug_pubnames and .debug_aranges sections. */
1283
1284static void
fba45db2 1285dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
c906108c
SS
1286{
1287 bfd *abfd = objfile->obfd;
1288 char *aranges_buffer, *pubnames_buffer;
1289 char *aranges_ptr, *pubnames_ptr;
1290 unsigned int entry_length, version, info_offset, info_size;
1291
1292 pubnames_buffer = dwarf2_read_section (objfile,
086df311 1293 dwarf_pubnames_section);
c906108c 1294 pubnames_ptr = pubnames_buffer;
6502dd73 1295 while ((pubnames_ptr - pubnames_buffer) < dwarf2_per_objfile->pubnames_size)
c906108c 1296 {
613e1657 1297 struct comp_unit_head cu_header;
891d2f0b 1298 unsigned int bytes_read;
613e1657
KB
1299
1300 entry_length = read_initial_length (abfd, pubnames_ptr, &cu_header,
891d2f0b 1301 &bytes_read);
613e1657 1302 pubnames_ptr += bytes_read;
c906108c
SS
1303 version = read_1_byte (abfd, pubnames_ptr);
1304 pubnames_ptr += 1;
1305 info_offset = read_4_bytes (abfd, pubnames_ptr);
1306 pubnames_ptr += 4;
1307 info_size = read_4_bytes (abfd, pubnames_ptr);
1308 pubnames_ptr += 4;
1309 }
1310
1311 aranges_buffer = dwarf2_read_section (objfile,
086df311 1312 dwarf_aranges_section);
c906108c
SS
1313
1314}
1315#endif
1316
107d2387 1317/* Read in the comp unit header information from the debug_info at
917c78fc 1318 info_ptr. */
107d2387 1319
fe1b8b76 1320static gdb_byte *
107d2387 1321read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 1322 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
1323{
1324 int signed_addr;
891d2f0b 1325 unsigned int bytes_read;
613e1657
KB
1326 cu_header->length = read_initial_length (abfd, info_ptr, cu_header,
1327 &bytes_read);
1328 info_ptr += bytes_read;
107d2387
AC
1329 cu_header->version = read_2_bytes (abfd, info_ptr);
1330 info_ptr += 2;
613e1657
KB
1331 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
1332 &bytes_read);
1333 info_ptr += bytes_read;
107d2387
AC
1334 cu_header->addr_size = read_1_byte (abfd, info_ptr);
1335 info_ptr += 1;
1336 signed_addr = bfd_get_sign_extend_vma (abfd);
1337 if (signed_addr < 0)
8e65ff28 1338 internal_error (__FILE__, __LINE__,
e2e0b3e5 1339 _("read_comp_unit_head: dwarf from non elf file"));
107d2387
AC
1340 cu_header->signed_addr_p = signed_addr;
1341 return info_ptr;
1342}
1343
fe1b8b76
JB
1344static gdb_byte *
1345partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
72bf9492
DJ
1346 bfd *abfd)
1347{
fe1b8b76 1348 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
1349
1350 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
1351
2b949cb6 1352 if (header->version != 2 && header->version != 3)
8a3fe4f8
AC
1353 error (_("Dwarf Error: wrong version in compilation unit header "
1354 "(is %d, should be %d) [in module %s]"), header->version,
72bf9492
DJ
1355 2, bfd_get_filename (abfd));
1356
1357 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev_size)
8a3fe4f8
AC
1358 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
1359 "(offset 0x%lx + 6) [in module %s]"),
72bf9492
DJ
1360 (long) header->abbrev_offset,
1361 (long) (beg_of_comp_unit - dwarf2_per_objfile->info_buffer),
1362 bfd_get_filename (abfd));
1363
1364 if (beg_of_comp_unit + header->length + header->initial_length_size
1365 > dwarf2_per_objfile->info_buffer + dwarf2_per_objfile->info_size)
8a3fe4f8
AC
1366 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
1367 "(offset 0x%lx + 0) [in module %s]"),
72bf9492
DJ
1368 (long) header->length,
1369 (long) (beg_of_comp_unit - dwarf2_per_objfile->info_buffer),
1370 bfd_get_filename (abfd));
1371
1372 return info_ptr;
1373}
1374
aaa75496
JB
1375/* Allocate a new partial symtab for file named NAME and mark this new
1376 partial symtab as being an include of PST. */
1377
1378static void
1379dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
1380 struct objfile *objfile)
1381{
1382 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
1383
1384 subpst->section_offsets = pst->section_offsets;
1385 subpst->textlow = 0;
1386 subpst->texthigh = 0;
1387
1388 subpst->dependencies = (struct partial_symtab **)
1389 obstack_alloc (&objfile->objfile_obstack,
1390 sizeof (struct partial_symtab *));
1391 subpst->dependencies[0] = pst;
1392 subpst->number_of_dependencies = 1;
1393
1394 subpst->globals_offset = 0;
1395 subpst->n_global_syms = 0;
1396 subpst->statics_offset = 0;
1397 subpst->n_static_syms = 0;
1398 subpst->symtab = NULL;
1399 subpst->read_symtab = pst->read_symtab;
1400 subpst->readin = 0;
1401
1402 /* No private part is necessary for include psymtabs. This property
1403 can be used to differentiate between such include psymtabs and
10b3939b 1404 the regular ones. */
58a9656e 1405 subpst->read_symtab_private = NULL;
aaa75496
JB
1406}
1407
1408/* Read the Line Number Program data and extract the list of files
1409 included by the source file represented by PST. Build an include
1410 partial symtab for each of these included files.
1411
1412 This procedure assumes that there *is* a Line Number Program in
1413 the given CU. Callers should check that PDI->HAS_STMT_LIST is set
1414 before calling this procedure. */
1415
1416static void
1417dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
1418 struct partial_die_info *pdi,
1419 struct partial_symtab *pst)
1420{
1421 struct objfile *objfile = cu->objfile;
1422 bfd *abfd = objfile->obfd;
1423 struct line_header *lh;
1424
1425 lh = dwarf_decode_line_header (pdi->line_offset, abfd, cu);
1426 if (lh == NULL)
1427 return; /* No linetable, so no includes. */
1428
1429 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
1430
1431 free_line_header (lh);
1432}
1433
1434
c906108c
SS
1435/* Build the partial symbol table by doing a quick pass through the
1436 .debug_info and .debug_abbrev sections. */
1437
1438static void
fba45db2 1439dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
c906108c
SS
1440{
1441 /* Instead of reading this into a big buffer, we should probably use
1442 mmap() on architectures that support it. (FIXME) */
1443 bfd *abfd = objfile->obfd;
fe1b8b76
JB
1444 gdb_byte *info_ptr;
1445 gdb_byte *beg_of_comp_unit;
c906108c
SS
1446 struct partial_die_info comp_unit_die;
1447 struct partial_symtab *pst;
ae038cb0 1448 struct cleanup *back_to;
e142c38c 1449 CORE_ADDR lowpc, highpc, baseaddr;
c906108c 1450
6502dd73 1451 info_ptr = dwarf2_per_objfile->info_buffer;
c906108c 1452
ae038cb0
DJ
1453 /* Any cached compilation units will be linked by the per-objfile
1454 read_in_chain. Make sure to free them when we're done. */
1455 back_to = make_cleanup (free_cached_comp_units, NULL);
1456
10b3939b
DJ
1457 create_all_comp_units (objfile);
1458
ff013f42
JK
1459 objfile->psymtabs_addrmap = addrmap_create_mutable
1460 (&objfile->objfile_obstack);
1461
6502dd73 1462 /* Since the objects we're extracting from .debug_info vary in
af703f96 1463 length, only the individual functions to extract them (like
72bf9492 1464 read_comp_unit_head and load_partial_die) can really know whether
af703f96
JB
1465 the buffer is large enough to hold another complete object.
1466
6502dd73
DJ
1467 At the moment, they don't actually check that. If .debug_info
1468 holds just one extra byte after the last compilation unit's dies,
1469 then read_comp_unit_head will happily read off the end of the
1470 buffer. read_partial_die is similarly casual. Those functions
1471 should be fixed.
af703f96
JB
1472
1473 For this loop condition, simply checking whether there's any data
1474 left at all should be sufficient. */
6502dd73
DJ
1475 while (info_ptr < (dwarf2_per_objfile->info_buffer
1476 + dwarf2_per_objfile->info_size))
c906108c 1477 {
f3dd6933 1478 struct cleanup *back_to_inner;
e7c27a73 1479 struct dwarf2_cu cu;
72bf9492
DJ
1480 struct abbrev_info *abbrev;
1481 unsigned int bytes_read;
1482 struct dwarf2_per_cu_data *this_cu;
1483
c906108c 1484 beg_of_comp_unit = info_ptr;
c906108c 1485
72bf9492
DJ
1486 memset (&cu, 0, sizeof (cu));
1487
1488 obstack_init (&cu.comp_unit_obstack);
1489
1490 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
1491
e7c27a73 1492 cu.objfile = objfile;
72bf9492 1493 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr, abfd);
e7c27a73 1494
57349743 1495 /* Complete the cu_header */
6502dd73 1496 cu.header.offset = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
d00adf39 1497 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
57349743 1498
e142c38c
DJ
1499 cu.list_in_scope = &file_symbols;
1500
c906108c 1501 /* Read the abbrevs for this compilation unit into a table */
e7c27a73 1502 dwarf2_read_abbrevs (abfd, &cu);
72bf9492 1503 make_cleanup (dwarf2_free_abbrev_table, &cu);
c906108c 1504
10b3939b 1505 this_cu = dwarf2_find_comp_unit (cu.header.offset, objfile);
ae038cb0 1506
c906108c 1507 /* Read the compilation unit die */
72bf9492
DJ
1508 abbrev = peek_die_abbrev (info_ptr, &bytes_read, &cu);
1509 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
1510 abfd, info_ptr, &cu);
c906108c 1511
31ffec48
DJ
1512 if (comp_unit_die.tag == DW_TAG_partial_unit)
1513 {
1514 info_ptr = (beg_of_comp_unit + cu.header.length
1515 + cu.header.initial_length_size);
1516 do_cleanups (back_to_inner);
1517 continue;
1518 }
1519
c906108c 1520 /* Set the language we're debugging */
e142c38c 1521 set_cu_language (comp_unit_die.language, &cu);
c906108c
SS
1522
1523 /* Allocate a new partial symbol table structure */
d4f3574e 1524 pst = start_psymtab_common (objfile, objfile->section_offsets,
96baa820 1525 comp_unit_die.name ? comp_unit_die.name : "",
ff013f42
JK
1526 /* TEXTLOW and TEXTHIGH are set below. */
1527 0,
c906108c
SS
1528 objfile->global_psymbols.next,
1529 objfile->static_psymbols.next);
1530
ae038cb0 1531 if (comp_unit_die.dirname)
a683b3c0
TT
1532 pst->dirname = obsavestring (comp_unit_die.dirname,
1533 strlen (comp_unit_die.dirname),
1534 &objfile->objfile_obstack);
57c22c6c 1535
10b3939b
DJ
1536 pst->read_symtab_private = (char *) this_cu;
1537
613e1657 1538 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1539
1540 /* Store the function that reads in the rest of the symbol table */
1541 pst->read_symtab = dwarf2_psymtab_to_symtab;
1542
10b3939b
DJ
1543 /* If this compilation unit was already read in, free the
1544 cached copy in order to read it in again. This is
1545 necessary because we skipped some symbols when we first
1546 read in the compilation unit (see load_partial_dies).
1547 This problem could be avoided, but the benefit is
1548 unclear. */
1549 if (this_cu->cu != NULL)
1550 free_one_cached_comp_unit (this_cu->cu);
ae038cb0 1551
10b3939b 1552 cu.per_cu = this_cu;
ae038cb0 1553
10b3939b
DJ
1554 /* Note that this is a pointer to our stack frame, being
1555 added to a global data structure. It will be cleaned up
1556 in free_stack_comp_unit when we finish with this
1557 compilation unit. */
1558 this_cu->cu = &cu;
ae038cb0 1559
10b3939b 1560 this_cu->psymtab = pst;
ae038cb0 1561
ff013f42
JK
1562 /* Possibly set the default values of LOWPC and HIGHPC from
1563 `DW_AT_ranges'. */
1564 if (cu.has_ranges_offset)
1565 {
1566 if (dwarf2_ranges_read (cu.ranges_offset, &comp_unit_die.lowpc,
1567 &comp_unit_die.highpc, &cu, pst))
1568 comp_unit_die.has_pc_info = 1;
1569 }
1570
c906108c
SS
1571 /* Check if comp unit has_children.
1572 If so, read the rest of the partial symbols from this comp unit.
1573 If not, there's no more debug_info for this comp unit. */
1574 if (comp_unit_die.has_children)
1575 {
72bf9492
DJ
1576 struct partial_die_info *first_die;
1577
91c24f0a
DC
1578 lowpc = ((CORE_ADDR) -1);
1579 highpc = ((CORE_ADDR) 0);
1580
72bf9492
DJ
1581 first_die = load_partial_dies (abfd, info_ptr, 1, &cu);
1582
1583 scan_partial_symbols (first_die, &lowpc, &highpc, &cu);
c906108c 1584
91c24f0a
DC
1585 /* If we didn't find a lowpc, set it to highpc to avoid
1586 complaints from `maint check'. */
1587 if (lowpc == ((CORE_ADDR) -1))
1588 lowpc = highpc;
72bf9492 1589
c906108c
SS
1590 /* If the compilation unit didn't have an explicit address range,
1591 then use the information extracted from its child dies. */
0b010bcc 1592 if (! comp_unit_die.has_pc_info)
c906108c 1593 {
c5aa993b 1594 comp_unit_die.lowpc = lowpc;
c906108c
SS
1595 comp_unit_die.highpc = highpc;
1596 }
1597 }
c5aa993b 1598 pst->textlow = comp_unit_die.lowpc + baseaddr;
c906108c
SS
1599 pst->texthigh = comp_unit_die.highpc + baseaddr;
1600
ff013f42
JK
1601 /* Store the contiguous range; `DW_AT_ranges' range is stored above. The
1602 range can be also empty for CUs with no code. */
1603 if (!cu.has_ranges_offset && pst->textlow < pst->texthigh)
1604 addrmap_set_empty (objfile->psymtabs_addrmap, pst->textlow,
1605 pst->texthigh - 1, pst);
1606
c906108c
SS
1607 pst->n_global_syms = objfile->global_psymbols.next -
1608 (objfile->global_psymbols.list + pst->globals_offset);
1609 pst->n_static_syms = objfile->static_psymbols.next -
1610 (objfile->static_psymbols.list + pst->statics_offset);
1611 sort_pst_symbols (pst);
1612
1613 /* If there is already a psymtab or symtab for a file of this
1614 name, remove it. (If there is a symtab, more drastic things
1615 also happen.) This happens in VxWorks. */
1616 free_named_symtabs (pst->filename);
1617
dd373385
EZ
1618 info_ptr = beg_of_comp_unit + cu.header.length
1619 + cu.header.initial_length_size;
1620
aaa75496
JB
1621 if (comp_unit_die.has_stmt_list)
1622 {
1623 /* Get the list of files included in the current compilation unit,
1624 and build a psymtab for each of them. */
1625 dwarf2_build_include_psymtabs (&cu, &comp_unit_die, pst);
1626 }
1627
f3dd6933 1628 do_cleanups (back_to_inner);
c906108c 1629 }
ff013f42
JK
1630
1631 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
1632 &objfile->objfile_obstack);
1633
ae038cb0
DJ
1634 do_cleanups (back_to);
1635}
1636
1637/* Load the DIEs for a secondary CU into memory. */
1638
1639static void
1640load_comp_unit (struct dwarf2_per_cu_data *this_cu, struct objfile *objfile)
1641{
1642 bfd *abfd = objfile->obfd;
fe1b8b76 1643 gdb_byte *info_ptr, *beg_of_comp_unit;
ae038cb0
DJ
1644 struct partial_die_info comp_unit_die;
1645 struct dwarf2_cu *cu;
1646 struct abbrev_info *abbrev;
1647 unsigned int bytes_read;
1648 struct cleanup *back_to;
1649
1650 info_ptr = dwarf2_per_objfile->info_buffer + this_cu->offset;
1651 beg_of_comp_unit = info_ptr;
1652
1653 cu = xmalloc (sizeof (struct dwarf2_cu));
1654 memset (cu, 0, sizeof (struct dwarf2_cu));
1655
1656 obstack_init (&cu->comp_unit_obstack);
1657
1658 cu->objfile = objfile;
1659 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr, abfd);
1660
1661 /* Complete the cu_header. */
1662 cu->header.offset = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
d00adf39 1663 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
ae038cb0
DJ
1664
1665 /* Read the abbrevs for this compilation unit into a table. */
1666 dwarf2_read_abbrevs (abfd, cu);
1667 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
1668
1669 /* Read the compilation unit die. */
1670 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
1671 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
1672 abfd, info_ptr, cu);
1673
1674 /* Set the language we're debugging. */
1675 set_cu_language (comp_unit_die.language, cu);
1676
1677 /* Link this compilation unit into the compilation unit tree. */
1678 this_cu->cu = cu;
1679 cu->per_cu = this_cu;
f792889a 1680 cu->type_hash = cu->per_cu->type_hash;
ae038cb0
DJ
1681
1682 /* Check if comp unit has_children.
1683 If so, read the rest of the partial symbols from this comp unit.
1684 If not, there's no more debug_info for this comp unit. */
1685 if (comp_unit_die.has_children)
1686 load_partial_dies (abfd, info_ptr, 0, cu);
1687
1688 do_cleanups (back_to);
1689}
1690
1691/* Create a list of all compilation units in OBJFILE. We do this only
1692 if an inter-comp-unit reference is found; presumably if there is one,
1693 there will be many, and one will occur early in the .debug_info section.
1694 So there's no point in building this list incrementally. */
1695
1696static void
1697create_all_comp_units (struct objfile *objfile)
1698{
1699 int n_allocated;
1700 int n_comp_units;
1701 struct dwarf2_per_cu_data **all_comp_units;
fe1b8b76 1702 gdb_byte *info_ptr = dwarf2_per_objfile->info_buffer;
ae038cb0
DJ
1703
1704 n_comp_units = 0;
1705 n_allocated = 10;
1706 all_comp_units = xmalloc (n_allocated
1707 * sizeof (struct dwarf2_per_cu_data *));
1708
1709 while (info_ptr < dwarf2_per_objfile->info_buffer + dwarf2_per_objfile->info_size)
1710 {
1711 struct comp_unit_head cu_header;
fe1b8b76 1712 gdb_byte *beg_of_comp_unit;
ae038cb0
DJ
1713 struct dwarf2_per_cu_data *this_cu;
1714 unsigned long offset;
891d2f0b 1715 unsigned int bytes_read;
ae038cb0
DJ
1716
1717 offset = info_ptr - dwarf2_per_objfile->info_buffer;
1718
1719 /* Read just enough information to find out where the next
1720 compilation unit is. */
dd373385 1721 cu_header.initial_length_size = 0;
ae038cb0
DJ
1722 cu_header.length = read_initial_length (objfile->obfd, info_ptr,
1723 &cu_header, &bytes_read);
1724
1725 /* Save the compilation unit for later lookup. */
1726 this_cu = obstack_alloc (&objfile->objfile_obstack,
1727 sizeof (struct dwarf2_per_cu_data));
1728 memset (this_cu, 0, sizeof (*this_cu));
1729 this_cu->offset = offset;
1730 this_cu->length = cu_header.length + cu_header.initial_length_size;
1731
1732 if (n_comp_units == n_allocated)
1733 {
1734 n_allocated *= 2;
1735 all_comp_units = xrealloc (all_comp_units,
1736 n_allocated
1737 * sizeof (struct dwarf2_per_cu_data *));
1738 }
1739 all_comp_units[n_comp_units++] = this_cu;
1740
1741 info_ptr = info_ptr + this_cu->length;
1742 }
1743
1744 dwarf2_per_objfile->all_comp_units
1745 = obstack_alloc (&objfile->objfile_obstack,
1746 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
1747 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
1748 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
1749 xfree (all_comp_units);
1750 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
1751}
1752
72bf9492
DJ
1753/* Process all loaded DIEs for compilation unit CU, starting at FIRST_DIE.
1754 Also set *LOWPC and *HIGHPC to the lowest and highest PC values found
1755 in CU. */
c906108c 1756
72bf9492
DJ
1757static void
1758scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
1759 CORE_ADDR *highpc, struct dwarf2_cu *cu)
c906108c 1760{
e7c27a73 1761 struct objfile *objfile = cu->objfile;
c906108c 1762 bfd *abfd = objfile->obfd;
72bf9492 1763 struct partial_die_info *pdi;
c906108c 1764
91c24f0a
DC
1765 /* Now, march along the PDI's, descending into ones which have
1766 interesting children but skipping the children of the other ones,
1767 until we reach the end of the compilation unit. */
c906108c 1768
72bf9492 1769 pdi = first_die;
91c24f0a 1770
72bf9492
DJ
1771 while (pdi != NULL)
1772 {
1773 fixup_partial_die (pdi, cu);
c906108c 1774
91c24f0a
DC
1775 /* Anonymous namespaces have no name but have interesting
1776 children, so we need to look at them. Ditto for anonymous
1777 enums. */
933c6fe4 1778
72bf9492
DJ
1779 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
1780 || pdi->tag == DW_TAG_enumeration_type)
c906108c 1781 {
72bf9492 1782 switch (pdi->tag)
c906108c
SS
1783 {
1784 case DW_TAG_subprogram:
bc30ff58 1785 add_partial_subprogram (pdi, lowpc, highpc, cu);
c906108c
SS
1786 break;
1787 case DW_TAG_variable:
1788 case DW_TAG_typedef:
91c24f0a 1789 case DW_TAG_union_type:
72bf9492 1790 if (!pdi->is_declaration)
63d06c5c 1791 {
72bf9492 1792 add_partial_symbol (pdi, cu);
63d06c5c
DC
1793 }
1794 break;
c906108c 1795 case DW_TAG_class_type:
680b30c7 1796 case DW_TAG_interface_type:
c906108c 1797 case DW_TAG_structure_type:
72bf9492 1798 if (!pdi->is_declaration)
c906108c 1799 {
72bf9492 1800 add_partial_symbol (pdi, cu);
c906108c
SS
1801 }
1802 break;
91c24f0a 1803 case DW_TAG_enumeration_type:
72bf9492
DJ
1804 if (!pdi->is_declaration)
1805 add_partial_enumeration (pdi, cu);
c906108c
SS
1806 break;
1807 case DW_TAG_base_type:
a02abb62 1808 case DW_TAG_subrange_type:
c906108c 1809 /* File scope base type definitions are added to the partial
c5aa993b 1810 symbol table. */
72bf9492 1811 add_partial_symbol (pdi, cu);
c906108c 1812 break;
d9fa45fe 1813 case DW_TAG_namespace:
72bf9492 1814 add_partial_namespace (pdi, lowpc, highpc, cu);
91c24f0a 1815 break;
c906108c
SS
1816 default:
1817 break;
1818 }
1819 }
1820
72bf9492
DJ
1821 /* If the die has a sibling, skip to the sibling. */
1822
1823 pdi = pdi->die_sibling;
1824 }
1825}
1826
1827/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 1828
72bf9492 1829 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
1830 name is concatenated with "::" and the partial DIE's name. For
1831 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
1832 Enumerators are an exception; they use the scope of their parent
1833 enumeration type, i.e. the name of the enumeration type is not
1834 prepended to the enumerator.
91c24f0a 1835
72bf9492
DJ
1836 There are two complexities. One is DW_AT_specification; in this
1837 case "parent" means the parent of the target of the specification,
1838 instead of the direct parent of the DIE. The other is compilers
1839 which do not emit DW_TAG_namespace; in this case we try to guess
1840 the fully qualified name of structure types from their members'
1841 linkage names. This must be done using the DIE's children rather
1842 than the children of any DW_AT_specification target. We only need
1843 to do this for structures at the top level, i.e. if the target of
1844 any DW_AT_specification (if any; otherwise the DIE itself) does not
1845 have a parent. */
1846
1847/* Compute the scope prefix associated with PDI's parent, in
1848 compilation unit CU. The result will be allocated on CU's
1849 comp_unit_obstack, or a copy of the already allocated PDI->NAME
1850 field. NULL is returned if no prefix is necessary. */
1851static char *
1852partial_die_parent_scope (struct partial_die_info *pdi,
1853 struct dwarf2_cu *cu)
1854{
1855 char *grandparent_scope;
1856 struct partial_die_info *parent, *real_pdi;
91c24f0a 1857
72bf9492
DJ
1858 /* We need to look at our parent DIE; if we have a DW_AT_specification,
1859 then this means the parent of the specification DIE. */
1860
1861 real_pdi = pdi;
72bf9492 1862 while (real_pdi->has_specification)
10b3939b 1863 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
1864
1865 parent = real_pdi->die_parent;
1866 if (parent == NULL)
1867 return NULL;
1868
1869 if (parent->scope_set)
1870 return parent->scope;
1871
1872 fixup_partial_die (parent, cu);
1873
10b3939b 1874 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492
DJ
1875
1876 if (parent->tag == DW_TAG_namespace
1877 || parent->tag == DW_TAG_structure_type
1878 || parent->tag == DW_TAG_class_type
680b30c7 1879 || parent->tag == DW_TAG_interface_type
72bf9492
DJ
1880 || parent->tag == DW_TAG_union_type)
1881 {
1882 if (grandparent_scope == NULL)
1883 parent->scope = parent->name;
1884 else
987504bb
JJ
1885 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
1886 parent->name, cu);
72bf9492
DJ
1887 }
1888 else if (parent->tag == DW_TAG_enumeration_type)
1889 /* Enumerators should not get the name of the enumeration as a prefix. */
1890 parent->scope = grandparent_scope;
1891 else
1892 {
1893 /* FIXME drow/2004-04-01: What should we be doing with
1894 function-local names? For partial symbols, we should probably be
1895 ignoring them. */
1896 complaint (&symfile_complaints,
e2e0b3e5 1897 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
1898 parent->tag, pdi->offset);
1899 parent->scope = grandparent_scope;
c906108c
SS
1900 }
1901
72bf9492
DJ
1902 parent->scope_set = 1;
1903 return parent->scope;
1904}
1905
1906/* Return the fully scoped name associated with PDI, from compilation unit
1907 CU. The result will be allocated with malloc. */
1908static char *
1909partial_die_full_name (struct partial_die_info *pdi,
1910 struct dwarf2_cu *cu)
1911{
1912 char *parent_scope;
1913
1914 parent_scope = partial_die_parent_scope (pdi, cu);
1915 if (parent_scope == NULL)
1916 return NULL;
1917 else
987504bb 1918 return typename_concat (NULL, parent_scope, pdi->name, cu);
c906108c
SS
1919}
1920
1921static void
72bf9492 1922add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 1923{
e7c27a73 1924 struct objfile *objfile = cu->objfile;
c906108c 1925 CORE_ADDR addr = 0;
decbce07 1926 char *actual_name = NULL;
72bf9492 1927 const char *my_prefix;
5c4e30ca 1928 const struct partial_symbol *psym = NULL;
e142c38c 1929 CORE_ADDR baseaddr;
72bf9492 1930 int built_actual_name = 0;
e142c38c
DJ
1931
1932 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 1933
72bf9492 1934 if (pdi_needs_namespace (pdi->tag))
63d06c5c 1935 {
72bf9492
DJ
1936 actual_name = partial_die_full_name (pdi, cu);
1937 if (actual_name)
1938 built_actual_name = 1;
63d06c5c
DC
1939 }
1940
72bf9492
DJ
1941 if (actual_name == NULL)
1942 actual_name = pdi->name;
1943
c906108c
SS
1944 switch (pdi->tag)
1945 {
1946 case DW_TAG_subprogram:
2cfa0c8d 1947 if (pdi->is_external || cu->language == language_ada)
c906108c 1948 {
2cfa0c8d
JB
1949 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
1950 of the global scope. But in Ada, we want to be able to access
1951 nested procedures globally. So all Ada subprograms are stored
1952 in the global scope. */
38d518c9 1953 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 1954 mst_text, objfile); */
38d518c9 1955 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
1956 VAR_DOMAIN, LOC_BLOCK,
1957 &objfile->global_psymbols,
1958 0, pdi->lowpc + baseaddr,
e142c38c 1959 cu->language, objfile);
c906108c
SS
1960 }
1961 else
1962 {
38d518c9 1963 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 1964 mst_file_text, objfile); */
38d518c9 1965 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
1966 VAR_DOMAIN, LOC_BLOCK,
1967 &objfile->static_psymbols,
1968 0, pdi->lowpc + baseaddr,
e142c38c 1969 cu->language, objfile);
c906108c
SS
1970 }
1971 break;
1972 case DW_TAG_variable:
1973 if (pdi->is_external)
1974 {
1975 /* Global Variable.
1976 Don't enter into the minimal symbol tables as there is
1977 a minimal symbol table entry from the ELF symbols already.
1978 Enter into partial symbol table if it has a location
1979 descriptor or a type.
1980 If the location descriptor is missing, new_symbol will create
1981 a LOC_UNRESOLVED symbol, the address of the variable will then
1982 be determined from the minimal symbol table whenever the variable
1983 is referenced.
1984 The address for the partial symbol table entry is not
1985 used by GDB, but it comes in handy for debugging partial symbol
1986 table building. */
1987
1988 if (pdi->locdesc)
e7c27a73 1989 addr = decode_locdesc (pdi->locdesc, cu);
c906108c 1990 if (pdi->locdesc || pdi->has_type)
38d518c9 1991 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
1992 VAR_DOMAIN, LOC_STATIC,
1993 &objfile->global_psymbols,
1994 0, addr + baseaddr,
e142c38c 1995 cu->language, objfile);
c906108c
SS
1996 }
1997 else
1998 {
1999 /* Static Variable. Skip symbols without location descriptors. */
2000 if (pdi->locdesc == NULL)
decbce07
MS
2001 {
2002 if (built_actual_name)
2003 xfree (actual_name);
2004 return;
2005 }
e7c27a73 2006 addr = decode_locdesc (pdi->locdesc, cu);
38d518c9 2007 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 2008 mst_file_data, objfile); */
38d518c9 2009 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2010 VAR_DOMAIN, LOC_STATIC,
2011 &objfile->static_psymbols,
2012 0, addr + baseaddr,
e142c38c 2013 cu->language, objfile);
c906108c
SS
2014 }
2015 break;
2016 case DW_TAG_typedef:
2017 case DW_TAG_base_type:
a02abb62 2018 case DW_TAG_subrange_type:
38d518c9 2019 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2020 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 2021 &objfile->static_psymbols,
e142c38c 2022 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 2023 break;
72bf9492
DJ
2024 case DW_TAG_namespace:
2025 add_psymbol_to_list (actual_name, strlen (actual_name),
2026 VAR_DOMAIN, LOC_TYPEDEF,
2027 &objfile->global_psymbols,
2028 0, (CORE_ADDR) 0, cu->language, objfile);
2029 break;
c906108c 2030 case DW_TAG_class_type:
680b30c7 2031 case DW_TAG_interface_type:
c906108c
SS
2032 case DW_TAG_structure_type:
2033 case DW_TAG_union_type:
2034 case DW_TAG_enumeration_type:
fa4028e9
JB
2035 /* Skip external references. The DWARF standard says in the section
2036 about "Structure, Union, and Class Type Entries": "An incomplete
2037 structure, union or class type is represented by a structure,
2038 union or class entry that does not have a byte size attribute
2039 and that has a DW_AT_declaration attribute." */
2040 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
2041 {
2042 if (built_actual_name)
2043 xfree (actual_name);
2044 return;
2045 }
fa4028e9 2046
63d06c5c
DC
2047 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
2048 static vs. global. */
38d518c9 2049 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2050 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
2051 (cu->language == language_cplus
2052 || cu->language == language_java)
63d06c5c
DC
2053 ? &objfile->global_psymbols
2054 : &objfile->static_psymbols,
e142c38c 2055 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 2056
c906108c
SS
2057 break;
2058 case DW_TAG_enumerator:
38d518c9 2059 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2060 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
2061 (cu->language == language_cplus
2062 || cu->language == language_java)
f6fe98ef
DJ
2063 ? &objfile->global_psymbols
2064 : &objfile->static_psymbols,
e142c38c 2065 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
2066 break;
2067 default:
2068 break;
2069 }
5c4e30ca
DC
2070
2071 /* Check to see if we should scan the name for possible namespace
2072 info. Only do this if this is C++, if we don't have namespace
2073 debugging info in the file, if the psym is of an appropriate type
2074 (otherwise we'll have psym == NULL), and if we actually had a
2075 mangled name to begin with. */
2076
72bf9492
DJ
2077 /* FIXME drow/2004-02-22: Why don't we do this for classes, i.e. the
2078 cases which do not set PSYM above? */
2079
e142c38c 2080 if (cu->language == language_cplus
72bf9492 2081 && cu->has_namespace_info == 0
5c4e30ca
DC
2082 && psym != NULL
2083 && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
2084 cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
2085 objfile);
72bf9492
DJ
2086
2087 if (built_actual_name)
2088 xfree (actual_name);
c906108c
SS
2089}
2090
72bf9492
DJ
2091/* Determine whether a die of type TAG living in a C++ class or
2092 namespace needs to have the name of the scope prepended to the
63d06c5c
DC
2093 name listed in the die. */
2094
2095static int
72bf9492 2096pdi_needs_namespace (enum dwarf_tag tag)
63d06c5c 2097{
63d06c5c
DC
2098 switch (tag)
2099 {
72bf9492 2100 case DW_TAG_namespace:
63d06c5c
DC
2101 case DW_TAG_typedef:
2102 case DW_TAG_class_type:
680b30c7 2103 case DW_TAG_interface_type:
63d06c5c
DC
2104 case DW_TAG_structure_type:
2105 case DW_TAG_union_type:
2106 case DW_TAG_enumeration_type:
2107 case DW_TAG_enumerator:
2108 return 1;
2109 default:
2110 return 0;
2111 }
2112}
2113
5c4e30ca
DC
2114/* Read a partial die corresponding to a namespace; also, add a symbol
2115 corresponding to that namespace to the symbol table. NAMESPACE is
2116 the name of the enclosing namespace. */
91c24f0a 2117
72bf9492
DJ
2118static void
2119add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 2120 CORE_ADDR *lowpc, CORE_ADDR *highpc,
72bf9492 2121 struct dwarf2_cu *cu)
91c24f0a 2122{
e7c27a73 2123 struct objfile *objfile = cu->objfile;
5c4e30ca 2124
72bf9492 2125 /* Add a symbol for the namespace. */
e7c27a73 2126
72bf9492 2127 add_partial_symbol (pdi, cu);
5c4e30ca
DC
2128
2129 /* Now scan partial symbols in that namespace. */
2130
91c24f0a 2131 if (pdi->has_children)
72bf9492 2132 scan_partial_symbols (pdi->die_child, lowpc, highpc, cu);
91c24f0a
DC
2133}
2134
bc30ff58
JB
2135/* Read a partial die corresponding to a subprogram and create a partial
2136 symbol for that subprogram. When the CU language allows it, this
2137 routine also defines a partial symbol for each nested subprogram
2138 that this subprogram contains.
2139
2140 DIE my also be a lexical block, in which case we simply search
2141 recursively for suprograms defined inside that lexical block.
2142 Again, this is only performed when the CU language allows this
2143 type of definitions. */
2144
2145static void
2146add_partial_subprogram (struct partial_die_info *pdi,
2147 CORE_ADDR *lowpc, CORE_ADDR *highpc,
2148 struct dwarf2_cu *cu)
2149{
2150 if (pdi->tag == DW_TAG_subprogram)
2151 {
2152 if (pdi->has_pc_info)
2153 {
2154 if (pdi->lowpc < *lowpc)
2155 *lowpc = pdi->lowpc;
2156 if (pdi->highpc > *highpc)
2157 *highpc = pdi->highpc;
2158 if (!pdi->is_declaration)
2159 add_partial_symbol (pdi, cu);
2160 }
2161 }
2162
2163 if (! pdi->has_children)
2164 return;
2165
2166 if (cu->language == language_ada)
2167 {
2168 pdi = pdi->die_child;
2169 while (pdi != NULL)
2170 {
2171 fixup_partial_die (pdi, cu);
2172 if (pdi->tag == DW_TAG_subprogram
2173 || pdi->tag == DW_TAG_lexical_block)
2174 add_partial_subprogram (pdi, lowpc, highpc, cu);
2175 pdi = pdi->die_sibling;
2176 }
2177 }
2178}
2179
72bf9492
DJ
2180/* See if we can figure out if the class lives in a namespace. We do
2181 this by looking for a member function; its demangled name will
2182 contain namespace info, if there is any. */
63d06c5c 2183
72bf9492
DJ
2184static void
2185guess_structure_name (struct partial_die_info *struct_pdi,
2186 struct dwarf2_cu *cu)
63d06c5c 2187{
987504bb
JJ
2188 if ((cu->language == language_cplus
2189 || cu->language == language_java)
72bf9492 2190 && cu->has_namespace_info == 0
63d06c5c
DC
2191 && struct_pdi->has_children)
2192 {
63d06c5c
DC
2193 /* NOTE: carlton/2003-10-07: Getting the info this way changes
2194 what template types look like, because the demangler
2195 frequently doesn't give the same name as the debug info. We
2196 could fix this by only using the demangled name to get the
134d01f1 2197 prefix (but see comment in read_structure_type). */
63d06c5c 2198
72bf9492
DJ
2199 struct partial_die_info *child_pdi = struct_pdi->die_child;
2200 struct partial_die_info *real_pdi;
5d51ca54 2201
72bf9492
DJ
2202 /* If this DIE (this DIE's specification, if any) has a parent, then
2203 we should not do this. We'll prepend the parent's fully qualified
2204 name when we create the partial symbol. */
5d51ca54 2205
72bf9492 2206 real_pdi = struct_pdi;
72bf9492 2207 while (real_pdi->has_specification)
10b3939b 2208 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 2209
72bf9492
DJ
2210 if (real_pdi->die_parent != NULL)
2211 return;
63d06c5c 2212
72bf9492
DJ
2213 while (child_pdi != NULL)
2214 {
2215 if (child_pdi->tag == DW_TAG_subprogram)
63d06c5c 2216 {
72bf9492 2217 char *actual_class_name
31c27f77
JJ
2218 = language_class_name_from_physname (cu->language_defn,
2219 child_pdi->name);
63d06c5c 2220 if (actual_class_name != NULL)
72bf9492
DJ
2221 {
2222 struct_pdi->name
2223 = obsavestring (actual_class_name,
2224 strlen (actual_class_name),
2225 &cu->comp_unit_obstack);
2226 xfree (actual_class_name);
2227 }
63d06c5c
DC
2228 break;
2229 }
72bf9492
DJ
2230
2231 child_pdi = child_pdi->die_sibling;
63d06c5c
DC
2232 }
2233 }
63d06c5c
DC
2234}
2235
91c24f0a
DC
2236/* Read a partial die corresponding to an enumeration type. */
2237
72bf9492
DJ
2238static void
2239add_partial_enumeration (struct partial_die_info *enum_pdi,
2240 struct dwarf2_cu *cu)
91c24f0a 2241{
e7c27a73 2242 struct objfile *objfile = cu->objfile;
91c24f0a 2243 bfd *abfd = objfile->obfd;
72bf9492 2244 struct partial_die_info *pdi;
91c24f0a
DC
2245
2246 if (enum_pdi->name != NULL)
72bf9492
DJ
2247 add_partial_symbol (enum_pdi, cu);
2248
2249 pdi = enum_pdi->die_child;
2250 while (pdi)
91c24f0a 2251 {
72bf9492 2252 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 2253 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 2254 else
72bf9492
DJ
2255 add_partial_symbol (pdi, cu);
2256 pdi = pdi->die_sibling;
91c24f0a 2257 }
91c24f0a
DC
2258}
2259
4bb7a0a7
DJ
2260/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
2261 Return the corresponding abbrev, or NULL if the number is zero (indicating
2262 an empty DIE). In either case *BYTES_READ will be set to the length of
2263 the initial number. */
2264
2265static struct abbrev_info *
fe1b8b76 2266peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 2267 struct dwarf2_cu *cu)
4bb7a0a7
DJ
2268{
2269 bfd *abfd = cu->objfile->obfd;
2270 unsigned int abbrev_number;
2271 struct abbrev_info *abbrev;
2272
2273 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
2274
2275 if (abbrev_number == 0)
2276 return NULL;
2277
2278 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
2279 if (!abbrev)
2280 {
8a3fe4f8 2281 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
2282 bfd_get_filename (abfd));
2283 }
2284
2285 return abbrev;
2286}
2287
2288/* Scan the debug information for CU starting at INFO_PTR. Returns a
2289 pointer to the end of a series of DIEs, terminated by an empty
2290 DIE. Any children of the skipped DIEs will also be skipped. */
2291
fe1b8b76
JB
2292static gdb_byte *
2293skip_children (gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
2294{
2295 struct abbrev_info *abbrev;
2296 unsigned int bytes_read;
2297
2298 while (1)
2299 {
2300 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
2301 if (abbrev == NULL)
2302 return info_ptr + bytes_read;
2303 else
2304 info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
2305 }
2306}
2307
2308/* Scan the debug information for CU starting at INFO_PTR. INFO_PTR
2309 should point just after the initial uleb128 of a DIE, and the
2310 abbrev corresponding to that skipped uleb128 should be passed in
2311 ABBREV. Returns a pointer to this DIE's sibling, skipping any
2312 children. */
2313
fe1b8b76
JB
2314static gdb_byte *
2315skip_one_die (gdb_byte *info_ptr, struct abbrev_info *abbrev,
4bb7a0a7
DJ
2316 struct dwarf2_cu *cu)
2317{
2318 unsigned int bytes_read;
2319 struct attribute attr;
2320 bfd *abfd = cu->objfile->obfd;
2321 unsigned int form, i;
2322
2323 for (i = 0; i < abbrev->num_attrs; i++)
2324 {
2325 /* The only abbrev we care about is DW_AT_sibling. */
2326 if (abbrev->attrs[i].name == DW_AT_sibling)
2327 {
2328 read_attribute (&attr, &abbrev->attrs[i],
2329 abfd, info_ptr, cu);
2330 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 2331 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 2332 else
6502dd73
DJ
2333 return dwarf2_per_objfile->info_buffer
2334 + dwarf2_get_ref_die_offset (&attr, cu);
4bb7a0a7
DJ
2335 }
2336
2337 /* If it isn't DW_AT_sibling, skip this attribute. */
2338 form = abbrev->attrs[i].form;
2339 skip_attribute:
2340 switch (form)
2341 {
2342 case DW_FORM_addr:
2343 case DW_FORM_ref_addr:
2344 info_ptr += cu->header.addr_size;
2345 break;
2346 case DW_FORM_data1:
2347 case DW_FORM_ref1:
2348 case DW_FORM_flag:
2349 info_ptr += 1;
2350 break;
2351 case DW_FORM_data2:
2352 case DW_FORM_ref2:
2353 info_ptr += 2;
2354 break;
2355 case DW_FORM_data4:
2356 case DW_FORM_ref4:
2357 info_ptr += 4;
2358 break;
2359 case DW_FORM_data8:
2360 case DW_FORM_ref8:
2361 info_ptr += 8;
2362 break;
2363 case DW_FORM_string:
2364 read_string (abfd, info_ptr, &bytes_read);
2365 info_ptr += bytes_read;
2366 break;
2367 case DW_FORM_strp:
2368 info_ptr += cu->header.offset_size;
2369 break;
2370 case DW_FORM_block:
2371 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2372 info_ptr += bytes_read;
2373 break;
2374 case DW_FORM_block1:
2375 info_ptr += 1 + read_1_byte (abfd, info_ptr);
2376 break;
2377 case DW_FORM_block2:
2378 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
2379 break;
2380 case DW_FORM_block4:
2381 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
2382 break;
2383 case DW_FORM_sdata:
2384 case DW_FORM_udata:
2385 case DW_FORM_ref_udata:
2386 info_ptr = skip_leb128 (abfd, info_ptr);
2387 break;
2388 case DW_FORM_indirect:
2389 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2390 info_ptr += bytes_read;
2391 /* We need to continue parsing from here, so just go back to
2392 the top. */
2393 goto skip_attribute;
2394
2395 default:
8a3fe4f8 2396 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
2397 dwarf_form_name (form),
2398 bfd_get_filename (abfd));
2399 }
2400 }
2401
2402 if (abbrev->has_children)
2403 return skip_children (info_ptr, cu);
2404 else
2405 return info_ptr;
2406}
2407
2408/* Locate ORIG_PDI's sibling; INFO_PTR should point to the start of
2409 the next DIE after ORIG_PDI. */
91c24f0a 2410
fe1b8b76
JB
2411static gdb_byte *
2412locate_pdi_sibling (struct partial_die_info *orig_pdi, gdb_byte *info_ptr,
e7c27a73 2413 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
2414{
2415 /* Do we know the sibling already? */
72bf9492 2416
91c24f0a
DC
2417 if (orig_pdi->sibling)
2418 return orig_pdi->sibling;
2419
2420 /* Are there any children to deal with? */
2421
2422 if (!orig_pdi->has_children)
2423 return info_ptr;
2424
4bb7a0a7 2425 /* Skip the children the long way. */
91c24f0a 2426
4bb7a0a7 2427 return skip_children (info_ptr, cu);
91c24f0a
DC
2428}
2429
c906108c
SS
2430/* Expand this partial symbol table into a full symbol table. */
2431
2432static void
fba45db2 2433dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c
SS
2434{
2435 /* FIXME: This is barely more than a stub. */
2436 if (pst != NULL)
2437 {
2438 if (pst->readin)
2439 {
8a3fe4f8 2440 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
2441 }
2442 else
2443 {
2444 if (info_verbose)
2445 {
a3f17187 2446 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
2447 gdb_flush (gdb_stdout);
2448 }
2449
10b3939b
DJ
2450 /* Restore our global data. */
2451 dwarf2_per_objfile = objfile_data (pst->objfile,
2452 dwarf2_objfile_data_key);
2453
c906108c
SS
2454 psymtab_to_symtab_1 (pst);
2455
2456 /* Finish up the debug error message. */
2457 if (info_verbose)
a3f17187 2458 printf_filtered (_("done.\n"));
c906108c
SS
2459 }
2460 }
2461}
2462
10b3939b
DJ
2463/* Add PER_CU to the queue. */
2464
2465static void
03dd20cc 2466queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
2467{
2468 struct dwarf2_queue_item *item;
2469
2470 per_cu->queued = 1;
2471 item = xmalloc (sizeof (*item));
2472 item->per_cu = per_cu;
2473 item->next = NULL;
2474
2475 if (dwarf2_queue == NULL)
2476 dwarf2_queue = item;
2477 else
2478 dwarf2_queue_tail->next = item;
2479
2480 dwarf2_queue_tail = item;
03dd20cc
DJ
2481
2482 /* Either PER_CU is the CU we want to process, or we're following a reference
2483 pointing into PER_CU. Either way, we need its DIEs now. */
2484 load_full_comp_unit (item->per_cu, objfile);
2485 item->per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
2486 dwarf2_per_objfile->read_in_chain = item->per_cu;
10b3939b
DJ
2487}
2488
2489/* Process the queue. */
2490
2491static void
2492process_queue (struct objfile *objfile)
2493{
2494 struct dwarf2_queue_item *item, *next_item;
2495
03dd20cc
DJ
2496 /* The queue starts out with one item, but following a DIE reference
2497 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
2498 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
2499 {
31ffec48 2500 if (item->per_cu->psymtab && !item->per_cu->psymtab->readin)
10b3939b
DJ
2501 process_full_comp_unit (item->per_cu);
2502
2503 item->per_cu->queued = 0;
2504 next_item = item->next;
2505 xfree (item);
2506 }
2507
2508 dwarf2_queue_tail = NULL;
2509}
2510
2511/* Free all allocated queue entries. This function only releases anything if
2512 an error was thrown; if the queue was processed then it would have been
2513 freed as we went along. */
2514
2515static void
2516dwarf2_release_queue (void *dummy)
2517{
2518 struct dwarf2_queue_item *item, *last;
2519
2520 item = dwarf2_queue;
2521 while (item)
2522 {
2523 /* Anything still marked queued is likely to be in an
2524 inconsistent state, so discard it. */
2525 if (item->per_cu->queued)
2526 {
2527 if (item->per_cu->cu != NULL)
2528 free_one_cached_comp_unit (item->per_cu->cu);
2529 item->per_cu->queued = 0;
2530 }
2531
2532 last = item;
2533 item = item->next;
2534 xfree (last);
2535 }
2536
2537 dwarf2_queue = dwarf2_queue_tail = NULL;
2538}
2539
2540/* Read in full symbols for PST, and anything it depends on. */
2541
c906108c 2542static void
fba45db2 2543psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 2544{
10b3939b 2545 struct dwarf2_per_cu_data *per_cu;
c906108c 2546 struct cleanup *back_to;
aaa75496
JB
2547 int i;
2548
2549 for (i = 0; i < pst->number_of_dependencies; i++)
2550 if (!pst->dependencies[i]->readin)
2551 {
2552 /* Inform about additional files that need to be read in. */
2553 if (info_verbose)
2554 {
a3f17187 2555 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
2556 fputs_filtered (" ", gdb_stdout);
2557 wrap_here ("");
2558 fputs_filtered ("and ", gdb_stdout);
2559 wrap_here ("");
2560 printf_filtered ("%s...", pst->dependencies[i]->filename);
2561 wrap_here (""); /* Flush output */
2562 gdb_flush (gdb_stdout);
2563 }
2564 psymtab_to_symtab_1 (pst->dependencies[i]);
2565 }
2566
10b3939b
DJ
2567 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
2568
2569 if (per_cu == NULL)
aaa75496
JB
2570 {
2571 /* It's an include file, no symbols to read for it.
2572 Everything is in the parent symtab. */
2573 pst->readin = 1;
2574 return;
2575 }
c906108c 2576
10b3939b
DJ
2577 back_to = make_cleanup (dwarf2_release_queue, NULL);
2578
03dd20cc 2579 queue_comp_unit (per_cu, pst->objfile);
10b3939b
DJ
2580
2581 process_queue (pst->objfile);
2582
2583 /* Age the cache, releasing compilation units that have not
2584 been used recently. */
2585 age_cached_comp_units ();
2586
2587 do_cleanups (back_to);
2588}
2589
2590/* Load the DIEs associated with PST and PER_CU into memory. */
2591
2592static struct dwarf2_cu *
31ffec48 2593load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 2594{
31ffec48 2595 bfd *abfd = objfile->obfd;
10b3939b
DJ
2596 struct dwarf2_cu *cu;
2597 unsigned long offset;
fe1b8b76 2598 gdb_byte *info_ptr;
10b3939b
DJ
2599 struct cleanup *back_to, *free_cu_cleanup;
2600 struct attribute *attr;
2601 CORE_ADDR baseaddr;
6502dd73 2602
c906108c 2603 /* Set local variables from the partial symbol table info. */
10b3939b 2604 offset = per_cu->offset;
6502dd73
DJ
2605
2606 info_ptr = dwarf2_per_objfile->info_buffer + offset;
63d06c5c 2607
10b3939b
DJ
2608 cu = xmalloc (sizeof (struct dwarf2_cu));
2609 memset (cu, 0, sizeof (struct dwarf2_cu));
c906108c 2610
10b3939b
DJ
2611 /* If an error occurs while loading, release our storage. */
2612 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 2613
31ffec48 2614 cu->objfile = objfile;
e7c27a73 2615
c906108c 2616 /* read in the comp_unit header */
10b3939b 2617 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c
SS
2618
2619 /* Read the abbrevs for this compilation unit */
10b3939b
DJ
2620 dwarf2_read_abbrevs (abfd, cu);
2621 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
2622
2623 cu->header.offset = offset;
c906108c 2624
10b3939b
DJ
2625 cu->per_cu = per_cu;
2626 per_cu->cu = cu;
f792889a 2627 cu->type_hash = per_cu->type_hash;
e142c38c 2628
10b3939b
DJ
2629 /* We use this obstack for block values in dwarf_alloc_block. */
2630 obstack_init (&cu->comp_unit_obstack);
2631
2632 cu->dies = read_comp_unit (info_ptr, abfd, cu);
2633
2634 /* We try not to read any attributes in this function, because not
2635 all objfiles needed for references have been loaded yet, and symbol
2636 table processing isn't initialized. But we have to set the CU language,
2637 or we won't be able to build types correctly. */
2638 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
2639 if (attr)
2640 set_cu_language (DW_UNSND (attr), cu);
2641 else
2642 set_cu_language (language_minimal, cu);
2643
2644 do_cleanups (back_to);
e142c38c 2645
10b3939b
DJ
2646 /* We've successfully allocated this compilation unit. Let our caller
2647 clean it up when finished with it. */
2648 discard_cleanups (free_cu_cleanup);
c906108c 2649
10b3939b
DJ
2650 return cu;
2651}
2652
2653/* Generate full symbol information for PST and CU, whose DIEs have
2654 already been loaded into memory. */
2655
2656static void
2657process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
2658{
2659 struct partial_symtab *pst = per_cu->psymtab;
2660 struct dwarf2_cu *cu = per_cu->cu;
2661 struct objfile *objfile = pst->objfile;
2662 bfd *abfd = objfile->obfd;
2663 CORE_ADDR lowpc, highpc;
2664 struct symtab *symtab;
2665 struct cleanup *back_to;
2666 struct attribute *attr;
2667 CORE_ADDR baseaddr;
2668
2669 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2670
10b3939b
DJ
2671 buildsym_init ();
2672 back_to = make_cleanup (really_free_pendings, NULL);
2673
2674 cu->list_in_scope = &file_symbols;
c906108c 2675
0d53c4c4
DJ
2676 /* Find the base address of the compilation unit for range lists and
2677 location lists. It will normally be specified by DW_AT_low_pc.
2678 In DWARF-3 draft 4, the base address could be overridden by
2679 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2680 compilation units with discontinuous ranges. */
2681
d00adf39
DE
2682 cu->base_known = 0;
2683 cu->base_address = 0;
0d53c4c4 2684
10b3939b 2685 attr = dwarf2_attr (cu->dies, DW_AT_entry_pc, cu);
0d53c4c4
DJ
2686 if (attr)
2687 {
d00adf39
DE
2688 cu->base_address = DW_ADDR (attr);
2689 cu->base_known = 1;
0d53c4c4
DJ
2690 }
2691 else
2692 {
10b3939b 2693 attr = dwarf2_attr (cu->dies, DW_AT_low_pc, cu);
0d53c4c4
DJ
2694 if (attr)
2695 {
d00adf39
DE
2696 cu->base_address = DW_ADDR (attr);
2697 cu->base_known = 1;
0d53c4c4
DJ
2698 }
2699 }
2700
c906108c 2701 /* Do line number decoding in read_file_scope () */
10b3939b 2702 process_die (cu->dies, cu);
c906108c 2703
fae299cd
DC
2704 /* Some compilers don't define a DW_AT_high_pc attribute for the
2705 compilation unit. If the DW_AT_high_pc is missing, synthesize
2706 it, by scanning the DIE's below the compilation unit. */
10b3939b 2707 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 2708
613e1657 2709 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
2710
2711 /* Set symtab language to language from DW_AT_language.
2712 If the compilation is from a C file generated by language preprocessors,
2713 do not set the language if it was already deduced by start_subfile. */
2714 if (symtab != NULL
10b3939b 2715 && !(cu->language == language_c && symtab->language != language_c))
c906108c 2716 {
10b3939b 2717 symtab->language = cu->language;
c906108c
SS
2718 }
2719 pst->symtab = symtab;
2720 pst->readin = 1;
c906108c
SS
2721
2722 do_cleanups (back_to);
2723}
2724
2725/* Process a die and its children. */
2726
2727static void
e7c27a73 2728process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
2729{
2730 switch (die->tag)
2731 {
2732 case DW_TAG_padding:
2733 break;
2734 case DW_TAG_compile_unit:
e7c27a73 2735 read_file_scope (die, cu);
c906108c
SS
2736 break;
2737 case DW_TAG_subprogram:
e7c27a73 2738 read_func_scope (die, cu);
c906108c
SS
2739 break;
2740 case DW_TAG_inlined_subroutine:
2741 /* FIXME: These are ignored for now.
c5aa993b
JM
2742 They could be used to set breakpoints on all inlined instances
2743 of a function and make GDB `next' properly over inlined functions. */
c906108c
SS
2744 break;
2745 case DW_TAG_lexical_block:
14898363
L
2746 case DW_TAG_try_block:
2747 case DW_TAG_catch_block:
e7c27a73 2748 read_lexical_block_scope (die, cu);
c906108c
SS
2749 break;
2750 case DW_TAG_class_type:
680b30c7 2751 case DW_TAG_interface_type:
c906108c
SS
2752 case DW_TAG_structure_type:
2753 case DW_TAG_union_type:
134d01f1 2754 process_structure_scope (die, cu);
c906108c
SS
2755 break;
2756 case DW_TAG_enumeration_type:
134d01f1 2757 process_enumeration_scope (die, cu);
c906108c 2758 break;
134d01f1 2759
f792889a
DJ
2760 /* These dies have a type, but processing them does not create
2761 a symbol or recurse to process the children. Therefore we can
2762 read them on-demand through read_type_die. */
c906108c 2763 case DW_TAG_subroutine_type:
72019c9c 2764 case DW_TAG_set_type:
c906108c 2765 case DW_TAG_array_type:
c906108c 2766 case DW_TAG_pointer_type:
c906108c 2767 case DW_TAG_ptr_to_member_type:
c906108c 2768 case DW_TAG_reference_type:
c906108c 2769 case DW_TAG_string_type:
c906108c 2770 break;
134d01f1 2771
c906108c 2772 case DW_TAG_base_type:
a02abb62 2773 case DW_TAG_subrange_type:
134d01f1
DJ
2774 /* Add a typedef symbol for the type definition, if it has a
2775 DW_AT_name. */
f792889a 2776 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 2777 break;
c906108c 2778 case DW_TAG_common_block:
e7c27a73 2779 read_common_block (die, cu);
c906108c
SS
2780 break;
2781 case DW_TAG_common_inclusion:
2782 break;
d9fa45fe 2783 case DW_TAG_namespace:
63d06c5c 2784 processing_has_namespace_info = 1;
e7c27a73 2785 read_namespace (die, cu);
d9fa45fe
DC
2786 break;
2787 case DW_TAG_imported_declaration:
2788 case DW_TAG_imported_module:
2789 /* FIXME: carlton/2002-10-16: Eventually, we should use the
2790 information contained in these. DW_TAG_imported_declaration
2791 dies shouldn't have children; DW_TAG_imported_module dies
2792 shouldn't in the C++ case, but conceivably could in the
d68209e4 2793 Fortran case. */
63d06c5c 2794 processing_has_namespace_info = 1;
d68209e4
JK
2795 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
2796 dwarf_tag_name (die->tag));
d9fa45fe 2797 break;
c906108c 2798 default:
e7c27a73 2799 new_symbol (die, NULL, cu);
c906108c
SS
2800 break;
2801 }
2802}
2803
0114d602
DJ
2804/* Return the fully qualified name of DIE, based on its DW_AT_name.
2805 If scope qualifiers are appropriate they will be added. The result
2806 will be allocated on the objfile_obstack, or NULL if the DIE does
2807 not have a name. */
2808
2809static const char *
2810dwarf2_full_name (struct die_info *die, struct dwarf2_cu *cu)
2811{
2812 struct attribute *attr;
2813 char *prefix, *name;
2814 struct ui_file *buf = NULL;
2815
2816 name = dwarf2_name (die, cu);
2817 if (!name)
2818 return NULL;
2819
2820 /* These are the only languages we know how to qualify names in. */
2821 if (cu->language != language_cplus
2822 && cu->language != language_java)
2823 return name;
2824
2825 /* If no prefix is necessary for this type of DIE, return the
2826 unqualified name. The other three tags listed could be handled
2827 in pdi_needs_namespace, but that requires broader changes. */
2828 if (!pdi_needs_namespace (die->tag)
2829 && die->tag != DW_TAG_subprogram
2830 && die->tag != DW_TAG_variable
2831 && die->tag != DW_TAG_member)
2832 return name;
2833
2834 prefix = determine_prefix (die, cu);
2835 if (*prefix != '\0')
2836 name = typename_concat (&cu->objfile->objfile_obstack, prefix,
2837 name, cu);
2838
2839 return name;
2840}
2841
5fb290d7 2842static void
e142c38c 2843initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 2844{
e142c38c 2845 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
2846}
2847
cb1df416
DJ
2848static void
2849free_cu_line_header (void *arg)
2850{
2851 struct dwarf2_cu *cu = arg;
2852
2853 free_line_header (cu->line_header);
2854 cu->line_header = NULL;
2855}
2856
c906108c 2857static void
e7c27a73 2858read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 2859{
e7c27a73
DJ
2860 struct objfile *objfile = cu->objfile;
2861 struct comp_unit_head *cu_header = &cu->header;
debd256d 2862 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 2863 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
2864 CORE_ADDR highpc = ((CORE_ADDR) 0);
2865 struct attribute *attr;
e1024ff1 2866 char *name = NULL;
c906108c
SS
2867 char *comp_dir = NULL;
2868 struct die_info *child_die;
2869 bfd *abfd = objfile->obfd;
debd256d 2870 struct line_header *line_header = 0;
e142c38c
DJ
2871 CORE_ADDR baseaddr;
2872
2873 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 2874
fae299cd 2875 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
2876
2877 /* If we didn't find a lowpc, set it to highpc to avoid complaints
2878 from finish_block. */
2acceee2 2879 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
2880 lowpc = highpc;
2881 lowpc += baseaddr;
2882 highpc += baseaddr;
2883
39cbfefa
DJ
2884 /* Find the filename. Do not use dwarf2_name here, since the filename
2885 is not a source language identifier. */
e142c38c 2886 attr = dwarf2_attr (die, DW_AT_name, cu);
c906108c
SS
2887 if (attr)
2888 {
2889 name = DW_STRING (attr);
2890 }
e1024ff1 2891
e142c38c 2892 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
c906108c 2893 if (attr)
e1024ff1
DJ
2894 comp_dir = DW_STRING (attr);
2895 else if (name != NULL && IS_ABSOLUTE_PATH (name))
c906108c 2896 {
e1024ff1
DJ
2897 comp_dir = ldirname (name);
2898 if (comp_dir != NULL)
2899 make_cleanup (xfree, comp_dir);
2900 }
2901 if (comp_dir != NULL)
2902 {
2903 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2904 directory, get rid of it. */
2905 char *cp = strchr (comp_dir, ':');
c906108c 2906
e1024ff1
DJ
2907 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2908 comp_dir = cp + 1;
c906108c
SS
2909 }
2910
e1024ff1
DJ
2911 if (name == NULL)
2912 name = "<unknown>";
2913
e142c38c 2914 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
2915 if (attr)
2916 {
e142c38c 2917 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
2918 }
2919
b0f35d58
DL
2920 attr = dwarf2_attr (die, DW_AT_producer, cu);
2921 if (attr)
2922 cu->producer = DW_STRING (attr);
303b6f5d 2923
c906108c
SS
2924 /* We assume that we're processing GCC output. */
2925 processing_gcc_compilation = 2;
c906108c 2926
df8a16a1
DJ
2927 processing_has_namespace_info = 0;
2928
c906108c
SS
2929 start_symtab (name, comp_dir, lowpc);
2930 record_debugformat ("DWARF 2");
303b6f5d 2931 record_producer (cu->producer);
c906108c 2932
e142c38c 2933 initialize_cu_func_list (cu);
c906108c 2934
cb1df416
DJ
2935 /* Decode line number information if present. We do this before
2936 processing child DIEs, so that the line header table is available
2937 for DW_AT_decl_file. */
e142c38c 2938 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
2939 if (attr)
2940 {
debd256d 2941 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 2942 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
2943 if (line_header)
2944 {
cb1df416
DJ
2945 cu->line_header = line_header;
2946 make_cleanup (free_cu_line_header, cu);
aaa75496 2947 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 2948 }
5fb290d7 2949 }
debd256d 2950
cb1df416
DJ
2951 /* Process all dies in compilation unit. */
2952 if (die->child != NULL)
2953 {
2954 child_die = die->child;
2955 while (child_die && child_die->tag)
2956 {
2957 process_die (child_die, cu);
2958 child_die = sibling_die (child_die);
2959 }
2960 }
2961
2e276125
JB
2962 /* Decode macro information, if present. Dwarf 2 macro information
2963 refers to information in the line number info statement program
2964 header, so we can only read it if we've read the header
2965 successfully. */
e142c38c 2966 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 2967 if (attr && line_header)
2e276125
JB
2968 {
2969 unsigned int macro_offset = DW_UNSND (attr);
2970 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 2971 comp_dir, abfd, cu);
2e276125 2972 }
debd256d 2973 do_cleanups (back_to);
5fb290d7
DJ
2974}
2975
2976static void
e142c38c
DJ
2977add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
2978 struct dwarf2_cu *cu)
5fb290d7
DJ
2979{
2980 struct function_range *thisfn;
2981
2982 thisfn = (struct function_range *)
7b5a2f43 2983 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
2984 thisfn->name = name;
2985 thisfn->lowpc = lowpc;
2986 thisfn->highpc = highpc;
2987 thisfn->seen_line = 0;
2988 thisfn->next = NULL;
2989
e142c38c
DJ
2990 if (cu->last_fn == NULL)
2991 cu->first_fn = thisfn;
5fb290d7 2992 else
e142c38c 2993 cu->last_fn->next = thisfn;
5fb290d7 2994
e142c38c 2995 cu->last_fn = thisfn;
c906108c
SS
2996}
2997
2998static void
e7c27a73 2999read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3000{
e7c27a73 3001 struct objfile *objfile = cu->objfile;
52f0bd74 3002 struct context_stack *new;
c906108c
SS
3003 CORE_ADDR lowpc;
3004 CORE_ADDR highpc;
3005 struct die_info *child_die;
3006 struct attribute *attr;
3007 char *name;
e142c38c 3008 CORE_ADDR baseaddr;
801e3a5b 3009 struct block *block;
c906108c 3010
e142c38c
DJ
3011 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3012
3013 name = dwarf2_linkage_name (die, cu);
c906108c
SS
3014
3015 /* Ignore functions with missing or empty names and functions with
3016 missing or invalid low and high pc attributes. */
e7c27a73 3017 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3018 return;
3019
3020 lowpc += baseaddr;
3021 highpc += baseaddr;
3022
5fb290d7 3023 /* Record the function range for dwarf_decode_lines. */
e142c38c 3024 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 3025
c906108c 3026 new = push_context (0, lowpc);
f792889a 3027 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 3028
4cecd739
DJ
3029 /* If there is a location expression for DW_AT_frame_base, record
3030 it. */
e142c38c 3031 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 3032 if (attr)
c034e007
AC
3033 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3034 expression is being recorded directly in the function's symbol
3035 and not in a separate frame-base object. I guess this hack is
3036 to avoid adding some sort of frame-base adjunct/annex to the
3037 function's symbol :-(. The problem with doing this is that it
3038 results in a function symbol with a location expression that
3039 has nothing to do with the location of the function, ouch! The
3040 relationship should be: a function's symbol has-a frame base; a
3041 frame-base has-a location expression. */
e7c27a73 3042 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 3043
e142c38c 3044 cu->list_in_scope = &local_symbols;
c906108c 3045
639d11d3 3046 if (die->child != NULL)
c906108c 3047 {
639d11d3 3048 child_die = die->child;
c906108c
SS
3049 while (child_die && child_die->tag)
3050 {
e7c27a73 3051 process_die (child_die, cu);
c906108c
SS
3052 child_die = sibling_die (child_die);
3053 }
3054 }
3055
3056 new = pop_context ();
3057 /* Make a block for the local symbols within. */
801e3a5b
JB
3058 block = finish_block (new->name, &local_symbols, new->old_blocks,
3059 lowpc, highpc, objfile);
3060
df8a16a1
DJ
3061 /* For C++, set the block's scope. */
3062 if (cu->language == language_cplus)
3063 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 3064 determine_prefix (die, cu),
df8a16a1
DJ
3065 processing_has_namespace_info);
3066
801e3a5b
JB
3067 /* If we have address ranges, record them. */
3068 dwarf2_record_block_ranges (die, block, baseaddr, cu);
208d8187
JB
3069
3070 /* In C++, we can have functions nested inside functions (e.g., when
3071 a function declares a class that has methods). This means that
3072 when we finish processing a function scope, we may need to go
3073 back to building a containing block's symbol lists. */
3074 local_symbols = new->locals;
3075 param_symbols = new->params;
3076
921e78cf
JB
3077 /* If we've finished processing a top-level function, subsequent
3078 symbols go in the file symbol list. */
3079 if (outermost_context_p ())
e142c38c 3080 cu->list_in_scope = &file_symbols;
c906108c
SS
3081}
3082
3083/* Process all the DIES contained within a lexical block scope. Start
3084 a new scope, process the dies, and then close the scope. */
3085
3086static void
e7c27a73 3087read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3088{
e7c27a73 3089 struct objfile *objfile = cu->objfile;
52f0bd74 3090 struct context_stack *new;
c906108c
SS
3091 CORE_ADDR lowpc, highpc;
3092 struct die_info *child_die;
e142c38c
DJ
3093 CORE_ADDR baseaddr;
3094
3095 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
3096
3097 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
3098 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
3099 as multiple lexical blocks? Handling children in a sane way would
3100 be nasty. Might be easier to properly extend generic blocks to
3101 describe ranges. */
e7c27a73 3102 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3103 return;
3104 lowpc += baseaddr;
3105 highpc += baseaddr;
3106
3107 push_context (0, lowpc);
639d11d3 3108 if (die->child != NULL)
c906108c 3109 {
639d11d3 3110 child_die = die->child;
c906108c
SS
3111 while (child_die && child_die->tag)
3112 {
e7c27a73 3113 process_die (child_die, cu);
c906108c
SS
3114 child_die = sibling_die (child_die);
3115 }
3116 }
3117 new = pop_context ();
3118
3119 if (local_symbols != NULL)
3120 {
801e3a5b
JB
3121 struct block *block
3122 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
3123 highpc, objfile);
3124
3125 /* Note that recording ranges after traversing children, as we
3126 do here, means that recording a parent's ranges entails
3127 walking across all its children's ranges as they appear in
3128 the address map, which is quadratic behavior.
3129
3130 It would be nicer to record the parent's ranges before
3131 traversing its children, simply overriding whatever you find
3132 there. But since we don't even decide whether to create a
3133 block until after we've traversed its children, that's hard
3134 to do. */
3135 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
3136 }
3137 local_symbols = new->locals;
3138}
3139
43039443 3140/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
3141 Return 1 if the attributes are present and valid, otherwise, return 0.
3142 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
3143
3144static int
3145dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
3146 CORE_ADDR *high_return, struct dwarf2_cu *cu,
3147 struct partial_symtab *ranges_pst)
43039443
JK
3148{
3149 struct objfile *objfile = cu->objfile;
3150 struct comp_unit_head *cu_header = &cu->header;
3151 bfd *obfd = objfile->obfd;
3152 unsigned int addr_size = cu_header->addr_size;
3153 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3154 /* Base address selection entry. */
3155 CORE_ADDR base;
3156 int found_base;
3157 unsigned int dummy;
3158 gdb_byte *buffer;
3159 CORE_ADDR marker;
3160 int low_set;
3161 CORE_ADDR low = 0;
3162 CORE_ADDR high = 0;
ff013f42 3163 CORE_ADDR baseaddr;
43039443 3164
d00adf39
DE
3165 found_base = cu->base_known;
3166 base = cu->base_address;
43039443
JK
3167
3168 if (offset >= dwarf2_per_objfile->ranges_size)
3169 {
3170 complaint (&symfile_complaints,
3171 _("Offset %d out of bounds for DW_AT_ranges attribute"),
3172 offset);
3173 return 0;
3174 }
3175 buffer = dwarf2_per_objfile->ranges_buffer + offset;
3176
3177 /* Read in the largest possible address. */
3178 marker = read_address (obfd, buffer, cu, &dummy);
3179 if ((marker & mask) == mask)
3180 {
3181 /* If we found the largest possible address, then
3182 read the base address. */
3183 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3184 buffer += 2 * addr_size;
3185 offset += 2 * addr_size;
3186 found_base = 1;
3187 }
3188
3189 low_set = 0;
3190
e7030f15 3191 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 3192
43039443
JK
3193 while (1)
3194 {
3195 CORE_ADDR range_beginning, range_end;
3196
3197 range_beginning = read_address (obfd, buffer, cu, &dummy);
3198 buffer += addr_size;
3199 range_end = read_address (obfd, buffer, cu, &dummy);
3200 buffer += addr_size;
3201 offset += 2 * addr_size;
3202
3203 /* An end of list marker is a pair of zero addresses. */
3204 if (range_beginning == 0 && range_end == 0)
3205 /* Found the end of list entry. */
3206 break;
3207
3208 /* Each base address selection entry is a pair of 2 values.
3209 The first is the largest possible address, the second is
3210 the base address. Check for a base address here. */
3211 if ((range_beginning & mask) == mask)
3212 {
3213 /* If we found the largest possible address, then
3214 read the base address. */
3215 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3216 found_base = 1;
3217 continue;
3218 }
3219
3220 if (!found_base)
3221 {
3222 /* We have no valid base address for the ranges
3223 data. */
3224 complaint (&symfile_complaints,
3225 _("Invalid .debug_ranges data (no base address)"));
3226 return 0;
3227 }
3228
3229 range_beginning += base;
3230 range_end += base;
3231
ff013f42
JK
3232 if (ranges_pst != NULL && range_beginning < range_end)
3233 addrmap_set_empty (objfile->psymtabs_addrmap,
3234 range_beginning + baseaddr, range_end - 1 + baseaddr,
3235 ranges_pst);
3236
43039443
JK
3237 /* FIXME: This is recording everything as a low-high
3238 segment of consecutive addresses. We should have a
3239 data structure for discontiguous block ranges
3240 instead. */
3241 if (! low_set)
3242 {
3243 low = range_beginning;
3244 high = range_end;
3245 low_set = 1;
3246 }
3247 else
3248 {
3249 if (range_beginning < low)
3250 low = range_beginning;
3251 if (range_end > high)
3252 high = range_end;
3253 }
3254 }
3255
3256 if (! low_set)
3257 /* If the first entry is an end-of-list marker, the range
3258 describes an empty scope, i.e. no instructions. */
3259 return 0;
3260
3261 if (low_return)
3262 *low_return = low;
3263 if (high_return)
3264 *high_return = high;
3265 return 1;
3266}
3267
af34e669
DJ
3268/* Get low and high pc attributes from a die. Return 1 if the attributes
3269 are present and valid, otherwise, return 0. Return -1 if the range is
3270 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 3271static int
af34e669 3272dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
e7c27a73 3273 CORE_ADDR *highpc, struct dwarf2_cu *cu)
c906108c
SS
3274{
3275 struct attribute *attr;
af34e669
DJ
3276 CORE_ADDR low = 0;
3277 CORE_ADDR high = 0;
3278 int ret = 0;
c906108c 3279
e142c38c 3280 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 3281 if (attr)
af34e669
DJ
3282 {
3283 high = DW_ADDR (attr);
e142c38c 3284 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
3285 if (attr)
3286 low = DW_ADDR (attr);
3287 else
3288 /* Found high w/o low attribute. */
3289 return 0;
3290
3291 /* Found consecutive range of addresses. */
3292 ret = 1;
3293 }
c906108c 3294 else
af34e669 3295 {
e142c38c 3296 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
3297 if (attr != NULL)
3298 {
af34e669 3299 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 3300 .debug_ranges section. */
ff013f42 3301 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, NULL))
af34e669 3302 return 0;
43039443 3303 /* Found discontinuous range of addresses. */
af34e669
DJ
3304 ret = -1;
3305 }
3306 }
c906108c
SS
3307
3308 if (high < low)
3309 return 0;
3310
3311 /* When using the GNU linker, .gnu.linkonce. sections are used to
3312 eliminate duplicate copies of functions and vtables and such.
3313 The linker will arbitrarily choose one and discard the others.
3314 The AT_*_pc values for such functions refer to local labels in
3315 these sections. If the section from that file was discarded, the
3316 labels are not in the output, so the relocs get a value of 0.
3317 If this is a discarded function, mark the pc bounds as invalid,
3318 so that GDB will ignore it. */
72dca2f5 3319 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
3320 return 0;
3321
3322 *lowpc = low;
3323 *highpc = high;
af34e669 3324 return ret;
c906108c
SS
3325}
3326
fae299cd
DC
3327/* Get the low and high pc's represented by the scope DIE, and store
3328 them in *LOWPC and *HIGHPC. If the correct values can't be
3329 determined, set *LOWPC to -1 and *HIGHPC to 0. */
3330
3331static void
3332get_scope_pc_bounds (struct die_info *die,
3333 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3334 struct dwarf2_cu *cu)
3335{
3336 CORE_ADDR best_low = (CORE_ADDR) -1;
3337 CORE_ADDR best_high = (CORE_ADDR) 0;
3338 CORE_ADDR current_low, current_high;
3339
3340 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu))
3341 {
3342 best_low = current_low;
3343 best_high = current_high;
3344 }
3345 else
3346 {
3347 struct die_info *child = die->child;
3348
3349 while (child && child->tag)
3350 {
3351 switch (child->tag) {
3352 case DW_TAG_subprogram:
3353 if (dwarf2_get_pc_bounds (child, &current_low, &current_high, cu))
3354 {
3355 best_low = min (best_low, current_low);
3356 best_high = max (best_high, current_high);
3357 }
3358 break;
3359 case DW_TAG_namespace:
3360 /* FIXME: carlton/2004-01-16: Should we do this for
3361 DW_TAG_class_type/DW_TAG_structure_type, too? I think
3362 that current GCC's always emit the DIEs corresponding
3363 to definitions of methods of classes as children of a
3364 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
3365 the DIEs giving the declarations, which could be
3366 anywhere). But I don't see any reason why the
3367 standards says that they have to be there. */
3368 get_scope_pc_bounds (child, &current_low, &current_high, cu);
3369
3370 if (current_low != ((CORE_ADDR) -1))
3371 {
3372 best_low = min (best_low, current_low);
3373 best_high = max (best_high, current_high);
3374 }
3375 break;
3376 default:
3377 /* Ignore. */
3378 break;
3379 }
3380
3381 child = sibling_die (child);
3382 }
3383 }
3384
3385 *lowpc = best_low;
3386 *highpc = best_high;
3387}
3388
801e3a5b
JB
3389/* Record the address ranges for BLOCK, offset by BASEADDR, as given
3390 in DIE. */
3391static void
3392dwarf2_record_block_ranges (struct die_info *die, struct block *block,
3393 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
3394{
3395 struct attribute *attr;
3396
3397 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
3398 if (attr)
3399 {
3400 CORE_ADDR high = DW_ADDR (attr);
3401 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3402 if (attr)
3403 {
3404 CORE_ADDR low = DW_ADDR (attr);
3405 record_block_range (block, baseaddr + low, baseaddr + high - 1);
3406 }
3407 }
3408
3409 attr = dwarf2_attr (die, DW_AT_ranges, cu);
3410 if (attr)
3411 {
3412 bfd *obfd = cu->objfile->obfd;
3413
3414 /* The value of the DW_AT_ranges attribute is the offset of the
3415 address range list in the .debug_ranges section. */
3416 unsigned long offset = DW_UNSND (attr);
3417 gdb_byte *buffer = dwarf2_per_objfile->ranges_buffer + offset;
3418
3419 /* For some target architectures, but not others, the
3420 read_address function sign-extends the addresses it returns.
3421 To recognize base address selection entries, we need a
3422 mask. */
3423 unsigned int addr_size = cu->header.addr_size;
3424 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3425
3426 /* The base address, to which the next pair is relative. Note
3427 that this 'base' is a DWARF concept: most entries in a range
3428 list are relative, to reduce the number of relocs against the
3429 debugging information. This is separate from this function's
3430 'baseaddr' argument, which GDB uses to relocate debugging
3431 information from a shared library based on the address at
3432 which the library was loaded. */
d00adf39
DE
3433 CORE_ADDR base = cu->base_address;
3434 int base_known = cu->base_known;
801e3a5b
JB
3435
3436 if (offset >= dwarf2_per_objfile->ranges_size)
3437 {
3438 complaint (&symfile_complaints,
3439 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
3440 offset);
3441 return;
3442 }
3443
3444 for (;;)
3445 {
3446 unsigned int bytes_read;
3447 CORE_ADDR start, end;
3448
3449 start = read_address (obfd, buffer, cu, &bytes_read);
3450 buffer += bytes_read;
3451 end = read_address (obfd, buffer, cu, &bytes_read);
3452 buffer += bytes_read;
3453
3454 /* Did we find the end of the range list? */
3455 if (start == 0 && end == 0)
3456 break;
3457
3458 /* Did we find a base address selection entry? */
3459 else if ((start & base_select_mask) == base_select_mask)
3460 {
3461 base = end;
3462 base_known = 1;
3463 }
3464
3465 /* We found an ordinary address range. */
3466 else
3467 {
3468 if (!base_known)
3469 {
3470 complaint (&symfile_complaints,
3471 _("Invalid .debug_ranges data (no base address)"));
3472 return;
3473 }
3474
3475 record_block_range (block,
3476 baseaddr + base + start,
3477 baseaddr + base + end - 1);
3478 }
3479 }
3480 }
3481}
3482
c906108c
SS
3483/* Add an aggregate field to the field list. */
3484
3485static void
107d2387 3486dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73
DJ
3487 struct dwarf2_cu *cu)
3488{
3489 struct objfile *objfile = cu->objfile;
5e2b427d 3490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
3491 struct nextfield *new_field;
3492 struct attribute *attr;
3493 struct field *fp;
3494 char *fieldname = "";
3495
3496 /* Allocate a new field list entry and link it in. */
3497 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 3498 make_cleanup (xfree, new_field);
c906108c
SS
3499 memset (new_field, 0, sizeof (struct nextfield));
3500 new_field->next = fip->fields;
3501 fip->fields = new_field;
3502 fip->nfields++;
3503
3504 /* Handle accessibility and virtuality of field.
3505 The default accessibility for members is public, the default
3506 accessibility for inheritance is private. */
3507 if (die->tag != DW_TAG_inheritance)
3508 new_field->accessibility = DW_ACCESS_public;
3509 else
3510 new_field->accessibility = DW_ACCESS_private;
3511 new_field->virtuality = DW_VIRTUALITY_none;
3512
e142c38c 3513 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
3514 if (attr)
3515 new_field->accessibility = DW_UNSND (attr);
3516 if (new_field->accessibility != DW_ACCESS_public)
3517 fip->non_public_fields = 1;
e142c38c 3518 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
3519 if (attr)
3520 new_field->virtuality = DW_UNSND (attr);
3521
3522 fp = &new_field->field;
a9a9bd0f 3523
e142c38c 3524 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 3525 {
a9a9bd0f
DC
3526 /* Data member other than a C++ static data member. */
3527
c906108c 3528 /* Get type of field. */
e7c27a73 3529 fp->type = die_type (die, cu);
c906108c 3530
01ad7f36
DJ
3531 FIELD_STATIC_KIND (*fp) = 0;
3532
c906108c 3533 /* Get bit size of field (zero if none). */
e142c38c 3534 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
3535 if (attr)
3536 {
3537 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
3538 }
3539 else
3540 {
3541 FIELD_BITSIZE (*fp) = 0;
3542 }
3543
3544 /* Get bit offset of field. */
e142c38c 3545 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
3546 if (attr)
3547 {
c6a0999f
JB
3548 int byte_offset;
3549
3690dd37
JB
3550 if (attr_form_is_section_offset (attr))
3551 {
3552 dwarf2_complex_location_expr_complaint ();
c6a0999f 3553 byte_offset = 0;
3690dd37
JB
3554 }
3555 else if (attr_form_is_constant (attr))
c6a0999f 3556 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
3690dd37 3557 else
c6a0999f
JB
3558 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
3559
3560 FIELD_BITPOS (*fp) = byte_offset * bits_per_byte;
c906108c
SS
3561 }
3562 else
3563 FIELD_BITPOS (*fp) = 0;
e142c38c 3564 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
3565 if (attr)
3566 {
5e2b427d 3567 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
3568 {
3569 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
3570 additional bit offset from the MSB of the containing
3571 anonymous object to the MSB of the field. We don't
3572 have to do anything special since we don't need to
3573 know the size of the anonymous object. */
c906108c
SS
3574 FIELD_BITPOS (*fp) += DW_UNSND (attr);
3575 }
3576 else
3577 {
3578 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
3579 MSB of the anonymous object, subtract off the number of
3580 bits from the MSB of the field to the MSB of the
3581 object, and then subtract off the number of bits of
3582 the field itself. The result is the bit offset of
3583 the LSB of the field. */
c906108c
SS
3584 int anonymous_size;
3585 int bit_offset = DW_UNSND (attr);
3586
e142c38c 3587 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
3588 if (attr)
3589 {
3590 /* The size of the anonymous object containing
3591 the bit field is explicit, so use the
3592 indicated size (in bytes). */
3593 anonymous_size = DW_UNSND (attr);
3594 }
3595 else
3596 {
3597 /* The size of the anonymous object containing
3598 the bit field must be inferred from the type
3599 attribute of the data member containing the
3600 bit field. */
3601 anonymous_size = TYPE_LENGTH (fp->type);
3602 }
3603 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
3604 - bit_offset - FIELD_BITSIZE (*fp);
3605 }
3606 }
3607
3608 /* Get name of field. */
39cbfefa
DJ
3609 fieldname = dwarf2_name (die, cu);
3610 if (fieldname == NULL)
3611 fieldname = "";
d8151005
DJ
3612
3613 /* The name is already allocated along with this objfile, so we don't
3614 need to duplicate it for the type. */
3615 fp->name = fieldname;
c906108c
SS
3616
3617 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 3618 pointer or virtual base class pointer) to private. */
e142c38c 3619 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c
SS
3620 {
3621 new_field->accessibility = DW_ACCESS_private;
3622 fip->non_public_fields = 1;
3623 }
3624 }
a9a9bd0f 3625 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 3626 {
a9a9bd0f
DC
3627 /* C++ static member. */
3628
3629 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
3630 is a declaration, but all versions of G++ as of this writing
3631 (so through at least 3.2.1) incorrectly generate
3632 DW_TAG_variable tags. */
3633
c906108c 3634 char *physname;
c906108c 3635
a9a9bd0f 3636 /* Get name of field. */
39cbfefa
DJ
3637 fieldname = dwarf2_name (die, cu);
3638 if (fieldname == NULL)
c906108c
SS
3639 return;
3640
2df3850c 3641 /* Get physical name. */
e142c38c 3642 physname = dwarf2_linkage_name (die, cu);
c906108c 3643
d8151005
DJ
3644 /* The name is already allocated along with this objfile, so we don't
3645 need to duplicate it for the type. */
3646 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 3647 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 3648 FIELD_NAME (*fp) = fieldname;
c906108c
SS
3649 }
3650 else if (die->tag == DW_TAG_inheritance)
3651 {
3652 /* C++ base class field. */
e142c38c 3653 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 3654 if (attr)
e7c27a73 3655 FIELD_BITPOS (*fp) = (decode_locdesc (DW_BLOCK (attr), cu)
107d2387 3656 * bits_per_byte);
c906108c 3657 FIELD_BITSIZE (*fp) = 0;
01ad7f36 3658 FIELD_STATIC_KIND (*fp) = 0;
e7c27a73 3659 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
3660 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
3661 fip->nbaseclasses++;
3662 }
3663}
3664
3665/* Create the vector of fields, and attach it to the type. */
3666
3667static void
fba45db2 3668dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 3669 struct dwarf2_cu *cu)
c906108c
SS
3670{
3671 int nfields = fip->nfields;
3672
3673 /* Record the field count, allocate space for the array of fields,
3674 and create blank accessibility bitfields if necessary. */
3675 TYPE_NFIELDS (type) = nfields;
3676 TYPE_FIELDS (type) = (struct field *)
3677 TYPE_ALLOC (type, sizeof (struct field) * nfields);
3678 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
3679
3680 if (fip->non_public_fields)
3681 {
3682 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3683
3684 TYPE_FIELD_PRIVATE_BITS (type) =
3685 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3686 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
3687
3688 TYPE_FIELD_PROTECTED_BITS (type) =
3689 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3690 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
3691
3692 TYPE_FIELD_IGNORE_BITS (type) =
3693 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3694 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
3695 }
3696
3697 /* If the type has baseclasses, allocate and clear a bit vector for
3698 TYPE_FIELD_VIRTUAL_BITS. */
3699 if (fip->nbaseclasses)
3700 {
3701 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 3702 unsigned char *pointer;
c906108c
SS
3703
3704 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
3705 pointer = TYPE_ALLOC (type, num_bytes);
3706 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
3707 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
3708 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
3709 }
3710
3711 /* Copy the saved-up fields into the field vector. Start from the head
3712 of the list, adding to the tail of the field array, so that they end
3713 up in the same order in the array in which they were added to the list. */
3714 while (nfields-- > 0)
3715 {
3716 TYPE_FIELD (type, nfields) = fip->fields->field;
3717 switch (fip->fields->accessibility)
3718 {
c5aa993b
JM
3719 case DW_ACCESS_private:
3720 SET_TYPE_FIELD_PRIVATE (type, nfields);
3721 break;
c906108c 3722
c5aa993b
JM
3723 case DW_ACCESS_protected:
3724 SET_TYPE_FIELD_PROTECTED (type, nfields);
3725 break;
c906108c 3726
c5aa993b
JM
3727 case DW_ACCESS_public:
3728 break;
c906108c 3729
c5aa993b
JM
3730 default:
3731 /* Unknown accessibility. Complain and treat it as public. */
3732 {
e2e0b3e5 3733 complaint (&symfile_complaints, _("unsupported accessibility %d"),
4d3c2250 3734 fip->fields->accessibility);
c5aa993b
JM
3735 }
3736 break;
c906108c
SS
3737 }
3738 if (nfields < fip->nbaseclasses)
3739 {
3740 switch (fip->fields->virtuality)
3741 {
c5aa993b
JM
3742 case DW_VIRTUALITY_virtual:
3743 case DW_VIRTUALITY_pure_virtual:
3744 SET_TYPE_FIELD_VIRTUAL (type, nfields);
3745 break;
c906108c
SS
3746 }
3747 }
3748 fip->fields = fip->fields->next;
3749 }
3750}
3751
c906108c
SS
3752/* Add a member function to the proper fieldlist. */
3753
3754static void
107d2387 3755dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 3756 struct type *type, struct dwarf2_cu *cu)
c906108c 3757{
e7c27a73 3758 struct objfile *objfile = cu->objfile;
c906108c
SS
3759 struct attribute *attr;
3760 struct fnfieldlist *flp;
3761 int i;
3762 struct fn_field *fnp;
3763 char *fieldname;
3764 char *physname;
3765 struct nextfnfield *new_fnfield;
f792889a 3766 struct type *this_type;
c906108c 3767
2df3850c 3768 /* Get name of member function. */
39cbfefa
DJ
3769 fieldname = dwarf2_name (die, cu);
3770 if (fieldname == NULL)
2df3850c 3771 return;
c906108c 3772
2df3850c 3773 /* Get the mangled name. */
e142c38c 3774 physname = dwarf2_linkage_name (die, cu);
c906108c
SS
3775
3776 /* Look up member function name in fieldlist. */
3777 for (i = 0; i < fip->nfnfields; i++)
3778 {
27bfe10e 3779 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
3780 break;
3781 }
3782
3783 /* Create new list element if necessary. */
3784 if (i < fip->nfnfields)
3785 flp = &fip->fnfieldlists[i];
3786 else
3787 {
3788 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
3789 {
3790 fip->fnfieldlists = (struct fnfieldlist *)
3791 xrealloc (fip->fnfieldlists,
3792 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 3793 * sizeof (struct fnfieldlist));
c906108c 3794 if (fip->nfnfields == 0)
c13c43fd 3795 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
3796 }
3797 flp = &fip->fnfieldlists[fip->nfnfields];
3798 flp->name = fieldname;
3799 flp->length = 0;
3800 flp->head = NULL;
3801 fip->nfnfields++;
3802 }
3803
3804 /* Create a new member function field and chain it to the field list
3805 entry. */
3806 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 3807 make_cleanup (xfree, new_fnfield);
c906108c
SS
3808 memset (new_fnfield, 0, sizeof (struct nextfnfield));
3809 new_fnfield->next = flp->head;
3810 flp->head = new_fnfield;
3811 flp->length++;
3812
3813 /* Fill in the member function field info. */
3814 fnp = &new_fnfield->fnfield;
d8151005
DJ
3815 /* The name is already allocated along with this objfile, so we don't
3816 need to duplicate it for the type. */
3817 fnp->physname = physname ? physname : "";
c906108c 3818 fnp->type = alloc_type (objfile);
f792889a
DJ
3819 this_type = read_type_die (die, cu);
3820 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 3821 {
f792889a 3822 int nparams = TYPE_NFIELDS (this_type);
c906108c 3823
f792889a 3824 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
3825 of the method itself (TYPE_CODE_METHOD). */
3826 smash_to_method_type (fnp->type, type,
f792889a
DJ
3827 TYPE_TARGET_TYPE (this_type),
3828 TYPE_FIELDS (this_type),
3829 TYPE_NFIELDS (this_type),
3830 TYPE_VARARGS (this_type));
c906108c
SS
3831
3832 /* Handle static member functions.
c5aa993b
JM
3833 Dwarf2 has no clean way to discern C++ static and non-static
3834 member functions. G++ helps GDB by marking the first
3835 parameter for non-static member functions (which is the
3836 this pointer) as artificial. We obtain this information
3837 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 3838 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
3839 fnp->voffset = VOFFSET_STATIC;
3840 }
3841 else
e2e0b3e5 3842 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 3843 physname);
c906108c
SS
3844
3845 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 3846 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 3847 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
3848
3849 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
3850 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
3851
3852 /* Get accessibility. */
e142c38c 3853 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
3854 if (attr)
3855 {
3856 switch (DW_UNSND (attr))
3857 {
c5aa993b
JM
3858 case DW_ACCESS_private:
3859 fnp->is_private = 1;
3860 break;
3861 case DW_ACCESS_protected:
3862 fnp->is_protected = 1;
3863 break;
c906108c
SS
3864 }
3865 }
3866
b02dede2 3867 /* Check for artificial methods. */
e142c38c 3868 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
3869 if (attr && DW_UNSND (attr) != 0)
3870 fnp->is_artificial = 1;
3871
c906108c 3872 /* Get index in virtual function table if it is a virtual member function. */
e142c38c 3873 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
c906108c 3874 if (attr)
8e19ed76
PS
3875 {
3876 /* Support the .debug_loc offsets */
3877 if (attr_form_is_block (attr))
3878 {
e7c27a73 3879 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8e19ed76 3880 }
3690dd37 3881 else if (attr_form_is_section_offset (attr))
8e19ed76 3882 {
4d3c2250 3883 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
3884 }
3885 else
3886 {
4d3c2250
KB
3887 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
3888 fieldname);
8e19ed76
PS
3889 }
3890 }
c906108c
SS
3891}
3892
3893/* Create the vector of member function fields, and attach it to the type. */
3894
3895static void
fba45db2 3896dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 3897 struct dwarf2_cu *cu)
c906108c
SS
3898{
3899 struct fnfieldlist *flp;
3900 int total_length = 0;
3901 int i;
3902
3903 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3904 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
3905 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
3906
3907 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
3908 {
3909 struct nextfnfield *nfp = flp->head;
3910 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
3911 int k;
3912
3913 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
3914 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
3915 fn_flp->fn_fields = (struct fn_field *)
3916 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
3917 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 3918 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
3919
3920 total_length += flp->length;
3921 }
3922
3923 TYPE_NFN_FIELDS (type) = fip->nfnfields;
3924 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
3925}
3926
1168df01
JB
3927/* Returns non-zero if NAME is the name of a vtable member in CU's
3928 language, zero otherwise. */
3929static int
3930is_vtable_name (const char *name, struct dwarf2_cu *cu)
3931{
3932 static const char vptr[] = "_vptr";
987504bb 3933 static const char vtable[] = "vtable";
1168df01 3934
987504bb
JJ
3935 /* Look for the C++ and Java forms of the vtable. */
3936 if ((cu->language == language_java
3937 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
3938 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
3939 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
3940 return 1;
3941
3942 return 0;
3943}
3944
c0dd20ea
DJ
3945/* GCC outputs unnamed structures that are really pointers to member
3946 functions, with the ABI-specified layout. If DIE (from CU) describes
3947 such a structure, set its type, and return nonzero. Otherwise return
61049d3b
DJ
3948 zero.
3949
3950 GCC shouldn't do this; it should just output pointer to member DIEs.
3951 This is GCC PR debug/28767. */
c0dd20ea 3952
f792889a 3953static struct type *
c0dd20ea
DJ
3954quirk_gcc_member_function_pointer (struct die_info *die, struct dwarf2_cu *cu)
3955{
3956 struct objfile *objfile = cu->objfile;
3957 struct type *type;
3958 struct die_info *pfn_die, *delta_die;
3959 struct attribute *pfn_name, *delta_name;
3960 struct type *pfn_type, *domain_type;
3961
3962 /* Check for a structure with no name and two children. */
3963 if (die->tag != DW_TAG_structure_type
3964 || dwarf2_attr (die, DW_AT_name, cu) != NULL
3965 || die->child == NULL
3966 || die->child->sibling == NULL
3967 || (die->child->sibling->sibling != NULL
3968 && die->child->sibling->sibling->tag != DW_TAG_padding))
f792889a 3969 return NULL;
c0dd20ea
DJ
3970
3971 /* Check for __pfn and __delta members. */
3972 pfn_die = die->child;
3973 pfn_name = dwarf2_attr (pfn_die, DW_AT_name, cu);
3974 if (pfn_die->tag != DW_TAG_member
3975 || pfn_name == NULL
3976 || DW_STRING (pfn_name) == NULL
3977 || strcmp ("__pfn", DW_STRING (pfn_name)) != 0)
f792889a 3978 return NULL;
c0dd20ea
DJ
3979
3980 delta_die = pfn_die->sibling;
3981 delta_name = dwarf2_attr (delta_die, DW_AT_name, cu);
3982 if (delta_die->tag != DW_TAG_member
3983 || delta_name == NULL
3984 || DW_STRING (delta_name) == NULL
3985 || strcmp ("__delta", DW_STRING (delta_name)) != 0)
f792889a 3986 return NULL;
c0dd20ea
DJ
3987
3988 /* Find the type of the method. */
3989 pfn_type = die_type (pfn_die, cu);
3990 if (pfn_type == NULL
3991 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
3992 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
f792889a 3993 return NULL;
c0dd20ea
DJ
3994
3995 /* Look for the "this" argument. */
3996 pfn_type = TYPE_TARGET_TYPE (pfn_type);
3997 if (TYPE_NFIELDS (pfn_type) == 0
3998 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
f792889a 3999 return NULL;
c0dd20ea
DJ
4000
4001 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
4002 type = alloc_type (objfile);
4003 smash_to_method_type (type, domain_type, TYPE_TARGET_TYPE (pfn_type),
4004 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
4005 TYPE_VARARGS (pfn_type));
0d5de010 4006 type = lookup_methodptr_type (type);
f792889a 4007 return set_die_type (die, type, cu);
c0dd20ea 4008}
1168df01 4009
c906108c
SS
4010/* Called when we find the DIE that starts a structure or union scope
4011 (definition) to process all dies that define the members of the
4012 structure or union.
4013
4014 NOTE: we need to call struct_type regardless of whether or not the
4015 DIE has an at_name attribute, since it might be an anonymous
4016 structure or union. This gets the type entered into our set of
4017 user defined types.
4018
4019 However, if the structure is incomplete (an opaque struct/union)
4020 then suppress creating a symbol table entry for it since gdb only
4021 wants to find the one with the complete definition. Note that if
4022 it is complete, we just call new_symbol, which does it's own
4023 checking about whether the struct/union is anonymous or not (and
4024 suppresses creating a symbol table entry itself). */
4025
f792889a 4026static struct type *
134d01f1 4027read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4028{
e7c27a73 4029 struct objfile *objfile = cu->objfile;
c906108c
SS
4030 struct type *type;
4031 struct attribute *attr;
39cbfefa 4032 char *name;
0114d602 4033 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c 4034
f792889a
DJ
4035 type = quirk_gcc_member_function_pointer (die, cu);
4036 if (type)
4037 return type;
c906108c 4038
c0dd20ea 4039 type = alloc_type (objfile);
c906108c 4040 INIT_CPLUS_SPECIFIC (type);
39cbfefa
DJ
4041 name = dwarf2_name (die, cu);
4042 if (name != NULL)
c906108c 4043 {
987504bb
JJ
4044 if (cu->language == language_cplus
4045 || cu->language == language_java)
63d06c5c 4046 {
0114d602
DJ
4047 const char *new_prefix = determine_class_name (die, cu);
4048 TYPE_TAG_NAME (type) = (char *) new_prefix;
63d06c5c
DC
4049 }
4050 else
4051 {
d8151005
DJ
4052 /* The name is already allocated along with this objfile, so
4053 we don't need to duplicate it for the type. */
39cbfefa 4054 TYPE_TAG_NAME (type) = name;
63d06c5c 4055 }
c906108c
SS
4056 }
4057
4058 if (die->tag == DW_TAG_structure_type)
4059 {
4060 TYPE_CODE (type) = TYPE_CODE_STRUCT;
4061 }
4062 else if (die->tag == DW_TAG_union_type)
4063 {
4064 TYPE_CODE (type) = TYPE_CODE_UNION;
4065 }
4066 else
4067 {
4068 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
c5aa993b 4069 in gdbtypes.h. */
c906108c
SS
4070 TYPE_CODE (type) = TYPE_CODE_CLASS;
4071 }
4072
e142c38c 4073 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4074 if (attr)
4075 {
4076 TYPE_LENGTH (type) = DW_UNSND (attr);
4077 }
4078 else
4079 {
4080 TYPE_LENGTH (type) = 0;
4081 }
4082
876cecd0 4083 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 4084 if (die_is_declaration (die, cu))
876cecd0 4085 TYPE_STUB (type) = 1;
dc718098 4086
c906108c
SS
4087 /* We need to add the type field to the die immediately so we don't
4088 infinitely recurse when dealing with pointers to the structure
4089 type within the structure itself. */
1c379e20 4090 set_die_type (die, type, cu);
c906108c 4091
e142c38c 4092 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
4093 {
4094 struct field_info fi;
4095 struct die_info *child_die;
c906108c
SS
4096
4097 memset (&fi, 0, sizeof (struct field_info));
4098
639d11d3 4099 child_die = die->child;
c906108c
SS
4100
4101 while (child_die && child_die->tag)
4102 {
a9a9bd0f
DC
4103 if (child_die->tag == DW_TAG_member
4104 || child_die->tag == DW_TAG_variable)
c906108c 4105 {
a9a9bd0f
DC
4106 /* NOTE: carlton/2002-11-05: A C++ static data member
4107 should be a DW_TAG_member that is a declaration, but
4108 all versions of G++ as of this writing (so through at
4109 least 3.2.1) incorrectly generate DW_TAG_variable
4110 tags for them instead. */
e7c27a73 4111 dwarf2_add_field (&fi, child_die, cu);
c906108c 4112 }
8713b1b1 4113 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
4114 {
4115 /* C++ member function. */
e7c27a73 4116 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
4117 }
4118 else if (child_die->tag == DW_TAG_inheritance)
4119 {
4120 /* C++ base class field. */
e7c27a73 4121 dwarf2_add_field (&fi, child_die, cu);
c906108c 4122 }
c906108c
SS
4123 child_die = sibling_die (child_die);
4124 }
4125
4126 /* Attach fields and member functions to the type. */
4127 if (fi.nfields)
e7c27a73 4128 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
4129 if (fi.nfnfields)
4130 {
e7c27a73 4131 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 4132
c5aa993b 4133 /* Get the type which refers to the base class (possibly this
c906108c
SS
4134 class itself) which contains the vtable pointer for the current
4135 class from the DW_AT_containing_type attribute. */
4136
e142c38c 4137 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 4138 {
e7c27a73 4139 struct type *t = die_containing_type (die, cu);
c906108c
SS
4140
4141 TYPE_VPTR_BASETYPE (type) = t;
4142 if (type == t)
4143 {
c906108c
SS
4144 int i;
4145
4146 /* Our own class provides vtbl ptr. */
4147 for (i = TYPE_NFIELDS (t) - 1;
4148 i >= TYPE_N_BASECLASSES (t);
4149 --i)
4150 {
4151 char *fieldname = TYPE_FIELD_NAME (t, i);
4152
1168df01 4153 if (is_vtable_name (fieldname, cu))
c906108c
SS
4154 {
4155 TYPE_VPTR_FIELDNO (type) = i;
4156 break;
4157 }
4158 }
4159
4160 /* Complain if virtual function table field not found. */
4161 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 4162 complaint (&symfile_complaints,
e2e0b3e5 4163 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
4164 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
4165 "");
c906108c
SS
4166 }
4167 else
4168 {
4169 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
4170 }
4171 }
f6235d4c
EZ
4172 else if (cu->producer
4173 && strncmp (cu->producer,
4174 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
4175 {
4176 /* The IBM XLC compiler does not provide direct indication
4177 of the containing type, but the vtable pointer is
4178 always named __vfp. */
4179
4180 int i;
4181
4182 for (i = TYPE_NFIELDS (type) - 1;
4183 i >= TYPE_N_BASECLASSES (type);
4184 --i)
4185 {
4186 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
4187 {
4188 TYPE_VPTR_FIELDNO (type) = i;
4189 TYPE_VPTR_BASETYPE (type) = type;
4190 break;
4191 }
4192 }
4193 }
c906108c 4194 }
c906108c 4195 }
63d06c5c 4196
0114d602 4197 do_cleanups (back_to);
f792889a 4198 return type;
c906108c
SS
4199}
4200
134d01f1
DJ
4201static void
4202process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
4203{
4204 struct objfile *objfile = cu->objfile;
90aeadfc 4205 struct die_info *child_die = die->child;
f792889a 4206 struct type *this_type;
c906108c 4207
f792889a
DJ
4208 this_type = get_die_type (die, cu);
4209 if (this_type == NULL)
4210 this_type = read_structure_type (die, cu);
c906108c 4211
90aeadfc
DC
4212 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
4213 snapshots) has been known to create a die giving a declaration
4214 for a class that has, as a child, a die giving a definition for a
4215 nested class. So we have to process our children even if the
4216 current die is a declaration. Normally, of course, a declaration
4217 won't have any children at all. */
134d01f1 4218
90aeadfc
DC
4219 while (child_die != NULL && child_die->tag)
4220 {
4221 if (child_die->tag == DW_TAG_member
4222 || child_die->tag == DW_TAG_variable
4223 || child_die->tag == DW_TAG_inheritance)
134d01f1 4224 {
90aeadfc 4225 /* Do nothing. */
134d01f1 4226 }
90aeadfc
DC
4227 else
4228 process_die (child_die, cu);
134d01f1 4229
90aeadfc 4230 child_die = sibling_die (child_die);
134d01f1
DJ
4231 }
4232
fa4028e9
JB
4233 /* Do not consider external references. According to the DWARF standard,
4234 these DIEs are identified by the fact that they have no byte_size
4235 attribute, and a declaration attribute. */
4236 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
4237 || !die_is_declaration (die, cu))
f792889a 4238 new_symbol (die, this_type, cu);
134d01f1
DJ
4239}
4240
4241/* Given a DW_AT_enumeration_type die, set its type. We do not
4242 complete the type's fields yet, or create any symbols. */
c906108c 4243
f792889a 4244static struct type *
134d01f1 4245read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4246{
e7c27a73 4247 struct objfile *objfile = cu->objfile;
c906108c 4248 struct type *type;
c906108c 4249 struct attribute *attr;
0114d602 4250 const char *name;
134d01f1 4251
c906108c
SS
4252 type = alloc_type (objfile);
4253
4254 TYPE_CODE (type) = TYPE_CODE_ENUM;
0114d602 4255 name = dwarf2_full_name (die, cu);
39cbfefa 4256 if (name != NULL)
0114d602 4257 TYPE_TAG_NAME (type) = (char *) name;
c906108c 4258
e142c38c 4259 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4260 if (attr)
4261 {
4262 TYPE_LENGTH (type) = DW_UNSND (attr);
4263 }
4264 else
4265 {
4266 TYPE_LENGTH (type) = 0;
4267 }
4268
137033e9
JB
4269 /* The enumeration DIE can be incomplete. In Ada, any type can be
4270 declared as private in the package spec, and then defined only
4271 inside the package body. Such types are known as Taft Amendment
4272 Types. When another package uses such a type, an incomplete DIE
4273 may be generated by the compiler. */
02eb380e 4274 if (die_is_declaration (die, cu))
876cecd0 4275 TYPE_STUB (type) = 1;
02eb380e 4276
f792889a 4277 return set_die_type (die, type, cu);
134d01f1
DJ
4278}
4279
8176b9b8 4280/* Determine the name of the type represented by DIE, which should be
0114d602
DJ
4281 a named C++ or Java compound type. Return the name in question,
4282 allocated on the objfile obstack. */
8176b9b8 4283
0114d602 4284static const char *
8176b9b8
DC
4285determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
4286{
0114d602 4287 const char *new_prefix = NULL;
8176b9b8
DC
4288
4289 /* If we don't have namespace debug info, guess the name by trying
4290 to demangle the names of members, just like we did in
72bf9492 4291 guess_structure_name. */
8176b9b8
DC
4292 if (!processing_has_namespace_info)
4293 {
4294 struct die_info *child;
4295
4296 for (child = die->child;
4297 child != NULL && child->tag != 0;
4298 child = sibling_die (child))
4299 {
4300 if (child->tag == DW_TAG_subprogram)
4301 {
0114d602 4302 char *phys_prefix
31c27f77
JJ
4303 = language_class_name_from_physname (cu->language_defn,
4304 dwarf2_linkage_name
8176b9b8
DC
4305 (child, cu));
4306
0114d602
DJ
4307 if (phys_prefix != NULL)
4308 {
4309 new_prefix
4310 = obsavestring (phys_prefix, strlen (phys_prefix),
4311 &cu->objfile->objfile_obstack);
4312 xfree (phys_prefix);
4313 break;
4314 }
8176b9b8
DC
4315 }
4316 }
4317 }
4318
4319 if (new_prefix == NULL)
0114d602 4320 new_prefix = dwarf2_full_name (die, cu);
8176b9b8
DC
4321
4322 return new_prefix;
4323}
4324
134d01f1
DJ
4325/* Given a pointer to a die which begins an enumeration, process all
4326 the dies that define the members of the enumeration, and create the
4327 symbol for the enumeration type.
4328
4329 NOTE: We reverse the order of the element list. */
4330
4331static void
4332process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
4333{
4334 struct objfile *objfile = cu->objfile;
4335 struct die_info *child_die;
4336 struct field *fields;
134d01f1
DJ
4337 struct symbol *sym;
4338 int num_fields;
4339 int unsigned_enum = 1;
39cbfefa 4340 char *name;
f792889a 4341 struct type *this_type;
134d01f1 4342
c906108c
SS
4343 num_fields = 0;
4344 fields = NULL;
f792889a
DJ
4345 this_type = get_die_type (die, cu);
4346 if (this_type == NULL)
4347 this_type = read_enumeration_type (die, cu);
639d11d3 4348 if (die->child != NULL)
c906108c 4349 {
639d11d3 4350 child_die = die->child;
c906108c
SS
4351 while (child_die && child_die->tag)
4352 {
4353 if (child_die->tag != DW_TAG_enumerator)
4354 {
e7c27a73 4355 process_die (child_die, cu);
c906108c
SS
4356 }
4357 else
4358 {
39cbfefa
DJ
4359 name = dwarf2_name (child_die, cu);
4360 if (name)
c906108c 4361 {
f792889a 4362 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
4363 if (SYMBOL_VALUE (sym) < 0)
4364 unsigned_enum = 0;
4365
4366 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
4367 {
4368 fields = (struct field *)
4369 xrealloc (fields,
4370 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 4371 * sizeof (struct field));
c906108c
SS
4372 }
4373
3567439c 4374 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c
SS
4375 FIELD_TYPE (fields[num_fields]) = NULL;
4376 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
4377 FIELD_BITSIZE (fields[num_fields]) = 0;
01ad7f36 4378 FIELD_STATIC_KIND (fields[num_fields]) = 0;
c906108c
SS
4379
4380 num_fields++;
4381 }
4382 }
4383
4384 child_die = sibling_die (child_die);
4385 }
4386
4387 if (num_fields)
4388 {
f792889a
DJ
4389 TYPE_NFIELDS (this_type) = num_fields;
4390 TYPE_FIELDS (this_type) = (struct field *)
4391 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
4392 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 4393 sizeof (struct field) * num_fields);
b8c9b27d 4394 xfree (fields);
c906108c
SS
4395 }
4396 if (unsigned_enum)
876cecd0 4397 TYPE_UNSIGNED (this_type) = 1;
c906108c 4398 }
134d01f1 4399
f792889a 4400 new_symbol (die, this_type, cu);
c906108c
SS
4401}
4402
4403/* Extract all information from a DW_TAG_array_type DIE and put it in
4404 the DIE's type field. For now, this only handles one dimensional
4405 arrays. */
4406
f792889a 4407static struct type *
e7c27a73 4408read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4409{
e7c27a73 4410 struct objfile *objfile = cu->objfile;
c906108c
SS
4411 struct die_info *child_die;
4412 struct type *type = NULL;
4413 struct type *element_type, *range_type, *index_type;
4414 struct type **range_types = NULL;
4415 struct attribute *attr;
4416 int ndim = 0;
4417 struct cleanup *back_to;
39cbfefa 4418 char *name;
c906108c 4419
e7c27a73 4420 element_type = die_type (die, cu);
c906108c
SS
4421
4422 /* Irix 6.2 native cc creates array types without children for
4423 arrays with unspecified length. */
639d11d3 4424 if (die->child == NULL)
c906108c 4425 {
6ccb9162 4426 index_type = builtin_type_int32;
c906108c 4427 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
4428 type = create_array_type (NULL, element_type, range_type);
4429 return set_die_type (die, type, cu);
c906108c
SS
4430 }
4431
4432 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 4433 child_die = die->child;
c906108c
SS
4434 while (child_die && child_die->tag)
4435 {
4436 if (child_die->tag == DW_TAG_subrange_type)
4437 {
f792889a
DJ
4438 struct type *child_type = read_type_die (child_die, cu);
4439 if (child_type != NULL)
a02abb62
JB
4440 {
4441 /* The range type was succesfully read. Save it for
4442 the array type creation. */
4443 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
4444 {
4445 range_types = (struct type **)
4446 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
4447 * sizeof (struct type *));
4448 if (ndim == 0)
4449 make_cleanup (free_current_contents, &range_types);
4450 }
f792889a 4451 range_types[ndim++] = child_type;
a02abb62 4452 }
c906108c
SS
4453 }
4454 child_die = sibling_die (child_die);
4455 }
4456
4457 /* Dwarf2 dimensions are output from left to right, create the
4458 necessary array types in backwards order. */
7ca2d3a3 4459
c906108c 4460 type = element_type;
7ca2d3a3
DL
4461
4462 if (read_array_order (die, cu) == DW_ORD_col_major)
4463 {
4464 int i = 0;
4465 while (i < ndim)
4466 type = create_array_type (NULL, type, range_types[i++]);
4467 }
4468 else
4469 {
4470 while (ndim-- > 0)
4471 type = create_array_type (NULL, type, range_types[ndim]);
4472 }
c906108c 4473
f5f8a009
EZ
4474 /* Understand Dwarf2 support for vector types (like they occur on
4475 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
4476 array type. This is not part of the Dwarf2/3 standard yet, but a
4477 custom vendor extension. The main difference between a regular
4478 array and the vector variant is that vectors are passed by value
4479 to functions. */
e142c38c 4480 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 4481 if (attr)
ea37ba09 4482 make_vector_type (type);
f5f8a009 4483
39cbfefa
DJ
4484 name = dwarf2_name (die, cu);
4485 if (name)
4486 TYPE_NAME (type) = name;
714e295e 4487
c906108c
SS
4488 do_cleanups (back_to);
4489
4490 /* Install the type in the die. */
f792889a 4491 return set_die_type (die, type, cu);
c906108c
SS
4492}
4493
7ca2d3a3
DL
4494static enum dwarf_array_dim_ordering
4495read_array_order (struct die_info *die, struct dwarf2_cu *cu)
4496{
4497 struct attribute *attr;
4498
4499 attr = dwarf2_attr (die, DW_AT_ordering, cu);
4500
4501 if (attr) return DW_SND (attr);
4502
4503 /*
4504 GNU F77 is a special case, as at 08/2004 array type info is the
4505 opposite order to the dwarf2 specification, but data is still
4506 laid out as per normal fortran.
4507
4508 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
4509 version checking.
4510 */
4511
4512 if (cu->language == language_fortran &&
4513 cu->producer && strstr (cu->producer, "GNU F77"))
4514 {
4515 return DW_ORD_row_major;
4516 }
4517
4518 switch (cu->language_defn->la_array_ordering)
4519 {
4520 case array_column_major:
4521 return DW_ORD_col_major;
4522 case array_row_major:
4523 default:
4524 return DW_ORD_row_major;
4525 };
4526}
4527
72019c9c
GM
4528/* Extract all information from a DW_TAG_set_type DIE and put it in
4529 the DIE's type field. */
4530
f792889a 4531static struct type *
72019c9c
GM
4532read_set_type (struct die_info *die, struct dwarf2_cu *cu)
4533{
f792889a
DJ
4534 struct type *set_type = create_set_type (NULL, die_type (die, cu));
4535
4536 return set_die_type (die, set_type, cu);
72019c9c 4537}
7ca2d3a3 4538
c906108c
SS
4539/* First cut: install each common block member as a global variable. */
4540
4541static void
e7c27a73 4542read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4543{
4544 struct die_info *child_die;
4545 struct attribute *attr;
4546 struct symbol *sym;
4547 CORE_ADDR base = (CORE_ADDR) 0;
4548
e142c38c 4549 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
4550 if (attr)
4551 {
8e19ed76
PS
4552 /* Support the .debug_loc offsets */
4553 if (attr_form_is_block (attr))
4554 {
e7c27a73 4555 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 4556 }
3690dd37 4557 else if (attr_form_is_section_offset (attr))
8e19ed76 4558 {
4d3c2250 4559 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4560 }
4561 else
4562 {
4d3c2250
KB
4563 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4564 "common block member");
8e19ed76 4565 }
c906108c 4566 }
639d11d3 4567 if (die->child != NULL)
c906108c 4568 {
639d11d3 4569 child_die = die->child;
c906108c
SS
4570 while (child_die && child_die->tag)
4571 {
e7c27a73 4572 sym = new_symbol (child_die, NULL, cu);
e142c38c 4573 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
4574 if (attr)
4575 {
4576 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 4577 base + decode_locdesc (DW_BLOCK (attr), cu);
c906108c
SS
4578 add_symbol_to_list (sym, &global_symbols);
4579 }
4580 child_die = sibling_die (child_die);
4581 }
4582 }
4583}
4584
0114d602 4585/* Create a type for a C++ namespace. */
d9fa45fe 4586
0114d602
DJ
4587static struct type *
4588read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 4589{
e7c27a73 4590 struct objfile *objfile = cu->objfile;
0114d602 4591 const char *previous_prefix, *name;
9219021c 4592 int is_anonymous;
0114d602
DJ
4593 struct type *type;
4594
4595 /* For extensions, reuse the type of the original namespace. */
4596 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
4597 {
4598 struct die_info *ext_die;
4599 struct dwarf2_cu *ext_cu = cu;
4600 ext_die = dwarf2_extension (die, &ext_cu);
4601 type = read_type_die (ext_die, ext_cu);
4602 return set_die_type (die, type, cu);
4603 }
9219021c 4604
e142c38c 4605 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
4606
4607 /* Now build the name of the current namespace. */
4608
0114d602
DJ
4609 previous_prefix = determine_prefix (die, cu);
4610 if (previous_prefix[0] != '\0')
4611 name = typename_concat (&objfile->objfile_obstack,
4612 previous_prefix, name, cu);
4613
4614 /* Create the type. */
4615 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
4616 objfile);
4617 TYPE_NAME (type) = (char *) name;
4618 TYPE_TAG_NAME (type) = TYPE_NAME (type);
4619
4620 set_die_type (die, type, cu);
4621
4622 return type;
4623}
4624
4625/* Read a C++ namespace. */
4626
4627static void
4628read_namespace (struct die_info *die, struct dwarf2_cu *cu)
4629{
4630 struct objfile *objfile = cu->objfile;
4631 const char *name;
4632 int is_anonymous;
9219021c 4633
5c4e30ca
DC
4634 /* Add a symbol associated to this if we haven't seen the namespace
4635 before. Also, add a using directive if it's an anonymous
4636 namespace. */
9219021c 4637
f2f0e013 4638 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
4639 {
4640 struct type *type;
4641
0114d602 4642 type = read_type_die (die, cu);
e7c27a73 4643 new_symbol (die, type, cu);
5c4e30ca 4644
0114d602 4645 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 4646 if (is_anonymous)
0114d602
DJ
4647 {
4648 const char *previous_prefix = determine_prefix (die, cu);
4649 cp_add_using_directive (TYPE_NAME (type),
4650 strlen (previous_prefix),
4651 strlen (TYPE_NAME (type)));
4652 }
5c4e30ca 4653 }
9219021c 4654
639d11d3 4655 if (die->child != NULL)
d9fa45fe 4656 {
639d11d3 4657 struct die_info *child_die = die->child;
d9fa45fe
DC
4658
4659 while (child_die && child_die->tag)
4660 {
e7c27a73 4661 process_die (child_die, cu);
d9fa45fe
DC
4662 child_die = sibling_die (child_die);
4663 }
4664 }
38d518c9
EZ
4665}
4666
4667/* Return the name of the namespace represented by DIE. Set
4668 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
4669 namespace. */
4670
4671static const char *
e142c38c 4672namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
4673{
4674 struct die_info *current_die;
4675 const char *name = NULL;
4676
4677 /* Loop through the extensions until we find a name. */
4678
4679 for (current_die = die;
4680 current_die != NULL;
f2f0e013 4681 current_die = dwarf2_extension (die, &cu))
38d518c9 4682 {
e142c38c 4683 name = dwarf2_name (current_die, cu);
38d518c9
EZ
4684 if (name != NULL)
4685 break;
4686 }
4687
4688 /* Is it an anonymous namespace? */
4689
4690 *is_anonymous = (name == NULL);
4691 if (*is_anonymous)
4692 name = "(anonymous namespace)";
4693
4694 return name;
d9fa45fe
DC
4695}
4696
c906108c
SS
4697/* Extract all information from a DW_TAG_pointer_type DIE and add to
4698 the user defined type vector. */
4699
f792889a 4700static struct type *
e7c27a73 4701read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4702{
5e2b427d 4703 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 4704 struct comp_unit_head *cu_header = &cu->header;
c906108c 4705 struct type *type;
8b2dbe47
KB
4706 struct attribute *attr_byte_size;
4707 struct attribute *attr_address_class;
4708 int byte_size, addr_class;
c906108c 4709
e7c27a73 4710 type = lookup_pointer_type (die_type (die, cu));
8b2dbe47 4711
e142c38c 4712 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
4713 if (attr_byte_size)
4714 byte_size = DW_UNSND (attr_byte_size);
c906108c 4715 else
8b2dbe47
KB
4716 byte_size = cu_header->addr_size;
4717
e142c38c 4718 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
4719 if (attr_address_class)
4720 addr_class = DW_UNSND (attr_address_class);
4721 else
4722 addr_class = DW_ADDR_none;
4723
4724 /* If the pointer size or address class is different than the
4725 default, create a type variant marked as such and set the
4726 length accordingly. */
4727 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 4728 {
5e2b427d 4729 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
4730 {
4731 int type_flags;
4732
849957d9 4733 type_flags = gdbarch_address_class_type_flags
5e2b427d 4734 (gdbarch, byte_size, addr_class);
876cecd0
TT
4735 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
4736 == 0);
8b2dbe47
KB
4737 type = make_type_with_address_space (type, type_flags);
4738 }
4739 else if (TYPE_LENGTH (type) != byte_size)
4740 {
e2e0b3e5 4741 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47
KB
4742 }
4743 else {
4744 /* Should we also complain about unhandled address classes? */
4745 }
c906108c 4746 }
8b2dbe47
KB
4747
4748 TYPE_LENGTH (type) = byte_size;
f792889a 4749 return set_die_type (die, type, cu);
c906108c
SS
4750}
4751
4752/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
4753 the user defined type vector. */
4754
f792889a 4755static struct type *
e7c27a73 4756read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4757{
e7c27a73 4758 struct objfile *objfile = cu->objfile;
c906108c
SS
4759 struct type *type;
4760 struct type *to_type;
4761 struct type *domain;
4762
e7c27a73
DJ
4763 to_type = die_type (die, cu);
4764 domain = die_containing_type (die, cu);
0d5de010
DJ
4765
4766 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
4767 type = lookup_methodptr_type (to_type);
4768 else
4769 type = lookup_memberptr_type (to_type, domain);
c906108c 4770
f792889a 4771 return set_die_type (die, type, cu);
c906108c
SS
4772}
4773
4774/* Extract all information from a DW_TAG_reference_type DIE and add to
4775 the user defined type vector. */
4776
f792889a 4777static struct type *
e7c27a73 4778read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4779{
e7c27a73 4780 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
4781 struct type *type;
4782 struct attribute *attr;
4783
e7c27a73 4784 type = lookup_reference_type (die_type (die, cu));
e142c38c 4785 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4786 if (attr)
4787 {
4788 TYPE_LENGTH (type) = DW_UNSND (attr);
4789 }
4790 else
4791 {
107d2387 4792 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 4793 }
f792889a 4794 return set_die_type (die, type, cu);
c906108c
SS
4795}
4796
f792889a 4797static struct type *
e7c27a73 4798read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4799{
f792889a 4800 struct type *base_type, *cv_type;
c906108c 4801
e7c27a73 4802 base_type = die_type (die, cu);
f792889a
DJ
4803 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
4804 return set_die_type (die, cv_type, cu);
c906108c
SS
4805}
4806
f792889a 4807static struct type *
e7c27a73 4808read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4809{
f792889a 4810 struct type *base_type, *cv_type;
c906108c 4811
e7c27a73 4812 base_type = die_type (die, cu);
f792889a
DJ
4813 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
4814 return set_die_type (die, cv_type, cu);
c906108c
SS
4815}
4816
4817/* Extract all information from a DW_TAG_string_type DIE and add to
4818 the user defined type vector. It isn't really a user defined type,
4819 but it behaves like one, with other DIE's using an AT_user_def_type
4820 attribute to reference it. */
4821
f792889a 4822static struct type *
e7c27a73 4823read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4824{
e7c27a73 4825 struct objfile *objfile = cu->objfile;
c906108c
SS
4826 struct type *type, *range_type, *index_type, *char_type;
4827 struct attribute *attr;
4828 unsigned int length;
4829
e142c38c 4830 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
4831 if (attr)
4832 {
4833 length = DW_UNSND (attr);
4834 }
4835 else
4836 {
b21b22e0 4837 /* check for the DW_AT_byte_size attribute */
e142c38c 4838 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
4839 if (attr)
4840 {
4841 length = DW_UNSND (attr);
4842 }
4843 else
4844 {
4845 length = 1;
4846 }
c906108c 4847 }
6ccb9162
UW
4848
4849 index_type = builtin_type_int32;
c906108c 4850 range_type = create_range_type (NULL, index_type, 1, length);
6ccb9162
UW
4851 type = create_string_type (NULL, range_type);
4852
f792889a 4853 return set_die_type (die, type, cu);
c906108c
SS
4854}
4855
4856/* Handle DIES due to C code like:
4857
4858 struct foo
c5aa993b
JM
4859 {
4860 int (*funcp)(int a, long l);
4861 int b;
4862 };
c906108c
SS
4863
4864 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 4865 */
c906108c 4866
f792889a 4867static struct type *
e7c27a73 4868read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4869{
4870 struct type *type; /* Type that this function returns */
4871 struct type *ftype; /* Function that returns above type */
4872 struct attribute *attr;
4873
e7c27a73 4874 type = die_type (die, cu);
1326e61b 4875 ftype = make_function_type (type, (struct type **) 0);
c906108c 4876
5b8101ae 4877 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 4878 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 4879 if ((attr && (DW_UNSND (attr) != 0))
987504bb 4880 || cu->language == language_cplus
5b8101ae
PM
4881 || cu->language == language_java
4882 || cu->language == language_pascal)
876cecd0 4883 TYPE_PROTOTYPED (ftype) = 1;
c906108c 4884
c055b101
CV
4885 /* Store the calling convention in the type if it's available in
4886 the subroutine die. Otherwise set the calling convention to
4887 the default value DW_CC_normal. */
4888 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
4889 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
4890
639d11d3 4891 if (die->child != NULL)
c906108c
SS
4892 {
4893 struct die_info *child_die;
4894 int nparams = 0;
4895 int iparams = 0;
4896
4897 /* Count the number of parameters.
4898 FIXME: GDB currently ignores vararg functions, but knows about
4899 vararg member functions. */
639d11d3 4900 child_die = die->child;
c906108c
SS
4901 while (child_die && child_die->tag)
4902 {
4903 if (child_die->tag == DW_TAG_formal_parameter)
4904 nparams++;
4905 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 4906 TYPE_VARARGS (ftype) = 1;
c906108c
SS
4907 child_die = sibling_die (child_die);
4908 }
4909
4910 /* Allocate storage for parameters and fill them in. */
4911 TYPE_NFIELDS (ftype) = nparams;
4912 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 4913 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 4914
639d11d3 4915 child_die = die->child;
c906108c
SS
4916 while (child_die && child_die->tag)
4917 {
4918 if (child_die->tag == DW_TAG_formal_parameter)
4919 {
4920 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
4921 member functions. G++ helps GDB by marking the first
4922 parameter for non-static member functions (which is the
4923 this pointer) as artificial. We pass this information
4924 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 4925 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
4926 if (attr)
4927 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
4928 else
4929 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
e7c27a73 4930 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
4931 iparams++;
4932 }
4933 child_die = sibling_die (child_die);
4934 }
4935 }
4936
f792889a 4937 return set_die_type (die, ftype, cu);
c906108c
SS
4938}
4939
f792889a 4940static struct type *
e7c27a73 4941read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4942{
e7c27a73 4943 struct objfile *objfile = cu->objfile;
2f038fcb 4944 struct attribute *attr;
0114d602 4945 const char *name = NULL;
f792889a 4946 struct type *this_type;
c906108c 4947
0114d602 4948 name = dwarf2_full_name (die, cu);
f792889a 4949 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
4950 TYPE_FLAG_TARGET_STUB, NULL, objfile);
4951 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
4952 set_die_type (die, this_type, cu);
4953 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
4954 return this_type;
c906108c
SS
4955}
4956
4957/* Find a representation of a given base type and install
4958 it in the TYPE field of the die. */
4959
f792889a 4960static struct type *
e7c27a73 4961read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4962{
e7c27a73 4963 struct objfile *objfile = cu->objfile;
c906108c
SS
4964 struct type *type;
4965 struct attribute *attr;
4966 int encoding = 0, size = 0;
39cbfefa 4967 char *name;
6ccb9162
UW
4968 enum type_code code = TYPE_CODE_INT;
4969 int type_flags = 0;
4970 struct type *target_type = NULL;
c906108c 4971
e142c38c 4972 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
4973 if (attr)
4974 {
4975 encoding = DW_UNSND (attr);
4976 }
e142c38c 4977 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4978 if (attr)
4979 {
4980 size = DW_UNSND (attr);
4981 }
39cbfefa 4982 name = dwarf2_name (die, cu);
6ccb9162 4983 if (!name)
c906108c 4984 {
6ccb9162
UW
4985 complaint (&symfile_complaints,
4986 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 4987 }
6ccb9162
UW
4988
4989 switch (encoding)
c906108c 4990 {
6ccb9162
UW
4991 case DW_ATE_address:
4992 /* Turn DW_ATE_address into a void * pointer. */
4993 code = TYPE_CODE_PTR;
4994 type_flags |= TYPE_FLAG_UNSIGNED;
4995 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
4996 break;
4997 case DW_ATE_boolean:
4998 code = TYPE_CODE_BOOL;
4999 type_flags |= TYPE_FLAG_UNSIGNED;
5000 break;
5001 case DW_ATE_complex_float:
5002 code = TYPE_CODE_COMPLEX;
5003 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
5004 break;
5005 case DW_ATE_decimal_float:
5006 code = TYPE_CODE_DECFLOAT;
5007 break;
5008 case DW_ATE_float:
5009 code = TYPE_CODE_FLT;
5010 break;
5011 case DW_ATE_signed:
5012 break;
5013 case DW_ATE_unsigned:
5014 type_flags |= TYPE_FLAG_UNSIGNED;
5015 break;
5016 case DW_ATE_signed_char:
1760d9d5 5017 if (cu->language == language_ada || cu->language == language_m2)
6ccb9162
UW
5018 code = TYPE_CODE_CHAR;
5019 break;
5020 case DW_ATE_unsigned_char:
1760d9d5 5021 if (cu->language == language_ada || cu->language == language_m2)
6ccb9162
UW
5022 code = TYPE_CODE_CHAR;
5023 type_flags |= TYPE_FLAG_UNSIGNED;
5024 break;
5025 default:
5026 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
5027 dwarf_type_encoding_name (encoding));
5028 break;
c906108c 5029 }
6ccb9162 5030
0114d602
DJ
5031 type = init_type (code, size, type_flags, NULL, objfile);
5032 TYPE_NAME (type) = name;
6ccb9162
UW
5033 TYPE_TARGET_TYPE (type) = target_type;
5034
0114d602 5035 if (name && strcmp (name, "char") == 0)
876cecd0 5036 TYPE_NOSIGN (type) = 1;
0114d602 5037
f792889a 5038 return set_die_type (die, type, cu);
c906108c
SS
5039}
5040
a02abb62
JB
5041/* Read the given DW_AT_subrange DIE. */
5042
f792889a 5043static struct type *
a02abb62
JB
5044read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
5045{
5e2b427d 5046 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
5047 struct type *base_type;
5048 struct type *range_type;
5049 struct attribute *attr;
5050 int low = 0;
5051 int high = -1;
39cbfefa 5052 char *name;
a02abb62 5053
a02abb62 5054 base_type = die_type (die, cu);
3d1f72c2 5055 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
a02abb62
JB
5056 {
5057 complaint (&symfile_complaints,
e2e0b3e5 5058 _("DW_AT_type missing from DW_TAG_subrange_type"));
17a912b6 5059 base_type
5e2b427d 5060 = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6ccb9162 5061 0, NULL, cu->objfile);
a02abb62
JB
5062 }
5063
e142c38c 5064 if (cu->language == language_fortran)
a02abb62
JB
5065 {
5066 /* FORTRAN implies a lower bound of 1, if not given. */
5067 low = 1;
5068 }
5069
dd5e6932
DJ
5070 /* FIXME: For variable sized arrays either of these could be
5071 a variable rather than a constant value. We'll allow it,
5072 but we don't know how to handle it. */
e142c38c 5073 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
5074 if (attr)
5075 low = dwarf2_get_attr_constant_value (attr, 0);
5076
e142c38c 5077 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62
JB
5078 if (attr)
5079 {
5080 if (attr->form == DW_FORM_block1)
5081 {
5082 /* GCC encodes arrays with unspecified or dynamic length
5083 with a DW_FORM_block1 attribute.
5084 FIXME: GDB does not yet know how to handle dynamic
5085 arrays properly, treat them as arrays with unspecified
5086 length for now.
5087
5088 FIXME: jimb/2003-09-22: GDB does not really know
5089 how to handle arrays of unspecified length
5090 either; we just represent them as zero-length
5091 arrays. Choose an appropriate upper bound given
5092 the lower bound we've computed above. */
5093 high = low - 1;
5094 }
5095 else
5096 high = dwarf2_get_attr_constant_value (attr, 1);
5097 }
5098
5099 range_type = create_range_type (NULL, base_type, low, high);
5100
39cbfefa
DJ
5101 name = dwarf2_name (die, cu);
5102 if (name)
5103 TYPE_NAME (range_type) = name;
a02abb62 5104
e142c38c 5105 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
5106 if (attr)
5107 TYPE_LENGTH (range_type) = DW_UNSND (attr);
5108
f792889a 5109 return set_die_type (die, range_type, cu);
a02abb62
JB
5110}
5111
f792889a 5112static struct type *
81a17f79
JB
5113read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
5114{
5115 struct type *type;
81a17f79 5116
81a17f79
JB
5117 /* For now, we only support the C meaning of an unspecified type: void. */
5118
0114d602
DJ
5119 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
5120 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 5121
f792889a 5122 return set_die_type (die, type, cu);
81a17f79 5123}
a02abb62 5124
51545339
DJ
5125/* Trivial hash function for die_info: the hash value of a DIE
5126 is its offset in .debug_info for this objfile. */
5127
5128static hashval_t
5129die_hash (const void *item)
5130{
5131 const struct die_info *die = item;
5132 return die->offset;
5133}
5134
5135/* Trivial comparison function for die_info structures: two DIEs
5136 are equal if they have the same offset. */
5137
5138static int
5139die_eq (const void *item_lhs, const void *item_rhs)
5140{
5141 const struct die_info *die_lhs = item_lhs;
5142 const struct die_info *die_rhs = item_rhs;
5143 return die_lhs->offset == die_rhs->offset;
5144}
5145
c906108c
SS
5146/* Read a whole compilation unit into a linked list of dies. */
5147
f9aca02d 5148static struct die_info *
fe1b8b76 5149read_comp_unit (gdb_byte *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
c906108c 5150{
51545339
DJ
5151 cu->die_hash
5152 = htab_create_alloc_ex (cu->header.length / 12,
5153 die_hash,
5154 die_eq,
5155 NULL,
5156 &cu->comp_unit_obstack,
5157 hashtab_obstack_allocate,
5158 dummy_obstack_deallocate);
5159
e7c27a73 5160 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
639d11d3
DC
5161}
5162
5163/* Read a single die and all its descendents. Set the die's sibling
5164 field to NULL; set other fields in the die correctly, and set all
5165 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
5166 location of the info_ptr after reading all of those dies. PARENT
5167 is the parent of the die in question. */
5168
5169static struct die_info *
fe1b8b76 5170read_die_and_children (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 5171 struct dwarf2_cu *cu,
fe1b8b76 5172 gdb_byte **new_info_ptr,
639d11d3
DC
5173 struct die_info *parent)
5174{
5175 struct die_info *die;
fe1b8b76 5176 gdb_byte *cur_ptr;
639d11d3
DC
5177 int has_children;
5178
e7c27a73 5179 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
1d325ec1
DJ
5180 if (die == NULL)
5181 {
5182 *new_info_ptr = cur_ptr;
5183 return NULL;
5184 }
51545339 5185 store_in_ref_table (die, cu);
639d11d3
DC
5186
5187 if (has_children)
5188 {
e7c27a73 5189 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
639d11d3
DC
5190 new_info_ptr, die);
5191 }
5192 else
5193 {
5194 die->child = NULL;
5195 *new_info_ptr = cur_ptr;
5196 }
5197
5198 die->sibling = NULL;
5199 die->parent = parent;
5200 return die;
5201}
5202
5203/* Read a die, all of its descendents, and all of its siblings; set
5204 all of the fields of all of the dies correctly. Arguments are as
5205 in read_die_and_children. */
5206
5207static struct die_info *
fe1b8b76 5208read_die_and_siblings (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 5209 struct dwarf2_cu *cu,
fe1b8b76 5210 gdb_byte **new_info_ptr,
639d11d3
DC
5211 struct die_info *parent)
5212{
5213 struct die_info *first_die, *last_sibling;
fe1b8b76 5214 gdb_byte *cur_ptr;
639d11d3 5215
c906108c 5216 cur_ptr = info_ptr;
639d11d3
DC
5217 first_die = last_sibling = NULL;
5218
5219 while (1)
c906108c 5220 {
639d11d3 5221 struct die_info *die
e7c27a73 5222 = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
639d11d3 5223
1d325ec1 5224 if (die == NULL)
c906108c 5225 {
639d11d3
DC
5226 *new_info_ptr = cur_ptr;
5227 return first_die;
c906108c 5228 }
1d325ec1
DJ
5229
5230 if (!first_die)
5231 first_die = die;
c906108c 5232 else
1d325ec1
DJ
5233 last_sibling->sibling = die;
5234
5235 last_sibling = die;
c906108c 5236 }
c906108c
SS
5237}
5238
233a11ab
CS
5239/* Decompress a section that was compressed using zlib. Store the
5240 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
5241
5242static void
5243zlib_decompress_section (struct objfile *objfile, asection *sectp,
5244 gdb_byte **outbuf, bfd_size_type *outsize)
5245{
ef72380d 5246 bfd *abfd = objfile->obfd;
233a11ab
CS
5247#ifndef HAVE_ZLIB_H
5248 error (_("Support for zlib-compressed DWARF data (from '%s') "
5249 "is disabled in this copy of GDB"),
5250 bfd_get_filename (abfd));
5251#else
233a11ab
CS
5252 bfd_size_type compressed_size = bfd_get_section_size (sectp);
5253 gdb_byte *compressed_buffer = xmalloc (compressed_size);
5254 bfd_size_type uncompressed_size;
5255 gdb_byte *uncompressed_buffer;
5256 z_stream strm;
5257 int rc;
5258 int header_size = 12;
5259
5260 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5261 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
5262 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
5263 bfd_get_filename (abfd));
5264
5265 /* Read the zlib header. In this case, it should be "ZLIB" followed
5266 by the uncompressed section size, 8 bytes in big-endian order. */
5267 if (compressed_size < header_size
5268 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
5269 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
5270 bfd_get_filename (abfd));
5271 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
5272 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
5273 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
5274 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
5275 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
5276 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
5277 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
5278 uncompressed_size += compressed_buffer[11];
5279
5280 /* It is possible the section consists of several compressed
5281 buffers concatenated together, so we uncompress in a loop. */
5282 strm.zalloc = NULL;
5283 strm.zfree = NULL;
5284 strm.opaque = NULL;
5285 strm.avail_in = compressed_size - header_size;
5286 strm.next_in = (Bytef*) compressed_buffer + header_size;
5287 strm.avail_out = uncompressed_size;
5288 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
5289 uncompressed_size);
5290 rc = inflateInit (&strm);
5291 while (strm.avail_in > 0)
5292 {
5293 if (rc != Z_OK)
5294 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
5295 bfd_get_filename (abfd), rc);
5296 strm.next_out = ((Bytef*) uncompressed_buffer
5297 + (uncompressed_size - strm.avail_out));
5298 rc = inflate (&strm, Z_FINISH);
5299 if (rc != Z_STREAM_END)
5300 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
5301 bfd_get_filename (abfd), rc);
5302 rc = inflateReset (&strm);
5303 }
5304 rc = inflateEnd (&strm);
5305 if (rc != Z_OK
5306 || strm.avail_out != 0)
5307 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
5308 bfd_get_filename (abfd), rc);
5309
5310 xfree (compressed_buffer);
5311 *outbuf = uncompressed_buffer;
5312 *outsize = uncompressed_size;
5313#endif
5314}
5315
5316
c906108c 5317/* Read the contents of the section at OFFSET and of size SIZE from the
233a11ab
CS
5318 object file specified by OBJFILE into the objfile_obstack and return it.
5319 If the section is compressed, uncompress it before returning. */
c906108c 5320
fe1b8b76 5321gdb_byte *
188dd5d6 5322dwarf2_read_section (struct objfile *objfile, asection *sectp)
c906108c
SS
5323{
5324 bfd *abfd = objfile->obfd;
fe1b8b76 5325 gdb_byte *buf, *retbuf;
2c500098 5326 bfd_size_type size = bfd_get_section_size (sectp);
233a11ab 5327 unsigned char header[4];
c906108c
SS
5328
5329 if (size == 0)
5330 return NULL;
5331
233a11ab
CS
5332 /* Check if the file has a 4-byte header indicating compression. */
5333 if (size > sizeof (header)
5334 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
5335 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
5336 {
5337 /* Upon decompression, update the buffer and its size. */
5338 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
5339 {
5340 zlib_decompress_section (objfile, sectp, &buf, &size);
5341 dwarf2_resize_section (sectp, size);
5342 return buf;
5343 }
5344 }
5345
5346 /* If we get here, we are a normal, not-compressed section. */
fe1b8b76 5347 buf = obstack_alloc (&objfile->objfile_obstack, size);
233a11ab
CS
5348 /* When debugging .o files, we may need to apply relocations; see
5349 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
5350 We never compress sections in .o files, so we only need to
5351 try this when the section is not compressed. */
fe1b8b76 5352 retbuf = symfile_relocate_debug_section (abfd, sectp, buf);
086df311
DJ
5353 if (retbuf != NULL)
5354 return retbuf;
5355
188dd5d6
DJ
5356 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5357 || bfd_bread (buf, size, abfd) != size)
8a3fe4f8 5358 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
188dd5d6
DJ
5359 bfd_get_filename (abfd));
5360
c906108c
SS
5361 return buf;
5362}
5363
5364/* In DWARF version 2, the description of the debugging information is
5365 stored in a separate .debug_abbrev section. Before we read any
5366 dies from a section we read in all abbreviations and install them
72bf9492
DJ
5367 in a hash table. This function also sets flags in CU describing
5368 the data found in the abbrev table. */
c906108c
SS
5369
5370static void
e7c27a73 5371dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 5372{
e7c27a73 5373 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 5374 gdb_byte *abbrev_ptr;
c906108c
SS
5375 struct abbrev_info *cur_abbrev;
5376 unsigned int abbrev_number, bytes_read, abbrev_name;
5377 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
5378 struct attr_abbrev *cur_attrs;
5379 unsigned int allocated_attrs;
c906108c 5380
57349743 5381 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
5382 obstack_init (&cu->abbrev_obstack);
5383 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
5384 (ABBREV_HASH_SIZE
5385 * sizeof (struct abbrev_info *)));
5386 memset (cu->dwarf2_abbrevs, 0,
5387 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 5388
6502dd73 5389 abbrev_ptr = dwarf2_per_objfile->abbrev_buffer + cu_header->abbrev_offset;
c906108c
SS
5390 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5391 abbrev_ptr += bytes_read;
5392
f3dd6933
DJ
5393 allocated_attrs = ATTR_ALLOC_CHUNK;
5394 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
5395
c906108c
SS
5396 /* loop until we reach an abbrev number of 0 */
5397 while (abbrev_number)
5398 {
f3dd6933 5399 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
5400
5401 /* read in abbrev header */
5402 cur_abbrev->number = abbrev_number;
5403 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5404 abbrev_ptr += bytes_read;
5405 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
5406 abbrev_ptr += 1;
5407
72bf9492
DJ
5408 if (cur_abbrev->tag == DW_TAG_namespace)
5409 cu->has_namespace_info = 1;
5410
c906108c
SS
5411 /* now read in declarations */
5412 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5413 abbrev_ptr += bytes_read;
5414 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5415 abbrev_ptr += bytes_read;
5416 while (abbrev_name)
5417 {
f3dd6933 5418 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 5419 {
f3dd6933
DJ
5420 allocated_attrs += ATTR_ALLOC_CHUNK;
5421 cur_attrs
5422 = xrealloc (cur_attrs, (allocated_attrs
5423 * sizeof (struct attr_abbrev)));
c906108c 5424 }
ae038cb0
DJ
5425
5426 /* Record whether this compilation unit might have
5427 inter-compilation-unit references. If we don't know what form
5428 this attribute will have, then it might potentially be a
5429 DW_FORM_ref_addr, so we conservatively expect inter-CU
5430 references. */
5431
5432 if (abbrev_form == DW_FORM_ref_addr
5433 || abbrev_form == DW_FORM_indirect)
5434 cu->has_form_ref_addr = 1;
5435
f3dd6933
DJ
5436 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
5437 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
5438 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5439 abbrev_ptr += bytes_read;
5440 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5441 abbrev_ptr += bytes_read;
5442 }
5443
f3dd6933
DJ
5444 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
5445 (cur_abbrev->num_attrs
5446 * sizeof (struct attr_abbrev)));
5447 memcpy (cur_abbrev->attrs, cur_attrs,
5448 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
5449
c906108c 5450 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
5451 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
5452 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
5453
5454 /* Get next abbreviation.
5455 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
5456 always properly terminated with an abbrev number of 0.
5457 Exit loop if we encounter an abbreviation which we have
5458 already read (which means we are about to read the abbreviations
5459 for the next compile unit) or if the end of the abbreviation
5460 table is reached. */
6502dd73
DJ
5461 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev_buffer)
5462 >= dwarf2_per_objfile->abbrev_size)
c906108c
SS
5463 break;
5464 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5465 abbrev_ptr += bytes_read;
e7c27a73 5466 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
5467 break;
5468 }
f3dd6933
DJ
5469
5470 xfree (cur_attrs);
c906108c
SS
5471}
5472
f3dd6933 5473/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 5474
c906108c 5475static void
f3dd6933 5476dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 5477{
f3dd6933 5478 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 5479
f3dd6933
DJ
5480 obstack_free (&cu->abbrev_obstack, NULL);
5481 cu->dwarf2_abbrevs = NULL;
c906108c
SS
5482}
5483
5484/* Lookup an abbrev_info structure in the abbrev hash table. */
5485
5486static struct abbrev_info *
e7c27a73 5487dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
5488{
5489 unsigned int hash_number;
5490 struct abbrev_info *abbrev;
5491
5492 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 5493 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
5494
5495 while (abbrev)
5496 {
5497 if (abbrev->number == number)
5498 return abbrev;
5499 else
5500 abbrev = abbrev->next;
5501 }
5502 return NULL;
5503}
5504
72bf9492
DJ
5505/* Returns nonzero if TAG represents a type that we might generate a partial
5506 symbol for. */
5507
5508static int
5509is_type_tag_for_partial (int tag)
5510{
5511 switch (tag)
5512 {
5513#if 0
5514 /* Some types that would be reasonable to generate partial symbols for,
5515 that we don't at present. */
5516 case DW_TAG_array_type:
5517 case DW_TAG_file_type:
5518 case DW_TAG_ptr_to_member_type:
5519 case DW_TAG_set_type:
5520 case DW_TAG_string_type:
5521 case DW_TAG_subroutine_type:
5522#endif
5523 case DW_TAG_base_type:
5524 case DW_TAG_class_type:
680b30c7 5525 case DW_TAG_interface_type:
72bf9492
DJ
5526 case DW_TAG_enumeration_type:
5527 case DW_TAG_structure_type:
5528 case DW_TAG_subrange_type:
5529 case DW_TAG_typedef:
5530 case DW_TAG_union_type:
5531 return 1;
5532 default:
5533 return 0;
5534 }
5535}
5536
5537/* Load all DIEs that are interesting for partial symbols into memory. */
5538
5539static struct partial_die_info *
fe1b8b76 5540load_partial_dies (bfd *abfd, gdb_byte *info_ptr, int building_psymtab,
72bf9492
DJ
5541 struct dwarf2_cu *cu)
5542{
5543 struct partial_die_info *part_die;
5544 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
5545 struct abbrev_info *abbrev;
5546 unsigned int bytes_read;
5afb4e99 5547 unsigned int load_all = 0;
72bf9492
DJ
5548
5549 int nesting_level = 1;
5550
5551 parent_die = NULL;
5552 last_die = NULL;
5553
5afb4e99
DJ
5554 if (cu->per_cu && cu->per_cu->load_all_dies)
5555 load_all = 1;
5556
72bf9492
DJ
5557 cu->partial_dies
5558 = htab_create_alloc_ex (cu->header.length / 12,
5559 partial_die_hash,
5560 partial_die_eq,
5561 NULL,
5562 &cu->comp_unit_obstack,
5563 hashtab_obstack_allocate,
5564 dummy_obstack_deallocate);
5565
5566 part_die = obstack_alloc (&cu->comp_unit_obstack,
5567 sizeof (struct partial_die_info));
5568
5569 while (1)
5570 {
5571 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5572
5573 /* A NULL abbrev means the end of a series of children. */
5574 if (abbrev == NULL)
5575 {
5576 if (--nesting_level == 0)
5577 {
5578 /* PART_DIE was probably the last thing allocated on the
5579 comp_unit_obstack, so we could call obstack_free
5580 here. We don't do that because the waste is small,
5581 and will be cleaned up when we're done with this
5582 compilation unit. This way, we're also more robust
5583 against other users of the comp_unit_obstack. */
5584 return first_die;
5585 }
5586 info_ptr += bytes_read;
5587 last_die = parent_die;
5588 parent_die = parent_die->die_parent;
5589 continue;
5590 }
5591
5afb4e99
DJ
5592 /* Check whether this DIE is interesting enough to save. Normally
5593 we would not be interested in members here, but there may be
5594 later variables referencing them via DW_AT_specification (for
5595 static members). */
5596 if (!load_all
5597 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
5598 && abbrev->tag != DW_TAG_enumerator
5599 && abbrev->tag != DW_TAG_subprogram
bc30ff58 5600 && abbrev->tag != DW_TAG_lexical_block
72bf9492 5601 && abbrev->tag != DW_TAG_variable
5afb4e99
DJ
5602 && abbrev->tag != DW_TAG_namespace
5603 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
5604 {
5605 /* Otherwise we skip to the next sibling, if any. */
5606 info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
5607 continue;
5608 }
5609
5610 info_ptr = read_partial_die (part_die, abbrev, bytes_read,
5611 abfd, info_ptr, cu);
5612
5613 /* This two-pass algorithm for processing partial symbols has a
5614 high cost in cache pressure. Thus, handle some simple cases
5615 here which cover the majority of C partial symbols. DIEs
5616 which neither have specification tags in them, nor could have
5617 specification tags elsewhere pointing at them, can simply be
5618 processed and discarded.
5619
5620 This segment is also optional; scan_partial_symbols and
5621 add_partial_symbol will handle these DIEs if we chain
5622 them in normally. When compilers which do not emit large
5623 quantities of duplicate debug information are more common,
5624 this code can probably be removed. */
5625
5626 /* Any complete simple types at the top level (pretty much all
5627 of them, for a language without namespaces), can be processed
5628 directly. */
5629 if (parent_die == NULL
5630 && part_die->has_specification == 0
5631 && part_die->is_declaration == 0
5632 && (part_die->tag == DW_TAG_typedef
5633 || part_die->tag == DW_TAG_base_type
5634 || part_die->tag == DW_TAG_subrange_type))
5635 {
5636 if (building_psymtab && part_die->name != NULL)
5637 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5638 VAR_DOMAIN, LOC_TYPEDEF,
5639 &cu->objfile->static_psymbols,
5640 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5641 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5642 continue;
5643 }
5644
5645 /* If we're at the second level, and we're an enumerator, and
5646 our parent has no specification (meaning possibly lives in a
5647 namespace elsewhere), then we can add the partial symbol now
5648 instead of queueing it. */
5649 if (part_die->tag == DW_TAG_enumerator
5650 && parent_die != NULL
5651 && parent_die->die_parent == NULL
5652 && parent_die->tag == DW_TAG_enumeration_type
5653 && parent_die->has_specification == 0)
5654 {
5655 if (part_die->name == NULL)
e2e0b3e5 5656 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492
DJ
5657 else if (building_psymtab)
5658 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5659 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5660 (cu->language == language_cplus
5661 || cu->language == language_java)
72bf9492
DJ
5662 ? &cu->objfile->global_psymbols
5663 : &cu->objfile->static_psymbols,
5664 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5665
5666 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5667 continue;
5668 }
5669
5670 /* We'll save this DIE so link it in. */
5671 part_die->die_parent = parent_die;
5672 part_die->die_sibling = NULL;
5673 part_die->die_child = NULL;
5674
5675 if (last_die && last_die == parent_die)
5676 last_die->die_child = part_die;
5677 else if (last_die)
5678 last_die->die_sibling = part_die;
5679
5680 last_die = part_die;
5681
5682 if (first_die == NULL)
5683 first_die = part_die;
5684
5685 /* Maybe add the DIE to the hash table. Not all DIEs that we
5686 find interesting need to be in the hash table, because we
5687 also have the parent/sibling/child chains; only those that we
5688 might refer to by offset later during partial symbol reading.
5689
5690 For now this means things that might have be the target of a
5691 DW_AT_specification, DW_AT_abstract_origin, or
5692 DW_AT_extension. DW_AT_extension will refer only to
5693 namespaces; DW_AT_abstract_origin refers to functions (and
5694 many things under the function DIE, but we do not recurse
5695 into function DIEs during partial symbol reading) and
5696 possibly variables as well; DW_AT_specification refers to
5697 declarations. Declarations ought to have the DW_AT_declaration
5698 flag. It happens that GCC forgets to put it in sometimes, but
5699 only for functions, not for types.
5700
5701 Adding more things than necessary to the hash table is harmless
5702 except for the performance cost. Adding too few will result in
5afb4e99
DJ
5703 wasted time in find_partial_die, when we reread the compilation
5704 unit with load_all_dies set. */
72bf9492 5705
5afb4e99
DJ
5706 if (load_all
5707 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
5708 || abbrev->tag == DW_TAG_variable
5709 || abbrev->tag == DW_TAG_namespace
5710 || part_die->is_declaration)
5711 {
5712 void **slot;
5713
5714 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
5715 part_die->offset, INSERT);
5716 *slot = part_die;
5717 }
5718
5719 part_die = obstack_alloc (&cu->comp_unit_obstack,
5720 sizeof (struct partial_die_info));
5721
5722 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 5723 we have no reason to follow the children of structures; for other
72bf9492 5724 languages we have to, both so that we can get at method physnames
bc30ff58
JB
5725 to infer fully qualified class names, and for DW_AT_specification.
5726
5727 For Ada, we need to scan the children of subprograms and lexical
5728 blocks as well because Ada allows the definition of nested
5729 entities that could be interesting for the debugger, such as
5730 nested subprograms for instance. */
72bf9492 5731 if (last_die->has_children
5afb4e99
DJ
5732 && (load_all
5733 || last_die->tag == DW_TAG_namespace
72bf9492
DJ
5734 || last_die->tag == DW_TAG_enumeration_type
5735 || (cu->language != language_c
5736 && (last_die->tag == DW_TAG_class_type
680b30c7 5737 || last_die->tag == DW_TAG_interface_type
72bf9492 5738 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
5739 || last_die->tag == DW_TAG_union_type))
5740 || (cu->language == language_ada
5741 && (last_die->tag == DW_TAG_subprogram
5742 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
5743 {
5744 nesting_level++;
5745 parent_die = last_die;
5746 continue;
5747 }
5748
5749 /* Otherwise we skip to the next sibling, if any. */
5750 info_ptr = locate_pdi_sibling (last_die, info_ptr, abfd, cu);
5751
5752 /* Back to the top, do it again. */
5753 }
5754}
5755
c906108c
SS
5756/* Read a minimal amount of information into the minimal die structure. */
5757
fe1b8b76 5758static gdb_byte *
72bf9492
DJ
5759read_partial_die (struct partial_die_info *part_die,
5760 struct abbrev_info *abbrev,
5761 unsigned int abbrev_len, bfd *abfd,
fe1b8b76 5762 gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 5763{
72bf9492 5764 unsigned int bytes_read, i;
c906108c 5765 struct attribute attr;
c5aa993b 5766 int has_low_pc_attr = 0;
c906108c 5767 int has_high_pc_attr = 0;
e7030f15 5768 CORE_ADDR base_address = 0;
85cbf3d3
JK
5769 enum
5770 {
5771 base_address_none,
5772 base_address_low_pc,
5773 /* Overrides BASE_ADDRESS_LOW_PC. */
5774 base_address_entry_pc
5775 }
5776 base_address_type = base_address_none;
c906108c 5777
72bf9492 5778 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 5779
6502dd73 5780 part_die->offset = info_ptr - dwarf2_per_objfile->info_buffer;
72bf9492
DJ
5781
5782 info_ptr += abbrev_len;
5783
5784 if (abbrev == NULL)
5785 return info_ptr;
5786
c906108c
SS
5787 part_die->tag = abbrev->tag;
5788 part_die->has_children = abbrev->has_children;
c906108c
SS
5789
5790 for (i = 0; i < abbrev->num_attrs; ++i)
5791 {
e7c27a73 5792 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
5793
5794 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 5795 partial symbol table. */
c906108c
SS
5796 switch (attr.name)
5797 {
5798 case DW_AT_name:
5799
5800 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
5801 if (part_die->name == NULL)
5802 part_die->name = DW_STRING (&attr);
5803 break;
57c22c6c
BR
5804 case DW_AT_comp_dir:
5805 if (part_die->dirname == NULL)
5806 part_die->dirname = DW_STRING (&attr);
5807 break;
c906108c
SS
5808 case DW_AT_MIPS_linkage_name:
5809 part_die->name = DW_STRING (&attr);
5810 break;
5811 case DW_AT_low_pc:
5812 has_low_pc_attr = 1;
5813 part_die->lowpc = DW_ADDR (&attr);
85cbf3d3
JK
5814 if (part_die->tag == DW_TAG_compile_unit
5815 && base_address_type < base_address_low_pc)
5816 {
5817 base_address = DW_ADDR (&attr);
5818 base_address_type = base_address_low_pc;
5819 }
c906108c
SS
5820 break;
5821 case DW_AT_high_pc:
5822 has_high_pc_attr = 1;
5823 part_die->highpc = DW_ADDR (&attr);
5824 break;
85cbf3d3
JK
5825 case DW_AT_entry_pc:
5826 if (part_die->tag == DW_TAG_compile_unit
5827 && base_address_type < base_address_entry_pc)
5828 {
5829 base_address = DW_ADDR (&attr);
5830 base_address_type = base_address_entry_pc;
5831 }
5832 break;
43039443 5833 case DW_AT_ranges:
ff013f42
JK
5834 if (part_die->tag == DW_TAG_compile_unit)
5835 {
5836 cu->ranges_offset = DW_UNSND (&attr);
5837 cu->has_ranges_offset = 1;
5838 }
43039443 5839 break;
c906108c 5840 case DW_AT_location:
8e19ed76
PS
5841 /* Support the .debug_loc offsets */
5842 if (attr_form_is_block (&attr))
5843 {
5844 part_die->locdesc = DW_BLOCK (&attr);
5845 }
3690dd37 5846 else if (attr_form_is_section_offset (&attr))
8e19ed76 5847 {
4d3c2250 5848 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
5849 }
5850 else
5851 {
4d3c2250
KB
5852 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5853 "partial symbol information");
8e19ed76 5854 }
c906108c
SS
5855 break;
5856 case DW_AT_language:
5857 part_die->language = DW_UNSND (&attr);
5858 break;
5859 case DW_AT_external:
5860 part_die->is_external = DW_UNSND (&attr);
5861 break;
5862 case DW_AT_declaration:
5863 part_die->is_declaration = DW_UNSND (&attr);
5864 break;
5865 case DW_AT_type:
5866 part_die->has_type = 1;
5867 break;
5868 case DW_AT_abstract_origin:
5869 case DW_AT_specification:
72bf9492
DJ
5870 case DW_AT_extension:
5871 part_die->has_specification = 1;
5872 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr, cu);
c906108c
SS
5873 break;
5874 case DW_AT_sibling:
5875 /* Ignore absolute siblings, they might point outside of
5876 the current compile unit. */
5877 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 5878 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 5879 else
6502dd73
DJ
5880 part_die->sibling = dwarf2_per_objfile->info_buffer
5881 + dwarf2_get_ref_die_offset (&attr, cu);
c906108c 5882 break;
aaa75496
JB
5883 case DW_AT_stmt_list:
5884 part_die->has_stmt_list = 1;
5885 part_die->line_offset = DW_UNSND (&attr);
5886 break;
fa4028e9
JB
5887 case DW_AT_byte_size:
5888 part_die->has_byte_size = 1;
5889 break;
68511cec
CES
5890 case DW_AT_calling_convention:
5891 /* DWARF doesn't provide a way to identify a program's source-level
5892 entry point. DW_AT_calling_convention attributes are only meant
5893 to describe functions' calling conventions.
5894
5895 However, because it's a necessary piece of information in
5896 Fortran, and because DW_CC_program is the only piece of debugging
5897 information whose definition refers to a 'main program' at all,
5898 several compilers have begun marking Fortran main programs with
5899 DW_CC_program --- even when those functions use the standard
5900 calling conventions.
5901
5902 So until DWARF specifies a way to provide this information and
5903 compilers pick up the new representation, we'll support this
5904 practice. */
5905 if (DW_UNSND (&attr) == DW_CC_program
5906 && cu->language == language_fortran)
5907 set_main_name (part_die->name);
5908 break;
c906108c
SS
5909 default:
5910 break;
5911 }
5912 }
5913
c906108c
SS
5914 /* When using the GNU linker, .gnu.linkonce. sections are used to
5915 eliminate duplicate copies of functions and vtables and such.
5916 The linker will arbitrarily choose one and discard the others.
5917 The AT_*_pc values for such functions refer to local labels in
5918 these sections. If the section from that file was discarded, the
5919 labels are not in the output, so the relocs get a value of 0.
5920 If this is a discarded function, mark the pc bounds as invalid,
5921 so that GDB will ignore it. */
5922 if (has_low_pc_attr && has_high_pc_attr
5923 && part_die->lowpc < part_die->highpc
5924 && (part_die->lowpc != 0
72dca2f5 5925 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 5926 part_die->has_pc_info = 1;
85cbf3d3 5927
d00adf39 5928 if (base_address_type != base_address_none && !cu->base_known)
85cbf3d3
JK
5929 {
5930 gdb_assert (part_die->tag == DW_TAG_compile_unit);
d00adf39
DE
5931 cu->base_known = 1;
5932 cu->base_address = base_address;
85cbf3d3
JK
5933 }
5934
c906108c
SS
5935 return info_ptr;
5936}
5937
72bf9492
DJ
5938/* Find a cached partial DIE at OFFSET in CU. */
5939
5940static struct partial_die_info *
5941find_partial_die_in_comp_unit (unsigned long offset, struct dwarf2_cu *cu)
5942{
5943 struct partial_die_info *lookup_die = NULL;
5944 struct partial_die_info part_die;
5945
5946 part_die.offset = offset;
5947 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
5948
72bf9492
DJ
5949 return lookup_die;
5950}
5951
5952/* Find a partial DIE at OFFSET, which may or may not be in CU. */
5953
5954static struct partial_die_info *
10b3939b 5955find_partial_die (unsigned long offset, struct dwarf2_cu *cu)
72bf9492 5956{
5afb4e99
DJ
5957 struct dwarf2_per_cu_data *per_cu = NULL;
5958 struct partial_die_info *pd = NULL;
72bf9492
DJ
5959
5960 if (offset >= cu->header.offset
5961 && offset < cu->header.offset + cu->header.length)
5afb4e99
DJ
5962 {
5963 pd = find_partial_die_in_comp_unit (offset, cu);
5964 if (pd != NULL)
5965 return pd;
5966 }
72bf9492 5967
ae038cb0
DJ
5968 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5969
ae038cb0
DJ
5970 if (per_cu->cu == NULL)
5971 {
5972 load_comp_unit (per_cu, cu->objfile);
5973 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5974 dwarf2_per_objfile->read_in_chain = per_cu;
5975 }
5976
5977 per_cu->cu->last_used = 0;
5afb4e99
DJ
5978 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
5979
5980 if (pd == NULL && per_cu->load_all_dies == 0)
5981 {
5982 struct cleanup *back_to;
5983 struct partial_die_info comp_unit_die;
5984 struct abbrev_info *abbrev;
5985 unsigned int bytes_read;
5986 char *info_ptr;
5987
5988 per_cu->load_all_dies = 1;
5989
5990 /* Re-read the DIEs. */
5991 back_to = make_cleanup (null_cleanup, 0);
5992 if (per_cu->cu->dwarf2_abbrevs == NULL)
5993 {
5994 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
5995 back_to = make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5996 }
d00adf39
DE
5997 info_ptr = (dwarf2_per_objfile->info_buffer
5998 + per_cu->cu->header.offset
5999 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
6000 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6001 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
6002 per_cu->cu->objfile->obfd, info_ptr,
6003 per_cu->cu);
6004 if (comp_unit_die.has_children)
6005 load_partial_dies (per_cu->cu->objfile->obfd, info_ptr, 0, per_cu->cu);
6006 do_cleanups (back_to);
6007
6008 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6009 }
6010
6011 if (pd == NULL)
6012 internal_error (__FILE__, __LINE__,
6013 _("could not find partial DIE 0x%lx in cache [from module %s]\n"),
6014 offset, bfd_get_filename (cu->objfile->obfd));
6015 return pd;
72bf9492
DJ
6016}
6017
6018/* Adjust PART_DIE before generating a symbol for it. This function
6019 may set the is_external flag or change the DIE's name. */
6020
6021static void
6022fixup_partial_die (struct partial_die_info *part_die,
6023 struct dwarf2_cu *cu)
6024{
6025 /* If we found a reference attribute and the DIE has no name, try
6026 to find a name in the referred to DIE. */
6027
6028 if (part_die->name == NULL && part_die->has_specification)
6029 {
6030 struct partial_die_info *spec_die;
72bf9492 6031
10b3939b 6032 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 6033
10b3939b 6034 fixup_partial_die (spec_die, cu);
72bf9492
DJ
6035
6036 if (spec_die->name)
6037 {
6038 part_die->name = spec_die->name;
6039
6040 /* Copy DW_AT_external attribute if it is set. */
6041 if (spec_die->is_external)
6042 part_die->is_external = spec_die->is_external;
6043 }
6044 }
6045
6046 /* Set default names for some unnamed DIEs. */
6047 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
6048 || part_die->tag == DW_TAG_class_type))
6049 part_die->name = "(anonymous class)";
6050
6051 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
6052 part_die->name = "(anonymous namespace)";
6053
6054 if (part_die->tag == DW_TAG_structure_type
6055 || part_die->tag == DW_TAG_class_type
6056 || part_die->tag == DW_TAG_union_type)
6057 guess_structure_name (part_die, cu);
6058}
6059
639d11d3
DC
6060/* Read the die from the .debug_info section buffer. Set DIEP to
6061 point to a newly allocated die with its information, except for its
6062 child, sibling, and parent fields. Set HAS_CHILDREN to tell
6063 whether the die has children or not. */
c906108c 6064
fe1b8b76
JB
6065static gdb_byte *
6066read_full_die (struct die_info **diep, bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6067 struct dwarf2_cu *cu, int *has_children)
c906108c
SS
6068{
6069 unsigned int abbrev_number, bytes_read, i, offset;
6070 struct abbrev_info *abbrev;
6071 struct die_info *die;
6072
6502dd73 6073 offset = info_ptr - dwarf2_per_objfile->info_buffer;
c906108c
SS
6074 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6075 info_ptr += bytes_read;
6076 if (!abbrev_number)
6077 {
1d325ec1 6078 *diep = NULL;
639d11d3 6079 *has_children = 0;
c906108c
SS
6080 return info_ptr;
6081 }
6082
e7c27a73 6083 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
c906108c
SS
6084 if (!abbrev)
6085 {
8a3fe4f8 6086 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
72bf9492 6087 abbrev_number,
639d11d3 6088 bfd_get_filename (abfd));
c906108c 6089 }
b60c80d6 6090 die = dwarf_alloc_die (cu, abbrev->num_attrs);
c906108c
SS
6091 die->offset = offset;
6092 die->tag = abbrev->tag;
c906108c 6093 die->abbrev = abbrev_number;
c906108c
SS
6094
6095 die->num_attrs = abbrev->num_attrs;
c906108c
SS
6096
6097 for (i = 0; i < abbrev->num_attrs; ++i)
03dd20cc
DJ
6098 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6099 abfd, info_ptr, cu);
c906108c
SS
6100
6101 *diep = die;
639d11d3 6102 *has_children = abbrev->has_children;
c906108c
SS
6103 return info_ptr;
6104}
6105
a8329558 6106/* Read an attribute value described by an attribute form. */
c906108c 6107
fe1b8b76 6108static gdb_byte *
a8329558 6109read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 6110 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6111 struct dwarf2_cu *cu)
c906108c 6112{
e7c27a73 6113 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6114 unsigned int bytes_read;
6115 struct dwarf_block *blk;
6116
a8329558
KW
6117 attr->form = form;
6118 switch (form)
c906108c
SS
6119 {
6120 case DW_FORM_addr:
6121 case DW_FORM_ref_addr:
e7c27a73 6122 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 6123 info_ptr += bytes_read;
c906108c
SS
6124 break;
6125 case DW_FORM_block2:
7b5a2f43 6126 blk = dwarf_alloc_block (cu);
c906108c
SS
6127 blk->size = read_2_bytes (abfd, info_ptr);
6128 info_ptr += 2;
6129 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6130 info_ptr += blk->size;
6131 DW_BLOCK (attr) = blk;
6132 break;
6133 case DW_FORM_block4:
7b5a2f43 6134 blk = dwarf_alloc_block (cu);
c906108c
SS
6135 blk->size = read_4_bytes (abfd, info_ptr);
6136 info_ptr += 4;
6137 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6138 info_ptr += blk->size;
6139 DW_BLOCK (attr) = blk;
6140 break;
6141 case DW_FORM_data2:
6142 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
6143 info_ptr += 2;
6144 break;
6145 case DW_FORM_data4:
6146 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
6147 info_ptr += 4;
6148 break;
6149 case DW_FORM_data8:
6150 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
6151 info_ptr += 8;
6152 break;
6153 case DW_FORM_string:
6154 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
6155 info_ptr += bytes_read;
6156 break;
4bdf3d34
JJ
6157 case DW_FORM_strp:
6158 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
6159 &bytes_read);
6160 info_ptr += bytes_read;
6161 break;
c906108c 6162 case DW_FORM_block:
7b5a2f43 6163 blk = dwarf_alloc_block (cu);
c906108c
SS
6164 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6165 info_ptr += bytes_read;
6166 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6167 info_ptr += blk->size;
6168 DW_BLOCK (attr) = blk;
6169 break;
6170 case DW_FORM_block1:
7b5a2f43 6171 blk = dwarf_alloc_block (cu);
c906108c
SS
6172 blk->size = read_1_byte (abfd, info_ptr);
6173 info_ptr += 1;
6174 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6175 info_ptr += blk->size;
6176 DW_BLOCK (attr) = blk;
6177 break;
6178 case DW_FORM_data1:
6179 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6180 info_ptr += 1;
6181 break;
6182 case DW_FORM_flag:
6183 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6184 info_ptr += 1;
6185 break;
6186 case DW_FORM_sdata:
6187 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
6188 info_ptr += bytes_read;
6189 break;
6190 case DW_FORM_udata:
6191 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6192 info_ptr += bytes_read;
6193 break;
6194 case DW_FORM_ref1:
10b3939b 6195 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
6196 info_ptr += 1;
6197 break;
6198 case DW_FORM_ref2:
10b3939b 6199 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
6200 info_ptr += 2;
6201 break;
6202 case DW_FORM_ref4:
10b3939b 6203 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
6204 info_ptr += 4;
6205 break;
613e1657 6206 case DW_FORM_ref8:
10b3939b 6207 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
6208 info_ptr += 8;
6209 break;
c906108c 6210 case DW_FORM_ref_udata:
10b3939b
DJ
6211 DW_ADDR (attr) = (cu->header.offset
6212 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
6213 info_ptr += bytes_read;
6214 break;
c906108c 6215 case DW_FORM_indirect:
a8329558
KW
6216 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6217 info_ptr += bytes_read;
e7c27a73 6218 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 6219 break;
c906108c 6220 default:
8a3fe4f8 6221 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
6222 dwarf_form_name (form),
6223 bfd_get_filename (abfd));
c906108c 6224 }
28e94949
JB
6225
6226 /* We have seen instances where the compiler tried to emit a byte
6227 size attribute of -1 which ended up being encoded as an unsigned
6228 0xffffffff. Although 0xffffffff is technically a valid size value,
6229 an object of this size seems pretty unlikely so we can relatively
6230 safely treat these cases as if the size attribute was invalid and
6231 treat them as zero by default. */
6232 if (attr->name == DW_AT_byte_size
6233 && form == DW_FORM_data4
6234 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
6235 {
6236 complaint
6237 (&symfile_complaints,
6238 _("Suspicious DW_AT_byte_size value treated as zero instead of 0x%lx"),
6239 DW_UNSND (attr));
6240 DW_UNSND (attr) = 0;
6241 }
28e94949 6242
c906108c
SS
6243 return info_ptr;
6244}
6245
a8329558
KW
6246/* Read an attribute described by an abbreviated attribute. */
6247
fe1b8b76 6248static gdb_byte *
a8329558 6249read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 6250 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
6251{
6252 attr->name = abbrev->name;
e7c27a73 6253 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
6254}
6255
c906108c
SS
6256/* read dwarf information from a buffer */
6257
6258static unsigned int
fe1b8b76 6259read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 6260{
fe1b8b76 6261 return bfd_get_8 (abfd, buf);
c906108c
SS
6262}
6263
6264static int
fe1b8b76 6265read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 6266{
fe1b8b76 6267 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
6268}
6269
6270static unsigned int
fe1b8b76 6271read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6272{
fe1b8b76 6273 return bfd_get_16 (abfd, buf);
c906108c
SS
6274}
6275
6276static int
fe1b8b76 6277read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6278{
fe1b8b76 6279 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
6280}
6281
6282static unsigned int
fe1b8b76 6283read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6284{
fe1b8b76 6285 return bfd_get_32 (abfd, buf);
c906108c
SS
6286}
6287
6288static int
fe1b8b76 6289read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6290{
fe1b8b76 6291 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
6292}
6293
ce5d95e1 6294static unsigned long
fe1b8b76 6295read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6296{
fe1b8b76 6297 return bfd_get_64 (abfd, buf);
c906108c
SS
6298}
6299
6300static CORE_ADDR
fe1b8b76 6301read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 6302 unsigned int *bytes_read)
c906108c 6303{
e7c27a73 6304 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6305 CORE_ADDR retval = 0;
6306
107d2387 6307 if (cu_header->signed_addr_p)
c906108c 6308 {
107d2387
AC
6309 switch (cu_header->addr_size)
6310 {
6311 case 2:
fe1b8b76 6312 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
6313 break;
6314 case 4:
fe1b8b76 6315 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
6316 break;
6317 case 8:
fe1b8b76 6318 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
6319 break;
6320 default:
8e65ff28 6321 internal_error (__FILE__, __LINE__,
e2e0b3e5 6322 _("read_address: bad switch, signed [in module %s]"),
659b0389 6323 bfd_get_filename (abfd));
107d2387
AC
6324 }
6325 }
6326 else
6327 {
6328 switch (cu_header->addr_size)
6329 {
6330 case 2:
fe1b8b76 6331 retval = bfd_get_16 (abfd, buf);
107d2387
AC
6332 break;
6333 case 4:
fe1b8b76 6334 retval = bfd_get_32 (abfd, buf);
107d2387
AC
6335 break;
6336 case 8:
fe1b8b76 6337 retval = bfd_get_64 (abfd, buf);
107d2387
AC
6338 break;
6339 default:
8e65ff28 6340 internal_error (__FILE__, __LINE__,
e2e0b3e5 6341 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 6342 bfd_get_filename (abfd));
107d2387 6343 }
c906108c 6344 }
64367e0a 6345
107d2387
AC
6346 *bytes_read = cu_header->addr_size;
6347 return retval;
c906108c
SS
6348}
6349
f7ef9339 6350/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
6351 specification allows the initial length to take up either 4 bytes
6352 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
6353 bytes describe the length and all offsets will be 8 bytes in length
6354 instead of 4.
6355
f7ef9339
KB
6356 An older, non-standard 64-bit format is also handled by this
6357 function. The older format in question stores the initial length
6358 as an 8-byte quantity without an escape value. Lengths greater
6359 than 2^32 aren't very common which means that the initial 4 bytes
6360 is almost always zero. Since a length value of zero doesn't make
6361 sense for the 32-bit format, this initial zero can be considered to
6362 be an escape value which indicates the presence of the older 64-bit
6363 format. As written, the code can't detect (old format) lengths
917c78fc
MK
6364 greater than 4GB. If it becomes necessary to handle lengths
6365 somewhat larger than 4GB, we could allow other small values (such
6366 as the non-sensical values of 1, 2, and 3) to also be used as
6367 escape values indicating the presence of the old format.
f7ef9339 6368
917c78fc
MK
6369 The value returned via bytes_read should be used to increment the
6370 relevant pointer after calling read_initial_length().
613e1657
KB
6371
6372 As a side effect, this function sets the fields initial_length_size
6373 and offset_size in cu_header to the values appropriate for the
6374 length field. (The format of the initial length field determines
dd373385 6375 the width of file offsets to be fetched later with read_offset().)
613e1657
KB
6376
6377 [ Note: read_initial_length() and read_offset() are based on the
6378 document entitled "DWARF Debugging Information Format", revision
f7ef9339 6379 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
6380 from:
6381
f7ef9339 6382 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
613e1657
KB
6383
6384 This document is only a draft and is subject to change. (So beware.)
6385
f7ef9339 6386 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
6387 determined empirically by examining 64-bit ELF files produced by
6388 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
6389
6390 - Kevin, July 16, 2002
613e1657
KB
6391 ] */
6392
6393static LONGEST
fe1b8b76 6394read_initial_length (bfd *abfd, gdb_byte *buf, struct comp_unit_head *cu_header,
891d2f0b 6395 unsigned int *bytes_read)
613e1657 6396{
fe1b8b76 6397 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 6398
dd373385 6399 if (length == 0xffffffff)
613e1657 6400 {
fe1b8b76 6401 length = bfd_get_64 (abfd, buf + 4);
613e1657 6402 *bytes_read = 12;
613e1657 6403 }
dd373385 6404 else if (length == 0)
f7ef9339 6405 {
dd373385 6406 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 6407 length = bfd_get_64 (abfd, buf);
f7ef9339 6408 *bytes_read = 8;
f7ef9339 6409 }
613e1657
KB
6410 else
6411 {
6412 *bytes_read = 4;
613e1657
KB
6413 }
6414
dd373385
EZ
6415 if (cu_header)
6416 {
6417 gdb_assert (cu_header->initial_length_size == 0
6418 || cu_header->initial_length_size == 4
6419 || cu_header->initial_length_size == 8
6420 || cu_header->initial_length_size == 12);
6421
6422 if (cu_header->initial_length_size != 0
6423 && cu_header->initial_length_size != *bytes_read)
6424 complaint (&symfile_complaints,
6425 _("intermixed 32-bit and 64-bit DWARF sections"));
6426
6427 cu_header->initial_length_size = *bytes_read;
6428 cu_header->offset_size = (*bytes_read == 4) ? 4 : 8;
6429 }
6430
6431 return length;
613e1657
KB
6432}
6433
6434/* Read an offset from the data stream. The size of the offset is
917c78fc 6435 given by cu_header->offset_size. */
613e1657
KB
6436
6437static LONGEST
fe1b8b76 6438read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 6439 unsigned int *bytes_read)
613e1657
KB
6440{
6441 LONGEST retval = 0;
6442
6443 switch (cu_header->offset_size)
6444 {
6445 case 4:
fe1b8b76 6446 retval = bfd_get_32 (abfd, buf);
613e1657
KB
6447 *bytes_read = 4;
6448 break;
6449 case 8:
fe1b8b76 6450 retval = bfd_get_64 (abfd, buf);
613e1657
KB
6451 *bytes_read = 8;
6452 break;
6453 default:
8e65ff28 6454 internal_error (__FILE__, __LINE__,
e2e0b3e5 6455 _("read_offset: bad switch [in module %s]"),
659b0389 6456 bfd_get_filename (abfd));
613e1657
KB
6457 }
6458
917c78fc 6459 return retval;
613e1657
KB
6460}
6461
fe1b8b76
JB
6462static gdb_byte *
6463read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
6464{
6465 /* If the size of a host char is 8 bits, we can return a pointer
6466 to the buffer, otherwise we have to copy the data to a buffer
6467 allocated on the temporary obstack. */
4bdf3d34 6468 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 6469 return buf;
c906108c
SS
6470}
6471
6472static char *
fe1b8b76 6473read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
6474{
6475 /* If the size of a host char is 8 bits, we can return a pointer
6476 to the string, otherwise we have to copy the string to a buffer
6477 allocated on the temporary obstack. */
4bdf3d34 6478 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
6479 if (*buf == '\0')
6480 {
6481 *bytes_read_ptr = 1;
6482 return NULL;
6483 }
fe1b8b76
JB
6484 *bytes_read_ptr = strlen ((char *) buf) + 1;
6485 return (char *) buf;
4bdf3d34
JJ
6486}
6487
6488static char *
fe1b8b76 6489read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
6490 const struct comp_unit_head *cu_header,
6491 unsigned int *bytes_read_ptr)
6492{
6493 LONGEST str_offset = read_offset (abfd, buf, cu_header,
891d2f0b 6494 bytes_read_ptr);
c906108c 6495
6502dd73 6496 if (dwarf2_per_objfile->str_buffer == NULL)
c906108c 6497 {
8a3fe4f8 6498 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 6499 bfd_get_filename (abfd));
4bdf3d34 6500 return NULL;
c906108c 6501 }
6502dd73 6502 if (str_offset >= dwarf2_per_objfile->str_size)
c906108c 6503 {
8a3fe4f8 6504 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 6505 bfd_get_filename (abfd));
c906108c
SS
6506 return NULL;
6507 }
4bdf3d34 6508 gdb_assert (HOST_CHAR_BIT == 8);
6502dd73 6509 if (dwarf2_per_objfile->str_buffer[str_offset] == '\0')
4bdf3d34 6510 return NULL;
fe1b8b76 6511 return (char *) (dwarf2_per_objfile->str_buffer + str_offset);
c906108c
SS
6512}
6513
ce5d95e1 6514static unsigned long
fe1b8b76 6515read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 6516{
ce5d95e1
JB
6517 unsigned long result;
6518 unsigned int num_read;
c906108c
SS
6519 int i, shift;
6520 unsigned char byte;
6521
6522 result = 0;
6523 shift = 0;
6524 num_read = 0;
6525 i = 0;
6526 while (1)
6527 {
fe1b8b76 6528 byte = bfd_get_8 (abfd, buf);
c906108c
SS
6529 buf++;
6530 num_read++;
ce5d95e1 6531 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
6532 if ((byte & 128) == 0)
6533 {
6534 break;
6535 }
6536 shift += 7;
6537 }
6538 *bytes_read_ptr = num_read;
6539 return result;
6540}
6541
ce5d95e1 6542static long
fe1b8b76 6543read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 6544{
ce5d95e1 6545 long result;
77e0b926 6546 int i, shift, num_read;
c906108c
SS
6547 unsigned char byte;
6548
6549 result = 0;
6550 shift = 0;
c906108c
SS
6551 num_read = 0;
6552 i = 0;
6553 while (1)
6554 {
fe1b8b76 6555 byte = bfd_get_8 (abfd, buf);
c906108c
SS
6556 buf++;
6557 num_read++;
ce5d95e1 6558 result |= ((long)(byte & 127) << shift);
c906108c
SS
6559 shift += 7;
6560 if ((byte & 128) == 0)
6561 {
6562 break;
6563 }
6564 }
77e0b926
DJ
6565 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
6566 result |= -(((long)1) << shift);
c906108c
SS
6567 *bytes_read_ptr = num_read;
6568 return result;
6569}
6570
4bb7a0a7
DJ
6571/* Return a pointer to just past the end of an LEB128 number in BUF. */
6572
fe1b8b76
JB
6573static gdb_byte *
6574skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
6575{
6576 int byte;
6577
6578 while (1)
6579 {
fe1b8b76 6580 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
6581 buf++;
6582 if ((byte & 128) == 0)
6583 return buf;
6584 }
6585}
6586
c906108c 6587static void
e142c38c 6588set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
6589{
6590 switch (lang)
6591 {
6592 case DW_LANG_C89:
6593 case DW_LANG_C:
e142c38c 6594 cu->language = language_c;
c906108c
SS
6595 break;
6596 case DW_LANG_C_plus_plus:
e142c38c 6597 cu->language = language_cplus;
c906108c
SS
6598 break;
6599 case DW_LANG_Fortran77:
6600 case DW_LANG_Fortran90:
b21b22e0 6601 case DW_LANG_Fortran95:
e142c38c 6602 cu->language = language_fortran;
c906108c
SS
6603 break;
6604 case DW_LANG_Mips_Assembler:
e142c38c 6605 cu->language = language_asm;
c906108c 6606 break;
bebd888e 6607 case DW_LANG_Java:
e142c38c 6608 cu->language = language_java;
bebd888e 6609 break;
c906108c 6610 case DW_LANG_Ada83:
8aaf0b47 6611 case DW_LANG_Ada95:
bc5f45f8
JB
6612 cu->language = language_ada;
6613 break;
72019c9c
GM
6614 case DW_LANG_Modula2:
6615 cu->language = language_m2;
6616 break;
fe8e67fd
PM
6617 case DW_LANG_Pascal83:
6618 cu->language = language_pascal;
6619 break;
22566fbd
DJ
6620 case DW_LANG_ObjC:
6621 cu->language = language_objc;
6622 break;
c906108c
SS
6623 case DW_LANG_Cobol74:
6624 case DW_LANG_Cobol85:
c906108c 6625 default:
e142c38c 6626 cu->language = language_minimal;
c906108c
SS
6627 break;
6628 }
e142c38c 6629 cu->language_defn = language_def (cu->language);
c906108c
SS
6630}
6631
6632/* Return the named attribute or NULL if not there. */
6633
6634static struct attribute *
e142c38c 6635dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
6636{
6637 unsigned int i;
6638 struct attribute *spec = NULL;
6639
6640 for (i = 0; i < die->num_attrs; ++i)
6641 {
6642 if (die->attrs[i].name == name)
10b3939b 6643 return &die->attrs[i];
c906108c
SS
6644 if (die->attrs[i].name == DW_AT_specification
6645 || die->attrs[i].name == DW_AT_abstract_origin)
6646 spec = &die->attrs[i];
6647 }
c906108c 6648
10b3939b 6649 if (spec)
f2f0e013
DJ
6650 {
6651 die = follow_die_ref (die, spec, &cu);
6652 return dwarf2_attr (die, name, cu);
6653 }
c5aa993b 6654
c906108c
SS
6655 return NULL;
6656}
6657
05cf31d1
JB
6658/* Return non-zero iff the attribute NAME is defined for the given DIE,
6659 and holds a non-zero value. This function should only be used for
6660 DW_FORM_flag attributes. */
6661
6662static int
6663dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
6664{
6665 struct attribute *attr = dwarf2_attr (die, name, cu);
6666
6667 return (attr && DW_UNSND (attr));
6668}
6669
3ca72b44 6670static int
e142c38c 6671die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 6672{
05cf31d1
JB
6673 /* A DIE is a declaration if it has a DW_AT_declaration attribute
6674 which value is non-zero. However, we have to be careful with
6675 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
6676 (via dwarf2_flag_true_p) follows this attribute. So we may
6677 end up accidently finding a declaration attribute that belongs
6678 to a different DIE referenced by the specification attribute,
6679 even though the given DIE does not have a declaration attribute. */
6680 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
6681 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
6682}
6683
63d06c5c 6684/* Return the die giving the specification for DIE, if there is
f2f0e013
DJ
6685 one. *SPEC_CU is the CU containing DIE on input, and the CU
6686 containing the return value on output. */
63d06c5c
DC
6687
6688static struct die_info *
f2f0e013 6689die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 6690{
f2f0e013
DJ
6691 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
6692 *spec_cu);
63d06c5c
DC
6693
6694 if (spec_attr == NULL)
6695 return NULL;
6696 else
f2f0e013 6697 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 6698}
c906108c 6699
debd256d
JB
6700/* Free the line_header structure *LH, and any arrays and strings it
6701 refers to. */
6702static void
6703free_line_header (struct line_header *lh)
6704{
6705 if (lh->standard_opcode_lengths)
a8bc7b56 6706 xfree (lh->standard_opcode_lengths);
debd256d
JB
6707
6708 /* Remember that all the lh->file_names[i].name pointers are
6709 pointers into debug_line_buffer, and don't need to be freed. */
6710 if (lh->file_names)
a8bc7b56 6711 xfree (lh->file_names);
debd256d
JB
6712
6713 /* Similarly for the include directory names. */
6714 if (lh->include_dirs)
a8bc7b56 6715 xfree (lh->include_dirs);
debd256d 6716
a8bc7b56 6717 xfree (lh);
debd256d
JB
6718}
6719
6720
6721/* Add an entry to LH's include directory table. */
6722static void
6723add_include_dir (struct line_header *lh, char *include_dir)
c906108c 6724{
debd256d
JB
6725 /* Grow the array if necessary. */
6726 if (lh->include_dirs_size == 0)
c5aa993b 6727 {
debd256d
JB
6728 lh->include_dirs_size = 1; /* for testing */
6729 lh->include_dirs = xmalloc (lh->include_dirs_size
6730 * sizeof (*lh->include_dirs));
6731 }
6732 else if (lh->num_include_dirs >= lh->include_dirs_size)
6733 {
6734 lh->include_dirs_size *= 2;
6735 lh->include_dirs = xrealloc (lh->include_dirs,
6736 (lh->include_dirs_size
6737 * sizeof (*lh->include_dirs)));
c5aa993b 6738 }
c906108c 6739
debd256d
JB
6740 lh->include_dirs[lh->num_include_dirs++] = include_dir;
6741}
6742
6743
6744/* Add an entry to LH's file name table. */
6745static void
6746add_file_name (struct line_header *lh,
6747 char *name,
6748 unsigned int dir_index,
6749 unsigned int mod_time,
6750 unsigned int length)
6751{
6752 struct file_entry *fe;
6753
6754 /* Grow the array if necessary. */
6755 if (lh->file_names_size == 0)
6756 {
6757 lh->file_names_size = 1; /* for testing */
6758 lh->file_names = xmalloc (lh->file_names_size
6759 * sizeof (*lh->file_names));
6760 }
6761 else if (lh->num_file_names >= lh->file_names_size)
6762 {
6763 lh->file_names_size *= 2;
6764 lh->file_names = xrealloc (lh->file_names,
6765 (lh->file_names_size
6766 * sizeof (*lh->file_names)));
6767 }
6768
6769 fe = &lh->file_names[lh->num_file_names++];
6770 fe->name = name;
6771 fe->dir_index = dir_index;
6772 fe->mod_time = mod_time;
6773 fe->length = length;
aaa75496 6774 fe->included_p = 0;
cb1df416 6775 fe->symtab = NULL;
debd256d
JB
6776}
6777
6778
6779/* Read the statement program header starting at OFFSET in
6502dd73
DJ
6780 .debug_line, according to the endianness of ABFD. Return a pointer
6781 to a struct line_header, allocated using xmalloc.
debd256d
JB
6782
6783 NOTE: the strings in the include directory and file name tables of
6784 the returned object point into debug_line_buffer, and must not be
6785 freed. */
6786static struct line_header *
6787dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 6788 struct dwarf2_cu *cu)
debd256d
JB
6789{
6790 struct cleanup *back_to;
6791 struct line_header *lh;
fe1b8b76 6792 gdb_byte *line_ptr;
891d2f0b 6793 unsigned int bytes_read;
debd256d
JB
6794 int i;
6795 char *cur_dir, *cur_file;
6796
6502dd73 6797 if (dwarf2_per_objfile->line_buffer == NULL)
debd256d 6798 {
e2e0b3e5 6799 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
6800 return 0;
6801 }
6802
a738430d
MK
6803 /* Make sure that at least there's room for the total_length field.
6804 That could be 12 bytes long, but we're just going to fudge that. */
6502dd73 6805 if (offset + 4 >= dwarf2_per_objfile->line_size)
debd256d 6806 {
4d3c2250 6807 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
6808 return 0;
6809 }
6810
6811 lh = xmalloc (sizeof (*lh));
6812 memset (lh, 0, sizeof (*lh));
6813 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
6814 (void *) lh);
6815
6502dd73 6816 line_ptr = dwarf2_per_objfile->line_buffer + offset;
debd256d 6817
a738430d 6818 /* Read in the header. */
dd373385
EZ
6819 lh->total_length =
6820 read_initial_length (abfd, line_ptr, &cu->header, &bytes_read);
debd256d 6821 line_ptr += bytes_read;
6502dd73
DJ
6822 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line_buffer
6823 + dwarf2_per_objfile->line_size))
debd256d 6824 {
4d3c2250 6825 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
6826 return 0;
6827 }
6828 lh->statement_program_end = line_ptr + lh->total_length;
6829 lh->version = read_2_bytes (abfd, line_ptr);
6830 line_ptr += 2;
e7c27a73 6831 lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
debd256d
JB
6832 line_ptr += bytes_read;
6833 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
6834 line_ptr += 1;
6835 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
6836 line_ptr += 1;
6837 lh->line_base = read_1_signed_byte (abfd, line_ptr);
6838 line_ptr += 1;
6839 lh->line_range = read_1_byte (abfd, line_ptr);
6840 line_ptr += 1;
6841 lh->opcode_base = read_1_byte (abfd, line_ptr);
6842 line_ptr += 1;
6843 lh->standard_opcode_lengths
fe1b8b76 6844 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
6845
6846 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
6847 for (i = 1; i < lh->opcode_base; ++i)
6848 {
6849 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
6850 line_ptr += 1;
6851 }
6852
a738430d 6853 /* Read directory table. */
debd256d
JB
6854 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6855 {
6856 line_ptr += bytes_read;
6857 add_include_dir (lh, cur_dir);
6858 }
6859 line_ptr += bytes_read;
6860
a738430d 6861 /* Read file name table. */
debd256d
JB
6862 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6863 {
6864 unsigned int dir_index, mod_time, length;
6865
6866 line_ptr += bytes_read;
6867 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6868 line_ptr += bytes_read;
6869 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6870 line_ptr += bytes_read;
6871 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6872 line_ptr += bytes_read;
6873
6874 add_file_name (lh, cur_file, dir_index, mod_time, length);
6875 }
6876 line_ptr += bytes_read;
6877 lh->statement_program_start = line_ptr;
6878
6502dd73
DJ
6879 if (line_ptr > (dwarf2_per_objfile->line_buffer
6880 + dwarf2_per_objfile->line_size))
4d3c2250 6881 complaint (&symfile_complaints,
e2e0b3e5 6882 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
6883
6884 discard_cleanups (back_to);
6885 return lh;
6886}
c906108c 6887
5fb290d7
DJ
6888/* This function exists to work around a bug in certain compilers
6889 (particularly GCC 2.95), in which the first line number marker of a
6890 function does not show up until after the prologue, right before
6891 the second line number marker. This function shifts ADDRESS down
6892 to the beginning of the function if necessary, and is called on
6893 addresses passed to record_line. */
6894
6895static CORE_ADDR
e142c38c 6896check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
6897{
6898 struct function_range *fn;
6899
6900 /* Find the function_range containing address. */
e142c38c 6901 if (!cu->first_fn)
5fb290d7
DJ
6902 return address;
6903
e142c38c
DJ
6904 if (!cu->cached_fn)
6905 cu->cached_fn = cu->first_fn;
5fb290d7 6906
e142c38c 6907 fn = cu->cached_fn;
5fb290d7
DJ
6908 while (fn)
6909 if (fn->lowpc <= address && fn->highpc > address)
6910 goto found;
6911 else
6912 fn = fn->next;
6913
e142c38c
DJ
6914 fn = cu->first_fn;
6915 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
6916 if (fn->lowpc <= address && fn->highpc > address)
6917 goto found;
6918 else
6919 fn = fn->next;
6920
6921 return address;
6922
6923 found:
6924 if (fn->seen_line)
6925 return address;
6926 if (address != fn->lowpc)
4d3c2250 6927 complaint (&symfile_complaints,
e2e0b3e5 6928 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 6929 (unsigned long) address, fn->name);
5fb290d7
DJ
6930 fn->seen_line = 1;
6931 return fn->lowpc;
6932}
6933
aaa75496
JB
6934/* Decode the Line Number Program (LNP) for the given line_header
6935 structure and CU. The actual information extracted and the type
6936 of structures created from the LNP depends on the value of PST.
6937
6938 1. If PST is NULL, then this procedure uses the data from the program
6939 to create all necessary symbol tables, and their linetables.
6940 The compilation directory of the file is passed in COMP_DIR,
6941 and must not be NULL.
6942
6943 2. If PST is not NULL, this procedure reads the program to determine
6944 the list of files included by the unit represented by PST, and
6945 builds all the associated partial symbol tables. In this case,
6946 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
6947 is not used to compute the full name of the symtab, and therefore
6948 omitting it when building the partial symtab does not introduce
6949 the potential for inconsistency - a partial symtab and its associated
6950 symbtab having a different fullname -). */
debd256d 6951
c906108c 6952static void
debd256d 6953dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 6954 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 6955{
a8c50c1f 6956 gdb_byte *line_ptr, *extended_end;
fe1b8b76 6957 gdb_byte *line_end;
a8c50c1f 6958 unsigned int bytes_read, extended_len;
c906108c 6959 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
6960 CORE_ADDR baseaddr;
6961 struct objfile *objfile = cu->objfile;
aaa75496 6962 const int decode_for_pst_p = (pst != NULL);
cb1df416 6963 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
6964
6965 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6966
debd256d
JB
6967 line_ptr = lh->statement_program_start;
6968 line_end = lh->statement_program_end;
c906108c
SS
6969
6970 /* Read the statement sequences until there's nothing left. */
6971 while (line_ptr < line_end)
6972 {
6973 /* state machine registers */
6974 CORE_ADDR address = 0;
6975 unsigned int file = 1;
6976 unsigned int line = 1;
6977 unsigned int column = 0;
debd256d 6978 int is_stmt = lh->default_is_stmt;
c906108c
SS
6979 int basic_block = 0;
6980 int end_sequence = 0;
6981
aaa75496 6982 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 6983 {
aaa75496 6984 /* Start a subfile for the current file of the state machine. */
debd256d
JB
6985 /* lh->include_dirs and lh->file_names are 0-based, but the
6986 directory and file name numbers in the statement program
6987 are 1-based. */
6988 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 6989 char *dir = NULL;
a738430d 6990
debd256d
JB
6991 if (fe->dir_index)
6992 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
6993
6994 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
6995 }
6996
a738430d 6997 /* Decode the table. */
c5aa993b 6998 while (!end_sequence)
c906108c
SS
6999 {
7000 op_code = read_1_byte (abfd, line_ptr);
7001 line_ptr += 1;
9aa1fe7e 7002
debd256d 7003 if (op_code >= lh->opcode_base)
a738430d
MK
7004 {
7005 /* Special operand. */
debd256d
JB
7006 adj_opcode = op_code - lh->opcode_base;
7007 address += (adj_opcode / lh->line_range)
7008 * lh->minimum_instruction_length;
7009 line += lh->line_base + (adj_opcode % lh->line_range);
25e43795
DJ
7010 if (lh->num_file_names < file)
7011 dwarf2_debug_line_missing_file_complaint ();
7012 else
7013 {
7014 lh->file_names[file - 1].included_p = 1;
7015 if (!decode_for_pst_p)
7016 {
7017 if (last_subfile != current_subfile)
7018 {
7019 if (last_subfile)
7020 record_line (last_subfile, 0, address);
7021 last_subfile = current_subfile;
7022 }
7023 /* Append row to matrix using current values. */
7024 record_line (current_subfile, line,
7025 check_cu_functions (address, cu));
366da635 7026 }
25e43795 7027 }
9aa1fe7e
GK
7028 basic_block = 1;
7029 }
7030 else switch (op_code)
c906108c
SS
7031 {
7032 case DW_LNS_extended_op:
a8c50c1f 7033 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 7034 line_ptr += bytes_read;
a8c50c1f 7035 extended_end = line_ptr + extended_len;
c906108c
SS
7036 extended_op = read_1_byte (abfd, line_ptr);
7037 line_ptr += 1;
7038 switch (extended_op)
7039 {
7040 case DW_LNE_end_sequence:
7041 end_sequence = 1;
25e43795
DJ
7042
7043 if (lh->num_file_names < file)
7044 dwarf2_debug_line_missing_file_complaint ();
7045 else
7046 {
7047 lh->file_names[file - 1].included_p = 1;
7048 if (!decode_for_pst_p)
7049 record_line (current_subfile, 0, address);
7050 }
c906108c
SS
7051 break;
7052 case DW_LNE_set_address:
e7c27a73 7053 address = read_address (abfd, line_ptr, cu, &bytes_read);
107d2387
AC
7054 line_ptr += bytes_read;
7055 address += baseaddr;
c906108c
SS
7056 break;
7057 case DW_LNE_define_file:
debd256d
JB
7058 {
7059 char *cur_file;
7060 unsigned int dir_index, mod_time, length;
7061
7062 cur_file = read_string (abfd, line_ptr, &bytes_read);
7063 line_ptr += bytes_read;
7064 dir_index =
7065 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7066 line_ptr += bytes_read;
7067 mod_time =
7068 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7069 line_ptr += bytes_read;
7070 length =
7071 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7072 line_ptr += bytes_read;
7073 add_file_name (lh, cur_file, dir_index, mod_time, length);
7074 }
c906108c
SS
7075 break;
7076 default:
4d3c2250 7077 complaint (&symfile_complaints,
e2e0b3e5 7078 _("mangled .debug_line section"));
debd256d 7079 return;
c906108c 7080 }
a8c50c1f
DJ
7081 /* Make sure that we parsed the extended op correctly. If e.g.
7082 we expected a different address size than the producer used,
7083 we may have read the wrong number of bytes. */
7084 if (line_ptr != extended_end)
7085 {
7086 complaint (&symfile_complaints,
7087 _("mangled .debug_line section"));
7088 return;
7089 }
c906108c
SS
7090 break;
7091 case DW_LNS_copy:
25e43795
DJ
7092 if (lh->num_file_names < file)
7093 dwarf2_debug_line_missing_file_complaint ();
7094 else
366da635 7095 {
25e43795
DJ
7096 lh->file_names[file - 1].included_p = 1;
7097 if (!decode_for_pst_p)
7098 {
7099 if (last_subfile != current_subfile)
7100 {
7101 if (last_subfile)
7102 record_line (last_subfile, 0, address);
7103 last_subfile = current_subfile;
7104 }
7105 record_line (current_subfile, line,
7106 check_cu_functions (address, cu));
7107 }
366da635 7108 }
c906108c
SS
7109 basic_block = 0;
7110 break;
7111 case DW_LNS_advance_pc:
debd256d 7112 address += lh->minimum_instruction_length
c906108c
SS
7113 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7114 line_ptr += bytes_read;
7115 break;
7116 case DW_LNS_advance_line:
7117 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
7118 line_ptr += bytes_read;
7119 break;
7120 case DW_LNS_set_file:
debd256d 7121 {
a738430d
MK
7122 /* The arrays lh->include_dirs and lh->file_names are
7123 0-based, but the directory and file name numbers in
7124 the statement program are 1-based. */
debd256d 7125 struct file_entry *fe;
4f1520fb 7126 char *dir = NULL;
a738430d 7127
debd256d
JB
7128 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7129 line_ptr += bytes_read;
25e43795
DJ
7130 if (lh->num_file_names < file)
7131 dwarf2_debug_line_missing_file_complaint ();
7132 else
7133 {
7134 fe = &lh->file_names[file - 1];
7135 if (fe->dir_index)
7136 dir = lh->include_dirs[fe->dir_index - 1];
7137 if (!decode_for_pst_p)
7138 {
7139 last_subfile = current_subfile;
7140 dwarf2_start_subfile (fe->name, dir, comp_dir);
7141 }
7142 }
debd256d 7143 }
c906108c
SS
7144 break;
7145 case DW_LNS_set_column:
7146 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7147 line_ptr += bytes_read;
7148 break;
7149 case DW_LNS_negate_stmt:
7150 is_stmt = (!is_stmt);
7151 break;
7152 case DW_LNS_set_basic_block:
7153 basic_block = 1;
7154 break;
c2c6d25f
JM
7155 /* Add to the address register of the state machine the
7156 address increment value corresponding to special opcode
a738430d
MK
7157 255. I.e., this value is scaled by the minimum
7158 instruction length since special opcode 255 would have
7159 scaled the the increment. */
c906108c 7160 case DW_LNS_const_add_pc:
debd256d
JB
7161 address += (lh->minimum_instruction_length
7162 * ((255 - lh->opcode_base) / lh->line_range));
c906108c
SS
7163 break;
7164 case DW_LNS_fixed_advance_pc:
7165 address += read_2_bytes (abfd, line_ptr);
7166 line_ptr += 2;
7167 break;
9aa1fe7e 7168 default:
a738430d
MK
7169 {
7170 /* Unknown standard opcode, ignore it. */
9aa1fe7e 7171 int i;
a738430d 7172
debd256d 7173 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
7174 {
7175 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7176 line_ptr += bytes_read;
7177 }
7178 }
c906108c
SS
7179 }
7180 }
7181 }
aaa75496
JB
7182
7183 if (decode_for_pst_p)
7184 {
7185 int file_index;
7186
7187 /* Now that we're done scanning the Line Header Program, we can
7188 create the psymtab of each included file. */
7189 for (file_index = 0; file_index < lh->num_file_names; file_index++)
7190 if (lh->file_names[file_index].included_p == 1)
7191 {
5b5464ad
JB
7192 const struct file_entry fe = lh->file_names [file_index];
7193 char *include_name = fe.name;
7194 char *dir_name = NULL;
7195 char *pst_filename = pst->filename;
7196
7197 if (fe.dir_index)
7198 dir_name = lh->include_dirs[fe.dir_index - 1];
7199
7200 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
7201 {
1754f103
MK
7202 include_name = concat (dir_name, SLASH_STRING,
7203 include_name, (char *)NULL);
5b5464ad
JB
7204 make_cleanup (xfree, include_name);
7205 }
7206
7207 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
7208 {
1754f103
MK
7209 pst_filename = concat (pst->dirname, SLASH_STRING,
7210 pst_filename, (char *)NULL);
5b5464ad
JB
7211 make_cleanup (xfree, pst_filename);
7212 }
7213
7214 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
7215 dwarf2_create_include_psymtab (include_name, pst, objfile);
7216 }
7217 }
cb1df416
DJ
7218 else
7219 {
7220 /* Make sure a symtab is created for every file, even files
7221 which contain only variables (i.e. no code with associated
7222 line numbers). */
7223
7224 int i;
7225 struct file_entry *fe;
7226
7227 for (i = 0; i < lh->num_file_names; i++)
7228 {
7229 char *dir = NULL;
7230 fe = &lh->file_names[i];
7231 if (fe->dir_index)
7232 dir = lh->include_dirs[fe->dir_index - 1];
7233 dwarf2_start_subfile (fe->name, dir, comp_dir);
7234
7235 /* Skip the main file; we don't need it, and it must be
7236 allocated last, so that it will show up before the
7237 non-primary symtabs in the objfile's symtab list. */
7238 if (current_subfile == first_subfile)
7239 continue;
7240
7241 if (current_subfile->symtab == NULL)
7242 current_subfile->symtab = allocate_symtab (current_subfile->name,
7243 cu->objfile);
7244 fe->symtab = current_subfile->symtab;
7245 }
7246 }
c906108c
SS
7247}
7248
7249/* Start a subfile for DWARF. FILENAME is the name of the file and
7250 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
7251 or NULL if not known. COMP_DIR is the compilation directory for the
7252 linetable's compilation unit or NULL if not known.
c906108c
SS
7253 This routine tries to keep line numbers from identical absolute and
7254 relative file names in a common subfile.
7255
7256 Using the `list' example from the GDB testsuite, which resides in
7257 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
7258 of /srcdir/list0.c yields the following debugging information for list0.c:
7259
c5aa993b
JM
7260 DW_AT_name: /srcdir/list0.c
7261 DW_AT_comp_dir: /compdir
357e46e7 7262 files.files[0].name: list0.h
c5aa993b 7263 files.files[0].dir: /srcdir
357e46e7 7264 files.files[1].name: list0.c
c5aa993b 7265 files.files[1].dir: /srcdir
c906108c
SS
7266
7267 The line number information for list0.c has to end up in a single
4f1520fb
FR
7268 subfile, so that `break /srcdir/list0.c:1' works as expected.
7269 start_subfile will ensure that this happens provided that we pass the
7270 concatenation of files.files[1].dir and files.files[1].name as the
7271 subfile's name. */
c906108c
SS
7272
7273static void
4f1520fb 7274dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 7275{
4f1520fb
FR
7276 char *fullname;
7277
7278 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
7279 `start_symtab' will always pass the contents of DW_AT_comp_dir as
7280 second argument to start_subfile. To be consistent, we do the
7281 same here. In order not to lose the line information directory,
7282 we concatenate it to the filename when it makes sense.
7283 Note that the Dwarf3 standard says (speaking of filenames in line
7284 information): ``The directory index is ignored for file names
7285 that represent full path names''. Thus ignoring dirname in the
7286 `else' branch below isn't an issue. */
c906108c 7287
d5166ae1 7288 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
7289 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
7290 else
7291 fullname = filename;
c906108c 7292
4f1520fb
FR
7293 start_subfile (fullname, comp_dir);
7294
7295 if (fullname != filename)
7296 xfree (fullname);
c906108c
SS
7297}
7298
4c2df51b
DJ
7299static void
7300var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 7301 struct dwarf2_cu *cu)
4c2df51b 7302{
e7c27a73
DJ
7303 struct objfile *objfile = cu->objfile;
7304 struct comp_unit_head *cu_header = &cu->header;
7305
4c2df51b
DJ
7306 /* NOTE drow/2003-01-30: There used to be a comment and some special
7307 code here to turn a symbol with DW_AT_external and a
7308 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
7309 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
7310 with some versions of binutils) where shared libraries could have
7311 relocations against symbols in their debug information - the
7312 minimal symbol would have the right address, but the debug info
7313 would not. It's no longer necessary, because we will explicitly
7314 apply relocations when we read in the debug information now. */
7315
7316 /* A DW_AT_location attribute with no contents indicates that a
7317 variable has been optimized away. */
7318 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
7319 {
7320 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
7321 return;
7322 }
7323
7324 /* Handle one degenerate form of location expression specially, to
7325 preserve GDB's previous behavior when section offsets are
7326 specified. If this is just a DW_OP_addr then mark this symbol
7327 as LOC_STATIC. */
7328
7329 if (attr_form_is_block (attr)
7330 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
7331 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
7332 {
891d2f0b 7333 unsigned int dummy;
4c2df51b
DJ
7334
7335 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 7336 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 7337 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
7338 fixup_symbol_section (sym, objfile);
7339 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
7340 SYMBOL_SECTION (sym));
4c2df51b
DJ
7341 return;
7342 }
7343
7344 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
7345 expression evaluator, and use LOC_COMPUTED only when necessary
7346 (i.e. when the value of a register or memory location is
7347 referenced, or a thread-local block, etc.). Then again, it might
7348 not be worthwhile. I'm assuming that it isn't unless performance
7349 or memory numbers show me otherwise. */
7350
e7c27a73 7351 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
7352 SYMBOL_CLASS (sym) = LOC_COMPUTED;
7353}
7354
c906108c
SS
7355/* Given a pointer to a DWARF information entry, figure out if we need
7356 to make a symbol table entry for it, and if so, create a new entry
7357 and return a pointer to it.
7358 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 7359 used the passed type. */
c906108c
SS
7360
7361static struct symbol *
e7c27a73 7362new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 7363{
e7c27a73 7364 struct objfile *objfile = cu->objfile;
5e2b427d 7365 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7366 struct symbol *sym = NULL;
7367 char *name;
7368 struct attribute *attr = NULL;
7369 struct attribute *attr2 = NULL;
e142c38c
DJ
7370 CORE_ADDR baseaddr;
7371
7372 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7373
5c4e30ca 7374 if (die->tag != DW_TAG_namespace)
e142c38c 7375 name = dwarf2_linkage_name (die, cu);
5c4e30ca
DC
7376 else
7377 name = TYPE_NAME (type);
7378
c906108c
SS
7379 if (name)
7380 {
4a146b47 7381 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
7382 sizeof (struct symbol));
7383 OBJSTAT (objfile, n_syms++);
7384 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
7385
7386 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 7387 SYMBOL_LANGUAGE (sym) = cu->language;
2de7ced7 7388 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
c906108c
SS
7389
7390 /* Default assumptions.
c5aa993b 7391 Use the passed type or decode it from the die. */
176620f1 7392 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 7393 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
7394 if (type != NULL)
7395 SYMBOL_TYPE (sym) = type;
7396 else
e7c27a73 7397 SYMBOL_TYPE (sym) = die_type (die, cu);
e142c38c 7398 attr = dwarf2_attr (die, DW_AT_decl_line, cu);
c906108c
SS
7399 if (attr)
7400 {
7401 SYMBOL_LINE (sym) = DW_UNSND (attr);
7402 }
cb1df416
DJ
7403
7404 attr = dwarf2_attr (die, DW_AT_decl_file, cu);
7405 if (attr)
7406 {
7407 int file_index = DW_UNSND (attr);
7408 if (cu->line_header == NULL
7409 || file_index > cu->line_header->num_file_names)
7410 complaint (&symfile_complaints,
7411 _("file index out of range"));
1c3d648d 7412 else if (file_index > 0)
cb1df416
DJ
7413 {
7414 struct file_entry *fe;
7415 fe = &cu->line_header->file_names[file_index - 1];
7416 SYMBOL_SYMTAB (sym) = fe->symtab;
7417 }
7418 }
7419
c906108c
SS
7420 switch (die->tag)
7421 {
7422 case DW_TAG_label:
e142c38c 7423 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
7424 if (attr)
7425 {
7426 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
7427 }
7428 SYMBOL_CLASS (sym) = LOC_LABEL;
7429 break;
7430 case DW_TAG_subprogram:
7431 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
7432 finish_block. */
7433 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 7434 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
7435 if ((attr2 && (DW_UNSND (attr2) != 0))
7436 || cu->language == language_ada)
c906108c 7437 {
2cfa0c8d
JB
7438 /* Subprograms marked external are stored as a global symbol.
7439 Ada subprograms, whether marked external or not, are always
7440 stored as a global symbol, because we want to be able to
7441 access them globally. For instance, we want to be able
7442 to break on a nested subprogram without having to
7443 specify the context. */
c906108c
SS
7444 add_symbol_to_list (sym, &global_symbols);
7445 }
7446 else
7447 {
e142c38c 7448 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7449 }
7450 break;
7451 case DW_TAG_variable:
7452 /* Compilation with minimal debug info may result in variables
7453 with missing type entries. Change the misleading `void' type
7454 to something sensible. */
7455 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 7456 SYMBOL_TYPE (sym)
5e2b427d 7457 = builtin_type (gdbarch)->nodebug_data_symbol;
64c50499 7458
e142c38c 7459 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7460 if (attr)
7461 {
e7c27a73 7462 dwarf2_const_value (attr, sym, cu);
e142c38c 7463 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
7464 if (attr2 && (DW_UNSND (attr2) != 0))
7465 add_symbol_to_list (sym, &global_symbols);
7466 else
e142c38c 7467 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7468 break;
7469 }
e142c38c 7470 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7471 if (attr)
7472 {
e7c27a73 7473 var_decode_location (attr, sym, cu);
e142c38c 7474 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 7475 if (attr2 && (DW_UNSND (attr2) != 0))
4c2df51b 7476 add_symbol_to_list (sym, &global_symbols);
c906108c 7477 else
e142c38c 7478 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7479 }
7480 else
7481 {
7482 /* We do not know the address of this symbol.
c5aa993b
JM
7483 If it is an external symbol and we have type information
7484 for it, enter the symbol as a LOC_UNRESOLVED symbol.
7485 The address of the variable will then be determined from
7486 the minimal symbol table whenever the variable is
7487 referenced. */
e142c38c 7488 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 7489 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 7490 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c
SS
7491 {
7492 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
7493 add_symbol_to_list (sym, &global_symbols);
7494 }
7495 }
7496 break;
7497 case DW_TAG_formal_parameter:
2a2d4dc3 7498 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 7499 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7500 if (attr)
7501 {
e7c27a73 7502 var_decode_location (attr, sym, cu);
c906108c 7503 }
e142c38c 7504 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7505 if (attr)
7506 {
e7c27a73 7507 dwarf2_const_value (attr, sym, cu);
c906108c 7508 }
e142c38c 7509 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7510 break;
7511 case DW_TAG_unspecified_parameters:
7512 /* From varargs functions; gdb doesn't seem to have any
7513 interest in this information, so just ignore it for now.
7514 (FIXME?) */
7515 break;
7516 case DW_TAG_class_type:
680b30c7 7517 case DW_TAG_interface_type:
c906108c
SS
7518 case DW_TAG_structure_type:
7519 case DW_TAG_union_type:
72019c9c 7520 case DW_TAG_set_type:
c906108c
SS
7521 case DW_TAG_enumeration_type:
7522 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 7523 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 7524
63d06c5c
DC
7525 /* Make sure that the symbol includes appropriate enclosing
7526 classes/namespaces in its name. These are calculated in
134d01f1 7527 read_structure_type, and the correct name is saved in
63d06c5c
DC
7528 the type. */
7529
987504bb
JJ
7530 if (cu->language == language_cplus
7531 || cu->language == language_java)
c906108c 7532 {
63d06c5c
DC
7533 struct type *type = SYMBOL_TYPE (sym);
7534
7535 if (TYPE_TAG_NAME (type) != NULL)
7536 {
7537 /* FIXME: carlton/2003-11-10: Should this use
7538 SYMBOL_SET_NAMES instead? (The same problem also
d8151005
DJ
7539 arises further down in this function.) */
7540 /* The type's name is already allocated along with
7541 this objfile, so we don't need to duplicate it
7542 for the symbol. */
7543 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
63d06c5c 7544 }
c906108c 7545 }
63d06c5c
DC
7546
7547 {
987504bb 7548 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
7549 really ever be static objects: otherwise, if you try
7550 to, say, break of a class's method and you're in a file
7551 which doesn't mention that class, it won't work unless
7552 the check for all static symbols in lookup_symbol_aux
7553 saves you. See the OtherFileClass tests in
7554 gdb.c++/namespace.exp. */
7555
7556 struct pending **list_to_add;
7557
e142c38c 7558 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
7559 && (cu->language == language_cplus
7560 || cu->language == language_java)
e142c38c 7561 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
7562
7563 add_symbol_to_list (sym, list_to_add);
7564
7565 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 7566 defines a typedef for "foo". A Java class declaration also
5eeb2539 7567 defines a typedef for the class. */
987504bb 7568 if (cu->language == language_cplus
8c6860bb
JB
7569 || cu->language == language_java
7570 || cu->language == language_ada)
63d06c5c 7571 {
d8151005
DJ
7572 /* The symbol's name is already allocated along with
7573 this objfile, so we don't need to duplicate it for
7574 the type. */
63d06c5c 7575 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 7576 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
7577 }
7578 }
c906108c
SS
7579 break;
7580 case DW_TAG_typedef:
0114d602 7581 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
63d06c5c
DC
7582 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7583 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 7584 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 7585 break;
c906108c 7586 case DW_TAG_base_type:
a02abb62 7587 case DW_TAG_subrange_type:
c906108c 7588 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 7589 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 7590 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7591 break;
7592 case DW_TAG_enumerator:
0114d602 7593 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
e142c38c 7594 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7595 if (attr)
7596 {
e7c27a73 7597 dwarf2_const_value (attr, sym, cu);
c906108c 7598 }
63d06c5c
DC
7599 {
7600 /* NOTE: carlton/2003-11-10: See comment above in the
7601 DW_TAG_class_type, etc. block. */
7602
7603 struct pending **list_to_add;
7604
e142c38c 7605 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
7606 && (cu->language == language_cplus
7607 || cu->language == language_java)
e142c38c 7608 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
7609
7610 add_symbol_to_list (sym, list_to_add);
7611 }
c906108c 7612 break;
5c4e30ca
DC
7613 case DW_TAG_namespace:
7614 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7615 add_symbol_to_list (sym, &global_symbols);
7616 break;
c906108c
SS
7617 default:
7618 /* Not a tag we recognize. Hopefully we aren't processing
7619 trash data, but since we must specifically ignore things
7620 we don't recognize, there is nothing else we should do at
7621 this point. */
e2e0b3e5 7622 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 7623 dwarf_tag_name (die->tag));
c906108c
SS
7624 break;
7625 }
df8a16a1
DJ
7626
7627 /* For the benefit of old versions of GCC, check for anonymous
7628 namespaces based on the demangled name. */
7629 if (!processing_has_namespace_info
7630 && cu->language == language_cplus
7631 && dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu) != NULL)
7632 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
7633 }
7634 return (sym);
7635}
7636
7637/* Copy constant value from an attribute to a symbol. */
7638
7639static void
107d2387 7640dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 7641 struct dwarf2_cu *cu)
c906108c 7642{
e7c27a73
DJ
7643 struct objfile *objfile = cu->objfile;
7644 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7645 struct dwarf_block *blk;
7646
7647 switch (attr->form)
7648 {
7649 case DW_FORM_addr:
107d2387 7650 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
3567439c 7651 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
7652 cu_header->addr_size,
7653 TYPE_LENGTH (SYMBOL_TYPE
7654 (sym)));
4e38b386 7655 SYMBOL_VALUE_BYTES (sym) =
4a146b47 7656 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
fbd9dcd3
AC
7657 /* NOTE: cagney/2003-05-09: In-lined store_address call with
7658 it's body - store_unsigned_integer. */
7659 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
7660 DW_ADDR (attr));
c906108c
SS
7661 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7662 break;
93b5768b
PA
7663 case DW_FORM_strp:
7664 /* DW_STRING is already allocated on the obstack, point directly
7665 to it. */
7666 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
7667 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7668 break;
c906108c
SS
7669 case DW_FORM_block1:
7670 case DW_FORM_block2:
7671 case DW_FORM_block4:
7672 case DW_FORM_block:
7673 blk = DW_BLOCK (attr);
7674 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 7675 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
7676 blk->size,
7677 TYPE_LENGTH (SYMBOL_TYPE
7678 (sym)));
4e38b386 7679 SYMBOL_VALUE_BYTES (sym) =
4a146b47 7680 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
7681 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
7682 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7683 break;
2df3850c
JM
7684
7685 /* The DW_AT_const_value attributes are supposed to carry the
7686 symbol's value "represented as it would be on the target
7687 architecture." By the time we get here, it's already been
7688 converted to host endianness, so we just need to sign- or
7689 zero-extend it as appropriate. */
7690 case DW_FORM_data1:
7691 dwarf2_const_value_data (attr, sym, 8);
7692 break;
c906108c 7693 case DW_FORM_data2:
2df3850c
JM
7694 dwarf2_const_value_data (attr, sym, 16);
7695 break;
c906108c 7696 case DW_FORM_data4:
2df3850c
JM
7697 dwarf2_const_value_data (attr, sym, 32);
7698 break;
c906108c 7699 case DW_FORM_data8:
2df3850c
JM
7700 dwarf2_const_value_data (attr, sym, 64);
7701 break;
7702
c906108c 7703 case DW_FORM_sdata:
2df3850c
JM
7704 SYMBOL_VALUE (sym) = DW_SND (attr);
7705 SYMBOL_CLASS (sym) = LOC_CONST;
7706 break;
7707
c906108c
SS
7708 case DW_FORM_udata:
7709 SYMBOL_VALUE (sym) = DW_UNSND (attr);
7710 SYMBOL_CLASS (sym) = LOC_CONST;
7711 break;
2df3850c 7712
c906108c 7713 default:
4d3c2250 7714 complaint (&symfile_complaints,
e2e0b3e5 7715 _("unsupported const value attribute form: '%s'"),
4d3c2250 7716 dwarf_form_name (attr->form));
c906108c
SS
7717 SYMBOL_VALUE (sym) = 0;
7718 SYMBOL_CLASS (sym) = LOC_CONST;
7719 break;
7720 }
7721}
7722
2df3850c
JM
7723
7724/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
7725 or zero-extend it as appropriate for the symbol's type. */
7726static void
7727dwarf2_const_value_data (struct attribute *attr,
7728 struct symbol *sym,
7729 int bits)
7730{
7731 LONGEST l = DW_UNSND (attr);
7732
7733 if (bits < sizeof (l) * 8)
7734 {
7735 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
7736 l &= ((LONGEST) 1 << bits) - 1;
7737 else
bf9198f1 7738 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
7739 }
7740
7741 SYMBOL_VALUE (sym) = l;
7742 SYMBOL_CLASS (sym) = LOC_CONST;
7743}
7744
7745
c906108c
SS
7746/* Return the type of the die in question using its DW_AT_type attribute. */
7747
7748static struct type *
e7c27a73 7749die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7750{
5e2b427d 7751 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
c906108c
SS
7752 struct type *type;
7753 struct attribute *type_attr;
7754 struct die_info *type_die;
c906108c 7755
e142c38c 7756 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
7757 if (!type_attr)
7758 {
7759 /* A missing DW_AT_type represents a void type. */
5e2b427d 7760 return builtin_type (gdbarch)->builtin_void;
c906108c
SS
7761 }
7762 else
f2f0e013 7763 type_die = follow_die_ref (die, type_attr, &cu);
10b3939b 7764
e7c27a73 7765 type = tag_type_to_type (type_die, cu);
c906108c
SS
7766 if (!type)
7767 {
7768 dump_die (type_die);
8a3fe4f8 7769 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
e7c27a73 7770 cu->objfile->name);
c906108c
SS
7771 }
7772 return type;
7773}
7774
7775/* Return the containing type of the die in question using its
7776 DW_AT_containing_type attribute. */
7777
7778static struct type *
e7c27a73 7779die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7780{
7781 struct type *type = NULL;
7782 struct attribute *type_attr;
7783 struct die_info *type_die = NULL;
c906108c 7784
e142c38c 7785 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
c906108c
SS
7786 if (type_attr)
7787 {
f2f0e013 7788 type_die = follow_die_ref (die, type_attr, &cu);
e7c27a73 7789 type = tag_type_to_type (type_die, cu);
c906108c
SS
7790 }
7791 if (!type)
7792 {
7793 if (type_die)
7794 dump_die (type_die);
8a3fe4f8 7795 error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
e7c27a73 7796 cu->objfile->name);
c906108c
SS
7797 }
7798 return type;
7799}
7800
c906108c 7801static struct type *
e7c27a73 7802tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7803{
f792889a
DJ
7804 struct type *this_type;
7805
7806 this_type = read_type_die (die, cu);
7807 if (!this_type)
c906108c 7808 {
f792889a
DJ
7809 dump_die (die);
7810 error (_("Dwarf Error: Cannot find type of die [in module %s]"),
7811 cu->objfile->name);
c906108c 7812 }
f792889a 7813 return this_type;
c906108c
SS
7814}
7815
f792889a 7816static struct type *
e7c27a73 7817read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7818{
f792889a
DJ
7819 struct type *this_type;
7820
7821 this_type = get_die_type (die, cu);
7822 if (this_type)
7823 return this_type;
7824
c906108c
SS
7825 switch (die->tag)
7826 {
7827 case DW_TAG_class_type:
680b30c7 7828 case DW_TAG_interface_type:
c906108c
SS
7829 case DW_TAG_structure_type:
7830 case DW_TAG_union_type:
f792889a 7831 this_type = read_structure_type (die, cu);
c906108c
SS
7832 break;
7833 case DW_TAG_enumeration_type:
f792889a 7834 this_type = read_enumeration_type (die, cu);
c906108c
SS
7835 break;
7836 case DW_TAG_subprogram:
7837 case DW_TAG_subroutine_type:
f792889a 7838 this_type = read_subroutine_type (die, cu);
c906108c
SS
7839 break;
7840 case DW_TAG_array_type:
f792889a 7841 this_type = read_array_type (die, cu);
c906108c 7842 break;
72019c9c 7843 case DW_TAG_set_type:
f792889a 7844 this_type = read_set_type (die, cu);
72019c9c 7845 break;
c906108c 7846 case DW_TAG_pointer_type:
f792889a 7847 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
7848 break;
7849 case DW_TAG_ptr_to_member_type:
f792889a 7850 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
7851 break;
7852 case DW_TAG_reference_type:
f792889a 7853 this_type = read_tag_reference_type (die, cu);
c906108c
SS
7854 break;
7855 case DW_TAG_const_type:
f792889a 7856 this_type = read_tag_const_type (die, cu);
c906108c
SS
7857 break;
7858 case DW_TAG_volatile_type:
f792889a 7859 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
7860 break;
7861 case DW_TAG_string_type:
f792889a 7862 this_type = read_tag_string_type (die, cu);
c906108c
SS
7863 break;
7864 case DW_TAG_typedef:
f792889a 7865 this_type = read_typedef (die, cu);
c906108c 7866 break;
a02abb62 7867 case DW_TAG_subrange_type:
f792889a 7868 this_type = read_subrange_type (die, cu);
a02abb62 7869 break;
c906108c 7870 case DW_TAG_base_type:
f792889a 7871 this_type = read_base_type (die, cu);
c906108c 7872 break;
81a17f79 7873 case DW_TAG_unspecified_type:
f792889a 7874 this_type = read_unspecified_type (die, cu);
81a17f79 7875 break;
0114d602
DJ
7876 case DW_TAG_namespace:
7877 this_type = read_namespace_type (die, cu);
7878 break;
c906108c 7879 default:
a1f5b845 7880 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 7881 dwarf_tag_name (die->tag));
c906108c
SS
7882 break;
7883 }
63d06c5c 7884
f792889a 7885 return this_type;
63d06c5c
DC
7886}
7887
fdde2d81 7888/* Return the name of the namespace/class that DIE is defined within,
0114d602 7889 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 7890
0114d602
DJ
7891 For example, if we're within the method foo() in the following
7892 code:
7893
7894 namespace N {
7895 class C {
7896 void foo () {
7897 }
7898 };
7899 }
7900
7901 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
7902
7903static char *
e142c38c 7904determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 7905{
0114d602
DJ
7906 struct die_info *parent, *spec_die;
7907 struct dwarf2_cu *spec_cu;
7908 struct type *parent_type;
63d06c5c 7909
987504bb
JJ
7910 if (cu->language != language_cplus
7911 && cu->language != language_java)
0114d602
DJ
7912 return "";
7913
7914 /* We have to be careful in the presence of DW_AT_specification.
7915 For example, with GCC 3.4, given the code
7916
7917 namespace N {
7918 void foo() {
7919 // Definition of N::foo.
7920 }
7921 }
7922
7923 then we'll have a tree of DIEs like this:
7924
7925 1: DW_TAG_compile_unit
7926 2: DW_TAG_namespace // N
7927 3: DW_TAG_subprogram // declaration of N::foo
7928 4: DW_TAG_subprogram // definition of N::foo
7929 DW_AT_specification // refers to die #3
7930
7931 Thus, when processing die #4, we have to pretend that we're in
7932 the context of its DW_AT_specification, namely the contex of die
7933 #3. */
7934 spec_cu = cu;
7935 spec_die = die_specification (die, &spec_cu);
7936 if (spec_die == NULL)
7937 parent = die->parent;
7938 else
63d06c5c 7939 {
0114d602
DJ
7940 parent = spec_die->parent;
7941 cu = spec_cu;
63d06c5c 7942 }
0114d602
DJ
7943
7944 if (parent == NULL)
7945 return "";
63d06c5c 7946 else
0114d602
DJ
7947 switch (parent->tag)
7948 {
63d06c5c 7949 case DW_TAG_namespace:
0114d602
DJ
7950 parent_type = read_type_die (parent, cu);
7951 /* We give a name to even anonymous namespaces. */
7952 return TYPE_TAG_NAME (parent_type);
63d06c5c 7953 case DW_TAG_class_type:
680b30c7 7954 case DW_TAG_interface_type:
63d06c5c 7955 case DW_TAG_structure_type:
0114d602
DJ
7956 case DW_TAG_union_type:
7957 parent_type = read_type_die (parent, cu);
7958 if (TYPE_TAG_NAME (parent_type) != NULL)
7959 return TYPE_TAG_NAME (parent_type);
7960 else
7961 /* An anonymous structure is only allowed non-static data
7962 members; no typedefs, no member functions, et cetera.
7963 So it does not need a prefix. */
7964 return "";
63d06c5c 7965 default:
8176b9b8 7966 return determine_prefix (parent, cu);
63d06c5c 7967 }
63d06c5c
DC
7968}
7969
987504bb
JJ
7970/* Return a newly-allocated string formed by concatenating PREFIX and
7971 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
7972 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
7973 perform an obconcat, otherwise allocate storage for the result. The CU argument
7974 is used to determine the language and hence, the appropriate separator. */
7975
7976#define MAX_SEP_LEN 2 /* sizeof ("::") */
63d06c5c
DC
7977
7978static char *
987504bb
JJ
7979typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
7980 struct dwarf2_cu *cu)
63d06c5c 7981{
987504bb 7982 char *sep;
63d06c5c 7983
987504bb
JJ
7984 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
7985 sep = "";
7986 else if (cu->language == language_java)
7987 sep = ".";
7988 else
7989 sep = "::";
63d06c5c 7990
987504bb
JJ
7991 if (obs == NULL)
7992 {
7993 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
7994 retval[0] = '\0';
7995
7996 if (prefix)
7997 {
7998 strcpy (retval, prefix);
7999 strcat (retval, sep);
8000 }
8001 if (suffix)
8002 strcat (retval, suffix);
8003
63d06c5c
DC
8004 return retval;
8005 }
987504bb
JJ
8006 else
8007 {
8008 /* We have an obstack. */
8009 return obconcat (obs, prefix, sep, suffix);
8010 }
63d06c5c
DC
8011}
8012
c906108c
SS
8013/* Return sibling of die, NULL if no sibling. */
8014
f9aca02d 8015static struct die_info *
fba45db2 8016sibling_die (struct die_info *die)
c906108c 8017{
639d11d3 8018 return die->sibling;
c906108c
SS
8019}
8020
8021/* Get linkage name of a die, return NULL if not found. */
8022
8023static char *
e142c38c 8024dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8025{
8026 struct attribute *attr;
8027
e142c38c 8028 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
c906108c
SS
8029 if (attr && DW_STRING (attr))
8030 return DW_STRING (attr);
e142c38c 8031 attr = dwarf2_attr (die, DW_AT_name, cu);
c906108c
SS
8032 if (attr && DW_STRING (attr))
8033 return DW_STRING (attr);
8034 return NULL;
8035}
8036
9219021c
DC
8037/* Get name of a die, return NULL if not found. */
8038
8039static char *
e142c38c 8040dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
8041{
8042 struct attribute *attr;
8043
e142c38c 8044 attr = dwarf2_attr (die, DW_AT_name, cu);
9219021c
DC
8045 if (attr && DW_STRING (attr))
8046 return DW_STRING (attr);
8047 return NULL;
8048}
8049
8050/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
8051 is none. *EXT_CU is the CU containing DIE on input, and the CU
8052 containing the return value on output. */
9219021c
DC
8053
8054static struct die_info *
f2f0e013 8055dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
8056{
8057 struct attribute *attr;
9219021c 8058
f2f0e013 8059 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
8060 if (attr == NULL)
8061 return NULL;
8062
f2f0e013 8063 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
8064}
8065
c906108c
SS
8066/* Convert a DIE tag into its string name. */
8067
8068static char *
aa1ee363 8069dwarf_tag_name (unsigned tag)
c906108c
SS
8070{
8071 switch (tag)
8072 {
8073 case DW_TAG_padding:
8074 return "DW_TAG_padding";
8075 case DW_TAG_array_type:
8076 return "DW_TAG_array_type";
8077 case DW_TAG_class_type:
8078 return "DW_TAG_class_type";
8079 case DW_TAG_entry_point:
8080 return "DW_TAG_entry_point";
8081 case DW_TAG_enumeration_type:
8082 return "DW_TAG_enumeration_type";
8083 case DW_TAG_formal_parameter:
8084 return "DW_TAG_formal_parameter";
8085 case DW_TAG_imported_declaration:
8086 return "DW_TAG_imported_declaration";
8087 case DW_TAG_label:
8088 return "DW_TAG_label";
8089 case DW_TAG_lexical_block:
8090 return "DW_TAG_lexical_block";
8091 case DW_TAG_member:
8092 return "DW_TAG_member";
8093 case DW_TAG_pointer_type:
8094 return "DW_TAG_pointer_type";
8095 case DW_TAG_reference_type:
8096 return "DW_TAG_reference_type";
8097 case DW_TAG_compile_unit:
8098 return "DW_TAG_compile_unit";
8099 case DW_TAG_string_type:
8100 return "DW_TAG_string_type";
8101 case DW_TAG_structure_type:
8102 return "DW_TAG_structure_type";
8103 case DW_TAG_subroutine_type:
8104 return "DW_TAG_subroutine_type";
8105 case DW_TAG_typedef:
8106 return "DW_TAG_typedef";
8107 case DW_TAG_union_type:
8108 return "DW_TAG_union_type";
8109 case DW_TAG_unspecified_parameters:
8110 return "DW_TAG_unspecified_parameters";
8111 case DW_TAG_variant:
8112 return "DW_TAG_variant";
8113 case DW_TAG_common_block:
8114 return "DW_TAG_common_block";
8115 case DW_TAG_common_inclusion:
8116 return "DW_TAG_common_inclusion";
8117 case DW_TAG_inheritance:
8118 return "DW_TAG_inheritance";
8119 case DW_TAG_inlined_subroutine:
8120 return "DW_TAG_inlined_subroutine";
8121 case DW_TAG_module:
8122 return "DW_TAG_module";
8123 case DW_TAG_ptr_to_member_type:
8124 return "DW_TAG_ptr_to_member_type";
8125 case DW_TAG_set_type:
8126 return "DW_TAG_set_type";
8127 case DW_TAG_subrange_type:
8128 return "DW_TAG_subrange_type";
8129 case DW_TAG_with_stmt:
8130 return "DW_TAG_with_stmt";
8131 case DW_TAG_access_declaration:
8132 return "DW_TAG_access_declaration";
8133 case DW_TAG_base_type:
8134 return "DW_TAG_base_type";
8135 case DW_TAG_catch_block:
8136 return "DW_TAG_catch_block";
8137 case DW_TAG_const_type:
8138 return "DW_TAG_const_type";
8139 case DW_TAG_constant:
8140 return "DW_TAG_constant";
8141 case DW_TAG_enumerator:
8142 return "DW_TAG_enumerator";
8143 case DW_TAG_file_type:
8144 return "DW_TAG_file_type";
8145 case DW_TAG_friend:
8146 return "DW_TAG_friend";
8147 case DW_TAG_namelist:
8148 return "DW_TAG_namelist";
8149 case DW_TAG_namelist_item:
8150 return "DW_TAG_namelist_item";
8151 case DW_TAG_packed_type:
8152 return "DW_TAG_packed_type";
8153 case DW_TAG_subprogram:
8154 return "DW_TAG_subprogram";
8155 case DW_TAG_template_type_param:
8156 return "DW_TAG_template_type_param";
8157 case DW_TAG_template_value_param:
8158 return "DW_TAG_template_value_param";
8159 case DW_TAG_thrown_type:
8160 return "DW_TAG_thrown_type";
8161 case DW_TAG_try_block:
8162 return "DW_TAG_try_block";
8163 case DW_TAG_variant_part:
8164 return "DW_TAG_variant_part";
8165 case DW_TAG_variable:
8166 return "DW_TAG_variable";
8167 case DW_TAG_volatile_type:
8168 return "DW_TAG_volatile_type";
d9fa45fe
DC
8169 case DW_TAG_dwarf_procedure:
8170 return "DW_TAG_dwarf_procedure";
8171 case DW_TAG_restrict_type:
8172 return "DW_TAG_restrict_type";
8173 case DW_TAG_interface_type:
8174 return "DW_TAG_interface_type";
8175 case DW_TAG_namespace:
8176 return "DW_TAG_namespace";
8177 case DW_TAG_imported_module:
8178 return "DW_TAG_imported_module";
8179 case DW_TAG_unspecified_type:
8180 return "DW_TAG_unspecified_type";
8181 case DW_TAG_partial_unit:
8182 return "DW_TAG_partial_unit";
8183 case DW_TAG_imported_unit:
8184 return "DW_TAG_imported_unit";
b7619582
GF
8185 case DW_TAG_condition:
8186 return "DW_TAG_condition";
8187 case DW_TAG_shared_type:
8188 return "DW_TAG_shared_type";
c906108c
SS
8189 case DW_TAG_MIPS_loop:
8190 return "DW_TAG_MIPS_loop";
b7619582
GF
8191 case DW_TAG_HP_array_descriptor:
8192 return "DW_TAG_HP_array_descriptor";
c906108c
SS
8193 case DW_TAG_format_label:
8194 return "DW_TAG_format_label";
8195 case DW_TAG_function_template:
8196 return "DW_TAG_function_template";
8197 case DW_TAG_class_template:
8198 return "DW_TAG_class_template";
b7619582
GF
8199 case DW_TAG_GNU_BINCL:
8200 return "DW_TAG_GNU_BINCL";
8201 case DW_TAG_GNU_EINCL:
8202 return "DW_TAG_GNU_EINCL";
8203 case DW_TAG_upc_shared_type:
8204 return "DW_TAG_upc_shared_type";
8205 case DW_TAG_upc_strict_type:
8206 return "DW_TAG_upc_strict_type";
8207 case DW_TAG_upc_relaxed_type:
8208 return "DW_TAG_upc_relaxed_type";
8209 case DW_TAG_PGI_kanji_type:
8210 return "DW_TAG_PGI_kanji_type";
8211 case DW_TAG_PGI_interface_block:
8212 return "DW_TAG_PGI_interface_block";
c906108c
SS
8213 default:
8214 return "DW_TAG_<unknown>";
8215 }
8216}
8217
8218/* Convert a DWARF attribute code into its string name. */
8219
8220static char *
aa1ee363 8221dwarf_attr_name (unsigned attr)
c906108c
SS
8222{
8223 switch (attr)
8224 {
8225 case DW_AT_sibling:
8226 return "DW_AT_sibling";
8227 case DW_AT_location:
8228 return "DW_AT_location";
8229 case DW_AT_name:
8230 return "DW_AT_name";
8231 case DW_AT_ordering:
8232 return "DW_AT_ordering";
8233 case DW_AT_subscr_data:
8234 return "DW_AT_subscr_data";
8235 case DW_AT_byte_size:
8236 return "DW_AT_byte_size";
8237 case DW_AT_bit_offset:
8238 return "DW_AT_bit_offset";
8239 case DW_AT_bit_size:
8240 return "DW_AT_bit_size";
8241 case DW_AT_element_list:
8242 return "DW_AT_element_list";
8243 case DW_AT_stmt_list:
8244 return "DW_AT_stmt_list";
8245 case DW_AT_low_pc:
8246 return "DW_AT_low_pc";
8247 case DW_AT_high_pc:
8248 return "DW_AT_high_pc";
8249 case DW_AT_language:
8250 return "DW_AT_language";
8251 case DW_AT_member:
8252 return "DW_AT_member";
8253 case DW_AT_discr:
8254 return "DW_AT_discr";
8255 case DW_AT_discr_value:
8256 return "DW_AT_discr_value";
8257 case DW_AT_visibility:
8258 return "DW_AT_visibility";
8259 case DW_AT_import:
8260 return "DW_AT_import";
8261 case DW_AT_string_length:
8262 return "DW_AT_string_length";
8263 case DW_AT_common_reference:
8264 return "DW_AT_common_reference";
8265 case DW_AT_comp_dir:
8266 return "DW_AT_comp_dir";
8267 case DW_AT_const_value:
8268 return "DW_AT_const_value";
8269 case DW_AT_containing_type:
8270 return "DW_AT_containing_type";
8271 case DW_AT_default_value:
8272 return "DW_AT_default_value";
8273 case DW_AT_inline:
8274 return "DW_AT_inline";
8275 case DW_AT_is_optional:
8276 return "DW_AT_is_optional";
8277 case DW_AT_lower_bound:
8278 return "DW_AT_lower_bound";
8279 case DW_AT_producer:
8280 return "DW_AT_producer";
8281 case DW_AT_prototyped:
8282 return "DW_AT_prototyped";
8283 case DW_AT_return_addr:
8284 return "DW_AT_return_addr";
8285 case DW_AT_start_scope:
8286 return "DW_AT_start_scope";
09fa0d7c
JK
8287 case DW_AT_bit_stride:
8288 return "DW_AT_bit_stride";
c906108c
SS
8289 case DW_AT_upper_bound:
8290 return "DW_AT_upper_bound";
8291 case DW_AT_abstract_origin:
8292 return "DW_AT_abstract_origin";
8293 case DW_AT_accessibility:
8294 return "DW_AT_accessibility";
8295 case DW_AT_address_class:
8296 return "DW_AT_address_class";
8297 case DW_AT_artificial:
8298 return "DW_AT_artificial";
8299 case DW_AT_base_types:
8300 return "DW_AT_base_types";
8301 case DW_AT_calling_convention:
8302 return "DW_AT_calling_convention";
8303 case DW_AT_count:
8304 return "DW_AT_count";
8305 case DW_AT_data_member_location:
8306 return "DW_AT_data_member_location";
8307 case DW_AT_decl_column:
8308 return "DW_AT_decl_column";
8309 case DW_AT_decl_file:
8310 return "DW_AT_decl_file";
8311 case DW_AT_decl_line:
8312 return "DW_AT_decl_line";
8313 case DW_AT_declaration:
8314 return "DW_AT_declaration";
8315 case DW_AT_discr_list:
8316 return "DW_AT_discr_list";
8317 case DW_AT_encoding:
8318 return "DW_AT_encoding";
8319 case DW_AT_external:
8320 return "DW_AT_external";
8321 case DW_AT_frame_base:
8322 return "DW_AT_frame_base";
8323 case DW_AT_friend:
8324 return "DW_AT_friend";
8325 case DW_AT_identifier_case:
8326 return "DW_AT_identifier_case";
8327 case DW_AT_macro_info:
8328 return "DW_AT_macro_info";
8329 case DW_AT_namelist_items:
8330 return "DW_AT_namelist_items";
8331 case DW_AT_priority:
8332 return "DW_AT_priority";
8333 case DW_AT_segment:
8334 return "DW_AT_segment";
8335 case DW_AT_specification:
8336 return "DW_AT_specification";
8337 case DW_AT_static_link:
8338 return "DW_AT_static_link";
8339 case DW_AT_type:
8340 return "DW_AT_type";
8341 case DW_AT_use_location:
8342 return "DW_AT_use_location";
8343 case DW_AT_variable_parameter:
8344 return "DW_AT_variable_parameter";
8345 case DW_AT_virtuality:
8346 return "DW_AT_virtuality";
8347 case DW_AT_vtable_elem_location:
8348 return "DW_AT_vtable_elem_location";
b7619582 8349 /* DWARF 3 values. */
d9fa45fe
DC
8350 case DW_AT_allocated:
8351 return "DW_AT_allocated";
8352 case DW_AT_associated:
8353 return "DW_AT_associated";
8354 case DW_AT_data_location:
8355 return "DW_AT_data_location";
09fa0d7c
JK
8356 case DW_AT_byte_stride:
8357 return "DW_AT_byte_stride";
d9fa45fe
DC
8358 case DW_AT_entry_pc:
8359 return "DW_AT_entry_pc";
8360 case DW_AT_use_UTF8:
8361 return "DW_AT_use_UTF8";
8362 case DW_AT_extension:
8363 return "DW_AT_extension";
8364 case DW_AT_ranges:
8365 return "DW_AT_ranges";
8366 case DW_AT_trampoline:
8367 return "DW_AT_trampoline";
8368 case DW_AT_call_column:
8369 return "DW_AT_call_column";
8370 case DW_AT_call_file:
8371 return "DW_AT_call_file";
8372 case DW_AT_call_line:
8373 return "DW_AT_call_line";
b7619582
GF
8374 case DW_AT_description:
8375 return "DW_AT_description";
8376 case DW_AT_binary_scale:
8377 return "DW_AT_binary_scale";
8378 case DW_AT_decimal_scale:
8379 return "DW_AT_decimal_scale";
8380 case DW_AT_small:
8381 return "DW_AT_small";
8382 case DW_AT_decimal_sign:
8383 return "DW_AT_decimal_sign";
8384 case DW_AT_digit_count:
8385 return "DW_AT_digit_count";
8386 case DW_AT_picture_string:
8387 return "DW_AT_picture_string";
8388 case DW_AT_mutable:
8389 return "DW_AT_mutable";
8390 case DW_AT_threads_scaled:
8391 return "DW_AT_threads_scaled";
8392 case DW_AT_explicit:
8393 return "DW_AT_explicit";
8394 case DW_AT_object_pointer:
8395 return "DW_AT_object_pointer";
8396 case DW_AT_endianity:
8397 return "DW_AT_endianity";
8398 case DW_AT_elemental:
8399 return "DW_AT_elemental";
8400 case DW_AT_pure:
8401 return "DW_AT_pure";
8402 case DW_AT_recursive:
8403 return "DW_AT_recursive";
c906108c 8404#ifdef MIPS
b7619582 8405 /* SGI/MIPS extensions. */
c906108c
SS
8406 case DW_AT_MIPS_fde:
8407 return "DW_AT_MIPS_fde";
8408 case DW_AT_MIPS_loop_begin:
8409 return "DW_AT_MIPS_loop_begin";
8410 case DW_AT_MIPS_tail_loop_begin:
8411 return "DW_AT_MIPS_tail_loop_begin";
8412 case DW_AT_MIPS_epilog_begin:
8413 return "DW_AT_MIPS_epilog_begin";
8414 case DW_AT_MIPS_loop_unroll_factor:
8415 return "DW_AT_MIPS_loop_unroll_factor";
8416 case DW_AT_MIPS_software_pipeline_depth:
8417 return "DW_AT_MIPS_software_pipeline_depth";
8418 case DW_AT_MIPS_linkage_name:
8419 return "DW_AT_MIPS_linkage_name";
b7619582
GF
8420 case DW_AT_MIPS_stride:
8421 return "DW_AT_MIPS_stride";
8422 case DW_AT_MIPS_abstract_name:
8423 return "DW_AT_MIPS_abstract_name";
8424 case DW_AT_MIPS_clone_origin:
8425 return "DW_AT_MIPS_clone_origin";
8426 case DW_AT_MIPS_has_inlines:
8427 return "DW_AT_MIPS_has_inlines";
8428#endif
8429 /* HP extensions. */
8430 case DW_AT_HP_block_index:
8431 return "DW_AT_HP_block_index";
8432 case DW_AT_HP_unmodifiable:
8433 return "DW_AT_HP_unmodifiable";
8434 case DW_AT_HP_actuals_stmt_list:
8435 return "DW_AT_HP_actuals_stmt_list";
8436 case DW_AT_HP_proc_per_section:
8437 return "DW_AT_HP_proc_per_section";
8438 case DW_AT_HP_raw_data_ptr:
8439 return "DW_AT_HP_raw_data_ptr";
8440 case DW_AT_HP_pass_by_reference:
8441 return "DW_AT_HP_pass_by_reference";
8442 case DW_AT_HP_opt_level:
8443 return "DW_AT_HP_opt_level";
8444 case DW_AT_HP_prof_version_id:
8445 return "DW_AT_HP_prof_version_id";
8446 case DW_AT_HP_opt_flags:
8447 return "DW_AT_HP_opt_flags";
8448 case DW_AT_HP_cold_region_low_pc:
8449 return "DW_AT_HP_cold_region_low_pc";
8450 case DW_AT_HP_cold_region_high_pc:
8451 return "DW_AT_HP_cold_region_high_pc";
8452 case DW_AT_HP_all_variables_modifiable:
8453 return "DW_AT_HP_all_variables_modifiable";
8454 case DW_AT_HP_linkage_name:
8455 return "DW_AT_HP_linkage_name";
8456 case DW_AT_HP_prof_flags:
8457 return "DW_AT_HP_prof_flags";
8458 /* GNU extensions. */
c906108c
SS
8459 case DW_AT_sf_names:
8460 return "DW_AT_sf_names";
8461 case DW_AT_src_info:
8462 return "DW_AT_src_info";
8463 case DW_AT_mac_info:
8464 return "DW_AT_mac_info";
8465 case DW_AT_src_coords:
8466 return "DW_AT_src_coords";
8467 case DW_AT_body_begin:
8468 return "DW_AT_body_begin";
8469 case DW_AT_body_end:
8470 return "DW_AT_body_end";
f5f8a009
EZ
8471 case DW_AT_GNU_vector:
8472 return "DW_AT_GNU_vector";
b7619582
GF
8473 /* VMS extensions. */
8474 case DW_AT_VMS_rtnbeg_pd_address:
8475 return "DW_AT_VMS_rtnbeg_pd_address";
8476 /* UPC extension. */
8477 case DW_AT_upc_threads_scaled:
8478 return "DW_AT_upc_threads_scaled";
8479 /* PGI (STMicroelectronics) extensions. */
8480 case DW_AT_PGI_lbase:
8481 return "DW_AT_PGI_lbase";
8482 case DW_AT_PGI_soffset:
8483 return "DW_AT_PGI_soffset";
8484 case DW_AT_PGI_lstride:
8485 return "DW_AT_PGI_lstride";
c906108c
SS
8486 default:
8487 return "DW_AT_<unknown>";
8488 }
8489}
8490
8491/* Convert a DWARF value form code into its string name. */
8492
8493static char *
aa1ee363 8494dwarf_form_name (unsigned form)
c906108c
SS
8495{
8496 switch (form)
8497 {
8498 case DW_FORM_addr:
8499 return "DW_FORM_addr";
8500 case DW_FORM_block2:
8501 return "DW_FORM_block2";
8502 case DW_FORM_block4:
8503 return "DW_FORM_block4";
8504 case DW_FORM_data2:
8505 return "DW_FORM_data2";
8506 case DW_FORM_data4:
8507 return "DW_FORM_data4";
8508 case DW_FORM_data8:
8509 return "DW_FORM_data8";
8510 case DW_FORM_string:
8511 return "DW_FORM_string";
8512 case DW_FORM_block:
8513 return "DW_FORM_block";
8514 case DW_FORM_block1:
8515 return "DW_FORM_block1";
8516 case DW_FORM_data1:
8517 return "DW_FORM_data1";
8518 case DW_FORM_flag:
8519 return "DW_FORM_flag";
8520 case DW_FORM_sdata:
8521 return "DW_FORM_sdata";
8522 case DW_FORM_strp:
8523 return "DW_FORM_strp";
8524 case DW_FORM_udata:
8525 return "DW_FORM_udata";
8526 case DW_FORM_ref_addr:
8527 return "DW_FORM_ref_addr";
8528 case DW_FORM_ref1:
8529 return "DW_FORM_ref1";
8530 case DW_FORM_ref2:
8531 return "DW_FORM_ref2";
8532 case DW_FORM_ref4:
8533 return "DW_FORM_ref4";
8534 case DW_FORM_ref8:
8535 return "DW_FORM_ref8";
8536 case DW_FORM_ref_udata:
8537 return "DW_FORM_ref_udata";
8538 case DW_FORM_indirect:
8539 return "DW_FORM_indirect";
8540 default:
8541 return "DW_FORM_<unknown>";
8542 }
8543}
8544
8545/* Convert a DWARF stack opcode into its string name. */
8546
8547static char *
aa1ee363 8548dwarf_stack_op_name (unsigned op)
c906108c
SS
8549{
8550 switch (op)
8551 {
8552 case DW_OP_addr:
8553 return "DW_OP_addr";
8554 case DW_OP_deref:
8555 return "DW_OP_deref";
8556 case DW_OP_const1u:
8557 return "DW_OP_const1u";
8558 case DW_OP_const1s:
8559 return "DW_OP_const1s";
8560 case DW_OP_const2u:
8561 return "DW_OP_const2u";
8562 case DW_OP_const2s:
8563 return "DW_OP_const2s";
8564 case DW_OP_const4u:
8565 return "DW_OP_const4u";
8566 case DW_OP_const4s:
8567 return "DW_OP_const4s";
8568 case DW_OP_const8u:
8569 return "DW_OP_const8u";
8570 case DW_OP_const8s:
8571 return "DW_OP_const8s";
8572 case DW_OP_constu:
8573 return "DW_OP_constu";
8574 case DW_OP_consts:
8575 return "DW_OP_consts";
8576 case DW_OP_dup:
8577 return "DW_OP_dup";
8578 case DW_OP_drop:
8579 return "DW_OP_drop";
8580 case DW_OP_over:
8581 return "DW_OP_over";
8582 case DW_OP_pick:
8583 return "DW_OP_pick";
8584 case DW_OP_swap:
8585 return "DW_OP_swap";
8586 case DW_OP_rot:
8587 return "DW_OP_rot";
8588 case DW_OP_xderef:
8589 return "DW_OP_xderef";
8590 case DW_OP_abs:
8591 return "DW_OP_abs";
8592 case DW_OP_and:
8593 return "DW_OP_and";
8594 case DW_OP_div:
8595 return "DW_OP_div";
8596 case DW_OP_minus:
8597 return "DW_OP_minus";
8598 case DW_OP_mod:
8599 return "DW_OP_mod";
8600 case DW_OP_mul:
8601 return "DW_OP_mul";
8602 case DW_OP_neg:
8603 return "DW_OP_neg";
8604 case DW_OP_not:
8605 return "DW_OP_not";
8606 case DW_OP_or:
8607 return "DW_OP_or";
8608 case DW_OP_plus:
8609 return "DW_OP_plus";
8610 case DW_OP_plus_uconst:
8611 return "DW_OP_plus_uconst";
8612 case DW_OP_shl:
8613 return "DW_OP_shl";
8614 case DW_OP_shr:
8615 return "DW_OP_shr";
8616 case DW_OP_shra:
8617 return "DW_OP_shra";
8618 case DW_OP_xor:
8619 return "DW_OP_xor";
8620 case DW_OP_bra:
8621 return "DW_OP_bra";
8622 case DW_OP_eq:
8623 return "DW_OP_eq";
8624 case DW_OP_ge:
8625 return "DW_OP_ge";
8626 case DW_OP_gt:
8627 return "DW_OP_gt";
8628 case DW_OP_le:
8629 return "DW_OP_le";
8630 case DW_OP_lt:
8631 return "DW_OP_lt";
8632 case DW_OP_ne:
8633 return "DW_OP_ne";
8634 case DW_OP_skip:
8635 return "DW_OP_skip";
8636 case DW_OP_lit0:
8637 return "DW_OP_lit0";
8638 case DW_OP_lit1:
8639 return "DW_OP_lit1";
8640 case DW_OP_lit2:
8641 return "DW_OP_lit2";
8642 case DW_OP_lit3:
8643 return "DW_OP_lit3";
8644 case DW_OP_lit4:
8645 return "DW_OP_lit4";
8646 case DW_OP_lit5:
8647 return "DW_OP_lit5";
8648 case DW_OP_lit6:
8649 return "DW_OP_lit6";
8650 case DW_OP_lit7:
8651 return "DW_OP_lit7";
8652 case DW_OP_lit8:
8653 return "DW_OP_lit8";
8654 case DW_OP_lit9:
8655 return "DW_OP_lit9";
8656 case DW_OP_lit10:
8657 return "DW_OP_lit10";
8658 case DW_OP_lit11:
8659 return "DW_OP_lit11";
8660 case DW_OP_lit12:
8661 return "DW_OP_lit12";
8662 case DW_OP_lit13:
8663 return "DW_OP_lit13";
8664 case DW_OP_lit14:
8665 return "DW_OP_lit14";
8666 case DW_OP_lit15:
8667 return "DW_OP_lit15";
8668 case DW_OP_lit16:
8669 return "DW_OP_lit16";
8670 case DW_OP_lit17:
8671 return "DW_OP_lit17";
8672 case DW_OP_lit18:
8673 return "DW_OP_lit18";
8674 case DW_OP_lit19:
8675 return "DW_OP_lit19";
8676 case DW_OP_lit20:
8677 return "DW_OP_lit20";
8678 case DW_OP_lit21:
8679 return "DW_OP_lit21";
8680 case DW_OP_lit22:
8681 return "DW_OP_lit22";
8682 case DW_OP_lit23:
8683 return "DW_OP_lit23";
8684 case DW_OP_lit24:
8685 return "DW_OP_lit24";
8686 case DW_OP_lit25:
8687 return "DW_OP_lit25";
8688 case DW_OP_lit26:
8689 return "DW_OP_lit26";
8690 case DW_OP_lit27:
8691 return "DW_OP_lit27";
8692 case DW_OP_lit28:
8693 return "DW_OP_lit28";
8694 case DW_OP_lit29:
8695 return "DW_OP_lit29";
8696 case DW_OP_lit30:
8697 return "DW_OP_lit30";
8698 case DW_OP_lit31:
8699 return "DW_OP_lit31";
8700 case DW_OP_reg0:
8701 return "DW_OP_reg0";
8702 case DW_OP_reg1:
8703 return "DW_OP_reg1";
8704 case DW_OP_reg2:
8705 return "DW_OP_reg2";
8706 case DW_OP_reg3:
8707 return "DW_OP_reg3";
8708 case DW_OP_reg4:
8709 return "DW_OP_reg4";
8710 case DW_OP_reg5:
8711 return "DW_OP_reg5";
8712 case DW_OP_reg6:
8713 return "DW_OP_reg6";
8714 case DW_OP_reg7:
8715 return "DW_OP_reg7";
8716 case DW_OP_reg8:
8717 return "DW_OP_reg8";
8718 case DW_OP_reg9:
8719 return "DW_OP_reg9";
8720 case DW_OP_reg10:
8721 return "DW_OP_reg10";
8722 case DW_OP_reg11:
8723 return "DW_OP_reg11";
8724 case DW_OP_reg12:
8725 return "DW_OP_reg12";
8726 case DW_OP_reg13:
8727 return "DW_OP_reg13";
8728 case DW_OP_reg14:
8729 return "DW_OP_reg14";
8730 case DW_OP_reg15:
8731 return "DW_OP_reg15";
8732 case DW_OP_reg16:
8733 return "DW_OP_reg16";
8734 case DW_OP_reg17:
8735 return "DW_OP_reg17";
8736 case DW_OP_reg18:
8737 return "DW_OP_reg18";
8738 case DW_OP_reg19:
8739 return "DW_OP_reg19";
8740 case DW_OP_reg20:
8741 return "DW_OP_reg20";
8742 case DW_OP_reg21:
8743 return "DW_OP_reg21";
8744 case DW_OP_reg22:
8745 return "DW_OP_reg22";
8746 case DW_OP_reg23:
8747 return "DW_OP_reg23";
8748 case DW_OP_reg24:
8749 return "DW_OP_reg24";
8750 case DW_OP_reg25:
8751 return "DW_OP_reg25";
8752 case DW_OP_reg26:
8753 return "DW_OP_reg26";
8754 case DW_OP_reg27:
8755 return "DW_OP_reg27";
8756 case DW_OP_reg28:
8757 return "DW_OP_reg28";
8758 case DW_OP_reg29:
8759 return "DW_OP_reg29";
8760 case DW_OP_reg30:
8761 return "DW_OP_reg30";
8762 case DW_OP_reg31:
8763 return "DW_OP_reg31";
8764 case DW_OP_breg0:
8765 return "DW_OP_breg0";
8766 case DW_OP_breg1:
8767 return "DW_OP_breg1";
8768 case DW_OP_breg2:
8769 return "DW_OP_breg2";
8770 case DW_OP_breg3:
8771 return "DW_OP_breg3";
8772 case DW_OP_breg4:
8773 return "DW_OP_breg4";
8774 case DW_OP_breg5:
8775 return "DW_OP_breg5";
8776 case DW_OP_breg6:
8777 return "DW_OP_breg6";
8778 case DW_OP_breg7:
8779 return "DW_OP_breg7";
8780 case DW_OP_breg8:
8781 return "DW_OP_breg8";
8782 case DW_OP_breg9:
8783 return "DW_OP_breg9";
8784 case DW_OP_breg10:
8785 return "DW_OP_breg10";
8786 case DW_OP_breg11:
8787 return "DW_OP_breg11";
8788 case DW_OP_breg12:
8789 return "DW_OP_breg12";
8790 case DW_OP_breg13:
8791 return "DW_OP_breg13";
8792 case DW_OP_breg14:
8793 return "DW_OP_breg14";
8794 case DW_OP_breg15:
8795 return "DW_OP_breg15";
8796 case DW_OP_breg16:
8797 return "DW_OP_breg16";
8798 case DW_OP_breg17:
8799 return "DW_OP_breg17";
8800 case DW_OP_breg18:
8801 return "DW_OP_breg18";
8802 case DW_OP_breg19:
8803 return "DW_OP_breg19";
8804 case DW_OP_breg20:
8805 return "DW_OP_breg20";
8806 case DW_OP_breg21:
8807 return "DW_OP_breg21";
8808 case DW_OP_breg22:
8809 return "DW_OP_breg22";
8810 case DW_OP_breg23:
8811 return "DW_OP_breg23";
8812 case DW_OP_breg24:
8813 return "DW_OP_breg24";
8814 case DW_OP_breg25:
8815 return "DW_OP_breg25";
8816 case DW_OP_breg26:
8817 return "DW_OP_breg26";
8818 case DW_OP_breg27:
8819 return "DW_OP_breg27";
8820 case DW_OP_breg28:
8821 return "DW_OP_breg28";
8822 case DW_OP_breg29:
8823 return "DW_OP_breg29";
8824 case DW_OP_breg30:
8825 return "DW_OP_breg30";
8826 case DW_OP_breg31:
8827 return "DW_OP_breg31";
8828 case DW_OP_regx:
8829 return "DW_OP_regx";
8830 case DW_OP_fbreg:
8831 return "DW_OP_fbreg";
8832 case DW_OP_bregx:
8833 return "DW_OP_bregx";
8834 case DW_OP_piece:
8835 return "DW_OP_piece";
8836 case DW_OP_deref_size:
8837 return "DW_OP_deref_size";
8838 case DW_OP_xderef_size:
8839 return "DW_OP_xderef_size";
8840 case DW_OP_nop:
8841 return "DW_OP_nop";
b7619582 8842 /* DWARF 3 extensions. */
ed348acc
EZ
8843 case DW_OP_push_object_address:
8844 return "DW_OP_push_object_address";
8845 case DW_OP_call2:
8846 return "DW_OP_call2";
8847 case DW_OP_call4:
8848 return "DW_OP_call4";
8849 case DW_OP_call_ref:
8850 return "DW_OP_call_ref";
b7619582
GF
8851 /* GNU extensions. */
8852 case DW_OP_form_tls_address:
8853 return "DW_OP_form_tls_address";
8854 case DW_OP_call_frame_cfa:
8855 return "DW_OP_call_frame_cfa";
8856 case DW_OP_bit_piece:
8857 return "DW_OP_bit_piece";
ed348acc
EZ
8858 case DW_OP_GNU_push_tls_address:
8859 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
8860 case DW_OP_GNU_uninit:
8861 return "DW_OP_GNU_uninit";
b7619582
GF
8862 /* HP extensions. */
8863 case DW_OP_HP_is_value:
8864 return "DW_OP_HP_is_value";
8865 case DW_OP_HP_fltconst4:
8866 return "DW_OP_HP_fltconst4";
8867 case DW_OP_HP_fltconst8:
8868 return "DW_OP_HP_fltconst8";
8869 case DW_OP_HP_mod_range:
8870 return "DW_OP_HP_mod_range";
8871 case DW_OP_HP_unmod_range:
8872 return "DW_OP_HP_unmod_range";
8873 case DW_OP_HP_tls:
8874 return "DW_OP_HP_tls";
c906108c
SS
8875 default:
8876 return "OP_<unknown>";
8877 }
8878}
8879
8880static char *
fba45db2 8881dwarf_bool_name (unsigned mybool)
c906108c
SS
8882{
8883 if (mybool)
8884 return "TRUE";
8885 else
8886 return "FALSE";
8887}
8888
8889/* Convert a DWARF type code into its string name. */
8890
8891static char *
aa1ee363 8892dwarf_type_encoding_name (unsigned enc)
c906108c
SS
8893{
8894 switch (enc)
8895 {
b7619582
GF
8896 case DW_ATE_void:
8897 return "DW_ATE_void";
c906108c
SS
8898 case DW_ATE_address:
8899 return "DW_ATE_address";
8900 case DW_ATE_boolean:
8901 return "DW_ATE_boolean";
8902 case DW_ATE_complex_float:
8903 return "DW_ATE_complex_float";
8904 case DW_ATE_float:
8905 return "DW_ATE_float";
8906 case DW_ATE_signed:
8907 return "DW_ATE_signed";
8908 case DW_ATE_signed_char:
8909 return "DW_ATE_signed_char";
8910 case DW_ATE_unsigned:
8911 return "DW_ATE_unsigned";
8912 case DW_ATE_unsigned_char:
8913 return "DW_ATE_unsigned_char";
b7619582 8914 /* DWARF 3. */
d9fa45fe
DC
8915 case DW_ATE_imaginary_float:
8916 return "DW_ATE_imaginary_float";
b7619582
GF
8917 case DW_ATE_packed_decimal:
8918 return "DW_ATE_packed_decimal";
8919 case DW_ATE_numeric_string:
8920 return "DW_ATE_numeric_string";
8921 case DW_ATE_edited:
8922 return "DW_ATE_edited";
8923 case DW_ATE_signed_fixed:
8924 return "DW_ATE_signed_fixed";
8925 case DW_ATE_unsigned_fixed:
8926 return "DW_ATE_unsigned_fixed";
8927 case DW_ATE_decimal_float:
8928 return "DW_ATE_decimal_float";
8929 /* HP extensions. */
8930 case DW_ATE_HP_float80:
8931 return "DW_ATE_HP_float80";
8932 case DW_ATE_HP_complex_float80:
8933 return "DW_ATE_HP_complex_float80";
8934 case DW_ATE_HP_float128:
8935 return "DW_ATE_HP_float128";
8936 case DW_ATE_HP_complex_float128:
8937 return "DW_ATE_HP_complex_float128";
8938 case DW_ATE_HP_floathpintel:
8939 return "DW_ATE_HP_floathpintel";
8940 case DW_ATE_HP_imaginary_float80:
8941 return "DW_ATE_HP_imaginary_float80";
8942 case DW_ATE_HP_imaginary_float128:
8943 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
8944 default:
8945 return "DW_ATE_<unknown>";
8946 }
8947}
8948
8949/* Convert a DWARF call frame info operation to its string name. */
8950
8951#if 0
8952static char *
aa1ee363 8953dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
8954{
8955 switch (cfi_opc)
8956 {
8957 case DW_CFA_advance_loc:
8958 return "DW_CFA_advance_loc";
8959 case DW_CFA_offset:
8960 return "DW_CFA_offset";
8961 case DW_CFA_restore:
8962 return "DW_CFA_restore";
8963 case DW_CFA_nop:
8964 return "DW_CFA_nop";
8965 case DW_CFA_set_loc:
8966 return "DW_CFA_set_loc";
8967 case DW_CFA_advance_loc1:
8968 return "DW_CFA_advance_loc1";
8969 case DW_CFA_advance_loc2:
8970 return "DW_CFA_advance_loc2";
8971 case DW_CFA_advance_loc4:
8972 return "DW_CFA_advance_loc4";
8973 case DW_CFA_offset_extended:
8974 return "DW_CFA_offset_extended";
8975 case DW_CFA_restore_extended:
8976 return "DW_CFA_restore_extended";
8977 case DW_CFA_undefined:
8978 return "DW_CFA_undefined";
8979 case DW_CFA_same_value:
8980 return "DW_CFA_same_value";
8981 case DW_CFA_register:
8982 return "DW_CFA_register";
8983 case DW_CFA_remember_state:
8984 return "DW_CFA_remember_state";
8985 case DW_CFA_restore_state:
8986 return "DW_CFA_restore_state";
8987 case DW_CFA_def_cfa:
8988 return "DW_CFA_def_cfa";
8989 case DW_CFA_def_cfa_register:
8990 return "DW_CFA_def_cfa_register";
8991 case DW_CFA_def_cfa_offset:
8992 return "DW_CFA_def_cfa_offset";
b7619582 8993 /* DWARF 3. */
985cb1a3
JM
8994 case DW_CFA_def_cfa_expression:
8995 return "DW_CFA_def_cfa_expression";
8996 case DW_CFA_expression:
8997 return "DW_CFA_expression";
8998 case DW_CFA_offset_extended_sf:
8999 return "DW_CFA_offset_extended_sf";
9000 case DW_CFA_def_cfa_sf:
9001 return "DW_CFA_def_cfa_sf";
9002 case DW_CFA_def_cfa_offset_sf:
9003 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
9004 case DW_CFA_val_offset:
9005 return "DW_CFA_val_offset";
9006 case DW_CFA_val_offset_sf:
9007 return "DW_CFA_val_offset_sf";
9008 case DW_CFA_val_expression:
9009 return "DW_CFA_val_expression";
9010 /* SGI/MIPS specific. */
c906108c
SS
9011 case DW_CFA_MIPS_advance_loc8:
9012 return "DW_CFA_MIPS_advance_loc8";
b7619582 9013 /* GNU extensions. */
985cb1a3
JM
9014 case DW_CFA_GNU_window_save:
9015 return "DW_CFA_GNU_window_save";
9016 case DW_CFA_GNU_args_size:
9017 return "DW_CFA_GNU_args_size";
9018 case DW_CFA_GNU_negative_offset_extended:
9019 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
9020 default:
9021 return "DW_CFA_<unknown>";
9022 }
9023}
9024#endif
9025
f9aca02d 9026static void
fba45db2 9027dump_die (struct die_info *die)
c906108c
SS
9028{
9029 unsigned int i;
9030
48cd0caa 9031 fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
c906108c 9032 dwarf_tag_name (die->tag), die->abbrev, die->offset);
48cd0caa 9033 fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
639d11d3 9034 dwarf_bool_name (die->child != NULL));
c906108c 9035
48cd0caa 9036 fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
c906108c
SS
9037 for (i = 0; i < die->num_attrs; ++i)
9038 {
48cd0caa 9039 fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
c906108c
SS
9040 dwarf_attr_name (die->attrs[i].name),
9041 dwarf_form_name (die->attrs[i].form));
9042 switch (die->attrs[i].form)
9043 {
9044 case DW_FORM_ref_addr:
9045 case DW_FORM_addr:
48cd0caa 9046 fprintf_unfiltered (gdb_stderr, "address: ");
ed49a04f 9047 fputs_filtered (paddress (DW_ADDR (&die->attrs[i])), gdb_stderr);
c906108c
SS
9048 break;
9049 case DW_FORM_block2:
9050 case DW_FORM_block4:
9051 case DW_FORM_block:
9052 case DW_FORM_block1:
48cd0caa 9053 fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 9054 break;
10b3939b
DJ
9055 case DW_FORM_ref1:
9056 case DW_FORM_ref2:
9057 case DW_FORM_ref4:
9058 fprintf_unfiltered (gdb_stderr, "constant ref: %ld (adjusted)",
9059 (long) (DW_ADDR (&die->attrs[i])));
9060 break;
c906108c
SS
9061 case DW_FORM_data1:
9062 case DW_FORM_data2:
9063 case DW_FORM_data4:
ce5d95e1 9064 case DW_FORM_data8:
c906108c
SS
9065 case DW_FORM_udata:
9066 case DW_FORM_sdata:
48cd0caa 9067 fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
c906108c
SS
9068 break;
9069 case DW_FORM_string:
4bdf3d34 9070 case DW_FORM_strp:
48cd0caa 9071 fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
c906108c 9072 DW_STRING (&die->attrs[i])
c5aa993b 9073 ? DW_STRING (&die->attrs[i]) : "");
c906108c
SS
9074 break;
9075 case DW_FORM_flag:
9076 if (DW_UNSND (&die->attrs[i]))
48cd0caa 9077 fprintf_unfiltered (gdb_stderr, "flag: TRUE");
c906108c 9078 else
48cd0caa 9079 fprintf_unfiltered (gdb_stderr, "flag: FALSE");
c906108c 9080 break;
a8329558
KW
9081 case DW_FORM_indirect:
9082 /* the reader will have reduced the indirect form to
9083 the "base form" so this form should not occur */
48cd0caa 9084 fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
a8329558 9085 break;
c906108c 9086 default:
48cd0caa 9087 fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
c5aa993b 9088 die->attrs[i].form);
c906108c 9089 }
48cd0caa 9090 fprintf_unfiltered (gdb_stderr, "\n");
c906108c
SS
9091 }
9092}
9093
f9aca02d 9094static void
fba45db2 9095dump_die_list (struct die_info *die)
c906108c
SS
9096{
9097 while (die)
9098 {
9099 dump_die (die);
639d11d3
DC
9100 if (die->child != NULL)
9101 dump_die_list (die->child);
9102 if (die->sibling != NULL)
9103 dump_die_list (die->sibling);
c906108c
SS
9104 }
9105}
9106
f9aca02d 9107static void
51545339 9108store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9109{
51545339 9110 void **slot;
c906108c 9111
51545339
DJ
9112 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
9113
9114 *slot = die;
c906108c
SS
9115}
9116
9117static unsigned int
e142c38c 9118dwarf2_get_ref_die_offset (struct attribute *attr, struct dwarf2_cu *cu)
c906108c
SS
9119{
9120 unsigned int result = 0;
9121
9122 switch (attr->form)
9123 {
9124 case DW_FORM_ref_addr:
c906108c
SS
9125 case DW_FORM_ref1:
9126 case DW_FORM_ref2:
9127 case DW_FORM_ref4:
613e1657 9128 case DW_FORM_ref8:
c906108c 9129 case DW_FORM_ref_udata:
10b3939b 9130 result = DW_ADDR (attr);
c906108c
SS
9131 break;
9132 default:
4d3c2250 9133 complaint (&symfile_complaints,
e2e0b3e5 9134 _("unsupported die ref attribute form: '%s'"),
4d3c2250 9135 dwarf_form_name (attr->form));
c906108c
SS
9136 }
9137 return result;
9138}
9139
a02abb62
JB
9140/* Return the constant value held by the given attribute. Return -1
9141 if the value held by the attribute is not constant. */
9142
9143static int
9144dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
9145{
9146 if (attr->form == DW_FORM_sdata)
9147 return DW_SND (attr);
9148 else if (attr->form == DW_FORM_udata
9149 || attr->form == DW_FORM_data1
9150 || attr->form == DW_FORM_data2
9151 || attr->form == DW_FORM_data4
9152 || attr->form == DW_FORM_data8)
9153 return DW_UNSND (attr);
9154 else
9155 {
e2e0b3e5 9156 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
9157 dwarf_form_name (attr->form));
9158 return default_value;
9159 }
9160}
9161
03dd20cc
DJ
9162/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
9163 unit and add it to our queue. */
9164
9165static void
9166maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
9167 struct dwarf2_per_cu_data *per_cu)
9168{
9169 /* Mark the dependence relation so that we don't flush PER_CU
9170 too early. */
9171 dwarf2_add_dependence (this_cu, per_cu);
9172
9173 /* If it's already on the queue, we have nothing to do. */
9174 if (per_cu->queued)
9175 return;
9176
9177 /* If the compilation unit is already loaded, just mark it as
9178 used. */
9179 if (per_cu->cu != NULL)
9180 {
9181 per_cu->cu->last_used = 0;
9182 return;
9183 }
9184
9185 /* Add it to the queue. */
9186 queue_comp_unit (per_cu, this_cu->objfile);
9187}
9188
f9aca02d 9189static struct die_info *
10b3939b 9190follow_die_ref (struct die_info *src_die, struct attribute *attr,
f2f0e013 9191 struct dwarf2_cu **ref_cu)
c906108c
SS
9192{
9193 struct die_info *die;
10b3939b 9194 unsigned int offset;
10b3939b 9195 struct die_info temp_die;
f2f0e013 9196 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b
DJ
9197
9198 offset = dwarf2_get_ref_die_offset (attr, cu);
9199
9200 if (DW_ADDR (attr) < cu->header.offset
9201 || DW_ADDR (attr) >= cu->header.offset + cu->header.length)
9202 {
9203 struct dwarf2_per_cu_data *per_cu;
9204 per_cu = dwarf2_find_containing_comp_unit (DW_ADDR (attr),
9205 cu->objfile);
03dd20cc
DJ
9206
9207 /* If necessary, add it to the queue and load its DIEs. */
9208 maybe_queue_comp_unit (cu, per_cu);
9209
10b3939b
DJ
9210 target_cu = per_cu->cu;
9211 }
9212 else
9213 target_cu = cu;
c906108c 9214
f2f0e013 9215 *ref_cu = target_cu;
51545339
DJ
9216 temp_die.offset = offset;
9217 die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
9218 if (die)
9219 return die;
10b3939b 9220
8a3fe4f8
AC
9221 error (_("Dwarf Error: Cannot find DIE at 0x%lx referenced from DIE "
9222 "at 0x%lx [in module %s]"),
1d325ec1 9223 (long) offset, (long) src_die->offset, cu->objfile->name);
c906108c
SS
9224}
9225
c906108c
SS
9226/* Decode simple location descriptions.
9227 Given a pointer to a dwarf block that defines a location, compute
9228 the location and return the value.
9229
4cecd739
DJ
9230 NOTE drow/2003-11-18: This function is called in two situations
9231 now: for the address of static or global variables (partial symbols
9232 only) and for offsets into structures which are expected to be
9233 (more or less) constant. The partial symbol case should go away,
9234 and only the constant case should remain. That will let this
9235 function complain more accurately. A few special modes are allowed
9236 without complaint for global variables (for instance, global
9237 register values and thread-local values).
c906108c
SS
9238
9239 A location description containing no operations indicates that the
4cecd739 9240 object is optimized out. The return value is 0 for that case.
6b992462
DJ
9241 FIXME drow/2003-11-16: No callers check for this case any more; soon all
9242 callers will only want a very basic result and this can become a
9243 complaint.
c906108c 9244
c906108c
SS
9245 Note that stack[0] is unused except as a default error return.
9246 Note that stack overflow is not yet handled. */
9247
9248static CORE_ADDR
e7c27a73 9249decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 9250{
e7c27a73
DJ
9251 struct objfile *objfile = cu->objfile;
9252 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9253 int i;
9254 int size = blk->size;
fe1b8b76 9255 gdb_byte *data = blk->data;
c906108c
SS
9256 CORE_ADDR stack[64];
9257 int stacki;
9258 unsigned int bytes_read, unsnd;
fe1b8b76 9259 gdb_byte op;
c906108c
SS
9260
9261 i = 0;
9262 stacki = 0;
9263 stack[stacki] = 0;
c906108c
SS
9264
9265 while (i < size)
9266 {
c906108c
SS
9267 op = data[i++];
9268 switch (op)
9269 {
f1bea926
JM
9270 case DW_OP_lit0:
9271 case DW_OP_lit1:
9272 case DW_OP_lit2:
9273 case DW_OP_lit3:
9274 case DW_OP_lit4:
9275 case DW_OP_lit5:
9276 case DW_OP_lit6:
9277 case DW_OP_lit7:
9278 case DW_OP_lit8:
9279 case DW_OP_lit9:
9280 case DW_OP_lit10:
9281 case DW_OP_lit11:
9282 case DW_OP_lit12:
9283 case DW_OP_lit13:
9284 case DW_OP_lit14:
9285 case DW_OP_lit15:
9286 case DW_OP_lit16:
9287 case DW_OP_lit17:
9288 case DW_OP_lit18:
9289 case DW_OP_lit19:
9290 case DW_OP_lit20:
9291 case DW_OP_lit21:
9292 case DW_OP_lit22:
9293 case DW_OP_lit23:
9294 case DW_OP_lit24:
9295 case DW_OP_lit25:
9296 case DW_OP_lit26:
9297 case DW_OP_lit27:
9298 case DW_OP_lit28:
9299 case DW_OP_lit29:
9300 case DW_OP_lit30:
9301 case DW_OP_lit31:
9302 stack[++stacki] = op - DW_OP_lit0;
9303 break;
9304
c906108c
SS
9305 case DW_OP_reg0:
9306 case DW_OP_reg1:
9307 case DW_OP_reg2:
9308 case DW_OP_reg3:
9309 case DW_OP_reg4:
9310 case DW_OP_reg5:
9311 case DW_OP_reg6:
9312 case DW_OP_reg7:
9313 case DW_OP_reg8:
9314 case DW_OP_reg9:
9315 case DW_OP_reg10:
9316 case DW_OP_reg11:
9317 case DW_OP_reg12:
9318 case DW_OP_reg13:
9319 case DW_OP_reg14:
9320 case DW_OP_reg15:
9321 case DW_OP_reg16:
9322 case DW_OP_reg17:
9323 case DW_OP_reg18:
9324 case DW_OP_reg19:
9325 case DW_OP_reg20:
9326 case DW_OP_reg21:
9327 case DW_OP_reg22:
9328 case DW_OP_reg23:
9329 case DW_OP_reg24:
9330 case DW_OP_reg25:
9331 case DW_OP_reg26:
9332 case DW_OP_reg27:
9333 case DW_OP_reg28:
9334 case DW_OP_reg29:
9335 case DW_OP_reg30:
9336 case DW_OP_reg31:
c906108c 9337 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
9338 if (i < size)
9339 dwarf2_complex_location_expr_complaint ();
c906108c
SS
9340 break;
9341
9342 case DW_OP_regx:
c906108c
SS
9343 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9344 i += bytes_read;
c906108c 9345 stack[++stacki] = unsnd;
4cecd739
DJ
9346 if (i < size)
9347 dwarf2_complex_location_expr_complaint ();
c906108c
SS
9348 break;
9349
9350 case DW_OP_addr:
107d2387 9351 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 9352 cu, &bytes_read);
107d2387 9353 i += bytes_read;
c906108c
SS
9354 break;
9355
9356 case DW_OP_const1u:
9357 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
9358 i += 1;
9359 break;
9360
9361 case DW_OP_const1s:
9362 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
9363 i += 1;
9364 break;
9365
9366 case DW_OP_const2u:
9367 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
9368 i += 2;
9369 break;
9370
9371 case DW_OP_const2s:
9372 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
9373 i += 2;
9374 break;
9375
9376 case DW_OP_const4u:
9377 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
9378 i += 4;
9379 break;
9380
9381 case DW_OP_const4s:
9382 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
9383 i += 4;
9384 break;
9385
9386 case DW_OP_constu:
9387 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 9388 &bytes_read);
c906108c
SS
9389 i += bytes_read;
9390 break;
9391
9392 case DW_OP_consts:
9393 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
9394 i += bytes_read;
9395 break;
9396
f1bea926
JM
9397 case DW_OP_dup:
9398 stack[stacki + 1] = stack[stacki];
9399 stacki++;
9400 break;
9401
c906108c
SS
9402 case DW_OP_plus:
9403 stack[stacki - 1] += stack[stacki];
9404 stacki--;
9405 break;
9406
9407 case DW_OP_plus_uconst:
9408 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9409 i += bytes_read;
9410 break;
9411
9412 case DW_OP_minus:
f1bea926 9413 stack[stacki - 1] -= stack[stacki];
c906108c
SS
9414 stacki--;
9415 break;
9416
7a292a7a 9417 case DW_OP_deref:
7a292a7a 9418 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
9419 this using GDB's address_class enum. This is valid for partial
9420 global symbols, although the variable's address will be bogus
9421 in the psymtab. */
7a292a7a 9422 if (i < size)
4d3c2250 9423 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
9424 break;
9425
9d774e44 9426 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
9427 /* The top of the stack has the offset from the beginning
9428 of the thread control block at which the variable is located. */
9429 /* Nothing should follow this operator, so the top of stack would
9430 be returned. */
4cecd739
DJ
9431 /* This is valid for partial global symbols, but the variable's
9432 address will be bogus in the psymtab. */
9d774e44 9433 if (i < size)
4d3c2250 9434 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
9435 break;
9436
42be36b3
CT
9437 case DW_OP_GNU_uninit:
9438 break;
9439
c906108c 9440 default:
e2e0b3e5 9441 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
4d3c2250 9442 dwarf_stack_op_name (op));
c906108c
SS
9443 return (stack[stacki]);
9444 }
9445 }
9446 return (stack[stacki]);
9447}
9448
9449/* memory allocation interface */
9450
c906108c 9451static struct dwarf_block *
7b5a2f43 9452dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
9453{
9454 struct dwarf_block *blk;
9455
9456 blk = (struct dwarf_block *)
7b5a2f43 9457 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
9458 return (blk);
9459}
9460
9461static struct abbrev_info *
f3dd6933 9462dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
9463{
9464 struct abbrev_info *abbrev;
9465
f3dd6933
DJ
9466 abbrev = (struct abbrev_info *)
9467 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
9468 memset (abbrev, 0, sizeof (struct abbrev_info));
9469 return (abbrev);
9470}
9471
9472static struct die_info *
b60c80d6 9473dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
9474{
9475 struct die_info *die;
b60c80d6
DJ
9476 size_t size = sizeof (struct die_info);
9477
9478 if (num_attrs > 1)
9479 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 9480
b60c80d6 9481 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
9482 memset (die, 0, sizeof (struct die_info));
9483 return (die);
9484}
2e276125
JB
9485
9486\f
9487/* Macro support. */
9488
9489
9490/* Return the full name of file number I in *LH's file name table.
9491 Use COMP_DIR as the name of the current directory of the
9492 compilation. The result is allocated using xmalloc; the caller is
9493 responsible for freeing it. */
9494static char *
9495file_full_name (int file, struct line_header *lh, const char *comp_dir)
9496{
6a83a1e6
EZ
9497 /* Is the file number a valid index into the line header's file name
9498 table? Remember that file numbers start with one, not zero. */
9499 if (1 <= file && file <= lh->num_file_names)
9500 {
9501 struct file_entry *fe = &lh->file_names[file - 1];
2e276125 9502
6a83a1e6
EZ
9503 if (IS_ABSOLUTE_PATH (fe->name))
9504 return xstrdup (fe->name);
9505 else
9506 {
9507 const char *dir;
9508 int dir_len;
9509 char *full_name;
9510
9511 if (fe->dir_index)
9512 dir = lh->include_dirs[fe->dir_index - 1];
9513 else
9514 dir = comp_dir;
9515
9516 if (dir)
9517 {
9518 dir_len = strlen (dir);
9519 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
9520 strcpy (full_name, dir);
9521 full_name[dir_len] = '/';
9522 strcpy (full_name + dir_len + 1, fe->name);
9523 return full_name;
9524 }
9525 else
9526 return xstrdup (fe->name);
9527 }
9528 }
2e276125
JB
9529 else
9530 {
6a83a1e6
EZ
9531 /* The compiler produced a bogus file number. We can at least
9532 record the macro definitions made in the file, even if we
9533 won't be able to find the file by name. */
9534 char fake_name[80];
9535 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 9536
6a83a1e6
EZ
9537 complaint (&symfile_complaints,
9538 _("bad file number in macro information (%d)"),
9539 file);
2e276125 9540
6a83a1e6 9541 return xstrdup (fake_name);
2e276125
JB
9542 }
9543}
9544
9545
9546static struct macro_source_file *
9547macro_start_file (int file, int line,
9548 struct macro_source_file *current_file,
9549 const char *comp_dir,
9550 struct line_header *lh, struct objfile *objfile)
9551{
9552 /* The full name of this source file. */
9553 char *full_name = file_full_name (file, lh, comp_dir);
9554
9555 /* We don't create a macro table for this compilation unit
9556 at all until we actually get a filename. */
9557 if (! pending_macros)
4a146b47 9558 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 9559 objfile->macro_cache);
2e276125
JB
9560
9561 if (! current_file)
9562 /* If we have no current file, then this must be the start_file
9563 directive for the compilation unit's main source file. */
9564 current_file = macro_set_main (pending_macros, full_name);
9565 else
9566 current_file = macro_include (current_file, line, full_name);
9567
9568 xfree (full_name);
9569
9570 return current_file;
9571}
9572
9573
9574/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
9575 followed by a null byte. */
9576static char *
9577copy_string (const char *buf, int len)
9578{
9579 char *s = xmalloc (len + 1);
9580 memcpy (s, buf, len);
9581 s[len] = '\0';
9582
9583 return s;
9584}
9585
9586
9587static const char *
9588consume_improper_spaces (const char *p, const char *body)
9589{
9590 if (*p == ' ')
9591 {
4d3c2250 9592 complaint (&symfile_complaints,
e2e0b3e5 9593 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 9594 body);
2e276125
JB
9595
9596 while (*p == ' ')
9597 p++;
9598 }
9599
9600 return p;
9601}
9602
9603
9604static void
9605parse_macro_definition (struct macro_source_file *file, int line,
9606 const char *body)
9607{
9608 const char *p;
9609
9610 /* The body string takes one of two forms. For object-like macro
9611 definitions, it should be:
9612
9613 <macro name> " " <definition>
9614
9615 For function-like macro definitions, it should be:
9616
9617 <macro name> "() " <definition>
9618 or
9619 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
9620
9621 Spaces may appear only where explicitly indicated, and in the
9622 <definition>.
9623
9624 The Dwarf 2 spec says that an object-like macro's name is always
9625 followed by a space, but versions of GCC around March 2002 omit
9626 the space when the macro's definition is the empty string.
9627
9628 The Dwarf 2 spec says that there should be no spaces between the
9629 formal arguments in a function-like macro's formal argument list,
9630 but versions of GCC around March 2002 include spaces after the
9631 commas. */
9632
9633
9634 /* Find the extent of the macro name. The macro name is terminated
9635 by either a space or null character (for an object-like macro) or
9636 an opening paren (for a function-like macro). */
9637 for (p = body; *p; p++)
9638 if (*p == ' ' || *p == '(')
9639 break;
9640
9641 if (*p == ' ' || *p == '\0')
9642 {
9643 /* It's an object-like macro. */
9644 int name_len = p - body;
9645 char *name = copy_string (body, name_len);
9646 const char *replacement;
9647
9648 if (*p == ' ')
9649 replacement = body + name_len + 1;
9650 else
9651 {
4d3c2250 9652 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9653 replacement = body + name_len;
9654 }
9655
9656 macro_define_object (file, line, name, replacement);
9657
9658 xfree (name);
9659 }
9660 else if (*p == '(')
9661 {
9662 /* It's a function-like macro. */
9663 char *name = copy_string (body, p - body);
9664 int argc = 0;
9665 int argv_size = 1;
9666 char **argv = xmalloc (argv_size * sizeof (*argv));
9667
9668 p++;
9669
9670 p = consume_improper_spaces (p, body);
9671
9672 /* Parse the formal argument list. */
9673 while (*p && *p != ')')
9674 {
9675 /* Find the extent of the current argument name. */
9676 const char *arg_start = p;
9677
9678 while (*p && *p != ',' && *p != ')' && *p != ' ')
9679 p++;
9680
9681 if (! *p || p == arg_start)
4d3c2250 9682 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9683 else
9684 {
9685 /* Make sure argv has room for the new argument. */
9686 if (argc >= argv_size)
9687 {
9688 argv_size *= 2;
9689 argv = xrealloc (argv, argv_size * sizeof (*argv));
9690 }
9691
9692 argv[argc++] = copy_string (arg_start, p - arg_start);
9693 }
9694
9695 p = consume_improper_spaces (p, body);
9696
9697 /* Consume the comma, if present. */
9698 if (*p == ',')
9699 {
9700 p++;
9701
9702 p = consume_improper_spaces (p, body);
9703 }
9704 }
9705
9706 if (*p == ')')
9707 {
9708 p++;
9709
9710 if (*p == ' ')
9711 /* Perfectly formed definition, no complaints. */
9712 macro_define_function (file, line, name,
9713 argc, (const char **) argv,
9714 p + 1);
9715 else if (*p == '\0')
9716 {
9717 /* Complain, but do define it. */
4d3c2250 9718 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9719 macro_define_function (file, line, name,
9720 argc, (const char **) argv,
9721 p);
9722 }
9723 else
9724 /* Just complain. */
4d3c2250 9725 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9726 }
9727 else
9728 /* Just complain. */
4d3c2250 9729 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9730
9731 xfree (name);
9732 {
9733 int i;
9734
9735 for (i = 0; i < argc; i++)
9736 xfree (argv[i]);
9737 }
9738 xfree (argv);
9739 }
9740 else
4d3c2250 9741 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9742}
9743
9744
9745static void
9746dwarf_decode_macros (struct line_header *lh, unsigned int offset,
9747 char *comp_dir, bfd *abfd,
e7c27a73 9748 struct dwarf2_cu *cu)
2e276125 9749{
fe1b8b76 9750 gdb_byte *mac_ptr, *mac_end;
2e276125
JB
9751 struct macro_source_file *current_file = 0;
9752
6502dd73 9753 if (dwarf2_per_objfile->macinfo_buffer == NULL)
2e276125 9754 {
e2e0b3e5 9755 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
9756 return;
9757 }
9758
6502dd73
DJ
9759 mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
9760 mac_end = dwarf2_per_objfile->macinfo_buffer
9761 + dwarf2_per_objfile->macinfo_size;
2e276125
JB
9762
9763 for (;;)
9764 {
9765 enum dwarf_macinfo_record_type macinfo_type;
9766
9767 /* Do we at least have room for a macinfo type byte? */
9768 if (mac_ptr >= mac_end)
9769 {
4d3c2250 9770 dwarf2_macros_too_long_complaint ();
2e276125
JB
9771 return;
9772 }
9773
9774 macinfo_type = read_1_byte (abfd, mac_ptr);
9775 mac_ptr++;
9776
9777 switch (macinfo_type)
9778 {
9779 /* A zero macinfo type indicates the end of the macro
9780 information. */
9781 case 0:
9782 return;
9783
9784 case DW_MACINFO_define:
9785 case DW_MACINFO_undef:
9786 {
891d2f0b 9787 unsigned int bytes_read;
2e276125
JB
9788 int line;
9789 char *body;
9790
9791 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9792 mac_ptr += bytes_read;
9793 body = read_string (abfd, mac_ptr, &bytes_read);
9794 mac_ptr += bytes_read;
9795
9796 if (! current_file)
4d3c2250 9797 complaint (&symfile_complaints,
e2e0b3e5 9798 _("debug info gives macro %s outside of any file: %s"),
4d3c2250
KB
9799 macinfo_type ==
9800 DW_MACINFO_define ? "definition" : macinfo_type ==
9801 DW_MACINFO_undef ? "undefinition" :
9802 "something-or-other", body);
2e276125
JB
9803 else
9804 {
9805 if (macinfo_type == DW_MACINFO_define)
9806 parse_macro_definition (current_file, line, body);
9807 else if (macinfo_type == DW_MACINFO_undef)
9808 macro_undef (current_file, line, body);
9809 }
9810 }
9811 break;
9812
9813 case DW_MACINFO_start_file:
9814 {
891d2f0b 9815 unsigned int bytes_read;
2e276125
JB
9816 int line, file;
9817
9818 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9819 mac_ptr += bytes_read;
9820 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9821 mac_ptr += bytes_read;
9822
9823 current_file = macro_start_file (file, line,
9824 current_file, comp_dir,
e7c27a73 9825 lh, cu->objfile);
2e276125
JB
9826 }
9827 break;
9828
9829 case DW_MACINFO_end_file:
9830 if (! current_file)
4d3c2250 9831 complaint (&symfile_complaints,
e2e0b3e5 9832 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
9833 else
9834 {
9835 current_file = current_file->included_by;
9836 if (! current_file)
9837 {
9838 enum dwarf_macinfo_record_type next_type;
9839
9840 /* GCC circa March 2002 doesn't produce the zero
9841 type byte marking the end of the compilation
9842 unit. Complain if it's not there, but exit no
9843 matter what. */
9844
9845 /* Do we at least have room for a macinfo type byte? */
9846 if (mac_ptr >= mac_end)
9847 {
4d3c2250 9848 dwarf2_macros_too_long_complaint ();
2e276125
JB
9849 return;
9850 }
9851
9852 /* We don't increment mac_ptr here, so this is just
9853 a look-ahead. */
9854 next_type = read_1_byte (abfd, mac_ptr);
9855 if (next_type != 0)
4d3c2250 9856 complaint (&symfile_complaints,
e2e0b3e5 9857 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
9858
9859 return;
9860 }
9861 }
9862 break;
9863
9864 case DW_MACINFO_vendor_ext:
9865 {
891d2f0b 9866 unsigned int bytes_read;
2e276125
JB
9867 int constant;
9868 char *string;
9869
9870 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9871 mac_ptr += bytes_read;
9872 string = read_string (abfd, mac_ptr, &bytes_read);
9873 mac_ptr += bytes_read;
9874
9875 /* We don't recognize any vendor extensions. */
9876 }
9877 break;
9878 }
9879 }
9880}
8e19ed76
PS
9881
9882/* Check if the attribute's form is a DW_FORM_block*
9883 if so return true else false. */
9884static int
9885attr_form_is_block (struct attribute *attr)
9886{
9887 return (attr == NULL ? 0 :
9888 attr->form == DW_FORM_block1
9889 || attr->form == DW_FORM_block2
9890 || attr->form == DW_FORM_block4
9891 || attr->form == DW_FORM_block);
9892}
4c2df51b 9893
c6a0999f
JB
9894/* Return non-zero if ATTR's value is a section offset --- classes
9895 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
9896 You may use DW_UNSND (attr) to retrieve such offsets.
9897
9898 Section 7.5.4, "Attribute Encodings", explains that no attribute
9899 may have a value that belongs to more than one of these classes; it
9900 would be ambiguous if we did, because we use the same forms for all
9901 of them. */
3690dd37
JB
9902static int
9903attr_form_is_section_offset (struct attribute *attr)
9904{
9905 return (attr->form == DW_FORM_data4
9906 || attr->form == DW_FORM_data8);
9907}
9908
9909
9910/* Return non-zero if ATTR's value falls in the 'constant' class, or
9911 zero otherwise. When this function returns true, you can apply
9912 dwarf2_get_attr_constant_value to it.
9913
9914 However, note that for some attributes you must check
9915 attr_form_is_section_offset before using this test. DW_FORM_data4
9916 and DW_FORM_data8 are members of both the constant class, and of
9917 the classes that contain offsets into other debug sections
9918 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
9919 that, if an attribute's can be either a constant or one of the
9920 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
9921 taken as section offsets, not constants. */
9922static int
9923attr_form_is_constant (struct attribute *attr)
9924{
9925 switch (attr->form)
9926 {
9927 case DW_FORM_sdata:
9928 case DW_FORM_udata:
9929 case DW_FORM_data1:
9930 case DW_FORM_data2:
9931 case DW_FORM_data4:
9932 case DW_FORM_data8:
9933 return 1;
9934 default:
9935 return 0;
9936 }
9937}
9938
4c2df51b
DJ
9939static void
9940dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 9941 struct dwarf2_cu *cu)
4c2df51b 9942{
3690dd37 9943 if (attr_form_is_section_offset (attr)
99bcc461
DJ
9944 /* ".debug_loc" may not exist at all, or the offset may be outside
9945 the section. If so, fall through to the complaint in the
9946 other branch. */
9947 && DW_UNSND (attr) < dwarf2_per_objfile->loc_size)
4c2df51b 9948 {
0d53c4c4 9949 struct dwarf2_loclist_baton *baton;
4c2df51b 9950
4a146b47 9951 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 9952 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
9953 baton->per_cu = cu->per_cu;
9954 gdb_assert (baton->per_cu);
4c2df51b 9955
0d53c4c4
DJ
9956 /* We don't know how long the location list is, but make sure we
9957 don't run off the edge of the section. */
6502dd73
DJ
9958 baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
9959 baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
d00adf39
DE
9960 baton->base_address = cu->base_address;
9961 if (cu->base_known == 0)
0d53c4c4 9962 complaint (&symfile_complaints,
e2e0b3e5 9963 _("Location list used without specifying the CU base address."));
4c2df51b 9964
a67af2b9 9965 SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
9966 SYMBOL_LOCATION_BATON (sym) = baton;
9967 }
9968 else
9969 {
9970 struct dwarf2_locexpr_baton *baton;
9971
4a146b47 9972 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 9973 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
9974 baton->per_cu = cu->per_cu;
9975 gdb_assert (baton->per_cu);
0d53c4c4
DJ
9976
9977 if (attr_form_is_block (attr))
9978 {
9979 /* Note that we're just copying the block's data pointer
9980 here, not the actual data. We're still pointing into the
6502dd73
DJ
9981 info_buffer for SYM's objfile; right now we never release
9982 that buffer, but when we do clean up properly this may
9983 need to change. */
0d53c4c4
DJ
9984 baton->size = DW_BLOCK (attr)->size;
9985 baton->data = DW_BLOCK (attr)->data;
9986 }
9987 else
9988 {
9989 dwarf2_invalid_attrib_class_complaint ("location description",
9990 SYMBOL_NATURAL_NAME (sym));
9991 baton->size = 0;
9992 baton->data = NULL;
9993 }
9994
a67af2b9 9995 SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
9996 SYMBOL_LOCATION_BATON (sym) = baton;
9997 }
4c2df51b 9998}
6502dd73 9999
ae0d2f24
UW
10000/* Return the OBJFILE associated with the compilation unit CU. */
10001
10002struct objfile *
10003dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
10004{
10005 struct objfile *objfile = per_cu->psymtab->objfile;
10006
10007 /* Return the master objfile, so that we can report and look up the
10008 correct file containing this variable. */
10009 if (objfile->separate_debug_objfile_backlink)
10010 objfile = objfile->separate_debug_objfile_backlink;
10011
10012 return objfile;
10013}
10014
10015/* Return the address size given in the compilation unit header for CU. */
10016
10017CORE_ADDR
10018dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
10019{
10020 if (per_cu->cu)
10021 return per_cu->cu->header.addr_size;
10022 else
10023 {
10024 /* If the CU is not currently read in, we re-read its header. */
10025 struct objfile *objfile = per_cu->psymtab->objfile;
10026 struct dwarf2_per_objfile *per_objfile
10027 = objfile_data (objfile, dwarf2_objfile_data_key);
10028 gdb_byte *info_ptr = per_objfile->info_buffer + per_cu->offset;
10029
10030 struct comp_unit_head cu_header;
10031 memset (&cu_header, 0, sizeof cu_header);
10032 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
10033 return cu_header.addr_size;
10034 }
10035}
10036
ae038cb0 10037/* Locate the compilation unit from CU's objfile which contains the
10b3939b 10038 DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
10039
10040static struct dwarf2_per_cu_data *
10041dwarf2_find_containing_comp_unit (unsigned long offset,
10042 struct objfile *objfile)
10043{
10044 struct dwarf2_per_cu_data *this_cu;
10045 int low, high;
10046
ae038cb0
DJ
10047 low = 0;
10048 high = dwarf2_per_objfile->n_comp_units - 1;
10049 while (high > low)
10050 {
10051 int mid = low + (high - low) / 2;
10052 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
10053 high = mid;
10054 else
10055 low = mid + 1;
10056 }
10057 gdb_assert (low == high);
10058 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
10059 {
10b3939b 10060 if (low == 0)
8a3fe4f8
AC
10061 error (_("Dwarf Error: could not find partial DIE containing "
10062 "offset 0x%lx [in module %s]"),
10b3939b
DJ
10063 (long) offset, bfd_get_filename (objfile->obfd));
10064
ae038cb0
DJ
10065 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
10066 return dwarf2_per_objfile->all_comp_units[low-1];
10067 }
10068 else
10069 {
10070 this_cu = dwarf2_per_objfile->all_comp_units[low];
10071 if (low == dwarf2_per_objfile->n_comp_units - 1
10072 && offset >= this_cu->offset + this_cu->length)
8a3fe4f8 10073 error (_("invalid dwarf2 offset %ld"), offset);
ae038cb0
DJ
10074 gdb_assert (offset < this_cu->offset + this_cu->length);
10075 return this_cu;
10076 }
10077}
10078
10b3939b
DJ
10079/* Locate the compilation unit from OBJFILE which is located at exactly
10080 OFFSET. Raises an error on failure. */
10081
ae038cb0
DJ
10082static struct dwarf2_per_cu_data *
10083dwarf2_find_comp_unit (unsigned long offset, struct objfile *objfile)
10084{
10085 struct dwarf2_per_cu_data *this_cu;
10086 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10087 if (this_cu->offset != offset)
8a3fe4f8 10088 error (_("no compilation unit with offset %ld."), offset);
ae038cb0
DJ
10089 return this_cu;
10090}
10091
10092/* Release one cached compilation unit, CU. We unlink it from the tree
10093 of compilation units, but we don't remove it from the read_in_chain;
10094 the caller is responsible for that. */
10095
10096static void
10097free_one_comp_unit (void *data)
10098{
10099 struct dwarf2_cu *cu = data;
10100
10101 if (cu->per_cu != NULL)
10102 cu->per_cu->cu = NULL;
10103 cu->per_cu = NULL;
10104
10105 obstack_free (&cu->comp_unit_obstack, NULL);
10106
10107 xfree (cu);
10108}
10109
72bf9492 10110/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
10111 when we're finished with it. We can't free the pointer itself, but be
10112 sure to unlink it from the cache. Also release any associated storage
10113 and perform cache maintenance.
72bf9492
DJ
10114
10115 Only used during partial symbol parsing. */
10116
10117static void
10118free_stack_comp_unit (void *data)
10119{
10120 struct dwarf2_cu *cu = data;
10121
10122 obstack_free (&cu->comp_unit_obstack, NULL);
10123 cu->partial_dies = NULL;
ae038cb0
DJ
10124
10125 if (cu->per_cu != NULL)
10126 {
10127 /* This compilation unit is on the stack in our caller, so we
10128 should not xfree it. Just unlink it. */
10129 cu->per_cu->cu = NULL;
10130 cu->per_cu = NULL;
10131
10132 /* If we had a per-cu pointer, then we may have other compilation
10133 units loaded, so age them now. */
10134 age_cached_comp_units ();
10135 }
10136}
10137
10138/* Free all cached compilation units. */
10139
10140static void
10141free_cached_comp_units (void *data)
10142{
10143 struct dwarf2_per_cu_data *per_cu, **last_chain;
10144
10145 per_cu = dwarf2_per_objfile->read_in_chain;
10146 last_chain = &dwarf2_per_objfile->read_in_chain;
10147 while (per_cu != NULL)
10148 {
10149 struct dwarf2_per_cu_data *next_cu;
10150
10151 next_cu = per_cu->cu->read_in_chain;
10152
10153 free_one_comp_unit (per_cu->cu);
10154 *last_chain = next_cu;
10155
10156 per_cu = next_cu;
10157 }
10158}
10159
10160/* Increase the age counter on each cached compilation unit, and free
10161 any that are too old. */
10162
10163static void
10164age_cached_comp_units (void)
10165{
10166 struct dwarf2_per_cu_data *per_cu, **last_chain;
10167
10168 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
10169 per_cu = dwarf2_per_objfile->read_in_chain;
10170 while (per_cu != NULL)
10171 {
10172 per_cu->cu->last_used ++;
10173 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
10174 dwarf2_mark (per_cu->cu);
10175 per_cu = per_cu->cu->read_in_chain;
10176 }
10177
10178 per_cu = dwarf2_per_objfile->read_in_chain;
10179 last_chain = &dwarf2_per_objfile->read_in_chain;
10180 while (per_cu != NULL)
10181 {
10182 struct dwarf2_per_cu_data *next_cu;
10183
10184 next_cu = per_cu->cu->read_in_chain;
10185
10186 if (!per_cu->cu->mark)
10187 {
10188 free_one_comp_unit (per_cu->cu);
10189 *last_chain = next_cu;
10190 }
10191 else
10192 last_chain = &per_cu->cu->read_in_chain;
10193
10194 per_cu = next_cu;
10195 }
10196}
10197
10198/* Remove a single compilation unit from the cache. */
10199
10200static void
10201free_one_cached_comp_unit (void *target_cu)
10202{
10203 struct dwarf2_per_cu_data *per_cu, **last_chain;
10204
10205 per_cu = dwarf2_per_objfile->read_in_chain;
10206 last_chain = &dwarf2_per_objfile->read_in_chain;
10207 while (per_cu != NULL)
10208 {
10209 struct dwarf2_per_cu_data *next_cu;
10210
10211 next_cu = per_cu->cu->read_in_chain;
10212
10213 if (per_cu->cu == target_cu)
10214 {
10215 free_one_comp_unit (per_cu->cu);
10216 *last_chain = next_cu;
10217 break;
10218 }
10219 else
10220 last_chain = &per_cu->cu->read_in_chain;
10221
10222 per_cu = next_cu;
10223 }
10224}
10225
fe3e1990
DJ
10226/* Release all extra memory associated with OBJFILE. */
10227
10228void
10229dwarf2_free_objfile (struct objfile *objfile)
10230{
10231 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10232
10233 if (dwarf2_per_objfile == NULL)
10234 return;
10235
10236 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
10237 free_cached_comp_units (NULL);
10238
10239 /* Everything else should be on the objfile obstack. */
10240}
10241
1c379e20
DJ
10242/* A pair of DIE offset and GDB type pointer. We store these
10243 in a hash table separate from the DIEs, and preserve them
10244 when the DIEs are flushed out of cache. */
10245
10246struct dwarf2_offset_and_type
10247{
10248 unsigned int offset;
10249 struct type *type;
10250};
10251
10252/* Hash function for a dwarf2_offset_and_type. */
10253
10254static hashval_t
10255offset_and_type_hash (const void *item)
10256{
10257 const struct dwarf2_offset_and_type *ofs = item;
10258 return ofs->offset;
10259}
10260
10261/* Equality function for a dwarf2_offset_and_type. */
10262
10263static int
10264offset_and_type_eq (const void *item_lhs, const void *item_rhs)
10265{
10266 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
10267 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
10268 return ofs_lhs->offset == ofs_rhs->offset;
10269}
10270
10271/* Set the type associated with DIE to TYPE. Save it in CU's hash
f792889a 10272 table if necessary. For convenience, return TYPE. */
1c379e20 10273
f792889a 10274static struct type *
1c379e20
DJ
10275set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10276{
10277 struct dwarf2_offset_and_type **slot, ofs;
10278
f792889a
DJ
10279 if (cu->type_hash == NULL)
10280 {
10281 gdb_assert (cu->per_cu != NULL);
10282 cu->per_cu->type_hash
10283 = htab_create_alloc_ex (cu->header.length / 24,
10284 offset_and_type_hash,
10285 offset_and_type_eq,
10286 NULL,
10287 &cu->objfile->objfile_obstack,
10288 hashtab_obstack_allocate,
10289 dummy_obstack_deallocate);
10290 cu->type_hash = cu->per_cu->type_hash;
10291 }
1c379e20
DJ
10292
10293 ofs.offset = die->offset;
10294 ofs.type = type;
10295 slot = (struct dwarf2_offset_and_type **)
f792889a 10296 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
1c379e20
DJ
10297 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
10298 **slot = ofs;
f792889a 10299 return type;
1c379e20
DJ
10300}
10301
f792889a
DJ
10302/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
10303 not have a saved type. */
1c379e20
DJ
10304
10305static struct type *
f792889a 10306get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
10307{
10308 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
10309 htab_t type_hash = cu->type_hash;
10310
10311 if (type_hash == NULL)
10312 return NULL;
1c379e20
DJ
10313
10314 ofs.offset = die->offset;
10315 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
10316 if (slot)
10317 return slot->type;
10318 else
10319 return NULL;
10320}
10321
10b3939b
DJ
10322/* Set the mark field in CU and in every other compilation unit in the
10323 cache that we must keep because we are keeping CU. */
10324
10325/* Add a dependence relationship from CU to REF_PER_CU. */
10326
10327static void
10328dwarf2_add_dependence (struct dwarf2_cu *cu,
10329 struct dwarf2_per_cu_data *ref_per_cu)
10330{
10331 void **slot;
10332
10333 if (cu->dependencies == NULL)
10334 cu->dependencies
10335 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
10336 NULL, &cu->comp_unit_obstack,
10337 hashtab_obstack_allocate,
10338 dummy_obstack_deallocate);
10339
10340 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
10341 if (*slot == NULL)
10342 *slot = ref_per_cu;
10343}
1c379e20 10344
ae038cb0
DJ
10345/* Set the mark field in CU and in every other compilation unit in the
10346 cache that we must keep because we are keeping CU. */
10347
10b3939b
DJ
10348static int
10349dwarf2_mark_helper (void **slot, void *data)
10350{
10351 struct dwarf2_per_cu_data *per_cu;
10352
10353 per_cu = (struct dwarf2_per_cu_data *) *slot;
10354 if (per_cu->cu->mark)
10355 return 1;
10356 per_cu->cu->mark = 1;
10357
10358 if (per_cu->cu->dependencies != NULL)
10359 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
10360
10361 return 1;
10362}
10363
ae038cb0
DJ
10364static void
10365dwarf2_mark (struct dwarf2_cu *cu)
10366{
10367 if (cu->mark)
10368 return;
10369 cu->mark = 1;
10b3939b
DJ
10370 if (cu->dependencies != NULL)
10371 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
10372}
10373
10374static void
10375dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
10376{
10377 while (per_cu)
10378 {
10379 per_cu->cu->mark = 0;
10380 per_cu = per_cu->cu->read_in_chain;
10381 }
72bf9492
DJ
10382}
10383
72bf9492
DJ
10384/* Trivial hash function for partial_die_info: the hash value of a DIE
10385 is its offset in .debug_info for this objfile. */
10386
10387static hashval_t
10388partial_die_hash (const void *item)
10389{
10390 const struct partial_die_info *part_die = item;
10391 return part_die->offset;
10392}
10393
10394/* Trivial comparison function for partial_die_info structures: two DIEs
10395 are equal if they have the same offset. */
10396
10397static int
10398partial_die_eq (const void *item_lhs, const void *item_rhs)
10399{
10400 const struct partial_die_info *part_die_lhs = item_lhs;
10401 const struct partial_die_info *part_die_rhs = item_rhs;
10402 return part_die_lhs->offset == part_die_rhs->offset;
10403}
10404
ae038cb0
DJ
10405static struct cmd_list_element *set_dwarf2_cmdlist;
10406static struct cmd_list_element *show_dwarf2_cmdlist;
10407
10408static void
10409set_dwarf2_cmd (char *args, int from_tty)
10410{
10411 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
10412}
10413
10414static void
10415show_dwarf2_cmd (char *args, int from_tty)
10416{
10417 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
10418}
10419
6502dd73
DJ
10420void _initialize_dwarf2_read (void);
10421
10422void
10423_initialize_dwarf2_read (void)
10424{
10425 dwarf2_objfile_data_key = register_objfile_data ();
ae038cb0 10426
1bedd215
AC
10427 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
10428Set DWARF 2 specific variables.\n\
10429Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
10430 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
10431 0/*allow-unknown*/, &maintenance_set_cmdlist);
10432
1bedd215
AC
10433 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
10434Show DWARF 2 specific variables\n\
10435Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
10436 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
10437 0/*allow-unknown*/, &maintenance_show_cmdlist);
10438
10439 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
10440 &dwarf2_max_cache_age, _("\
10441Set the upper bound on the age of cached dwarf2 compilation units."), _("\
10442Show the upper bound on the age of cached dwarf2 compilation units."), _("\
10443A higher limit means that cached compilation units will be stored\n\
10444in memory longer, and more total memory will be used. Zero disables\n\
10445caching, which can slow down startup."),
2c5b56ce 10446 NULL,
920d2a44 10447 show_dwarf2_max_cache_age,
2c5b56ce 10448 &set_dwarf2_cmdlist,
ae038cb0 10449 &show_dwarf2_cmdlist);
6502dd73 10450}
This page took 1.370229 seconds and 4 git commands to generate.