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