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