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