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