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