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