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