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:
134d01f1
DJ
2852 /* Add a typedef symbol for the type definition, if it has a
2853 DW_AT_name. */
f792889a 2854 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 2855 break;
c906108c 2856 case DW_TAG_common_block:
e7c27a73 2857 read_common_block (die, cu);
c906108c
SS
2858 break;
2859 case DW_TAG_common_inclusion:
2860 break;
d9fa45fe 2861 case DW_TAG_namespace:
63d06c5c 2862 processing_has_namespace_info = 1;
e7c27a73 2863 read_namespace (die, cu);
d9fa45fe 2864 break;
5d7cb8df
JK
2865 case DW_TAG_module:
2866 read_module (die, cu);
2867 break;
d9fa45fe
DC
2868 case DW_TAG_imported_declaration:
2869 case DW_TAG_imported_module:
2870 /* FIXME: carlton/2002-10-16: Eventually, we should use the
2871 information contained in these. DW_TAG_imported_declaration
2872 dies shouldn't have children; DW_TAG_imported_module dies
2873 shouldn't in the C++ case, but conceivably could in the
d68209e4 2874 Fortran case. */
63d06c5c 2875 processing_has_namespace_info = 1;
d68209e4
JK
2876 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
2877 dwarf_tag_name (die->tag));
d9fa45fe 2878 break;
c906108c 2879 default:
e7c27a73 2880 new_symbol (die, NULL, cu);
c906108c
SS
2881 break;
2882 }
2883}
2884
0114d602
DJ
2885/* Return the fully qualified name of DIE, based on its DW_AT_name.
2886 If scope qualifiers are appropriate they will be added. The result
2887 will be allocated on the objfile_obstack, or NULL if the DIE does
2888 not have a name. */
2889
2890static const char *
2891dwarf2_full_name (struct die_info *die, struct dwarf2_cu *cu)
2892{
2893 struct attribute *attr;
2894 char *prefix, *name;
2895 struct ui_file *buf = NULL;
2896
2897 name = dwarf2_name (die, cu);
2898 if (!name)
2899 return NULL;
2900
2901 /* These are the only languages we know how to qualify names in. */
2902 if (cu->language != language_cplus
2903 && cu->language != language_java)
2904 return name;
2905
2906 /* If no prefix is necessary for this type of DIE, return the
2907 unqualified name. The other three tags listed could be handled
2908 in pdi_needs_namespace, but that requires broader changes. */
2909 if (!pdi_needs_namespace (die->tag)
2910 && die->tag != DW_TAG_subprogram
2911 && die->tag != DW_TAG_variable
2912 && die->tag != DW_TAG_member)
2913 return name;
2914
2915 prefix = determine_prefix (die, cu);
2916 if (*prefix != '\0')
2917 name = typename_concat (&cu->objfile->objfile_obstack, prefix,
2918 name, cu);
2919
2920 return name;
2921}
2922
5fb290d7 2923static void
e142c38c 2924initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 2925{
e142c38c 2926 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
2927}
2928
cb1df416
DJ
2929static void
2930free_cu_line_header (void *arg)
2931{
2932 struct dwarf2_cu *cu = arg;
2933
2934 free_line_header (cu->line_header);
2935 cu->line_header = NULL;
2936}
2937
c906108c 2938static void
e7c27a73 2939read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 2940{
e7c27a73
DJ
2941 struct objfile *objfile = cu->objfile;
2942 struct comp_unit_head *cu_header = &cu->header;
debd256d 2943 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 2944 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
2945 CORE_ADDR highpc = ((CORE_ADDR) 0);
2946 struct attribute *attr;
e1024ff1 2947 char *name = NULL;
c906108c
SS
2948 char *comp_dir = NULL;
2949 struct die_info *child_die;
2950 bfd *abfd = objfile->obfd;
debd256d 2951 struct line_header *line_header = 0;
e142c38c
DJ
2952 CORE_ADDR baseaddr;
2953
2954 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 2955
fae299cd 2956 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
2957
2958 /* If we didn't find a lowpc, set it to highpc to avoid complaints
2959 from finish_block. */
2acceee2 2960 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
2961 lowpc = highpc;
2962 lowpc += baseaddr;
2963 highpc += baseaddr;
2964
39cbfefa
DJ
2965 /* Find the filename. Do not use dwarf2_name here, since the filename
2966 is not a source language identifier. */
e142c38c 2967 attr = dwarf2_attr (die, DW_AT_name, cu);
c906108c
SS
2968 if (attr)
2969 {
2970 name = DW_STRING (attr);
2971 }
e1024ff1 2972
e142c38c 2973 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
c906108c 2974 if (attr)
e1024ff1
DJ
2975 comp_dir = DW_STRING (attr);
2976 else if (name != NULL && IS_ABSOLUTE_PATH (name))
c906108c 2977 {
e1024ff1
DJ
2978 comp_dir = ldirname (name);
2979 if (comp_dir != NULL)
2980 make_cleanup (xfree, comp_dir);
2981 }
2982 if (comp_dir != NULL)
2983 {
2984 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2985 directory, get rid of it. */
2986 char *cp = strchr (comp_dir, ':');
c906108c 2987
e1024ff1
DJ
2988 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2989 comp_dir = cp + 1;
c906108c
SS
2990 }
2991
e1024ff1
DJ
2992 if (name == NULL)
2993 name = "<unknown>";
2994
e142c38c 2995 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
2996 if (attr)
2997 {
e142c38c 2998 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
2999 }
3000
b0f35d58
DL
3001 attr = dwarf2_attr (die, DW_AT_producer, cu);
3002 if (attr)
3003 cu->producer = DW_STRING (attr);
303b6f5d 3004
c906108c
SS
3005 /* We assume that we're processing GCC output. */
3006 processing_gcc_compilation = 2;
c906108c 3007
df8a16a1
DJ
3008 processing_has_namespace_info = 0;
3009
c906108c
SS
3010 start_symtab (name, comp_dir, lowpc);
3011 record_debugformat ("DWARF 2");
303b6f5d 3012 record_producer (cu->producer);
c906108c 3013
e142c38c 3014 initialize_cu_func_list (cu);
c906108c 3015
cb1df416
DJ
3016 /* Decode line number information if present. We do this before
3017 processing child DIEs, so that the line header table is available
3018 for DW_AT_decl_file. */
e142c38c 3019 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
3020 if (attr)
3021 {
debd256d 3022 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 3023 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
3024 if (line_header)
3025 {
cb1df416
DJ
3026 cu->line_header = line_header;
3027 make_cleanup (free_cu_line_header, cu);
aaa75496 3028 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 3029 }
5fb290d7 3030 }
debd256d 3031
cb1df416
DJ
3032 /* Process all dies in compilation unit. */
3033 if (die->child != NULL)
3034 {
3035 child_die = die->child;
3036 while (child_die && child_die->tag)
3037 {
3038 process_die (child_die, cu);
3039 child_die = sibling_die (child_die);
3040 }
3041 }
3042
2e276125
JB
3043 /* Decode macro information, if present. Dwarf 2 macro information
3044 refers to information in the line number info statement program
3045 header, so we can only read it if we've read the header
3046 successfully. */
e142c38c 3047 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 3048 if (attr && line_header)
2e276125
JB
3049 {
3050 unsigned int macro_offset = DW_UNSND (attr);
3051 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 3052 comp_dir, abfd, cu);
2e276125 3053 }
debd256d 3054 do_cleanups (back_to);
5fb290d7
DJ
3055}
3056
3057static void
e142c38c
DJ
3058add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
3059 struct dwarf2_cu *cu)
5fb290d7
DJ
3060{
3061 struct function_range *thisfn;
3062
3063 thisfn = (struct function_range *)
7b5a2f43 3064 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
3065 thisfn->name = name;
3066 thisfn->lowpc = lowpc;
3067 thisfn->highpc = highpc;
3068 thisfn->seen_line = 0;
3069 thisfn->next = NULL;
3070
e142c38c
DJ
3071 if (cu->last_fn == NULL)
3072 cu->first_fn = thisfn;
5fb290d7 3073 else
e142c38c 3074 cu->last_fn->next = thisfn;
5fb290d7 3075
e142c38c 3076 cu->last_fn = thisfn;
c906108c
SS
3077}
3078
3079static void
e7c27a73 3080read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3081{
e7c27a73 3082 struct objfile *objfile = cu->objfile;
52f0bd74 3083 struct context_stack *new;
c906108c
SS
3084 CORE_ADDR lowpc;
3085 CORE_ADDR highpc;
3086 struct die_info *child_die;
3087 struct attribute *attr;
3088 char *name;
e142c38c 3089 CORE_ADDR baseaddr;
801e3a5b 3090 struct block *block;
c906108c 3091
e142c38c
DJ
3092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3093
3094 name = dwarf2_linkage_name (die, cu);
c906108c
SS
3095
3096 /* Ignore functions with missing or empty names and functions with
3097 missing or invalid low and high pc attributes. */
e7c27a73 3098 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3099 return;
3100
3101 lowpc += baseaddr;
3102 highpc += baseaddr;
3103
5fb290d7 3104 /* Record the function range for dwarf_decode_lines. */
e142c38c 3105 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 3106
c906108c 3107 new = push_context (0, lowpc);
f792889a 3108 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 3109
4cecd739
DJ
3110 /* If there is a location expression for DW_AT_frame_base, record
3111 it. */
e142c38c 3112 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 3113 if (attr)
c034e007
AC
3114 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3115 expression is being recorded directly in the function's symbol
3116 and not in a separate frame-base object. I guess this hack is
3117 to avoid adding some sort of frame-base adjunct/annex to the
3118 function's symbol :-(. The problem with doing this is that it
3119 results in a function symbol with a location expression that
3120 has nothing to do with the location of the function, ouch! The
3121 relationship should be: a function's symbol has-a frame base; a
3122 frame-base has-a location expression. */
e7c27a73 3123 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 3124
e142c38c 3125 cu->list_in_scope = &local_symbols;
c906108c 3126
639d11d3 3127 if (die->child != NULL)
c906108c 3128 {
639d11d3 3129 child_die = die->child;
c906108c
SS
3130 while (child_die && child_die->tag)
3131 {
e7c27a73 3132 process_die (child_die, cu);
c906108c
SS
3133 child_die = sibling_die (child_die);
3134 }
3135 }
3136
3137 new = pop_context ();
3138 /* Make a block for the local symbols within. */
801e3a5b
JB
3139 block = finish_block (new->name, &local_symbols, new->old_blocks,
3140 lowpc, highpc, objfile);
3141
df8a16a1
DJ
3142 /* For C++, set the block's scope. */
3143 if (cu->language == language_cplus)
3144 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 3145 determine_prefix (die, cu),
df8a16a1
DJ
3146 processing_has_namespace_info);
3147
801e3a5b
JB
3148 /* If we have address ranges, record them. */
3149 dwarf2_record_block_ranges (die, block, baseaddr, cu);
208d8187
JB
3150
3151 /* In C++, we can have functions nested inside functions (e.g., when
3152 a function declares a class that has methods). This means that
3153 when we finish processing a function scope, we may need to go
3154 back to building a containing block's symbol lists. */
3155 local_symbols = new->locals;
3156 param_symbols = new->params;
3157
921e78cf
JB
3158 /* If we've finished processing a top-level function, subsequent
3159 symbols go in the file symbol list. */
3160 if (outermost_context_p ())
e142c38c 3161 cu->list_in_scope = &file_symbols;
c906108c
SS
3162}
3163
3164/* Process all the DIES contained within a lexical block scope. Start
3165 a new scope, process the dies, and then close the scope. */
3166
3167static void
e7c27a73 3168read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3169{
e7c27a73 3170 struct objfile *objfile = cu->objfile;
52f0bd74 3171 struct context_stack *new;
c906108c
SS
3172 CORE_ADDR lowpc, highpc;
3173 struct die_info *child_die;
e142c38c
DJ
3174 CORE_ADDR baseaddr;
3175
3176 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
3177
3178 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
3179 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
3180 as multiple lexical blocks? Handling children in a sane way would
3181 be nasty. Might be easier to properly extend generic blocks to
3182 describe ranges. */
e7c27a73 3183 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3184 return;
3185 lowpc += baseaddr;
3186 highpc += baseaddr;
3187
3188 push_context (0, lowpc);
639d11d3 3189 if (die->child != NULL)
c906108c 3190 {
639d11d3 3191 child_die = die->child;
c906108c
SS
3192 while (child_die && child_die->tag)
3193 {
e7c27a73 3194 process_die (child_die, cu);
c906108c
SS
3195 child_die = sibling_die (child_die);
3196 }
3197 }
3198 new = pop_context ();
3199
3200 if (local_symbols != NULL)
3201 {
801e3a5b
JB
3202 struct block *block
3203 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
3204 highpc, objfile);
3205
3206 /* Note that recording ranges after traversing children, as we
3207 do here, means that recording a parent's ranges entails
3208 walking across all its children's ranges as they appear in
3209 the address map, which is quadratic behavior.
3210
3211 It would be nicer to record the parent's ranges before
3212 traversing its children, simply overriding whatever you find
3213 there. But since we don't even decide whether to create a
3214 block until after we've traversed its children, that's hard
3215 to do. */
3216 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
3217 }
3218 local_symbols = new->locals;
3219}
3220
43039443 3221/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
3222 Return 1 if the attributes are present and valid, otherwise, return 0.
3223 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
3224
3225static int
3226dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
3227 CORE_ADDR *high_return, struct dwarf2_cu *cu,
3228 struct partial_symtab *ranges_pst)
43039443
JK
3229{
3230 struct objfile *objfile = cu->objfile;
3231 struct comp_unit_head *cu_header = &cu->header;
3232 bfd *obfd = objfile->obfd;
3233 unsigned int addr_size = cu_header->addr_size;
3234 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3235 /* Base address selection entry. */
3236 CORE_ADDR base;
3237 int found_base;
3238 unsigned int dummy;
3239 gdb_byte *buffer;
3240 CORE_ADDR marker;
3241 int low_set;
3242 CORE_ADDR low = 0;
3243 CORE_ADDR high = 0;
ff013f42 3244 CORE_ADDR baseaddr;
43039443 3245
d00adf39
DE
3246 found_base = cu->base_known;
3247 base = cu->base_address;
43039443
JK
3248
3249 if (offset >= dwarf2_per_objfile->ranges_size)
3250 {
3251 complaint (&symfile_complaints,
3252 _("Offset %d out of bounds for DW_AT_ranges attribute"),
3253 offset);
3254 return 0;
3255 }
3256 buffer = dwarf2_per_objfile->ranges_buffer + offset;
3257
3258 /* Read in the largest possible address. */
3259 marker = read_address (obfd, buffer, cu, &dummy);
3260 if ((marker & mask) == mask)
3261 {
3262 /* If we found the largest possible address, then
3263 read the base address. */
3264 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3265 buffer += 2 * addr_size;
3266 offset += 2 * addr_size;
3267 found_base = 1;
3268 }
3269
3270 low_set = 0;
3271
e7030f15 3272 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 3273
43039443
JK
3274 while (1)
3275 {
3276 CORE_ADDR range_beginning, range_end;
3277
3278 range_beginning = read_address (obfd, buffer, cu, &dummy);
3279 buffer += addr_size;
3280 range_end = read_address (obfd, buffer, cu, &dummy);
3281 buffer += addr_size;
3282 offset += 2 * addr_size;
3283
3284 /* An end of list marker is a pair of zero addresses. */
3285 if (range_beginning == 0 && range_end == 0)
3286 /* Found the end of list entry. */
3287 break;
3288
3289 /* Each base address selection entry is a pair of 2 values.
3290 The first is the largest possible address, the second is
3291 the base address. Check for a base address here. */
3292 if ((range_beginning & mask) == mask)
3293 {
3294 /* If we found the largest possible address, then
3295 read the base address. */
3296 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3297 found_base = 1;
3298 continue;
3299 }
3300
3301 if (!found_base)
3302 {
3303 /* We have no valid base address for the ranges
3304 data. */
3305 complaint (&symfile_complaints,
3306 _("Invalid .debug_ranges data (no base address)"));
3307 return 0;
3308 }
3309
3310 range_beginning += base;
3311 range_end += base;
3312
ff013f42
JK
3313 if (ranges_pst != NULL && range_beginning < range_end)
3314 addrmap_set_empty (objfile->psymtabs_addrmap,
3315 range_beginning + baseaddr, range_end - 1 + baseaddr,
3316 ranges_pst);
3317
43039443
JK
3318 /* FIXME: This is recording everything as a low-high
3319 segment of consecutive addresses. We should have a
3320 data structure for discontiguous block ranges
3321 instead. */
3322 if (! low_set)
3323 {
3324 low = range_beginning;
3325 high = range_end;
3326 low_set = 1;
3327 }
3328 else
3329 {
3330 if (range_beginning < low)
3331 low = range_beginning;
3332 if (range_end > high)
3333 high = range_end;
3334 }
3335 }
3336
3337 if (! low_set)
3338 /* If the first entry is an end-of-list marker, the range
3339 describes an empty scope, i.e. no instructions. */
3340 return 0;
3341
3342 if (low_return)
3343 *low_return = low;
3344 if (high_return)
3345 *high_return = high;
3346 return 1;
3347}
3348
af34e669
DJ
3349/* Get low and high pc attributes from a die. Return 1 if the attributes
3350 are present and valid, otherwise, return 0. Return -1 if the range is
3351 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 3352static int
af34e669 3353dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
e7c27a73 3354 CORE_ADDR *highpc, struct dwarf2_cu *cu)
c906108c
SS
3355{
3356 struct attribute *attr;
af34e669
DJ
3357 CORE_ADDR low = 0;
3358 CORE_ADDR high = 0;
3359 int ret = 0;
c906108c 3360
e142c38c 3361 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 3362 if (attr)
af34e669
DJ
3363 {
3364 high = DW_ADDR (attr);
e142c38c 3365 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
3366 if (attr)
3367 low = DW_ADDR (attr);
3368 else
3369 /* Found high w/o low attribute. */
3370 return 0;
3371
3372 /* Found consecutive range of addresses. */
3373 ret = 1;
3374 }
c906108c 3375 else
af34e669 3376 {
e142c38c 3377 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
3378 if (attr != NULL)
3379 {
af34e669 3380 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 3381 .debug_ranges section. */
ff013f42 3382 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, NULL))
af34e669 3383 return 0;
43039443 3384 /* Found discontinuous range of addresses. */
af34e669
DJ
3385 ret = -1;
3386 }
3387 }
c906108c
SS
3388
3389 if (high < low)
3390 return 0;
3391
3392 /* When using the GNU linker, .gnu.linkonce. sections are used to
3393 eliminate duplicate copies of functions and vtables and such.
3394 The linker will arbitrarily choose one and discard the others.
3395 The AT_*_pc values for such functions refer to local labels in
3396 these sections. If the section from that file was discarded, the
3397 labels are not in the output, so the relocs get a value of 0.
3398 If this is a discarded function, mark the pc bounds as invalid,
3399 so that GDB will ignore it. */
72dca2f5 3400 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
3401 return 0;
3402
3403 *lowpc = low;
3404 *highpc = high;
af34e669 3405 return ret;
c906108c
SS
3406}
3407
b084d499
JB
3408/* Assuming that DIE represents a subprogram DIE or a lexical block, get
3409 its low and high PC addresses. Do nothing if these addresses could not
3410 be determined. Otherwise, set LOWPC to the low address if it is smaller,
3411 and HIGHPC to the high address if greater than HIGHPC. */
3412
3413static void
3414dwarf2_get_subprogram_pc_bounds (struct die_info *die,
3415 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3416 struct dwarf2_cu *cu)
3417{
3418 CORE_ADDR low, high;
3419 struct die_info *child = die->child;
3420
3421 if (dwarf2_get_pc_bounds (die, &low, &high, cu))
3422 {
3423 *lowpc = min (*lowpc, low);
3424 *highpc = max (*highpc, high);
3425 }
3426
3427 /* If the language does not allow nested subprograms (either inside
3428 subprograms or lexical blocks), we're done. */
3429 if (cu->language != language_ada)
3430 return;
3431
3432 /* Check all the children of the given DIE. If it contains nested
3433 subprograms, then check their pc bounds. Likewise, we need to
3434 check lexical blocks as well, as they may also contain subprogram
3435 definitions. */
3436 while (child && child->tag)
3437 {
3438 if (child->tag == DW_TAG_subprogram
3439 || child->tag == DW_TAG_lexical_block)
3440 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
3441 child = sibling_die (child);
3442 }
3443}
3444
fae299cd
DC
3445/* Get the low and high pc's represented by the scope DIE, and store
3446 them in *LOWPC and *HIGHPC. If the correct values can't be
3447 determined, set *LOWPC to -1 and *HIGHPC to 0. */
3448
3449static void
3450get_scope_pc_bounds (struct die_info *die,
3451 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3452 struct dwarf2_cu *cu)
3453{
3454 CORE_ADDR best_low = (CORE_ADDR) -1;
3455 CORE_ADDR best_high = (CORE_ADDR) 0;
3456 CORE_ADDR current_low, current_high;
3457
3458 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu))
3459 {
3460 best_low = current_low;
3461 best_high = current_high;
3462 }
3463 else
3464 {
3465 struct die_info *child = die->child;
3466
3467 while (child && child->tag)
3468 {
3469 switch (child->tag) {
3470 case DW_TAG_subprogram:
b084d499 3471 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
3472 break;
3473 case DW_TAG_namespace:
3474 /* FIXME: carlton/2004-01-16: Should we do this for
3475 DW_TAG_class_type/DW_TAG_structure_type, too? I think
3476 that current GCC's always emit the DIEs corresponding
3477 to definitions of methods of classes as children of a
3478 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
3479 the DIEs giving the declarations, which could be
3480 anywhere). But I don't see any reason why the
3481 standards says that they have to be there. */
3482 get_scope_pc_bounds (child, &current_low, &current_high, cu);
3483
3484 if (current_low != ((CORE_ADDR) -1))
3485 {
3486 best_low = min (best_low, current_low);
3487 best_high = max (best_high, current_high);
3488 }
3489 break;
3490 default:
3491 /* Ignore. */
3492 break;
3493 }
3494
3495 child = sibling_die (child);
3496 }
3497 }
3498
3499 *lowpc = best_low;
3500 *highpc = best_high;
3501}
3502
801e3a5b
JB
3503/* Record the address ranges for BLOCK, offset by BASEADDR, as given
3504 in DIE. */
3505static void
3506dwarf2_record_block_ranges (struct die_info *die, struct block *block,
3507 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
3508{
3509 struct attribute *attr;
3510
3511 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
3512 if (attr)
3513 {
3514 CORE_ADDR high = DW_ADDR (attr);
3515 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3516 if (attr)
3517 {
3518 CORE_ADDR low = DW_ADDR (attr);
3519 record_block_range (block, baseaddr + low, baseaddr + high - 1);
3520 }
3521 }
3522
3523 attr = dwarf2_attr (die, DW_AT_ranges, cu);
3524 if (attr)
3525 {
3526 bfd *obfd = cu->objfile->obfd;
3527
3528 /* The value of the DW_AT_ranges attribute is the offset of the
3529 address range list in the .debug_ranges section. */
3530 unsigned long offset = DW_UNSND (attr);
3531 gdb_byte *buffer = dwarf2_per_objfile->ranges_buffer + offset;
3532
3533 /* For some target architectures, but not others, the
3534 read_address function sign-extends the addresses it returns.
3535 To recognize base address selection entries, we need a
3536 mask. */
3537 unsigned int addr_size = cu->header.addr_size;
3538 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3539
3540 /* The base address, to which the next pair is relative. Note
3541 that this 'base' is a DWARF concept: most entries in a range
3542 list are relative, to reduce the number of relocs against the
3543 debugging information. This is separate from this function's
3544 'baseaddr' argument, which GDB uses to relocate debugging
3545 information from a shared library based on the address at
3546 which the library was loaded. */
d00adf39
DE
3547 CORE_ADDR base = cu->base_address;
3548 int base_known = cu->base_known;
801e3a5b
JB
3549
3550 if (offset >= dwarf2_per_objfile->ranges_size)
3551 {
3552 complaint (&symfile_complaints,
3553 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
3554 offset);
3555 return;
3556 }
3557
3558 for (;;)
3559 {
3560 unsigned int bytes_read;
3561 CORE_ADDR start, end;
3562
3563 start = read_address (obfd, buffer, cu, &bytes_read);
3564 buffer += bytes_read;
3565 end = read_address (obfd, buffer, cu, &bytes_read);
3566 buffer += bytes_read;
3567
3568 /* Did we find the end of the range list? */
3569 if (start == 0 && end == 0)
3570 break;
3571
3572 /* Did we find a base address selection entry? */
3573 else if ((start & base_select_mask) == base_select_mask)
3574 {
3575 base = end;
3576 base_known = 1;
3577 }
3578
3579 /* We found an ordinary address range. */
3580 else
3581 {
3582 if (!base_known)
3583 {
3584 complaint (&symfile_complaints,
3585 _("Invalid .debug_ranges data (no base address)"));
3586 return;
3587 }
3588
3589 record_block_range (block,
3590 baseaddr + base + start,
3591 baseaddr + base + end - 1);
3592 }
3593 }
3594 }
3595}
3596
c906108c
SS
3597/* Add an aggregate field to the field list. */
3598
3599static void
107d2387 3600dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73
DJ
3601 struct dwarf2_cu *cu)
3602{
3603 struct objfile *objfile = cu->objfile;
5e2b427d 3604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
3605 struct nextfield *new_field;
3606 struct attribute *attr;
3607 struct field *fp;
3608 char *fieldname = "";
3609
3610 /* Allocate a new field list entry and link it in. */
3611 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 3612 make_cleanup (xfree, new_field);
c906108c
SS
3613 memset (new_field, 0, sizeof (struct nextfield));
3614 new_field->next = fip->fields;
3615 fip->fields = new_field;
3616 fip->nfields++;
3617
3618 /* Handle accessibility and virtuality of field.
3619 The default accessibility for members is public, the default
3620 accessibility for inheritance is private. */
3621 if (die->tag != DW_TAG_inheritance)
3622 new_field->accessibility = DW_ACCESS_public;
3623 else
3624 new_field->accessibility = DW_ACCESS_private;
3625 new_field->virtuality = DW_VIRTUALITY_none;
3626
e142c38c 3627 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
3628 if (attr)
3629 new_field->accessibility = DW_UNSND (attr);
3630 if (new_field->accessibility != DW_ACCESS_public)
3631 fip->non_public_fields = 1;
e142c38c 3632 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
3633 if (attr)
3634 new_field->virtuality = DW_UNSND (attr);
3635
3636 fp = &new_field->field;
a9a9bd0f 3637
e142c38c 3638 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 3639 {
a9a9bd0f
DC
3640 /* Data member other than a C++ static data member. */
3641
c906108c 3642 /* Get type of field. */
e7c27a73 3643 fp->type = die_type (die, cu);
c906108c 3644
d6a843b5 3645 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 3646
c906108c 3647 /* Get bit size of field (zero if none). */
e142c38c 3648 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
3649 if (attr)
3650 {
3651 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
3652 }
3653 else
3654 {
3655 FIELD_BITSIZE (*fp) = 0;
3656 }
3657
3658 /* Get bit offset of field. */
e142c38c 3659 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
3660 if (attr)
3661 {
c6a0999f
JB
3662 int byte_offset;
3663
3690dd37
JB
3664 if (attr_form_is_section_offset (attr))
3665 {
3666 dwarf2_complex_location_expr_complaint ();
c6a0999f 3667 byte_offset = 0;
3690dd37
JB
3668 }
3669 else if (attr_form_is_constant (attr))
c6a0999f 3670 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
3690dd37 3671 else
c6a0999f
JB
3672 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
3673
d6a843b5 3674 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 3675 }
e142c38c 3676 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
3677 if (attr)
3678 {
5e2b427d 3679 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
3680 {
3681 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
3682 additional bit offset from the MSB of the containing
3683 anonymous object to the MSB of the field. We don't
3684 have to do anything special since we don't need to
3685 know the size of the anonymous object. */
c906108c
SS
3686 FIELD_BITPOS (*fp) += DW_UNSND (attr);
3687 }
3688 else
3689 {
3690 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
3691 MSB of the anonymous object, subtract off the number of
3692 bits from the MSB of the field to the MSB of the
3693 object, and then subtract off the number of bits of
3694 the field itself. The result is the bit offset of
3695 the LSB of the field. */
c906108c
SS
3696 int anonymous_size;
3697 int bit_offset = DW_UNSND (attr);
3698
e142c38c 3699 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
3700 if (attr)
3701 {
3702 /* The size of the anonymous object containing
3703 the bit field is explicit, so use the
3704 indicated size (in bytes). */
3705 anonymous_size = DW_UNSND (attr);
3706 }
3707 else
3708 {
3709 /* The size of the anonymous object containing
3710 the bit field must be inferred from the type
3711 attribute of the data member containing the
3712 bit field. */
3713 anonymous_size = TYPE_LENGTH (fp->type);
3714 }
3715 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
3716 - bit_offset - FIELD_BITSIZE (*fp);
3717 }
3718 }
3719
3720 /* Get name of field. */
39cbfefa
DJ
3721 fieldname = dwarf2_name (die, cu);
3722 if (fieldname == NULL)
3723 fieldname = "";
d8151005
DJ
3724
3725 /* The name is already allocated along with this objfile, so we don't
3726 need to duplicate it for the type. */
3727 fp->name = fieldname;
c906108c
SS
3728
3729 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 3730 pointer or virtual base class pointer) to private. */
e142c38c 3731 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c
SS
3732 {
3733 new_field->accessibility = DW_ACCESS_private;
3734 fip->non_public_fields = 1;
3735 }
3736 }
a9a9bd0f 3737 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 3738 {
a9a9bd0f
DC
3739 /* C++ static member. */
3740
3741 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
3742 is a declaration, but all versions of G++ as of this writing
3743 (so through at least 3.2.1) incorrectly generate
3744 DW_TAG_variable tags. */
3745
c906108c 3746 char *physname;
c906108c 3747
a9a9bd0f 3748 /* Get name of field. */
39cbfefa
DJ
3749 fieldname = dwarf2_name (die, cu);
3750 if (fieldname == NULL)
c906108c
SS
3751 return;
3752
2df3850c 3753 /* Get physical name. */
e142c38c 3754 physname = dwarf2_linkage_name (die, cu);
c906108c 3755
d8151005
DJ
3756 /* The name is already allocated along with this objfile, so we don't
3757 need to duplicate it for the type. */
3758 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 3759 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 3760 FIELD_NAME (*fp) = fieldname;
c906108c
SS
3761 }
3762 else if (die->tag == DW_TAG_inheritance)
3763 {
3764 /* C++ base class field. */
e142c38c 3765 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 3766 if (attr)
d6a843b5
JK
3767 SET_FIELD_BITPOS (*fp, decode_locdesc (DW_BLOCK (attr), cu)
3768 * bits_per_byte);
c906108c 3769 FIELD_BITSIZE (*fp) = 0;
e7c27a73 3770 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
3771 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
3772 fip->nbaseclasses++;
3773 }
3774}
3775
3776/* Create the vector of fields, and attach it to the type. */
3777
3778static void
fba45db2 3779dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 3780 struct dwarf2_cu *cu)
c906108c
SS
3781{
3782 int nfields = fip->nfields;
3783
3784 /* Record the field count, allocate space for the array of fields,
3785 and create blank accessibility bitfields if necessary. */
3786 TYPE_NFIELDS (type) = nfields;
3787 TYPE_FIELDS (type) = (struct field *)
3788 TYPE_ALLOC (type, sizeof (struct field) * nfields);
3789 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
3790
3791 if (fip->non_public_fields)
3792 {
3793 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3794
3795 TYPE_FIELD_PRIVATE_BITS (type) =
3796 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3797 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
3798
3799 TYPE_FIELD_PROTECTED_BITS (type) =
3800 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3801 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
3802
3803 TYPE_FIELD_IGNORE_BITS (type) =
3804 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3805 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
3806 }
3807
3808 /* If the type has baseclasses, allocate and clear a bit vector for
3809 TYPE_FIELD_VIRTUAL_BITS. */
3810 if (fip->nbaseclasses)
3811 {
3812 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 3813 unsigned char *pointer;
c906108c
SS
3814
3815 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
3816 pointer = TYPE_ALLOC (type, num_bytes);
3817 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
3818 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
3819 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
3820 }
3821
3822 /* Copy the saved-up fields into the field vector. Start from the head
3823 of the list, adding to the tail of the field array, so that they end
3824 up in the same order in the array in which they were added to the list. */
3825 while (nfields-- > 0)
3826 {
3827 TYPE_FIELD (type, nfields) = fip->fields->field;
3828 switch (fip->fields->accessibility)
3829 {
c5aa993b
JM
3830 case DW_ACCESS_private:
3831 SET_TYPE_FIELD_PRIVATE (type, nfields);
3832 break;
c906108c 3833
c5aa993b
JM
3834 case DW_ACCESS_protected:
3835 SET_TYPE_FIELD_PROTECTED (type, nfields);
3836 break;
c906108c 3837
c5aa993b
JM
3838 case DW_ACCESS_public:
3839 break;
c906108c 3840
c5aa993b
JM
3841 default:
3842 /* Unknown accessibility. Complain and treat it as public. */
3843 {
e2e0b3e5 3844 complaint (&symfile_complaints, _("unsupported accessibility %d"),
4d3c2250 3845 fip->fields->accessibility);
c5aa993b
JM
3846 }
3847 break;
c906108c
SS
3848 }
3849 if (nfields < fip->nbaseclasses)
3850 {
3851 switch (fip->fields->virtuality)
3852 {
c5aa993b
JM
3853 case DW_VIRTUALITY_virtual:
3854 case DW_VIRTUALITY_pure_virtual:
3855 SET_TYPE_FIELD_VIRTUAL (type, nfields);
3856 break;
c906108c
SS
3857 }
3858 }
3859 fip->fields = fip->fields->next;
3860 }
3861}
3862
c906108c
SS
3863/* Add a member function to the proper fieldlist. */
3864
3865static void
107d2387 3866dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 3867 struct type *type, struct dwarf2_cu *cu)
c906108c 3868{
e7c27a73 3869 struct objfile *objfile = cu->objfile;
c906108c
SS
3870 struct attribute *attr;
3871 struct fnfieldlist *flp;
3872 int i;
3873 struct fn_field *fnp;
3874 char *fieldname;
3875 char *physname;
3876 struct nextfnfield *new_fnfield;
f792889a 3877 struct type *this_type;
c906108c 3878
2df3850c 3879 /* Get name of member function. */
39cbfefa
DJ
3880 fieldname = dwarf2_name (die, cu);
3881 if (fieldname == NULL)
2df3850c 3882 return;
c906108c 3883
2df3850c 3884 /* Get the mangled name. */
e142c38c 3885 physname = dwarf2_linkage_name (die, cu);
c906108c
SS
3886
3887 /* Look up member function name in fieldlist. */
3888 for (i = 0; i < fip->nfnfields; i++)
3889 {
27bfe10e 3890 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
3891 break;
3892 }
3893
3894 /* Create new list element if necessary. */
3895 if (i < fip->nfnfields)
3896 flp = &fip->fnfieldlists[i];
3897 else
3898 {
3899 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
3900 {
3901 fip->fnfieldlists = (struct fnfieldlist *)
3902 xrealloc (fip->fnfieldlists,
3903 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 3904 * sizeof (struct fnfieldlist));
c906108c 3905 if (fip->nfnfields == 0)
c13c43fd 3906 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
3907 }
3908 flp = &fip->fnfieldlists[fip->nfnfields];
3909 flp->name = fieldname;
3910 flp->length = 0;
3911 flp->head = NULL;
3912 fip->nfnfields++;
3913 }
3914
3915 /* Create a new member function field and chain it to the field list
3916 entry. */
3917 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 3918 make_cleanup (xfree, new_fnfield);
c906108c
SS
3919 memset (new_fnfield, 0, sizeof (struct nextfnfield));
3920 new_fnfield->next = flp->head;
3921 flp->head = new_fnfield;
3922 flp->length++;
3923
3924 /* Fill in the member function field info. */
3925 fnp = &new_fnfield->fnfield;
d8151005
DJ
3926 /* The name is already allocated along with this objfile, so we don't
3927 need to duplicate it for the type. */
3928 fnp->physname = physname ? physname : "";
c906108c 3929 fnp->type = alloc_type (objfile);
f792889a
DJ
3930 this_type = read_type_die (die, cu);
3931 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 3932 {
f792889a 3933 int nparams = TYPE_NFIELDS (this_type);
c906108c 3934
f792889a 3935 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
3936 of the method itself (TYPE_CODE_METHOD). */
3937 smash_to_method_type (fnp->type, type,
f792889a
DJ
3938 TYPE_TARGET_TYPE (this_type),
3939 TYPE_FIELDS (this_type),
3940 TYPE_NFIELDS (this_type),
3941 TYPE_VARARGS (this_type));
c906108c
SS
3942
3943 /* Handle static member functions.
c5aa993b
JM
3944 Dwarf2 has no clean way to discern C++ static and non-static
3945 member functions. G++ helps GDB by marking the first
3946 parameter for non-static member functions (which is the
3947 this pointer) as artificial. We obtain this information
3948 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 3949 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
3950 fnp->voffset = VOFFSET_STATIC;
3951 }
3952 else
e2e0b3e5 3953 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 3954 physname);
c906108c
SS
3955
3956 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 3957 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 3958 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
3959
3960 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
3961 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
3962
3963 /* Get accessibility. */
e142c38c 3964 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
3965 if (attr)
3966 {
3967 switch (DW_UNSND (attr))
3968 {
c5aa993b
JM
3969 case DW_ACCESS_private:
3970 fnp->is_private = 1;
3971 break;
3972 case DW_ACCESS_protected:
3973 fnp->is_protected = 1;
3974 break;
c906108c
SS
3975 }
3976 }
3977
b02dede2 3978 /* Check for artificial methods. */
e142c38c 3979 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
3980 if (attr && DW_UNSND (attr) != 0)
3981 fnp->is_artificial = 1;
3982
c906108c 3983 /* Get index in virtual function table if it is a virtual member function. */
e142c38c 3984 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
c906108c 3985 if (attr)
8e19ed76
PS
3986 {
3987 /* Support the .debug_loc offsets */
3988 if (attr_form_is_block (attr))
3989 {
e7c27a73 3990 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8e19ed76 3991 }
3690dd37 3992 else if (attr_form_is_section_offset (attr))
8e19ed76 3993 {
4d3c2250 3994 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
3995 }
3996 else
3997 {
4d3c2250
KB
3998 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
3999 fieldname);
8e19ed76
PS
4000 }
4001 }
c906108c
SS
4002}
4003
4004/* Create the vector of member function fields, and attach it to the type. */
4005
4006static void
fba45db2 4007dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 4008 struct dwarf2_cu *cu)
c906108c
SS
4009{
4010 struct fnfieldlist *flp;
4011 int total_length = 0;
4012 int i;
4013
4014 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4015 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
4016 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
4017
4018 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
4019 {
4020 struct nextfnfield *nfp = flp->head;
4021 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
4022 int k;
4023
4024 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
4025 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
4026 fn_flp->fn_fields = (struct fn_field *)
4027 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
4028 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 4029 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
4030
4031 total_length += flp->length;
4032 }
4033
4034 TYPE_NFN_FIELDS (type) = fip->nfnfields;
4035 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
4036}
4037
1168df01
JB
4038/* Returns non-zero if NAME is the name of a vtable member in CU's
4039 language, zero otherwise. */
4040static int
4041is_vtable_name (const char *name, struct dwarf2_cu *cu)
4042{
4043 static const char vptr[] = "_vptr";
987504bb 4044 static const char vtable[] = "vtable";
1168df01 4045
987504bb
JJ
4046 /* Look for the C++ and Java forms of the vtable. */
4047 if ((cu->language == language_java
4048 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
4049 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
4050 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
4051 return 1;
4052
4053 return 0;
4054}
4055
c0dd20ea
DJ
4056/* GCC outputs unnamed structures that are really pointers to member
4057 functions, with the ABI-specified layout. If DIE (from CU) describes
4058 such a structure, set its type, and return nonzero. Otherwise return
61049d3b
DJ
4059 zero.
4060
4061 GCC shouldn't do this; it should just output pointer to member DIEs.
4062 This is GCC PR debug/28767. */
c0dd20ea 4063
f792889a 4064static struct type *
c0dd20ea
DJ
4065quirk_gcc_member_function_pointer (struct die_info *die, struct dwarf2_cu *cu)
4066{
4067 struct objfile *objfile = cu->objfile;
4068 struct type *type;
4069 struct die_info *pfn_die, *delta_die;
4070 struct attribute *pfn_name, *delta_name;
4071 struct type *pfn_type, *domain_type;
4072
4073 /* Check for a structure with no name and two children. */
4074 if (die->tag != DW_TAG_structure_type
4075 || dwarf2_attr (die, DW_AT_name, cu) != NULL
4076 || die->child == NULL
4077 || die->child->sibling == NULL
4078 || (die->child->sibling->sibling != NULL
4079 && die->child->sibling->sibling->tag != DW_TAG_padding))
f792889a 4080 return NULL;
c0dd20ea
DJ
4081
4082 /* Check for __pfn and __delta members. */
4083 pfn_die = die->child;
4084 pfn_name = dwarf2_attr (pfn_die, DW_AT_name, cu);
4085 if (pfn_die->tag != DW_TAG_member
4086 || pfn_name == NULL
4087 || DW_STRING (pfn_name) == NULL
4088 || strcmp ("__pfn", DW_STRING (pfn_name)) != 0)
f792889a 4089 return NULL;
c0dd20ea
DJ
4090
4091 delta_die = pfn_die->sibling;
4092 delta_name = dwarf2_attr (delta_die, DW_AT_name, cu);
4093 if (delta_die->tag != DW_TAG_member
4094 || delta_name == NULL
4095 || DW_STRING (delta_name) == NULL
4096 || strcmp ("__delta", DW_STRING (delta_name)) != 0)
f792889a 4097 return NULL;
c0dd20ea
DJ
4098
4099 /* Find the type of the method. */
4100 pfn_type = die_type (pfn_die, cu);
4101 if (pfn_type == NULL
4102 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
4103 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
f792889a 4104 return NULL;
c0dd20ea
DJ
4105
4106 /* Look for the "this" argument. */
4107 pfn_type = TYPE_TARGET_TYPE (pfn_type);
4108 if (TYPE_NFIELDS (pfn_type) == 0
4109 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
f792889a 4110 return NULL;
c0dd20ea
DJ
4111
4112 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
4113 type = alloc_type (objfile);
4114 smash_to_method_type (type, domain_type, TYPE_TARGET_TYPE (pfn_type),
4115 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
4116 TYPE_VARARGS (pfn_type));
0d5de010 4117 type = lookup_methodptr_type (type);
f792889a 4118 return set_die_type (die, type, cu);
c0dd20ea 4119}
1168df01 4120
c906108c
SS
4121/* Called when we find the DIE that starts a structure or union scope
4122 (definition) to process all dies that define the members of the
4123 structure or union.
4124
4125 NOTE: we need to call struct_type regardless of whether or not the
4126 DIE has an at_name attribute, since it might be an anonymous
4127 structure or union. This gets the type entered into our set of
4128 user defined types.
4129
4130 However, if the structure is incomplete (an opaque struct/union)
4131 then suppress creating a symbol table entry for it since gdb only
4132 wants to find the one with the complete definition. Note that if
4133 it is complete, we just call new_symbol, which does it's own
4134 checking about whether the struct/union is anonymous or not (and
4135 suppresses creating a symbol table entry itself). */
4136
f792889a 4137static struct type *
134d01f1 4138read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4139{
e7c27a73 4140 struct objfile *objfile = cu->objfile;
c906108c
SS
4141 struct type *type;
4142 struct attribute *attr;
39cbfefa 4143 char *name;
0114d602 4144 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c 4145
f792889a
DJ
4146 type = quirk_gcc_member_function_pointer (die, cu);
4147 if (type)
4148 return type;
c906108c 4149
c0dd20ea 4150 type = alloc_type (objfile);
c906108c 4151 INIT_CPLUS_SPECIFIC (type);
39cbfefa
DJ
4152 name = dwarf2_name (die, cu);
4153 if (name != NULL)
c906108c 4154 {
987504bb
JJ
4155 if (cu->language == language_cplus
4156 || cu->language == language_java)
63d06c5c 4157 {
0114d602
DJ
4158 const char *new_prefix = determine_class_name (die, cu);
4159 TYPE_TAG_NAME (type) = (char *) new_prefix;
63d06c5c
DC
4160 }
4161 else
4162 {
d8151005
DJ
4163 /* The name is already allocated along with this objfile, so
4164 we don't need to duplicate it for the type. */
39cbfefa 4165 TYPE_TAG_NAME (type) = name;
63d06c5c 4166 }
c906108c
SS
4167 }
4168
4169 if (die->tag == DW_TAG_structure_type)
4170 {
4171 TYPE_CODE (type) = TYPE_CODE_STRUCT;
4172 }
4173 else if (die->tag == DW_TAG_union_type)
4174 {
4175 TYPE_CODE (type) = TYPE_CODE_UNION;
4176 }
4177 else
4178 {
4179 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
c5aa993b 4180 in gdbtypes.h. */
c906108c
SS
4181 TYPE_CODE (type) = TYPE_CODE_CLASS;
4182 }
4183
e142c38c 4184 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4185 if (attr)
4186 {
4187 TYPE_LENGTH (type) = DW_UNSND (attr);
4188 }
4189 else
4190 {
4191 TYPE_LENGTH (type) = 0;
4192 }
4193
876cecd0 4194 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 4195 if (die_is_declaration (die, cu))
876cecd0 4196 TYPE_STUB (type) = 1;
dc718098 4197
c906108c
SS
4198 /* We need to add the type field to the die immediately so we don't
4199 infinitely recurse when dealing with pointers to the structure
4200 type within the structure itself. */
1c379e20 4201 set_die_type (die, type, cu);
c906108c 4202
e142c38c 4203 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
4204 {
4205 struct field_info fi;
4206 struct die_info *child_die;
c906108c
SS
4207
4208 memset (&fi, 0, sizeof (struct field_info));
4209
639d11d3 4210 child_die = die->child;
c906108c
SS
4211
4212 while (child_die && child_die->tag)
4213 {
a9a9bd0f
DC
4214 if (child_die->tag == DW_TAG_member
4215 || child_die->tag == DW_TAG_variable)
c906108c 4216 {
a9a9bd0f
DC
4217 /* NOTE: carlton/2002-11-05: A C++ static data member
4218 should be a DW_TAG_member that is a declaration, but
4219 all versions of G++ as of this writing (so through at
4220 least 3.2.1) incorrectly generate DW_TAG_variable
4221 tags for them instead. */
e7c27a73 4222 dwarf2_add_field (&fi, child_die, cu);
c906108c 4223 }
8713b1b1 4224 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
4225 {
4226 /* C++ member function. */
e7c27a73 4227 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
4228 }
4229 else if (child_die->tag == DW_TAG_inheritance)
4230 {
4231 /* C++ base class field. */
e7c27a73 4232 dwarf2_add_field (&fi, child_die, cu);
c906108c 4233 }
c906108c
SS
4234 child_die = sibling_die (child_die);
4235 }
4236
4237 /* Attach fields and member functions to the type. */
4238 if (fi.nfields)
e7c27a73 4239 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
4240 if (fi.nfnfields)
4241 {
e7c27a73 4242 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 4243
c5aa993b 4244 /* Get the type which refers to the base class (possibly this
c906108c
SS
4245 class itself) which contains the vtable pointer for the current
4246 class from the DW_AT_containing_type attribute. */
4247
e142c38c 4248 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 4249 {
e7c27a73 4250 struct type *t = die_containing_type (die, cu);
c906108c
SS
4251
4252 TYPE_VPTR_BASETYPE (type) = t;
4253 if (type == t)
4254 {
c906108c
SS
4255 int i;
4256
4257 /* Our own class provides vtbl ptr. */
4258 for (i = TYPE_NFIELDS (t) - 1;
4259 i >= TYPE_N_BASECLASSES (t);
4260 --i)
4261 {
4262 char *fieldname = TYPE_FIELD_NAME (t, i);
4263
1168df01 4264 if (is_vtable_name (fieldname, cu))
c906108c
SS
4265 {
4266 TYPE_VPTR_FIELDNO (type) = i;
4267 break;
4268 }
4269 }
4270
4271 /* Complain if virtual function table field not found. */
4272 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 4273 complaint (&symfile_complaints,
e2e0b3e5 4274 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
4275 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
4276 "");
c906108c
SS
4277 }
4278 else
4279 {
4280 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
4281 }
4282 }
f6235d4c
EZ
4283 else if (cu->producer
4284 && strncmp (cu->producer,
4285 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
4286 {
4287 /* The IBM XLC compiler does not provide direct indication
4288 of the containing type, but the vtable pointer is
4289 always named __vfp. */
4290
4291 int i;
4292
4293 for (i = TYPE_NFIELDS (type) - 1;
4294 i >= TYPE_N_BASECLASSES (type);
4295 --i)
4296 {
4297 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
4298 {
4299 TYPE_VPTR_FIELDNO (type) = i;
4300 TYPE_VPTR_BASETYPE (type) = type;
4301 break;
4302 }
4303 }
4304 }
c906108c 4305 }
c906108c 4306 }
63d06c5c 4307
0114d602 4308 do_cleanups (back_to);
f792889a 4309 return type;
c906108c
SS
4310}
4311
134d01f1
DJ
4312static void
4313process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
4314{
4315 struct objfile *objfile = cu->objfile;
90aeadfc 4316 struct die_info *child_die = die->child;
f792889a 4317 struct type *this_type;
c906108c 4318
f792889a
DJ
4319 this_type = get_die_type (die, cu);
4320 if (this_type == NULL)
4321 this_type = read_structure_type (die, cu);
c906108c 4322
90aeadfc
DC
4323 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
4324 snapshots) has been known to create a die giving a declaration
4325 for a class that has, as a child, a die giving a definition for a
4326 nested class. So we have to process our children even if the
4327 current die is a declaration. Normally, of course, a declaration
4328 won't have any children at all. */
134d01f1 4329
90aeadfc
DC
4330 while (child_die != NULL && child_die->tag)
4331 {
4332 if (child_die->tag == DW_TAG_member
4333 || child_die->tag == DW_TAG_variable
4334 || child_die->tag == DW_TAG_inheritance)
134d01f1 4335 {
90aeadfc 4336 /* Do nothing. */
134d01f1 4337 }
90aeadfc
DC
4338 else
4339 process_die (child_die, cu);
134d01f1 4340
90aeadfc 4341 child_die = sibling_die (child_die);
134d01f1
DJ
4342 }
4343
fa4028e9
JB
4344 /* Do not consider external references. According to the DWARF standard,
4345 these DIEs are identified by the fact that they have no byte_size
4346 attribute, and a declaration attribute. */
4347 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
4348 || !die_is_declaration (die, cu))
f792889a 4349 new_symbol (die, this_type, cu);
134d01f1
DJ
4350}
4351
4352/* Given a DW_AT_enumeration_type die, set its type. We do not
4353 complete the type's fields yet, or create any symbols. */
c906108c 4354
f792889a 4355static struct type *
134d01f1 4356read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4357{
e7c27a73 4358 struct objfile *objfile = cu->objfile;
c906108c 4359 struct type *type;
c906108c 4360 struct attribute *attr;
0114d602 4361 const char *name;
134d01f1 4362
c906108c
SS
4363 type = alloc_type (objfile);
4364
4365 TYPE_CODE (type) = TYPE_CODE_ENUM;
0114d602 4366 name = dwarf2_full_name (die, cu);
39cbfefa 4367 if (name != NULL)
0114d602 4368 TYPE_TAG_NAME (type) = (char *) name;
c906108c 4369
e142c38c 4370 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4371 if (attr)
4372 {
4373 TYPE_LENGTH (type) = DW_UNSND (attr);
4374 }
4375 else
4376 {
4377 TYPE_LENGTH (type) = 0;
4378 }
4379
137033e9
JB
4380 /* The enumeration DIE can be incomplete. In Ada, any type can be
4381 declared as private in the package spec, and then defined only
4382 inside the package body. Such types are known as Taft Amendment
4383 Types. When another package uses such a type, an incomplete DIE
4384 may be generated by the compiler. */
02eb380e 4385 if (die_is_declaration (die, cu))
876cecd0 4386 TYPE_STUB (type) = 1;
02eb380e 4387
f792889a 4388 return set_die_type (die, type, cu);
134d01f1
DJ
4389}
4390
8176b9b8 4391/* Determine the name of the type represented by DIE, which should be
0114d602
DJ
4392 a named C++ or Java compound type. Return the name in question,
4393 allocated on the objfile obstack. */
8176b9b8 4394
0114d602 4395static const char *
8176b9b8
DC
4396determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
4397{
0114d602 4398 const char *new_prefix = NULL;
8176b9b8
DC
4399
4400 /* If we don't have namespace debug info, guess the name by trying
4401 to demangle the names of members, just like we did in
72bf9492 4402 guess_structure_name. */
8176b9b8
DC
4403 if (!processing_has_namespace_info)
4404 {
4405 struct die_info *child;
4406
4407 for (child = die->child;
4408 child != NULL && child->tag != 0;
4409 child = sibling_die (child))
4410 {
4411 if (child->tag == DW_TAG_subprogram)
4412 {
0114d602 4413 char *phys_prefix
31c27f77
JJ
4414 = language_class_name_from_physname (cu->language_defn,
4415 dwarf2_linkage_name
8176b9b8
DC
4416 (child, cu));
4417
0114d602
DJ
4418 if (phys_prefix != NULL)
4419 {
4420 new_prefix
4421 = obsavestring (phys_prefix, strlen (phys_prefix),
4422 &cu->objfile->objfile_obstack);
4423 xfree (phys_prefix);
4424 break;
4425 }
8176b9b8
DC
4426 }
4427 }
4428 }
4429
4430 if (new_prefix == NULL)
0114d602 4431 new_prefix = dwarf2_full_name (die, cu);
8176b9b8
DC
4432
4433 return new_prefix;
4434}
4435
134d01f1
DJ
4436/* Given a pointer to a die which begins an enumeration, process all
4437 the dies that define the members of the enumeration, and create the
4438 symbol for the enumeration type.
4439
4440 NOTE: We reverse the order of the element list. */
4441
4442static void
4443process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
4444{
4445 struct objfile *objfile = cu->objfile;
4446 struct die_info *child_die;
4447 struct field *fields;
134d01f1
DJ
4448 struct symbol *sym;
4449 int num_fields;
4450 int unsigned_enum = 1;
39cbfefa 4451 char *name;
f792889a 4452 struct type *this_type;
134d01f1 4453
c906108c
SS
4454 num_fields = 0;
4455 fields = NULL;
f792889a
DJ
4456 this_type = get_die_type (die, cu);
4457 if (this_type == NULL)
4458 this_type = read_enumeration_type (die, cu);
639d11d3 4459 if (die->child != NULL)
c906108c 4460 {
639d11d3 4461 child_die = die->child;
c906108c
SS
4462 while (child_die && child_die->tag)
4463 {
4464 if (child_die->tag != DW_TAG_enumerator)
4465 {
e7c27a73 4466 process_die (child_die, cu);
c906108c
SS
4467 }
4468 else
4469 {
39cbfefa
DJ
4470 name = dwarf2_name (child_die, cu);
4471 if (name)
c906108c 4472 {
f792889a 4473 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
4474 if (SYMBOL_VALUE (sym) < 0)
4475 unsigned_enum = 0;
4476
4477 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
4478 {
4479 fields = (struct field *)
4480 xrealloc (fields,
4481 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 4482 * sizeof (struct field));
c906108c
SS
4483 }
4484
3567439c 4485 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 4486 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 4487 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
4488 FIELD_BITSIZE (fields[num_fields]) = 0;
4489
4490 num_fields++;
4491 }
4492 }
4493
4494 child_die = sibling_die (child_die);
4495 }
4496
4497 if (num_fields)
4498 {
f792889a
DJ
4499 TYPE_NFIELDS (this_type) = num_fields;
4500 TYPE_FIELDS (this_type) = (struct field *)
4501 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
4502 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 4503 sizeof (struct field) * num_fields);
b8c9b27d 4504 xfree (fields);
c906108c
SS
4505 }
4506 if (unsigned_enum)
876cecd0 4507 TYPE_UNSIGNED (this_type) = 1;
c906108c 4508 }
134d01f1 4509
f792889a 4510 new_symbol (die, this_type, cu);
c906108c
SS
4511}
4512
4513/* Extract all information from a DW_TAG_array_type DIE and put it in
4514 the DIE's type field. For now, this only handles one dimensional
4515 arrays. */
4516
f792889a 4517static struct type *
e7c27a73 4518read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4519{
e7c27a73 4520 struct objfile *objfile = cu->objfile;
c906108c
SS
4521 struct die_info *child_die;
4522 struct type *type = NULL;
4523 struct type *element_type, *range_type, *index_type;
4524 struct type **range_types = NULL;
4525 struct attribute *attr;
4526 int ndim = 0;
4527 struct cleanup *back_to;
39cbfefa 4528 char *name;
c906108c 4529
e7c27a73 4530 element_type = die_type (die, cu);
c906108c
SS
4531
4532 /* Irix 6.2 native cc creates array types without children for
4533 arrays with unspecified length. */
639d11d3 4534 if (die->child == NULL)
c906108c 4535 {
6ccb9162 4536 index_type = builtin_type_int32;
c906108c 4537 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
4538 type = create_array_type (NULL, element_type, range_type);
4539 return set_die_type (die, type, cu);
c906108c
SS
4540 }
4541
4542 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 4543 child_die = die->child;
c906108c
SS
4544 while (child_die && child_die->tag)
4545 {
4546 if (child_die->tag == DW_TAG_subrange_type)
4547 {
f792889a
DJ
4548 struct type *child_type = read_type_die (child_die, cu);
4549 if (child_type != NULL)
a02abb62
JB
4550 {
4551 /* The range type was succesfully read. Save it for
4552 the array type creation. */
4553 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
4554 {
4555 range_types = (struct type **)
4556 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
4557 * sizeof (struct type *));
4558 if (ndim == 0)
4559 make_cleanup (free_current_contents, &range_types);
4560 }
f792889a 4561 range_types[ndim++] = child_type;
a02abb62 4562 }
c906108c
SS
4563 }
4564 child_die = sibling_die (child_die);
4565 }
4566
4567 /* Dwarf2 dimensions are output from left to right, create the
4568 necessary array types in backwards order. */
7ca2d3a3 4569
c906108c 4570 type = element_type;
7ca2d3a3
DL
4571
4572 if (read_array_order (die, cu) == DW_ORD_col_major)
4573 {
4574 int i = 0;
4575 while (i < ndim)
4576 type = create_array_type (NULL, type, range_types[i++]);
4577 }
4578 else
4579 {
4580 while (ndim-- > 0)
4581 type = create_array_type (NULL, type, range_types[ndim]);
4582 }
c906108c 4583
f5f8a009
EZ
4584 /* Understand Dwarf2 support for vector types (like they occur on
4585 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
4586 array type. This is not part of the Dwarf2/3 standard yet, but a
4587 custom vendor extension. The main difference between a regular
4588 array and the vector variant is that vectors are passed by value
4589 to functions. */
e142c38c 4590 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 4591 if (attr)
ea37ba09 4592 make_vector_type (type);
f5f8a009 4593
39cbfefa
DJ
4594 name = dwarf2_name (die, cu);
4595 if (name)
4596 TYPE_NAME (type) = name;
714e295e 4597
c906108c
SS
4598 do_cleanups (back_to);
4599
4600 /* Install the type in the die. */
f792889a 4601 return set_die_type (die, type, cu);
c906108c
SS
4602}
4603
7ca2d3a3
DL
4604static enum dwarf_array_dim_ordering
4605read_array_order (struct die_info *die, struct dwarf2_cu *cu)
4606{
4607 struct attribute *attr;
4608
4609 attr = dwarf2_attr (die, DW_AT_ordering, cu);
4610
4611 if (attr) return DW_SND (attr);
4612
4613 /*
4614 GNU F77 is a special case, as at 08/2004 array type info is the
4615 opposite order to the dwarf2 specification, but data is still
4616 laid out as per normal fortran.
4617
4618 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
4619 version checking.
4620 */
4621
4622 if (cu->language == language_fortran &&
4623 cu->producer && strstr (cu->producer, "GNU F77"))
4624 {
4625 return DW_ORD_row_major;
4626 }
4627
4628 switch (cu->language_defn->la_array_ordering)
4629 {
4630 case array_column_major:
4631 return DW_ORD_col_major;
4632 case array_row_major:
4633 default:
4634 return DW_ORD_row_major;
4635 };
4636}
4637
72019c9c
GM
4638/* Extract all information from a DW_TAG_set_type DIE and put it in
4639 the DIE's type field. */
4640
f792889a 4641static struct type *
72019c9c
GM
4642read_set_type (struct die_info *die, struct dwarf2_cu *cu)
4643{
f792889a
DJ
4644 struct type *set_type = create_set_type (NULL, die_type (die, cu));
4645
4646 return set_die_type (die, set_type, cu);
72019c9c 4647}
7ca2d3a3 4648
c906108c
SS
4649/* First cut: install each common block member as a global variable. */
4650
4651static void
e7c27a73 4652read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4653{
4654 struct die_info *child_die;
4655 struct attribute *attr;
4656 struct symbol *sym;
4657 CORE_ADDR base = (CORE_ADDR) 0;
4658
e142c38c 4659 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
4660 if (attr)
4661 {
8e19ed76
PS
4662 /* Support the .debug_loc offsets */
4663 if (attr_form_is_block (attr))
4664 {
e7c27a73 4665 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 4666 }
3690dd37 4667 else if (attr_form_is_section_offset (attr))
8e19ed76 4668 {
4d3c2250 4669 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4670 }
4671 else
4672 {
4d3c2250
KB
4673 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4674 "common block member");
8e19ed76 4675 }
c906108c 4676 }
639d11d3 4677 if (die->child != NULL)
c906108c 4678 {
639d11d3 4679 child_die = die->child;
c906108c
SS
4680 while (child_die && child_die->tag)
4681 {
e7c27a73 4682 sym = new_symbol (child_die, NULL, cu);
e142c38c 4683 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
4684 if (attr)
4685 {
4686 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 4687 base + decode_locdesc (DW_BLOCK (attr), cu);
c906108c
SS
4688 add_symbol_to_list (sym, &global_symbols);
4689 }
4690 child_die = sibling_die (child_die);
4691 }
4692 }
4693}
4694
0114d602 4695/* Create a type for a C++ namespace. */
d9fa45fe 4696
0114d602
DJ
4697static struct type *
4698read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 4699{
e7c27a73 4700 struct objfile *objfile = cu->objfile;
0114d602 4701 const char *previous_prefix, *name;
9219021c 4702 int is_anonymous;
0114d602
DJ
4703 struct type *type;
4704
4705 /* For extensions, reuse the type of the original namespace. */
4706 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
4707 {
4708 struct die_info *ext_die;
4709 struct dwarf2_cu *ext_cu = cu;
4710 ext_die = dwarf2_extension (die, &ext_cu);
4711 type = read_type_die (ext_die, ext_cu);
4712 return set_die_type (die, type, cu);
4713 }
9219021c 4714
e142c38c 4715 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
4716
4717 /* Now build the name of the current namespace. */
4718
0114d602
DJ
4719 previous_prefix = determine_prefix (die, cu);
4720 if (previous_prefix[0] != '\0')
4721 name = typename_concat (&objfile->objfile_obstack,
4722 previous_prefix, name, cu);
4723
4724 /* Create the type. */
4725 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
4726 objfile);
4727 TYPE_NAME (type) = (char *) name;
4728 TYPE_TAG_NAME (type) = TYPE_NAME (type);
4729
4730 set_die_type (die, type, cu);
4731
4732 return type;
4733}
4734
4735/* Read a C++ namespace. */
4736
4737static void
4738read_namespace (struct die_info *die, struct dwarf2_cu *cu)
4739{
4740 struct objfile *objfile = cu->objfile;
4741 const char *name;
4742 int is_anonymous;
9219021c 4743
5c4e30ca
DC
4744 /* Add a symbol associated to this if we haven't seen the namespace
4745 before. Also, add a using directive if it's an anonymous
4746 namespace. */
9219021c 4747
f2f0e013 4748 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
4749 {
4750 struct type *type;
4751
0114d602 4752 type = read_type_die (die, cu);
e7c27a73 4753 new_symbol (die, type, cu);
5c4e30ca 4754
0114d602 4755 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 4756 if (is_anonymous)
0114d602
DJ
4757 {
4758 const char *previous_prefix = determine_prefix (die, cu);
4759 cp_add_using_directive (TYPE_NAME (type),
4760 strlen (previous_prefix),
4761 strlen (TYPE_NAME (type)));
4762 }
5c4e30ca 4763 }
9219021c 4764
639d11d3 4765 if (die->child != NULL)
d9fa45fe 4766 {
639d11d3 4767 struct die_info *child_die = die->child;
d9fa45fe
DC
4768
4769 while (child_die && child_die->tag)
4770 {
e7c27a73 4771 process_die (child_die, cu);
d9fa45fe
DC
4772 child_die = sibling_die (child_die);
4773 }
4774 }
38d518c9
EZ
4775}
4776
5d7cb8df
JK
4777/* Read a Fortran module. */
4778
4779static void
4780read_module (struct die_info *die, struct dwarf2_cu *cu)
4781{
4782 struct die_info *child_die = die->child;
4783
4784 /* FIXME: Support the separate Fortran module namespaces. */
4785
4786 while (child_die && child_die->tag)
4787 {
4788 process_die (child_die, cu);
4789 child_die = sibling_die (child_die);
4790 }
4791}
4792
38d518c9
EZ
4793/* Return the name of the namespace represented by DIE. Set
4794 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
4795 namespace. */
4796
4797static const char *
e142c38c 4798namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
4799{
4800 struct die_info *current_die;
4801 const char *name = NULL;
4802
4803 /* Loop through the extensions until we find a name. */
4804
4805 for (current_die = die;
4806 current_die != NULL;
f2f0e013 4807 current_die = dwarf2_extension (die, &cu))
38d518c9 4808 {
e142c38c 4809 name = dwarf2_name (current_die, cu);
38d518c9
EZ
4810 if (name != NULL)
4811 break;
4812 }
4813
4814 /* Is it an anonymous namespace? */
4815
4816 *is_anonymous = (name == NULL);
4817 if (*is_anonymous)
4818 name = "(anonymous namespace)";
4819
4820 return name;
d9fa45fe
DC
4821}
4822
c906108c
SS
4823/* Extract all information from a DW_TAG_pointer_type DIE and add to
4824 the user defined type vector. */
4825
f792889a 4826static struct type *
e7c27a73 4827read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4828{
5e2b427d 4829 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 4830 struct comp_unit_head *cu_header = &cu->header;
c906108c 4831 struct type *type;
8b2dbe47
KB
4832 struct attribute *attr_byte_size;
4833 struct attribute *attr_address_class;
4834 int byte_size, addr_class;
c906108c 4835
e7c27a73 4836 type = lookup_pointer_type (die_type (die, cu));
8b2dbe47 4837
e142c38c 4838 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
4839 if (attr_byte_size)
4840 byte_size = DW_UNSND (attr_byte_size);
c906108c 4841 else
8b2dbe47
KB
4842 byte_size = cu_header->addr_size;
4843
e142c38c 4844 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
4845 if (attr_address_class)
4846 addr_class = DW_UNSND (attr_address_class);
4847 else
4848 addr_class = DW_ADDR_none;
4849
4850 /* If the pointer size or address class is different than the
4851 default, create a type variant marked as such and set the
4852 length accordingly. */
4853 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 4854 {
5e2b427d 4855 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
4856 {
4857 int type_flags;
4858
849957d9 4859 type_flags = gdbarch_address_class_type_flags
5e2b427d 4860 (gdbarch, byte_size, addr_class);
876cecd0
TT
4861 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
4862 == 0);
8b2dbe47
KB
4863 type = make_type_with_address_space (type, type_flags);
4864 }
4865 else if (TYPE_LENGTH (type) != byte_size)
4866 {
e2e0b3e5 4867 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47
KB
4868 }
4869 else {
4870 /* Should we also complain about unhandled address classes? */
4871 }
c906108c 4872 }
8b2dbe47
KB
4873
4874 TYPE_LENGTH (type) = byte_size;
f792889a 4875 return set_die_type (die, type, cu);
c906108c
SS
4876}
4877
4878/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
4879 the user defined type vector. */
4880
f792889a 4881static struct type *
e7c27a73 4882read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4883{
e7c27a73 4884 struct objfile *objfile = cu->objfile;
c906108c
SS
4885 struct type *type;
4886 struct type *to_type;
4887 struct type *domain;
4888
e7c27a73
DJ
4889 to_type = die_type (die, cu);
4890 domain = die_containing_type (die, cu);
0d5de010
DJ
4891
4892 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
4893 type = lookup_methodptr_type (to_type);
4894 else
4895 type = lookup_memberptr_type (to_type, domain);
c906108c 4896
f792889a 4897 return set_die_type (die, type, cu);
c906108c
SS
4898}
4899
4900/* Extract all information from a DW_TAG_reference_type DIE and add to
4901 the user defined type vector. */
4902
f792889a 4903static struct type *
e7c27a73 4904read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4905{
e7c27a73 4906 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
4907 struct type *type;
4908 struct attribute *attr;
4909
e7c27a73 4910 type = lookup_reference_type (die_type (die, cu));
e142c38c 4911 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4912 if (attr)
4913 {
4914 TYPE_LENGTH (type) = DW_UNSND (attr);
4915 }
4916 else
4917 {
107d2387 4918 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 4919 }
f792889a 4920 return set_die_type (die, type, cu);
c906108c
SS
4921}
4922
f792889a 4923static struct type *
e7c27a73 4924read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4925{
f792889a 4926 struct type *base_type, *cv_type;
c906108c 4927
e7c27a73 4928 base_type = die_type (die, cu);
f792889a
DJ
4929 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
4930 return set_die_type (die, cv_type, cu);
c906108c
SS
4931}
4932
f792889a 4933static struct type *
e7c27a73 4934read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4935{
f792889a 4936 struct type *base_type, *cv_type;
c906108c 4937
e7c27a73 4938 base_type = die_type (die, cu);
f792889a
DJ
4939 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
4940 return set_die_type (die, cv_type, cu);
c906108c
SS
4941}
4942
4943/* Extract all information from a DW_TAG_string_type DIE and add to
4944 the user defined type vector. It isn't really a user defined type,
4945 but it behaves like one, with other DIE's using an AT_user_def_type
4946 attribute to reference it. */
4947
f792889a 4948static struct type *
e7c27a73 4949read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4950{
e7c27a73 4951 struct objfile *objfile = cu->objfile;
c906108c
SS
4952 struct type *type, *range_type, *index_type, *char_type;
4953 struct attribute *attr;
4954 unsigned int length;
4955
e142c38c 4956 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
4957 if (attr)
4958 {
4959 length = DW_UNSND (attr);
4960 }
4961 else
4962 {
b21b22e0 4963 /* check for the DW_AT_byte_size attribute */
e142c38c 4964 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
4965 if (attr)
4966 {
4967 length = DW_UNSND (attr);
4968 }
4969 else
4970 {
4971 length = 1;
4972 }
c906108c 4973 }
6ccb9162
UW
4974
4975 index_type = builtin_type_int32;
c906108c 4976 range_type = create_range_type (NULL, index_type, 1, length);
6ccb9162
UW
4977 type = create_string_type (NULL, range_type);
4978
f792889a 4979 return set_die_type (die, type, cu);
c906108c
SS
4980}
4981
4982/* Handle DIES due to C code like:
4983
4984 struct foo
c5aa993b
JM
4985 {
4986 int (*funcp)(int a, long l);
4987 int b;
4988 };
c906108c
SS
4989
4990 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 4991 */
c906108c 4992
f792889a 4993static struct type *
e7c27a73 4994read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4995{
4996 struct type *type; /* Type that this function returns */
4997 struct type *ftype; /* Function that returns above type */
4998 struct attribute *attr;
4999
e7c27a73 5000 type = die_type (die, cu);
1326e61b 5001 ftype = make_function_type (type, (struct type **) 0);
c906108c 5002
5b8101ae 5003 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 5004 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 5005 if ((attr && (DW_UNSND (attr) != 0))
987504bb 5006 || cu->language == language_cplus
5b8101ae
PM
5007 || cu->language == language_java
5008 || cu->language == language_pascal)
876cecd0 5009 TYPE_PROTOTYPED (ftype) = 1;
c906108c 5010
c055b101
CV
5011 /* Store the calling convention in the type if it's available in
5012 the subroutine die. Otherwise set the calling convention to
5013 the default value DW_CC_normal. */
5014 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
5015 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
5016
639d11d3 5017 if (die->child != NULL)
c906108c
SS
5018 {
5019 struct die_info *child_die;
5020 int nparams = 0;
5021 int iparams = 0;
5022
5023 /* Count the number of parameters.
5024 FIXME: GDB currently ignores vararg functions, but knows about
5025 vararg member functions. */
639d11d3 5026 child_die = die->child;
c906108c
SS
5027 while (child_die && child_die->tag)
5028 {
5029 if (child_die->tag == DW_TAG_formal_parameter)
5030 nparams++;
5031 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 5032 TYPE_VARARGS (ftype) = 1;
c906108c
SS
5033 child_die = sibling_die (child_die);
5034 }
5035
5036 /* Allocate storage for parameters and fill them in. */
5037 TYPE_NFIELDS (ftype) = nparams;
5038 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 5039 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 5040
639d11d3 5041 child_die = die->child;
c906108c
SS
5042 while (child_die && child_die->tag)
5043 {
5044 if (child_die->tag == DW_TAG_formal_parameter)
5045 {
5046 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
5047 member functions. G++ helps GDB by marking the first
5048 parameter for non-static member functions (which is the
5049 this pointer) as artificial. We pass this information
5050 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 5051 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
5052 if (attr)
5053 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
5054 else
5055 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
e7c27a73 5056 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
5057 iparams++;
5058 }
5059 child_die = sibling_die (child_die);
5060 }
5061 }
5062
f792889a 5063 return set_die_type (die, ftype, cu);
c906108c
SS
5064}
5065
f792889a 5066static struct type *
e7c27a73 5067read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5068{
e7c27a73 5069 struct objfile *objfile = cu->objfile;
2f038fcb 5070 struct attribute *attr;
0114d602 5071 const char *name = NULL;
f792889a 5072 struct type *this_type;
c906108c 5073
0114d602 5074 name = dwarf2_full_name (die, cu);
f792889a 5075 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
5076 TYPE_FLAG_TARGET_STUB, NULL, objfile);
5077 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
5078 set_die_type (die, this_type, cu);
5079 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
5080 return this_type;
c906108c
SS
5081}
5082
5083/* Find a representation of a given base type and install
5084 it in the TYPE field of the die. */
5085
f792889a 5086static struct type *
e7c27a73 5087read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5088{
e7c27a73 5089 struct objfile *objfile = cu->objfile;
c906108c
SS
5090 struct type *type;
5091 struct attribute *attr;
5092 int encoding = 0, size = 0;
39cbfefa 5093 char *name;
6ccb9162
UW
5094 enum type_code code = TYPE_CODE_INT;
5095 int type_flags = 0;
5096 struct type *target_type = NULL;
c906108c 5097
e142c38c 5098 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
5099 if (attr)
5100 {
5101 encoding = DW_UNSND (attr);
5102 }
e142c38c 5103 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5104 if (attr)
5105 {
5106 size = DW_UNSND (attr);
5107 }
39cbfefa 5108 name = dwarf2_name (die, cu);
6ccb9162 5109 if (!name)
c906108c 5110 {
6ccb9162
UW
5111 complaint (&symfile_complaints,
5112 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 5113 }
6ccb9162
UW
5114
5115 switch (encoding)
c906108c 5116 {
6ccb9162
UW
5117 case DW_ATE_address:
5118 /* Turn DW_ATE_address into a void * pointer. */
5119 code = TYPE_CODE_PTR;
5120 type_flags |= TYPE_FLAG_UNSIGNED;
5121 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
5122 break;
5123 case DW_ATE_boolean:
5124 code = TYPE_CODE_BOOL;
5125 type_flags |= TYPE_FLAG_UNSIGNED;
5126 break;
5127 case DW_ATE_complex_float:
5128 code = TYPE_CODE_COMPLEX;
5129 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
5130 break;
5131 case DW_ATE_decimal_float:
5132 code = TYPE_CODE_DECFLOAT;
5133 break;
5134 case DW_ATE_float:
5135 code = TYPE_CODE_FLT;
5136 break;
5137 case DW_ATE_signed:
5138 break;
5139 case DW_ATE_unsigned:
5140 type_flags |= TYPE_FLAG_UNSIGNED;
5141 break;
5142 case DW_ATE_signed_char:
868a0084
PM
5143 if (cu->language == language_ada || cu->language == language_m2
5144 || cu->language == language_pascal)
6ccb9162
UW
5145 code = TYPE_CODE_CHAR;
5146 break;
5147 case DW_ATE_unsigned_char:
868a0084
PM
5148 if (cu->language == language_ada || cu->language == language_m2
5149 || cu->language == language_pascal)
6ccb9162
UW
5150 code = TYPE_CODE_CHAR;
5151 type_flags |= TYPE_FLAG_UNSIGNED;
5152 break;
5153 default:
5154 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
5155 dwarf_type_encoding_name (encoding));
5156 break;
c906108c 5157 }
6ccb9162 5158
0114d602
DJ
5159 type = init_type (code, size, type_flags, NULL, objfile);
5160 TYPE_NAME (type) = name;
6ccb9162
UW
5161 TYPE_TARGET_TYPE (type) = target_type;
5162
0114d602 5163 if (name && strcmp (name, "char") == 0)
876cecd0 5164 TYPE_NOSIGN (type) = 1;
0114d602 5165
f792889a 5166 return set_die_type (die, type, cu);
c906108c
SS
5167}
5168
a02abb62
JB
5169/* Read the given DW_AT_subrange DIE. */
5170
f792889a 5171static struct type *
a02abb62
JB
5172read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
5173{
5e2b427d 5174 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
5175 struct type *base_type;
5176 struct type *range_type;
5177 struct attribute *attr;
5178 int low = 0;
5179 int high = -1;
39cbfefa 5180 char *name;
a02abb62 5181
a02abb62 5182 base_type = die_type (die, cu);
3d1f72c2 5183 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
a02abb62
JB
5184 {
5185 complaint (&symfile_complaints,
e2e0b3e5 5186 _("DW_AT_type missing from DW_TAG_subrange_type"));
17a912b6 5187 base_type
5e2b427d 5188 = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6ccb9162 5189 0, NULL, cu->objfile);
a02abb62
JB
5190 }
5191
e142c38c 5192 if (cu->language == language_fortran)
a02abb62
JB
5193 {
5194 /* FORTRAN implies a lower bound of 1, if not given. */
5195 low = 1;
5196 }
5197
dd5e6932
DJ
5198 /* FIXME: For variable sized arrays either of these could be
5199 a variable rather than a constant value. We'll allow it,
5200 but we don't know how to handle it. */
e142c38c 5201 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
5202 if (attr)
5203 low = dwarf2_get_attr_constant_value (attr, 0);
5204
e142c38c 5205 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62
JB
5206 if (attr)
5207 {
5208 if (attr->form == DW_FORM_block1)
5209 {
5210 /* GCC encodes arrays with unspecified or dynamic length
5211 with a DW_FORM_block1 attribute.
5212 FIXME: GDB does not yet know how to handle dynamic
5213 arrays properly, treat them as arrays with unspecified
5214 length for now.
5215
5216 FIXME: jimb/2003-09-22: GDB does not really know
5217 how to handle arrays of unspecified length
5218 either; we just represent them as zero-length
5219 arrays. Choose an appropriate upper bound given
5220 the lower bound we've computed above. */
5221 high = low - 1;
5222 }
5223 else
5224 high = dwarf2_get_attr_constant_value (attr, 1);
5225 }
5226
5227 range_type = create_range_type (NULL, base_type, low, high);
5228
39cbfefa
DJ
5229 name = dwarf2_name (die, cu);
5230 if (name)
5231 TYPE_NAME (range_type) = name;
a02abb62 5232
e142c38c 5233 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
5234 if (attr)
5235 TYPE_LENGTH (range_type) = DW_UNSND (attr);
5236
f792889a 5237 return set_die_type (die, range_type, cu);
a02abb62
JB
5238}
5239
f792889a 5240static struct type *
81a17f79
JB
5241read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
5242{
5243 struct type *type;
81a17f79 5244
81a17f79
JB
5245 /* For now, we only support the C meaning of an unspecified type: void. */
5246
0114d602
DJ
5247 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
5248 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 5249
f792889a 5250 return set_die_type (die, type, cu);
81a17f79 5251}
a02abb62 5252
51545339
DJ
5253/* Trivial hash function for die_info: the hash value of a DIE
5254 is its offset in .debug_info for this objfile. */
5255
5256static hashval_t
5257die_hash (const void *item)
5258{
5259 const struct die_info *die = item;
5260 return die->offset;
5261}
5262
5263/* Trivial comparison function for die_info structures: two DIEs
5264 are equal if they have the same offset. */
5265
5266static int
5267die_eq (const void *item_lhs, const void *item_rhs)
5268{
5269 const struct die_info *die_lhs = item_lhs;
5270 const struct die_info *die_rhs = item_rhs;
5271 return die_lhs->offset == die_rhs->offset;
5272}
5273
c906108c
SS
5274/* Read a whole compilation unit into a linked list of dies. */
5275
f9aca02d 5276static struct die_info *
fe1b8b76 5277read_comp_unit (gdb_byte *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
c906108c 5278{
51545339
DJ
5279 cu->die_hash
5280 = htab_create_alloc_ex (cu->header.length / 12,
5281 die_hash,
5282 die_eq,
5283 NULL,
5284 &cu->comp_unit_obstack,
5285 hashtab_obstack_allocate,
5286 dummy_obstack_deallocate);
5287
e7c27a73 5288 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
639d11d3
DC
5289}
5290
d97bc12b
DE
5291/* Main entry point for reading a DIE and all children.
5292 Read the DIE and dump it if requested. */
5293
5294static struct die_info *
5295read_die_and_children (gdb_byte *info_ptr, bfd *abfd,
5296 struct dwarf2_cu *cu,
5297 gdb_byte **new_info_ptr,
5298 struct die_info *parent)
5299{
5300 struct die_info *result = read_die_and_children_1 (info_ptr, abfd, cu,
5301 new_info_ptr, parent);
5302
5303 if (dwarf2_die_debug)
5304 {
5305 fprintf_unfiltered (gdb_stdlog, "Read die from .debug_info:\n");
5306 dump_die (result, dwarf2_die_debug);
5307 }
5308
5309 return result;
5310}
5311
639d11d3
DC
5312/* Read a single die and all its descendents. Set the die's sibling
5313 field to NULL; set other fields in the die correctly, and set all
5314 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
5315 location of the info_ptr after reading all of those dies. PARENT
5316 is the parent of the die in question. */
5317
5318static struct die_info *
d97bc12b
DE
5319read_die_and_children_1 (gdb_byte *info_ptr, bfd *abfd,
5320 struct dwarf2_cu *cu,
5321 gdb_byte **new_info_ptr,
5322 struct die_info *parent)
639d11d3
DC
5323{
5324 struct die_info *die;
fe1b8b76 5325 gdb_byte *cur_ptr;
639d11d3
DC
5326 int has_children;
5327
e7c27a73 5328 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
1d325ec1
DJ
5329 if (die == NULL)
5330 {
5331 *new_info_ptr = cur_ptr;
5332 return NULL;
5333 }
51545339 5334 store_in_ref_table (die, cu);
639d11d3
DC
5335
5336 if (has_children)
5337 {
e7c27a73 5338 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
639d11d3
DC
5339 new_info_ptr, die);
5340 }
5341 else
5342 {
5343 die->child = NULL;
5344 *new_info_ptr = cur_ptr;
5345 }
5346
5347 die->sibling = NULL;
5348 die->parent = parent;
5349 return die;
5350}
5351
5352/* Read a die, all of its descendents, and all of its siblings; set
5353 all of the fields of all of the dies correctly. Arguments are as
5354 in read_die_and_children. */
5355
5356static struct die_info *
fe1b8b76 5357read_die_and_siblings (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 5358 struct dwarf2_cu *cu,
fe1b8b76 5359 gdb_byte **new_info_ptr,
639d11d3
DC
5360 struct die_info *parent)
5361{
5362 struct die_info *first_die, *last_sibling;
fe1b8b76 5363 gdb_byte *cur_ptr;
639d11d3 5364
c906108c 5365 cur_ptr = info_ptr;
639d11d3
DC
5366 first_die = last_sibling = NULL;
5367
5368 while (1)
c906108c 5369 {
639d11d3 5370 struct die_info *die
d97bc12b 5371 = read_die_and_children_1 (cur_ptr, abfd, cu, &cur_ptr, parent);
639d11d3 5372
1d325ec1 5373 if (die == NULL)
c906108c 5374 {
639d11d3
DC
5375 *new_info_ptr = cur_ptr;
5376 return first_die;
c906108c 5377 }
1d325ec1
DJ
5378
5379 if (!first_die)
5380 first_die = die;
c906108c 5381 else
1d325ec1
DJ
5382 last_sibling->sibling = die;
5383
5384 last_sibling = die;
c906108c 5385 }
c906108c
SS
5386}
5387
233a11ab
CS
5388/* Decompress a section that was compressed using zlib. Store the
5389 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
5390
5391static void
5392zlib_decompress_section (struct objfile *objfile, asection *sectp,
5393 gdb_byte **outbuf, bfd_size_type *outsize)
5394{
ef72380d 5395 bfd *abfd = objfile->obfd;
233a11ab
CS
5396#ifndef HAVE_ZLIB_H
5397 error (_("Support for zlib-compressed DWARF data (from '%s') "
5398 "is disabled in this copy of GDB"),
5399 bfd_get_filename (abfd));
5400#else
233a11ab
CS
5401 bfd_size_type compressed_size = bfd_get_section_size (sectp);
5402 gdb_byte *compressed_buffer = xmalloc (compressed_size);
5403 bfd_size_type uncompressed_size;
5404 gdb_byte *uncompressed_buffer;
5405 z_stream strm;
5406 int rc;
5407 int header_size = 12;
5408
5409 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5410 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
5411 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
5412 bfd_get_filename (abfd));
5413
5414 /* Read the zlib header. In this case, it should be "ZLIB" followed
5415 by the uncompressed section size, 8 bytes in big-endian order. */
5416 if (compressed_size < header_size
5417 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
5418 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
5419 bfd_get_filename (abfd));
5420 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
5421 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
5422 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
5423 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
5424 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
5425 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
5426 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
5427 uncompressed_size += compressed_buffer[11];
5428
5429 /* It is possible the section consists of several compressed
5430 buffers concatenated together, so we uncompress in a loop. */
5431 strm.zalloc = NULL;
5432 strm.zfree = NULL;
5433 strm.opaque = NULL;
5434 strm.avail_in = compressed_size - header_size;
5435 strm.next_in = (Bytef*) compressed_buffer + header_size;
5436 strm.avail_out = uncompressed_size;
5437 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
5438 uncompressed_size);
5439 rc = inflateInit (&strm);
5440 while (strm.avail_in > 0)
5441 {
5442 if (rc != Z_OK)
5443 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
5444 bfd_get_filename (abfd), rc);
5445 strm.next_out = ((Bytef*) uncompressed_buffer
5446 + (uncompressed_size - strm.avail_out));
5447 rc = inflate (&strm, Z_FINISH);
5448 if (rc != Z_STREAM_END)
5449 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
5450 bfd_get_filename (abfd), rc);
5451 rc = inflateReset (&strm);
5452 }
5453 rc = inflateEnd (&strm);
5454 if (rc != Z_OK
5455 || strm.avail_out != 0)
5456 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
5457 bfd_get_filename (abfd), rc);
5458
5459 xfree (compressed_buffer);
5460 *outbuf = uncompressed_buffer;
5461 *outsize = uncompressed_size;
5462#endif
5463}
5464
5465
c906108c 5466/* Read the contents of the section at OFFSET and of size SIZE from the
233a11ab
CS
5467 object file specified by OBJFILE into the objfile_obstack and return it.
5468 If the section is compressed, uncompress it before returning. */
c906108c 5469
fe1b8b76 5470gdb_byte *
188dd5d6 5471dwarf2_read_section (struct objfile *objfile, asection *sectp)
c906108c
SS
5472{
5473 bfd *abfd = objfile->obfd;
fe1b8b76 5474 gdb_byte *buf, *retbuf;
2c500098 5475 bfd_size_type size = bfd_get_section_size (sectp);
233a11ab 5476 unsigned char header[4];
c906108c
SS
5477
5478 if (size == 0)
5479 return NULL;
5480
233a11ab
CS
5481 /* Check if the file has a 4-byte header indicating compression. */
5482 if (size > sizeof (header)
5483 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
5484 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
5485 {
5486 /* Upon decompression, update the buffer and its size. */
5487 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
5488 {
5489 zlib_decompress_section (objfile, sectp, &buf, &size);
5490 dwarf2_resize_section (sectp, size);
5491 return buf;
5492 }
5493 }
5494
5495 /* If we get here, we are a normal, not-compressed section. */
fe1b8b76 5496 buf = obstack_alloc (&objfile->objfile_obstack, size);
233a11ab
CS
5497 /* When debugging .o files, we may need to apply relocations; see
5498 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
5499 We never compress sections in .o files, so we only need to
5500 try this when the section is not compressed. */
fe1b8b76 5501 retbuf = symfile_relocate_debug_section (abfd, sectp, buf);
086df311
DJ
5502 if (retbuf != NULL)
5503 return retbuf;
5504
188dd5d6
DJ
5505 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5506 || bfd_bread (buf, size, abfd) != size)
8a3fe4f8 5507 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
188dd5d6
DJ
5508 bfd_get_filename (abfd));
5509
c906108c
SS
5510 return buf;
5511}
5512
5513/* In DWARF version 2, the description of the debugging information is
5514 stored in a separate .debug_abbrev section. Before we read any
5515 dies from a section we read in all abbreviations and install them
72bf9492
DJ
5516 in a hash table. This function also sets flags in CU describing
5517 the data found in the abbrev table. */
c906108c
SS
5518
5519static void
e7c27a73 5520dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 5521{
e7c27a73 5522 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 5523 gdb_byte *abbrev_ptr;
c906108c
SS
5524 struct abbrev_info *cur_abbrev;
5525 unsigned int abbrev_number, bytes_read, abbrev_name;
5526 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
5527 struct attr_abbrev *cur_attrs;
5528 unsigned int allocated_attrs;
c906108c 5529
57349743 5530 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
5531 obstack_init (&cu->abbrev_obstack);
5532 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
5533 (ABBREV_HASH_SIZE
5534 * sizeof (struct abbrev_info *)));
5535 memset (cu->dwarf2_abbrevs, 0,
5536 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 5537
6502dd73 5538 abbrev_ptr = dwarf2_per_objfile->abbrev_buffer + cu_header->abbrev_offset;
c906108c
SS
5539 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5540 abbrev_ptr += bytes_read;
5541
f3dd6933
DJ
5542 allocated_attrs = ATTR_ALLOC_CHUNK;
5543 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
5544
c906108c
SS
5545 /* loop until we reach an abbrev number of 0 */
5546 while (abbrev_number)
5547 {
f3dd6933 5548 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
5549
5550 /* read in abbrev header */
5551 cur_abbrev->number = abbrev_number;
5552 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5553 abbrev_ptr += bytes_read;
5554 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
5555 abbrev_ptr += 1;
5556
72bf9492
DJ
5557 if (cur_abbrev->tag == DW_TAG_namespace)
5558 cu->has_namespace_info = 1;
5559
c906108c
SS
5560 /* now read in declarations */
5561 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5562 abbrev_ptr += bytes_read;
5563 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5564 abbrev_ptr += bytes_read;
5565 while (abbrev_name)
5566 {
f3dd6933 5567 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 5568 {
f3dd6933
DJ
5569 allocated_attrs += ATTR_ALLOC_CHUNK;
5570 cur_attrs
5571 = xrealloc (cur_attrs, (allocated_attrs
5572 * sizeof (struct attr_abbrev)));
c906108c 5573 }
ae038cb0
DJ
5574
5575 /* Record whether this compilation unit might have
5576 inter-compilation-unit references. If we don't know what form
5577 this attribute will have, then it might potentially be a
5578 DW_FORM_ref_addr, so we conservatively expect inter-CU
5579 references. */
5580
5581 if (abbrev_form == DW_FORM_ref_addr
5582 || abbrev_form == DW_FORM_indirect)
5583 cu->has_form_ref_addr = 1;
5584
f3dd6933
DJ
5585 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
5586 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
5587 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5588 abbrev_ptr += bytes_read;
5589 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5590 abbrev_ptr += bytes_read;
5591 }
5592
f3dd6933
DJ
5593 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
5594 (cur_abbrev->num_attrs
5595 * sizeof (struct attr_abbrev)));
5596 memcpy (cur_abbrev->attrs, cur_attrs,
5597 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
5598
c906108c 5599 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
5600 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
5601 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
5602
5603 /* Get next abbreviation.
5604 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
5605 always properly terminated with an abbrev number of 0.
5606 Exit loop if we encounter an abbreviation which we have
5607 already read (which means we are about to read the abbreviations
5608 for the next compile unit) or if the end of the abbreviation
5609 table is reached. */
6502dd73
DJ
5610 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev_buffer)
5611 >= dwarf2_per_objfile->abbrev_size)
c906108c
SS
5612 break;
5613 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5614 abbrev_ptr += bytes_read;
e7c27a73 5615 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
5616 break;
5617 }
f3dd6933
DJ
5618
5619 xfree (cur_attrs);
c906108c
SS
5620}
5621
f3dd6933 5622/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 5623
c906108c 5624static void
f3dd6933 5625dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 5626{
f3dd6933 5627 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 5628
f3dd6933
DJ
5629 obstack_free (&cu->abbrev_obstack, NULL);
5630 cu->dwarf2_abbrevs = NULL;
c906108c
SS
5631}
5632
5633/* Lookup an abbrev_info structure in the abbrev hash table. */
5634
5635static struct abbrev_info *
e7c27a73 5636dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
5637{
5638 unsigned int hash_number;
5639 struct abbrev_info *abbrev;
5640
5641 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 5642 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
5643
5644 while (abbrev)
5645 {
5646 if (abbrev->number == number)
5647 return abbrev;
5648 else
5649 abbrev = abbrev->next;
5650 }
5651 return NULL;
5652}
5653
72bf9492
DJ
5654/* Returns nonzero if TAG represents a type that we might generate a partial
5655 symbol for. */
5656
5657static int
5658is_type_tag_for_partial (int tag)
5659{
5660 switch (tag)
5661 {
5662#if 0
5663 /* Some types that would be reasonable to generate partial symbols for,
5664 that we don't at present. */
5665 case DW_TAG_array_type:
5666 case DW_TAG_file_type:
5667 case DW_TAG_ptr_to_member_type:
5668 case DW_TAG_set_type:
5669 case DW_TAG_string_type:
5670 case DW_TAG_subroutine_type:
5671#endif
5672 case DW_TAG_base_type:
5673 case DW_TAG_class_type:
680b30c7 5674 case DW_TAG_interface_type:
72bf9492
DJ
5675 case DW_TAG_enumeration_type:
5676 case DW_TAG_structure_type:
5677 case DW_TAG_subrange_type:
5678 case DW_TAG_typedef:
5679 case DW_TAG_union_type:
5680 return 1;
5681 default:
5682 return 0;
5683 }
5684}
5685
5686/* Load all DIEs that are interesting for partial symbols into memory. */
5687
5688static struct partial_die_info *
fe1b8b76 5689load_partial_dies (bfd *abfd, gdb_byte *info_ptr, int building_psymtab,
72bf9492
DJ
5690 struct dwarf2_cu *cu)
5691{
5692 struct partial_die_info *part_die;
5693 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
5694 struct abbrev_info *abbrev;
5695 unsigned int bytes_read;
5afb4e99 5696 unsigned int load_all = 0;
72bf9492
DJ
5697
5698 int nesting_level = 1;
5699
5700 parent_die = NULL;
5701 last_die = NULL;
5702
5afb4e99
DJ
5703 if (cu->per_cu && cu->per_cu->load_all_dies)
5704 load_all = 1;
5705
72bf9492
DJ
5706 cu->partial_dies
5707 = htab_create_alloc_ex (cu->header.length / 12,
5708 partial_die_hash,
5709 partial_die_eq,
5710 NULL,
5711 &cu->comp_unit_obstack,
5712 hashtab_obstack_allocate,
5713 dummy_obstack_deallocate);
5714
5715 part_die = obstack_alloc (&cu->comp_unit_obstack,
5716 sizeof (struct partial_die_info));
5717
5718 while (1)
5719 {
5720 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5721
5722 /* A NULL abbrev means the end of a series of children. */
5723 if (abbrev == NULL)
5724 {
5725 if (--nesting_level == 0)
5726 {
5727 /* PART_DIE was probably the last thing allocated on the
5728 comp_unit_obstack, so we could call obstack_free
5729 here. We don't do that because the waste is small,
5730 and will be cleaned up when we're done with this
5731 compilation unit. This way, we're also more robust
5732 against other users of the comp_unit_obstack. */
5733 return first_die;
5734 }
5735 info_ptr += bytes_read;
5736 last_die = parent_die;
5737 parent_die = parent_die->die_parent;
5738 continue;
5739 }
5740
5afb4e99
DJ
5741 /* Check whether this DIE is interesting enough to save. Normally
5742 we would not be interested in members here, but there may be
5743 later variables referencing them via DW_AT_specification (for
5744 static members). */
5745 if (!load_all
5746 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
5747 && abbrev->tag != DW_TAG_enumerator
5748 && abbrev->tag != DW_TAG_subprogram
bc30ff58 5749 && abbrev->tag != DW_TAG_lexical_block
72bf9492 5750 && abbrev->tag != DW_TAG_variable
5afb4e99
DJ
5751 && abbrev->tag != DW_TAG_namespace
5752 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
5753 {
5754 /* Otherwise we skip to the next sibling, if any. */
5755 info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
5756 continue;
5757 }
5758
5759 info_ptr = read_partial_die (part_die, abbrev, bytes_read,
5760 abfd, info_ptr, cu);
5761
5762 /* This two-pass algorithm for processing partial symbols has a
5763 high cost in cache pressure. Thus, handle some simple cases
5764 here which cover the majority of C partial symbols. DIEs
5765 which neither have specification tags in them, nor could have
5766 specification tags elsewhere pointing at them, can simply be
5767 processed and discarded.
5768
5769 This segment is also optional; scan_partial_symbols and
5770 add_partial_symbol will handle these DIEs if we chain
5771 them in normally. When compilers which do not emit large
5772 quantities of duplicate debug information are more common,
5773 this code can probably be removed. */
5774
5775 /* Any complete simple types at the top level (pretty much all
5776 of them, for a language without namespaces), can be processed
5777 directly. */
5778 if (parent_die == NULL
5779 && part_die->has_specification == 0
5780 && part_die->is_declaration == 0
5781 && (part_die->tag == DW_TAG_typedef
5782 || part_die->tag == DW_TAG_base_type
5783 || part_die->tag == DW_TAG_subrange_type))
5784 {
5785 if (building_psymtab && part_die->name != NULL)
5786 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5787 VAR_DOMAIN, LOC_TYPEDEF,
5788 &cu->objfile->static_psymbols,
5789 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5790 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5791 continue;
5792 }
5793
5794 /* If we're at the second level, and we're an enumerator, and
5795 our parent has no specification (meaning possibly lives in a
5796 namespace elsewhere), then we can add the partial symbol now
5797 instead of queueing it. */
5798 if (part_die->tag == DW_TAG_enumerator
5799 && parent_die != NULL
5800 && parent_die->die_parent == NULL
5801 && parent_die->tag == DW_TAG_enumeration_type
5802 && parent_die->has_specification == 0)
5803 {
5804 if (part_die->name == NULL)
e2e0b3e5 5805 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492
DJ
5806 else if (building_psymtab)
5807 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5808 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5809 (cu->language == language_cplus
5810 || cu->language == language_java)
72bf9492
DJ
5811 ? &cu->objfile->global_psymbols
5812 : &cu->objfile->static_psymbols,
5813 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5814
5815 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5816 continue;
5817 }
5818
5819 /* We'll save this DIE so link it in. */
5820 part_die->die_parent = parent_die;
5821 part_die->die_sibling = NULL;
5822 part_die->die_child = NULL;
5823
5824 if (last_die && last_die == parent_die)
5825 last_die->die_child = part_die;
5826 else if (last_die)
5827 last_die->die_sibling = part_die;
5828
5829 last_die = part_die;
5830
5831 if (first_die == NULL)
5832 first_die = part_die;
5833
5834 /* Maybe add the DIE to the hash table. Not all DIEs that we
5835 find interesting need to be in the hash table, because we
5836 also have the parent/sibling/child chains; only those that we
5837 might refer to by offset later during partial symbol reading.
5838
5839 For now this means things that might have be the target of a
5840 DW_AT_specification, DW_AT_abstract_origin, or
5841 DW_AT_extension. DW_AT_extension will refer only to
5842 namespaces; DW_AT_abstract_origin refers to functions (and
5843 many things under the function DIE, but we do not recurse
5844 into function DIEs during partial symbol reading) and
5845 possibly variables as well; DW_AT_specification refers to
5846 declarations. Declarations ought to have the DW_AT_declaration
5847 flag. It happens that GCC forgets to put it in sometimes, but
5848 only for functions, not for types.
5849
5850 Adding more things than necessary to the hash table is harmless
5851 except for the performance cost. Adding too few will result in
5afb4e99
DJ
5852 wasted time in find_partial_die, when we reread the compilation
5853 unit with load_all_dies set. */
72bf9492 5854
5afb4e99
DJ
5855 if (load_all
5856 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
5857 || abbrev->tag == DW_TAG_variable
5858 || abbrev->tag == DW_TAG_namespace
5859 || part_die->is_declaration)
5860 {
5861 void **slot;
5862
5863 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
5864 part_die->offset, INSERT);
5865 *slot = part_die;
5866 }
5867
5868 part_die = obstack_alloc (&cu->comp_unit_obstack,
5869 sizeof (struct partial_die_info));
5870
5871 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 5872 we have no reason to follow the children of structures; for other
72bf9492 5873 languages we have to, both so that we can get at method physnames
bc30ff58
JB
5874 to infer fully qualified class names, and for DW_AT_specification.
5875
5876 For Ada, we need to scan the children of subprograms and lexical
5877 blocks as well because Ada allows the definition of nested
5878 entities that could be interesting for the debugger, such as
5879 nested subprograms for instance. */
72bf9492 5880 if (last_die->has_children
5afb4e99
DJ
5881 && (load_all
5882 || last_die->tag == DW_TAG_namespace
72bf9492
DJ
5883 || last_die->tag == DW_TAG_enumeration_type
5884 || (cu->language != language_c
5885 && (last_die->tag == DW_TAG_class_type
680b30c7 5886 || last_die->tag == DW_TAG_interface_type
72bf9492 5887 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
5888 || last_die->tag == DW_TAG_union_type))
5889 || (cu->language == language_ada
5890 && (last_die->tag == DW_TAG_subprogram
5891 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
5892 {
5893 nesting_level++;
5894 parent_die = last_die;
5895 continue;
5896 }
5897
5898 /* Otherwise we skip to the next sibling, if any. */
5899 info_ptr = locate_pdi_sibling (last_die, info_ptr, abfd, cu);
5900
5901 /* Back to the top, do it again. */
5902 }
5903}
5904
c906108c
SS
5905/* Read a minimal amount of information into the minimal die structure. */
5906
fe1b8b76 5907static gdb_byte *
72bf9492
DJ
5908read_partial_die (struct partial_die_info *part_die,
5909 struct abbrev_info *abbrev,
5910 unsigned int abbrev_len, bfd *abfd,
fe1b8b76 5911 gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 5912{
72bf9492 5913 unsigned int bytes_read, i;
c906108c 5914 struct attribute attr;
c5aa993b 5915 int has_low_pc_attr = 0;
c906108c 5916 int has_high_pc_attr = 0;
e7030f15 5917 CORE_ADDR base_address = 0;
85cbf3d3
JK
5918 enum
5919 {
5920 base_address_none,
5921 base_address_low_pc,
5922 /* Overrides BASE_ADDRESS_LOW_PC. */
5923 base_address_entry_pc
5924 }
5925 base_address_type = base_address_none;
c906108c 5926
72bf9492 5927 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 5928
6502dd73 5929 part_die->offset = info_ptr - dwarf2_per_objfile->info_buffer;
72bf9492
DJ
5930
5931 info_ptr += abbrev_len;
5932
5933 if (abbrev == NULL)
5934 return info_ptr;
5935
c906108c
SS
5936 part_die->tag = abbrev->tag;
5937 part_die->has_children = abbrev->has_children;
c906108c
SS
5938
5939 for (i = 0; i < abbrev->num_attrs; ++i)
5940 {
e7c27a73 5941 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
5942
5943 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 5944 partial symbol table. */
c906108c
SS
5945 switch (attr.name)
5946 {
5947 case DW_AT_name:
5948
5949 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
5950 if (part_die->name == NULL)
5951 part_die->name = DW_STRING (&attr);
5952 break;
57c22c6c
BR
5953 case DW_AT_comp_dir:
5954 if (part_die->dirname == NULL)
5955 part_die->dirname = DW_STRING (&attr);
5956 break;
c906108c
SS
5957 case DW_AT_MIPS_linkage_name:
5958 part_die->name = DW_STRING (&attr);
5959 break;
5960 case DW_AT_low_pc:
5961 has_low_pc_attr = 1;
5962 part_die->lowpc = DW_ADDR (&attr);
85cbf3d3
JK
5963 if (part_die->tag == DW_TAG_compile_unit
5964 && base_address_type < base_address_low_pc)
5965 {
5966 base_address = DW_ADDR (&attr);
5967 base_address_type = base_address_low_pc;
5968 }
c906108c
SS
5969 break;
5970 case DW_AT_high_pc:
5971 has_high_pc_attr = 1;
5972 part_die->highpc = DW_ADDR (&attr);
5973 break;
85cbf3d3
JK
5974 case DW_AT_entry_pc:
5975 if (part_die->tag == DW_TAG_compile_unit
5976 && base_address_type < base_address_entry_pc)
5977 {
5978 base_address = DW_ADDR (&attr);
5979 base_address_type = base_address_entry_pc;
5980 }
5981 break;
43039443 5982 case DW_AT_ranges:
ff013f42
JK
5983 if (part_die->tag == DW_TAG_compile_unit)
5984 {
5985 cu->ranges_offset = DW_UNSND (&attr);
5986 cu->has_ranges_offset = 1;
5987 }
43039443 5988 break;
c906108c 5989 case DW_AT_location:
8e19ed76
PS
5990 /* Support the .debug_loc offsets */
5991 if (attr_form_is_block (&attr))
5992 {
5993 part_die->locdesc = DW_BLOCK (&attr);
5994 }
3690dd37 5995 else if (attr_form_is_section_offset (&attr))
8e19ed76 5996 {
4d3c2250 5997 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
5998 }
5999 else
6000 {
4d3c2250
KB
6001 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6002 "partial symbol information");
8e19ed76 6003 }
c906108c
SS
6004 break;
6005 case DW_AT_language:
6006 part_die->language = DW_UNSND (&attr);
6007 break;
6008 case DW_AT_external:
6009 part_die->is_external = DW_UNSND (&attr);
6010 break;
6011 case DW_AT_declaration:
6012 part_die->is_declaration = DW_UNSND (&attr);
6013 break;
6014 case DW_AT_type:
6015 part_die->has_type = 1;
6016 break;
6017 case DW_AT_abstract_origin:
6018 case DW_AT_specification:
72bf9492
DJ
6019 case DW_AT_extension:
6020 part_die->has_specification = 1;
c764a876 6021 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
6022 break;
6023 case DW_AT_sibling:
6024 /* Ignore absolute siblings, they might point outside of
6025 the current compile unit. */
6026 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 6027 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 6028 else
6502dd73 6029 part_die->sibling = dwarf2_per_objfile->info_buffer
c764a876 6030 + dwarf2_get_ref_die_offset (&attr);
c906108c 6031 break;
aaa75496
JB
6032 case DW_AT_stmt_list:
6033 part_die->has_stmt_list = 1;
6034 part_die->line_offset = DW_UNSND (&attr);
6035 break;
fa4028e9
JB
6036 case DW_AT_byte_size:
6037 part_die->has_byte_size = 1;
6038 break;
68511cec
CES
6039 case DW_AT_calling_convention:
6040 /* DWARF doesn't provide a way to identify a program's source-level
6041 entry point. DW_AT_calling_convention attributes are only meant
6042 to describe functions' calling conventions.
6043
6044 However, because it's a necessary piece of information in
6045 Fortran, and because DW_CC_program is the only piece of debugging
6046 information whose definition refers to a 'main program' at all,
6047 several compilers have begun marking Fortran main programs with
6048 DW_CC_program --- even when those functions use the standard
6049 calling conventions.
6050
6051 So until DWARF specifies a way to provide this information and
6052 compilers pick up the new representation, we'll support this
6053 practice. */
6054 if (DW_UNSND (&attr) == DW_CC_program
6055 && cu->language == language_fortran)
6056 set_main_name (part_die->name);
6057 break;
c906108c
SS
6058 default:
6059 break;
6060 }
6061 }
6062
c906108c
SS
6063 /* When using the GNU linker, .gnu.linkonce. sections are used to
6064 eliminate duplicate copies of functions and vtables and such.
6065 The linker will arbitrarily choose one and discard the others.
6066 The AT_*_pc values for such functions refer to local labels in
6067 these sections. If the section from that file was discarded, the
6068 labels are not in the output, so the relocs get a value of 0.
6069 If this is a discarded function, mark the pc bounds as invalid,
6070 so that GDB will ignore it. */
6071 if (has_low_pc_attr && has_high_pc_attr
6072 && part_die->lowpc < part_die->highpc
6073 && (part_die->lowpc != 0
72dca2f5 6074 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 6075 part_die->has_pc_info = 1;
85cbf3d3 6076
d00adf39 6077 if (base_address_type != base_address_none && !cu->base_known)
85cbf3d3
JK
6078 {
6079 gdb_assert (part_die->tag == DW_TAG_compile_unit);
d00adf39
DE
6080 cu->base_known = 1;
6081 cu->base_address = base_address;
85cbf3d3
JK
6082 }
6083
c906108c
SS
6084 return info_ptr;
6085}
6086
72bf9492
DJ
6087/* Find a cached partial DIE at OFFSET in CU. */
6088
6089static struct partial_die_info *
c764a876 6090find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
6091{
6092 struct partial_die_info *lookup_die = NULL;
6093 struct partial_die_info part_die;
6094
6095 part_die.offset = offset;
6096 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6097
72bf9492
DJ
6098 return lookup_die;
6099}
6100
6101/* Find a partial DIE at OFFSET, which may or may not be in CU. */
6102
6103static struct partial_die_info *
c764a876 6104find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 6105{
5afb4e99
DJ
6106 struct dwarf2_per_cu_data *per_cu = NULL;
6107 struct partial_die_info *pd = NULL;
72bf9492 6108
45452591 6109 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
6110 {
6111 pd = find_partial_die_in_comp_unit (offset, cu);
6112 if (pd != NULL)
6113 return pd;
6114 }
72bf9492 6115
ae038cb0
DJ
6116 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6117
ae038cb0
DJ
6118 if (per_cu->cu == NULL)
6119 {
6120 load_comp_unit (per_cu, cu->objfile);
6121 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6122 dwarf2_per_objfile->read_in_chain = per_cu;
6123 }
6124
6125 per_cu->cu->last_used = 0;
5afb4e99
DJ
6126 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6127
6128 if (pd == NULL && per_cu->load_all_dies == 0)
6129 {
6130 struct cleanup *back_to;
6131 struct partial_die_info comp_unit_die;
6132 struct abbrev_info *abbrev;
6133 unsigned int bytes_read;
6134 char *info_ptr;
6135
6136 per_cu->load_all_dies = 1;
6137
6138 /* Re-read the DIEs. */
6139 back_to = make_cleanup (null_cleanup, 0);
6140 if (per_cu->cu->dwarf2_abbrevs == NULL)
6141 {
6142 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
6143 back_to = make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
6144 }
d00adf39
DE
6145 info_ptr = (dwarf2_per_objfile->info_buffer
6146 + per_cu->cu->header.offset
6147 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
6148 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6149 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
6150 per_cu->cu->objfile->obfd, info_ptr,
6151 per_cu->cu);
6152 if (comp_unit_die.has_children)
6153 load_partial_dies (per_cu->cu->objfile->obfd, info_ptr, 0, per_cu->cu);
6154 do_cleanups (back_to);
6155
6156 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6157 }
6158
6159 if (pd == NULL)
6160 internal_error (__FILE__, __LINE__,
c764a876 6161 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
6162 offset, bfd_get_filename (cu->objfile->obfd));
6163 return pd;
72bf9492
DJ
6164}
6165
6166/* Adjust PART_DIE before generating a symbol for it. This function
6167 may set the is_external flag or change the DIE's name. */
6168
6169static void
6170fixup_partial_die (struct partial_die_info *part_die,
6171 struct dwarf2_cu *cu)
6172{
6173 /* If we found a reference attribute and the DIE has no name, try
6174 to find a name in the referred to DIE. */
6175
6176 if (part_die->name == NULL && part_die->has_specification)
6177 {
6178 struct partial_die_info *spec_die;
72bf9492 6179
10b3939b 6180 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 6181
10b3939b 6182 fixup_partial_die (spec_die, cu);
72bf9492
DJ
6183
6184 if (spec_die->name)
6185 {
6186 part_die->name = spec_die->name;
6187
6188 /* Copy DW_AT_external attribute if it is set. */
6189 if (spec_die->is_external)
6190 part_die->is_external = spec_die->is_external;
6191 }
6192 }
6193
6194 /* Set default names for some unnamed DIEs. */
6195 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
6196 || part_die->tag == DW_TAG_class_type))
6197 part_die->name = "(anonymous class)";
6198
6199 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
6200 part_die->name = "(anonymous namespace)";
6201
6202 if (part_die->tag == DW_TAG_structure_type
6203 || part_die->tag == DW_TAG_class_type
6204 || part_die->tag == DW_TAG_union_type)
6205 guess_structure_name (part_die, cu);
6206}
6207
639d11d3
DC
6208/* Read the die from the .debug_info section buffer. Set DIEP to
6209 point to a newly allocated die with its information, except for its
6210 child, sibling, and parent fields. Set HAS_CHILDREN to tell
6211 whether the die has children or not. */
c906108c 6212
fe1b8b76
JB
6213static gdb_byte *
6214read_full_die (struct die_info **diep, bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6215 struct dwarf2_cu *cu, int *has_children)
c906108c
SS
6216{
6217 unsigned int abbrev_number, bytes_read, i, offset;
6218 struct abbrev_info *abbrev;
6219 struct die_info *die;
6220
6502dd73 6221 offset = info_ptr - dwarf2_per_objfile->info_buffer;
c906108c
SS
6222 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6223 info_ptr += bytes_read;
6224 if (!abbrev_number)
6225 {
1d325ec1 6226 *diep = NULL;
639d11d3 6227 *has_children = 0;
c906108c
SS
6228 return info_ptr;
6229 }
6230
e7c27a73 6231 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
c906108c
SS
6232 if (!abbrev)
6233 {
8a3fe4f8 6234 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
72bf9492 6235 abbrev_number,
639d11d3 6236 bfd_get_filename (abfd));
c906108c 6237 }
b60c80d6 6238 die = dwarf_alloc_die (cu, abbrev->num_attrs);
c906108c
SS
6239 die->offset = offset;
6240 die->tag = abbrev->tag;
c906108c 6241 die->abbrev = abbrev_number;
c906108c
SS
6242
6243 die->num_attrs = abbrev->num_attrs;
c906108c
SS
6244
6245 for (i = 0; i < abbrev->num_attrs; ++i)
03dd20cc
DJ
6246 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6247 abfd, info_ptr, cu);
c906108c
SS
6248
6249 *diep = die;
639d11d3 6250 *has_children = abbrev->has_children;
c906108c
SS
6251 return info_ptr;
6252}
6253
a8329558 6254/* Read an attribute value described by an attribute form. */
c906108c 6255
fe1b8b76 6256static gdb_byte *
a8329558 6257read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 6258 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6259 struct dwarf2_cu *cu)
c906108c 6260{
e7c27a73 6261 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6262 unsigned int bytes_read;
6263 struct dwarf_block *blk;
6264
a8329558
KW
6265 attr->form = form;
6266 switch (form)
c906108c
SS
6267 {
6268 case DW_FORM_addr:
6269 case DW_FORM_ref_addr:
e7c27a73 6270 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 6271 info_ptr += bytes_read;
c906108c
SS
6272 break;
6273 case DW_FORM_block2:
7b5a2f43 6274 blk = dwarf_alloc_block (cu);
c906108c
SS
6275 blk->size = read_2_bytes (abfd, info_ptr);
6276 info_ptr += 2;
6277 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6278 info_ptr += blk->size;
6279 DW_BLOCK (attr) = blk;
6280 break;
6281 case DW_FORM_block4:
7b5a2f43 6282 blk = dwarf_alloc_block (cu);
c906108c
SS
6283 blk->size = read_4_bytes (abfd, info_ptr);
6284 info_ptr += 4;
6285 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6286 info_ptr += blk->size;
6287 DW_BLOCK (attr) = blk;
6288 break;
6289 case DW_FORM_data2:
6290 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
6291 info_ptr += 2;
6292 break;
6293 case DW_FORM_data4:
6294 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
6295 info_ptr += 4;
6296 break;
6297 case DW_FORM_data8:
6298 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
6299 info_ptr += 8;
6300 break;
6301 case DW_FORM_string:
6302 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
6303 info_ptr += bytes_read;
6304 break;
4bdf3d34
JJ
6305 case DW_FORM_strp:
6306 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
6307 &bytes_read);
6308 info_ptr += bytes_read;
6309 break;
c906108c 6310 case DW_FORM_block:
7b5a2f43 6311 blk = dwarf_alloc_block (cu);
c906108c
SS
6312 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6313 info_ptr += bytes_read;
6314 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6315 info_ptr += blk->size;
6316 DW_BLOCK (attr) = blk;
6317 break;
6318 case DW_FORM_block1:
7b5a2f43 6319 blk = dwarf_alloc_block (cu);
c906108c
SS
6320 blk->size = read_1_byte (abfd, info_ptr);
6321 info_ptr += 1;
6322 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6323 info_ptr += blk->size;
6324 DW_BLOCK (attr) = blk;
6325 break;
6326 case DW_FORM_data1:
6327 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6328 info_ptr += 1;
6329 break;
6330 case DW_FORM_flag:
6331 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6332 info_ptr += 1;
6333 break;
6334 case DW_FORM_sdata:
6335 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
6336 info_ptr += bytes_read;
6337 break;
6338 case DW_FORM_udata:
6339 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6340 info_ptr += bytes_read;
6341 break;
6342 case DW_FORM_ref1:
10b3939b 6343 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
6344 info_ptr += 1;
6345 break;
6346 case DW_FORM_ref2:
10b3939b 6347 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
6348 info_ptr += 2;
6349 break;
6350 case DW_FORM_ref4:
10b3939b 6351 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
6352 info_ptr += 4;
6353 break;
613e1657 6354 case DW_FORM_ref8:
10b3939b 6355 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
6356 info_ptr += 8;
6357 break;
c906108c 6358 case DW_FORM_ref_udata:
10b3939b
DJ
6359 DW_ADDR (attr) = (cu->header.offset
6360 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
6361 info_ptr += bytes_read;
6362 break;
c906108c 6363 case DW_FORM_indirect:
a8329558
KW
6364 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6365 info_ptr += bytes_read;
e7c27a73 6366 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 6367 break;
c906108c 6368 default:
8a3fe4f8 6369 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
6370 dwarf_form_name (form),
6371 bfd_get_filename (abfd));
c906108c 6372 }
28e94949
JB
6373
6374 /* We have seen instances where the compiler tried to emit a byte
6375 size attribute of -1 which ended up being encoded as an unsigned
6376 0xffffffff. Although 0xffffffff is technically a valid size value,
6377 an object of this size seems pretty unlikely so we can relatively
6378 safely treat these cases as if the size attribute was invalid and
6379 treat them as zero by default. */
6380 if (attr->name == DW_AT_byte_size
6381 && form == DW_FORM_data4
6382 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
6383 {
6384 complaint
6385 (&symfile_complaints,
6386 _("Suspicious DW_AT_byte_size value treated as zero instead of 0x%lx"),
6387 DW_UNSND (attr));
6388 DW_UNSND (attr) = 0;
6389 }
28e94949 6390
c906108c
SS
6391 return info_ptr;
6392}
6393
a8329558
KW
6394/* Read an attribute described by an abbreviated attribute. */
6395
fe1b8b76 6396static gdb_byte *
a8329558 6397read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 6398 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
6399{
6400 attr->name = abbrev->name;
e7c27a73 6401 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
6402}
6403
c906108c
SS
6404/* read dwarf information from a buffer */
6405
6406static unsigned int
fe1b8b76 6407read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 6408{
fe1b8b76 6409 return bfd_get_8 (abfd, buf);
c906108c
SS
6410}
6411
6412static int
fe1b8b76 6413read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 6414{
fe1b8b76 6415 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
6416}
6417
6418static unsigned int
fe1b8b76 6419read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6420{
fe1b8b76 6421 return bfd_get_16 (abfd, buf);
c906108c
SS
6422}
6423
6424static int
fe1b8b76 6425read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6426{
fe1b8b76 6427 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
6428}
6429
6430static unsigned int
fe1b8b76 6431read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6432{
fe1b8b76 6433 return bfd_get_32 (abfd, buf);
c906108c
SS
6434}
6435
6436static int
fe1b8b76 6437read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6438{
fe1b8b76 6439 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
6440}
6441
ce5d95e1 6442static unsigned long
fe1b8b76 6443read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6444{
fe1b8b76 6445 return bfd_get_64 (abfd, buf);
c906108c
SS
6446}
6447
6448static CORE_ADDR
fe1b8b76 6449read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 6450 unsigned int *bytes_read)
c906108c 6451{
e7c27a73 6452 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6453 CORE_ADDR retval = 0;
6454
107d2387 6455 if (cu_header->signed_addr_p)
c906108c 6456 {
107d2387
AC
6457 switch (cu_header->addr_size)
6458 {
6459 case 2:
fe1b8b76 6460 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
6461 break;
6462 case 4:
fe1b8b76 6463 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
6464 break;
6465 case 8:
fe1b8b76 6466 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
6467 break;
6468 default:
8e65ff28 6469 internal_error (__FILE__, __LINE__,
e2e0b3e5 6470 _("read_address: bad switch, signed [in module %s]"),
659b0389 6471 bfd_get_filename (abfd));
107d2387
AC
6472 }
6473 }
6474 else
6475 {
6476 switch (cu_header->addr_size)
6477 {
6478 case 2:
fe1b8b76 6479 retval = bfd_get_16 (abfd, buf);
107d2387
AC
6480 break;
6481 case 4:
fe1b8b76 6482 retval = bfd_get_32 (abfd, buf);
107d2387
AC
6483 break;
6484 case 8:
fe1b8b76 6485 retval = bfd_get_64 (abfd, buf);
107d2387
AC
6486 break;
6487 default:
8e65ff28 6488 internal_error (__FILE__, __LINE__,
e2e0b3e5 6489 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 6490 bfd_get_filename (abfd));
107d2387 6491 }
c906108c 6492 }
64367e0a 6493
107d2387
AC
6494 *bytes_read = cu_header->addr_size;
6495 return retval;
c906108c
SS
6496}
6497
f7ef9339 6498/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
6499 specification allows the initial length to take up either 4 bytes
6500 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
6501 bytes describe the length and all offsets will be 8 bytes in length
6502 instead of 4.
6503
f7ef9339
KB
6504 An older, non-standard 64-bit format is also handled by this
6505 function. The older format in question stores the initial length
6506 as an 8-byte quantity without an escape value. Lengths greater
6507 than 2^32 aren't very common which means that the initial 4 bytes
6508 is almost always zero. Since a length value of zero doesn't make
6509 sense for the 32-bit format, this initial zero can be considered to
6510 be an escape value which indicates the presence of the older 64-bit
6511 format. As written, the code can't detect (old format) lengths
917c78fc
MK
6512 greater than 4GB. If it becomes necessary to handle lengths
6513 somewhat larger than 4GB, we could allow other small values (such
6514 as the non-sensical values of 1, 2, and 3) to also be used as
6515 escape values indicating the presence of the old format.
f7ef9339 6516
917c78fc
MK
6517 The value returned via bytes_read should be used to increment the
6518 relevant pointer after calling read_initial_length().
c764a876 6519
613e1657
KB
6520 [ Note: read_initial_length() and read_offset() are based on the
6521 document entitled "DWARF Debugging Information Format", revision
f7ef9339 6522 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
6523 from:
6524
f7ef9339 6525 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
613e1657
KB
6526
6527 This document is only a draft and is subject to change. (So beware.)
6528
f7ef9339 6529 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
6530 determined empirically by examining 64-bit ELF files produced by
6531 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
6532
6533 - Kevin, July 16, 2002
613e1657
KB
6534 ] */
6535
6536static LONGEST
c764a876 6537read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 6538{
fe1b8b76 6539 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 6540
dd373385 6541 if (length == 0xffffffff)
613e1657 6542 {
fe1b8b76 6543 length = bfd_get_64 (abfd, buf + 4);
613e1657 6544 *bytes_read = 12;
613e1657 6545 }
dd373385 6546 else if (length == 0)
f7ef9339 6547 {
dd373385 6548 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 6549 length = bfd_get_64 (abfd, buf);
f7ef9339 6550 *bytes_read = 8;
f7ef9339 6551 }
613e1657
KB
6552 else
6553 {
6554 *bytes_read = 4;
613e1657
KB
6555 }
6556
c764a876
DE
6557 return length;
6558}
dd373385 6559
c764a876
DE
6560/* Cover function for read_initial_length.
6561 Returns the length of the object at BUF, and stores the size of the
6562 initial length in *BYTES_READ and stores the size that offsets will be in
6563 *OFFSET_SIZE.
6564 If the initial length size is not equivalent to that specified in
6565 CU_HEADER then issue a complaint.
6566 This is useful when reading non-comp-unit headers. */
dd373385 6567
c764a876
DE
6568static LONGEST
6569read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
6570 const struct comp_unit_head *cu_header,
6571 unsigned int *bytes_read,
6572 unsigned int *offset_size)
6573{
6574 LONGEST length = read_initial_length (abfd, buf, bytes_read);
6575
6576 gdb_assert (cu_header->initial_length_size == 4
6577 || cu_header->initial_length_size == 8
6578 || cu_header->initial_length_size == 12);
6579
6580 if (cu_header->initial_length_size != *bytes_read)
6581 complaint (&symfile_complaints,
6582 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 6583
c764a876 6584 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 6585 return length;
613e1657
KB
6586}
6587
6588/* Read an offset from the data stream. The size of the offset is
917c78fc 6589 given by cu_header->offset_size. */
613e1657
KB
6590
6591static LONGEST
fe1b8b76 6592read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 6593 unsigned int *bytes_read)
c764a876
DE
6594{
6595 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
6596 *bytes_read = cu_header->offset_size;
6597 return offset;
6598}
6599
6600/* Read an offset from the data stream. */
6601
6602static LONGEST
6603read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
6604{
6605 LONGEST retval = 0;
6606
c764a876 6607 switch (offset_size)
613e1657
KB
6608 {
6609 case 4:
fe1b8b76 6610 retval = bfd_get_32 (abfd, buf);
613e1657
KB
6611 break;
6612 case 8:
fe1b8b76 6613 retval = bfd_get_64 (abfd, buf);
613e1657
KB
6614 break;
6615 default:
8e65ff28 6616 internal_error (__FILE__, __LINE__,
c764a876 6617 _("read_offset_1: bad switch [in module %s]"),
659b0389 6618 bfd_get_filename (abfd));
613e1657
KB
6619 }
6620
917c78fc 6621 return retval;
613e1657
KB
6622}
6623
fe1b8b76
JB
6624static gdb_byte *
6625read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
6626{
6627 /* If the size of a host char is 8 bits, we can return a pointer
6628 to the buffer, otherwise we have to copy the data to a buffer
6629 allocated on the temporary obstack. */
4bdf3d34 6630 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 6631 return buf;
c906108c
SS
6632}
6633
6634static char *
fe1b8b76 6635read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
6636{
6637 /* If the size of a host char is 8 bits, we can return a pointer
6638 to the string, otherwise we have to copy the string to a buffer
6639 allocated on the temporary obstack. */
4bdf3d34 6640 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
6641 if (*buf == '\0')
6642 {
6643 *bytes_read_ptr = 1;
6644 return NULL;
6645 }
fe1b8b76
JB
6646 *bytes_read_ptr = strlen ((char *) buf) + 1;
6647 return (char *) buf;
4bdf3d34
JJ
6648}
6649
6650static char *
fe1b8b76 6651read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
6652 const struct comp_unit_head *cu_header,
6653 unsigned int *bytes_read_ptr)
6654{
c764a876 6655 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 6656
6502dd73 6657 if (dwarf2_per_objfile->str_buffer == NULL)
c906108c 6658 {
8a3fe4f8 6659 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 6660 bfd_get_filename (abfd));
4bdf3d34 6661 return NULL;
c906108c 6662 }
6502dd73 6663 if (str_offset >= dwarf2_per_objfile->str_size)
c906108c 6664 {
8a3fe4f8 6665 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 6666 bfd_get_filename (abfd));
c906108c
SS
6667 return NULL;
6668 }
4bdf3d34 6669 gdb_assert (HOST_CHAR_BIT == 8);
6502dd73 6670 if (dwarf2_per_objfile->str_buffer[str_offset] == '\0')
4bdf3d34 6671 return NULL;
fe1b8b76 6672 return (char *) (dwarf2_per_objfile->str_buffer + str_offset);
c906108c
SS
6673}
6674
ce5d95e1 6675static unsigned long
fe1b8b76 6676read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 6677{
ce5d95e1
JB
6678 unsigned long result;
6679 unsigned int num_read;
c906108c
SS
6680 int i, shift;
6681 unsigned char byte;
6682
6683 result = 0;
6684 shift = 0;
6685 num_read = 0;
6686 i = 0;
6687 while (1)
6688 {
fe1b8b76 6689 byte = bfd_get_8 (abfd, buf);
c906108c
SS
6690 buf++;
6691 num_read++;
ce5d95e1 6692 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
6693 if ((byte & 128) == 0)
6694 {
6695 break;
6696 }
6697 shift += 7;
6698 }
6699 *bytes_read_ptr = num_read;
6700 return result;
6701}
6702
ce5d95e1 6703static long
fe1b8b76 6704read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 6705{
ce5d95e1 6706 long result;
77e0b926 6707 int i, shift, num_read;
c906108c
SS
6708 unsigned char byte;
6709
6710 result = 0;
6711 shift = 0;
c906108c
SS
6712 num_read = 0;
6713 i = 0;
6714 while (1)
6715 {
fe1b8b76 6716 byte = bfd_get_8 (abfd, buf);
c906108c
SS
6717 buf++;
6718 num_read++;
ce5d95e1 6719 result |= ((long)(byte & 127) << shift);
c906108c
SS
6720 shift += 7;
6721 if ((byte & 128) == 0)
6722 {
6723 break;
6724 }
6725 }
77e0b926
DJ
6726 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
6727 result |= -(((long)1) << shift);
c906108c
SS
6728 *bytes_read_ptr = num_read;
6729 return result;
6730}
6731
4bb7a0a7
DJ
6732/* Return a pointer to just past the end of an LEB128 number in BUF. */
6733
fe1b8b76
JB
6734static gdb_byte *
6735skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
6736{
6737 int byte;
6738
6739 while (1)
6740 {
fe1b8b76 6741 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
6742 buf++;
6743 if ((byte & 128) == 0)
6744 return buf;
6745 }
6746}
6747
c906108c 6748static void
e142c38c 6749set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
6750{
6751 switch (lang)
6752 {
6753 case DW_LANG_C89:
6754 case DW_LANG_C:
e142c38c 6755 cu->language = language_c;
c906108c
SS
6756 break;
6757 case DW_LANG_C_plus_plus:
e142c38c 6758 cu->language = language_cplus;
c906108c
SS
6759 break;
6760 case DW_LANG_Fortran77:
6761 case DW_LANG_Fortran90:
b21b22e0 6762 case DW_LANG_Fortran95:
e142c38c 6763 cu->language = language_fortran;
c906108c
SS
6764 break;
6765 case DW_LANG_Mips_Assembler:
e142c38c 6766 cu->language = language_asm;
c906108c 6767 break;
bebd888e 6768 case DW_LANG_Java:
e142c38c 6769 cu->language = language_java;
bebd888e 6770 break;
c906108c 6771 case DW_LANG_Ada83:
8aaf0b47 6772 case DW_LANG_Ada95:
bc5f45f8
JB
6773 cu->language = language_ada;
6774 break;
72019c9c
GM
6775 case DW_LANG_Modula2:
6776 cu->language = language_m2;
6777 break;
fe8e67fd
PM
6778 case DW_LANG_Pascal83:
6779 cu->language = language_pascal;
6780 break;
22566fbd
DJ
6781 case DW_LANG_ObjC:
6782 cu->language = language_objc;
6783 break;
c906108c
SS
6784 case DW_LANG_Cobol74:
6785 case DW_LANG_Cobol85:
c906108c 6786 default:
e142c38c 6787 cu->language = language_minimal;
c906108c
SS
6788 break;
6789 }
e142c38c 6790 cu->language_defn = language_def (cu->language);
c906108c
SS
6791}
6792
6793/* Return the named attribute or NULL if not there. */
6794
6795static struct attribute *
e142c38c 6796dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
6797{
6798 unsigned int i;
6799 struct attribute *spec = NULL;
6800
6801 for (i = 0; i < die->num_attrs; ++i)
6802 {
6803 if (die->attrs[i].name == name)
10b3939b 6804 return &die->attrs[i];
c906108c
SS
6805 if (die->attrs[i].name == DW_AT_specification
6806 || die->attrs[i].name == DW_AT_abstract_origin)
6807 spec = &die->attrs[i];
6808 }
c906108c 6809
10b3939b 6810 if (spec)
f2f0e013
DJ
6811 {
6812 die = follow_die_ref (die, spec, &cu);
6813 return dwarf2_attr (die, name, cu);
6814 }
c5aa993b 6815
c906108c
SS
6816 return NULL;
6817}
6818
05cf31d1
JB
6819/* Return non-zero iff the attribute NAME is defined for the given DIE,
6820 and holds a non-zero value. This function should only be used for
6821 DW_FORM_flag attributes. */
6822
6823static int
6824dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
6825{
6826 struct attribute *attr = dwarf2_attr (die, name, cu);
6827
6828 return (attr && DW_UNSND (attr));
6829}
6830
3ca72b44 6831static int
e142c38c 6832die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 6833{
05cf31d1
JB
6834 /* A DIE is a declaration if it has a DW_AT_declaration attribute
6835 which value is non-zero. However, we have to be careful with
6836 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
6837 (via dwarf2_flag_true_p) follows this attribute. So we may
6838 end up accidently finding a declaration attribute that belongs
6839 to a different DIE referenced by the specification attribute,
6840 even though the given DIE does not have a declaration attribute. */
6841 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
6842 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
6843}
6844
63d06c5c 6845/* Return the die giving the specification for DIE, if there is
f2f0e013
DJ
6846 one. *SPEC_CU is the CU containing DIE on input, and the CU
6847 containing the return value on output. */
63d06c5c
DC
6848
6849static struct die_info *
f2f0e013 6850die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 6851{
f2f0e013
DJ
6852 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
6853 *spec_cu);
63d06c5c
DC
6854
6855 if (spec_attr == NULL)
6856 return NULL;
6857 else
f2f0e013 6858 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 6859}
c906108c 6860
debd256d
JB
6861/* Free the line_header structure *LH, and any arrays and strings it
6862 refers to. */
6863static void
6864free_line_header (struct line_header *lh)
6865{
6866 if (lh->standard_opcode_lengths)
a8bc7b56 6867 xfree (lh->standard_opcode_lengths);
debd256d
JB
6868
6869 /* Remember that all the lh->file_names[i].name pointers are
6870 pointers into debug_line_buffer, and don't need to be freed. */
6871 if (lh->file_names)
a8bc7b56 6872 xfree (lh->file_names);
debd256d
JB
6873
6874 /* Similarly for the include directory names. */
6875 if (lh->include_dirs)
a8bc7b56 6876 xfree (lh->include_dirs);
debd256d 6877
a8bc7b56 6878 xfree (lh);
debd256d
JB
6879}
6880
6881
6882/* Add an entry to LH's include directory table. */
6883static void
6884add_include_dir (struct line_header *lh, char *include_dir)
c906108c 6885{
debd256d
JB
6886 /* Grow the array if necessary. */
6887 if (lh->include_dirs_size == 0)
c5aa993b 6888 {
debd256d
JB
6889 lh->include_dirs_size = 1; /* for testing */
6890 lh->include_dirs = xmalloc (lh->include_dirs_size
6891 * sizeof (*lh->include_dirs));
6892 }
6893 else if (lh->num_include_dirs >= lh->include_dirs_size)
6894 {
6895 lh->include_dirs_size *= 2;
6896 lh->include_dirs = xrealloc (lh->include_dirs,
6897 (lh->include_dirs_size
6898 * sizeof (*lh->include_dirs)));
c5aa993b 6899 }
c906108c 6900
debd256d
JB
6901 lh->include_dirs[lh->num_include_dirs++] = include_dir;
6902}
6903
6904
6905/* Add an entry to LH's file name table. */
6906static void
6907add_file_name (struct line_header *lh,
6908 char *name,
6909 unsigned int dir_index,
6910 unsigned int mod_time,
6911 unsigned int length)
6912{
6913 struct file_entry *fe;
6914
6915 /* Grow the array if necessary. */
6916 if (lh->file_names_size == 0)
6917 {
6918 lh->file_names_size = 1; /* for testing */
6919 lh->file_names = xmalloc (lh->file_names_size
6920 * sizeof (*lh->file_names));
6921 }
6922 else if (lh->num_file_names >= lh->file_names_size)
6923 {
6924 lh->file_names_size *= 2;
6925 lh->file_names = xrealloc (lh->file_names,
6926 (lh->file_names_size
6927 * sizeof (*lh->file_names)));
6928 }
6929
6930 fe = &lh->file_names[lh->num_file_names++];
6931 fe->name = name;
6932 fe->dir_index = dir_index;
6933 fe->mod_time = mod_time;
6934 fe->length = length;
aaa75496 6935 fe->included_p = 0;
cb1df416 6936 fe->symtab = NULL;
debd256d
JB
6937}
6938
6939
6940/* Read the statement program header starting at OFFSET in
6502dd73
DJ
6941 .debug_line, according to the endianness of ABFD. Return a pointer
6942 to a struct line_header, allocated using xmalloc.
debd256d
JB
6943
6944 NOTE: the strings in the include directory and file name tables of
6945 the returned object point into debug_line_buffer, and must not be
6946 freed. */
6947static struct line_header *
6948dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 6949 struct dwarf2_cu *cu)
debd256d
JB
6950{
6951 struct cleanup *back_to;
6952 struct line_header *lh;
fe1b8b76 6953 gdb_byte *line_ptr;
c764a876 6954 unsigned int bytes_read, offset_size;
debd256d
JB
6955 int i;
6956 char *cur_dir, *cur_file;
6957
6502dd73 6958 if (dwarf2_per_objfile->line_buffer == NULL)
debd256d 6959 {
e2e0b3e5 6960 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
6961 return 0;
6962 }
6963
a738430d
MK
6964 /* Make sure that at least there's room for the total_length field.
6965 That could be 12 bytes long, but we're just going to fudge that. */
6502dd73 6966 if (offset + 4 >= dwarf2_per_objfile->line_size)
debd256d 6967 {
4d3c2250 6968 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
6969 return 0;
6970 }
6971
6972 lh = xmalloc (sizeof (*lh));
6973 memset (lh, 0, sizeof (*lh));
6974 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
6975 (void *) lh);
6976
6502dd73 6977 line_ptr = dwarf2_per_objfile->line_buffer + offset;
debd256d 6978
a738430d 6979 /* Read in the header. */
dd373385 6980 lh->total_length =
c764a876
DE
6981 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
6982 &bytes_read, &offset_size);
debd256d 6983 line_ptr += bytes_read;
6502dd73
DJ
6984 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line_buffer
6985 + dwarf2_per_objfile->line_size))
debd256d 6986 {
4d3c2250 6987 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
6988 return 0;
6989 }
6990 lh->statement_program_end = line_ptr + lh->total_length;
6991 lh->version = read_2_bytes (abfd, line_ptr);
6992 line_ptr += 2;
c764a876
DE
6993 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
6994 line_ptr += offset_size;
debd256d
JB
6995 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
6996 line_ptr += 1;
6997 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
6998 line_ptr += 1;
6999 lh->line_base = read_1_signed_byte (abfd, line_ptr);
7000 line_ptr += 1;
7001 lh->line_range = read_1_byte (abfd, line_ptr);
7002 line_ptr += 1;
7003 lh->opcode_base = read_1_byte (abfd, line_ptr);
7004 line_ptr += 1;
7005 lh->standard_opcode_lengths
fe1b8b76 7006 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
7007
7008 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
7009 for (i = 1; i < lh->opcode_base; ++i)
7010 {
7011 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
7012 line_ptr += 1;
7013 }
7014
a738430d 7015 /* Read directory table. */
debd256d
JB
7016 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7017 {
7018 line_ptr += bytes_read;
7019 add_include_dir (lh, cur_dir);
7020 }
7021 line_ptr += bytes_read;
7022
a738430d 7023 /* Read file name table. */
debd256d
JB
7024 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7025 {
7026 unsigned int dir_index, mod_time, length;
7027
7028 line_ptr += bytes_read;
7029 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7030 line_ptr += bytes_read;
7031 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7032 line_ptr += bytes_read;
7033 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7034 line_ptr += bytes_read;
7035
7036 add_file_name (lh, cur_file, dir_index, mod_time, length);
7037 }
7038 line_ptr += bytes_read;
7039 lh->statement_program_start = line_ptr;
7040
6502dd73
DJ
7041 if (line_ptr > (dwarf2_per_objfile->line_buffer
7042 + dwarf2_per_objfile->line_size))
4d3c2250 7043 complaint (&symfile_complaints,
e2e0b3e5 7044 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
7045
7046 discard_cleanups (back_to);
7047 return lh;
7048}
c906108c 7049
5fb290d7
DJ
7050/* This function exists to work around a bug in certain compilers
7051 (particularly GCC 2.95), in which the first line number marker of a
7052 function does not show up until after the prologue, right before
7053 the second line number marker. This function shifts ADDRESS down
7054 to the beginning of the function if necessary, and is called on
7055 addresses passed to record_line. */
7056
7057static CORE_ADDR
e142c38c 7058check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
7059{
7060 struct function_range *fn;
7061
7062 /* Find the function_range containing address. */
e142c38c 7063 if (!cu->first_fn)
5fb290d7
DJ
7064 return address;
7065
e142c38c
DJ
7066 if (!cu->cached_fn)
7067 cu->cached_fn = cu->first_fn;
5fb290d7 7068
e142c38c 7069 fn = cu->cached_fn;
5fb290d7
DJ
7070 while (fn)
7071 if (fn->lowpc <= address && fn->highpc > address)
7072 goto found;
7073 else
7074 fn = fn->next;
7075
e142c38c
DJ
7076 fn = cu->first_fn;
7077 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
7078 if (fn->lowpc <= address && fn->highpc > address)
7079 goto found;
7080 else
7081 fn = fn->next;
7082
7083 return address;
7084
7085 found:
7086 if (fn->seen_line)
7087 return address;
7088 if (address != fn->lowpc)
4d3c2250 7089 complaint (&symfile_complaints,
e2e0b3e5 7090 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 7091 (unsigned long) address, fn->name);
5fb290d7
DJ
7092 fn->seen_line = 1;
7093 return fn->lowpc;
7094}
7095
aaa75496
JB
7096/* Decode the Line Number Program (LNP) for the given line_header
7097 structure and CU. The actual information extracted and the type
7098 of structures created from the LNP depends on the value of PST.
7099
7100 1. If PST is NULL, then this procedure uses the data from the program
7101 to create all necessary symbol tables, and their linetables.
7102 The compilation directory of the file is passed in COMP_DIR,
7103 and must not be NULL.
7104
7105 2. If PST is not NULL, this procedure reads the program to determine
7106 the list of files included by the unit represented by PST, and
7107 builds all the associated partial symbol tables. In this case,
7108 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
7109 is not used to compute the full name of the symtab, and therefore
7110 omitting it when building the partial symtab does not introduce
7111 the potential for inconsistency - a partial symtab and its associated
7112 symbtab having a different fullname -). */
debd256d 7113
c906108c 7114static void
debd256d 7115dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 7116 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 7117{
a8c50c1f 7118 gdb_byte *line_ptr, *extended_end;
fe1b8b76 7119 gdb_byte *line_end;
a8c50c1f 7120 unsigned int bytes_read, extended_len;
c906108c 7121 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
7122 CORE_ADDR baseaddr;
7123 struct objfile *objfile = cu->objfile;
aaa75496 7124 const int decode_for_pst_p = (pst != NULL);
cb1df416 7125 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
7126
7127 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7128
debd256d
JB
7129 line_ptr = lh->statement_program_start;
7130 line_end = lh->statement_program_end;
c906108c
SS
7131
7132 /* Read the statement sequences until there's nothing left. */
7133 while (line_ptr < line_end)
7134 {
7135 /* state machine registers */
7136 CORE_ADDR address = 0;
7137 unsigned int file = 1;
7138 unsigned int line = 1;
7139 unsigned int column = 0;
debd256d 7140 int is_stmt = lh->default_is_stmt;
c906108c
SS
7141 int basic_block = 0;
7142 int end_sequence = 0;
7143
aaa75496 7144 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 7145 {
aaa75496 7146 /* Start a subfile for the current file of the state machine. */
debd256d
JB
7147 /* lh->include_dirs and lh->file_names are 0-based, but the
7148 directory and file name numbers in the statement program
7149 are 1-based. */
7150 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 7151 char *dir = NULL;
a738430d 7152
debd256d
JB
7153 if (fe->dir_index)
7154 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
7155
7156 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
7157 }
7158
a738430d 7159 /* Decode the table. */
c5aa993b 7160 while (!end_sequence)
c906108c
SS
7161 {
7162 op_code = read_1_byte (abfd, line_ptr);
7163 line_ptr += 1;
59205f5a
JB
7164 if (line_ptr > line_end)
7165 {
7166 dwarf2_debug_line_missing_end_sequence_complaint ();
7167 break;
7168 }
9aa1fe7e 7169
debd256d 7170 if (op_code >= lh->opcode_base)
a738430d
MK
7171 {
7172 /* Special operand. */
debd256d
JB
7173 adj_opcode = op_code - lh->opcode_base;
7174 address += (adj_opcode / lh->line_range)
7175 * lh->minimum_instruction_length;
7176 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 7177 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7178 dwarf2_debug_line_missing_file_complaint ();
7179 else
7180 {
7181 lh->file_names[file - 1].included_p = 1;
7182 if (!decode_for_pst_p)
7183 {
7184 if (last_subfile != current_subfile)
7185 {
7186 if (last_subfile)
7187 record_line (last_subfile, 0, address);
7188 last_subfile = current_subfile;
7189 }
7190 /* Append row to matrix using current values. */
7191 record_line (current_subfile, line,
7192 check_cu_functions (address, cu));
366da635 7193 }
25e43795 7194 }
9aa1fe7e
GK
7195 basic_block = 1;
7196 }
7197 else switch (op_code)
c906108c
SS
7198 {
7199 case DW_LNS_extended_op:
a8c50c1f 7200 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 7201 line_ptr += bytes_read;
a8c50c1f 7202 extended_end = line_ptr + extended_len;
c906108c
SS
7203 extended_op = read_1_byte (abfd, line_ptr);
7204 line_ptr += 1;
7205 switch (extended_op)
7206 {
7207 case DW_LNE_end_sequence:
7208 end_sequence = 1;
c906108c
SS
7209 break;
7210 case DW_LNE_set_address:
e7c27a73 7211 address = read_address (abfd, line_ptr, cu, &bytes_read);
107d2387
AC
7212 line_ptr += bytes_read;
7213 address += baseaddr;
c906108c
SS
7214 break;
7215 case DW_LNE_define_file:
debd256d
JB
7216 {
7217 char *cur_file;
7218 unsigned int dir_index, mod_time, length;
7219
7220 cur_file = read_string (abfd, line_ptr, &bytes_read);
7221 line_ptr += bytes_read;
7222 dir_index =
7223 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7224 line_ptr += bytes_read;
7225 mod_time =
7226 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7227 line_ptr += bytes_read;
7228 length =
7229 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7230 line_ptr += bytes_read;
7231 add_file_name (lh, cur_file, dir_index, mod_time, length);
7232 }
c906108c
SS
7233 break;
7234 default:
4d3c2250 7235 complaint (&symfile_complaints,
e2e0b3e5 7236 _("mangled .debug_line section"));
debd256d 7237 return;
c906108c 7238 }
a8c50c1f
DJ
7239 /* Make sure that we parsed the extended op correctly. If e.g.
7240 we expected a different address size than the producer used,
7241 we may have read the wrong number of bytes. */
7242 if (line_ptr != extended_end)
7243 {
7244 complaint (&symfile_complaints,
7245 _("mangled .debug_line section"));
7246 return;
7247 }
c906108c
SS
7248 break;
7249 case DW_LNS_copy:
59205f5a 7250 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7251 dwarf2_debug_line_missing_file_complaint ();
7252 else
366da635 7253 {
25e43795
DJ
7254 lh->file_names[file - 1].included_p = 1;
7255 if (!decode_for_pst_p)
7256 {
7257 if (last_subfile != current_subfile)
7258 {
7259 if (last_subfile)
7260 record_line (last_subfile, 0, address);
7261 last_subfile = current_subfile;
7262 }
7263 record_line (current_subfile, line,
7264 check_cu_functions (address, cu));
7265 }
366da635 7266 }
c906108c
SS
7267 basic_block = 0;
7268 break;
7269 case DW_LNS_advance_pc:
debd256d 7270 address += lh->minimum_instruction_length
c906108c
SS
7271 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7272 line_ptr += bytes_read;
7273 break;
7274 case DW_LNS_advance_line:
7275 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
7276 line_ptr += bytes_read;
7277 break;
7278 case DW_LNS_set_file:
debd256d 7279 {
a738430d
MK
7280 /* The arrays lh->include_dirs and lh->file_names are
7281 0-based, but the directory and file name numbers in
7282 the statement program are 1-based. */
debd256d 7283 struct file_entry *fe;
4f1520fb 7284 char *dir = NULL;
a738430d 7285
debd256d
JB
7286 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7287 line_ptr += bytes_read;
59205f5a 7288 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7289 dwarf2_debug_line_missing_file_complaint ();
7290 else
7291 {
7292 fe = &lh->file_names[file - 1];
7293 if (fe->dir_index)
7294 dir = lh->include_dirs[fe->dir_index - 1];
7295 if (!decode_for_pst_p)
7296 {
7297 last_subfile = current_subfile;
7298 dwarf2_start_subfile (fe->name, dir, comp_dir);
7299 }
7300 }
debd256d 7301 }
c906108c
SS
7302 break;
7303 case DW_LNS_set_column:
7304 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7305 line_ptr += bytes_read;
7306 break;
7307 case DW_LNS_negate_stmt:
7308 is_stmt = (!is_stmt);
7309 break;
7310 case DW_LNS_set_basic_block:
7311 basic_block = 1;
7312 break;
c2c6d25f
JM
7313 /* Add to the address register of the state machine the
7314 address increment value corresponding to special opcode
a738430d
MK
7315 255. I.e., this value is scaled by the minimum
7316 instruction length since special opcode 255 would have
7317 scaled the the increment. */
c906108c 7318 case DW_LNS_const_add_pc:
debd256d
JB
7319 address += (lh->minimum_instruction_length
7320 * ((255 - lh->opcode_base) / lh->line_range));
c906108c
SS
7321 break;
7322 case DW_LNS_fixed_advance_pc:
7323 address += read_2_bytes (abfd, line_ptr);
7324 line_ptr += 2;
7325 break;
9aa1fe7e 7326 default:
a738430d
MK
7327 {
7328 /* Unknown standard opcode, ignore it. */
9aa1fe7e 7329 int i;
a738430d 7330
debd256d 7331 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
7332 {
7333 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7334 line_ptr += bytes_read;
7335 }
7336 }
c906108c
SS
7337 }
7338 }
59205f5a
JB
7339 if (lh->num_file_names < file || file == 0)
7340 dwarf2_debug_line_missing_file_complaint ();
7341 else
7342 {
7343 lh->file_names[file - 1].included_p = 1;
7344 if (!decode_for_pst_p)
7345 record_line (current_subfile, 0, address);
7346 }
c906108c 7347 }
aaa75496
JB
7348
7349 if (decode_for_pst_p)
7350 {
7351 int file_index;
7352
7353 /* Now that we're done scanning the Line Header Program, we can
7354 create the psymtab of each included file. */
7355 for (file_index = 0; file_index < lh->num_file_names; file_index++)
7356 if (lh->file_names[file_index].included_p == 1)
7357 {
5b5464ad
JB
7358 const struct file_entry fe = lh->file_names [file_index];
7359 char *include_name = fe.name;
7360 char *dir_name = NULL;
7361 char *pst_filename = pst->filename;
7362
7363 if (fe.dir_index)
7364 dir_name = lh->include_dirs[fe.dir_index - 1];
7365
7366 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
7367 {
1754f103
MK
7368 include_name = concat (dir_name, SLASH_STRING,
7369 include_name, (char *)NULL);
5b5464ad
JB
7370 make_cleanup (xfree, include_name);
7371 }
7372
7373 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
7374 {
1754f103
MK
7375 pst_filename = concat (pst->dirname, SLASH_STRING,
7376 pst_filename, (char *)NULL);
5b5464ad
JB
7377 make_cleanup (xfree, pst_filename);
7378 }
7379
7380 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
7381 dwarf2_create_include_psymtab (include_name, pst, objfile);
7382 }
7383 }
cb1df416
DJ
7384 else
7385 {
7386 /* Make sure a symtab is created for every file, even files
7387 which contain only variables (i.e. no code with associated
7388 line numbers). */
7389
7390 int i;
7391 struct file_entry *fe;
7392
7393 for (i = 0; i < lh->num_file_names; i++)
7394 {
7395 char *dir = NULL;
7396 fe = &lh->file_names[i];
7397 if (fe->dir_index)
7398 dir = lh->include_dirs[fe->dir_index - 1];
7399 dwarf2_start_subfile (fe->name, dir, comp_dir);
7400
7401 /* Skip the main file; we don't need it, and it must be
7402 allocated last, so that it will show up before the
7403 non-primary symtabs in the objfile's symtab list. */
7404 if (current_subfile == first_subfile)
7405 continue;
7406
7407 if (current_subfile->symtab == NULL)
7408 current_subfile->symtab = allocate_symtab (current_subfile->name,
7409 cu->objfile);
7410 fe->symtab = current_subfile->symtab;
7411 }
7412 }
c906108c
SS
7413}
7414
7415/* Start a subfile for DWARF. FILENAME is the name of the file and
7416 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
7417 or NULL if not known. COMP_DIR is the compilation directory for the
7418 linetable's compilation unit or NULL if not known.
c906108c
SS
7419 This routine tries to keep line numbers from identical absolute and
7420 relative file names in a common subfile.
7421
7422 Using the `list' example from the GDB testsuite, which resides in
7423 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
7424 of /srcdir/list0.c yields the following debugging information for list0.c:
7425
c5aa993b
JM
7426 DW_AT_name: /srcdir/list0.c
7427 DW_AT_comp_dir: /compdir
357e46e7 7428 files.files[0].name: list0.h
c5aa993b 7429 files.files[0].dir: /srcdir
357e46e7 7430 files.files[1].name: list0.c
c5aa993b 7431 files.files[1].dir: /srcdir
c906108c
SS
7432
7433 The line number information for list0.c has to end up in a single
4f1520fb
FR
7434 subfile, so that `break /srcdir/list0.c:1' works as expected.
7435 start_subfile will ensure that this happens provided that we pass the
7436 concatenation of files.files[1].dir and files.files[1].name as the
7437 subfile's name. */
c906108c
SS
7438
7439static void
4f1520fb 7440dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 7441{
4f1520fb
FR
7442 char *fullname;
7443
7444 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
7445 `start_symtab' will always pass the contents of DW_AT_comp_dir as
7446 second argument to start_subfile. To be consistent, we do the
7447 same here. In order not to lose the line information directory,
7448 we concatenate it to the filename when it makes sense.
7449 Note that the Dwarf3 standard says (speaking of filenames in line
7450 information): ``The directory index is ignored for file names
7451 that represent full path names''. Thus ignoring dirname in the
7452 `else' branch below isn't an issue. */
c906108c 7453
d5166ae1 7454 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
7455 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
7456 else
7457 fullname = filename;
c906108c 7458
4f1520fb
FR
7459 start_subfile (fullname, comp_dir);
7460
7461 if (fullname != filename)
7462 xfree (fullname);
c906108c
SS
7463}
7464
4c2df51b
DJ
7465static void
7466var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 7467 struct dwarf2_cu *cu)
4c2df51b 7468{
e7c27a73
DJ
7469 struct objfile *objfile = cu->objfile;
7470 struct comp_unit_head *cu_header = &cu->header;
7471
4c2df51b
DJ
7472 /* NOTE drow/2003-01-30: There used to be a comment and some special
7473 code here to turn a symbol with DW_AT_external and a
7474 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
7475 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
7476 with some versions of binutils) where shared libraries could have
7477 relocations against symbols in their debug information - the
7478 minimal symbol would have the right address, but the debug info
7479 would not. It's no longer necessary, because we will explicitly
7480 apply relocations when we read in the debug information now. */
7481
7482 /* A DW_AT_location attribute with no contents indicates that a
7483 variable has been optimized away. */
7484 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
7485 {
7486 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
7487 return;
7488 }
7489
7490 /* Handle one degenerate form of location expression specially, to
7491 preserve GDB's previous behavior when section offsets are
7492 specified. If this is just a DW_OP_addr then mark this symbol
7493 as LOC_STATIC. */
7494
7495 if (attr_form_is_block (attr)
7496 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
7497 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
7498 {
891d2f0b 7499 unsigned int dummy;
4c2df51b
DJ
7500
7501 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 7502 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 7503 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
7504 fixup_symbol_section (sym, objfile);
7505 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
7506 SYMBOL_SECTION (sym));
4c2df51b
DJ
7507 return;
7508 }
7509
7510 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
7511 expression evaluator, and use LOC_COMPUTED only when necessary
7512 (i.e. when the value of a register or memory location is
7513 referenced, or a thread-local block, etc.). Then again, it might
7514 not be worthwhile. I'm assuming that it isn't unless performance
7515 or memory numbers show me otherwise. */
7516
e7c27a73 7517 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
7518 SYMBOL_CLASS (sym) = LOC_COMPUTED;
7519}
7520
c906108c
SS
7521/* Given a pointer to a DWARF information entry, figure out if we need
7522 to make a symbol table entry for it, and if so, create a new entry
7523 and return a pointer to it.
7524 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 7525 used the passed type. */
c906108c
SS
7526
7527static struct symbol *
e7c27a73 7528new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 7529{
e7c27a73 7530 struct objfile *objfile = cu->objfile;
5e2b427d 7531 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7532 struct symbol *sym = NULL;
7533 char *name;
7534 struct attribute *attr = NULL;
7535 struct attribute *attr2 = NULL;
e142c38c
DJ
7536 CORE_ADDR baseaddr;
7537
7538 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7539
5c4e30ca 7540 if (die->tag != DW_TAG_namespace)
e142c38c 7541 name = dwarf2_linkage_name (die, cu);
5c4e30ca
DC
7542 else
7543 name = TYPE_NAME (type);
7544
c906108c
SS
7545 if (name)
7546 {
4a146b47 7547 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
7548 sizeof (struct symbol));
7549 OBJSTAT (objfile, n_syms++);
7550 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
7551
7552 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 7553 SYMBOL_LANGUAGE (sym) = cu->language;
2de7ced7 7554 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
c906108c
SS
7555
7556 /* Default assumptions.
c5aa993b 7557 Use the passed type or decode it from the die. */
176620f1 7558 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 7559 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
7560 if (type != NULL)
7561 SYMBOL_TYPE (sym) = type;
7562 else
e7c27a73 7563 SYMBOL_TYPE (sym) = die_type (die, cu);
e142c38c 7564 attr = dwarf2_attr (die, DW_AT_decl_line, cu);
c906108c
SS
7565 if (attr)
7566 {
7567 SYMBOL_LINE (sym) = DW_UNSND (attr);
7568 }
cb1df416
DJ
7569
7570 attr = dwarf2_attr (die, DW_AT_decl_file, cu);
7571 if (attr)
7572 {
7573 int file_index = DW_UNSND (attr);
7574 if (cu->line_header == NULL
7575 || file_index > cu->line_header->num_file_names)
7576 complaint (&symfile_complaints,
7577 _("file index out of range"));
1c3d648d 7578 else if (file_index > 0)
cb1df416
DJ
7579 {
7580 struct file_entry *fe;
7581 fe = &cu->line_header->file_names[file_index - 1];
7582 SYMBOL_SYMTAB (sym) = fe->symtab;
7583 }
7584 }
7585
c906108c
SS
7586 switch (die->tag)
7587 {
7588 case DW_TAG_label:
e142c38c 7589 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
7590 if (attr)
7591 {
7592 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
7593 }
7594 SYMBOL_CLASS (sym) = LOC_LABEL;
7595 break;
7596 case DW_TAG_subprogram:
7597 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
7598 finish_block. */
7599 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 7600 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
7601 if ((attr2 && (DW_UNSND (attr2) != 0))
7602 || cu->language == language_ada)
c906108c 7603 {
2cfa0c8d
JB
7604 /* Subprograms marked external are stored as a global symbol.
7605 Ada subprograms, whether marked external or not, are always
7606 stored as a global symbol, because we want to be able to
7607 access them globally. For instance, we want to be able
7608 to break on a nested subprogram without having to
7609 specify the context. */
c906108c
SS
7610 add_symbol_to_list (sym, &global_symbols);
7611 }
7612 else
7613 {
e142c38c 7614 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7615 }
7616 break;
7617 case DW_TAG_variable:
7618 /* Compilation with minimal debug info may result in variables
7619 with missing type entries. Change the misleading `void' type
7620 to something sensible. */
7621 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 7622 SYMBOL_TYPE (sym)
5e2b427d 7623 = builtin_type (gdbarch)->nodebug_data_symbol;
64c50499 7624
e142c38c 7625 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7626 if (attr)
7627 {
e7c27a73 7628 dwarf2_const_value (attr, sym, cu);
e142c38c 7629 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
7630 if (attr2 && (DW_UNSND (attr2) != 0))
7631 add_symbol_to_list (sym, &global_symbols);
7632 else
e142c38c 7633 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7634 break;
7635 }
e142c38c 7636 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7637 if (attr)
7638 {
e7c27a73 7639 var_decode_location (attr, sym, cu);
e142c38c 7640 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 7641 if (attr2 && (DW_UNSND (attr2) != 0))
4c2df51b 7642 add_symbol_to_list (sym, &global_symbols);
c906108c 7643 else
e142c38c 7644 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7645 }
7646 else
7647 {
7648 /* We do not know the address of this symbol.
c5aa993b
JM
7649 If it is an external symbol and we have type information
7650 for it, enter the symbol as a LOC_UNRESOLVED symbol.
7651 The address of the variable will then be determined from
7652 the minimal symbol table whenever the variable is
7653 referenced. */
e142c38c 7654 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 7655 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 7656 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c
SS
7657 {
7658 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
7659 add_symbol_to_list (sym, &global_symbols);
7660 }
7661 }
7662 break;
7663 case DW_TAG_formal_parameter:
2a2d4dc3 7664 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 7665 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7666 if (attr)
7667 {
e7c27a73 7668 var_decode_location (attr, sym, cu);
c906108c 7669 }
e142c38c 7670 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7671 if (attr)
7672 {
e7c27a73 7673 dwarf2_const_value (attr, sym, cu);
c906108c 7674 }
e142c38c 7675 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7676 break;
7677 case DW_TAG_unspecified_parameters:
7678 /* From varargs functions; gdb doesn't seem to have any
7679 interest in this information, so just ignore it for now.
7680 (FIXME?) */
7681 break;
7682 case DW_TAG_class_type:
680b30c7 7683 case DW_TAG_interface_type:
c906108c
SS
7684 case DW_TAG_structure_type:
7685 case DW_TAG_union_type:
72019c9c 7686 case DW_TAG_set_type:
c906108c
SS
7687 case DW_TAG_enumeration_type:
7688 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 7689 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 7690
63d06c5c
DC
7691 /* Make sure that the symbol includes appropriate enclosing
7692 classes/namespaces in its name. These are calculated in
134d01f1 7693 read_structure_type, and the correct name is saved in
63d06c5c
DC
7694 the type. */
7695
987504bb
JJ
7696 if (cu->language == language_cplus
7697 || cu->language == language_java)
c906108c 7698 {
63d06c5c
DC
7699 struct type *type = SYMBOL_TYPE (sym);
7700
7701 if (TYPE_TAG_NAME (type) != NULL)
7702 {
7703 /* FIXME: carlton/2003-11-10: Should this use
7704 SYMBOL_SET_NAMES instead? (The same problem also
d8151005
DJ
7705 arises further down in this function.) */
7706 /* The type's name is already allocated along with
7707 this objfile, so we don't need to duplicate it
7708 for the symbol. */
7709 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
63d06c5c 7710 }
c906108c 7711 }
63d06c5c
DC
7712
7713 {
987504bb 7714 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
7715 really ever be static objects: otherwise, if you try
7716 to, say, break of a class's method and you're in a file
7717 which doesn't mention that class, it won't work unless
7718 the check for all static symbols in lookup_symbol_aux
7719 saves you. See the OtherFileClass tests in
7720 gdb.c++/namespace.exp. */
7721
7722 struct pending **list_to_add;
7723
e142c38c 7724 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
7725 && (cu->language == language_cplus
7726 || cu->language == language_java)
e142c38c 7727 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
7728
7729 add_symbol_to_list (sym, list_to_add);
7730
7731 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 7732 defines a typedef for "foo". A Java class declaration also
5eeb2539 7733 defines a typedef for the class. */
987504bb 7734 if (cu->language == language_cplus
8c6860bb
JB
7735 || cu->language == language_java
7736 || cu->language == language_ada)
63d06c5c 7737 {
d8151005
DJ
7738 /* The symbol's name is already allocated along with
7739 this objfile, so we don't need to duplicate it for
7740 the type. */
63d06c5c 7741 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 7742 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
7743 }
7744 }
c906108c
SS
7745 break;
7746 case DW_TAG_typedef:
0114d602 7747 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
63d06c5c
DC
7748 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7749 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 7750 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 7751 break;
c906108c 7752 case DW_TAG_base_type:
a02abb62 7753 case DW_TAG_subrange_type:
c906108c 7754 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 7755 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 7756 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7757 break;
7758 case DW_TAG_enumerator:
0114d602 7759 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
e142c38c 7760 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7761 if (attr)
7762 {
e7c27a73 7763 dwarf2_const_value (attr, sym, cu);
c906108c 7764 }
63d06c5c
DC
7765 {
7766 /* NOTE: carlton/2003-11-10: See comment above in the
7767 DW_TAG_class_type, etc. block. */
7768
7769 struct pending **list_to_add;
7770
e142c38c 7771 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
7772 && (cu->language == language_cplus
7773 || cu->language == language_java)
e142c38c 7774 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
7775
7776 add_symbol_to_list (sym, list_to_add);
7777 }
c906108c 7778 break;
5c4e30ca
DC
7779 case DW_TAG_namespace:
7780 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7781 add_symbol_to_list (sym, &global_symbols);
7782 break;
c906108c
SS
7783 default:
7784 /* Not a tag we recognize. Hopefully we aren't processing
7785 trash data, but since we must specifically ignore things
7786 we don't recognize, there is nothing else we should do at
7787 this point. */
e2e0b3e5 7788 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 7789 dwarf_tag_name (die->tag));
c906108c
SS
7790 break;
7791 }
df8a16a1
DJ
7792
7793 /* For the benefit of old versions of GCC, check for anonymous
7794 namespaces based on the demangled name. */
7795 if (!processing_has_namespace_info
7796 && cu->language == language_cplus
7797 && dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu) != NULL)
7798 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
7799 }
7800 return (sym);
7801}
7802
7803/* Copy constant value from an attribute to a symbol. */
7804
7805static void
107d2387 7806dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 7807 struct dwarf2_cu *cu)
c906108c 7808{
e7c27a73
DJ
7809 struct objfile *objfile = cu->objfile;
7810 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7811 struct dwarf_block *blk;
7812
7813 switch (attr->form)
7814 {
7815 case DW_FORM_addr:
107d2387 7816 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
3567439c 7817 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
7818 cu_header->addr_size,
7819 TYPE_LENGTH (SYMBOL_TYPE
7820 (sym)));
4e38b386 7821 SYMBOL_VALUE_BYTES (sym) =
4a146b47 7822 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
fbd9dcd3
AC
7823 /* NOTE: cagney/2003-05-09: In-lined store_address call with
7824 it's body - store_unsigned_integer. */
7825 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
7826 DW_ADDR (attr));
c906108c
SS
7827 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7828 break;
93b5768b
PA
7829 case DW_FORM_strp:
7830 /* DW_STRING is already allocated on the obstack, point directly
7831 to it. */
7832 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
7833 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7834 break;
c906108c
SS
7835 case DW_FORM_block1:
7836 case DW_FORM_block2:
7837 case DW_FORM_block4:
7838 case DW_FORM_block:
7839 blk = DW_BLOCK (attr);
7840 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 7841 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
7842 blk->size,
7843 TYPE_LENGTH (SYMBOL_TYPE
7844 (sym)));
4e38b386 7845 SYMBOL_VALUE_BYTES (sym) =
4a146b47 7846 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
7847 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
7848 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7849 break;
2df3850c
JM
7850
7851 /* The DW_AT_const_value attributes are supposed to carry the
7852 symbol's value "represented as it would be on the target
7853 architecture." By the time we get here, it's already been
7854 converted to host endianness, so we just need to sign- or
7855 zero-extend it as appropriate. */
7856 case DW_FORM_data1:
7857 dwarf2_const_value_data (attr, sym, 8);
7858 break;
c906108c 7859 case DW_FORM_data2:
2df3850c
JM
7860 dwarf2_const_value_data (attr, sym, 16);
7861 break;
c906108c 7862 case DW_FORM_data4:
2df3850c
JM
7863 dwarf2_const_value_data (attr, sym, 32);
7864 break;
c906108c 7865 case DW_FORM_data8:
2df3850c
JM
7866 dwarf2_const_value_data (attr, sym, 64);
7867 break;
7868
c906108c 7869 case DW_FORM_sdata:
2df3850c
JM
7870 SYMBOL_VALUE (sym) = DW_SND (attr);
7871 SYMBOL_CLASS (sym) = LOC_CONST;
7872 break;
7873
c906108c
SS
7874 case DW_FORM_udata:
7875 SYMBOL_VALUE (sym) = DW_UNSND (attr);
7876 SYMBOL_CLASS (sym) = LOC_CONST;
7877 break;
2df3850c 7878
c906108c 7879 default:
4d3c2250 7880 complaint (&symfile_complaints,
e2e0b3e5 7881 _("unsupported const value attribute form: '%s'"),
4d3c2250 7882 dwarf_form_name (attr->form));
c906108c
SS
7883 SYMBOL_VALUE (sym) = 0;
7884 SYMBOL_CLASS (sym) = LOC_CONST;
7885 break;
7886 }
7887}
7888
2df3850c
JM
7889
7890/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
7891 or zero-extend it as appropriate for the symbol's type. */
7892static void
7893dwarf2_const_value_data (struct attribute *attr,
7894 struct symbol *sym,
7895 int bits)
7896{
7897 LONGEST l = DW_UNSND (attr);
7898
7899 if (bits < sizeof (l) * 8)
7900 {
7901 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
7902 l &= ((LONGEST) 1 << bits) - 1;
7903 else
bf9198f1 7904 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
7905 }
7906
7907 SYMBOL_VALUE (sym) = l;
7908 SYMBOL_CLASS (sym) = LOC_CONST;
7909}
7910
7911
c906108c
SS
7912/* Return the type of the die in question using its DW_AT_type attribute. */
7913
7914static struct type *
e7c27a73 7915die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7916{
5e2b427d 7917 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
c906108c
SS
7918 struct type *type;
7919 struct attribute *type_attr;
7920 struct die_info *type_die;
c906108c 7921
e142c38c 7922 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
7923 if (!type_attr)
7924 {
7925 /* A missing DW_AT_type represents a void type. */
5e2b427d 7926 return builtin_type (gdbarch)->builtin_void;
c906108c
SS
7927 }
7928 else
f2f0e013 7929 type_die = follow_die_ref (die, type_attr, &cu);
10b3939b 7930
e7c27a73 7931 type = tag_type_to_type (type_die, cu);
c906108c
SS
7932 if (!type)
7933 {
d97bc12b 7934 dump_die_for_error (type_die);
8a3fe4f8 7935 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
e7c27a73 7936 cu->objfile->name);
c906108c
SS
7937 }
7938 return type;
7939}
7940
7941/* Return the containing type of the die in question using its
7942 DW_AT_containing_type attribute. */
7943
7944static struct type *
e7c27a73 7945die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7946{
7947 struct type *type = NULL;
7948 struct attribute *type_attr;
7949 struct die_info *type_die = NULL;
c906108c 7950
e142c38c 7951 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
c906108c
SS
7952 if (type_attr)
7953 {
f2f0e013 7954 type_die = follow_die_ref (die, type_attr, &cu);
e7c27a73 7955 type = tag_type_to_type (type_die, cu);
c906108c
SS
7956 }
7957 if (!type)
7958 {
7959 if (type_die)
d97bc12b 7960 dump_die_for_error (type_die);
8a3fe4f8 7961 error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
e7c27a73 7962 cu->objfile->name);
c906108c
SS
7963 }
7964 return type;
7965}
7966
c906108c 7967static struct type *
e7c27a73 7968tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7969{
f792889a
DJ
7970 struct type *this_type;
7971
7972 this_type = read_type_die (die, cu);
7973 if (!this_type)
c906108c 7974 {
d97bc12b 7975 dump_die_for_error (die);
f792889a
DJ
7976 error (_("Dwarf Error: Cannot find type of die [in module %s]"),
7977 cu->objfile->name);
c906108c 7978 }
f792889a 7979 return this_type;
c906108c
SS
7980}
7981
f792889a 7982static struct type *
e7c27a73 7983read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7984{
f792889a
DJ
7985 struct type *this_type;
7986
7987 this_type = get_die_type (die, cu);
7988 if (this_type)
7989 return this_type;
7990
c906108c
SS
7991 switch (die->tag)
7992 {
7993 case DW_TAG_class_type:
680b30c7 7994 case DW_TAG_interface_type:
c906108c
SS
7995 case DW_TAG_structure_type:
7996 case DW_TAG_union_type:
f792889a 7997 this_type = read_structure_type (die, cu);
c906108c
SS
7998 break;
7999 case DW_TAG_enumeration_type:
f792889a 8000 this_type = read_enumeration_type (die, cu);
c906108c
SS
8001 break;
8002 case DW_TAG_subprogram:
8003 case DW_TAG_subroutine_type:
f792889a 8004 this_type = read_subroutine_type (die, cu);
c906108c
SS
8005 break;
8006 case DW_TAG_array_type:
f792889a 8007 this_type = read_array_type (die, cu);
c906108c 8008 break;
72019c9c 8009 case DW_TAG_set_type:
f792889a 8010 this_type = read_set_type (die, cu);
72019c9c 8011 break;
c906108c 8012 case DW_TAG_pointer_type:
f792889a 8013 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
8014 break;
8015 case DW_TAG_ptr_to_member_type:
f792889a 8016 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
8017 break;
8018 case DW_TAG_reference_type:
f792889a 8019 this_type = read_tag_reference_type (die, cu);
c906108c
SS
8020 break;
8021 case DW_TAG_const_type:
f792889a 8022 this_type = read_tag_const_type (die, cu);
c906108c
SS
8023 break;
8024 case DW_TAG_volatile_type:
f792889a 8025 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
8026 break;
8027 case DW_TAG_string_type:
f792889a 8028 this_type = read_tag_string_type (die, cu);
c906108c
SS
8029 break;
8030 case DW_TAG_typedef:
f792889a 8031 this_type = read_typedef (die, cu);
c906108c 8032 break;
a02abb62 8033 case DW_TAG_subrange_type:
f792889a 8034 this_type = read_subrange_type (die, cu);
a02abb62 8035 break;
c906108c 8036 case DW_TAG_base_type:
f792889a 8037 this_type = read_base_type (die, cu);
c906108c 8038 break;
81a17f79 8039 case DW_TAG_unspecified_type:
f792889a 8040 this_type = read_unspecified_type (die, cu);
81a17f79 8041 break;
0114d602
DJ
8042 case DW_TAG_namespace:
8043 this_type = read_namespace_type (die, cu);
8044 break;
c906108c 8045 default:
a1f5b845 8046 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 8047 dwarf_tag_name (die->tag));
c906108c
SS
8048 break;
8049 }
63d06c5c 8050
f792889a 8051 return this_type;
63d06c5c
DC
8052}
8053
fdde2d81 8054/* Return the name of the namespace/class that DIE is defined within,
0114d602 8055 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 8056
0114d602
DJ
8057 For example, if we're within the method foo() in the following
8058 code:
8059
8060 namespace N {
8061 class C {
8062 void foo () {
8063 }
8064 };
8065 }
8066
8067 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
8068
8069static char *
e142c38c 8070determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 8071{
0114d602
DJ
8072 struct die_info *parent, *spec_die;
8073 struct dwarf2_cu *spec_cu;
8074 struct type *parent_type;
63d06c5c 8075
987504bb
JJ
8076 if (cu->language != language_cplus
8077 && cu->language != language_java)
0114d602
DJ
8078 return "";
8079
8080 /* We have to be careful in the presence of DW_AT_specification.
8081 For example, with GCC 3.4, given the code
8082
8083 namespace N {
8084 void foo() {
8085 // Definition of N::foo.
8086 }
8087 }
8088
8089 then we'll have a tree of DIEs like this:
8090
8091 1: DW_TAG_compile_unit
8092 2: DW_TAG_namespace // N
8093 3: DW_TAG_subprogram // declaration of N::foo
8094 4: DW_TAG_subprogram // definition of N::foo
8095 DW_AT_specification // refers to die #3
8096
8097 Thus, when processing die #4, we have to pretend that we're in
8098 the context of its DW_AT_specification, namely the contex of die
8099 #3. */
8100 spec_cu = cu;
8101 spec_die = die_specification (die, &spec_cu);
8102 if (spec_die == NULL)
8103 parent = die->parent;
8104 else
63d06c5c 8105 {
0114d602
DJ
8106 parent = spec_die->parent;
8107 cu = spec_cu;
63d06c5c 8108 }
0114d602
DJ
8109
8110 if (parent == NULL)
8111 return "";
63d06c5c 8112 else
0114d602
DJ
8113 switch (parent->tag)
8114 {
63d06c5c 8115 case DW_TAG_namespace:
0114d602
DJ
8116 parent_type = read_type_die (parent, cu);
8117 /* We give a name to even anonymous namespaces. */
8118 return TYPE_TAG_NAME (parent_type);
63d06c5c 8119 case DW_TAG_class_type:
680b30c7 8120 case DW_TAG_interface_type:
63d06c5c 8121 case DW_TAG_structure_type:
0114d602
DJ
8122 case DW_TAG_union_type:
8123 parent_type = read_type_die (parent, cu);
8124 if (TYPE_TAG_NAME (parent_type) != NULL)
8125 return TYPE_TAG_NAME (parent_type);
8126 else
8127 /* An anonymous structure is only allowed non-static data
8128 members; no typedefs, no member functions, et cetera.
8129 So it does not need a prefix. */
8130 return "";
63d06c5c 8131 default:
8176b9b8 8132 return determine_prefix (parent, cu);
63d06c5c 8133 }
63d06c5c
DC
8134}
8135
987504bb
JJ
8136/* Return a newly-allocated string formed by concatenating PREFIX and
8137 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
8138 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
8139 perform an obconcat, otherwise allocate storage for the result. The CU argument
8140 is used to determine the language and hence, the appropriate separator. */
8141
8142#define MAX_SEP_LEN 2 /* sizeof ("::") */
63d06c5c
DC
8143
8144static char *
987504bb
JJ
8145typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
8146 struct dwarf2_cu *cu)
63d06c5c 8147{
987504bb 8148 char *sep;
63d06c5c 8149
987504bb
JJ
8150 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
8151 sep = "";
8152 else if (cu->language == language_java)
8153 sep = ".";
8154 else
8155 sep = "::";
63d06c5c 8156
6dd47d34
DE
8157 if (prefix == NULL)
8158 prefix = "";
8159 if (suffix == NULL)
8160 suffix = "";
8161
987504bb
JJ
8162 if (obs == NULL)
8163 {
8164 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
6dd47d34
DE
8165 strcpy (retval, prefix);
8166 strcat (retval, sep);
8167 strcat (retval, suffix);
63d06c5c
DC
8168 return retval;
8169 }
987504bb
JJ
8170 else
8171 {
8172 /* We have an obstack. */
8173 return obconcat (obs, prefix, sep, suffix);
8174 }
63d06c5c
DC
8175}
8176
c906108c
SS
8177/* Return sibling of die, NULL if no sibling. */
8178
f9aca02d 8179static struct die_info *
fba45db2 8180sibling_die (struct die_info *die)
c906108c 8181{
639d11d3 8182 return die->sibling;
c906108c
SS
8183}
8184
8185/* Get linkage name of a die, return NULL if not found. */
8186
8187static char *
e142c38c 8188dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8189{
8190 struct attribute *attr;
8191
e142c38c 8192 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
c906108c
SS
8193 if (attr && DW_STRING (attr))
8194 return DW_STRING (attr);
e142c38c 8195 attr = dwarf2_attr (die, DW_AT_name, cu);
c906108c
SS
8196 if (attr && DW_STRING (attr))
8197 return DW_STRING (attr);
8198 return NULL;
8199}
8200
9219021c
DC
8201/* Get name of a die, return NULL if not found. */
8202
8203static char *
e142c38c 8204dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
8205{
8206 struct attribute *attr;
8207
e142c38c 8208 attr = dwarf2_attr (die, DW_AT_name, cu);
9219021c
DC
8209 if (attr && DW_STRING (attr))
8210 return DW_STRING (attr);
8211 return NULL;
8212}
8213
8214/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
8215 is none. *EXT_CU is the CU containing DIE on input, and the CU
8216 containing the return value on output. */
9219021c
DC
8217
8218static struct die_info *
f2f0e013 8219dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
8220{
8221 struct attribute *attr;
9219021c 8222
f2f0e013 8223 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
8224 if (attr == NULL)
8225 return NULL;
8226
f2f0e013 8227 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
8228}
8229
c906108c
SS
8230/* Convert a DIE tag into its string name. */
8231
8232static char *
aa1ee363 8233dwarf_tag_name (unsigned tag)
c906108c
SS
8234{
8235 switch (tag)
8236 {
8237 case DW_TAG_padding:
8238 return "DW_TAG_padding";
8239 case DW_TAG_array_type:
8240 return "DW_TAG_array_type";
8241 case DW_TAG_class_type:
8242 return "DW_TAG_class_type";
8243 case DW_TAG_entry_point:
8244 return "DW_TAG_entry_point";
8245 case DW_TAG_enumeration_type:
8246 return "DW_TAG_enumeration_type";
8247 case DW_TAG_formal_parameter:
8248 return "DW_TAG_formal_parameter";
8249 case DW_TAG_imported_declaration:
8250 return "DW_TAG_imported_declaration";
8251 case DW_TAG_label:
8252 return "DW_TAG_label";
8253 case DW_TAG_lexical_block:
8254 return "DW_TAG_lexical_block";
8255 case DW_TAG_member:
8256 return "DW_TAG_member";
8257 case DW_TAG_pointer_type:
8258 return "DW_TAG_pointer_type";
8259 case DW_TAG_reference_type:
8260 return "DW_TAG_reference_type";
8261 case DW_TAG_compile_unit:
8262 return "DW_TAG_compile_unit";
8263 case DW_TAG_string_type:
8264 return "DW_TAG_string_type";
8265 case DW_TAG_structure_type:
8266 return "DW_TAG_structure_type";
8267 case DW_TAG_subroutine_type:
8268 return "DW_TAG_subroutine_type";
8269 case DW_TAG_typedef:
8270 return "DW_TAG_typedef";
8271 case DW_TAG_union_type:
8272 return "DW_TAG_union_type";
8273 case DW_TAG_unspecified_parameters:
8274 return "DW_TAG_unspecified_parameters";
8275 case DW_TAG_variant:
8276 return "DW_TAG_variant";
8277 case DW_TAG_common_block:
8278 return "DW_TAG_common_block";
8279 case DW_TAG_common_inclusion:
8280 return "DW_TAG_common_inclusion";
8281 case DW_TAG_inheritance:
8282 return "DW_TAG_inheritance";
8283 case DW_TAG_inlined_subroutine:
8284 return "DW_TAG_inlined_subroutine";
8285 case DW_TAG_module:
8286 return "DW_TAG_module";
8287 case DW_TAG_ptr_to_member_type:
8288 return "DW_TAG_ptr_to_member_type";
8289 case DW_TAG_set_type:
8290 return "DW_TAG_set_type";
8291 case DW_TAG_subrange_type:
8292 return "DW_TAG_subrange_type";
8293 case DW_TAG_with_stmt:
8294 return "DW_TAG_with_stmt";
8295 case DW_TAG_access_declaration:
8296 return "DW_TAG_access_declaration";
8297 case DW_TAG_base_type:
8298 return "DW_TAG_base_type";
8299 case DW_TAG_catch_block:
8300 return "DW_TAG_catch_block";
8301 case DW_TAG_const_type:
8302 return "DW_TAG_const_type";
8303 case DW_TAG_constant:
8304 return "DW_TAG_constant";
8305 case DW_TAG_enumerator:
8306 return "DW_TAG_enumerator";
8307 case DW_TAG_file_type:
8308 return "DW_TAG_file_type";
8309 case DW_TAG_friend:
8310 return "DW_TAG_friend";
8311 case DW_TAG_namelist:
8312 return "DW_TAG_namelist";
8313 case DW_TAG_namelist_item:
8314 return "DW_TAG_namelist_item";
8315 case DW_TAG_packed_type:
8316 return "DW_TAG_packed_type";
8317 case DW_TAG_subprogram:
8318 return "DW_TAG_subprogram";
8319 case DW_TAG_template_type_param:
8320 return "DW_TAG_template_type_param";
8321 case DW_TAG_template_value_param:
8322 return "DW_TAG_template_value_param";
8323 case DW_TAG_thrown_type:
8324 return "DW_TAG_thrown_type";
8325 case DW_TAG_try_block:
8326 return "DW_TAG_try_block";
8327 case DW_TAG_variant_part:
8328 return "DW_TAG_variant_part";
8329 case DW_TAG_variable:
8330 return "DW_TAG_variable";
8331 case DW_TAG_volatile_type:
8332 return "DW_TAG_volatile_type";
d9fa45fe
DC
8333 case DW_TAG_dwarf_procedure:
8334 return "DW_TAG_dwarf_procedure";
8335 case DW_TAG_restrict_type:
8336 return "DW_TAG_restrict_type";
8337 case DW_TAG_interface_type:
8338 return "DW_TAG_interface_type";
8339 case DW_TAG_namespace:
8340 return "DW_TAG_namespace";
8341 case DW_TAG_imported_module:
8342 return "DW_TAG_imported_module";
8343 case DW_TAG_unspecified_type:
8344 return "DW_TAG_unspecified_type";
8345 case DW_TAG_partial_unit:
8346 return "DW_TAG_partial_unit";
8347 case DW_TAG_imported_unit:
8348 return "DW_TAG_imported_unit";
b7619582
GF
8349 case DW_TAG_condition:
8350 return "DW_TAG_condition";
8351 case DW_TAG_shared_type:
8352 return "DW_TAG_shared_type";
c906108c
SS
8353 case DW_TAG_MIPS_loop:
8354 return "DW_TAG_MIPS_loop";
b7619582
GF
8355 case DW_TAG_HP_array_descriptor:
8356 return "DW_TAG_HP_array_descriptor";
c906108c
SS
8357 case DW_TAG_format_label:
8358 return "DW_TAG_format_label";
8359 case DW_TAG_function_template:
8360 return "DW_TAG_function_template";
8361 case DW_TAG_class_template:
8362 return "DW_TAG_class_template";
b7619582
GF
8363 case DW_TAG_GNU_BINCL:
8364 return "DW_TAG_GNU_BINCL";
8365 case DW_TAG_GNU_EINCL:
8366 return "DW_TAG_GNU_EINCL";
8367 case DW_TAG_upc_shared_type:
8368 return "DW_TAG_upc_shared_type";
8369 case DW_TAG_upc_strict_type:
8370 return "DW_TAG_upc_strict_type";
8371 case DW_TAG_upc_relaxed_type:
8372 return "DW_TAG_upc_relaxed_type";
8373 case DW_TAG_PGI_kanji_type:
8374 return "DW_TAG_PGI_kanji_type";
8375 case DW_TAG_PGI_interface_block:
8376 return "DW_TAG_PGI_interface_block";
c906108c
SS
8377 default:
8378 return "DW_TAG_<unknown>";
8379 }
8380}
8381
8382/* Convert a DWARF attribute code into its string name. */
8383
8384static char *
aa1ee363 8385dwarf_attr_name (unsigned attr)
c906108c
SS
8386{
8387 switch (attr)
8388 {
8389 case DW_AT_sibling:
8390 return "DW_AT_sibling";
8391 case DW_AT_location:
8392 return "DW_AT_location";
8393 case DW_AT_name:
8394 return "DW_AT_name";
8395 case DW_AT_ordering:
8396 return "DW_AT_ordering";
8397 case DW_AT_subscr_data:
8398 return "DW_AT_subscr_data";
8399 case DW_AT_byte_size:
8400 return "DW_AT_byte_size";
8401 case DW_AT_bit_offset:
8402 return "DW_AT_bit_offset";
8403 case DW_AT_bit_size:
8404 return "DW_AT_bit_size";
8405 case DW_AT_element_list:
8406 return "DW_AT_element_list";
8407 case DW_AT_stmt_list:
8408 return "DW_AT_stmt_list";
8409 case DW_AT_low_pc:
8410 return "DW_AT_low_pc";
8411 case DW_AT_high_pc:
8412 return "DW_AT_high_pc";
8413 case DW_AT_language:
8414 return "DW_AT_language";
8415 case DW_AT_member:
8416 return "DW_AT_member";
8417 case DW_AT_discr:
8418 return "DW_AT_discr";
8419 case DW_AT_discr_value:
8420 return "DW_AT_discr_value";
8421 case DW_AT_visibility:
8422 return "DW_AT_visibility";
8423 case DW_AT_import:
8424 return "DW_AT_import";
8425 case DW_AT_string_length:
8426 return "DW_AT_string_length";
8427 case DW_AT_common_reference:
8428 return "DW_AT_common_reference";
8429 case DW_AT_comp_dir:
8430 return "DW_AT_comp_dir";
8431 case DW_AT_const_value:
8432 return "DW_AT_const_value";
8433 case DW_AT_containing_type:
8434 return "DW_AT_containing_type";
8435 case DW_AT_default_value:
8436 return "DW_AT_default_value";
8437 case DW_AT_inline:
8438 return "DW_AT_inline";
8439 case DW_AT_is_optional:
8440 return "DW_AT_is_optional";
8441 case DW_AT_lower_bound:
8442 return "DW_AT_lower_bound";
8443 case DW_AT_producer:
8444 return "DW_AT_producer";
8445 case DW_AT_prototyped:
8446 return "DW_AT_prototyped";
8447 case DW_AT_return_addr:
8448 return "DW_AT_return_addr";
8449 case DW_AT_start_scope:
8450 return "DW_AT_start_scope";
09fa0d7c
JK
8451 case DW_AT_bit_stride:
8452 return "DW_AT_bit_stride";
c906108c
SS
8453 case DW_AT_upper_bound:
8454 return "DW_AT_upper_bound";
8455 case DW_AT_abstract_origin:
8456 return "DW_AT_abstract_origin";
8457 case DW_AT_accessibility:
8458 return "DW_AT_accessibility";
8459 case DW_AT_address_class:
8460 return "DW_AT_address_class";
8461 case DW_AT_artificial:
8462 return "DW_AT_artificial";
8463 case DW_AT_base_types:
8464 return "DW_AT_base_types";
8465 case DW_AT_calling_convention:
8466 return "DW_AT_calling_convention";
8467 case DW_AT_count:
8468 return "DW_AT_count";
8469 case DW_AT_data_member_location:
8470 return "DW_AT_data_member_location";
8471 case DW_AT_decl_column:
8472 return "DW_AT_decl_column";
8473 case DW_AT_decl_file:
8474 return "DW_AT_decl_file";
8475 case DW_AT_decl_line:
8476 return "DW_AT_decl_line";
8477 case DW_AT_declaration:
8478 return "DW_AT_declaration";
8479 case DW_AT_discr_list:
8480 return "DW_AT_discr_list";
8481 case DW_AT_encoding:
8482 return "DW_AT_encoding";
8483 case DW_AT_external:
8484 return "DW_AT_external";
8485 case DW_AT_frame_base:
8486 return "DW_AT_frame_base";
8487 case DW_AT_friend:
8488 return "DW_AT_friend";
8489 case DW_AT_identifier_case:
8490 return "DW_AT_identifier_case";
8491 case DW_AT_macro_info:
8492 return "DW_AT_macro_info";
8493 case DW_AT_namelist_items:
8494 return "DW_AT_namelist_items";
8495 case DW_AT_priority:
8496 return "DW_AT_priority";
8497 case DW_AT_segment:
8498 return "DW_AT_segment";
8499 case DW_AT_specification:
8500 return "DW_AT_specification";
8501 case DW_AT_static_link:
8502 return "DW_AT_static_link";
8503 case DW_AT_type:
8504 return "DW_AT_type";
8505 case DW_AT_use_location:
8506 return "DW_AT_use_location";
8507 case DW_AT_variable_parameter:
8508 return "DW_AT_variable_parameter";
8509 case DW_AT_virtuality:
8510 return "DW_AT_virtuality";
8511 case DW_AT_vtable_elem_location:
8512 return "DW_AT_vtable_elem_location";
b7619582 8513 /* DWARF 3 values. */
d9fa45fe
DC
8514 case DW_AT_allocated:
8515 return "DW_AT_allocated";
8516 case DW_AT_associated:
8517 return "DW_AT_associated";
8518 case DW_AT_data_location:
8519 return "DW_AT_data_location";
09fa0d7c
JK
8520 case DW_AT_byte_stride:
8521 return "DW_AT_byte_stride";
d9fa45fe
DC
8522 case DW_AT_entry_pc:
8523 return "DW_AT_entry_pc";
8524 case DW_AT_use_UTF8:
8525 return "DW_AT_use_UTF8";
8526 case DW_AT_extension:
8527 return "DW_AT_extension";
8528 case DW_AT_ranges:
8529 return "DW_AT_ranges";
8530 case DW_AT_trampoline:
8531 return "DW_AT_trampoline";
8532 case DW_AT_call_column:
8533 return "DW_AT_call_column";
8534 case DW_AT_call_file:
8535 return "DW_AT_call_file";
8536 case DW_AT_call_line:
8537 return "DW_AT_call_line";
b7619582
GF
8538 case DW_AT_description:
8539 return "DW_AT_description";
8540 case DW_AT_binary_scale:
8541 return "DW_AT_binary_scale";
8542 case DW_AT_decimal_scale:
8543 return "DW_AT_decimal_scale";
8544 case DW_AT_small:
8545 return "DW_AT_small";
8546 case DW_AT_decimal_sign:
8547 return "DW_AT_decimal_sign";
8548 case DW_AT_digit_count:
8549 return "DW_AT_digit_count";
8550 case DW_AT_picture_string:
8551 return "DW_AT_picture_string";
8552 case DW_AT_mutable:
8553 return "DW_AT_mutable";
8554 case DW_AT_threads_scaled:
8555 return "DW_AT_threads_scaled";
8556 case DW_AT_explicit:
8557 return "DW_AT_explicit";
8558 case DW_AT_object_pointer:
8559 return "DW_AT_object_pointer";
8560 case DW_AT_endianity:
8561 return "DW_AT_endianity";
8562 case DW_AT_elemental:
8563 return "DW_AT_elemental";
8564 case DW_AT_pure:
8565 return "DW_AT_pure";
8566 case DW_AT_recursive:
8567 return "DW_AT_recursive";
b7619582 8568 /* SGI/MIPS extensions. */
c764a876 8569#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
8570 case DW_AT_MIPS_fde:
8571 return "DW_AT_MIPS_fde";
c764a876 8572#endif
c906108c
SS
8573 case DW_AT_MIPS_loop_begin:
8574 return "DW_AT_MIPS_loop_begin";
8575 case DW_AT_MIPS_tail_loop_begin:
8576 return "DW_AT_MIPS_tail_loop_begin";
8577 case DW_AT_MIPS_epilog_begin:
8578 return "DW_AT_MIPS_epilog_begin";
8579 case DW_AT_MIPS_loop_unroll_factor:
8580 return "DW_AT_MIPS_loop_unroll_factor";
8581 case DW_AT_MIPS_software_pipeline_depth:
8582 return "DW_AT_MIPS_software_pipeline_depth";
8583 case DW_AT_MIPS_linkage_name:
8584 return "DW_AT_MIPS_linkage_name";
b7619582
GF
8585 case DW_AT_MIPS_stride:
8586 return "DW_AT_MIPS_stride";
8587 case DW_AT_MIPS_abstract_name:
8588 return "DW_AT_MIPS_abstract_name";
8589 case DW_AT_MIPS_clone_origin:
8590 return "DW_AT_MIPS_clone_origin";
8591 case DW_AT_MIPS_has_inlines:
8592 return "DW_AT_MIPS_has_inlines";
b7619582 8593 /* HP extensions. */
c764a876 8594#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
8595 case DW_AT_HP_block_index:
8596 return "DW_AT_HP_block_index";
c764a876 8597#endif
b7619582
GF
8598 case DW_AT_HP_unmodifiable:
8599 return "DW_AT_HP_unmodifiable";
8600 case DW_AT_HP_actuals_stmt_list:
8601 return "DW_AT_HP_actuals_stmt_list";
8602 case DW_AT_HP_proc_per_section:
8603 return "DW_AT_HP_proc_per_section";
8604 case DW_AT_HP_raw_data_ptr:
8605 return "DW_AT_HP_raw_data_ptr";
8606 case DW_AT_HP_pass_by_reference:
8607 return "DW_AT_HP_pass_by_reference";
8608 case DW_AT_HP_opt_level:
8609 return "DW_AT_HP_opt_level";
8610 case DW_AT_HP_prof_version_id:
8611 return "DW_AT_HP_prof_version_id";
8612 case DW_AT_HP_opt_flags:
8613 return "DW_AT_HP_opt_flags";
8614 case DW_AT_HP_cold_region_low_pc:
8615 return "DW_AT_HP_cold_region_low_pc";
8616 case DW_AT_HP_cold_region_high_pc:
8617 return "DW_AT_HP_cold_region_high_pc";
8618 case DW_AT_HP_all_variables_modifiable:
8619 return "DW_AT_HP_all_variables_modifiable";
8620 case DW_AT_HP_linkage_name:
8621 return "DW_AT_HP_linkage_name";
8622 case DW_AT_HP_prof_flags:
8623 return "DW_AT_HP_prof_flags";
8624 /* GNU extensions. */
c906108c
SS
8625 case DW_AT_sf_names:
8626 return "DW_AT_sf_names";
8627 case DW_AT_src_info:
8628 return "DW_AT_src_info";
8629 case DW_AT_mac_info:
8630 return "DW_AT_mac_info";
8631 case DW_AT_src_coords:
8632 return "DW_AT_src_coords";
8633 case DW_AT_body_begin:
8634 return "DW_AT_body_begin";
8635 case DW_AT_body_end:
8636 return "DW_AT_body_end";
f5f8a009
EZ
8637 case DW_AT_GNU_vector:
8638 return "DW_AT_GNU_vector";
b7619582
GF
8639 /* VMS extensions. */
8640 case DW_AT_VMS_rtnbeg_pd_address:
8641 return "DW_AT_VMS_rtnbeg_pd_address";
8642 /* UPC extension. */
8643 case DW_AT_upc_threads_scaled:
8644 return "DW_AT_upc_threads_scaled";
8645 /* PGI (STMicroelectronics) extensions. */
8646 case DW_AT_PGI_lbase:
8647 return "DW_AT_PGI_lbase";
8648 case DW_AT_PGI_soffset:
8649 return "DW_AT_PGI_soffset";
8650 case DW_AT_PGI_lstride:
8651 return "DW_AT_PGI_lstride";
c906108c
SS
8652 default:
8653 return "DW_AT_<unknown>";
8654 }
8655}
8656
8657/* Convert a DWARF value form code into its string name. */
8658
8659static char *
aa1ee363 8660dwarf_form_name (unsigned form)
c906108c
SS
8661{
8662 switch (form)
8663 {
8664 case DW_FORM_addr:
8665 return "DW_FORM_addr";
8666 case DW_FORM_block2:
8667 return "DW_FORM_block2";
8668 case DW_FORM_block4:
8669 return "DW_FORM_block4";
8670 case DW_FORM_data2:
8671 return "DW_FORM_data2";
8672 case DW_FORM_data4:
8673 return "DW_FORM_data4";
8674 case DW_FORM_data8:
8675 return "DW_FORM_data8";
8676 case DW_FORM_string:
8677 return "DW_FORM_string";
8678 case DW_FORM_block:
8679 return "DW_FORM_block";
8680 case DW_FORM_block1:
8681 return "DW_FORM_block1";
8682 case DW_FORM_data1:
8683 return "DW_FORM_data1";
8684 case DW_FORM_flag:
8685 return "DW_FORM_flag";
8686 case DW_FORM_sdata:
8687 return "DW_FORM_sdata";
8688 case DW_FORM_strp:
8689 return "DW_FORM_strp";
8690 case DW_FORM_udata:
8691 return "DW_FORM_udata";
8692 case DW_FORM_ref_addr:
8693 return "DW_FORM_ref_addr";
8694 case DW_FORM_ref1:
8695 return "DW_FORM_ref1";
8696 case DW_FORM_ref2:
8697 return "DW_FORM_ref2";
8698 case DW_FORM_ref4:
8699 return "DW_FORM_ref4";
8700 case DW_FORM_ref8:
8701 return "DW_FORM_ref8";
8702 case DW_FORM_ref_udata:
8703 return "DW_FORM_ref_udata";
8704 case DW_FORM_indirect:
8705 return "DW_FORM_indirect";
8706 default:
8707 return "DW_FORM_<unknown>";
8708 }
8709}
8710
8711/* Convert a DWARF stack opcode into its string name. */
8712
8713static char *
aa1ee363 8714dwarf_stack_op_name (unsigned op)
c906108c
SS
8715{
8716 switch (op)
8717 {
8718 case DW_OP_addr:
8719 return "DW_OP_addr";
8720 case DW_OP_deref:
8721 return "DW_OP_deref";
8722 case DW_OP_const1u:
8723 return "DW_OP_const1u";
8724 case DW_OP_const1s:
8725 return "DW_OP_const1s";
8726 case DW_OP_const2u:
8727 return "DW_OP_const2u";
8728 case DW_OP_const2s:
8729 return "DW_OP_const2s";
8730 case DW_OP_const4u:
8731 return "DW_OP_const4u";
8732 case DW_OP_const4s:
8733 return "DW_OP_const4s";
8734 case DW_OP_const8u:
8735 return "DW_OP_const8u";
8736 case DW_OP_const8s:
8737 return "DW_OP_const8s";
8738 case DW_OP_constu:
8739 return "DW_OP_constu";
8740 case DW_OP_consts:
8741 return "DW_OP_consts";
8742 case DW_OP_dup:
8743 return "DW_OP_dup";
8744 case DW_OP_drop:
8745 return "DW_OP_drop";
8746 case DW_OP_over:
8747 return "DW_OP_over";
8748 case DW_OP_pick:
8749 return "DW_OP_pick";
8750 case DW_OP_swap:
8751 return "DW_OP_swap";
8752 case DW_OP_rot:
8753 return "DW_OP_rot";
8754 case DW_OP_xderef:
8755 return "DW_OP_xderef";
8756 case DW_OP_abs:
8757 return "DW_OP_abs";
8758 case DW_OP_and:
8759 return "DW_OP_and";
8760 case DW_OP_div:
8761 return "DW_OP_div";
8762 case DW_OP_minus:
8763 return "DW_OP_minus";
8764 case DW_OP_mod:
8765 return "DW_OP_mod";
8766 case DW_OP_mul:
8767 return "DW_OP_mul";
8768 case DW_OP_neg:
8769 return "DW_OP_neg";
8770 case DW_OP_not:
8771 return "DW_OP_not";
8772 case DW_OP_or:
8773 return "DW_OP_or";
8774 case DW_OP_plus:
8775 return "DW_OP_plus";
8776 case DW_OP_plus_uconst:
8777 return "DW_OP_plus_uconst";
8778 case DW_OP_shl:
8779 return "DW_OP_shl";
8780 case DW_OP_shr:
8781 return "DW_OP_shr";
8782 case DW_OP_shra:
8783 return "DW_OP_shra";
8784 case DW_OP_xor:
8785 return "DW_OP_xor";
8786 case DW_OP_bra:
8787 return "DW_OP_bra";
8788 case DW_OP_eq:
8789 return "DW_OP_eq";
8790 case DW_OP_ge:
8791 return "DW_OP_ge";
8792 case DW_OP_gt:
8793 return "DW_OP_gt";
8794 case DW_OP_le:
8795 return "DW_OP_le";
8796 case DW_OP_lt:
8797 return "DW_OP_lt";
8798 case DW_OP_ne:
8799 return "DW_OP_ne";
8800 case DW_OP_skip:
8801 return "DW_OP_skip";
8802 case DW_OP_lit0:
8803 return "DW_OP_lit0";
8804 case DW_OP_lit1:
8805 return "DW_OP_lit1";
8806 case DW_OP_lit2:
8807 return "DW_OP_lit2";
8808 case DW_OP_lit3:
8809 return "DW_OP_lit3";
8810 case DW_OP_lit4:
8811 return "DW_OP_lit4";
8812 case DW_OP_lit5:
8813 return "DW_OP_lit5";
8814 case DW_OP_lit6:
8815 return "DW_OP_lit6";
8816 case DW_OP_lit7:
8817 return "DW_OP_lit7";
8818 case DW_OP_lit8:
8819 return "DW_OP_lit8";
8820 case DW_OP_lit9:
8821 return "DW_OP_lit9";
8822 case DW_OP_lit10:
8823 return "DW_OP_lit10";
8824 case DW_OP_lit11:
8825 return "DW_OP_lit11";
8826 case DW_OP_lit12:
8827 return "DW_OP_lit12";
8828 case DW_OP_lit13:
8829 return "DW_OP_lit13";
8830 case DW_OP_lit14:
8831 return "DW_OP_lit14";
8832 case DW_OP_lit15:
8833 return "DW_OP_lit15";
8834 case DW_OP_lit16:
8835 return "DW_OP_lit16";
8836 case DW_OP_lit17:
8837 return "DW_OP_lit17";
8838 case DW_OP_lit18:
8839 return "DW_OP_lit18";
8840 case DW_OP_lit19:
8841 return "DW_OP_lit19";
8842 case DW_OP_lit20:
8843 return "DW_OP_lit20";
8844 case DW_OP_lit21:
8845 return "DW_OP_lit21";
8846 case DW_OP_lit22:
8847 return "DW_OP_lit22";
8848 case DW_OP_lit23:
8849 return "DW_OP_lit23";
8850 case DW_OP_lit24:
8851 return "DW_OP_lit24";
8852 case DW_OP_lit25:
8853 return "DW_OP_lit25";
8854 case DW_OP_lit26:
8855 return "DW_OP_lit26";
8856 case DW_OP_lit27:
8857 return "DW_OP_lit27";
8858 case DW_OP_lit28:
8859 return "DW_OP_lit28";
8860 case DW_OP_lit29:
8861 return "DW_OP_lit29";
8862 case DW_OP_lit30:
8863 return "DW_OP_lit30";
8864 case DW_OP_lit31:
8865 return "DW_OP_lit31";
8866 case DW_OP_reg0:
8867 return "DW_OP_reg0";
8868 case DW_OP_reg1:
8869 return "DW_OP_reg1";
8870 case DW_OP_reg2:
8871 return "DW_OP_reg2";
8872 case DW_OP_reg3:
8873 return "DW_OP_reg3";
8874 case DW_OP_reg4:
8875 return "DW_OP_reg4";
8876 case DW_OP_reg5:
8877 return "DW_OP_reg5";
8878 case DW_OP_reg6:
8879 return "DW_OP_reg6";
8880 case DW_OP_reg7:
8881 return "DW_OP_reg7";
8882 case DW_OP_reg8:
8883 return "DW_OP_reg8";
8884 case DW_OP_reg9:
8885 return "DW_OP_reg9";
8886 case DW_OP_reg10:
8887 return "DW_OP_reg10";
8888 case DW_OP_reg11:
8889 return "DW_OP_reg11";
8890 case DW_OP_reg12:
8891 return "DW_OP_reg12";
8892 case DW_OP_reg13:
8893 return "DW_OP_reg13";
8894 case DW_OP_reg14:
8895 return "DW_OP_reg14";
8896 case DW_OP_reg15:
8897 return "DW_OP_reg15";
8898 case DW_OP_reg16:
8899 return "DW_OP_reg16";
8900 case DW_OP_reg17:
8901 return "DW_OP_reg17";
8902 case DW_OP_reg18:
8903 return "DW_OP_reg18";
8904 case DW_OP_reg19:
8905 return "DW_OP_reg19";
8906 case DW_OP_reg20:
8907 return "DW_OP_reg20";
8908 case DW_OP_reg21:
8909 return "DW_OP_reg21";
8910 case DW_OP_reg22:
8911 return "DW_OP_reg22";
8912 case DW_OP_reg23:
8913 return "DW_OP_reg23";
8914 case DW_OP_reg24:
8915 return "DW_OP_reg24";
8916 case DW_OP_reg25:
8917 return "DW_OP_reg25";
8918 case DW_OP_reg26:
8919 return "DW_OP_reg26";
8920 case DW_OP_reg27:
8921 return "DW_OP_reg27";
8922 case DW_OP_reg28:
8923 return "DW_OP_reg28";
8924 case DW_OP_reg29:
8925 return "DW_OP_reg29";
8926 case DW_OP_reg30:
8927 return "DW_OP_reg30";
8928 case DW_OP_reg31:
8929 return "DW_OP_reg31";
8930 case DW_OP_breg0:
8931 return "DW_OP_breg0";
8932 case DW_OP_breg1:
8933 return "DW_OP_breg1";
8934 case DW_OP_breg2:
8935 return "DW_OP_breg2";
8936 case DW_OP_breg3:
8937 return "DW_OP_breg3";
8938 case DW_OP_breg4:
8939 return "DW_OP_breg4";
8940 case DW_OP_breg5:
8941 return "DW_OP_breg5";
8942 case DW_OP_breg6:
8943 return "DW_OP_breg6";
8944 case DW_OP_breg7:
8945 return "DW_OP_breg7";
8946 case DW_OP_breg8:
8947 return "DW_OP_breg8";
8948 case DW_OP_breg9:
8949 return "DW_OP_breg9";
8950 case DW_OP_breg10:
8951 return "DW_OP_breg10";
8952 case DW_OP_breg11:
8953 return "DW_OP_breg11";
8954 case DW_OP_breg12:
8955 return "DW_OP_breg12";
8956 case DW_OP_breg13:
8957 return "DW_OP_breg13";
8958 case DW_OP_breg14:
8959 return "DW_OP_breg14";
8960 case DW_OP_breg15:
8961 return "DW_OP_breg15";
8962 case DW_OP_breg16:
8963 return "DW_OP_breg16";
8964 case DW_OP_breg17:
8965 return "DW_OP_breg17";
8966 case DW_OP_breg18:
8967 return "DW_OP_breg18";
8968 case DW_OP_breg19:
8969 return "DW_OP_breg19";
8970 case DW_OP_breg20:
8971 return "DW_OP_breg20";
8972 case DW_OP_breg21:
8973 return "DW_OP_breg21";
8974 case DW_OP_breg22:
8975 return "DW_OP_breg22";
8976 case DW_OP_breg23:
8977 return "DW_OP_breg23";
8978 case DW_OP_breg24:
8979 return "DW_OP_breg24";
8980 case DW_OP_breg25:
8981 return "DW_OP_breg25";
8982 case DW_OP_breg26:
8983 return "DW_OP_breg26";
8984 case DW_OP_breg27:
8985 return "DW_OP_breg27";
8986 case DW_OP_breg28:
8987 return "DW_OP_breg28";
8988 case DW_OP_breg29:
8989 return "DW_OP_breg29";
8990 case DW_OP_breg30:
8991 return "DW_OP_breg30";
8992 case DW_OP_breg31:
8993 return "DW_OP_breg31";
8994 case DW_OP_regx:
8995 return "DW_OP_regx";
8996 case DW_OP_fbreg:
8997 return "DW_OP_fbreg";
8998 case DW_OP_bregx:
8999 return "DW_OP_bregx";
9000 case DW_OP_piece:
9001 return "DW_OP_piece";
9002 case DW_OP_deref_size:
9003 return "DW_OP_deref_size";
9004 case DW_OP_xderef_size:
9005 return "DW_OP_xderef_size";
9006 case DW_OP_nop:
9007 return "DW_OP_nop";
b7619582 9008 /* DWARF 3 extensions. */
ed348acc
EZ
9009 case DW_OP_push_object_address:
9010 return "DW_OP_push_object_address";
9011 case DW_OP_call2:
9012 return "DW_OP_call2";
9013 case DW_OP_call4:
9014 return "DW_OP_call4";
9015 case DW_OP_call_ref:
9016 return "DW_OP_call_ref";
b7619582
GF
9017 /* GNU extensions. */
9018 case DW_OP_form_tls_address:
9019 return "DW_OP_form_tls_address";
9020 case DW_OP_call_frame_cfa:
9021 return "DW_OP_call_frame_cfa";
9022 case DW_OP_bit_piece:
9023 return "DW_OP_bit_piece";
ed348acc
EZ
9024 case DW_OP_GNU_push_tls_address:
9025 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
9026 case DW_OP_GNU_uninit:
9027 return "DW_OP_GNU_uninit";
b7619582
GF
9028 /* HP extensions. */
9029 case DW_OP_HP_is_value:
9030 return "DW_OP_HP_is_value";
9031 case DW_OP_HP_fltconst4:
9032 return "DW_OP_HP_fltconst4";
9033 case DW_OP_HP_fltconst8:
9034 return "DW_OP_HP_fltconst8";
9035 case DW_OP_HP_mod_range:
9036 return "DW_OP_HP_mod_range";
9037 case DW_OP_HP_unmod_range:
9038 return "DW_OP_HP_unmod_range";
9039 case DW_OP_HP_tls:
9040 return "DW_OP_HP_tls";
c906108c
SS
9041 default:
9042 return "OP_<unknown>";
9043 }
9044}
9045
9046static char *
fba45db2 9047dwarf_bool_name (unsigned mybool)
c906108c
SS
9048{
9049 if (mybool)
9050 return "TRUE";
9051 else
9052 return "FALSE";
9053}
9054
9055/* Convert a DWARF type code into its string name. */
9056
9057static char *
aa1ee363 9058dwarf_type_encoding_name (unsigned enc)
c906108c
SS
9059{
9060 switch (enc)
9061 {
b7619582
GF
9062 case DW_ATE_void:
9063 return "DW_ATE_void";
c906108c
SS
9064 case DW_ATE_address:
9065 return "DW_ATE_address";
9066 case DW_ATE_boolean:
9067 return "DW_ATE_boolean";
9068 case DW_ATE_complex_float:
9069 return "DW_ATE_complex_float";
9070 case DW_ATE_float:
9071 return "DW_ATE_float";
9072 case DW_ATE_signed:
9073 return "DW_ATE_signed";
9074 case DW_ATE_signed_char:
9075 return "DW_ATE_signed_char";
9076 case DW_ATE_unsigned:
9077 return "DW_ATE_unsigned";
9078 case DW_ATE_unsigned_char:
9079 return "DW_ATE_unsigned_char";
b7619582 9080 /* DWARF 3. */
d9fa45fe
DC
9081 case DW_ATE_imaginary_float:
9082 return "DW_ATE_imaginary_float";
b7619582
GF
9083 case DW_ATE_packed_decimal:
9084 return "DW_ATE_packed_decimal";
9085 case DW_ATE_numeric_string:
9086 return "DW_ATE_numeric_string";
9087 case DW_ATE_edited:
9088 return "DW_ATE_edited";
9089 case DW_ATE_signed_fixed:
9090 return "DW_ATE_signed_fixed";
9091 case DW_ATE_unsigned_fixed:
9092 return "DW_ATE_unsigned_fixed";
9093 case DW_ATE_decimal_float:
9094 return "DW_ATE_decimal_float";
9095 /* HP extensions. */
9096 case DW_ATE_HP_float80:
9097 return "DW_ATE_HP_float80";
9098 case DW_ATE_HP_complex_float80:
9099 return "DW_ATE_HP_complex_float80";
9100 case DW_ATE_HP_float128:
9101 return "DW_ATE_HP_float128";
9102 case DW_ATE_HP_complex_float128:
9103 return "DW_ATE_HP_complex_float128";
9104 case DW_ATE_HP_floathpintel:
9105 return "DW_ATE_HP_floathpintel";
9106 case DW_ATE_HP_imaginary_float80:
9107 return "DW_ATE_HP_imaginary_float80";
9108 case DW_ATE_HP_imaginary_float128:
9109 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
9110 default:
9111 return "DW_ATE_<unknown>";
9112 }
9113}
9114
9115/* Convert a DWARF call frame info operation to its string name. */
9116
9117#if 0
9118static char *
aa1ee363 9119dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
9120{
9121 switch (cfi_opc)
9122 {
9123 case DW_CFA_advance_loc:
9124 return "DW_CFA_advance_loc";
9125 case DW_CFA_offset:
9126 return "DW_CFA_offset";
9127 case DW_CFA_restore:
9128 return "DW_CFA_restore";
9129 case DW_CFA_nop:
9130 return "DW_CFA_nop";
9131 case DW_CFA_set_loc:
9132 return "DW_CFA_set_loc";
9133 case DW_CFA_advance_loc1:
9134 return "DW_CFA_advance_loc1";
9135 case DW_CFA_advance_loc2:
9136 return "DW_CFA_advance_loc2";
9137 case DW_CFA_advance_loc4:
9138 return "DW_CFA_advance_loc4";
9139 case DW_CFA_offset_extended:
9140 return "DW_CFA_offset_extended";
9141 case DW_CFA_restore_extended:
9142 return "DW_CFA_restore_extended";
9143 case DW_CFA_undefined:
9144 return "DW_CFA_undefined";
9145 case DW_CFA_same_value:
9146 return "DW_CFA_same_value";
9147 case DW_CFA_register:
9148 return "DW_CFA_register";
9149 case DW_CFA_remember_state:
9150 return "DW_CFA_remember_state";
9151 case DW_CFA_restore_state:
9152 return "DW_CFA_restore_state";
9153 case DW_CFA_def_cfa:
9154 return "DW_CFA_def_cfa";
9155 case DW_CFA_def_cfa_register:
9156 return "DW_CFA_def_cfa_register";
9157 case DW_CFA_def_cfa_offset:
9158 return "DW_CFA_def_cfa_offset";
b7619582 9159 /* DWARF 3. */
985cb1a3
JM
9160 case DW_CFA_def_cfa_expression:
9161 return "DW_CFA_def_cfa_expression";
9162 case DW_CFA_expression:
9163 return "DW_CFA_expression";
9164 case DW_CFA_offset_extended_sf:
9165 return "DW_CFA_offset_extended_sf";
9166 case DW_CFA_def_cfa_sf:
9167 return "DW_CFA_def_cfa_sf";
9168 case DW_CFA_def_cfa_offset_sf:
9169 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
9170 case DW_CFA_val_offset:
9171 return "DW_CFA_val_offset";
9172 case DW_CFA_val_offset_sf:
9173 return "DW_CFA_val_offset_sf";
9174 case DW_CFA_val_expression:
9175 return "DW_CFA_val_expression";
9176 /* SGI/MIPS specific. */
c906108c
SS
9177 case DW_CFA_MIPS_advance_loc8:
9178 return "DW_CFA_MIPS_advance_loc8";
b7619582 9179 /* GNU extensions. */
985cb1a3
JM
9180 case DW_CFA_GNU_window_save:
9181 return "DW_CFA_GNU_window_save";
9182 case DW_CFA_GNU_args_size:
9183 return "DW_CFA_GNU_args_size";
9184 case DW_CFA_GNU_negative_offset_extended:
9185 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
9186 default:
9187 return "DW_CFA_<unknown>";
9188 }
9189}
9190#endif
9191
f9aca02d 9192static void
d97bc12b 9193dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
9194{
9195 unsigned int i;
9196
d97bc12b
DE
9197 print_spaces (indent, f);
9198 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 9199 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
9200
9201 if (die->parent != NULL)
9202 {
9203 print_spaces (indent, f);
9204 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9205 die->parent->offset);
9206 }
9207
9208 print_spaces (indent, f);
9209 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 9210 dwarf_bool_name (die->child != NULL));
c906108c 9211
d97bc12b
DE
9212 print_spaces (indent, f);
9213 fprintf_unfiltered (f, " attributes:\n");
9214
c906108c
SS
9215 for (i = 0; i < die->num_attrs; ++i)
9216 {
d97bc12b
DE
9217 print_spaces (indent, f);
9218 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
9219 dwarf_attr_name (die->attrs[i].name),
9220 dwarf_form_name (die->attrs[i].form));
d97bc12b 9221
c906108c
SS
9222 switch (die->attrs[i].form)
9223 {
9224 case DW_FORM_ref_addr:
9225 case DW_FORM_addr:
d97bc12b
DE
9226 fprintf_unfiltered (f, "address: ");
9227 fputs_filtered (paddress (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
9228 break;
9229 case DW_FORM_block2:
9230 case DW_FORM_block4:
9231 case DW_FORM_block:
9232 case DW_FORM_block1:
d97bc12b 9233 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 9234 break;
10b3939b
DJ
9235 case DW_FORM_ref1:
9236 case DW_FORM_ref2:
9237 case DW_FORM_ref4:
d97bc12b 9238 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
9239 (long) (DW_ADDR (&die->attrs[i])));
9240 break;
c906108c
SS
9241 case DW_FORM_data1:
9242 case DW_FORM_data2:
9243 case DW_FORM_data4:
ce5d95e1 9244 case DW_FORM_data8:
c906108c
SS
9245 case DW_FORM_udata:
9246 case DW_FORM_sdata:
d97bc12b 9247 fprintf_unfiltered (f, "constant: %ld", DW_UNSND (&die->attrs[i]));
c906108c
SS
9248 break;
9249 case DW_FORM_string:
4bdf3d34 9250 case DW_FORM_strp:
d97bc12b 9251 fprintf_unfiltered (f, "string: \"%s\"",
c906108c 9252 DW_STRING (&die->attrs[i])
c5aa993b 9253 ? DW_STRING (&die->attrs[i]) : "");
c906108c
SS
9254 break;
9255 case DW_FORM_flag:
9256 if (DW_UNSND (&die->attrs[i]))
d97bc12b 9257 fprintf_unfiltered (f, "flag: TRUE");
c906108c 9258 else
d97bc12b 9259 fprintf_unfiltered (f, "flag: FALSE");
c906108c 9260 break;
a8329558
KW
9261 case DW_FORM_indirect:
9262 /* the reader will have reduced the indirect form to
9263 the "base form" so this form should not occur */
d97bc12b 9264 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 9265 break;
c906108c 9266 default:
d97bc12b 9267 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 9268 die->attrs[i].form);
d97bc12b 9269 break;
c906108c 9270 }
d97bc12b 9271 fprintf_unfiltered (f, "\n");
c906108c
SS
9272 }
9273}
9274
f9aca02d 9275static void
d97bc12b 9276dump_die_for_error (struct die_info *die)
c906108c 9277{
d97bc12b
DE
9278 dump_die_shallow (gdb_stderr, 0, die);
9279}
9280
9281static void
9282dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
9283{
9284 int indent = level * 4;
9285
9286 gdb_assert (die != NULL);
9287
9288 if (level >= max_level)
9289 return;
9290
9291 dump_die_shallow (f, indent, die);
9292
9293 if (die->child != NULL)
c906108c 9294 {
d97bc12b
DE
9295 print_spaces (indent, f);
9296 fprintf_unfiltered (f, " Children:");
9297 if (level + 1 < max_level)
9298 {
9299 fprintf_unfiltered (f, "\n");
9300 dump_die_1 (f, level + 1, max_level, die->child);
9301 }
9302 else
9303 {
9304 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
9305 }
9306 }
9307
9308 if (die->sibling != NULL && level > 0)
9309 {
9310 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
9311 }
9312}
9313
d97bc12b
DE
9314/* This is called from the pdie macro in gdbinit.in.
9315 It's not static so gcc will keep a copy callable from gdb. */
9316
9317void
9318dump_die (struct die_info *die, int max_level)
9319{
9320 dump_die_1 (gdb_stdlog, 0, max_level, die);
9321}
9322
f9aca02d 9323static void
51545339 9324store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9325{
51545339 9326 void **slot;
c906108c 9327
51545339
DJ
9328 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
9329
9330 *slot = die;
c906108c
SS
9331}
9332
9333static unsigned int
c764a876 9334dwarf2_get_ref_die_offset (struct attribute *attr)
c906108c
SS
9335{
9336 unsigned int result = 0;
9337
9338 switch (attr->form)
9339 {
9340 case DW_FORM_ref_addr:
c906108c
SS
9341 case DW_FORM_ref1:
9342 case DW_FORM_ref2:
9343 case DW_FORM_ref4:
613e1657 9344 case DW_FORM_ref8:
c906108c 9345 case DW_FORM_ref_udata:
10b3939b 9346 result = DW_ADDR (attr);
c906108c
SS
9347 break;
9348 default:
4d3c2250 9349 complaint (&symfile_complaints,
e2e0b3e5 9350 _("unsupported die ref attribute form: '%s'"),
4d3c2250 9351 dwarf_form_name (attr->form));
c906108c
SS
9352 }
9353 return result;
9354}
9355
a02abb62
JB
9356/* Return the constant value held by the given attribute. Return -1
9357 if the value held by the attribute is not constant. */
9358
9359static int
9360dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
9361{
9362 if (attr->form == DW_FORM_sdata)
9363 return DW_SND (attr);
9364 else if (attr->form == DW_FORM_udata
9365 || attr->form == DW_FORM_data1
9366 || attr->form == DW_FORM_data2
9367 || attr->form == DW_FORM_data4
9368 || attr->form == DW_FORM_data8)
9369 return DW_UNSND (attr);
9370 else
9371 {
e2e0b3e5 9372 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
9373 dwarf_form_name (attr->form));
9374 return default_value;
9375 }
9376}
9377
03dd20cc
DJ
9378/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
9379 unit and add it to our queue. */
9380
9381static void
9382maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
9383 struct dwarf2_per_cu_data *per_cu)
9384{
9385 /* Mark the dependence relation so that we don't flush PER_CU
9386 too early. */
9387 dwarf2_add_dependence (this_cu, per_cu);
9388
9389 /* If it's already on the queue, we have nothing to do. */
9390 if (per_cu->queued)
9391 return;
9392
9393 /* If the compilation unit is already loaded, just mark it as
9394 used. */
9395 if (per_cu->cu != NULL)
9396 {
9397 per_cu->cu->last_used = 0;
9398 return;
9399 }
9400
9401 /* Add it to the queue. */
9402 queue_comp_unit (per_cu, this_cu->objfile);
9403}
9404
f9aca02d 9405static struct die_info *
10b3939b 9406follow_die_ref (struct die_info *src_die, struct attribute *attr,
f2f0e013 9407 struct dwarf2_cu **ref_cu)
c906108c
SS
9408{
9409 struct die_info *die;
10b3939b 9410 unsigned int offset;
10b3939b 9411 struct die_info temp_die;
f2f0e013 9412 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 9413
c764a876 9414 offset = dwarf2_get_ref_die_offset (attr);
10b3939b 9415
45452591 9416 if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
9417 {
9418 struct dwarf2_per_cu_data *per_cu;
45452591 9419 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
9420
9421 /* If necessary, add it to the queue and load its DIEs. */
9422 maybe_queue_comp_unit (cu, per_cu);
9423
10b3939b
DJ
9424 target_cu = per_cu->cu;
9425 }
9426 else
9427 target_cu = cu;
c906108c 9428
f2f0e013 9429 *ref_cu = target_cu;
51545339
DJ
9430 temp_die.offset = offset;
9431 die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
9432 if (die)
9433 return die;
10b3939b 9434
8a3fe4f8
AC
9435 error (_("Dwarf Error: Cannot find DIE at 0x%lx referenced from DIE "
9436 "at 0x%lx [in module %s]"),
1d325ec1 9437 (long) offset, (long) src_die->offset, cu->objfile->name);
c906108c
SS
9438}
9439
c906108c
SS
9440/* Decode simple location descriptions.
9441 Given a pointer to a dwarf block that defines a location, compute
9442 the location and return the value.
9443
4cecd739
DJ
9444 NOTE drow/2003-11-18: This function is called in two situations
9445 now: for the address of static or global variables (partial symbols
9446 only) and for offsets into structures which are expected to be
9447 (more or less) constant. The partial symbol case should go away,
9448 and only the constant case should remain. That will let this
9449 function complain more accurately. A few special modes are allowed
9450 without complaint for global variables (for instance, global
9451 register values and thread-local values).
c906108c
SS
9452
9453 A location description containing no operations indicates that the
4cecd739 9454 object is optimized out. The return value is 0 for that case.
6b992462
DJ
9455 FIXME drow/2003-11-16: No callers check for this case any more; soon all
9456 callers will only want a very basic result and this can become a
9457 complaint.
c906108c 9458
c906108c
SS
9459 Note that stack[0] is unused except as a default error return.
9460 Note that stack overflow is not yet handled. */
9461
9462static CORE_ADDR
e7c27a73 9463decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 9464{
e7c27a73
DJ
9465 struct objfile *objfile = cu->objfile;
9466 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9467 int i;
9468 int size = blk->size;
fe1b8b76 9469 gdb_byte *data = blk->data;
c906108c
SS
9470 CORE_ADDR stack[64];
9471 int stacki;
9472 unsigned int bytes_read, unsnd;
fe1b8b76 9473 gdb_byte op;
c906108c
SS
9474
9475 i = 0;
9476 stacki = 0;
9477 stack[stacki] = 0;
c906108c
SS
9478
9479 while (i < size)
9480 {
c906108c
SS
9481 op = data[i++];
9482 switch (op)
9483 {
f1bea926
JM
9484 case DW_OP_lit0:
9485 case DW_OP_lit1:
9486 case DW_OP_lit2:
9487 case DW_OP_lit3:
9488 case DW_OP_lit4:
9489 case DW_OP_lit5:
9490 case DW_OP_lit6:
9491 case DW_OP_lit7:
9492 case DW_OP_lit8:
9493 case DW_OP_lit9:
9494 case DW_OP_lit10:
9495 case DW_OP_lit11:
9496 case DW_OP_lit12:
9497 case DW_OP_lit13:
9498 case DW_OP_lit14:
9499 case DW_OP_lit15:
9500 case DW_OP_lit16:
9501 case DW_OP_lit17:
9502 case DW_OP_lit18:
9503 case DW_OP_lit19:
9504 case DW_OP_lit20:
9505 case DW_OP_lit21:
9506 case DW_OP_lit22:
9507 case DW_OP_lit23:
9508 case DW_OP_lit24:
9509 case DW_OP_lit25:
9510 case DW_OP_lit26:
9511 case DW_OP_lit27:
9512 case DW_OP_lit28:
9513 case DW_OP_lit29:
9514 case DW_OP_lit30:
9515 case DW_OP_lit31:
9516 stack[++stacki] = op - DW_OP_lit0;
9517 break;
9518
c906108c
SS
9519 case DW_OP_reg0:
9520 case DW_OP_reg1:
9521 case DW_OP_reg2:
9522 case DW_OP_reg3:
9523 case DW_OP_reg4:
9524 case DW_OP_reg5:
9525 case DW_OP_reg6:
9526 case DW_OP_reg7:
9527 case DW_OP_reg8:
9528 case DW_OP_reg9:
9529 case DW_OP_reg10:
9530 case DW_OP_reg11:
9531 case DW_OP_reg12:
9532 case DW_OP_reg13:
9533 case DW_OP_reg14:
9534 case DW_OP_reg15:
9535 case DW_OP_reg16:
9536 case DW_OP_reg17:
9537 case DW_OP_reg18:
9538 case DW_OP_reg19:
9539 case DW_OP_reg20:
9540 case DW_OP_reg21:
9541 case DW_OP_reg22:
9542 case DW_OP_reg23:
9543 case DW_OP_reg24:
9544 case DW_OP_reg25:
9545 case DW_OP_reg26:
9546 case DW_OP_reg27:
9547 case DW_OP_reg28:
9548 case DW_OP_reg29:
9549 case DW_OP_reg30:
9550 case DW_OP_reg31:
c906108c 9551 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
9552 if (i < size)
9553 dwarf2_complex_location_expr_complaint ();
c906108c
SS
9554 break;
9555
9556 case DW_OP_regx:
c906108c
SS
9557 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9558 i += bytes_read;
c906108c 9559 stack[++stacki] = unsnd;
4cecd739
DJ
9560 if (i < size)
9561 dwarf2_complex_location_expr_complaint ();
c906108c
SS
9562 break;
9563
9564 case DW_OP_addr:
107d2387 9565 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 9566 cu, &bytes_read);
107d2387 9567 i += bytes_read;
c906108c
SS
9568 break;
9569
9570 case DW_OP_const1u:
9571 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
9572 i += 1;
9573 break;
9574
9575 case DW_OP_const1s:
9576 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
9577 i += 1;
9578 break;
9579
9580 case DW_OP_const2u:
9581 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
9582 i += 2;
9583 break;
9584
9585 case DW_OP_const2s:
9586 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
9587 i += 2;
9588 break;
9589
9590 case DW_OP_const4u:
9591 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
9592 i += 4;
9593 break;
9594
9595 case DW_OP_const4s:
9596 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
9597 i += 4;
9598 break;
9599
9600 case DW_OP_constu:
9601 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 9602 &bytes_read);
c906108c
SS
9603 i += bytes_read;
9604 break;
9605
9606 case DW_OP_consts:
9607 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
9608 i += bytes_read;
9609 break;
9610
f1bea926
JM
9611 case DW_OP_dup:
9612 stack[stacki + 1] = stack[stacki];
9613 stacki++;
9614 break;
9615
c906108c
SS
9616 case DW_OP_plus:
9617 stack[stacki - 1] += stack[stacki];
9618 stacki--;
9619 break;
9620
9621 case DW_OP_plus_uconst:
9622 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9623 i += bytes_read;
9624 break;
9625
9626 case DW_OP_minus:
f1bea926 9627 stack[stacki - 1] -= stack[stacki];
c906108c
SS
9628 stacki--;
9629 break;
9630
7a292a7a 9631 case DW_OP_deref:
7a292a7a 9632 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
9633 this using GDB's address_class enum. This is valid for partial
9634 global symbols, although the variable's address will be bogus
9635 in the psymtab. */
7a292a7a 9636 if (i < size)
4d3c2250 9637 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
9638 break;
9639
9d774e44 9640 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
9641 /* The top of the stack has the offset from the beginning
9642 of the thread control block at which the variable is located. */
9643 /* Nothing should follow this operator, so the top of stack would
9644 be returned. */
4cecd739
DJ
9645 /* This is valid for partial global symbols, but the variable's
9646 address will be bogus in the psymtab. */
9d774e44 9647 if (i < size)
4d3c2250 9648 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
9649 break;
9650
42be36b3
CT
9651 case DW_OP_GNU_uninit:
9652 break;
9653
c906108c 9654 default:
e2e0b3e5 9655 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
4d3c2250 9656 dwarf_stack_op_name (op));
c906108c
SS
9657 return (stack[stacki]);
9658 }
9659 }
9660 return (stack[stacki]);
9661}
9662
9663/* memory allocation interface */
9664
c906108c 9665static struct dwarf_block *
7b5a2f43 9666dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
9667{
9668 struct dwarf_block *blk;
9669
9670 blk = (struct dwarf_block *)
7b5a2f43 9671 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
9672 return (blk);
9673}
9674
9675static struct abbrev_info *
f3dd6933 9676dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
9677{
9678 struct abbrev_info *abbrev;
9679
f3dd6933
DJ
9680 abbrev = (struct abbrev_info *)
9681 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
9682 memset (abbrev, 0, sizeof (struct abbrev_info));
9683 return (abbrev);
9684}
9685
9686static struct die_info *
b60c80d6 9687dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
9688{
9689 struct die_info *die;
b60c80d6
DJ
9690 size_t size = sizeof (struct die_info);
9691
9692 if (num_attrs > 1)
9693 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 9694
b60c80d6 9695 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
9696 memset (die, 0, sizeof (struct die_info));
9697 return (die);
9698}
2e276125
JB
9699
9700\f
9701/* Macro support. */
9702
9703
9704/* Return the full name of file number I in *LH's file name table.
9705 Use COMP_DIR as the name of the current directory of the
9706 compilation. The result is allocated using xmalloc; the caller is
9707 responsible for freeing it. */
9708static char *
9709file_full_name (int file, struct line_header *lh, const char *comp_dir)
9710{
6a83a1e6
EZ
9711 /* Is the file number a valid index into the line header's file name
9712 table? Remember that file numbers start with one, not zero. */
9713 if (1 <= file && file <= lh->num_file_names)
9714 {
9715 struct file_entry *fe = &lh->file_names[file - 1];
2e276125 9716
6a83a1e6
EZ
9717 if (IS_ABSOLUTE_PATH (fe->name))
9718 return xstrdup (fe->name);
9719 else
9720 {
9721 const char *dir;
9722 int dir_len;
9723 char *full_name;
9724
9725 if (fe->dir_index)
9726 dir = lh->include_dirs[fe->dir_index - 1];
9727 else
9728 dir = comp_dir;
9729
9730 if (dir)
9731 {
9732 dir_len = strlen (dir);
9733 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
9734 strcpy (full_name, dir);
9735 full_name[dir_len] = '/';
9736 strcpy (full_name + dir_len + 1, fe->name);
9737 return full_name;
9738 }
9739 else
9740 return xstrdup (fe->name);
9741 }
9742 }
2e276125
JB
9743 else
9744 {
6a83a1e6
EZ
9745 /* The compiler produced a bogus file number. We can at least
9746 record the macro definitions made in the file, even if we
9747 won't be able to find the file by name. */
9748 char fake_name[80];
9749 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 9750
6a83a1e6
EZ
9751 complaint (&symfile_complaints,
9752 _("bad file number in macro information (%d)"),
9753 file);
2e276125 9754
6a83a1e6 9755 return xstrdup (fake_name);
2e276125
JB
9756 }
9757}
9758
9759
9760static struct macro_source_file *
9761macro_start_file (int file, int line,
9762 struct macro_source_file *current_file,
9763 const char *comp_dir,
9764 struct line_header *lh, struct objfile *objfile)
9765{
9766 /* The full name of this source file. */
9767 char *full_name = file_full_name (file, lh, comp_dir);
9768
9769 /* We don't create a macro table for this compilation unit
9770 at all until we actually get a filename. */
9771 if (! pending_macros)
4a146b47 9772 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 9773 objfile->macro_cache);
2e276125
JB
9774
9775 if (! current_file)
9776 /* If we have no current file, then this must be the start_file
9777 directive for the compilation unit's main source file. */
9778 current_file = macro_set_main (pending_macros, full_name);
9779 else
9780 current_file = macro_include (current_file, line, full_name);
9781
9782 xfree (full_name);
9783
9784 return current_file;
9785}
9786
9787
9788/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
9789 followed by a null byte. */
9790static char *
9791copy_string (const char *buf, int len)
9792{
9793 char *s = xmalloc (len + 1);
9794 memcpy (s, buf, len);
9795 s[len] = '\0';
9796
9797 return s;
9798}
9799
9800
9801static const char *
9802consume_improper_spaces (const char *p, const char *body)
9803{
9804 if (*p == ' ')
9805 {
4d3c2250 9806 complaint (&symfile_complaints,
e2e0b3e5 9807 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 9808 body);
2e276125
JB
9809
9810 while (*p == ' ')
9811 p++;
9812 }
9813
9814 return p;
9815}
9816
9817
9818static void
9819parse_macro_definition (struct macro_source_file *file, int line,
9820 const char *body)
9821{
9822 const char *p;
9823
9824 /* The body string takes one of two forms. For object-like macro
9825 definitions, it should be:
9826
9827 <macro name> " " <definition>
9828
9829 For function-like macro definitions, it should be:
9830
9831 <macro name> "() " <definition>
9832 or
9833 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
9834
9835 Spaces may appear only where explicitly indicated, and in the
9836 <definition>.
9837
9838 The Dwarf 2 spec says that an object-like macro's name is always
9839 followed by a space, but versions of GCC around March 2002 omit
9840 the space when the macro's definition is the empty string.
9841
9842 The Dwarf 2 spec says that there should be no spaces between the
9843 formal arguments in a function-like macro's formal argument list,
9844 but versions of GCC around March 2002 include spaces after the
9845 commas. */
9846
9847
9848 /* Find the extent of the macro name. The macro name is terminated
9849 by either a space or null character (for an object-like macro) or
9850 an opening paren (for a function-like macro). */
9851 for (p = body; *p; p++)
9852 if (*p == ' ' || *p == '(')
9853 break;
9854
9855 if (*p == ' ' || *p == '\0')
9856 {
9857 /* It's an object-like macro. */
9858 int name_len = p - body;
9859 char *name = copy_string (body, name_len);
9860 const char *replacement;
9861
9862 if (*p == ' ')
9863 replacement = body + name_len + 1;
9864 else
9865 {
4d3c2250 9866 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9867 replacement = body + name_len;
9868 }
9869
9870 macro_define_object (file, line, name, replacement);
9871
9872 xfree (name);
9873 }
9874 else if (*p == '(')
9875 {
9876 /* It's a function-like macro. */
9877 char *name = copy_string (body, p - body);
9878 int argc = 0;
9879 int argv_size = 1;
9880 char **argv = xmalloc (argv_size * sizeof (*argv));
9881
9882 p++;
9883
9884 p = consume_improper_spaces (p, body);
9885
9886 /* Parse the formal argument list. */
9887 while (*p && *p != ')')
9888 {
9889 /* Find the extent of the current argument name. */
9890 const char *arg_start = p;
9891
9892 while (*p && *p != ',' && *p != ')' && *p != ' ')
9893 p++;
9894
9895 if (! *p || p == arg_start)
4d3c2250 9896 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9897 else
9898 {
9899 /* Make sure argv has room for the new argument. */
9900 if (argc >= argv_size)
9901 {
9902 argv_size *= 2;
9903 argv = xrealloc (argv, argv_size * sizeof (*argv));
9904 }
9905
9906 argv[argc++] = copy_string (arg_start, p - arg_start);
9907 }
9908
9909 p = consume_improper_spaces (p, body);
9910
9911 /* Consume the comma, if present. */
9912 if (*p == ',')
9913 {
9914 p++;
9915
9916 p = consume_improper_spaces (p, body);
9917 }
9918 }
9919
9920 if (*p == ')')
9921 {
9922 p++;
9923
9924 if (*p == ' ')
9925 /* Perfectly formed definition, no complaints. */
9926 macro_define_function (file, line, name,
9927 argc, (const char **) argv,
9928 p + 1);
9929 else if (*p == '\0')
9930 {
9931 /* Complain, but do define it. */
4d3c2250 9932 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9933 macro_define_function (file, line, name,
9934 argc, (const char **) argv,
9935 p);
9936 }
9937 else
9938 /* Just complain. */
4d3c2250 9939 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9940 }
9941 else
9942 /* Just complain. */
4d3c2250 9943 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9944
9945 xfree (name);
9946 {
9947 int i;
9948
9949 for (i = 0; i < argc; i++)
9950 xfree (argv[i]);
9951 }
9952 xfree (argv);
9953 }
9954 else
4d3c2250 9955 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9956}
9957
9958
9959static void
9960dwarf_decode_macros (struct line_header *lh, unsigned int offset,
9961 char *comp_dir, bfd *abfd,
e7c27a73 9962 struct dwarf2_cu *cu)
2e276125 9963{
fe1b8b76 9964 gdb_byte *mac_ptr, *mac_end;
2e276125
JB
9965 struct macro_source_file *current_file = 0;
9966
6502dd73 9967 if (dwarf2_per_objfile->macinfo_buffer == NULL)
2e276125 9968 {
e2e0b3e5 9969 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
9970 return;
9971 }
9972
6502dd73
DJ
9973 mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
9974 mac_end = dwarf2_per_objfile->macinfo_buffer
9975 + dwarf2_per_objfile->macinfo_size;
2e276125
JB
9976
9977 for (;;)
9978 {
9979 enum dwarf_macinfo_record_type macinfo_type;
9980
9981 /* Do we at least have room for a macinfo type byte? */
9982 if (mac_ptr >= mac_end)
9983 {
4d3c2250 9984 dwarf2_macros_too_long_complaint ();
2e276125
JB
9985 return;
9986 }
9987
9988 macinfo_type = read_1_byte (abfd, mac_ptr);
9989 mac_ptr++;
9990
9991 switch (macinfo_type)
9992 {
9993 /* A zero macinfo type indicates the end of the macro
9994 information. */
9995 case 0:
9996 return;
9997
9998 case DW_MACINFO_define:
9999 case DW_MACINFO_undef:
10000 {
891d2f0b 10001 unsigned int bytes_read;
2e276125
JB
10002 int line;
10003 char *body;
10004
10005 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10006 mac_ptr += bytes_read;
10007 body = read_string (abfd, mac_ptr, &bytes_read);
10008 mac_ptr += bytes_read;
10009
10010 if (! current_file)
4d3c2250 10011 complaint (&symfile_complaints,
e2e0b3e5 10012 _("debug info gives macro %s outside of any file: %s"),
4d3c2250
KB
10013 macinfo_type ==
10014 DW_MACINFO_define ? "definition" : macinfo_type ==
10015 DW_MACINFO_undef ? "undefinition" :
10016 "something-or-other", body);
2e276125
JB
10017 else
10018 {
10019 if (macinfo_type == DW_MACINFO_define)
10020 parse_macro_definition (current_file, line, body);
10021 else if (macinfo_type == DW_MACINFO_undef)
10022 macro_undef (current_file, line, body);
10023 }
10024 }
10025 break;
10026
10027 case DW_MACINFO_start_file:
10028 {
891d2f0b 10029 unsigned int bytes_read;
2e276125
JB
10030 int line, file;
10031
10032 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10033 mac_ptr += bytes_read;
10034 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10035 mac_ptr += bytes_read;
10036
10037 current_file = macro_start_file (file, line,
10038 current_file, comp_dir,
e7c27a73 10039 lh, cu->objfile);
2e276125
JB
10040 }
10041 break;
10042
10043 case DW_MACINFO_end_file:
10044 if (! current_file)
4d3c2250 10045 complaint (&symfile_complaints,
e2e0b3e5 10046 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
10047 else
10048 {
10049 current_file = current_file->included_by;
10050 if (! current_file)
10051 {
10052 enum dwarf_macinfo_record_type next_type;
10053
10054 /* GCC circa March 2002 doesn't produce the zero
10055 type byte marking the end of the compilation
10056 unit. Complain if it's not there, but exit no
10057 matter what. */
10058
10059 /* Do we at least have room for a macinfo type byte? */
10060 if (mac_ptr >= mac_end)
10061 {
4d3c2250 10062 dwarf2_macros_too_long_complaint ();
2e276125
JB
10063 return;
10064 }
10065
10066 /* We don't increment mac_ptr here, so this is just
10067 a look-ahead. */
10068 next_type = read_1_byte (abfd, mac_ptr);
10069 if (next_type != 0)
4d3c2250 10070 complaint (&symfile_complaints,
e2e0b3e5 10071 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
10072
10073 return;
10074 }
10075 }
10076 break;
10077
10078 case DW_MACINFO_vendor_ext:
10079 {
891d2f0b 10080 unsigned int bytes_read;
2e276125
JB
10081 int constant;
10082 char *string;
10083
10084 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10085 mac_ptr += bytes_read;
10086 string = read_string (abfd, mac_ptr, &bytes_read);
10087 mac_ptr += bytes_read;
10088
10089 /* We don't recognize any vendor extensions. */
10090 }
10091 break;
10092 }
10093 }
10094}
8e19ed76
PS
10095
10096/* Check if the attribute's form is a DW_FORM_block*
10097 if so return true else false. */
10098static int
10099attr_form_is_block (struct attribute *attr)
10100{
10101 return (attr == NULL ? 0 :
10102 attr->form == DW_FORM_block1
10103 || attr->form == DW_FORM_block2
10104 || attr->form == DW_FORM_block4
10105 || attr->form == DW_FORM_block);
10106}
4c2df51b 10107
c6a0999f
JB
10108/* Return non-zero if ATTR's value is a section offset --- classes
10109 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
10110 You may use DW_UNSND (attr) to retrieve such offsets.
10111
10112 Section 7.5.4, "Attribute Encodings", explains that no attribute
10113 may have a value that belongs to more than one of these classes; it
10114 would be ambiguous if we did, because we use the same forms for all
10115 of them. */
3690dd37
JB
10116static int
10117attr_form_is_section_offset (struct attribute *attr)
10118{
10119 return (attr->form == DW_FORM_data4
10120 || attr->form == DW_FORM_data8);
10121}
10122
10123
10124/* Return non-zero if ATTR's value falls in the 'constant' class, or
10125 zero otherwise. When this function returns true, you can apply
10126 dwarf2_get_attr_constant_value to it.
10127
10128 However, note that for some attributes you must check
10129 attr_form_is_section_offset before using this test. DW_FORM_data4
10130 and DW_FORM_data8 are members of both the constant class, and of
10131 the classes that contain offsets into other debug sections
10132 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
10133 that, if an attribute's can be either a constant or one of the
10134 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
10135 taken as section offsets, not constants. */
10136static int
10137attr_form_is_constant (struct attribute *attr)
10138{
10139 switch (attr->form)
10140 {
10141 case DW_FORM_sdata:
10142 case DW_FORM_udata:
10143 case DW_FORM_data1:
10144 case DW_FORM_data2:
10145 case DW_FORM_data4:
10146 case DW_FORM_data8:
10147 return 1;
10148 default:
10149 return 0;
10150 }
10151}
10152
4c2df51b
DJ
10153static void
10154dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 10155 struct dwarf2_cu *cu)
4c2df51b 10156{
3690dd37 10157 if (attr_form_is_section_offset (attr)
99bcc461
DJ
10158 /* ".debug_loc" may not exist at all, or the offset may be outside
10159 the section. If so, fall through to the complaint in the
10160 other branch. */
10161 && DW_UNSND (attr) < dwarf2_per_objfile->loc_size)
4c2df51b 10162 {
0d53c4c4 10163 struct dwarf2_loclist_baton *baton;
4c2df51b 10164
4a146b47 10165 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 10166 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
10167 baton->per_cu = cu->per_cu;
10168 gdb_assert (baton->per_cu);
4c2df51b 10169
0d53c4c4
DJ
10170 /* We don't know how long the location list is, but make sure we
10171 don't run off the edge of the section. */
6502dd73
DJ
10172 baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
10173 baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
d00adf39
DE
10174 baton->base_address = cu->base_address;
10175 if (cu->base_known == 0)
0d53c4c4 10176 complaint (&symfile_complaints,
e2e0b3e5 10177 _("Location list used without specifying the CU base address."));
4c2df51b 10178
a67af2b9 10179 SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
10180 SYMBOL_LOCATION_BATON (sym) = baton;
10181 }
10182 else
10183 {
10184 struct dwarf2_locexpr_baton *baton;
10185
4a146b47 10186 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 10187 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
10188 baton->per_cu = cu->per_cu;
10189 gdb_assert (baton->per_cu);
0d53c4c4
DJ
10190
10191 if (attr_form_is_block (attr))
10192 {
10193 /* Note that we're just copying the block's data pointer
10194 here, not the actual data. We're still pointing into the
6502dd73
DJ
10195 info_buffer for SYM's objfile; right now we never release
10196 that buffer, but when we do clean up properly this may
10197 need to change. */
0d53c4c4
DJ
10198 baton->size = DW_BLOCK (attr)->size;
10199 baton->data = DW_BLOCK (attr)->data;
10200 }
10201 else
10202 {
10203 dwarf2_invalid_attrib_class_complaint ("location description",
10204 SYMBOL_NATURAL_NAME (sym));
10205 baton->size = 0;
10206 baton->data = NULL;
10207 }
10208
a67af2b9 10209 SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
10210 SYMBOL_LOCATION_BATON (sym) = baton;
10211 }
4c2df51b 10212}
6502dd73 10213
ae0d2f24
UW
10214/* Return the OBJFILE associated with the compilation unit CU. */
10215
10216struct objfile *
10217dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
10218{
10219 struct objfile *objfile = per_cu->psymtab->objfile;
10220
10221 /* Return the master objfile, so that we can report and look up the
10222 correct file containing this variable. */
10223 if (objfile->separate_debug_objfile_backlink)
10224 objfile = objfile->separate_debug_objfile_backlink;
10225
10226 return objfile;
10227}
10228
10229/* Return the address size given in the compilation unit header for CU. */
10230
10231CORE_ADDR
10232dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
10233{
10234 if (per_cu->cu)
10235 return per_cu->cu->header.addr_size;
10236 else
10237 {
10238 /* If the CU is not currently read in, we re-read its header. */
10239 struct objfile *objfile = per_cu->psymtab->objfile;
10240 struct dwarf2_per_objfile *per_objfile
10241 = objfile_data (objfile, dwarf2_objfile_data_key);
10242 gdb_byte *info_ptr = per_objfile->info_buffer + per_cu->offset;
10243
10244 struct comp_unit_head cu_header;
10245 memset (&cu_header, 0, sizeof cu_header);
10246 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
10247 return cu_header.addr_size;
10248 }
10249}
10250
ae038cb0 10251/* Locate the compilation unit from CU's objfile which contains the
10b3939b 10252 DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
10253
10254static struct dwarf2_per_cu_data *
c764a876 10255dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
10256 struct objfile *objfile)
10257{
10258 struct dwarf2_per_cu_data *this_cu;
10259 int low, high;
10260
ae038cb0
DJ
10261 low = 0;
10262 high = dwarf2_per_objfile->n_comp_units - 1;
10263 while (high > low)
10264 {
10265 int mid = low + (high - low) / 2;
10266 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
10267 high = mid;
10268 else
10269 low = mid + 1;
10270 }
10271 gdb_assert (low == high);
10272 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
10273 {
10b3939b 10274 if (low == 0)
8a3fe4f8
AC
10275 error (_("Dwarf Error: could not find partial DIE containing "
10276 "offset 0x%lx [in module %s]"),
10b3939b
DJ
10277 (long) offset, bfd_get_filename (objfile->obfd));
10278
ae038cb0
DJ
10279 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
10280 return dwarf2_per_objfile->all_comp_units[low-1];
10281 }
10282 else
10283 {
10284 this_cu = dwarf2_per_objfile->all_comp_units[low];
10285 if (low == dwarf2_per_objfile->n_comp_units - 1
10286 && offset >= this_cu->offset + this_cu->length)
c764a876 10287 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
10288 gdb_assert (offset < this_cu->offset + this_cu->length);
10289 return this_cu;
10290 }
10291}
10292
10b3939b
DJ
10293/* Locate the compilation unit from OBJFILE which is located at exactly
10294 OFFSET. Raises an error on failure. */
10295
ae038cb0 10296static struct dwarf2_per_cu_data *
c764a876 10297dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
10298{
10299 struct dwarf2_per_cu_data *this_cu;
10300 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10301 if (this_cu->offset != offset)
c764a876 10302 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
10303 return this_cu;
10304}
10305
10306/* Release one cached compilation unit, CU. We unlink it from the tree
10307 of compilation units, but we don't remove it from the read_in_chain;
10308 the caller is responsible for that. */
10309
10310static void
10311free_one_comp_unit (void *data)
10312{
10313 struct dwarf2_cu *cu = data;
10314
10315 if (cu->per_cu != NULL)
10316 cu->per_cu->cu = NULL;
10317 cu->per_cu = NULL;
10318
10319 obstack_free (&cu->comp_unit_obstack, NULL);
10320
10321 xfree (cu);
10322}
10323
72bf9492 10324/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
10325 when we're finished with it. We can't free the pointer itself, but be
10326 sure to unlink it from the cache. Also release any associated storage
10327 and perform cache maintenance.
72bf9492
DJ
10328
10329 Only used during partial symbol parsing. */
10330
10331static void
10332free_stack_comp_unit (void *data)
10333{
10334 struct dwarf2_cu *cu = data;
10335
10336 obstack_free (&cu->comp_unit_obstack, NULL);
10337 cu->partial_dies = NULL;
ae038cb0
DJ
10338
10339 if (cu->per_cu != NULL)
10340 {
10341 /* This compilation unit is on the stack in our caller, so we
10342 should not xfree it. Just unlink it. */
10343 cu->per_cu->cu = NULL;
10344 cu->per_cu = NULL;
10345
10346 /* If we had a per-cu pointer, then we may have other compilation
10347 units loaded, so age them now. */
10348 age_cached_comp_units ();
10349 }
10350}
10351
10352/* Free all cached compilation units. */
10353
10354static void
10355free_cached_comp_units (void *data)
10356{
10357 struct dwarf2_per_cu_data *per_cu, **last_chain;
10358
10359 per_cu = dwarf2_per_objfile->read_in_chain;
10360 last_chain = &dwarf2_per_objfile->read_in_chain;
10361 while (per_cu != NULL)
10362 {
10363 struct dwarf2_per_cu_data *next_cu;
10364
10365 next_cu = per_cu->cu->read_in_chain;
10366
10367 free_one_comp_unit (per_cu->cu);
10368 *last_chain = next_cu;
10369
10370 per_cu = next_cu;
10371 }
10372}
10373
10374/* Increase the age counter on each cached compilation unit, and free
10375 any that are too old. */
10376
10377static void
10378age_cached_comp_units (void)
10379{
10380 struct dwarf2_per_cu_data *per_cu, **last_chain;
10381
10382 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
10383 per_cu = dwarf2_per_objfile->read_in_chain;
10384 while (per_cu != NULL)
10385 {
10386 per_cu->cu->last_used ++;
10387 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
10388 dwarf2_mark (per_cu->cu);
10389 per_cu = per_cu->cu->read_in_chain;
10390 }
10391
10392 per_cu = dwarf2_per_objfile->read_in_chain;
10393 last_chain = &dwarf2_per_objfile->read_in_chain;
10394 while (per_cu != NULL)
10395 {
10396 struct dwarf2_per_cu_data *next_cu;
10397
10398 next_cu = per_cu->cu->read_in_chain;
10399
10400 if (!per_cu->cu->mark)
10401 {
10402 free_one_comp_unit (per_cu->cu);
10403 *last_chain = next_cu;
10404 }
10405 else
10406 last_chain = &per_cu->cu->read_in_chain;
10407
10408 per_cu = next_cu;
10409 }
10410}
10411
10412/* Remove a single compilation unit from the cache. */
10413
10414static void
10415free_one_cached_comp_unit (void *target_cu)
10416{
10417 struct dwarf2_per_cu_data *per_cu, **last_chain;
10418
10419 per_cu = dwarf2_per_objfile->read_in_chain;
10420 last_chain = &dwarf2_per_objfile->read_in_chain;
10421 while (per_cu != NULL)
10422 {
10423 struct dwarf2_per_cu_data *next_cu;
10424
10425 next_cu = per_cu->cu->read_in_chain;
10426
10427 if (per_cu->cu == target_cu)
10428 {
10429 free_one_comp_unit (per_cu->cu);
10430 *last_chain = next_cu;
10431 break;
10432 }
10433 else
10434 last_chain = &per_cu->cu->read_in_chain;
10435
10436 per_cu = next_cu;
10437 }
10438}
10439
fe3e1990
DJ
10440/* Release all extra memory associated with OBJFILE. */
10441
10442void
10443dwarf2_free_objfile (struct objfile *objfile)
10444{
10445 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10446
10447 if (dwarf2_per_objfile == NULL)
10448 return;
10449
10450 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
10451 free_cached_comp_units (NULL);
10452
10453 /* Everything else should be on the objfile obstack. */
10454}
10455
1c379e20
DJ
10456/* A pair of DIE offset and GDB type pointer. We store these
10457 in a hash table separate from the DIEs, and preserve them
10458 when the DIEs are flushed out of cache. */
10459
10460struct dwarf2_offset_and_type
10461{
10462 unsigned int offset;
10463 struct type *type;
10464};
10465
10466/* Hash function for a dwarf2_offset_and_type. */
10467
10468static hashval_t
10469offset_and_type_hash (const void *item)
10470{
10471 const struct dwarf2_offset_and_type *ofs = item;
10472 return ofs->offset;
10473}
10474
10475/* Equality function for a dwarf2_offset_and_type. */
10476
10477static int
10478offset_and_type_eq (const void *item_lhs, const void *item_rhs)
10479{
10480 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
10481 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
10482 return ofs_lhs->offset == ofs_rhs->offset;
10483}
10484
10485/* Set the type associated with DIE to TYPE. Save it in CU's hash
f792889a 10486 table if necessary. For convenience, return TYPE. */
1c379e20 10487
f792889a 10488static struct type *
1c379e20
DJ
10489set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10490{
10491 struct dwarf2_offset_and_type **slot, ofs;
10492
f792889a
DJ
10493 if (cu->type_hash == NULL)
10494 {
10495 gdb_assert (cu->per_cu != NULL);
10496 cu->per_cu->type_hash
10497 = htab_create_alloc_ex (cu->header.length / 24,
10498 offset_and_type_hash,
10499 offset_and_type_eq,
10500 NULL,
10501 &cu->objfile->objfile_obstack,
10502 hashtab_obstack_allocate,
10503 dummy_obstack_deallocate);
10504 cu->type_hash = cu->per_cu->type_hash;
10505 }
1c379e20
DJ
10506
10507 ofs.offset = die->offset;
10508 ofs.type = type;
10509 slot = (struct dwarf2_offset_and_type **)
f792889a 10510 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
1c379e20
DJ
10511 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
10512 **slot = ofs;
f792889a 10513 return type;
1c379e20
DJ
10514}
10515
f792889a
DJ
10516/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
10517 not have a saved type. */
1c379e20
DJ
10518
10519static struct type *
f792889a 10520get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
10521{
10522 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
10523 htab_t type_hash = cu->type_hash;
10524
10525 if (type_hash == NULL)
10526 return NULL;
1c379e20
DJ
10527
10528 ofs.offset = die->offset;
10529 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
10530 if (slot)
10531 return slot->type;
10532 else
10533 return NULL;
10534}
10535
10b3939b
DJ
10536/* Set the mark field in CU and in every other compilation unit in the
10537 cache that we must keep because we are keeping CU. */
10538
10539/* Add a dependence relationship from CU to REF_PER_CU. */
10540
10541static void
10542dwarf2_add_dependence (struct dwarf2_cu *cu,
10543 struct dwarf2_per_cu_data *ref_per_cu)
10544{
10545 void **slot;
10546
10547 if (cu->dependencies == NULL)
10548 cu->dependencies
10549 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
10550 NULL, &cu->comp_unit_obstack,
10551 hashtab_obstack_allocate,
10552 dummy_obstack_deallocate);
10553
10554 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
10555 if (*slot == NULL)
10556 *slot = ref_per_cu;
10557}
1c379e20 10558
ae038cb0
DJ
10559/* Set the mark field in CU and in every other compilation unit in the
10560 cache that we must keep because we are keeping CU. */
10561
10b3939b
DJ
10562static int
10563dwarf2_mark_helper (void **slot, void *data)
10564{
10565 struct dwarf2_per_cu_data *per_cu;
10566
10567 per_cu = (struct dwarf2_per_cu_data *) *slot;
10568 if (per_cu->cu->mark)
10569 return 1;
10570 per_cu->cu->mark = 1;
10571
10572 if (per_cu->cu->dependencies != NULL)
10573 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
10574
10575 return 1;
10576}
10577
ae038cb0
DJ
10578static void
10579dwarf2_mark (struct dwarf2_cu *cu)
10580{
10581 if (cu->mark)
10582 return;
10583 cu->mark = 1;
10b3939b
DJ
10584 if (cu->dependencies != NULL)
10585 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
10586}
10587
10588static void
10589dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
10590{
10591 while (per_cu)
10592 {
10593 per_cu->cu->mark = 0;
10594 per_cu = per_cu->cu->read_in_chain;
10595 }
72bf9492
DJ
10596}
10597
72bf9492
DJ
10598/* Trivial hash function for partial_die_info: the hash value of a DIE
10599 is its offset in .debug_info for this objfile. */
10600
10601static hashval_t
10602partial_die_hash (const void *item)
10603{
10604 const struct partial_die_info *part_die = item;
10605 return part_die->offset;
10606}
10607
10608/* Trivial comparison function for partial_die_info structures: two DIEs
10609 are equal if they have the same offset. */
10610
10611static int
10612partial_die_eq (const void *item_lhs, const void *item_rhs)
10613{
10614 const struct partial_die_info *part_die_lhs = item_lhs;
10615 const struct partial_die_info *part_die_rhs = item_rhs;
10616 return part_die_lhs->offset == part_die_rhs->offset;
10617}
10618
ae038cb0
DJ
10619static struct cmd_list_element *set_dwarf2_cmdlist;
10620static struct cmd_list_element *show_dwarf2_cmdlist;
10621
10622static void
10623set_dwarf2_cmd (char *args, int from_tty)
10624{
10625 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
10626}
10627
10628static void
10629show_dwarf2_cmd (char *args, int from_tty)
10630{
10631 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
10632}
10633
6502dd73
DJ
10634void _initialize_dwarf2_read (void);
10635
10636void
10637_initialize_dwarf2_read (void)
10638{
10639 dwarf2_objfile_data_key = register_objfile_data ();
ae038cb0 10640
1bedd215
AC
10641 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
10642Set DWARF 2 specific variables.\n\
10643Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
10644 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
10645 0/*allow-unknown*/, &maintenance_set_cmdlist);
10646
1bedd215
AC
10647 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
10648Show DWARF 2 specific variables\n\
10649Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
10650 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
10651 0/*allow-unknown*/, &maintenance_show_cmdlist);
10652
10653 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
10654 &dwarf2_max_cache_age, _("\
10655Set the upper bound on the age of cached dwarf2 compilation units."), _("\
10656Show the upper bound on the age of cached dwarf2 compilation units."), _("\
10657A higher limit means that cached compilation units will be stored\n\
10658in memory longer, and more total memory will be used. Zero disables\n\
10659caching, which can slow down startup."),
2c5b56ce 10660 NULL,
920d2a44 10661 show_dwarf2_max_cache_age,
2c5b56ce 10662 &set_dwarf2_cmdlist,
ae038cb0 10663 &show_dwarf2_cmdlist);
d97bc12b
DE
10664
10665 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
10666Set debugging of the dwarf2 DIE reader."), _("\
10667Show debugging of the dwarf2 DIE reader."), _("\
10668When enabled (non-zero), DIEs are dumped after they are read in.\n\
10669The value is the maximum depth to print."),
10670 NULL,
10671 NULL,
10672 &setdebuglist, &showdebuglist);
6502dd73 10673}
This page took 1.554581 seconds and 4 git commands to generate.