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