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