1 /* DWARF 2 debugging format support for GDB.
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.
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
14 This file is part of GDB.
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.
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.
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/>. */
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
41 #include "complaints.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
51 #include "typeprint.h"
54 #include "exceptions.h"
56 #include "completer.h"
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
71 #define MAP_FAILED ((void *) -1)
75 typedef struct symbol *symbolp;
79 /* .debug_info header for a compilation unit
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82 typedef struct comp_unit_header
84 unsigned int length; /* length of the .debug_info
86 unsigned short version; /* version number -- 2 for DWARF
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
95 /* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98 typedef struct statement_prologue
100 unsigned int total_length; /* byte length of the statement
102 unsigned short version; /* version number -- 2 for DWARF
104 unsigned int prologue_length; /* # bytes between prologue &
106 unsigned char minimum_instruction_length; /* byte size of
108 unsigned char default_is_stmt; /* initial value of is_stmt
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
114 unsigned char *standard_opcode_lengths;
118 /* When non-zero, dump DIEs after they are read in. */
119 static int dwarf2_die_debug = 0;
123 /* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
127 static int processing_has_namespace_info;
129 static const struct objfile_data *dwarf2_objfile_data_key;
131 struct dwarf2_section_info
137 /* True if we have tried to read this section. */
141 /* All offsets in the index are of this type. It must be
142 architecture-independent. */
143 typedef uint32_t offset_type;
145 DEF_VEC_I (offset_type);
147 /* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
151 /* The total length of the buffer. */
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
165 struct dwarf2_per_objfile
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
174 struct dwarf2_section_info types;
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
177 struct dwarf2_section_info gdb_index;
180 struct objfile *objfile;
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
184 struct dwarf2_per_cu_data **all_comp_units;
186 /* The number of compilation units in ALL_COMP_UNITS. */
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
203 /* A flag indicating wether this objfile has a section loaded at a
205 int has_section_at_zero;
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
210 /* The mapped index. */
211 struct mapped_index *index_table;
213 /* Set during partial symbol reading, to prevent queueing of full
215 int reading_partial_symbols;
218 static struct dwarf2_per_objfile *dwarf2_per_objfile;
220 /* names of the debugging sections */
222 /* Note that if the debugging section has been compressed, it might
223 have a name like .zdebug_info. */
225 #define INFO_SECTION "debug_info"
226 #define ABBREV_SECTION "debug_abbrev"
227 #define LINE_SECTION "debug_line"
228 #define LOC_SECTION "debug_loc"
229 #define MACINFO_SECTION "debug_macinfo"
230 #define STR_SECTION "debug_str"
231 #define RANGES_SECTION "debug_ranges"
232 #define TYPES_SECTION "debug_types"
233 #define FRAME_SECTION "debug_frame"
234 #define EH_FRAME_SECTION "eh_frame"
235 #define GDB_INDEX_SECTION "gdb_index"
237 /* local data types */
239 /* We hold several abbreviation tables in memory at the same time. */
240 #ifndef ABBREV_HASH_SIZE
241 #define ABBREV_HASH_SIZE 121
244 /* The data in a compilation unit header, after target2host
245 translation, looks like this. */
246 struct comp_unit_head
250 unsigned char addr_size;
251 unsigned char signed_addr_p;
252 unsigned int abbrev_offset;
254 /* Size of file offsets; either 4 or 8. */
255 unsigned int offset_size;
257 /* Size of the length field; either 4 or 12. */
258 unsigned int initial_length_size;
260 /* Offset to the first byte of this compilation unit header in the
261 .debug_info section, for resolving relative reference dies. */
264 /* Offset to first die in this cu from the start of the cu.
265 This will be the first byte following the compilation unit header. */
266 unsigned int first_die_offset;
269 /* Type used for delaying computation of method physnames.
270 See comments for compute_delayed_physnames. */
271 struct delayed_method_info
273 /* The type to which the method is attached, i.e., its parent class. */
276 /* The index of the method in the type's function fieldlists. */
279 /* The index of the method in the fieldlist. */
282 /* The name of the DIE. */
285 /* The DIE associated with this method. */
286 struct die_info *die;
289 typedef struct delayed_method_info delayed_method_info;
290 DEF_VEC_O (delayed_method_info);
292 /* Internal state when decoding a particular compilation unit. */
295 /* The objfile containing this compilation unit. */
296 struct objfile *objfile;
298 /* The header of the compilation unit. */
299 struct comp_unit_head header;
301 /* Base address of this compilation unit. */
302 CORE_ADDR base_address;
304 /* Non-zero if base_address has been set. */
307 struct function_range *first_fn, *last_fn, *cached_fn;
309 /* The language we are debugging. */
310 enum language language;
311 const struct language_defn *language_defn;
313 const char *producer;
315 /* The generic symbol table building routines have separate lists for
316 file scope symbols and all all other scopes (local scopes). So
317 we need to select the right one to pass to add_symbol_to_list().
318 We do it by keeping a pointer to the correct list in list_in_scope.
320 FIXME: The original dwarf code just treated the file scope as the
321 first local scope, and all other local scopes as nested local
322 scopes, and worked fine. Check to see if we really need to
323 distinguish these in buildsym.c. */
324 struct pending **list_in_scope;
326 /* DWARF abbreviation table associated with this compilation unit. */
327 struct abbrev_info **dwarf2_abbrevs;
329 /* Storage for the abbrev table. */
330 struct obstack abbrev_obstack;
332 /* Hash table holding all the loaded partial DIEs. */
335 /* Storage for things with the same lifetime as this read-in compilation
336 unit, including partial DIEs. */
337 struct obstack comp_unit_obstack;
339 /* When multiple dwarf2_cu structures are living in memory, this field
340 chains them all together, so that they can be released efficiently.
341 We will probably also want a generation counter so that most-recently-used
342 compilation units are cached... */
343 struct dwarf2_per_cu_data *read_in_chain;
345 /* Backchain to our per_cu entry if the tree has been built. */
346 struct dwarf2_per_cu_data *per_cu;
348 /* Pointer to the die -> type map. Although it is stored
349 permanently in per_cu, we copy it here to avoid double
353 /* How many compilation units ago was this CU last referenced? */
356 /* A hash table of die offsets for following references. */
359 /* Full DIEs if read in. */
360 struct die_info *dies;
362 /* A set of pointers to dwarf2_per_cu_data objects for compilation
363 units referenced by this one. Only set during full symbol processing;
364 partial symbol tables do not have dependencies. */
367 /* Header data from the line table, during full symbol processing. */
368 struct line_header *line_header;
370 /* A list of methods which need to have physnames computed
371 after all type information has been read. */
372 VEC (delayed_method_info) *method_list;
374 /* Mark used when releasing cached dies. */
375 unsigned int mark : 1;
377 /* This flag will be set if this compilation unit might include
378 inter-compilation-unit references. */
379 unsigned int has_form_ref_addr : 1;
381 /* This flag will be set if this compilation unit includes any
382 DW_TAG_namespace DIEs. If we know that there are explicit
383 DIEs for namespaces, we don't need to try to infer them
384 from mangled names. */
385 unsigned int has_namespace_info : 1;
388 /* When using the index (and thus not using psymtabs), each CU has an
389 object of this type. This is used to hold information needed by
390 the various "quick" methods. */
391 struct dwarf2_per_cu_quick_data
393 /* The line table. This can be NULL if there was no line table. */
394 struct line_header *lines;
396 /* The file names from the line table. */
397 const char **file_names;
398 /* The file names from the line table after being run through
400 const char **full_names;
402 /* The corresponding symbol table. This is NULL if symbols for this
403 CU have not yet been read. */
404 struct symtab *symtab;
406 /* A temporary mark bit used when iterating over all CUs in
407 expand_symtabs_matching. */
408 unsigned int mark : 1;
410 /* True if we've tried to read the line table. */
411 unsigned int read_lines : 1;
414 /* Persistent data held for a compilation unit, even when not
415 processing it. We put a pointer to this structure in the
416 read_symtab_private field of the psymtab. If we encounter
417 inter-compilation-unit references, we also maintain a sorted
418 list of all compilation units. */
420 struct dwarf2_per_cu_data
422 /* The start offset and length of this compilation unit. 2**29-1
423 bytes should suffice to store the length of any compilation unit
424 - if it doesn't, GDB will fall over anyway.
425 NOTE: Unlike comp_unit_head.length, this length includes
426 initial_length_size. */
428 unsigned int length : 29;
430 /* Flag indicating this compilation unit will be read in before
431 any of the current compilation units are processed. */
432 unsigned int queued : 1;
434 /* This flag will be set if we need to load absolutely all DIEs
435 for this compilation unit, instead of just the ones we think
436 are interesting. It gets set if we look for a DIE in the
437 hash table and don't find it. */
438 unsigned int load_all_dies : 1;
440 /* Non-zero if this CU is from .debug_types.
441 Otherwise it's from .debug_info. */
442 unsigned int from_debug_types : 1;
444 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
445 of the CU cache it gets reset to NULL again. */
446 struct dwarf2_cu *cu;
448 /* If full symbols for this CU have been read in, then this field
449 holds a map of DIE offsets to types. It isn't always possible
450 to reconstruct this information later, so we have to preserve
454 /* The corresponding objfile. */
455 struct objfile *objfile;
457 /* When using partial symbol tables, the 'psymtab' field is active.
458 Otherwise the 'quick' field is active. */
461 /* The partial symbol table associated with this compilation unit,
462 or NULL for partial units (which do not have an associated
464 struct partial_symtab *psymtab;
466 /* Data needed by the "quick" functions. */
467 struct dwarf2_per_cu_quick_data *quick;
471 /* Entry in the signatured_types hash table. */
473 struct signatured_type
477 /* Offset in .debug_types of the TU (type_unit) for this type. */
480 /* Offset in .debug_types of the type defined by this TU. */
481 unsigned int type_offset;
483 /* The CU(/TU) of this type. */
484 struct dwarf2_per_cu_data per_cu;
487 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
488 which are used for both .debug_info and .debug_types dies.
489 All parameters here are unchanging for the life of the call.
490 This struct exists to abstract away the constant parameters of
493 struct die_reader_specs
495 /* The bfd of this objfile. */
498 /* The CU of the DIE we are parsing. */
499 struct dwarf2_cu *cu;
501 /* Pointer to start of section buffer.
502 This is either the start of .debug_info or .debug_types. */
503 const gdb_byte *buffer;
506 /* The line number information for a compilation unit (found in the
507 .debug_line section) begins with a "statement program header",
508 which contains the following information. */
511 unsigned int total_length;
512 unsigned short version;
513 unsigned int header_length;
514 unsigned char minimum_instruction_length;
515 unsigned char maximum_ops_per_instruction;
516 unsigned char default_is_stmt;
518 unsigned char line_range;
519 unsigned char opcode_base;
521 /* standard_opcode_lengths[i] is the number of operands for the
522 standard opcode whose value is i. This means that
523 standard_opcode_lengths[0] is unused, and the last meaningful
524 element is standard_opcode_lengths[opcode_base - 1]. */
525 unsigned char *standard_opcode_lengths;
527 /* The include_directories table. NOTE! These strings are not
528 allocated with xmalloc; instead, they are pointers into
529 debug_line_buffer. If you try to free them, `free' will get
531 unsigned int num_include_dirs, include_dirs_size;
534 /* The file_names table. NOTE! These strings are not allocated
535 with xmalloc; instead, they are pointers into debug_line_buffer.
536 Don't try to free them directly. */
537 unsigned int num_file_names, file_names_size;
541 unsigned int dir_index;
542 unsigned int mod_time;
544 int included_p; /* Non-zero if referenced by the Line Number Program. */
545 struct symtab *symtab; /* The associated symbol table, if any. */
548 /* The start and end of the statement program following this
549 header. These point into dwarf2_per_objfile->line_buffer. */
550 gdb_byte *statement_program_start, *statement_program_end;
553 /* When we construct a partial symbol table entry we only
554 need this much information. */
555 struct partial_die_info
557 /* Offset of this DIE. */
560 /* DWARF-2 tag for this DIE. */
561 ENUM_BITFIELD(dwarf_tag) tag : 16;
563 /* Assorted flags describing the data found in this DIE. */
564 unsigned int has_children : 1;
565 unsigned int is_external : 1;
566 unsigned int is_declaration : 1;
567 unsigned int has_type : 1;
568 unsigned int has_specification : 1;
569 unsigned int has_pc_info : 1;
571 /* Flag set if the SCOPE field of this structure has been
573 unsigned int scope_set : 1;
575 /* Flag set if the DIE has a byte_size attribute. */
576 unsigned int has_byte_size : 1;
578 /* Flag set if any of the DIE's children are template arguments. */
579 unsigned int has_template_arguments : 1;
581 /* The name of this DIE. Normally the value of DW_AT_name, but
582 sometimes a default name for unnamed DIEs. */
585 /* The scope to prepend to our children. This is generally
586 allocated on the comp_unit_obstack, so will disappear
587 when this compilation unit leaves the cache. */
590 /* The location description associated with this DIE, if any. */
591 struct dwarf_block *locdesc;
593 /* If HAS_PC_INFO, the PC range associated with this DIE. */
597 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
598 DW_AT_sibling, if any. */
601 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
602 DW_AT_specification (or DW_AT_abstract_origin or
604 unsigned int spec_offset;
606 /* Pointers to this DIE's parent, first child, and next sibling,
608 struct partial_die_info *die_parent, *die_child, *die_sibling;
611 /* This data structure holds the information of an abbrev. */
614 unsigned int number; /* number identifying abbrev */
615 enum dwarf_tag tag; /* dwarf tag */
616 unsigned short has_children; /* boolean */
617 unsigned short num_attrs; /* number of attributes */
618 struct attr_abbrev *attrs; /* an array of attribute descriptions */
619 struct abbrev_info *next; /* next in chain */
624 ENUM_BITFIELD(dwarf_attribute) name : 16;
625 ENUM_BITFIELD(dwarf_form) form : 16;
628 /* Attributes have a name and a value */
631 ENUM_BITFIELD(dwarf_attribute) name : 16;
632 ENUM_BITFIELD(dwarf_form) form : 15;
634 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
635 field should be in u.str (existing only for DW_STRING) but it is kept
636 here for better struct attribute alignment. */
637 unsigned int string_is_canonical : 1;
642 struct dwarf_block *blk;
646 struct signatured_type *signatured_type;
651 /* This data structure holds a complete die structure. */
654 /* DWARF-2 tag for this DIE. */
655 ENUM_BITFIELD(dwarf_tag) tag : 16;
657 /* Number of attributes */
658 unsigned char num_attrs;
660 /* True if we're presently building the full type name for the
661 type derived from this DIE. */
662 unsigned char building_fullname : 1;
667 /* Offset in .debug_info or .debug_types section. */
670 /* The dies in a compilation unit form an n-ary tree. PARENT
671 points to this die's parent; CHILD points to the first child of
672 this node; and all the children of a given node are chained
673 together via their SIBLING fields, terminated by a die whose
675 struct die_info *child; /* Its first child, if any. */
676 struct die_info *sibling; /* Its next sibling, if any. */
677 struct die_info *parent; /* Its parent, if any. */
679 /* An array of attributes, with NUM_ATTRS elements. There may be
680 zero, but it's not common and zero-sized arrays are not
681 sufficiently portable C. */
682 struct attribute attrs[1];
685 struct function_range
688 CORE_ADDR lowpc, highpc;
690 struct function_range *next;
693 /* Get at parts of an attribute structure */
695 #define DW_STRING(attr) ((attr)->u.str)
696 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
697 #define DW_UNSND(attr) ((attr)->u.unsnd)
698 #define DW_BLOCK(attr) ((attr)->u.blk)
699 #define DW_SND(attr) ((attr)->u.snd)
700 #define DW_ADDR(attr) ((attr)->u.addr)
701 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
703 /* Blocks are a bunch of untyped bytes. */
710 #ifndef ATTR_ALLOC_CHUNK
711 #define ATTR_ALLOC_CHUNK 4
714 /* Allocate fields for structs, unions and enums in this size. */
715 #ifndef DW_FIELD_ALLOC_CHUNK
716 #define DW_FIELD_ALLOC_CHUNK 4
719 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
720 but this would require a corresponding change in unpack_field_as_long
722 static int bits_per_byte = 8;
724 /* The routines that read and process dies for a C struct or C++ class
725 pass lists of data member fields and lists of member function fields
726 in an instance of a field_info structure, as defined below. */
729 /* List of data member and baseclasses fields. */
732 struct nextfield *next;
737 *fields, *baseclasses;
739 /* Number of fields (including baseclasses). */
742 /* Number of baseclasses. */
745 /* Set if the accesibility of one of the fields is not public. */
746 int non_public_fields;
748 /* Member function fields array, entries are allocated in the order they
749 are encountered in the object file. */
752 struct nextfnfield *next;
753 struct fn_field fnfield;
757 /* Member function fieldlist array, contains name of possibly overloaded
758 member function, number of overloaded member functions and a pointer
759 to the head of the member function field chain. */
764 struct nextfnfield *head;
768 /* Number of entries in the fnfieldlists array. */
771 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
772 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
773 struct typedef_field_list
775 struct typedef_field field;
776 struct typedef_field_list *next;
779 unsigned typedef_field_list_count;
782 /* One item on the queue of compilation units to read in full symbols
784 struct dwarf2_queue_item
786 struct dwarf2_per_cu_data *per_cu;
787 struct dwarf2_queue_item *next;
790 /* The current queue. */
791 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
793 /* Loaded secondary compilation units are kept in memory until they
794 have not been referenced for the processing of this many
795 compilation units. Set this to zero to disable caching. Cache
796 sizes of up to at least twenty will improve startup time for
797 typical inter-CU-reference binaries, at an obvious memory cost. */
798 static int dwarf2_max_cache_age = 5;
800 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
801 struct cmd_list_element *c, const char *value)
803 fprintf_filtered (file, _("\
804 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
809 /* Various complaints about symbol reading that don't abort the process */
812 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
814 complaint (&symfile_complaints,
815 _("statement list doesn't fit in .debug_line section"));
819 dwarf2_debug_line_missing_file_complaint (void)
821 complaint (&symfile_complaints,
822 _(".debug_line section has line data without a file"));
826 dwarf2_debug_line_missing_end_sequence_complaint (void)
828 complaint (&symfile_complaints,
829 _(".debug_line section has line program sequence without an end"));
833 dwarf2_complex_location_expr_complaint (void)
835 complaint (&symfile_complaints, _("location expression too complex"));
839 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
842 complaint (&symfile_complaints,
843 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
848 dwarf2_macros_too_long_complaint (void)
850 complaint (&symfile_complaints,
851 _("macro info runs off end of `.debug_macinfo' section"));
855 dwarf2_macro_malformed_definition_complaint (const char *arg1)
857 complaint (&symfile_complaints,
858 _("macro debug info contains a malformed macro definition:\n`%s'"),
863 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
865 complaint (&symfile_complaints,
866 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
869 /* local function prototypes */
871 static void dwarf2_locate_sections (bfd *, asection *, void *);
873 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
876 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
878 struct partial_symtab *);
880 static void dwarf2_build_psymtabs_hard (struct objfile *);
882 static void scan_partial_symbols (struct partial_die_info *,
883 CORE_ADDR *, CORE_ADDR *,
884 int, struct dwarf2_cu *);
886 static void add_partial_symbol (struct partial_die_info *,
889 static void add_partial_namespace (struct partial_die_info *pdi,
890 CORE_ADDR *lowpc, CORE_ADDR *highpc,
891 int need_pc, struct dwarf2_cu *cu);
893 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
894 CORE_ADDR *highpc, int need_pc,
895 struct dwarf2_cu *cu);
897 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
898 struct dwarf2_cu *cu);
900 static void add_partial_subprogram (struct partial_die_info *pdi,
901 CORE_ADDR *lowpc, CORE_ADDR *highpc,
902 int need_pc, struct dwarf2_cu *cu);
904 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
905 gdb_byte *buffer, gdb_byte *info_ptr,
906 bfd *abfd, struct dwarf2_cu *cu);
908 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
910 static void psymtab_to_symtab_1 (struct partial_symtab *);
912 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
914 static void dwarf2_free_abbrev_table (void *);
916 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
919 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
922 static struct partial_die_info *load_partial_dies (bfd *,
923 gdb_byte *, gdb_byte *,
924 int, struct dwarf2_cu *);
926 static gdb_byte *read_partial_die (struct partial_die_info *,
927 struct abbrev_info *abbrev,
929 gdb_byte *, gdb_byte *,
932 static struct partial_die_info *find_partial_die (unsigned int,
935 static void fixup_partial_die (struct partial_die_info *,
938 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
939 bfd *, gdb_byte *, struct dwarf2_cu *);
941 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
942 bfd *, gdb_byte *, struct dwarf2_cu *);
944 static unsigned int read_1_byte (bfd *, gdb_byte *);
946 static int read_1_signed_byte (bfd *, gdb_byte *);
948 static unsigned int read_2_bytes (bfd *, gdb_byte *);
950 static unsigned int read_4_bytes (bfd *, gdb_byte *);
952 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
954 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
957 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
959 static LONGEST read_checked_initial_length_and_offset
960 (bfd *, gdb_byte *, const struct comp_unit_head *,
961 unsigned int *, unsigned int *);
963 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
966 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
968 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
970 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
972 static char *read_indirect_string (bfd *, gdb_byte *,
973 const struct comp_unit_head *,
976 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
978 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
980 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
982 static void set_cu_language (unsigned int, struct dwarf2_cu *);
984 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
991 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
992 struct dwarf2_cu *cu);
994 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
996 static struct die_info *die_specification (struct die_info *die,
997 struct dwarf2_cu **);
999 static void free_line_header (struct line_header *lh);
1001 static void add_file_name (struct line_header *, char *, unsigned int,
1002 unsigned int, unsigned int);
1004 static struct line_header *(dwarf_decode_line_header
1005 (unsigned int offset,
1006 bfd *abfd, struct dwarf2_cu *cu));
1008 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
1009 struct dwarf2_cu *, struct partial_symtab *);
1011 static void dwarf2_start_subfile (char *, char *, char *);
1013 static struct symbol *new_symbol (struct die_info *, struct type *,
1014 struct dwarf2_cu *);
1016 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1017 struct dwarf2_cu *, struct symbol *);
1019 static void dwarf2_const_value (struct attribute *, struct symbol *,
1020 struct dwarf2_cu *);
1022 static void dwarf2_const_value_attr (struct attribute *attr,
1025 struct obstack *obstack,
1026 struct dwarf2_cu *cu, long *value,
1028 struct dwarf2_locexpr_baton **baton);
1030 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1032 static int need_gnat_info (struct dwarf2_cu *);
1034 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1036 static void set_descriptive_type (struct type *, struct die_info *,
1037 struct dwarf2_cu *);
1039 static struct type *die_containing_type (struct die_info *,
1040 struct dwarf2_cu *);
1042 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
1044 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1046 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1048 static char *typename_concat (struct obstack *obs, const char *prefix,
1049 const char *suffix, int physname,
1050 struct dwarf2_cu *cu);
1052 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1054 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1056 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1058 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1060 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1061 struct dwarf2_cu *, struct partial_symtab *);
1063 static int dwarf2_get_pc_bounds (struct die_info *,
1064 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1065 struct partial_symtab *);
1067 static void get_scope_pc_bounds (struct die_info *,
1068 CORE_ADDR *, CORE_ADDR *,
1069 struct dwarf2_cu *);
1071 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1072 CORE_ADDR, struct dwarf2_cu *);
1074 static void dwarf2_add_field (struct field_info *, struct die_info *,
1075 struct dwarf2_cu *);
1077 static void dwarf2_attach_fields_to_type (struct field_info *,
1078 struct type *, struct dwarf2_cu *);
1080 static void dwarf2_add_member_fn (struct field_info *,
1081 struct die_info *, struct type *,
1082 struct dwarf2_cu *);
1084 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1085 struct type *, struct dwarf2_cu *);
1087 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1089 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1091 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1093 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1095 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1097 static struct type *read_module_type (struct die_info *die,
1098 struct dwarf2_cu *cu);
1100 static const char *namespace_name (struct die_info *die,
1101 int *is_anonymous, struct dwarf2_cu *);
1103 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1105 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1107 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1108 struct dwarf2_cu *);
1110 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1112 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1114 gdb_byte **new_info_ptr,
1115 struct die_info *parent);
1117 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1119 gdb_byte **new_info_ptr,
1120 struct die_info *parent);
1122 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1124 gdb_byte **new_info_ptr,
1125 struct die_info *parent);
1127 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1128 struct die_info **, gdb_byte *,
1131 static void process_die (struct die_info *, struct dwarf2_cu *);
1133 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1136 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1138 static const char *dwarf2_full_name (char *name,
1139 struct die_info *die,
1140 struct dwarf2_cu *cu);
1142 static struct die_info *dwarf2_extension (struct die_info *die,
1143 struct dwarf2_cu **);
1145 static char *dwarf_tag_name (unsigned int);
1147 static char *dwarf_attr_name (unsigned int);
1149 static char *dwarf_form_name (unsigned int);
1151 static char *dwarf_bool_name (unsigned int);
1153 static char *dwarf_type_encoding_name (unsigned int);
1156 static char *dwarf_cfi_name (unsigned int);
1159 static struct die_info *sibling_die (struct die_info *);
1161 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1163 static void dump_die_for_error (struct die_info *);
1165 static void dump_die_1 (struct ui_file *, int level, int max_level,
1168 /*static*/ void dump_die (struct die_info *, int max_level);
1170 static void store_in_ref_table (struct die_info *,
1171 struct dwarf2_cu *);
1173 static int is_ref_attr (struct attribute *);
1175 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1177 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1179 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1181 struct dwarf2_cu **);
1183 static struct die_info *follow_die_ref (struct die_info *,
1185 struct dwarf2_cu **);
1187 static struct die_info *follow_die_sig (struct die_info *,
1189 struct dwarf2_cu **);
1191 static void read_signatured_type_at_offset (struct objfile *objfile,
1192 unsigned int offset);
1194 static void read_signatured_type (struct objfile *,
1195 struct signatured_type *type_sig);
1197 /* memory allocation interface */
1199 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1201 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1203 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1205 static void initialize_cu_func_list (struct dwarf2_cu *);
1207 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1208 struct dwarf2_cu *);
1210 static void dwarf_decode_macros (struct line_header *, unsigned int,
1211 char *, bfd *, struct dwarf2_cu *);
1213 static int attr_form_is_block (struct attribute *);
1215 static int attr_form_is_section_offset (struct attribute *);
1217 static int attr_form_is_constant (struct attribute *);
1219 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1221 struct dwarf2_cu *cu);
1223 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1224 struct abbrev_info *abbrev,
1225 struct dwarf2_cu *cu);
1227 static void free_stack_comp_unit (void *);
1229 static hashval_t partial_die_hash (const void *item);
1231 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1233 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1234 (unsigned int offset, struct objfile *objfile);
1236 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1237 (unsigned int offset, struct objfile *objfile);
1239 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1241 static void free_one_comp_unit (void *);
1243 static void free_cached_comp_units (void *);
1245 static void age_cached_comp_units (void);
1247 static void free_one_cached_comp_unit (void *);
1249 static struct type *set_die_type (struct die_info *, struct type *,
1250 struct dwarf2_cu *);
1252 static void create_all_comp_units (struct objfile *);
1254 static int create_debug_types_hash_table (struct objfile *objfile);
1256 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1259 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1261 static void dwarf2_add_dependence (struct dwarf2_cu *,
1262 struct dwarf2_per_cu_data *);
1264 static void dwarf2_mark (struct dwarf2_cu *);
1266 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1268 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1270 static void dwarf2_release_queue (void *dummy);
1272 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1273 struct objfile *objfile);
1275 static void process_queue (struct objfile *objfile);
1277 static void find_file_and_directory (struct die_info *die,
1278 struct dwarf2_cu *cu,
1279 char **name, char **comp_dir);
1281 static char *file_full_name (int file, struct line_header *lh,
1282 const char *comp_dir);
1284 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1287 unsigned int buffer_size,
1290 static void init_cu_die_reader (struct die_reader_specs *reader,
1291 struct dwarf2_cu *cu);
1293 static htab_t allocate_signatured_type_hash_table (struct objfile *objfile);
1297 /* Convert VALUE between big- and little-endian. */
1299 byte_swap (offset_type value)
1303 result = (value & 0xff) << 24;
1304 result |= (value & 0xff00) << 8;
1305 result |= (value & 0xff0000) >> 8;
1306 result |= (value & 0xff000000) >> 24;
1310 #define MAYBE_SWAP(V) byte_swap (V)
1313 #define MAYBE_SWAP(V) (V)
1314 #endif /* WORDS_BIGENDIAN */
1316 /* The suffix for an index file. */
1317 #define INDEX_SUFFIX ".gdb-index"
1319 static const char *dwarf2_physname (char *name, struct die_info *die,
1320 struct dwarf2_cu *cu);
1322 /* Try to locate the sections we need for DWARF 2 debugging
1323 information and return true if we have enough to do something. */
1326 dwarf2_has_info (struct objfile *objfile)
1328 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1329 if (!dwarf2_per_objfile)
1331 /* Initialize per-objfile state. */
1332 struct dwarf2_per_objfile *data
1333 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1335 memset (data, 0, sizeof (*data));
1336 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1337 dwarf2_per_objfile = data;
1339 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1340 dwarf2_per_objfile->objfile = objfile;
1342 return (dwarf2_per_objfile->info.asection != NULL
1343 && dwarf2_per_objfile->abbrev.asection != NULL);
1346 /* When loading sections, we can either look for ".<name>", or for
1347 * ".z<name>", which indicates a compressed section. */
1350 section_is_p (const char *section_name, const char *name)
1352 return (section_name[0] == '.'
1353 && (strcmp (section_name + 1, name) == 0
1354 || (section_name[1] == 'z'
1355 && strcmp (section_name + 2, name) == 0)));
1358 /* This function is mapped across the sections and remembers the
1359 offset and size of each of the debugging sections we are interested
1363 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1365 if (section_is_p (sectp->name, INFO_SECTION))
1367 dwarf2_per_objfile->info.asection = sectp;
1368 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1370 else if (section_is_p (sectp->name, ABBREV_SECTION))
1372 dwarf2_per_objfile->abbrev.asection = sectp;
1373 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1375 else if (section_is_p (sectp->name, LINE_SECTION))
1377 dwarf2_per_objfile->line.asection = sectp;
1378 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1380 else if (section_is_p (sectp->name, LOC_SECTION))
1382 dwarf2_per_objfile->loc.asection = sectp;
1383 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1385 else if (section_is_p (sectp->name, MACINFO_SECTION))
1387 dwarf2_per_objfile->macinfo.asection = sectp;
1388 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1390 else if (section_is_p (sectp->name, STR_SECTION))
1392 dwarf2_per_objfile->str.asection = sectp;
1393 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1395 else if (section_is_p (sectp->name, FRAME_SECTION))
1397 dwarf2_per_objfile->frame.asection = sectp;
1398 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1400 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1402 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1404 if (aflag & SEC_HAS_CONTENTS)
1406 dwarf2_per_objfile->eh_frame.asection = sectp;
1407 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1410 else if (section_is_p (sectp->name, RANGES_SECTION))
1412 dwarf2_per_objfile->ranges.asection = sectp;
1413 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1415 else if (section_is_p (sectp->name, TYPES_SECTION))
1417 dwarf2_per_objfile->types.asection = sectp;
1418 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1420 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1422 dwarf2_per_objfile->gdb_index.asection = sectp;
1423 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1426 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1427 && bfd_section_vma (abfd, sectp) == 0)
1428 dwarf2_per_objfile->has_section_at_zero = 1;
1431 /* Decompress a section that was compressed using zlib. Store the
1432 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1435 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1436 gdb_byte **outbuf, bfd_size_type *outsize)
1438 bfd *abfd = objfile->obfd;
1440 error (_("Support for zlib-compressed DWARF data (from '%s') "
1441 "is disabled in this copy of GDB"),
1442 bfd_get_filename (abfd));
1444 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1445 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1446 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1447 bfd_size_type uncompressed_size;
1448 gdb_byte *uncompressed_buffer;
1451 int header_size = 12;
1453 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1454 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1455 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1456 bfd_get_filename (abfd));
1458 /* Read the zlib header. In this case, it should be "ZLIB" followed
1459 by the uncompressed section size, 8 bytes in big-endian order. */
1460 if (compressed_size < header_size
1461 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1462 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1463 bfd_get_filename (abfd));
1464 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1465 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[11];
1473 /* It is possible the section consists of several compressed
1474 buffers concatenated together, so we uncompress in a loop. */
1478 strm.avail_in = compressed_size - header_size;
1479 strm.next_in = (Bytef*) compressed_buffer + header_size;
1480 strm.avail_out = uncompressed_size;
1481 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1483 rc = inflateInit (&strm);
1484 while (strm.avail_in > 0)
1487 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1488 bfd_get_filename (abfd), rc);
1489 strm.next_out = ((Bytef*) uncompressed_buffer
1490 + (uncompressed_size - strm.avail_out));
1491 rc = inflate (&strm, Z_FINISH);
1492 if (rc != Z_STREAM_END)
1493 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1494 bfd_get_filename (abfd), rc);
1495 rc = inflateReset (&strm);
1497 rc = inflateEnd (&strm);
1499 || strm.avail_out != 0)
1500 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1501 bfd_get_filename (abfd), rc);
1503 do_cleanups (cleanup);
1504 *outbuf = uncompressed_buffer;
1505 *outsize = uncompressed_size;
1509 /* Read the contents of the section SECTP from object file specified by
1510 OBJFILE, store info about the section into INFO.
1511 If the section is compressed, uncompress it before returning. */
1514 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1516 bfd *abfd = objfile->obfd;
1517 asection *sectp = info->asection;
1518 gdb_byte *buf, *retbuf;
1519 unsigned char header[4];
1523 info->buffer = NULL;
1524 info->was_mmapped = 0;
1527 if (info->asection == NULL || info->size == 0)
1530 /* Check if the file has a 4-byte header indicating compression. */
1531 if (info->size > sizeof (header)
1532 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1533 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1535 /* Upon decompression, update the buffer and its size. */
1536 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1538 zlib_decompress_section (objfile, sectp, &info->buffer,
1546 pagesize = getpagesize ();
1548 /* Only try to mmap sections which are large enough: we don't want to
1549 waste space due to fragmentation. Also, only try mmap for sections
1550 without relocations. */
1552 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1554 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1555 size_t map_length = info->size + sectp->filepos - pg_offset;
1556 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1557 MAP_PRIVATE, pg_offset);
1559 if (retbuf != MAP_FAILED)
1561 info->was_mmapped = 1;
1562 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1563 #if HAVE_POSIX_MADVISE
1564 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1571 /* If we get here, we are a normal, not-compressed section. */
1573 = obstack_alloc (&objfile->objfile_obstack, info->size);
1575 /* When debugging .o files, we may need to apply relocations; see
1576 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1577 We never compress sections in .o files, so we only need to
1578 try this when the section is not compressed. */
1579 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1582 info->buffer = retbuf;
1586 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1587 || bfd_bread (buf, info->size, abfd) != info->size)
1588 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1589 bfd_get_filename (abfd));
1592 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1596 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1597 asection **sectp, gdb_byte **bufp,
1598 bfd_size_type *sizep)
1600 struct dwarf2_per_objfile *data
1601 = objfile_data (objfile, dwarf2_objfile_data_key);
1602 struct dwarf2_section_info *info;
1604 /* We may see an objfile without any DWARF, in which case we just
1613 if (section_is_p (section_name, EH_FRAME_SECTION))
1614 info = &data->eh_frame;
1615 else if (section_is_p (section_name, FRAME_SECTION))
1616 info = &data->frame;
1618 gdb_assert_not_reached ("unexpected section");
1620 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1621 /* We haven't read this section in yet. Do it now. */
1622 dwarf2_read_section (objfile, info);
1624 *sectp = info->asection;
1625 *bufp = info->buffer;
1626 *sizep = info->size;
1631 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1634 dw2_do_instantiate_symtab (struct objfile *objfile,
1635 struct dwarf2_per_cu_data *per_cu)
1637 struct cleanup *back_to;
1639 back_to = make_cleanup (dwarf2_release_queue, NULL);
1641 queue_comp_unit (per_cu, objfile);
1643 if (per_cu->from_debug_types)
1644 read_signatured_type_at_offset (objfile, per_cu->offset);
1646 load_full_comp_unit (per_cu, objfile);
1648 process_queue (objfile);
1650 /* Age the cache, releasing compilation units that have not
1651 been used recently. */
1652 age_cached_comp_units ();
1654 do_cleanups (back_to);
1657 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1658 the objfile from which this CU came. Returns the resulting symbol
1660 static struct symtab *
1661 dw2_instantiate_symtab (struct objfile *objfile,
1662 struct dwarf2_per_cu_data *per_cu)
1664 if (!per_cu->v.quick->symtab)
1666 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1667 increment_reading_symtab ();
1668 dw2_do_instantiate_symtab (objfile, per_cu);
1669 do_cleanups (back_to);
1671 return per_cu->v.quick->symtab;
1674 /* Return the CU given its index. */
1675 static struct dwarf2_per_cu_data *
1676 dw2_get_cu (int index)
1678 if (index >= dwarf2_per_objfile->n_comp_units)
1680 index -= dwarf2_per_objfile->n_comp_units;
1681 return dwarf2_per_objfile->type_comp_units[index];
1683 return dwarf2_per_objfile->all_comp_units[index];
1686 /* A helper function that knows how to read a 64-bit value in a way
1687 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1690 extract_cu_value (const char *bytes, ULONGEST *result)
1692 if (sizeof (ULONGEST) < 8)
1696 /* Ignore the upper 4 bytes if they are all zero. */
1697 for (i = 0; i < 4; ++i)
1698 if (bytes[i + 4] != 0)
1701 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1704 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1708 /* Read the CU list from the mapped index, and use it to create all
1709 the CU objects for this objfile. Return 0 if something went wrong,
1710 1 if everything went ok. */
1712 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1713 offset_type cu_list_elements)
1717 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1718 dwarf2_per_objfile->all_comp_units
1719 = obstack_alloc (&objfile->objfile_obstack,
1720 dwarf2_per_objfile->n_comp_units
1721 * sizeof (struct dwarf2_per_cu_data *));
1723 for (i = 0; i < cu_list_elements; i += 2)
1725 struct dwarf2_per_cu_data *the_cu;
1726 ULONGEST offset, length;
1728 if (!extract_cu_value (cu_list, &offset)
1729 || !extract_cu_value (cu_list + 8, &length))
1733 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1734 struct dwarf2_per_cu_data);
1735 the_cu->offset = offset;
1736 the_cu->length = length;
1737 the_cu->objfile = objfile;
1738 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1739 struct dwarf2_per_cu_quick_data);
1740 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1746 /* Create the signatured type hash table from the index. */
1748 create_signatured_type_hash_from_index (struct objfile *objfile,
1749 const gdb_byte *bytes,
1750 offset_type elements)
1755 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1756 dwarf2_per_objfile->type_comp_units
1757 = obstack_alloc (&objfile->objfile_obstack,
1758 dwarf2_per_objfile->n_type_comp_units
1759 * sizeof (struct dwarf2_per_cu_data *));
1761 type_hash = allocate_signatured_type_hash_table (objfile);
1763 for (i = 0; i < elements; i += 3)
1765 struct signatured_type *type_sig;
1766 ULONGEST offset, type_offset, signature;
1769 if (!extract_cu_value (bytes, &offset)
1770 || !extract_cu_value (bytes + 8, &type_offset))
1772 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1775 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1776 struct signatured_type);
1777 type_sig->signature = signature;
1778 type_sig->offset = offset;
1779 type_sig->type_offset = type_offset;
1780 type_sig->per_cu.from_debug_types = 1;
1781 type_sig->per_cu.offset = offset;
1782 type_sig->per_cu.objfile = objfile;
1783 type_sig->per_cu.v.quick
1784 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1785 struct dwarf2_per_cu_quick_data);
1787 slot = htab_find_slot (type_hash, type_sig, INSERT);
1790 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1793 dwarf2_per_objfile->signatured_types = type_hash;
1798 /* Read the address map data from the mapped index, and use it to
1799 populate the objfile's psymtabs_addrmap. */
1801 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1803 const gdb_byte *iter, *end;
1804 struct obstack temp_obstack;
1805 struct addrmap *mutable_map;
1806 struct cleanup *cleanup;
1809 obstack_init (&temp_obstack);
1810 cleanup = make_cleanup_obstack_free (&temp_obstack);
1811 mutable_map = addrmap_create_mutable (&temp_obstack);
1813 iter = index->address_table;
1814 end = iter + index->address_table_size;
1816 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1820 ULONGEST hi, lo, cu_index;
1821 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1823 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1825 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1828 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1829 dw2_get_cu (cu_index));
1832 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1833 &objfile->objfile_obstack);
1834 do_cleanups (cleanup);
1837 /* The hash function for strings in the mapped index. This is the
1838 same as the hashtab.c hash function, but we keep a separate copy to
1839 maintain control over the implementation. This is necessary
1840 because the hash function is tied to the format of the mapped index
1843 mapped_index_string_hash (const void *p)
1845 const unsigned char *str = (const unsigned char *) p;
1849 while ((c = *str++) != 0)
1850 r = r * 67 + c - 113;
1855 /* Find a slot in the mapped index INDEX for the object named NAME.
1856 If NAME is found, set *VEC_OUT to point to the CU vector in the
1857 constant pool and return 1. If NAME cannot be found, return 0. */
1859 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1860 offset_type **vec_out)
1862 offset_type hash = mapped_index_string_hash (name);
1863 offset_type slot, step;
1865 slot = hash & (index->index_table_slots - 1);
1866 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1870 /* Convert a slot number to an offset into the table. */
1871 offset_type i = 2 * slot;
1873 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1876 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1877 if (!strcmp (name, str))
1879 *vec_out = (offset_type *) (index->constant_pool
1880 + MAYBE_SWAP (index->index_table[i + 1]));
1884 slot = (slot + step) & (index->index_table_slots - 1);
1888 /* Read the index file. If everything went ok, initialize the "quick"
1889 elements of all the CUs and return 1. Otherwise, return 0. */
1891 dwarf2_read_index (struct objfile *objfile)
1894 struct mapped_index *map;
1895 offset_type *metadata;
1896 const gdb_byte *cu_list;
1897 const gdb_byte *types_list = NULL;
1898 offset_type version, cu_list_elements;
1899 offset_type types_list_elements = 0;
1902 if (dwarf2_per_objfile->gdb_index.asection == NULL
1903 || dwarf2_per_objfile->gdb_index.size == 0)
1905 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1907 addr = dwarf2_per_objfile->gdb_index.buffer;
1908 /* Version check. */
1909 version = MAYBE_SWAP (*(offset_type *) addr);
1912 /* Index version 1 neglected to account for .debug_types. So,
1913 if we see .debug_types, we cannot use this index. */
1914 if (dwarf2_per_objfile->types.asection != NULL
1915 && dwarf2_per_objfile->types.size != 0)
1918 else if (version != 2)
1921 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1922 map->total_size = dwarf2_per_objfile->gdb_index.size;
1924 metadata = (offset_type *) (addr + sizeof (offset_type));
1927 cu_list = addr + MAYBE_SWAP (metadata[i]);
1928 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1934 types_list = addr + MAYBE_SWAP (metadata[i]);
1935 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1936 - MAYBE_SWAP (metadata[i]))
1941 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1942 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1943 - MAYBE_SWAP (metadata[i]));
1946 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1947 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1948 - MAYBE_SWAP (metadata[i]))
1949 / (2 * sizeof (offset_type)));
1952 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1954 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1958 && types_list_elements
1959 && !create_signatured_type_hash_from_index (objfile, types_list,
1960 types_list_elements))
1963 create_addrmap_from_index (objfile, map);
1965 dwarf2_per_objfile->index_table = map;
1966 dwarf2_per_objfile->using_index = 1;
1971 /* A helper for the "quick" functions which sets the global
1972 dwarf2_per_objfile according to OBJFILE. */
1974 dw2_setup (struct objfile *objfile)
1976 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1977 gdb_assert (dwarf2_per_objfile);
1980 /* A helper for the "quick" functions which attempts to read the line
1981 table for THIS_CU. */
1983 dw2_require_line_header (struct objfile *objfile,
1984 struct dwarf2_per_cu_data *this_cu)
1986 bfd *abfd = objfile->obfd;
1987 struct line_header *lh = NULL;
1988 struct attribute *attr;
1989 struct cleanup *cleanups;
1990 struct die_info *comp_unit_die;
1991 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1992 int has_children, i;
1993 struct dwarf2_cu cu;
1994 unsigned int bytes_read, buffer_size;
1995 struct die_reader_specs reader_specs;
1996 char *name, *comp_dir;
1998 if (this_cu->v.quick->read_lines)
2000 this_cu->v.quick->read_lines = 1;
2002 memset (&cu, 0, sizeof (cu));
2003 cu.objfile = objfile;
2004 obstack_init (&cu.comp_unit_obstack);
2006 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2008 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2009 buffer_size = dwarf2_per_objfile->info.size;
2010 buffer = dwarf2_per_objfile->info.buffer;
2011 info_ptr = buffer + this_cu->offset;
2012 beg_of_comp_unit = info_ptr;
2014 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2015 buffer, buffer_size,
2018 /* Complete the cu_header. */
2019 cu.header.offset = beg_of_comp_unit - buffer;
2020 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2023 cu.per_cu = this_cu;
2025 dwarf2_read_abbrevs (abfd, &cu);
2026 make_cleanup (dwarf2_free_abbrev_table, &cu);
2028 if (this_cu->from_debug_types)
2029 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2030 init_cu_die_reader (&reader_specs, &cu);
2031 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2034 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2037 unsigned int line_offset = DW_UNSND (attr);
2038 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2042 do_cleanups (cleanups);
2046 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2048 this_cu->v.quick->lines = lh;
2050 this_cu->v.quick->file_names
2051 = obstack_alloc (&objfile->objfile_obstack,
2052 lh->num_file_names * sizeof (char *));
2053 for (i = 0; i < lh->num_file_names; ++i)
2054 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2056 do_cleanups (cleanups);
2059 /* A helper for the "quick" functions which computes and caches the
2060 real path for a given file name from the line table.
2061 dw2_require_line_header must have been called before this is
2064 dw2_require_full_path (struct objfile *objfile,
2065 struct dwarf2_per_cu_data *cu,
2068 if (!cu->v.quick->full_names)
2069 cu->v.quick->full_names
2070 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2071 cu->v.quick->lines->num_file_names,
2074 if (!cu->v.quick->full_names[index])
2075 cu->v.quick->full_names[index]
2076 = gdb_realpath (cu->v.quick->file_names[index]);
2078 return cu->v.quick->full_names[index];
2081 static struct symtab *
2082 dw2_find_last_source_symtab (struct objfile *objfile)
2085 dw2_setup (objfile);
2086 index = dwarf2_per_objfile->n_comp_units - 1;
2087 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2091 dw2_forget_cached_source_info (struct objfile *objfile)
2095 dw2_setup (objfile);
2096 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2097 + dwarf2_per_objfile->n_type_comp_units); ++i)
2099 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2101 if (cu->v.quick->full_names)
2105 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2106 xfree ((void *) cu->v.quick->full_names[j]);
2112 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2113 const char *full_path, const char *real_path,
2114 struct symtab **result)
2117 int check_basename = lbasename (name) == name;
2118 struct dwarf2_per_cu_data *base_cu = NULL;
2120 dw2_setup (objfile);
2121 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2122 + dwarf2_per_objfile->n_type_comp_units); ++i)
2125 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2127 if (cu->v.quick->symtab)
2130 dw2_require_line_header (objfile, cu);
2131 if (!cu->v.quick->lines)
2134 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2136 const char *this_name = cu->v.quick->file_names[j];
2138 if (FILENAME_CMP (name, this_name) == 0)
2140 *result = dw2_instantiate_symtab (objfile, cu);
2144 if (check_basename && ! base_cu
2145 && FILENAME_CMP (lbasename (this_name), name) == 0)
2148 if (full_path != NULL)
2150 const char *this_full_name = dw2_require_full_path (objfile,
2154 && FILENAME_CMP (full_path, this_full_name) == 0)
2156 *result = dw2_instantiate_symtab (objfile, cu);
2161 if (real_path != NULL)
2163 const char *this_full_name = dw2_require_full_path (objfile,
2166 if (this_full_name != NULL)
2168 char *rp = gdb_realpath (this_full_name);
2169 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2172 *result = dw2_instantiate_symtab (objfile, cu);
2183 *result = dw2_instantiate_symtab (objfile, base_cu);
2190 static struct symtab *
2191 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2192 const char *name, domain_enum domain)
2194 /* We do all the work in the pre_expand_symtabs_matching hook
2199 /* A helper function that expands all symtabs that hold an object
2202 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2204 dw2_setup (objfile);
2206 if (dwarf2_per_objfile->index_table)
2210 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2213 offset_type i, len = MAYBE_SWAP (*vec);
2214 for (i = 0; i < len; ++i)
2216 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2217 struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2219 dw2_instantiate_symtab (objfile, cu);
2226 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2227 int kind, const char *name,
2230 dw2_do_expand_symtabs_matching (objfile, name);
2234 dw2_print_stats (struct objfile *objfile)
2238 dw2_setup (objfile);
2240 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2241 + dwarf2_per_objfile->n_type_comp_units); ++i)
2243 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2245 if (!cu->v.quick->symtab)
2248 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2252 dw2_dump (struct objfile *objfile)
2254 /* Nothing worth printing. */
2258 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2259 struct section_offsets *delta)
2261 /* There's nothing to relocate here. */
2265 dw2_expand_symtabs_for_function (struct objfile *objfile,
2266 const char *func_name)
2268 dw2_do_expand_symtabs_matching (objfile, func_name);
2272 dw2_expand_all_symtabs (struct objfile *objfile)
2276 dw2_setup (objfile);
2278 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2279 + dwarf2_per_objfile->n_type_comp_units); ++i)
2281 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2283 dw2_instantiate_symtab (objfile, cu);
2288 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2289 const char *filename)
2293 dw2_setup (objfile);
2294 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2295 + dwarf2_per_objfile->n_type_comp_units); ++i)
2298 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2300 if (cu->v.quick->symtab)
2303 dw2_require_line_header (objfile, cu);
2304 if (!cu->v.quick->lines)
2307 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2309 const char *this_name = cu->v.quick->file_names[j];
2310 if (strcmp (this_name, filename) == 0)
2312 dw2_instantiate_symtab (objfile, cu);
2320 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2322 struct dwarf2_per_cu_data *cu;
2325 dw2_setup (objfile);
2327 if (!dwarf2_per_objfile->index_table)
2330 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2334 /* Note that this just looks at the very first one named NAME -- but
2335 actually we are looking for a function. find_main_filename
2336 should be rewritten so that it doesn't require a custom hook. It
2337 could just use the ordinary symbol tables. */
2338 /* vec[0] is the length, which must always be >0. */
2339 cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2341 dw2_require_line_header (objfile, cu);
2342 if (!cu->v.quick->lines)
2345 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
2349 dw2_map_ada_symtabs (struct objfile *objfile,
2350 int (*wild_match) (const char *, int, const char *),
2351 int (*is_name_suffix) (const char *),
2352 void (*callback) (struct objfile *,
2353 struct symtab *, void *),
2354 const char *name, int global,
2355 domain_enum namespace, int wild,
2358 /* For now, we don't support Ada, so this function can't be
2360 internal_error (__FILE__, __LINE__,
2361 _("map_ada_symtabs called via index method"));
2365 dw2_expand_symtabs_matching (struct objfile *objfile,
2366 int (*file_matcher) (const char *, void *),
2367 int (*name_matcher) (const char *, void *),
2374 dw2_setup (objfile);
2375 if (!dwarf2_per_objfile->index_table)
2378 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2379 + dwarf2_per_objfile->n_type_comp_units); ++i)
2382 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2384 cu->v.quick->mark = 0;
2385 if (cu->v.quick->symtab)
2388 dw2_require_line_header (objfile, cu);
2389 if (!cu->v.quick->lines)
2392 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2394 if (file_matcher (cu->v.quick->file_names[j], data))
2396 cu->v.quick->mark = 1;
2403 iter < dwarf2_per_objfile->index_table->index_table_slots;
2406 offset_type idx = 2 * iter;
2408 offset_type *vec, vec_len, vec_idx;
2410 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2411 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2414 name = (dwarf2_per_objfile->index_table->constant_pool
2415 + dwarf2_per_objfile->index_table->index_table[idx]);
2417 if (! (*name_matcher) (name, data))
2420 /* The name was matched, now expand corresponding CUs that were
2422 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2423 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2424 vec_len = MAYBE_SWAP (vec[0]);
2425 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2427 struct dwarf2_per_cu_data *cu;
2429 cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2430 if (cu->v.quick->mark)
2431 dw2_instantiate_symtab (objfile, cu);
2436 static struct symtab *
2437 dw2_find_pc_sect_symtab (struct objfile *objfile,
2438 struct minimal_symbol *msymbol,
2440 struct obj_section *section,
2443 struct dwarf2_per_cu_data *data;
2445 dw2_setup (objfile);
2447 if (!objfile->psymtabs_addrmap)
2450 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2454 if (warn_if_readin && data->v.quick->symtab)
2455 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2456 paddress (get_objfile_arch (objfile), pc));
2458 return dw2_instantiate_symtab (objfile, data);
2462 dw2_map_symbol_names (struct objfile *objfile,
2463 void (*fun) (const char *, void *),
2467 dw2_setup (objfile);
2469 if (!dwarf2_per_objfile->index_table)
2473 iter < dwarf2_per_objfile->index_table->index_table_slots;
2476 offset_type idx = 2 * iter;
2478 offset_type *vec, vec_len, vec_idx;
2480 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2481 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2484 name = (dwarf2_per_objfile->index_table->constant_pool
2485 + dwarf2_per_objfile->index_table->index_table[idx]);
2487 (*fun) (name, data);
2492 dw2_map_symbol_filenames (struct objfile *objfile,
2493 void (*fun) (const char *, const char *, void *),
2498 dw2_setup (objfile);
2499 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2500 + dwarf2_per_objfile->n_type_comp_units); ++i)
2503 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2505 if (cu->v.quick->symtab)
2508 dw2_require_line_header (objfile, cu);
2509 if (!cu->v.quick->lines)
2512 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2514 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2515 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2521 dw2_has_symbols (struct objfile *objfile)
2526 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2529 dw2_find_last_source_symtab,
2530 dw2_forget_cached_source_info,
2533 dw2_pre_expand_symtabs_matching,
2537 dw2_expand_symtabs_for_function,
2538 dw2_expand_all_symtabs,
2539 dw2_expand_symtabs_with_filename,
2540 dw2_find_symbol_file,
2541 dw2_map_ada_symtabs,
2542 dw2_expand_symtabs_matching,
2543 dw2_find_pc_sect_symtab,
2544 dw2_map_symbol_names,
2545 dw2_map_symbol_filenames
2548 /* Initialize for reading DWARF for this objfile. Return 0 if this
2549 file will use psymtabs, or 1 if using the GNU index. */
2552 dwarf2_initialize_objfile (struct objfile *objfile)
2554 /* If we're about to read full symbols, don't bother with the
2555 indices. In this case we also don't care if some other debug
2556 format is making psymtabs, because they are all about to be
2558 if ((objfile->flags & OBJF_READNOW))
2562 dwarf2_per_objfile->using_index = 1;
2563 create_all_comp_units (objfile);
2564 create_debug_types_hash_table (objfile);
2566 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2567 + dwarf2_per_objfile->n_type_comp_units); ++i)
2569 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2571 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2572 struct dwarf2_per_cu_quick_data);
2575 /* Return 1 so that gdb sees the "quick" functions. However,
2576 these functions will be no-ops because we will have expanded
2581 if (dwarf2_read_index (objfile))
2584 dwarf2_build_psymtabs (objfile);
2590 /* Build a partial symbol table. */
2593 dwarf2_build_psymtabs (struct objfile *objfile)
2595 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2597 init_psymbol_list (objfile, 1024);
2600 dwarf2_build_psymtabs_hard (objfile);
2603 /* Return TRUE if OFFSET is within CU_HEADER. */
2606 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2608 unsigned int bottom = cu_header->offset;
2609 unsigned int top = (cu_header->offset
2611 + cu_header->initial_length_size);
2613 return (offset >= bottom && offset < top);
2616 /* Read in the comp unit header information from the debug_info at info_ptr.
2617 NOTE: This leaves members offset, first_die_offset to be filled in
2621 read_comp_unit_head (struct comp_unit_head *cu_header,
2622 gdb_byte *info_ptr, bfd *abfd)
2625 unsigned int bytes_read;
2627 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2628 cu_header->initial_length_size = bytes_read;
2629 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2630 info_ptr += bytes_read;
2631 cu_header->version = read_2_bytes (abfd, info_ptr);
2633 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2635 info_ptr += bytes_read;
2636 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2638 signed_addr = bfd_get_sign_extend_vma (abfd);
2639 if (signed_addr < 0)
2640 internal_error (__FILE__, __LINE__,
2641 _("read_comp_unit_head: dwarf from non elf file"));
2642 cu_header->signed_addr_p = signed_addr;
2648 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2649 gdb_byte *buffer, unsigned int buffer_size,
2652 gdb_byte *beg_of_comp_unit = info_ptr;
2654 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2656 if (header->version != 2 && header->version != 3 && header->version != 4)
2657 error (_("Dwarf Error: wrong version in compilation unit header "
2658 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2659 bfd_get_filename (abfd));
2661 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2662 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2663 "(offset 0x%lx + 6) [in module %s]"),
2664 (long) header->abbrev_offset,
2665 (long) (beg_of_comp_unit - buffer),
2666 bfd_get_filename (abfd));
2668 if (beg_of_comp_unit + header->length + header->initial_length_size
2669 > buffer + buffer_size)
2670 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2671 "(offset 0x%lx + 0) [in module %s]"),
2672 (long) header->length,
2673 (long) (beg_of_comp_unit - buffer),
2674 bfd_get_filename (abfd));
2679 /* Read in the types comp unit header information from .debug_types entry at
2680 types_ptr. The result is a pointer to one past the end of the header. */
2683 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2684 ULONGEST *signature,
2685 gdb_byte *types_ptr, bfd *abfd)
2687 gdb_byte *initial_types_ptr = types_ptr;
2689 dwarf2_read_section (dwarf2_per_objfile->objfile,
2690 &dwarf2_per_objfile->types);
2691 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2693 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2695 *signature = read_8_bytes (abfd, types_ptr);
2697 types_ptr += cu_header->offset_size;
2698 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2703 /* Allocate a new partial symtab for file named NAME and mark this new
2704 partial symtab as being an include of PST. */
2707 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2708 struct objfile *objfile)
2710 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2712 subpst->section_offsets = pst->section_offsets;
2713 subpst->textlow = 0;
2714 subpst->texthigh = 0;
2716 subpst->dependencies = (struct partial_symtab **)
2717 obstack_alloc (&objfile->objfile_obstack,
2718 sizeof (struct partial_symtab *));
2719 subpst->dependencies[0] = pst;
2720 subpst->number_of_dependencies = 1;
2722 subpst->globals_offset = 0;
2723 subpst->n_global_syms = 0;
2724 subpst->statics_offset = 0;
2725 subpst->n_static_syms = 0;
2726 subpst->symtab = NULL;
2727 subpst->read_symtab = pst->read_symtab;
2730 /* No private part is necessary for include psymtabs. This property
2731 can be used to differentiate between such include psymtabs and
2732 the regular ones. */
2733 subpst->read_symtab_private = NULL;
2736 /* Read the Line Number Program data and extract the list of files
2737 included by the source file represented by PST. Build an include
2738 partial symtab for each of these included files. */
2741 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2742 struct die_info *die,
2743 struct partial_symtab *pst)
2745 struct objfile *objfile = cu->objfile;
2746 bfd *abfd = objfile->obfd;
2747 struct line_header *lh = NULL;
2748 struct attribute *attr;
2750 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2753 unsigned int line_offset = DW_UNSND (attr);
2755 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2758 return; /* No linetable, so no includes. */
2760 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2762 free_line_header (lh);
2766 hash_type_signature (const void *item)
2768 const struct signatured_type *type_sig = item;
2770 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2771 return type_sig->signature;
2775 eq_type_signature (const void *item_lhs, const void *item_rhs)
2777 const struct signatured_type *lhs = item_lhs;
2778 const struct signatured_type *rhs = item_rhs;
2780 return lhs->signature == rhs->signature;
2783 /* Allocate a hash table for signatured types. */
2786 allocate_signatured_type_hash_table (struct objfile *objfile)
2788 return htab_create_alloc_ex (41,
2789 hash_type_signature,
2792 &objfile->objfile_obstack,
2793 hashtab_obstack_allocate,
2794 dummy_obstack_deallocate);
2797 /* A helper function to add a signatured type CU to a list. */
2800 add_signatured_type_cu_to_list (void **slot, void *datum)
2802 struct signatured_type *sigt = *slot;
2803 struct dwarf2_per_cu_data ***datap = datum;
2805 **datap = &sigt->per_cu;
2811 /* Create the hash table of all entries in the .debug_types section.
2812 The result is zero if there is an error (e.g. missing .debug_types section),
2813 otherwise non-zero. */
2816 create_debug_types_hash_table (struct objfile *objfile)
2820 struct dwarf2_per_cu_data **iter;
2822 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2823 info_ptr = dwarf2_per_objfile->types.buffer;
2825 if (info_ptr == NULL)
2827 dwarf2_per_objfile->signatured_types = NULL;
2831 types_htab = allocate_signatured_type_hash_table (objfile);
2833 if (dwarf2_die_debug)
2834 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2836 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2838 unsigned int offset;
2839 unsigned int offset_size;
2840 unsigned int type_offset;
2841 unsigned int length, initial_length_size;
2842 unsigned short version;
2844 struct signatured_type *type_sig;
2846 gdb_byte *ptr = info_ptr;
2848 offset = ptr - dwarf2_per_objfile->types.buffer;
2850 /* We need to read the type's signature in order to build the hash
2851 table, but we don't need to read anything else just yet. */
2853 /* Sanity check to ensure entire cu is present. */
2854 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2855 if (ptr + length + initial_length_size
2856 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2858 complaint (&symfile_complaints,
2859 _("debug type entry runs off end of `.debug_types' section, ignored"));
2863 offset_size = initial_length_size == 4 ? 4 : 8;
2864 ptr += initial_length_size;
2865 version = bfd_get_16 (objfile->obfd, ptr);
2867 ptr += offset_size; /* abbrev offset */
2868 ptr += 1; /* address size */
2869 signature = bfd_get_64 (objfile->obfd, ptr);
2871 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2873 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2874 memset (type_sig, 0, sizeof (*type_sig));
2875 type_sig->signature = signature;
2876 type_sig->offset = offset;
2877 type_sig->type_offset = type_offset;
2878 type_sig->per_cu.objfile = objfile;
2879 type_sig->per_cu.from_debug_types = 1;
2881 slot = htab_find_slot (types_htab, type_sig, INSERT);
2882 gdb_assert (slot != NULL);
2885 if (dwarf2_die_debug)
2886 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2887 offset, phex (signature, sizeof (signature)));
2889 info_ptr = info_ptr + initial_length_size + length;
2892 dwarf2_per_objfile->signatured_types = types_htab;
2894 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2895 dwarf2_per_objfile->type_comp_units
2896 = obstack_alloc (&objfile->objfile_obstack,
2897 dwarf2_per_objfile->n_type_comp_units
2898 * sizeof (struct dwarf2_per_cu_data *));
2899 iter = &dwarf2_per_objfile->type_comp_units[0];
2900 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2901 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2902 == dwarf2_per_objfile->n_type_comp_units);
2907 /* Lookup a signature based type.
2908 Returns NULL if SIG is not present in the table. */
2910 static struct signatured_type *
2911 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2913 struct signatured_type find_entry, *entry;
2915 if (dwarf2_per_objfile->signatured_types == NULL)
2917 complaint (&symfile_complaints,
2918 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2922 find_entry.signature = sig;
2923 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2927 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2930 init_cu_die_reader (struct die_reader_specs *reader,
2931 struct dwarf2_cu *cu)
2933 reader->abfd = cu->objfile->obfd;
2935 if (cu->per_cu->from_debug_types)
2937 gdb_assert (dwarf2_per_objfile->types.readin);
2938 reader->buffer = dwarf2_per_objfile->types.buffer;
2942 gdb_assert (dwarf2_per_objfile->info.readin);
2943 reader->buffer = dwarf2_per_objfile->info.buffer;
2947 /* Find the base address of the compilation unit for range lists and
2948 location lists. It will normally be specified by DW_AT_low_pc.
2949 In DWARF-3 draft 4, the base address could be overridden by
2950 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2951 compilation units with discontinuous ranges. */
2954 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2956 struct attribute *attr;
2959 cu->base_address = 0;
2961 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2964 cu->base_address = DW_ADDR (attr);
2969 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2972 cu->base_address = DW_ADDR (attr);
2978 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2979 to combine the common parts.
2980 Process a compilation unit for a psymtab.
2981 BUFFER is a pointer to the beginning of the dwarf section buffer,
2982 either .debug_info or debug_types.
2983 INFO_PTR is a pointer to the start of the CU.
2984 Returns a pointer to the next CU. */
2987 process_psymtab_comp_unit (struct objfile *objfile,
2988 struct dwarf2_per_cu_data *this_cu,
2989 gdb_byte *buffer, gdb_byte *info_ptr,
2990 unsigned int buffer_size)
2992 bfd *abfd = objfile->obfd;
2993 gdb_byte *beg_of_comp_unit = info_ptr;
2994 struct die_info *comp_unit_die;
2995 struct partial_symtab *pst;
2997 struct cleanup *back_to_inner;
2998 struct dwarf2_cu cu;
2999 int has_children, has_pc_info;
3000 struct attribute *attr;
3001 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3002 struct die_reader_specs reader_specs;
3004 memset (&cu, 0, sizeof (cu));
3005 cu.objfile = objfile;
3006 obstack_init (&cu.comp_unit_obstack);
3008 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3010 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3011 buffer, buffer_size,
3014 /* Complete the cu_header. */
3015 cu.header.offset = beg_of_comp_unit - buffer;
3016 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3018 cu.list_in_scope = &file_symbols;
3020 /* If this compilation unit was already read in, free the
3021 cached copy in order to read it in again. This is
3022 necessary because we skipped some symbols when we first
3023 read in the compilation unit (see load_partial_dies).
3024 This problem could be avoided, but the benefit is
3026 if (this_cu->cu != NULL)
3027 free_one_cached_comp_unit (this_cu->cu);
3029 /* Note that this is a pointer to our stack frame, being
3030 added to a global data structure. It will be cleaned up
3031 in free_stack_comp_unit when we finish with this
3032 compilation unit. */
3034 cu.per_cu = this_cu;
3036 /* Read the abbrevs for this compilation unit into a table. */
3037 dwarf2_read_abbrevs (abfd, &cu);
3038 make_cleanup (dwarf2_free_abbrev_table, &cu);
3040 /* Read the compilation unit die. */
3041 if (this_cu->from_debug_types)
3042 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3043 init_cu_die_reader (&reader_specs, &cu);
3044 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3047 if (this_cu->from_debug_types)
3049 /* offset,length haven't been set yet for type units. */
3050 this_cu->offset = cu.header.offset;
3051 this_cu->length = cu.header.length + cu.header.initial_length_size;
3053 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3055 info_ptr = (beg_of_comp_unit + cu.header.length
3056 + cu.header.initial_length_size);
3057 do_cleanups (back_to_inner);
3061 /* Set the language we're debugging. */
3062 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3064 set_cu_language (DW_UNSND (attr), &cu);
3066 set_cu_language (language_minimal, &cu);
3068 /* Allocate a new partial symbol table structure. */
3069 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3070 pst = start_psymtab_common (objfile, objfile->section_offsets,
3071 (attr != NULL) ? DW_STRING (attr) : "",
3072 /* TEXTLOW and TEXTHIGH are set below. */
3074 objfile->global_psymbols.next,
3075 objfile->static_psymbols.next);
3077 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3079 pst->dirname = DW_STRING (attr);
3081 pst->read_symtab_private = this_cu;
3083 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3085 /* Store the function that reads in the rest of the symbol table */
3086 pst->read_symtab = dwarf2_psymtab_to_symtab;
3088 this_cu->v.psymtab = pst;
3090 dwarf2_find_base_address (comp_unit_die, &cu);
3092 /* Possibly set the default values of LOWPC and HIGHPC from
3094 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3095 &best_highpc, &cu, pst);
3096 if (has_pc_info == 1 && best_lowpc < best_highpc)
3097 /* Store the contiguous range if it is not empty; it can be empty for
3098 CUs with no code. */
3099 addrmap_set_empty (objfile->psymtabs_addrmap,
3100 best_lowpc + baseaddr,
3101 best_highpc + baseaddr - 1, pst);
3103 /* Check if comp unit has_children.
3104 If so, read the rest of the partial symbols from this comp unit.
3105 If not, there's no more debug_info for this comp unit. */
3108 struct partial_die_info *first_die;
3109 CORE_ADDR lowpc, highpc;
3111 lowpc = ((CORE_ADDR) -1);
3112 highpc = ((CORE_ADDR) 0);
3114 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3116 scan_partial_symbols (first_die, &lowpc, &highpc,
3117 ! has_pc_info, &cu);
3119 /* If we didn't find a lowpc, set it to highpc to avoid
3120 complaints from `maint check'. */
3121 if (lowpc == ((CORE_ADDR) -1))
3124 /* If the compilation unit didn't have an explicit address range,
3125 then use the information extracted from its child dies. */
3129 best_highpc = highpc;
3132 pst->textlow = best_lowpc + baseaddr;
3133 pst->texthigh = best_highpc + baseaddr;
3135 pst->n_global_syms = objfile->global_psymbols.next -
3136 (objfile->global_psymbols.list + pst->globals_offset);
3137 pst->n_static_syms = objfile->static_psymbols.next -
3138 (objfile->static_psymbols.list + pst->statics_offset);
3139 sort_pst_symbols (pst);
3141 info_ptr = (beg_of_comp_unit + cu.header.length
3142 + cu.header.initial_length_size);
3144 if (this_cu->from_debug_types)
3146 /* It's not clear we want to do anything with stmt lists here.
3147 Waiting to see what gcc ultimately does. */
3151 /* Get the list of files included in the current compilation unit,
3152 and build a psymtab for each of them. */
3153 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3156 do_cleanups (back_to_inner);
3161 /* Traversal function for htab_traverse_noresize.
3162 Process one .debug_types comp-unit. */
3165 process_type_comp_unit (void **slot, void *info)
3167 struct signatured_type *entry = (struct signatured_type *) *slot;
3168 struct objfile *objfile = (struct objfile *) info;
3169 struct dwarf2_per_cu_data *this_cu;
3171 this_cu = &entry->per_cu;
3173 gdb_assert (dwarf2_per_objfile->types.readin);
3174 process_psymtab_comp_unit (objfile, this_cu,
3175 dwarf2_per_objfile->types.buffer,
3176 dwarf2_per_objfile->types.buffer + entry->offset,
3177 dwarf2_per_objfile->types.size);
3182 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3183 Build partial symbol tables for the .debug_types comp-units. */
3186 build_type_psymtabs (struct objfile *objfile)
3188 if (! create_debug_types_hash_table (objfile))
3191 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3192 process_type_comp_unit, objfile);
3195 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3198 psymtabs_addrmap_cleanup (void *o)
3200 struct objfile *objfile = o;
3202 objfile->psymtabs_addrmap = NULL;
3205 /* Build the partial symbol table by doing a quick pass through the
3206 .debug_info and .debug_abbrev sections. */
3209 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3212 struct cleanup *back_to, *addrmap_cleanup;
3213 struct obstack temp_obstack;
3215 dwarf2_per_objfile->reading_partial_symbols = 1;
3217 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3218 info_ptr = dwarf2_per_objfile->info.buffer;
3220 /* Any cached compilation units will be linked by the per-objfile
3221 read_in_chain. Make sure to free them when we're done. */
3222 back_to = make_cleanup (free_cached_comp_units, NULL);
3224 build_type_psymtabs (objfile);
3226 create_all_comp_units (objfile);
3228 /* Create a temporary address map on a temporary obstack. We later
3229 copy this to the final obstack. */
3230 obstack_init (&temp_obstack);
3231 make_cleanup_obstack_free (&temp_obstack);
3232 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3233 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3235 /* Since the objects we're extracting from .debug_info vary in
3236 length, only the individual functions to extract them (like
3237 read_comp_unit_head and load_partial_die) can really know whether
3238 the buffer is large enough to hold another complete object.
3240 At the moment, they don't actually check that. If .debug_info
3241 holds just one extra byte after the last compilation unit's dies,
3242 then read_comp_unit_head will happily read off the end of the
3243 buffer. read_partial_die is similarly casual. Those functions
3246 For this loop condition, simply checking whether there's any data
3247 left at all should be sufficient. */
3249 while (info_ptr < (dwarf2_per_objfile->info.buffer
3250 + dwarf2_per_objfile->info.size))
3252 struct dwarf2_per_cu_data *this_cu;
3254 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3257 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3258 dwarf2_per_objfile->info.buffer,
3260 dwarf2_per_objfile->info.size);
3263 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3264 &objfile->objfile_obstack);
3265 discard_cleanups (addrmap_cleanup);
3267 do_cleanups (back_to);
3270 /* Load the partial DIEs for a secondary CU into memory. */
3273 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3274 struct objfile *objfile)
3276 bfd *abfd = objfile->obfd;
3277 gdb_byte *info_ptr, *beg_of_comp_unit;
3278 struct die_info *comp_unit_die;
3279 struct dwarf2_cu *cu;
3280 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3281 struct attribute *attr;
3283 struct die_reader_specs reader_specs;
3286 gdb_assert (! this_cu->from_debug_types);
3288 gdb_assert (dwarf2_per_objfile->info.readin);
3289 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3290 beg_of_comp_unit = info_ptr;
3292 if (this_cu->cu == NULL)
3294 cu = alloc_one_comp_unit (objfile);
3298 /* If an error occurs while loading, release our storage. */
3299 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3301 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3302 dwarf2_per_objfile->info.buffer,
3303 dwarf2_per_objfile->info.size,
3306 /* Complete the cu_header. */
3307 cu->header.offset = this_cu->offset;
3308 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3310 /* Link this compilation unit into the compilation unit tree. */
3312 cu->per_cu = this_cu;
3313 cu->type_hash = this_cu->type_hash;
3315 /* Link this CU into read_in_chain. */
3316 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3317 dwarf2_per_objfile->read_in_chain = this_cu;
3322 info_ptr += cu->header.first_die_offset;
3325 /* Read the abbrevs for this compilation unit into a table. */
3326 gdb_assert (cu->dwarf2_abbrevs == NULL);
3327 dwarf2_read_abbrevs (abfd, cu);
3328 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3330 /* Read the compilation unit die. */
3331 init_cu_die_reader (&reader_specs, cu);
3332 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3335 /* Set the language we're debugging. */
3336 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3338 set_cu_language (DW_UNSND (attr), cu);
3340 set_cu_language (language_minimal, cu);
3342 /* Check if comp unit has_children.
3343 If so, read the rest of the partial symbols from this comp unit.
3344 If not, there's no more debug_info for this comp unit. */
3346 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3348 do_cleanups (free_abbrevs_cleanup);
3352 /* We've successfully allocated this compilation unit. Let our
3353 caller clean it up when finished with it. */
3354 discard_cleanups (free_cu_cleanup);
3358 /* Create a list of all compilation units in OBJFILE. We do this only
3359 if an inter-comp-unit reference is found; presumably if there is one,
3360 there will be many, and one will occur early in the .debug_info section.
3361 So there's no point in building this list incrementally. */
3364 create_all_comp_units (struct objfile *objfile)
3368 struct dwarf2_per_cu_data **all_comp_units;
3371 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3372 info_ptr = dwarf2_per_objfile->info.buffer;
3376 all_comp_units = xmalloc (n_allocated
3377 * sizeof (struct dwarf2_per_cu_data *));
3379 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3381 unsigned int length, initial_length_size;
3382 struct dwarf2_per_cu_data *this_cu;
3383 unsigned int offset;
3385 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3387 /* Read just enough information to find out where the next
3388 compilation unit is. */
3389 length = read_initial_length (objfile->obfd, info_ptr,
3390 &initial_length_size);
3392 /* Save the compilation unit for later lookup. */
3393 this_cu = obstack_alloc (&objfile->objfile_obstack,
3394 sizeof (struct dwarf2_per_cu_data));
3395 memset (this_cu, 0, sizeof (*this_cu));
3396 this_cu->offset = offset;
3397 this_cu->length = length + initial_length_size;
3398 this_cu->objfile = objfile;
3400 if (n_comp_units == n_allocated)
3403 all_comp_units = xrealloc (all_comp_units,
3405 * sizeof (struct dwarf2_per_cu_data *));
3407 all_comp_units[n_comp_units++] = this_cu;
3409 info_ptr = info_ptr + this_cu->length;
3412 dwarf2_per_objfile->all_comp_units
3413 = obstack_alloc (&objfile->objfile_obstack,
3414 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3415 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3416 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3417 xfree (all_comp_units);
3418 dwarf2_per_objfile->n_comp_units = n_comp_units;
3421 /* Process all loaded DIEs for compilation unit CU, starting at
3422 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3423 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3424 DW_AT_ranges). If NEED_PC is set, then this function will set
3425 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3426 and record the covered ranges in the addrmap. */
3429 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3430 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3432 struct partial_die_info *pdi;
3434 /* Now, march along the PDI's, descending into ones which have
3435 interesting children but skipping the children of the other ones,
3436 until we reach the end of the compilation unit. */
3442 fixup_partial_die (pdi, cu);
3444 /* Anonymous namespaces or modules have no name but have interesting
3445 children, so we need to look at them. Ditto for anonymous
3448 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3449 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3453 case DW_TAG_subprogram:
3454 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3456 case DW_TAG_variable:
3457 case DW_TAG_typedef:
3458 case DW_TAG_union_type:
3459 if (!pdi->is_declaration)
3461 add_partial_symbol (pdi, cu);
3464 case DW_TAG_class_type:
3465 case DW_TAG_interface_type:
3466 case DW_TAG_structure_type:
3467 if (!pdi->is_declaration)
3469 add_partial_symbol (pdi, cu);
3472 case DW_TAG_enumeration_type:
3473 if (!pdi->is_declaration)
3474 add_partial_enumeration (pdi, cu);
3476 case DW_TAG_base_type:
3477 case DW_TAG_subrange_type:
3478 /* File scope base type definitions are added to the partial
3480 add_partial_symbol (pdi, cu);
3482 case DW_TAG_namespace:
3483 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3486 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3493 /* If the die has a sibling, skip to the sibling. */
3495 pdi = pdi->die_sibling;
3499 /* Functions used to compute the fully scoped name of a partial DIE.
3501 Normally, this is simple. For C++, the parent DIE's fully scoped
3502 name is concatenated with "::" and the partial DIE's name. For
3503 Java, the same thing occurs except that "." is used instead of "::".
3504 Enumerators are an exception; they use the scope of their parent
3505 enumeration type, i.e. the name of the enumeration type is not
3506 prepended to the enumerator.
3508 There are two complexities. One is DW_AT_specification; in this
3509 case "parent" means the parent of the target of the specification,
3510 instead of the direct parent of the DIE. The other is compilers
3511 which do not emit DW_TAG_namespace; in this case we try to guess
3512 the fully qualified name of structure types from their members'
3513 linkage names. This must be done using the DIE's children rather
3514 than the children of any DW_AT_specification target. We only need
3515 to do this for structures at the top level, i.e. if the target of
3516 any DW_AT_specification (if any; otherwise the DIE itself) does not
3519 /* Compute the scope prefix associated with PDI's parent, in
3520 compilation unit CU. The result will be allocated on CU's
3521 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3522 field. NULL is returned if no prefix is necessary. */
3524 partial_die_parent_scope (struct partial_die_info *pdi,
3525 struct dwarf2_cu *cu)
3527 char *grandparent_scope;
3528 struct partial_die_info *parent, *real_pdi;
3530 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3531 then this means the parent of the specification DIE. */
3534 while (real_pdi->has_specification)
3535 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3537 parent = real_pdi->die_parent;
3541 if (parent->scope_set)
3542 return parent->scope;
3544 fixup_partial_die (parent, cu);
3546 grandparent_scope = partial_die_parent_scope (parent, cu);
3548 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3549 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3550 Work around this problem here. */
3551 if (cu->language == language_cplus
3552 && parent->tag == DW_TAG_namespace
3553 && strcmp (parent->name, "::") == 0
3554 && grandparent_scope == NULL)
3556 parent->scope = NULL;
3557 parent->scope_set = 1;
3561 if (parent->tag == DW_TAG_namespace
3562 || parent->tag == DW_TAG_module
3563 || parent->tag == DW_TAG_structure_type
3564 || parent->tag == DW_TAG_class_type
3565 || parent->tag == DW_TAG_interface_type
3566 || parent->tag == DW_TAG_union_type
3567 || parent->tag == DW_TAG_enumeration_type)
3569 if (grandparent_scope == NULL)
3570 parent->scope = parent->name;
3572 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3573 parent->name, 0, cu);
3575 else if (parent->tag == DW_TAG_enumerator)
3576 /* Enumerators should not get the name of the enumeration as a prefix. */
3577 parent->scope = grandparent_scope;
3580 /* FIXME drow/2004-04-01: What should we be doing with
3581 function-local names? For partial symbols, we should probably be
3583 complaint (&symfile_complaints,
3584 _("unhandled containing DIE tag %d for DIE at %d"),
3585 parent->tag, pdi->offset);
3586 parent->scope = grandparent_scope;
3589 parent->scope_set = 1;
3590 return parent->scope;
3593 /* Return the fully scoped name associated with PDI, from compilation unit
3594 CU. The result will be allocated with malloc. */
3596 partial_die_full_name (struct partial_die_info *pdi,
3597 struct dwarf2_cu *cu)
3601 /* If this is a template instantiation, we can not work out the
3602 template arguments from partial DIEs. So, unfortunately, we have
3603 to go through the full DIEs. At least any work we do building
3604 types here will be reused if full symbols are loaded later. */
3605 if (pdi->has_template_arguments)
3607 fixup_partial_die (pdi, cu);
3609 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3611 struct die_info *die;
3612 struct attribute attr;
3613 struct dwarf2_cu *ref_cu = cu;
3616 attr.form = DW_FORM_ref_addr;
3617 attr.u.addr = pdi->offset;
3618 die = follow_die_ref (NULL, &attr, &ref_cu);
3620 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3624 parent_scope = partial_die_parent_scope (pdi, cu);
3625 if (parent_scope == NULL)
3628 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3632 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3634 struct objfile *objfile = cu->objfile;
3636 char *actual_name = NULL;
3637 const struct partial_symbol *psym = NULL;
3639 int built_actual_name = 0;
3641 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3643 actual_name = partial_die_full_name (pdi, cu);
3645 built_actual_name = 1;
3647 if (actual_name == NULL)
3648 actual_name = pdi->name;
3652 case DW_TAG_subprogram:
3653 if (pdi->is_external || cu->language == language_ada)
3655 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3656 of the global scope. But in Ada, we want to be able to access
3657 nested procedures globally. So all Ada subprograms are stored
3658 in the global scope. */
3659 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3660 mst_text, objfile); */
3661 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3663 VAR_DOMAIN, LOC_BLOCK,
3664 &objfile->global_psymbols,
3665 0, pdi->lowpc + baseaddr,
3666 cu->language, objfile);
3670 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3671 mst_file_text, objfile); */
3672 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3674 VAR_DOMAIN, LOC_BLOCK,
3675 &objfile->static_psymbols,
3676 0, pdi->lowpc + baseaddr,
3677 cu->language, objfile);
3680 case DW_TAG_variable:
3682 addr = decode_locdesc (pdi->locdesc, cu);
3686 && !dwarf2_per_objfile->has_section_at_zero)
3688 /* A global or static variable may also have been stripped
3689 out by the linker if unused, in which case its address
3690 will be nullified; do not add such variables into partial
3691 symbol table then. */
3693 else if (pdi->is_external)
3696 Don't enter into the minimal symbol tables as there is
3697 a minimal symbol table entry from the ELF symbols already.
3698 Enter into partial symbol table if it has a location
3699 descriptor or a type.
3700 If the location descriptor is missing, new_symbol will create
3701 a LOC_UNRESOLVED symbol, the address of the variable will then
3702 be determined from the minimal symbol table whenever the variable
3704 The address for the partial symbol table entry is not
3705 used by GDB, but it comes in handy for debugging partial symbol
3708 if (pdi->locdesc || pdi->has_type)
3709 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3711 VAR_DOMAIN, LOC_STATIC,
3712 &objfile->global_psymbols,
3714 cu->language, objfile);
3718 /* Static Variable. Skip symbols without location descriptors. */
3719 if (pdi->locdesc == NULL)
3721 if (built_actual_name)
3722 xfree (actual_name);
3725 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3726 mst_file_data, objfile); */
3727 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3729 VAR_DOMAIN, LOC_STATIC,
3730 &objfile->static_psymbols,
3732 cu->language, objfile);
3735 case DW_TAG_typedef:
3736 case DW_TAG_base_type:
3737 case DW_TAG_subrange_type:
3738 add_psymbol_to_list (actual_name, strlen (actual_name),
3740 VAR_DOMAIN, LOC_TYPEDEF,
3741 &objfile->static_psymbols,
3742 0, (CORE_ADDR) 0, cu->language, objfile);
3744 case DW_TAG_namespace:
3745 add_psymbol_to_list (actual_name, strlen (actual_name),
3747 VAR_DOMAIN, LOC_TYPEDEF,
3748 &objfile->global_psymbols,
3749 0, (CORE_ADDR) 0, cu->language, objfile);
3751 case DW_TAG_class_type:
3752 case DW_TAG_interface_type:
3753 case DW_TAG_structure_type:
3754 case DW_TAG_union_type:
3755 case DW_TAG_enumeration_type:
3756 /* Skip external references. The DWARF standard says in the section
3757 about "Structure, Union, and Class Type Entries": "An incomplete
3758 structure, union or class type is represented by a structure,
3759 union or class entry that does not have a byte size attribute
3760 and that has a DW_AT_declaration attribute." */
3761 if (!pdi->has_byte_size && pdi->is_declaration)
3763 if (built_actual_name)
3764 xfree (actual_name);
3768 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3769 static vs. global. */
3770 add_psymbol_to_list (actual_name, strlen (actual_name),
3772 STRUCT_DOMAIN, LOC_TYPEDEF,
3773 (cu->language == language_cplus
3774 || cu->language == language_java)
3775 ? &objfile->global_psymbols
3776 : &objfile->static_psymbols,
3777 0, (CORE_ADDR) 0, cu->language, objfile);
3780 case DW_TAG_enumerator:
3781 add_psymbol_to_list (actual_name, strlen (actual_name),
3783 VAR_DOMAIN, LOC_CONST,
3784 (cu->language == language_cplus
3785 || cu->language == language_java)
3786 ? &objfile->global_psymbols
3787 : &objfile->static_psymbols,
3788 0, (CORE_ADDR) 0, cu->language, objfile);
3794 if (built_actual_name)
3795 xfree (actual_name);
3798 /* Read a partial die corresponding to a namespace; also, add a symbol
3799 corresponding to that namespace to the symbol table. NAMESPACE is
3800 the name of the enclosing namespace. */
3803 add_partial_namespace (struct partial_die_info *pdi,
3804 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3805 int need_pc, struct dwarf2_cu *cu)
3807 /* Add a symbol for the namespace. */
3809 add_partial_symbol (pdi, cu);
3811 /* Now scan partial symbols in that namespace. */
3813 if (pdi->has_children)
3814 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3817 /* Read a partial die corresponding to a Fortran module. */
3820 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3821 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3823 /* Now scan partial symbols in that module. */
3825 if (pdi->has_children)
3826 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3829 /* Read a partial die corresponding to a subprogram and create a partial
3830 symbol for that subprogram. When the CU language allows it, this
3831 routine also defines a partial symbol for each nested subprogram
3832 that this subprogram contains.
3834 DIE my also be a lexical block, in which case we simply search
3835 recursively for suprograms defined inside that lexical block.
3836 Again, this is only performed when the CU language allows this
3837 type of definitions. */
3840 add_partial_subprogram (struct partial_die_info *pdi,
3841 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3842 int need_pc, struct dwarf2_cu *cu)
3844 if (pdi->tag == DW_TAG_subprogram)
3846 if (pdi->has_pc_info)
3848 if (pdi->lowpc < *lowpc)
3849 *lowpc = pdi->lowpc;
3850 if (pdi->highpc > *highpc)
3851 *highpc = pdi->highpc;
3855 struct objfile *objfile = cu->objfile;
3857 baseaddr = ANOFFSET (objfile->section_offsets,
3858 SECT_OFF_TEXT (objfile));
3859 addrmap_set_empty (objfile->psymtabs_addrmap,
3860 pdi->lowpc + baseaddr,
3861 pdi->highpc - 1 + baseaddr,
3862 cu->per_cu->v.psymtab);
3864 if (!pdi->is_declaration)
3865 /* Ignore subprogram DIEs that do not have a name, they are
3866 illegal. Do not emit a complaint at this point, we will
3867 do so when we convert this psymtab into a symtab. */
3869 add_partial_symbol (pdi, cu);
3873 if (! pdi->has_children)
3876 if (cu->language == language_ada)
3878 pdi = pdi->die_child;
3881 fixup_partial_die (pdi, cu);
3882 if (pdi->tag == DW_TAG_subprogram
3883 || pdi->tag == DW_TAG_lexical_block)
3884 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3885 pdi = pdi->die_sibling;
3890 /* See if we can figure out if the class lives in a namespace. We do
3891 this by looking for a member function; its demangled name will
3892 contain namespace info, if there is any. */
3895 guess_structure_name (struct partial_die_info *struct_pdi,
3896 struct dwarf2_cu *cu)
3898 if ((cu->language == language_cplus
3899 || cu->language == language_java)
3900 && cu->has_namespace_info == 0
3901 && struct_pdi->has_children)
3903 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3904 what template types look like, because the demangler
3905 frequently doesn't give the same name as the debug info. We
3906 could fix this by only using the demangled name to get the
3907 prefix (but see comment in read_structure_type). */
3909 struct partial_die_info *real_pdi;
3911 /* If this DIE (this DIE's specification, if any) has a parent, then
3912 we should not do this. We'll prepend the parent's fully qualified
3913 name when we create the partial symbol. */
3915 real_pdi = struct_pdi;
3916 while (real_pdi->has_specification)
3917 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3919 if (real_pdi->die_parent != NULL)
3924 /* Read a partial die corresponding to an enumeration type. */
3927 add_partial_enumeration (struct partial_die_info *enum_pdi,
3928 struct dwarf2_cu *cu)
3930 struct partial_die_info *pdi;
3932 if (enum_pdi->name != NULL)
3933 add_partial_symbol (enum_pdi, cu);
3935 pdi = enum_pdi->die_child;
3938 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3939 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3941 add_partial_symbol (pdi, cu);
3942 pdi = pdi->die_sibling;
3946 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3947 Return the corresponding abbrev, or NULL if the number is zero (indicating
3948 an empty DIE). In either case *BYTES_READ will be set to the length of
3949 the initial number. */
3951 static struct abbrev_info *
3952 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3953 struct dwarf2_cu *cu)
3955 bfd *abfd = cu->objfile->obfd;
3956 unsigned int abbrev_number;
3957 struct abbrev_info *abbrev;
3959 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3961 if (abbrev_number == 0)
3964 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3967 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3968 bfd_get_filename (abfd));
3974 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3975 Returns a pointer to the end of a series of DIEs, terminated by an empty
3976 DIE. Any children of the skipped DIEs will also be skipped. */
3979 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3981 struct abbrev_info *abbrev;
3982 unsigned int bytes_read;
3986 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3988 return info_ptr + bytes_read;
3990 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3994 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3995 INFO_PTR should point just after the initial uleb128 of a DIE, and the
3996 abbrev corresponding to that skipped uleb128 should be passed in
3997 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4001 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4002 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4004 unsigned int bytes_read;
4005 struct attribute attr;
4006 bfd *abfd = cu->objfile->obfd;
4007 unsigned int form, i;
4009 for (i = 0; i < abbrev->num_attrs; i++)
4011 /* The only abbrev we care about is DW_AT_sibling. */
4012 if (abbrev->attrs[i].name == DW_AT_sibling)
4014 read_attribute (&attr, &abbrev->attrs[i],
4015 abfd, info_ptr, cu);
4016 if (attr.form == DW_FORM_ref_addr)
4017 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4019 return buffer + dwarf2_get_ref_die_offset (&attr);
4022 /* If it isn't DW_AT_sibling, skip this attribute. */
4023 form = abbrev->attrs[i].form;
4027 case DW_FORM_ref_addr:
4028 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4029 and later it is offset sized. */
4030 if (cu->header.version == 2)
4031 info_ptr += cu->header.addr_size;
4033 info_ptr += cu->header.offset_size;
4036 info_ptr += cu->header.addr_size;
4043 case DW_FORM_flag_present:
4058 case DW_FORM_string:
4059 read_direct_string (abfd, info_ptr, &bytes_read);
4060 info_ptr += bytes_read;
4062 case DW_FORM_sec_offset:
4064 info_ptr += cu->header.offset_size;
4066 case DW_FORM_exprloc:
4068 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4069 info_ptr += bytes_read;
4071 case DW_FORM_block1:
4072 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4074 case DW_FORM_block2:
4075 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4077 case DW_FORM_block4:
4078 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4082 case DW_FORM_ref_udata:
4083 info_ptr = skip_leb128 (abfd, info_ptr);
4085 case DW_FORM_indirect:
4086 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4087 info_ptr += bytes_read;
4088 /* We need to continue parsing from here, so just go back to
4090 goto skip_attribute;
4093 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4094 dwarf_form_name (form),
4095 bfd_get_filename (abfd));
4099 if (abbrev->has_children)
4100 return skip_children (buffer, info_ptr, cu);
4105 /* Locate ORIG_PDI's sibling.
4106 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4110 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4111 gdb_byte *buffer, gdb_byte *info_ptr,
4112 bfd *abfd, struct dwarf2_cu *cu)
4114 /* Do we know the sibling already? */
4116 if (orig_pdi->sibling)
4117 return orig_pdi->sibling;
4119 /* Are there any children to deal with? */
4121 if (!orig_pdi->has_children)
4124 /* Skip the children the long way. */
4126 return skip_children (buffer, info_ptr, cu);
4129 /* Expand this partial symbol table into a full symbol table. */
4132 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4138 warning (_("bug: psymtab for %s is already read in."), pst->filename);
4144 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4145 gdb_flush (gdb_stdout);
4148 /* Restore our global data. */
4149 dwarf2_per_objfile = objfile_data (pst->objfile,
4150 dwarf2_objfile_data_key);
4152 /* If this psymtab is constructed from a debug-only objfile, the
4153 has_section_at_zero flag will not necessarily be correct. We
4154 can get the correct value for this flag by looking at the data
4155 associated with the (presumably stripped) associated objfile. */
4156 if (pst->objfile->separate_debug_objfile_backlink)
4158 struct dwarf2_per_objfile *dpo_backlink
4159 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4160 dwarf2_objfile_data_key);
4162 dwarf2_per_objfile->has_section_at_zero
4163 = dpo_backlink->has_section_at_zero;
4166 dwarf2_per_objfile->reading_partial_symbols = 0;
4168 psymtab_to_symtab_1 (pst);
4170 /* Finish up the debug error message. */
4172 printf_filtered (_("done.\n"));
4177 /* Add PER_CU to the queue. */
4180 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4182 struct dwarf2_queue_item *item;
4185 item = xmalloc (sizeof (*item));
4186 item->per_cu = per_cu;
4189 if (dwarf2_queue == NULL)
4190 dwarf2_queue = item;
4192 dwarf2_queue_tail->next = item;
4194 dwarf2_queue_tail = item;
4197 /* Process the queue. */
4200 process_queue (struct objfile *objfile)
4202 struct dwarf2_queue_item *item, *next_item;
4204 /* The queue starts out with one item, but following a DIE reference
4205 may load a new CU, adding it to the end of the queue. */
4206 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4208 if (dwarf2_per_objfile->using_index
4209 ? !item->per_cu->v.quick->symtab
4210 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4211 process_full_comp_unit (item->per_cu);
4213 item->per_cu->queued = 0;
4214 next_item = item->next;
4218 dwarf2_queue_tail = NULL;
4221 /* Free all allocated queue entries. This function only releases anything if
4222 an error was thrown; if the queue was processed then it would have been
4223 freed as we went along. */
4226 dwarf2_release_queue (void *dummy)
4228 struct dwarf2_queue_item *item, *last;
4230 item = dwarf2_queue;
4233 /* Anything still marked queued is likely to be in an
4234 inconsistent state, so discard it. */
4235 if (item->per_cu->queued)
4237 if (item->per_cu->cu != NULL)
4238 free_one_cached_comp_unit (item->per_cu->cu);
4239 item->per_cu->queued = 0;
4247 dwarf2_queue = dwarf2_queue_tail = NULL;
4250 /* Read in full symbols for PST, and anything it depends on. */
4253 psymtab_to_symtab_1 (struct partial_symtab *pst)
4255 struct dwarf2_per_cu_data *per_cu;
4256 struct cleanup *back_to;
4259 for (i = 0; i < pst->number_of_dependencies; i++)
4260 if (!pst->dependencies[i]->readin)
4262 /* Inform about additional files that need to be read in. */
4265 /* FIXME: i18n: Need to make this a single string. */
4266 fputs_filtered (" ", gdb_stdout);
4268 fputs_filtered ("and ", gdb_stdout);
4270 printf_filtered ("%s...", pst->dependencies[i]->filename);
4271 wrap_here (""); /* Flush output */
4272 gdb_flush (gdb_stdout);
4274 psymtab_to_symtab_1 (pst->dependencies[i]);
4277 per_cu = pst->read_symtab_private;
4281 /* It's an include file, no symbols to read for it.
4282 Everything is in the parent symtab. */
4287 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4290 /* Load the DIEs associated with PER_CU into memory. */
4293 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4295 bfd *abfd = objfile->obfd;
4296 struct dwarf2_cu *cu;
4297 unsigned int offset;
4298 gdb_byte *info_ptr, *beg_of_comp_unit;
4299 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4300 struct attribute *attr;
4303 gdb_assert (! per_cu->from_debug_types);
4305 /* Set local variables from the partial symbol table info. */
4306 offset = per_cu->offset;
4308 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4309 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4310 beg_of_comp_unit = info_ptr;
4312 if (per_cu->cu == NULL)
4314 cu = alloc_one_comp_unit (objfile);
4318 /* If an error occurs while loading, release our storage. */
4319 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4321 /* Read in the comp_unit header. */
4322 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4324 /* Complete the cu_header. */
4325 cu->header.offset = offset;
4326 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4328 /* Read the abbrevs for this compilation unit. */
4329 dwarf2_read_abbrevs (abfd, cu);
4330 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4332 /* Link this compilation unit into the compilation unit tree. */
4334 cu->per_cu = per_cu;
4335 cu->type_hash = per_cu->type_hash;
4337 /* Link this CU into read_in_chain. */
4338 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4339 dwarf2_per_objfile->read_in_chain = per_cu;
4344 info_ptr += cu->header.first_die_offset;
4347 cu->dies = read_comp_unit (info_ptr, cu);
4349 /* We try not to read any attributes in this function, because not
4350 all objfiles needed for references have been loaded yet, and symbol
4351 table processing isn't initialized. But we have to set the CU language,
4352 or we won't be able to build types correctly. */
4353 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4355 set_cu_language (DW_UNSND (attr), cu);
4357 set_cu_language (language_minimal, cu);
4359 /* Similarly, if we do not read the producer, we can not apply
4360 producer-specific interpretation. */
4361 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4363 cu->producer = DW_STRING (attr);
4367 do_cleanups (free_abbrevs_cleanup);
4369 /* We've successfully allocated this compilation unit. Let our
4370 caller clean it up when finished with it. */
4371 discard_cleanups (free_cu_cleanup);
4375 /* Add a DIE to the delayed physname list. */
4378 add_to_method_list (struct type *type, int fnfield_index, int index,
4379 const char *name, struct die_info *die,
4380 struct dwarf2_cu *cu)
4382 struct delayed_method_info mi;
4384 mi.fnfield_index = fnfield_index;
4388 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4391 /* A cleanup for freeing the delayed method list. */
4394 free_delayed_list (void *ptr)
4396 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4397 if (cu->method_list != NULL)
4399 VEC_free (delayed_method_info, cu->method_list);
4400 cu->method_list = NULL;
4404 /* Compute the physnames of any methods on the CU's method list.
4406 The computation of method physnames is delayed in order to avoid the
4407 (bad) condition that one of the method's formal parameters is of an as yet
4411 compute_delayed_physnames (struct dwarf2_cu *cu)
4414 struct delayed_method_info *mi;
4415 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4418 struct fn_fieldlist *fn_flp
4419 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4420 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4421 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4425 /* Generate full symbol information for PST and CU, whose DIEs have
4426 already been loaded into memory. */
4429 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4431 struct dwarf2_cu *cu = per_cu->cu;
4432 struct objfile *objfile = per_cu->objfile;
4433 CORE_ADDR lowpc, highpc;
4434 struct symtab *symtab;
4435 struct cleanup *back_to, *delayed_list_cleanup;
4438 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4441 back_to = make_cleanup (really_free_pendings, NULL);
4442 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4444 cu->list_in_scope = &file_symbols;
4446 dwarf2_find_base_address (cu->dies, cu);
4448 /* Do line number decoding in read_file_scope () */
4449 process_die (cu->dies, cu);
4451 /* Now that we have processed all the DIEs in the CU, all the types
4452 should be complete, and it should now be safe to compute all of the
4454 compute_delayed_physnames (cu);
4455 do_cleanups (delayed_list_cleanup);
4457 /* Some compilers don't define a DW_AT_high_pc attribute for the
4458 compilation unit. If the DW_AT_high_pc is missing, synthesize
4459 it, by scanning the DIE's below the compilation unit. */
4460 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4462 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4464 /* Set symtab language to language from DW_AT_language.
4465 If the compilation is from a C file generated by language preprocessors,
4466 do not set the language if it was already deduced by start_subfile. */
4468 && !(cu->language == language_c && symtab->language != language_c))
4470 symtab->language = cu->language;
4473 if (dwarf2_per_objfile->using_index)
4474 per_cu->v.quick->symtab = symtab;
4477 struct partial_symtab *pst = per_cu->v.psymtab;
4478 pst->symtab = symtab;
4482 do_cleanups (back_to);
4485 /* Process a die and its children. */
4488 process_die (struct die_info *die, struct dwarf2_cu *cu)
4492 case DW_TAG_padding:
4494 case DW_TAG_compile_unit:
4495 read_file_scope (die, cu);
4497 case DW_TAG_type_unit:
4498 read_type_unit_scope (die, cu);
4500 case DW_TAG_subprogram:
4501 case DW_TAG_inlined_subroutine:
4502 read_func_scope (die, cu);
4504 case DW_TAG_lexical_block:
4505 case DW_TAG_try_block:
4506 case DW_TAG_catch_block:
4507 read_lexical_block_scope (die, cu);
4509 case DW_TAG_class_type:
4510 case DW_TAG_interface_type:
4511 case DW_TAG_structure_type:
4512 case DW_TAG_union_type:
4513 process_structure_scope (die, cu);
4515 case DW_TAG_enumeration_type:
4516 process_enumeration_scope (die, cu);
4519 /* These dies have a type, but processing them does not create
4520 a symbol or recurse to process the children. Therefore we can
4521 read them on-demand through read_type_die. */
4522 case DW_TAG_subroutine_type:
4523 case DW_TAG_set_type:
4524 case DW_TAG_array_type:
4525 case DW_TAG_pointer_type:
4526 case DW_TAG_ptr_to_member_type:
4527 case DW_TAG_reference_type:
4528 case DW_TAG_string_type:
4531 case DW_TAG_base_type:
4532 case DW_TAG_subrange_type:
4533 case DW_TAG_typedef:
4534 /* Add a typedef symbol for the type definition, if it has a
4536 new_symbol (die, read_type_die (die, cu), cu);
4538 case DW_TAG_common_block:
4539 read_common_block (die, cu);
4541 case DW_TAG_common_inclusion:
4543 case DW_TAG_namespace:
4544 processing_has_namespace_info = 1;
4545 read_namespace (die, cu);
4548 processing_has_namespace_info = 1;
4549 read_module (die, cu);
4551 case DW_TAG_imported_declaration:
4552 case DW_TAG_imported_module:
4553 processing_has_namespace_info = 1;
4554 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4555 || cu->language != language_fortran))
4556 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4557 dwarf_tag_name (die->tag));
4558 read_import_statement (die, cu);
4561 new_symbol (die, NULL, cu);
4566 /* A helper function for dwarf2_compute_name which determines whether DIE
4567 needs to have the name of the scope prepended to the name listed in the
4571 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4573 struct attribute *attr;
4577 case DW_TAG_namespace:
4578 case DW_TAG_typedef:
4579 case DW_TAG_class_type:
4580 case DW_TAG_interface_type:
4581 case DW_TAG_structure_type:
4582 case DW_TAG_union_type:
4583 case DW_TAG_enumeration_type:
4584 case DW_TAG_enumerator:
4585 case DW_TAG_subprogram:
4589 case DW_TAG_variable:
4590 /* We only need to prefix "globally" visible variables. These include
4591 any variable marked with DW_AT_external or any variable that
4592 lives in a namespace. [Variables in anonymous namespaces
4593 require prefixing, but they are not DW_AT_external.] */
4595 if (dwarf2_attr (die, DW_AT_specification, cu))
4597 struct dwarf2_cu *spec_cu = cu;
4599 return die_needs_namespace (die_specification (die, &spec_cu),
4603 attr = dwarf2_attr (die, DW_AT_external, cu);
4604 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4605 && die->parent->tag != DW_TAG_module)
4607 /* A variable in a lexical block of some kind does not need a
4608 namespace, even though in C++ such variables may be external
4609 and have a mangled name. */
4610 if (die->parent->tag == DW_TAG_lexical_block
4611 || die->parent->tag == DW_TAG_try_block
4612 || die->parent->tag == DW_TAG_catch_block
4613 || die->parent->tag == DW_TAG_subprogram)
4622 /* Retrieve the last character from a mem_file. */
4625 do_ui_file_peek_last (void *object, const char *buffer, long length)
4627 char *last_char_p = (char *) object;
4630 *last_char_p = buffer[length - 1];
4633 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4634 compute the physname for the object, which include a method's
4635 formal parameters (C++/Java) and return type (Java).
4637 For Ada, return the DIE's linkage name rather than the fully qualified
4638 name. PHYSNAME is ignored..
4640 The result is allocated on the objfile_obstack and canonicalized. */
4643 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4647 name = dwarf2_name (die, cu);
4649 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4650 compute it by typename_concat inside GDB. */
4651 if (cu->language == language_ada
4652 || (cu->language == language_fortran && physname))
4654 /* For Ada unit, we prefer the linkage name over the name, as
4655 the former contains the exported name, which the user expects
4656 to be able to reference. Ideally, we want the user to be able
4657 to reference this entity using either natural or linkage name,
4658 but we haven't started looking at this enhancement yet. */
4659 struct attribute *attr;
4661 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4663 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4664 if (attr && DW_STRING (attr))
4665 return DW_STRING (attr);
4668 /* These are the only languages we know how to qualify names in. */
4670 && (cu->language == language_cplus || cu->language == language_java
4671 || cu->language == language_fortran))
4673 if (die_needs_namespace (die, cu))
4677 struct ui_file *buf;
4679 prefix = determine_prefix (die, cu);
4680 buf = mem_fileopen ();
4681 if (*prefix != '\0')
4683 char *prefixed_name = typename_concat (NULL, prefix, name,
4686 fputs_unfiltered (prefixed_name, buf);
4687 xfree (prefixed_name);
4690 fputs_unfiltered (name ? name : "", buf);
4692 /* Template parameters may be specified in the DIE's DW_AT_name, or
4693 as children with DW_TAG_template_type_param or
4694 DW_TAG_value_type_param. If the latter, add them to the name
4695 here. If the name already has template parameters, then
4696 skip this step; some versions of GCC emit both, and
4697 it is more efficient to use the pre-computed name.
4699 Something to keep in mind about this process: it is very
4700 unlikely, or in some cases downright impossible, to produce
4701 something that will match the mangled name of a function.
4702 If the definition of the function has the same debug info,
4703 we should be able to match up with it anyway. But fallbacks
4704 using the minimal symbol, for instance to find a method
4705 implemented in a stripped copy of libstdc++, will not work.
4706 If we do not have debug info for the definition, we will have to
4707 match them up some other way.
4709 When we do name matching there is a related problem with function
4710 templates; two instantiated function templates are allowed to
4711 differ only by their return types, which we do not add here. */
4713 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4715 struct attribute *attr;
4716 struct die_info *child;
4719 die->building_fullname = 1;
4721 for (child = die->child; child != NULL; child = child->sibling)
4726 struct dwarf2_locexpr_baton *baton;
4729 if (child->tag != DW_TAG_template_type_param
4730 && child->tag != DW_TAG_template_value_param)
4735 fputs_unfiltered ("<", buf);
4739 fputs_unfiltered (", ", buf);
4741 attr = dwarf2_attr (child, DW_AT_type, cu);
4744 complaint (&symfile_complaints,
4745 _("template parameter missing DW_AT_type"));
4746 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4749 type = die_type (child, cu);
4751 if (child->tag == DW_TAG_template_type_param)
4753 c_print_type (type, "", buf, -1, 0);
4757 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4760 complaint (&symfile_complaints,
4761 _("template parameter missing DW_AT_const_value"));
4762 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4766 dwarf2_const_value_attr (attr, type, name,
4767 &cu->comp_unit_obstack, cu,
4768 &value, &bytes, &baton);
4770 if (TYPE_NOSIGN (type))
4771 /* GDB prints characters as NUMBER 'CHAR'. If that's
4772 changed, this can use value_print instead. */
4773 c_printchar (value, type, buf);
4776 struct value_print_options opts;
4779 v = dwarf2_evaluate_loc_desc (type, NULL,
4783 else if (bytes != NULL)
4785 v = allocate_value (type);
4786 memcpy (value_contents_writeable (v), bytes,
4787 TYPE_LENGTH (type));
4790 v = value_from_longest (type, value);
4792 /* Specify decimal so that we do not depend on the radix. */
4793 get_formatted_print_options (&opts, 'd');
4795 value_print (v, buf, &opts);
4801 die->building_fullname = 0;
4805 /* Close the argument list, with a space if necessary
4806 (nested templates). */
4807 char last_char = '\0';
4808 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4809 if (last_char == '>')
4810 fputs_unfiltered (" >", buf);
4812 fputs_unfiltered (">", buf);
4816 /* For Java and C++ methods, append formal parameter type
4817 information, if PHYSNAME. */
4819 if (physname && die->tag == DW_TAG_subprogram
4820 && (cu->language == language_cplus
4821 || cu->language == language_java))
4823 struct type *type = read_type_die (die, cu);
4825 c_type_print_args (type, buf, 0, cu->language);
4827 if (cu->language == language_java)
4829 /* For java, we must append the return type to method
4831 if (die->tag == DW_TAG_subprogram)
4832 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4835 else if (cu->language == language_cplus)
4837 if (TYPE_NFIELDS (type) > 0
4838 && TYPE_FIELD_ARTIFICIAL (type, 0)
4839 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4840 fputs_unfiltered (" const", buf);
4844 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4846 ui_file_delete (buf);
4848 if (cu->language == language_cplus)
4851 = dwarf2_canonicalize_name (name, cu,
4852 &cu->objfile->objfile_obstack);
4863 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4864 If scope qualifiers are appropriate they will be added. The result
4865 will be allocated on the objfile_obstack, or NULL if the DIE does
4866 not have a name. NAME may either be from a previous call to
4867 dwarf2_name or NULL.
4869 The output string will be canonicalized (if C++/Java). */
4872 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4874 return dwarf2_compute_name (name, die, cu, 0);
4877 /* Construct a physname for the given DIE in CU. NAME may either be
4878 from a previous call to dwarf2_name or NULL. The result will be
4879 allocated on the objfile_objstack or NULL if the DIE does not have a
4882 The output string will be canonicalized (if C++/Java). */
4885 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4887 return dwarf2_compute_name (name, die, cu, 1);
4890 /* Read the import statement specified by the given die and record it. */
4893 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4895 struct attribute *import_attr;
4896 struct die_info *imported_die;
4897 struct dwarf2_cu *imported_cu;
4898 const char *imported_name;
4899 const char *imported_name_prefix;
4900 const char *canonical_name;
4901 const char *import_alias;
4902 const char *imported_declaration = NULL;
4903 const char *import_prefix;
4907 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4908 if (import_attr == NULL)
4910 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4911 dwarf_tag_name (die->tag));
4916 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4917 imported_name = dwarf2_name (imported_die, imported_cu);
4918 if (imported_name == NULL)
4920 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4922 The import in the following code:
4936 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4937 <52> DW_AT_decl_file : 1
4938 <53> DW_AT_decl_line : 6
4939 <54> DW_AT_import : <0x75>
4940 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4942 <5b> DW_AT_decl_file : 1
4943 <5c> DW_AT_decl_line : 2
4944 <5d> DW_AT_type : <0x6e>
4946 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4947 <76> DW_AT_byte_size : 4
4948 <77> DW_AT_encoding : 5 (signed)
4950 imports the wrong die ( 0x75 instead of 0x58 ).
4951 This case will be ignored until the gcc bug is fixed. */
4955 /* Figure out the local name after import. */
4956 import_alias = dwarf2_name (die, cu);
4958 /* Figure out where the statement is being imported to. */
4959 import_prefix = determine_prefix (die, cu);
4961 /* Figure out what the scope of the imported die is and prepend it
4962 to the name of the imported die. */
4963 imported_name_prefix = determine_prefix (imported_die, imported_cu);
4965 if (imported_die->tag != DW_TAG_namespace
4966 && imported_die->tag != DW_TAG_module)
4968 imported_declaration = imported_name;
4969 canonical_name = imported_name_prefix;
4971 else if (strlen (imported_name_prefix) > 0)
4973 temp = alloca (strlen (imported_name_prefix)
4974 + 2 + strlen (imported_name) + 1);
4975 strcpy (temp, imported_name_prefix);
4976 strcat (temp, "::");
4977 strcat (temp, imported_name);
4978 canonical_name = temp;
4981 canonical_name = imported_name;
4983 cp_add_using_directive (import_prefix,
4986 imported_declaration,
4987 &cu->objfile->objfile_obstack);
4991 initialize_cu_func_list (struct dwarf2_cu *cu)
4993 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4997 free_cu_line_header (void *arg)
4999 struct dwarf2_cu *cu = arg;
5001 free_line_header (cu->line_header);
5002 cu->line_header = NULL;
5006 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5007 char **name, char **comp_dir)
5009 struct attribute *attr;
5014 /* Find the filename. Do not use dwarf2_name here, since the filename
5015 is not a source language identifier. */
5016 attr = dwarf2_attr (die, DW_AT_name, cu);
5019 *name = DW_STRING (attr);
5022 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5024 *comp_dir = DW_STRING (attr);
5025 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5027 *comp_dir = ldirname (*name);
5028 if (*comp_dir != NULL)
5029 make_cleanup (xfree, *comp_dir);
5031 if (*comp_dir != NULL)
5033 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5034 directory, get rid of it. */
5035 char *cp = strchr (*comp_dir, ':');
5037 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5042 *name = "<unknown>";
5046 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5048 struct objfile *objfile = cu->objfile;
5049 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5050 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5051 CORE_ADDR highpc = ((CORE_ADDR) 0);
5052 struct attribute *attr;
5054 char *comp_dir = NULL;
5055 struct die_info *child_die;
5056 bfd *abfd = objfile->obfd;
5057 struct line_header *line_header = 0;
5060 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5062 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5064 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5065 from finish_block. */
5066 if (lowpc == ((CORE_ADDR) -1))
5071 find_file_and_directory (die, cu, &name, &comp_dir);
5073 attr = dwarf2_attr (die, DW_AT_language, cu);
5076 set_cu_language (DW_UNSND (attr), cu);
5079 attr = dwarf2_attr (die, DW_AT_producer, cu);
5081 cu->producer = DW_STRING (attr);
5083 /* We assume that we're processing GCC output. */
5084 processing_gcc_compilation = 2;
5086 processing_has_namespace_info = 0;
5088 start_symtab (name, comp_dir, lowpc);
5089 record_debugformat ("DWARF 2");
5090 record_producer (cu->producer);
5092 initialize_cu_func_list (cu);
5094 /* Decode line number information if present. We do this before
5095 processing child DIEs, so that the line header table is available
5096 for DW_AT_decl_file. */
5097 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5100 unsigned int line_offset = DW_UNSND (attr);
5101 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5104 cu->line_header = line_header;
5105 make_cleanup (free_cu_line_header, cu);
5106 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5110 /* Process all dies in compilation unit. */
5111 if (die->child != NULL)
5113 child_die = die->child;
5114 while (child_die && child_die->tag)
5116 process_die (child_die, cu);
5117 child_die = sibling_die (child_die);
5121 /* Decode macro information, if present. Dwarf 2 macro information
5122 refers to information in the line number info statement program
5123 header, so we can only read it if we've read the header
5125 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5126 if (attr && line_header)
5128 unsigned int macro_offset = DW_UNSND (attr);
5130 dwarf_decode_macros (line_header, macro_offset,
5131 comp_dir, abfd, cu);
5133 do_cleanups (back_to);
5136 /* For TUs we want to skip the first top level sibling if it's not the
5137 actual type being defined by this TU. In this case the first top
5138 level sibling is there to provide context only. */
5141 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5143 struct objfile *objfile = cu->objfile;
5144 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5146 struct attribute *attr;
5148 char *comp_dir = NULL;
5149 struct die_info *child_die;
5150 bfd *abfd = objfile->obfd;
5152 /* start_symtab needs a low pc, but we don't really have one.
5153 Do what read_file_scope would do in the absence of such info. */
5154 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5156 /* Find the filename. Do not use dwarf2_name here, since the filename
5157 is not a source language identifier. */
5158 attr = dwarf2_attr (die, DW_AT_name, cu);
5160 name = DW_STRING (attr);
5162 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5164 comp_dir = DW_STRING (attr);
5165 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5167 comp_dir = ldirname (name);
5168 if (comp_dir != NULL)
5169 make_cleanup (xfree, comp_dir);
5175 attr = dwarf2_attr (die, DW_AT_language, cu);
5177 set_cu_language (DW_UNSND (attr), cu);
5179 /* This isn't technically needed today. It is done for symmetry
5180 with read_file_scope. */
5181 attr = dwarf2_attr (die, DW_AT_producer, cu);
5183 cu->producer = DW_STRING (attr);
5185 /* We assume that we're processing GCC output. */
5186 processing_gcc_compilation = 2;
5188 processing_has_namespace_info = 0;
5190 start_symtab (name, comp_dir, lowpc);
5191 record_debugformat ("DWARF 2");
5192 record_producer (cu->producer);
5194 /* Process the dies in the type unit. */
5195 if (die->child == NULL)
5197 dump_die_for_error (die);
5198 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5199 bfd_get_filename (abfd));
5202 child_die = die->child;
5204 while (child_die && child_die->tag)
5206 process_die (child_die, cu);
5208 child_die = sibling_die (child_die);
5211 do_cleanups (back_to);
5215 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5216 struct dwarf2_cu *cu)
5218 struct function_range *thisfn;
5220 thisfn = (struct function_range *)
5221 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5222 thisfn->name = name;
5223 thisfn->lowpc = lowpc;
5224 thisfn->highpc = highpc;
5225 thisfn->seen_line = 0;
5226 thisfn->next = NULL;
5228 if (cu->last_fn == NULL)
5229 cu->first_fn = thisfn;
5231 cu->last_fn->next = thisfn;
5233 cu->last_fn = thisfn;
5236 /* qsort helper for inherit_abstract_dies. */
5239 unsigned_int_compar (const void *ap, const void *bp)
5241 unsigned int a = *(unsigned int *) ap;
5242 unsigned int b = *(unsigned int *) bp;
5244 return (a > b) - (b > a);
5247 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5248 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5249 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5252 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5254 struct die_info *child_die;
5255 unsigned die_children_count;
5256 /* CU offsets which were referenced by children of the current DIE. */
5258 unsigned *offsets_end, *offsetp;
5259 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5260 struct die_info *origin_die;
5261 /* Iterator of the ORIGIN_DIE children. */
5262 struct die_info *origin_child_die;
5263 struct cleanup *cleanups;
5264 struct attribute *attr;
5266 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5270 origin_die = follow_die_ref (die, attr, &cu);
5271 if (die->tag != origin_die->tag
5272 && !(die->tag == DW_TAG_inlined_subroutine
5273 && origin_die->tag == DW_TAG_subprogram))
5274 complaint (&symfile_complaints,
5275 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5276 die->offset, origin_die->offset);
5278 child_die = die->child;
5279 die_children_count = 0;
5280 while (child_die && child_die->tag)
5282 child_die = sibling_die (child_die);
5283 die_children_count++;
5285 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5286 cleanups = make_cleanup (xfree, offsets);
5288 offsets_end = offsets;
5289 child_die = die->child;
5290 while (child_die && child_die->tag)
5292 /* For each CHILD_DIE, find the corresponding child of
5293 ORIGIN_DIE. If there is more than one layer of
5294 DW_AT_abstract_origin, follow them all; there shouldn't be,
5295 but GCC versions at least through 4.4 generate this (GCC PR
5297 struct die_info *child_origin_die = child_die;
5301 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
5304 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
5307 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5308 counterpart may exist. */
5309 if (child_origin_die != child_die)
5311 if (child_die->tag != child_origin_die->tag
5312 && !(child_die->tag == DW_TAG_inlined_subroutine
5313 && child_origin_die->tag == DW_TAG_subprogram))
5314 complaint (&symfile_complaints,
5315 _("Child DIE 0x%x and its abstract origin 0x%x have "
5316 "different tags"), child_die->offset,
5317 child_origin_die->offset);
5318 if (child_origin_die->parent != origin_die)
5319 complaint (&symfile_complaints,
5320 _("Child DIE 0x%x and its abstract origin 0x%x have "
5321 "different parents"), child_die->offset,
5322 child_origin_die->offset);
5324 *offsets_end++ = child_origin_die->offset;
5326 child_die = sibling_die (child_die);
5328 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5329 unsigned_int_compar);
5330 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5331 if (offsetp[-1] == *offsetp)
5332 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5333 "to DIE 0x%x as their abstract origin"),
5334 die->offset, *offsetp);
5337 origin_child_die = origin_die->child;
5338 while (origin_child_die && origin_child_die->tag)
5340 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5341 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5343 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5345 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5346 process_die (origin_child_die, cu);
5348 origin_child_die = sibling_die (origin_child_die);
5351 do_cleanups (cleanups);
5355 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5357 struct objfile *objfile = cu->objfile;
5358 struct context_stack *new;
5361 struct die_info *child_die;
5362 struct attribute *attr, *call_line, *call_file;
5365 struct block *block;
5366 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5367 VEC (symbolp) *template_args = NULL;
5368 struct template_symbol *templ_func = NULL;
5372 /* If we do not have call site information, we can't show the
5373 caller of this inlined function. That's too confusing, so
5374 only use the scope for local variables. */
5375 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5376 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5377 if (call_line == NULL || call_file == NULL)
5379 read_lexical_block_scope (die, cu);
5384 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5386 name = dwarf2_name (die, cu);
5388 /* Ignore functions with missing or empty names. These are actually
5389 illegal according to the DWARF standard. */
5392 complaint (&symfile_complaints,
5393 _("missing name for subprogram DIE at %d"), die->offset);
5397 /* Ignore functions with missing or invalid low and high pc attributes. */
5398 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5400 attr = dwarf2_attr (die, DW_AT_external, cu);
5401 if (!attr || !DW_UNSND (attr))
5402 complaint (&symfile_complaints,
5403 _("cannot get low and high bounds for subprogram DIE at %d"),
5411 /* Record the function range for dwarf_decode_lines. */
5412 add_to_cu_func_list (name, lowpc, highpc, cu);
5414 /* If we have any template arguments, then we must allocate a
5415 different sort of symbol. */
5416 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5418 if (child_die->tag == DW_TAG_template_type_param
5419 || child_die->tag == DW_TAG_template_value_param)
5421 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5422 struct template_symbol);
5423 templ_func->base.is_cplus_template_function = 1;
5428 new = push_context (0, lowpc);
5429 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5430 (struct symbol *) templ_func);
5432 /* If there is a location expression for DW_AT_frame_base, record
5434 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5436 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5437 expression is being recorded directly in the function's symbol
5438 and not in a separate frame-base object. I guess this hack is
5439 to avoid adding some sort of frame-base adjunct/annex to the
5440 function's symbol :-(. The problem with doing this is that it
5441 results in a function symbol with a location expression that
5442 has nothing to do with the location of the function, ouch! The
5443 relationship should be: a function's symbol has-a frame base; a
5444 frame-base has-a location expression. */
5445 dwarf2_symbol_mark_computed (attr, new->name, cu);
5447 cu->list_in_scope = &local_symbols;
5449 if (die->child != NULL)
5451 child_die = die->child;
5452 while (child_die && child_die->tag)
5454 if (child_die->tag == DW_TAG_template_type_param
5455 || child_die->tag == DW_TAG_template_value_param)
5457 struct symbol *arg = new_symbol (child_die, NULL, cu);
5459 VEC_safe_push (symbolp, template_args, arg);
5462 process_die (child_die, cu);
5463 child_die = sibling_die (child_die);
5467 inherit_abstract_dies (die, cu);
5469 /* If we have a DW_AT_specification, we might need to import using
5470 directives from the context of the specification DIE. See the
5471 comment in determine_prefix. */
5472 if (cu->language == language_cplus
5473 && dwarf2_attr (die, DW_AT_specification, cu))
5475 struct dwarf2_cu *spec_cu = cu;
5476 struct die_info *spec_die = die_specification (die, &spec_cu);
5480 child_die = spec_die->child;
5481 while (child_die && child_die->tag)
5483 if (child_die->tag == DW_TAG_imported_module)
5484 process_die (child_die, spec_cu);
5485 child_die = sibling_die (child_die);
5488 /* In some cases, GCC generates specification DIEs that
5489 themselves contain DW_AT_specification attributes. */
5490 spec_die = die_specification (spec_die, &spec_cu);
5494 new = pop_context ();
5495 /* Make a block for the local symbols within. */
5496 block = finish_block (new->name, &local_symbols, new->old_blocks,
5497 lowpc, highpc, objfile);
5499 /* For C++, set the block's scope. */
5500 if (cu->language == language_cplus || cu->language == language_fortran)
5501 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5502 determine_prefix (die, cu),
5503 processing_has_namespace_info);
5505 /* If we have address ranges, record them. */
5506 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5508 /* Attach template arguments to function. */
5509 if (! VEC_empty (symbolp, template_args))
5511 gdb_assert (templ_func != NULL);
5513 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5514 templ_func->template_arguments
5515 = obstack_alloc (&objfile->objfile_obstack,
5516 (templ_func->n_template_arguments
5517 * sizeof (struct symbol *)));
5518 memcpy (templ_func->template_arguments,
5519 VEC_address (symbolp, template_args),
5520 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5521 VEC_free (symbolp, template_args);
5524 /* In C++, we can have functions nested inside functions (e.g., when
5525 a function declares a class that has methods). This means that
5526 when we finish processing a function scope, we may need to go
5527 back to building a containing block's symbol lists. */
5528 local_symbols = new->locals;
5529 param_symbols = new->params;
5530 using_directives = new->using_directives;
5532 /* If we've finished processing a top-level function, subsequent
5533 symbols go in the file symbol list. */
5534 if (outermost_context_p ())
5535 cu->list_in_scope = &file_symbols;
5538 /* Process all the DIES contained within a lexical block scope. Start
5539 a new scope, process the dies, and then close the scope. */
5542 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5544 struct objfile *objfile = cu->objfile;
5545 struct context_stack *new;
5546 CORE_ADDR lowpc, highpc;
5547 struct die_info *child_die;
5550 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5552 /* Ignore blocks with missing or invalid low and high pc attributes. */
5553 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5554 as multiple lexical blocks? Handling children in a sane way would
5555 be nasty. Might be easier to properly extend generic blocks to
5557 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5562 push_context (0, lowpc);
5563 if (die->child != NULL)
5565 child_die = die->child;
5566 while (child_die && child_die->tag)
5568 process_die (child_die, cu);
5569 child_die = sibling_die (child_die);
5572 new = pop_context ();
5574 if (local_symbols != NULL || using_directives != NULL)
5577 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5580 /* Note that recording ranges after traversing children, as we
5581 do here, means that recording a parent's ranges entails
5582 walking across all its children's ranges as they appear in
5583 the address map, which is quadratic behavior.
5585 It would be nicer to record the parent's ranges before
5586 traversing its children, simply overriding whatever you find
5587 there. But since we don't even decide whether to create a
5588 block until after we've traversed its children, that's hard
5590 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5592 local_symbols = new->locals;
5593 using_directives = new->using_directives;
5596 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5597 Return 1 if the attributes are present and valid, otherwise, return 0.
5598 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5601 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5602 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5603 struct partial_symtab *ranges_pst)
5605 struct objfile *objfile = cu->objfile;
5606 struct comp_unit_head *cu_header = &cu->header;
5607 bfd *obfd = objfile->obfd;
5608 unsigned int addr_size = cu_header->addr_size;
5609 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5610 /* Base address selection entry. */
5621 found_base = cu->base_known;
5622 base = cu->base_address;
5624 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5625 if (offset >= dwarf2_per_objfile->ranges.size)
5627 complaint (&symfile_complaints,
5628 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5632 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5634 /* Read in the largest possible address. */
5635 marker = read_address (obfd, buffer, cu, &dummy);
5636 if ((marker & mask) == mask)
5638 /* If we found the largest possible address, then
5639 read the base address. */
5640 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5641 buffer += 2 * addr_size;
5642 offset += 2 * addr_size;
5648 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5652 CORE_ADDR range_beginning, range_end;
5654 range_beginning = read_address (obfd, buffer, cu, &dummy);
5655 buffer += addr_size;
5656 range_end = read_address (obfd, buffer, cu, &dummy);
5657 buffer += addr_size;
5658 offset += 2 * addr_size;
5660 /* An end of list marker is a pair of zero addresses. */
5661 if (range_beginning == 0 && range_end == 0)
5662 /* Found the end of list entry. */
5665 /* Each base address selection entry is a pair of 2 values.
5666 The first is the largest possible address, the second is
5667 the base address. Check for a base address here. */
5668 if ((range_beginning & mask) == mask)
5670 /* If we found the largest possible address, then
5671 read the base address. */
5672 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5679 /* We have no valid base address for the ranges
5681 complaint (&symfile_complaints,
5682 _("Invalid .debug_ranges data (no base address)"));
5686 range_beginning += base;
5689 if (ranges_pst != NULL && range_beginning < range_end)
5690 addrmap_set_empty (objfile->psymtabs_addrmap,
5691 range_beginning + baseaddr, range_end - 1 + baseaddr,
5694 /* FIXME: This is recording everything as a low-high
5695 segment of consecutive addresses. We should have a
5696 data structure for discontiguous block ranges
5700 low = range_beginning;
5706 if (range_beginning < low)
5707 low = range_beginning;
5708 if (range_end > high)
5714 /* If the first entry is an end-of-list marker, the range
5715 describes an empty scope, i.e. no instructions. */
5721 *high_return = high;
5725 /* Get low and high pc attributes from a die. Return 1 if the attributes
5726 are present and valid, otherwise, return 0. Return -1 if the range is
5727 discontinuous, i.e. derived from DW_AT_ranges information. */
5729 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5730 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5731 struct partial_symtab *pst)
5733 struct attribute *attr;
5738 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5741 high = DW_ADDR (attr);
5742 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5744 low = DW_ADDR (attr);
5746 /* Found high w/o low attribute. */
5749 /* Found consecutive range of addresses. */
5754 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5757 /* Value of the DW_AT_ranges attribute is the offset in the
5758 .debug_ranges section. */
5759 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5761 /* Found discontinuous range of addresses. */
5769 /* When using the GNU linker, .gnu.linkonce. sections are used to
5770 eliminate duplicate copies of functions and vtables and such.
5771 The linker will arbitrarily choose one and discard the others.
5772 The AT_*_pc values for such functions refer to local labels in
5773 these sections. If the section from that file was discarded, the
5774 labels are not in the output, so the relocs get a value of 0.
5775 If this is a discarded function, mark the pc bounds as invalid,
5776 so that GDB will ignore it. */
5777 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5785 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5786 its low and high PC addresses. Do nothing if these addresses could not
5787 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5788 and HIGHPC to the high address if greater than HIGHPC. */
5791 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5792 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5793 struct dwarf2_cu *cu)
5795 CORE_ADDR low, high;
5796 struct die_info *child = die->child;
5798 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5800 *lowpc = min (*lowpc, low);
5801 *highpc = max (*highpc, high);
5804 /* If the language does not allow nested subprograms (either inside
5805 subprograms or lexical blocks), we're done. */
5806 if (cu->language != language_ada)
5809 /* Check all the children of the given DIE. If it contains nested
5810 subprograms, then check their pc bounds. Likewise, we need to
5811 check lexical blocks as well, as they may also contain subprogram
5813 while (child && child->tag)
5815 if (child->tag == DW_TAG_subprogram
5816 || child->tag == DW_TAG_lexical_block)
5817 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5818 child = sibling_die (child);
5822 /* Get the low and high pc's represented by the scope DIE, and store
5823 them in *LOWPC and *HIGHPC. If the correct values can't be
5824 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5827 get_scope_pc_bounds (struct die_info *die,
5828 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5829 struct dwarf2_cu *cu)
5831 CORE_ADDR best_low = (CORE_ADDR) -1;
5832 CORE_ADDR best_high = (CORE_ADDR) 0;
5833 CORE_ADDR current_low, current_high;
5835 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL))
5837 best_low = current_low;
5838 best_high = current_high;
5842 struct die_info *child = die->child;
5844 while (child && child->tag)
5846 switch (child->tag) {
5847 case DW_TAG_subprogram:
5848 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5850 case DW_TAG_namespace:
5852 /* FIXME: carlton/2004-01-16: Should we do this for
5853 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5854 that current GCC's always emit the DIEs corresponding
5855 to definitions of methods of classes as children of a
5856 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5857 the DIEs giving the declarations, which could be
5858 anywhere). But I don't see any reason why the
5859 standards says that they have to be there. */
5860 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
5862 if (current_low != ((CORE_ADDR) -1))
5864 best_low = min (best_low, current_low);
5865 best_high = max (best_high, current_high);
5873 child = sibling_die (child);
5878 *highpc = best_high;
5881 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5884 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5885 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5887 struct attribute *attr;
5889 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5892 CORE_ADDR high = DW_ADDR (attr);
5894 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5897 CORE_ADDR low = DW_ADDR (attr);
5899 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5903 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5906 bfd *obfd = cu->objfile->obfd;
5908 /* The value of the DW_AT_ranges attribute is the offset of the
5909 address range list in the .debug_ranges section. */
5910 unsigned long offset = DW_UNSND (attr);
5911 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5913 /* For some target architectures, but not others, the
5914 read_address function sign-extends the addresses it returns.
5915 To recognize base address selection entries, we need a
5917 unsigned int addr_size = cu->header.addr_size;
5918 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5920 /* The base address, to which the next pair is relative. Note
5921 that this 'base' is a DWARF concept: most entries in a range
5922 list are relative, to reduce the number of relocs against the
5923 debugging information. This is separate from this function's
5924 'baseaddr' argument, which GDB uses to relocate debugging
5925 information from a shared library based on the address at
5926 which the library was loaded. */
5927 CORE_ADDR base = cu->base_address;
5928 int base_known = cu->base_known;
5930 gdb_assert (dwarf2_per_objfile->ranges.readin);
5931 if (offset >= dwarf2_per_objfile->ranges.size)
5933 complaint (&symfile_complaints,
5934 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5941 unsigned int bytes_read;
5942 CORE_ADDR start, end;
5944 start = read_address (obfd, buffer, cu, &bytes_read);
5945 buffer += bytes_read;
5946 end = read_address (obfd, buffer, cu, &bytes_read);
5947 buffer += bytes_read;
5949 /* Did we find the end of the range list? */
5950 if (start == 0 && end == 0)
5953 /* Did we find a base address selection entry? */
5954 else if ((start & base_select_mask) == base_select_mask)
5960 /* We found an ordinary address range. */
5965 complaint (&symfile_complaints,
5966 _("Invalid .debug_ranges data (no base address)"));
5970 record_block_range (block,
5971 baseaddr + base + start,
5972 baseaddr + base + end - 1);
5978 /* Add an aggregate field to the field list. */
5981 dwarf2_add_field (struct field_info *fip, struct die_info *die,
5982 struct dwarf2_cu *cu)
5984 struct objfile *objfile = cu->objfile;
5985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5986 struct nextfield *new_field;
5987 struct attribute *attr;
5989 char *fieldname = "";
5991 /* Allocate a new field list entry and link it in. */
5992 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
5993 make_cleanup (xfree, new_field);
5994 memset (new_field, 0, sizeof (struct nextfield));
5996 if (die->tag == DW_TAG_inheritance)
5998 new_field->next = fip->baseclasses;
5999 fip->baseclasses = new_field;
6003 new_field->next = fip->fields;
6004 fip->fields = new_field;
6008 /* Handle accessibility and virtuality of field.
6009 The default accessibility for members is public, the default
6010 accessibility for inheritance is private. */
6011 if (die->tag != DW_TAG_inheritance)
6012 new_field->accessibility = DW_ACCESS_public;
6014 new_field->accessibility = DW_ACCESS_private;
6015 new_field->virtuality = DW_VIRTUALITY_none;
6017 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6019 new_field->accessibility = DW_UNSND (attr);
6020 if (new_field->accessibility != DW_ACCESS_public)
6021 fip->non_public_fields = 1;
6022 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6024 new_field->virtuality = DW_UNSND (attr);
6026 fp = &new_field->field;
6028 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6030 /* Data member other than a C++ static data member. */
6032 /* Get type of field. */
6033 fp->type = die_type (die, cu);
6035 SET_FIELD_BITPOS (*fp, 0);
6037 /* Get bit size of field (zero if none). */
6038 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6041 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6045 FIELD_BITSIZE (*fp) = 0;
6048 /* Get bit offset of field. */
6049 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6052 int byte_offset = 0;
6054 if (attr_form_is_section_offset (attr))
6055 dwarf2_complex_location_expr_complaint ();
6056 else if (attr_form_is_constant (attr))
6057 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6058 else if (attr_form_is_block (attr))
6059 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6061 dwarf2_complex_location_expr_complaint ();
6063 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6065 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6068 if (gdbarch_bits_big_endian (gdbarch))
6070 /* For big endian bits, the DW_AT_bit_offset gives the
6071 additional bit offset from the MSB of the containing
6072 anonymous object to the MSB of the field. We don't
6073 have to do anything special since we don't need to
6074 know the size of the anonymous object. */
6075 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6079 /* For little endian bits, compute the bit offset to the
6080 MSB of the anonymous object, subtract off the number of
6081 bits from the MSB of the field to the MSB of the
6082 object, and then subtract off the number of bits of
6083 the field itself. The result is the bit offset of
6084 the LSB of the field. */
6086 int bit_offset = DW_UNSND (attr);
6088 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6091 /* The size of the anonymous object containing
6092 the bit field is explicit, so use the
6093 indicated size (in bytes). */
6094 anonymous_size = DW_UNSND (attr);
6098 /* The size of the anonymous object containing
6099 the bit field must be inferred from the type
6100 attribute of the data member containing the
6102 anonymous_size = TYPE_LENGTH (fp->type);
6104 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6105 - bit_offset - FIELD_BITSIZE (*fp);
6109 /* Get name of field. */
6110 fieldname = dwarf2_name (die, cu);
6111 if (fieldname == NULL)
6114 /* The name is already allocated along with this objfile, so we don't
6115 need to duplicate it for the type. */
6116 fp->name = fieldname;
6118 /* Change accessibility for artificial fields (e.g. virtual table
6119 pointer or virtual base class pointer) to private. */
6120 if (dwarf2_attr (die, DW_AT_artificial, cu))
6122 FIELD_ARTIFICIAL (*fp) = 1;
6123 new_field->accessibility = DW_ACCESS_private;
6124 fip->non_public_fields = 1;
6127 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6129 /* C++ static member. */
6131 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6132 is a declaration, but all versions of G++ as of this writing
6133 (so through at least 3.2.1) incorrectly generate
6134 DW_TAG_variable tags. */
6138 /* Get name of field. */
6139 fieldname = dwarf2_name (die, cu);
6140 if (fieldname == NULL)
6143 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6145 /* Only create a symbol if this is an external value.
6146 new_symbol checks this and puts the value in the global symbol
6147 table, which we want. If it is not external, new_symbol
6148 will try to put the value in cu->list_in_scope which is wrong. */
6149 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6151 /* A static const member, not much different than an enum as far as
6152 we're concerned, except that we can support more types. */
6153 new_symbol (die, NULL, cu);
6156 /* Get physical name. */
6157 physname = (char *) dwarf2_physname (fieldname, die, cu);
6159 /* The name is already allocated along with this objfile, so we don't
6160 need to duplicate it for the type. */
6161 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6162 FIELD_TYPE (*fp) = die_type (die, cu);
6163 FIELD_NAME (*fp) = fieldname;
6165 else if (die->tag == DW_TAG_inheritance)
6167 /* C++ base class field. */
6168 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6171 int byte_offset = 0;
6173 if (attr_form_is_section_offset (attr))
6174 dwarf2_complex_location_expr_complaint ();
6175 else if (attr_form_is_constant (attr))
6176 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6177 else if (attr_form_is_block (attr))
6178 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6180 dwarf2_complex_location_expr_complaint ();
6182 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6184 FIELD_BITSIZE (*fp) = 0;
6185 FIELD_TYPE (*fp) = die_type (die, cu);
6186 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6187 fip->nbaseclasses++;
6191 /* Add a typedef defined in the scope of the FIP's class. */
6194 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6195 struct dwarf2_cu *cu)
6197 struct objfile *objfile = cu->objfile;
6198 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6199 struct typedef_field_list *new_field;
6200 struct attribute *attr;
6201 struct typedef_field *fp;
6202 char *fieldname = "";
6204 /* Allocate a new field list entry and link it in. */
6205 new_field = xzalloc (sizeof (*new_field));
6206 make_cleanup (xfree, new_field);
6208 gdb_assert (die->tag == DW_TAG_typedef);
6210 fp = &new_field->field;
6212 /* Get name of field. */
6213 fp->name = dwarf2_name (die, cu);
6214 if (fp->name == NULL)
6217 fp->type = read_type_die (die, cu);
6219 new_field->next = fip->typedef_field_list;
6220 fip->typedef_field_list = new_field;
6221 fip->typedef_field_list_count++;
6224 /* Create the vector of fields, and attach it to the type. */
6227 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6228 struct dwarf2_cu *cu)
6230 int nfields = fip->nfields;
6232 /* Record the field count, allocate space for the array of fields,
6233 and create blank accessibility bitfields if necessary. */
6234 TYPE_NFIELDS (type) = nfields;
6235 TYPE_FIELDS (type) = (struct field *)
6236 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6237 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6239 if (fip->non_public_fields && cu->language != language_ada)
6241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6243 TYPE_FIELD_PRIVATE_BITS (type) =
6244 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6245 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6247 TYPE_FIELD_PROTECTED_BITS (type) =
6248 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6249 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6251 TYPE_FIELD_IGNORE_BITS (type) =
6252 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6253 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6256 /* If the type has baseclasses, allocate and clear a bit vector for
6257 TYPE_FIELD_VIRTUAL_BITS. */
6258 if (fip->nbaseclasses && cu->language != language_ada)
6260 int num_bytes = B_BYTES (fip->nbaseclasses);
6261 unsigned char *pointer;
6263 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6264 pointer = TYPE_ALLOC (type, num_bytes);
6265 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6266 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6267 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6270 /* Copy the saved-up fields into the field vector. Start from the head
6271 of the list, adding to the tail of the field array, so that they end
6272 up in the same order in the array in which they were added to the list. */
6273 while (nfields-- > 0)
6275 struct nextfield *fieldp;
6279 fieldp = fip->fields;
6280 fip->fields = fieldp->next;
6284 fieldp = fip->baseclasses;
6285 fip->baseclasses = fieldp->next;
6288 TYPE_FIELD (type, nfields) = fieldp->field;
6289 switch (fieldp->accessibility)
6291 case DW_ACCESS_private:
6292 if (cu->language != language_ada)
6293 SET_TYPE_FIELD_PRIVATE (type, nfields);
6296 case DW_ACCESS_protected:
6297 if (cu->language != language_ada)
6298 SET_TYPE_FIELD_PROTECTED (type, nfields);
6301 case DW_ACCESS_public:
6305 /* Unknown accessibility. Complain and treat it as public. */
6307 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6308 fieldp->accessibility);
6312 if (nfields < fip->nbaseclasses)
6314 switch (fieldp->virtuality)
6316 case DW_VIRTUALITY_virtual:
6317 case DW_VIRTUALITY_pure_virtual:
6318 if (cu->language == language_ada)
6319 error ("unexpected virtuality in component of Ada type");
6320 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6327 /* Add a member function to the proper fieldlist. */
6330 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6331 struct type *type, struct dwarf2_cu *cu)
6333 struct objfile *objfile = cu->objfile;
6334 struct attribute *attr;
6335 struct fnfieldlist *flp;
6337 struct fn_field *fnp;
6339 struct nextfnfield *new_fnfield;
6340 struct type *this_type;
6342 if (cu->language == language_ada)
6343 error ("unexpected member function in Ada type");
6345 /* Get name of member function. */
6346 fieldname = dwarf2_name (die, cu);
6347 if (fieldname == NULL)
6350 /* Look up member function name in fieldlist. */
6351 for (i = 0; i < fip->nfnfields; i++)
6353 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6357 /* Create new list element if necessary. */
6358 if (i < fip->nfnfields)
6359 flp = &fip->fnfieldlists[i];
6362 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6364 fip->fnfieldlists = (struct fnfieldlist *)
6365 xrealloc (fip->fnfieldlists,
6366 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6367 * sizeof (struct fnfieldlist));
6368 if (fip->nfnfields == 0)
6369 make_cleanup (free_current_contents, &fip->fnfieldlists);
6371 flp = &fip->fnfieldlists[fip->nfnfields];
6372 flp->name = fieldname;
6375 i = fip->nfnfields++;
6378 /* Create a new member function field and chain it to the field list
6380 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6381 make_cleanup (xfree, new_fnfield);
6382 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6383 new_fnfield->next = flp->head;
6384 flp->head = new_fnfield;
6387 /* Fill in the member function field info. */
6388 fnp = &new_fnfield->fnfield;
6390 /* Delay processing of the physname until later. */
6391 if (cu->language == language_cplus || cu->language == language_java)
6393 add_to_method_list (type, i, flp->length - 1, fieldname,
6398 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6399 fnp->physname = physname ? physname : "";
6402 fnp->type = alloc_type (objfile);
6403 this_type = read_type_die (die, cu);
6404 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6406 int nparams = TYPE_NFIELDS (this_type);
6408 /* TYPE is the domain of this method, and THIS_TYPE is the type
6409 of the method itself (TYPE_CODE_METHOD). */
6410 smash_to_method_type (fnp->type, type,
6411 TYPE_TARGET_TYPE (this_type),
6412 TYPE_FIELDS (this_type),
6413 TYPE_NFIELDS (this_type),
6414 TYPE_VARARGS (this_type));
6416 /* Handle static member functions.
6417 Dwarf2 has no clean way to discern C++ static and non-static
6418 member functions. G++ helps GDB by marking the first
6419 parameter for non-static member functions (which is the
6420 this pointer) as artificial. We obtain this information
6421 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6422 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6423 fnp->voffset = VOFFSET_STATIC;
6426 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6427 dwarf2_full_name (fieldname, die, cu));
6429 /* Get fcontext from DW_AT_containing_type if present. */
6430 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6431 fnp->fcontext = die_containing_type (die, cu);
6433 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6434 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6436 /* Get accessibility. */
6437 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6440 switch (DW_UNSND (attr))
6442 case DW_ACCESS_private:
6443 fnp->is_private = 1;
6445 case DW_ACCESS_protected:
6446 fnp->is_protected = 1;
6451 /* Check for artificial methods. */
6452 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6453 if (attr && DW_UNSND (attr) != 0)
6454 fnp->is_artificial = 1;
6456 /* Get index in virtual function table if it is a virtual member
6457 function. For older versions of GCC, this is an offset in the
6458 appropriate virtual table, as specified by DW_AT_containing_type.
6459 For everyone else, it is an expression to be evaluated relative
6460 to the object address. */
6462 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6465 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6467 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6469 /* Old-style GCC. */
6470 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6472 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6473 || (DW_BLOCK (attr)->size > 1
6474 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6475 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6477 struct dwarf_block blk;
6480 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6482 blk.size = DW_BLOCK (attr)->size - offset;
6483 blk.data = DW_BLOCK (attr)->data + offset;
6484 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6485 if ((fnp->voffset % cu->header.addr_size) != 0)
6486 dwarf2_complex_location_expr_complaint ();
6488 fnp->voffset /= cu->header.addr_size;
6492 dwarf2_complex_location_expr_complaint ();
6495 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6497 else if (attr_form_is_section_offset (attr))
6499 dwarf2_complex_location_expr_complaint ();
6503 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6509 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6510 if (attr && DW_UNSND (attr))
6512 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6513 complaint (&symfile_complaints,
6514 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6515 fieldname, die->offset);
6516 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6517 TYPE_CPLUS_DYNAMIC (type) = 1;
6522 /* Create the vector of member function fields, and attach it to the type. */
6525 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6526 struct dwarf2_cu *cu)
6528 struct fnfieldlist *flp;
6529 int total_length = 0;
6532 if (cu->language == language_ada)
6533 error ("unexpected member functions in Ada type");
6535 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6536 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6537 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6539 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6541 struct nextfnfield *nfp = flp->head;
6542 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6545 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6546 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6547 fn_flp->fn_fields = (struct fn_field *)
6548 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6549 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6550 fn_flp->fn_fields[k] = nfp->fnfield;
6552 total_length += flp->length;
6555 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6556 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6559 /* Returns non-zero if NAME is the name of a vtable member in CU's
6560 language, zero otherwise. */
6562 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6564 static const char vptr[] = "_vptr";
6565 static const char vtable[] = "vtable";
6567 /* Look for the C++ and Java forms of the vtable. */
6568 if ((cu->language == language_java
6569 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6570 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6571 && is_cplus_marker (name[sizeof (vptr) - 1])))
6577 /* GCC outputs unnamed structures that are really pointers to member
6578 functions, with the ABI-specified layout. If TYPE describes
6579 such a structure, smash it into a member function type.
6581 GCC shouldn't do this; it should just output pointer to member DIEs.
6582 This is GCC PR debug/28767. */
6585 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6587 struct type *pfn_type, *domain_type, *new_type;
6589 /* Check for a structure with no name and two children. */
6590 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6593 /* Check for __pfn and __delta members. */
6594 if (TYPE_FIELD_NAME (type, 0) == NULL
6595 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6596 || TYPE_FIELD_NAME (type, 1) == NULL
6597 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6600 /* Find the type of the method. */
6601 pfn_type = TYPE_FIELD_TYPE (type, 0);
6602 if (pfn_type == NULL
6603 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6604 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6607 /* Look for the "this" argument. */
6608 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6609 if (TYPE_NFIELDS (pfn_type) == 0
6610 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6611 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6614 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6615 new_type = alloc_type (objfile);
6616 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6617 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6618 TYPE_VARARGS (pfn_type));
6619 smash_to_methodptr_type (type, new_type);
6622 /* Called when we find the DIE that starts a structure or union scope
6623 (definition) to process all dies that define the members of the
6626 NOTE: we need to call struct_type regardless of whether or not the
6627 DIE has an at_name attribute, since it might be an anonymous
6628 structure or union. This gets the type entered into our set of
6631 However, if the structure is incomplete (an opaque struct/union)
6632 then suppress creating a symbol table entry for it since gdb only
6633 wants to find the one with the complete definition. Note that if
6634 it is complete, we just call new_symbol, which does it's own
6635 checking about whether the struct/union is anonymous or not (and
6636 suppresses creating a symbol table entry itself). */
6638 static struct type *
6639 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6641 struct objfile *objfile = cu->objfile;
6643 struct attribute *attr;
6645 struct cleanup *back_to;
6647 /* If the definition of this type lives in .debug_types, read that type.
6648 Don't follow DW_AT_specification though, that will take us back up
6649 the chain and we want to go down. */
6650 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6653 struct dwarf2_cu *type_cu = cu;
6654 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6656 /* We could just recurse on read_structure_type, but we need to call
6657 get_die_type to ensure only one type for this DIE is created.
6658 This is important, for example, because for c++ classes we need
6659 TYPE_NAME set which is only done by new_symbol. Blech. */
6660 type = read_type_die (type_die, type_cu);
6662 /* TYPE_CU may not be the same as CU.
6663 Ensure TYPE is recorded in CU's type_hash table. */
6664 return set_die_type (die, type, cu);
6667 back_to = make_cleanup (null_cleanup, 0);
6669 type = alloc_type (objfile);
6670 INIT_CPLUS_SPECIFIC (type);
6672 name = dwarf2_name (die, cu);
6675 if (cu->language == language_cplus
6676 || cu->language == language_java)
6678 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6680 /* dwarf2_full_name might have already finished building the DIE's
6681 type. If so, there is no need to continue. */
6682 if (get_die_type (die, cu) != NULL)
6683 return get_die_type (die, cu);
6685 TYPE_TAG_NAME (type) = full_name;
6686 if (die->tag == DW_TAG_structure_type
6687 || die->tag == DW_TAG_class_type)
6688 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6692 /* The name is already allocated along with this objfile, so
6693 we don't need to duplicate it for the type. */
6694 TYPE_TAG_NAME (type) = (char *) name;
6695 if (die->tag == DW_TAG_class_type)
6696 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6700 if (die->tag == DW_TAG_structure_type)
6702 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6704 else if (die->tag == DW_TAG_union_type)
6706 TYPE_CODE (type) = TYPE_CODE_UNION;
6710 TYPE_CODE (type) = TYPE_CODE_CLASS;
6713 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6714 TYPE_DECLARED_CLASS (type) = 1;
6716 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6719 TYPE_LENGTH (type) = DW_UNSND (attr);
6723 TYPE_LENGTH (type) = 0;
6726 TYPE_STUB_SUPPORTED (type) = 1;
6727 if (die_is_declaration (die, cu))
6728 TYPE_STUB (type) = 1;
6729 else if (attr == NULL && die->child == NULL
6730 && producer_is_realview (cu->producer))
6731 /* RealView does not output the required DW_AT_declaration
6732 on incomplete types. */
6733 TYPE_STUB (type) = 1;
6735 /* We need to add the type field to the die immediately so we don't
6736 infinitely recurse when dealing with pointers to the structure
6737 type within the structure itself. */
6738 set_die_type (die, type, cu);
6740 /* set_die_type should be already done. */
6741 set_descriptive_type (type, die, cu);
6743 if (die->child != NULL && ! die_is_declaration (die, cu))
6745 struct field_info fi;
6746 struct die_info *child_die;
6747 VEC (symbolp) *template_args = NULL;
6749 memset (&fi, 0, sizeof (struct field_info));
6751 child_die = die->child;
6753 while (child_die && child_die->tag)
6755 if (child_die->tag == DW_TAG_member
6756 || child_die->tag == DW_TAG_variable)
6758 /* NOTE: carlton/2002-11-05: A C++ static data member
6759 should be a DW_TAG_member that is a declaration, but
6760 all versions of G++ as of this writing (so through at
6761 least 3.2.1) incorrectly generate DW_TAG_variable
6762 tags for them instead. */
6763 dwarf2_add_field (&fi, child_die, cu);
6765 else if (child_die->tag == DW_TAG_subprogram)
6767 /* C++ member function. */
6768 dwarf2_add_member_fn (&fi, child_die, type, cu);
6770 else if (child_die->tag == DW_TAG_inheritance)
6772 /* C++ base class field. */
6773 dwarf2_add_field (&fi, child_die, cu);
6775 else if (child_die->tag == DW_TAG_typedef)
6776 dwarf2_add_typedef (&fi, child_die, cu);
6777 else if (child_die->tag == DW_TAG_template_type_param
6778 || child_die->tag == DW_TAG_template_value_param)
6780 struct symbol *arg = new_symbol (child_die, NULL, cu);
6782 VEC_safe_push (symbolp, template_args, arg);
6785 child_die = sibling_die (child_die);
6788 /* Attach template arguments to type. */
6789 if (! VEC_empty (symbolp, template_args))
6791 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6792 TYPE_N_TEMPLATE_ARGUMENTS (type)
6793 = VEC_length (symbolp, template_args);
6794 TYPE_TEMPLATE_ARGUMENTS (type)
6795 = obstack_alloc (&objfile->objfile_obstack,
6796 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6797 * sizeof (struct symbol *)));
6798 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6799 VEC_address (symbolp, template_args),
6800 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6801 * sizeof (struct symbol *)));
6802 VEC_free (symbolp, template_args);
6805 /* Attach fields and member functions to the type. */
6807 dwarf2_attach_fields_to_type (&fi, type, cu);
6810 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6812 /* Get the type which refers to the base class (possibly this
6813 class itself) which contains the vtable pointer for the current
6814 class from the DW_AT_containing_type attribute. This use of
6815 DW_AT_containing_type is a GNU extension. */
6817 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6819 struct type *t = die_containing_type (die, cu);
6821 TYPE_VPTR_BASETYPE (type) = t;
6826 /* Our own class provides vtbl ptr. */
6827 for (i = TYPE_NFIELDS (t) - 1;
6828 i >= TYPE_N_BASECLASSES (t);
6831 char *fieldname = TYPE_FIELD_NAME (t, i);
6833 if (is_vtable_name (fieldname, cu))
6835 TYPE_VPTR_FIELDNO (type) = i;
6840 /* Complain if virtual function table field not found. */
6841 if (i < TYPE_N_BASECLASSES (t))
6842 complaint (&symfile_complaints,
6843 _("virtual function table pointer not found when defining class '%s'"),
6844 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6849 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6852 else if (cu->producer
6853 && strncmp (cu->producer,
6854 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6856 /* The IBM XLC compiler does not provide direct indication
6857 of the containing type, but the vtable pointer is
6858 always named __vfp. */
6862 for (i = TYPE_NFIELDS (type) - 1;
6863 i >= TYPE_N_BASECLASSES (type);
6866 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6868 TYPE_VPTR_FIELDNO (type) = i;
6869 TYPE_VPTR_BASETYPE (type) = type;
6876 /* Copy fi.typedef_field_list linked list elements content into the
6877 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6878 if (fi.typedef_field_list)
6880 int i = fi.typedef_field_list_count;
6882 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6883 TYPE_TYPEDEF_FIELD_ARRAY (type)
6884 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6885 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6887 /* Reverse the list order to keep the debug info elements order. */
6890 struct typedef_field *dest, *src;
6892 dest = &TYPE_TYPEDEF_FIELD (type, i);
6893 src = &fi.typedef_field_list->field;
6894 fi.typedef_field_list = fi.typedef_field_list->next;
6900 quirk_gcc_member_function_pointer (type, cu->objfile);
6902 do_cleanups (back_to);
6907 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6909 struct die_info *child_die = die->child;
6910 struct type *this_type;
6912 this_type = get_die_type (die, cu);
6913 if (this_type == NULL)
6914 this_type = read_structure_type (die, cu);
6916 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6917 snapshots) has been known to create a die giving a declaration
6918 for a class that has, as a child, a die giving a definition for a
6919 nested class. So we have to process our children even if the
6920 current die is a declaration. Normally, of course, a declaration
6921 won't have any children at all. */
6923 while (child_die != NULL && child_die->tag)
6925 if (child_die->tag == DW_TAG_member
6926 || child_die->tag == DW_TAG_variable
6927 || child_die->tag == DW_TAG_inheritance
6928 || child_die->tag == DW_TAG_template_value_param
6929 || child_die->tag == DW_TAG_template_type_param)
6934 process_die (child_die, cu);
6936 child_die = sibling_die (child_die);
6939 /* Do not consider external references. According to the DWARF standard,
6940 these DIEs are identified by the fact that they have no byte_size
6941 attribute, and a declaration attribute. */
6942 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6943 || !die_is_declaration (die, cu))
6944 new_symbol (die, this_type, cu);
6947 /* Given a DW_AT_enumeration_type die, set its type. We do not
6948 complete the type's fields yet, or create any symbols. */
6950 static struct type *
6951 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6953 struct objfile *objfile = cu->objfile;
6955 struct attribute *attr;
6958 /* If the definition of this type lives in .debug_types, read that type.
6959 Don't follow DW_AT_specification though, that will take us back up
6960 the chain and we want to go down. */
6961 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6964 struct dwarf2_cu *type_cu = cu;
6965 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6967 type = read_type_die (type_die, type_cu);
6969 /* TYPE_CU may not be the same as CU.
6970 Ensure TYPE is recorded in CU's type_hash table. */
6971 return set_die_type (die, type, cu);
6974 type = alloc_type (objfile);
6976 TYPE_CODE (type) = TYPE_CODE_ENUM;
6977 name = dwarf2_full_name (NULL, die, cu);
6979 TYPE_TAG_NAME (type) = (char *) name;
6981 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6984 TYPE_LENGTH (type) = DW_UNSND (attr);
6988 TYPE_LENGTH (type) = 0;
6991 /* The enumeration DIE can be incomplete. In Ada, any type can be
6992 declared as private in the package spec, and then defined only
6993 inside the package body. Such types are known as Taft Amendment
6994 Types. When another package uses such a type, an incomplete DIE
6995 may be generated by the compiler. */
6996 if (die_is_declaration (die, cu))
6997 TYPE_STUB (type) = 1;
6999 return set_die_type (die, type, cu);
7002 /* Given a pointer to a die which begins an enumeration, process all
7003 the dies that define the members of the enumeration, and create the
7004 symbol for the enumeration type.
7006 NOTE: We reverse the order of the element list. */
7009 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7011 struct type *this_type;
7013 this_type = get_die_type (die, cu);
7014 if (this_type == NULL)
7015 this_type = read_enumeration_type (die, cu);
7017 if (die->child != NULL)
7019 struct die_info *child_die;
7021 struct field *fields = NULL;
7023 int unsigned_enum = 1;
7026 child_die = die->child;
7027 while (child_die && child_die->tag)
7029 if (child_die->tag != DW_TAG_enumerator)
7031 process_die (child_die, cu);
7035 name = dwarf2_name (child_die, cu);
7038 sym = new_symbol (child_die, this_type, cu);
7039 if (SYMBOL_VALUE (sym) < 0)
7042 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7044 fields = (struct field *)
7046 (num_fields + DW_FIELD_ALLOC_CHUNK)
7047 * sizeof (struct field));
7050 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7051 FIELD_TYPE (fields[num_fields]) = NULL;
7052 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7053 FIELD_BITSIZE (fields[num_fields]) = 0;
7059 child_die = sibling_die (child_die);
7064 TYPE_NFIELDS (this_type) = num_fields;
7065 TYPE_FIELDS (this_type) = (struct field *)
7066 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7067 memcpy (TYPE_FIELDS (this_type), fields,
7068 sizeof (struct field) * num_fields);
7072 TYPE_UNSIGNED (this_type) = 1;
7075 new_symbol (die, this_type, cu);
7078 /* Extract all information from a DW_TAG_array_type DIE and put it in
7079 the DIE's type field. For now, this only handles one dimensional
7082 static struct type *
7083 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7085 struct objfile *objfile = cu->objfile;
7086 struct die_info *child_die;
7088 struct type *element_type, *range_type, *index_type;
7089 struct type **range_types = NULL;
7090 struct attribute *attr;
7092 struct cleanup *back_to;
7095 element_type = die_type (die, cu);
7097 /* The die_type call above may have already set the type for this DIE. */
7098 type = get_die_type (die, cu);
7102 /* Irix 6.2 native cc creates array types without children for
7103 arrays with unspecified length. */
7104 if (die->child == NULL)
7106 index_type = objfile_type (objfile)->builtin_int;
7107 range_type = create_range_type (NULL, index_type, 0, -1);
7108 type = create_array_type (NULL, element_type, range_type);
7109 return set_die_type (die, type, cu);
7112 back_to = make_cleanup (null_cleanup, NULL);
7113 child_die = die->child;
7114 while (child_die && child_die->tag)
7116 if (child_die->tag == DW_TAG_subrange_type)
7118 struct type *child_type = read_type_die (child_die, cu);
7120 if (child_type != NULL)
7122 /* The range type was succesfully read. Save it for
7123 the array type creation. */
7124 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7126 range_types = (struct type **)
7127 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7128 * sizeof (struct type *));
7130 make_cleanup (free_current_contents, &range_types);
7132 range_types[ndim++] = child_type;
7135 child_die = sibling_die (child_die);
7138 /* Dwarf2 dimensions are output from left to right, create the
7139 necessary array types in backwards order. */
7141 type = element_type;
7143 if (read_array_order (die, cu) == DW_ORD_col_major)
7148 type = create_array_type (NULL, type, range_types[i++]);
7153 type = create_array_type (NULL, type, range_types[ndim]);
7156 /* Understand Dwarf2 support for vector types (like they occur on
7157 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7158 array type. This is not part of the Dwarf2/3 standard yet, but a
7159 custom vendor extension. The main difference between a regular
7160 array and the vector variant is that vectors are passed by value
7162 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7164 make_vector_type (type);
7166 name = dwarf2_name (die, cu);
7168 TYPE_NAME (type) = name;
7170 /* Install the type in the die. */
7171 set_die_type (die, type, cu);
7173 /* set_die_type should be already done. */
7174 set_descriptive_type (type, die, cu);
7176 do_cleanups (back_to);
7181 static enum dwarf_array_dim_ordering
7182 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7184 struct attribute *attr;
7186 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7188 if (attr) return DW_SND (attr);
7191 GNU F77 is a special case, as at 08/2004 array type info is the
7192 opposite order to the dwarf2 specification, but data is still
7193 laid out as per normal fortran.
7195 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7199 if (cu->language == language_fortran
7200 && cu->producer && strstr (cu->producer, "GNU F77"))
7202 return DW_ORD_row_major;
7205 switch (cu->language_defn->la_array_ordering)
7207 case array_column_major:
7208 return DW_ORD_col_major;
7209 case array_row_major:
7211 return DW_ORD_row_major;
7215 /* Extract all information from a DW_TAG_set_type DIE and put it in
7216 the DIE's type field. */
7218 static struct type *
7219 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7221 struct type *domain_type, *set_type;
7222 struct attribute *attr;
7224 domain_type = die_type (die, cu);
7226 /* The die_type call above may have already set the type for this DIE. */
7227 set_type = get_die_type (die, cu);
7231 set_type = create_set_type (NULL, domain_type);
7233 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7235 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7237 return set_die_type (die, set_type, cu);
7240 /* First cut: install each common block member as a global variable. */
7243 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7245 struct die_info *child_die;
7246 struct attribute *attr;
7248 CORE_ADDR base = (CORE_ADDR) 0;
7250 attr = dwarf2_attr (die, DW_AT_location, cu);
7253 /* Support the .debug_loc offsets */
7254 if (attr_form_is_block (attr))
7256 base = decode_locdesc (DW_BLOCK (attr), cu);
7258 else if (attr_form_is_section_offset (attr))
7260 dwarf2_complex_location_expr_complaint ();
7264 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7265 "common block member");
7268 if (die->child != NULL)
7270 child_die = die->child;
7271 while (child_die && child_die->tag)
7273 sym = new_symbol (child_die, NULL, cu);
7274 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7277 CORE_ADDR byte_offset = 0;
7279 if (attr_form_is_section_offset (attr))
7280 dwarf2_complex_location_expr_complaint ();
7281 else if (attr_form_is_constant (attr))
7282 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7283 else if (attr_form_is_block (attr))
7284 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7286 dwarf2_complex_location_expr_complaint ();
7288 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7289 add_symbol_to_list (sym, &global_symbols);
7291 child_die = sibling_die (child_die);
7296 /* Create a type for a C++ namespace. */
7298 static struct type *
7299 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7301 struct objfile *objfile = cu->objfile;
7302 const char *previous_prefix, *name;
7306 /* For extensions, reuse the type of the original namespace. */
7307 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7309 struct die_info *ext_die;
7310 struct dwarf2_cu *ext_cu = cu;
7312 ext_die = dwarf2_extension (die, &ext_cu);
7313 type = read_type_die (ext_die, ext_cu);
7315 /* EXT_CU may not be the same as CU.
7316 Ensure TYPE is recorded in CU's type_hash table. */
7317 return set_die_type (die, type, cu);
7320 name = namespace_name (die, &is_anonymous, cu);
7322 /* Now build the name of the current namespace. */
7324 previous_prefix = determine_prefix (die, cu);
7325 if (previous_prefix[0] != '\0')
7326 name = typename_concat (&objfile->objfile_obstack,
7327 previous_prefix, name, 0, cu);
7329 /* Create the type. */
7330 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7332 TYPE_NAME (type) = (char *) name;
7333 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7335 return set_die_type (die, type, cu);
7338 /* Read a C++ namespace. */
7341 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7343 struct objfile *objfile = cu->objfile;
7347 /* Add a symbol associated to this if we haven't seen the namespace
7348 before. Also, add a using directive if it's an anonymous
7351 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7355 type = read_type_die (die, cu);
7356 new_symbol (die, type, cu);
7358 name = namespace_name (die, &is_anonymous, cu);
7361 const char *previous_prefix = determine_prefix (die, cu);
7363 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7364 NULL, &objfile->objfile_obstack);
7368 if (die->child != NULL)
7370 struct die_info *child_die = die->child;
7372 while (child_die && child_die->tag)
7374 process_die (child_die, cu);
7375 child_die = sibling_die (child_die);
7380 /* Read a Fortran module as type. This DIE can be only a declaration used for
7381 imported module. Still we need that type as local Fortran "use ... only"
7382 declaration imports depend on the created type in determine_prefix. */
7384 static struct type *
7385 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7387 struct objfile *objfile = cu->objfile;
7391 module_name = dwarf2_name (die, cu);
7393 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7395 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7397 /* determine_prefix uses TYPE_TAG_NAME. */
7398 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7400 return set_die_type (die, type, cu);
7403 /* Read a Fortran module. */
7406 read_module (struct die_info *die, struct dwarf2_cu *cu)
7408 struct die_info *child_die = die->child;
7410 while (child_die && child_die->tag)
7412 process_die (child_die, cu);
7413 child_die = sibling_die (child_die);
7417 /* Return the name of the namespace represented by DIE. Set
7418 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7422 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7424 struct die_info *current_die;
7425 const char *name = NULL;
7427 /* Loop through the extensions until we find a name. */
7429 for (current_die = die;
7430 current_die != NULL;
7431 current_die = dwarf2_extension (die, &cu))
7433 name = dwarf2_name (current_die, cu);
7438 /* Is it an anonymous namespace? */
7440 *is_anonymous = (name == NULL);
7442 name = "(anonymous namespace)";
7447 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7448 the user defined type vector. */
7450 static struct type *
7451 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7453 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7454 struct comp_unit_head *cu_header = &cu->header;
7456 struct attribute *attr_byte_size;
7457 struct attribute *attr_address_class;
7458 int byte_size, addr_class;
7459 struct type *target_type;
7461 target_type = die_type (die, cu);
7463 /* The die_type call above may have already set the type for this DIE. */
7464 type = get_die_type (die, cu);
7468 type = lookup_pointer_type (target_type);
7470 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7472 byte_size = DW_UNSND (attr_byte_size);
7474 byte_size = cu_header->addr_size;
7476 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7477 if (attr_address_class)
7478 addr_class = DW_UNSND (attr_address_class);
7480 addr_class = DW_ADDR_none;
7482 /* If the pointer size or address class is different than the
7483 default, create a type variant marked as such and set the
7484 length accordingly. */
7485 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7487 if (gdbarch_address_class_type_flags_p (gdbarch))
7491 type_flags = gdbarch_address_class_type_flags
7492 (gdbarch, byte_size, addr_class);
7493 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7495 type = make_type_with_address_space (type, type_flags);
7497 else if (TYPE_LENGTH (type) != byte_size)
7499 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7503 /* Should we also complain about unhandled address classes? */
7507 TYPE_LENGTH (type) = byte_size;
7508 return set_die_type (die, type, cu);
7511 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7512 the user defined type vector. */
7514 static struct type *
7515 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7518 struct type *to_type;
7519 struct type *domain;
7521 to_type = die_type (die, cu);
7522 domain = die_containing_type (die, cu);
7524 /* The calls above may have already set the type for this DIE. */
7525 type = get_die_type (die, cu);
7529 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7530 type = lookup_methodptr_type (to_type);
7532 type = lookup_memberptr_type (to_type, domain);
7534 return set_die_type (die, type, cu);
7537 /* Extract all information from a DW_TAG_reference_type DIE and add to
7538 the user defined type vector. */
7540 static struct type *
7541 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7543 struct comp_unit_head *cu_header = &cu->header;
7544 struct type *type, *target_type;
7545 struct attribute *attr;
7547 target_type = die_type (die, cu);
7549 /* The die_type call above may have already set the type for this DIE. */
7550 type = get_die_type (die, cu);
7554 type = lookup_reference_type (target_type);
7555 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7558 TYPE_LENGTH (type) = DW_UNSND (attr);
7562 TYPE_LENGTH (type) = cu_header->addr_size;
7564 return set_die_type (die, type, cu);
7567 static struct type *
7568 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7570 struct type *base_type, *cv_type;
7572 base_type = die_type (die, cu);
7574 /* The die_type call above may have already set the type for this DIE. */
7575 cv_type = get_die_type (die, cu);
7579 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7580 return set_die_type (die, cv_type, cu);
7583 static struct type *
7584 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7586 struct type *base_type, *cv_type;
7588 base_type = die_type (die, cu);
7590 /* The die_type call above may have already set the type for this DIE. */
7591 cv_type = get_die_type (die, cu);
7595 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7596 return set_die_type (die, cv_type, cu);
7599 /* Extract all information from a DW_TAG_string_type DIE and add to
7600 the user defined type vector. It isn't really a user defined type,
7601 but it behaves like one, with other DIE's using an AT_user_def_type
7602 attribute to reference it. */
7604 static struct type *
7605 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7607 struct objfile *objfile = cu->objfile;
7608 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7609 struct type *type, *range_type, *index_type, *char_type;
7610 struct attribute *attr;
7611 unsigned int length;
7613 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7616 length = DW_UNSND (attr);
7620 /* check for the DW_AT_byte_size attribute */
7621 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7624 length = DW_UNSND (attr);
7632 index_type = objfile_type (objfile)->builtin_int;
7633 range_type = create_range_type (NULL, index_type, 1, length);
7634 char_type = language_string_char_type (cu->language_defn, gdbarch);
7635 type = create_string_type (NULL, char_type, range_type);
7637 return set_die_type (die, type, cu);
7640 /* Handle DIES due to C code like:
7644 int (*funcp)(int a, long l);
7648 ('funcp' generates a DW_TAG_subroutine_type DIE)
7651 static struct type *
7652 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7654 struct type *type; /* Type that this function returns */
7655 struct type *ftype; /* Function that returns above type */
7656 struct attribute *attr;
7658 type = die_type (die, cu);
7660 /* The die_type call above may have already set the type for this DIE. */
7661 ftype = get_die_type (die, cu);
7665 ftype = lookup_function_type (type);
7667 /* All functions in C++, Pascal and Java have prototypes. */
7668 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7669 if ((attr && (DW_UNSND (attr) != 0))
7670 || cu->language == language_cplus
7671 || cu->language == language_java
7672 || cu->language == language_pascal)
7673 TYPE_PROTOTYPED (ftype) = 1;
7674 else if (producer_is_realview (cu->producer))
7675 /* RealView does not emit DW_AT_prototyped. We can not
7676 distinguish prototyped and unprototyped functions; default to
7677 prototyped, since that is more common in modern code (and
7678 RealView warns about unprototyped functions). */
7679 TYPE_PROTOTYPED (ftype) = 1;
7681 /* Store the calling convention in the type if it's available in
7682 the subroutine die. Otherwise set the calling convention to
7683 the default value DW_CC_normal. */
7684 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7685 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7687 /* We need to add the subroutine type to the die immediately so
7688 we don't infinitely recurse when dealing with parameters
7689 declared as the same subroutine type. */
7690 set_die_type (die, ftype, cu);
7692 if (die->child != NULL)
7694 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7695 struct die_info *child_die;
7696 int nparams, iparams;
7698 /* Count the number of parameters.
7699 FIXME: GDB currently ignores vararg functions, but knows about
7700 vararg member functions. */
7702 child_die = die->child;
7703 while (child_die && child_die->tag)
7705 if (child_die->tag == DW_TAG_formal_parameter)
7707 else if (child_die->tag == DW_TAG_unspecified_parameters)
7708 TYPE_VARARGS (ftype) = 1;
7709 child_die = sibling_die (child_die);
7712 /* Allocate storage for parameters and fill them in. */
7713 TYPE_NFIELDS (ftype) = nparams;
7714 TYPE_FIELDS (ftype) = (struct field *)
7715 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7717 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7718 even if we error out during the parameters reading below. */
7719 for (iparams = 0; iparams < nparams; iparams++)
7720 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7723 child_die = die->child;
7724 while (child_die && child_die->tag)
7726 if (child_die->tag == DW_TAG_formal_parameter)
7728 struct type *arg_type;
7730 /* DWARF version 2 has no clean way to discern C++
7731 static and non-static member functions. G++ helps
7732 GDB by marking the first parameter for non-static
7733 member functions (which is the this pointer) as
7734 artificial. We pass this information to
7735 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7737 DWARF version 3 added DW_AT_object_pointer, which GCC
7738 4.5 does not yet generate. */
7739 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7741 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7744 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7746 /* GCC/43521: In java, the formal parameter
7747 "this" is sometimes not marked with DW_AT_artificial. */
7748 if (cu->language == language_java)
7750 const char *name = dwarf2_name (child_die, cu);
7752 if (name && !strcmp (name, "this"))
7753 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7756 arg_type = die_type (child_die, cu);
7758 /* RealView does not mark THIS as const, which the testsuite
7759 expects. GCC marks THIS as const in method definitions,
7760 but not in the class specifications (GCC PR 43053). */
7761 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7762 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7765 struct dwarf2_cu *arg_cu = cu;
7766 const char *name = dwarf2_name (child_die, cu);
7768 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7771 /* If the compiler emits this, use it. */
7772 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7775 else if (name && strcmp (name, "this") == 0)
7776 /* Function definitions will have the argument names. */
7778 else if (name == NULL && iparams == 0)
7779 /* Declarations may not have the names, so like
7780 elsewhere in GDB, assume an artificial first
7781 argument is "this". */
7785 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7789 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7792 child_die = sibling_die (child_die);
7799 static struct type *
7800 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7802 struct objfile *objfile = cu->objfile;
7803 const char *name = NULL;
7804 struct type *this_type;
7806 name = dwarf2_full_name (NULL, die, cu);
7807 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7808 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7809 TYPE_NAME (this_type) = (char *) name;
7810 set_die_type (die, this_type, cu);
7811 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7815 /* Find a representation of a given base type and install
7816 it in the TYPE field of the die. */
7818 static struct type *
7819 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7821 struct objfile *objfile = cu->objfile;
7823 struct attribute *attr;
7824 int encoding = 0, size = 0;
7826 enum type_code code = TYPE_CODE_INT;
7828 struct type *target_type = NULL;
7830 attr = dwarf2_attr (die, DW_AT_encoding, cu);
7833 encoding = DW_UNSND (attr);
7835 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7838 size = DW_UNSND (attr);
7840 name = dwarf2_name (die, cu);
7843 complaint (&symfile_complaints,
7844 _("DW_AT_name missing from DW_TAG_base_type"));
7849 case DW_ATE_address:
7850 /* Turn DW_ATE_address into a void * pointer. */
7851 code = TYPE_CODE_PTR;
7852 type_flags |= TYPE_FLAG_UNSIGNED;
7853 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7855 case DW_ATE_boolean:
7856 code = TYPE_CODE_BOOL;
7857 type_flags |= TYPE_FLAG_UNSIGNED;
7859 case DW_ATE_complex_float:
7860 code = TYPE_CODE_COMPLEX;
7861 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7863 case DW_ATE_decimal_float:
7864 code = TYPE_CODE_DECFLOAT;
7867 code = TYPE_CODE_FLT;
7871 case DW_ATE_unsigned:
7872 type_flags |= TYPE_FLAG_UNSIGNED;
7874 case DW_ATE_signed_char:
7875 if (cu->language == language_ada || cu->language == language_m2
7876 || cu->language == language_pascal)
7877 code = TYPE_CODE_CHAR;
7879 case DW_ATE_unsigned_char:
7880 if (cu->language == language_ada || cu->language == language_m2
7881 || cu->language == language_pascal)
7882 code = TYPE_CODE_CHAR;
7883 type_flags |= TYPE_FLAG_UNSIGNED;
7886 /* We just treat this as an integer and then recognize the
7887 type by name elsewhere. */
7891 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7892 dwarf_type_encoding_name (encoding));
7896 type = init_type (code, size, type_flags, NULL, objfile);
7897 TYPE_NAME (type) = name;
7898 TYPE_TARGET_TYPE (type) = target_type;
7900 if (name && strcmp (name, "char") == 0)
7901 TYPE_NOSIGN (type) = 1;
7903 return set_die_type (die, type, cu);
7906 /* Read the given DW_AT_subrange DIE. */
7908 static struct type *
7909 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7911 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7912 struct type *base_type;
7913 struct type *range_type;
7914 struct attribute *attr;
7918 LONGEST negative_mask;
7920 base_type = die_type (die, cu);
7921 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7922 check_typedef (base_type);
7924 /* The die_type call above may have already set the type for this DIE. */
7925 range_type = get_die_type (die, cu);
7929 if (cu->language == language_fortran)
7931 /* FORTRAN implies a lower bound of 1, if not given. */
7935 /* FIXME: For variable sized arrays either of these could be
7936 a variable rather than a constant value. We'll allow it,
7937 but we don't know how to handle it. */
7938 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7940 low = dwarf2_get_attr_constant_value (attr, 0);
7942 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7945 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7947 /* GCC encodes arrays with unspecified or dynamic length
7948 with a DW_FORM_block1 attribute or a reference attribute.
7949 FIXME: GDB does not yet know how to handle dynamic
7950 arrays properly, treat them as arrays with unspecified
7953 FIXME: jimb/2003-09-22: GDB does not really know
7954 how to handle arrays of unspecified length
7955 either; we just represent them as zero-length
7956 arrays. Choose an appropriate upper bound given
7957 the lower bound we've computed above. */
7961 high = dwarf2_get_attr_constant_value (attr, 1);
7965 attr = dwarf2_attr (die, DW_AT_count, cu);
7968 int count = dwarf2_get_attr_constant_value (attr, 1);
7969 high = low + count - 1;
7973 /* Dwarf-2 specifications explicitly allows to create subrange types
7974 without specifying a base type.
7975 In that case, the base type must be set to the type of
7976 the lower bound, upper bound or count, in that order, if any of these
7977 three attributes references an object that has a type.
7978 If no base type is found, the Dwarf-2 specifications say that
7979 a signed integer type of size equal to the size of an address should
7981 For the following C code: `extern char gdb_int [];'
7982 GCC produces an empty range DIE.
7983 FIXME: muller/2010-05-28: Possible references to object for low bound,
7984 high bound or count are not yet handled by this code.
7986 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7988 struct objfile *objfile = cu->objfile;
7989 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7990 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7991 struct type *int_type = objfile_type (objfile)->builtin_int;
7993 /* Test "int", "long int", and "long long int" objfile types,
7994 and select the first one having a size above or equal to the
7995 architecture address size. */
7996 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7997 base_type = int_type;
8000 int_type = objfile_type (objfile)->builtin_long;
8001 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8002 base_type = int_type;
8005 int_type = objfile_type (objfile)->builtin_long_long;
8006 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8007 base_type = int_type;
8013 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8014 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8015 low |= negative_mask;
8016 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8017 high |= negative_mask;
8019 range_type = create_range_type (NULL, base_type, low, high);
8021 /* Mark arrays with dynamic length at least as an array of unspecified
8022 length. GDB could check the boundary but before it gets implemented at
8023 least allow accessing the array elements. */
8024 if (attr && attr->form == DW_FORM_block1)
8025 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8027 name = dwarf2_name (die, cu);
8029 TYPE_NAME (range_type) = name;
8031 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8033 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8035 set_die_type (die, range_type, cu);
8037 /* set_die_type should be already done. */
8038 set_descriptive_type (range_type, die, cu);
8043 static struct type *
8044 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8048 /* For now, we only support the C meaning of an unspecified type: void. */
8050 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8051 TYPE_NAME (type) = dwarf2_name (die, cu);
8053 return set_die_type (die, type, cu);
8056 /* Trivial hash function for die_info: the hash value of a DIE
8057 is its offset in .debug_info for this objfile. */
8060 die_hash (const void *item)
8062 const struct die_info *die = item;
8067 /* Trivial comparison function for die_info structures: two DIEs
8068 are equal if they have the same offset. */
8071 die_eq (const void *item_lhs, const void *item_rhs)
8073 const struct die_info *die_lhs = item_lhs;
8074 const struct die_info *die_rhs = item_rhs;
8076 return die_lhs->offset == die_rhs->offset;
8079 /* Read a whole compilation unit into a linked list of dies. */
8081 static struct die_info *
8082 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8084 struct die_reader_specs reader_specs;
8085 int read_abbrevs = 0;
8086 struct cleanup *back_to = NULL;
8087 struct die_info *die;
8089 if (cu->dwarf2_abbrevs == NULL)
8091 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8092 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8096 gdb_assert (cu->die_hash == NULL);
8098 = htab_create_alloc_ex (cu->header.length / 12,
8102 &cu->comp_unit_obstack,
8103 hashtab_obstack_allocate,
8104 dummy_obstack_deallocate);
8106 init_cu_die_reader (&reader_specs, cu);
8108 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8111 do_cleanups (back_to);
8116 /* Main entry point for reading a DIE and all children.
8117 Read the DIE and dump it if requested. */
8119 static struct die_info *
8120 read_die_and_children (const struct die_reader_specs *reader,
8122 gdb_byte **new_info_ptr,
8123 struct die_info *parent)
8125 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8126 new_info_ptr, parent);
8128 if (dwarf2_die_debug)
8130 fprintf_unfiltered (gdb_stdlog,
8131 "\nRead die from %s of %s:\n",
8132 reader->buffer == dwarf2_per_objfile->info.buffer
8134 : reader->buffer == dwarf2_per_objfile->types.buffer
8136 : "unknown section",
8137 reader->abfd->filename);
8138 dump_die (result, dwarf2_die_debug);
8144 /* Read a single die and all its descendents. Set the die's sibling
8145 field to NULL; set other fields in the die correctly, and set all
8146 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8147 location of the info_ptr after reading all of those dies. PARENT
8148 is the parent of the die in question. */
8150 static struct die_info *
8151 read_die_and_children_1 (const struct die_reader_specs *reader,
8153 gdb_byte **new_info_ptr,
8154 struct die_info *parent)
8156 struct die_info *die;
8160 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8163 *new_info_ptr = cur_ptr;
8166 store_in_ref_table (die, reader->cu);
8169 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8173 *new_info_ptr = cur_ptr;
8176 die->sibling = NULL;
8177 die->parent = parent;
8181 /* Read a die, all of its descendents, and all of its siblings; set
8182 all of the fields of all of the dies correctly. Arguments are as
8183 in read_die_and_children. */
8185 static struct die_info *
8186 read_die_and_siblings (const struct die_reader_specs *reader,
8188 gdb_byte **new_info_ptr,
8189 struct die_info *parent)
8191 struct die_info *first_die, *last_sibling;
8195 first_die = last_sibling = NULL;
8199 struct die_info *die
8200 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8204 *new_info_ptr = cur_ptr;
8211 last_sibling->sibling = die;
8217 /* Read the die from the .debug_info section buffer. Set DIEP to
8218 point to a newly allocated die with its information, except for its
8219 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8220 whether the die has children or not. */
8223 read_full_die (const struct die_reader_specs *reader,
8224 struct die_info **diep, gdb_byte *info_ptr,
8227 unsigned int abbrev_number, bytes_read, i, offset;
8228 struct abbrev_info *abbrev;
8229 struct die_info *die;
8230 struct dwarf2_cu *cu = reader->cu;
8231 bfd *abfd = reader->abfd;
8233 offset = info_ptr - reader->buffer;
8234 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8235 info_ptr += bytes_read;
8243 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8245 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8247 bfd_get_filename (abfd));
8249 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8250 die->offset = offset;
8251 die->tag = abbrev->tag;
8252 die->abbrev = abbrev_number;
8254 die->num_attrs = abbrev->num_attrs;
8256 for (i = 0; i < abbrev->num_attrs; ++i)
8257 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8258 abfd, info_ptr, cu);
8261 *has_children = abbrev->has_children;
8265 /* In DWARF version 2, the description of the debugging information is
8266 stored in a separate .debug_abbrev section. Before we read any
8267 dies from a section we read in all abbreviations and install them
8268 in a hash table. This function also sets flags in CU describing
8269 the data found in the abbrev table. */
8272 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8274 struct comp_unit_head *cu_header = &cu->header;
8275 gdb_byte *abbrev_ptr;
8276 struct abbrev_info *cur_abbrev;
8277 unsigned int abbrev_number, bytes_read, abbrev_name;
8278 unsigned int abbrev_form, hash_number;
8279 struct attr_abbrev *cur_attrs;
8280 unsigned int allocated_attrs;
8282 /* Initialize dwarf2 abbrevs */
8283 obstack_init (&cu->abbrev_obstack);
8284 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8286 * sizeof (struct abbrev_info *)));
8287 memset (cu->dwarf2_abbrevs, 0,
8288 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8290 dwarf2_read_section (dwarf2_per_objfile->objfile,
8291 &dwarf2_per_objfile->abbrev);
8292 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8293 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8294 abbrev_ptr += bytes_read;
8296 allocated_attrs = ATTR_ALLOC_CHUNK;
8297 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8299 /* loop until we reach an abbrev number of 0 */
8300 while (abbrev_number)
8302 cur_abbrev = dwarf_alloc_abbrev (cu);
8304 /* read in abbrev header */
8305 cur_abbrev->number = abbrev_number;
8306 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8307 abbrev_ptr += bytes_read;
8308 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8311 if (cur_abbrev->tag == DW_TAG_namespace)
8312 cu->has_namespace_info = 1;
8314 /* now read in declarations */
8315 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8316 abbrev_ptr += bytes_read;
8317 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8318 abbrev_ptr += bytes_read;
8321 if (cur_abbrev->num_attrs == allocated_attrs)
8323 allocated_attrs += ATTR_ALLOC_CHUNK;
8325 = xrealloc (cur_attrs, (allocated_attrs
8326 * sizeof (struct attr_abbrev)));
8329 /* Record whether this compilation unit might have
8330 inter-compilation-unit references. If we don't know what form
8331 this attribute will have, then it might potentially be a
8332 DW_FORM_ref_addr, so we conservatively expect inter-CU
8335 if (abbrev_form == DW_FORM_ref_addr
8336 || abbrev_form == DW_FORM_indirect)
8337 cu->has_form_ref_addr = 1;
8339 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8340 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8341 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8342 abbrev_ptr += bytes_read;
8343 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8344 abbrev_ptr += bytes_read;
8347 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8348 (cur_abbrev->num_attrs
8349 * sizeof (struct attr_abbrev)));
8350 memcpy (cur_abbrev->attrs, cur_attrs,
8351 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8353 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8354 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8355 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8357 /* Get next abbreviation.
8358 Under Irix6 the abbreviations for a compilation unit are not
8359 always properly terminated with an abbrev number of 0.
8360 Exit loop if we encounter an abbreviation which we have
8361 already read (which means we are about to read the abbreviations
8362 for the next compile unit) or if the end of the abbreviation
8363 table is reached. */
8364 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8365 >= dwarf2_per_objfile->abbrev.size)
8367 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8368 abbrev_ptr += bytes_read;
8369 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8376 /* Release the memory used by the abbrev table for a compilation unit. */
8379 dwarf2_free_abbrev_table (void *ptr_to_cu)
8381 struct dwarf2_cu *cu = ptr_to_cu;
8383 obstack_free (&cu->abbrev_obstack, NULL);
8384 cu->dwarf2_abbrevs = NULL;
8387 /* Lookup an abbrev_info structure in the abbrev hash table. */
8389 static struct abbrev_info *
8390 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8392 unsigned int hash_number;
8393 struct abbrev_info *abbrev;
8395 hash_number = number % ABBREV_HASH_SIZE;
8396 abbrev = cu->dwarf2_abbrevs[hash_number];
8400 if (abbrev->number == number)
8403 abbrev = abbrev->next;
8408 /* Returns nonzero if TAG represents a type that we might generate a partial
8412 is_type_tag_for_partial (int tag)
8417 /* Some types that would be reasonable to generate partial symbols for,
8418 that we don't at present. */
8419 case DW_TAG_array_type:
8420 case DW_TAG_file_type:
8421 case DW_TAG_ptr_to_member_type:
8422 case DW_TAG_set_type:
8423 case DW_TAG_string_type:
8424 case DW_TAG_subroutine_type:
8426 case DW_TAG_base_type:
8427 case DW_TAG_class_type:
8428 case DW_TAG_interface_type:
8429 case DW_TAG_enumeration_type:
8430 case DW_TAG_structure_type:
8431 case DW_TAG_subrange_type:
8432 case DW_TAG_typedef:
8433 case DW_TAG_union_type:
8440 /* Load all DIEs that are interesting for partial symbols into memory. */
8442 static struct partial_die_info *
8443 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8444 int building_psymtab, struct dwarf2_cu *cu)
8446 struct partial_die_info *part_die;
8447 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8448 struct abbrev_info *abbrev;
8449 unsigned int bytes_read;
8450 unsigned int load_all = 0;
8452 int nesting_level = 1;
8457 if (cu->per_cu && cu->per_cu->load_all_dies)
8461 = htab_create_alloc_ex (cu->header.length / 12,
8465 &cu->comp_unit_obstack,
8466 hashtab_obstack_allocate,
8467 dummy_obstack_deallocate);
8469 part_die = obstack_alloc (&cu->comp_unit_obstack,
8470 sizeof (struct partial_die_info));
8474 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8476 /* A NULL abbrev means the end of a series of children. */
8479 if (--nesting_level == 0)
8481 /* PART_DIE was probably the last thing allocated on the
8482 comp_unit_obstack, so we could call obstack_free
8483 here. We don't do that because the waste is small,
8484 and will be cleaned up when we're done with this
8485 compilation unit. This way, we're also more robust
8486 against other users of the comp_unit_obstack. */
8489 info_ptr += bytes_read;
8490 last_die = parent_die;
8491 parent_die = parent_die->die_parent;
8495 /* Check for template arguments. We never save these; if
8496 they're seen, we just mark the parent, and go on our way. */
8497 if (parent_die != NULL
8498 && cu->language == language_cplus
8499 && (abbrev->tag == DW_TAG_template_type_param
8500 || abbrev->tag == DW_TAG_template_value_param))
8502 parent_die->has_template_arguments = 1;
8506 /* We don't need a partial DIE for the template argument. */
8507 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8513 /* We only recurse into subprograms looking for template arguments.
8514 Skip their other children. */
8516 && cu->language == language_cplus
8517 && parent_die != NULL
8518 && parent_die->tag == DW_TAG_subprogram)
8520 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8524 /* Check whether this DIE is interesting enough to save. Normally
8525 we would not be interested in members here, but there may be
8526 later variables referencing them via DW_AT_specification (for
8529 && !is_type_tag_for_partial (abbrev->tag)
8530 && abbrev->tag != DW_TAG_enumerator
8531 && abbrev->tag != DW_TAG_subprogram
8532 && abbrev->tag != DW_TAG_lexical_block
8533 && abbrev->tag != DW_TAG_variable
8534 && abbrev->tag != DW_TAG_namespace
8535 && abbrev->tag != DW_TAG_module
8536 && abbrev->tag != DW_TAG_member)
8538 /* Otherwise we skip to the next sibling, if any. */
8539 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8543 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8544 buffer, info_ptr, cu);
8546 /* This two-pass algorithm for processing partial symbols has a
8547 high cost in cache pressure. Thus, handle some simple cases
8548 here which cover the majority of C partial symbols. DIEs
8549 which neither have specification tags in them, nor could have
8550 specification tags elsewhere pointing at them, can simply be
8551 processed and discarded.
8553 This segment is also optional; scan_partial_symbols and
8554 add_partial_symbol will handle these DIEs if we chain
8555 them in normally. When compilers which do not emit large
8556 quantities of duplicate debug information are more common,
8557 this code can probably be removed. */
8559 /* Any complete simple types at the top level (pretty much all
8560 of them, for a language without namespaces), can be processed
8562 if (parent_die == NULL
8563 && part_die->has_specification == 0
8564 && part_die->is_declaration == 0
8565 && (part_die->tag == DW_TAG_typedef
8566 || part_die->tag == DW_TAG_base_type
8567 || part_die->tag == DW_TAG_subrange_type))
8569 if (building_psymtab && part_die->name != NULL)
8570 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8571 VAR_DOMAIN, LOC_TYPEDEF,
8572 &cu->objfile->static_psymbols,
8573 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8574 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8578 /* If we're at the second level, and we're an enumerator, and
8579 our parent has no specification (meaning possibly lives in a
8580 namespace elsewhere), then we can add the partial symbol now
8581 instead of queueing it. */
8582 if (part_die->tag == DW_TAG_enumerator
8583 && parent_die != NULL
8584 && parent_die->die_parent == NULL
8585 && parent_die->tag == DW_TAG_enumeration_type
8586 && parent_die->has_specification == 0)
8588 if (part_die->name == NULL)
8589 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8590 else if (building_psymtab)
8591 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8592 VAR_DOMAIN, LOC_CONST,
8593 (cu->language == language_cplus
8594 || cu->language == language_java)
8595 ? &cu->objfile->global_psymbols
8596 : &cu->objfile->static_psymbols,
8597 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8599 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8603 /* We'll save this DIE so link it in. */
8604 part_die->die_parent = parent_die;
8605 part_die->die_sibling = NULL;
8606 part_die->die_child = NULL;
8608 if (last_die && last_die == parent_die)
8609 last_die->die_child = part_die;
8611 last_die->die_sibling = part_die;
8613 last_die = part_die;
8615 if (first_die == NULL)
8616 first_die = part_die;
8618 /* Maybe add the DIE to the hash table. Not all DIEs that we
8619 find interesting need to be in the hash table, because we
8620 also have the parent/sibling/child chains; only those that we
8621 might refer to by offset later during partial symbol reading.
8623 For now this means things that might have be the target of a
8624 DW_AT_specification, DW_AT_abstract_origin, or
8625 DW_AT_extension. DW_AT_extension will refer only to
8626 namespaces; DW_AT_abstract_origin refers to functions (and
8627 many things under the function DIE, but we do not recurse
8628 into function DIEs during partial symbol reading) and
8629 possibly variables as well; DW_AT_specification refers to
8630 declarations. Declarations ought to have the DW_AT_declaration
8631 flag. It happens that GCC forgets to put it in sometimes, but
8632 only for functions, not for types.
8634 Adding more things than necessary to the hash table is harmless
8635 except for the performance cost. Adding too few will result in
8636 wasted time in find_partial_die, when we reread the compilation
8637 unit with load_all_dies set. */
8640 || abbrev->tag == DW_TAG_subprogram
8641 || abbrev->tag == DW_TAG_variable
8642 || abbrev->tag == DW_TAG_namespace
8643 || part_die->is_declaration)
8647 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8648 part_die->offset, INSERT);
8652 part_die = obstack_alloc (&cu->comp_unit_obstack,
8653 sizeof (struct partial_die_info));
8655 /* For some DIEs we want to follow their children (if any). For C
8656 we have no reason to follow the children of structures; for other
8657 languages we have to, so that we can get at method physnames
8658 to infer fully qualified class names, for DW_AT_specification,
8659 and for C++ template arguments. For C++, we also look one level
8660 inside functions to find template arguments (if the name of the
8661 function does not already contain the template arguments).
8663 For Ada, we need to scan the children of subprograms and lexical
8664 blocks as well because Ada allows the definition of nested
8665 entities that could be interesting for the debugger, such as
8666 nested subprograms for instance. */
8667 if (last_die->has_children
8669 || last_die->tag == DW_TAG_namespace
8670 || last_die->tag == DW_TAG_module
8671 || last_die->tag == DW_TAG_enumeration_type
8672 || (cu->language == language_cplus
8673 && last_die->tag == DW_TAG_subprogram
8674 && (last_die->name == NULL
8675 || strchr (last_die->name, '<') == NULL))
8676 || (cu->language != language_c
8677 && (last_die->tag == DW_TAG_class_type
8678 || last_die->tag == DW_TAG_interface_type
8679 || last_die->tag == DW_TAG_structure_type
8680 || last_die->tag == DW_TAG_union_type))
8681 || (cu->language == language_ada
8682 && (last_die->tag == DW_TAG_subprogram
8683 || last_die->tag == DW_TAG_lexical_block))))
8686 parent_die = last_die;
8690 /* Otherwise we skip to the next sibling, if any. */
8691 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8693 /* Back to the top, do it again. */
8697 /* Read a minimal amount of information into the minimal die structure. */
8700 read_partial_die (struct partial_die_info *part_die,
8701 struct abbrev_info *abbrev,
8702 unsigned int abbrev_len, bfd *abfd,
8703 gdb_byte *buffer, gdb_byte *info_ptr,
8704 struct dwarf2_cu *cu)
8707 struct attribute attr;
8708 int has_low_pc_attr = 0;
8709 int has_high_pc_attr = 0;
8711 memset (part_die, 0, sizeof (struct partial_die_info));
8713 part_die->offset = info_ptr - buffer;
8715 info_ptr += abbrev_len;
8720 part_die->tag = abbrev->tag;
8721 part_die->has_children = abbrev->has_children;
8723 for (i = 0; i < abbrev->num_attrs; ++i)
8725 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8727 /* Store the data if it is of an attribute we want to keep in a
8728 partial symbol table. */
8732 switch (part_die->tag)
8734 case DW_TAG_compile_unit:
8735 case DW_TAG_type_unit:
8736 /* Compilation units have a DW_AT_name that is a filename, not
8737 a source language identifier. */
8738 case DW_TAG_enumeration_type:
8739 case DW_TAG_enumerator:
8740 /* These tags always have simple identifiers already; no need
8741 to canonicalize them. */
8742 part_die->name = DW_STRING (&attr);
8746 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8747 &cu->objfile->objfile_obstack);
8751 case DW_AT_linkage_name:
8752 case DW_AT_MIPS_linkage_name:
8753 /* Note that both forms of linkage name might appear. We
8754 assume they will be the same, and we only store the last
8756 if (cu->language == language_ada)
8757 part_die->name = DW_STRING (&attr);
8760 has_low_pc_attr = 1;
8761 part_die->lowpc = DW_ADDR (&attr);
8764 has_high_pc_attr = 1;
8765 part_die->highpc = DW_ADDR (&attr);
8767 case DW_AT_location:
8768 /* Support the .debug_loc offsets */
8769 if (attr_form_is_block (&attr))
8771 part_die->locdesc = DW_BLOCK (&attr);
8773 else if (attr_form_is_section_offset (&attr))
8775 dwarf2_complex_location_expr_complaint ();
8779 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8780 "partial symbol information");
8783 case DW_AT_external:
8784 part_die->is_external = DW_UNSND (&attr);
8786 case DW_AT_declaration:
8787 part_die->is_declaration = DW_UNSND (&attr);
8790 part_die->has_type = 1;
8792 case DW_AT_abstract_origin:
8793 case DW_AT_specification:
8794 case DW_AT_extension:
8795 part_die->has_specification = 1;
8796 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8799 /* Ignore absolute siblings, they might point outside of
8800 the current compile unit. */
8801 if (attr.form == DW_FORM_ref_addr)
8802 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8804 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8806 case DW_AT_byte_size:
8807 part_die->has_byte_size = 1;
8809 case DW_AT_calling_convention:
8810 /* DWARF doesn't provide a way to identify a program's source-level
8811 entry point. DW_AT_calling_convention attributes are only meant
8812 to describe functions' calling conventions.
8814 However, because it's a necessary piece of information in
8815 Fortran, and because DW_CC_program is the only piece of debugging
8816 information whose definition refers to a 'main program' at all,
8817 several compilers have begun marking Fortran main programs with
8818 DW_CC_program --- even when those functions use the standard
8819 calling conventions.
8821 So until DWARF specifies a way to provide this information and
8822 compilers pick up the new representation, we'll support this
8824 if (DW_UNSND (&attr) == DW_CC_program
8825 && cu->language == language_fortran)
8826 set_main_name (part_die->name);
8833 /* When using the GNU linker, .gnu.linkonce. sections are used to
8834 eliminate duplicate copies of functions and vtables and such.
8835 The linker will arbitrarily choose one and discard the others.
8836 The AT_*_pc values for such functions refer to local labels in
8837 these sections. If the section from that file was discarded, the
8838 labels are not in the output, so the relocs get a value of 0.
8839 If this is a discarded function, mark the pc bounds as invalid,
8840 so that GDB will ignore it. */
8841 if (has_low_pc_attr && has_high_pc_attr
8842 && part_die->lowpc < part_die->highpc
8843 && (part_die->lowpc != 0
8844 || dwarf2_per_objfile->has_section_at_zero))
8845 part_die->has_pc_info = 1;
8850 /* Find a cached partial DIE at OFFSET in CU. */
8852 static struct partial_die_info *
8853 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8855 struct partial_die_info *lookup_die = NULL;
8856 struct partial_die_info part_die;
8858 part_die.offset = offset;
8859 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8864 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8865 except in the case of .debug_types DIEs which do not reference
8866 outside their CU (they do however referencing other types via
8869 static struct partial_die_info *
8870 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8872 struct dwarf2_per_cu_data *per_cu = NULL;
8873 struct partial_die_info *pd = NULL;
8875 if (cu->per_cu->from_debug_types)
8877 pd = find_partial_die_in_comp_unit (offset, cu);
8883 if (offset_in_cu_p (&cu->header, offset))
8885 pd = find_partial_die_in_comp_unit (offset, cu);
8890 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8892 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8893 load_partial_comp_unit (per_cu, cu->objfile);
8895 per_cu->cu->last_used = 0;
8896 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8898 if (pd == NULL && per_cu->load_all_dies == 0)
8900 struct cleanup *back_to;
8901 struct partial_die_info comp_unit_die;
8902 struct abbrev_info *abbrev;
8903 unsigned int bytes_read;
8906 per_cu->load_all_dies = 1;
8908 /* Re-read the DIEs. */
8909 back_to = make_cleanup (null_cleanup, 0);
8910 if (per_cu->cu->dwarf2_abbrevs == NULL)
8912 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8913 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8915 info_ptr = (dwarf2_per_objfile->info.buffer
8916 + per_cu->cu->header.offset
8917 + per_cu->cu->header.first_die_offset);
8918 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8919 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8920 per_cu->cu->objfile->obfd,
8921 dwarf2_per_objfile->info.buffer, info_ptr,
8923 if (comp_unit_die.has_children)
8924 load_partial_dies (per_cu->cu->objfile->obfd,
8925 dwarf2_per_objfile->info.buffer, info_ptr,
8927 do_cleanups (back_to);
8929 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8935 internal_error (__FILE__, __LINE__,
8936 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8937 offset, bfd_get_filename (cu->objfile->obfd));
8941 /* Adjust PART_DIE before generating a symbol for it. This function
8942 may set the is_external flag or change the DIE's name. */
8945 fixup_partial_die (struct partial_die_info *part_die,
8946 struct dwarf2_cu *cu)
8948 /* If we found a reference attribute and the DIE has no name, try
8949 to find a name in the referred to DIE. */
8951 if (part_die->name == NULL && part_die->has_specification)
8953 struct partial_die_info *spec_die;
8955 spec_die = find_partial_die (part_die->spec_offset, cu);
8957 fixup_partial_die (spec_die, cu);
8961 part_die->name = spec_die->name;
8963 /* Copy DW_AT_external attribute if it is set. */
8964 if (spec_die->is_external)
8965 part_die->is_external = spec_die->is_external;
8969 /* Set default names for some unnamed DIEs. */
8970 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8971 || part_die->tag == DW_TAG_class_type))
8972 part_die->name = "(anonymous class)";
8974 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8975 part_die->name = "(anonymous namespace)";
8977 if (part_die->tag == DW_TAG_structure_type
8978 || part_die->tag == DW_TAG_class_type
8979 || part_die->tag == DW_TAG_union_type)
8980 guess_structure_name (part_die, cu);
8983 /* Read an attribute value described by an attribute form. */
8986 read_attribute_value (struct attribute *attr, unsigned form,
8987 bfd *abfd, gdb_byte *info_ptr,
8988 struct dwarf2_cu *cu)
8990 struct comp_unit_head *cu_header = &cu->header;
8991 unsigned int bytes_read;
8992 struct dwarf_block *blk;
8997 case DW_FORM_ref_addr:
8998 if (cu->header.version == 2)
8999 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9001 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9002 info_ptr += bytes_read;
9005 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9006 info_ptr += bytes_read;
9008 case DW_FORM_block2:
9009 blk = dwarf_alloc_block (cu);
9010 blk->size = read_2_bytes (abfd, info_ptr);
9012 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9013 info_ptr += blk->size;
9014 DW_BLOCK (attr) = blk;
9016 case DW_FORM_block4:
9017 blk = dwarf_alloc_block (cu);
9018 blk->size = read_4_bytes (abfd, info_ptr);
9020 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9021 info_ptr += blk->size;
9022 DW_BLOCK (attr) = blk;
9025 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9029 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9033 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9036 case DW_FORM_sec_offset:
9037 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9038 info_ptr += bytes_read;
9040 case DW_FORM_string:
9041 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9042 DW_STRING_IS_CANONICAL (attr) = 0;
9043 info_ptr += bytes_read;
9046 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9048 DW_STRING_IS_CANONICAL (attr) = 0;
9049 info_ptr += bytes_read;
9051 case DW_FORM_exprloc:
9053 blk = dwarf_alloc_block (cu);
9054 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9055 info_ptr += bytes_read;
9056 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9057 info_ptr += blk->size;
9058 DW_BLOCK (attr) = blk;
9060 case DW_FORM_block1:
9061 blk = dwarf_alloc_block (cu);
9062 blk->size = read_1_byte (abfd, info_ptr);
9064 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9065 info_ptr += blk->size;
9066 DW_BLOCK (attr) = blk;
9069 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9073 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9076 case DW_FORM_flag_present:
9077 DW_UNSND (attr) = 1;
9080 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9081 info_ptr += bytes_read;
9084 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9085 info_ptr += bytes_read;
9088 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9092 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9096 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9100 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9104 /* Convert the signature to something we can record in DW_UNSND
9106 NOTE: This is NULL if the type wasn't found. */
9107 DW_SIGNATURED_TYPE (attr) =
9108 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9111 case DW_FORM_ref_udata:
9112 DW_ADDR (attr) = (cu->header.offset
9113 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9114 info_ptr += bytes_read;
9116 case DW_FORM_indirect:
9117 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9118 info_ptr += bytes_read;
9119 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9122 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9123 dwarf_form_name (form),
9124 bfd_get_filename (abfd));
9127 /* We have seen instances where the compiler tried to emit a byte
9128 size attribute of -1 which ended up being encoded as an unsigned
9129 0xffffffff. Although 0xffffffff is technically a valid size value,
9130 an object of this size seems pretty unlikely so we can relatively
9131 safely treat these cases as if the size attribute was invalid and
9132 treat them as zero by default. */
9133 if (attr->name == DW_AT_byte_size
9134 && form == DW_FORM_data4
9135 && DW_UNSND (attr) >= 0xffffffff)
9138 (&symfile_complaints,
9139 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9140 hex_string (DW_UNSND (attr)));
9141 DW_UNSND (attr) = 0;
9147 /* Read an attribute described by an abbreviated attribute. */
9150 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9151 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9153 attr->name = abbrev->name;
9154 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9157 /* read dwarf information from a buffer */
9160 read_1_byte (bfd *abfd, gdb_byte *buf)
9162 return bfd_get_8 (abfd, buf);
9166 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9168 return bfd_get_signed_8 (abfd, buf);
9172 read_2_bytes (bfd *abfd, gdb_byte *buf)
9174 return bfd_get_16 (abfd, buf);
9178 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9180 return bfd_get_signed_16 (abfd, buf);
9184 read_4_bytes (bfd *abfd, gdb_byte *buf)
9186 return bfd_get_32 (abfd, buf);
9190 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9192 return bfd_get_signed_32 (abfd, buf);
9196 read_8_bytes (bfd *abfd, gdb_byte *buf)
9198 return bfd_get_64 (abfd, buf);
9202 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9203 unsigned int *bytes_read)
9205 struct comp_unit_head *cu_header = &cu->header;
9206 CORE_ADDR retval = 0;
9208 if (cu_header->signed_addr_p)
9210 switch (cu_header->addr_size)
9213 retval = bfd_get_signed_16 (abfd, buf);
9216 retval = bfd_get_signed_32 (abfd, buf);
9219 retval = bfd_get_signed_64 (abfd, buf);
9222 internal_error (__FILE__, __LINE__,
9223 _("read_address: bad switch, signed [in module %s]"),
9224 bfd_get_filename (abfd));
9229 switch (cu_header->addr_size)
9232 retval = bfd_get_16 (abfd, buf);
9235 retval = bfd_get_32 (abfd, buf);
9238 retval = bfd_get_64 (abfd, buf);
9241 internal_error (__FILE__, __LINE__,
9242 _("read_address: bad switch, unsigned [in module %s]"),
9243 bfd_get_filename (abfd));
9247 *bytes_read = cu_header->addr_size;
9251 /* Read the initial length from a section. The (draft) DWARF 3
9252 specification allows the initial length to take up either 4 bytes
9253 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9254 bytes describe the length and all offsets will be 8 bytes in length
9257 An older, non-standard 64-bit format is also handled by this
9258 function. The older format in question stores the initial length
9259 as an 8-byte quantity without an escape value. Lengths greater
9260 than 2^32 aren't very common which means that the initial 4 bytes
9261 is almost always zero. Since a length value of zero doesn't make
9262 sense for the 32-bit format, this initial zero can be considered to
9263 be an escape value which indicates the presence of the older 64-bit
9264 format. As written, the code can't detect (old format) lengths
9265 greater than 4GB. If it becomes necessary to handle lengths
9266 somewhat larger than 4GB, we could allow other small values (such
9267 as the non-sensical values of 1, 2, and 3) to also be used as
9268 escape values indicating the presence of the old format.
9270 The value returned via bytes_read should be used to increment the
9271 relevant pointer after calling read_initial_length().
9273 [ Note: read_initial_length() and read_offset() are based on the
9274 document entitled "DWARF Debugging Information Format", revision
9275 3, draft 8, dated November 19, 2001. This document was obtained
9278 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9280 This document is only a draft and is subject to change. (So beware.)
9282 Details regarding the older, non-standard 64-bit format were
9283 determined empirically by examining 64-bit ELF files produced by
9284 the SGI toolchain on an IRIX 6.5 machine.
9286 - Kevin, July 16, 2002
9290 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9292 LONGEST length = bfd_get_32 (abfd, buf);
9294 if (length == 0xffffffff)
9296 length = bfd_get_64 (abfd, buf + 4);
9299 else if (length == 0)
9301 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9302 length = bfd_get_64 (abfd, buf);
9313 /* Cover function for read_initial_length.
9314 Returns the length of the object at BUF, and stores the size of the
9315 initial length in *BYTES_READ and stores the size that offsets will be in
9317 If the initial length size is not equivalent to that specified in
9318 CU_HEADER then issue a complaint.
9319 This is useful when reading non-comp-unit headers. */
9322 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9323 const struct comp_unit_head *cu_header,
9324 unsigned int *bytes_read,
9325 unsigned int *offset_size)
9327 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9329 gdb_assert (cu_header->initial_length_size == 4
9330 || cu_header->initial_length_size == 8
9331 || cu_header->initial_length_size == 12);
9333 if (cu_header->initial_length_size != *bytes_read)
9334 complaint (&symfile_complaints,
9335 _("intermixed 32-bit and 64-bit DWARF sections"));
9337 *offset_size = (*bytes_read == 4) ? 4 : 8;
9341 /* Read an offset from the data stream. The size of the offset is
9342 given by cu_header->offset_size. */
9345 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9346 unsigned int *bytes_read)
9348 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9350 *bytes_read = cu_header->offset_size;
9354 /* Read an offset from the data stream. */
9357 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9361 switch (offset_size)
9364 retval = bfd_get_32 (abfd, buf);
9367 retval = bfd_get_64 (abfd, buf);
9370 internal_error (__FILE__, __LINE__,
9371 _("read_offset_1: bad switch [in module %s]"),
9372 bfd_get_filename (abfd));
9379 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9381 /* If the size of a host char is 8 bits, we can return a pointer
9382 to the buffer, otherwise we have to copy the data to a buffer
9383 allocated on the temporary obstack. */
9384 gdb_assert (HOST_CHAR_BIT == 8);
9389 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9391 /* If the size of a host char is 8 bits, we can return a pointer
9392 to the string, otherwise we have to copy the string to a buffer
9393 allocated on the temporary obstack. */
9394 gdb_assert (HOST_CHAR_BIT == 8);
9397 *bytes_read_ptr = 1;
9400 *bytes_read_ptr = strlen ((char *) buf) + 1;
9401 return (char *) buf;
9405 read_indirect_string (bfd *abfd, gdb_byte *buf,
9406 const struct comp_unit_head *cu_header,
9407 unsigned int *bytes_read_ptr)
9409 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9411 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9412 if (dwarf2_per_objfile->str.buffer == NULL)
9414 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9415 bfd_get_filename (abfd));
9418 if (str_offset >= dwarf2_per_objfile->str.size)
9420 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9421 bfd_get_filename (abfd));
9424 gdb_assert (HOST_CHAR_BIT == 8);
9425 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9427 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9430 static unsigned long
9431 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9433 unsigned long result;
9434 unsigned int num_read;
9444 byte = bfd_get_8 (abfd, buf);
9447 result |= ((unsigned long)(byte & 127) << shift);
9448 if ((byte & 128) == 0)
9454 *bytes_read_ptr = num_read;
9459 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9462 int i, shift, num_read;
9471 byte = bfd_get_8 (abfd, buf);
9474 result |= ((long)(byte & 127) << shift);
9476 if ((byte & 128) == 0)
9481 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9482 result |= -(((long)1) << shift);
9483 *bytes_read_ptr = num_read;
9487 /* Return a pointer to just past the end of an LEB128 number in BUF. */
9490 skip_leb128 (bfd *abfd, gdb_byte *buf)
9496 byte = bfd_get_8 (abfd, buf);
9498 if ((byte & 128) == 0)
9504 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9511 cu->language = language_c;
9513 case DW_LANG_C_plus_plus:
9514 cu->language = language_cplus;
9517 cu->language = language_d;
9519 case DW_LANG_Fortran77:
9520 case DW_LANG_Fortran90:
9521 case DW_LANG_Fortran95:
9522 cu->language = language_fortran;
9524 case DW_LANG_Mips_Assembler:
9525 cu->language = language_asm;
9528 cu->language = language_java;
9532 cu->language = language_ada;
9534 case DW_LANG_Modula2:
9535 cu->language = language_m2;
9537 case DW_LANG_Pascal83:
9538 cu->language = language_pascal;
9541 cu->language = language_objc;
9543 case DW_LANG_Cobol74:
9544 case DW_LANG_Cobol85:
9546 cu->language = language_minimal;
9549 cu->language_defn = language_def (cu->language);
9552 /* Return the named attribute or NULL if not there. */
9554 static struct attribute *
9555 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9558 struct attribute *spec = NULL;
9560 for (i = 0; i < die->num_attrs; ++i)
9562 if (die->attrs[i].name == name)
9563 return &die->attrs[i];
9564 if (die->attrs[i].name == DW_AT_specification
9565 || die->attrs[i].name == DW_AT_abstract_origin)
9566 spec = &die->attrs[i];
9571 die = follow_die_ref (die, spec, &cu);
9572 return dwarf2_attr (die, name, cu);
9578 /* Return the named attribute or NULL if not there,
9579 but do not follow DW_AT_specification, etc.
9580 This is for use in contexts where we're reading .debug_types dies.
9581 Following DW_AT_specification, DW_AT_abstract_origin will take us
9582 back up the chain, and we want to go down. */
9584 static struct attribute *
9585 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9586 struct dwarf2_cu *cu)
9590 for (i = 0; i < die->num_attrs; ++i)
9591 if (die->attrs[i].name == name)
9592 return &die->attrs[i];
9597 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9598 and holds a non-zero value. This function should only be used for
9599 DW_FORM_flag or DW_FORM_flag_present attributes. */
9602 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9604 struct attribute *attr = dwarf2_attr (die, name, cu);
9606 return (attr && DW_UNSND (attr));
9610 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9612 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9613 which value is non-zero. However, we have to be careful with
9614 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9615 (via dwarf2_flag_true_p) follows this attribute. So we may
9616 end up accidently finding a declaration attribute that belongs
9617 to a different DIE referenced by the specification attribute,
9618 even though the given DIE does not have a declaration attribute. */
9619 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9620 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9623 /* Return the die giving the specification for DIE, if there is
9624 one. *SPEC_CU is the CU containing DIE on input, and the CU
9625 containing the return value on output. If there is no
9626 specification, but there is an abstract origin, that is
9629 static struct die_info *
9630 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9632 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9635 if (spec_attr == NULL)
9636 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9638 if (spec_attr == NULL)
9641 return follow_die_ref (die, spec_attr, spec_cu);
9644 /* Free the line_header structure *LH, and any arrays and strings it
9647 free_line_header (struct line_header *lh)
9649 if (lh->standard_opcode_lengths)
9650 xfree (lh->standard_opcode_lengths);
9652 /* Remember that all the lh->file_names[i].name pointers are
9653 pointers into debug_line_buffer, and don't need to be freed. */
9655 xfree (lh->file_names);
9657 /* Similarly for the include directory names. */
9658 if (lh->include_dirs)
9659 xfree (lh->include_dirs);
9665 /* Add an entry to LH's include directory table. */
9667 add_include_dir (struct line_header *lh, char *include_dir)
9669 /* Grow the array if necessary. */
9670 if (lh->include_dirs_size == 0)
9672 lh->include_dirs_size = 1; /* for testing */
9673 lh->include_dirs = xmalloc (lh->include_dirs_size
9674 * sizeof (*lh->include_dirs));
9676 else if (lh->num_include_dirs >= lh->include_dirs_size)
9678 lh->include_dirs_size *= 2;
9679 lh->include_dirs = xrealloc (lh->include_dirs,
9680 (lh->include_dirs_size
9681 * sizeof (*lh->include_dirs)));
9684 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9688 /* Add an entry to LH's file name table. */
9690 add_file_name (struct line_header *lh,
9692 unsigned int dir_index,
9693 unsigned int mod_time,
9694 unsigned int length)
9696 struct file_entry *fe;
9698 /* Grow the array if necessary. */
9699 if (lh->file_names_size == 0)
9701 lh->file_names_size = 1; /* for testing */
9702 lh->file_names = xmalloc (lh->file_names_size
9703 * sizeof (*lh->file_names));
9705 else if (lh->num_file_names >= lh->file_names_size)
9707 lh->file_names_size *= 2;
9708 lh->file_names = xrealloc (lh->file_names,
9709 (lh->file_names_size
9710 * sizeof (*lh->file_names)));
9713 fe = &lh->file_names[lh->num_file_names++];
9715 fe->dir_index = dir_index;
9716 fe->mod_time = mod_time;
9717 fe->length = length;
9723 /* Read the statement program header starting at OFFSET in
9724 .debug_line, according to the endianness of ABFD. Return a pointer
9725 to a struct line_header, allocated using xmalloc.
9727 NOTE: the strings in the include directory and file name tables of
9728 the returned object point into debug_line_buffer, and must not be
9730 static struct line_header *
9731 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9732 struct dwarf2_cu *cu)
9734 struct cleanup *back_to;
9735 struct line_header *lh;
9737 unsigned int bytes_read, offset_size;
9739 char *cur_dir, *cur_file;
9741 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9742 if (dwarf2_per_objfile->line.buffer == NULL)
9744 complaint (&symfile_complaints, _("missing .debug_line section"));
9748 /* Make sure that at least there's room for the total_length field.
9749 That could be 12 bytes long, but we're just going to fudge that. */
9750 if (offset + 4 >= dwarf2_per_objfile->line.size)
9752 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9756 lh = xmalloc (sizeof (*lh));
9757 memset (lh, 0, sizeof (*lh));
9758 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9761 line_ptr = dwarf2_per_objfile->line.buffer + offset;
9763 /* Read in the header. */
9765 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9766 &bytes_read, &offset_size);
9767 line_ptr += bytes_read;
9768 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9769 + dwarf2_per_objfile->line.size))
9771 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9774 lh->statement_program_end = line_ptr + lh->total_length;
9775 lh->version = read_2_bytes (abfd, line_ptr);
9777 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9778 line_ptr += offset_size;
9779 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9781 if (lh->version >= 4)
9783 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9787 lh->maximum_ops_per_instruction = 1;
9789 if (lh->maximum_ops_per_instruction == 0)
9791 lh->maximum_ops_per_instruction = 1;
9792 complaint (&symfile_complaints,
9793 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9796 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9798 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9800 lh->line_range = read_1_byte (abfd, line_ptr);
9802 lh->opcode_base = read_1_byte (abfd, line_ptr);
9804 lh->standard_opcode_lengths
9805 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9807 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9808 for (i = 1; i < lh->opcode_base; ++i)
9810 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9814 /* Read directory table. */
9815 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9817 line_ptr += bytes_read;
9818 add_include_dir (lh, cur_dir);
9820 line_ptr += bytes_read;
9822 /* Read file name table. */
9823 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9825 unsigned int dir_index, mod_time, length;
9827 line_ptr += bytes_read;
9828 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9829 line_ptr += bytes_read;
9830 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9831 line_ptr += bytes_read;
9832 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9833 line_ptr += bytes_read;
9835 add_file_name (lh, cur_file, dir_index, mod_time, length);
9837 line_ptr += bytes_read;
9838 lh->statement_program_start = line_ptr;
9840 if (line_ptr > (dwarf2_per_objfile->line.buffer
9841 + dwarf2_per_objfile->line.size))
9842 complaint (&symfile_complaints,
9843 _("line number info header doesn't fit in `.debug_line' section"));
9845 discard_cleanups (back_to);
9849 /* This function exists to work around a bug in certain compilers
9850 (particularly GCC 2.95), in which the first line number marker of a
9851 function does not show up until after the prologue, right before
9852 the second line number marker. This function shifts ADDRESS down
9853 to the beginning of the function if necessary, and is called on
9854 addresses passed to record_line. */
9857 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9859 struct function_range *fn;
9861 /* Find the function_range containing address. */
9866 cu->cached_fn = cu->first_fn;
9870 if (fn->lowpc <= address && fn->highpc > address)
9876 while (fn && fn != cu->cached_fn)
9877 if (fn->lowpc <= address && fn->highpc > address)
9887 if (address != fn->lowpc)
9888 complaint (&symfile_complaints,
9889 _("misplaced first line number at 0x%lx for '%s'"),
9890 (unsigned long) address, fn->name);
9895 /* Decode the Line Number Program (LNP) for the given line_header
9896 structure and CU. The actual information extracted and the type
9897 of structures created from the LNP depends on the value of PST.
9899 1. If PST is NULL, then this procedure uses the data from the program
9900 to create all necessary symbol tables, and their linetables.
9901 The compilation directory of the file is passed in COMP_DIR,
9902 and must not be NULL.
9904 2. If PST is not NULL, this procedure reads the program to determine
9905 the list of files included by the unit represented by PST, and
9906 builds all the associated partial symbol tables. In this case,
9907 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9908 is not used to compute the full name of the symtab, and therefore
9909 omitting it when building the partial symtab does not introduce
9910 the potential for inconsistency - a partial symtab and its associated
9911 symbtab having a different fullname -). */
9914 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
9915 struct dwarf2_cu *cu, struct partial_symtab *pst)
9917 gdb_byte *line_ptr, *extended_end;
9919 unsigned int bytes_read, extended_len;
9920 unsigned char op_code, extended_op, adj_opcode;
9922 struct objfile *objfile = cu->objfile;
9923 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9924 const int decode_for_pst_p = (pst != NULL);
9925 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
9927 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9929 line_ptr = lh->statement_program_start;
9930 line_end = lh->statement_program_end;
9932 /* Read the statement sequences until there's nothing left. */
9933 while (line_ptr < line_end)
9935 /* state machine registers */
9936 CORE_ADDR address = 0;
9937 unsigned int file = 1;
9938 unsigned int line = 1;
9939 unsigned int column = 0;
9940 int is_stmt = lh->default_is_stmt;
9941 int basic_block = 0;
9942 int end_sequence = 0;
9944 unsigned char op_index = 0;
9946 if (!decode_for_pst_p && lh->num_file_names >= file)
9948 /* Start a subfile for the current file of the state machine. */
9949 /* lh->include_dirs and lh->file_names are 0-based, but the
9950 directory and file name numbers in the statement program
9952 struct file_entry *fe = &lh->file_names[file - 1];
9956 dir = lh->include_dirs[fe->dir_index - 1];
9958 dwarf2_start_subfile (fe->name, dir, comp_dir);
9961 /* Decode the table. */
9962 while (!end_sequence)
9964 op_code = read_1_byte (abfd, line_ptr);
9966 if (line_ptr > line_end)
9968 dwarf2_debug_line_missing_end_sequence_complaint ();
9972 if (op_code >= lh->opcode_base)
9974 /* Special operand. */
9975 adj_opcode = op_code - lh->opcode_base;
9976 address += (((op_index + (adj_opcode / lh->line_range))
9977 / lh->maximum_ops_per_instruction)
9978 * lh->minimum_instruction_length);
9979 op_index = ((op_index + (adj_opcode / lh->line_range))
9980 % lh->maximum_ops_per_instruction);
9981 line += lh->line_base + (adj_opcode % lh->line_range);
9982 if (lh->num_file_names < file || file == 0)
9983 dwarf2_debug_line_missing_file_complaint ();
9984 /* For now we ignore lines not starting on an
9985 instruction boundary. */
9986 else if (op_index == 0)
9988 lh->file_names[file - 1].included_p = 1;
9989 if (!decode_for_pst_p && is_stmt)
9991 if (last_subfile != current_subfile)
9993 addr = gdbarch_addr_bits_remove (gdbarch, address);
9995 record_line (last_subfile, 0, addr);
9996 last_subfile = current_subfile;
9998 /* Append row to matrix using current values. */
9999 addr = check_cu_functions (address, cu);
10000 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10001 record_line (current_subfile, line, addr);
10006 else switch (op_code)
10008 case DW_LNS_extended_op:
10009 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10010 line_ptr += bytes_read;
10011 extended_end = line_ptr + extended_len;
10012 extended_op = read_1_byte (abfd, line_ptr);
10014 switch (extended_op)
10016 case DW_LNE_end_sequence:
10019 case DW_LNE_set_address:
10020 address = read_address (abfd, line_ptr, cu, &bytes_read);
10022 line_ptr += bytes_read;
10023 address += baseaddr;
10025 case DW_LNE_define_file:
10028 unsigned int dir_index, mod_time, length;
10030 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10031 line_ptr += bytes_read;
10033 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10034 line_ptr += bytes_read;
10036 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10037 line_ptr += bytes_read;
10039 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10040 line_ptr += bytes_read;
10041 add_file_name (lh, cur_file, dir_index, mod_time, length);
10044 case DW_LNE_set_discriminator:
10045 /* The discriminator is not interesting to the debugger;
10047 line_ptr = extended_end;
10050 complaint (&symfile_complaints,
10051 _("mangled .debug_line section"));
10054 /* Make sure that we parsed the extended op correctly. If e.g.
10055 we expected a different address size than the producer used,
10056 we may have read the wrong number of bytes. */
10057 if (line_ptr != extended_end)
10059 complaint (&symfile_complaints,
10060 _("mangled .debug_line section"));
10065 if (lh->num_file_names < file || file == 0)
10066 dwarf2_debug_line_missing_file_complaint ();
10069 lh->file_names[file - 1].included_p = 1;
10070 if (!decode_for_pst_p && is_stmt)
10072 if (last_subfile != current_subfile)
10074 addr = gdbarch_addr_bits_remove (gdbarch, address);
10076 record_line (last_subfile, 0, addr);
10077 last_subfile = current_subfile;
10079 addr = check_cu_functions (address, cu);
10080 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10081 record_line (current_subfile, line, addr);
10086 case DW_LNS_advance_pc:
10089 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10091 address += (((op_index + adjust)
10092 / lh->maximum_ops_per_instruction)
10093 * lh->minimum_instruction_length);
10094 op_index = ((op_index + adjust)
10095 % lh->maximum_ops_per_instruction);
10096 line_ptr += bytes_read;
10099 case DW_LNS_advance_line:
10100 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10101 line_ptr += bytes_read;
10103 case DW_LNS_set_file:
10105 /* The arrays lh->include_dirs and lh->file_names are
10106 0-based, but the directory and file name numbers in
10107 the statement program are 1-based. */
10108 struct file_entry *fe;
10111 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10112 line_ptr += bytes_read;
10113 if (lh->num_file_names < file || file == 0)
10114 dwarf2_debug_line_missing_file_complaint ();
10117 fe = &lh->file_names[file - 1];
10119 dir = lh->include_dirs[fe->dir_index - 1];
10120 if (!decode_for_pst_p)
10122 last_subfile = current_subfile;
10123 dwarf2_start_subfile (fe->name, dir, comp_dir);
10128 case DW_LNS_set_column:
10129 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10130 line_ptr += bytes_read;
10132 case DW_LNS_negate_stmt:
10133 is_stmt = (!is_stmt);
10135 case DW_LNS_set_basic_block:
10138 /* Add to the address register of the state machine the
10139 address increment value corresponding to special opcode
10140 255. I.e., this value is scaled by the minimum
10141 instruction length since special opcode 255 would have
10142 scaled the the increment. */
10143 case DW_LNS_const_add_pc:
10145 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10147 address += (((op_index + adjust)
10148 / lh->maximum_ops_per_instruction)
10149 * lh->minimum_instruction_length);
10150 op_index = ((op_index + adjust)
10151 % lh->maximum_ops_per_instruction);
10154 case DW_LNS_fixed_advance_pc:
10155 address += read_2_bytes (abfd, line_ptr);
10161 /* Unknown standard opcode, ignore it. */
10164 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10166 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10167 line_ptr += bytes_read;
10172 if (lh->num_file_names < file || file == 0)
10173 dwarf2_debug_line_missing_file_complaint ();
10176 lh->file_names[file - 1].included_p = 1;
10177 if (!decode_for_pst_p)
10179 addr = gdbarch_addr_bits_remove (gdbarch, address);
10180 record_line (current_subfile, 0, addr);
10185 if (decode_for_pst_p)
10189 /* Now that we're done scanning the Line Header Program, we can
10190 create the psymtab of each included file. */
10191 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10192 if (lh->file_names[file_index].included_p == 1)
10194 const struct file_entry fe = lh->file_names [file_index];
10195 char *include_name = fe.name;
10196 char *dir_name = NULL;
10197 char *pst_filename = pst->filename;
10200 dir_name = lh->include_dirs[fe.dir_index - 1];
10202 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
10204 include_name = concat (dir_name, SLASH_STRING,
10205 include_name, (char *)NULL);
10206 make_cleanup (xfree, include_name);
10209 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10211 pst_filename = concat (pst->dirname, SLASH_STRING,
10212 pst_filename, (char *)NULL);
10213 make_cleanup (xfree, pst_filename);
10216 if (strcmp (include_name, pst_filename) != 0)
10217 dwarf2_create_include_psymtab (include_name, pst, objfile);
10222 /* Make sure a symtab is created for every file, even files
10223 which contain only variables (i.e. no code with associated
10227 struct file_entry *fe;
10229 for (i = 0; i < lh->num_file_names; i++)
10233 fe = &lh->file_names[i];
10235 dir = lh->include_dirs[fe->dir_index - 1];
10236 dwarf2_start_subfile (fe->name, dir, comp_dir);
10238 /* Skip the main file; we don't need it, and it must be
10239 allocated last, so that it will show up before the
10240 non-primary symtabs in the objfile's symtab list. */
10241 if (current_subfile == first_subfile)
10244 if (current_subfile->symtab == NULL)
10245 current_subfile->symtab = allocate_symtab (current_subfile->name,
10247 fe->symtab = current_subfile->symtab;
10252 /* Start a subfile for DWARF. FILENAME is the name of the file and
10253 DIRNAME the name of the source directory which contains FILENAME
10254 or NULL if not known. COMP_DIR is the compilation directory for the
10255 linetable's compilation unit or NULL if not known.
10256 This routine tries to keep line numbers from identical absolute and
10257 relative file names in a common subfile.
10259 Using the `list' example from the GDB testsuite, which resides in
10260 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10261 of /srcdir/list0.c yields the following debugging information for list0.c:
10263 DW_AT_name: /srcdir/list0.c
10264 DW_AT_comp_dir: /compdir
10265 files.files[0].name: list0.h
10266 files.files[0].dir: /srcdir
10267 files.files[1].name: list0.c
10268 files.files[1].dir: /srcdir
10270 The line number information for list0.c has to end up in a single
10271 subfile, so that `break /srcdir/list0.c:1' works as expected.
10272 start_subfile will ensure that this happens provided that we pass the
10273 concatenation of files.files[1].dir and files.files[1].name as the
10277 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
10281 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10282 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10283 second argument to start_subfile. To be consistent, we do the
10284 same here. In order not to lose the line information directory,
10285 we concatenate it to the filename when it makes sense.
10286 Note that the Dwarf3 standard says (speaking of filenames in line
10287 information): ``The directory index is ignored for file names
10288 that represent full path names''. Thus ignoring dirname in the
10289 `else' branch below isn't an issue. */
10291 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10292 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10294 fullname = filename;
10296 start_subfile (fullname, comp_dir);
10298 if (fullname != filename)
10303 var_decode_location (struct attribute *attr, struct symbol *sym,
10304 struct dwarf2_cu *cu)
10306 struct objfile *objfile = cu->objfile;
10307 struct comp_unit_head *cu_header = &cu->header;
10309 /* NOTE drow/2003-01-30: There used to be a comment and some special
10310 code here to turn a symbol with DW_AT_external and a
10311 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10312 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10313 with some versions of binutils) where shared libraries could have
10314 relocations against symbols in their debug information - the
10315 minimal symbol would have the right address, but the debug info
10316 would not. It's no longer necessary, because we will explicitly
10317 apply relocations when we read in the debug information now. */
10319 /* A DW_AT_location attribute with no contents indicates that a
10320 variable has been optimized away. */
10321 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10323 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10327 /* Handle one degenerate form of location expression specially, to
10328 preserve GDB's previous behavior when section offsets are
10329 specified. If this is just a DW_OP_addr then mark this symbol
10332 if (attr_form_is_block (attr)
10333 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10334 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10336 unsigned int dummy;
10338 SYMBOL_VALUE_ADDRESS (sym) =
10339 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10340 SYMBOL_CLASS (sym) = LOC_STATIC;
10341 fixup_symbol_section (sym, objfile);
10342 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10343 SYMBOL_SECTION (sym));
10347 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10348 expression evaluator, and use LOC_COMPUTED only when necessary
10349 (i.e. when the value of a register or memory location is
10350 referenced, or a thread-local block, etc.). Then again, it might
10351 not be worthwhile. I'm assuming that it isn't unless performance
10352 or memory numbers show me otherwise. */
10354 dwarf2_symbol_mark_computed (attr, sym, cu);
10355 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10358 /* Given a pointer to a DWARF information entry, figure out if we need
10359 to make a symbol table entry for it, and if so, create a new entry
10360 and return a pointer to it.
10361 If TYPE is NULL, determine symbol type from the die, otherwise
10362 used the passed type.
10363 If SPACE is not NULL, use it to hold the new symbol. If it is
10364 NULL, allocate a new symbol on the objfile's obstack. */
10366 static struct symbol *
10367 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10368 struct symbol *space)
10370 struct objfile *objfile = cu->objfile;
10371 struct symbol *sym = NULL;
10373 struct attribute *attr = NULL;
10374 struct attribute *attr2 = NULL;
10375 CORE_ADDR baseaddr;
10376 struct pending **list_to_add = NULL;
10378 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10380 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10382 name = dwarf2_name (die, cu);
10385 const char *linkagename;
10386 int suppress_add = 0;
10391 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10392 OBJSTAT (objfile, n_syms++);
10394 /* Cache this symbol's name and the name's demangled form (if any). */
10395 SYMBOL_SET_LANGUAGE (sym, cu->language);
10396 linkagename = dwarf2_physname (name, die, cu);
10397 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10399 /* Fortran does not have mangling standard and the mangling does differ
10400 between gfortran, iFort etc. */
10401 if (cu->language == language_fortran
10402 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10403 symbol_set_demangled_name (&(sym->ginfo),
10404 (char *) dwarf2_full_name (name, die, cu),
10407 /* Default assumptions.
10408 Use the passed type or decode it from the die. */
10409 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10410 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10412 SYMBOL_TYPE (sym) = type;
10414 SYMBOL_TYPE (sym) = die_type (die, cu);
10415 attr = dwarf2_attr (die,
10416 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10420 SYMBOL_LINE (sym) = DW_UNSND (attr);
10423 attr = dwarf2_attr (die,
10424 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10428 int file_index = DW_UNSND (attr);
10430 if (cu->line_header == NULL
10431 || file_index > cu->line_header->num_file_names)
10432 complaint (&symfile_complaints,
10433 _("file index out of range"));
10434 else if (file_index > 0)
10436 struct file_entry *fe;
10438 fe = &cu->line_header->file_names[file_index - 1];
10439 SYMBOL_SYMTAB (sym) = fe->symtab;
10446 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10449 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10451 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10452 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10453 SYMBOL_CLASS (sym) = LOC_LABEL;
10454 add_symbol_to_list (sym, cu->list_in_scope);
10456 case DW_TAG_subprogram:
10457 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10459 SYMBOL_CLASS (sym) = LOC_BLOCK;
10460 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10461 if ((attr2 && (DW_UNSND (attr2) != 0))
10462 || cu->language == language_ada)
10464 /* Subprograms marked external are stored as a global symbol.
10465 Ada subprograms, whether marked external or not, are always
10466 stored as a global symbol, because we want to be able to
10467 access them globally. For instance, we want to be able
10468 to break on a nested subprogram without having to
10469 specify the context. */
10470 list_to_add = &global_symbols;
10474 list_to_add = cu->list_in_scope;
10477 case DW_TAG_inlined_subroutine:
10478 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10480 SYMBOL_CLASS (sym) = LOC_BLOCK;
10481 SYMBOL_INLINED (sym) = 1;
10482 /* Do not add the symbol to any lists. It will be found via
10483 BLOCK_FUNCTION from the blockvector. */
10485 case DW_TAG_template_value_param:
10487 /* Fall through. */
10488 case DW_TAG_variable:
10489 case DW_TAG_member:
10490 /* Compilation with minimal debug info may result in variables
10491 with missing type entries. Change the misleading `void' type
10492 to something sensible. */
10493 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10495 = objfile_type (objfile)->nodebug_data_symbol;
10497 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10498 /* In the case of DW_TAG_member, we should only be called for
10499 static const members. */
10500 if (die->tag == DW_TAG_member)
10502 /* dwarf2_add_field uses die_is_declaration,
10503 so we do the same. */
10504 gdb_assert (die_is_declaration (die, cu));
10509 dwarf2_const_value (attr, sym, cu);
10510 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10513 if (attr2 && (DW_UNSND (attr2) != 0))
10514 list_to_add = &global_symbols;
10516 list_to_add = cu->list_in_scope;
10520 attr = dwarf2_attr (die, DW_AT_location, cu);
10523 var_decode_location (attr, sym, cu);
10524 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10525 if (SYMBOL_CLASS (sym) == LOC_STATIC
10526 && SYMBOL_VALUE_ADDRESS (sym) == 0
10527 && !dwarf2_per_objfile->has_section_at_zero)
10529 /* When a static variable is eliminated by the linker,
10530 the corresponding debug information is not stripped
10531 out, but the variable address is set to null;
10532 do not add such variables into symbol table. */
10534 else if (attr2 && (DW_UNSND (attr2) != 0))
10536 /* Workaround gfortran PR debug/40040 - it uses
10537 DW_AT_location for variables in -fPIC libraries which may
10538 get overriden by other libraries/executable and get
10539 a different address. Resolve it by the minimal symbol
10540 which may come from inferior's executable using copy
10541 relocation. Make this workaround only for gfortran as for
10542 other compilers GDB cannot guess the minimal symbol
10543 Fortran mangling kind. */
10544 if (cu->language == language_fortran && die->parent
10545 && die->parent->tag == DW_TAG_module
10547 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10548 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10550 /* A variable with DW_AT_external is never static,
10551 but it may be block-scoped. */
10552 list_to_add = (cu->list_in_scope == &file_symbols
10553 ? &global_symbols : cu->list_in_scope);
10556 list_to_add = cu->list_in_scope;
10560 /* We do not know the address of this symbol.
10561 If it is an external symbol and we have type information
10562 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10563 The address of the variable will then be determined from
10564 the minimal symbol table whenever the variable is
10566 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10567 if (attr2 && (DW_UNSND (attr2) != 0)
10568 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10570 /* A variable with DW_AT_external is never static, but it
10571 may be block-scoped. */
10572 list_to_add = (cu->list_in_scope == &file_symbols
10573 ? &global_symbols : cu->list_in_scope);
10575 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10577 else if (!die_is_declaration (die, cu))
10579 /* Use the default LOC_OPTIMIZED_OUT class. */
10580 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10582 list_to_add = cu->list_in_scope;
10586 case DW_TAG_formal_parameter:
10587 /* If we are inside a function, mark this as an argument. If
10588 not, we might be looking at an argument to an inlined function
10589 when we do not have enough information to show inlined frames;
10590 pretend it's a local variable in that case so that the user can
10592 if (context_stack_depth > 0
10593 && context_stack[context_stack_depth - 1].name != NULL)
10594 SYMBOL_IS_ARGUMENT (sym) = 1;
10595 attr = dwarf2_attr (die, DW_AT_location, cu);
10598 var_decode_location (attr, sym, cu);
10600 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10603 dwarf2_const_value (attr, sym, cu);
10605 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10606 if (attr && DW_UNSND (attr))
10608 struct type *ref_type;
10610 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10611 SYMBOL_TYPE (sym) = ref_type;
10614 list_to_add = cu->list_in_scope;
10616 case DW_TAG_unspecified_parameters:
10617 /* From varargs functions; gdb doesn't seem to have any
10618 interest in this information, so just ignore it for now.
10621 case DW_TAG_template_type_param:
10623 /* Fall through. */
10624 case DW_TAG_class_type:
10625 case DW_TAG_interface_type:
10626 case DW_TAG_structure_type:
10627 case DW_TAG_union_type:
10628 case DW_TAG_set_type:
10629 case DW_TAG_enumeration_type:
10630 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10631 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10634 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10635 really ever be static objects: otherwise, if you try
10636 to, say, break of a class's method and you're in a file
10637 which doesn't mention that class, it won't work unless
10638 the check for all static symbols in lookup_symbol_aux
10639 saves you. See the OtherFileClass tests in
10640 gdb.c++/namespace.exp. */
10644 list_to_add = (cu->list_in_scope == &file_symbols
10645 && (cu->language == language_cplus
10646 || cu->language == language_java)
10647 ? &global_symbols : cu->list_in_scope);
10650 /* The semantics of C++ state that "struct foo { ... }" also
10651 defines a typedef for "foo". A Java class declaration also
10652 defines a typedef for the class. */
10653 if (cu->language == language_cplus
10654 || cu->language == language_java
10655 || cu->language == language_ada)
10657 /* The symbol's name is already allocated along with
10658 this objfile, so we don't need to duplicate it for
10660 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10661 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10665 case DW_TAG_typedef:
10666 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10667 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10668 list_to_add = cu->list_in_scope;
10670 case DW_TAG_base_type:
10671 case DW_TAG_subrange_type:
10672 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10673 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10674 list_to_add = cu->list_in_scope;
10676 case DW_TAG_enumerator:
10677 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10680 dwarf2_const_value (attr, sym, cu);
10683 /* NOTE: carlton/2003-11-10: See comment above in the
10684 DW_TAG_class_type, etc. block. */
10686 list_to_add = (cu->list_in_scope == &file_symbols
10687 && (cu->language == language_cplus
10688 || cu->language == language_java)
10689 ? &global_symbols : cu->list_in_scope);
10692 case DW_TAG_namespace:
10693 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10694 list_to_add = &global_symbols;
10697 /* Not a tag we recognize. Hopefully we aren't processing
10698 trash data, but since we must specifically ignore things
10699 we don't recognize, there is nothing else we should do at
10701 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10702 dwarf_tag_name (die->tag));
10708 sym->hash_next = objfile->template_symbols;
10709 objfile->template_symbols = sym;
10710 list_to_add = NULL;
10713 if (list_to_add != NULL)
10714 add_symbol_to_list (sym, list_to_add);
10716 /* For the benefit of old versions of GCC, check for anonymous
10717 namespaces based on the demangled name. */
10718 if (!processing_has_namespace_info
10719 && cu->language == language_cplus)
10720 cp_scan_for_anonymous_namespaces (sym);
10725 /* A wrapper for new_symbol_full that always allocates a new symbol. */
10727 static struct symbol *
10728 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10730 return new_symbol_full (die, type, cu, NULL);
10733 /* Given an attr with a DW_FORM_dataN value in host byte order,
10734 zero-extend it as appropriate for the symbol's type. The DWARF
10735 standard (v4) is not entirely clear about the meaning of using
10736 DW_FORM_dataN for a constant with a signed type, where the type is
10737 wider than the data. The conclusion of a discussion on the DWARF
10738 list was that this is unspecified. We choose to always zero-extend
10739 because that is the interpretation long in use by GCC. */
10742 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10743 const char *name, struct obstack *obstack,
10744 struct dwarf2_cu *cu, long *value, int bits)
10746 struct objfile *objfile = cu->objfile;
10747 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10748 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10749 LONGEST l = DW_UNSND (attr);
10751 if (bits < sizeof (*value) * 8)
10753 l &= ((LONGEST) 1 << bits) - 1;
10756 else if (bits == sizeof (*value) * 8)
10760 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10761 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10768 /* Read a constant value from an attribute. Either set *VALUE, or if
10769 the value does not fit in *VALUE, set *BYTES - either already
10770 allocated on the objfile obstack, or newly allocated on OBSTACK,
10771 or, set *BATON, if we translated the constant to a location
10775 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10776 const char *name, struct obstack *obstack,
10777 struct dwarf2_cu *cu,
10778 long *value, gdb_byte **bytes,
10779 struct dwarf2_locexpr_baton **baton)
10781 struct objfile *objfile = cu->objfile;
10782 struct comp_unit_head *cu_header = &cu->header;
10783 struct dwarf_block *blk;
10784 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10785 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10791 switch (attr->form)
10797 if (TYPE_LENGTH (type) != cu_header->addr_size)
10798 dwarf2_const_value_length_mismatch_complaint (name,
10799 cu_header->addr_size,
10800 TYPE_LENGTH (type));
10801 /* Symbols of this form are reasonably rare, so we just
10802 piggyback on the existing location code rather than writing
10803 a new implementation of symbol_computed_ops. */
10804 *baton = obstack_alloc (&objfile->objfile_obstack,
10805 sizeof (struct dwarf2_locexpr_baton));
10806 (*baton)->per_cu = cu->per_cu;
10807 gdb_assert ((*baton)->per_cu);
10809 (*baton)->size = 2 + cu_header->addr_size;
10810 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10811 (*baton)->data = data;
10813 data[0] = DW_OP_addr;
10814 store_unsigned_integer (&data[1], cu_header->addr_size,
10815 byte_order, DW_ADDR (attr));
10816 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10819 case DW_FORM_string:
10821 /* DW_STRING is already allocated on the objfile obstack, point
10823 *bytes = (gdb_byte *) DW_STRING (attr);
10825 case DW_FORM_block1:
10826 case DW_FORM_block2:
10827 case DW_FORM_block4:
10828 case DW_FORM_block:
10829 case DW_FORM_exprloc:
10830 blk = DW_BLOCK (attr);
10831 if (TYPE_LENGTH (type) != blk->size)
10832 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10833 TYPE_LENGTH (type));
10834 *bytes = blk->data;
10837 /* The DW_AT_const_value attributes are supposed to carry the
10838 symbol's value "represented as it would be on the target
10839 architecture." By the time we get here, it's already been
10840 converted to host endianness, so we just need to sign- or
10841 zero-extend it as appropriate. */
10842 case DW_FORM_data1:
10843 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
10845 case DW_FORM_data2:
10846 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
10848 case DW_FORM_data4:
10849 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
10851 case DW_FORM_data8:
10852 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
10855 case DW_FORM_sdata:
10856 *value = DW_SND (attr);
10859 case DW_FORM_udata:
10860 *value = DW_UNSND (attr);
10864 complaint (&symfile_complaints,
10865 _("unsupported const value attribute form: '%s'"),
10866 dwarf_form_name (attr->form));
10873 /* Copy constant value from an attribute to a symbol. */
10876 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10877 struct dwarf2_cu *cu)
10879 struct objfile *objfile = cu->objfile;
10880 struct comp_unit_head *cu_header = &cu->header;
10883 struct dwarf2_locexpr_baton *baton;
10885 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10886 SYMBOL_PRINT_NAME (sym),
10887 &objfile->objfile_obstack, cu,
10888 &value, &bytes, &baton);
10892 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10893 SYMBOL_LOCATION_BATON (sym) = baton;
10894 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10896 else if (bytes != NULL)
10898 SYMBOL_VALUE_BYTES (sym) = bytes;
10899 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10903 SYMBOL_VALUE (sym) = value;
10904 SYMBOL_CLASS (sym) = LOC_CONST;
10909 /* Return the type of the die in question using its DW_AT_type attribute. */
10911 static struct type *
10912 die_type (struct die_info *die, struct dwarf2_cu *cu)
10914 struct attribute *type_attr;
10915 struct die_info *type_die;
10917 type_attr = dwarf2_attr (die, DW_AT_type, cu);
10920 /* A missing DW_AT_type represents a void type. */
10921 return objfile_type (cu->objfile)->builtin_void;
10924 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10926 return tag_type_to_type (type_die, cu);
10929 /* True iff CU's producer generates GNAT Ada auxiliary information
10930 that allows to find parallel types through that information instead
10931 of having to do expensive parallel lookups by type name. */
10934 need_gnat_info (struct dwarf2_cu *cu)
10936 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10937 of GNAT produces this auxiliary information, without any indication
10938 that it is produced. Part of enhancing the FSF version of GNAT
10939 to produce that information will be to put in place an indicator
10940 that we can use in order to determine whether the descriptive type
10941 info is available or not. One suggestion that has been made is
10942 to use a new attribute, attached to the CU die. For now, assume
10943 that the descriptive type info is not available. */
10948 /* Return the auxiliary type of the die in question using its
10949 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10950 attribute is not present. */
10952 static struct type *
10953 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10955 struct attribute *type_attr;
10956 struct die_info *type_die;
10958 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10962 type_die = follow_die_ref (die, type_attr, &cu);
10963 return tag_type_to_type (type_die, cu);
10966 /* If DIE has a descriptive_type attribute, then set the TYPE's
10967 descriptive type accordingly. */
10970 set_descriptive_type (struct type *type, struct die_info *die,
10971 struct dwarf2_cu *cu)
10973 struct type *descriptive_type = die_descriptive_type (die, cu);
10975 if (descriptive_type)
10977 ALLOCATE_GNAT_AUX_TYPE (type);
10978 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10982 /* Return the containing type of the die in question using its
10983 DW_AT_containing_type attribute. */
10985 static struct type *
10986 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10988 struct attribute *type_attr;
10989 struct die_info *type_die;
10991 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
10993 error (_("Dwarf Error: Problem turning containing type into gdb type "
10994 "[in module %s]"), cu->objfile->name);
10996 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10997 return tag_type_to_type (type_die, cu);
11000 static struct type *
11001 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
11003 struct type *this_type;
11005 this_type = read_type_die (die, cu);
11008 char *message, *saved;
11010 /* read_type_die already issued a complaint. */
11011 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11015 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11016 message, strlen (message));
11019 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11024 static struct type *
11025 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11027 struct type *this_type;
11029 this_type = get_die_type (die, cu);
11035 case DW_TAG_class_type:
11036 case DW_TAG_interface_type:
11037 case DW_TAG_structure_type:
11038 case DW_TAG_union_type:
11039 this_type = read_structure_type (die, cu);
11041 case DW_TAG_enumeration_type:
11042 this_type = read_enumeration_type (die, cu);
11044 case DW_TAG_subprogram:
11045 case DW_TAG_subroutine_type:
11046 case DW_TAG_inlined_subroutine:
11047 this_type = read_subroutine_type (die, cu);
11049 case DW_TAG_array_type:
11050 this_type = read_array_type (die, cu);
11052 case DW_TAG_set_type:
11053 this_type = read_set_type (die, cu);
11055 case DW_TAG_pointer_type:
11056 this_type = read_tag_pointer_type (die, cu);
11058 case DW_TAG_ptr_to_member_type:
11059 this_type = read_tag_ptr_to_member_type (die, cu);
11061 case DW_TAG_reference_type:
11062 this_type = read_tag_reference_type (die, cu);
11064 case DW_TAG_const_type:
11065 this_type = read_tag_const_type (die, cu);
11067 case DW_TAG_volatile_type:
11068 this_type = read_tag_volatile_type (die, cu);
11070 case DW_TAG_string_type:
11071 this_type = read_tag_string_type (die, cu);
11073 case DW_TAG_typedef:
11074 this_type = read_typedef (die, cu);
11076 case DW_TAG_subrange_type:
11077 this_type = read_subrange_type (die, cu);
11079 case DW_TAG_base_type:
11080 this_type = read_base_type (die, cu);
11082 case DW_TAG_unspecified_type:
11083 this_type = read_unspecified_type (die, cu);
11085 case DW_TAG_namespace:
11086 this_type = read_namespace_type (die, cu);
11088 case DW_TAG_module:
11089 this_type = read_module_type (die, cu);
11092 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11093 dwarf_tag_name (die->tag));
11100 /* Return the name of the namespace/class that DIE is defined within,
11101 or "" if we can't tell. The caller should not xfree the result.
11103 For example, if we're within the method foo() in the following
11113 then determine_prefix on foo's die will return "N::C". */
11116 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11118 struct die_info *parent, *spec_die;
11119 struct dwarf2_cu *spec_cu;
11120 struct type *parent_type;
11122 if (cu->language != language_cplus && cu->language != language_java
11123 && cu->language != language_fortran)
11126 /* We have to be careful in the presence of DW_AT_specification.
11127 For example, with GCC 3.4, given the code
11131 // Definition of N::foo.
11135 then we'll have a tree of DIEs like this:
11137 1: DW_TAG_compile_unit
11138 2: DW_TAG_namespace // N
11139 3: DW_TAG_subprogram // declaration of N::foo
11140 4: DW_TAG_subprogram // definition of N::foo
11141 DW_AT_specification // refers to die #3
11143 Thus, when processing die #4, we have to pretend that we're in
11144 the context of its DW_AT_specification, namely the contex of die
11147 spec_die = die_specification (die, &spec_cu);
11148 if (spec_die == NULL)
11149 parent = die->parent;
11152 parent = spec_die->parent;
11156 if (parent == NULL)
11158 else if (parent->building_fullname)
11161 const char *parent_name;
11163 /* It has been seen on RealView 2.2 built binaries,
11164 DW_TAG_template_type_param types actually _defined_ as
11165 children of the parent class:
11168 template class <class Enum> Class{};
11169 Class<enum E> class_e;
11171 1: DW_TAG_class_type (Class)
11172 2: DW_TAG_enumeration_type (E)
11173 3: DW_TAG_enumerator (enum1:0)
11174 3: DW_TAG_enumerator (enum2:1)
11176 2: DW_TAG_template_type_param
11177 DW_AT_type DW_FORM_ref_udata (E)
11179 Besides being broken debug info, it can put GDB into an
11180 infinite loop. Consider:
11182 When we're building the full name for Class<E>, we'll start
11183 at Class, and go look over its template type parameters,
11184 finding E. We'll then try to build the full name of E, and
11185 reach here. We're now trying to build the full name of E,
11186 and look over the parent DIE for containing scope. In the
11187 broken case, if we followed the parent DIE of E, we'd again
11188 find Class, and once again go look at its template type
11189 arguments, etc., etc. Simply don't consider such parent die
11190 as source-level parent of this die (it can't be, the language
11191 doesn't allow it), and break the loop here. */
11192 name = dwarf2_name (die, cu);
11193 parent_name = dwarf2_name (parent, cu);
11194 complaint (&symfile_complaints,
11195 _("template param type '%s' defined within parent '%s'"),
11196 name ? name : "<unknown>",
11197 parent_name ? parent_name : "<unknown>");
11201 switch (parent->tag)
11203 case DW_TAG_namespace:
11204 parent_type = read_type_die (parent, cu);
11205 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11206 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11207 Work around this problem here. */
11208 if (cu->language == language_cplus
11209 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11211 /* We give a name to even anonymous namespaces. */
11212 return TYPE_TAG_NAME (parent_type);
11213 case DW_TAG_class_type:
11214 case DW_TAG_interface_type:
11215 case DW_TAG_structure_type:
11216 case DW_TAG_union_type:
11217 case DW_TAG_module:
11218 parent_type = read_type_die (parent, cu);
11219 if (TYPE_TAG_NAME (parent_type) != NULL)
11220 return TYPE_TAG_NAME (parent_type);
11222 /* An anonymous structure is only allowed non-static data
11223 members; no typedefs, no member functions, et cetera.
11224 So it does not need a prefix. */
11227 return determine_prefix (parent, cu);
11231 /* Return a newly-allocated string formed by concatenating PREFIX and
11232 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11233 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11234 perform an obconcat, otherwise allocate storage for the result. The CU argument
11235 is used to determine the language and hence, the appropriate separator. */
11237 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
11240 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11241 int physname, struct dwarf2_cu *cu)
11243 const char *lead = "";
11246 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11248 else if (cu->language == language_java)
11250 else if (cu->language == language_fortran && physname)
11252 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11253 DW_AT_MIPS_linkage_name is preferred and used instead. */
11261 if (prefix == NULL)
11263 if (suffix == NULL)
11268 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11270 strcpy (retval, lead);
11271 strcat (retval, prefix);
11272 strcat (retval, sep);
11273 strcat (retval, suffix);
11278 /* We have an obstack. */
11279 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11283 /* Return sibling of die, NULL if no sibling. */
11285 static struct die_info *
11286 sibling_die (struct die_info *die)
11288 return die->sibling;
11291 /* Get name of a die, return NULL if not found. */
11294 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11295 struct obstack *obstack)
11297 if (name && cu->language == language_cplus)
11299 char *canon_name = cp_canonicalize_string (name);
11301 if (canon_name != NULL)
11303 if (strcmp (canon_name, name) != 0)
11304 name = obsavestring (canon_name, strlen (canon_name),
11306 xfree (canon_name);
11313 /* Get name of a die, return NULL if not found. */
11316 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11318 struct attribute *attr;
11320 attr = dwarf2_attr (die, DW_AT_name, cu);
11321 if (!attr || !DW_STRING (attr))
11326 case DW_TAG_compile_unit:
11327 /* Compilation units have a DW_AT_name that is a filename, not
11328 a source language identifier. */
11329 case DW_TAG_enumeration_type:
11330 case DW_TAG_enumerator:
11331 /* These tags always have simple identifiers already; no need
11332 to canonicalize them. */
11333 return DW_STRING (attr);
11335 case DW_TAG_subprogram:
11336 /* Java constructors will all be named "<init>", so return
11337 the class name when we see this special case. */
11338 if (cu->language == language_java
11339 && DW_STRING (attr) != NULL
11340 && strcmp (DW_STRING (attr), "<init>") == 0)
11342 struct dwarf2_cu *spec_cu = cu;
11343 struct die_info *spec_die;
11345 /* GCJ will output '<init>' for Java constructor names.
11346 For this special case, return the name of the parent class. */
11348 /* GCJ may output suprogram DIEs with AT_specification set.
11349 If so, use the name of the specified DIE. */
11350 spec_die = die_specification (die, &spec_cu);
11351 if (spec_die != NULL)
11352 return dwarf2_name (spec_die, spec_cu);
11357 if (die->tag == DW_TAG_class_type)
11358 return dwarf2_name (die, cu);
11360 while (die->tag != DW_TAG_compile_unit);
11364 case DW_TAG_class_type:
11365 case DW_TAG_interface_type:
11366 case DW_TAG_structure_type:
11367 case DW_TAG_union_type:
11368 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11369 structures or unions. These were of the form "._%d" in GCC 4.1,
11370 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11371 and GCC 4.4. We work around this problem by ignoring these. */
11372 if (strncmp (DW_STRING (attr), "._", 2) == 0
11373 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11381 if (!DW_STRING_IS_CANONICAL (attr))
11384 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11385 &cu->objfile->objfile_obstack);
11386 DW_STRING_IS_CANONICAL (attr) = 1;
11388 return DW_STRING (attr);
11391 /* Return the die that this die in an extension of, or NULL if there
11392 is none. *EXT_CU is the CU containing DIE on input, and the CU
11393 containing the return value on output. */
11395 static struct die_info *
11396 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11398 struct attribute *attr;
11400 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11404 return follow_die_ref (die, attr, ext_cu);
11407 /* Convert a DIE tag into its string name. */
11410 dwarf_tag_name (unsigned tag)
11414 case DW_TAG_padding:
11415 return "DW_TAG_padding";
11416 case DW_TAG_array_type:
11417 return "DW_TAG_array_type";
11418 case DW_TAG_class_type:
11419 return "DW_TAG_class_type";
11420 case DW_TAG_entry_point:
11421 return "DW_TAG_entry_point";
11422 case DW_TAG_enumeration_type:
11423 return "DW_TAG_enumeration_type";
11424 case DW_TAG_formal_parameter:
11425 return "DW_TAG_formal_parameter";
11426 case DW_TAG_imported_declaration:
11427 return "DW_TAG_imported_declaration";
11429 return "DW_TAG_label";
11430 case DW_TAG_lexical_block:
11431 return "DW_TAG_lexical_block";
11432 case DW_TAG_member:
11433 return "DW_TAG_member";
11434 case DW_TAG_pointer_type:
11435 return "DW_TAG_pointer_type";
11436 case DW_TAG_reference_type:
11437 return "DW_TAG_reference_type";
11438 case DW_TAG_compile_unit:
11439 return "DW_TAG_compile_unit";
11440 case DW_TAG_string_type:
11441 return "DW_TAG_string_type";
11442 case DW_TAG_structure_type:
11443 return "DW_TAG_structure_type";
11444 case DW_TAG_subroutine_type:
11445 return "DW_TAG_subroutine_type";
11446 case DW_TAG_typedef:
11447 return "DW_TAG_typedef";
11448 case DW_TAG_union_type:
11449 return "DW_TAG_union_type";
11450 case DW_TAG_unspecified_parameters:
11451 return "DW_TAG_unspecified_parameters";
11452 case DW_TAG_variant:
11453 return "DW_TAG_variant";
11454 case DW_TAG_common_block:
11455 return "DW_TAG_common_block";
11456 case DW_TAG_common_inclusion:
11457 return "DW_TAG_common_inclusion";
11458 case DW_TAG_inheritance:
11459 return "DW_TAG_inheritance";
11460 case DW_TAG_inlined_subroutine:
11461 return "DW_TAG_inlined_subroutine";
11462 case DW_TAG_module:
11463 return "DW_TAG_module";
11464 case DW_TAG_ptr_to_member_type:
11465 return "DW_TAG_ptr_to_member_type";
11466 case DW_TAG_set_type:
11467 return "DW_TAG_set_type";
11468 case DW_TAG_subrange_type:
11469 return "DW_TAG_subrange_type";
11470 case DW_TAG_with_stmt:
11471 return "DW_TAG_with_stmt";
11472 case DW_TAG_access_declaration:
11473 return "DW_TAG_access_declaration";
11474 case DW_TAG_base_type:
11475 return "DW_TAG_base_type";
11476 case DW_TAG_catch_block:
11477 return "DW_TAG_catch_block";
11478 case DW_TAG_const_type:
11479 return "DW_TAG_const_type";
11480 case DW_TAG_constant:
11481 return "DW_TAG_constant";
11482 case DW_TAG_enumerator:
11483 return "DW_TAG_enumerator";
11484 case DW_TAG_file_type:
11485 return "DW_TAG_file_type";
11486 case DW_TAG_friend:
11487 return "DW_TAG_friend";
11488 case DW_TAG_namelist:
11489 return "DW_TAG_namelist";
11490 case DW_TAG_namelist_item:
11491 return "DW_TAG_namelist_item";
11492 case DW_TAG_packed_type:
11493 return "DW_TAG_packed_type";
11494 case DW_TAG_subprogram:
11495 return "DW_TAG_subprogram";
11496 case DW_TAG_template_type_param:
11497 return "DW_TAG_template_type_param";
11498 case DW_TAG_template_value_param:
11499 return "DW_TAG_template_value_param";
11500 case DW_TAG_thrown_type:
11501 return "DW_TAG_thrown_type";
11502 case DW_TAG_try_block:
11503 return "DW_TAG_try_block";
11504 case DW_TAG_variant_part:
11505 return "DW_TAG_variant_part";
11506 case DW_TAG_variable:
11507 return "DW_TAG_variable";
11508 case DW_TAG_volatile_type:
11509 return "DW_TAG_volatile_type";
11510 case DW_TAG_dwarf_procedure:
11511 return "DW_TAG_dwarf_procedure";
11512 case DW_TAG_restrict_type:
11513 return "DW_TAG_restrict_type";
11514 case DW_TAG_interface_type:
11515 return "DW_TAG_interface_type";
11516 case DW_TAG_namespace:
11517 return "DW_TAG_namespace";
11518 case DW_TAG_imported_module:
11519 return "DW_TAG_imported_module";
11520 case DW_TAG_unspecified_type:
11521 return "DW_TAG_unspecified_type";
11522 case DW_TAG_partial_unit:
11523 return "DW_TAG_partial_unit";
11524 case DW_TAG_imported_unit:
11525 return "DW_TAG_imported_unit";
11526 case DW_TAG_condition:
11527 return "DW_TAG_condition";
11528 case DW_TAG_shared_type:
11529 return "DW_TAG_shared_type";
11530 case DW_TAG_type_unit:
11531 return "DW_TAG_type_unit";
11532 case DW_TAG_MIPS_loop:
11533 return "DW_TAG_MIPS_loop";
11534 case DW_TAG_HP_array_descriptor:
11535 return "DW_TAG_HP_array_descriptor";
11536 case DW_TAG_format_label:
11537 return "DW_TAG_format_label";
11538 case DW_TAG_function_template:
11539 return "DW_TAG_function_template";
11540 case DW_TAG_class_template:
11541 return "DW_TAG_class_template";
11542 case DW_TAG_GNU_BINCL:
11543 return "DW_TAG_GNU_BINCL";
11544 case DW_TAG_GNU_EINCL:
11545 return "DW_TAG_GNU_EINCL";
11546 case DW_TAG_upc_shared_type:
11547 return "DW_TAG_upc_shared_type";
11548 case DW_TAG_upc_strict_type:
11549 return "DW_TAG_upc_strict_type";
11550 case DW_TAG_upc_relaxed_type:
11551 return "DW_TAG_upc_relaxed_type";
11552 case DW_TAG_PGI_kanji_type:
11553 return "DW_TAG_PGI_kanji_type";
11554 case DW_TAG_PGI_interface_block:
11555 return "DW_TAG_PGI_interface_block";
11557 return "DW_TAG_<unknown>";
11561 /* Convert a DWARF attribute code into its string name. */
11564 dwarf_attr_name (unsigned attr)
11568 case DW_AT_sibling:
11569 return "DW_AT_sibling";
11570 case DW_AT_location:
11571 return "DW_AT_location";
11573 return "DW_AT_name";
11574 case DW_AT_ordering:
11575 return "DW_AT_ordering";
11576 case DW_AT_subscr_data:
11577 return "DW_AT_subscr_data";
11578 case DW_AT_byte_size:
11579 return "DW_AT_byte_size";
11580 case DW_AT_bit_offset:
11581 return "DW_AT_bit_offset";
11582 case DW_AT_bit_size:
11583 return "DW_AT_bit_size";
11584 case DW_AT_element_list:
11585 return "DW_AT_element_list";
11586 case DW_AT_stmt_list:
11587 return "DW_AT_stmt_list";
11589 return "DW_AT_low_pc";
11590 case DW_AT_high_pc:
11591 return "DW_AT_high_pc";
11592 case DW_AT_language:
11593 return "DW_AT_language";
11595 return "DW_AT_member";
11597 return "DW_AT_discr";
11598 case DW_AT_discr_value:
11599 return "DW_AT_discr_value";
11600 case DW_AT_visibility:
11601 return "DW_AT_visibility";
11603 return "DW_AT_import";
11604 case DW_AT_string_length:
11605 return "DW_AT_string_length";
11606 case DW_AT_common_reference:
11607 return "DW_AT_common_reference";
11608 case DW_AT_comp_dir:
11609 return "DW_AT_comp_dir";
11610 case DW_AT_const_value:
11611 return "DW_AT_const_value";
11612 case DW_AT_containing_type:
11613 return "DW_AT_containing_type";
11614 case DW_AT_default_value:
11615 return "DW_AT_default_value";
11617 return "DW_AT_inline";
11618 case DW_AT_is_optional:
11619 return "DW_AT_is_optional";
11620 case DW_AT_lower_bound:
11621 return "DW_AT_lower_bound";
11622 case DW_AT_producer:
11623 return "DW_AT_producer";
11624 case DW_AT_prototyped:
11625 return "DW_AT_prototyped";
11626 case DW_AT_return_addr:
11627 return "DW_AT_return_addr";
11628 case DW_AT_start_scope:
11629 return "DW_AT_start_scope";
11630 case DW_AT_bit_stride:
11631 return "DW_AT_bit_stride";
11632 case DW_AT_upper_bound:
11633 return "DW_AT_upper_bound";
11634 case DW_AT_abstract_origin:
11635 return "DW_AT_abstract_origin";
11636 case DW_AT_accessibility:
11637 return "DW_AT_accessibility";
11638 case DW_AT_address_class:
11639 return "DW_AT_address_class";
11640 case DW_AT_artificial:
11641 return "DW_AT_artificial";
11642 case DW_AT_base_types:
11643 return "DW_AT_base_types";
11644 case DW_AT_calling_convention:
11645 return "DW_AT_calling_convention";
11647 return "DW_AT_count";
11648 case DW_AT_data_member_location:
11649 return "DW_AT_data_member_location";
11650 case DW_AT_decl_column:
11651 return "DW_AT_decl_column";
11652 case DW_AT_decl_file:
11653 return "DW_AT_decl_file";
11654 case DW_AT_decl_line:
11655 return "DW_AT_decl_line";
11656 case DW_AT_declaration:
11657 return "DW_AT_declaration";
11658 case DW_AT_discr_list:
11659 return "DW_AT_discr_list";
11660 case DW_AT_encoding:
11661 return "DW_AT_encoding";
11662 case DW_AT_external:
11663 return "DW_AT_external";
11664 case DW_AT_frame_base:
11665 return "DW_AT_frame_base";
11667 return "DW_AT_friend";
11668 case DW_AT_identifier_case:
11669 return "DW_AT_identifier_case";
11670 case DW_AT_macro_info:
11671 return "DW_AT_macro_info";
11672 case DW_AT_namelist_items:
11673 return "DW_AT_namelist_items";
11674 case DW_AT_priority:
11675 return "DW_AT_priority";
11676 case DW_AT_segment:
11677 return "DW_AT_segment";
11678 case DW_AT_specification:
11679 return "DW_AT_specification";
11680 case DW_AT_static_link:
11681 return "DW_AT_static_link";
11683 return "DW_AT_type";
11684 case DW_AT_use_location:
11685 return "DW_AT_use_location";
11686 case DW_AT_variable_parameter:
11687 return "DW_AT_variable_parameter";
11688 case DW_AT_virtuality:
11689 return "DW_AT_virtuality";
11690 case DW_AT_vtable_elem_location:
11691 return "DW_AT_vtable_elem_location";
11692 /* DWARF 3 values. */
11693 case DW_AT_allocated:
11694 return "DW_AT_allocated";
11695 case DW_AT_associated:
11696 return "DW_AT_associated";
11697 case DW_AT_data_location:
11698 return "DW_AT_data_location";
11699 case DW_AT_byte_stride:
11700 return "DW_AT_byte_stride";
11701 case DW_AT_entry_pc:
11702 return "DW_AT_entry_pc";
11703 case DW_AT_use_UTF8:
11704 return "DW_AT_use_UTF8";
11705 case DW_AT_extension:
11706 return "DW_AT_extension";
11708 return "DW_AT_ranges";
11709 case DW_AT_trampoline:
11710 return "DW_AT_trampoline";
11711 case DW_AT_call_column:
11712 return "DW_AT_call_column";
11713 case DW_AT_call_file:
11714 return "DW_AT_call_file";
11715 case DW_AT_call_line:
11716 return "DW_AT_call_line";
11717 case DW_AT_description:
11718 return "DW_AT_description";
11719 case DW_AT_binary_scale:
11720 return "DW_AT_binary_scale";
11721 case DW_AT_decimal_scale:
11722 return "DW_AT_decimal_scale";
11724 return "DW_AT_small";
11725 case DW_AT_decimal_sign:
11726 return "DW_AT_decimal_sign";
11727 case DW_AT_digit_count:
11728 return "DW_AT_digit_count";
11729 case DW_AT_picture_string:
11730 return "DW_AT_picture_string";
11731 case DW_AT_mutable:
11732 return "DW_AT_mutable";
11733 case DW_AT_threads_scaled:
11734 return "DW_AT_threads_scaled";
11735 case DW_AT_explicit:
11736 return "DW_AT_explicit";
11737 case DW_AT_object_pointer:
11738 return "DW_AT_object_pointer";
11739 case DW_AT_endianity:
11740 return "DW_AT_endianity";
11741 case DW_AT_elemental:
11742 return "DW_AT_elemental";
11744 return "DW_AT_pure";
11745 case DW_AT_recursive:
11746 return "DW_AT_recursive";
11747 /* DWARF 4 values. */
11748 case DW_AT_signature:
11749 return "DW_AT_signature";
11750 case DW_AT_linkage_name:
11751 return "DW_AT_linkage_name";
11752 /* SGI/MIPS extensions. */
11753 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11754 case DW_AT_MIPS_fde:
11755 return "DW_AT_MIPS_fde";
11757 case DW_AT_MIPS_loop_begin:
11758 return "DW_AT_MIPS_loop_begin";
11759 case DW_AT_MIPS_tail_loop_begin:
11760 return "DW_AT_MIPS_tail_loop_begin";
11761 case DW_AT_MIPS_epilog_begin:
11762 return "DW_AT_MIPS_epilog_begin";
11763 case DW_AT_MIPS_loop_unroll_factor:
11764 return "DW_AT_MIPS_loop_unroll_factor";
11765 case DW_AT_MIPS_software_pipeline_depth:
11766 return "DW_AT_MIPS_software_pipeline_depth";
11767 case DW_AT_MIPS_linkage_name:
11768 return "DW_AT_MIPS_linkage_name";
11769 case DW_AT_MIPS_stride:
11770 return "DW_AT_MIPS_stride";
11771 case DW_AT_MIPS_abstract_name:
11772 return "DW_AT_MIPS_abstract_name";
11773 case DW_AT_MIPS_clone_origin:
11774 return "DW_AT_MIPS_clone_origin";
11775 case DW_AT_MIPS_has_inlines:
11776 return "DW_AT_MIPS_has_inlines";
11777 /* HP extensions. */
11778 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11779 case DW_AT_HP_block_index:
11780 return "DW_AT_HP_block_index";
11782 case DW_AT_HP_unmodifiable:
11783 return "DW_AT_HP_unmodifiable";
11784 case DW_AT_HP_actuals_stmt_list:
11785 return "DW_AT_HP_actuals_stmt_list";
11786 case DW_AT_HP_proc_per_section:
11787 return "DW_AT_HP_proc_per_section";
11788 case DW_AT_HP_raw_data_ptr:
11789 return "DW_AT_HP_raw_data_ptr";
11790 case DW_AT_HP_pass_by_reference:
11791 return "DW_AT_HP_pass_by_reference";
11792 case DW_AT_HP_opt_level:
11793 return "DW_AT_HP_opt_level";
11794 case DW_AT_HP_prof_version_id:
11795 return "DW_AT_HP_prof_version_id";
11796 case DW_AT_HP_opt_flags:
11797 return "DW_AT_HP_opt_flags";
11798 case DW_AT_HP_cold_region_low_pc:
11799 return "DW_AT_HP_cold_region_low_pc";
11800 case DW_AT_HP_cold_region_high_pc:
11801 return "DW_AT_HP_cold_region_high_pc";
11802 case DW_AT_HP_all_variables_modifiable:
11803 return "DW_AT_HP_all_variables_modifiable";
11804 case DW_AT_HP_linkage_name:
11805 return "DW_AT_HP_linkage_name";
11806 case DW_AT_HP_prof_flags:
11807 return "DW_AT_HP_prof_flags";
11808 /* GNU extensions. */
11809 case DW_AT_sf_names:
11810 return "DW_AT_sf_names";
11811 case DW_AT_src_info:
11812 return "DW_AT_src_info";
11813 case DW_AT_mac_info:
11814 return "DW_AT_mac_info";
11815 case DW_AT_src_coords:
11816 return "DW_AT_src_coords";
11817 case DW_AT_body_begin:
11818 return "DW_AT_body_begin";
11819 case DW_AT_body_end:
11820 return "DW_AT_body_end";
11821 case DW_AT_GNU_vector:
11822 return "DW_AT_GNU_vector";
11823 case DW_AT_GNU_odr_signature:
11824 return "DW_AT_GNU_odr_signature";
11825 /* VMS extensions. */
11826 case DW_AT_VMS_rtnbeg_pd_address:
11827 return "DW_AT_VMS_rtnbeg_pd_address";
11828 /* UPC extension. */
11829 case DW_AT_upc_threads_scaled:
11830 return "DW_AT_upc_threads_scaled";
11831 /* PGI (STMicroelectronics) extensions. */
11832 case DW_AT_PGI_lbase:
11833 return "DW_AT_PGI_lbase";
11834 case DW_AT_PGI_soffset:
11835 return "DW_AT_PGI_soffset";
11836 case DW_AT_PGI_lstride:
11837 return "DW_AT_PGI_lstride";
11839 return "DW_AT_<unknown>";
11843 /* Convert a DWARF value form code into its string name. */
11846 dwarf_form_name (unsigned form)
11851 return "DW_FORM_addr";
11852 case DW_FORM_block2:
11853 return "DW_FORM_block2";
11854 case DW_FORM_block4:
11855 return "DW_FORM_block4";
11856 case DW_FORM_data2:
11857 return "DW_FORM_data2";
11858 case DW_FORM_data4:
11859 return "DW_FORM_data4";
11860 case DW_FORM_data8:
11861 return "DW_FORM_data8";
11862 case DW_FORM_string:
11863 return "DW_FORM_string";
11864 case DW_FORM_block:
11865 return "DW_FORM_block";
11866 case DW_FORM_block1:
11867 return "DW_FORM_block1";
11868 case DW_FORM_data1:
11869 return "DW_FORM_data1";
11871 return "DW_FORM_flag";
11872 case DW_FORM_sdata:
11873 return "DW_FORM_sdata";
11875 return "DW_FORM_strp";
11876 case DW_FORM_udata:
11877 return "DW_FORM_udata";
11878 case DW_FORM_ref_addr:
11879 return "DW_FORM_ref_addr";
11881 return "DW_FORM_ref1";
11883 return "DW_FORM_ref2";
11885 return "DW_FORM_ref4";
11887 return "DW_FORM_ref8";
11888 case DW_FORM_ref_udata:
11889 return "DW_FORM_ref_udata";
11890 case DW_FORM_indirect:
11891 return "DW_FORM_indirect";
11892 case DW_FORM_sec_offset:
11893 return "DW_FORM_sec_offset";
11894 case DW_FORM_exprloc:
11895 return "DW_FORM_exprloc";
11896 case DW_FORM_flag_present:
11897 return "DW_FORM_flag_present";
11899 return "DW_FORM_sig8";
11901 return "DW_FORM_<unknown>";
11905 /* Convert a DWARF stack opcode into its string name. */
11908 dwarf_stack_op_name (unsigned op, int def)
11913 return "DW_OP_addr";
11915 return "DW_OP_deref";
11916 case DW_OP_const1u:
11917 return "DW_OP_const1u";
11918 case DW_OP_const1s:
11919 return "DW_OP_const1s";
11920 case DW_OP_const2u:
11921 return "DW_OP_const2u";
11922 case DW_OP_const2s:
11923 return "DW_OP_const2s";
11924 case DW_OP_const4u:
11925 return "DW_OP_const4u";
11926 case DW_OP_const4s:
11927 return "DW_OP_const4s";
11928 case DW_OP_const8u:
11929 return "DW_OP_const8u";
11930 case DW_OP_const8s:
11931 return "DW_OP_const8s";
11933 return "DW_OP_constu";
11935 return "DW_OP_consts";
11937 return "DW_OP_dup";
11939 return "DW_OP_drop";
11941 return "DW_OP_over";
11943 return "DW_OP_pick";
11945 return "DW_OP_swap";
11947 return "DW_OP_rot";
11949 return "DW_OP_xderef";
11951 return "DW_OP_abs";
11953 return "DW_OP_and";
11955 return "DW_OP_div";
11957 return "DW_OP_minus";
11959 return "DW_OP_mod";
11961 return "DW_OP_mul";
11963 return "DW_OP_neg";
11965 return "DW_OP_not";
11969 return "DW_OP_plus";
11970 case DW_OP_plus_uconst:
11971 return "DW_OP_plus_uconst";
11973 return "DW_OP_shl";
11975 return "DW_OP_shr";
11977 return "DW_OP_shra";
11979 return "DW_OP_xor";
11981 return "DW_OP_bra";
11995 return "DW_OP_skip";
11997 return "DW_OP_lit0";
11999 return "DW_OP_lit1";
12001 return "DW_OP_lit2";
12003 return "DW_OP_lit3";
12005 return "DW_OP_lit4";
12007 return "DW_OP_lit5";
12009 return "DW_OP_lit6";
12011 return "DW_OP_lit7";
12013 return "DW_OP_lit8";
12015 return "DW_OP_lit9";
12017 return "DW_OP_lit10";
12019 return "DW_OP_lit11";
12021 return "DW_OP_lit12";
12023 return "DW_OP_lit13";
12025 return "DW_OP_lit14";
12027 return "DW_OP_lit15";
12029 return "DW_OP_lit16";
12031 return "DW_OP_lit17";
12033 return "DW_OP_lit18";
12035 return "DW_OP_lit19";
12037 return "DW_OP_lit20";
12039 return "DW_OP_lit21";
12041 return "DW_OP_lit22";
12043 return "DW_OP_lit23";
12045 return "DW_OP_lit24";
12047 return "DW_OP_lit25";
12049 return "DW_OP_lit26";
12051 return "DW_OP_lit27";
12053 return "DW_OP_lit28";
12055 return "DW_OP_lit29";
12057 return "DW_OP_lit30";
12059 return "DW_OP_lit31";
12061 return "DW_OP_reg0";
12063 return "DW_OP_reg1";
12065 return "DW_OP_reg2";
12067 return "DW_OP_reg3";
12069 return "DW_OP_reg4";
12071 return "DW_OP_reg5";
12073 return "DW_OP_reg6";
12075 return "DW_OP_reg7";
12077 return "DW_OP_reg8";
12079 return "DW_OP_reg9";
12081 return "DW_OP_reg10";
12083 return "DW_OP_reg11";
12085 return "DW_OP_reg12";
12087 return "DW_OP_reg13";
12089 return "DW_OP_reg14";
12091 return "DW_OP_reg15";
12093 return "DW_OP_reg16";
12095 return "DW_OP_reg17";
12097 return "DW_OP_reg18";
12099 return "DW_OP_reg19";
12101 return "DW_OP_reg20";
12103 return "DW_OP_reg21";
12105 return "DW_OP_reg22";
12107 return "DW_OP_reg23";
12109 return "DW_OP_reg24";
12111 return "DW_OP_reg25";
12113 return "DW_OP_reg26";
12115 return "DW_OP_reg27";
12117 return "DW_OP_reg28";
12119 return "DW_OP_reg29";
12121 return "DW_OP_reg30";
12123 return "DW_OP_reg31";
12125 return "DW_OP_breg0";
12127 return "DW_OP_breg1";
12129 return "DW_OP_breg2";
12131 return "DW_OP_breg3";
12133 return "DW_OP_breg4";
12135 return "DW_OP_breg5";
12137 return "DW_OP_breg6";
12139 return "DW_OP_breg7";
12141 return "DW_OP_breg8";
12143 return "DW_OP_breg9";
12145 return "DW_OP_breg10";
12147 return "DW_OP_breg11";
12149 return "DW_OP_breg12";
12151 return "DW_OP_breg13";
12153 return "DW_OP_breg14";
12155 return "DW_OP_breg15";
12157 return "DW_OP_breg16";
12159 return "DW_OP_breg17";
12161 return "DW_OP_breg18";
12163 return "DW_OP_breg19";
12165 return "DW_OP_breg20";
12167 return "DW_OP_breg21";
12169 return "DW_OP_breg22";
12171 return "DW_OP_breg23";
12173 return "DW_OP_breg24";
12175 return "DW_OP_breg25";
12177 return "DW_OP_breg26";
12179 return "DW_OP_breg27";
12181 return "DW_OP_breg28";
12183 return "DW_OP_breg29";
12185 return "DW_OP_breg30";
12187 return "DW_OP_breg31";
12189 return "DW_OP_regx";
12191 return "DW_OP_fbreg";
12193 return "DW_OP_bregx";
12195 return "DW_OP_piece";
12196 case DW_OP_deref_size:
12197 return "DW_OP_deref_size";
12198 case DW_OP_xderef_size:
12199 return "DW_OP_xderef_size";
12201 return "DW_OP_nop";
12202 /* DWARF 3 extensions. */
12203 case DW_OP_push_object_address:
12204 return "DW_OP_push_object_address";
12206 return "DW_OP_call2";
12208 return "DW_OP_call4";
12209 case DW_OP_call_ref:
12210 return "DW_OP_call_ref";
12211 case DW_OP_form_tls_address:
12212 return "DW_OP_form_tls_address";
12213 case DW_OP_call_frame_cfa:
12214 return "DW_OP_call_frame_cfa";
12215 case DW_OP_bit_piece:
12216 return "DW_OP_bit_piece";
12217 /* DWARF 4 extensions. */
12218 case DW_OP_implicit_value:
12219 return "DW_OP_implicit_value";
12220 case DW_OP_stack_value:
12221 return "DW_OP_stack_value";
12222 /* GNU extensions. */
12223 case DW_OP_GNU_push_tls_address:
12224 return "DW_OP_GNU_push_tls_address";
12225 case DW_OP_GNU_uninit:
12226 return "DW_OP_GNU_uninit";
12228 return def ? "OP_<unknown>" : NULL;
12233 dwarf_bool_name (unsigned mybool)
12241 /* Convert a DWARF type code into its string name. */
12244 dwarf_type_encoding_name (unsigned enc)
12249 return "DW_ATE_void";
12250 case DW_ATE_address:
12251 return "DW_ATE_address";
12252 case DW_ATE_boolean:
12253 return "DW_ATE_boolean";
12254 case DW_ATE_complex_float:
12255 return "DW_ATE_complex_float";
12257 return "DW_ATE_float";
12258 case DW_ATE_signed:
12259 return "DW_ATE_signed";
12260 case DW_ATE_signed_char:
12261 return "DW_ATE_signed_char";
12262 case DW_ATE_unsigned:
12263 return "DW_ATE_unsigned";
12264 case DW_ATE_unsigned_char:
12265 return "DW_ATE_unsigned_char";
12267 case DW_ATE_imaginary_float:
12268 return "DW_ATE_imaginary_float";
12269 case DW_ATE_packed_decimal:
12270 return "DW_ATE_packed_decimal";
12271 case DW_ATE_numeric_string:
12272 return "DW_ATE_numeric_string";
12273 case DW_ATE_edited:
12274 return "DW_ATE_edited";
12275 case DW_ATE_signed_fixed:
12276 return "DW_ATE_signed_fixed";
12277 case DW_ATE_unsigned_fixed:
12278 return "DW_ATE_unsigned_fixed";
12279 case DW_ATE_decimal_float:
12280 return "DW_ATE_decimal_float";
12283 return "DW_ATE_UTF";
12284 /* HP extensions. */
12285 case DW_ATE_HP_float80:
12286 return "DW_ATE_HP_float80";
12287 case DW_ATE_HP_complex_float80:
12288 return "DW_ATE_HP_complex_float80";
12289 case DW_ATE_HP_float128:
12290 return "DW_ATE_HP_float128";
12291 case DW_ATE_HP_complex_float128:
12292 return "DW_ATE_HP_complex_float128";
12293 case DW_ATE_HP_floathpintel:
12294 return "DW_ATE_HP_floathpintel";
12295 case DW_ATE_HP_imaginary_float80:
12296 return "DW_ATE_HP_imaginary_float80";
12297 case DW_ATE_HP_imaginary_float128:
12298 return "DW_ATE_HP_imaginary_float128";
12300 return "DW_ATE_<unknown>";
12304 /* Convert a DWARF call frame info operation to its string name. */
12308 dwarf_cfi_name (unsigned cfi_opc)
12312 case DW_CFA_advance_loc:
12313 return "DW_CFA_advance_loc";
12314 case DW_CFA_offset:
12315 return "DW_CFA_offset";
12316 case DW_CFA_restore:
12317 return "DW_CFA_restore";
12319 return "DW_CFA_nop";
12320 case DW_CFA_set_loc:
12321 return "DW_CFA_set_loc";
12322 case DW_CFA_advance_loc1:
12323 return "DW_CFA_advance_loc1";
12324 case DW_CFA_advance_loc2:
12325 return "DW_CFA_advance_loc2";
12326 case DW_CFA_advance_loc4:
12327 return "DW_CFA_advance_loc4";
12328 case DW_CFA_offset_extended:
12329 return "DW_CFA_offset_extended";
12330 case DW_CFA_restore_extended:
12331 return "DW_CFA_restore_extended";
12332 case DW_CFA_undefined:
12333 return "DW_CFA_undefined";
12334 case DW_CFA_same_value:
12335 return "DW_CFA_same_value";
12336 case DW_CFA_register:
12337 return "DW_CFA_register";
12338 case DW_CFA_remember_state:
12339 return "DW_CFA_remember_state";
12340 case DW_CFA_restore_state:
12341 return "DW_CFA_restore_state";
12342 case DW_CFA_def_cfa:
12343 return "DW_CFA_def_cfa";
12344 case DW_CFA_def_cfa_register:
12345 return "DW_CFA_def_cfa_register";
12346 case DW_CFA_def_cfa_offset:
12347 return "DW_CFA_def_cfa_offset";
12349 case DW_CFA_def_cfa_expression:
12350 return "DW_CFA_def_cfa_expression";
12351 case DW_CFA_expression:
12352 return "DW_CFA_expression";
12353 case DW_CFA_offset_extended_sf:
12354 return "DW_CFA_offset_extended_sf";
12355 case DW_CFA_def_cfa_sf:
12356 return "DW_CFA_def_cfa_sf";
12357 case DW_CFA_def_cfa_offset_sf:
12358 return "DW_CFA_def_cfa_offset_sf";
12359 case DW_CFA_val_offset:
12360 return "DW_CFA_val_offset";
12361 case DW_CFA_val_offset_sf:
12362 return "DW_CFA_val_offset_sf";
12363 case DW_CFA_val_expression:
12364 return "DW_CFA_val_expression";
12365 /* SGI/MIPS specific. */
12366 case DW_CFA_MIPS_advance_loc8:
12367 return "DW_CFA_MIPS_advance_loc8";
12368 /* GNU extensions. */
12369 case DW_CFA_GNU_window_save:
12370 return "DW_CFA_GNU_window_save";
12371 case DW_CFA_GNU_args_size:
12372 return "DW_CFA_GNU_args_size";
12373 case DW_CFA_GNU_negative_offset_extended:
12374 return "DW_CFA_GNU_negative_offset_extended";
12376 return "DW_CFA_<unknown>";
12382 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12386 print_spaces (indent, f);
12387 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12388 dwarf_tag_name (die->tag), die->abbrev, die->offset);
12390 if (die->parent != NULL)
12392 print_spaces (indent, f);
12393 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12394 die->parent->offset);
12397 print_spaces (indent, f);
12398 fprintf_unfiltered (f, " has children: %s\n",
12399 dwarf_bool_name (die->child != NULL));
12401 print_spaces (indent, f);
12402 fprintf_unfiltered (f, " attributes:\n");
12404 for (i = 0; i < die->num_attrs; ++i)
12406 print_spaces (indent, f);
12407 fprintf_unfiltered (f, " %s (%s) ",
12408 dwarf_attr_name (die->attrs[i].name),
12409 dwarf_form_name (die->attrs[i].form));
12411 switch (die->attrs[i].form)
12413 case DW_FORM_ref_addr:
12415 fprintf_unfiltered (f, "address: ");
12416 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12418 case DW_FORM_block2:
12419 case DW_FORM_block4:
12420 case DW_FORM_block:
12421 case DW_FORM_block1:
12422 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12424 case DW_FORM_exprloc:
12425 fprintf_unfiltered (f, "expression: size %u",
12426 DW_BLOCK (&die->attrs[i])->size);
12431 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12432 (long) (DW_ADDR (&die->attrs[i])));
12434 case DW_FORM_data1:
12435 case DW_FORM_data2:
12436 case DW_FORM_data4:
12437 case DW_FORM_data8:
12438 case DW_FORM_udata:
12439 case DW_FORM_sdata:
12440 fprintf_unfiltered (f, "constant: %s",
12441 pulongest (DW_UNSND (&die->attrs[i])));
12443 case DW_FORM_sec_offset:
12444 fprintf_unfiltered (f, "section offset: %s",
12445 pulongest (DW_UNSND (&die->attrs[i])));
12448 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12449 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12450 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12452 fprintf_unfiltered (f, "signatured type, offset: unknown");
12454 case DW_FORM_string:
12456 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12457 DW_STRING (&die->attrs[i])
12458 ? DW_STRING (&die->attrs[i]) : "",
12459 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12462 if (DW_UNSND (&die->attrs[i]))
12463 fprintf_unfiltered (f, "flag: TRUE");
12465 fprintf_unfiltered (f, "flag: FALSE");
12467 case DW_FORM_flag_present:
12468 fprintf_unfiltered (f, "flag: TRUE");
12470 case DW_FORM_indirect:
12471 /* the reader will have reduced the indirect form to
12472 the "base form" so this form should not occur */
12473 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12476 fprintf_unfiltered (f, "unsupported attribute form: %d.",
12477 die->attrs[i].form);
12480 fprintf_unfiltered (f, "\n");
12485 dump_die_for_error (struct die_info *die)
12487 dump_die_shallow (gdb_stderr, 0, die);
12491 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12493 int indent = level * 4;
12495 gdb_assert (die != NULL);
12497 if (level >= max_level)
12500 dump_die_shallow (f, indent, die);
12502 if (die->child != NULL)
12504 print_spaces (indent, f);
12505 fprintf_unfiltered (f, " Children:");
12506 if (level + 1 < max_level)
12508 fprintf_unfiltered (f, "\n");
12509 dump_die_1 (f, level + 1, max_level, die->child);
12513 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12517 if (die->sibling != NULL && level > 0)
12519 dump_die_1 (f, level, max_level, die->sibling);
12523 /* This is called from the pdie macro in gdbinit.in.
12524 It's not static so gcc will keep a copy callable from gdb. */
12527 dump_die (struct die_info *die, int max_level)
12529 dump_die_1 (gdb_stdlog, 0, max_level, die);
12533 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12537 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12543 is_ref_attr (struct attribute *attr)
12545 switch (attr->form)
12547 case DW_FORM_ref_addr:
12552 case DW_FORM_ref_udata:
12559 static unsigned int
12560 dwarf2_get_ref_die_offset (struct attribute *attr)
12562 if (is_ref_attr (attr))
12563 return DW_ADDR (attr);
12565 complaint (&symfile_complaints,
12566 _("unsupported die ref attribute form: '%s'"),
12567 dwarf_form_name (attr->form));
12571 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12572 * the value held by the attribute is not constant. */
12575 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12577 if (attr->form == DW_FORM_sdata)
12578 return DW_SND (attr);
12579 else if (attr->form == DW_FORM_udata
12580 || attr->form == DW_FORM_data1
12581 || attr->form == DW_FORM_data2
12582 || attr->form == DW_FORM_data4
12583 || attr->form == DW_FORM_data8)
12584 return DW_UNSND (attr);
12587 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12588 dwarf_form_name (attr->form));
12589 return default_value;
12593 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
12594 unit and add it to our queue.
12595 The result is non-zero if PER_CU was queued, otherwise the result is zero
12596 meaning either PER_CU is already queued or it is already loaded. */
12599 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12600 struct dwarf2_per_cu_data *per_cu)
12602 /* We may arrive here during partial symbol reading, if we need full
12603 DIEs to process an unusual case (e.g. template arguments). Do
12604 not queue PER_CU, just tell our caller to load its DIEs. */
12605 if (dwarf2_per_objfile->reading_partial_symbols)
12607 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12612 /* Mark the dependence relation so that we don't flush PER_CU
12614 dwarf2_add_dependence (this_cu, per_cu);
12616 /* If it's already on the queue, we have nothing to do. */
12617 if (per_cu->queued)
12620 /* If the compilation unit is already loaded, just mark it as
12622 if (per_cu->cu != NULL)
12624 per_cu->cu->last_used = 0;
12628 /* Add it to the queue. */
12629 queue_comp_unit (per_cu, this_cu->objfile);
12634 /* Follow reference or signature attribute ATTR of SRC_DIE.
12635 On entry *REF_CU is the CU of SRC_DIE.
12636 On exit *REF_CU is the CU of the result. */
12638 static struct die_info *
12639 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12640 struct dwarf2_cu **ref_cu)
12642 struct die_info *die;
12644 if (is_ref_attr (attr))
12645 die = follow_die_ref (src_die, attr, ref_cu);
12646 else if (attr->form == DW_FORM_sig8)
12647 die = follow_die_sig (src_die, attr, ref_cu);
12650 dump_die_for_error (src_die);
12651 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12652 (*ref_cu)->objfile->name);
12658 /* Follow reference OFFSET.
12659 On entry *REF_CU is the CU of source DIE referencing OFFSET.
12660 On exit *REF_CU is the CU of the result. */
12662 static struct die_info *
12663 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12665 struct die_info temp_die;
12666 struct dwarf2_cu *target_cu, *cu = *ref_cu;
12668 gdb_assert (cu->per_cu != NULL);
12672 if (cu->per_cu->from_debug_types)
12674 /* .debug_types CUs cannot reference anything outside their CU.
12675 If they need to, they have to reference a signatured type via
12677 if (! offset_in_cu_p (&cu->header, offset))
12680 else if (! offset_in_cu_p (&cu->header, offset))
12682 struct dwarf2_per_cu_data *per_cu;
12684 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12686 /* If necessary, add it to the queue and load its DIEs. */
12687 if (maybe_queue_comp_unit (cu, per_cu))
12688 load_full_comp_unit (per_cu, cu->objfile);
12690 target_cu = per_cu->cu;
12692 else if (cu->dies == NULL)
12694 /* We're loading full DIEs during partial symbol reading. */
12695 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12696 load_full_comp_unit (cu->per_cu, cu->objfile);
12699 *ref_cu = target_cu;
12700 temp_die.offset = offset;
12701 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12704 /* Follow reference attribute ATTR of SRC_DIE.
12705 On entry *REF_CU is the CU of SRC_DIE.
12706 On exit *REF_CU is the CU of the result. */
12708 static struct die_info *
12709 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12710 struct dwarf2_cu **ref_cu)
12712 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12713 struct dwarf2_cu *cu = *ref_cu;
12714 struct die_info *die;
12716 die = follow_die_offset (offset, ref_cu);
12718 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12719 "at 0x%x [in module %s]"),
12720 offset, src_die->offset, cu->objfile->name);
12725 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12726 value is intended for DW_OP_call*. */
12728 struct dwarf2_locexpr_baton
12729 dwarf2_fetch_die_location_block (unsigned int offset,
12730 struct dwarf2_per_cu_data *per_cu)
12732 struct dwarf2_cu *cu = per_cu->cu;
12733 struct die_info *die;
12734 struct attribute *attr;
12735 struct dwarf2_locexpr_baton retval;
12737 die = follow_die_offset (offset, &cu);
12739 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12740 offset, per_cu->cu->objfile->name);
12742 attr = dwarf2_attr (die, DW_AT_location, cu);
12745 /* DWARF: "If there is no such attribute, then there is no effect.". */
12747 retval.data = NULL;
12752 if (!attr_form_is_block (attr))
12753 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12754 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12755 offset, per_cu->cu->objfile->name);
12757 retval.data = DW_BLOCK (attr)->data;
12758 retval.size = DW_BLOCK (attr)->size;
12760 retval.per_cu = cu->per_cu;
12764 /* Follow the signature attribute ATTR in SRC_DIE.
12765 On entry *REF_CU is the CU of SRC_DIE.
12766 On exit *REF_CU is the CU of the result. */
12768 static struct die_info *
12769 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12770 struct dwarf2_cu **ref_cu)
12772 struct objfile *objfile = (*ref_cu)->objfile;
12773 struct die_info temp_die;
12774 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12775 struct dwarf2_cu *sig_cu;
12776 struct die_info *die;
12778 /* sig_type will be NULL if the signatured type is missing from
12780 if (sig_type == NULL)
12781 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12782 "at 0x%x [in module %s]"),
12783 src_die->offset, objfile->name);
12785 /* If necessary, add it to the queue and load its DIEs. */
12787 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12788 read_signatured_type (objfile, sig_type);
12790 gdb_assert (sig_type->per_cu.cu != NULL);
12792 sig_cu = sig_type->per_cu.cu;
12793 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12794 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12801 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12802 "at 0x%x [in module %s]"),
12803 sig_type->type_offset, src_die->offset, objfile->name);
12806 /* Given an offset of a signatured type, return its signatured_type. */
12808 static struct signatured_type *
12809 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12811 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12812 unsigned int length, initial_length_size;
12813 unsigned int sig_offset;
12814 struct signatured_type find_entry, *type_sig;
12816 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12817 sig_offset = (initial_length_size
12819 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12820 + 1 /*address_size*/);
12821 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12822 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12824 /* This is only used to lookup previously recorded types.
12825 If we didn't find it, it's our bug. */
12826 gdb_assert (type_sig != NULL);
12827 gdb_assert (offset == type_sig->offset);
12832 /* Read in signatured type at OFFSET and build its CU and die(s). */
12835 read_signatured_type_at_offset (struct objfile *objfile,
12836 unsigned int offset)
12838 struct signatured_type *type_sig;
12840 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12842 /* We have the section offset, but we need the signature to do the
12843 hash table lookup. */
12844 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12846 gdb_assert (type_sig->per_cu.cu == NULL);
12848 read_signatured_type (objfile, type_sig);
12850 gdb_assert (type_sig->per_cu.cu != NULL);
12853 /* Read in a signatured type and build its CU and DIEs. */
12856 read_signatured_type (struct objfile *objfile,
12857 struct signatured_type *type_sig)
12859 gdb_byte *types_ptr;
12860 struct die_reader_specs reader_specs;
12861 struct dwarf2_cu *cu;
12862 ULONGEST signature;
12863 struct cleanup *back_to, *free_cu_cleanup;
12864 struct attribute *attr;
12866 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12867 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12869 gdb_assert (type_sig->per_cu.cu == NULL);
12871 cu = xmalloc (sizeof (struct dwarf2_cu));
12872 memset (cu, 0, sizeof (struct dwarf2_cu));
12873 obstack_init (&cu->comp_unit_obstack);
12874 cu->objfile = objfile;
12875 type_sig->per_cu.cu = cu;
12876 cu->per_cu = &type_sig->per_cu;
12878 /* If an error occurs while loading, release our storage. */
12879 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12881 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12882 types_ptr, objfile->obfd);
12883 gdb_assert (signature == type_sig->signature);
12886 = htab_create_alloc_ex (cu->header.length / 12,
12890 &cu->comp_unit_obstack,
12891 hashtab_obstack_allocate,
12892 dummy_obstack_deallocate);
12894 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12895 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12897 init_cu_die_reader (&reader_specs, cu);
12899 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12902 /* We try not to read any attributes in this function, because not
12903 all objfiles needed for references have been loaded yet, and symbol
12904 table processing isn't initialized. But we have to set the CU language,
12905 or we won't be able to build types correctly. */
12906 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12908 set_cu_language (DW_UNSND (attr), cu);
12910 set_cu_language (language_minimal, cu);
12912 do_cleanups (back_to);
12914 /* We've successfully allocated this compilation unit. Let our caller
12915 clean it up when finished with it. */
12916 discard_cleanups (free_cu_cleanup);
12918 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12919 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
12922 /* Decode simple location descriptions.
12923 Given a pointer to a dwarf block that defines a location, compute
12924 the location and return the value.
12926 NOTE drow/2003-11-18: This function is called in two situations
12927 now: for the address of static or global variables (partial symbols
12928 only) and for offsets into structures which are expected to be
12929 (more or less) constant. The partial symbol case should go away,
12930 and only the constant case should remain. That will let this
12931 function complain more accurately. A few special modes are allowed
12932 without complaint for global variables (for instance, global
12933 register values and thread-local values).
12935 A location description containing no operations indicates that the
12936 object is optimized out. The return value is 0 for that case.
12937 FIXME drow/2003-11-16: No callers check for this case any more; soon all
12938 callers will only want a very basic result and this can become a
12941 Note that stack[0] is unused except as a default error return.
12942 Note that stack overflow is not yet handled. */
12945 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
12947 struct objfile *objfile = cu->objfile;
12949 int size = blk->size;
12950 gdb_byte *data = blk->data;
12951 CORE_ADDR stack[64];
12953 unsigned int bytes_read, unsnd;
12997 stack[++stacki] = op - DW_OP_lit0;
13032 stack[++stacki] = op - DW_OP_reg0;
13034 dwarf2_complex_location_expr_complaint ();
13038 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13040 stack[++stacki] = unsnd;
13042 dwarf2_complex_location_expr_complaint ();
13046 stack[++stacki] = read_address (objfile->obfd, &data[i],
13051 case DW_OP_const1u:
13052 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13056 case DW_OP_const1s:
13057 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13061 case DW_OP_const2u:
13062 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13066 case DW_OP_const2s:
13067 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13071 case DW_OP_const4u:
13072 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13076 case DW_OP_const4s:
13077 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13082 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13088 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13093 stack[stacki + 1] = stack[stacki];
13098 stack[stacki - 1] += stack[stacki];
13102 case DW_OP_plus_uconst:
13103 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13108 stack[stacki - 1] -= stack[stacki];
13113 /* If we're not the last op, then we definitely can't encode
13114 this using GDB's address_class enum. This is valid for partial
13115 global symbols, although the variable's address will be bogus
13118 dwarf2_complex_location_expr_complaint ();
13121 case DW_OP_GNU_push_tls_address:
13122 /* The top of the stack has the offset from the beginning
13123 of the thread control block at which the variable is located. */
13124 /* Nothing should follow this operator, so the top of stack would
13126 /* This is valid for partial global symbols, but the variable's
13127 address will be bogus in the psymtab. */
13129 dwarf2_complex_location_expr_complaint ();
13132 case DW_OP_GNU_uninit:
13136 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13137 dwarf_stack_op_name (op, 1));
13138 return (stack[stacki]);
13141 return (stack[stacki]);
13144 /* memory allocation interface */
13146 static struct dwarf_block *
13147 dwarf_alloc_block (struct dwarf2_cu *cu)
13149 struct dwarf_block *blk;
13151 blk = (struct dwarf_block *)
13152 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13156 static struct abbrev_info *
13157 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13159 struct abbrev_info *abbrev;
13161 abbrev = (struct abbrev_info *)
13162 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13163 memset (abbrev, 0, sizeof (struct abbrev_info));
13167 static struct die_info *
13168 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13170 struct die_info *die;
13171 size_t size = sizeof (struct die_info);
13174 size += (num_attrs - 1) * sizeof (struct attribute);
13176 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13177 memset (die, 0, sizeof (struct die_info));
13182 /* Macro support. */
13185 /* Return the full name of file number I in *LH's file name table.
13186 Use COMP_DIR as the name of the current directory of the
13187 compilation. The result is allocated using xmalloc; the caller is
13188 responsible for freeing it. */
13190 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13192 /* Is the file number a valid index into the line header's file name
13193 table? Remember that file numbers start with one, not zero. */
13194 if (1 <= file && file <= lh->num_file_names)
13196 struct file_entry *fe = &lh->file_names[file - 1];
13198 if (IS_ABSOLUTE_PATH (fe->name))
13199 return xstrdup (fe->name);
13207 dir = lh->include_dirs[fe->dir_index - 1];
13213 dir_len = strlen (dir);
13214 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13215 strcpy (full_name, dir);
13216 full_name[dir_len] = '/';
13217 strcpy (full_name + dir_len + 1, fe->name);
13221 return xstrdup (fe->name);
13226 /* The compiler produced a bogus file number. We can at least
13227 record the macro definitions made in the file, even if we
13228 won't be able to find the file by name. */
13229 char fake_name[80];
13231 sprintf (fake_name, "<bad macro file number %d>", file);
13233 complaint (&symfile_complaints,
13234 _("bad file number in macro information (%d)"),
13237 return xstrdup (fake_name);
13242 static struct macro_source_file *
13243 macro_start_file (int file, int line,
13244 struct macro_source_file *current_file,
13245 const char *comp_dir,
13246 struct line_header *lh, struct objfile *objfile)
13248 /* The full name of this source file. */
13249 char *full_name = file_full_name (file, lh, comp_dir);
13251 /* We don't create a macro table for this compilation unit
13252 at all until we actually get a filename. */
13253 if (! pending_macros)
13254 pending_macros = new_macro_table (&objfile->objfile_obstack,
13255 objfile->macro_cache);
13257 if (! current_file)
13258 /* If we have no current file, then this must be the start_file
13259 directive for the compilation unit's main source file. */
13260 current_file = macro_set_main (pending_macros, full_name);
13262 current_file = macro_include (current_file, line, full_name);
13266 return current_file;
13270 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13271 followed by a null byte. */
13273 copy_string (const char *buf, int len)
13275 char *s = xmalloc (len + 1);
13277 memcpy (s, buf, len);
13283 static const char *
13284 consume_improper_spaces (const char *p, const char *body)
13288 complaint (&symfile_complaints,
13289 _("macro definition contains spaces in formal argument list:\n`%s'"),
13301 parse_macro_definition (struct macro_source_file *file, int line,
13306 /* The body string takes one of two forms. For object-like macro
13307 definitions, it should be:
13309 <macro name> " " <definition>
13311 For function-like macro definitions, it should be:
13313 <macro name> "() " <definition>
13315 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13317 Spaces may appear only where explicitly indicated, and in the
13320 The Dwarf 2 spec says that an object-like macro's name is always
13321 followed by a space, but versions of GCC around March 2002 omit
13322 the space when the macro's definition is the empty string.
13324 The Dwarf 2 spec says that there should be no spaces between the
13325 formal arguments in a function-like macro's formal argument list,
13326 but versions of GCC around March 2002 include spaces after the
13330 /* Find the extent of the macro name. The macro name is terminated
13331 by either a space or null character (for an object-like macro) or
13332 an opening paren (for a function-like macro). */
13333 for (p = body; *p; p++)
13334 if (*p == ' ' || *p == '(')
13337 if (*p == ' ' || *p == '\0')
13339 /* It's an object-like macro. */
13340 int name_len = p - body;
13341 char *name = copy_string (body, name_len);
13342 const char *replacement;
13345 replacement = body + name_len + 1;
13348 dwarf2_macro_malformed_definition_complaint (body);
13349 replacement = body + name_len;
13352 macro_define_object (file, line, name, replacement);
13356 else if (*p == '(')
13358 /* It's a function-like macro. */
13359 char *name = copy_string (body, p - body);
13362 char **argv = xmalloc (argv_size * sizeof (*argv));
13366 p = consume_improper_spaces (p, body);
13368 /* Parse the formal argument list. */
13369 while (*p && *p != ')')
13371 /* Find the extent of the current argument name. */
13372 const char *arg_start = p;
13374 while (*p && *p != ',' && *p != ')' && *p != ' ')
13377 if (! *p || p == arg_start)
13378 dwarf2_macro_malformed_definition_complaint (body);
13381 /* Make sure argv has room for the new argument. */
13382 if (argc >= argv_size)
13385 argv = xrealloc (argv, argv_size * sizeof (*argv));
13388 argv[argc++] = copy_string (arg_start, p - arg_start);
13391 p = consume_improper_spaces (p, body);
13393 /* Consume the comma, if present. */
13398 p = consume_improper_spaces (p, body);
13407 /* Perfectly formed definition, no complaints. */
13408 macro_define_function (file, line, name,
13409 argc, (const char **) argv,
13411 else if (*p == '\0')
13413 /* Complain, but do define it. */
13414 dwarf2_macro_malformed_definition_complaint (body);
13415 macro_define_function (file, line, name,
13416 argc, (const char **) argv,
13420 /* Just complain. */
13421 dwarf2_macro_malformed_definition_complaint (body);
13424 /* Just complain. */
13425 dwarf2_macro_malformed_definition_complaint (body);
13431 for (i = 0; i < argc; i++)
13437 dwarf2_macro_malformed_definition_complaint (body);
13442 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13443 char *comp_dir, bfd *abfd,
13444 struct dwarf2_cu *cu)
13446 gdb_byte *mac_ptr, *mac_end;
13447 struct macro_source_file *current_file = 0;
13448 enum dwarf_macinfo_record_type macinfo_type;
13449 int at_commandline;
13451 dwarf2_read_section (dwarf2_per_objfile->objfile,
13452 &dwarf2_per_objfile->macinfo);
13453 if (dwarf2_per_objfile->macinfo.buffer == NULL)
13455 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13459 /* First pass: Find the name of the base filename.
13460 This filename is needed in order to process all macros whose definition
13461 (or undefinition) comes from the command line. These macros are defined
13462 before the first DW_MACINFO_start_file entry, and yet still need to be
13463 associated to the base file.
13465 To determine the base file name, we scan the macro definitions until we
13466 reach the first DW_MACINFO_start_file entry. We then initialize
13467 CURRENT_FILE accordingly so that any macro definition found before the
13468 first DW_MACINFO_start_file can still be associated to the base file. */
13470 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13471 mac_end = dwarf2_per_objfile->macinfo.buffer
13472 + dwarf2_per_objfile->macinfo.size;
13476 /* Do we at least have room for a macinfo type byte? */
13477 if (mac_ptr >= mac_end)
13479 /* Complaint is printed during the second pass as GDB will probably
13480 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13484 macinfo_type = read_1_byte (abfd, mac_ptr);
13487 switch (macinfo_type)
13489 /* A zero macinfo type indicates the end of the macro
13494 case DW_MACINFO_define:
13495 case DW_MACINFO_undef:
13496 /* Only skip the data by MAC_PTR. */
13498 unsigned int bytes_read;
13500 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13501 mac_ptr += bytes_read;
13502 read_direct_string (abfd, mac_ptr, &bytes_read);
13503 mac_ptr += bytes_read;
13507 case DW_MACINFO_start_file:
13509 unsigned int bytes_read;
13512 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13513 mac_ptr += bytes_read;
13514 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13515 mac_ptr += bytes_read;
13517 current_file = macro_start_file (file, line, current_file, comp_dir,
13522 case DW_MACINFO_end_file:
13523 /* No data to skip by MAC_PTR. */
13526 case DW_MACINFO_vendor_ext:
13527 /* Only skip the data by MAC_PTR. */
13529 unsigned int bytes_read;
13531 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13532 mac_ptr += bytes_read;
13533 read_direct_string (abfd, mac_ptr, &bytes_read);
13534 mac_ptr += bytes_read;
13541 } while (macinfo_type != 0 && current_file == NULL);
13543 /* Second pass: Process all entries.
13545 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13546 command-line macro definitions/undefinitions. This flag is unset when we
13547 reach the first DW_MACINFO_start_file entry. */
13549 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13551 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13552 GDB is still reading the definitions from command line. First
13553 DW_MACINFO_start_file will need to be ignored as it was already executed
13554 to create CURRENT_FILE for the main source holding also the command line
13555 definitions. On first met DW_MACINFO_start_file this flag is reset to
13556 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13558 at_commandline = 1;
13562 /* Do we at least have room for a macinfo type byte? */
13563 if (mac_ptr >= mac_end)
13565 dwarf2_macros_too_long_complaint ();
13569 macinfo_type = read_1_byte (abfd, mac_ptr);
13572 switch (macinfo_type)
13574 /* A zero macinfo type indicates the end of the macro
13579 case DW_MACINFO_define:
13580 case DW_MACINFO_undef:
13582 unsigned int bytes_read;
13586 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13587 mac_ptr += bytes_read;
13588 body = read_direct_string (abfd, mac_ptr, &bytes_read);
13589 mac_ptr += bytes_read;
13591 if (! current_file)
13593 /* DWARF violation as no main source is present. */
13594 complaint (&symfile_complaints,
13595 _("debug info with no main source gives macro %s "
13597 macinfo_type == DW_MACINFO_define ?
13599 macinfo_type == DW_MACINFO_undef ?
13600 _("undefinition") :
13601 _("something-or-other"), line, body);
13604 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13605 complaint (&symfile_complaints,
13606 _("debug info gives %s macro %s with %s line %d: %s"),
13607 at_commandline ? _("command-line") : _("in-file"),
13608 macinfo_type == DW_MACINFO_define ?
13610 macinfo_type == DW_MACINFO_undef ?
13611 _("undefinition") :
13612 _("something-or-other"),
13613 line == 0 ? _("zero") : _("non-zero"), line, body);
13615 if (macinfo_type == DW_MACINFO_define)
13616 parse_macro_definition (current_file, line, body);
13617 else if (macinfo_type == DW_MACINFO_undef)
13618 macro_undef (current_file, line, body);
13622 case DW_MACINFO_start_file:
13624 unsigned int bytes_read;
13627 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13628 mac_ptr += bytes_read;
13629 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13630 mac_ptr += bytes_read;
13632 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13633 complaint (&symfile_complaints,
13634 _("debug info gives source %d included "
13635 "from %s at %s line %d"),
13636 file, at_commandline ? _("command-line") : _("file"),
13637 line == 0 ? _("zero") : _("non-zero"), line);
13639 if (at_commandline)
13641 /* This DW_MACINFO_start_file was executed in the pass one. */
13642 at_commandline = 0;
13645 current_file = macro_start_file (file, line,
13646 current_file, comp_dir,
13651 case DW_MACINFO_end_file:
13652 if (! current_file)
13653 complaint (&symfile_complaints,
13654 _("macro debug info has an unmatched `close_file' directive"));
13657 current_file = current_file->included_by;
13658 if (! current_file)
13660 enum dwarf_macinfo_record_type next_type;
13662 /* GCC circa March 2002 doesn't produce the zero
13663 type byte marking the end of the compilation
13664 unit. Complain if it's not there, but exit no
13667 /* Do we at least have room for a macinfo type byte? */
13668 if (mac_ptr >= mac_end)
13670 dwarf2_macros_too_long_complaint ();
13674 /* We don't increment mac_ptr here, so this is just
13676 next_type = read_1_byte (abfd, mac_ptr);
13677 if (next_type != 0)
13678 complaint (&symfile_complaints,
13679 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13686 case DW_MACINFO_vendor_ext:
13688 unsigned int bytes_read;
13692 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13693 mac_ptr += bytes_read;
13694 string = read_direct_string (abfd, mac_ptr, &bytes_read);
13695 mac_ptr += bytes_read;
13697 /* We don't recognize any vendor extensions. */
13701 } while (macinfo_type != 0);
13704 /* Check if the attribute's form is a DW_FORM_block*
13705 if so return true else false. */
13707 attr_form_is_block (struct attribute *attr)
13709 return (attr == NULL ? 0 :
13710 attr->form == DW_FORM_block1
13711 || attr->form == DW_FORM_block2
13712 || attr->form == DW_FORM_block4
13713 || attr->form == DW_FORM_block
13714 || attr->form == DW_FORM_exprloc);
13717 /* Return non-zero if ATTR's value is a section offset --- classes
13718 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13719 You may use DW_UNSND (attr) to retrieve such offsets.
13721 Section 7.5.4, "Attribute Encodings", explains that no attribute
13722 may have a value that belongs to more than one of these classes; it
13723 would be ambiguous if we did, because we use the same forms for all
13726 attr_form_is_section_offset (struct attribute *attr)
13728 return (attr->form == DW_FORM_data4
13729 || attr->form == DW_FORM_data8
13730 || attr->form == DW_FORM_sec_offset);
13734 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13735 zero otherwise. When this function returns true, you can apply
13736 dwarf2_get_attr_constant_value to it.
13738 However, note that for some attributes you must check
13739 attr_form_is_section_offset before using this test. DW_FORM_data4
13740 and DW_FORM_data8 are members of both the constant class, and of
13741 the classes that contain offsets into other debug sections
13742 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13743 that, if an attribute's can be either a constant or one of the
13744 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13745 taken as section offsets, not constants. */
13747 attr_form_is_constant (struct attribute *attr)
13749 switch (attr->form)
13751 case DW_FORM_sdata:
13752 case DW_FORM_udata:
13753 case DW_FORM_data1:
13754 case DW_FORM_data2:
13755 case DW_FORM_data4:
13756 case DW_FORM_data8:
13764 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13765 struct dwarf2_cu *cu)
13767 if (attr_form_is_section_offset (attr)
13768 /* ".debug_loc" may not exist at all, or the offset may be outside
13769 the section. If so, fall through to the complaint in the
13771 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13773 struct dwarf2_loclist_baton *baton;
13775 baton = obstack_alloc (&cu->objfile->objfile_obstack,
13776 sizeof (struct dwarf2_loclist_baton));
13777 baton->per_cu = cu->per_cu;
13778 gdb_assert (baton->per_cu);
13780 dwarf2_read_section (dwarf2_per_objfile->objfile,
13781 &dwarf2_per_objfile->loc);
13783 /* We don't know how long the location list is, but make sure we
13784 don't run off the edge of the section. */
13785 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13786 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13787 baton->base_address = cu->base_address;
13788 if (cu->base_known == 0)
13789 complaint (&symfile_complaints,
13790 _("Location list used without specifying the CU base address."));
13792 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13793 SYMBOL_LOCATION_BATON (sym) = baton;
13797 struct dwarf2_locexpr_baton *baton;
13799 baton = obstack_alloc (&cu->objfile->objfile_obstack,
13800 sizeof (struct dwarf2_locexpr_baton));
13801 baton->per_cu = cu->per_cu;
13802 gdb_assert (baton->per_cu);
13804 if (attr_form_is_block (attr))
13806 /* Note that we're just copying the block's data pointer
13807 here, not the actual data. We're still pointing into the
13808 info_buffer for SYM's objfile; right now we never release
13809 that buffer, but when we do clean up properly this may
13811 baton->size = DW_BLOCK (attr)->size;
13812 baton->data = DW_BLOCK (attr)->data;
13816 dwarf2_invalid_attrib_class_complaint ("location description",
13817 SYMBOL_NATURAL_NAME (sym));
13819 baton->data = NULL;
13822 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13823 SYMBOL_LOCATION_BATON (sym) = baton;
13827 /* Return the OBJFILE associated with the compilation unit CU. If CU
13828 came from a separate debuginfo file, then the master objfile is
13832 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13834 struct objfile *objfile = per_cu->objfile;
13836 /* Return the master objfile, so that we can report and look up the
13837 correct file containing this variable. */
13838 if (objfile->separate_debug_objfile_backlink)
13839 objfile = objfile->separate_debug_objfile_backlink;
13844 /* Return the address size given in the compilation unit header for CU. */
13847 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13850 return per_cu->cu->header.addr_size;
13853 /* If the CU is not currently read in, we re-read its header. */
13854 struct objfile *objfile = per_cu->objfile;
13855 struct dwarf2_per_objfile *per_objfile
13856 = objfile_data (objfile, dwarf2_objfile_data_key);
13857 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13858 struct comp_unit_head cu_header;
13860 memset (&cu_header, 0, sizeof cu_header);
13861 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13862 return cu_header.addr_size;
13866 /* Return the offset size given in the compilation unit header for CU. */
13869 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13872 return per_cu->cu->header.offset_size;
13875 /* If the CU is not currently read in, we re-read its header. */
13876 struct objfile *objfile = per_cu->objfile;
13877 struct dwarf2_per_objfile *per_objfile
13878 = objfile_data (objfile, dwarf2_objfile_data_key);
13879 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13880 struct comp_unit_head cu_header;
13882 memset (&cu_header, 0, sizeof cu_header);
13883 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13884 return cu_header.offset_size;
13888 /* Return the text offset of the CU. The returned offset comes from
13889 this CU's objfile. If this objfile came from a separate debuginfo
13890 file, then the offset may be different from the corresponding
13891 offset in the parent objfile. */
13894 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13896 struct objfile *objfile = per_cu->objfile;
13898 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13901 /* Locate the .debug_info compilation unit from CU's objfile which contains
13902 the DIE at OFFSET. Raises an error on failure. */
13904 static struct dwarf2_per_cu_data *
13905 dwarf2_find_containing_comp_unit (unsigned int offset,
13906 struct objfile *objfile)
13908 struct dwarf2_per_cu_data *this_cu;
13912 high = dwarf2_per_objfile->n_comp_units - 1;
13915 int mid = low + (high - low) / 2;
13917 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13922 gdb_assert (low == high);
13923 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13926 error (_("Dwarf Error: could not find partial DIE containing "
13927 "offset 0x%lx [in module %s]"),
13928 (long) offset, bfd_get_filename (objfile->obfd));
13930 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13931 return dwarf2_per_objfile->all_comp_units[low-1];
13935 this_cu = dwarf2_per_objfile->all_comp_units[low];
13936 if (low == dwarf2_per_objfile->n_comp_units - 1
13937 && offset >= this_cu->offset + this_cu->length)
13938 error (_("invalid dwarf2 offset %u"), offset);
13939 gdb_assert (offset < this_cu->offset + this_cu->length);
13944 /* Locate the compilation unit from OBJFILE which is located at exactly
13945 OFFSET. Raises an error on failure. */
13947 static struct dwarf2_per_cu_data *
13948 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
13950 struct dwarf2_per_cu_data *this_cu;
13952 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13953 if (this_cu->offset != offset)
13954 error (_("no compilation unit with offset %u."), offset);
13958 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
13960 static struct dwarf2_cu *
13961 alloc_one_comp_unit (struct objfile *objfile)
13963 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13964 cu->objfile = objfile;
13965 obstack_init (&cu->comp_unit_obstack);
13969 /* Release one cached compilation unit, CU. We unlink it from the tree
13970 of compilation units, but we don't remove it from the read_in_chain;
13971 the caller is responsible for that.
13972 NOTE: DATA is a void * because this function is also used as a
13973 cleanup routine. */
13976 free_one_comp_unit (void *data)
13978 struct dwarf2_cu *cu = data;
13980 if (cu->per_cu != NULL)
13981 cu->per_cu->cu = NULL;
13984 obstack_free (&cu->comp_unit_obstack, NULL);
13989 /* This cleanup function is passed the address of a dwarf2_cu on the stack
13990 when we're finished with it. We can't free the pointer itself, but be
13991 sure to unlink it from the cache. Also release any associated storage
13992 and perform cache maintenance.
13994 Only used during partial symbol parsing. */
13997 free_stack_comp_unit (void *data)
13999 struct dwarf2_cu *cu = data;
14001 obstack_free (&cu->comp_unit_obstack, NULL);
14002 cu->partial_dies = NULL;
14004 if (cu->per_cu != NULL)
14006 /* This compilation unit is on the stack in our caller, so we
14007 should not xfree it. Just unlink it. */
14008 cu->per_cu->cu = NULL;
14011 /* If we had a per-cu pointer, then we may have other compilation
14012 units loaded, so age them now. */
14013 age_cached_comp_units ();
14017 /* Free all cached compilation units. */
14020 free_cached_comp_units (void *data)
14022 struct dwarf2_per_cu_data *per_cu, **last_chain;
14024 per_cu = dwarf2_per_objfile->read_in_chain;
14025 last_chain = &dwarf2_per_objfile->read_in_chain;
14026 while (per_cu != NULL)
14028 struct dwarf2_per_cu_data *next_cu;
14030 next_cu = per_cu->cu->read_in_chain;
14032 free_one_comp_unit (per_cu->cu);
14033 *last_chain = next_cu;
14039 /* Increase the age counter on each cached compilation unit, and free
14040 any that are too old. */
14043 age_cached_comp_units (void)
14045 struct dwarf2_per_cu_data *per_cu, **last_chain;
14047 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14048 per_cu = dwarf2_per_objfile->read_in_chain;
14049 while (per_cu != NULL)
14051 per_cu->cu->last_used ++;
14052 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14053 dwarf2_mark (per_cu->cu);
14054 per_cu = per_cu->cu->read_in_chain;
14057 per_cu = dwarf2_per_objfile->read_in_chain;
14058 last_chain = &dwarf2_per_objfile->read_in_chain;
14059 while (per_cu != NULL)
14061 struct dwarf2_per_cu_data *next_cu;
14063 next_cu = per_cu->cu->read_in_chain;
14065 if (!per_cu->cu->mark)
14067 free_one_comp_unit (per_cu->cu);
14068 *last_chain = next_cu;
14071 last_chain = &per_cu->cu->read_in_chain;
14077 /* Remove a single compilation unit from the cache. */
14080 free_one_cached_comp_unit (void *target_cu)
14082 struct dwarf2_per_cu_data *per_cu, **last_chain;
14084 per_cu = dwarf2_per_objfile->read_in_chain;
14085 last_chain = &dwarf2_per_objfile->read_in_chain;
14086 while (per_cu != NULL)
14088 struct dwarf2_per_cu_data *next_cu;
14090 next_cu = per_cu->cu->read_in_chain;
14092 if (per_cu->cu == target_cu)
14094 free_one_comp_unit (per_cu->cu);
14095 *last_chain = next_cu;
14099 last_chain = &per_cu->cu->read_in_chain;
14105 /* Release all extra memory associated with OBJFILE. */
14108 dwarf2_free_objfile (struct objfile *objfile)
14110 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14112 if (dwarf2_per_objfile == NULL)
14115 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14116 free_cached_comp_units (NULL);
14118 if (dwarf2_per_objfile->using_index)
14122 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14125 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14127 if (!cu->v.quick->lines)
14130 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
14132 if (cu->v.quick->file_names)
14133 xfree ((void *) cu->v.quick->file_names[j]);
14134 if (cu->v.quick->full_names)
14135 xfree ((void *) cu->v.quick->full_names[j]);
14138 free_line_header (cu->v.quick->lines);
14142 /* Everything else should be on the objfile obstack. */
14145 /* A pair of DIE offset and GDB type pointer. We store these
14146 in a hash table separate from the DIEs, and preserve them
14147 when the DIEs are flushed out of cache. */
14149 struct dwarf2_offset_and_type
14151 unsigned int offset;
14155 /* Hash function for a dwarf2_offset_and_type. */
14158 offset_and_type_hash (const void *item)
14160 const struct dwarf2_offset_and_type *ofs = item;
14162 return ofs->offset;
14165 /* Equality function for a dwarf2_offset_and_type. */
14168 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14170 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14171 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14173 return ofs_lhs->offset == ofs_rhs->offset;
14176 /* Set the type associated with DIE to TYPE. Save it in CU's hash
14177 table if necessary. For convenience, return TYPE.
14179 The DIEs reading must have careful ordering to:
14180 * Not cause infite loops trying to read in DIEs as a prerequisite for
14181 reading current DIE.
14182 * Not trying to dereference contents of still incompletely read in types
14183 while reading in other DIEs.
14184 * Enable referencing still incompletely read in types just by a pointer to
14185 the type without accessing its fields.
14187 Therefore caller should follow these rules:
14188 * Try to fetch any prerequisite types we may need to build this DIE type
14189 before building the type and calling set_die_type.
14190 * After building typer call set_die_type for current DIE as soon as
14191 possible before fetching more types to complete the current type.
14192 * Make the type as complete as possible before fetching more types. */
14194 static struct type *
14195 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14197 struct dwarf2_offset_and_type **slot, ofs;
14199 /* For Ada types, make sure that the gnat-specific data is always
14200 initialized (if not already set). There are a few types where
14201 we should not be doing so, because the type-specific area is
14202 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14203 where the type-specific area is used to store the floatformat).
14204 But this is not a problem, because the gnat-specific information
14205 is actually not needed for these types. */
14206 if (need_gnat_info (cu)
14207 && TYPE_CODE (type) != TYPE_CODE_FUNC
14208 && TYPE_CODE (type) != TYPE_CODE_FLT
14209 && !HAVE_GNAT_AUX_INFO (type))
14210 INIT_GNAT_SPECIFIC (type);
14212 if (cu->type_hash == NULL)
14214 gdb_assert (cu->per_cu != NULL);
14215 cu->per_cu->type_hash
14216 = htab_create_alloc_ex (cu->header.length / 24,
14217 offset_and_type_hash,
14218 offset_and_type_eq,
14220 &cu->objfile->objfile_obstack,
14221 hashtab_obstack_allocate,
14222 dummy_obstack_deallocate);
14223 cu->type_hash = cu->per_cu->type_hash;
14226 ofs.offset = die->offset;
14228 slot = (struct dwarf2_offset_and_type **)
14229 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
14231 complaint (&symfile_complaints,
14232 _("A problem internal to GDB: DIE 0x%x has type already set"),
14234 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
14239 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
14240 not have a saved type. */
14242 static struct type *
14243 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14245 struct dwarf2_offset_and_type *slot, ofs;
14246 htab_t type_hash = cu->type_hash;
14248 if (type_hash == NULL)
14251 ofs.offset = die->offset;
14252 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14259 /* Add a dependence relationship from CU to REF_PER_CU. */
14262 dwarf2_add_dependence (struct dwarf2_cu *cu,
14263 struct dwarf2_per_cu_data *ref_per_cu)
14267 if (cu->dependencies == NULL)
14269 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14270 NULL, &cu->comp_unit_obstack,
14271 hashtab_obstack_allocate,
14272 dummy_obstack_deallocate);
14274 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14276 *slot = ref_per_cu;
14279 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14280 Set the mark field in every compilation unit in the
14281 cache that we must keep because we are keeping CU. */
14284 dwarf2_mark_helper (void **slot, void *data)
14286 struct dwarf2_per_cu_data *per_cu;
14288 per_cu = (struct dwarf2_per_cu_data *) *slot;
14289 if (per_cu->cu->mark)
14291 per_cu->cu->mark = 1;
14293 if (per_cu->cu->dependencies != NULL)
14294 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14299 /* Set the mark field in CU and in every other compilation unit in the
14300 cache that we must keep because we are keeping CU. */
14303 dwarf2_mark (struct dwarf2_cu *cu)
14308 if (cu->dependencies != NULL)
14309 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14313 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14317 per_cu->cu->mark = 0;
14318 per_cu = per_cu->cu->read_in_chain;
14322 /* Trivial hash function for partial_die_info: the hash value of a DIE
14323 is its offset in .debug_info for this objfile. */
14326 partial_die_hash (const void *item)
14328 const struct partial_die_info *part_die = item;
14330 return part_die->offset;
14333 /* Trivial comparison function for partial_die_info structures: two DIEs
14334 are equal if they have the same offset. */
14337 partial_die_eq (const void *item_lhs, const void *item_rhs)
14339 const struct partial_die_info *part_die_lhs = item_lhs;
14340 const struct partial_die_info *part_die_rhs = item_rhs;
14342 return part_die_lhs->offset == part_die_rhs->offset;
14345 static struct cmd_list_element *set_dwarf2_cmdlist;
14346 static struct cmd_list_element *show_dwarf2_cmdlist;
14349 set_dwarf2_cmd (char *args, int from_tty)
14351 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14355 show_dwarf2_cmd (char *args, int from_tty)
14357 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14360 /* If section described by INFO was mmapped, munmap it now. */
14363 munmap_section_buffer (struct dwarf2_section_info *info)
14365 if (info->was_mmapped)
14368 intptr_t begin = (intptr_t) info->buffer;
14369 intptr_t map_begin = begin & ~(pagesize - 1);
14370 size_t map_length = info->size + begin - map_begin;
14372 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14374 /* Without HAVE_MMAP, we should never be here to begin with. */
14375 gdb_assert_not_reached ("no mmap support");
14380 /* munmap debug sections for OBJFILE, if necessary. */
14383 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14385 struct dwarf2_per_objfile *data = d;
14387 munmap_section_buffer (&data->info);
14388 munmap_section_buffer (&data->abbrev);
14389 munmap_section_buffer (&data->line);
14390 munmap_section_buffer (&data->str);
14391 munmap_section_buffer (&data->macinfo);
14392 munmap_section_buffer (&data->ranges);
14393 munmap_section_buffer (&data->loc);
14394 munmap_section_buffer (&data->frame);
14395 munmap_section_buffer (&data->eh_frame);
14396 munmap_section_buffer (&data->gdb_index);
14401 /* The contents of the hash table we create when building the string
14403 struct strtab_entry
14405 offset_type offset;
14409 /* Hash function for a strtab_entry. */
14411 hash_strtab_entry (const void *e)
14413 const struct strtab_entry *entry = e;
14414 return mapped_index_string_hash (entry->str);
14417 /* Equality function for a strtab_entry. */
14419 eq_strtab_entry (const void *a, const void *b)
14421 const struct strtab_entry *ea = a;
14422 const struct strtab_entry *eb = b;
14423 return !strcmp (ea->str, eb->str);
14426 /* Create a strtab_entry hash table. */
14428 create_strtab (void)
14430 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14431 xfree, xcalloc, xfree);
14434 /* Add a string to the constant pool. Return the string's offset in
14437 add_string (htab_t table, struct obstack *cpool, const char *str)
14440 struct strtab_entry entry;
14441 struct strtab_entry *result;
14444 slot = htab_find_slot (table, &entry, INSERT);
14449 result = XNEW (struct strtab_entry);
14450 result->offset = obstack_object_size (cpool);
14452 obstack_grow_str0 (cpool, str);
14455 return result->offset;
14458 /* An entry in the symbol table. */
14459 struct symtab_index_entry
14461 /* The name of the symbol. */
14463 /* The offset of the name in the constant pool. */
14464 offset_type index_offset;
14465 /* A sorted vector of the indices of all the CUs that hold an object
14467 VEC (offset_type) *cu_indices;
14470 /* The symbol table. This is a power-of-2-sized hash table. */
14471 struct mapped_symtab
14473 offset_type n_elements;
14475 struct symtab_index_entry **data;
14478 /* Hash function for a symtab_index_entry. */
14480 hash_symtab_entry (const void *e)
14482 const struct symtab_index_entry *entry = e;
14483 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14484 sizeof (offset_type) * VEC_length (offset_type,
14485 entry->cu_indices),
14489 /* Equality function for a symtab_index_entry. */
14491 eq_symtab_entry (const void *a, const void *b)
14493 const struct symtab_index_entry *ea = a;
14494 const struct symtab_index_entry *eb = b;
14495 int len = VEC_length (offset_type, ea->cu_indices);
14496 if (len != VEC_length (offset_type, eb->cu_indices))
14498 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14499 VEC_address (offset_type, eb->cu_indices),
14500 sizeof (offset_type) * len);
14503 /* Destroy a symtab_index_entry. */
14505 delete_symtab_entry (void *p)
14507 struct symtab_index_entry *entry = p;
14508 VEC_free (offset_type, entry->cu_indices);
14512 /* Create a hash table holding symtab_index_entry objects. */
14514 create_index_table (void)
14516 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14517 delete_symtab_entry, xcalloc, xfree);
14520 /* Create a new mapped symtab object. */
14521 static struct mapped_symtab *
14522 create_mapped_symtab (void)
14524 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14525 symtab->n_elements = 0;
14526 symtab->size = 1024;
14527 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14531 /* Destroy a mapped_symtab. */
14533 cleanup_mapped_symtab (void *p)
14535 struct mapped_symtab *symtab = p;
14536 /* The contents of the array are freed when the other hash table is
14538 xfree (symtab->data);
14542 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14544 static struct symtab_index_entry **
14545 find_slot (struct mapped_symtab *symtab, const char *name)
14547 offset_type index, step, hash = mapped_index_string_hash (name);
14549 index = hash & (symtab->size - 1);
14550 step = ((hash * 17) & (symtab->size - 1)) | 1;
14554 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14555 return &symtab->data[index];
14556 index = (index + step) & (symtab->size - 1);
14560 /* Expand SYMTAB's hash table. */
14562 hash_expand (struct mapped_symtab *symtab)
14564 offset_type old_size = symtab->size;
14566 struct symtab_index_entry **old_entries = symtab->data;
14569 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14571 for (i = 0; i < old_size; ++i)
14573 if (old_entries[i])
14575 struct symtab_index_entry **slot = find_slot (symtab,
14576 old_entries[i]->name);
14577 *slot = old_entries[i];
14581 xfree (old_entries);
14584 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14585 is the index of the CU in which the symbol appears. */
14587 add_index_entry (struct mapped_symtab *symtab, const char *name,
14588 offset_type cu_index)
14590 struct symtab_index_entry **slot;
14592 ++symtab->n_elements;
14593 if (4 * symtab->n_elements / 3 >= symtab->size)
14594 hash_expand (symtab);
14596 slot = find_slot (symtab, name);
14599 *slot = XNEW (struct symtab_index_entry);
14600 (*slot)->name = name;
14601 (*slot)->cu_indices = NULL;
14603 /* Don't push an index twice. Due to how we add entries we only
14604 have to check the last one. */
14605 if (VEC_empty (offset_type, (*slot)->cu_indices)
14606 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14607 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14610 /* Add a vector of indices to the constant pool. */
14612 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14613 struct symtab_index_entry *entry)
14617 slot = htab_find_slot (index_table, entry, INSERT);
14620 offset_type len = VEC_length (offset_type, entry->cu_indices);
14621 offset_type val = MAYBE_SWAP (len);
14626 entry->index_offset = obstack_object_size (cpool);
14628 obstack_grow (cpool, &val, sizeof (val));
14630 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14633 val = MAYBE_SWAP (iter);
14634 obstack_grow (cpool, &val, sizeof (val));
14639 struct symtab_index_entry *old_entry = *slot;
14640 entry->index_offset = old_entry->index_offset;
14643 return entry->index_offset;
14646 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14647 constant pool entries going into the obstack CPOOL. */
14649 write_hash_table (struct mapped_symtab *symtab,
14650 struct obstack *output, struct obstack *cpool)
14653 htab_t index_table;
14656 index_table = create_index_table ();
14657 str_table = create_strtab ();
14658 /* We add all the index vectors to the constant pool first, to
14659 ensure alignment is ok. */
14660 for (i = 0; i < symtab->size; ++i)
14662 if (symtab->data[i])
14663 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14666 /* Now write out the hash table. */
14667 for (i = 0; i < symtab->size; ++i)
14669 offset_type str_off, vec_off;
14671 if (symtab->data[i])
14673 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14674 vec_off = symtab->data[i]->index_offset;
14678 /* While 0 is a valid constant pool index, it is not valid
14679 to have 0 for both offsets. */
14684 str_off = MAYBE_SWAP (str_off);
14685 vec_off = MAYBE_SWAP (vec_off);
14687 obstack_grow (output, &str_off, sizeof (str_off));
14688 obstack_grow (output, &vec_off, sizeof (vec_off));
14691 htab_delete (str_table);
14692 htab_delete (index_table);
14695 /* Write an address entry to ADDR_OBSTACK. The addresses are taken
14696 from PST; CU_INDEX is the index of the CU in the vector of all
14699 add_address_entry (struct objfile *objfile,
14700 struct obstack *addr_obstack, struct partial_symtab *pst,
14701 unsigned int cu_index)
14703 offset_type offset;
14705 CORE_ADDR baseaddr;
14707 /* Don't bother recording empty ranges. */
14708 if (pst->textlow == pst->texthigh)
14711 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14713 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14714 obstack_grow (addr_obstack, addr, 8);
14715 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14716 obstack_grow (addr_obstack, addr, 8);
14717 offset = MAYBE_SWAP (cu_index);
14718 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14721 /* Add a list of partial symbols to SYMTAB. */
14723 write_psymbols (struct mapped_symtab *symtab,
14724 struct partial_symbol **psymp,
14726 offset_type cu_index)
14728 for (; count-- > 0; ++psymp)
14730 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14731 error (_("Ada is not currently supported by the index"));
14732 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14736 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
14737 exception if there is an error. */
14739 write_obstack (FILE *file, struct obstack *obstack)
14741 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14743 != obstack_object_size (obstack))
14744 error (_("couldn't data write to file"));
14747 /* Unlink a file if the argument is not NULL. */
14749 unlink_if_set (void *p)
14751 char **filename = p;
14753 unlink (*filename);
14756 /* A helper struct used when iterating over debug_types. */
14757 struct signatured_type_index_data
14759 struct objfile *objfile;
14760 struct mapped_symtab *symtab;
14761 struct obstack *types_list;
14765 /* A helper function that writes a single signatured_type to an
14768 write_one_signatured_type (void **slot, void *d)
14770 struct signatured_type_index_data *info = d;
14771 struct signatured_type *entry = (struct signatured_type *) *slot;
14772 struct dwarf2_per_cu_data *cu = &entry->per_cu;
14773 struct partial_symtab *psymtab = cu->v.psymtab;
14776 write_psymbols (info->symtab,
14777 info->objfile->global_psymbols.list + psymtab->globals_offset,
14778 psymtab->n_global_syms, info->cu_index);
14779 write_psymbols (info->symtab,
14780 info->objfile->static_psymbols.list + psymtab->statics_offset,
14781 psymtab->n_static_syms, info->cu_index);
14783 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14784 obstack_grow (info->types_list, val, 8);
14785 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14786 obstack_grow (info->types_list, val, 8);
14787 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14788 obstack_grow (info->types_list, val, 8);
14795 /* Create an index file for OBJFILE in the directory DIR. */
14797 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14799 struct cleanup *cleanup;
14800 char *filename, *cleanup_filename;
14801 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14802 struct obstack cu_list, types_cu_list;
14805 struct mapped_symtab *symtab;
14806 offset_type val, size_of_contents, total_len;
14810 if (!objfile->psymtabs)
14812 if (dwarf2_per_objfile->using_index)
14813 error (_("Cannot use an index to create the index"));
14815 if (stat (objfile->name, &st) < 0)
14816 perror_with_name (_("Could not stat"));
14818 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14819 INDEX_SUFFIX, (char *) NULL);
14820 cleanup = make_cleanup (xfree, filename);
14822 out_file = fopen (filename, "wb");
14824 error (_("Can't open `%s' for writing"), filename);
14826 cleanup_filename = filename;
14827 make_cleanup (unlink_if_set, &cleanup_filename);
14829 symtab = create_mapped_symtab ();
14830 make_cleanup (cleanup_mapped_symtab, symtab);
14832 obstack_init (&addr_obstack);
14833 make_cleanup_obstack_free (&addr_obstack);
14835 obstack_init (&cu_list);
14836 make_cleanup_obstack_free (&cu_list);
14838 obstack_init (&types_cu_list);
14839 make_cleanup_obstack_free (&types_cu_list);
14841 /* The list is already sorted, so we don't need to do additional
14842 work here. Also, the debug_types entries do not appear in
14843 all_comp_units, but only in their own hash table. */
14844 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14846 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14847 struct partial_symtab *psymtab = cu->v.psymtab;
14850 write_psymbols (symtab,
14851 objfile->global_psymbols.list + psymtab->globals_offset,
14852 psymtab->n_global_syms, i);
14853 write_psymbols (symtab,
14854 objfile->static_psymbols.list + psymtab->statics_offset,
14855 psymtab->n_static_syms, i);
14857 add_address_entry (objfile, &addr_obstack, psymtab, i);
14859 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14860 obstack_grow (&cu_list, val, 8);
14861 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14862 obstack_grow (&cu_list, val, 8);
14865 /* Write out the .debug_type entries, if any. */
14866 if (dwarf2_per_objfile->signatured_types)
14868 struct signatured_type_index_data sig_data;
14870 sig_data.objfile = objfile;
14871 sig_data.symtab = symtab;
14872 sig_data.types_list = &types_cu_list;
14873 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14874 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14875 write_one_signatured_type, &sig_data);
14878 obstack_init (&constant_pool);
14879 make_cleanup_obstack_free (&constant_pool);
14880 obstack_init (&symtab_obstack);
14881 make_cleanup_obstack_free (&symtab_obstack);
14882 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14884 obstack_init (&contents);
14885 make_cleanup_obstack_free (&contents);
14886 size_of_contents = 6 * sizeof (offset_type);
14887 total_len = size_of_contents;
14889 /* The version number. */
14890 val = MAYBE_SWAP (2);
14891 obstack_grow (&contents, &val, sizeof (val));
14893 /* The offset of the CU list from the start of the file. */
14894 val = MAYBE_SWAP (total_len);
14895 obstack_grow (&contents, &val, sizeof (val));
14896 total_len += obstack_object_size (&cu_list);
14898 /* The offset of the types CU list from the start of the file. */
14899 val = MAYBE_SWAP (total_len);
14900 obstack_grow (&contents, &val, sizeof (val));
14901 total_len += obstack_object_size (&types_cu_list);
14903 /* The offset of the address table from the start of the file. */
14904 val = MAYBE_SWAP (total_len);
14905 obstack_grow (&contents, &val, sizeof (val));
14906 total_len += obstack_object_size (&addr_obstack);
14908 /* The offset of the symbol table from the start of the file. */
14909 val = MAYBE_SWAP (total_len);
14910 obstack_grow (&contents, &val, sizeof (val));
14911 total_len += obstack_object_size (&symtab_obstack);
14913 /* The offset of the constant pool from the start of the file. */
14914 val = MAYBE_SWAP (total_len);
14915 obstack_grow (&contents, &val, sizeof (val));
14916 total_len += obstack_object_size (&constant_pool);
14918 gdb_assert (obstack_object_size (&contents) == size_of_contents);
14920 write_obstack (out_file, &contents);
14921 write_obstack (out_file, &cu_list);
14922 write_obstack (out_file, &types_cu_list);
14923 write_obstack (out_file, &addr_obstack);
14924 write_obstack (out_file, &symtab_obstack);
14925 write_obstack (out_file, &constant_pool);
14929 /* We want to keep the file, so we set cleanup_filename to NULL
14930 here. See unlink_if_set. */
14931 cleanup_filename = NULL;
14933 do_cleanups (cleanup);
14936 /* The mapped index file format is designed to be directly mmap()able
14937 on any architecture. In most cases, a datum is represented using a
14938 little-endian 32-bit integer value, called an offset_type. Big
14939 endian machines must byte-swap the values before using them.
14940 Exceptions to this rule are noted. The data is laid out such that
14941 alignment is always respected.
14943 A mapped index consists of several sections.
14945 1. The file header. This is a sequence of values, of offset_type
14946 unless otherwise noted:
14947 [0] The version number. Currently 1 or 2. The differences are
14948 noted below. Version 1 did not account for .debug_types sections;
14949 the presence of a .debug_types section invalidates any version 1
14950 index that may exist.
14951 [1] The offset, from the start of the file, of the CU list.
14952 [1.5] In version 2, the offset, from the start of the file, of the
14953 types CU list. This offset does not appear in version 1. Note
14954 that this can be empty, in which case this offset will be equal to
14956 [2] The offset, from the start of the file, of the address section.
14957 [3] The offset, from the start of the file, of the symbol table.
14958 [4] The offset, from the start of the file, of the constant pool.
14960 2. The CU list. This is a sequence of pairs of 64-bit
14961 little-endian values, sorted by the CU offset. The first element
14962 in each pair is the offset of a CU in the .debug_info section. The
14963 second element in each pair is the length of that CU. References
14964 to a CU elsewhere in the map are done using a CU index, which is
14965 just the 0-based index into this table. Note that if there are
14966 type CUs, then conceptually CUs and type CUs form a single list for
14967 the purposes of CU indices.
14969 2.5 The types CU list. This does not appear in a version 1 index.
14970 This is a sequence of triplets of 64-bit little-endian values. In
14971 a triplet, the first value is the CU offset, the second value is
14972 the type offset in the CU, and the third value is the type
14973 signature. The types CU list is not sorted.
14975 3. The address section. The address section consists of a sequence
14976 of address entries. Each address entry has three elements.
14977 [0] The low address. This is a 64-bit little-endian value.
14978 [1] The high address. This is a 64-bit little-endian value.
14979 [2] The CU index. This is an offset_type value.
14981 4. The symbol table. This is a hash table. The size of the hash
14982 table is always a power of 2. The initial hash and the step are
14983 currently defined by the `find_slot' function.
14985 Each slot in the hash table consists of a pair of offset_type
14986 values. The first value is the offset of the symbol's name in the
14987 constant pool. The second value is the offset of the CU vector in
14990 If both values are 0, then this slot in the hash table is empty.
14991 This is ok because while 0 is a valid constant pool index, it
14992 cannot be a valid index for both a string and a CU vector.
14994 A string in the constant pool is stored as a \0-terminated string,
14997 A CU vector in the constant pool is a sequence of offset_type
14998 values. The first value is the number of CU indices in the vector.
14999 Each subsequent value is the index of a CU in the CU list. This
15000 element in the hash table is used to indicate which CUs define the
15003 5. The constant pool. This is simply a bunch of bytes. It is
15004 organized so that alignment is correct: CU vectors are stored
15005 first, followed by strings. */
15007 save_gdb_index_command (char *arg, int from_tty)
15009 struct objfile *objfile;
15012 error (_("usage: save gdb-index DIRECTORY"));
15014 ALL_OBJFILES (objfile)
15018 /* If the objfile does not correspond to an actual file, skip it. */
15019 if (stat (objfile->name, &st) < 0)
15022 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15023 if (dwarf2_per_objfile)
15025 volatile struct gdb_exception except;
15027 TRY_CATCH (except, RETURN_MASK_ERROR)
15029 write_psymtabs_to_index (objfile, arg);
15031 if (except.reason < 0)
15032 exception_fprintf (gdb_stderr, except,
15033 _("Error while writing index for `%s': "),
15041 int dwarf2_always_disassemble;
15044 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15045 struct cmd_list_element *c, const char *value)
15047 fprintf_filtered (file, _("\
15048 Whether to always disassemble DWARF expressions is %s.\n"),
15052 void _initialize_dwarf2_read (void);
15055 _initialize_dwarf2_read (void)
15057 struct cmd_list_element *c;
15059 dwarf2_objfile_data_key
15060 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15062 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15063 Set DWARF 2 specific variables.\n\
15064 Configure DWARF 2 variables such as the cache size"),
15065 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15066 0/*allow-unknown*/, &maintenance_set_cmdlist);
15068 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15069 Show DWARF 2 specific variables\n\
15070 Show DWARF 2 variables such as the cache size"),
15071 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15072 0/*allow-unknown*/, &maintenance_show_cmdlist);
15074 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15075 &dwarf2_max_cache_age, _("\
15076 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15077 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15078 A higher limit means that cached compilation units will be stored\n\
15079 in memory longer, and more total memory will be used. Zero disables\n\
15080 caching, which can slow down startup."),
15082 show_dwarf2_max_cache_age,
15083 &set_dwarf2_cmdlist,
15084 &show_dwarf2_cmdlist);
15086 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15087 &dwarf2_always_disassemble, _("\
15088 Set whether `info address' always disassembles DWARF expressions."), _("\
15089 Show whether `info address' always disassembles DWARF expressions."), _("\
15090 When enabled, DWARF expressions are always printed in an assembly-like\n\
15091 syntax. When disabled, expressions will be printed in a more\n\
15092 conversational style, when possible."),
15094 show_dwarf2_always_disassemble,
15095 &set_dwarf2_cmdlist,
15096 &show_dwarf2_cmdlist);
15098 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15099 Set debugging of the dwarf2 DIE reader."), _("\
15100 Show debugging of the dwarf2 DIE reader."), _("\
15101 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15102 The value is the maximum depth to print."),
15105 &setdebuglist, &showdebuglist);
15107 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15108 _("Save a .gdb-index file"),
15110 set_cmd_completer (c, filename_completer);