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