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