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