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