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