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