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