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