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