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