2011-07-22 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83 typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99 typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in. */
120 static int dwarf2_die_debug = 0;
121
122 /* When non-zero, cross-check physname against demangler. */
123 static int check_physname = 0;
124
125 static int pagesize;
126
127 /* When set, the file that we're processing is known to have debugging
128 info for C++ namespaces. GCC 3.3.x did not produce this information,
129 but later versions do. */
130
131 static int processing_has_namespace_info;
132
133 static const struct objfile_data *dwarf2_objfile_data_key;
134
135 struct dwarf2_section_info
136 {
137 asection *asection;
138 gdb_byte *buffer;
139 bfd_size_type size;
140 /* Not NULL if the section was actually mmapped. */
141 void *map_addr;
142 /* Page aligned size of mmapped area. */
143 bfd_size_type map_len;
144 /* True if we have tried to read this section. */
145 int readin;
146 };
147
148 typedef struct dwarf2_section_info dwarf2_section_info_def;
149 DEF_VEC_O (dwarf2_section_info_def);
150
151 /* All offsets in the index are of this type. It must be
152 architecture-independent. */
153 typedef uint32_t offset_type;
154
155 DEF_VEC_I (offset_type);
156
157 /* A description of the mapped index. The file format is described in
158 a comment by the code that writes the index. */
159 struct mapped_index
160 {
161 /* Index data format version. */
162 int version;
163
164 /* The total length of the buffer. */
165 off_t total_size;
166
167 /* A pointer to the address table data. */
168 const gdb_byte *address_table;
169
170 /* Size of the address table data in bytes. */
171 offset_type address_table_size;
172
173 /* The symbol table, implemented as a hash table. */
174 const offset_type *symbol_table;
175
176 /* Size in slots, each slot is 2 offset_types. */
177 offset_type symbol_table_slots;
178
179 /* A pointer to the constant pool. */
180 const char *constant_pool;
181 };
182
183 struct dwarf2_per_objfile
184 {
185 struct dwarf2_section_info info;
186 struct dwarf2_section_info abbrev;
187 struct dwarf2_section_info line;
188 struct dwarf2_section_info loc;
189 struct dwarf2_section_info macinfo;
190 struct dwarf2_section_info str;
191 struct dwarf2_section_info ranges;
192 struct dwarf2_section_info frame;
193 struct dwarf2_section_info eh_frame;
194 struct dwarf2_section_info gdb_index;
195
196 VEC (dwarf2_section_info_def) *types;
197
198 /* Back link. */
199 struct objfile *objfile;
200
201 /* A list of all the compilation units. This is used to locate
202 the target compilation unit of a particular reference. */
203 struct dwarf2_per_cu_data **all_comp_units;
204
205 /* The number of compilation units in ALL_COMP_UNITS. */
206 int n_comp_units;
207
208 /* The number of .debug_types-related CUs. */
209 int n_type_comp_units;
210
211 /* The .debug_types-related CUs. */
212 struct dwarf2_per_cu_data **type_comp_units;
213
214 /* A chain of compilation units that are currently read in, so that
215 they can be freed later. */
216 struct dwarf2_per_cu_data *read_in_chain;
217
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
222 /* A flag indicating wether this objfile has a section loaded at a
223 VMA of 0. */
224 int has_section_at_zero;
225
226 /* True if we are using the mapped index,
227 or we are faking it for OBJF_READNOW's sake. */
228 unsigned char using_index;
229
230 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
231 struct mapped_index *index_table;
232
233 /* When using index_table, this keeps track of all quick_file_names entries.
234 TUs can share line table entries with CUs or other TUs, and there can be
235 a lot more TUs than unique line tables, so we maintain a separate table
236 of all line table entries to support the sharing. */
237 htab_t quick_file_names_table;
238
239 /* Set during partial symbol reading, to prevent queueing of full
240 symbols. */
241 int reading_partial_symbols;
242
243 /* Table mapping type .debug_info DIE offsets to types.
244 This is NULL if not allocated yet.
245 It (currently) makes sense to allocate debug_types_type_hash lazily.
246 To keep things simple we allocate both lazily. */
247 htab_t debug_info_type_hash;
248
249 /* Table mapping type .debug_types DIE offsets to types.
250 This is NULL if not allocated yet. */
251 htab_t debug_types_type_hash;
252 };
253
254 static struct dwarf2_per_objfile *dwarf2_per_objfile;
255
256 /* Default names of the debugging sections. */
257
258 /* Note that if the debugging section has been compressed, it might
259 have a name like .zdebug_info. */
260
261 static const struct dwarf2_debug_sections dwarf2_elf_names = {
262 { ".debug_info", ".zdebug_info" },
263 { ".debug_abbrev", ".zdebug_abbrev" },
264 { ".debug_line", ".zdebug_line" },
265 { ".debug_loc", ".zdebug_loc" },
266 { ".debug_macinfo", ".zdebug_macinfo" },
267 { ".debug_str", ".zdebug_str" },
268 { ".debug_ranges", ".zdebug_ranges" },
269 { ".debug_types", ".zdebug_types" },
270 { ".debug_frame", ".zdebug_frame" },
271 { ".eh_frame", NULL },
272 { ".gdb_index", ".zgdb_index" }
273 };
274
275 /* local data types */
276
277 /* We hold several abbreviation tables in memory at the same time. */
278 #ifndef ABBREV_HASH_SIZE
279 #define ABBREV_HASH_SIZE 121
280 #endif
281
282 /* The data in a compilation unit header, after target2host
283 translation, looks like this. */
284 struct comp_unit_head
285 {
286 unsigned int length;
287 short version;
288 unsigned char addr_size;
289 unsigned char signed_addr_p;
290 unsigned int abbrev_offset;
291
292 /* Size of file offsets; either 4 or 8. */
293 unsigned int offset_size;
294
295 /* Size of the length field; either 4 or 12. */
296 unsigned int initial_length_size;
297
298 /* Offset to the first byte of this compilation unit header in the
299 .debug_info section, for resolving relative reference dies. */
300 unsigned int offset;
301
302 /* Offset to first die in this cu from the start of the cu.
303 This will be the first byte following the compilation unit header. */
304 unsigned int first_die_offset;
305 };
306
307 /* Type used for delaying computation of method physnames.
308 See comments for compute_delayed_physnames. */
309 struct delayed_method_info
310 {
311 /* The type to which the method is attached, i.e., its parent class. */
312 struct type *type;
313
314 /* The index of the method in the type's function fieldlists. */
315 int fnfield_index;
316
317 /* The index of the method in the fieldlist. */
318 int index;
319
320 /* The name of the DIE. */
321 const char *name;
322
323 /* The DIE associated with this method. */
324 struct die_info *die;
325 };
326
327 typedef struct delayed_method_info delayed_method_info;
328 DEF_VEC_O (delayed_method_info);
329
330 /* Internal state when decoding a particular compilation unit. */
331 struct dwarf2_cu
332 {
333 /* The objfile containing this compilation unit. */
334 struct objfile *objfile;
335
336 /* The header of the compilation unit. */
337 struct comp_unit_head header;
338
339 /* Base address of this compilation unit. */
340 CORE_ADDR base_address;
341
342 /* Non-zero if base_address has been set. */
343 int base_known;
344
345 struct function_range *first_fn, *last_fn, *cached_fn;
346
347 /* The language we are debugging. */
348 enum language language;
349 const struct language_defn *language_defn;
350
351 const char *producer;
352
353 /* The generic symbol table building routines have separate lists for
354 file scope symbols and all all other scopes (local scopes). So
355 we need to select the right one to pass to add_symbol_to_list().
356 We do it by keeping a pointer to the correct list in list_in_scope.
357
358 FIXME: The original dwarf code just treated the file scope as the
359 first local scope, and all other local scopes as nested local
360 scopes, and worked fine. Check to see if we really need to
361 distinguish these in buildsym.c. */
362 struct pending **list_in_scope;
363
364 /* DWARF abbreviation table associated with this compilation unit. */
365 struct abbrev_info **dwarf2_abbrevs;
366
367 /* Storage for the abbrev table. */
368 struct obstack abbrev_obstack;
369
370 /* Hash table holding all the loaded partial DIEs. */
371 htab_t partial_dies;
372
373 /* Storage for things with the same lifetime as this read-in compilation
374 unit, including partial DIEs. */
375 struct obstack comp_unit_obstack;
376
377 /* When multiple dwarf2_cu structures are living in memory, this field
378 chains them all together, so that they can be released efficiently.
379 We will probably also want a generation counter so that most-recently-used
380 compilation units are cached... */
381 struct dwarf2_per_cu_data *read_in_chain;
382
383 /* Backchain to our per_cu entry if the tree has been built. */
384 struct dwarf2_per_cu_data *per_cu;
385
386 /* How many compilation units ago was this CU last referenced? */
387 int last_used;
388
389 /* A hash table of die offsets for following references. */
390 htab_t die_hash;
391
392 /* Full DIEs if read in. */
393 struct die_info *dies;
394
395 /* A set of pointers to dwarf2_per_cu_data objects for compilation
396 units referenced by this one. Only set during full symbol processing;
397 partial symbol tables do not have dependencies. */
398 htab_t dependencies;
399
400 /* Header data from the line table, during full symbol processing. */
401 struct line_header *line_header;
402
403 /* A list of methods which need to have physnames computed
404 after all type information has been read. */
405 VEC (delayed_method_info) *method_list;
406
407 /* Mark used when releasing cached dies. */
408 unsigned int mark : 1;
409
410 /* This flag will be set if this compilation unit might include
411 inter-compilation-unit references. */
412 unsigned int has_form_ref_addr : 1;
413
414 /* This flag will be set if this compilation unit includes any
415 DW_TAG_namespace DIEs. If we know that there are explicit
416 DIEs for namespaces, we don't need to try to infer them
417 from mangled names. */
418 unsigned int has_namespace_info : 1;
419
420 /* This CU references .debug_loc. See the symtab->locations_valid field.
421 This test is imperfect as there may exist optimized debug code not using
422 any location list and still facing inlining issues if handled as
423 unoptimized code. For a future better test see GCC PR other/32998. */
424
425 unsigned int has_loclist : 1;
426 };
427
428 /* Persistent data held for a compilation unit, even when not
429 processing it. We put a pointer to this structure in the
430 read_symtab_private field of the psymtab. If we encounter
431 inter-compilation-unit references, we also maintain a sorted
432 list of all compilation units. */
433
434 struct dwarf2_per_cu_data
435 {
436 /* The start offset and length of this compilation unit. 2**29-1
437 bytes should suffice to store the length of any compilation unit
438 - if it doesn't, GDB will fall over anyway.
439 NOTE: Unlike comp_unit_head.length, this length includes
440 initial_length_size. */
441 unsigned int offset;
442 unsigned int length : 29;
443
444 /* Flag indicating this compilation unit will be read in before
445 any of the current compilation units are processed. */
446 unsigned int queued : 1;
447
448 /* This flag will be set if we need to load absolutely all DIEs
449 for this compilation unit, instead of just the ones we think
450 are interesting. It gets set if we look for a DIE in the
451 hash table and don't find it. */
452 unsigned int load_all_dies : 1;
453
454 /* Non-null if this CU is from .debug_types; in which case it points
455 to the section. Otherwise it's from .debug_info. */
456 struct dwarf2_section_info *debug_type_section;
457
458 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
459 of the CU cache it gets reset to NULL again. */
460 struct dwarf2_cu *cu;
461
462 /* The corresponding objfile. */
463 struct objfile *objfile;
464
465 /* When using partial symbol tables, the 'psymtab' field is active.
466 Otherwise the 'quick' field is active. */
467 union
468 {
469 /* The partial symbol table associated with this compilation unit,
470 or NULL for partial units (which do not have an associated
471 symtab). */
472 struct partial_symtab *psymtab;
473
474 /* Data needed by the "quick" functions. */
475 struct dwarf2_per_cu_quick_data *quick;
476 } v;
477 };
478
479 /* Entry in the signatured_types hash table. */
480
481 struct signatured_type
482 {
483 ULONGEST signature;
484
485 /* Offset in .debug_types of the type defined by this TU. */
486 unsigned int type_offset;
487
488 /* The CU(/TU) of this type. */
489 struct dwarf2_per_cu_data per_cu;
490 };
491
492 /* Struct used to pass misc. parameters to read_die_and_children, et
493 al. which are used for both .debug_info and .debug_types dies.
494 All parameters here are unchanging for the life of the call. This
495 struct exists to abstract away the constant parameters of die
496 reading. */
497
498 struct die_reader_specs
499 {
500 /* The bfd of this objfile. */
501 bfd* abfd;
502
503 /* The CU of the DIE we are parsing. */
504 struct dwarf2_cu *cu;
505
506 /* Pointer to start of section buffer.
507 This is either the start of .debug_info or .debug_types. */
508 const gdb_byte *buffer;
509 };
510
511 /* The line number information for a compilation unit (found in the
512 .debug_line section) begins with a "statement program header",
513 which contains the following information. */
514 struct line_header
515 {
516 unsigned int total_length;
517 unsigned short version;
518 unsigned int header_length;
519 unsigned char minimum_instruction_length;
520 unsigned char maximum_ops_per_instruction;
521 unsigned char default_is_stmt;
522 int line_base;
523 unsigned char line_range;
524 unsigned char opcode_base;
525
526 /* standard_opcode_lengths[i] is the number of operands for the
527 standard opcode whose value is i. This means that
528 standard_opcode_lengths[0] is unused, and the last meaningful
529 element is standard_opcode_lengths[opcode_base - 1]. */
530 unsigned char *standard_opcode_lengths;
531
532 /* The include_directories table. NOTE! These strings are not
533 allocated with xmalloc; instead, they are pointers into
534 debug_line_buffer. If you try to free them, `free' will get
535 indigestion. */
536 unsigned int num_include_dirs, include_dirs_size;
537 char **include_dirs;
538
539 /* The file_names table. NOTE! These strings are not allocated
540 with xmalloc; instead, they are pointers into debug_line_buffer.
541 Don't try to free them directly. */
542 unsigned int num_file_names, file_names_size;
543 struct file_entry
544 {
545 char *name;
546 unsigned int dir_index;
547 unsigned int mod_time;
548 unsigned int length;
549 int included_p; /* Non-zero if referenced by the Line Number Program. */
550 struct symtab *symtab; /* The associated symbol table, if any. */
551 } *file_names;
552
553 /* The start and end of the statement program following this
554 header. These point into dwarf2_per_objfile->line_buffer. */
555 gdb_byte *statement_program_start, *statement_program_end;
556 };
557
558 /* When we construct a partial symbol table entry we only
559 need this much information. */
560 struct partial_die_info
561 {
562 /* Offset of this DIE. */
563 unsigned int offset;
564
565 /* DWARF-2 tag for this DIE. */
566 ENUM_BITFIELD(dwarf_tag) tag : 16;
567
568 /* Assorted flags describing the data found in this DIE. */
569 unsigned int has_children : 1;
570 unsigned int is_external : 1;
571 unsigned int is_declaration : 1;
572 unsigned int has_type : 1;
573 unsigned int has_specification : 1;
574 unsigned int has_pc_info : 1;
575
576 /* Flag set if the SCOPE field of this structure has been
577 computed. */
578 unsigned int scope_set : 1;
579
580 /* Flag set if the DIE has a byte_size attribute. */
581 unsigned int has_byte_size : 1;
582
583 /* Flag set if any of the DIE's children are template arguments. */
584 unsigned int has_template_arguments : 1;
585
586 /* Flag set if fixup_partial_die has been called on this die. */
587 unsigned int fixup_called : 1;
588
589 /* The name of this DIE. Normally the value of DW_AT_name, but
590 sometimes a default name for unnamed DIEs. */
591 char *name;
592
593 /* The linkage name, if present. */
594 const char *linkage_name;
595
596 /* The scope to prepend to our children. This is generally
597 allocated on the comp_unit_obstack, so will disappear
598 when this compilation unit leaves the cache. */
599 char *scope;
600
601 /* The location description associated with this DIE, if any. */
602 struct dwarf_block *locdesc;
603
604 /* If HAS_PC_INFO, the PC range associated with this DIE. */
605 CORE_ADDR lowpc;
606 CORE_ADDR highpc;
607
608 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
609 DW_AT_sibling, if any. */
610 /* NOTE: This member isn't strictly necessary, read_partial_die could
611 return DW_AT_sibling values to its caller load_partial_dies. */
612 gdb_byte *sibling;
613
614 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
615 DW_AT_specification (or DW_AT_abstract_origin or
616 DW_AT_extension). */
617 unsigned int spec_offset;
618
619 /* Pointers to this DIE's parent, first child, and next sibling,
620 if any. */
621 struct partial_die_info *die_parent, *die_child, *die_sibling;
622 };
623
624 /* This data structure holds the information of an abbrev. */
625 struct abbrev_info
626 {
627 unsigned int number; /* number identifying abbrev */
628 enum dwarf_tag tag; /* dwarf tag */
629 unsigned short has_children; /* boolean */
630 unsigned short num_attrs; /* number of attributes */
631 struct attr_abbrev *attrs; /* an array of attribute descriptions */
632 struct abbrev_info *next; /* next in chain */
633 };
634
635 struct attr_abbrev
636 {
637 ENUM_BITFIELD(dwarf_attribute) name : 16;
638 ENUM_BITFIELD(dwarf_form) form : 16;
639 };
640
641 /* Attributes have a name and a value. */
642 struct attribute
643 {
644 ENUM_BITFIELD(dwarf_attribute) name : 16;
645 ENUM_BITFIELD(dwarf_form) form : 15;
646
647 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
648 field should be in u.str (existing only for DW_STRING) but it is kept
649 here for better struct attribute alignment. */
650 unsigned int string_is_canonical : 1;
651
652 union
653 {
654 char *str;
655 struct dwarf_block *blk;
656 ULONGEST unsnd;
657 LONGEST snd;
658 CORE_ADDR addr;
659 struct signatured_type *signatured_type;
660 }
661 u;
662 };
663
664 /* This data structure holds a complete die structure. */
665 struct die_info
666 {
667 /* DWARF-2 tag for this DIE. */
668 ENUM_BITFIELD(dwarf_tag) tag : 16;
669
670 /* Number of attributes */
671 unsigned char num_attrs;
672
673 /* True if we're presently building the full type name for the
674 type derived from this DIE. */
675 unsigned char building_fullname : 1;
676
677 /* Abbrev number */
678 unsigned int abbrev;
679
680 /* Offset in .debug_info or .debug_types section. */
681 unsigned int offset;
682
683 /* The dies in a compilation unit form an n-ary tree. PARENT
684 points to this die's parent; CHILD points to the first child of
685 this node; and all the children of a given node are chained
686 together via their SIBLING fields. */
687 struct die_info *child; /* Its first child, if any. */
688 struct die_info *sibling; /* Its next sibling, if any. */
689 struct die_info *parent; /* Its parent, if any. */
690
691 /* An array of attributes, with NUM_ATTRS elements. There may be
692 zero, but it's not common and zero-sized arrays are not
693 sufficiently portable C. */
694 struct attribute attrs[1];
695 };
696
697 struct function_range
698 {
699 const char *name;
700 CORE_ADDR lowpc, highpc;
701 int seen_line;
702 struct function_range *next;
703 };
704
705 /* Get at parts of an attribute structure. */
706
707 #define DW_STRING(attr) ((attr)->u.str)
708 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
709 #define DW_UNSND(attr) ((attr)->u.unsnd)
710 #define DW_BLOCK(attr) ((attr)->u.blk)
711 #define DW_SND(attr) ((attr)->u.snd)
712 #define DW_ADDR(attr) ((attr)->u.addr)
713 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
714
715 /* Blocks are a bunch of untyped bytes. */
716 struct dwarf_block
717 {
718 unsigned int size;
719 gdb_byte *data;
720 };
721
722 #ifndef ATTR_ALLOC_CHUNK
723 #define ATTR_ALLOC_CHUNK 4
724 #endif
725
726 /* Allocate fields for structs, unions and enums in this size. */
727 #ifndef DW_FIELD_ALLOC_CHUNK
728 #define DW_FIELD_ALLOC_CHUNK 4
729 #endif
730
731 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
732 but this would require a corresponding change in unpack_field_as_long
733 and friends. */
734 static int bits_per_byte = 8;
735
736 /* The routines that read and process dies for a C struct or C++ class
737 pass lists of data member fields and lists of member function fields
738 in an instance of a field_info structure, as defined below. */
739 struct field_info
740 {
741 /* List of data member and baseclasses fields. */
742 struct nextfield
743 {
744 struct nextfield *next;
745 int accessibility;
746 int virtuality;
747 struct field field;
748 }
749 *fields, *baseclasses;
750
751 /* Number of fields (including baseclasses). */
752 int nfields;
753
754 /* Number of baseclasses. */
755 int nbaseclasses;
756
757 /* Set if the accesibility of one of the fields is not public. */
758 int non_public_fields;
759
760 /* Member function fields array, entries are allocated in the order they
761 are encountered in the object file. */
762 struct nextfnfield
763 {
764 struct nextfnfield *next;
765 struct fn_field fnfield;
766 }
767 *fnfields;
768
769 /* Member function fieldlist array, contains name of possibly overloaded
770 member function, number of overloaded member functions and a pointer
771 to the head of the member function field chain. */
772 struct fnfieldlist
773 {
774 char *name;
775 int length;
776 struct nextfnfield *head;
777 }
778 *fnfieldlists;
779
780 /* Number of entries in the fnfieldlists array. */
781 int nfnfields;
782
783 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
784 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
785 struct typedef_field_list
786 {
787 struct typedef_field field;
788 struct typedef_field_list *next;
789 }
790 *typedef_field_list;
791 unsigned typedef_field_list_count;
792 };
793
794 /* One item on the queue of compilation units to read in full symbols
795 for. */
796 struct dwarf2_queue_item
797 {
798 struct dwarf2_per_cu_data *per_cu;
799 struct dwarf2_queue_item *next;
800 };
801
802 /* The current queue. */
803 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
804
805 /* Loaded secondary compilation units are kept in memory until they
806 have not been referenced for the processing of this many
807 compilation units. Set this to zero to disable caching. Cache
808 sizes of up to at least twenty will improve startup time for
809 typical inter-CU-reference binaries, at an obvious memory cost. */
810 static int dwarf2_max_cache_age = 5;
811 static void
812 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
813 struct cmd_list_element *c, const char *value)
814 {
815 fprintf_filtered (file, _("The upper bound on the age of cached "
816 "dwarf2 compilation units is %s.\n"),
817 value);
818 }
819
820
821 /* Various complaints about symbol reading that don't abort the process. */
822
823 static void
824 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
825 {
826 complaint (&symfile_complaints,
827 _("statement list doesn't fit in .debug_line section"));
828 }
829
830 static void
831 dwarf2_debug_line_missing_file_complaint (void)
832 {
833 complaint (&symfile_complaints,
834 _(".debug_line section has line data without a file"));
835 }
836
837 static void
838 dwarf2_debug_line_missing_end_sequence_complaint (void)
839 {
840 complaint (&symfile_complaints,
841 _(".debug_line section has line "
842 "program sequence without an end"));
843 }
844
845 static void
846 dwarf2_complex_location_expr_complaint (void)
847 {
848 complaint (&symfile_complaints, _("location expression too complex"));
849 }
850
851 static void
852 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
853 int arg3)
854 {
855 complaint (&symfile_complaints,
856 _("const value length mismatch for '%s', got %d, expected %d"),
857 arg1, arg2, arg3);
858 }
859
860 static void
861 dwarf2_macros_too_long_complaint (void)
862 {
863 complaint (&symfile_complaints,
864 _("macro info runs off end of `.debug_macinfo' section"));
865 }
866
867 static void
868 dwarf2_macro_malformed_definition_complaint (const char *arg1)
869 {
870 complaint (&symfile_complaints,
871 _("macro debug info contains a "
872 "malformed macro definition:\n`%s'"),
873 arg1);
874 }
875
876 static void
877 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
878 {
879 complaint (&symfile_complaints,
880 _("invalid attribute class or form for '%s' in '%s'"),
881 arg1, arg2);
882 }
883
884 /* local function prototypes */
885
886 static void dwarf2_locate_sections (bfd *, asection *, void *);
887
888 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
889 struct objfile *);
890
891 static void dwarf2_find_base_address (struct die_info *die,
892 struct dwarf2_cu *cu);
893
894 static void dwarf2_build_psymtabs_hard (struct objfile *);
895
896 static void scan_partial_symbols (struct partial_die_info *,
897 CORE_ADDR *, CORE_ADDR *,
898 int, struct dwarf2_cu *);
899
900 static void add_partial_symbol (struct partial_die_info *,
901 struct dwarf2_cu *);
902
903 static void add_partial_namespace (struct partial_die_info *pdi,
904 CORE_ADDR *lowpc, CORE_ADDR *highpc,
905 int need_pc, struct dwarf2_cu *cu);
906
907 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
908 CORE_ADDR *highpc, int need_pc,
909 struct dwarf2_cu *cu);
910
911 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
912 struct dwarf2_cu *cu);
913
914 static void add_partial_subprogram (struct partial_die_info *pdi,
915 CORE_ADDR *lowpc, CORE_ADDR *highpc,
916 int need_pc, struct dwarf2_cu *cu);
917
918 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
919 gdb_byte *buffer, gdb_byte *info_ptr,
920 bfd *abfd, struct dwarf2_cu *cu);
921
922 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
923
924 static void psymtab_to_symtab_1 (struct partial_symtab *);
925
926 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
927
928 static void dwarf2_free_abbrev_table (void *);
929
930 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
931 struct dwarf2_cu *);
932
933 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
934 struct dwarf2_cu *);
935
936 static struct partial_die_info *load_partial_dies (bfd *,
937 gdb_byte *, gdb_byte *,
938 int, struct dwarf2_cu *);
939
940 static gdb_byte *read_partial_die (struct partial_die_info *,
941 struct abbrev_info *abbrev,
942 unsigned int, bfd *,
943 gdb_byte *, gdb_byte *,
944 struct dwarf2_cu *);
945
946 static struct partial_die_info *find_partial_die (unsigned int,
947 struct dwarf2_cu *);
948
949 static void fixup_partial_die (struct partial_die_info *,
950 struct dwarf2_cu *);
951
952 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
953 bfd *, gdb_byte *, struct dwarf2_cu *);
954
955 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
956 bfd *, gdb_byte *, struct dwarf2_cu *);
957
958 static unsigned int read_1_byte (bfd *, gdb_byte *);
959
960 static int read_1_signed_byte (bfd *, gdb_byte *);
961
962 static unsigned int read_2_bytes (bfd *, gdb_byte *);
963
964 static unsigned int read_4_bytes (bfd *, gdb_byte *);
965
966 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
967
968 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
969 unsigned int *);
970
971 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
972
973 static LONGEST read_checked_initial_length_and_offset
974 (bfd *, gdb_byte *, const struct comp_unit_head *,
975 unsigned int *, unsigned int *);
976
977 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
978 unsigned int *);
979
980 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
981
982 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
983
984 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
985
986 static char *read_indirect_string (bfd *, gdb_byte *,
987 const struct comp_unit_head *,
988 unsigned int *);
989
990 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
991
992 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
993
994 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
995
996 static void set_cu_language (unsigned int, struct dwarf2_cu *);
997
998 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
999 struct dwarf2_cu *);
1000
1001 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1002 unsigned int,
1003 struct dwarf2_cu *);
1004
1005 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1006 struct dwarf2_cu *cu);
1007
1008 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1009
1010 static struct die_info *die_specification (struct die_info *die,
1011 struct dwarf2_cu **);
1012
1013 static void free_line_header (struct line_header *lh);
1014
1015 static void add_file_name (struct line_header *, char *, unsigned int,
1016 unsigned int, unsigned int);
1017
1018 static struct line_header *(dwarf_decode_line_header
1019 (unsigned int offset,
1020 bfd *abfd, struct dwarf2_cu *cu));
1021
1022 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1023 struct dwarf2_cu *, struct partial_symtab *);
1024
1025 static void dwarf2_start_subfile (char *, const char *, const char *);
1026
1027 static struct symbol *new_symbol (struct die_info *, struct type *,
1028 struct dwarf2_cu *);
1029
1030 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1031 struct dwarf2_cu *, struct symbol *);
1032
1033 static void dwarf2_const_value (struct attribute *, struct symbol *,
1034 struct dwarf2_cu *);
1035
1036 static void dwarf2_const_value_attr (struct attribute *attr,
1037 struct type *type,
1038 const char *name,
1039 struct obstack *obstack,
1040 struct dwarf2_cu *cu, long *value,
1041 gdb_byte **bytes,
1042 struct dwarf2_locexpr_baton **baton);
1043
1044 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1045
1046 static int need_gnat_info (struct dwarf2_cu *);
1047
1048 static struct type *die_descriptive_type (struct die_info *,
1049 struct dwarf2_cu *);
1050
1051 static void set_descriptive_type (struct type *, struct die_info *,
1052 struct dwarf2_cu *);
1053
1054 static struct type *die_containing_type (struct die_info *,
1055 struct dwarf2_cu *);
1056
1057 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1058 struct dwarf2_cu *);
1059
1060 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1061
1062 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1063
1064 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1065
1066 static char *typename_concat (struct obstack *obs, const char *prefix,
1067 const char *suffix, int physname,
1068 struct dwarf2_cu *cu);
1069
1070 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1071
1072 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1073
1074 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1075
1076 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1077
1078 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1079 struct dwarf2_cu *, struct partial_symtab *);
1080
1081 static int dwarf2_get_pc_bounds (struct die_info *,
1082 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1083 struct partial_symtab *);
1084
1085 static void get_scope_pc_bounds (struct die_info *,
1086 CORE_ADDR *, CORE_ADDR *,
1087 struct dwarf2_cu *);
1088
1089 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1090 CORE_ADDR, struct dwarf2_cu *);
1091
1092 static void dwarf2_add_field (struct field_info *, struct die_info *,
1093 struct dwarf2_cu *);
1094
1095 static void dwarf2_attach_fields_to_type (struct field_info *,
1096 struct type *, struct dwarf2_cu *);
1097
1098 static void dwarf2_add_member_fn (struct field_info *,
1099 struct die_info *, struct type *,
1100 struct dwarf2_cu *);
1101
1102 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1103 struct type *,
1104 struct dwarf2_cu *);
1105
1106 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1107
1108 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1109
1110 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1111
1112 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1113
1114 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1115
1116 static struct type *read_module_type (struct die_info *die,
1117 struct dwarf2_cu *cu);
1118
1119 static const char *namespace_name (struct die_info *die,
1120 int *is_anonymous, struct dwarf2_cu *);
1121
1122 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1123
1124 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1125
1126 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1127 struct dwarf2_cu *);
1128
1129 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1130
1131 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1132 gdb_byte *info_ptr,
1133 gdb_byte **new_info_ptr,
1134 struct die_info *parent);
1135
1136 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1137 gdb_byte *info_ptr,
1138 gdb_byte **new_info_ptr,
1139 struct die_info *parent);
1140
1141 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1142 gdb_byte *info_ptr,
1143 gdb_byte **new_info_ptr,
1144 struct die_info *parent);
1145
1146 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1147 struct die_info **, gdb_byte *,
1148 int *);
1149
1150 static void process_die (struct die_info *, struct dwarf2_cu *);
1151
1152 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1153 struct obstack *);
1154
1155 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1156
1157 static const char *dwarf2_full_name (char *name,
1158 struct die_info *die,
1159 struct dwarf2_cu *cu);
1160
1161 static struct die_info *dwarf2_extension (struct die_info *die,
1162 struct dwarf2_cu **);
1163
1164 static char *dwarf_tag_name (unsigned int);
1165
1166 static char *dwarf_attr_name (unsigned int);
1167
1168 static char *dwarf_form_name (unsigned int);
1169
1170 static char *dwarf_bool_name (unsigned int);
1171
1172 static char *dwarf_type_encoding_name (unsigned int);
1173
1174 #if 0
1175 static char *dwarf_cfi_name (unsigned int);
1176 #endif
1177
1178 static struct die_info *sibling_die (struct die_info *);
1179
1180 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1181
1182 static void dump_die_for_error (struct die_info *);
1183
1184 static void dump_die_1 (struct ui_file *, int level, int max_level,
1185 struct die_info *);
1186
1187 /*static*/ void dump_die (struct die_info *, int max_level);
1188
1189 static void store_in_ref_table (struct die_info *,
1190 struct dwarf2_cu *);
1191
1192 static int is_ref_attr (struct attribute *);
1193
1194 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1195
1196 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1197
1198 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1199 struct attribute *,
1200 struct dwarf2_cu **);
1201
1202 static struct die_info *follow_die_ref (struct die_info *,
1203 struct attribute *,
1204 struct dwarf2_cu **);
1205
1206 static struct die_info *follow_die_sig (struct die_info *,
1207 struct attribute *,
1208 struct dwarf2_cu **);
1209
1210 static struct signatured_type *lookup_signatured_type_at_offset
1211 (struct objfile *objfile,
1212 struct dwarf2_section_info *section,
1213 unsigned int offset);
1214
1215 static void read_signatured_type_at_offset (struct objfile *objfile,
1216 struct dwarf2_section_info *sect,
1217 unsigned int offset);
1218
1219 static void read_signatured_type (struct objfile *,
1220 struct signatured_type *type_sig);
1221
1222 /* memory allocation interface */
1223
1224 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1225
1226 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1227
1228 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1229
1230 static void initialize_cu_func_list (struct dwarf2_cu *);
1231
1232 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1233 struct dwarf2_cu *);
1234
1235 static void dwarf_decode_macros (struct line_header *, unsigned int,
1236 char *, bfd *, struct dwarf2_cu *);
1237
1238 static int attr_form_is_block (struct attribute *);
1239
1240 static int attr_form_is_section_offset (struct attribute *);
1241
1242 static int attr_form_is_constant (struct attribute *);
1243
1244 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1245 struct dwarf2_loclist_baton *baton,
1246 struct attribute *attr);
1247
1248 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1249 struct symbol *sym,
1250 struct dwarf2_cu *cu);
1251
1252 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1253 struct abbrev_info *abbrev,
1254 struct dwarf2_cu *cu);
1255
1256 static void free_stack_comp_unit (void *);
1257
1258 static hashval_t partial_die_hash (const void *item);
1259
1260 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1261
1262 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1263 (unsigned int offset, struct objfile *objfile);
1264
1265 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1266 (unsigned int offset, struct objfile *objfile);
1267
1268 static void init_one_comp_unit (struct dwarf2_cu *cu,
1269 struct objfile *objfile);
1270
1271 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1272 struct die_info *comp_unit_die);
1273
1274 static void free_one_comp_unit (void *);
1275
1276 static void free_cached_comp_units (void *);
1277
1278 static void age_cached_comp_units (void);
1279
1280 static void free_one_cached_comp_unit (void *);
1281
1282 static struct type *set_die_type (struct die_info *, struct type *,
1283 struct dwarf2_cu *);
1284
1285 static void create_all_comp_units (struct objfile *);
1286
1287 static int create_debug_types_hash_table (struct objfile *objfile);
1288
1289 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1290 struct objfile *);
1291
1292 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1293
1294 static void dwarf2_add_dependence (struct dwarf2_cu *,
1295 struct dwarf2_per_cu_data *);
1296
1297 static void dwarf2_mark (struct dwarf2_cu *);
1298
1299 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1300
1301 static struct type *get_die_type_at_offset (unsigned int,
1302 struct dwarf2_per_cu_data *per_cu);
1303
1304 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1305
1306 static void dwarf2_release_queue (void *dummy);
1307
1308 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1309 struct objfile *objfile);
1310
1311 static void process_queue (struct objfile *objfile);
1312
1313 static void find_file_and_directory (struct die_info *die,
1314 struct dwarf2_cu *cu,
1315 char **name, char **comp_dir);
1316
1317 static char *file_full_name (int file, struct line_header *lh,
1318 const char *comp_dir);
1319
1320 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1321 gdb_byte *info_ptr,
1322 gdb_byte *buffer,
1323 unsigned int buffer_size,
1324 bfd *abfd);
1325
1326 static void init_cu_die_reader (struct die_reader_specs *reader,
1327 struct dwarf2_cu *cu);
1328
1329 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1330
1331 #if WORDS_BIGENDIAN
1332
1333 /* Convert VALUE between big- and little-endian. */
1334 static offset_type
1335 byte_swap (offset_type value)
1336 {
1337 offset_type result;
1338
1339 result = (value & 0xff) << 24;
1340 result |= (value & 0xff00) << 8;
1341 result |= (value & 0xff0000) >> 8;
1342 result |= (value & 0xff000000) >> 24;
1343 return result;
1344 }
1345
1346 #define MAYBE_SWAP(V) byte_swap (V)
1347
1348 #else
1349 #define MAYBE_SWAP(V) (V)
1350 #endif /* WORDS_BIGENDIAN */
1351
1352 /* The suffix for an index file. */
1353 #define INDEX_SUFFIX ".gdb-index"
1354
1355 static const char *dwarf2_physname (char *name, struct die_info *die,
1356 struct dwarf2_cu *cu);
1357
1358 /* Try to locate the sections we need for DWARF 2 debugging
1359 information and return true if we have enough to do something.
1360 NAMES points to the dwarf2 section names, or is NULL if the standard
1361 ELF names are used. */
1362
1363 int
1364 dwarf2_has_info (struct objfile *objfile,
1365 const struct dwarf2_debug_sections *names)
1366 {
1367 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1368 if (!dwarf2_per_objfile)
1369 {
1370 /* Initialize per-objfile state. */
1371 struct dwarf2_per_objfile *data
1372 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1373
1374 memset (data, 0, sizeof (*data));
1375 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1376 dwarf2_per_objfile = data;
1377
1378 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1379 (void *) names);
1380 dwarf2_per_objfile->objfile = objfile;
1381 }
1382 return (dwarf2_per_objfile->info.asection != NULL
1383 && dwarf2_per_objfile->abbrev.asection != NULL);
1384 }
1385
1386 /* When loading sections, we look either for uncompressed section or for
1387 compressed section names. */
1388
1389 static int
1390 section_is_p (const char *section_name,
1391 const struct dwarf2_section_names *names)
1392 {
1393 if (names->normal != NULL
1394 && strcmp (section_name, names->normal) == 0)
1395 return 1;
1396 if (names->compressed != NULL
1397 && strcmp (section_name, names->compressed) == 0)
1398 return 1;
1399 return 0;
1400 }
1401
1402 /* This function is mapped across the sections and remembers the
1403 offset and size of each of the debugging sections we are interested
1404 in. */
1405
1406 static void
1407 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1408 {
1409 const struct dwarf2_debug_sections *names;
1410
1411 if (vnames == NULL)
1412 names = &dwarf2_elf_names;
1413 else
1414 names = (const struct dwarf2_debug_sections *) vnames;
1415
1416 if (section_is_p (sectp->name, &names->info))
1417 {
1418 dwarf2_per_objfile->info.asection = sectp;
1419 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1420 }
1421 else if (section_is_p (sectp->name, &names->abbrev))
1422 {
1423 dwarf2_per_objfile->abbrev.asection = sectp;
1424 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1425 }
1426 else if (section_is_p (sectp->name, &names->line))
1427 {
1428 dwarf2_per_objfile->line.asection = sectp;
1429 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1430 }
1431 else if (section_is_p (sectp->name, &names->loc))
1432 {
1433 dwarf2_per_objfile->loc.asection = sectp;
1434 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1435 }
1436 else if (section_is_p (sectp->name, &names->macinfo))
1437 {
1438 dwarf2_per_objfile->macinfo.asection = sectp;
1439 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1440 }
1441 else if (section_is_p (sectp->name, &names->str))
1442 {
1443 dwarf2_per_objfile->str.asection = sectp;
1444 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1445 }
1446 else if (section_is_p (sectp->name, &names->frame))
1447 {
1448 dwarf2_per_objfile->frame.asection = sectp;
1449 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1450 }
1451 else if (section_is_p (sectp->name, &names->eh_frame))
1452 {
1453 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1454
1455 if (aflag & SEC_HAS_CONTENTS)
1456 {
1457 dwarf2_per_objfile->eh_frame.asection = sectp;
1458 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1459 }
1460 }
1461 else if (section_is_p (sectp->name, &names->ranges))
1462 {
1463 dwarf2_per_objfile->ranges.asection = sectp;
1464 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1465 }
1466 else if (section_is_p (sectp->name, &names->types))
1467 {
1468 struct dwarf2_section_info type_section;
1469
1470 memset (&type_section, 0, sizeof (type_section));
1471 type_section.asection = sectp;
1472 type_section.size = bfd_get_section_size (sectp);
1473
1474 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1475 &type_section);
1476 }
1477 else if (section_is_p (sectp->name, &names->gdb_index))
1478 {
1479 dwarf2_per_objfile->gdb_index.asection = sectp;
1480 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1481 }
1482
1483 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1484 && bfd_section_vma (abfd, sectp) == 0)
1485 dwarf2_per_objfile->has_section_at_zero = 1;
1486 }
1487
1488 /* Decompress a section that was compressed using zlib. Store the
1489 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1490
1491 static void
1492 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1493 gdb_byte **outbuf, bfd_size_type *outsize)
1494 {
1495 bfd *abfd = objfile->obfd;
1496 #ifndef HAVE_ZLIB_H
1497 error (_("Support for zlib-compressed DWARF data (from '%s') "
1498 "is disabled in this copy of GDB"),
1499 bfd_get_filename (abfd));
1500 #else
1501 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1502 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1503 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1504 bfd_size_type uncompressed_size;
1505 gdb_byte *uncompressed_buffer;
1506 z_stream strm;
1507 int rc;
1508 int header_size = 12;
1509
1510 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1511 || bfd_bread (compressed_buffer,
1512 compressed_size, abfd) != compressed_size)
1513 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1514 bfd_get_filename (abfd));
1515
1516 /* Read the zlib header. In this case, it should be "ZLIB" followed
1517 by the uncompressed section size, 8 bytes in big-endian order. */
1518 if (compressed_size < header_size
1519 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1520 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1521 bfd_get_filename (abfd));
1522 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1523 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1524 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1525 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1526 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1527 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1528 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1529 uncompressed_size += compressed_buffer[11];
1530
1531 /* It is possible the section consists of several compressed
1532 buffers concatenated together, so we uncompress in a loop. */
1533 strm.zalloc = NULL;
1534 strm.zfree = NULL;
1535 strm.opaque = NULL;
1536 strm.avail_in = compressed_size - header_size;
1537 strm.next_in = (Bytef*) compressed_buffer + header_size;
1538 strm.avail_out = uncompressed_size;
1539 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1540 uncompressed_size);
1541 rc = inflateInit (&strm);
1542 while (strm.avail_in > 0)
1543 {
1544 if (rc != Z_OK)
1545 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1546 bfd_get_filename (abfd), rc);
1547 strm.next_out = ((Bytef*) uncompressed_buffer
1548 + (uncompressed_size - strm.avail_out));
1549 rc = inflate (&strm, Z_FINISH);
1550 if (rc != Z_STREAM_END)
1551 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1552 bfd_get_filename (abfd), rc);
1553 rc = inflateReset (&strm);
1554 }
1555 rc = inflateEnd (&strm);
1556 if (rc != Z_OK
1557 || strm.avail_out != 0)
1558 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1559 bfd_get_filename (abfd), rc);
1560
1561 do_cleanups (cleanup);
1562 *outbuf = uncompressed_buffer;
1563 *outsize = uncompressed_size;
1564 #endif
1565 }
1566
1567 /* A helper function that decides whether a section is empty. */
1568
1569 static int
1570 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1571 {
1572 return info->asection == NULL || info->size == 0;
1573 }
1574
1575 /* Read the contents of the section SECTP from object file specified by
1576 OBJFILE, store info about the section into INFO.
1577 If the section is compressed, uncompress it before returning. */
1578
1579 static void
1580 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1581 {
1582 bfd *abfd = objfile->obfd;
1583 asection *sectp = info->asection;
1584 gdb_byte *buf, *retbuf;
1585 unsigned char header[4];
1586
1587 if (info->readin)
1588 return;
1589 info->buffer = NULL;
1590 info->map_addr = NULL;
1591 info->readin = 1;
1592
1593 if (dwarf2_section_empty_p (info))
1594 return;
1595
1596 /* Check if the file has a 4-byte header indicating compression. */
1597 if (info->size > sizeof (header)
1598 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1599 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1600 {
1601 /* Upon decompression, update the buffer and its size. */
1602 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1603 {
1604 zlib_decompress_section (objfile, sectp, &info->buffer,
1605 &info->size);
1606 return;
1607 }
1608 }
1609
1610 #ifdef HAVE_MMAP
1611 if (pagesize == 0)
1612 pagesize = getpagesize ();
1613
1614 /* Only try to mmap sections which are large enough: we don't want to
1615 waste space due to fragmentation. Also, only try mmap for sections
1616 without relocations. */
1617
1618 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1619 {
1620 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1621 MAP_PRIVATE, sectp->filepos,
1622 &info->map_addr, &info->map_len);
1623
1624 if ((caddr_t)info->buffer != MAP_FAILED)
1625 {
1626 #if HAVE_POSIX_MADVISE
1627 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1628 #endif
1629 return;
1630 }
1631 }
1632 #endif
1633
1634 /* If we get here, we are a normal, not-compressed section. */
1635 info->buffer = buf
1636 = obstack_alloc (&objfile->objfile_obstack, info->size);
1637
1638 /* When debugging .o files, we may need to apply relocations; see
1639 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1640 We never compress sections in .o files, so we only need to
1641 try this when the section is not compressed. */
1642 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1643 if (retbuf != NULL)
1644 {
1645 info->buffer = retbuf;
1646 return;
1647 }
1648
1649 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1650 || bfd_bread (buf, info->size, abfd) != info->size)
1651 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1652 bfd_get_filename (abfd));
1653 }
1654
1655 /* A helper function that returns the size of a section in a safe way.
1656 If you are positive that the section has been read before using the
1657 size, then it is safe to refer to the dwarf2_section_info object's
1658 "size" field directly. In other cases, you must call this
1659 function, because for compressed sections the size field is not set
1660 correctly until the section has been read. */
1661
1662 static bfd_size_type
1663 dwarf2_section_size (struct objfile *objfile,
1664 struct dwarf2_section_info *info)
1665 {
1666 if (!info->readin)
1667 dwarf2_read_section (objfile, info);
1668 return info->size;
1669 }
1670
1671 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1672 SECTION_NAME. */
1673
1674 void
1675 dwarf2_get_section_info (struct objfile *objfile,
1676 enum dwarf2_section_enum sect,
1677 asection **sectp, gdb_byte **bufp,
1678 bfd_size_type *sizep)
1679 {
1680 struct dwarf2_per_objfile *data
1681 = objfile_data (objfile, dwarf2_objfile_data_key);
1682 struct dwarf2_section_info *info;
1683
1684 /* We may see an objfile without any DWARF, in which case we just
1685 return nothing. */
1686 if (data == NULL)
1687 {
1688 *sectp = NULL;
1689 *bufp = NULL;
1690 *sizep = 0;
1691 return;
1692 }
1693 switch (sect)
1694 {
1695 case DWARF2_DEBUG_FRAME:
1696 info = &data->frame;
1697 break;
1698 case DWARF2_EH_FRAME:
1699 info = &data->eh_frame;
1700 break;
1701 default:
1702 gdb_assert_not_reached ("unexpected section");
1703 }
1704
1705 dwarf2_read_section (objfile, info);
1706
1707 *sectp = info->asection;
1708 *bufp = info->buffer;
1709 *sizep = info->size;
1710 }
1711
1712 \f
1713 /* DWARF quick_symbols_functions support. */
1714
1715 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1716 unique line tables, so we maintain a separate table of all .debug_line
1717 derived entries to support the sharing.
1718 All the quick functions need is the list of file names. We discard the
1719 line_header when we're done and don't need to record it here. */
1720 struct quick_file_names
1721 {
1722 /* The offset in .debug_line of the line table. We hash on this. */
1723 unsigned int offset;
1724
1725 /* The number of entries in file_names, real_names. */
1726 unsigned int num_file_names;
1727
1728 /* The file names from the line table, after being run through
1729 file_full_name. */
1730 const char **file_names;
1731
1732 /* The file names from the line table after being run through
1733 gdb_realpath. These are computed lazily. */
1734 const char **real_names;
1735 };
1736
1737 /* When using the index (and thus not using psymtabs), each CU has an
1738 object of this type. This is used to hold information needed by
1739 the various "quick" methods. */
1740 struct dwarf2_per_cu_quick_data
1741 {
1742 /* The file table. This can be NULL if there was no file table
1743 or it's currently not read in.
1744 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1745 struct quick_file_names *file_names;
1746
1747 /* The corresponding symbol table. This is NULL if symbols for this
1748 CU have not yet been read. */
1749 struct symtab *symtab;
1750
1751 /* A temporary mark bit used when iterating over all CUs in
1752 expand_symtabs_matching. */
1753 unsigned int mark : 1;
1754
1755 /* True if we've tried to read the file table and found there isn't one.
1756 There will be no point in trying to read it again next time. */
1757 unsigned int no_file_data : 1;
1758 };
1759
1760 /* Hash function for a quick_file_names. */
1761
1762 static hashval_t
1763 hash_file_name_entry (const void *e)
1764 {
1765 const struct quick_file_names *file_data = e;
1766
1767 return file_data->offset;
1768 }
1769
1770 /* Equality function for a quick_file_names. */
1771
1772 static int
1773 eq_file_name_entry (const void *a, const void *b)
1774 {
1775 const struct quick_file_names *ea = a;
1776 const struct quick_file_names *eb = b;
1777
1778 return ea->offset == eb->offset;
1779 }
1780
1781 /* Delete function for a quick_file_names. */
1782
1783 static void
1784 delete_file_name_entry (void *e)
1785 {
1786 struct quick_file_names *file_data = e;
1787 int i;
1788
1789 for (i = 0; i < file_data->num_file_names; ++i)
1790 {
1791 xfree ((void*) file_data->file_names[i]);
1792 if (file_data->real_names)
1793 xfree ((void*) file_data->real_names[i]);
1794 }
1795
1796 /* The space for the struct itself lives on objfile_obstack,
1797 so we don't free it here. */
1798 }
1799
1800 /* Create a quick_file_names hash table. */
1801
1802 static htab_t
1803 create_quick_file_names_table (unsigned int nr_initial_entries)
1804 {
1805 return htab_create_alloc (nr_initial_entries,
1806 hash_file_name_entry, eq_file_name_entry,
1807 delete_file_name_entry, xcalloc, xfree);
1808 }
1809
1810 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1811 have to be created afterwards. You should call age_cached_comp_units after
1812 processing PER_CU->CU. dw2_setup must have been already called. */
1813
1814 static void
1815 load_cu (struct dwarf2_per_cu_data *per_cu)
1816 {
1817 if (per_cu->debug_type_section)
1818 read_signatured_type_at_offset (per_cu->objfile,
1819 per_cu->debug_type_section,
1820 per_cu->offset);
1821 else
1822 load_full_comp_unit (per_cu, per_cu->objfile);
1823
1824 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1825
1826 gdb_assert (per_cu->cu != NULL);
1827 }
1828
1829 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1830 this CU came. */
1831
1832 static void
1833 dw2_do_instantiate_symtab (struct objfile *objfile,
1834 struct dwarf2_per_cu_data *per_cu)
1835 {
1836 struct cleanup *back_to;
1837
1838 back_to = make_cleanup (dwarf2_release_queue, NULL);
1839
1840 queue_comp_unit (per_cu, objfile);
1841
1842 load_cu (per_cu);
1843
1844 process_queue (objfile);
1845
1846 /* Age the cache, releasing compilation units that have not
1847 been used recently. */
1848 age_cached_comp_units ();
1849
1850 do_cleanups (back_to);
1851 }
1852
1853 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1854 the objfile from which this CU came. Returns the resulting symbol
1855 table. */
1856
1857 static struct symtab *
1858 dw2_instantiate_symtab (struct objfile *objfile,
1859 struct dwarf2_per_cu_data *per_cu)
1860 {
1861 if (!per_cu->v.quick->symtab)
1862 {
1863 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1864 increment_reading_symtab ();
1865 dw2_do_instantiate_symtab (objfile, per_cu);
1866 do_cleanups (back_to);
1867 }
1868 return per_cu->v.quick->symtab;
1869 }
1870
1871 /* Return the CU given its index. */
1872
1873 static struct dwarf2_per_cu_data *
1874 dw2_get_cu (int index)
1875 {
1876 if (index >= dwarf2_per_objfile->n_comp_units)
1877 {
1878 index -= dwarf2_per_objfile->n_comp_units;
1879 return dwarf2_per_objfile->type_comp_units[index];
1880 }
1881 return dwarf2_per_objfile->all_comp_units[index];
1882 }
1883
1884 /* A helper function that knows how to read a 64-bit value in a way
1885 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1886 otherwise. */
1887
1888 static int
1889 extract_cu_value (const char *bytes, ULONGEST *result)
1890 {
1891 if (sizeof (ULONGEST) < 8)
1892 {
1893 int i;
1894
1895 /* Ignore the upper 4 bytes if they are all zero. */
1896 for (i = 0; i < 4; ++i)
1897 if (bytes[i + 4] != 0)
1898 return 0;
1899
1900 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1901 }
1902 else
1903 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1904 return 1;
1905 }
1906
1907 /* Read the CU list from the mapped index, and use it to create all
1908 the CU objects for this objfile. Return 0 if something went wrong,
1909 1 if everything went ok. */
1910
1911 static int
1912 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1913 offset_type cu_list_elements)
1914 {
1915 offset_type i;
1916
1917 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1918 dwarf2_per_objfile->all_comp_units
1919 = obstack_alloc (&objfile->objfile_obstack,
1920 dwarf2_per_objfile->n_comp_units
1921 * sizeof (struct dwarf2_per_cu_data *));
1922
1923 for (i = 0; i < cu_list_elements; i += 2)
1924 {
1925 struct dwarf2_per_cu_data *the_cu;
1926 ULONGEST offset, length;
1927
1928 if (!extract_cu_value (cu_list, &offset)
1929 || !extract_cu_value (cu_list + 8, &length))
1930 return 0;
1931 cu_list += 2 * 8;
1932
1933 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1934 struct dwarf2_per_cu_data);
1935 the_cu->offset = offset;
1936 the_cu->length = length;
1937 the_cu->objfile = objfile;
1938 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1939 struct dwarf2_per_cu_quick_data);
1940 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1941 }
1942
1943 return 1;
1944 }
1945
1946 /* Create the signatured type hash table from the index. */
1947
1948 static int
1949 create_signatured_type_table_from_index (struct objfile *objfile,
1950 struct dwarf2_section_info *section,
1951 const gdb_byte *bytes,
1952 offset_type elements)
1953 {
1954 offset_type i;
1955 htab_t sig_types_hash;
1956
1957 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1958 dwarf2_per_objfile->type_comp_units
1959 = obstack_alloc (&objfile->objfile_obstack,
1960 dwarf2_per_objfile->n_type_comp_units
1961 * sizeof (struct dwarf2_per_cu_data *));
1962
1963 sig_types_hash = allocate_signatured_type_table (objfile);
1964
1965 for (i = 0; i < elements; i += 3)
1966 {
1967 struct signatured_type *type_sig;
1968 ULONGEST offset, type_offset, signature;
1969 void **slot;
1970
1971 if (!extract_cu_value (bytes, &offset)
1972 || !extract_cu_value (bytes + 8, &type_offset))
1973 return 0;
1974 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1975 bytes += 3 * 8;
1976
1977 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1978 struct signatured_type);
1979 type_sig->signature = signature;
1980 type_sig->type_offset = type_offset;
1981 type_sig->per_cu.debug_type_section = section;
1982 type_sig->per_cu.offset = offset;
1983 type_sig->per_cu.objfile = objfile;
1984 type_sig->per_cu.v.quick
1985 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1986 struct dwarf2_per_cu_quick_data);
1987
1988 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1989 *slot = type_sig;
1990
1991 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1992 }
1993
1994 dwarf2_per_objfile->signatured_types = sig_types_hash;
1995
1996 return 1;
1997 }
1998
1999 /* Read the address map data from the mapped index, and use it to
2000 populate the objfile's psymtabs_addrmap. */
2001
2002 static void
2003 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2004 {
2005 const gdb_byte *iter, *end;
2006 struct obstack temp_obstack;
2007 struct addrmap *mutable_map;
2008 struct cleanup *cleanup;
2009 CORE_ADDR baseaddr;
2010
2011 obstack_init (&temp_obstack);
2012 cleanup = make_cleanup_obstack_free (&temp_obstack);
2013 mutable_map = addrmap_create_mutable (&temp_obstack);
2014
2015 iter = index->address_table;
2016 end = iter + index->address_table_size;
2017
2018 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2019
2020 while (iter < end)
2021 {
2022 ULONGEST hi, lo, cu_index;
2023 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2024 iter += 8;
2025 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2026 iter += 8;
2027 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2028 iter += 4;
2029
2030 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2031 dw2_get_cu (cu_index));
2032 }
2033
2034 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2035 &objfile->objfile_obstack);
2036 do_cleanups (cleanup);
2037 }
2038
2039 /* The hash function for strings in the mapped index. This is the same as
2040 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2041 implementation. This is necessary because the hash function is tied to the
2042 format of the mapped index file. The hash values do not have to match with
2043 SYMBOL_HASH_NEXT.
2044
2045 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2046
2047 static hashval_t
2048 mapped_index_string_hash (int index_version, const void *p)
2049 {
2050 const unsigned char *str = (const unsigned char *) p;
2051 hashval_t r = 0;
2052 unsigned char c;
2053
2054 while ((c = *str++) != 0)
2055 {
2056 if (index_version >= 5)
2057 c = tolower (c);
2058 r = r * 67 + c - 113;
2059 }
2060
2061 return r;
2062 }
2063
2064 /* Find a slot in the mapped index INDEX for the object named NAME.
2065 If NAME is found, set *VEC_OUT to point to the CU vector in the
2066 constant pool and return 1. If NAME cannot be found, return 0. */
2067
2068 static int
2069 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2070 offset_type **vec_out)
2071 {
2072 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2073 offset_type hash;
2074 offset_type slot, step;
2075 int (*cmp) (const char *, const char *);
2076
2077 if (current_language->la_language == language_cplus
2078 || current_language->la_language == language_java
2079 || current_language->la_language == language_fortran)
2080 {
2081 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2082 not contain any. */
2083 const char *paren = strchr (name, '(');
2084
2085 if (paren)
2086 {
2087 char *dup;
2088
2089 dup = xmalloc (paren - name + 1);
2090 memcpy (dup, name, paren - name);
2091 dup[paren - name] = 0;
2092
2093 make_cleanup (xfree, dup);
2094 name = dup;
2095 }
2096 }
2097
2098 /* Index version 4 did not support case insensitive searches. But the
2099 indexes for case insensitive languages are built in lowercase, therefore
2100 simulate our NAME being searched is also lowercased. */
2101 hash = mapped_index_string_hash ((index->version == 4
2102 && case_sensitivity == case_sensitive_off
2103 ? 5 : index->version),
2104 name);
2105
2106 slot = hash & (index->symbol_table_slots - 1);
2107 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2108 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2109
2110 for (;;)
2111 {
2112 /* Convert a slot number to an offset into the table. */
2113 offset_type i = 2 * slot;
2114 const char *str;
2115 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2116 {
2117 do_cleanups (back_to);
2118 return 0;
2119 }
2120
2121 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2122 if (!cmp (name, str))
2123 {
2124 *vec_out = (offset_type *) (index->constant_pool
2125 + MAYBE_SWAP (index->symbol_table[i + 1]));
2126 do_cleanups (back_to);
2127 return 1;
2128 }
2129
2130 slot = (slot + step) & (index->symbol_table_slots - 1);
2131 }
2132 }
2133
2134 /* Read the index file. If everything went ok, initialize the "quick"
2135 elements of all the CUs and return 1. Otherwise, return 0. */
2136
2137 static int
2138 dwarf2_read_index (struct objfile *objfile)
2139 {
2140 char *addr;
2141 struct mapped_index *map;
2142 offset_type *metadata;
2143 const gdb_byte *cu_list;
2144 const gdb_byte *types_list = NULL;
2145 offset_type version, cu_list_elements;
2146 offset_type types_list_elements = 0;
2147 int i;
2148
2149 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2150 return 0;
2151
2152 /* Older elfutils strip versions could keep the section in the main
2153 executable while splitting it for the separate debug info file. */
2154 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2155 & SEC_HAS_CONTENTS) == 0)
2156 return 0;
2157
2158 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2159
2160 addr = dwarf2_per_objfile->gdb_index.buffer;
2161 /* Version check. */
2162 version = MAYBE_SWAP (*(offset_type *) addr);
2163 /* Versions earlier than 3 emitted every copy of a psymbol. This
2164 causes the index to behave very poorly for certain requests. Version 3
2165 contained incomplete addrmap. So, it seems better to just ignore such
2166 indices. Index version 4 uses a different hash function than index
2167 version 5 and later. */
2168 if (version < 4)
2169 return 0;
2170 /* Indexes with higher version than the one supported by GDB may be no
2171 longer backward compatible. */
2172 if (version > 5)
2173 return 0;
2174
2175 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2176 map->version = version;
2177 map->total_size = dwarf2_per_objfile->gdb_index.size;
2178
2179 metadata = (offset_type *) (addr + sizeof (offset_type));
2180
2181 i = 0;
2182 cu_list = addr + MAYBE_SWAP (metadata[i]);
2183 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2184 / 8);
2185 ++i;
2186
2187 types_list = addr + MAYBE_SWAP (metadata[i]);
2188 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2189 - MAYBE_SWAP (metadata[i]))
2190 / 8);
2191 ++i;
2192
2193 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2194 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2195 - MAYBE_SWAP (metadata[i]));
2196 ++i;
2197
2198 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2199 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2200 - MAYBE_SWAP (metadata[i]))
2201 / (2 * sizeof (offset_type)));
2202 ++i;
2203
2204 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2205
2206 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2207 return 0;
2208
2209 if (types_list_elements)
2210 {
2211 struct dwarf2_section_info *section;
2212
2213 /* We can only handle a single .debug_types when we have an
2214 index. */
2215 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2216 return 0;
2217
2218 section = VEC_index (dwarf2_section_info_def,
2219 dwarf2_per_objfile->types, 0);
2220
2221 if (!create_signatured_type_table_from_index (objfile, section,
2222 types_list,
2223 types_list_elements))
2224 return 0;
2225 }
2226
2227 create_addrmap_from_index (objfile, map);
2228
2229 dwarf2_per_objfile->index_table = map;
2230 dwarf2_per_objfile->using_index = 1;
2231 dwarf2_per_objfile->quick_file_names_table =
2232 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2233
2234 return 1;
2235 }
2236
2237 /* A helper for the "quick" functions which sets the global
2238 dwarf2_per_objfile according to OBJFILE. */
2239
2240 static void
2241 dw2_setup (struct objfile *objfile)
2242 {
2243 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2244 gdb_assert (dwarf2_per_objfile);
2245 }
2246
2247 /* A helper for the "quick" functions which attempts to read the line
2248 table for THIS_CU. */
2249
2250 static struct quick_file_names *
2251 dw2_get_file_names (struct objfile *objfile,
2252 struct dwarf2_per_cu_data *this_cu)
2253 {
2254 bfd *abfd = objfile->obfd;
2255 struct line_header *lh;
2256 struct attribute *attr;
2257 struct cleanup *cleanups;
2258 struct die_info *comp_unit_die;
2259 struct dwarf2_section_info* sec;
2260 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2261 int has_children, i;
2262 struct dwarf2_cu cu;
2263 unsigned int bytes_read, buffer_size;
2264 struct die_reader_specs reader_specs;
2265 char *name, *comp_dir;
2266 void **slot;
2267 struct quick_file_names *qfn;
2268 unsigned int line_offset;
2269
2270 if (this_cu->v.quick->file_names != NULL)
2271 return this_cu->v.quick->file_names;
2272 /* If we know there is no line data, no point in looking again. */
2273 if (this_cu->v.quick->no_file_data)
2274 return NULL;
2275
2276 init_one_comp_unit (&cu, objfile);
2277 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2278
2279 if (this_cu->debug_type_section)
2280 sec = this_cu->debug_type_section;
2281 else
2282 sec = &dwarf2_per_objfile->info;
2283 dwarf2_read_section (objfile, sec);
2284 buffer_size = sec->size;
2285 buffer = sec->buffer;
2286 info_ptr = buffer + this_cu->offset;
2287 beg_of_comp_unit = info_ptr;
2288
2289 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2290 buffer, buffer_size,
2291 abfd);
2292
2293 /* Complete the cu_header. */
2294 cu.header.offset = beg_of_comp_unit - buffer;
2295 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2296
2297 this_cu->cu = &cu;
2298 cu.per_cu = this_cu;
2299
2300 dwarf2_read_abbrevs (abfd, &cu);
2301 make_cleanup (dwarf2_free_abbrev_table, &cu);
2302
2303 if (this_cu->debug_type_section)
2304 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2305 init_cu_die_reader (&reader_specs, &cu);
2306 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2307 &has_children);
2308
2309 lh = NULL;
2310 slot = NULL;
2311 line_offset = 0;
2312 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2313 if (attr)
2314 {
2315 struct quick_file_names find_entry;
2316
2317 line_offset = DW_UNSND (attr);
2318
2319 /* We may have already read in this line header (TU line header sharing).
2320 If we have we're done. */
2321 find_entry.offset = line_offset;
2322 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2323 &find_entry, INSERT);
2324 if (*slot != NULL)
2325 {
2326 do_cleanups (cleanups);
2327 this_cu->v.quick->file_names = *slot;
2328 return *slot;
2329 }
2330
2331 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2332 }
2333 if (lh == NULL)
2334 {
2335 do_cleanups (cleanups);
2336 this_cu->v.quick->no_file_data = 1;
2337 return NULL;
2338 }
2339
2340 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2341 qfn->offset = line_offset;
2342 gdb_assert (slot != NULL);
2343 *slot = qfn;
2344
2345 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2346
2347 qfn->num_file_names = lh->num_file_names;
2348 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2349 lh->num_file_names * sizeof (char *));
2350 for (i = 0; i < lh->num_file_names; ++i)
2351 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2352 qfn->real_names = NULL;
2353
2354 free_line_header (lh);
2355 do_cleanups (cleanups);
2356
2357 this_cu->v.quick->file_names = qfn;
2358 return qfn;
2359 }
2360
2361 /* A helper for the "quick" functions which computes and caches the
2362 real path for a given file name from the line table. */
2363
2364 static const char *
2365 dw2_get_real_path (struct objfile *objfile,
2366 struct quick_file_names *qfn, int index)
2367 {
2368 if (qfn->real_names == NULL)
2369 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2370 qfn->num_file_names, sizeof (char *));
2371
2372 if (qfn->real_names[index] == NULL)
2373 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2374
2375 return qfn->real_names[index];
2376 }
2377
2378 static struct symtab *
2379 dw2_find_last_source_symtab (struct objfile *objfile)
2380 {
2381 int index;
2382
2383 dw2_setup (objfile);
2384 index = dwarf2_per_objfile->n_comp_units - 1;
2385 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2386 }
2387
2388 /* Traversal function for dw2_forget_cached_source_info. */
2389
2390 static int
2391 dw2_free_cached_file_names (void **slot, void *info)
2392 {
2393 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2394
2395 if (file_data->real_names)
2396 {
2397 int i;
2398
2399 for (i = 0; i < file_data->num_file_names; ++i)
2400 {
2401 xfree ((void*) file_data->real_names[i]);
2402 file_data->real_names[i] = NULL;
2403 }
2404 }
2405
2406 return 1;
2407 }
2408
2409 static void
2410 dw2_forget_cached_source_info (struct objfile *objfile)
2411 {
2412 dw2_setup (objfile);
2413
2414 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2415 dw2_free_cached_file_names, NULL);
2416 }
2417
2418 static int
2419 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2420 const char *full_path, const char *real_path,
2421 struct symtab **result)
2422 {
2423 int i;
2424 int check_basename = lbasename (name) == name;
2425 struct dwarf2_per_cu_data *base_cu = NULL;
2426
2427 dw2_setup (objfile);
2428
2429 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2430 + dwarf2_per_objfile->n_type_comp_units); ++i)
2431 {
2432 int j;
2433 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2434 struct quick_file_names *file_data;
2435
2436 if (per_cu->v.quick->symtab)
2437 continue;
2438
2439 file_data = dw2_get_file_names (objfile, per_cu);
2440 if (file_data == NULL)
2441 continue;
2442
2443 for (j = 0; j < file_data->num_file_names; ++j)
2444 {
2445 const char *this_name = file_data->file_names[j];
2446
2447 if (FILENAME_CMP (name, this_name) == 0)
2448 {
2449 *result = dw2_instantiate_symtab (objfile, per_cu);
2450 return 1;
2451 }
2452
2453 if (check_basename && ! base_cu
2454 && FILENAME_CMP (lbasename (this_name), name) == 0)
2455 base_cu = per_cu;
2456
2457 if (full_path != NULL)
2458 {
2459 const char *this_real_name = dw2_get_real_path (objfile,
2460 file_data, j);
2461
2462 if (this_real_name != NULL
2463 && FILENAME_CMP (full_path, this_real_name) == 0)
2464 {
2465 *result = dw2_instantiate_symtab (objfile, per_cu);
2466 return 1;
2467 }
2468 }
2469
2470 if (real_path != NULL)
2471 {
2472 const char *this_real_name = dw2_get_real_path (objfile,
2473 file_data, j);
2474
2475 if (this_real_name != NULL
2476 && FILENAME_CMP (real_path, this_real_name) == 0)
2477 {
2478 *result = dw2_instantiate_symtab (objfile, per_cu);
2479 return 1;
2480 }
2481 }
2482 }
2483 }
2484
2485 if (base_cu)
2486 {
2487 *result = dw2_instantiate_symtab (objfile, base_cu);
2488 return 1;
2489 }
2490
2491 return 0;
2492 }
2493
2494 static struct symtab *
2495 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2496 const char *name, domain_enum domain)
2497 {
2498 /* We do all the work in the pre_expand_symtabs_matching hook
2499 instead. */
2500 return NULL;
2501 }
2502
2503 /* A helper function that expands all symtabs that hold an object
2504 named NAME. */
2505
2506 static void
2507 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2508 {
2509 dw2_setup (objfile);
2510
2511 /* index_table is NULL if OBJF_READNOW. */
2512 if (dwarf2_per_objfile->index_table)
2513 {
2514 offset_type *vec;
2515
2516 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2517 name, &vec))
2518 {
2519 offset_type i, len = MAYBE_SWAP (*vec);
2520 for (i = 0; i < len; ++i)
2521 {
2522 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2523 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2524
2525 dw2_instantiate_symtab (objfile, per_cu);
2526 }
2527 }
2528 }
2529 }
2530
2531 static void
2532 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2533 enum block_enum block_kind, const char *name,
2534 domain_enum domain)
2535 {
2536 dw2_do_expand_symtabs_matching (objfile, name);
2537 }
2538
2539 static void
2540 dw2_print_stats (struct objfile *objfile)
2541 {
2542 int i, count;
2543
2544 dw2_setup (objfile);
2545 count = 0;
2546 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2547 + dwarf2_per_objfile->n_type_comp_units); ++i)
2548 {
2549 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2550
2551 if (!per_cu->v.quick->symtab)
2552 ++count;
2553 }
2554 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2555 }
2556
2557 static void
2558 dw2_dump (struct objfile *objfile)
2559 {
2560 /* Nothing worth printing. */
2561 }
2562
2563 static void
2564 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2565 struct section_offsets *delta)
2566 {
2567 /* There's nothing to relocate here. */
2568 }
2569
2570 static void
2571 dw2_expand_symtabs_for_function (struct objfile *objfile,
2572 const char *func_name)
2573 {
2574 dw2_do_expand_symtabs_matching (objfile, func_name);
2575 }
2576
2577 static void
2578 dw2_expand_all_symtabs (struct objfile *objfile)
2579 {
2580 int i;
2581
2582 dw2_setup (objfile);
2583
2584 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2585 + dwarf2_per_objfile->n_type_comp_units); ++i)
2586 {
2587 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2588
2589 dw2_instantiate_symtab (objfile, per_cu);
2590 }
2591 }
2592
2593 static void
2594 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2595 const char *filename)
2596 {
2597 int i;
2598
2599 dw2_setup (objfile);
2600
2601 /* We don't need to consider type units here.
2602 This is only called for examining code, e.g. expand_line_sal.
2603 There can be an order of magnitude (or more) more type units
2604 than comp units, and we avoid them if we can. */
2605
2606 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2607 {
2608 int j;
2609 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2610 struct quick_file_names *file_data;
2611
2612 if (per_cu->v.quick->symtab)
2613 continue;
2614
2615 file_data = dw2_get_file_names (objfile, per_cu);
2616 if (file_data == NULL)
2617 continue;
2618
2619 for (j = 0; j < file_data->num_file_names; ++j)
2620 {
2621 const char *this_name = file_data->file_names[j];
2622 if (FILENAME_CMP (this_name, filename) == 0)
2623 {
2624 dw2_instantiate_symtab (objfile, per_cu);
2625 break;
2626 }
2627 }
2628 }
2629 }
2630
2631 static const char *
2632 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2633 {
2634 struct dwarf2_per_cu_data *per_cu;
2635 offset_type *vec;
2636 struct quick_file_names *file_data;
2637
2638 dw2_setup (objfile);
2639
2640 /* index_table is NULL if OBJF_READNOW. */
2641 if (!dwarf2_per_objfile->index_table)
2642 return NULL;
2643
2644 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2645 name, &vec))
2646 return NULL;
2647
2648 /* Note that this just looks at the very first one named NAME -- but
2649 actually we are looking for a function. find_main_filename
2650 should be rewritten so that it doesn't require a custom hook. It
2651 could just use the ordinary symbol tables. */
2652 /* vec[0] is the length, which must always be >0. */
2653 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2654
2655 file_data = dw2_get_file_names (objfile, per_cu);
2656 if (file_data == NULL)
2657 return NULL;
2658
2659 return file_data->file_names[file_data->num_file_names - 1];
2660 }
2661
2662 static void
2663 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2664 struct objfile *objfile, int global,
2665 int (*callback) (struct block *,
2666 struct symbol *, void *),
2667 void *data, symbol_compare_ftype *match,
2668 symbol_compare_ftype *ordered_compare)
2669 {
2670 /* Currently unimplemented; used for Ada. The function can be called if the
2671 current language is Ada for a non-Ada objfile using GNU index. As Ada
2672 does not look for non-Ada symbols this function should just return. */
2673 }
2674
2675 static void
2676 dw2_expand_symtabs_matching (struct objfile *objfile,
2677 int (*file_matcher) (const char *, void *),
2678 int (*name_matcher) (const char *, void *),
2679 enum search_domain kind,
2680 void *data)
2681 {
2682 int i;
2683 offset_type iter;
2684 struct mapped_index *index;
2685
2686 dw2_setup (objfile);
2687
2688 /* index_table is NULL if OBJF_READNOW. */
2689 if (!dwarf2_per_objfile->index_table)
2690 return;
2691 index = dwarf2_per_objfile->index_table;
2692
2693 if (file_matcher != NULL)
2694 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2695 + dwarf2_per_objfile->n_type_comp_units); ++i)
2696 {
2697 int j;
2698 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2699 struct quick_file_names *file_data;
2700
2701 per_cu->v.quick->mark = 0;
2702 if (per_cu->v.quick->symtab)
2703 continue;
2704
2705 file_data = dw2_get_file_names (objfile, per_cu);
2706 if (file_data == NULL)
2707 continue;
2708
2709 for (j = 0; j < file_data->num_file_names; ++j)
2710 {
2711 if (file_matcher (file_data->file_names[j], data))
2712 {
2713 per_cu->v.quick->mark = 1;
2714 break;
2715 }
2716 }
2717 }
2718
2719 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2720 {
2721 offset_type idx = 2 * iter;
2722 const char *name;
2723 offset_type *vec, vec_len, vec_idx;
2724
2725 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2726 continue;
2727
2728 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2729
2730 if (! (*name_matcher) (name, data))
2731 continue;
2732
2733 /* The name was matched, now expand corresponding CUs that were
2734 marked. */
2735 vec = (offset_type *) (index->constant_pool
2736 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2737 vec_len = MAYBE_SWAP (vec[0]);
2738 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2739 {
2740 struct dwarf2_per_cu_data *per_cu;
2741
2742 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2743 if (file_matcher == NULL || per_cu->v.quick->mark)
2744 dw2_instantiate_symtab (objfile, per_cu);
2745 }
2746 }
2747 }
2748
2749 static struct symtab *
2750 dw2_find_pc_sect_symtab (struct objfile *objfile,
2751 struct minimal_symbol *msymbol,
2752 CORE_ADDR pc,
2753 struct obj_section *section,
2754 int warn_if_readin)
2755 {
2756 struct dwarf2_per_cu_data *data;
2757
2758 dw2_setup (objfile);
2759
2760 if (!objfile->psymtabs_addrmap)
2761 return NULL;
2762
2763 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2764 if (!data)
2765 return NULL;
2766
2767 if (warn_if_readin && data->v.quick->symtab)
2768 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2769 paddress (get_objfile_arch (objfile), pc));
2770
2771 return dw2_instantiate_symtab (objfile, data);
2772 }
2773
2774 static void
2775 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2776 void *data)
2777 {
2778 int i;
2779
2780 dw2_setup (objfile);
2781
2782 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2783 + dwarf2_per_objfile->n_type_comp_units); ++i)
2784 {
2785 int j;
2786 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2787 struct quick_file_names *file_data;
2788
2789 if (per_cu->v.quick->symtab)
2790 continue;
2791
2792 file_data = dw2_get_file_names (objfile, per_cu);
2793 if (file_data == NULL)
2794 continue;
2795
2796 for (j = 0; j < file_data->num_file_names; ++j)
2797 {
2798 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2799 j);
2800 (*fun) (file_data->file_names[j], this_real_name, data);
2801 }
2802 }
2803 }
2804
2805 static int
2806 dw2_has_symbols (struct objfile *objfile)
2807 {
2808 return 1;
2809 }
2810
2811 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2812 {
2813 dw2_has_symbols,
2814 dw2_find_last_source_symtab,
2815 dw2_forget_cached_source_info,
2816 dw2_lookup_symtab,
2817 dw2_lookup_symbol,
2818 dw2_pre_expand_symtabs_matching,
2819 dw2_print_stats,
2820 dw2_dump,
2821 dw2_relocate,
2822 dw2_expand_symtabs_for_function,
2823 dw2_expand_all_symtabs,
2824 dw2_expand_symtabs_with_filename,
2825 dw2_find_symbol_file,
2826 dw2_map_matching_symbols,
2827 dw2_expand_symtabs_matching,
2828 dw2_find_pc_sect_symtab,
2829 dw2_map_symbol_filenames
2830 };
2831
2832 /* Initialize for reading DWARF for this objfile. Return 0 if this
2833 file will use psymtabs, or 1 if using the GNU index. */
2834
2835 int
2836 dwarf2_initialize_objfile (struct objfile *objfile)
2837 {
2838 /* If we're about to read full symbols, don't bother with the
2839 indices. In this case we also don't care if some other debug
2840 format is making psymtabs, because they are all about to be
2841 expanded anyway. */
2842 if ((objfile->flags & OBJF_READNOW))
2843 {
2844 int i;
2845
2846 dwarf2_per_objfile->using_index = 1;
2847 create_all_comp_units (objfile);
2848 create_debug_types_hash_table (objfile);
2849 dwarf2_per_objfile->quick_file_names_table =
2850 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2851
2852 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2853 + dwarf2_per_objfile->n_type_comp_units); ++i)
2854 {
2855 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2856
2857 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2858 struct dwarf2_per_cu_quick_data);
2859 }
2860
2861 /* Return 1 so that gdb sees the "quick" functions. However,
2862 these functions will be no-ops because we will have expanded
2863 all symtabs. */
2864 return 1;
2865 }
2866
2867 if (dwarf2_read_index (objfile))
2868 return 1;
2869
2870 return 0;
2871 }
2872
2873 \f
2874
2875 /* Build a partial symbol table. */
2876
2877 void
2878 dwarf2_build_psymtabs (struct objfile *objfile)
2879 {
2880 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2881 {
2882 init_psymbol_list (objfile, 1024);
2883 }
2884
2885 dwarf2_build_psymtabs_hard (objfile);
2886 }
2887
2888 /* Return TRUE if OFFSET is within CU_HEADER. */
2889
2890 static inline int
2891 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2892 {
2893 unsigned int bottom = cu_header->offset;
2894 unsigned int top = (cu_header->offset
2895 + cu_header->length
2896 + cu_header->initial_length_size);
2897
2898 return (offset >= bottom && offset < top);
2899 }
2900
2901 /* Read in the comp unit header information from the debug_info at info_ptr.
2902 NOTE: This leaves members offset, first_die_offset to be filled in
2903 by the caller. */
2904
2905 static gdb_byte *
2906 read_comp_unit_head (struct comp_unit_head *cu_header,
2907 gdb_byte *info_ptr, bfd *abfd)
2908 {
2909 int signed_addr;
2910 unsigned int bytes_read;
2911
2912 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2913 cu_header->initial_length_size = bytes_read;
2914 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2915 info_ptr += bytes_read;
2916 cu_header->version = read_2_bytes (abfd, info_ptr);
2917 info_ptr += 2;
2918 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2919 &bytes_read);
2920 info_ptr += bytes_read;
2921 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2922 info_ptr += 1;
2923 signed_addr = bfd_get_sign_extend_vma (abfd);
2924 if (signed_addr < 0)
2925 internal_error (__FILE__, __LINE__,
2926 _("read_comp_unit_head: dwarf from non elf file"));
2927 cu_header->signed_addr_p = signed_addr;
2928
2929 return info_ptr;
2930 }
2931
2932 static gdb_byte *
2933 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2934 gdb_byte *buffer, unsigned int buffer_size,
2935 bfd *abfd)
2936 {
2937 gdb_byte *beg_of_comp_unit = info_ptr;
2938
2939 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2940
2941 if (header->version != 2 && header->version != 3 && header->version != 4)
2942 error (_("Dwarf Error: wrong version in compilation unit header "
2943 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2944 bfd_get_filename (abfd));
2945
2946 if (header->abbrev_offset
2947 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2948 &dwarf2_per_objfile->abbrev))
2949 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2950 "(offset 0x%lx + 6) [in module %s]"),
2951 (long) header->abbrev_offset,
2952 (long) (beg_of_comp_unit - buffer),
2953 bfd_get_filename (abfd));
2954
2955 if (beg_of_comp_unit + header->length + header->initial_length_size
2956 > buffer + buffer_size)
2957 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2958 "(offset 0x%lx + 0) [in module %s]"),
2959 (long) header->length,
2960 (long) (beg_of_comp_unit - buffer),
2961 bfd_get_filename (abfd));
2962
2963 return info_ptr;
2964 }
2965
2966 /* Read in the types comp unit header information from .debug_types entry at
2967 types_ptr. The result is a pointer to one past the end of the header. */
2968
2969 static gdb_byte *
2970 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2971 struct dwarf2_section_info *section,
2972 ULONGEST *signature,
2973 gdb_byte *types_ptr, bfd *abfd)
2974 {
2975 gdb_byte *initial_types_ptr = types_ptr;
2976
2977 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
2978 cu_header->offset = types_ptr - section->buffer;
2979
2980 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2981
2982 *signature = read_8_bytes (abfd, types_ptr);
2983 types_ptr += 8;
2984 types_ptr += cu_header->offset_size;
2985 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2986
2987 return types_ptr;
2988 }
2989
2990 /* Allocate a new partial symtab for file named NAME and mark this new
2991 partial symtab as being an include of PST. */
2992
2993 static void
2994 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2995 struct objfile *objfile)
2996 {
2997 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2998
2999 subpst->section_offsets = pst->section_offsets;
3000 subpst->textlow = 0;
3001 subpst->texthigh = 0;
3002
3003 subpst->dependencies = (struct partial_symtab **)
3004 obstack_alloc (&objfile->objfile_obstack,
3005 sizeof (struct partial_symtab *));
3006 subpst->dependencies[0] = pst;
3007 subpst->number_of_dependencies = 1;
3008
3009 subpst->globals_offset = 0;
3010 subpst->n_global_syms = 0;
3011 subpst->statics_offset = 0;
3012 subpst->n_static_syms = 0;
3013 subpst->symtab = NULL;
3014 subpst->read_symtab = pst->read_symtab;
3015 subpst->readin = 0;
3016
3017 /* No private part is necessary for include psymtabs. This property
3018 can be used to differentiate between such include psymtabs and
3019 the regular ones. */
3020 subpst->read_symtab_private = NULL;
3021 }
3022
3023 /* Read the Line Number Program data and extract the list of files
3024 included by the source file represented by PST. Build an include
3025 partial symtab for each of these included files. */
3026
3027 static void
3028 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3029 struct die_info *die,
3030 struct partial_symtab *pst)
3031 {
3032 struct objfile *objfile = cu->objfile;
3033 bfd *abfd = objfile->obfd;
3034 struct line_header *lh = NULL;
3035 struct attribute *attr;
3036
3037 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3038 if (attr)
3039 {
3040 unsigned int line_offset = DW_UNSND (attr);
3041
3042 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3043 }
3044 if (lh == NULL)
3045 return; /* No linetable, so no includes. */
3046
3047 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3048 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3049
3050 free_line_header (lh);
3051 }
3052
3053 static hashval_t
3054 hash_type_signature (const void *item)
3055 {
3056 const struct signatured_type *type_sig = item;
3057
3058 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3059 return type_sig->signature;
3060 }
3061
3062 static int
3063 eq_type_signature (const void *item_lhs, const void *item_rhs)
3064 {
3065 const struct signatured_type *lhs = item_lhs;
3066 const struct signatured_type *rhs = item_rhs;
3067
3068 return lhs->signature == rhs->signature;
3069 }
3070
3071 /* Allocate a hash table for signatured types. */
3072
3073 static htab_t
3074 allocate_signatured_type_table (struct objfile *objfile)
3075 {
3076 return htab_create_alloc_ex (41,
3077 hash_type_signature,
3078 eq_type_signature,
3079 NULL,
3080 &objfile->objfile_obstack,
3081 hashtab_obstack_allocate,
3082 dummy_obstack_deallocate);
3083 }
3084
3085 /* A helper function to add a signatured type CU to a list. */
3086
3087 static int
3088 add_signatured_type_cu_to_list (void **slot, void *datum)
3089 {
3090 struct signatured_type *sigt = *slot;
3091 struct dwarf2_per_cu_data ***datap = datum;
3092
3093 **datap = &sigt->per_cu;
3094 ++*datap;
3095
3096 return 1;
3097 }
3098
3099 /* Create the hash table of all entries in the .debug_types section.
3100 The result is zero if there is an error (e.g. missing .debug_types section),
3101 otherwise non-zero. */
3102
3103 static int
3104 create_debug_types_hash_table (struct objfile *objfile)
3105 {
3106 htab_t types_htab = NULL;
3107 struct dwarf2_per_cu_data **iter;
3108 int ix;
3109 struct dwarf2_section_info *section;
3110
3111 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3112 {
3113 dwarf2_per_objfile->signatured_types = NULL;
3114 return 0;
3115 }
3116
3117 for (ix = 0;
3118 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3119 ix, section);
3120 ++ix)
3121 {
3122 gdb_byte *info_ptr, *end_ptr;
3123
3124 dwarf2_read_section (objfile, section);
3125 info_ptr = section->buffer;
3126
3127 if (info_ptr == NULL)
3128 continue;
3129
3130 if (types_htab == NULL)
3131 types_htab = allocate_signatured_type_table (objfile);
3132
3133 if (dwarf2_die_debug)
3134 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3135
3136 end_ptr = info_ptr + section->size;
3137 while (info_ptr < end_ptr)
3138 {
3139 unsigned int offset;
3140 unsigned int offset_size;
3141 unsigned int type_offset;
3142 unsigned int length, initial_length_size;
3143 unsigned short version;
3144 ULONGEST signature;
3145 struct signatured_type *type_sig;
3146 void **slot;
3147 gdb_byte *ptr = info_ptr;
3148
3149 offset = ptr - section->buffer;
3150
3151 /* We need to read the type's signature in order to build the hash
3152 table, but we don't need to read anything else just yet. */
3153
3154 /* Sanity check to ensure entire cu is present. */
3155 length = read_initial_length (objfile->obfd, ptr,
3156 &initial_length_size);
3157 if (ptr + length + initial_length_size > end_ptr)
3158 {
3159 complaint (&symfile_complaints,
3160 _("debug type entry runs off end "
3161 "of `.debug_types' section, ignored"));
3162 break;
3163 }
3164
3165 offset_size = initial_length_size == 4 ? 4 : 8;
3166 ptr += initial_length_size;
3167 version = bfd_get_16 (objfile->obfd, ptr);
3168 ptr += 2;
3169 ptr += offset_size; /* abbrev offset */
3170 ptr += 1; /* address size */
3171 signature = bfd_get_64 (objfile->obfd, ptr);
3172 ptr += 8;
3173 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3174
3175 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3176 memset (type_sig, 0, sizeof (*type_sig));
3177 type_sig->signature = signature;
3178 type_sig->type_offset = type_offset;
3179 type_sig->per_cu.objfile = objfile;
3180 type_sig->per_cu.debug_type_section = section;
3181 type_sig->per_cu.offset = offset;
3182
3183 slot = htab_find_slot (types_htab, type_sig, INSERT);
3184 gdb_assert (slot != NULL);
3185 if (*slot != NULL)
3186 {
3187 const struct signatured_type *dup_sig = *slot;
3188
3189 complaint (&symfile_complaints,
3190 _("debug type entry at offset 0x%x is duplicate to the "
3191 "entry at offset 0x%x, signature 0x%s"),
3192 offset, dup_sig->per_cu.offset,
3193 phex (signature, sizeof (signature)));
3194 gdb_assert (signature == dup_sig->signature);
3195 }
3196 *slot = type_sig;
3197
3198 if (dwarf2_die_debug)
3199 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3200 offset, phex (signature, sizeof (signature)));
3201
3202 info_ptr = info_ptr + initial_length_size + length;
3203 }
3204 }
3205
3206 dwarf2_per_objfile->signatured_types = types_htab;
3207
3208 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3209 dwarf2_per_objfile->type_comp_units
3210 = obstack_alloc (&objfile->objfile_obstack,
3211 dwarf2_per_objfile->n_type_comp_units
3212 * sizeof (struct dwarf2_per_cu_data *));
3213 iter = &dwarf2_per_objfile->type_comp_units[0];
3214 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3215 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3216 == dwarf2_per_objfile->n_type_comp_units);
3217
3218 return 1;
3219 }
3220
3221 /* Lookup a signature based type.
3222 Returns NULL if SIG is not present in the table. */
3223
3224 static struct signatured_type *
3225 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3226 {
3227 struct signatured_type find_entry, *entry;
3228
3229 if (dwarf2_per_objfile->signatured_types == NULL)
3230 {
3231 complaint (&symfile_complaints,
3232 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3233 return 0;
3234 }
3235
3236 find_entry.signature = sig;
3237 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3238 return entry;
3239 }
3240
3241 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3242
3243 static void
3244 init_cu_die_reader (struct die_reader_specs *reader,
3245 struct dwarf2_cu *cu)
3246 {
3247 reader->abfd = cu->objfile->obfd;
3248 reader->cu = cu;
3249 if (cu->per_cu->debug_type_section)
3250 {
3251 gdb_assert (cu->per_cu->debug_type_section->readin);
3252 reader->buffer = cu->per_cu->debug_type_section->buffer;
3253 }
3254 else
3255 {
3256 gdb_assert (dwarf2_per_objfile->info.readin);
3257 reader->buffer = dwarf2_per_objfile->info.buffer;
3258 }
3259 }
3260
3261 /* Find the base address of the compilation unit for range lists and
3262 location lists. It will normally be specified by DW_AT_low_pc.
3263 In DWARF-3 draft 4, the base address could be overridden by
3264 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3265 compilation units with discontinuous ranges. */
3266
3267 static void
3268 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3269 {
3270 struct attribute *attr;
3271
3272 cu->base_known = 0;
3273 cu->base_address = 0;
3274
3275 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3276 if (attr)
3277 {
3278 cu->base_address = DW_ADDR (attr);
3279 cu->base_known = 1;
3280 }
3281 else
3282 {
3283 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3284 if (attr)
3285 {
3286 cu->base_address = DW_ADDR (attr);
3287 cu->base_known = 1;
3288 }
3289 }
3290 }
3291
3292 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3293 to combine the common parts.
3294 Process a compilation unit for a psymtab.
3295 BUFFER is a pointer to the beginning of the dwarf section buffer,
3296 either .debug_info or debug_types.
3297 INFO_PTR is a pointer to the start of the CU.
3298 Returns a pointer to the next CU. */
3299
3300 static gdb_byte *
3301 process_psymtab_comp_unit (struct objfile *objfile,
3302 struct dwarf2_per_cu_data *this_cu,
3303 gdb_byte *buffer, gdb_byte *info_ptr,
3304 unsigned int buffer_size)
3305 {
3306 bfd *abfd = objfile->obfd;
3307 gdb_byte *beg_of_comp_unit = info_ptr;
3308 struct die_info *comp_unit_die;
3309 struct partial_symtab *pst;
3310 CORE_ADDR baseaddr;
3311 struct cleanup *back_to_inner;
3312 struct dwarf2_cu cu;
3313 int has_children, has_pc_info;
3314 struct attribute *attr;
3315 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3316 struct die_reader_specs reader_specs;
3317 const char *filename;
3318
3319 init_one_comp_unit (&cu, objfile);
3320 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3321
3322 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3323 buffer, buffer_size,
3324 abfd);
3325
3326 /* Complete the cu_header. */
3327 cu.header.offset = beg_of_comp_unit - buffer;
3328 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3329
3330 cu.list_in_scope = &file_symbols;
3331
3332 /* If this compilation unit was already read in, free the
3333 cached copy in order to read it in again. This is
3334 necessary because we skipped some symbols when we first
3335 read in the compilation unit (see load_partial_dies).
3336 This problem could be avoided, but the benefit is
3337 unclear. */
3338 if (this_cu->cu != NULL)
3339 free_one_cached_comp_unit (this_cu->cu);
3340
3341 /* Note that this is a pointer to our stack frame, being
3342 added to a global data structure. It will be cleaned up
3343 in free_stack_comp_unit when we finish with this
3344 compilation unit. */
3345 this_cu->cu = &cu;
3346 cu.per_cu = this_cu;
3347
3348 /* Read the abbrevs for this compilation unit into a table. */
3349 dwarf2_read_abbrevs (abfd, &cu);
3350 make_cleanup (dwarf2_free_abbrev_table, &cu);
3351
3352 /* Read the compilation unit die. */
3353 if (this_cu->debug_type_section)
3354 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3355 init_cu_die_reader (&reader_specs, &cu);
3356 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3357 &has_children);
3358
3359 if (this_cu->debug_type_section)
3360 {
3361 /* LENGTH has not been set yet for type units. */
3362 gdb_assert (this_cu->offset == cu.header.offset);
3363 this_cu->length = cu.header.length + cu.header.initial_length_size;
3364 }
3365 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3366 {
3367 info_ptr = (beg_of_comp_unit + cu.header.length
3368 + cu.header.initial_length_size);
3369 do_cleanups (back_to_inner);
3370 return info_ptr;
3371 }
3372
3373 prepare_one_comp_unit (&cu, comp_unit_die);
3374
3375 /* Allocate a new partial symbol table structure. */
3376 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3377 if (attr == NULL || !DW_STRING (attr))
3378 filename = "";
3379 else
3380 filename = DW_STRING (attr);
3381 pst = start_psymtab_common (objfile, objfile->section_offsets,
3382 filename,
3383 /* TEXTLOW and TEXTHIGH are set below. */
3384 0,
3385 objfile->global_psymbols.next,
3386 objfile->static_psymbols.next);
3387
3388 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3389 if (attr != NULL)
3390 pst->dirname = DW_STRING (attr);
3391
3392 pst->read_symtab_private = this_cu;
3393
3394 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3395
3396 /* Store the function that reads in the rest of the symbol table. */
3397 pst->read_symtab = dwarf2_psymtab_to_symtab;
3398
3399 this_cu->v.psymtab = pst;
3400
3401 dwarf2_find_base_address (comp_unit_die, &cu);
3402
3403 /* Possibly set the default values of LOWPC and HIGHPC from
3404 `DW_AT_ranges'. */
3405 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3406 &best_highpc, &cu, pst);
3407 if (has_pc_info == 1 && best_lowpc < best_highpc)
3408 /* Store the contiguous range if it is not empty; it can be empty for
3409 CUs with no code. */
3410 addrmap_set_empty (objfile->psymtabs_addrmap,
3411 best_lowpc + baseaddr,
3412 best_highpc + baseaddr - 1, pst);
3413
3414 /* Check if comp unit has_children.
3415 If so, read the rest of the partial symbols from this comp unit.
3416 If not, there's no more debug_info for this comp unit. */
3417 if (has_children)
3418 {
3419 struct partial_die_info *first_die;
3420 CORE_ADDR lowpc, highpc;
3421
3422 lowpc = ((CORE_ADDR) -1);
3423 highpc = ((CORE_ADDR) 0);
3424
3425 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3426
3427 scan_partial_symbols (first_die, &lowpc, &highpc,
3428 ! has_pc_info, &cu);
3429
3430 /* If we didn't find a lowpc, set it to highpc to avoid
3431 complaints from `maint check'. */
3432 if (lowpc == ((CORE_ADDR) -1))
3433 lowpc = highpc;
3434
3435 /* If the compilation unit didn't have an explicit address range,
3436 then use the information extracted from its child dies. */
3437 if (! has_pc_info)
3438 {
3439 best_lowpc = lowpc;
3440 best_highpc = highpc;
3441 }
3442 }
3443 pst->textlow = best_lowpc + baseaddr;
3444 pst->texthigh = best_highpc + baseaddr;
3445
3446 pst->n_global_syms = objfile->global_psymbols.next -
3447 (objfile->global_psymbols.list + pst->globals_offset);
3448 pst->n_static_syms = objfile->static_psymbols.next -
3449 (objfile->static_psymbols.list + pst->statics_offset);
3450 sort_pst_symbols (pst);
3451
3452 info_ptr = (beg_of_comp_unit + cu.header.length
3453 + cu.header.initial_length_size);
3454
3455 if (this_cu->debug_type_section)
3456 {
3457 /* It's not clear we want to do anything with stmt lists here.
3458 Waiting to see what gcc ultimately does. */
3459 }
3460 else
3461 {
3462 /* Get the list of files included in the current compilation unit,
3463 and build a psymtab for each of them. */
3464 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3465 }
3466
3467 do_cleanups (back_to_inner);
3468
3469 return info_ptr;
3470 }
3471
3472 /* Traversal function for htab_traverse_noresize.
3473 Process one .debug_types comp-unit. */
3474
3475 static int
3476 process_type_comp_unit (void **slot, void *info)
3477 {
3478 struct signatured_type *entry = (struct signatured_type *) *slot;
3479 struct objfile *objfile = (struct objfile *) info;
3480 struct dwarf2_per_cu_data *this_cu;
3481
3482 this_cu = &entry->per_cu;
3483
3484 gdb_assert (this_cu->debug_type_section->readin);
3485 process_psymtab_comp_unit (objfile, this_cu,
3486 this_cu->debug_type_section->buffer,
3487 (this_cu->debug_type_section->buffer
3488 + this_cu->offset),
3489 this_cu->debug_type_section->size);
3490
3491 return 1;
3492 }
3493
3494 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3495 Build partial symbol tables for the .debug_types comp-units. */
3496
3497 static void
3498 build_type_psymtabs (struct objfile *objfile)
3499 {
3500 if (! create_debug_types_hash_table (objfile))
3501 return;
3502
3503 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3504 process_type_comp_unit, objfile);
3505 }
3506
3507 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3508
3509 static void
3510 psymtabs_addrmap_cleanup (void *o)
3511 {
3512 struct objfile *objfile = o;
3513
3514 objfile->psymtabs_addrmap = NULL;
3515 }
3516
3517 /* Build the partial symbol table by doing a quick pass through the
3518 .debug_info and .debug_abbrev sections. */
3519
3520 static void
3521 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3522 {
3523 gdb_byte *info_ptr;
3524 struct cleanup *back_to, *addrmap_cleanup;
3525 struct obstack temp_obstack;
3526
3527 dwarf2_per_objfile->reading_partial_symbols = 1;
3528
3529 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3530 info_ptr = dwarf2_per_objfile->info.buffer;
3531
3532 /* Any cached compilation units will be linked by the per-objfile
3533 read_in_chain. Make sure to free them when we're done. */
3534 back_to = make_cleanup (free_cached_comp_units, NULL);
3535
3536 build_type_psymtabs (objfile);
3537
3538 create_all_comp_units (objfile);
3539
3540 /* Create a temporary address map on a temporary obstack. We later
3541 copy this to the final obstack. */
3542 obstack_init (&temp_obstack);
3543 make_cleanup_obstack_free (&temp_obstack);
3544 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3545 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3546
3547 /* Since the objects we're extracting from .debug_info vary in
3548 length, only the individual functions to extract them (like
3549 read_comp_unit_head and load_partial_die) can really know whether
3550 the buffer is large enough to hold another complete object.
3551
3552 At the moment, they don't actually check that. If .debug_info
3553 holds just one extra byte after the last compilation unit's dies,
3554 then read_comp_unit_head will happily read off the end of the
3555 buffer. read_partial_die is similarly casual. Those functions
3556 should be fixed.
3557
3558 For this loop condition, simply checking whether there's any data
3559 left at all should be sufficient. */
3560
3561 while (info_ptr < (dwarf2_per_objfile->info.buffer
3562 + dwarf2_per_objfile->info.size))
3563 {
3564 struct dwarf2_per_cu_data *this_cu;
3565
3566 this_cu = dwarf2_find_comp_unit (info_ptr
3567 - dwarf2_per_objfile->info.buffer,
3568 objfile);
3569
3570 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3571 dwarf2_per_objfile->info.buffer,
3572 info_ptr,
3573 dwarf2_per_objfile->info.size);
3574 }
3575
3576 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3577 &objfile->objfile_obstack);
3578 discard_cleanups (addrmap_cleanup);
3579
3580 do_cleanups (back_to);
3581 }
3582
3583 /* Load the partial DIEs for a secondary CU into memory. */
3584
3585 static void
3586 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3587 struct objfile *objfile)
3588 {
3589 bfd *abfd = objfile->obfd;
3590 gdb_byte *info_ptr, *beg_of_comp_unit;
3591 struct die_info *comp_unit_die;
3592 struct dwarf2_cu *cu;
3593 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3594 int has_children;
3595 struct die_reader_specs reader_specs;
3596 int read_cu = 0;
3597
3598 gdb_assert (! this_cu->debug_type_section);
3599
3600 gdb_assert (dwarf2_per_objfile->info.readin);
3601 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3602 beg_of_comp_unit = info_ptr;
3603
3604 if (this_cu->cu == NULL)
3605 {
3606 cu = xmalloc (sizeof (*cu));
3607 init_one_comp_unit (cu, objfile);
3608
3609 read_cu = 1;
3610
3611 /* If an error occurs while loading, release our storage. */
3612 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3613
3614 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3615 dwarf2_per_objfile->info.buffer,
3616 dwarf2_per_objfile->info.size,
3617 abfd);
3618
3619 /* Complete the cu_header. */
3620 cu->header.offset = this_cu->offset;
3621 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3622
3623 /* Link this compilation unit into the compilation unit tree. */
3624 this_cu->cu = cu;
3625 cu->per_cu = this_cu;
3626
3627 /* Link this CU into read_in_chain. */
3628 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3629 dwarf2_per_objfile->read_in_chain = this_cu;
3630 }
3631 else
3632 {
3633 cu = this_cu->cu;
3634 info_ptr += cu->header.first_die_offset;
3635 }
3636
3637 /* Read the abbrevs for this compilation unit into a table. */
3638 gdb_assert (cu->dwarf2_abbrevs == NULL);
3639 dwarf2_read_abbrevs (abfd, cu);
3640 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3641
3642 /* Read the compilation unit die. */
3643 init_cu_die_reader (&reader_specs, cu);
3644 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3645 &has_children);
3646
3647 prepare_one_comp_unit (cu, comp_unit_die);
3648
3649 /* Check if comp unit has_children.
3650 If so, read the rest of the partial symbols from this comp unit.
3651 If not, there's no more debug_info for this comp unit. */
3652 if (has_children)
3653 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3654
3655 do_cleanups (free_abbrevs_cleanup);
3656
3657 if (read_cu)
3658 {
3659 /* We've successfully allocated this compilation unit. Let our
3660 caller clean it up when finished with it. */
3661 discard_cleanups (free_cu_cleanup);
3662 }
3663 }
3664
3665 /* Create a list of all compilation units in OBJFILE. We do this only
3666 if an inter-comp-unit reference is found; presumably if there is one,
3667 there will be many, and one will occur early in the .debug_info section.
3668 So there's no point in building this list incrementally. */
3669
3670 static void
3671 create_all_comp_units (struct objfile *objfile)
3672 {
3673 int n_allocated;
3674 int n_comp_units;
3675 struct dwarf2_per_cu_data **all_comp_units;
3676 gdb_byte *info_ptr;
3677
3678 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3679 info_ptr = dwarf2_per_objfile->info.buffer;
3680
3681 n_comp_units = 0;
3682 n_allocated = 10;
3683 all_comp_units = xmalloc (n_allocated
3684 * sizeof (struct dwarf2_per_cu_data *));
3685
3686 while (info_ptr < dwarf2_per_objfile->info.buffer
3687 + dwarf2_per_objfile->info.size)
3688 {
3689 unsigned int length, initial_length_size;
3690 struct dwarf2_per_cu_data *this_cu;
3691 unsigned int offset;
3692
3693 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3694
3695 /* Read just enough information to find out where the next
3696 compilation unit is. */
3697 length = read_initial_length (objfile->obfd, info_ptr,
3698 &initial_length_size);
3699
3700 /* Save the compilation unit for later lookup. */
3701 this_cu = obstack_alloc (&objfile->objfile_obstack,
3702 sizeof (struct dwarf2_per_cu_data));
3703 memset (this_cu, 0, sizeof (*this_cu));
3704 this_cu->offset = offset;
3705 this_cu->length = length + initial_length_size;
3706 this_cu->objfile = objfile;
3707
3708 if (n_comp_units == n_allocated)
3709 {
3710 n_allocated *= 2;
3711 all_comp_units = xrealloc (all_comp_units,
3712 n_allocated
3713 * sizeof (struct dwarf2_per_cu_data *));
3714 }
3715 all_comp_units[n_comp_units++] = this_cu;
3716
3717 info_ptr = info_ptr + this_cu->length;
3718 }
3719
3720 dwarf2_per_objfile->all_comp_units
3721 = obstack_alloc (&objfile->objfile_obstack,
3722 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3723 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3724 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3725 xfree (all_comp_units);
3726 dwarf2_per_objfile->n_comp_units = n_comp_units;
3727 }
3728
3729 /* Process all loaded DIEs for compilation unit CU, starting at
3730 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3731 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3732 DW_AT_ranges). If NEED_PC is set, then this function will set
3733 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3734 and record the covered ranges in the addrmap. */
3735
3736 static void
3737 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3738 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3739 {
3740 struct partial_die_info *pdi;
3741
3742 /* Now, march along the PDI's, descending into ones which have
3743 interesting children but skipping the children of the other ones,
3744 until we reach the end of the compilation unit. */
3745
3746 pdi = first_die;
3747
3748 while (pdi != NULL)
3749 {
3750 fixup_partial_die (pdi, cu);
3751
3752 /* Anonymous namespaces or modules have no name but have interesting
3753 children, so we need to look at them. Ditto for anonymous
3754 enums. */
3755
3756 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3757 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3758 {
3759 switch (pdi->tag)
3760 {
3761 case DW_TAG_subprogram:
3762 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3763 break;
3764 case DW_TAG_constant:
3765 case DW_TAG_variable:
3766 case DW_TAG_typedef:
3767 case DW_TAG_union_type:
3768 if (!pdi->is_declaration)
3769 {
3770 add_partial_symbol (pdi, cu);
3771 }
3772 break;
3773 case DW_TAG_class_type:
3774 case DW_TAG_interface_type:
3775 case DW_TAG_structure_type:
3776 if (!pdi->is_declaration)
3777 {
3778 add_partial_symbol (pdi, cu);
3779 }
3780 break;
3781 case DW_TAG_enumeration_type:
3782 if (!pdi->is_declaration)
3783 add_partial_enumeration (pdi, cu);
3784 break;
3785 case DW_TAG_base_type:
3786 case DW_TAG_subrange_type:
3787 /* File scope base type definitions are added to the partial
3788 symbol table. */
3789 add_partial_symbol (pdi, cu);
3790 break;
3791 case DW_TAG_namespace:
3792 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3793 break;
3794 case DW_TAG_module:
3795 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3796 break;
3797 default:
3798 break;
3799 }
3800 }
3801
3802 /* If the die has a sibling, skip to the sibling. */
3803
3804 pdi = pdi->die_sibling;
3805 }
3806 }
3807
3808 /* Functions used to compute the fully scoped name of a partial DIE.
3809
3810 Normally, this is simple. For C++, the parent DIE's fully scoped
3811 name is concatenated with "::" and the partial DIE's name. For
3812 Java, the same thing occurs except that "." is used instead of "::".
3813 Enumerators are an exception; they use the scope of their parent
3814 enumeration type, i.e. the name of the enumeration type is not
3815 prepended to the enumerator.
3816
3817 There are two complexities. One is DW_AT_specification; in this
3818 case "parent" means the parent of the target of the specification,
3819 instead of the direct parent of the DIE. The other is compilers
3820 which do not emit DW_TAG_namespace; in this case we try to guess
3821 the fully qualified name of structure types from their members'
3822 linkage names. This must be done using the DIE's children rather
3823 than the children of any DW_AT_specification target. We only need
3824 to do this for structures at the top level, i.e. if the target of
3825 any DW_AT_specification (if any; otherwise the DIE itself) does not
3826 have a parent. */
3827
3828 /* Compute the scope prefix associated with PDI's parent, in
3829 compilation unit CU. The result will be allocated on CU's
3830 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3831 field. NULL is returned if no prefix is necessary. */
3832 static char *
3833 partial_die_parent_scope (struct partial_die_info *pdi,
3834 struct dwarf2_cu *cu)
3835 {
3836 char *grandparent_scope;
3837 struct partial_die_info *parent, *real_pdi;
3838
3839 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3840 then this means the parent of the specification DIE. */
3841
3842 real_pdi = pdi;
3843 while (real_pdi->has_specification)
3844 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3845
3846 parent = real_pdi->die_parent;
3847 if (parent == NULL)
3848 return NULL;
3849
3850 if (parent->scope_set)
3851 return parent->scope;
3852
3853 fixup_partial_die (parent, cu);
3854
3855 grandparent_scope = partial_die_parent_scope (parent, cu);
3856
3857 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3858 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3859 Work around this problem here. */
3860 if (cu->language == language_cplus
3861 && parent->tag == DW_TAG_namespace
3862 && strcmp (parent->name, "::") == 0
3863 && grandparent_scope == NULL)
3864 {
3865 parent->scope = NULL;
3866 parent->scope_set = 1;
3867 return NULL;
3868 }
3869
3870 if (parent->tag == DW_TAG_namespace
3871 || parent->tag == DW_TAG_module
3872 || parent->tag == DW_TAG_structure_type
3873 || parent->tag == DW_TAG_class_type
3874 || parent->tag == DW_TAG_interface_type
3875 || parent->tag == DW_TAG_union_type
3876 || parent->tag == DW_TAG_enumeration_type)
3877 {
3878 if (grandparent_scope == NULL)
3879 parent->scope = parent->name;
3880 else
3881 parent->scope = typename_concat (&cu->comp_unit_obstack,
3882 grandparent_scope,
3883 parent->name, 0, cu);
3884 }
3885 else if (parent->tag == DW_TAG_enumerator)
3886 /* Enumerators should not get the name of the enumeration as a prefix. */
3887 parent->scope = grandparent_scope;
3888 else
3889 {
3890 /* FIXME drow/2004-04-01: What should we be doing with
3891 function-local names? For partial symbols, we should probably be
3892 ignoring them. */
3893 complaint (&symfile_complaints,
3894 _("unhandled containing DIE tag %d for DIE at %d"),
3895 parent->tag, pdi->offset);
3896 parent->scope = grandparent_scope;
3897 }
3898
3899 parent->scope_set = 1;
3900 return parent->scope;
3901 }
3902
3903 /* Return the fully scoped name associated with PDI, from compilation unit
3904 CU. The result will be allocated with malloc. */
3905 static char *
3906 partial_die_full_name (struct partial_die_info *pdi,
3907 struct dwarf2_cu *cu)
3908 {
3909 char *parent_scope;
3910
3911 /* If this is a template instantiation, we can not work out the
3912 template arguments from partial DIEs. So, unfortunately, we have
3913 to go through the full DIEs. At least any work we do building
3914 types here will be reused if full symbols are loaded later. */
3915 if (pdi->has_template_arguments)
3916 {
3917 fixup_partial_die (pdi, cu);
3918
3919 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3920 {
3921 struct die_info *die;
3922 struct attribute attr;
3923 struct dwarf2_cu *ref_cu = cu;
3924
3925 attr.name = 0;
3926 attr.form = DW_FORM_ref_addr;
3927 attr.u.addr = pdi->offset;
3928 die = follow_die_ref (NULL, &attr, &ref_cu);
3929
3930 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3931 }
3932 }
3933
3934 parent_scope = partial_die_parent_scope (pdi, cu);
3935 if (parent_scope == NULL)
3936 return NULL;
3937 else
3938 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3939 }
3940
3941 static void
3942 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3943 {
3944 struct objfile *objfile = cu->objfile;
3945 CORE_ADDR addr = 0;
3946 char *actual_name = NULL;
3947 const struct partial_symbol *psym = NULL;
3948 CORE_ADDR baseaddr;
3949 int built_actual_name = 0;
3950
3951 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3952
3953 actual_name = partial_die_full_name (pdi, cu);
3954 if (actual_name)
3955 built_actual_name = 1;
3956
3957 if (actual_name == NULL)
3958 actual_name = pdi->name;
3959
3960 switch (pdi->tag)
3961 {
3962 case DW_TAG_subprogram:
3963 if (pdi->is_external || cu->language == language_ada)
3964 {
3965 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3966 of the global scope. But in Ada, we want to be able to access
3967 nested procedures globally. So all Ada subprograms are stored
3968 in the global scope. */
3969 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3970 mst_text, objfile); */
3971 add_psymbol_to_list (actual_name, strlen (actual_name),
3972 built_actual_name,
3973 VAR_DOMAIN, LOC_BLOCK,
3974 &objfile->global_psymbols,
3975 0, pdi->lowpc + baseaddr,
3976 cu->language, objfile);
3977 }
3978 else
3979 {
3980 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3981 mst_file_text, objfile); */
3982 add_psymbol_to_list (actual_name, strlen (actual_name),
3983 built_actual_name,
3984 VAR_DOMAIN, LOC_BLOCK,
3985 &objfile->static_psymbols,
3986 0, pdi->lowpc + baseaddr,
3987 cu->language, objfile);
3988 }
3989 break;
3990 case DW_TAG_constant:
3991 {
3992 struct psymbol_allocation_list *list;
3993
3994 if (pdi->is_external)
3995 list = &objfile->global_psymbols;
3996 else
3997 list = &objfile->static_psymbols;
3998 add_psymbol_to_list (actual_name, strlen (actual_name),
3999 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4000 list, 0, 0, cu->language, objfile);
4001 }
4002 break;
4003 case DW_TAG_variable:
4004 if (pdi->locdesc)
4005 addr = decode_locdesc (pdi->locdesc, cu);
4006
4007 if (pdi->locdesc
4008 && addr == 0
4009 && !dwarf2_per_objfile->has_section_at_zero)
4010 {
4011 /* A global or static variable may also have been stripped
4012 out by the linker if unused, in which case its address
4013 will be nullified; do not add such variables into partial
4014 symbol table then. */
4015 }
4016 else if (pdi->is_external)
4017 {
4018 /* Global Variable.
4019 Don't enter into the minimal symbol tables as there is
4020 a minimal symbol table entry from the ELF symbols already.
4021 Enter into partial symbol table if it has a location
4022 descriptor or a type.
4023 If the location descriptor is missing, new_symbol will create
4024 a LOC_UNRESOLVED symbol, the address of the variable will then
4025 be determined from the minimal symbol table whenever the variable
4026 is referenced.
4027 The address for the partial symbol table entry is not
4028 used by GDB, but it comes in handy for debugging partial symbol
4029 table building. */
4030
4031 if (pdi->locdesc || pdi->has_type)
4032 add_psymbol_to_list (actual_name, strlen (actual_name),
4033 built_actual_name,
4034 VAR_DOMAIN, LOC_STATIC,
4035 &objfile->global_psymbols,
4036 0, addr + baseaddr,
4037 cu->language, objfile);
4038 }
4039 else
4040 {
4041 /* Static Variable. Skip symbols without location descriptors. */
4042 if (pdi->locdesc == NULL)
4043 {
4044 if (built_actual_name)
4045 xfree (actual_name);
4046 return;
4047 }
4048 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4049 mst_file_data, objfile); */
4050 add_psymbol_to_list (actual_name, strlen (actual_name),
4051 built_actual_name,
4052 VAR_DOMAIN, LOC_STATIC,
4053 &objfile->static_psymbols,
4054 0, addr + baseaddr,
4055 cu->language, objfile);
4056 }
4057 break;
4058 case DW_TAG_typedef:
4059 case DW_TAG_base_type:
4060 case DW_TAG_subrange_type:
4061 add_psymbol_to_list (actual_name, strlen (actual_name),
4062 built_actual_name,
4063 VAR_DOMAIN, LOC_TYPEDEF,
4064 &objfile->static_psymbols,
4065 0, (CORE_ADDR) 0, cu->language, objfile);
4066 break;
4067 case DW_TAG_namespace:
4068 add_psymbol_to_list (actual_name, strlen (actual_name),
4069 built_actual_name,
4070 VAR_DOMAIN, LOC_TYPEDEF,
4071 &objfile->global_psymbols,
4072 0, (CORE_ADDR) 0, cu->language, objfile);
4073 break;
4074 case DW_TAG_class_type:
4075 case DW_TAG_interface_type:
4076 case DW_TAG_structure_type:
4077 case DW_TAG_union_type:
4078 case DW_TAG_enumeration_type:
4079 /* Skip external references. The DWARF standard says in the section
4080 about "Structure, Union, and Class Type Entries": "An incomplete
4081 structure, union or class type is represented by a structure,
4082 union or class entry that does not have a byte size attribute
4083 and that has a DW_AT_declaration attribute." */
4084 if (!pdi->has_byte_size && pdi->is_declaration)
4085 {
4086 if (built_actual_name)
4087 xfree (actual_name);
4088 return;
4089 }
4090
4091 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4092 static vs. global. */
4093 add_psymbol_to_list (actual_name, strlen (actual_name),
4094 built_actual_name,
4095 STRUCT_DOMAIN, LOC_TYPEDEF,
4096 (cu->language == language_cplus
4097 || cu->language == language_java)
4098 ? &objfile->global_psymbols
4099 : &objfile->static_psymbols,
4100 0, (CORE_ADDR) 0, cu->language, objfile);
4101
4102 break;
4103 case DW_TAG_enumerator:
4104 add_psymbol_to_list (actual_name, strlen (actual_name),
4105 built_actual_name,
4106 VAR_DOMAIN, LOC_CONST,
4107 (cu->language == language_cplus
4108 || cu->language == language_java)
4109 ? &objfile->global_psymbols
4110 : &objfile->static_psymbols,
4111 0, (CORE_ADDR) 0, cu->language, objfile);
4112 break;
4113 default:
4114 break;
4115 }
4116
4117 if (built_actual_name)
4118 xfree (actual_name);
4119 }
4120
4121 /* Read a partial die corresponding to a namespace; also, add a symbol
4122 corresponding to that namespace to the symbol table. NAMESPACE is
4123 the name of the enclosing namespace. */
4124
4125 static void
4126 add_partial_namespace (struct partial_die_info *pdi,
4127 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4128 int need_pc, struct dwarf2_cu *cu)
4129 {
4130 /* Add a symbol for the namespace. */
4131
4132 add_partial_symbol (pdi, cu);
4133
4134 /* Now scan partial symbols in that namespace. */
4135
4136 if (pdi->has_children)
4137 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4138 }
4139
4140 /* Read a partial die corresponding to a Fortran module. */
4141
4142 static void
4143 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4144 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4145 {
4146 /* Now scan partial symbols in that module. */
4147
4148 if (pdi->has_children)
4149 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4150 }
4151
4152 /* Read a partial die corresponding to a subprogram and create a partial
4153 symbol for that subprogram. When the CU language allows it, this
4154 routine also defines a partial symbol for each nested subprogram
4155 that this subprogram contains.
4156
4157 DIE my also be a lexical block, in which case we simply search
4158 recursively for suprograms defined inside that lexical block.
4159 Again, this is only performed when the CU language allows this
4160 type of definitions. */
4161
4162 static void
4163 add_partial_subprogram (struct partial_die_info *pdi,
4164 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4165 int need_pc, struct dwarf2_cu *cu)
4166 {
4167 if (pdi->tag == DW_TAG_subprogram)
4168 {
4169 if (pdi->has_pc_info)
4170 {
4171 if (pdi->lowpc < *lowpc)
4172 *lowpc = pdi->lowpc;
4173 if (pdi->highpc > *highpc)
4174 *highpc = pdi->highpc;
4175 if (need_pc)
4176 {
4177 CORE_ADDR baseaddr;
4178 struct objfile *objfile = cu->objfile;
4179
4180 baseaddr = ANOFFSET (objfile->section_offsets,
4181 SECT_OFF_TEXT (objfile));
4182 addrmap_set_empty (objfile->psymtabs_addrmap,
4183 pdi->lowpc + baseaddr,
4184 pdi->highpc - 1 + baseaddr,
4185 cu->per_cu->v.psymtab);
4186 }
4187 if (!pdi->is_declaration)
4188 /* Ignore subprogram DIEs that do not have a name, they are
4189 illegal. Do not emit a complaint at this point, we will
4190 do so when we convert this psymtab into a symtab. */
4191 if (pdi->name)
4192 add_partial_symbol (pdi, cu);
4193 }
4194 }
4195
4196 if (! pdi->has_children)
4197 return;
4198
4199 if (cu->language == language_ada)
4200 {
4201 pdi = pdi->die_child;
4202 while (pdi != NULL)
4203 {
4204 fixup_partial_die (pdi, cu);
4205 if (pdi->tag == DW_TAG_subprogram
4206 || pdi->tag == DW_TAG_lexical_block)
4207 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4208 pdi = pdi->die_sibling;
4209 }
4210 }
4211 }
4212
4213 /* Read a partial die corresponding to an enumeration type. */
4214
4215 static void
4216 add_partial_enumeration (struct partial_die_info *enum_pdi,
4217 struct dwarf2_cu *cu)
4218 {
4219 struct partial_die_info *pdi;
4220
4221 if (enum_pdi->name != NULL)
4222 add_partial_symbol (enum_pdi, cu);
4223
4224 pdi = enum_pdi->die_child;
4225 while (pdi)
4226 {
4227 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4228 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4229 else
4230 add_partial_symbol (pdi, cu);
4231 pdi = pdi->die_sibling;
4232 }
4233 }
4234
4235 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4236 Return the corresponding abbrev, or NULL if the number is zero (indicating
4237 an empty DIE). In either case *BYTES_READ will be set to the length of
4238 the initial number. */
4239
4240 static struct abbrev_info *
4241 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4242 struct dwarf2_cu *cu)
4243 {
4244 bfd *abfd = cu->objfile->obfd;
4245 unsigned int abbrev_number;
4246 struct abbrev_info *abbrev;
4247
4248 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4249
4250 if (abbrev_number == 0)
4251 return NULL;
4252
4253 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4254 if (!abbrev)
4255 {
4256 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4257 abbrev_number, bfd_get_filename (abfd));
4258 }
4259
4260 return abbrev;
4261 }
4262
4263 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4264 Returns a pointer to the end of a series of DIEs, terminated by an empty
4265 DIE. Any children of the skipped DIEs will also be skipped. */
4266
4267 static gdb_byte *
4268 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4269 {
4270 struct abbrev_info *abbrev;
4271 unsigned int bytes_read;
4272
4273 while (1)
4274 {
4275 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4276 if (abbrev == NULL)
4277 return info_ptr + bytes_read;
4278 else
4279 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4280 }
4281 }
4282
4283 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4284 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4285 abbrev corresponding to that skipped uleb128 should be passed in
4286 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4287 children. */
4288
4289 static gdb_byte *
4290 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4291 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4292 {
4293 unsigned int bytes_read;
4294 struct attribute attr;
4295 bfd *abfd = cu->objfile->obfd;
4296 unsigned int form, i;
4297
4298 for (i = 0; i < abbrev->num_attrs; i++)
4299 {
4300 /* The only abbrev we care about is DW_AT_sibling. */
4301 if (abbrev->attrs[i].name == DW_AT_sibling)
4302 {
4303 read_attribute (&attr, &abbrev->attrs[i],
4304 abfd, info_ptr, cu);
4305 if (attr.form == DW_FORM_ref_addr)
4306 complaint (&symfile_complaints,
4307 _("ignoring absolute DW_AT_sibling"));
4308 else
4309 return buffer + dwarf2_get_ref_die_offset (&attr);
4310 }
4311
4312 /* If it isn't DW_AT_sibling, skip this attribute. */
4313 form = abbrev->attrs[i].form;
4314 skip_attribute:
4315 switch (form)
4316 {
4317 case DW_FORM_ref_addr:
4318 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4319 and later it is offset sized. */
4320 if (cu->header.version == 2)
4321 info_ptr += cu->header.addr_size;
4322 else
4323 info_ptr += cu->header.offset_size;
4324 break;
4325 case DW_FORM_addr:
4326 info_ptr += cu->header.addr_size;
4327 break;
4328 case DW_FORM_data1:
4329 case DW_FORM_ref1:
4330 case DW_FORM_flag:
4331 info_ptr += 1;
4332 break;
4333 case DW_FORM_flag_present:
4334 break;
4335 case DW_FORM_data2:
4336 case DW_FORM_ref2:
4337 info_ptr += 2;
4338 break;
4339 case DW_FORM_data4:
4340 case DW_FORM_ref4:
4341 info_ptr += 4;
4342 break;
4343 case DW_FORM_data8:
4344 case DW_FORM_ref8:
4345 case DW_FORM_ref_sig8:
4346 info_ptr += 8;
4347 break;
4348 case DW_FORM_string:
4349 read_direct_string (abfd, info_ptr, &bytes_read);
4350 info_ptr += bytes_read;
4351 break;
4352 case DW_FORM_sec_offset:
4353 case DW_FORM_strp:
4354 info_ptr += cu->header.offset_size;
4355 break;
4356 case DW_FORM_exprloc:
4357 case DW_FORM_block:
4358 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4359 info_ptr += bytes_read;
4360 break;
4361 case DW_FORM_block1:
4362 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4363 break;
4364 case DW_FORM_block2:
4365 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4366 break;
4367 case DW_FORM_block4:
4368 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4369 break;
4370 case DW_FORM_sdata:
4371 case DW_FORM_udata:
4372 case DW_FORM_ref_udata:
4373 info_ptr = skip_leb128 (abfd, info_ptr);
4374 break;
4375 case DW_FORM_indirect:
4376 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4377 info_ptr += bytes_read;
4378 /* We need to continue parsing from here, so just go back to
4379 the top. */
4380 goto skip_attribute;
4381
4382 default:
4383 error (_("Dwarf Error: Cannot handle %s "
4384 "in DWARF reader [in module %s]"),
4385 dwarf_form_name (form),
4386 bfd_get_filename (abfd));
4387 }
4388 }
4389
4390 if (abbrev->has_children)
4391 return skip_children (buffer, info_ptr, cu);
4392 else
4393 return info_ptr;
4394 }
4395
4396 /* Locate ORIG_PDI's sibling.
4397 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4398 in BUFFER. */
4399
4400 static gdb_byte *
4401 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4402 gdb_byte *buffer, gdb_byte *info_ptr,
4403 bfd *abfd, struct dwarf2_cu *cu)
4404 {
4405 /* Do we know the sibling already? */
4406
4407 if (orig_pdi->sibling)
4408 return orig_pdi->sibling;
4409
4410 /* Are there any children to deal with? */
4411
4412 if (!orig_pdi->has_children)
4413 return info_ptr;
4414
4415 /* Skip the children the long way. */
4416
4417 return skip_children (buffer, info_ptr, cu);
4418 }
4419
4420 /* Expand this partial symbol table into a full symbol table. */
4421
4422 static void
4423 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4424 {
4425 if (pst != NULL)
4426 {
4427 if (pst->readin)
4428 {
4429 warning (_("bug: psymtab for %s is already read in."),
4430 pst->filename);
4431 }
4432 else
4433 {
4434 if (info_verbose)
4435 {
4436 printf_filtered (_("Reading in symbols for %s..."),
4437 pst->filename);
4438 gdb_flush (gdb_stdout);
4439 }
4440
4441 /* Restore our global data. */
4442 dwarf2_per_objfile = objfile_data (pst->objfile,
4443 dwarf2_objfile_data_key);
4444
4445 /* If this psymtab is constructed from a debug-only objfile, the
4446 has_section_at_zero flag will not necessarily be correct. We
4447 can get the correct value for this flag by looking at the data
4448 associated with the (presumably stripped) associated objfile. */
4449 if (pst->objfile->separate_debug_objfile_backlink)
4450 {
4451 struct dwarf2_per_objfile *dpo_backlink
4452 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4453 dwarf2_objfile_data_key);
4454
4455 dwarf2_per_objfile->has_section_at_zero
4456 = dpo_backlink->has_section_at_zero;
4457 }
4458
4459 dwarf2_per_objfile->reading_partial_symbols = 0;
4460
4461 psymtab_to_symtab_1 (pst);
4462
4463 /* Finish up the debug error message. */
4464 if (info_verbose)
4465 printf_filtered (_("done.\n"));
4466 }
4467 }
4468 }
4469
4470 /* Add PER_CU to the queue. */
4471
4472 static void
4473 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4474 {
4475 struct dwarf2_queue_item *item;
4476
4477 per_cu->queued = 1;
4478 item = xmalloc (sizeof (*item));
4479 item->per_cu = per_cu;
4480 item->next = NULL;
4481
4482 if (dwarf2_queue == NULL)
4483 dwarf2_queue = item;
4484 else
4485 dwarf2_queue_tail->next = item;
4486
4487 dwarf2_queue_tail = item;
4488 }
4489
4490 /* Process the queue. */
4491
4492 static void
4493 process_queue (struct objfile *objfile)
4494 {
4495 struct dwarf2_queue_item *item, *next_item;
4496
4497 /* The queue starts out with one item, but following a DIE reference
4498 may load a new CU, adding it to the end of the queue. */
4499 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4500 {
4501 if (dwarf2_per_objfile->using_index
4502 ? !item->per_cu->v.quick->symtab
4503 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4504 process_full_comp_unit (item->per_cu);
4505
4506 item->per_cu->queued = 0;
4507 next_item = item->next;
4508 xfree (item);
4509 }
4510
4511 dwarf2_queue_tail = NULL;
4512 }
4513
4514 /* Free all allocated queue entries. This function only releases anything if
4515 an error was thrown; if the queue was processed then it would have been
4516 freed as we went along. */
4517
4518 static void
4519 dwarf2_release_queue (void *dummy)
4520 {
4521 struct dwarf2_queue_item *item, *last;
4522
4523 item = dwarf2_queue;
4524 while (item)
4525 {
4526 /* Anything still marked queued is likely to be in an
4527 inconsistent state, so discard it. */
4528 if (item->per_cu->queued)
4529 {
4530 if (item->per_cu->cu != NULL)
4531 free_one_cached_comp_unit (item->per_cu->cu);
4532 item->per_cu->queued = 0;
4533 }
4534
4535 last = item;
4536 item = item->next;
4537 xfree (last);
4538 }
4539
4540 dwarf2_queue = dwarf2_queue_tail = NULL;
4541 }
4542
4543 /* Read in full symbols for PST, and anything it depends on. */
4544
4545 static void
4546 psymtab_to_symtab_1 (struct partial_symtab *pst)
4547 {
4548 struct dwarf2_per_cu_data *per_cu;
4549 struct cleanup *back_to;
4550 int i;
4551
4552 for (i = 0; i < pst->number_of_dependencies; i++)
4553 if (!pst->dependencies[i]->readin)
4554 {
4555 /* Inform about additional files that need to be read in. */
4556 if (info_verbose)
4557 {
4558 /* FIXME: i18n: Need to make this a single string. */
4559 fputs_filtered (" ", gdb_stdout);
4560 wrap_here ("");
4561 fputs_filtered ("and ", gdb_stdout);
4562 wrap_here ("");
4563 printf_filtered ("%s...", pst->dependencies[i]->filename);
4564 wrap_here (""); /* Flush output. */
4565 gdb_flush (gdb_stdout);
4566 }
4567 psymtab_to_symtab_1 (pst->dependencies[i]);
4568 }
4569
4570 per_cu = pst->read_symtab_private;
4571
4572 if (per_cu == NULL)
4573 {
4574 /* It's an include file, no symbols to read for it.
4575 Everything is in the parent symtab. */
4576 pst->readin = 1;
4577 return;
4578 }
4579
4580 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4581 }
4582
4583 /* Load the DIEs associated with PER_CU into memory. */
4584
4585 static void
4586 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4587 struct objfile *objfile)
4588 {
4589 bfd *abfd = objfile->obfd;
4590 struct dwarf2_cu *cu;
4591 unsigned int offset;
4592 gdb_byte *info_ptr, *beg_of_comp_unit;
4593 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4594 struct attribute *attr;
4595 int read_cu = 0;
4596
4597 gdb_assert (! per_cu->debug_type_section);
4598
4599 /* Set local variables from the partial symbol table info. */
4600 offset = per_cu->offset;
4601
4602 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4603 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4604 beg_of_comp_unit = info_ptr;
4605
4606 if (per_cu->cu == NULL)
4607 {
4608 cu = xmalloc (sizeof (*cu));
4609 init_one_comp_unit (cu, objfile);
4610
4611 read_cu = 1;
4612
4613 /* If an error occurs while loading, release our storage. */
4614 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4615
4616 /* Read in the comp_unit header. */
4617 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4618
4619 /* Complete the cu_header. */
4620 cu->header.offset = offset;
4621 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4622
4623 /* Read the abbrevs for this compilation unit. */
4624 dwarf2_read_abbrevs (abfd, cu);
4625 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4626
4627 /* Link this compilation unit into the compilation unit tree. */
4628 per_cu->cu = cu;
4629 cu->per_cu = per_cu;
4630
4631 /* Link this CU into read_in_chain. */
4632 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4633 dwarf2_per_objfile->read_in_chain = per_cu;
4634 }
4635 else
4636 {
4637 cu = per_cu->cu;
4638 info_ptr += cu->header.first_die_offset;
4639 }
4640
4641 cu->dies = read_comp_unit (info_ptr, cu);
4642
4643 /* We try not to read any attributes in this function, because not
4644 all objfiles needed for references have been loaded yet, and symbol
4645 table processing isn't initialized. But we have to set the CU language,
4646 or we won't be able to build types correctly. */
4647 prepare_one_comp_unit (cu, cu->dies);
4648
4649 /* Similarly, if we do not read the producer, we can not apply
4650 producer-specific interpretation. */
4651 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4652 if (attr)
4653 cu->producer = DW_STRING (attr);
4654
4655 if (read_cu)
4656 {
4657 do_cleanups (free_abbrevs_cleanup);
4658
4659 /* We've successfully allocated this compilation unit. Let our
4660 caller clean it up when finished with it. */
4661 discard_cleanups (free_cu_cleanup);
4662 }
4663 }
4664
4665 /* Add a DIE to the delayed physname list. */
4666
4667 static void
4668 add_to_method_list (struct type *type, int fnfield_index, int index,
4669 const char *name, struct die_info *die,
4670 struct dwarf2_cu *cu)
4671 {
4672 struct delayed_method_info mi;
4673 mi.type = type;
4674 mi.fnfield_index = fnfield_index;
4675 mi.index = index;
4676 mi.name = name;
4677 mi.die = die;
4678 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4679 }
4680
4681 /* A cleanup for freeing the delayed method list. */
4682
4683 static void
4684 free_delayed_list (void *ptr)
4685 {
4686 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4687 if (cu->method_list != NULL)
4688 {
4689 VEC_free (delayed_method_info, cu->method_list);
4690 cu->method_list = NULL;
4691 }
4692 }
4693
4694 /* Compute the physnames of any methods on the CU's method list.
4695
4696 The computation of method physnames is delayed in order to avoid the
4697 (bad) condition that one of the method's formal parameters is of an as yet
4698 incomplete type. */
4699
4700 static void
4701 compute_delayed_physnames (struct dwarf2_cu *cu)
4702 {
4703 int i;
4704 struct delayed_method_info *mi;
4705 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4706 {
4707 const char *physname;
4708 struct fn_fieldlist *fn_flp
4709 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4710 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4711 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4712 }
4713 }
4714
4715 /* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4716 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4717 higher return INT_MAX. */
4718
4719 static int
4720 producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
4721 {
4722 const char *cs;
4723 int major, minor;
4724
4725 if (cu->producer == NULL)
4726 {
4727 /* For unknown compilers expect their behavior is not compliant. For GCC
4728 this case can also happen for -gdwarf-4 type units supported since
4729 gcc-4.5. */
4730
4731 return -1;
4732 }
4733
4734 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4735
4736 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4737 {
4738 /* For non-GCC compilers expect their behavior is not compliant. */
4739
4740 return -1;
4741 }
4742 cs = &cu->producer[strlen ("GNU ")];
4743 while (*cs && !isdigit (*cs))
4744 cs++;
4745 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4746 {
4747 /* Not recognized as GCC. */
4748
4749 return -1;
4750 }
4751
4752 if (major < 4)
4753 return -1;
4754 if (major > 4)
4755 return INT_MAX;
4756 return minor;
4757 }
4758
4759 /* Generate full symbol information for PST and CU, whose DIEs have
4760 already been loaded into memory. */
4761
4762 static void
4763 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4764 {
4765 struct dwarf2_cu *cu = per_cu->cu;
4766 struct objfile *objfile = per_cu->objfile;
4767 CORE_ADDR lowpc, highpc;
4768 struct symtab *symtab;
4769 struct cleanup *back_to, *delayed_list_cleanup;
4770 CORE_ADDR baseaddr;
4771
4772 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4773
4774 buildsym_init ();
4775 back_to = make_cleanup (really_free_pendings, NULL);
4776 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4777
4778 cu->list_in_scope = &file_symbols;
4779
4780 /* Do line number decoding in read_file_scope () */
4781 process_die (cu->dies, cu);
4782
4783 /* Now that we have processed all the DIEs in the CU, all the types
4784 should be complete, and it should now be safe to compute all of the
4785 physnames. */
4786 compute_delayed_physnames (cu);
4787 do_cleanups (delayed_list_cleanup);
4788
4789 /* Some compilers don't define a DW_AT_high_pc attribute for the
4790 compilation unit. If the DW_AT_high_pc is missing, synthesize
4791 it, by scanning the DIE's below the compilation unit. */
4792 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4793
4794 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4795
4796 if (symtab != NULL)
4797 {
4798 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4799
4800 /* Set symtab language to language from DW_AT_language. If the
4801 compilation is from a C file generated by language preprocessors, do
4802 not set the language if it was already deduced by start_subfile. */
4803 if (!(cu->language == language_c && symtab->language != language_c))
4804 symtab->language = cu->language;
4805
4806 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4807 produce DW_AT_location with location lists but it can be possibly
4808 invalid without -fvar-tracking.
4809
4810 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4811 needed, it would be wrong due to missing DW_AT_producer there.
4812
4813 Still one can confuse GDB by using non-standard GCC compilation
4814 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4815 */
4816 if (cu->has_loclist && gcc_4_minor >= 0)
4817 symtab->locations_valid = 1;
4818
4819 if (gcc_4_minor >= 5)
4820 symtab->epilogue_unwind_valid = 1;
4821 }
4822
4823 if (dwarf2_per_objfile->using_index)
4824 per_cu->v.quick->symtab = symtab;
4825 else
4826 {
4827 struct partial_symtab *pst = per_cu->v.psymtab;
4828 pst->symtab = symtab;
4829 pst->readin = 1;
4830 }
4831
4832 do_cleanups (back_to);
4833 }
4834
4835 /* Process a die and its children. */
4836
4837 static void
4838 process_die (struct die_info *die, struct dwarf2_cu *cu)
4839 {
4840 switch (die->tag)
4841 {
4842 case DW_TAG_padding:
4843 break;
4844 case DW_TAG_compile_unit:
4845 read_file_scope (die, cu);
4846 break;
4847 case DW_TAG_type_unit:
4848 read_type_unit_scope (die, cu);
4849 break;
4850 case DW_TAG_subprogram:
4851 case DW_TAG_inlined_subroutine:
4852 read_func_scope (die, cu);
4853 break;
4854 case DW_TAG_lexical_block:
4855 case DW_TAG_try_block:
4856 case DW_TAG_catch_block:
4857 read_lexical_block_scope (die, cu);
4858 break;
4859 case DW_TAG_class_type:
4860 case DW_TAG_interface_type:
4861 case DW_TAG_structure_type:
4862 case DW_TAG_union_type:
4863 process_structure_scope (die, cu);
4864 break;
4865 case DW_TAG_enumeration_type:
4866 process_enumeration_scope (die, cu);
4867 break;
4868
4869 /* These dies have a type, but processing them does not create
4870 a symbol or recurse to process the children. Therefore we can
4871 read them on-demand through read_type_die. */
4872 case DW_TAG_subroutine_type:
4873 case DW_TAG_set_type:
4874 case DW_TAG_array_type:
4875 case DW_TAG_pointer_type:
4876 case DW_TAG_ptr_to_member_type:
4877 case DW_TAG_reference_type:
4878 case DW_TAG_string_type:
4879 break;
4880
4881 case DW_TAG_base_type:
4882 case DW_TAG_subrange_type:
4883 case DW_TAG_typedef:
4884 /* Add a typedef symbol for the type definition, if it has a
4885 DW_AT_name. */
4886 new_symbol (die, read_type_die (die, cu), cu);
4887 break;
4888 case DW_TAG_common_block:
4889 read_common_block (die, cu);
4890 break;
4891 case DW_TAG_common_inclusion:
4892 break;
4893 case DW_TAG_namespace:
4894 processing_has_namespace_info = 1;
4895 read_namespace (die, cu);
4896 break;
4897 case DW_TAG_module:
4898 processing_has_namespace_info = 1;
4899 read_module (die, cu);
4900 break;
4901 case DW_TAG_imported_declaration:
4902 case DW_TAG_imported_module:
4903 processing_has_namespace_info = 1;
4904 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4905 || cu->language != language_fortran))
4906 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4907 dwarf_tag_name (die->tag));
4908 read_import_statement (die, cu);
4909 break;
4910 default:
4911 new_symbol (die, NULL, cu);
4912 break;
4913 }
4914 }
4915
4916 /* A helper function for dwarf2_compute_name which determines whether DIE
4917 needs to have the name of the scope prepended to the name listed in the
4918 die. */
4919
4920 static int
4921 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4922 {
4923 struct attribute *attr;
4924
4925 switch (die->tag)
4926 {
4927 case DW_TAG_namespace:
4928 case DW_TAG_typedef:
4929 case DW_TAG_class_type:
4930 case DW_TAG_interface_type:
4931 case DW_TAG_structure_type:
4932 case DW_TAG_union_type:
4933 case DW_TAG_enumeration_type:
4934 case DW_TAG_enumerator:
4935 case DW_TAG_subprogram:
4936 case DW_TAG_member:
4937 return 1;
4938
4939 case DW_TAG_variable:
4940 case DW_TAG_constant:
4941 /* We only need to prefix "globally" visible variables. These include
4942 any variable marked with DW_AT_external or any variable that
4943 lives in a namespace. [Variables in anonymous namespaces
4944 require prefixing, but they are not DW_AT_external.] */
4945
4946 if (dwarf2_attr (die, DW_AT_specification, cu))
4947 {
4948 struct dwarf2_cu *spec_cu = cu;
4949
4950 return die_needs_namespace (die_specification (die, &spec_cu),
4951 spec_cu);
4952 }
4953
4954 attr = dwarf2_attr (die, DW_AT_external, cu);
4955 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4956 && die->parent->tag != DW_TAG_module)
4957 return 0;
4958 /* A variable in a lexical block of some kind does not need a
4959 namespace, even though in C++ such variables may be external
4960 and have a mangled name. */
4961 if (die->parent->tag == DW_TAG_lexical_block
4962 || die->parent->tag == DW_TAG_try_block
4963 || die->parent->tag == DW_TAG_catch_block
4964 || die->parent->tag == DW_TAG_subprogram)
4965 return 0;
4966 return 1;
4967
4968 default:
4969 return 0;
4970 }
4971 }
4972
4973 /* Retrieve the last character from a mem_file. */
4974
4975 static void
4976 do_ui_file_peek_last (void *object, const char *buffer, long length)
4977 {
4978 char *last_char_p = (char *) object;
4979
4980 if (length > 0)
4981 *last_char_p = buffer[length - 1];
4982 }
4983
4984 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4985 compute the physname for the object, which include a method's
4986 formal parameters (C++/Java) and return type (Java).
4987
4988 For Ada, return the DIE's linkage name rather than the fully qualified
4989 name. PHYSNAME is ignored..
4990
4991 The result is allocated on the objfile_obstack and canonicalized. */
4992
4993 static const char *
4994 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4995 int physname)
4996 {
4997 if (name == NULL)
4998 name = dwarf2_name (die, cu);
4999
5000 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5001 compute it by typename_concat inside GDB. */
5002 if (cu->language == language_ada
5003 || (cu->language == language_fortran && physname))
5004 {
5005 /* For Ada unit, we prefer the linkage name over the name, as
5006 the former contains the exported name, which the user expects
5007 to be able to reference. Ideally, we want the user to be able
5008 to reference this entity using either natural or linkage name,
5009 but we haven't started looking at this enhancement yet. */
5010 struct attribute *attr;
5011
5012 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5013 if (attr == NULL)
5014 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5015 if (attr && DW_STRING (attr))
5016 return DW_STRING (attr);
5017 }
5018
5019 /* These are the only languages we know how to qualify names in. */
5020 if (name != NULL
5021 && (cu->language == language_cplus || cu->language == language_java
5022 || cu->language == language_fortran))
5023 {
5024 if (die_needs_namespace (die, cu))
5025 {
5026 long length;
5027 char *prefix;
5028 struct ui_file *buf;
5029
5030 prefix = determine_prefix (die, cu);
5031 buf = mem_fileopen ();
5032 if (*prefix != '\0')
5033 {
5034 char *prefixed_name = typename_concat (NULL, prefix, name,
5035 physname, cu);
5036
5037 fputs_unfiltered (prefixed_name, buf);
5038 xfree (prefixed_name);
5039 }
5040 else
5041 fputs_unfiltered (name, buf);
5042
5043 /* Template parameters may be specified in the DIE's DW_AT_name, or
5044 as children with DW_TAG_template_type_param or
5045 DW_TAG_value_type_param. If the latter, add them to the name
5046 here. If the name already has template parameters, then
5047 skip this step; some versions of GCC emit both, and
5048 it is more efficient to use the pre-computed name.
5049
5050 Something to keep in mind about this process: it is very
5051 unlikely, or in some cases downright impossible, to produce
5052 something that will match the mangled name of a function.
5053 If the definition of the function has the same debug info,
5054 we should be able to match up with it anyway. But fallbacks
5055 using the minimal symbol, for instance to find a method
5056 implemented in a stripped copy of libstdc++, will not work.
5057 If we do not have debug info for the definition, we will have to
5058 match them up some other way.
5059
5060 When we do name matching there is a related problem with function
5061 templates; two instantiated function templates are allowed to
5062 differ only by their return types, which we do not add here. */
5063
5064 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5065 {
5066 struct attribute *attr;
5067 struct die_info *child;
5068 int first = 1;
5069
5070 die->building_fullname = 1;
5071
5072 for (child = die->child; child != NULL; child = child->sibling)
5073 {
5074 struct type *type;
5075 long value;
5076 gdb_byte *bytes;
5077 struct dwarf2_locexpr_baton *baton;
5078 struct value *v;
5079
5080 if (child->tag != DW_TAG_template_type_param
5081 && child->tag != DW_TAG_template_value_param)
5082 continue;
5083
5084 if (first)
5085 {
5086 fputs_unfiltered ("<", buf);
5087 first = 0;
5088 }
5089 else
5090 fputs_unfiltered (", ", buf);
5091
5092 attr = dwarf2_attr (child, DW_AT_type, cu);
5093 if (attr == NULL)
5094 {
5095 complaint (&symfile_complaints,
5096 _("template parameter missing DW_AT_type"));
5097 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5098 continue;
5099 }
5100 type = die_type (child, cu);
5101
5102 if (child->tag == DW_TAG_template_type_param)
5103 {
5104 c_print_type (type, "", buf, -1, 0);
5105 continue;
5106 }
5107
5108 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5109 if (attr == NULL)
5110 {
5111 complaint (&symfile_complaints,
5112 _("template parameter missing "
5113 "DW_AT_const_value"));
5114 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5115 continue;
5116 }
5117
5118 dwarf2_const_value_attr (attr, type, name,
5119 &cu->comp_unit_obstack, cu,
5120 &value, &bytes, &baton);
5121
5122 if (TYPE_NOSIGN (type))
5123 /* GDB prints characters as NUMBER 'CHAR'. If that's
5124 changed, this can use value_print instead. */
5125 c_printchar (value, type, buf);
5126 else
5127 {
5128 struct value_print_options opts;
5129
5130 if (baton != NULL)
5131 v = dwarf2_evaluate_loc_desc (type, NULL,
5132 baton->data,
5133 baton->size,
5134 baton->per_cu);
5135 else if (bytes != NULL)
5136 {
5137 v = allocate_value (type);
5138 memcpy (value_contents_writeable (v), bytes,
5139 TYPE_LENGTH (type));
5140 }
5141 else
5142 v = value_from_longest (type, value);
5143
5144 /* Specify decimal so that we do not depend on
5145 the radix. */
5146 get_formatted_print_options (&opts, 'd');
5147 opts.raw = 1;
5148 value_print (v, buf, &opts);
5149 release_value (v);
5150 value_free (v);
5151 }
5152 }
5153
5154 die->building_fullname = 0;
5155
5156 if (!first)
5157 {
5158 /* Close the argument list, with a space if necessary
5159 (nested templates). */
5160 char last_char = '\0';
5161 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5162 if (last_char == '>')
5163 fputs_unfiltered (" >", buf);
5164 else
5165 fputs_unfiltered (">", buf);
5166 }
5167 }
5168
5169 /* For Java and C++ methods, append formal parameter type
5170 information, if PHYSNAME. */
5171
5172 if (physname && die->tag == DW_TAG_subprogram
5173 && (cu->language == language_cplus
5174 || cu->language == language_java))
5175 {
5176 struct type *type = read_type_die (die, cu);
5177
5178 c_type_print_args (type, buf, 1, cu->language);
5179
5180 if (cu->language == language_java)
5181 {
5182 /* For java, we must append the return type to method
5183 names. */
5184 if (die->tag == DW_TAG_subprogram)
5185 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5186 0, 0);
5187 }
5188 else if (cu->language == language_cplus)
5189 {
5190 /* Assume that an artificial first parameter is
5191 "this", but do not crash if it is not. RealView
5192 marks unnamed (and thus unused) parameters as
5193 artificial; there is no way to differentiate
5194 the two cases. */
5195 if (TYPE_NFIELDS (type) > 0
5196 && TYPE_FIELD_ARTIFICIAL (type, 0)
5197 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5198 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5199 0))))
5200 fputs_unfiltered (" const", buf);
5201 }
5202 }
5203
5204 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5205 &length);
5206 ui_file_delete (buf);
5207
5208 if (cu->language == language_cplus)
5209 {
5210 char *cname
5211 = dwarf2_canonicalize_name (name, cu,
5212 &cu->objfile->objfile_obstack);
5213
5214 if (cname != NULL)
5215 name = cname;
5216 }
5217 }
5218 }
5219
5220 return name;
5221 }
5222
5223 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5224 If scope qualifiers are appropriate they will be added. The result
5225 will be allocated on the objfile_obstack, or NULL if the DIE does
5226 not have a name. NAME may either be from a previous call to
5227 dwarf2_name or NULL.
5228
5229 The output string will be canonicalized (if C++/Java). */
5230
5231 static const char *
5232 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5233 {
5234 return dwarf2_compute_name (name, die, cu, 0);
5235 }
5236
5237 /* Construct a physname for the given DIE in CU. NAME may either be
5238 from a previous call to dwarf2_name or NULL. The result will be
5239 allocated on the objfile_objstack or NULL if the DIE does not have a
5240 name.
5241
5242 The output string will be canonicalized (if C++/Java). */
5243
5244 static const char *
5245 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5246 {
5247 struct attribute *attr;
5248 const char *retval, *mangled = NULL, *canon = NULL;
5249 struct cleanup *back_to;
5250 int need_copy = 1;
5251
5252 /* In this case dwarf2_compute_name is just a shortcut not building anything
5253 on its own. */
5254 if (!die_needs_namespace (die, cu))
5255 return dwarf2_compute_name (name, die, cu, 1);
5256
5257 back_to = make_cleanup (null_cleanup, NULL);
5258
5259 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5260 if (!attr)
5261 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5262
5263 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5264 has computed. */
5265 if (attr && DW_STRING (attr))
5266 {
5267 char *demangled;
5268
5269 mangled = DW_STRING (attr);
5270
5271 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5272 type. It is easier for GDB users to search for such functions as
5273 `name(params)' than `long name(params)'. In such case the minimal
5274 symbol names do not match the full symbol names but for template
5275 functions there is never a need to look up their definition from their
5276 declaration so the only disadvantage remains the minimal symbol
5277 variant `long name(params)' does not have the proper inferior type.
5278 */
5279
5280 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5281 | (cu->language == language_java
5282 ? DMGL_JAVA | DMGL_RET_POSTFIX
5283 : DMGL_RET_DROP)));
5284 if (demangled)
5285 {
5286 make_cleanup (xfree, demangled);
5287 canon = demangled;
5288 }
5289 else
5290 {
5291 canon = mangled;
5292 need_copy = 0;
5293 }
5294 }
5295
5296 if (canon == NULL || check_physname)
5297 {
5298 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5299
5300 if (canon != NULL && strcmp (physname, canon) != 0)
5301 {
5302 /* It may not mean a bug in GDB. The compiler could also
5303 compute DW_AT_linkage_name incorrectly. But in such case
5304 GDB would need to be bug-to-bug compatible. */
5305
5306 complaint (&symfile_complaints,
5307 _("Computed physname <%s> does not match demangled <%s> "
5308 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5309 physname, canon, mangled, die->offset, cu->objfile->name);
5310
5311 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5312 is available here - over computed PHYSNAME. It is safer
5313 against both buggy GDB and buggy compilers. */
5314
5315 retval = canon;
5316 }
5317 else
5318 {
5319 retval = physname;
5320 need_copy = 0;
5321 }
5322 }
5323 else
5324 retval = canon;
5325
5326 if (need_copy)
5327 retval = obsavestring (retval, strlen (retval),
5328 &cu->objfile->objfile_obstack);
5329
5330 do_cleanups (back_to);
5331 return retval;
5332 }
5333
5334 /* Read the import statement specified by the given die and record it. */
5335
5336 static void
5337 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5338 {
5339 struct attribute *import_attr;
5340 struct die_info *imported_die, *child_die;
5341 struct dwarf2_cu *imported_cu;
5342 const char *imported_name;
5343 const char *imported_name_prefix;
5344 const char *canonical_name;
5345 const char *import_alias;
5346 const char *imported_declaration = NULL;
5347 const char *import_prefix;
5348 VEC (const_char_ptr) *excludes = NULL;
5349 struct cleanup *cleanups;
5350
5351 char *temp;
5352
5353 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5354 if (import_attr == NULL)
5355 {
5356 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5357 dwarf_tag_name (die->tag));
5358 return;
5359 }
5360
5361 imported_cu = cu;
5362 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5363 imported_name = dwarf2_name (imported_die, imported_cu);
5364 if (imported_name == NULL)
5365 {
5366 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5367
5368 The import in the following code:
5369 namespace A
5370 {
5371 typedef int B;
5372 }
5373
5374 int main ()
5375 {
5376 using A::B;
5377 B b;
5378 return b;
5379 }
5380
5381 ...
5382 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5383 <52> DW_AT_decl_file : 1
5384 <53> DW_AT_decl_line : 6
5385 <54> DW_AT_import : <0x75>
5386 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5387 <59> DW_AT_name : B
5388 <5b> DW_AT_decl_file : 1
5389 <5c> DW_AT_decl_line : 2
5390 <5d> DW_AT_type : <0x6e>
5391 ...
5392 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5393 <76> DW_AT_byte_size : 4
5394 <77> DW_AT_encoding : 5 (signed)
5395
5396 imports the wrong die ( 0x75 instead of 0x58 ).
5397 This case will be ignored until the gcc bug is fixed. */
5398 return;
5399 }
5400
5401 /* Figure out the local name after import. */
5402 import_alias = dwarf2_name (die, cu);
5403
5404 /* Figure out where the statement is being imported to. */
5405 import_prefix = determine_prefix (die, cu);
5406
5407 /* Figure out what the scope of the imported die is and prepend it
5408 to the name of the imported die. */
5409 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5410
5411 if (imported_die->tag != DW_TAG_namespace
5412 && imported_die->tag != DW_TAG_module)
5413 {
5414 imported_declaration = imported_name;
5415 canonical_name = imported_name_prefix;
5416 }
5417 else if (strlen (imported_name_prefix) > 0)
5418 {
5419 temp = alloca (strlen (imported_name_prefix)
5420 + 2 + strlen (imported_name) + 1);
5421 strcpy (temp, imported_name_prefix);
5422 strcat (temp, "::");
5423 strcat (temp, imported_name);
5424 canonical_name = temp;
5425 }
5426 else
5427 canonical_name = imported_name;
5428
5429 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5430
5431 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5432 for (child_die = die->child; child_die && child_die->tag;
5433 child_die = sibling_die (child_die))
5434 {
5435 /* DWARF-4: A Fortran use statement with a “rename list” may be
5436 represented by an imported module entry with an import attribute
5437 referring to the module and owned entries corresponding to those
5438 entities that are renamed as part of being imported. */
5439
5440 if (child_die->tag != DW_TAG_imported_declaration)
5441 {
5442 complaint (&symfile_complaints,
5443 _("child DW_TAG_imported_declaration expected "
5444 "- DIE at 0x%x [in module %s]"),
5445 child_die->offset, cu->objfile->name);
5446 continue;
5447 }
5448
5449 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5450 if (import_attr == NULL)
5451 {
5452 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5453 dwarf_tag_name (child_die->tag));
5454 continue;
5455 }
5456
5457 imported_cu = cu;
5458 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5459 &imported_cu);
5460 imported_name = dwarf2_name (imported_die, imported_cu);
5461 if (imported_name == NULL)
5462 {
5463 complaint (&symfile_complaints,
5464 _("child DW_TAG_imported_declaration has unknown "
5465 "imported name - DIE at 0x%x [in module %s]"),
5466 child_die->offset, cu->objfile->name);
5467 continue;
5468 }
5469
5470 VEC_safe_push (const_char_ptr, excludes, imported_name);
5471
5472 process_die (child_die, cu);
5473 }
5474
5475 cp_add_using_directive (import_prefix,
5476 canonical_name,
5477 import_alias,
5478 imported_declaration,
5479 excludes,
5480 &cu->objfile->objfile_obstack);
5481
5482 do_cleanups (cleanups);
5483 }
5484
5485 static void
5486 initialize_cu_func_list (struct dwarf2_cu *cu)
5487 {
5488 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5489 }
5490
5491 /* Cleanup function for read_file_scope. */
5492
5493 static void
5494 free_cu_line_header (void *arg)
5495 {
5496 struct dwarf2_cu *cu = arg;
5497
5498 free_line_header (cu->line_header);
5499 cu->line_header = NULL;
5500 }
5501
5502 static void
5503 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5504 char **name, char **comp_dir)
5505 {
5506 struct attribute *attr;
5507
5508 *name = NULL;
5509 *comp_dir = NULL;
5510
5511 /* Find the filename. Do not use dwarf2_name here, since the filename
5512 is not a source language identifier. */
5513 attr = dwarf2_attr (die, DW_AT_name, cu);
5514 if (attr)
5515 {
5516 *name = DW_STRING (attr);
5517 }
5518
5519 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5520 if (attr)
5521 *comp_dir = DW_STRING (attr);
5522 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5523 {
5524 *comp_dir = ldirname (*name);
5525 if (*comp_dir != NULL)
5526 make_cleanup (xfree, *comp_dir);
5527 }
5528 if (*comp_dir != NULL)
5529 {
5530 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5531 directory, get rid of it. */
5532 char *cp = strchr (*comp_dir, ':');
5533
5534 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5535 *comp_dir = cp + 1;
5536 }
5537
5538 if (*name == NULL)
5539 *name = "<unknown>";
5540 }
5541
5542 /* Handle DW_AT_stmt_list for a compilation unit. */
5543
5544 static void
5545 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5546 const char *comp_dir)
5547 {
5548 struct attribute *attr;
5549 struct objfile *objfile = cu->objfile;
5550 bfd *abfd = objfile->obfd;
5551
5552 /* Decode line number information if present. We do this before
5553 processing child DIEs, so that the line header table is available
5554 for DW_AT_decl_file. */
5555 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5556 if (attr)
5557 {
5558 unsigned int line_offset = DW_UNSND (attr);
5559 struct line_header *line_header
5560 = dwarf_decode_line_header (line_offset, abfd, cu);
5561
5562 if (line_header)
5563 {
5564 cu->line_header = line_header;
5565 make_cleanup (free_cu_line_header, cu);
5566 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5567 }
5568 }
5569 }
5570
5571 /* Process DW_TAG_compile_unit. */
5572
5573 static void
5574 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5575 {
5576 struct objfile *objfile = cu->objfile;
5577 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5578 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5579 CORE_ADDR highpc = ((CORE_ADDR) 0);
5580 struct attribute *attr;
5581 char *name = NULL;
5582 char *comp_dir = NULL;
5583 struct die_info *child_die;
5584 bfd *abfd = objfile->obfd;
5585 CORE_ADDR baseaddr;
5586
5587 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5588
5589 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5590
5591 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5592 from finish_block. */
5593 if (lowpc == ((CORE_ADDR) -1))
5594 lowpc = highpc;
5595 lowpc += baseaddr;
5596 highpc += baseaddr;
5597
5598 find_file_and_directory (die, cu, &name, &comp_dir);
5599
5600 attr = dwarf2_attr (die, DW_AT_language, cu);
5601 if (attr)
5602 {
5603 set_cu_language (DW_UNSND (attr), cu);
5604 }
5605
5606 attr = dwarf2_attr (die, DW_AT_producer, cu);
5607 if (attr)
5608 cu->producer = DW_STRING (attr);
5609
5610 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5611 standardised yet. As a workaround for the language detection we fall
5612 back to the DW_AT_producer string. */
5613 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5614 cu->language = language_opencl;
5615
5616 /* We assume that we're processing GCC output. */
5617 processing_gcc_compilation = 2;
5618
5619 processing_has_namespace_info = 0;
5620
5621 start_symtab (name, comp_dir, lowpc);
5622 record_debugformat ("DWARF 2");
5623 record_producer (cu->producer);
5624
5625 initialize_cu_func_list (cu);
5626
5627 handle_DW_AT_stmt_list (die, cu, comp_dir);
5628
5629 /* Process all dies in compilation unit. */
5630 if (die->child != NULL)
5631 {
5632 child_die = die->child;
5633 while (child_die && child_die->tag)
5634 {
5635 process_die (child_die, cu);
5636 child_die = sibling_die (child_die);
5637 }
5638 }
5639
5640 /* Decode macro information, if present. Dwarf 2 macro information
5641 refers to information in the line number info statement program
5642 header, so we can only read it if we've read the header
5643 successfully. */
5644 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5645 if (attr && cu->line_header)
5646 {
5647 unsigned int macro_offset = DW_UNSND (attr);
5648
5649 dwarf_decode_macros (cu->line_header, macro_offset,
5650 comp_dir, abfd, cu);
5651 }
5652 do_cleanups (back_to);
5653 }
5654
5655 /* Process DW_TAG_type_unit.
5656 For TUs we want to skip the first top level sibling if it's not the
5657 actual type being defined by this TU. In this case the first top
5658 level sibling is there to provide context only. */
5659
5660 static void
5661 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5662 {
5663 struct objfile *objfile = cu->objfile;
5664 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5665 CORE_ADDR lowpc;
5666 struct attribute *attr;
5667 char *name = NULL;
5668 char *comp_dir = NULL;
5669 struct die_info *child_die;
5670 bfd *abfd = objfile->obfd;
5671
5672 /* start_symtab needs a low pc, but we don't really have one.
5673 Do what read_file_scope would do in the absence of such info. */
5674 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5675
5676 /* Find the filename. Do not use dwarf2_name here, since the filename
5677 is not a source language identifier. */
5678 attr = dwarf2_attr (die, DW_AT_name, cu);
5679 if (attr)
5680 name = DW_STRING (attr);
5681
5682 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5683 if (attr)
5684 comp_dir = DW_STRING (attr);
5685 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5686 {
5687 comp_dir = ldirname (name);
5688 if (comp_dir != NULL)
5689 make_cleanup (xfree, comp_dir);
5690 }
5691
5692 if (name == NULL)
5693 name = "<unknown>";
5694
5695 attr = dwarf2_attr (die, DW_AT_language, cu);
5696 if (attr)
5697 set_cu_language (DW_UNSND (attr), cu);
5698
5699 /* This isn't technically needed today. It is done for symmetry
5700 with read_file_scope. */
5701 attr = dwarf2_attr (die, DW_AT_producer, cu);
5702 if (attr)
5703 cu->producer = DW_STRING (attr);
5704
5705 /* We assume that we're processing GCC output. */
5706 processing_gcc_compilation = 2;
5707
5708 processing_has_namespace_info = 0;
5709
5710 start_symtab (name, comp_dir, lowpc);
5711 record_debugformat ("DWARF 2");
5712 record_producer (cu->producer);
5713
5714 handle_DW_AT_stmt_list (die, cu, comp_dir);
5715
5716 /* Process the dies in the type unit. */
5717 if (die->child == NULL)
5718 {
5719 dump_die_for_error (die);
5720 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5721 bfd_get_filename (abfd));
5722 }
5723
5724 child_die = die->child;
5725
5726 while (child_die && child_die->tag)
5727 {
5728 process_die (child_die, cu);
5729
5730 child_die = sibling_die (child_die);
5731 }
5732
5733 do_cleanups (back_to);
5734 }
5735
5736 static void
5737 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5738 struct dwarf2_cu *cu)
5739 {
5740 struct function_range *thisfn;
5741
5742 thisfn = (struct function_range *)
5743 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5744 thisfn->name = name;
5745 thisfn->lowpc = lowpc;
5746 thisfn->highpc = highpc;
5747 thisfn->seen_line = 0;
5748 thisfn->next = NULL;
5749
5750 if (cu->last_fn == NULL)
5751 cu->first_fn = thisfn;
5752 else
5753 cu->last_fn->next = thisfn;
5754
5755 cu->last_fn = thisfn;
5756 }
5757
5758 /* qsort helper for inherit_abstract_dies. */
5759
5760 static int
5761 unsigned_int_compar (const void *ap, const void *bp)
5762 {
5763 unsigned int a = *(unsigned int *) ap;
5764 unsigned int b = *(unsigned int *) bp;
5765
5766 return (a > b) - (b > a);
5767 }
5768
5769 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5770 Inherit only the children of the DW_AT_abstract_origin DIE not being
5771 already referenced by DW_AT_abstract_origin from the children of the
5772 current DIE. */
5773
5774 static void
5775 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5776 {
5777 struct die_info *child_die;
5778 unsigned die_children_count;
5779 /* CU offsets which were referenced by children of the current DIE. */
5780 unsigned *offsets;
5781 unsigned *offsets_end, *offsetp;
5782 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5783 struct die_info *origin_die;
5784 /* Iterator of the ORIGIN_DIE children. */
5785 struct die_info *origin_child_die;
5786 struct cleanup *cleanups;
5787 struct attribute *attr;
5788 struct dwarf2_cu *origin_cu;
5789 struct pending **origin_previous_list_in_scope;
5790
5791 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5792 if (!attr)
5793 return;
5794
5795 /* Note that following die references may follow to a die in a
5796 different cu. */
5797
5798 origin_cu = cu;
5799 origin_die = follow_die_ref (die, attr, &origin_cu);
5800
5801 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5802 symbols in. */
5803 origin_previous_list_in_scope = origin_cu->list_in_scope;
5804 origin_cu->list_in_scope = cu->list_in_scope;
5805
5806 if (die->tag != origin_die->tag
5807 && !(die->tag == DW_TAG_inlined_subroutine
5808 && origin_die->tag == DW_TAG_subprogram))
5809 complaint (&symfile_complaints,
5810 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5811 die->offset, origin_die->offset);
5812
5813 child_die = die->child;
5814 die_children_count = 0;
5815 while (child_die && child_die->tag)
5816 {
5817 child_die = sibling_die (child_die);
5818 die_children_count++;
5819 }
5820 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5821 cleanups = make_cleanup (xfree, offsets);
5822
5823 offsets_end = offsets;
5824 child_die = die->child;
5825 while (child_die && child_die->tag)
5826 {
5827 /* For each CHILD_DIE, find the corresponding child of
5828 ORIGIN_DIE. If there is more than one layer of
5829 DW_AT_abstract_origin, follow them all; there shouldn't be,
5830 but GCC versions at least through 4.4 generate this (GCC PR
5831 40573). */
5832 struct die_info *child_origin_die = child_die;
5833 struct dwarf2_cu *child_origin_cu = cu;
5834
5835 while (1)
5836 {
5837 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5838 child_origin_cu);
5839 if (attr == NULL)
5840 break;
5841 child_origin_die = follow_die_ref (child_origin_die, attr,
5842 &child_origin_cu);
5843 }
5844
5845 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5846 counterpart may exist. */
5847 if (child_origin_die != child_die)
5848 {
5849 if (child_die->tag != child_origin_die->tag
5850 && !(child_die->tag == DW_TAG_inlined_subroutine
5851 && child_origin_die->tag == DW_TAG_subprogram))
5852 complaint (&symfile_complaints,
5853 _("Child DIE 0x%x and its abstract origin 0x%x have "
5854 "different tags"), child_die->offset,
5855 child_origin_die->offset);
5856 if (child_origin_die->parent != origin_die)
5857 complaint (&symfile_complaints,
5858 _("Child DIE 0x%x and its abstract origin 0x%x have "
5859 "different parents"), child_die->offset,
5860 child_origin_die->offset);
5861 else
5862 *offsets_end++ = child_origin_die->offset;
5863 }
5864 child_die = sibling_die (child_die);
5865 }
5866 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5867 unsigned_int_compar);
5868 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5869 if (offsetp[-1] == *offsetp)
5870 complaint (&symfile_complaints,
5871 _("Multiple children of DIE 0x%x refer "
5872 "to DIE 0x%x as their abstract origin"),
5873 die->offset, *offsetp);
5874
5875 offsetp = offsets;
5876 origin_child_die = origin_die->child;
5877 while (origin_child_die && origin_child_die->tag)
5878 {
5879 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5880 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5881 offsetp++;
5882 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5883 {
5884 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5885 process_die (origin_child_die, origin_cu);
5886 }
5887 origin_child_die = sibling_die (origin_child_die);
5888 }
5889 origin_cu->list_in_scope = origin_previous_list_in_scope;
5890
5891 do_cleanups (cleanups);
5892 }
5893
5894 static void
5895 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5896 {
5897 struct objfile *objfile = cu->objfile;
5898 struct context_stack *new;
5899 CORE_ADDR lowpc;
5900 CORE_ADDR highpc;
5901 struct die_info *child_die;
5902 struct attribute *attr, *call_line, *call_file;
5903 char *name;
5904 CORE_ADDR baseaddr;
5905 struct block *block;
5906 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5907 VEC (symbolp) *template_args = NULL;
5908 struct template_symbol *templ_func = NULL;
5909
5910 if (inlined_func)
5911 {
5912 /* If we do not have call site information, we can't show the
5913 caller of this inlined function. That's too confusing, so
5914 only use the scope for local variables. */
5915 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5916 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5917 if (call_line == NULL || call_file == NULL)
5918 {
5919 read_lexical_block_scope (die, cu);
5920 return;
5921 }
5922 }
5923
5924 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5925
5926 name = dwarf2_name (die, cu);
5927
5928 /* Ignore functions with missing or empty names. These are actually
5929 illegal according to the DWARF standard. */
5930 if (name == NULL)
5931 {
5932 complaint (&symfile_complaints,
5933 _("missing name for subprogram DIE at %d"), die->offset);
5934 return;
5935 }
5936
5937 /* Ignore functions with missing or invalid low and high pc attributes. */
5938 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5939 {
5940 attr = dwarf2_attr (die, DW_AT_external, cu);
5941 if (!attr || !DW_UNSND (attr))
5942 complaint (&symfile_complaints,
5943 _("cannot get low and high bounds "
5944 "for subprogram DIE at %d"),
5945 die->offset);
5946 return;
5947 }
5948
5949 lowpc += baseaddr;
5950 highpc += baseaddr;
5951
5952 /* Record the function range for dwarf_decode_lines. */
5953 add_to_cu_func_list (name, lowpc, highpc, cu);
5954
5955 /* If we have any template arguments, then we must allocate a
5956 different sort of symbol. */
5957 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5958 {
5959 if (child_die->tag == DW_TAG_template_type_param
5960 || child_die->tag == DW_TAG_template_value_param)
5961 {
5962 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5963 struct template_symbol);
5964 templ_func->base.is_cplus_template_function = 1;
5965 break;
5966 }
5967 }
5968
5969 new = push_context (0, lowpc);
5970 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5971 (struct symbol *) templ_func);
5972
5973 /* If there is a location expression for DW_AT_frame_base, record
5974 it. */
5975 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5976 if (attr)
5977 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5978 expression is being recorded directly in the function's symbol
5979 and not in a separate frame-base object. I guess this hack is
5980 to avoid adding some sort of frame-base adjunct/annex to the
5981 function's symbol :-(. The problem with doing this is that it
5982 results in a function symbol with a location expression that
5983 has nothing to do with the location of the function, ouch! The
5984 relationship should be: a function's symbol has-a frame base; a
5985 frame-base has-a location expression. */
5986 dwarf2_symbol_mark_computed (attr, new->name, cu);
5987
5988 cu->list_in_scope = &local_symbols;
5989
5990 if (die->child != NULL)
5991 {
5992 child_die = die->child;
5993 while (child_die && child_die->tag)
5994 {
5995 if (child_die->tag == DW_TAG_template_type_param
5996 || child_die->tag == DW_TAG_template_value_param)
5997 {
5998 struct symbol *arg = new_symbol (child_die, NULL, cu);
5999
6000 if (arg != NULL)
6001 VEC_safe_push (symbolp, template_args, arg);
6002 }
6003 else
6004 process_die (child_die, cu);
6005 child_die = sibling_die (child_die);
6006 }
6007 }
6008
6009 inherit_abstract_dies (die, cu);
6010
6011 /* If we have a DW_AT_specification, we might need to import using
6012 directives from the context of the specification DIE. See the
6013 comment in determine_prefix. */
6014 if (cu->language == language_cplus
6015 && dwarf2_attr (die, DW_AT_specification, cu))
6016 {
6017 struct dwarf2_cu *spec_cu = cu;
6018 struct die_info *spec_die = die_specification (die, &spec_cu);
6019
6020 while (spec_die)
6021 {
6022 child_die = spec_die->child;
6023 while (child_die && child_die->tag)
6024 {
6025 if (child_die->tag == DW_TAG_imported_module)
6026 process_die (child_die, spec_cu);
6027 child_die = sibling_die (child_die);
6028 }
6029
6030 /* In some cases, GCC generates specification DIEs that
6031 themselves contain DW_AT_specification attributes. */
6032 spec_die = die_specification (spec_die, &spec_cu);
6033 }
6034 }
6035
6036 new = pop_context ();
6037 /* Make a block for the local symbols within. */
6038 block = finish_block (new->name, &local_symbols, new->old_blocks,
6039 lowpc, highpc, objfile);
6040
6041 /* For C++, set the block's scope. */
6042 if (cu->language == language_cplus || cu->language == language_fortran)
6043 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6044 determine_prefix (die, cu),
6045 processing_has_namespace_info);
6046
6047 /* If we have address ranges, record them. */
6048 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6049
6050 /* Attach template arguments to function. */
6051 if (! VEC_empty (symbolp, template_args))
6052 {
6053 gdb_assert (templ_func != NULL);
6054
6055 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6056 templ_func->template_arguments
6057 = obstack_alloc (&objfile->objfile_obstack,
6058 (templ_func->n_template_arguments
6059 * sizeof (struct symbol *)));
6060 memcpy (templ_func->template_arguments,
6061 VEC_address (symbolp, template_args),
6062 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6063 VEC_free (symbolp, template_args);
6064 }
6065
6066 /* In C++, we can have functions nested inside functions (e.g., when
6067 a function declares a class that has methods). This means that
6068 when we finish processing a function scope, we may need to go
6069 back to building a containing block's symbol lists. */
6070 local_symbols = new->locals;
6071 param_symbols = new->params;
6072 using_directives = new->using_directives;
6073
6074 /* If we've finished processing a top-level function, subsequent
6075 symbols go in the file symbol list. */
6076 if (outermost_context_p ())
6077 cu->list_in_scope = &file_symbols;
6078 }
6079
6080 /* Process all the DIES contained within a lexical block scope. Start
6081 a new scope, process the dies, and then close the scope. */
6082
6083 static void
6084 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6085 {
6086 struct objfile *objfile = cu->objfile;
6087 struct context_stack *new;
6088 CORE_ADDR lowpc, highpc;
6089 struct die_info *child_die;
6090 CORE_ADDR baseaddr;
6091
6092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6093
6094 /* Ignore blocks with missing or invalid low and high pc attributes. */
6095 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6096 as multiple lexical blocks? Handling children in a sane way would
6097 be nasty. Might be easier to properly extend generic blocks to
6098 describe ranges. */
6099 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6100 return;
6101 lowpc += baseaddr;
6102 highpc += baseaddr;
6103
6104 push_context (0, lowpc);
6105 if (die->child != NULL)
6106 {
6107 child_die = die->child;
6108 while (child_die && child_die->tag)
6109 {
6110 process_die (child_die, cu);
6111 child_die = sibling_die (child_die);
6112 }
6113 }
6114 new = pop_context ();
6115
6116 if (local_symbols != NULL || using_directives != NULL)
6117 {
6118 struct block *block
6119 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6120 highpc, objfile);
6121
6122 /* Note that recording ranges after traversing children, as we
6123 do here, means that recording a parent's ranges entails
6124 walking across all its children's ranges as they appear in
6125 the address map, which is quadratic behavior.
6126
6127 It would be nicer to record the parent's ranges before
6128 traversing its children, simply overriding whatever you find
6129 there. But since we don't even decide whether to create a
6130 block until after we've traversed its children, that's hard
6131 to do. */
6132 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6133 }
6134 local_symbols = new->locals;
6135 using_directives = new->using_directives;
6136 }
6137
6138 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6139 Return 1 if the attributes are present and valid, otherwise, return 0.
6140 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6141
6142 static int
6143 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6144 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6145 struct partial_symtab *ranges_pst)
6146 {
6147 struct objfile *objfile = cu->objfile;
6148 struct comp_unit_head *cu_header = &cu->header;
6149 bfd *obfd = objfile->obfd;
6150 unsigned int addr_size = cu_header->addr_size;
6151 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6152 /* Base address selection entry. */
6153 CORE_ADDR base;
6154 int found_base;
6155 unsigned int dummy;
6156 gdb_byte *buffer;
6157 CORE_ADDR marker;
6158 int low_set;
6159 CORE_ADDR low = 0;
6160 CORE_ADDR high = 0;
6161 CORE_ADDR baseaddr;
6162
6163 found_base = cu->base_known;
6164 base = cu->base_address;
6165
6166 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6167 if (offset >= dwarf2_per_objfile->ranges.size)
6168 {
6169 complaint (&symfile_complaints,
6170 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6171 offset);
6172 return 0;
6173 }
6174 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6175
6176 /* Read in the largest possible address. */
6177 marker = read_address (obfd, buffer, cu, &dummy);
6178 if ((marker & mask) == mask)
6179 {
6180 /* If we found the largest possible address, then
6181 read the base address. */
6182 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6183 buffer += 2 * addr_size;
6184 offset += 2 * addr_size;
6185 found_base = 1;
6186 }
6187
6188 low_set = 0;
6189
6190 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6191
6192 while (1)
6193 {
6194 CORE_ADDR range_beginning, range_end;
6195
6196 range_beginning = read_address (obfd, buffer, cu, &dummy);
6197 buffer += addr_size;
6198 range_end = read_address (obfd, buffer, cu, &dummy);
6199 buffer += addr_size;
6200 offset += 2 * addr_size;
6201
6202 /* An end of list marker is a pair of zero addresses. */
6203 if (range_beginning == 0 && range_end == 0)
6204 /* Found the end of list entry. */
6205 break;
6206
6207 /* Each base address selection entry is a pair of 2 values.
6208 The first is the largest possible address, the second is
6209 the base address. Check for a base address here. */
6210 if ((range_beginning & mask) == mask)
6211 {
6212 /* If we found the largest possible address, then
6213 read the base address. */
6214 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6215 found_base = 1;
6216 continue;
6217 }
6218
6219 if (!found_base)
6220 {
6221 /* We have no valid base address for the ranges
6222 data. */
6223 complaint (&symfile_complaints,
6224 _("Invalid .debug_ranges data (no base address)"));
6225 return 0;
6226 }
6227
6228 if (range_beginning > range_end)
6229 {
6230 /* Inverted range entries are invalid. */
6231 complaint (&symfile_complaints,
6232 _("Invalid .debug_ranges data (inverted range)"));
6233 return 0;
6234 }
6235
6236 /* Empty range entries have no effect. */
6237 if (range_beginning == range_end)
6238 continue;
6239
6240 range_beginning += base;
6241 range_end += base;
6242
6243 if (ranges_pst != NULL)
6244 addrmap_set_empty (objfile->psymtabs_addrmap,
6245 range_beginning + baseaddr,
6246 range_end - 1 + baseaddr,
6247 ranges_pst);
6248
6249 /* FIXME: This is recording everything as a low-high
6250 segment of consecutive addresses. We should have a
6251 data structure for discontiguous block ranges
6252 instead. */
6253 if (! low_set)
6254 {
6255 low = range_beginning;
6256 high = range_end;
6257 low_set = 1;
6258 }
6259 else
6260 {
6261 if (range_beginning < low)
6262 low = range_beginning;
6263 if (range_end > high)
6264 high = range_end;
6265 }
6266 }
6267
6268 if (! low_set)
6269 /* If the first entry is an end-of-list marker, the range
6270 describes an empty scope, i.e. no instructions. */
6271 return 0;
6272
6273 if (low_return)
6274 *low_return = low;
6275 if (high_return)
6276 *high_return = high;
6277 return 1;
6278 }
6279
6280 /* Get low and high pc attributes from a die. Return 1 if the attributes
6281 are present and valid, otherwise, return 0. Return -1 if the range is
6282 discontinuous, i.e. derived from DW_AT_ranges information. */
6283 static int
6284 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6285 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6286 struct partial_symtab *pst)
6287 {
6288 struct attribute *attr;
6289 CORE_ADDR low = 0;
6290 CORE_ADDR high = 0;
6291 int ret = 0;
6292
6293 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6294 if (attr)
6295 {
6296 high = DW_ADDR (attr);
6297 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6298 if (attr)
6299 low = DW_ADDR (attr);
6300 else
6301 /* Found high w/o low attribute. */
6302 return 0;
6303
6304 /* Found consecutive range of addresses. */
6305 ret = 1;
6306 }
6307 else
6308 {
6309 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6310 if (attr != NULL)
6311 {
6312 /* Value of the DW_AT_ranges attribute is the offset in the
6313 .debug_ranges section. */
6314 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6315 return 0;
6316 /* Found discontinuous range of addresses. */
6317 ret = -1;
6318 }
6319 }
6320
6321 /* read_partial_die has also the strict LOW < HIGH requirement. */
6322 if (high <= low)
6323 return 0;
6324
6325 /* When using the GNU linker, .gnu.linkonce. sections are used to
6326 eliminate duplicate copies of functions and vtables and such.
6327 The linker will arbitrarily choose one and discard the others.
6328 The AT_*_pc values for such functions refer to local labels in
6329 these sections. If the section from that file was discarded, the
6330 labels are not in the output, so the relocs get a value of 0.
6331 If this is a discarded function, mark the pc bounds as invalid,
6332 so that GDB will ignore it. */
6333 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6334 return 0;
6335
6336 *lowpc = low;
6337 *highpc = high;
6338 return ret;
6339 }
6340
6341 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6342 its low and high PC addresses. Do nothing if these addresses could not
6343 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6344 and HIGHPC to the high address if greater than HIGHPC. */
6345
6346 static void
6347 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6348 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6349 struct dwarf2_cu *cu)
6350 {
6351 CORE_ADDR low, high;
6352 struct die_info *child = die->child;
6353
6354 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6355 {
6356 *lowpc = min (*lowpc, low);
6357 *highpc = max (*highpc, high);
6358 }
6359
6360 /* If the language does not allow nested subprograms (either inside
6361 subprograms or lexical blocks), we're done. */
6362 if (cu->language != language_ada)
6363 return;
6364
6365 /* Check all the children of the given DIE. If it contains nested
6366 subprograms, then check their pc bounds. Likewise, we need to
6367 check lexical blocks as well, as they may also contain subprogram
6368 definitions. */
6369 while (child && child->tag)
6370 {
6371 if (child->tag == DW_TAG_subprogram
6372 || child->tag == DW_TAG_lexical_block)
6373 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6374 child = sibling_die (child);
6375 }
6376 }
6377
6378 /* Get the low and high pc's represented by the scope DIE, and store
6379 them in *LOWPC and *HIGHPC. If the correct values can't be
6380 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6381
6382 static void
6383 get_scope_pc_bounds (struct die_info *die,
6384 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6385 struct dwarf2_cu *cu)
6386 {
6387 CORE_ADDR best_low = (CORE_ADDR) -1;
6388 CORE_ADDR best_high = (CORE_ADDR) 0;
6389 CORE_ADDR current_low, current_high;
6390
6391 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6392 {
6393 best_low = current_low;
6394 best_high = current_high;
6395 }
6396 else
6397 {
6398 struct die_info *child = die->child;
6399
6400 while (child && child->tag)
6401 {
6402 switch (child->tag) {
6403 case DW_TAG_subprogram:
6404 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6405 break;
6406 case DW_TAG_namespace:
6407 case DW_TAG_module:
6408 /* FIXME: carlton/2004-01-16: Should we do this for
6409 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6410 that current GCC's always emit the DIEs corresponding
6411 to definitions of methods of classes as children of a
6412 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6413 the DIEs giving the declarations, which could be
6414 anywhere). But I don't see any reason why the
6415 standards says that they have to be there. */
6416 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6417
6418 if (current_low != ((CORE_ADDR) -1))
6419 {
6420 best_low = min (best_low, current_low);
6421 best_high = max (best_high, current_high);
6422 }
6423 break;
6424 default:
6425 /* Ignore. */
6426 break;
6427 }
6428
6429 child = sibling_die (child);
6430 }
6431 }
6432
6433 *lowpc = best_low;
6434 *highpc = best_high;
6435 }
6436
6437 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6438 in DIE. */
6439 static void
6440 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6441 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6442 {
6443 struct attribute *attr;
6444
6445 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6446 if (attr)
6447 {
6448 CORE_ADDR high = DW_ADDR (attr);
6449
6450 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6451 if (attr)
6452 {
6453 CORE_ADDR low = DW_ADDR (attr);
6454
6455 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6456 }
6457 }
6458
6459 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6460 if (attr)
6461 {
6462 bfd *obfd = cu->objfile->obfd;
6463
6464 /* The value of the DW_AT_ranges attribute is the offset of the
6465 address range list in the .debug_ranges section. */
6466 unsigned long offset = DW_UNSND (attr);
6467 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6468
6469 /* For some target architectures, but not others, the
6470 read_address function sign-extends the addresses it returns.
6471 To recognize base address selection entries, we need a
6472 mask. */
6473 unsigned int addr_size = cu->header.addr_size;
6474 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6475
6476 /* The base address, to which the next pair is relative. Note
6477 that this 'base' is a DWARF concept: most entries in a range
6478 list are relative, to reduce the number of relocs against the
6479 debugging information. This is separate from this function's
6480 'baseaddr' argument, which GDB uses to relocate debugging
6481 information from a shared library based on the address at
6482 which the library was loaded. */
6483 CORE_ADDR base = cu->base_address;
6484 int base_known = cu->base_known;
6485
6486 gdb_assert (dwarf2_per_objfile->ranges.readin);
6487 if (offset >= dwarf2_per_objfile->ranges.size)
6488 {
6489 complaint (&symfile_complaints,
6490 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6491 offset);
6492 return;
6493 }
6494
6495 for (;;)
6496 {
6497 unsigned int bytes_read;
6498 CORE_ADDR start, end;
6499
6500 start = read_address (obfd, buffer, cu, &bytes_read);
6501 buffer += bytes_read;
6502 end = read_address (obfd, buffer, cu, &bytes_read);
6503 buffer += bytes_read;
6504
6505 /* Did we find the end of the range list? */
6506 if (start == 0 && end == 0)
6507 break;
6508
6509 /* Did we find a base address selection entry? */
6510 else if ((start & base_select_mask) == base_select_mask)
6511 {
6512 base = end;
6513 base_known = 1;
6514 }
6515
6516 /* We found an ordinary address range. */
6517 else
6518 {
6519 if (!base_known)
6520 {
6521 complaint (&symfile_complaints,
6522 _("Invalid .debug_ranges data "
6523 "(no base address)"));
6524 return;
6525 }
6526
6527 if (start > end)
6528 {
6529 /* Inverted range entries are invalid. */
6530 complaint (&symfile_complaints,
6531 _("Invalid .debug_ranges data "
6532 "(inverted range)"));
6533 return;
6534 }
6535
6536 /* Empty range entries have no effect. */
6537 if (start == end)
6538 continue;
6539
6540 record_block_range (block,
6541 baseaddr + base + start,
6542 baseaddr + base + end - 1);
6543 }
6544 }
6545 }
6546 }
6547
6548 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6549 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6550 during 4.6.0 experimental. */
6551
6552 static int
6553 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6554 {
6555 const char *cs;
6556 int major, minor, release;
6557
6558 if (cu->producer == NULL)
6559 {
6560 /* For unknown compilers expect their behavior is DWARF version
6561 compliant.
6562
6563 GCC started to support .debug_types sections by -gdwarf-4 since
6564 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6565 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6566 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6567 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6568
6569 return 0;
6570 }
6571
6572 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6573
6574 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6575 {
6576 /* For non-GCC compilers expect their behavior is DWARF version
6577 compliant. */
6578
6579 return 0;
6580 }
6581 cs = &cu->producer[strlen ("GNU ")];
6582 while (*cs && !isdigit (*cs))
6583 cs++;
6584 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6585 {
6586 /* Not recognized as GCC. */
6587
6588 return 0;
6589 }
6590
6591 return major < 4 || (major == 4 && minor < 6);
6592 }
6593
6594 /* Return the default accessibility type if it is not overriden by
6595 DW_AT_accessibility. */
6596
6597 static enum dwarf_access_attribute
6598 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6599 {
6600 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6601 {
6602 /* The default DWARF 2 accessibility for members is public, the default
6603 accessibility for inheritance is private. */
6604
6605 if (die->tag != DW_TAG_inheritance)
6606 return DW_ACCESS_public;
6607 else
6608 return DW_ACCESS_private;
6609 }
6610 else
6611 {
6612 /* DWARF 3+ defines the default accessibility a different way. The same
6613 rules apply now for DW_TAG_inheritance as for the members and it only
6614 depends on the container kind. */
6615
6616 if (die->parent->tag == DW_TAG_class_type)
6617 return DW_ACCESS_private;
6618 else
6619 return DW_ACCESS_public;
6620 }
6621 }
6622
6623 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6624 offset. If the attribute was not found return 0, otherwise return
6625 1. If it was found but could not properly be handled, set *OFFSET
6626 to 0. */
6627
6628 static int
6629 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6630 LONGEST *offset)
6631 {
6632 struct attribute *attr;
6633
6634 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6635 if (attr != NULL)
6636 {
6637 *offset = 0;
6638
6639 /* Note that we do not check for a section offset first here.
6640 This is because DW_AT_data_member_location is new in DWARF 4,
6641 so if we see it, we can assume that a constant form is really
6642 a constant and not a section offset. */
6643 if (attr_form_is_constant (attr))
6644 *offset = dwarf2_get_attr_constant_value (attr, 0);
6645 else if (attr_form_is_section_offset (attr))
6646 dwarf2_complex_location_expr_complaint ();
6647 else if (attr_form_is_block (attr))
6648 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6649 else
6650 dwarf2_complex_location_expr_complaint ();
6651
6652 return 1;
6653 }
6654
6655 return 0;
6656 }
6657
6658 /* Add an aggregate field to the field list. */
6659
6660 static void
6661 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6662 struct dwarf2_cu *cu)
6663 {
6664 struct objfile *objfile = cu->objfile;
6665 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6666 struct nextfield *new_field;
6667 struct attribute *attr;
6668 struct field *fp;
6669 char *fieldname = "";
6670
6671 /* Allocate a new field list entry and link it in. */
6672 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6673 make_cleanup (xfree, new_field);
6674 memset (new_field, 0, sizeof (struct nextfield));
6675
6676 if (die->tag == DW_TAG_inheritance)
6677 {
6678 new_field->next = fip->baseclasses;
6679 fip->baseclasses = new_field;
6680 }
6681 else
6682 {
6683 new_field->next = fip->fields;
6684 fip->fields = new_field;
6685 }
6686 fip->nfields++;
6687
6688 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6689 if (attr)
6690 new_field->accessibility = DW_UNSND (attr);
6691 else
6692 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6693 if (new_field->accessibility != DW_ACCESS_public)
6694 fip->non_public_fields = 1;
6695
6696 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6697 if (attr)
6698 new_field->virtuality = DW_UNSND (attr);
6699 else
6700 new_field->virtuality = DW_VIRTUALITY_none;
6701
6702 fp = &new_field->field;
6703
6704 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6705 {
6706 LONGEST offset;
6707
6708 /* Data member other than a C++ static data member. */
6709
6710 /* Get type of field. */
6711 fp->type = die_type (die, cu);
6712
6713 SET_FIELD_BITPOS (*fp, 0);
6714
6715 /* Get bit size of field (zero if none). */
6716 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6717 if (attr)
6718 {
6719 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6720 }
6721 else
6722 {
6723 FIELD_BITSIZE (*fp) = 0;
6724 }
6725
6726 /* Get bit offset of field. */
6727 if (handle_data_member_location (die, cu, &offset))
6728 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6729 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6730 if (attr)
6731 {
6732 if (gdbarch_bits_big_endian (gdbarch))
6733 {
6734 /* For big endian bits, the DW_AT_bit_offset gives the
6735 additional bit offset from the MSB of the containing
6736 anonymous object to the MSB of the field. We don't
6737 have to do anything special since we don't need to
6738 know the size of the anonymous object. */
6739 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6740 }
6741 else
6742 {
6743 /* For little endian bits, compute the bit offset to the
6744 MSB of the anonymous object, subtract off the number of
6745 bits from the MSB of the field to the MSB of the
6746 object, and then subtract off the number of bits of
6747 the field itself. The result is the bit offset of
6748 the LSB of the field. */
6749 int anonymous_size;
6750 int bit_offset = DW_UNSND (attr);
6751
6752 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6753 if (attr)
6754 {
6755 /* The size of the anonymous object containing
6756 the bit field is explicit, so use the
6757 indicated size (in bytes). */
6758 anonymous_size = DW_UNSND (attr);
6759 }
6760 else
6761 {
6762 /* The size of the anonymous object containing
6763 the bit field must be inferred from the type
6764 attribute of the data member containing the
6765 bit field. */
6766 anonymous_size = TYPE_LENGTH (fp->type);
6767 }
6768 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6769 - bit_offset - FIELD_BITSIZE (*fp);
6770 }
6771 }
6772
6773 /* Get name of field. */
6774 fieldname = dwarf2_name (die, cu);
6775 if (fieldname == NULL)
6776 fieldname = "";
6777
6778 /* The name is already allocated along with this objfile, so we don't
6779 need to duplicate it for the type. */
6780 fp->name = fieldname;
6781
6782 /* Change accessibility for artificial fields (e.g. virtual table
6783 pointer or virtual base class pointer) to private. */
6784 if (dwarf2_attr (die, DW_AT_artificial, cu))
6785 {
6786 FIELD_ARTIFICIAL (*fp) = 1;
6787 new_field->accessibility = DW_ACCESS_private;
6788 fip->non_public_fields = 1;
6789 }
6790 }
6791 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6792 {
6793 /* C++ static member. */
6794
6795 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6796 is a declaration, but all versions of G++ as of this writing
6797 (so through at least 3.2.1) incorrectly generate
6798 DW_TAG_variable tags. */
6799
6800 const char *physname;
6801
6802 /* Get name of field. */
6803 fieldname = dwarf2_name (die, cu);
6804 if (fieldname == NULL)
6805 return;
6806
6807 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6808 if (attr
6809 /* Only create a symbol if this is an external value.
6810 new_symbol checks this and puts the value in the global symbol
6811 table, which we want. If it is not external, new_symbol
6812 will try to put the value in cu->list_in_scope which is wrong. */
6813 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6814 {
6815 /* A static const member, not much different than an enum as far as
6816 we're concerned, except that we can support more types. */
6817 new_symbol (die, NULL, cu);
6818 }
6819
6820 /* Get physical name. */
6821 physname = dwarf2_physname (fieldname, die, cu);
6822
6823 /* The name is already allocated along with this objfile, so we don't
6824 need to duplicate it for the type. */
6825 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6826 FIELD_TYPE (*fp) = die_type (die, cu);
6827 FIELD_NAME (*fp) = fieldname;
6828 }
6829 else if (die->tag == DW_TAG_inheritance)
6830 {
6831 LONGEST offset;
6832
6833 /* C++ base class field. */
6834 if (handle_data_member_location (die, cu, &offset))
6835 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6836 FIELD_BITSIZE (*fp) = 0;
6837 FIELD_TYPE (*fp) = die_type (die, cu);
6838 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6839 fip->nbaseclasses++;
6840 }
6841 }
6842
6843 /* Add a typedef defined in the scope of the FIP's class. */
6844
6845 static void
6846 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6847 struct dwarf2_cu *cu)
6848 {
6849 struct objfile *objfile = cu->objfile;
6850 struct typedef_field_list *new_field;
6851 struct attribute *attr;
6852 struct typedef_field *fp;
6853 char *fieldname = "";
6854
6855 /* Allocate a new field list entry and link it in. */
6856 new_field = xzalloc (sizeof (*new_field));
6857 make_cleanup (xfree, new_field);
6858
6859 gdb_assert (die->tag == DW_TAG_typedef);
6860
6861 fp = &new_field->field;
6862
6863 /* Get name of field. */
6864 fp->name = dwarf2_name (die, cu);
6865 if (fp->name == NULL)
6866 return;
6867
6868 fp->type = read_type_die (die, cu);
6869
6870 new_field->next = fip->typedef_field_list;
6871 fip->typedef_field_list = new_field;
6872 fip->typedef_field_list_count++;
6873 }
6874
6875 /* Create the vector of fields, and attach it to the type. */
6876
6877 static void
6878 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6879 struct dwarf2_cu *cu)
6880 {
6881 int nfields = fip->nfields;
6882
6883 /* Record the field count, allocate space for the array of fields,
6884 and create blank accessibility bitfields if necessary. */
6885 TYPE_NFIELDS (type) = nfields;
6886 TYPE_FIELDS (type) = (struct field *)
6887 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6888 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6889
6890 if (fip->non_public_fields && cu->language != language_ada)
6891 {
6892 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6893
6894 TYPE_FIELD_PRIVATE_BITS (type) =
6895 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6896 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6897
6898 TYPE_FIELD_PROTECTED_BITS (type) =
6899 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6900 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6901
6902 TYPE_FIELD_IGNORE_BITS (type) =
6903 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6904 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6905 }
6906
6907 /* If the type has baseclasses, allocate and clear a bit vector for
6908 TYPE_FIELD_VIRTUAL_BITS. */
6909 if (fip->nbaseclasses && cu->language != language_ada)
6910 {
6911 int num_bytes = B_BYTES (fip->nbaseclasses);
6912 unsigned char *pointer;
6913
6914 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6915 pointer = TYPE_ALLOC (type, num_bytes);
6916 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6917 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6918 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6919 }
6920
6921 /* Copy the saved-up fields into the field vector. Start from the head of
6922 the list, adding to the tail of the field array, so that they end up in
6923 the same order in the array in which they were added to the list. */
6924 while (nfields-- > 0)
6925 {
6926 struct nextfield *fieldp;
6927
6928 if (fip->fields)
6929 {
6930 fieldp = fip->fields;
6931 fip->fields = fieldp->next;
6932 }
6933 else
6934 {
6935 fieldp = fip->baseclasses;
6936 fip->baseclasses = fieldp->next;
6937 }
6938
6939 TYPE_FIELD (type, nfields) = fieldp->field;
6940 switch (fieldp->accessibility)
6941 {
6942 case DW_ACCESS_private:
6943 if (cu->language != language_ada)
6944 SET_TYPE_FIELD_PRIVATE (type, nfields);
6945 break;
6946
6947 case DW_ACCESS_protected:
6948 if (cu->language != language_ada)
6949 SET_TYPE_FIELD_PROTECTED (type, nfields);
6950 break;
6951
6952 case DW_ACCESS_public:
6953 break;
6954
6955 default:
6956 /* Unknown accessibility. Complain and treat it as public. */
6957 {
6958 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6959 fieldp->accessibility);
6960 }
6961 break;
6962 }
6963 if (nfields < fip->nbaseclasses)
6964 {
6965 switch (fieldp->virtuality)
6966 {
6967 case DW_VIRTUALITY_virtual:
6968 case DW_VIRTUALITY_pure_virtual:
6969 if (cu->language == language_ada)
6970 error (_("unexpected virtuality in component of Ada type"));
6971 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6972 break;
6973 }
6974 }
6975 }
6976 }
6977
6978 /* Add a member function to the proper fieldlist. */
6979
6980 static void
6981 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6982 struct type *type, struct dwarf2_cu *cu)
6983 {
6984 struct objfile *objfile = cu->objfile;
6985 struct attribute *attr;
6986 struct fnfieldlist *flp;
6987 int i;
6988 struct fn_field *fnp;
6989 char *fieldname;
6990 struct nextfnfield *new_fnfield;
6991 struct type *this_type;
6992 enum dwarf_access_attribute accessibility;
6993
6994 if (cu->language == language_ada)
6995 error (_("unexpected member function in Ada type"));
6996
6997 /* Get name of member function. */
6998 fieldname = dwarf2_name (die, cu);
6999 if (fieldname == NULL)
7000 return;
7001
7002 /* Look up member function name in fieldlist. */
7003 for (i = 0; i < fip->nfnfields; i++)
7004 {
7005 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7006 break;
7007 }
7008
7009 /* Create new list element if necessary. */
7010 if (i < fip->nfnfields)
7011 flp = &fip->fnfieldlists[i];
7012 else
7013 {
7014 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7015 {
7016 fip->fnfieldlists = (struct fnfieldlist *)
7017 xrealloc (fip->fnfieldlists,
7018 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7019 * sizeof (struct fnfieldlist));
7020 if (fip->nfnfields == 0)
7021 make_cleanup (free_current_contents, &fip->fnfieldlists);
7022 }
7023 flp = &fip->fnfieldlists[fip->nfnfields];
7024 flp->name = fieldname;
7025 flp->length = 0;
7026 flp->head = NULL;
7027 i = fip->nfnfields++;
7028 }
7029
7030 /* Create a new member function field and chain it to the field list
7031 entry. */
7032 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7033 make_cleanup (xfree, new_fnfield);
7034 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7035 new_fnfield->next = flp->head;
7036 flp->head = new_fnfield;
7037 flp->length++;
7038
7039 /* Fill in the member function field info. */
7040 fnp = &new_fnfield->fnfield;
7041
7042 /* Delay processing of the physname until later. */
7043 if (cu->language == language_cplus || cu->language == language_java)
7044 {
7045 add_to_method_list (type, i, flp->length - 1, fieldname,
7046 die, cu);
7047 }
7048 else
7049 {
7050 const char *physname = dwarf2_physname (fieldname, die, cu);
7051 fnp->physname = physname ? physname : "";
7052 }
7053
7054 fnp->type = alloc_type (objfile);
7055 this_type = read_type_die (die, cu);
7056 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7057 {
7058 int nparams = TYPE_NFIELDS (this_type);
7059
7060 /* TYPE is the domain of this method, and THIS_TYPE is the type
7061 of the method itself (TYPE_CODE_METHOD). */
7062 smash_to_method_type (fnp->type, type,
7063 TYPE_TARGET_TYPE (this_type),
7064 TYPE_FIELDS (this_type),
7065 TYPE_NFIELDS (this_type),
7066 TYPE_VARARGS (this_type));
7067
7068 /* Handle static member functions.
7069 Dwarf2 has no clean way to discern C++ static and non-static
7070 member functions. G++ helps GDB by marking the first
7071 parameter for non-static member functions (which is the this
7072 pointer) as artificial. We obtain this information from
7073 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7074 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7075 fnp->voffset = VOFFSET_STATIC;
7076 }
7077 else
7078 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7079 dwarf2_full_name (fieldname, die, cu));
7080
7081 /* Get fcontext from DW_AT_containing_type if present. */
7082 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7083 fnp->fcontext = die_containing_type (die, cu);
7084
7085 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7086 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7087
7088 /* Get accessibility. */
7089 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7090 if (attr)
7091 accessibility = DW_UNSND (attr);
7092 else
7093 accessibility = dwarf2_default_access_attribute (die, cu);
7094 switch (accessibility)
7095 {
7096 case DW_ACCESS_private:
7097 fnp->is_private = 1;
7098 break;
7099 case DW_ACCESS_protected:
7100 fnp->is_protected = 1;
7101 break;
7102 }
7103
7104 /* Check for artificial methods. */
7105 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7106 if (attr && DW_UNSND (attr) != 0)
7107 fnp->is_artificial = 1;
7108
7109 /* Get index in virtual function table if it is a virtual member
7110 function. For older versions of GCC, this is an offset in the
7111 appropriate virtual table, as specified by DW_AT_containing_type.
7112 For everyone else, it is an expression to be evaluated relative
7113 to the object address. */
7114
7115 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7116 if (attr)
7117 {
7118 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7119 {
7120 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7121 {
7122 /* Old-style GCC. */
7123 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7124 }
7125 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7126 || (DW_BLOCK (attr)->size > 1
7127 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7128 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7129 {
7130 struct dwarf_block blk;
7131 int offset;
7132
7133 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7134 ? 1 : 2);
7135 blk.size = DW_BLOCK (attr)->size - offset;
7136 blk.data = DW_BLOCK (attr)->data + offset;
7137 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7138 if ((fnp->voffset % cu->header.addr_size) != 0)
7139 dwarf2_complex_location_expr_complaint ();
7140 else
7141 fnp->voffset /= cu->header.addr_size;
7142 fnp->voffset += 2;
7143 }
7144 else
7145 dwarf2_complex_location_expr_complaint ();
7146
7147 if (!fnp->fcontext)
7148 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7149 }
7150 else if (attr_form_is_section_offset (attr))
7151 {
7152 dwarf2_complex_location_expr_complaint ();
7153 }
7154 else
7155 {
7156 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7157 fieldname);
7158 }
7159 }
7160 else
7161 {
7162 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7163 if (attr && DW_UNSND (attr))
7164 {
7165 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7166 complaint (&symfile_complaints,
7167 _("Member function \"%s\" (offset %d) is virtual "
7168 "but the vtable offset is not specified"),
7169 fieldname, die->offset);
7170 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7171 TYPE_CPLUS_DYNAMIC (type) = 1;
7172 }
7173 }
7174 }
7175
7176 /* Create the vector of member function fields, and attach it to the type. */
7177
7178 static void
7179 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7180 struct dwarf2_cu *cu)
7181 {
7182 struct fnfieldlist *flp;
7183 int total_length = 0;
7184 int i;
7185
7186 if (cu->language == language_ada)
7187 error (_("unexpected member functions in Ada type"));
7188
7189 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7190 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7191 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7192
7193 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7194 {
7195 struct nextfnfield *nfp = flp->head;
7196 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7197 int k;
7198
7199 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7200 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7201 fn_flp->fn_fields = (struct fn_field *)
7202 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7203 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7204 fn_flp->fn_fields[k] = nfp->fnfield;
7205
7206 total_length += flp->length;
7207 }
7208
7209 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7210 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7211 }
7212
7213 /* Returns non-zero if NAME is the name of a vtable member in CU's
7214 language, zero otherwise. */
7215 static int
7216 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7217 {
7218 static const char vptr[] = "_vptr";
7219 static const char vtable[] = "vtable";
7220
7221 /* Look for the C++ and Java forms of the vtable. */
7222 if ((cu->language == language_java
7223 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7224 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7225 && is_cplus_marker (name[sizeof (vptr) - 1])))
7226 return 1;
7227
7228 return 0;
7229 }
7230
7231 /* GCC outputs unnamed structures that are really pointers to member
7232 functions, with the ABI-specified layout. If TYPE describes
7233 such a structure, smash it into a member function type.
7234
7235 GCC shouldn't do this; it should just output pointer to member DIEs.
7236 This is GCC PR debug/28767. */
7237
7238 static void
7239 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7240 {
7241 struct type *pfn_type, *domain_type, *new_type;
7242
7243 /* Check for a structure with no name and two children. */
7244 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7245 return;
7246
7247 /* Check for __pfn and __delta members. */
7248 if (TYPE_FIELD_NAME (type, 0) == NULL
7249 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7250 || TYPE_FIELD_NAME (type, 1) == NULL
7251 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7252 return;
7253
7254 /* Find the type of the method. */
7255 pfn_type = TYPE_FIELD_TYPE (type, 0);
7256 if (pfn_type == NULL
7257 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7258 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7259 return;
7260
7261 /* Look for the "this" argument. */
7262 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7263 if (TYPE_NFIELDS (pfn_type) == 0
7264 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7265 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7266 return;
7267
7268 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7269 new_type = alloc_type (objfile);
7270 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7271 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7272 TYPE_VARARGS (pfn_type));
7273 smash_to_methodptr_type (type, new_type);
7274 }
7275
7276 /* Called when we find the DIE that starts a structure or union scope
7277 (definition) to create a type for the structure or union. Fill in
7278 the type's name and general properties; the members will not be
7279 processed until process_structure_type.
7280
7281 NOTE: we need to call these functions regardless of whether or not the
7282 DIE has a DW_AT_name attribute, since it might be an anonymous
7283 structure or union. This gets the type entered into our set of
7284 user defined types.
7285
7286 However, if the structure is incomplete (an opaque struct/union)
7287 then suppress creating a symbol table entry for it since gdb only
7288 wants to find the one with the complete definition. Note that if
7289 it is complete, we just call new_symbol, which does it's own
7290 checking about whether the struct/union is anonymous or not (and
7291 suppresses creating a symbol table entry itself). */
7292
7293 static struct type *
7294 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7295 {
7296 struct objfile *objfile = cu->objfile;
7297 struct type *type;
7298 struct attribute *attr;
7299 char *name;
7300
7301 /* If the definition of this type lives in .debug_types, read that type.
7302 Don't follow DW_AT_specification though, that will take us back up
7303 the chain and we want to go down. */
7304 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7305 if (attr)
7306 {
7307 struct dwarf2_cu *type_cu = cu;
7308 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7309
7310 /* We could just recurse on read_structure_type, but we need to call
7311 get_die_type to ensure only one type for this DIE is created.
7312 This is important, for example, because for c++ classes we need
7313 TYPE_NAME set which is only done by new_symbol. Blech. */
7314 type = read_type_die (type_die, type_cu);
7315
7316 /* TYPE_CU may not be the same as CU.
7317 Ensure TYPE is recorded in CU's type_hash table. */
7318 return set_die_type (die, type, cu);
7319 }
7320
7321 type = alloc_type (objfile);
7322 INIT_CPLUS_SPECIFIC (type);
7323
7324 name = dwarf2_name (die, cu);
7325 if (name != NULL)
7326 {
7327 if (cu->language == language_cplus
7328 || cu->language == language_java)
7329 {
7330 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7331
7332 /* dwarf2_full_name might have already finished building the DIE's
7333 type. If so, there is no need to continue. */
7334 if (get_die_type (die, cu) != NULL)
7335 return get_die_type (die, cu);
7336
7337 TYPE_TAG_NAME (type) = full_name;
7338 if (die->tag == DW_TAG_structure_type
7339 || die->tag == DW_TAG_class_type)
7340 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7341 }
7342 else
7343 {
7344 /* The name is already allocated along with this objfile, so
7345 we don't need to duplicate it for the type. */
7346 TYPE_TAG_NAME (type) = (char *) name;
7347 if (die->tag == DW_TAG_class_type)
7348 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7349 }
7350 }
7351
7352 if (die->tag == DW_TAG_structure_type)
7353 {
7354 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7355 }
7356 else if (die->tag == DW_TAG_union_type)
7357 {
7358 TYPE_CODE (type) = TYPE_CODE_UNION;
7359 }
7360 else
7361 {
7362 TYPE_CODE (type) = TYPE_CODE_CLASS;
7363 }
7364
7365 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7366 TYPE_DECLARED_CLASS (type) = 1;
7367
7368 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7369 if (attr)
7370 {
7371 TYPE_LENGTH (type) = DW_UNSND (attr);
7372 }
7373 else
7374 {
7375 TYPE_LENGTH (type) = 0;
7376 }
7377
7378 TYPE_STUB_SUPPORTED (type) = 1;
7379 if (die_is_declaration (die, cu))
7380 TYPE_STUB (type) = 1;
7381 else if (attr == NULL && die->child == NULL
7382 && producer_is_realview (cu->producer))
7383 /* RealView does not output the required DW_AT_declaration
7384 on incomplete types. */
7385 TYPE_STUB (type) = 1;
7386
7387 /* We need to add the type field to the die immediately so we don't
7388 infinitely recurse when dealing with pointers to the structure
7389 type within the structure itself. */
7390 set_die_type (die, type, cu);
7391
7392 /* set_die_type should be already done. */
7393 set_descriptive_type (type, die, cu);
7394
7395 return type;
7396 }
7397
7398 /* Finish creating a structure or union type, including filling in
7399 its members and creating a symbol for it. */
7400
7401 static void
7402 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7403 {
7404 struct objfile *objfile = cu->objfile;
7405 struct die_info *child_die = die->child;
7406 struct type *type;
7407
7408 type = get_die_type (die, cu);
7409 if (type == NULL)
7410 type = read_structure_type (die, cu);
7411
7412 if (die->child != NULL && ! die_is_declaration (die, cu))
7413 {
7414 struct field_info fi;
7415 struct die_info *child_die;
7416 VEC (symbolp) *template_args = NULL;
7417 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7418
7419 memset (&fi, 0, sizeof (struct field_info));
7420
7421 child_die = die->child;
7422
7423 while (child_die && child_die->tag)
7424 {
7425 if (child_die->tag == DW_TAG_member
7426 || child_die->tag == DW_TAG_variable)
7427 {
7428 /* NOTE: carlton/2002-11-05: A C++ static data member
7429 should be a DW_TAG_member that is a declaration, but
7430 all versions of G++ as of this writing (so through at
7431 least 3.2.1) incorrectly generate DW_TAG_variable
7432 tags for them instead. */
7433 dwarf2_add_field (&fi, child_die, cu);
7434 }
7435 else if (child_die->tag == DW_TAG_subprogram)
7436 {
7437 /* C++ member function. */
7438 dwarf2_add_member_fn (&fi, child_die, type, cu);
7439 }
7440 else if (child_die->tag == DW_TAG_inheritance)
7441 {
7442 /* C++ base class field. */
7443 dwarf2_add_field (&fi, child_die, cu);
7444 }
7445 else if (child_die->tag == DW_TAG_typedef)
7446 dwarf2_add_typedef (&fi, child_die, cu);
7447 else if (child_die->tag == DW_TAG_template_type_param
7448 || child_die->tag == DW_TAG_template_value_param)
7449 {
7450 struct symbol *arg = new_symbol (child_die, NULL, cu);
7451
7452 if (arg != NULL)
7453 VEC_safe_push (symbolp, template_args, arg);
7454 }
7455
7456 child_die = sibling_die (child_die);
7457 }
7458
7459 /* Attach template arguments to type. */
7460 if (! VEC_empty (symbolp, template_args))
7461 {
7462 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7463 TYPE_N_TEMPLATE_ARGUMENTS (type)
7464 = VEC_length (symbolp, template_args);
7465 TYPE_TEMPLATE_ARGUMENTS (type)
7466 = obstack_alloc (&objfile->objfile_obstack,
7467 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7468 * sizeof (struct symbol *)));
7469 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7470 VEC_address (symbolp, template_args),
7471 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7472 * sizeof (struct symbol *)));
7473 VEC_free (symbolp, template_args);
7474 }
7475
7476 /* Attach fields and member functions to the type. */
7477 if (fi.nfields)
7478 dwarf2_attach_fields_to_type (&fi, type, cu);
7479 if (fi.nfnfields)
7480 {
7481 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7482
7483 /* Get the type which refers to the base class (possibly this
7484 class itself) which contains the vtable pointer for the current
7485 class from the DW_AT_containing_type attribute. This use of
7486 DW_AT_containing_type is a GNU extension. */
7487
7488 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7489 {
7490 struct type *t = die_containing_type (die, cu);
7491
7492 TYPE_VPTR_BASETYPE (type) = t;
7493 if (type == t)
7494 {
7495 int i;
7496
7497 /* Our own class provides vtbl ptr. */
7498 for (i = TYPE_NFIELDS (t) - 1;
7499 i >= TYPE_N_BASECLASSES (t);
7500 --i)
7501 {
7502 char *fieldname = TYPE_FIELD_NAME (t, i);
7503
7504 if (is_vtable_name (fieldname, cu))
7505 {
7506 TYPE_VPTR_FIELDNO (type) = i;
7507 break;
7508 }
7509 }
7510
7511 /* Complain if virtual function table field not found. */
7512 if (i < TYPE_N_BASECLASSES (t))
7513 complaint (&symfile_complaints,
7514 _("virtual function table pointer "
7515 "not found when defining class '%s'"),
7516 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7517 "");
7518 }
7519 else
7520 {
7521 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7522 }
7523 }
7524 else if (cu->producer
7525 && strncmp (cu->producer,
7526 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7527 {
7528 /* The IBM XLC compiler does not provide direct indication
7529 of the containing type, but the vtable pointer is
7530 always named __vfp. */
7531
7532 int i;
7533
7534 for (i = TYPE_NFIELDS (type) - 1;
7535 i >= TYPE_N_BASECLASSES (type);
7536 --i)
7537 {
7538 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7539 {
7540 TYPE_VPTR_FIELDNO (type) = i;
7541 TYPE_VPTR_BASETYPE (type) = type;
7542 break;
7543 }
7544 }
7545 }
7546 }
7547
7548 /* Copy fi.typedef_field_list linked list elements content into the
7549 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7550 if (fi.typedef_field_list)
7551 {
7552 int i = fi.typedef_field_list_count;
7553
7554 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7555 TYPE_TYPEDEF_FIELD_ARRAY (type)
7556 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7557 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7558
7559 /* Reverse the list order to keep the debug info elements order. */
7560 while (--i >= 0)
7561 {
7562 struct typedef_field *dest, *src;
7563
7564 dest = &TYPE_TYPEDEF_FIELD (type, i);
7565 src = &fi.typedef_field_list->field;
7566 fi.typedef_field_list = fi.typedef_field_list->next;
7567 *dest = *src;
7568 }
7569 }
7570
7571 do_cleanups (back_to);
7572
7573 if (HAVE_CPLUS_STRUCT (type))
7574 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7575 }
7576
7577 quirk_gcc_member_function_pointer (type, cu->objfile);
7578
7579 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7580 snapshots) has been known to create a die giving a declaration
7581 for a class that has, as a child, a die giving a definition for a
7582 nested class. So we have to process our children even if the
7583 current die is a declaration. Normally, of course, a declaration
7584 won't have any children at all. */
7585
7586 while (child_die != NULL && child_die->tag)
7587 {
7588 if (child_die->tag == DW_TAG_member
7589 || child_die->tag == DW_TAG_variable
7590 || child_die->tag == DW_TAG_inheritance
7591 || child_die->tag == DW_TAG_template_value_param
7592 || child_die->tag == DW_TAG_template_type_param)
7593 {
7594 /* Do nothing. */
7595 }
7596 else
7597 process_die (child_die, cu);
7598
7599 child_die = sibling_die (child_die);
7600 }
7601
7602 /* Do not consider external references. According to the DWARF standard,
7603 these DIEs are identified by the fact that they have no byte_size
7604 attribute, and a declaration attribute. */
7605 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7606 || !die_is_declaration (die, cu))
7607 new_symbol (die, type, cu);
7608 }
7609
7610 /* Given a DW_AT_enumeration_type die, set its type. We do not
7611 complete the type's fields yet, or create any symbols. */
7612
7613 static struct type *
7614 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7615 {
7616 struct objfile *objfile = cu->objfile;
7617 struct type *type;
7618 struct attribute *attr;
7619 const char *name;
7620
7621 /* If the definition of this type lives in .debug_types, read that type.
7622 Don't follow DW_AT_specification though, that will take us back up
7623 the chain and we want to go down. */
7624 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7625 if (attr)
7626 {
7627 struct dwarf2_cu *type_cu = cu;
7628 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7629
7630 type = read_type_die (type_die, type_cu);
7631
7632 /* TYPE_CU may not be the same as CU.
7633 Ensure TYPE is recorded in CU's type_hash table. */
7634 return set_die_type (die, type, cu);
7635 }
7636
7637 type = alloc_type (objfile);
7638
7639 TYPE_CODE (type) = TYPE_CODE_ENUM;
7640 name = dwarf2_full_name (NULL, die, cu);
7641 if (name != NULL)
7642 TYPE_TAG_NAME (type) = (char *) name;
7643
7644 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7645 if (attr)
7646 {
7647 TYPE_LENGTH (type) = DW_UNSND (attr);
7648 }
7649 else
7650 {
7651 TYPE_LENGTH (type) = 0;
7652 }
7653
7654 /* The enumeration DIE can be incomplete. In Ada, any type can be
7655 declared as private in the package spec, and then defined only
7656 inside the package body. Such types are known as Taft Amendment
7657 Types. When another package uses such a type, an incomplete DIE
7658 may be generated by the compiler. */
7659 if (die_is_declaration (die, cu))
7660 TYPE_STUB (type) = 1;
7661
7662 return set_die_type (die, type, cu);
7663 }
7664
7665 /* Given a pointer to a die which begins an enumeration, process all
7666 the dies that define the members of the enumeration, and create the
7667 symbol for the enumeration type.
7668
7669 NOTE: We reverse the order of the element list. */
7670
7671 static void
7672 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7673 {
7674 struct type *this_type;
7675
7676 this_type = get_die_type (die, cu);
7677 if (this_type == NULL)
7678 this_type = read_enumeration_type (die, cu);
7679
7680 if (die->child != NULL)
7681 {
7682 struct die_info *child_die;
7683 struct symbol *sym;
7684 struct field *fields = NULL;
7685 int num_fields = 0;
7686 int unsigned_enum = 1;
7687 char *name;
7688
7689 child_die = die->child;
7690 while (child_die && child_die->tag)
7691 {
7692 if (child_die->tag != DW_TAG_enumerator)
7693 {
7694 process_die (child_die, cu);
7695 }
7696 else
7697 {
7698 name = dwarf2_name (child_die, cu);
7699 if (name)
7700 {
7701 sym = new_symbol (child_die, this_type, cu);
7702 if (SYMBOL_VALUE (sym) < 0)
7703 unsigned_enum = 0;
7704
7705 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7706 {
7707 fields = (struct field *)
7708 xrealloc (fields,
7709 (num_fields + DW_FIELD_ALLOC_CHUNK)
7710 * sizeof (struct field));
7711 }
7712
7713 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7714 FIELD_TYPE (fields[num_fields]) = NULL;
7715 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7716 FIELD_BITSIZE (fields[num_fields]) = 0;
7717
7718 num_fields++;
7719 }
7720 }
7721
7722 child_die = sibling_die (child_die);
7723 }
7724
7725 if (num_fields)
7726 {
7727 TYPE_NFIELDS (this_type) = num_fields;
7728 TYPE_FIELDS (this_type) = (struct field *)
7729 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7730 memcpy (TYPE_FIELDS (this_type), fields,
7731 sizeof (struct field) * num_fields);
7732 xfree (fields);
7733 }
7734 if (unsigned_enum)
7735 TYPE_UNSIGNED (this_type) = 1;
7736 }
7737
7738 /* If we are reading an enum from a .debug_types unit, and the enum
7739 is a declaration, and the enum is not the signatured type in the
7740 unit, then we do not want to add a symbol for it. Adding a
7741 symbol would in some cases obscure the true definition of the
7742 enum, giving users an incomplete type when the definition is
7743 actually available. Note that we do not want to do this for all
7744 enums which are just declarations, because C++0x allows forward
7745 enum declarations. */
7746 if (cu->per_cu->debug_type_section
7747 && die_is_declaration (die, cu))
7748 {
7749 struct signatured_type *type_sig;
7750
7751 type_sig
7752 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7753 cu->per_cu->debug_type_section,
7754 cu->per_cu->offset);
7755 if (type_sig->type_offset != die->offset)
7756 return;
7757 }
7758
7759 new_symbol (die, this_type, cu);
7760 }
7761
7762 /* Extract all information from a DW_TAG_array_type DIE and put it in
7763 the DIE's type field. For now, this only handles one dimensional
7764 arrays. */
7765
7766 static struct type *
7767 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7768 {
7769 struct objfile *objfile = cu->objfile;
7770 struct die_info *child_die;
7771 struct type *type;
7772 struct type *element_type, *range_type, *index_type;
7773 struct type **range_types = NULL;
7774 struct attribute *attr;
7775 int ndim = 0;
7776 struct cleanup *back_to;
7777 char *name;
7778
7779 element_type = die_type (die, cu);
7780
7781 /* The die_type call above may have already set the type for this DIE. */
7782 type = get_die_type (die, cu);
7783 if (type)
7784 return type;
7785
7786 /* Irix 6.2 native cc creates array types without children for
7787 arrays with unspecified length. */
7788 if (die->child == NULL)
7789 {
7790 index_type = objfile_type (objfile)->builtin_int;
7791 range_type = create_range_type (NULL, index_type, 0, -1);
7792 type = create_array_type (NULL, element_type, range_type);
7793 return set_die_type (die, type, cu);
7794 }
7795
7796 back_to = make_cleanup (null_cleanup, NULL);
7797 child_die = die->child;
7798 while (child_die && child_die->tag)
7799 {
7800 if (child_die->tag == DW_TAG_subrange_type)
7801 {
7802 struct type *child_type = read_type_die (child_die, cu);
7803
7804 if (child_type != NULL)
7805 {
7806 /* The range type was succesfully read. Save it for the
7807 array type creation. */
7808 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7809 {
7810 range_types = (struct type **)
7811 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7812 * sizeof (struct type *));
7813 if (ndim == 0)
7814 make_cleanup (free_current_contents, &range_types);
7815 }
7816 range_types[ndim++] = child_type;
7817 }
7818 }
7819 child_die = sibling_die (child_die);
7820 }
7821
7822 /* Dwarf2 dimensions are output from left to right, create the
7823 necessary array types in backwards order. */
7824
7825 type = element_type;
7826
7827 if (read_array_order (die, cu) == DW_ORD_col_major)
7828 {
7829 int i = 0;
7830
7831 while (i < ndim)
7832 type = create_array_type (NULL, type, range_types[i++]);
7833 }
7834 else
7835 {
7836 while (ndim-- > 0)
7837 type = create_array_type (NULL, type, range_types[ndim]);
7838 }
7839
7840 /* Understand Dwarf2 support for vector types (like they occur on
7841 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7842 array type. This is not part of the Dwarf2/3 standard yet, but a
7843 custom vendor extension. The main difference between a regular
7844 array and the vector variant is that vectors are passed by value
7845 to functions. */
7846 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7847 if (attr)
7848 make_vector_type (type);
7849
7850 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7851 implementation may choose to implement triple vectors using this
7852 attribute. */
7853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7854 if (attr)
7855 {
7856 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7857 TYPE_LENGTH (type) = DW_UNSND (attr);
7858 else
7859 complaint (&symfile_complaints,
7860 _("DW_AT_byte_size for array type smaller "
7861 "than the total size of elements"));
7862 }
7863
7864 name = dwarf2_name (die, cu);
7865 if (name)
7866 TYPE_NAME (type) = name;
7867
7868 /* Install the type in the die. */
7869 set_die_type (die, type, cu);
7870
7871 /* set_die_type should be already done. */
7872 set_descriptive_type (type, die, cu);
7873
7874 do_cleanups (back_to);
7875
7876 return type;
7877 }
7878
7879 static enum dwarf_array_dim_ordering
7880 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7881 {
7882 struct attribute *attr;
7883
7884 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7885
7886 if (attr) return DW_SND (attr);
7887
7888 /* GNU F77 is a special case, as at 08/2004 array type info is the
7889 opposite order to the dwarf2 specification, but data is still
7890 laid out as per normal fortran.
7891
7892 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7893 version checking. */
7894
7895 if (cu->language == language_fortran
7896 && cu->producer && strstr (cu->producer, "GNU F77"))
7897 {
7898 return DW_ORD_row_major;
7899 }
7900
7901 switch (cu->language_defn->la_array_ordering)
7902 {
7903 case array_column_major:
7904 return DW_ORD_col_major;
7905 case array_row_major:
7906 default:
7907 return DW_ORD_row_major;
7908 };
7909 }
7910
7911 /* Extract all information from a DW_TAG_set_type DIE and put it in
7912 the DIE's type field. */
7913
7914 static struct type *
7915 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7916 {
7917 struct type *domain_type, *set_type;
7918 struct attribute *attr;
7919
7920 domain_type = die_type (die, cu);
7921
7922 /* The die_type call above may have already set the type for this DIE. */
7923 set_type = get_die_type (die, cu);
7924 if (set_type)
7925 return set_type;
7926
7927 set_type = create_set_type (NULL, domain_type);
7928
7929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7930 if (attr)
7931 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7932
7933 return set_die_type (die, set_type, cu);
7934 }
7935
7936 /* First cut: install each common block member as a global variable. */
7937
7938 static void
7939 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7940 {
7941 struct die_info *child_die;
7942 struct attribute *attr;
7943 struct symbol *sym;
7944 CORE_ADDR base = (CORE_ADDR) 0;
7945
7946 attr = dwarf2_attr (die, DW_AT_location, cu);
7947 if (attr)
7948 {
7949 /* Support the .debug_loc offsets. */
7950 if (attr_form_is_block (attr))
7951 {
7952 base = decode_locdesc (DW_BLOCK (attr), cu);
7953 }
7954 else if (attr_form_is_section_offset (attr))
7955 {
7956 dwarf2_complex_location_expr_complaint ();
7957 }
7958 else
7959 {
7960 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7961 "common block member");
7962 }
7963 }
7964 if (die->child != NULL)
7965 {
7966 child_die = die->child;
7967 while (child_die && child_die->tag)
7968 {
7969 LONGEST offset;
7970
7971 sym = new_symbol (child_die, NULL, cu);
7972 if (sym != NULL
7973 && handle_data_member_location (child_die, cu, &offset))
7974 {
7975 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7976 add_symbol_to_list (sym, &global_symbols);
7977 }
7978 child_die = sibling_die (child_die);
7979 }
7980 }
7981 }
7982
7983 /* Create a type for a C++ namespace. */
7984
7985 static struct type *
7986 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7987 {
7988 struct objfile *objfile = cu->objfile;
7989 const char *previous_prefix, *name;
7990 int is_anonymous;
7991 struct type *type;
7992
7993 /* For extensions, reuse the type of the original namespace. */
7994 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7995 {
7996 struct die_info *ext_die;
7997 struct dwarf2_cu *ext_cu = cu;
7998
7999 ext_die = dwarf2_extension (die, &ext_cu);
8000 type = read_type_die (ext_die, ext_cu);
8001
8002 /* EXT_CU may not be the same as CU.
8003 Ensure TYPE is recorded in CU's type_hash table. */
8004 return set_die_type (die, type, cu);
8005 }
8006
8007 name = namespace_name (die, &is_anonymous, cu);
8008
8009 /* Now build the name of the current namespace. */
8010
8011 previous_prefix = determine_prefix (die, cu);
8012 if (previous_prefix[0] != '\0')
8013 name = typename_concat (&objfile->objfile_obstack,
8014 previous_prefix, name, 0, cu);
8015
8016 /* Create the type. */
8017 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8018 objfile);
8019 TYPE_NAME (type) = (char *) name;
8020 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8021
8022 return set_die_type (die, type, cu);
8023 }
8024
8025 /* Read a C++ namespace. */
8026
8027 static void
8028 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8029 {
8030 struct objfile *objfile = cu->objfile;
8031 int is_anonymous;
8032
8033 /* Add a symbol associated to this if we haven't seen the namespace
8034 before. Also, add a using directive if it's an anonymous
8035 namespace. */
8036
8037 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8038 {
8039 struct type *type;
8040
8041 type = read_type_die (die, cu);
8042 new_symbol (die, type, cu);
8043
8044 namespace_name (die, &is_anonymous, cu);
8045 if (is_anonymous)
8046 {
8047 const char *previous_prefix = determine_prefix (die, cu);
8048
8049 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8050 NULL, NULL, &objfile->objfile_obstack);
8051 }
8052 }
8053
8054 if (die->child != NULL)
8055 {
8056 struct die_info *child_die = die->child;
8057
8058 while (child_die && child_die->tag)
8059 {
8060 process_die (child_die, cu);
8061 child_die = sibling_die (child_die);
8062 }
8063 }
8064 }
8065
8066 /* Read a Fortran module as type. This DIE can be only a declaration used for
8067 imported module. Still we need that type as local Fortran "use ... only"
8068 declaration imports depend on the created type in determine_prefix. */
8069
8070 static struct type *
8071 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8072 {
8073 struct objfile *objfile = cu->objfile;
8074 char *module_name;
8075 struct type *type;
8076
8077 module_name = dwarf2_name (die, cu);
8078 if (!module_name)
8079 complaint (&symfile_complaints,
8080 _("DW_TAG_module has no name, offset 0x%x"),
8081 die->offset);
8082 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8083
8084 /* determine_prefix uses TYPE_TAG_NAME. */
8085 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8086
8087 return set_die_type (die, type, cu);
8088 }
8089
8090 /* Read a Fortran module. */
8091
8092 static void
8093 read_module (struct die_info *die, struct dwarf2_cu *cu)
8094 {
8095 struct die_info *child_die = die->child;
8096
8097 while (child_die && child_die->tag)
8098 {
8099 process_die (child_die, cu);
8100 child_die = sibling_die (child_die);
8101 }
8102 }
8103
8104 /* Return the name of the namespace represented by DIE. Set
8105 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8106 namespace. */
8107
8108 static const char *
8109 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8110 {
8111 struct die_info *current_die;
8112 const char *name = NULL;
8113
8114 /* Loop through the extensions until we find a name. */
8115
8116 for (current_die = die;
8117 current_die != NULL;
8118 current_die = dwarf2_extension (die, &cu))
8119 {
8120 name = dwarf2_name (current_die, cu);
8121 if (name != NULL)
8122 break;
8123 }
8124
8125 /* Is it an anonymous namespace? */
8126
8127 *is_anonymous = (name == NULL);
8128 if (*is_anonymous)
8129 name = CP_ANONYMOUS_NAMESPACE_STR;
8130
8131 return name;
8132 }
8133
8134 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8135 the user defined type vector. */
8136
8137 static struct type *
8138 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8139 {
8140 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8141 struct comp_unit_head *cu_header = &cu->header;
8142 struct type *type;
8143 struct attribute *attr_byte_size;
8144 struct attribute *attr_address_class;
8145 int byte_size, addr_class;
8146 struct type *target_type;
8147
8148 target_type = die_type (die, cu);
8149
8150 /* The die_type call above may have already set the type for this DIE. */
8151 type = get_die_type (die, cu);
8152 if (type)
8153 return type;
8154
8155 type = lookup_pointer_type (target_type);
8156
8157 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8158 if (attr_byte_size)
8159 byte_size = DW_UNSND (attr_byte_size);
8160 else
8161 byte_size = cu_header->addr_size;
8162
8163 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8164 if (attr_address_class)
8165 addr_class = DW_UNSND (attr_address_class);
8166 else
8167 addr_class = DW_ADDR_none;
8168
8169 /* If the pointer size or address class is different than the
8170 default, create a type variant marked as such and set the
8171 length accordingly. */
8172 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8173 {
8174 if (gdbarch_address_class_type_flags_p (gdbarch))
8175 {
8176 int type_flags;
8177
8178 type_flags = gdbarch_address_class_type_flags
8179 (gdbarch, byte_size, addr_class);
8180 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8181 == 0);
8182 type = make_type_with_address_space (type, type_flags);
8183 }
8184 else if (TYPE_LENGTH (type) != byte_size)
8185 {
8186 complaint (&symfile_complaints,
8187 _("invalid pointer size %d"), byte_size);
8188 }
8189 else
8190 {
8191 /* Should we also complain about unhandled address classes? */
8192 }
8193 }
8194
8195 TYPE_LENGTH (type) = byte_size;
8196 return set_die_type (die, type, cu);
8197 }
8198
8199 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8200 the user defined type vector. */
8201
8202 static struct type *
8203 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8204 {
8205 struct type *type;
8206 struct type *to_type;
8207 struct type *domain;
8208
8209 to_type = die_type (die, cu);
8210 domain = die_containing_type (die, cu);
8211
8212 /* The calls above may have already set the type for this DIE. */
8213 type = get_die_type (die, cu);
8214 if (type)
8215 return type;
8216
8217 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8218 type = lookup_methodptr_type (to_type);
8219 else
8220 type = lookup_memberptr_type (to_type, domain);
8221
8222 return set_die_type (die, type, cu);
8223 }
8224
8225 /* Extract all information from a DW_TAG_reference_type DIE and add to
8226 the user defined type vector. */
8227
8228 static struct type *
8229 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8230 {
8231 struct comp_unit_head *cu_header = &cu->header;
8232 struct type *type, *target_type;
8233 struct attribute *attr;
8234
8235 target_type = die_type (die, cu);
8236
8237 /* The die_type call above may have already set the type for this DIE. */
8238 type = get_die_type (die, cu);
8239 if (type)
8240 return type;
8241
8242 type = lookup_reference_type (target_type);
8243 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8244 if (attr)
8245 {
8246 TYPE_LENGTH (type) = DW_UNSND (attr);
8247 }
8248 else
8249 {
8250 TYPE_LENGTH (type) = cu_header->addr_size;
8251 }
8252 return set_die_type (die, type, cu);
8253 }
8254
8255 static struct type *
8256 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8257 {
8258 struct type *base_type, *cv_type;
8259
8260 base_type = die_type (die, cu);
8261
8262 /* The die_type call above may have already set the type for this DIE. */
8263 cv_type = get_die_type (die, cu);
8264 if (cv_type)
8265 return cv_type;
8266
8267 /* In case the const qualifier is applied to an array type, the element type
8268 is so qualified, not the array type (section 6.7.3 of C99). */
8269 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8270 {
8271 struct type *el_type, *inner_array;
8272
8273 base_type = copy_type (base_type);
8274 inner_array = base_type;
8275
8276 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8277 {
8278 TYPE_TARGET_TYPE (inner_array) =
8279 copy_type (TYPE_TARGET_TYPE (inner_array));
8280 inner_array = TYPE_TARGET_TYPE (inner_array);
8281 }
8282
8283 el_type = TYPE_TARGET_TYPE (inner_array);
8284 TYPE_TARGET_TYPE (inner_array) =
8285 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8286
8287 return set_die_type (die, base_type, cu);
8288 }
8289
8290 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8291 return set_die_type (die, cv_type, cu);
8292 }
8293
8294 static struct type *
8295 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8296 {
8297 struct type *base_type, *cv_type;
8298
8299 base_type = die_type (die, cu);
8300
8301 /* The die_type call above may have already set the type for this DIE. */
8302 cv_type = get_die_type (die, cu);
8303 if (cv_type)
8304 return cv_type;
8305
8306 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8307 return set_die_type (die, cv_type, cu);
8308 }
8309
8310 /* Extract all information from a DW_TAG_string_type DIE and add to
8311 the user defined type vector. It isn't really a user defined type,
8312 but it behaves like one, with other DIE's using an AT_user_def_type
8313 attribute to reference it. */
8314
8315 static struct type *
8316 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8317 {
8318 struct objfile *objfile = cu->objfile;
8319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8320 struct type *type, *range_type, *index_type, *char_type;
8321 struct attribute *attr;
8322 unsigned int length;
8323
8324 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8325 if (attr)
8326 {
8327 length = DW_UNSND (attr);
8328 }
8329 else
8330 {
8331 /* Check for the DW_AT_byte_size attribute. */
8332 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8333 if (attr)
8334 {
8335 length = DW_UNSND (attr);
8336 }
8337 else
8338 {
8339 length = 1;
8340 }
8341 }
8342
8343 index_type = objfile_type (objfile)->builtin_int;
8344 range_type = create_range_type (NULL, index_type, 1, length);
8345 char_type = language_string_char_type (cu->language_defn, gdbarch);
8346 type = create_string_type (NULL, char_type, range_type);
8347
8348 return set_die_type (die, type, cu);
8349 }
8350
8351 /* Handle DIES due to C code like:
8352
8353 struct foo
8354 {
8355 int (*funcp)(int a, long l);
8356 int b;
8357 };
8358
8359 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8360
8361 static struct type *
8362 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8363 {
8364 struct type *type; /* Type that this function returns. */
8365 struct type *ftype; /* Function that returns above type. */
8366 struct attribute *attr;
8367
8368 type = die_type (die, cu);
8369
8370 /* The die_type call above may have already set the type for this DIE. */
8371 ftype = get_die_type (die, cu);
8372 if (ftype)
8373 return ftype;
8374
8375 ftype = lookup_function_type (type);
8376
8377 /* All functions in C++, Pascal and Java have prototypes. */
8378 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8379 if ((attr && (DW_UNSND (attr) != 0))
8380 || cu->language == language_cplus
8381 || cu->language == language_java
8382 || cu->language == language_pascal)
8383 TYPE_PROTOTYPED (ftype) = 1;
8384 else if (producer_is_realview (cu->producer))
8385 /* RealView does not emit DW_AT_prototyped. We can not
8386 distinguish prototyped and unprototyped functions; default to
8387 prototyped, since that is more common in modern code (and
8388 RealView warns about unprototyped functions). */
8389 TYPE_PROTOTYPED (ftype) = 1;
8390
8391 /* Store the calling convention in the type if it's available in
8392 the subroutine die. Otherwise set the calling convention to
8393 the default value DW_CC_normal. */
8394 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8395 if (attr)
8396 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8397 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8398 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8399 else
8400 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8401
8402 /* We need to add the subroutine type to the die immediately so
8403 we don't infinitely recurse when dealing with parameters
8404 declared as the same subroutine type. */
8405 set_die_type (die, ftype, cu);
8406
8407 if (die->child != NULL)
8408 {
8409 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8410 struct die_info *child_die;
8411 int nparams, iparams;
8412
8413 /* Count the number of parameters.
8414 FIXME: GDB currently ignores vararg functions, but knows about
8415 vararg member functions. */
8416 nparams = 0;
8417 child_die = die->child;
8418 while (child_die && child_die->tag)
8419 {
8420 if (child_die->tag == DW_TAG_formal_parameter)
8421 nparams++;
8422 else if (child_die->tag == DW_TAG_unspecified_parameters)
8423 TYPE_VARARGS (ftype) = 1;
8424 child_die = sibling_die (child_die);
8425 }
8426
8427 /* Allocate storage for parameters and fill them in. */
8428 TYPE_NFIELDS (ftype) = nparams;
8429 TYPE_FIELDS (ftype) = (struct field *)
8430 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8431
8432 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8433 even if we error out during the parameters reading below. */
8434 for (iparams = 0; iparams < nparams; iparams++)
8435 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8436
8437 iparams = 0;
8438 child_die = die->child;
8439 while (child_die && child_die->tag)
8440 {
8441 if (child_die->tag == DW_TAG_formal_parameter)
8442 {
8443 struct type *arg_type;
8444
8445 /* DWARF version 2 has no clean way to discern C++
8446 static and non-static member functions. G++ helps
8447 GDB by marking the first parameter for non-static
8448 member functions (which is the this pointer) as
8449 artificial. We pass this information to
8450 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8451
8452 DWARF version 3 added DW_AT_object_pointer, which GCC
8453 4.5 does not yet generate. */
8454 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8455 if (attr)
8456 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8457 else
8458 {
8459 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8460
8461 /* GCC/43521: In java, the formal parameter
8462 "this" is sometimes not marked with DW_AT_artificial. */
8463 if (cu->language == language_java)
8464 {
8465 const char *name = dwarf2_name (child_die, cu);
8466
8467 if (name && !strcmp (name, "this"))
8468 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8469 }
8470 }
8471 arg_type = die_type (child_die, cu);
8472
8473 /* RealView does not mark THIS as const, which the testsuite
8474 expects. GCC marks THIS as const in method definitions,
8475 but not in the class specifications (GCC PR 43053). */
8476 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8477 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8478 {
8479 int is_this = 0;
8480 struct dwarf2_cu *arg_cu = cu;
8481 const char *name = dwarf2_name (child_die, cu);
8482
8483 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8484 if (attr)
8485 {
8486 /* If the compiler emits this, use it. */
8487 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8488 is_this = 1;
8489 }
8490 else if (name && strcmp (name, "this") == 0)
8491 /* Function definitions will have the argument names. */
8492 is_this = 1;
8493 else if (name == NULL && iparams == 0)
8494 /* Declarations may not have the names, so like
8495 elsewhere in GDB, assume an artificial first
8496 argument is "this". */
8497 is_this = 1;
8498
8499 if (is_this)
8500 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8501 arg_type, 0);
8502 }
8503
8504 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8505 iparams++;
8506 }
8507 child_die = sibling_die (child_die);
8508 }
8509 }
8510
8511 return ftype;
8512 }
8513
8514 static struct type *
8515 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8516 {
8517 struct objfile *objfile = cu->objfile;
8518 const char *name = NULL;
8519 struct type *this_type;
8520
8521 name = dwarf2_full_name (NULL, die, cu);
8522 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8523 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8524 TYPE_NAME (this_type) = (char *) name;
8525 set_die_type (die, this_type, cu);
8526 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8527 return this_type;
8528 }
8529
8530 /* Find a representation of a given base type and install
8531 it in the TYPE field of the die. */
8532
8533 static struct type *
8534 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8535 {
8536 struct objfile *objfile = cu->objfile;
8537 struct type *type;
8538 struct attribute *attr;
8539 int encoding = 0, size = 0;
8540 char *name;
8541 enum type_code code = TYPE_CODE_INT;
8542 int type_flags = 0;
8543 struct type *target_type = NULL;
8544
8545 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8546 if (attr)
8547 {
8548 encoding = DW_UNSND (attr);
8549 }
8550 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8551 if (attr)
8552 {
8553 size = DW_UNSND (attr);
8554 }
8555 name = dwarf2_name (die, cu);
8556 if (!name)
8557 {
8558 complaint (&symfile_complaints,
8559 _("DW_AT_name missing from DW_TAG_base_type"));
8560 }
8561
8562 switch (encoding)
8563 {
8564 case DW_ATE_address:
8565 /* Turn DW_ATE_address into a void * pointer. */
8566 code = TYPE_CODE_PTR;
8567 type_flags |= TYPE_FLAG_UNSIGNED;
8568 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8569 break;
8570 case DW_ATE_boolean:
8571 code = TYPE_CODE_BOOL;
8572 type_flags |= TYPE_FLAG_UNSIGNED;
8573 break;
8574 case DW_ATE_complex_float:
8575 code = TYPE_CODE_COMPLEX;
8576 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8577 break;
8578 case DW_ATE_decimal_float:
8579 code = TYPE_CODE_DECFLOAT;
8580 break;
8581 case DW_ATE_float:
8582 code = TYPE_CODE_FLT;
8583 break;
8584 case DW_ATE_signed:
8585 break;
8586 case DW_ATE_unsigned:
8587 type_flags |= TYPE_FLAG_UNSIGNED;
8588 if (cu->language == language_fortran
8589 && name
8590 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8591 code = TYPE_CODE_CHAR;
8592 break;
8593 case DW_ATE_signed_char:
8594 if (cu->language == language_ada || cu->language == language_m2
8595 || cu->language == language_pascal
8596 || cu->language == language_fortran)
8597 code = TYPE_CODE_CHAR;
8598 break;
8599 case DW_ATE_unsigned_char:
8600 if (cu->language == language_ada || cu->language == language_m2
8601 || cu->language == language_pascal
8602 || cu->language == language_fortran)
8603 code = TYPE_CODE_CHAR;
8604 type_flags |= TYPE_FLAG_UNSIGNED;
8605 break;
8606 case DW_ATE_UTF:
8607 /* We just treat this as an integer and then recognize the
8608 type by name elsewhere. */
8609 break;
8610
8611 default:
8612 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8613 dwarf_type_encoding_name (encoding));
8614 break;
8615 }
8616
8617 type = init_type (code, size, type_flags, NULL, objfile);
8618 TYPE_NAME (type) = name;
8619 TYPE_TARGET_TYPE (type) = target_type;
8620
8621 if (name && strcmp (name, "char") == 0)
8622 TYPE_NOSIGN (type) = 1;
8623
8624 return set_die_type (die, type, cu);
8625 }
8626
8627 /* Read the given DW_AT_subrange DIE. */
8628
8629 static struct type *
8630 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8631 {
8632 struct type *base_type;
8633 struct type *range_type;
8634 struct attribute *attr;
8635 LONGEST low = 0;
8636 LONGEST high = -1;
8637 char *name;
8638 LONGEST negative_mask;
8639
8640 base_type = die_type (die, cu);
8641 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8642 check_typedef (base_type);
8643
8644 /* The die_type call above may have already set the type for this DIE. */
8645 range_type = get_die_type (die, cu);
8646 if (range_type)
8647 return range_type;
8648
8649 if (cu->language == language_fortran)
8650 {
8651 /* FORTRAN implies a lower bound of 1, if not given. */
8652 low = 1;
8653 }
8654
8655 /* FIXME: For variable sized arrays either of these could be
8656 a variable rather than a constant value. We'll allow it,
8657 but we don't know how to handle it. */
8658 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8659 if (attr)
8660 low = dwarf2_get_attr_constant_value (attr, 0);
8661
8662 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8663 if (attr)
8664 {
8665 if (attr_form_is_block (attr) || is_ref_attr (attr))
8666 {
8667 /* GCC encodes arrays with unspecified or dynamic length
8668 with a DW_FORM_block1 attribute or a reference attribute.
8669 FIXME: GDB does not yet know how to handle dynamic
8670 arrays properly, treat them as arrays with unspecified
8671 length for now.
8672
8673 FIXME: jimb/2003-09-22: GDB does not really know
8674 how to handle arrays of unspecified length
8675 either; we just represent them as zero-length
8676 arrays. Choose an appropriate upper bound given
8677 the lower bound we've computed above. */
8678 high = low - 1;
8679 }
8680 else
8681 high = dwarf2_get_attr_constant_value (attr, 1);
8682 }
8683 else
8684 {
8685 attr = dwarf2_attr (die, DW_AT_count, cu);
8686 if (attr)
8687 {
8688 int count = dwarf2_get_attr_constant_value (attr, 1);
8689 high = low + count - 1;
8690 }
8691 else
8692 {
8693 /* Unspecified array length. */
8694 high = low - 1;
8695 }
8696 }
8697
8698 /* Dwarf-2 specifications explicitly allows to create subrange types
8699 without specifying a base type.
8700 In that case, the base type must be set to the type of
8701 the lower bound, upper bound or count, in that order, if any of these
8702 three attributes references an object that has a type.
8703 If no base type is found, the Dwarf-2 specifications say that
8704 a signed integer type of size equal to the size of an address should
8705 be used.
8706 For the following C code: `extern char gdb_int [];'
8707 GCC produces an empty range DIE.
8708 FIXME: muller/2010-05-28: Possible references to object for low bound,
8709 high bound or count are not yet handled by this code. */
8710 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8711 {
8712 struct objfile *objfile = cu->objfile;
8713 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8714 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8715 struct type *int_type = objfile_type (objfile)->builtin_int;
8716
8717 /* Test "int", "long int", and "long long int" objfile types,
8718 and select the first one having a size above or equal to the
8719 architecture address size. */
8720 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8721 base_type = int_type;
8722 else
8723 {
8724 int_type = objfile_type (objfile)->builtin_long;
8725 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8726 base_type = int_type;
8727 else
8728 {
8729 int_type = objfile_type (objfile)->builtin_long_long;
8730 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8731 base_type = int_type;
8732 }
8733 }
8734 }
8735
8736 negative_mask =
8737 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8738 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8739 low |= negative_mask;
8740 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8741 high |= negative_mask;
8742
8743 range_type = create_range_type (NULL, base_type, low, high);
8744
8745 /* Mark arrays with dynamic length at least as an array of unspecified
8746 length. GDB could check the boundary but before it gets implemented at
8747 least allow accessing the array elements. */
8748 if (attr && attr_form_is_block (attr))
8749 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8750
8751 /* Ada expects an empty array on no boundary attributes. */
8752 if (attr == NULL && cu->language != language_ada)
8753 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8754
8755 name = dwarf2_name (die, cu);
8756 if (name)
8757 TYPE_NAME (range_type) = name;
8758
8759 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8760 if (attr)
8761 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8762
8763 set_die_type (die, range_type, cu);
8764
8765 /* set_die_type should be already done. */
8766 set_descriptive_type (range_type, die, cu);
8767
8768 return range_type;
8769 }
8770
8771 static struct type *
8772 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8773 {
8774 struct type *type;
8775
8776 /* For now, we only support the C meaning of an unspecified type: void. */
8777
8778 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8779 TYPE_NAME (type) = dwarf2_name (die, cu);
8780
8781 return set_die_type (die, type, cu);
8782 }
8783
8784 /* Trivial hash function for die_info: the hash value of a DIE
8785 is its offset in .debug_info for this objfile. */
8786
8787 static hashval_t
8788 die_hash (const void *item)
8789 {
8790 const struct die_info *die = item;
8791
8792 return die->offset;
8793 }
8794
8795 /* Trivial comparison function for die_info structures: two DIEs
8796 are equal if they have the same offset. */
8797
8798 static int
8799 die_eq (const void *item_lhs, const void *item_rhs)
8800 {
8801 const struct die_info *die_lhs = item_lhs;
8802 const struct die_info *die_rhs = item_rhs;
8803
8804 return die_lhs->offset == die_rhs->offset;
8805 }
8806
8807 /* Read a whole compilation unit into a linked list of dies. */
8808
8809 static struct die_info *
8810 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8811 {
8812 struct die_reader_specs reader_specs;
8813 int read_abbrevs = 0;
8814 struct cleanup *back_to = NULL;
8815 struct die_info *die;
8816
8817 if (cu->dwarf2_abbrevs == NULL)
8818 {
8819 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8820 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8821 read_abbrevs = 1;
8822 }
8823
8824 gdb_assert (cu->die_hash == NULL);
8825 cu->die_hash
8826 = htab_create_alloc_ex (cu->header.length / 12,
8827 die_hash,
8828 die_eq,
8829 NULL,
8830 &cu->comp_unit_obstack,
8831 hashtab_obstack_allocate,
8832 dummy_obstack_deallocate);
8833
8834 init_cu_die_reader (&reader_specs, cu);
8835
8836 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8837
8838 if (read_abbrevs)
8839 do_cleanups (back_to);
8840
8841 return die;
8842 }
8843
8844 /* Main entry point for reading a DIE and all children.
8845 Read the DIE and dump it if requested. */
8846
8847 static struct die_info *
8848 read_die_and_children (const struct die_reader_specs *reader,
8849 gdb_byte *info_ptr,
8850 gdb_byte **new_info_ptr,
8851 struct die_info *parent)
8852 {
8853 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8854 new_info_ptr, parent);
8855
8856 if (dwarf2_die_debug)
8857 {
8858 fprintf_unfiltered (gdb_stdlog,
8859 "\nRead die from %s of %s:\n",
8860 (reader->cu->per_cu->debug_type_section
8861 ? ".debug_types"
8862 : ".debug_info"),
8863 reader->abfd->filename);
8864 dump_die (result, dwarf2_die_debug);
8865 }
8866
8867 return result;
8868 }
8869
8870 /* Read a single die and all its descendents. Set the die's sibling
8871 field to NULL; set other fields in the die correctly, and set all
8872 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8873 location of the info_ptr after reading all of those dies. PARENT
8874 is the parent of the die in question. */
8875
8876 static struct die_info *
8877 read_die_and_children_1 (const struct die_reader_specs *reader,
8878 gdb_byte *info_ptr,
8879 gdb_byte **new_info_ptr,
8880 struct die_info *parent)
8881 {
8882 struct die_info *die;
8883 gdb_byte *cur_ptr;
8884 int has_children;
8885
8886 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8887 if (die == NULL)
8888 {
8889 *new_info_ptr = cur_ptr;
8890 return NULL;
8891 }
8892 store_in_ref_table (die, reader->cu);
8893
8894 if (has_children)
8895 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8896 else
8897 {
8898 die->child = NULL;
8899 *new_info_ptr = cur_ptr;
8900 }
8901
8902 die->sibling = NULL;
8903 die->parent = parent;
8904 return die;
8905 }
8906
8907 /* Read a die, all of its descendents, and all of its siblings; set
8908 all of the fields of all of the dies correctly. Arguments are as
8909 in read_die_and_children. */
8910
8911 static struct die_info *
8912 read_die_and_siblings (const struct die_reader_specs *reader,
8913 gdb_byte *info_ptr,
8914 gdb_byte **new_info_ptr,
8915 struct die_info *parent)
8916 {
8917 struct die_info *first_die, *last_sibling;
8918 gdb_byte *cur_ptr;
8919
8920 cur_ptr = info_ptr;
8921 first_die = last_sibling = NULL;
8922
8923 while (1)
8924 {
8925 struct die_info *die
8926 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8927
8928 if (die == NULL)
8929 {
8930 *new_info_ptr = cur_ptr;
8931 return first_die;
8932 }
8933
8934 if (!first_die)
8935 first_die = die;
8936 else
8937 last_sibling->sibling = die;
8938
8939 last_sibling = die;
8940 }
8941 }
8942
8943 /* Read the die from the .debug_info section buffer. Set DIEP to
8944 point to a newly allocated die with its information, except for its
8945 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8946 whether the die has children or not. */
8947
8948 static gdb_byte *
8949 read_full_die (const struct die_reader_specs *reader,
8950 struct die_info **diep, gdb_byte *info_ptr,
8951 int *has_children)
8952 {
8953 unsigned int abbrev_number, bytes_read, i, offset;
8954 struct abbrev_info *abbrev;
8955 struct die_info *die;
8956 struct dwarf2_cu *cu = reader->cu;
8957 bfd *abfd = reader->abfd;
8958
8959 offset = info_ptr - reader->buffer;
8960 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8961 info_ptr += bytes_read;
8962 if (!abbrev_number)
8963 {
8964 *diep = NULL;
8965 *has_children = 0;
8966 return info_ptr;
8967 }
8968
8969 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8970 if (!abbrev)
8971 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8972 abbrev_number,
8973 bfd_get_filename (abfd));
8974
8975 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8976 die->offset = offset;
8977 die->tag = abbrev->tag;
8978 die->abbrev = abbrev_number;
8979
8980 die->num_attrs = abbrev->num_attrs;
8981
8982 for (i = 0; i < abbrev->num_attrs; ++i)
8983 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8984 abfd, info_ptr, cu);
8985
8986 *diep = die;
8987 *has_children = abbrev->has_children;
8988 return info_ptr;
8989 }
8990
8991 /* In DWARF version 2, the description of the debugging information is
8992 stored in a separate .debug_abbrev section. Before we read any
8993 dies from a section we read in all abbreviations and install them
8994 in a hash table. This function also sets flags in CU describing
8995 the data found in the abbrev table. */
8996
8997 static void
8998 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8999 {
9000 struct comp_unit_head *cu_header = &cu->header;
9001 gdb_byte *abbrev_ptr;
9002 struct abbrev_info *cur_abbrev;
9003 unsigned int abbrev_number, bytes_read, abbrev_name;
9004 unsigned int abbrev_form, hash_number;
9005 struct attr_abbrev *cur_attrs;
9006 unsigned int allocated_attrs;
9007
9008 /* Initialize dwarf2 abbrevs. */
9009 obstack_init (&cu->abbrev_obstack);
9010 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9011 (ABBREV_HASH_SIZE
9012 * sizeof (struct abbrev_info *)));
9013 memset (cu->dwarf2_abbrevs, 0,
9014 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9015
9016 dwarf2_read_section (dwarf2_per_objfile->objfile,
9017 &dwarf2_per_objfile->abbrev);
9018 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9019 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9020 abbrev_ptr += bytes_read;
9021
9022 allocated_attrs = ATTR_ALLOC_CHUNK;
9023 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9024
9025 /* Loop until we reach an abbrev number of 0. */
9026 while (abbrev_number)
9027 {
9028 cur_abbrev = dwarf_alloc_abbrev (cu);
9029
9030 /* read in abbrev header */
9031 cur_abbrev->number = abbrev_number;
9032 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9033 abbrev_ptr += bytes_read;
9034 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9035 abbrev_ptr += 1;
9036
9037 if (cur_abbrev->tag == DW_TAG_namespace)
9038 cu->has_namespace_info = 1;
9039
9040 /* now read in declarations */
9041 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9042 abbrev_ptr += bytes_read;
9043 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9044 abbrev_ptr += bytes_read;
9045 while (abbrev_name)
9046 {
9047 if (cur_abbrev->num_attrs == allocated_attrs)
9048 {
9049 allocated_attrs += ATTR_ALLOC_CHUNK;
9050 cur_attrs
9051 = xrealloc (cur_attrs, (allocated_attrs
9052 * sizeof (struct attr_abbrev)));
9053 }
9054
9055 /* Record whether this compilation unit might have
9056 inter-compilation-unit references. If we don't know what form
9057 this attribute will have, then it might potentially be a
9058 DW_FORM_ref_addr, so we conservatively expect inter-CU
9059 references. */
9060
9061 if (abbrev_form == DW_FORM_ref_addr
9062 || abbrev_form == DW_FORM_indirect)
9063 cu->has_form_ref_addr = 1;
9064
9065 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9066 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9067 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9068 abbrev_ptr += bytes_read;
9069 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9070 abbrev_ptr += bytes_read;
9071 }
9072
9073 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9074 (cur_abbrev->num_attrs
9075 * sizeof (struct attr_abbrev)));
9076 memcpy (cur_abbrev->attrs, cur_attrs,
9077 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9078
9079 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9080 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9081 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9082
9083 /* Get next abbreviation.
9084 Under Irix6 the abbreviations for a compilation unit are not
9085 always properly terminated with an abbrev number of 0.
9086 Exit loop if we encounter an abbreviation which we have
9087 already read (which means we are about to read the abbreviations
9088 for the next compile unit) or if the end of the abbreviation
9089 table is reached. */
9090 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9091 >= dwarf2_per_objfile->abbrev.size)
9092 break;
9093 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9094 abbrev_ptr += bytes_read;
9095 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9096 break;
9097 }
9098
9099 xfree (cur_attrs);
9100 }
9101
9102 /* Release the memory used by the abbrev table for a compilation unit. */
9103
9104 static void
9105 dwarf2_free_abbrev_table (void *ptr_to_cu)
9106 {
9107 struct dwarf2_cu *cu = ptr_to_cu;
9108
9109 obstack_free (&cu->abbrev_obstack, NULL);
9110 cu->dwarf2_abbrevs = NULL;
9111 }
9112
9113 /* Lookup an abbrev_info structure in the abbrev hash table. */
9114
9115 static struct abbrev_info *
9116 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9117 {
9118 unsigned int hash_number;
9119 struct abbrev_info *abbrev;
9120
9121 hash_number = number % ABBREV_HASH_SIZE;
9122 abbrev = cu->dwarf2_abbrevs[hash_number];
9123
9124 while (abbrev)
9125 {
9126 if (abbrev->number == number)
9127 return abbrev;
9128 else
9129 abbrev = abbrev->next;
9130 }
9131 return NULL;
9132 }
9133
9134 /* Returns nonzero if TAG represents a type that we might generate a partial
9135 symbol for. */
9136
9137 static int
9138 is_type_tag_for_partial (int tag)
9139 {
9140 switch (tag)
9141 {
9142 #if 0
9143 /* Some types that would be reasonable to generate partial symbols for,
9144 that we don't at present. */
9145 case DW_TAG_array_type:
9146 case DW_TAG_file_type:
9147 case DW_TAG_ptr_to_member_type:
9148 case DW_TAG_set_type:
9149 case DW_TAG_string_type:
9150 case DW_TAG_subroutine_type:
9151 #endif
9152 case DW_TAG_base_type:
9153 case DW_TAG_class_type:
9154 case DW_TAG_interface_type:
9155 case DW_TAG_enumeration_type:
9156 case DW_TAG_structure_type:
9157 case DW_TAG_subrange_type:
9158 case DW_TAG_typedef:
9159 case DW_TAG_union_type:
9160 return 1;
9161 default:
9162 return 0;
9163 }
9164 }
9165
9166 /* Load all DIEs that are interesting for partial symbols into memory. */
9167
9168 static struct partial_die_info *
9169 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9170 int building_psymtab, struct dwarf2_cu *cu)
9171 {
9172 struct partial_die_info *part_die;
9173 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9174 struct abbrev_info *abbrev;
9175 unsigned int bytes_read;
9176 unsigned int load_all = 0;
9177
9178 int nesting_level = 1;
9179
9180 parent_die = NULL;
9181 last_die = NULL;
9182
9183 if (cu->per_cu && cu->per_cu->load_all_dies)
9184 load_all = 1;
9185
9186 cu->partial_dies
9187 = htab_create_alloc_ex (cu->header.length / 12,
9188 partial_die_hash,
9189 partial_die_eq,
9190 NULL,
9191 &cu->comp_unit_obstack,
9192 hashtab_obstack_allocate,
9193 dummy_obstack_deallocate);
9194
9195 part_die = obstack_alloc (&cu->comp_unit_obstack,
9196 sizeof (struct partial_die_info));
9197
9198 while (1)
9199 {
9200 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9201
9202 /* A NULL abbrev means the end of a series of children. */
9203 if (abbrev == NULL)
9204 {
9205 if (--nesting_level == 0)
9206 {
9207 /* PART_DIE was probably the last thing allocated on the
9208 comp_unit_obstack, so we could call obstack_free
9209 here. We don't do that because the waste is small,
9210 and will be cleaned up when we're done with this
9211 compilation unit. This way, we're also more robust
9212 against other users of the comp_unit_obstack. */
9213 return first_die;
9214 }
9215 info_ptr += bytes_read;
9216 last_die = parent_die;
9217 parent_die = parent_die->die_parent;
9218 continue;
9219 }
9220
9221 /* Check for template arguments. We never save these; if
9222 they're seen, we just mark the parent, and go on our way. */
9223 if (parent_die != NULL
9224 && cu->language == language_cplus
9225 && (abbrev->tag == DW_TAG_template_type_param
9226 || abbrev->tag == DW_TAG_template_value_param))
9227 {
9228 parent_die->has_template_arguments = 1;
9229
9230 if (!load_all)
9231 {
9232 /* We don't need a partial DIE for the template argument. */
9233 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9234 cu);
9235 continue;
9236 }
9237 }
9238
9239 /* We only recurse into subprograms looking for template arguments.
9240 Skip their other children. */
9241 if (!load_all
9242 && cu->language == language_cplus
9243 && parent_die != NULL
9244 && parent_die->tag == DW_TAG_subprogram)
9245 {
9246 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9247 continue;
9248 }
9249
9250 /* Check whether this DIE is interesting enough to save. Normally
9251 we would not be interested in members here, but there may be
9252 later variables referencing them via DW_AT_specification (for
9253 static members). */
9254 if (!load_all
9255 && !is_type_tag_for_partial (abbrev->tag)
9256 && abbrev->tag != DW_TAG_constant
9257 && abbrev->tag != DW_TAG_enumerator
9258 && abbrev->tag != DW_TAG_subprogram
9259 && abbrev->tag != DW_TAG_lexical_block
9260 && abbrev->tag != DW_TAG_variable
9261 && abbrev->tag != DW_TAG_namespace
9262 && abbrev->tag != DW_TAG_module
9263 && abbrev->tag != DW_TAG_member)
9264 {
9265 /* Otherwise we skip to the next sibling, if any. */
9266 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9267 continue;
9268 }
9269
9270 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9271 buffer, info_ptr, cu);
9272
9273 /* This two-pass algorithm for processing partial symbols has a
9274 high cost in cache pressure. Thus, handle some simple cases
9275 here which cover the majority of C partial symbols. DIEs
9276 which neither have specification tags in them, nor could have
9277 specification tags elsewhere pointing at them, can simply be
9278 processed and discarded.
9279
9280 This segment is also optional; scan_partial_symbols and
9281 add_partial_symbol will handle these DIEs if we chain
9282 them in normally. When compilers which do not emit large
9283 quantities of duplicate debug information are more common,
9284 this code can probably be removed. */
9285
9286 /* Any complete simple types at the top level (pretty much all
9287 of them, for a language without namespaces), can be processed
9288 directly. */
9289 if (parent_die == NULL
9290 && part_die->has_specification == 0
9291 && part_die->is_declaration == 0
9292 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9293 || part_die->tag == DW_TAG_base_type
9294 || part_die->tag == DW_TAG_subrange_type))
9295 {
9296 if (building_psymtab && part_die->name != NULL)
9297 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9298 VAR_DOMAIN, LOC_TYPEDEF,
9299 &cu->objfile->static_psymbols,
9300 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9301 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9302 continue;
9303 }
9304
9305 /* The exception for DW_TAG_typedef with has_children above is
9306 a workaround of GCC PR debug/47510. In the case of this complaint
9307 type_name_no_tag_or_error will error on such types later.
9308
9309 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9310 it could not find the child DIEs referenced later, this is checked
9311 above. In correct DWARF DW_TAG_typedef should have no children. */
9312
9313 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9314 complaint (&symfile_complaints,
9315 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9316 "- DIE at 0x%x [in module %s]"),
9317 part_die->offset, cu->objfile->name);
9318
9319 /* If we're at the second level, and we're an enumerator, and
9320 our parent has no specification (meaning possibly lives in a
9321 namespace elsewhere), then we can add the partial symbol now
9322 instead of queueing it. */
9323 if (part_die->tag == DW_TAG_enumerator
9324 && parent_die != NULL
9325 && parent_die->die_parent == NULL
9326 && parent_die->tag == DW_TAG_enumeration_type
9327 && parent_die->has_specification == 0)
9328 {
9329 if (part_die->name == NULL)
9330 complaint (&symfile_complaints,
9331 _("malformed enumerator DIE ignored"));
9332 else if (building_psymtab)
9333 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9334 VAR_DOMAIN, LOC_CONST,
9335 (cu->language == language_cplus
9336 || cu->language == language_java)
9337 ? &cu->objfile->global_psymbols
9338 : &cu->objfile->static_psymbols,
9339 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9340
9341 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9342 continue;
9343 }
9344
9345 /* We'll save this DIE so link it in. */
9346 part_die->die_parent = parent_die;
9347 part_die->die_sibling = NULL;
9348 part_die->die_child = NULL;
9349
9350 if (last_die && last_die == parent_die)
9351 last_die->die_child = part_die;
9352 else if (last_die)
9353 last_die->die_sibling = part_die;
9354
9355 last_die = part_die;
9356
9357 if (first_die == NULL)
9358 first_die = part_die;
9359
9360 /* Maybe add the DIE to the hash table. Not all DIEs that we
9361 find interesting need to be in the hash table, because we
9362 also have the parent/sibling/child chains; only those that we
9363 might refer to by offset later during partial symbol reading.
9364
9365 For now this means things that might have be the target of a
9366 DW_AT_specification, DW_AT_abstract_origin, or
9367 DW_AT_extension. DW_AT_extension will refer only to
9368 namespaces; DW_AT_abstract_origin refers to functions (and
9369 many things under the function DIE, but we do not recurse
9370 into function DIEs during partial symbol reading) and
9371 possibly variables as well; DW_AT_specification refers to
9372 declarations. Declarations ought to have the DW_AT_declaration
9373 flag. It happens that GCC forgets to put it in sometimes, but
9374 only for functions, not for types.
9375
9376 Adding more things than necessary to the hash table is harmless
9377 except for the performance cost. Adding too few will result in
9378 wasted time in find_partial_die, when we reread the compilation
9379 unit with load_all_dies set. */
9380
9381 if (load_all
9382 || abbrev->tag == DW_TAG_constant
9383 || abbrev->tag == DW_TAG_subprogram
9384 || abbrev->tag == DW_TAG_variable
9385 || abbrev->tag == DW_TAG_namespace
9386 || part_die->is_declaration)
9387 {
9388 void **slot;
9389
9390 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9391 part_die->offset, INSERT);
9392 *slot = part_die;
9393 }
9394
9395 part_die = obstack_alloc (&cu->comp_unit_obstack,
9396 sizeof (struct partial_die_info));
9397
9398 /* For some DIEs we want to follow their children (if any). For C
9399 we have no reason to follow the children of structures; for other
9400 languages we have to, so that we can get at method physnames
9401 to infer fully qualified class names, for DW_AT_specification,
9402 and for C++ template arguments. For C++, we also look one level
9403 inside functions to find template arguments (if the name of the
9404 function does not already contain the template arguments).
9405
9406 For Ada, we need to scan the children of subprograms and lexical
9407 blocks as well because Ada allows the definition of nested
9408 entities that could be interesting for the debugger, such as
9409 nested subprograms for instance. */
9410 if (last_die->has_children
9411 && (load_all
9412 || last_die->tag == DW_TAG_namespace
9413 || last_die->tag == DW_TAG_module
9414 || last_die->tag == DW_TAG_enumeration_type
9415 || (cu->language == language_cplus
9416 && last_die->tag == DW_TAG_subprogram
9417 && (last_die->name == NULL
9418 || strchr (last_die->name, '<') == NULL))
9419 || (cu->language != language_c
9420 && (last_die->tag == DW_TAG_class_type
9421 || last_die->tag == DW_TAG_interface_type
9422 || last_die->tag == DW_TAG_structure_type
9423 || last_die->tag == DW_TAG_union_type))
9424 || (cu->language == language_ada
9425 && (last_die->tag == DW_TAG_subprogram
9426 || last_die->tag == DW_TAG_lexical_block))))
9427 {
9428 nesting_level++;
9429 parent_die = last_die;
9430 continue;
9431 }
9432
9433 /* Otherwise we skip to the next sibling, if any. */
9434 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9435
9436 /* Back to the top, do it again. */
9437 }
9438 }
9439
9440 /* Read a minimal amount of information into the minimal die structure. */
9441
9442 static gdb_byte *
9443 read_partial_die (struct partial_die_info *part_die,
9444 struct abbrev_info *abbrev,
9445 unsigned int abbrev_len, bfd *abfd,
9446 gdb_byte *buffer, gdb_byte *info_ptr,
9447 struct dwarf2_cu *cu)
9448 {
9449 unsigned int i;
9450 struct attribute attr;
9451 int has_low_pc_attr = 0;
9452 int has_high_pc_attr = 0;
9453
9454 memset (part_die, 0, sizeof (struct partial_die_info));
9455
9456 part_die->offset = info_ptr - buffer;
9457
9458 info_ptr += abbrev_len;
9459
9460 if (abbrev == NULL)
9461 return info_ptr;
9462
9463 part_die->tag = abbrev->tag;
9464 part_die->has_children = abbrev->has_children;
9465
9466 for (i = 0; i < abbrev->num_attrs; ++i)
9467 {
9468 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9469
9470 /* Store the data if it is of an attribute we want to keep in a
9471 partial symbol table. */
9472 switch (attr.name)
9473 {
9474 case DW_AT_name:
9475 switch (part_die->tag)
9476 {
9477 case DW_TAG_compile_unit:
9478 case DW_TAG_type_unit:
9479 /* Compilation units have a DW_AT_name that is a filename, not
9480 a source language identifier. */
9481 case DW_TAG_enumeration_type:
9482 case DW_TAG_enumerator:
9483 /* These tags always have simple identifiers already; no need
9484 to canonicalize them. */
9485 part_die->name = DW_STRING (&attr);
9486 break;
9487 default:
9488 part_die->name
9489 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9490 &cu->objfile->objfile_obstack);
9491 break;
9492 }
9493 break;
9494 case DW_AT_linkage_name:
9495 case DW_AT_MIPS_linkage_name:
9496 /* Note that both forms of linkage name might appear. We
9497 assume they will be the same, and we only store the last
9498 one we see. */
9499 if (cu->language == language_ada)
9500 part_die->name = DW_STRING (&attr);
9501 part_die->linkage_name = DW_STRING (&attr);
9502 break;
9503 case DW_AT_low_pc:
9504 has_low_pc_attr = 1;
9505 part_die->lowpc = DW_ADDR (&attr);
9506 break;
9507 case DW_AT_high_pc:
9508 has_high_pc_attr = 1;
9509 part_die->highpc = DW_ADDR (&attr);
9510 break;
9511 case DW_AT_location:
9512 /* Support the .debug_loc offsets. */
9513 if (attr_form_is_block (&attr))
9514 {
9515 part_die->locdesc = DW_BLOCK (&attr);
9516 }
9517 else if (attr_form_is_section_offset (&attr))
9518 {
9519 dwarf2_complex_location_expr_complaint ();
9520 }
9521 else
9522 {
9523 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9524 "partial symbol information");
9525 }
9526 break;
9527 case DW_AT_external:
9528 part_die->is_external = DW_UNSND (&attr);
9529 break;
9530 case DW_AT_declaration:
9531 part_die->is_declaration = DW_UNSND (&attr);
9532 break;
9533 case DW_AT_type:
9534 part_die->has_type = 1;
9535 break;
9536 case DW_AT_abstract_origin:
9537 case DW_AT_specification:
9538 case DW_AT_extension:
9539 part_die->has_specification = 1;
9540 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9541 break;
9542 case DW_AT_sibling:
9543 /* Ignore absolute siblings, they might point outside of
9544 the current compile unit. */
9545 if (attr.form == DW_FORM_ref_addr)
9546 complaint (&symfile_complaints,
9547 _("ignoring absolute DW_AT_sibling"));
9548 else
9549 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9550 break;
9551 case DW_AT_byte_size:
9552 part_die->has_byte_size = 1;
9553 break;
9554 case DW_AT_calling_convention:
9555 /* DWARF doesn't provide a way to identify a program's source-level
9556 entry point. DW_AT_calling_convention attributes are only meant
9557 to describe functions' calling conventions.
9558
9559 However, because it's a necessary piece of information in
9560 Fortran, and because DW_CC_program is the only piece of debugging
9561 information whose definition refers to a 'main program' at all,
9562 several compilers have begun marking Fortran main programs with
9563 DW_CC_program --- even when those functions use the standard
9564 calling conventions.
9565
9566 So until DWARF specifies a way to provide this information and
9567 compilers pick up the new representation, we'll support this
9568 practice. */
9569 if (DW_UNSND (&attr) == DW_CC_program
9570 && cu->language == language_fortran)
9571 {
9572 set_main_name (part_die->name);
9573
9574 /* As this DIE has a static linkage the name would be difficult
9575 to look up later. */
9576 language_of_main = language_fortran;
9577 }
9578 break;
9579 default:
9580 break;
9581 }
9582 }
9583
9584 if (has_low_pc_attr && has_high_pc_attr)
9585 {
9586 /* When using the GNU linker, .gnu.linkonce. sections are used to
9587 eliminate duplicate copies of functions and vtables and such.
9588 The linker will arbitrarily choose one and discard the others.
9589 The AT_*_pc values for such functions refer to local labels in
9590 these sections. If the section from that file was discarded, the
9591 labels are not in the output, so the relocs get a value of 0.
9592 If this is a discarded function, mark the pc bounds as invalid,
9593 so that GDB will ignore it. */
9594 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9595 {
9596 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9597
9598 complaint (&symfile_complaints,
9599 _("DW_AT_low_pc %s is zero "
9600 "for DIE at 0x%x [in module %s]"),
9601 paddress (gdbarch, part_die->lowpc),
9602 part_die->offset, cu->objfile->name);
9603 }
9604 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9605 else if (part_die->lowpc >= part_die->highpc)
9606 {
9607 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9608
9609 complaint (&symfile_complaints,
9610 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9611 "for DIE at 0x%x [in module %s]"),
9612 paddress (gdbarch, part_die->lowpc),
9613 paddress (gdbarch, part_die->highpc),
9614 part_die->offset, cu->objfile->name);
9615 }
9616 else
9617 part_die->has_pc_info = 1;
9618 }
9619
9620 return info_ptr;
9621 }
9622
9623 /* Find a cached partial DIE at OFFSET in CU. */
9624
9625 static struct partial_die_info *
9626 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9627 {
9628 struct partial_die_info *lookup_die = NULL;
9629 struct partial_die_info part_die;
9630
9631 part_die.offset = offset;
9632 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9633
9634 return lookup_die;
9635 }
9636
9637 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9638 except in the case of .debug_types DIEs which do not reference
9639 outside their CU (they do however referencing other types via
9640 DW_FORM_ref_sig8). */
9641
9642 static struct partial_die_info *
9643 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9644 {
9645 struct dwarf2_per_cu_data *per_cu = NULL;
9646 struct partial_die_info *pd = NULL;
9647
9648 if (cu->per_cu->debug_type_section)
9649 {
9650 pd = find_partial_die_in_comp_unit (offset, cu);
9651 if (pd != NULL)
9652 return pd;
9653 goto not_found;
9654 }
9655
9656 if (offset_in_cu_p (&cu->header, offset))
9657 {
9658 pd = find_partial_die_in_comp_unit (offset, cu);
9659 if (pd != NULL)
9660 return pd;
9661 }
9662
9663 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9664
9665 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9666 load_partial_comp_unit (per_cu, cu->objfile);
9667
9668 per_cu->cu->last_used = 0;
9669 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9670
9671 if (pd == NULL && per_cu->load_all_dies == 0)
9672 {
9673 struct cleanup *back_to;
9674 struct partial_die_info comp_unit_die;
9675 struct abbrev_info *abbrev;
9676 unsigned int bytes_read;
9677 char *info_ptr;
9678
9679 per_cu->load_all_dies = 1;
9680
9681 /* Re-read the DIEs. */
9682 back_to = make_cleanup (null_cleanup, 0);
9683 if (per_cu->cu->dwarf2_abbrevs == NULL)
9684 {
9685 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9686 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9687 }
9688 info_ptr = (dwarf2_per_objfile->info.buffer
9689 + per_cu->cu->header.offset
9690 + per_cu->cu->header.first_die_offset);
9691 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9692 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9693 per_cu->cu->objfile->obfd,
9694 dwarf2_per_objfile->info.buffer, info_ptr,
9695 per_cu->cu);
9696 if (comp_unit_die.has_children)
9697 load_partial_dies (per_cu->cu->objfile->obfd,
9698 dwarf2_per_objfile->info.buffer, info_ptr,
9699 0, per_cu->cu);
9700 do_cleanups (back_to);
9701
9702 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9703 }
9704
9705 not_found:
9706
9707 if (pd == NULL)
9708 internal_error (__FILE__, __LINE__,
9709 _("could not find partial DIE 0x%x "
9710 "in cache [from module %s]\n"),
9711 offset, bfd_get_filename (cu->objfile->obfd));
9712 return pd;
9713 }
9714
9715 /* See if we can figure out if the class lives in a namespace. We do
9716 this by looking for a member function; its demangled name will
9717 contain namespace info, if there is any. */
9718
9719 static void
9720 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9721 struct dwarf2_cu *cu)
9722 {
9723 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9724 what template types look like, because the demangler
9725 frequently doesn't give the same name as the debug info. We
9726 could fix this by only using the demangled name to get the
9727 prefix (but see comment in read_structure_type). */
9728
9729 struct partial_die_info *real_pdi;
9730 struct partial_die_info *child_pdi;
9731
9732 /* If this DIE (this DIE's specification, if any) has a parent, then
9733 we should not do this. We'll prepend the parent's fully qualified
9734 name when we create the partial symbol. */
9735
9736 real_pdi = struct_pdi;
9737 while (real_pdi->has_specification)
9738 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9739
9740 if (real_pdi->die_parent != NULL)
9741 return;
9742
9743 for (child_pdi = struct_pdi->die_child;
9744 child_pdi != NULL;
9745 child_pdi = child_pdi->die_sibling)
9746 {
9747 if (child_pdi->tag == DW_TAG_subprogram
9748 && child_pdi->linkage_name != NULL)
9749 {
9750 char *actual_class_name
9751 = language_class_name_from_physname (cu->language_defn,
9752 child_pdi->linkage_name);
9753 if (actual_class_name != NULL)
9754 {
9755 struct_pdi->name
9756 = obsavestring (actual_class_name,
9757 strlen (actual_class_name),
9758 &cu->objfile->objfile_obstack);
9759 xfree (actual_class_name);
9760 }
9761 break;
9762 }
9763 }
9764 }
9765
9766 /* Adjust PART_DIE before generating a symbol for it. This function
9767 may set the is_external flag or change the DIE's name. */
9768
9769 static void
9770 fixup_partial_die (struct partial_die_info *part_die,
9771 struct dwarf2_cu *cu)
9772 {
9773 /* Once we've fixed up a die, there's no point in doing so again.
9774 This also avoids a memory leak if we were to call
9775 guess_partial_die_structure_name multiple times. */
9776 if (part_die->fixup_called)
9777 return;
9778
9779 /* If we found a reference attribute and the DIE has no name, try
9780 to find a name in the referred to DIE. */
9781
9782 if (part_die->name == NULL && part_die->has_specification)
9783 {
9784 struct partial_die_info *spec_die;
9785
9786 spec_die = find_partial_die (part_die->spec_offset, cu);
9787
9788 fixup_partial_die (spec_die, cu);
9789
9790 if (spec_die->name)
9791 {
9792 part_die->name = spec_die->name;
9793
9794 /* Copy DW_AT_external attribute if it is set. */
9795 if (spec_die->is_external)
9796 part_die->is_external = spec_die->is_external;
9797 }
9798 }
9799
9800 /* Set default names for some unnamed DIEs. */
9801
9802 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9803 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9804
9805 /* If there is no parent die to provide a namespace, and there are
9806 children, see if we can determine the namespace from their linkage
9807 name.
9808 NOTE: We need to do this even if cu->has_namespace_info != 0.
9809 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9810 if (cu->language == language_cplus
9811 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
9812 && part_die->die_parent == NULL
9813 && part_die->has_children
9814 && (part_die->tag == DW_TAG_class_type
9815 || part_die->tag == DW_TAG_structure_type
9816 || part_die->tag == DW_TAG_union_type))
9817 guess_partial_die_structure_name (part_die, cu);
9818
9819 /* GCC might emit a nameless struct or union that has a linkage
9820 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9821 if (part_die->name == NULL
9822 && (part_die->tag == DW_TAG_structure_type
9823 || part_die->tag == DW_TAG_union_type
9824 || part_die->tag == DW_TAG_class_type)
9825 && part_die->linkage_name != NULL)
9826 {
9827 char *demangled;
9828
9829 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9830 if (demangled)
9831 {
9832 part_die->name = obsavestring (demangled, strlen (demangled),
9833 &cu->objfile->objfile_obstack);
9834 xfree (demangled);
9835 }
9836 }
9837
9838 part_die->fixup_called = 1;
9839 }
9840
9841 /* Read an attribute value described by an attribute form. */
9842
9843 static gdb_byte *
9844 read_attribute_value (struct attribute *attr, unsigned form,
9845 bfd *abfd, gdb_byte *info_ptr,
9846 struct dwarf2_cu *cu)
9847 {
9848 struct comp_unit_head *cu_header = &cu->header;
9849 unsigned int bytes_read;
9850 struct dwarf_block *blk;
9851
9852 attr->form = form;
9853 switch (form)
9854 {
9855 case DW_FORM_ref_addr:
9856 if (cu->header.version == 2)
9857 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9858 else
9859 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9860 &cu->header, &bytes_read);
9861 info_ptr += bytes_read;
9862 break;
9863 case DW_FORM_addr:
9864 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9865 info_ptr += bytes_read;
9866 break;
9867 case DW_FORM_block2:
9868 blk = dwarf_alloc_block (cu);
9869 blk->size = read_2_bytes (abfd, info_ptr);
9870 info_ptr += 2;
9871 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9872 info_ptr += blk->size;
9873 DW_BLOCK (attr) = blk;
9874 break;
9875 case DW_FORM_block4:
9876 blk = dwarf_alloc_block (cu);
9877 blk->size = read_4_bytes (abfd, info_ptr);
9878 info_ptr += 4;
9879 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9880 info_ptr += blk->size;
9881 DW_BLOCK (attr) = blk;
9882 break;
9883 case DW_FORM_data2:
9884 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9885 info_ptr += 2;
9886 break;
9887 case DW_FORM_data4:
9888 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9889 info_ptr += 4;
9890 break;
9891 case DW_FORM_data8:
9892 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9893 info_ptr += 8;
9894 break;
9895 case DW_FORM_sec_offset:
9896 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9897 info_ptr += bytes_read;
9898 break;
9899 case DW_FORM_string:
9900 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9901 DW_STRING_IS_CANONICAL (attr) = 0;
9902 info_ptr += bytes_read;
9903 break;
9904 case DW_FORM_strp:
9905 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9906 &bytes_read);
9907 DW_STRING_IS_CANONICAL (attr) = 0;
9908 info_ptr += bytes_read;
9909 break;
9910 case DW_FORM_exprloc:
9911 case DW_FORM_block:
9912 blk = dwarf_alloc_block (cu);
9913 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9914 info_ptr += bytes_read;
9915 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9916 info_ptr += blk->size;
9917 DW_BLOCK (attr) = blk;
9918 break;
9919 case DW_FORM_block1:
9920 blk = dwarf_alloc_block (cu);
9921 blk->size = read_1_byte (abfd, info_ptr);
9922 info_ptr += 1;
9923 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9924 info_ptr += blk->size;
9925 DW_BLOCK (attr) = blk;
9926 break;
9927 case DW_FORM_data1:
9928 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9929 info_ptr += 1;
9930 break;
9931 case DW_FORM_flag:
9932 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9933 info_ptr += 1;
9934 break;
9935 case DW_FORM_flag_present:
9936 DW_UNSND (attr) = 1;
9937 break;
9938 case DW_FORM_sdata:
9939 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9940 info_ptr += bytes_read;
9941 break;
9942 case DW_FORM_udata:
9943 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9944 info_ptr += bytes_read;
9945 break;
9946 case DW_FORM_ref1:
9947 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9948 info_ptr += 1;
9949 break;
9950 case DW_FORM_ref2:
9951 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9952 info_ptr += 2;
9953 break;
9954 case DW_FORM_ref4:
9955 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9956 info_ptr += 4;
9957 break;
9958 case DW_FORM_ref8:
9959 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9960 info_ptr += 8;
9961 break;
9962 case DW_FORM_ref_sig8:
9963 /* Convert the signature to something we can record in DW_UNSND
9964 for later lookup.
9965 NOTE: This is NULL if the type wasn't found. */
9966 DW_SIGNATURED_TYPE (attr) =
9967 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9968 info_ptr += 8;
9969 break;
9970 case DW_FORM_ref_udata:
9971 DW_ADDR (attr) = (cu->header.offset
9972 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9973 info_ptr += bytes_read;
9974 break;
9975 case DW_FORM_indirect:
9976 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9977 info_ptr += bytes_read;
9978 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9979 break;
9980 default:
9981 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9982 dwarf_form_name (form),
9983 bfd_get_filename (abfd));
9984 }
9985
9986 /* We have seen instances where the compiler tried to emit a byte
9987 size attribute of -1 which ended up being encoded as an unsigned
9988 0xffffffff. Although 0xffffffff is technically a valid size value,
9989 an object of this size seems pretty unlikely so we can relatively
9990 safely treat these cases as if the size attribute was invalid and
9991 treat them as zero by default. */
9992 if (attr->name == DW_AT_byte_size
9993 && form == DW_FORM_data4
9994 && DW_UNSND (attr) >= 0xffffffff)
9995 {
9996 complaint
9997 (&symfile_complaints,
9998 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9999 hex_string (DW_UNSND (attr)));
10000 DW_UNSND (attr) = 0;
10001 }
10002
10003 return info_ptr;
10004 }
10005
10006 /* Read an attribute described by an abbreviated attribute. */
10007
10008 static gdb_byte *
10009 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10010 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10011 {
10012 attr->name = abbrev->name;
10013 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10014 }
10015
10016 /* Read dwarf information from a buffer. */
10017
10018 static unsigned int
10019 read_1_byte (bfd *abfd, gdb_byte *buf)
10020 {
10021 return bfd_get_8 (abfd, buf);
10022 }
10023
10024 static int
10025 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10026 {
10027 return bfd_get_signed_8 (abfd, buf);
10028 }
10029
10030 static unsigned int
10031 read_2_bytes (bfd *abfd, gdb_byte *buf)
10032 {
10033 return bfd_get_16 (abfd, buf);
10034 }
10035
10036 static int
10037 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10038 {
10039 return bfd_get_signed_16 (abfd, buf);
10040 }
10041
10042 static unsigned int
10043 read_4_bytes (bfd *abfd, gdb_byte *buf)
10044 {
10045 return bfd_get_32 (abfd, buf);
10046 }
10047
10048 static int
10049 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10050 {
10051 return bfd_get_signed_32 (abfd, buf);
10052 }
10053
10054 static ULONGEST
10055 read_8_bytes (bfd *abfd, gdb_byte *buf)
10056 {
10057 return bfd_get_64 (abfd, buf);
10058 }
10059
10060 static CORE_ADDR
10061 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10062 unsigned int *bytes_read)
10063 {
10064 struct comp_unit_head *cu_header = &cu->header;
10065 CORE_ADDR retval = 0;
10066
10067 if (cu_header->signed_addr_p)
10068 {
10069 switch (cu_header->addr_size)
10070 {
10071 case 2:
10072 retval = bfd_get_signed_16 (abfd, buf);
10073 break;
10074 case 4:
10075 retval = bfd_get_signed_32 (abfd, buf);
10076 break;
10077 case 8:
10078 retval = bfd_get_signed_64 (abfd, buf);
10079 break;
10080 default:
10081 internal_error (__FILE__, __LINE__,
10082 _("read_address: bad switch, signed [in module %s]"),
10083 bfd_get_filename (abfd));
10084 }
10085 }
10086 else
10087 {
10088 switch (cu_header->addr_size)
10089 {
10090 case 2:
10091 retval = bfd_get_16 (abfd, buf);
10092 break;
10093 case 4:
10094 retval = bfd_get_32 (abfd, buf);
10095 break;
10096 case 8:
10097 retval = bfd_get_64 (abfd, buf);
10098 break;
10099 default:
10100 internal_error (__FILE__, __LINE__,
10101 _("read_address: bad switch, "
10102 "unsigned [in module %s]"),
10103 bfd_get_filename (abfd));
10104 }
10105 }
10106
10107 *bytes_read = cu_header->addr_size;
10108 return retval;
10109 }
10110
10111 /* Read the initial length from a section. The (draft) DWARF 3
10112 specification allows the initial length to take up either 4 bytes
10113 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10114 bytes describe the length and all offsets will be 8 bytes in length
10115 instead of 4.
10116
10117 An older, non-standard 64-bit format is also handled by this
10118 function. The older format in question stores the initial length
10119 as an 8-byte quantity without an escape value. Lengths greater
10120 than 2^32 aren't very common which means that the initial 4 bytes
10121 is almost always zero. Since a length value of zero doesn't make
10122 sense for the 32-bit format, this initial zero can be considered to
10123 be an escape value which indicates the presence of the older 64-bit
10124 format. As written, the code can't detect (old format) lengths
10125 greater than 4GB. If it becomes necessary to handle lengths
10126 somewhat larger than 4GB, we could allow other small values (such
10127 as the non-sensical values of 1, 2, and 3) to also be used as
10128 escape values indicating the presence of the old format.
10129
10130 The value returned via bytes_read should be used to increment the
10131 relevant pointer after calling read_initial_length().
10132
10133 [ Note: read_initial_length() and read_offset() are based on the
10134 document entitled "DWARF Debugging Information Format", revision
10135 3, draft 8, dated November 19, 2001. This document was obtained
10136 from:
10137
10138 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10139
10140 This document is only a draft and is subject to change. (So beware.)
10141
10142 Details regarding the older, non-standard 64-bit format were
10143 determined empirically by examining 64-bit ELF files produced by
10144 the SGI toolchain on an IRIX 6.5 machine.
10145
10146 - Kevin, July 16, 2002
10147 ] */
10148
10149 static LONGEST
10150 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10151 {
10152 LONGEST length = bfd_get_32 (abfd, buf);
10153
10154 if (length == 0xffffffff)
10155 {
10156 length = bfd_get_64 (abfd, buf + 4);
10157 *bytes_read = 12;
10158 }
10159 else if (length == 0)
10160 {
10161 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10162 length = bfd_get_64 (abfd, buf);
10163 *bytes_read = 8;
10164 }
10165 else
10166 {
10167 *bytes_read = 4;
10168 }
10169
10170 return length;
10171 }
10172
10173 /* Cover function for read_initial_length.
10174 Returns the length of the object at BUF, and stores the size of the
10175 initial length in *BYTES_READ and stores the size that offsets will be in
10176 *OFFSET_SIZE.
10177 If the initial length size is not equivalent to that specified in
10178 CU_HEADER then issue a complaint.
10179 This is useful when reading non-comp-unit headers. */
10180
10181 static LONGEST
10182 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10183 const struct comp_unit_head *cu_header,
10184 unsigned int *bytes_read,
10185 unsigned int *offset_size)
10186 {
10187 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10188
10189 gdb_assert (cu_header->initial_length_size == 4
10190 || cu_header->initial_length_size == 8
10191 || cu_header->initial_length_size == 12);
10192
10193 if (cu_header->initial_length_size != *bytes_read)
10194 complaint (&symfile_complaints,
10195 _("intermixed 32-bit and 64-bit DWARF sections"));
10196
10197 *offset_size = (*bytes_read == 4) ? 4 : 8;
10198 return length;
10199 }
10200
10201 /* Read an offset from the data stream. The size of the offset is
10202 given by cu_header->offset_size. */
10203
10204 static LONGEST
10205 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10206 unsigned int *bytes_read)
10207 {
10208 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10209
10210 *bytes_read = cu_header->offset_size;
10211 return offset;
10212 }
10213
10214 /* Read an offset from the data stream. */
10215
10216 static LONGEST
10217 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10218 {
10219 LONGEST retval = 0;
10220
10221 switch (offset_size)
10222 {
10223 case 4:
10224 retval = bfd_get_32 (abfd, buf);
10225 break;
10226 case 8:
10227 retval = bfd_get_64 (abfd, buf);
10228 break;
10229 default:
10230 internal_error (__FILE__, __LINE__,
10231 _("read_offset_1: bad switch [in module %s]"),
10232 bfd_get_filename (abfd));
10233 }
10234
10235 return retval;
10236 }
10237
10238 static gdb_byte *
10239 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10240 {
10241 /* If the size of a host char is 8 bits, we can return a pointer
10242 to the buffer, otherwise we have to copy the data to a buffer
10243 allocated on the temporary obstack. */
10244 gdb_assert (HOST_CHAR_BIT == 8);
10245 return buf;
10246 }
10247
10248 static char *
10249 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10250 {
10251 /* If the size of a host char is 8 bits, we can return a pointer
10252 to the string, otherwise we have to copy the string to a buffer
10253 allocated on the temporary obstack. */
10254 gdb_assert (HOST_CHAR_BIT == 8);
10255 if (*buf == '\0')
10256 {
10257 *bytes_read_ptr = 1;
10258 return NULL;
10259 }
10260 *bytes_read_ptr = strlen ((char *) buf) + 1;
10261 return (char *) buf;
10262 }
10263
10264 static char *
10265 read_indirect_string (bfd *abfd, gdb_byte *buf,
10266 const struct comp_unit_head *cu_header,
10267 unsigned int *bytes_read_ptr)
10268 {
10269 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10270
10271 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10272 if (dwarf2_per_objfile->str.buffer == NULL)
10273 {
10274 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10275 bfd_get_filename (abfd));
10276 return NULL;
10277 }
10278 if (str_offset >= dwarf2_per_objfile->str.size)
10279 {
10280 error (_("DW_FORM_strp pointing outside of "
10281 ".debug_str section [in module %s]"),
10282 bfd_get_filename (abfd));
10283 return NULL;
10284 }
10285 gdb_assert (HOST_CHAR_BIT == 8);
10286 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10287 return NULL;
10288 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10289 }
10290
10291 static unsigned long
10292 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10293 {
10294 unsigned long result;
10295 unsigned int num_read;
10296 int i, shift;
10297 unsigned char byte;
10298
10299 result = 0;
10300 shift = 0;
10301 num_read = 0;
10302 i = 0;
10303 while (1)
10304 {
10305 byte = bfd_get_8 (abfd, buf);
10306 buf++;
10307 num_read++;
10308 result |= ((unsigned long)(byte & 127) << shift);
10309 if ((byte & 128) == 0)
10310 {
10311 break;
10312 }
10313 shift += 7;
10314 }
10315 *bytes_read_ptr = num_read;
10316 return result;
10317 }
10318
10319 static long
10320 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10321 {
10322 long result;
10323 int i, shift, num_read;
10324 unsigned char byte;
10325
10326 result = 0;
10327 shift = 0;
10328 num_read = 0;
10329 i = 0;
10330 while (1)
10331 {
10332 byte = bfd_get_8 (abfd, buf);
10333 buf++;
10334 num_read++;
10335 result |= ((long)(byte & 127) << shift);
10336 shift += 7;
10337 if ((byte & 128) == 0)
10338 {
10339 break;
10340 }
10341 }
10342 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10343 result |= -(((long)1) << shift);
10344 *bytes_read_ptr = num_read;
10345 return result;
10346 }
10347
10348 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10349
10350 static gdb_byte *
10351 skip_leb128 (bfd *abfd, gdb_byte *buf)
10352 {
10353 int byte;
10354
10355 while (1)
10356 {
10357 byte = bfd_get_8 (abfd, buf);
10358 buf++;
10359 if ((byte & 128) == 0)
10360 return buf;
10361 }
10362 }
10363
10364 static void
10365 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10366 {
10367 switch (lang)
10368 {
10369 case DW_LANG_C89:
10370 case DW_LANG_C99:
10371 case DW_LANG_C:
10372 cu->language = language_c;
10373 break;
10374 case DW_LANG_C_plus_plus:
10375 cu->language = language_cplus;
10376 break;
10377 case DW_LANG_D:
10378 cu->language = language_d;
10379 break;
10380 case DW_LANG_Fortran77:
10381 case DW_LANG_Fortran90:
10382 case DW_LANG_Fortran95:
10383 cu->language = language_fortran;
10384 break;
10385 case DW_LANG_Mips_Assembler:
10386 cu->language = language_asm;
10387 break;
10388 case DW_LANG_Java:
10389 cu->language = language_java;
10390 break;
10391 case DW_LANG_Ada83:
10392 case DW_LANG_Ada95:
10393 cu->language = language_ada;
10394 break;
10395 case DW_LANG_Modula2:
10396 cu->language = language_m2;
10397 break;
10398 case DW_LANG_Pascal83:
10399 cu->language = language_pascal;
10400 break;
10401 case DW_LANG_ObjC:
10402 cu->language = language_objc;
10403 break;
10404 case DW_LANG_Cobol74:
10405 case DW_LANG_Cobol85:
10406 default:
10407 cu->language = language_minimal;
10408 break;
10409 }
10410 cu->language_defn = language_def (cu->language);
10411 }
10412
10413 /* Return the named attribute or NULL if not there. */
10414
10415 static struct attribute *
10416 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10417 {
10418 unsigned int i;
10419 struct attribute *spec = NULL;
10420
10421 for (i = 0; i < die->num_attrs; ++i)
10422 {
10423 if (die->attrs[i].name == name)
10424 return &die->attrs[i];
10425 if (die->attrs[i].name == DW_AT_specification
10426 || die->attrs[i].name == DW_AT_abstract_origin)
10427 spec = &die->attrs[i];
10428 }
10429
10430 if (spec)
10431 {
10432 die = follow_die_ref (die, spec, &cu);
10433 return dwarf2_attr (die, name, cu);
10434 }
10435
10436 return NULL;
10437 }
10438
10439 /* Return the named attribute or NULL if not there,
10440 but do not follow DW_AT_specification, etc.
10441 This is for use in contexts where we're reading .debug_types dies.
10442 Following DW_AT_specification, DW_AT_abstract_origin will take us
10443 back up the chain, and we want to go down. */
10444
10445 static struct attribute *
10446 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10447 struct dwarf2_cu *cu)
10448 {
10449 unsigned int i;
10450
10451 for (i = 0; i < die->num_attrs; ++i)
10452 if (die->attrs[i].name == name)
10453 return &die->attrs[i];
10454
10455 return NULL;
10456 }
10457
10458 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10459 and holds a non-zero value. This function should only be used for
10460 DW_FORM_flag or DW_FORM_flag_present attributes. */
10461
10462 static int
10463 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10464 {
10465 struct attribute *attr = dwarf2_attr (die, name, cu);
10466
10467 return (attr && DW_UNSND (attr));
10468 }
10469
10470 static int
10471 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10472 {
10473 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10474 which value is non-zero. However, we have to be careful with
10475 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10476 (via dwarf2_flag_true_p) follows this attribute. So we may
10477 end up accidently finding a declaration attribute that belongs
10478 to a different DIE referenced by the specification attribute,
10479 even though the given DIE does not have a declaration attribute. */
10480 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10481 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10482 }
10483
10484 /* Return the die giving the specification for DIE, if there is
10485 one. *SPEC_CU is the CU containing DIE on input, and the CU
10486 containing the return value on output. If there is no
10487 specification, but there is an abstract origin, that is
10488 returned. */
10489
10490 static struct die_info *
10491 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10492 {
10493 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10494 *spec_cu);
10495
10496 if (spec_attr == NULL)
10497 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10498
10499 if (spec_attr == NULL)
10500 return NULL;
10501 else
10502 return follow_die_ref (die, spec_attr, spec_cu);
10503 }
10504
10505 /* Free the line_header structure *LH, and any arrays and strings it
10506 refers to.
10507 NOTE: This is also used as a "cleanup" function. */
10508
10509 static void
10510 free_line_header (struct line_header *lh)
10511 {
10512 if (lh->standard_opcode_lengths)
10513 xfree (lh->standard_opcode_lengths);
10514
10515 /* Remember that all the lh->file_names[i].name pointers are
10516 pointers into debug_line_buffer, and don't need to be freed. */
10517 if (lh->file_names)
10518 xfree (lh->file_names);
10519
10520 /* Similarly for the include directory names. */
10521 if (lh->include_dirs)
10522 xfree (lh->include_dirs);
10523
10524 xfree (lh);
10525 }
10526
10527 /* Add an entry to LH's include directory table. */
10528
10529 static void
10530 add_include_dir (struct line_header *lh, char *include_dir)
10531 {
10532 /* Grow the array if necessary. */
10533 if (lh->include_dirs_size == 0)
10534 {
10535 lh->include_dirs_size = 1; /* for testing */
10536 lh->include_dirs = xmalloc (lh->include_dirs_size
10537 * sizeof (*lh->include_dirs));
10538 }
10539 else if (lh->num_include_dirs >= lh->include_dirs_size)
10540 {
10541 lh->include_dirs_size *= 2;
10542 lh->include_dirs = xrealloc (lh->include_dirs,
10543 (lh->include_dirs_size
10544 * sizeof (*lh->include_dirs)));
10545 }
10546
10547 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10548 }
10549
10550 /* Add an entry to LH's file name table. */
10551
10552 static void
10553 add_file_name (struct line_header *lh,
10554 char *name,
10555 unsigned int dir_index,
10556 unsigned int mod_time,
10557 unsigned int length)
10558 {
10559 struct file_entry *fe;
10560
10561 /* Grow the array if necessary. */
10562 if (lh->file_names_size == 0)
10563 {
10564 lh->file_names_size = 1; /* for testing */
10565 lh->file_names = xmalloc (lh->file_names_size
10566 * sizeof (*lh->file_names));
10567 }
10568 else if (lh->num_file_names >= lh->file_names_size)
10569 {
10570 lh->file_names_size *= 2;
10571 lh->file_names = xrealloc (lh->file_names,
10572 (lh->file_names_size
10573 * sizeof (*lh->file_names)));
10574 }
10575
10576 fe = &lh->file_names[lh->num_file_names++];
10577 fe->name = name;
10578 fe->dir_index = dir_index;
10579 fe->mod_time = mod_time;
10580 fe->length = length;
10581 fe->included_p = 0;
10582 fe->symtab = NULL;
10583 }
10584
10585 /* Read the statement program header starting at OFFSET in
10586 .debug_line, according to the endianness of ABFD. Return a pointer
10587 to a struct line_header, allocated using xmalloc.
10588
10589 NOTE: the strings in the include directory and file name tables of
10590 the returned object point into debug_line_buffer, and must not be
10591 freed. */
10592
10593 static struct line_header *
10594 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10595 struct dwarf2_cu *cu)
10596 {
10597 struct cleanup *back_to;
10598 struct line_header *lh;
10599 gdb_byte *line_ptr;
10600 unsigned int bytes_read, offset_size;
10601 int i;
10602 char *cur_dir, *cur_file;
10603
10604 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10605 if (dwarf2_per_objfile->line.buffer == NULL)
10606 {
10607 complaint (&symfile_complaints, _("missing .debug_line section"));
10608 return 0;
10609 }
10610
10611 /* Make sure that at least there's room for the total_length field.
10612 That could be 12 bytes long, but we're just going to fudge that. */
10613 if (offset + 4 >= dwarf2_per_objfile->line.size)
10614 {
10615 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10616 return 0;
10617 }
10618
10619 lh = xmalloc (sizeof (*lh));
10620 memset (lh, 0, sizeof (*lh));
10621 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10622 (void *) lh);
10623
10624 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10625
10626 /* Read in the header. */
10627 lh->total_length =
10628 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10629 &bytes_read, &offset_size);
10630 line_ptr += bytes_read;
10631 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10632 + dwarf2_per_objfile->line.size))
10633 {
10634 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10635 return 0;
10636 }
10637 lh->statement_program_end = line_ptr + lh->total_length;
10638 lh->version = read_2_bytes (abfd, line_ptr);
10639 line_ptr += 2;
10640 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10641 line_ptr += offset_size;
10642 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10643 line_ptr += 1;
10644 if (lh->version >= 4)
10645 {
10646 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10647 line_ptr += 1;
10648 }
10649 else
10650 lh->maximum_ops_per_instruction = 1;
10651
10652 if (lh->maximum_ops_per_instruction == 0)
10653 {
10654 lh->maximum_ops_per_instruction = 1;
10655 complaint (&symfile_complaints,
10656 _("invalid maximum_ops_per_instruction "
10657 "in `.debug_line' section"));
10658 }
10659
10660 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10661 line_ptr += 1;
10662 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10663 line_ptr += 1;
10664 lh->line_range = read_1_byte (abfd, line_ptr);
10665 line_ptr += 1;
10666 lh->opcode_base = read_1_byte (abfd, line_ptr);
10667 line_ptr += 1;
10668 lh->standard_opcode_lengths
10669 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10670
10671 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10672 for (i = 1; i < lh->opcode_base; ++i)
10673 {
10674 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10675 line_ptr += 1;
10676 }
10677
10678 /* Read directory table. */
10679 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10680 {
10681 line_ptr += bytes_read;
10682 add_include_dir (lh, cur_dir);
10683 }
10684 line_ptr += bytes_read;
10685
10686 /* Read file name table. */
10687 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10688 {
10689 unsigned int dir_index, mod_time, length;
10690
10691 line_ptr += bytes_read;
10692 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10693 line_ptr += bytes_read;
10694 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10695 line_ptr += bytes_read;
10696 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10697 line_ptr += bytes_read;
10698
10699 add_file_name (lh, cur_file, dir_index, mod_time, length);
10700 }
10701 line_ptr += bytes_read;
10702 lh->statement_program_start = line_ptr;
10703
10704 if (line_ptr > (dwarf2_per_objfile->line.buffer
10705 + dwarf2_per_objfile->line.size))
10706 complaint (&symfile_complaints,
10707 _("line number info header doesn't "
10708 "fit in `.debug_line' section"));
10709
10710 discard_cleanups (back_to);
10711 return lh;
10712 }
10713
10714 /* This function exists to work around a bug in certain compilers
10715 (particularly GCC 2.95), in which the first line number marker of a
10716 function does not show up until after the prologue, right before
10717 the second line number marker. This function shifts ADDRESS down
10718 to the beginning of the function if necessary, and is called on
10719 addresses passed to record_line. */
10720
10721 static CORE_ADDR
10722 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10723 {
10724 struct function_range *fn;
10725
10726 /* Find the function_range containing address. */
10727 if (!cu->first_fn)
10728 return address;
10729
10730 if (!cu->cached_fn)
10731 cu->cached_fn = cu->first_fn;
10732
10733 fn = cu->cached_fn;
10734 while (fn)
10735 if (fn->lowpc <= address && fn->highpc > address)
10736 goto found;
10737 else
10738 fn = fn->next;
10739
10740 fn = cu->first_fn;
10741 while (fn && fn != cu->cached_fn)
10742 if (fn->lowpc <= address && fn->highpc > address)
10743 goto found;
10744 else
10745 fn = fn->next;
10746
10747 return address;
10748
10749 found:
10750 if (fn->seen_line)
10751 return address;
10752 if (address != fn->lowpc)
10753 complaint (&symfile_complaints,
10754 _("misplaced first line number at 0x%lx for '%s'"),
10755 (unsigned long) address, fn->name);
10756 fn->seen_line = 1;
10757 return fn->lowpc;
10758 }
10759
10760 /* Subroutine of dwarf_decode_lines to simplify it.
10761 Return the file name of the psymtab for included file FILE_INDEX
10762 in line header LH of PST.
10763 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10764 If space for the result is malloc'd, it will be freed by a cleanup.
10765 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10766
10767 static char *
10768 psymtab_include_file_name (const struct line_header *lh, int file_index,
10769 const struct partial_symtab *pst,
10770 const char *comp_dir)
10771 {
10772 const struct file_entry fe = lh->file_names [file_index];
10773 char *include_name = fe.name;
10774 char *include_name_to_compare = include_name;
10775 char *dir_name = NULL;
10776 const char *pst_filename;
10777 char *copied_name = NULL;
10778 int file_is_pst;
10779
10780 if (fe.dir_index)
10781 dir_name = lh->include_dirs[fe.dir_index - 1];
10782
10783 if (!IS_ABSOLUTE_PATH (include_name)
10784 && (dir_name != NULL || comp_dir != NULL))
10785 {
10786 /* Avoid creating a duplicate psymtab for PST.
10787 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10788 Before we do the comparison, however, we need to account
10789 for DIR_NAME and COMP_DIR.
10790 First prepend dir_name (if non-NULL). If we still don't
10791 have an absolute path prepend comp_dir (if non-NULL).
10792 However, the directory we record in the include-file's
10793 psymtab does not contain COMP_DIR (to match the
10794 corresponding symtab(s)).
10795
10796 Example:
10797
10798 bash$ cd /tmp
10799 bash$ gcc -g ./hello.c
10800 include_name = "hello.c"
10801 dir_name = "."
10802 DW_AT_comp_dir = comp_dir = "/tmp"
10803 DW_AT_name = "./hello.c" */
10804
10805 if (dir_name != NULL)
10806 {
10807 include_name = concat (dir_name, SLASH_STRING,
10808 include_name, (char *)NULL);
10809 include_name_to_compare = include_name;
10810 make_cleanup (xfree, include_name);
10811 }
10812 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10813 {
10814 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10815 include_name, (char *)NULL);
10816 }
10817 }
10818
10819 pst_filename = pst->filename;
10820 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10821 {
10822 copied_name = concat (pst->dirname, SLASH_STRING,
10823 pst_filename, (char *)NULL);
10824 pst_filename = copied_name;
10825 }
10826
10827 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10828
10829 if (include_name_to_compare != include_name)
10830 xfree (include_name_to_compare);
10831 if (copied_name != NULL)
10832 xfree (copied_name);
10833
10834 if (file_is_pst)
10835 return NULL;
10836 return include_name;
10837 }
10838
10839 /* Ignore this record_line request. */
10840
10841 static void
10842 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10843 {
10844 return;
10845 }
10846
10847 /* Decode the Line Number Program (LNP) for the given line_header
10848 structure and CU. The actual information extracted and the type
10849 of structures created from the LNP depends on the value of PST.
10850
10851 1. If PST is NULL, then this procedure uses the data from the program
10852 to create all necessary symbol tables, and their linetables.
10853
10854 2. If PST is not NULL, this procedure reads the program to determine
10855 the list of files included by the unit represented by PST, and
10856 builds all the associated partial symbol tables.
10857
10858 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10859 It is used for relative paths in the line table.
10860 NOTE: When processing partial symtabs (pst != NULL),
10861 comp_dir == pst->dirname.
10862
10863 NOTE: It is important that psymtabs have the same file name (via strcmp)
10864 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10865 symtab we don't use it in the name of the psymtabs we create.
10866 E.g. expand_line_sal requires this when finding psymtabs to expand.
10867 A good testcase for this is mb-inline.exp. */
10868
10869 static void
10870 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10871 struct dwarf2_cu *cu, struct partial_symtab *pst)
10872 {
10873 gdb_byte *line_ptr, *extended_end;
10874 gdb_byte *line_end;
10875 unsigned int bytes_read, extended_len;
10876 unsigned char op_code, extended_op, adj_opcode;
10877 CORE_ADDR baseaddr;
10878 struct objfile *objfile = cu->objfile;
10879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10880 const int decode_for_pst_p = (pst != NULL);
10881 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10882 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10883 = record_line;
10884
10885 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10886
10887 line_ptr = lh->statement_program_start;
10888 line_end = lh->statement_program_end;
10889
10890 /* Read the statement sequences until there's nothing left. */
10891 while (line_ptr < line_end)
10892 {
10893 /* state machine registers */
10894 CORE_ADDR address = 0;
10895 unsigned int file = 1;
10896 unsigned int line = 1;
10897 unsigned int column = 0;
10898 int is_stmt = lh->default_is_stmt;
10899 int basic_block = 0;
10900 int end_sequence = 0;
10901 CORE_ADDR addr;
10902 unsigned char op_index = 0;
10903
10904 if (!decode_for_pst_p && lh->num_file_names >= file)
10905 {
10906 /* Start a subfile for the current file of the state machine. */
10907 /* lh->include_dirs and lh->file_names are 0-based, but the
10908 directory and file name numbers in the statement program
10909 are 1-based. */
10910 struct file_entry *fe = &lh->file_names[file - 1];
10911 char *dir = NULL;
10912
10913 if (fe->dir_index)
10914 dir = lh->include_dirs[fe->dir_index - 1];
10915
10916 dwarf2_start_subfile (fe->name, dir, comp_dir);
10917 }
10918
10919 /* Decode the table. */
10920 while (!end_sequence)
10921 {
10922 op_code = read_1_byte (abfd, line_ptr);
10923 line_ptr += 1;
10924 if (line_ptr > line_end)
10925 {
10926 dwarf2_debug_line_missing_end_sequence_complaint ();
10927 break;
10928 }
10929
10930 if (op_code >= lh->opcode_base)
10931 {
10932 /* Special operand. */
10933 adj_opcode = op_code - lh->opcode_base;
10934 address += (((op_index + (adj_opcode / lh->line_range))
10935 / lh->maximum_ops_per_instruction)
10936 * lh->minimum_instruction_length);
10937 op_index = ((op_index + (adj_opcode / lh->line_range))
10938 % lh->maximum_ops_per_instruction);
10939 line += lh->line_base + (adj_opcode % lh->line_range);
10940 if (lh->num_file_names < file || file == 0)
10941 dwarf2_debug_line_missing_file_complaint ();
10942 /* For now we ignore lines not starting on an
10943 instruction boundary. */
10944 else if (op_index == 0)
10945 {
10946 lh->file_names[file - 1].included_p = 1;
10947 if (!decode_for_pst_p && is_stmt)
10948 {
10949 if (last_subfile != current_subfile)
10950 {
10951 addr = gdbarch_addr_bits_remove (gdbarch, address);
10952 if (last_subfile)
10953 (*p_record_line) (last_subfile, 0, addr);
10954 last_subfile = current_subfile;
10955 }
10956 /* Append row to matrix using current values. */
10957 addr = check_cu_functions (address, cu);
10958 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10959 (*p_record_line) (current_subfile, line, addr);
10960 }
10961 }
10962 basic_block = 0;
10963 }
10964 else switch (op_code)
10965 {
10966 case DW_LNS_extended_op:
10967 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10968 &bytes_read);
10969 line_ptr += bytes_read;
10970 extended_end = line_ptr + extended_len;
10971 extended_op = read_1_byte (abfd, line_ptr);
10972 line_ptr += 1;
10973 switch (extended_op)
10974 {
10975 case DW_LNE_end_sequence:
10976 p_record_line = record_line;
10977 end_sequence = 1;
10978 break;
10979 case DW_LNE_set_address:
10980 address = read_address (abfd, line_ptr, cu, &bytes_read);
10981
10982 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10983 {
10984 /* This line table is for a function which has been
10985 GCd by the linker. Ignore it. PR gdb/12528 */
10986
10987 long line_offset
10988 = line_ptr - dwarf2_per_objfile->line.buffer;
10989
10990 complaint (&symfile_complaints,
10991 _(".debug_line address at offset 0x%lx is 0 "
10992 "[in module %s]"),
10993 line_offset, cu->objfile->name);
10994 p_record_line = noop_record_line;
10995 }
10996
10997 op_index = 0;
10998 line_ptr += bytes_read;
10999 address += baseaddr;
11000 break;
11001 case DW_LNE_define_file:
11002 {
11003 char *cur_file;
11004 unsigned int dir_index, mod_time, length;
11005
11006 cur_file = read_direct_string (abfd, line_ptr,
11007 &bytes_read);
11008 line_ptr += bytes_read;
11009 dir_index =
11010 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11011 line_ptr += bytes_read;
11012 mod_time =
11013 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11014 line_ptr += bytes_read;
11015 length =
11016 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11017 line_ptr += bytes_read;
11018 add_file_name (lh, cur_file, dir_index, mod_time, length);
11019 }
11020 break;
11021 case DW_LNE_set_discriminator:
11022 /* The discriminator is not interesting to the debugger;
11023 just ignore it. */
11024 line_ptr = extended_end;
11025 break;
11026 default:
11027 complaint (&symfile_complaints,
11028 _("mangled .debug_line section"));
11029 return;
11030 }
11031 /* Make sure that we parsed the extended op correctly. If e.g.
11032 we expected a different address size than the producer used,
11033 we may have read the wrong number of bytes. */
11034 if (line_ptr != extended_end)
11035 {
11036 complaint (&symfile_complaints,
11037 _("mangled .debug_line section"));
11038 return;
11039 }
11040 break;
11041 case DW_LNS_copy:
11042 if (lh->num_file_names < file || file == 0)
11043 dwarf2_debug_line_missing_file_complaint ();
11044 else
11045 {
11046 lh->file_names[file - 1].included_p = 1;
11047 if (!decode_for_pst_p && is_stmt)
11048 {
11049 if (last_subfile != current_subfile)
11050 {
11051 addr = gdbarch_addr_bits_remove (gdbarch, address);
11052 if (last_subfile)
11053 (*p_record_line) (last_subfile, 0, addr);
11054 last_subfile = current_subfile;
11055 }
11056 addr = check_cu_functions (address, cu);
11057 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11058 (*p_record_line) (current_subfile, line, addr);
11059 }
11060 }
11061 basic_block = 0;
11062 break;
11063 case DW_LNS_advance_pc:
11064 {
11065 CORE_ADDR adjust
11066 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11067
11068 address += (((op_index + adjust)
11069 / lh->maximum_ops_per_instruction)
11070 * lh->minimum_instruction_length);
11071 op_index = ((op_index + adjust)
11072 % lh->maximum_ops_per_instruction);
11073 line_ptr += bytes_read;
11074 }
11075 break;
11076 case DW_LNS_advance_line:
11077 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11078 line_ptr += bytes_read;
11079 break;
11080 case DW_LNS_set_file:
11081 {
11082 /* The arrays lh->include_dirs and lh->file_names are
11083 0-based, but the directory and file name numbers in
11084 the statement program are 1-based. */
11085 struct file_entry *fe;
11086 char *dir = NULL;
11087
11088 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11089 line_ptr += bytes_read;
11090 if (lh->num_file_names < file || file == 0)
11091 dwarf2_debug_line_missing_file_complaint ();
11092 else
11093 {
11094 fe = &lh->file_names[file - 1];
11095 if (fe->dir_index)
11096 dir = lh->include_dirs[fe->dir_index - 1];
11097 if (!decode_for_pst_p)
11098 {
11099 last_subfile = current_subfile;
11100 dwarf2_start_subfile (fe->name, dir, comp_dir);
11101 }
11102 }
11103 }
11104 break;
11105 case DW_LNS_set_column:
11106 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11107 line_ptr += bytes_read;
11108 break;
11109 case DW_LNS_negate_stmt:
11110 is_stmt = (!is_stmt);
11111 break;
11112 case DW_LNS_set_basic_block:
11113 basic_block = 1;
11114 break;
11115 /* Add to the address register of the state machine the
11116 address increment value corresponding to special opcode
11117 255. I.e., this value is scaled by the minimum
11118 instruction length since special opcode 255 would have
11119 scaled the increment. */
11120 case DW_LNS_const_add_pc:
11121 {
11122 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11123
11124 address += (((op_index + adjust)
11125 / lh->maximum_ops_per_instruction)
11126 * lh->minimum_instruction_length);
11127 op_index = ((op_index + adjust)
11128 % lh->maximum_ops_per_instruction);
11129 }
11130 break;
11131 case DW_LNS_fixed_advance_pc:
11132 address += read_2_bytes (abfd, line_ptr);
11133 op_index = 0;
11134 line_ptr += 2;
11135 break;
11136 default:
11137 {
11138 /* Unknown standard opcode, ignore it. */
11139 int i;
11140
11141 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11142 {
11143 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11144 line_ptr += bytes_read;
11145 }
11146 }
11147 }
11148 }
11149 if (lh->num_file_names < file || file == 0)
11150 dwarf2_debug_line_missing_file_complaint ();
11151 else
11152 {
11153 lh->file_names[file - 1].included_p = 1;
11154 if (!decode_for_pst_p)
11155 {
11156 addr = gdbarch_addr_bits_remove (gdbarch, address);
11157 (*p_record_line) (current_subfile, 0, addr);
11158 }
11159 }
11160 }
11161
11162 if (decode_for_pst_p)
11163 {
11164 int file_index;
11165
11166 /* Now that we're done scanning the Line Header Program, we can
11167 create the psymtab of each included file. */
11168 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11169 if (lh->file_names[file_index].included_p == 1)
11170 {
11171 char *include_name =
11172 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11173 if (include_name != NULL)
11174 dwarf2_create_include_psymtab (include_name, pst, objfile);
11175 }
11176 }
11177 else
11178 {
11179 /* Make sure a symtab is created for every file, even files
11180 which contain only variables (i.e. no code with associated
11181 line numbers). */
11182
11183 int i;
11184 struct file_entry *fe;
11185
11186 for (i = 0; i < lh->num_file_names; i++)
11187 {
11188 char *dir = NULL;
11189
11190 fe = &lh->file_names[i];
11191 if (fe->dir_index)
11192 dir = lh->include_dirs[fe->dir_index - 1];
11193 dwarf2_start_subfile (fe->name, dir, comp_dir);
11194
11195 /* Skip the main file; we don't need it, and it must be
11196 allocated last, so that it will show up before the
11197 non-primary symtabs in the objfile's symtab list. */
11198 if (current_subfile == first_subfile)
11199 continue;
11200
11201 if (current_subfile->symtab == NULL)
11202 current_subfile->symtab = allocate_symtab (current_subfile->name,
11203 cu->objfile);
11204 fe->symtab = current_subfile->symtab;
11205 }
11206 }
11207 }
11208
11209 /* Start a subfile for DWARF. FILENAME is the name of the file and
11210 DIRNAME the name of the source directory which contains FILENAME
11211 or NULL if not known. COMP_DIR is the compilation directory for the
11212 linetable's compilation unit or NULL if not known.
11213 This routine tries to keep line numbers from identical absolute and
11214 relative file names in a common subfile.
11215
11216 Using the `list' example from the GDB testsuite, which resides in
11217 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11218 of /srcdir/list0.c yields the following debugging information for list0.c:
11219
11220 DW_AT_name: /srcdir/list0.c
11221 DW_AT_comp_dir: /compdir
11222 files.files[0].name: list0.h
11223 files.files[0].dir: /srcdir
11224 files.files[1].name: list0.c
11225 files.files[1].dir: /srcdir
11226
11227 The line number information for list0.c has to end up in a single
11228 subfile, so that `break /srcdir/list0.c:1' works as expected.
11229 start_subfile will ensure that this happens provided that we pass the
11230 concatenation of files.files[1].dir and files.files[1].name as the
11231 subfile's name. */
11232
11233 static void
11234 dwarf2_start_subfile (char *filename, const char *dirname,
11235 const char *comp_dir)
11236 {
11237 char *fullname;
11238
11239 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11240 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11241 second argument to start_subfile. To be consistent, we do the
11242 same here. In order not to lose the line information directory,
11243 we concatenate it to the filename when it makes sense.
11244 Note that the Dwarf3 standard says (speaking of filenames in line
11245 information): ``The directory index is ignored for file names
11246 that represent full path names''. Thus ignoring dirname in the
11247 `else' branch below isn't an issue. */
11248
11249 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11250 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11251 else
11252 fullname = filename;
11253
11254 start_subfile (fullname, comp_dir);
11255
11256 if (fullname != filename)
11257 xfree (fullname);
11258 }
11259
11260 static void
11261 var_decode_location (struct attribute *attr, struct symbol *sym,
11262 struct dwarf2_cu *cu)
11263 {
11264 struct objfile *objfile = cu->objfile;
11265 struct comp_unit_head *cu_header = &cu->header;
11266
11267 /* NOTE drow/2003-01-30: There used to be a comment and some special
11268 code here to turn a symbol with DW_AT_external and a
11269 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11270 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11271 with some versions of binutils) where shared libraries could have
11272 relocations against symbols in their debug information - the
11273 minimal symbol would have the right address, but the debug info
11274 would not. It's no longer necessary, because we will explicitly
11275 apply relocations when we read in the debug information now. */
11276
11277 /* A DW_AT_location attribute with no contents indicates that a
11278 variable has been optimized away. */
11279 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11280 {
11281 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11282 return;
11283 }
11284
11285 /* Handle one degenerate form of location expression specially, to
11286 preserve GDB's previous behavior when section offsets are
11287 specified. If this is just a DW_OP_addr then mark this symbol
11288 as LOC_STATIC. */
11289
11290 if (attr_form_is_block (attr)
11291 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11292 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11293 {
11294 unsigned int dummy;
11295
11296 SYMBOL_VALUE_ADDRESS (sym) =
11297 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11298 SYMBOL_CLASS (sym) = LOC_STATIC;
11299 fixup_symbol_section (sym, objfile);
11300 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11301 SYMBOL_SECTION (sym));
11302 return;
11303 }
11304
11305 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11306 expression evaluator, and use LOC_COMPUTED only when necessary
11307 (i.e. when the value of a register or memory location is
11308 referenced, or a thread-local block, etc.). Then again, it might
11309 not be worthwhile. I'm assuming that it isn't unless performance
11310 or memory numbers show me otherwise. */
11311
11312 dwarf2_symbol_mark_computed (attr, sym, cu);
11313 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11314
11315 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11316 cu->has_loclist = 1;
11317 }
11318
11319 /* Given a pointer to a DWARF information entry, figure out if we need
11320 to make a symbol table entry for it, and if so, create a new entry
11321 and return a pointer to it.
11322 If TYPE is NULL, determine symbol type from the die, otherwise
11323 used the passed type.
11324 If SPACE is not NULL, use it to hold the new symbol. If it is
11325 NULL, allocate a new symbol on the objfile's obstack. */
11326
11327 static struct symbol *
11328 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11329 struct symbol *space)
11330 {
11331 struct objfile *objfile = cu->objfile;
11332 struct symbol *sym = NULL;
11333 char *name;
11334 struct attribute *attr = NULL;
11335 struct attribute *attr2 = NULL;
11336 CORE_ADDR baseaddr;
11337 struct pending **list_to_add = NULL;
11338
11339 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11340
11341 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11342
11343 name = dwarf2_name (die, cu);
11344 if (name)
11345 {
11346 const char *linkagename;
11347 int suppress_add = 0;
11348
11349 if (space)
11350 sym = space;
11351 else
11352 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11353 OBJSTAT (objfile, n_syms++);
11354
11355 /* Cache this symbol's name and the name's demangled form (if any). */
11356 SYMBOL_SET_LANGUAGE (sym, cu->language);
11357 linkagename = dwarf2_physname (name, die, cu);
11358 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11359
11360 /* Fortran does not have mangling standard and the mangling does differ
11361 between gfortran, iFort etc. */
11362 if (cu->language == language_fortran
11363 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11364 symbol_set_demangled_name (&(sym->ginfo),
11365 (char *) dwarf2_full_name (name, die, cu),
11366 NULL);
11367
11368 /* Default assumptions.
11369 Use the passed type or decode it from the die. */
11370 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11371 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11372 if (type != NULL)
11373 SYMBOL_TYPE (sym) = type;
11374 else
11375 SYMBOL_TYPE (sym) = die_type (die, cu);
11376 attr = dwarf2_attr (die,
11377 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11378 cu);
11379 if (attr)
11380 {
11381 SYMBOL_LINE (sym) = DW_UNSND (attr);
11382 }
11383
11384 attr = dwarf2_attr (die,
11385 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11386 cu);
11387 if (attr)
11388 {
11389 int file_index = DW_UNSND (attr);
11390
11391 if (cu->line_header == NULL
11392 || file_index > cu->line_header->num_file_names)
11393 complaint (&symfile_complaints,
11394 _("file index out of range"));
11395 else if (file_index > 0)
11396 {
11397 struct file_entry *fe;
11398
11399 fe = &cu->line_header->file_names[file_index - 1];
11400 SYMBOL_SYMTAB (sym) = fe->symtab;
11401 }
11402 }
11403
11404 switch (die->tag)
11405 {
11406 case DW_TAG_label:
11407 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11408 if (attr)
11409 {
11410 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11411 }
11412 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11413 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11414 SYMBOL_CLASS (sym) = LOC_LABEL;
11415 add_symbol_to_list (sym, cu->list_in_scope);
11416 break;
11417 case DW_TAG_subprogram:
11418 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11419 finish_block. */
11420 SYMBOL_CLASS (sym) = LOC_BLOCK;
11421 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11422 if ((attr2 && (DW_UNSND (attr2) != 0))
11423 || cu->language == language_ada)
11424 {
11425 /* Subprograms marked external are stored as a global symbol.
11426 Ada subprograms, whether marked external or not, are always
11427 stored as a global symbol, because we want to be able to
11428 access them globally. For instance, we want to be able
11429 to break on a nested subprogram without having to
11430 specify the context. */
11431 list_to_add = &global_symbols;
11432 }
11433 else
11434 {
11435 list_to_add = cu->list_in_scope;
11436 }
11437 break;
11438 case DW_TAG_inlined_subroutine:
11439 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11440 finish_block. */
11441 SYMBOL_CLASS (sym) = LOC_BLOCK;
11442 SYMBOL_INLINED (sym) = 1;
11443 /* Do not add the symbol to any lists. It will be found via
11444 BLOCK_FUNCTION from the blockvector. */
11445 break;
11446 case DW_TAG_template_value_param:
11447 suppress_add = 1;
11448 /* Fall through. */
11449 case DW_TAG_constant:
11450 case DW_TAG_variable:
11451 case DW_TAG_member:
11452 /* Compilation with minimal debug info may result in
11453 variables with missing type entries. Change the
11454 misleading `void' type to something sensible. */
11455 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11456 SYMBOL_TYPE (sym)
11457 = objfile_type (objfile)->nodebug_data_symbol;
11458
11459 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11460 /* In the case of DW_TAG_member, we should only be called for
11461 static const members. */
11462 if (die->tag == DW_TAG_member)
11463 {
11464 /* dwarf2_add_field uses die_is_declaration,
11465 so we do the same. */
11466 gdb_assert (die_is_declaration (die, cu));
11467 gdb_assert (attr);
11468 }
11469 if (attr)
11470 {
11471 dwarf2_const_value (attr, sym, cu);
11472 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11473 if (!suppress_add)
11474 {
11475 if (attr2 && (DW_UNSND (attr2) != 0))
11476 list_to_add = &global_symbols;
11477 else
11478 list_to_add = cu->list_in_scope;
11479 }
11480 break;
11481 }
11482 attr = dwarf2_attr (die, DW_AT_location, cu);
11483 if (attr)
11484 {
11485 var_decode_location (attr, sym, cu);
11486 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11487 if (SYMBOL_CLASS (sym) == LOC_STATIC
11488 && SYMBOL_VALUE_ADDRESS (sym) == 0
11489 && !dwarf2_per_objfile->has_section_at_zero)
11490 {
11491 /* When a static variable is eliminated by the linker,
11492 the corresponding debug information is not stripped
11493 out, but the variable address is set to null;
11494 do not add such variables into symbol table. */
11495 }
11496 else if (attr2 && (DW_UNSND (attr2) != 0))
11497 {
11498 /* Workaround gfortran PR debug/40040 - it uses
11499 DW_AT_location for variables in -fPIC libraries which may
11500 get overriden by other libraries/executable and get
11501 a different address. Resolve it by the minimal symbol
11502 which may come from inferior's executable using copy
11503 relocation. Make this workaround only for gfortran as for
11504 other compilers GDB cannot guess the minimal symbol
11505 Fortran mangling kind. */
11506 if (cu->language == language_fortran && die->parent
11507 && die->parent->tag == DW_TAG_module
11508 && cu->producer
11509 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11510 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11511
11512 /* A variable with DW_AT_external is never static,
11513 but it may be block-scoped. */
11514 list_to_add = (cu->list_in_scope == &file_symbols
11515 ? &global_symbols : cu->list_in_scope);
11516 }
11517 else
11518 list_to_add = cu->list_in_scope;
11519 }
11520 else
11521 {
11522 /* We do not know the address of this symbol.
11523 If it is an external symbol and we have type information
11524 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11525 The address of the variable will then be determined from
11526 the minimal symbol table whenever the variable is
11527 referenced. */
11528 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11529 if (attr2 && (DW_UNSND (attr2) != 0)
11530 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11531 {
11532 /* A variable with DW_AT_external is never static, but it
11533 may be block-scoped. */
11534 list_to_add = (cu->list_in_scope == &file_symbols
11535 ? &global_symbols : cu->list_in_scope);
11536
11537 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11538 }
11539 else if (!die_is_declaration (die, cu))
11540 {
11541 /* Use the default LOC_OPTIMIZED_OUT class. */
11542 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11543 if (!suppress_add)
11544 list_to_add = cu->list_in_scope;
11545 }
11546 }
11547 break;
11548 case DW_TAG_formal_parameter:
11549 /* If we are inside a function, mark this as an argument. If
11550 not, we might be looking at an argument to an inlined function
11551 when we do not have enough information to show inlined frames;
11552 pretend it's a local variable in that case so that the user can
11553 still see it. */
11554 if (context_stack_depth > 0
11555 && context_stack[context_stack_depth - 1].name != NULL)
11556 SYMBOL_IS_ARGUMENT (sym) = 1;
11557 attr = dwarf2_attr (die, DW_AT_location, cu);
11558 if (attr)
11559 {
11560 var_decode_location (attr, sym, cu);
11561 }
11562 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11563 if (attr)
11564 {
11565 dwarf2_const_value (attr, sym, cu);
11566 }
11567 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11568 if (attr && DW_UNSND (attr))
11569 {
11570 struct type *ref_type;
11571
11572 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11573 SYMBOL_TYPE (sym) = ref_type;
11574 }
11575
11576 list_to_add = cu->list_in_scope;
11577 break;
11578 case DW_TAG_unspecified_parameters:
11579 /* From varargs functions; gdb doesn't seem to have any
11580 interest in this information, so just ignore it for now.
11581 (FIXME?) */
11582 break;
11583 case DW_TAG_template_type_param:
11584 suppress_add = 1;
11585 /* Fall through. */
11586 case DW_TAG_class_type:
11587 case DW_TAG_interface_type:
11588 case DW_TAG_structure_type:
11589 case DW_TAG_union_type:
11590 case DW_TAG_set_type:
11591 case DW_TAG_enumeration_type:
11592 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11593 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11594
11595 {
11596 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11597 really ever be static objects: otherwise, if you try
11598 to, say, break of a class's method and you're in a file
11599 which doesn't mention that class, it won't work unless
11600 the check for all static symbols in lookup_symbol_aux
11601 saves you. See the OtherFileClass tests in
11602 gdb.c++/namespace.exp. */
11603
11604 if (!suppress_add)
11605 {
11606 list_to_add = (cu->list_in_scope == &file_symbols
11607 && (cu->language == language_cplus
11608 || cu->language == language_java)
11609 ? &global_symbols : cu->list_in_scope);
11610
11611 /* The semantics of C++ state that "struct foo {
11612 ... }" also defines a typedef for "foo". A Java
11613 class declaration also defines a typedef for the
11614 class. */
11615 if (cu->language == language_cplus
11616 || cu->language == language_java
11617 || cu->language == language_ada)
11618 {
11619 /* The symbol's name is already allocated along
11620 with this objfile, so we don't need to
11621 duplicate it for the type. */
11622 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11623 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11624 }
11625 }
11626 }
11627 break;
11628 case DW_TAG_typedef:
11629 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11630 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11631 list_to_add = cu->list_in_scope;
11632 break;
11633 case DW_TAG_base_type:
11634 case DW_TAG_subrange_type:
11635 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11636 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11637 list_to_add = cu->list_in_scope;
11638 break;
11639 case DW_TAG_enumerator:
11640 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11641 if (attr)
11642 {
11643 dwarf2_const_value (attr, sym, cu);
11644 }
11645 {
11646 /* NOTE: carlton/2003-11-10: See comment above in the
11647 DW_TAG_class_type, etc. block. */
11648
11649 list_to_add = (cu->list_in_scope == &file_symbols
11650 && (cu->language == language_cplus
11651 || cu->language == language_java)
11652 ? &global_symbols : cu->list_in_scope);
11653 }
11654 break;
11655 case DW_TAG_namespace:
11656 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11657 list_to_add = &global_symbols;
11658 break;
11659 default:
11660 /* Not a tag we recognize. Hopefully we aren't processing
11661 trash data, but since we must specifically ignore things
11662 we don't recognize, there is nothing else we should do at
11663 this point. */
11664 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11665 dwarf_tag_name (die->tag));
11666 break;
11667 }
11668
11669 if (suppress_add)
11670 {
11671 sym->hash_next = objfile->template_symbols;
11672 objfile->template_symbols = sym;
11673 list_to_add = NULL;
11674 }
11675
11676 if (list_to_add != NULL)
11677 add_symbol_to_list (sym, list_to_add);
11678
11679 /* For the benefit of old versions of GCC, check for anonymous
11680 namespaces based on the demangled name. */
11681 if (!processing_has_namespace_info
11682 && cu->language == language_cplus)
11683 cp_scan_for_anonymous_namespaces (sym);
11684 }
11685 return (sym);
11686 }
11687
11688 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11689
11690 static struct symbol *
11691 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11692 {
11693 return new_symbol_full (die, type, cu, NULL);
11694 }
11695
11696 /* Given an attr with a DW_FORM_dataN value in host byte order,
11697 zero-extend it as appropriate for the symbol's type. The DWARF
11698 standard (v4) is not entirely clear about the meaning of using
11699 DW_FORM_dataN for a constant with a signed type, where the type is
11700 wider than the data. The conclusion of a discussion on the DWARF
11701 list was that this is unspecified. We choose to always zero-extend
11702 because that is the interpretation long in use by GCC. */
11703
11704 static gdb_byte *
11705 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11706 const char *name, struct obstack *obstack,
11707 struct dwarf2_cu *cu, long *value, int bits)
11708 {
11709 struct objfile *objfile = cu->objfile;
11710 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11711 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11712 LONGEST l = DW_UNSND (attr);
11713
11714 if (bits < sizeof (*value) * 8)
11715 {
11716 l &= ((LONGEST) 1 << bits) - 1;
11717 *value = l;
11718 }
11719 else if (bits == sizeof (*value) * 8)
11720 *value = l;
11721 else
11722 {
11723 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11724 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11725 return bytes;
11726 }
11727
11728 return NULL;
11729 }
11730
11731 /* Read a constant value from an attribute. Either set *VALUE, or if
11732 the value does not fit in *VALUE, set *BYTES - either already
11733 allocated on the objfile obstack, or newly allocated on OBSTACK,
11734 or, set *BATON, if we translated the constant to a location
11735 expression. */
11736
11737 static void
11738 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11739 const char *name, struct obstack *obstack,
11740 struct dwarf2_cu *cu,
11741 long *value, gdb_byte **bytes,
11742 struct dwarf2_locexpr_baton **baton)
11743 {
11744 struct objfile *objfile = cu->objfile;
11745 struct comp_unit_head *cu_header = &cu->header;
11746 struct dwarf_block *blk;
11747 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11748 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11749
11750 *value = 0;
11751 *bytes = NULL;
11752 *baton = NULL;
11753
11754 switch (attr->form)
11755 {
11756 case DW_FORM_addr:
11757 {
11758 gdb_byte *data;
11759
11760 if (TYPE_LENGTH (type) != cu_header->addr_size)
11761 dwarf2_const_value_length_mismatch_complaint (name,
11762 cu_header->addr_size,
11763 TYPE_LENGTH (type));
11764 /* Symbols of this form are reasonably rare, so we just
11765 piggyback on the existing location code rather than writing
11766 a new implementation of symbol_computed_ops. */
11767 *baton = obstack_alloc (&objfile->objfile_obstack,
11768 sizeof (struct dwarf2_locexpr_baton));
11769 (*baton)->per_cu = cu->per_cu;
11770 gdb_assert ((*baton)->per_cu);
11771
11772 (*baton)->size = 2 + cu_header->addr_size;
11773 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11774 (*baton)->data = data;
11775
11776 data[0] = DW_OP_addr;
11777 store_unsigned_integer (&data[1], cu_header->addr_size,
11778 byte_order, DW_ADDR (attr));
11779 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11780 }
11781 break;
11782 case DW_FORM_string:
11783 case DW_FORM_strp:
11784 /* DW_STRING is already allocated on the objfile obstack, point
11785 directly to it. */
11786 *bytes = (gdb_byte *) DW_STRING (attr);
11787 break;
11788 case DW_FORM_block1:
11789 case DW_FORM_block2:
11790 case DW_FORM_block4:
11791 case DW_FORM_block:
11792 case DW_FORM_exprloc:
11793 blk = DW_BLOCK (attr);
11794 if (TYPE_LENGTH (type) != blk->size)
11795 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11796 TYPE_LENGTH (type));
11797 *bytes = blk->data;
11798 break;
11799
11800 /* The DW_AT_const_value attributes are supposed to carry the
11801 symbol's value "represented as it would be on the target
11802 architecture." By the time we get here, it's already been
11803 converted to host endianness, so we just need to sign- or
11804 zero-extend it as appropriate. */
11805 case DW_FORM_data1:
11806 *bytes = dwarf2_const_value_data (attr, type, name,
11807 obstack, cu, value, 8);
11808 break;
11809 case DW_FORM_data2:
11810 *bytes = dwarf2_const_value_data (attr, type, name,
11811 obstack, cu, value, 16);
11812 break;
11813 case DW_FORM_data4:
11814 *bytes = dwarf2_const_value_data (attr, type, name,
11815 obstack, cu, value, 32);
11816 break;
11817 case DW_FORM_data8:
11818 *bytes = dwarf2_const_value_data (attr, type, name,
11819 obstack, cu, value, 64);
11820 break;
11821
11822 case DW_FORM_sdata:
11823 *value = DW_SND (attr);
11824 break;
11825
11826 case DW_FORM_udata:
11827 *value = DW_UNSND (attr);
11828 break;
11829
11830 default:
11831 complaint (&symfile_complaints,
11832 _("unsupported const value attribute form: '%s'"),
11833 dwarf_form_name (attr->form));
11834 *value = 0;
11835 break;
11836 }
11837 }
11838
11839
11840 /* Copy constant value from an attribute to a symbol. */
11841
11842 static void
11843 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11844 struct dwarf2_cu *cu)
11845 {
11846 struct objfile *objfile = cu->objfile;
11847 struct comp_unit_head *cu_header = &cu->header;
11848 long value;
11849 gdb_byte *bytes;
11850 struct dwarf2_locexpr_baton *baton;
11851
11852 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11853 SYMBOL_PRINT_NAME (sym),
11854 &objfile->objfile_obstack, cu,
11855 &value, &bytes, &baton);
11856
11857 if (baton != NULL)
11858 {
11859 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11860 SYMBOL_LOCATION_BATON (sym) = baton;
11861 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11862 }
11863 else if (bytes != NULL)
11864 {
11865 SYMBOL_VALUE_BYTES (sym) = bytes;
11866 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11867 }
11868 else
11869 {
11870 SYMBOL_VALUE (sym) = value;
11871 SYMBOL_CLASS (sym) = LOC_CONST;
11872 }
11873 }
11874
11875 /* Return the type of the die in question using its DW_AT_type attribute. */
11876
11877 static struct type *
11878 die_type (struct die_info *die, struct dwarf2_cu *cu)
11879 {
11880 struct attribute *type_attr;
11881
11882 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11883 if (!type_attr)
11884 {
11885 /* A missing DW_AT_type represents a void type. */
11886 return objfile_type (cu->objfile)->builtin_void;
11887 }
11888
11889 return lookup_die_type (die, type_attr, cu);
11890 }
11891
11892 /* True iff CU's producer generates GNAT Ada auxiliary information
11893 that allows to find parallel types through that information instead
11894 of having to do expensive parallel lookups by type name. */
11895
11896 static int
11897 need_gnat_info (struct dwarf2_cu *cu)
11898 {
11899 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11900 of GNAT produces this auxiliary information, without any indication
11901 that it is produced. Part of enhancing the FSF version of GNAT
11902 to produce that information will be to put in place an indicator
11903 that we can use in order to determine whether the descriptive type
11904 info is available or not. One suggestion that has been made is
11905 to use a new attribute, attached to the CU die. For now, assume
11906 that the descriptive type info is not available. */
11907 return 0;
11908 }
11909
11910 /* Return the auxiliary type of the die in question using its
11911 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11912 attribute is not present. */
11913
11914 static struct type *
11915 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11916 {
11917 struct attribute *type_attr;
11918
11919 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11920 if (!type_attr)
11921 return NULL;
11922
11923 return lookup_die_type (die, type_attr, cu);
11924 }
11925
11926 /* If DIE has a descriptive_type attribute, then set the TYPE's
11927 descriptive type accordingly. */
11928
11929 static void
11930 set_descriptive_type (struct type *type, struct die_info *die,
11931 struct dwarf2_cu *cu)
11932 {
11933 struct type *descriptive_type = die_descriptive_type (die, cu);
11934
11935 if (descriptive_type)
11936 {
11937 ALLOCATE_GNAT_AUX_TYPE (type);
11938 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11939 }
11940 }
11941
11942 /* Return the containing type of the die in question using its
11943 DW_AT_containing_type attribute. */
11944
11945 static struct type *
11946 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11947 {
11948 struct attribute *type_attr;
11949
11950 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11951 if (!type_attr)
11952 error (_("Dwarf Error: Problem turning containing type into gdb type "
11953 "[in module %s]"), cu->objfile->name);
11954
11955 return lookup_die_type (die, type_attr, cu);
11956 }
11957
11958 /* Look up the type of DIE in CU using its type attribute ATTR.
11959 If there is no type substitute an error marker. */
11960
11961 static struct type *
11962 lookup_die_type (struct die_info *die, struct attribute *attr,
11963 struct dwarf2_cu *cu)
11964 {
11965 struct type *this_type;
11966
11967 /* First see if we have it cached. */
11968
11969 if (is_ref_attr (attr))
11970 {
11971 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11972
11973 this_type = get_die_type_at_offset (offset, cu->per_cu);
11974 }
11975 else if (attr->form == DW_FORM_ref_sig8)
11976 {
11977 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11978 struct dwarf2_cu *sig_cu;
11979 unsigned int offset;
11980
11981 /* sig_type will be NULL if the signatured type is missing from
11982 the debug info. */
11983 if (sig_type == NULL)
11984 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11985 "at 0x%x [in module %s]"),
11986 die->offset, cu->objfile->name);
11987
11988 gdb_assert (sig_type->per_cu.debug_type_section);
11989 offset = sig_type->per_cu.offset + sig_type->type_offset;
11990 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11991 }
11992 else
11993 {
11994 dump_die_for_error (die);
11995 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11996 dwarf_attr_name (attr->name), cu->objfile->name);
11997 }
11998
11999 /* If not cached we need to read it in. */
12000
12001 if (this_type == NULL)
12002 {
12003 struct die_info *type_die;
12004 struct dwarf2_cu *type_cu = cu;
12005
12006 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12007 /* If the type is cached, we should have found it above. */
12008 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12009 this_type = read_type_die_1 (type_die, type_cu);
12010 }
12011
12012 /* If we still don't have a type use an error marker. */
12013
12014 if (this_type == NULL)
12015 {
12016 char *message, *saved;
12017
12018 /* read_type_die already issued a complaint. */
12019 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12020 cu->objfile->name,
12021 cu->header.offset,
12022 die->offset);
12023 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12024 message, strlen (message));
12025 xfree (message);
12026
12027 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12028 }
12029
12030 return this_type;
12031 }
12032
12033 /* Return the type in DIE, CU.
12034 Returns NULL for invalid types.
12035
12036 This first does a lookup in the appropriate type_hash table,
12037 and only reads the die in if necessary.
12038
12039 NOTE: This can be called when reading in partial or full symbols. */
12040
12041 static struct type *
12042 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12043 {
12044 struct type *this_type;
12045
12046 this_type = get_die_type (die, cu);
12047 if (this_type)
12048 return this_type;
12049
12050 return read_type_die_1 (die, cu);
12051 }
12052
12053 /* Read the type in DIE, CU.
12054 Returns NULL for invalid types. */
12055
12056 static struct type *
12057 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12058 {
12059 struct type *this_type = NULL;
12060
12061 switch (die->tag)
12062 {
12063 case DW_TAG_class_type:
12064 case DW_TAG_interface_type:
12065 case DW_TAG_structure_type:
12066 case DW_TAG_union_type:
12067 this_type = read_structure_type (die, cu);
12068 break;
12069 case DW_TAG_enumeration_type:
12070 this_type = read_enumeration_type (die, cu);
12071 break;
12072 case DW_TAG_subprogram:
12073 case DW_TAG_subroutine_type:
12074 case DW_TAG_inlined_subroutine:
12075 this_type = read_subroutine_type (die, cu);
12076 break;
12077 case DW_TAG_array_type:
12078 this_type = read_array_type (die, cu);
12079 break;
12080 case DW_TAG_set_type:
12081 this_type = read_set_type (die, cu);
12082 break;
12083 case DW_TAG_pointer_type:
12084 this_type = read_tag_pointer_type (die, cu);
12085 break;
12086 case DW_TAG_ptr_to_member_type:
12087 this_type = read_tag_ptr_to_member_type (die, cu);
12088 break;
12089 case DW_TAG_reference_type:
12090 this_type = read_tag_reference_type (die, cu);
12091 break;
12092 case DW_TAG_const_type:
12093 this_type = read_tag_const_type (die, cu);
12094 break;
12095 case DW_TAG_volatile_type:
12096 this_type = read_tag_volatile_type (die, cu);
12097 break;
12098 case DW_TAG_string_type:
12099 this_type = read_tag_string_type (die, cu);
12100 break;
12101 case DW_TAG_typedef:
12102 this_type = read_typedef (die, cu);
12103 break;
12104 case DW_TAG_subrange_type:
12105 this_type = read_subrange_type (die, cu);
12106 break;
12107 case DW_TAG_base_type:
12108 this_type = read_base_type (die, cu);
12109 break;
12110 case DW_TAG_unspecified_type:
12111 this_type = read_unspecified_type (die, cu);
12112 break;
12113 case DW_TAG_namespace:
12114 this_type = read_namespace_type (die, cu);
12115 break;
12116 case DW_TAG_module:
12117 this_type = read_module_type (die, cu);
12118 break;
12119 default:
12120 complaint (&symfile_complaints,
12121 _("unexpected tag in read_type_die: '%s'"),
12122 dwarf_tag_name (die->tag));
12123 break;
12124 }
12125
12126 return this_type;
12127 }
12128
12129 /* See if we can figure out if the class lives in a namespace. We do
12130 this by looking for a member function; its demangled name will
12131 contain namespace info, if there is any.
12132 Return the computed name or NULL.
12133 Space for the result is allocated on the objfile's obstack.
12134 This is the full-die version of guess_partial_die_structure_name.
12135 In this case we know DIE has no useful parent. */
12136
12137 static char *
12138 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12139 {
12140 struct die_info *spec_die;
12141 struct dwarf2_cu *spec_cu;
12142 struct die_info *child;
12143
12144 spec_cu = cu;
12145 spec_die = die_specification (die, &spec_cu);
12146 if (spec_die != NULL)
12147 {
12148 die = spec_die;
12149 cu = spec_cu;
12150 }
12151
12152 for (child = die->child;
12153 child != NULL;
12154 child = child->sibling)
12155 {
12156 if (child->tag == DW_TAG_subprogram)
12157 {
12158 struct attribute *attr;
12159
12160 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12161 if (attr == NULL)
12162 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12163 if (attr != NULL)
12164 {
12165 char *actual_name
12166 = language_class_name_from_physname (cu->language_defn,
12167 DW_STRING (attr));
12168 char *name = NULL;
12169
12170 if (actual_name != NULL)
12171 {
12172 char *die_name = dwarf2_name (die, cu);
12173
12174 if (die_name != NULL
12175 && strcmp (die_name, actual_name) != 0)
12176 {
12177 /* Strip off the class name from the full name.
12178 We want the prefix. */
12179 int die_name_len = strlen (die_name);
12180 int actual_name_len = strlen (actual_name);
12181
12182 /* Test for '::' as a sanity check. */
12183 if (actual_name_len > die_name_len + 2
12184 && actual_name[actual_name_len
12185 - die_name_len - 1] == ':')
12186 name =
12187 obsavestring (actual_name,
12188 actual_name_len - die_name_len - 2,
12189 &cu->objfile->objfile_obstack);
12190 }
12191 }
12192 xfree (actual_name);
12193 return name;
12194 }
12195 }
12196 }
12197
12198 return NULL;
12199 }
12200
12201 /* Return the name of the namespace/class that DIE is defined within,
12202 or "" if we can't tell. The caller should not xfree the result.
12203
12204 For example, if we're within the method foo() in the following
12205 code:
12206
12207 namespace N {
12208 class C {
12209 void foo () {
12210 }
12211 };
12212 }
12213
12214 then determine_prefix on foo's die will return "N::C". */
12215
12216 static char *
12217 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12218 {
12219 struct die_info *parent, *spec_die;
12220 struct dwarf2_cu *spec_cu;
12221 struct type *parent_type;
12222
12223 if (cu->language != language_cplus && cu->language != language_java
12224 && cu->language != language_fortran)
12225 return "";
12226
12227 /* We have to be careful in the presence of DW_AT_specification.
12228 For example, with GCC 3.4, given the code
12229
12230 namespace N {
12231 void foo() {
12232 // Definition of N::foo.
12233 }
12234 }
12235
12236 then we'll have a tree of DIEs like this:
12237
12238 1: DW_TAG_compile_unit
12239 2: DW_TAG_namespace // N
12240 3: DW_TAG_subprogram // declaration of N::foo
12241 4: DW_TAG_subprogram // definition of N::foo
12242 DW_AT_specification // refers to die #3
12243
12244 Thus, when processing die #4, we have to pretend that we're in
12245 the context of its DW_AT_specification, namely the contex of die
12246 #3. */
12247 spec_cu = cu;
12248 spec_die = die_specification (die, &spec_cu);
12249 if (spec_die == NULL)
12250 parent = die->parent;
12251 else
12252 {
12253 parent = spec_die->parent;
12254 cu = spec_cu;
12255 }
12256
12257 if (parent == NULL)
12258 return "";
12259 else if (parent->building_fullname)
12260 {
12261 const char *name;
12262 const char *parent_name;
12263
12264 /* It has been seen on RealView 2.2 built binaries,
12265 DW_TAG_template_type_param types actually _defined_ as
12266 children of the parent class:
12267
12268 enum E {};
12269 template class <class Enum> Class{};
12270 Class<enum E> class_e;
12271
12272 1: DW_TAG_class_type (Class)
12273 2: DW_TAG_enumeration_type (E)
12274 3: DW_TAG_enumerator (enum1:0)
12275 3: DW_TAG_enumerator (enum2:1)
12276 ...
12277 2: DW_TAG_template_type_param
12278 DW_AT_type DW_FORM_ref_udata (E)
12279
12280 Besides being broken debug info, it can put GDB into an
12281 infinite loop. Consider:
12282
12283 When we're building the full name for Class<E>, we'll start
12284 at Class, and go look over its template type parameters,
12285 finding E. We'll then try to build the full name of E, and
12286 reach here. We're now trying to build the full name of E,
12287 and look over the parent DIE for containing scope. In the
12288 broken case, if we followed the parent DIE of E, we'd again
12289 find Class, and once again go look at its template type
12290 arguments, etc., etc. Simply don't consider such parent die
12291 as source-level parent of this die (it can't be, the language
12292 doesn't allow it), and break the loop here. */
12293 name = dwarf2_name (die, cu);
12294 parent_name = dwarf2_name (parent, cu);
12295 complaint (&symfile_complaints,
12296 _("template param type '%s' defined within parent '%s'"),
12297 name ? name : "<unknown>",
12298 parent_name ? parent_name : "<unknown>");
12299 return "";
12300 }
12301 else
12302 switch (parent->tag)
12303 {
12304 case DW_TAG_namespace:
12305 parent_type = read_type_die (parent, cu);
12306 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12307 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12308 Work around this problem here. */
12309 if (cu->language == language_cplus
12310 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12311 return "";
12312 /* We give a name to even anonymous namespaces. */
12313 return TYPE_TAG_NAME (parent_type);
12314 case DW_TAG_class_type:
12315 case DW_TAG_interface_type:
12316 case DW_TAG_structure_type:
12317 case DW_TAG_union_type:
12318 case DW_TAG_module:
12319 parent_type = read_type_die (parent, cu);
12320 if (TYPE_TAG_NAME (parent_type) != NULL)
12321 return TYPE_TAG_NAME (parent_type);
12322 else
12323 /* An anonymous structure is only allowed non-static data
12324 members; no typedefs, no member functions, et cetera.
12325 So it does not need a prefix. */
12326 return "";
12327 case DW_TAG_compile_unit:
12328 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12329 if (cu->language == language_cplus
12330 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12331 && die->child != NULL
12332 && (die->tag == DW_TAG_class_type
12333 || die->tag == DW_TAG_structure_type
12334 || die->tag == DW_TAG_union_type))
12335 {
12336 char *name = guess_full_die_structure_name (die, cu);
12337 if (name != NULL)
12338 return name;
12339 }
12340 return "";
12341 default:
12342 return determine_prefix (parent, cu);
12343 }
12344 }
12345
12346 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12347 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12348 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12349 an obconcat, otherwise allocate storage for the result. The CU argument is
12350 used to determine the language and hence, the appropriate separator. */
12351
12352 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12353
12354 static char *
12355 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12356 int physname, struct dwarf2_cu *cu)
12357 {
12358 const char *lead = "";
12359 const char *sep;
12360
12361 if (suffix == NULL || suffix[0] == '\0'
12362 || prefix == NULL || prefix[0] == '\0')
12363 sep = "";
12364 else if (cu->language == language_java)
12365 sep = ".";
12366 else if (cu->language == language_fortran && physname)
12367 {
12368 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12369 DW_AT_MIPS_linkage_name is preferred and used instead. */
12370
12371 lead = "__";
12372 sep = "_MOD_";
12373 }
12374 else
12375 sep = "::";
12376
12377 if (prefix == NULL)
12378 prefix = "";
12379 if (suffix == NULL)
12380 suffix = "";
12381
12382 if (obs == NULL)
12383 {
12384 char *retval
12385 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12386
12387 strcpy (retval, lead);
12388 strcat (retval, prefix);
12389 strcat (retval, sep);
12390 strcat (retval, suffix);
12391 return retval;
12392 }
12393 else
12394 {
12395 /* We have an obstack. */
12396 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12397 }
12398 }
12399
12400 /* Return sibling of die, NULL if no sibling. */
12401
12402 static struct die_info *
12403 sibling_die (struct die_info *die)
12404 {
12405 return die->sibling;
12406 }
12407
12408 /* Get name of a die, return NULL if not found. */
12409
12410 static char *
12411 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12412 struct obstack *obstack)
12413 {
12414 if (name && cu->language == language_cplus)
12415 {
12416 char *canon_name = cp_canonicalize_string (name);
12417
12418 if (canon_name != NULL)
12419 {
12420 if (strcmp (canon_name, name) != 0)
12421 name = obsavestring (canon_name, strlen (canon_name),
12422 obstack);
12423 xfree (canon_name);
12424 }
12425 }
12426
12427 return name;
12428 }
12429
12430 /* Get name of a die, return NULL if not found. */
12431
12432 static char *
12433 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12434 {
12435 struct attribute *attr;
12436
12437 attr = dwarf2_attr (die, DW_AT_name, cu);
12438 if ((!attr || !DW_STRING (attr))
12439 && die->tag != DW_TAG_class_type
12440 && die->tag != DW_TAG_interface_type
12441 && die->tag != DW_TAG_structure_type
12442 && die->tag != DW_TAG_union_type)
12443 return NULL;
12444
12445 switch (die->tag)
12446 {
12447 case DW_TAG_compile_unit:
12448 /* Compilation units have a DW_AT_name that is a filename, not
12449 a source language identifier. */
12450 case DW_TAG_enumeration_type:
12451 case DW_TAG_enumerator:
12452 /* These tags always have simple identifiers already; no need
12453 to canonicalize them. */
12454 return DW_STRING (attr);
12455
12456 case DW_TAG_subprogram:
12457 /* Java constructors will all be named "<init>", so return
12458 the class name when we see this special case. */
12459 if (cu->language == language_java
12460 && DW_STRING (attr) != NULL
12461 && strcmp (DW_STRING (attr), "<init>") == 0)
12462 {
12463 struct dwarf2_cu *spec_cu = cu;
12464 struct die_info *spec_die;
12465
12466 /* GCJ will output '<init>' for Java constructor names.
12467 For this special case, return the name of the parent class. */
12468
12469 /* GCJ may output suprogram DIEs with AT_specification set.
12470 If so, use the name of the specified DIE. */
12471 spec_die = die_specification (die, &spec_cu);
12472 if (spec_die != NULL)
12473 return dwarf2_name (spec_die, spec_cu);
12474
12475 do
12476 {
12477 die = die->parent;
12478 if (die->tag == DW_TAG_class_type)
12479 return dwarf2_name (die, cu);
12480 }
12481 while (die->tag != DW_TAG_compile_unit);
12482 }
12483 break;
12484
12485 case DW_TAG_class_type:
12486 case DW_TAG_interface_type:
12487 case DW_TAG_structure_type:
12488 case DW_TAG_union_type:
12489 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12490 structures or unions. These were of the form "._%d" in GCC 4.1,
12491 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12492 and GCC 4.4. We work around this problem by ignoring these. */
12493 if (attr && DW_STRING (attr)
12494 && (strncmp (DW_STRING (attr), "._", 2) == 0
12495 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12496 return NULL;
12497
12498 /* GCC might emit a nameless typedef that has a linkage name. See
12499 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12500 if (!attr || DW_STRING (attr) == NULL)
12501 {
12502 char *demangled = NULL;
12503
12504 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12505 if (attr == NULL)
12506 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12507
12508 if (attr == NULL || DW_STRING (attr) == NULL)
12509 return NULL;
12510
12511 /* Avoid demangling DW_STRING (attr) the second time on a second
12512 call for the same DIE. */
12513 if (!DW_STRING_IS_CANONICAL (attr))
12514 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12515
12516 if (demangled)
12517 {
12518 /* FIXME: we already did this for the partial symbol... */
12519 DW_STRING (attr)
12520 = obsavestring (demangled, strlen (demangled),
12521 &cu->objfile->objfile_obstack);
12522 DW_STRING_IS_CANONICAL (attr) = 1;
12523 xfree (demangled);
12524 }
12525 }
12526 break;
12527
12528 default:
12529 break;
12530 }
12531
12532 if (!DW_STRING_IS_CANONICAL (attr))
12533 {
12534 DW_STRING (attr)
12535 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12536 &cu->objfile->objfile_obstack);
12537 DW_STRING_IS_CANONICAL (attr) = 1;
12538 }
12539 return DW_STRING (attr);
12540 }
12541
12542 /* Return the die that this die in an extension of, or NULL if there
12543 is none. *EXT_CU is the CU containing DIE on input, and the CU
12544 containing the return value on output. */
12545
12546 static struct die_info *
12547 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12548 {
12549 struct attribute *attr;
12550
12551 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12552 if (attr == NULL)
12553 return NULL;
12554
12555 return follow_die_ref (die, attr, ext_cu);
12556 }
12557
12558 /* Convert a DIE tag into its string name. */
12559
12560 static char *
12561 dwarf_tag_name (unsigned tag)
12562 {
12563 switch (tag)
12564 {
12565 case DW_TAG_padding:
12566 return "DW_TAG_padding";
12567 case DW_TAG_array_type:
12568 return "DW_TAG_array_type";
12569 case DW_TAG_class_type:
12570 return "DW_TAG_class_type";
12571 case DW_TAG_entry_point:
12572 return "DW_TAG_entry_point";
12573 case DW_TAG_enumeration_type:
12574 return "DW_TAG_enumeration_type";
12575 case DW_TAG_formal_parameter:
12576 return "DW_TAG_formal_parameter";
12577 case DW_TAG_imported_declaration:
12578 return "DW_TAG_imported_declaration";
12579 case DW_TAG_label:
12580 return "DW_TAG_label";
12581 case DW_TAG_lexical_block:
12582 return "DW_TAG_lexical_block";
12583 case DW_TAG_member:
12584 return "DW_TAG_member";
12585 case DW_TAG_pointer_type:
12586 return "DW_TAG_pointer_type";
12587 case DW_TAG_reference_type:
12588 return "DW_TAG_reference_type";
12589 case DW_TAG_compile_unit:
12590 return "DW_TAG_compile_unit";
12591 case DW_TAG_string_type:
12592 return "DW_TAG_string_type";
12593 case DW_TAG_structure_type:
12594 return "DW_TAG_structure_type";
12595 case DW_TAG_subroutine_type:
12596 return "DW_TAG_subroutine_type";
12597 case DW_TAG_typedef:
12598 return "DW_TAG_typedef";
12599 case DW_TAG_union_type:
12600 return "DW_TAG_union_type";
12601 case DW_TAG_unspecified_parameters:
12602 return "DW_TAG_unspecified_parameters";
12603 case DW_TAG_variant:
12604 return "DW_TAG_variant";
12605 case DW_TAG_common_block:
12606 return "DW_TAG_common_block";
12607 case DW_TAG_common_inclusion:
12608 return "DW_TAG_common_inclusion";
12609 case DW_TAG_inheritance:
12610 return "DW_TAG_inheritance";
12611 case DW_TAG_inlined_subroutine:
12612 return "DW_TAG_inlined_subroutine";
12613 case DW_TAG_module:
12614 return "DW_TAG_module";
12615 case DW_TAG_ptr_to_member_type:
12616 return "DW_TAG_ptr_to_member_type";
12617 case DW_TAG_set_type:
12618 return "DW_TAG_set_type";
12619 case DW_TAG_subrange_type:
12620 return "DW_TAG_subrange_type";
12621 case DW_TAG_with_stmt:
12622 return "DW_TAG_with_stmt";
12623 case DW_TAG_access_declaration:
12624 return "DW_TAG_access_declaration";
12625 case DW_TAG_base_type:
12626 return "DW_TAG_base_type";
12627 case DW_TAG_catch_block:
12628 return "DW_TAG_catch_block";
12629 case DW_TAG_const_type:
12630 return "DW_TAG_const_type";
12631 case DW_TAG_constant:
12632 return "DW_TAG_constant";
12633 case DW_TAG_enumerator:
12634 return "DW_TAG_enumerator";
12635 case DW_TAG_file_type:
12636 return "DW_TAG_file_type";
12637 case DW_TAG_friend:
12638 return "DW_TAG_friend";
12639 case DW_TAG_namelist:
12640 return "DW_TAG_namelist";
12641 case DW_TAG_namelist_item:
12642 return "DW_TAG_namelist_item";
12643 case DW_TAG_packed_type:
12644 return "DW_TAG_packed_type";
12645 case DW_TAG_subprogram:
12646 return "DW_TAG_subprogram";
12647 case DW_TAG_template_type_param:
12648 return "DW_TAG_template_type_param";
12649 case DW_TAG_template_value_param:
12650 return "DW_TAG_template_value_param";
12651 case DW_TAG_thrown_type:
12652 return "DW_TAG_thrown_type";
12653 case DW_TAG_try_block:
12654 return "DW_TAG_try_block";
12655 case DW_TAG_variant_part:
12656 return "DW_TAG_variant_part";
12657 case DW_TAG_variable:
12658 return "DW_TAG_variable";
12659 case DW_TAG_volatile_type:
12660 return "DW_TAG_volatile_type";
12661 case DW_TAG_dwarf_procedure:
12662 return "DW_TAG_dwarf_procedure";
12663 case DW_TAG_restrict_type:
12664 return "DW_TAG_restrict_type";
12665 case DW_TAG_interface_type:
12666 return "DW_TAG_interface_type";
12667 case DW_TAG_namespace:
12668 return "DW_TAG_namespace";
12669 case DW_TAG_imported_module:
12670 return "DW_TAG_imported_module";
12671 case DW_TAG_unspecified_type:
12672 return "DW_TAG_unspecified_type";
12673 case DW_TAG_partial_unit:
12674 return "DW_TAG_partial_unit";
12675 case DW_TAG_imported_unit:
12676 return "DW_TAG_imported_unit";
12677 case DW_TAG_condition:
12678 return "DW_TAG_condition";
12679 case DW_TAG_shared_type:
12680 return "DW_TAG_shared_type";
12681 case DW_TAG_type_unit:
12682 return "DW_TAG_type_unit";
12683 case DW_TAG_MIPS_loop:
12684 return "DW_TAG_MIPS_loop";
12685 case DW_TAG_HP_array_descriptor:
12686 return "DW_TAG_HP_array_descriptor";
12687 case DW_TAG_format_label:
12688 return "DW_TAG_format_label";
12689 case DW_TAG_function_template:
12690 return "DW_TAG_function_template";
12691 case DW_TAG_class_template:
12692 return "DW_TAG_class_template";
12693 case DW_TAG_GNU_BINCL:
12694 return "DW_TAG_GNU_BINCL";
12695 case DW_TAG_GNU_EINCL:
12696 return "DW_TAG_GNU_EINCL";
12697 case DW_TAG_upc_shared_type:
12698 return "DW_TAG_upc_shared_type";
12699 case DW_TAG_upc_strict_type:
12700 return "DW_TAG_upc_strict_type";
12701 case DW_TAG_upc_relaxed_type:
12702 return "DW_TAG_upc_relaxed_type";
12703 case DW_TAG_PGI_kanji_type:
12704 return "DW_TAG_PGI_kanji_type";
12705 case DW_TAG_PGI_interface_block:
12706 return "DW_TAG_PGI_interface_block";
12707 default:
12708 return "DW_TAG_<unknown>";
12709 }
12710 }
12711
12712 /* Convert a DWARF attribute code into its string name. */
12713
12714 static char *
12715 dwarf_attr_name (unsigned attr)
12716 {
12717 switch (attr)
12718 {
12719 case DW_AT_sibling:
12720 return "DW_AT_sibling";
12721 case DW_AT_location:
12722 return "DW_AT_location";
12723 case DW_AT_name:
12724 return "DW_AT_name";
12725 case DW_AT_ordering:
12726 return "DW_AT_ordering";
12727 case DW_AT_subscr_data:
12728 return "DW_AT_subscr_data";
12729 case DW_AT_byte_size:
12730 return "DW_AT_byte_size";
12731 case DW_AT_bit_offset:
12732 return "DW_AT_bit_offset";
12733 case DW_AT_bit_size:
12734 return "DW_AT_bit_size";
12735 case DW_AT_element_list:
12736 return "DW_AT_element_list";
12737 case DW_AT_stmt_list:
12738 return "DW_AT_stmt_list";
12739 case DW_AT_low_pc:
12740 return "DW_AT_low_pc";
12741 case DW_AT_high_pc:
12742 return "DW_AT_high_pc";
12743 case DW_AT_language:
12744 return "DW_AT_language";
12745 case DW_AT_member:
12746 return "DW_AT_member";
12747 case DW_AT_discr:
12748 return "DW_AT_discr";
12749 case DW_AT_discr_value:
12750 return "DW_AT_discr_value";
12751 case DW_AT_visibility:
12752 return "DW_AT_visibility";
12753 case DW_AT_import:
12754 return "DW_AT_import";
12755 case DW_AT_string_length:
12756 return "DW_AT_string_length";
12757 case DW_AT_common_reference:
12758 return "DW_AT_common_reference";
12759 case DW_AT_comp_dir:
12760 return "DW_AT_comp_dir";
12761 case DW_AT_const_value:
12762 return "DW_AT_const_value";
12763 case DW_AT_containing_type:
12764 return "DW_AT_containing_type";
12765 case DW_AT_default_value:
12766 return "DW_AT_default_value";
12767 case DW_AT_inline:
12768 return "DW_AT_inline";
12769 case DW_AT_is_optional:
12770 return "DW_AT_is_optional";
12771 case DW_AT_lower_bound:
12772 return "DW_AT_lower_bound";
12773 case DW_AT_producer:
12774 return "DW_AT_producer";
12775 case DW_AT_prototyped:
12776 return "DW_AT_prototyped";
12777 case DW_AT_return_addr:
12778 return "DW_AT_return_addr";
12779 case DW_AT_start_scope:
12780 return "DW_AT_start_scope";
12781 case DW_AT_bit_stride:
12782 return "DW_AT_bit_stride";
12783 case DW_AT_upper_bound:
12784 return "DW_AT_upper_bound";
12785 case DW_AT_abstract_origin:
12786 return "DW_AT_abstract_origin";
12787 case DW_AT_accessibility:
12788 return "DW_AT_accessibility";
12789 case DW_AT_address_class:
12790 return "DW_AT_address_class";
12791 case DW_AT_artificial:
12792 return "DW_AT_artificial";
12793 case DW_AT_base_types:
12794 return "DW_AT_base_types";
12795 case DW_AT_calling_convention:
12796 return "DW_AT_calling_convention";
12797 case DW_AT_count:
12798 return "DW_AT_count";
12799 case DW_AT_data_member_location:
12800 return "DW_AT_data_member_location";
12801 case DW_AT_decl_column:
12802 return "DW_AT_decl_column";
12803 case DW_AT_decl_file:
12804 return "DW_AT_decl_file";
12805 case DW_AT_decl_line:
12806 return "DW_AT_decl_line";
12807 case DW_AT_declaration:
12808 return "DW_AT_declaration";
12809 case DW_AT_discr_list:
12810 return "DW_AT_discr_list";
12811 case DW_AT_encoding:
12812 return "DW_AT_encoding";
12813 case DW_AT_external:
12814 return "DW_AT_external";
12815 case DW_AT_frame_base:
12816 return "DW_AT_frame_base";
12817 case DW_AT_friend:
12818 return "DW_AT_friend";
12819 case DW_AT_identifier_case:
12820 return "DW_AT_identifier_case";
12821 case DW_AT_macro_info:
12822 return "DW_AT_macro_info";
12823 case DW_AT_namelist_items:
12824 return "DW_AT_namelist_items";
12825 case DW_AT_priority:
12826 return "DW_AT_priority";
12827 case DW_AT_segment:
12828 return "DW_AT_segment";
12829 case DW_AT_specification:
12830 return "DW_AT_specification";
12831 case DW_AT_static_link:
12832 return "DW_AT_static_link";
12833 case DW_AT_type:
12834 return "DW_AT_type";
12835 case DW_AT_use_location:
12836 return "DW_AT_use_location";
12837 case DW_AT_variable_parameter:
12838 return "DW_AT_variable_parameter";
12839 case DW_AT_virtuality:
12840 return "DW_AT_virtuality";
12841 case DW_AT_vtable_elem_location:
12842 return "DW_AT_vtable_elem_location";
12843 /* DWARF 3 values. */
12844 case DW_AT_allocated:
12845 return "DW_AT_allocated";
12846 case DW_AT_associated:
12847 return "DW_AT_associated";
12848 case DW_AT_data_location:
12849 return "DW_AT_data_location";
12850 case DW_AT_byte_stride:
12851 return "DW_AT_byte_stride";
12852 case DW_AT_entry_pc:
12853 return "DW_AT_entry_pc";
12854 case DW_AT_use_UTF8:
12855 return "DW_AT_use_UTF8";
12856 case DW_AT_extension:
12857 return "DW_AT_extension";
12858 case DW_AT_ranges:
12859 return "DW_AT_ranges";
12860 case DW_AT_trampoline:
12861 return "DW_AT_trampoline";
12862 case DW_AT_call_column:
12863 return "DW_AT_call_column";
12864 case DW_AT_call_file:
12865 return "DW_AT_call_file";
12866 case DW_AT_call_line:
12867 return "DW_AT_call_line";
12868 case DW_AT_description:
12869 return "DW_AT_description";
12870 case DW_AT_binary_scale:
12871 return "DW_AT_binary_scale";
12872 case DW_AT_decimal_scale:
12873 return "DW_AT_decimal_scale";
12874 case DW_AT_small:
12875 return "DW_AT_small";
12876 case DW_AT_decimal_sign:
12877 return "DW_AT_decimal_sign";
12878 case DW_AT_digit_count:
12879 return "DW_AT_digit_count";
12880 case DW_AT_picture_string:
12881 return "DW_AT_picture_string";
12882 case DW_AT_mutable:
12883 return "DW_AT_mutable";
12884 case DW_AT_threads_scaled:
12885 return "DW_AT_threads_scaled";
12886 case DW_AT_explicit:
12887 return "DW_AT_explicit";
12888 case DW_AT_object_pointer:
12889 return "DW_AT_object_pointer";
12890 case DW_AT_endianity:
12891 return "DW_AT_endianity";
12892 case DW_AT_elemental:
12893 return "DW_AT_elemental";
12894 case DW_AT_pure:
12895 return "DW_AT_pure";
12896 case DW_AT_recursive:
12897 return "DW_AT_recursive";
12898 /* DWARF 4 values. */
12899 case DW_AT_signature:
12900 return "DW_AT_signature";
12901 case DW_AT_linkage_name:
12902 return "DW_AT_linkage_name";
12903 /* SGI/MIPS extensions. */
12904 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12905 case DW_AT_MIPS_fde:
12906 return "DW_AT_MIPS_fde";
12907 #endif
12908 case DW_AT_MIPS_loop_begin:
12909 return "DW_AT_MIPS_loop_begin";
12910 case DW_AT_MIPS_tail_loop_begin:
12911 return "DW_AT_MIPS_tail_loop_begin";
12912 case DW_AT_MIPS_epilog_begin:
12913 return "DW_AT_MIPS_epilog_begin";
12914 case DW_AT_MIPS_loop_unroll_factor:
12915 return "DW_AT_MIPS_loop_unroll_factor";
12916 case DW_AT_MIPS_software_pipeline_depth:
12917 return "DW_AT_MIPS_software_pipeline_depth";
12918 case DW_AT_MIPS_linkage_name:
12919 return "DW_AT_MIPS_linkage_name";
12920 case DW_AT_MIPS_stride:
12921 return "DW_AT_MIPS_stride";
12922 case DW_AT_MIPS_abstract_name:
12923 return "DW_AT_MIPS_abstract_name";
12924 case DW_AT_MIPS_clone_origin:
12925 return "DW_AT_MIPS_clone_origin";
12926 case DW_AT_MIPS_has_inlines:
12927 return "DW_AT_MIPS_has_inlines";
12928 /* HP extensions. */
12929 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12930 case DW_AT_HP_block_index:
12931 return "DW_AT_HP_block_index";
12932 #endif
12933 case DW_AT_HP_unmodifiable:
12934 return "DW_AT_HP_unmodifiable";
12935 case DW_AT_HP_actuals_stmt_list:
12936 return "DW_AT_HP_actuals_stmt_list";
12937 case DW_AT_HP_proc_per_section:
12938 return "DW_AT_HP_proc_per_section";
12939 case DW_AT_HP_raw_data_ptr:
12940 return "DW_AT_HP_raw_data_ptr";
12941 case DW_AT_HP_pass_by_reference:
12942 return "DW_AT_HP_pass_by_reference";
12943 case DW_AT_HP_opt_level:
12944 return "DW_AT_HP_opt_level";
12945 case DW_AT_HP_prof_version_id:
12946 return "DW_AT_HP_prof_version_id";
12947 case DW_AT_HP_opt_flags:
12948 return "DW_AT_HP_opt_flags";
12949 case DW_AT_HP_cold_region_low_pc:
12950 return "DW_AT_HP_cold_region_low_pc";
12951 case DW_AT_HP_cold_region_high_pc:
12952 return "DW_AT_HP_cold_region_high_pc";
12953 case DW_AT_HP_all_variables_modifiable:
12954 return "DW_AT_HP_all_variables_modifiable";
12955 case DW_AT_HP_linkage_name:
12956 return "DW_AT_HP_linkage_name";
12957 case DW_AT_HP_prof_flags:
12958 return "DW_AT_HP_prof_flags";
12959 /* GNU extensions. */
12960 case DW_AT_sf_names:
12961 return "DW_AT_sf_names";
12962 case DW_AT_src_info:
12963 return "DW_AT_src_info";
12964 case DW_AT_mac_info:
12965 return "DW_AT_mac_info";
12966 case DW_AT_src_coords:
12967 return "DW_AT_src_coords";
12968 case DW_AT_body_begin:
12969 return "DW_AT_body_begin";
12970 case DW_AT_body_end:
12971 return "DW_AT_body_end";
12972 case DW_AT_GNU_vector:
12973 return "DW_AT_GNU_vector";
12974 case DW_AT_GNU_odr_signature:
12975 return "DW_AT_GNU_odr_signature";
12976 /* VMS extensions. */
12977 case DW_AT_VMS_rtnbeg_pd_address:
12978 return "DW_AT_VMS_rtnbeg_pd_address";
12979 /* UPC extension. */
12980 case DW_AT_upc_threads_scaled:
12981 return "DW_AT_upc_threads_scaled";
12982 /* PGI (STMicroelectronics) extensions. */
12983 case DW_AT_PGI_lbase:
12984 return "DW_AT_PGI_lbase";
12985 case DW_AT_PGI_soffset:
12986 return "DW_AT_PGI_soffset";
12987 case DW_AT_PGI_lstride:
12988 return "DW_AT_PGI_lstride";
12989 default:
12990 return "DW_AT_<unknown>";
12991 }
12992 }
12993
12994 /* Convert a DWARF value form code into its string name. */
12995
12996 static char *
12997 dwarf_form_name (unsigned form)
12998 {
12999 switch (form)
13000 {
13001 case DW_FORM_addr:
13002 return "DW_FORM_addr";
13003 case DW_FORM_block2:
13004 return "DW_FORM_block2";
13005 case DW_FORM_block4:
13006 return "DW_FORM_block4";
13007 case DW_FORM_data2:
13008 return "DW_FORM_data2";
13009 case DW_FORM_data4:
13010 return "DW_FORM_data4";
13011 case DW_FORM_data8:
13012 return "DW_FORM_data8";
13013 case DW_FORM_string:
13014 return "DW_FORM_string";
13015 case DW_FORM_block:
13016 return "DW_FORM_block";
13017 case DW_FORM_block1:
13018 return "DW_FORM_block1";
13019 case DW_FORM_data1:
13020 return "DW_FORM_data1";
13021 case DW_FORM_flag:
13022 return "DW_FORM_flag";
13023 case DW_FORM_sdata:
13024 return "DW_FORM_sdata";
13025 case DW_FORM_strp:
13026 return "DW_FORM_strp";
13027 case DW_FORM_udata:
13028 return "DW_FORM_udata";
13029 case DW_FORM_ref_addr:
13030 return "DW_FORM_ref_addr";
13031 case DW_FORM_ref1:
13032 return "DW_FORM_ref1";
13033 case DW_FORM_ref2:
13034 return "DW_FORM_ref2";
13035 case DW_FORM_ref4:
13036 return "DW_FORM_ref4";
13037 case DW_FORM_ref8:
13038 return "DW_FORM_ref8";
13039 case DW_FORM_ref_udata:
13040 return "DW_FORM_ref_udata";
13041 case DW_FORM_indirect:
13042 return "DW_FORM_indirect";
13043 case DW_FORM_sec_offset:
13044 return "DW_FORM_sec_offset";
13045 case DW_FORM_exprloc:
13046 return "DW_FORM_exprloc";
13047 case DW_FORM_flag_present:
13048 return "DW_FORM_flag_present";
13049 case DW_FORM_ref_sig8:
13050 return "DW_FORM_ref_sig8";
13051 default:
13052 return "DW_FORM_<unknown>";
13053 }
13054 }
13055
13056 /* Convert a DWARF stack opcode into its string name. */
13057
13058 const char *
13059 dwarf_stack_op_name (unsigned op)
13060 {
13061 switch (op)
13062 {
13063 case DW_OP_addr:
13064 return "DW_OP_addr";
13065 case DW_OP_deref:
13066 return "DW_OP_deref";
13067 case DW_OP_const1u:
13068 return "DW_OP_const1u";
13069 case DW_OP_const1s:
13070 return "DW_OP_const1s";
13071 case DW_OP_const2u:
13072 return "DW_OP_const2u";
13073 case DW_OP_const2s:
13074 return "DW_OP_const2s";
13075 case DW_OP_const4u:
13076 return "DW_OP_const4u";
13077 case DW_OP_const4s:
13078 return "DW_OP_const4s";
13079 case DW_OP_const8u:
13080 return "DW_OP_const8u";
13081 case DW_OP_const8s:
13082 return "DW_OP_const8s";
13083 case DW_OP_constu:
13084 return "DW_OP_constu";
13085 case DW_OP_consts:
13086 return "DW_OP_consts";
13087 case DW_OP_dup:
13088 return "DW_OP_dup";
13089 case DW_OP_drop:
13090 return "DW_OP_drop";
13091 case DW_OP_over:
13092 return "DW_OP_over";
13093 case DW_OP_pick:
13094 return "DW_OP_pick";
13095 case DW_OP_swap:
13096 return "DW_OP_swap";
13097 case DW_OP_rot:
13098 return "DW_OP_rot";
13099 case DW_OP_xderef:
13100 return "DW_OP_xderef";
13101 case DW_OP_abs:
13102 return "DW_OP_abs";
13103 case DW_OP_and:
13104 return "DW_OP_and";
13105 case DW_OP_div:
13106 return "DW_OP_div";
13107 case DW_OP_minus:
13108 return "DW_OP_minus";
13109 case DW_OP_mod:
13110 return "DW_OP_mod";
13111 case DW_OP_mul:
13112 return "DW_OP_mul";
13113 case DW_OP_neg:
13114 return "DW_OP_neg";
13115 case DW_OP_not:
13116 return "DW_OP_not";
13117 case DW_OP_or:
13118 return "DW_OP_or";
13119 case DW_OP_plus:
13120 return "DW_OP_plus";
13121 case DW_OP_plus_uconst:
13122 return "DW_OP_plus_uconst";
13123 case DW_OP_shl:
13124 return "DW_OP_shl";
13125 case DW_OP_shr:
13126 return "DW_OP_shr";
13127 case DW_OP_shra:
13128 return "DW_OP_shra";
13129 case DW_OP_xor:
13130 return "DW_OP_xor";
13131 case DW_OP_bra:
13132 return "DW_OP_bra";
13133 case DW_OP_eq:
13134 return "DW_OP_eq";
13135 case DW_OP_ge:
13136 return "DW_OP_ge";
13137 case DW_OP_gt:
13138 return "DW_OP_gt";
13139 case DW_OP_le:
13140 return "DW_OP_le";
13141 case DW_OP_lt:
13142 return "DW_OP_lt";
13143 case DW_OP_ne:
13144 return "DW_OP_ne";
13145 case DW_OP_skip:
13146 return "DW_OP_skip";
13147 case DW_OP_lit0:
13148 return "DW_OP_lit0";
13149 case DW_OP_lit1:
13150 return "DW_OP_lit1";
13151 case DW_OP_lit2:
13152 return "DW_OP_lit2";
13153 case DW_OP_lit3:
13154 return "DW_OP_lit3";
13155 case DW_OP_lit4:
13156 return "DW_OP_lit4";
13157 case DW_OP_lit5:
13158 return "DW_OP_lit5";
13159 case DW_OP_lit6:
13160 return "DW_OP_lit6";
13161 case DW_OP_lit7:
13162 return "DW_OP_lit7";
13163 case DW_OP_lit8:
13164 return "DW_OP_lit8";
13165 case DW_OP_lit9:
13166 return "DW_OP_lit9";
13167 case DW_OP_lit10:
13168 return "DW_OP_lit10";
13169 case DW_OP_lit11:
13170 return "DW_OP_lit11";
13171 case DW_OP_lit12:
13172 return "DW_OP_lit12";
13173 case DW_OP_lit13:
13174 return "DW_OP_lit13";
13175 case DW_OP_lit14:
13176 return "DW_OP_lit14";
13177 case DW_OP_lit15:
13178 return "DW_OP_lit15";
13179 case DW_OP_lit16:
13180 return "DW_OP_lit16";
13181 case DW_OP_lit17:
13182 return "DW_OP_lit17";
13183 case DW_OP_lit18:
13184 return "DW_OP_lit18";
13185 case DW_OP_lit19:
13186 return "DW_OP_lit19";
13187 case DW_OP_lit20:
13188 return "DW_OP_lit20";
13189 case DW_OP_lit21:
13190 return "DW_OP_lit21";
13191 case DW_OP_lit22:
13192 return "DW_OP_lit22";
13193 case DW_OP_lit23:
13194 return "DW_OP_lit23";
13195 case DW_OP_lit24:
13196 return "DW_OP_lit24";
13197 case DW_OP_lit25:
13198 return "DW_OP_lit25";
13199 case DW_OP_lit26:
13200 return "DW_OP_lit26";
13201 case DW_OP_lit27:
13202 return "DW_OP_lit27";
13203 case DW_OP_lit28:
13204 return "DW_OP_lit28";
13205 case DW_OP_lit29:
13206 return "DW_OP_lit29";
13207 case DW_OP_lit30:
13208 return "DW_OP_lit30";
13209 case DW_OP_lit31:
13210 return "DW_OP_lit31";
13211 case DW_OP_reg0:
13212 return "DW_OP_reg0";
13213 case DW_OP_reg1:
13214 return "DW_OP_reg1";
13215 case DW_OP_reg2:
13216 return "DW_OP_reg2";
13217 case DW_OP_reg3:
13218 return "DW_OP_reg3";
13219 case DW_OP_reg4:
13220 return "DW_OP_reg4";
13221 case DW_OP_reg5:
13222 return "DW_OP_reg5";
13223 case DW_OP_reg6:
13224 return "DW_OP_reg6";
13225 case DW_OP_reg7:
13226 return "DW_OP_reg7";
13227 case DW_OP_reg8:
13228 return "DW_OP_reg8";
13229 case DW_OP_reg9:
13230 return "DW_OP_reg9";
13231 case DW_OP_reg10:
13232 return "DW_OP_reg10";
13233 case DW_OP_reg11:
13234 return "DW_OP_reg11";
13235 case DW_OP_reg12:
13236 return "DW_OP_reg12";
13237 case DW_OP_reg13:
13238 return "DW_OP_reg13";
13239 case DW_OP_reg14:
13240 return "DW_OP_reg14";
13241 case DW_OP_reg15:
13242 return "DW_OP_reg15";
13243 case DW_OP_reg16:
13244 return "DW_OP_reg16";
13245 case DW_OP_reg17:
13246 return "DW_OP_reg17";
13247 case DW_OP_reg18:
13248 return "DW_OP_reg18";
13249 case DW_OP_reg19:
13250 return "DW_OP_reg19";
13251 case DW_OP_reg20:
13252 return "DW_OP_reg20";
13253 case DW_OP_reg21:
13254 return "DW_OP_reg21";
13255 case DW_OP_reg22:
13256 return "DW_OP_reg22";
13257 case DW_OP_reg23:
13258 return "DW_OP_reg23";
13259 case DW_OP_reg24:
13260 return "DW_OP_reg24";
13261 case DW_OP_reg25:
13262 return "DW_OP_reg25";
13263 case DW_OP_reg26:
13264 return "DW_OP_reg26";
13265 case DW_OP_reg27:
13266 return "DW_OP_reg27";
13267 case DW_OP_reg28:
13268 return "DW_OP_reg28";
13269 case DW_OP_reg29:
13270 return "DW_OP_reg29";
13271 case DW_OP_reg30:
13272 return "DW_OP_reg30";
13273 case DW_OP_reg31:
13274 return "DW_OP_reg31";
13275 case DW_OP_breg0:
13276 return "DW_OP_breg0";
13277 case DW_OP_breg1:
13278 return "DW_OP_breg1";
13279 case DW_OP_breg2:
13280 return "DW_OP_breg2";
13281 case DW_OP_breg3:
13282 return "DW_OP_breg3";
13283 case DW_OP_breg4:
13284 return "DW_OP_breg4";
13285 case DW_OP_breg5:
13286 return "DW_OP_breg5";
13287 case DW_OP_breg6:
13288 return "DW_OP_breg6";
13289 case DW_OP_breg7:
13290 return "DW_OP_breg7";
13291 case DW_OP_breg8:
13292 return "DW_OP_breg8";
13293 case DW_OP_breg9:
13294 return "DW_OP_breg9";
13295 case DW_OP_breg10:
13296 return "DW_OP_breg10";
13297 case DW_OP_breg11:
13298 return "DW_OP_breg11";
13299 case DW_OP_breg12:
13300 return "DW_OP_breg12";
13301 case DW_OP_breg13:
13302 return "DW_OP_breg13";
13303 case DW_OP_breg14:
13304 return "DW_OP_breg14";
13305 case DW_OP_breg15:
13306 return "DW_OP_breg15";
13307 case DW_OP_breg16:
13308 return "DW_OP_breg16";
13309 case DW_OP_breg17:
13310 return "DW_OP_breg17";
13311 case DW_OP_breg18:
13312 return "DW_OP_breg18";
13313 case DW_OP_breg19:
13314 return "DW_OP_breg19";
13315 case DW_OP_breg20:
13316 return "DW_OP_breg20";
13317 case DW_OP_breg21:
13318 return "DW_OP_breg21";
13319 case DW_OP_breg22:
13320 return "DW_OP_breg22";
13321 case DW_OP_breg23:
13322 return "DW_OP_breg23";
13323 case DW_OP_breg24:
13324 return "DW_OP_breg24";
13325 case DW_OP_breg25:
13326 return "DW_OP_breg25";
13327 case DW_OP_breg26:
13328 return "DW_OP_breg26";
13329 case DW_OP_breg27:
13330 return "DW_OP_breg27";
13331 case DW_OP_breg28:
13332 return "DW_OP_breg28";
13333 case DW_OP_breg29:
13334 return "DW_OP_breg29";
13335 case DW_OP_breg30:
13336 return "DW_OP_breg30";
13337 case DW_OP_breg31:
13338 return "DW_OP_breg31";
13339 case DW_OP_regx:
13340 return "DW_OP_regx";
13341 case DW_OP_fbreg:
13342 return "DW_OP_fbreg";
13343 case DW_OP_bregx:
13344 return "DW_OP_bregx";
13345 case DW_OP_piece:
13346 return "DW_OP_piece";
13347 case DW_OP_deref_size:
13348 return "DW_OP_deref_size";
13349 case DW_OP_xderef_size:
13350 return "DW_OP_xderef_size";
13351 case DW_OP_nop:
13352 return "DW_OP_nop";
13353 /* DWARF 3 extensions. */
13354 case DW_OP_push_object_address:
13355 return "DW_OP_push_object_address";
13356 case DW_OP_call2:
13357 return "DW_OP_call2";
13358 case DW_OP_call4:
13359 return "DW_OP_call4";
13360 case DW_OP_call_ref:
13361 return "DW_OP_call_ref";
13362 case DW_OP_form_tls_address:
13363 return "DW_OP_form_tls_address";
13364 case DW_OP_call_frame_cfa:
13365 return "DW_OP_call_frame_cfa";
13366 case DW_OP_bit_piece:
13367 return "DW_OP_bit_piece";
13368 /* DWARF 4 extensions. */
13369 case DW_OP_implicit_value:
13370 return "DW_OP_implicit_value";
13371 case DW_OP_stack_value:
13372 return "DW_OP_stack_value";
13373 /* GNU extensions. */
13374 case DW_OP_GNU_push_tls_address:
13375 return "DW_OP_GNU_push_tls_address";
13376 case DW_OP_GNU_uninit:
13377 return "DW_OP_GNU_uninit";
13378 case DW_OP_GNU_implicit_pointer:
13379 return "DW_OP_GNU_implicit_pointer";
13380 case DW_OP_GNU_entry_value:
13381 return "DW_OP_GNU_entry_value";
13382 case DW_OP_GNU_const_type:
13383 return "DW_OP_GNU_const_type";
13384 case DW_OP_GNU_regval_type:
13385 return "DW_OP_GNU_regval_type";
13386 case DW_OP_GNU_deref_type:
13387 return "DW_OP_GNU_deref_type";
13388 case DW_OP_GNU_convert:
13389 return "DW_OP_GNU_convert";
13390 case DW_OP_GNU_reinterpret:
13391 return "DW_OP_GNU_reinterpret";
13392 default:
13393 return NULL;
13394 }
13395 }
13396
13397 static char *
13398 dwarf_bool_name (unsigned mybool)
13399 {
13400 if (mybool)
13401 return "TRUE";
13402 else
13403 return "FALSE";
13404 }
13405
13406 /* Convert a DWARF type code into its string name. */
13407
13408 static char *
13409 dwarf_type_encoding_name (unsigned enc)
13410 {
13411 switch (enc)
13412 {
13413 case DW_ATE_void:
13414 return "DW_ATE_void";
13415 case DW_ATE_address:
13416 return "DW_ATE_address";
13417 case DW_ATE_boolean:
13418 return "DW_ATE_boolean";
13419 case DW_ATE_complex_float:
13420 return "DW_ATE_complex_float";
13421 case DW_ATE_float:
13422 return "DW_ATE_float";
13423 case DW_ATE_signed:
13424 return "DW_ATE_signed";
13425 case DW_ATE_signed_char:
13426 return "DW_ATE_signed_char";
13427 case DW_ATE_unsigned:
13428 return "DW_ATE_unsigned";
13429 case DW_ATE_unsigned_char:
13430 return "DW_ATE_unsigned_char";
13431 /* DWARF 3. */
13432 case DW_ATE_imaginary_float:
13433 return "DW_ATE_imaginary_float";
13434 case DW_ATE_packed_decimal:
13435 return "DW_ATE_packed_decimal";
13436 case DW_ATE_numeric_string:
13437 return "DW_ATE_numeric_string";
13438 case DW_ATE_edited:
13439 return "DW_ATE_edited";
13440 case DW_ATE_signed_fixed:
13441 return "DW_ATE_signed_fixed";
13442 case DW_ATE_unsigned_fixed:
13443 return "DW_ATE_unsigned_fixed";
13444 case DW_ATE_decimal_float:
13445 return "DW_ATE_decimal_float";
13446 /* DWARF 4. */
13447 case DW_ATE_UTF:
13448 return "DW_ATE_UTF";
13449 /* HP extensions. */
13450 case DW_ATE_HP_float80:
13451 return "DW_ATE_HP_float80";
13452 case DW_ATE_HP_complex_float80:
13453 return "DW_ATE_HP_complex_float80";
13454 case DW_ATE_HP_float128:
13455 return "DW_ATE_HP_float128";
13456 case DW_ATE_HP_complex_float128:
13457 return "DW_ATE_HP_complex_float128";
13458 case DW_ATE_HP_floathpintel:
13459 return "DW_ATE_HP_floathpintel";
13460 case DW_ATE_HP_imaginary_float80:
13461 return "DW_ATE_HP_imaginary_float80";
13462 case DW_ATE_HP_imaginary_float128:
13463 return "DW_ATE_HP_imaginary_float128";
13464 default:
13465 return "DW_ATE_<unknown>";
13466 }
13467 }
13468
13469 /* Convert a DWARF call frame info operation to its string name. */
13470
13471 #if 0
13472 static char *
13473 dwarf_cfi_name (unsigned cfi_opc)
13474 {
13475 switch (cfi_opc)
13476 {
13477 case DW_CFA_advance_loc:
13478 return "DW_CFA_advance_loc";
13479 case DW_CFA_offset:
13480 return "DW_CFA_offset";
13481 case DW_CFA_restore:
13482 return "DW_CFA_restore";
13483 case DW_CFA_nop:
13484 return "DW_CFA_nop";
13485 case DW_CFA_set_loc:
13486 return "DW_CFA_set_loc";
13487 case DW_CFA_advance_loc1:
13488 return "DW_CFA_advance_loc1";
13489 case DW_CFA_advance_loc2:
13490 return "DW_CFA_advance_loc2";
13491 case DW_CFA_advance_loc4:
13492 return "DW_CFA_advance_loc4";
13493 case DW_CFA_offset_extended:
13494 return "DW_CFA_offset_extended";
13495 case DW_CFA_restore_extended:
13496 return "DW_CFA_restore_extended";
13497 case DW_CFA_undefined:
13498 return "DW_CFA_undefined";
13499 case DW_CFA_same_value:
13500 return "DW_CFA_same_value";
13501 case DW_CFA_register:
13502 return "DW_CFA_register";
13503 case DW_CFA_remember_state:
13504 return "DW_CFA_remember_state";
13505 case DW_CFA_restore_state:
13506 return "DW_CFA_restore_state";
13507 case DW_CFA_def_cfa:
13508 return "DW_CFA_def_cfa";
13509 case DW_CFA_def_cfa_register:
13510 return "DW_CFA_def_cfa_register";
13511 case DW_CFA_def_cfa_offset:
13512 return "DW_CFA_def_cfa_offset";
13513 /* DWARF 3. */
13514 case DW_CFA_def_cfa_expression:
13515 return "DW_CFA_def_cfa_expression";
13516 case DW_CFA_expression:
13517 return "DW_CFA_expression";
13518 case DW_CFA_offset_extended_sf:
13519 return "DW_CFA_offset_extended_sf";
13520 case DW_CFA_def_cfa_sf:
13521 return "DW_CFA_def_cfa_sf";
13522 case DW_CFA_def_cfa_offset_sf:
13523 return "DW_CFA_def_cfa_offset_sf";
13524 case DW_CFA_val_offset:
13525 return "DW_CFA_val_offset";
13526 case DW_CFA_val_offset_sf:
13527 return "DW_CFA_val_offset_sf";
13528 case DW_CFA_val_expression:
13529 return "DW_CFA_val_expression";
13530 /* SGI/MIPS specific. */
13531 case DW_CFA_MIPS_advance_loc8:
13532 return "DW_CFA_MIPS_advance_loc8";
13533 /* GNU extensions. */
13534 case DW_CFA_GNU_window_save:
13535 return "DW_CFA_GNU_window_save";
13536 case DW_CFA_GNU_args_size:
13537 return "DW_CFA_GNU_args_size";
13538 case DW_CFA_GNU_negative_offset_extended:
13539 return "DW_CFA_GNU_negative_offset_extended";
13540 default:
13541 return "DW_CFA_<unknown>";
13542 }
13543 }
13544 #endif
13545
13546 static void
13547 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13548 {
13549 unsigned int i;
13550
13551 print_spaces (indent, f);
13552 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13553 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13554
13555 if (die->parent != NULL)
13556 {
13557 print_spaces (indent, f);
13558 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13559 die->parent->offset);
13560 }
13561
13562 print_spaces (indent, f);
13563 fprintf_unfiltered (f, " has children: %s\n",
13564 dwarf_bool_name (die->child != NULL));
13565
13566 print_spaces (indent, f);
13567 fprintf_unfiltered (f, " attributes:\n");
13568
13569 for (i = 0; i < die->num_attrs; ++i)
13570 {
13571 print_spaces (indent, f);
13572 fprintf_unfiltered (f, " %s (%s) ",
13573 dwarf_attr_name (die->attrs[i].name),
13574 dwarf_form_name (die->attrs[i].form));
13575
13576 switch (die->attrs[i].form)
13577 {
13578 case DW_FORM_ref_addr:
13579 case DW_FORM_addr:
13580 fprintf_unfiltered (f, "address: ");
13581 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13582 break;
13583 case DW_FORM_block2:
13584 case DW_FORM_block4:
13585 case DW_FORM_block:
13586 case DW_FORM_block1:
13587 fprintf_unfiltered (f, "block: size %d",
13588 DW_BLOCK (&die->attrs[i])->size);
13589 break;
13590 case DW_FORM_exprloc:
13591 fprintf_unfiltered (f, "expression: size %u",
13592 DW_BLOCK (&die->attrs[i])->size);
13593 break;
13594 case DW_FORM_ref1:
13595 case DW_FORM_ref2:
13596 case DW_FORM_ref4:
13597 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13598 (long) (DW_ADDR (&die->attrs[i])));
13599 break;
13600 case DW_FORM_data1:
13601 case DW_FORM_data2:
13602 case DW_FORM_data4:
13603 case DW_FORM_data8:
13604 case DW_FORM_udata:
13605 case DW_FORM_sdata:
13606 fprintf_unfiltered (f, "constant: %s",
13607 pulongest (DW_UNSND (&die->attrs[i])));
13608 break;
13609 case DW_FORM_sec_offset:
13610 fprintf_unfiltered (f, "section offset: %s",
13611 pulongest (DW_UNSND (&die->attrs[i])));
13612 break;
13613 case DW_FORM_ref_sig8:
13614 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13615 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13616 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13617 else
13618 fprintf_unfiltered (f, "signatured type, offset: unknown");
13619 break;
13620 case DW_FORM_string:
13621 case DW_FORM_strp:
13622 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13623 DW_STRING (&die->attrs[i])
13624 ? DW_STRING (&die->attrs[i]) : "",
13625 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13626 break;
13627 case DW_FORM_flag:
13628 if (DW_UNSND (&die->attrs[i]))
13629 fprintf_unfiltered (f, "flag: TRUE");
13630 else
13631 fprintf_unfiltered (f, "flag: FALSE");
13632 break;
13633 case DW_FORM_flag_present:
13634 fprintf_unfiltered (f, "flag: TRUE");
13635 break;
13636 case DW_FORM_indirect:
13637 /* The reader will have reduced the indirect form to
13638 the "base form" so this form should not occur. */
13639 fprintf_unfiltered (f,
13640 "unexpected attribute form: DW_FORM_indirect");
13641 break;
13642 default:
13643 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13644 die->attrs[i].form);
13645 break;
13646 }
13647 fprintf_unfiltered (f, "\n");
13648 }
13649 }
13650
13651 static void
13652 dump_die_for_error (struct die_info *die)
13653 {
13654 dump_die_shallow (gdb_stderr, 0, die);
13655 }
13656
13657 static void
13658 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13659 {
13660 int indent = level * 4;
13661
13662 gdb_assert (die != NULL);
13663
13664 if (level >= max_level)
13665 return;
13666
13667 dump_die_shallow (f, indent, die);
13668
13669 if (die->child != NULL)
13670 {
13671 print_spaces (indent, f);
13672 fprintf_unfiltered (f, " Children:");
13673 if (level + 1 < max_level)
13674 {
13675 fprintf_unfiltered (f, "\n");
13676 dump_die_1 (f, level + 1, max_level, die->child);
13677 }
13678 else
13679 {
13680 fprintf_unfiltered (f,
13681 " [not printed, max nesting level reached]\n");
13682 }
13683 }
13684
13685 if (die->sibling != NULL && level > 0)
13686 {
13687 dump_die_1 (f, level, max_level, die->sibling);
13688 }
13689 }
13690
13691 /* This is called from the pdie macro in gdbinit.in.
13692 It's not static so gcc will keep a copy callable from gdb. */
13693
13694 void
13695 dump_die (struct die_info *die, int max_level)
13696 {
13697 dump_die_1 (gdb_stdlog, 0, max_level, die);
13698 }
13699
13700 static void
13701 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13702 {
13703 void **slot;
13704
13705 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13706
13707 *slot = die;
13708 }
13709
13710 static int
13711 is_ref_attr (struct attribute *attr)
13712 {
13713 switch (attr->form)
13714 {
13715 case DW_FORM_ref_addr:
13716 case DW_FORM_ref1:
13717 case DW_FORM_ref2:
13718 case DW_FORM_ref4:
13719 case DW_FORM_ref8:
13720 case DW_FORM_ref_udata:
13721 return 1;
13722 default:
13723 return 0;
13724 }
13725 }
13726
13727 static unsigned int
13728 dwarf2_get_ref_die_offset (struct attribute *attr)
13729 {
13730 if (is_ref_attr (attr))
13731 return DW_ADDR (attr);
13732
13733 complaint (&symfile_complaints,
13734 _("unsupported die ref attribute form: '%s'"),
13735 dwarf_form_name (attr->form));
13736 return 0;
13737 }
13738
13739 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13740 * the value held by the attribute is not constant. */
13741
13742 static LONGEST
13743 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13744 {
13745 if (attr->form == DW_FORM_sdata)
13746 return DW_SND (attr);
13747 else if (attr->form == DW_FORM_udata
13748 || attr->form == DW_FORM_data1
13749 || attr->form == DW_FORM_data2
13750 || attr->form == DW_FORM_data4
13751 || attr->form == DW_FORM_data8)
13752 return DW_UNSND (attr);
13753 else
13754 {
13755 complaint (&symfile_complaints,
13756 _("Attribute value is not a constant (%s)"),
13757 dwarf_form_name (attr->form));
13758 return default_value;
13759 }
13760 }
13761
13762 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13763 unit and add it to our queue.
13764 The result is non-zero if PER_CU was queued, otherwise the result is zero
13765 meaning either PER_CU is already queued or it is already loaded. */
13766
13767 static int
13768 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13769 struct dwarf2_per_cu_data *per_cu)
13770 {
13771 /* We may arrive here during partial symbol reading, if we need full
13772 DIEs to process an unusual case (e.g. template arguments). Do
13773 not queue PER_CU, just tell our caller to load its DIEs. */
13774 if (dwarf2_per_objfile->reading_partial_symbols)
13775 {
13776 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13777 return 1;
13778 return 0;
13779 }
13780
13781 /* Mark the dependence relation so that we don't flush PER_CU
13782 too early. */
13783 dwarf2_add_dependence (this_cu, per_cu);
13784
13785 /* If it's already on the queue, we have nothing to do. */
13786 if (per_cu->queued)
13787 return 0;
13788
13789 /* If the compilation unit is already loaded, just mark it as
13790 used. */
13791 if (per_cu->cu != NULL)
13792 {
13793 per_cu->cu->last_used = 0;
13794 return 0;
13795 }
13796
13797 /* Add it to the queue. */
13798 queue_comp_unit (per_cu, this_cu->objfile);
13799
13800 return 1;
13801 }
13802
13803 /* Follow reference or signature attribute ATTR of SRC_DIE.
13804 On entry *REF_CU is the CU of SRC_DIE.
13805 On exit *REF_CU is the CU of the result. */
13806
13807 static struct die_info *
13808 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13809 struct dwarf2_cu **ref_cu)
13810 {
13811 struct die_info *die;
13812
13813 if (is_ref_attr (attr))
13814 die = follow_die_ref (src_die, attr, ref_cu);
13815 else if (attr->form == DW_FORM_ref_sig8)
13816 die = follow_die_sig (src_die, attr, ref_cu);
13817 else
13818 {
13819 dump_die_for_error (src_die);
13820 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13821 (*ref_cu)->objfile->name);
13822 }
13823
13824 return die;
13825 }
13826
13827 /* Follow reference OFFSET.
13828 On entry *REF_CU is the CU of the source die referencing OFFSET.
13829 On exit *REF_CU is the CU of the result.
13830 Returns NULL if OFFSET is invalid. */
13831
13832 static struct die_info *
13833 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13834 {
13835 struct die_info temp_die;
13836 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13837
13838 gdb_assert (cu->per_cu != NULL);
13839
13840 target_cu = cu;
13841
13842 if (cu->per_cu->debug_type_section)
13843 {
13844 /* .debug_types CUs cannot reference anything outside their CU.
13845 If they need to, they have to reference a signatured type via
13846 DW_FORM_ref_sig8. */
13847 if (! offset_in_cu_p (&cu->header, offset))
13848 return NULL;
13849 }
13850 else if (! offset_in_cu_p (&cu->header, offset))
13851 {
13852 struct dwarf2_per_cu_data *per_cu;
13853
13854 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13855
13856 /* If necessary, add it to the queue and load its DIEs. */
13857 if (maybe_queue_comp_unit (cu, per_cu))
13858 load_full_comp_unit (per_cu, cu->objfile);
13859
13860 target_cu = per_cu->cu;
13861 }
13862 else if (cu->dies == NULL)
13863 {
13864 /* We're loading full DIEs during partial symbol reading. */
13865 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13866 load_full_comp_unit (cu->per_cu, cu->objfile);
13867 }
13868
13869 *ref_cu = target_cu;
13870 temp_die.offset = offset;
13871 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13872 }
13873
13874 /* Follow reference attribute ATTR of SRC_DIE.
13875 On entry *REF_CU is the CU of SRC_DIE.
13876 On exit *REF_CU is the CU of the result. */
13877
13878 static struct die_info *
13879 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13880 struct dwarf2_cu **ref_cu)
13881 {
13882 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13883 struct dwarf2_cu *cu = *ref_cu;
13884 struct die_info *die;
13885
13886 die = follow_die_offset (offset, ref_cu);
13887 if (!die)
13888 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13889 "at 0x%x [in module %s]"),
13890 offset, src_die->offset, cu->objfile->name);
13891
13892 return die;
13893 }
13894
13895 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13896 value is intended for DW_OP_call*. You must call xfree on returned
13897 dwarf2_locexpr_baton->data. */
13898
13899 struct dwarf2_locexpr_baton
13900 dwarf2_fetch_die_location_block (unsigned int offset,
13901 struct dwarf2_per_cu_data *per_cu,
13902 CORE_ADDR (*get_frame_pc) (void *baton),
13903 void *baton)
13904 {
13905 struct dwarf2_cu *cu;
13906 struct die_info *die;
13907 struct attribute *attr;
13908 struct dwarf2_locexpr_baton retval;
13909
13910 dw2_setup (per_cu->objfile);
13911
13912 if (per_cu->cu == NULL)
13913 load_cu (per_cu);
13914 cu = per_cu->cu;
13915
13916 die = follow_die_offset (offset, &cu);
13917 if (!die)
13918 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13919 offset, per_cu->cu->objfile->name);
13920
13921 attr = dwarf2_attr (die, DW_AT_location, cu);
13922 if (!attr)
13923 {
13924 /* DWARF: "If there is no such attribute, then there is no effect.". */
13925
13926 retval.data = NULL;
13927 retval.size = 0;
13928 }
13929 else if (attr_form_is_section_offset (attr))
13930 {
13931 struct dwarf2_loclist_baton loclist_baton;
13932 CORE_ADDR pc = (*get_frame_pc) (baton);
13933 size_t size;
13934
13935 fill_in_loclist_baton (cu, &loclist_baton, attr);
13936
13937 retval.data = dwarf2_find_location_expression (&loclist_baton,
13938 &size, pc);
13939 retval.size = size;
13940 }
13941 else
13942 {
13943 if (!attr_form_is_block (attr))
13944 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13945 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13946 offset, per_cu->cu->objfile->name);
13947
13948 retval.data = DW_BLOCK (attr)->data;
13949 retval.size = DW_BLOCK (attr)->size;
13950 }
13951 retval.per_cu = cu->per_cu;
13952
13953 if (retval.data)
13954 retval.data = xmemdup (retval.data, retval.size, retval.size);
13955
13956 age_cached_comp_units ();
13957
13958 return retval;
13959 }
13960
13961 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13962 PER_CU. */
13963
13964 struct type *
13965 dwarf2_get_die_type (unsigned int die_offset,
13966 struct dwarf2_per_cu_data *per_cu)
13967 {
13968 dw2_setup (per_cu->objfile);
13969 return get_die_type_at_offset (die_offset, per_cu);
13970 }
13971
13972 /* Follow the signature attribute ATTR in SRC_DIE.
13973 On entry *REF_CU is the CU of SRC_DIE.
13974 On exit *REF_CU is the CU of the result. */
13975
13976 static struct die_info *
13977 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13978 struct dwarf2_cu **ref_cu)
13979 {
13980 struct objfile *objfile = (*ref_cu)->objfile;
13981 struct die_info temp_die;
13982 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13983 struct dwarf2_cu *sig_cu;
13984 struct die_info *die;
13985
13986 /* sig_type will be NULL if the signatured type is missing from
13987 the debug info. */
13988 if (sig_type == NULL)
13989 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13990 "at 0x%x [in module %s]"),
13991 src_die->offset, objfile->name);
13992
13993 /* If necessary, add it to the queue and load its DIEs. */
13994
13995 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13996 read_signatured_type (objfile, sig_type);
13997
13998 gdb_assert (sig_type->per_cu.cu != NULL);
13999
14000 sig_cu = sig_type->per_cu.cu;
14001 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14002 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14003 if (die)
14004 {
14005 *ref_cu = sig_cu;
14006 return die;
14007 }
14008
14009 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14010 "from DIE at 0x%x [in module %s]"),
14011 sig_type->type_offset, src_die->offset, objfile->name);
14012 }
14013
14014 /* Given an offset of a signatured type, return its signatured_type. */
14015
14016 static struct signatured_type *
14017 lookup_signatured_type_at_offset (struct objfile *objfile,
14018 struct dwarf2_section_info *section,
14019 unsigned int offset)
14020 {
14021 gdb_byte *info_ptr = section->buffer + offset;
14022 unsigned int length, initial_length_size;
14023 unsigned int sig_offset;
14024 struct signatured_type find_entry, *type_sig;
14025
14026 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14027 sig_offset = (initial_length_size
14028 + 2 /*version*/
14029 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14030 + 1 /*address_size*/);
14031 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14032 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14033
14034 /* This is only used to lookup previously recorded types.
14035 If we didn't find it, it's our bug. */
14036 gdb_assert (type_sig != NULL);
14037 gdb_assert (offset == type_sig->per_cu.offset);
14038
14039 return type_sig;
14040 }
14041
14042 /* Read in signatured type at OFFSET and build its CU and die(s). */
14043
14044 static void
14045 read_signatured_type_at_offset (struct objfile *objfile,
14046 struct dwarf2_section_info *sect,
14047 unsigned int offset)
14048 {
14049 struct signatured_type *type_sig;
14050
14051 dwarf2_read_section (objfile, sect);
14052
14053 /* We have the section offset, but we need the signature to do the
14054 hash table lookup. */
14055 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14056
14057 gdb_assert (type_sig->per_cu.cu == NULL);
14058
14059 read_signatured_type (objfile, type_sig);
14060
14061 gdb_assert (type_sig->per_cu.cu != NULL);
14062 }
14063
14064 /* Read in a signatured type and build its CU and DIEs. */
14065
14066 static void
14067 read_signatured_type (struct objfile *objfile,
14068 struct signatured_type *type_sig)
14069 {
14070 gdb_byte *types_ptr;
14071 struct die_reader_specs reader_specs;
14072 struct dwarf2_cu *cu;
14073 ULONGEST signature;
14074 struct cleanup *back_to, *free_cu_cleanup;
14075 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14076
14077 dwarf2_read_section (objfile, section);
14078 types_ptr = section->buffer + type_sig->per_cu.offset;
14079
14080 gdb_assert (type_sig->per_cu.cu == NULL);
14081
14082 cu = xmalloc (sizeof (*cu));
14083 init_one_comp_unit (cu, objfile);
14084
14085 type_sig->per_cu.cu = cu;
14086 cu->per_cu = &type_sig->per_cu;
14087
14088 /* If an error occurs while loading, release our storage. */
14089 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14090
14091 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14092 types_ptr, objfile->obfd);
14093 gdb_assert (signature == type_sig->signature);
14094
14095 cu->die_hash
14096 = htab_create_alloc_ex (cu->header.length / 12,
14097 die_hash,
14098 die_eq,
14099 NULL,
14100 &cu->comp_unit_obstack,
14101 hashtab_obstack_allocate,
14102 dummy_obstack_deallocate);
14103
14104 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14105 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14106
14107 init_cu_die_reader (&reader_specs, cu);
14108
14109 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14110 NULL /*parent*/);
14111
14112 /* We try not to read any attributes in this function, because not
14113 all objfiles needed for references have been loaded yet, and symbol
14114 table processing isn't initialized. But we have to set the CU language,
14115 or we won't be able to build types correctly. */
14116 prepare_one_comp_unit (cu, cu->dies);
14117
14118 do_cleanups (back_to);
14119
14120 /* We've successfully allocated this compilation unit. Let our caller
14121 clean it up when finished with it. */
14122 discard_cleanups (free_cu_cleanup);
14123
14124 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14125 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14126 }
14127
14128 /* Decode simple location descriptions.
14129 Given a pointer to a dwarf block that defines a location, compute
14130 the location and return the value.
14131
14132 NOTE drow/2003-11-18: This function is called in two situations
14133 now: for the address of static or global variables (partial symbols
14134 only) and for offsets into structures which are expected to be
14135 (more or less) constant. The partial symbol case should go away,
14136 and only the constant case should remain. That will let this
14137 function complain more accurately. A few special modes are allowed
14138 without complaint for global variables (for instance, global
14139 register values and thread-local values).
14140
14141 A location description containing no operations indicates that the
14142 object is optimized out. The return value is 0 for that case.
14143 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14144 callers will only want a very basic result and this can become a
14145 complaint.
14146
14147 Note that stack[0] is unused except as a default error return. */
14148
14149 static CORE_ADDR
14150 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14151 {
14152 struct objfile *objfile = cu->objfile;
14153 int i;
14154 int size = blk->size;
14155 gdb_byte *data = blk->data;
14156 CORE_ADDR stack[64];
14157 int stacki;
14158 unsigned int bytes_read, unsnd;
14159 gdb_byte op;
14160
14161 i = 0;
14162 stacki = 0;
14163 stack[stacki] = 0;
14164 stack[++stacki] = 0;
14165
14166 while (i < size)
14167 {
14168 op = data[i++];
14169 switch (op)
14170 {
14171 case DW_OP_lit0:
14172 case DW_OP_lit1:
14173 case DW_OP_lit2:
14174 case DW_OP_lit3:
14175 case DW_OP_lit4:
14176 case DW_OP_lit5:
14177 case DW_OP_lit6:
14178 case DW_OP_lit7:
14179 case DW_OP_lit8:
14180 case DW_OP_lit9:
14181 case DW_OP_lit10:
14182 case DW_OP_lit11:
14183 case DW_OP_lit12:
14184 case DW_OP_lit13:
14185 case DW_OP_lit14:
14186 case DW_OP_lit15:
14187 case DW_OP_lit16:
14188 case DW_OP_lit17:
14189 case DW_OP_lit18:
14190 case DW_OP_lit19:
14191 case DW_OP_lit20:
14192 case DW_OP_lit21:
14193 case DW_OP_lit22:
14194 case DW_OP_lit23:
14195 case DW_OP_lit24:
14196 case DW_OP_lit25:
14197 case DW_OP_lit26:
14198 case DW_OP_lit27:
14199 case DW_OP_lit28:
14200 case DW_OP_lit29:
14201 case DW_OP_lit30:
14202 case DW_OP_lit31:
14203 stack[++stacki] = op - DW_OP_lit0;
14204 break;
14205
14206 case DW_OP_reg0:
14207 case DW_OP_reg1:
14208 case DW_OP_reg2:
14209 case DW_OP_reg3:
14210 case DW_OP_reg4:
14211 case DW_OP_reg5:
14212 case DW_OP_reg6:
14213 case DW_OP_reg7:
14214 case DW_OP_reg8:
14215 case DW_OP_reg9:
14216 case DW_OP_reg10:
14217 case DW_OP_reg11:
14218 case DW_OP_reg12:
14219 case DW_OP_reg13:
14220 case DW_OP_reg14:
14221 case DW_OP_reg15:
14222 case DW_OP_reg16:
14223 case DW_OP_reg17:
14224 case DW_OP_reg18:
14225 case DW_OP_reg19:
14226 case DW_OP_reg20:
14227 case DW_OP_reg21:
14228 case DW_OP_reg22:
14229 case DW_OP_reg23:
14230 case DW_OP_reg24:
14231 case DW_OP_reg25:
14232 case DW_OP_reg26:
14233 case DW_OP_reg27:
14234 case DW_OP_reg28:
14235 case DW_OP_reg29:
14236 case DW_OP_reg30:
14237 case DW_OP_reg31:
14238 stack[++stacki] = op - DW_OP_reg0;
14239 if (i < size)
14240 dwarf2_complex_location_expr_complaint ();
14241 break;
14242
14243 case DW_OP_regx:
14244 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14245 i += bytes_read;
14246 stack[++stacki] = unsnd;
14247 if (i < size)
14248 dwarf2_complex_location_expr_complaint ();
14249 break;
14250
14251 case DW_OP_addr:
14252 stack[++stacki] = read_address (objfile->obfd, &data[i],
14253 cu, &bytes_read);
14254 i += bytes_read;
14255 break;
14256
14257 case DW_OP_const1u:
14258 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14259 i += 1;
14260 break;
14261
14262 case DW_OP_const1s:
14263 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14264 i += 1;
14265 break;
14266
14267 case DW_OP_const2u:
14268 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14269 i += 2;
14270 break;
14271
14272 case DW_OP_const2s:
14273 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14274 i += 2;
14275 break;
14276
14277 case DW_OP_const4u:
14278 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14279 i += 4;
14280 break;
14281
14282 case DW_OP_const4s:
14283 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14284 i += 4;
14285 break;
14286
14287 case DW_OP_constu:
14288 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14289 &bytes_read);
14290 i += bytes_read;
14291 break;
14292
14293 case DW_OP_consts:
14294 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14295 i += bytes_read;
14296 break;
14297
14298 case DW_OP_dup:
14299 stack[stacki + 1] = stack[stacki];
14300 stacki++;
14301 break;
14302
14303 case DW_OP_plus:
14304 stack[stacki - 1] += stack[stacki];
14305 stacki--;
14306 break;
14307
14308 case DW_OP_plus_uconst:
14309 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14310 &bytes_read);
14311 i += bytes_read;
14312 break;
14313
14314 case DW_OP_minus:
14315 stack[stacki - 1] -= stack[stacki];
14316 stacki--;
14317 break;
14318
14319 case DW_OP_deref:
14320 /* If we're not the last op, then we definitely can't encode
14321 this using GDB's address_class enum. This is valid for partial
14322 global symbols, although the variable's address will be bogus
14323 in the psymtab. */
14324 if (i < size)
14325 dwarf2_complex_location_expr_complaint ();
14326 break;
14327
14328 case DW_OP_GNU_push_tls_address:
14329 /* The top of the stack has the offset from the beginning
14330 of the thread control block at which the variable is located. */
14331 /* Nothing should follow this operator, so the top of stack would
14332 be returned. */
14333 /* This is valid for partial global symbols, but the variable's
14334 address will be bogus in the psymtab. */
14335 if (i < size)
14336 dwarf2_complex_location_expr_complaint ();
14337 break;
14338
14339 case DW_OP_GNU_uninit:
14340 break;
14341
14342 default:
14343 {
14344 const char *name = dwarf_stack_op_name (op);
14345
14346 if (name)
14347 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14348 name);
14349 else
14350 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14351 op);
14352 }
14353
14354 return (stack[stacki]);
14355 }
14356
14357 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14358 outside of the allocated space. Also enforce minimum>0. */
14359 if (stacki >= ARRAY_SIZE (stack) - 1)
14360 {
14361 complaint (&symfile_complaints,
14362 _("location description stack overflow"));
14363 return 0;
14364 }
14365
14366 if (stacki <= 0)
14367 {
14368 complaint (&symfile_complaints,
14369 _("location description stack underflow"));
14370 return 0;
14371 }
14372 }
14373 return (stack[stacki]);
14374 }
14375
14376 /* memory allocation interface */
14377
14378 static struct dwarf_block *
14379 dwarf_alloc_block (struct dwarf2_cu *cu)
14380 {
14381 struct dwarf_block *blk;
14382
14383 blk = (struct dwarf_block *)
14384 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14385 return (blk);
14386 }
14387
14388 static struct abbrev_info *
14389 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14390 {
14391 struct abbrev_info *abbrev;
14392
14393 abbrev = (struct abbrev_info *)
14394 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14395 memset (abbrev, 0, sizeof (struct abbrev_info));
14396 return (abbrev);
14397 }
14398
14399 static struct die_info *
14400 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14401 {
14402 struct die_info *die;
14403 size_t size = sizeof (struct die_info);
14404
14405 if (num_attrs > 1)
14406 size += (num_attrs - 1) * sizeof (struct attribute);
14407
14408 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14409 memset (die, 0, sizeof (struct die_info));
14410 return (die);
14411 }
14412
14413 \f
14414 /* Macro support. */
14415
14416 /* Return the full name of file number I in *LH's file name table.
14417 Use COMP_DIR as the name of the current directory of the
14418 compilation. The result is allocated using xmalloc; the caller is
14419 responsible for freeing it. */
14420 static char *
14421 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14422 {
14423 /* Is the file number a valid index into the line header's file name
14424 table? Remember that file numbers start with one, not zero. */
14425 if (1 <= file && file <= lh->num_file_names)
14426 {
14427 struct file_entry *fe = &lh->file_names[file - 1];
14428
14429 if (IS_ABSOLUTE_PATH (fe->name))
14430 return xstrdup (fe->name);
14431 else
14432 {
14433 const char *dir;
14434 int dir_len;
14435 char *full_name;
14436
14437 if (fe->dir_index)
14438 dir = lh->include_dirs[fe->dir_index - 1];
14439 else
14440 dir = comp_dir;
14441
14442 if (dir)
14443 {
14444 dir_len = strlen (dir);
14445 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14446 strcpy (full_name, dir);
14447 full_name[dir_len] = '/';
14448 strcpy (full_name + dir_len + 1, fe->name);
14449 return full_name;
14450 }
14451 else
14452 return xstrdup (fe->name);
14453 }
14454 }
14455 else
14456 {
14457 /* The compiler produced a bogus file number. We can at least
14458 record the macro definitions made in the file, even if we
14459 won't be able to find the file by name. */
14460 char fake_name[80];
14461
14462 sprintf (fake_name, "<bad macro file number %d>", file);
14463
14464 complaint (&symfile_complaints,
14465 _("bad file number in macro information (%d)"),
14466 file);
14467
14468 return xstrdup (fake_name);
14469 }
14470 }
14471
14472
14473 static struct macro_source_file *
14474 macro_start_file (int file, int line,
14475 struct macro_source_file *current_file,
14476 const char *comp_dir,
14477 struct line_header *lh, struct objfile *objfile)
14478 {
14479 /* The full name of this source file. */
14480 char *full_name = file_full_name (file, lh, comp_dir);
14481
14482 /* We don't create a macro table for this compilation unit
14483 at all until we actually get a filename. */
14484 if (! pending_macros)
14485 pending_macros = new_macro_table (&objfile->objfile_obstack,
14486 objfile->macro_cache);
14487
14488 if (! current_file)
14489 /* If we have no current file, then this must be the start_file
14490 directive for the compilation unit's main source file. */
14491 current_file = macro_set_main (pending_macros, full_name);
14492 else
14493 current_file = macro_include (current_file, line, full_name);
14494
14495 xfree (full_name);
14496
14497 return current_file;
14498 }
14499
14500
14501 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14502 followed by a null byte. */
14503 static char *
14504 copy_string (const char *buf, int len)
14505 {
14506 char *s = xmalloc (len + 1);
14507
14508 memcpy (s, buf, len);
14509 s[len] = '\0';
14510 return s;
14511 }
14512
14513
14514 static const char *
14515 consume_improper_spaces (const char *p, const char *body)
14516 {
14517 if (*p == ' ')
14518 {
14519 complaint (&symfile_complaints,
14520 _("macro definition contains spaces "
14521 "in formal argument list:\n`%s'"),
14522 body);
14523
14524 while (*p == ' ')
14525 p++;
14526 }
14527
14528 return p;
14529 }
14530
14531
14532 static void
14533 parse_macro_definition (struct macro_source_file *file, int line,
14534 const char *body)
14535 {
14536 const char *p;
14537
14538 /* The body string takes one of two forms. For object-like macro
14539 definitions, it should be:
14540
14541 <macro name> " " <definition>
14542
14543 For function-like macro definitions, it should be:
14544
14545 <macro name> "() " <definition>
14546 or
14547 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14548
14549 Spaces may appear only where explicitly indicated, and in the
14550 <definition>.
14551
14552 The Dwarf 2 spec says that an object-like macro's name is always
14553 followed by a space, but versions of GCC around March 2002 omit
14554 the space when the macro's definition is the empty string.
14555
14556 The Dwarf 2 spec says that there should be no spaces between the
14557 formal arguments in a function-like macro's formal argument list,
14558 but versions of GCC around March 2002 include spaces after the
14559 commas. */
14560
14561
14562 /* Find the extent of the macro name. The macro name is terminated
14563 by either a space or null character (for an object-like macro) or
14564 an opening paren (for a function-like macro). */
14565 for (p = body; *p; p++)
14566 if (*p == ' ' || *p == '(')
14567 break;
14568
14569 if (*p == ' ' || *p == '\0')
14570 {
14571 /* It's an object-like macro. */
14572 int name_len = p - body;
14573 char *name = copy_string (body, name_len);
14574 const char *replacement;
14575
14576 if (*p == ' ')
14577 replacement = body + name_len + 1;
14578 else
14579 {
14580 dwarf2_macro_malformed_definition_complaint (body);
14581 replacement = body + name_len;
14582 }
14583
14584 macro_define_object (file, line, name, replacement);
14585
14586 xfree (name);
14587 }
14588 else if (*p == '(')
14589 {
14590 /* It's a function-like macro. */
14591 char *name = copy_string (body, p - body);
14592 int argc = 0;
14593 int argv_size = 1;
14594 char **argv = xmalloc (argv_size * sizeof (*argv));
14595
14596 p++;
14597
14598 p = consume_improper_spaces (p, body);
14599
14600 /* Parse the formal argument list. */
14601 while (*p && *p != ')')
14602 {
14603 /* Find the extent of the current argument name. */
14604 const char *arg_start = p;
14605
14606 while (*p && *p != ',' && *p != ')' && *p != ' ')
14607 p++;
14608
14609 if (! *p || p == arg_start)
14610 dwarf2_macro_malformed_definition_complaint (body);
14611 else
14612 {
14613 /* Make sure argv has room for the new argument. */
14614 if (argc >= argv_size)
14615 {
14616 argv_size *= 2;
14617 argv = xrealloc (argv, argv_size * sizeof (*argv));
14618 }
14619
14620 argv[argc++] = copy_string (arg_start, p - arg_start);
14621 }
14622
14623 p = consume_improper_spaces (p, body);
14624
14625 /* Consume the comma, if present. */
14626 if (*p == ',')
14627 {
14628 p++;
14629
14630 p = consume_improper_spaces (p, body);
14631 }
14632 }
14633
14634 if (*p == ')')
14635 {
14636 p++;
14637
14638 if (*p == ' ')
14639 /* Perfectly formed definition, no complaints. */
14640 macro_define_function (file, line, name,
14641 argc, (const char **) argv,
14642 p + 1);
14643 else if (*p == '\0')
14644 {
14645 /* Complain, but do define it. */
14646 dwarf2_macro_malformed_definition_complaint (body);
14647 macro_define_function (file, line, name,
14648 argc, (const char **) argv,
14649 p);
14650 }
14651 else
14652 /* Just complain. */
14653 dwarf2_macro_malformed_definition_complaint (body);
14654 }
14655 else
14656 /* Just complain. */
14657 dwarf2_macro_malformed_definition_complaint (body);
14658
14659 xfree (name);
14660 {
14661 int i;
14662
14663 for (i = 0; i < argc; i++)
14664 xfree (argv[i]);
14665 }
14666 xfree (argv);
14667 }
14668 else
14669 dwarf2_macro_malformed_definition_complaint (body);
14670 }
14671
14672
14673 static void
14674 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14675 char *comp_dir, bfd *abfd,
14676 struct dwarf2_cu *cu)
14677 {
14678 gdb_byte *mac_ptr, *mac_end;
14679 struct macro_source_file *current_file = 0;
14680 enum dwarf_macinfo_record_type macinfo_type;
14681 int at_commandline;
14682
14683 dwarf2_read_section (dwarf2_per_objfile->objfile,
14684 &dwarf2_per_objfile->macinfo);
14685 if (dwarf2_per_objfile->macinfo.buffer == NULL)
14686 {
14687 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14688 return;
14689 }
14690
14691 /* First pass: Find the name of the base filename.
14692 This filename is needed in order to process all macros whose definition
14693 (or undefinition) comes from the command line. These macros are defined
14694 before the first DW_MACINFO_start_file entry, and yet still need to be
14695 associated to the base file.
14696
14697 To determine the base file name, we scan the macro definitions until we
14698 reach the first DW_MACINFO_start_file entry. We then initialize
14699 CURRENT_FILE accordingly so that any macro definition found before the
14700 first DW_MACINFO_start_file can still be associated to the base file. */
14701
14702 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14703 mac_end = dwarf2_per_objfile->macinfo.buffer
14704 + dwarf2_per_objfile->macinfo.size;
14705
14706 do
14707 {
14708 /* Do we at least have room for a macinfo type byte? */
14709 if (mac_ptr >= mac_end)
14710 {
14711 /* Complaint is printed during the second pass as GDB will probably
14712 stop the first pass earlier upon finding
14713 DW_MACINFO_start_file. */
14714 break;
14715 }
14716
14717 macinfo_type = read_1_byte (abfd, mac_ptr);
14718 mac_ptr++;
14719
14720 switch (macinfo_type)
14721 {
14722 /* A zero macinfo type indicates the end of the macro
14723 information. */
14724 case 0:
14725 break;
14726
14727 case DW_MACINFO_define:
14728 case DW_MACINFO_undef:
14729 /* Only skip the data by MAC_PTR. */
14730 {
14731 unsigned int bytes_read;
14732
14733 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14734 mac_ptr += bytes_read;
14735 read_direct_string (abfd, mac_ptr, &bytes_read);
14736 mac_ptr += bytes_read;
14737 }
14738 break;
14739
14740 case DW_MACINFO_start_file:
14741 {
14742 unsigned int bytes_read;
14743 int line, file;
14744
14745 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14746 mac_ptr += bytes_read;
14747 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14748 mac_ptr += bytes_read;
14749
14750 current_file = macro_start_file (file, line, current_file,
14751 comp_dir, lh, cu->objfile);
14752 }
14753 break;
14754
14755 case DW_MACINFO_end_file:
14756 /* No data to skip by MAC_PTR. */
14757 break;
14758
14759 case DW_MACINFO_vendor_ext:
14760 /* Only skip the data by MAC_PTR. */
14761 {
14762 unsigned int bytes_read;
14763
14764 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14765 mac_ptr += bytes_read;
14766 read_direct_string (abfd, mac_ptr, &bytes_read);
14767 mac_ptr += bytes_read;
14768 }
14769 break;
14770
14771 default:
14772 break;
14773 }
14774 } while (macinfo_type != 0 && current_file == NULL);
14775
14776 /* Second pass: Process all entries.
14777
14778 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14779 command-line macro definitions/undefinitions. This flag is unset when we
14780 reach the first DW_MACINFO_start_file entry. */
14781
14782 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14783
14784 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14785 GDB is still reading the definitions from command line. First
14786 DW_MACINFO_start_file will need to be ignored as it was already executed
14787 to create CURRENT_FILE for the main source holding also the command line
14788 definitions. On first met DW_MACINFO_start_file this flag is reset to
14789 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14790
14791 at_commandline = 1;
14792
14793 do
14794 {
14795 /* Do we at least have room for a macinfo type byte? */
14796 if (mac_ptr >= mac_end)
14797 {
14798 dwarf2_macros_too_long_complaint ();
14799 break;
14800 }
14801
14802 macinfo_type = read_1_byte (abfd, mac_ptr);
14803 mac_ptr++;
14804
14805 switch (macinfo_type)
14806 {
14807 /* A zero macinfo type indicates the end of the macro
14808 information. */
14809 case 0:
14810 break;
14811
14812 case DW_MACINFO_define:
14813 case DW_MACINFO_undef:
14814 {
14815 unsigned int bytes_read;
14816 int line;
14817 char *body;
14818
14819 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14820 mac_ptr += bytes_read;
14821 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14822 mac_ptr += bytes_read;
14823
14824 if (! current_file)
14825 {
14826 /* DWARF violation as no main source is present. */
14827 complaint (&symfile_complaints,
14828 _("debug info with no main source gives macro %s "
14829 "on line %d: %s"),
14830 macinfo_type == DW_MACINFO_define ?
14831 _("definition") :
14832 macinfo_type == DW_MACINFO_undef ?
14833 _("undefinition") :
14834 _("something-or-other"), line, body);
14835 break;
14836 }
14837 if ((line == 0 && !at_commandline)
14838 || (line != 0 && at_commandline))
14839 complaint (&symfile_complaints,
14840 _("debug info gives %s macro %s with %s line %d: %s"),
14841 at_commandline ? _("command-line") : _("in-file"),
14842 macinfo_type == DW_MACINFO_define ?
14843 _("definition") :
14844 macinfo_type == DW_MACINFO_undef ?
14845 _("undefinition") :
14846 _("something-or-other"),
14847 line == 0 ? _("zero") : _("non-zero"), line, body);
14848
14849 if (macinfo_type == DW_MACINFO_define)
14850 parse_macro_definition (current_file, line, body);
14851 else if (macinfo_type == DW_MACINFO_undef)
14852 macro_undef (current_file, line, body);
14853 }
14854 break;
14855
14856 case DW_MACINFO_start_file:
14857 {
14858 unsigned int bytes_read;
14859 int line, file;
14860
14861 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14862 mac_ptr += bytes_read;
14863 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14864 mac_ptr += bytes_read;
14865
14866 if ((line == 0 && !at_commandline)
14867 || (line != 0 && at_commandline))
14868 complaint (&symfile_complaints,
14869 _("debug info gives source %d included "
14870 "from %s at %s line %d"),
14871 file, at_commandline ? _("command-line") : _("file"),
14872 line == 0 ? _("zero") : _("non-zero"), line);
14873
14874 if (at_commandline)
14875 {
14876 /* This DW_MACINFO_start_file was executed in the pass one. */
14877 at_commandline = 0;
14878 }
14879 else
14880 current_file = macro_start_file (file, line,
14881 current_file, comp_dir,
14882 lh, cu->objfile);
14883 }
14884 break;
14885
14886 case DW_MACINFO_end_file:
14887 if (! current_file)
14888 complaint (&symfile_complaints,
14889 _("macro debug info has an unmatched "
14890 "`close_file' directive"));
14891 else
14892 {
14893 current_file = current_file->included_by;
14894 if (! current_file)
14895 {
14896 enum dwarf_macinfo_record_type next_type;
14897
14898 /* GCC circa March 2002 doesn't produce the zero
14899 type byte marking the end of the compilation
14900 unit. Complain if it's not there, but exit no
14901 matter what. */
14902
14903 /* Do we at least have room for a macinfo type byte? */
14904 if (mac_ptr >= mac_end)
14905 {
14906 dwarf2_macros_too_long_complaint ();
14907 return;
14908 }
14909
14910 /* We don't increment mac_ptr here, so this is just
14911 a look-ahead. */
14912 next_type = read_1_byte (abfd, mac_ptr);
14913 if (next_type != 0)
14914 complaint (&symfile_complaints,
14915 _("no terminating 0-type entry for "
14916 "macros in `.debug_macinfo' section"));
14917
14918 return;
14919 }
14920 }
14921 break;
14922
14923 case DW_MACINFO_vendor_ext:
14924 {
14925 unsigned int bytes_read;
14926 int constant;
14927
14928 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14929 mac_ptr += bytes_read;
14930 read_direct_string (abfd, mac_ptr, &bytes_read);
14931 mac_ptr += bytes_read;
14932
14933 /* We don't recognize any vendor extensions. */
14934 }
14935 break;
14936 }
14937 } while (macinfo_type != 0);
14938 }
14939
14940 /* Check if the attribute's form is a DW_FORM_block*
14941 if so return true else false. */
14942 static int
14943 attr_form_is_block (struct attribute *attr)
14944 {
14945 return (attr == NULL ? 0 :
14946 attr->form == DW_FORM_block1
14947 || attr->form == DW_FORM_block2
14948 || attr->form == DW_FORM_block4
14949 || attr->form == DW_FORM_block
14950 || attr->form == DW_FORM_exprloc);
14951 }
14952
14953 /* Return non-zero if ATTR's value is a section offset --- classes
14954 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14955 You may use DW_UNSND (attr) to retrieve such offsets.
14956
14957 Section 7.5.4, "Attribute Encodings", explains that no attribute
14958 may have a value that belongs to more than one of these classes; it
14959 would be ambiguous if we did, because we use the same forms for all
14960 of them. */
14961 static int
14962 attr_form_is_section_offset (struct attribute *attr)
14963 {
14964 return (attr->form == DW_FORM_data4
14965 || attr->form == DW_FORM_data8
14966 || attr->form == DW_FORM_sec_offset);
14967 }
14968
14969
14970 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14971 zero otherwise. When this function returns true, you can apply
14972 dwarf2_get_attr_constant_value to it.
14973
14974 However, note that for some attributes you must check
14975 attr_form_is_section_offset before using this test. DW_FORM_data4
14976 and DW_FORM_data8 are members of both the constant class, and of
14977 the classes that contain offsets into other debug sections
14978 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14979 that, if an attribute's can be either a constant or one of the
14980 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14981 taken as section offsets, not constants. */
14982 static int
14983 attr_form_is_constant (struct attribute *attr)
14984 {
14985 switch (attr->form)
14986 {
14987 case DW_FORM_sdata:
14988 case DW_FORM_udata:
14989 case DW_FORM_data1:
14990 case DW_FORM_data2:
14991 case DW_FORM_data4:
14992 case DW_FORM_data8:
14993 return 1;
14994 default:
14995 return 0;
14996 }
14997 }
14998
14999 /* A helper function that fills in a dwarf2_loclist_baton. */
15000
15001 static void
15002 fill_in_loclist_baton (struct dwarf2_cu *cu,
15003 struct dwarf2_loclist_baton *baton,
15004 struct attribute *attr)
15005 {
15006 dwarf2_read_section (dwarf2_per_objfile->objfile,
15007 &dwarf2_per_objfile->loc);
15008
15009 baton->per_cu = cu->per_cu;
15010 gdb_assert (baton->per_cu);
15011 /* We don't know how long the location list is, but make sure we
15012 don't run off the edge of the section. */
15013 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15014 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15015 baton->base_address = cu->base_address;
15016 }
15017
15018 static void
15019 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15020 struct dwarf2_cu *cu)
15021 {
15022 if (attr_form_is_section_offset (attr)
15023 /* ".debug_loc" may not exist at all, or the offset may be outside
15024 the section. If so, fall through to the complaint in the
15025 other branch. */
15026 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15027 &dwarf2_per_objfile->loc))
15028 {
15029 struct dwarf2_loclist_baton *baton;
15030
15031 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15032 sizeof (struct dwarf2_loclist_baton));
15033
15034 fill_in_loclist_baton (cu, baton, attr);
15035
15036 if (cu->base_known == 0)
15037 complaint (&symfile_complaints,
15038 _("Location list used without "
15039 "specifying the CU base address."));
15040
15041 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15042 SYMBOL_LOCATION_BATON (sym) = baton;
15043 }
15044 else
15045 {
15046 struct dwarf2_locexpr_baton *baton;
15047
15048 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15049 sizeof (struct dwarf2_locexpr_baton));
15050 baton->per_cu = cu->per_cu;
15051 gdb_assert (baton->per_cu);
15052
15053 if (attr_form_is_block (attr))
15054 {
15055 /* Note that we're just copying the block's data pointer
15056 here, not the actual data. We're still pointing into the
15057 info_buffer for SYM's objfile; right now we never release
15058 that buffer, but when we do clean up properly this may
15059 need to change. */
15060 baton->size = DW_BLOCK (attr)->size;
15061 baton->data = DW_BLOCK (attr)->data;
15062 }
15063 else
15064 {
15065 dwarf2_invalid_attrib_class_complaint ("location description",
15066 SYMBOL_NATURAL_NAME (sym));
15067 baton->size = 0;
15068 baton->data = NULL;
15069 }
15070
15071 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15072 SYMBOL_LOCATION_BATON (sym) = baton;
15073 }
15074 }
15075
15076 /* Return the OBJFILE associated with the compilation unit CU. If CU
15077 came from a separate debuginfo file, then the master objfile is
15078 returned. */
15079
15080 struct objfile *
15081 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15082 {
15083 struct objfile *objfile = per_cu->objfile;
15084
15085 /* Return the master objfile, so that we can report and look up the
15086 correct file containing this variable. */
15087 if (objfile->separate_debug_objfile_backlink)
15088 objfile = objfile->separate_debug_objfile_backlink;
15089
15090 return objfile;
15091 }
15092
15093 /* Return the address size given in the compilation unit header for CU. */
15094
15095 CORE_ADDR
15096 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15097 {
15098 if (per_cu->cu)
15099 return per_cu->cu->header.addr_size;
15100 else
15101 {
15102 /* If the CU is not currently read in, we re-read its header. */
15103 struct objfile *objfile = per_cu->objfile;
15104 struct dwarf2_per_objfile *per_objfile
15105 = objfile_data (objfile, dwarf2_objfile_data_key);
15106 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15107 struct comp_unit_head cu_header;
15108
15109 memset (&cu_header, 0, sizeof cu_header);
15110 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15111 return cu_header.addr_size;
15112 }
15113 }
15114
15115 /* Return the offset size given in the compilation unit header for CU. */
15116
15117 int
15118 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15119 {
15120 if (per_cu->cu)
15121 return per_cu->cu->header.offset_size;
15122 else
15123 {
15124 /* If the CU is not currently read in, we re-read its header. */
15125 struct objfile *objfile = per_cu->objfile;
15126 struct dwarf2_per_objfile *per_objfile
15127 = objfile_data (objfile, dwarf2_objfile_data_key);
15128 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15129 struct comp_unit_head cu_header;
15130
15131 memset (&cu_header, 0, sizeof cu_header);
15132 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15133 return cu_header.offset_size;
15134 }
15135 }
15136
15137 /* Return the text offset of the CU. The returned offset comes from
15138 this CU's objfile. If this objfile came from a separate debuginfo
15139 file, then the offset may be different from the corresponding
15140 offset in the parent objfile. */
15141
15142 CORE_ADDR
15143 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15144 {
15145 struct objfile *objfile = per_cu->objfile;
15146
15147 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15148 }
15149
15150 /* Locate the .debug_info compilation unit from CU's objfile which contains
15151 the DIE at OFFSET. Raises an error on failure. */
15152
15153 static struct dwarf2_per_cu_data *
15154 dwarf2_find_containing_comp_unit (unsigned int offset,
15155 struct objfile *objfile)
15156 {
15157 struct dwarf2_per_cu_data *this_cu;
15158 int low, high;
15159
15160 low = 0;
15161 high = dwarf2_per_objfile->n_comp_units - 1;
15162 while (high > low)
15163 {
15164 int mid = low + (high - low) / 2;
15165
15166 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15167 high = mid;
15168 else
15169 low = mid + 1;
15170 }
15171 gdb_assert (low == high);
15172 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15173 {
15174 if (low == 0)
15175 error (_("Dwarf Error: could not find partial DIE containing "
15176 "offset 0x%lx [in module %s]"),
15177 (long) offset, bfd_get_filename (objfile->obfd));
15178
15179 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15180 return dwarf2_per_objfile->all_comp_units[low-1];
15181 }
15182 else
15183 {
15184 this_cu = dwarf2_per_objfile->all_comp_units[low];
15185 if (low == dwarf2_per_objfile->n_comp_units - 1
15186 && offset >= this_cu->offset + this_cu->length)
15187 error (_("invalid dwarf2 offset %u"), offset);
15188 gdb_assert (offset < this_cu->offset + this_cu->length);
15189 return this_cu;
15190 }
15191 }
15192
15193 /* Locate the compilation unit from OBJFILE which is located at exactly
15194 OFFSET. Raises an error on failure. */
15195
15196 static struct dwarf2_per_cu_data *
15197 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15198 {
15199 struct dwarf2_per_cu_data *this_cu;
15200
15201 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15202 if (this_cu->offset != offset)
15203 error (_("no compilation unit with offset %u."), offset);
15204 return this_cu;
15205 }
15206
15207 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15208
15209 static void
15210 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15211 {
15212 memset (cu, 0, sizeof (*cu));
15213 cu->objfile = objfile;
15214 obstack_init (&cu->comp_unit_obstack);
15215 }
15216
15217 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15218
15219 static void
15220 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15221 {
15222 struct attribute *attr;
15223
15224 /* Set the language we're debugging. */
15225 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15226 if (attr)
15227 set_cu_language (DW_UNSND (attr), cu);
15228 else
15229 {
15230 cu->language = language_minimal;
15231 cu->language_defn = language_def (cu->language);
15232 }
15233 }
15234
15235 /* Release one cached compilation unit, CU. We unlink it from the tree
15236 of compilation units, but we don't remove it from the read_in_chain;
15237 the caller is responsible for that.
15238 NOTE: DATA is a void * because this function is also used as a
15239 cleanup routine. */
15240
15241 static void
15242 free_one_comp_unit (void *data)
15243 {
15244 struct dwarf2_cu *cu = data;
15245
15246 if (cu->per_cu != NULL)
15247 cu->per_cu->cu = NULL;
15248 cu->per_cu = NULL;
15249
15250 obstack_free (&cu->comp_unit_obstack, NULL);
15251
15252 xfree (cu);
15253 }
15254
15255 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15256 when we're finished with it. We can't free the pointer itself, but be
15257 sure to unlink it from the cache. Also release any associated storage
15258 and perform cache maintenance.
15259
15260 Only used during partial symbol parsing. */
15261
15262 static void
15263 free_stack_comp_unit (void *data)
15264 {
15265 struct dwarf2_cu *cu = data;
15266
15267 obstack_free (&cu->comp_unit_obstack, NULL);
15268 cu->partial_dies = NULL;
15269
15270 if (cu->per_cu != NULL)
15271 {
15272 /* This compilation unit is on the stack in our caller, so we
15273 should not xfree it. Just unlink it. */
15274 cu->per_cu->cu = NULL;
15275 cu->per_cu = NULL;
15276
15277 /* If we had a per-cu pointer, then we may have other compilation
15278 units loaded, so age them now. */
15279 age_cached_comp_units ();
15280 }
15281 }
15282
15283 /* Free all cached compilation units. */
15284
15285 static void
15286 free_cached_comp_units (void *data)
15287 {
15288 struct dwarf2_per_cu_data *per_cu, **last_chain;
15289
15290 per_cu = dwarf2_per_objfile->read_in_chain;
15291 last_chain = &dwarf2_per_objfile->read_in_chain;
15292 while (per_cu != NULL)
15293 {
15294 struct dwarf2_per_cu_data *next_cu;
15295
15296 next_cu = per_cu->cu->read_in_chain;
15297
15298 free_one_comp_unit (per_cu->cu);
15299 *last_chain = next_cu;
15300
15301 per_cu = next_cu;
15302 }
15303 }
15304
15305 /* Increase the age counter on each cached compilation unit, and free
15306 any that are too old. */
15307
15308 static void
15309 age_cached_comp_units (void)
15310 {
15311 struct dwarf2_per_cu_data *per_cu, **last_chain;
15312
15313 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15314 per_cu = dwarf2_per_objfile->read_in_chain;
15315 while (per_cu != NULL)
15316 {
15317 per_cu->cu->last_used ++;
15318 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15319 dwarf2_mark (per_cu->cu);
15320 per_cu = per_cu->cu->read_in_chain;
15321 }
15322
15323 per_cu = dwarf2_per_objfile->read_in_chain;
15324 last_chain = &dwarf2_per_objfile->read_in_chain;
15325 while (per_cu != NULL)
15326 {
15327 struct dwarf2_per_cu_data *next_cu;
15328
15329 next_cu = per_cu->cu->read_in_chain;
15330
15331 if (!per_cu->cu->mark)
15332 {
15333 free_one_comp_unit (per_cu->cu);
15334 *last_chain = next_cu;
15335 }
15336 else
15337 last_chain = &per_cu->cu->read_in_chain;
15338
15339 per_cu = next_cu;
15340 }
15341 }
15342
15343 /* Remove a single compilation unit from the cache. */
15344
15345 static void
15346 free_one_cached_comp_unit (void *target_cu)
15347 {
15348 struct dwarf2_per_cu_data *per_cu, **last_chain;
15349
15350 per_cu = dwarf2_per_objfile->read_in_chain;
15351 last_chain = &dwarf2_per_objfile->read_in_chain;
15352 while (per_cu != NULL)
15353 {
15354 struct dwarf2_per_cu_data *next_cu;
15355
15356 next_cu = per_cu->cu->read_in_chain;
15357
15358 if (per_cu->cu == target_cu)
15359 {
15360 free_one_comp_unit (per_cu->cu);
15361 *last_chain = next_cu;
15362 break;
15363 }
15364 else
15365 last_chain = &per_cu->cu->read_in_chain;
15366
15367 per_cu = next_cu;
15368 }
15369 }
15370
15371 /* Release all extra memory associated with OBJFILE. */
15372
15373 void
15374 dwarf2_free_objfile (struct objfile *objfile)
15375 {
15376 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15377
15378 if (dwarf2_per_objfile == NULL)
15379 return;
15380
15381 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15382 free_cached_comp_units (NULL);
15383
15384 if (dwarf2_per_objfile->quick_file_names_table)
15385 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15386
15387 /* Everything else should be on the objfile obstack. */
15388 }
15389
15390 /* A pair of DIE offset and GDB type pointer. We store these
15391 in a hash table separate from the DIEs, and preserve them
15392 when the DIEs are flushed out of cache. */
15393
15394 struct dwarf2_offset_and_type
15395 {
15396 unsigned int offset;
15397 struct type *type;
15398 };
15399
15400 /* Hash function for a dwarf2_offset_and_type. */
15401
15402 static hashval_t
15403 offset_and_type_hash (const void *item)
15404 {
15405 const struct dwarf2_offset_and_type *ofs = item;
15406
15407 return ofs->offset;
15408 }
15409
15410 /* Equality function for a dwarf2_offset_and_type. */
15411
15412 static int
15413 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15414 {
15415 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15416 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15417
15418 return ofs_lhs->offset == ofs_rhs->offset;
15419 }
15420
15421 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15422 table if necessary. For convenience, return TYPE.
15423
15424 The DIEs reading must have careful ordering to:
15425 * Not cause infite loops trying to read in DIEs as a prerequisite for
15426 reading current DIE.
15427 * Not trying to dereference contents of still incompletely read in types
15428 while reading in other DIEs.
15429 * Enable referencing still incompletely read in types just by a pointer to
15430 the type without accessing its fields.
15431
15432 Therefore caller should follow these rules:
15433 * Try to fetch any prerequisite types we may need to build this DIE type
15434 before building the type and calling set_die_type.
15435 * After building type call set_die_type for current DIE as soon as
15436 possible before fetching more types to complete the current type.
15437 * Make the type as complete as possible before fetching more types. */
15438
15439 static struct type *
15440 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15441 {
15442 struct dwarf2_offset_and_type **slot, ofs;
15443 struct objfile *objfile = cu->objfile;
15444 htab_t *type_hash_ptr;
15445
15446 /* For Ada types, make sure that the gnat-specific data is always
15447 initialized (if not already set). There are a few types where
15448 we should not be doing so, because the type-specific area is
15449 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15450 where the type-specific area is used to store the floatformat).
15451 But this is not a problem, because the gnat-specific information
15452 is actually not needed for these types. */
15453 if (need_gnat_info (cu)
15454 && TYPE_CODE (type) != TYPE_CODE_FUNC
15455 && TYPE_CODE (type) != TYPE_CODE_FLT
15456 && !HAVE_GNAT_AUX_INFO (type))
15457 INIT_GNAT_SPECIFIC (type);
15458
15459 if (cu->per_cu->debug_type_section)
15460 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15461 else
15462 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15463
15464 if (*type_hash_ptr == NULL)
15465 {
15466 *type_hash_ptr
15467 = htab_create_alloc_ex (127,
15468 offset_and_type_hash,
15469 offset_and_type_eq,
15470 NULL,
15471 &objfile->objfile_obstack,
15472 hashtab_obstack_allocate,
15473 dummy_obstack_deallocate);
15474 }
15475
15476 ofs.offset = die->offset;
15477 ofs.type = type;
15478 slot = (struct dwarf2_offset_and_type **)
15479 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15480 if (*slot)
15481 complaint (&symfile_complaints,
15482 _("A problem internal to GDB: DIE 0x%x has type already set"),
15483 die->offset);
15484 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15485 **slot = ofs;
15486 return type;
15487 }
15488
15489 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15490 table, or return NULL if the die does not have a saved type. */
15491
15492 static struct type *
15493 get_die_type_at_offset (unsigned int offset,
15494 struct dwarf2_per_cu_data *per_cu)
15495 {
15496 struct dwarf2_offset_and_type *slot, ofs;
15497 htab_t type_hash;
15498
15499 if (per_cu->debug_type_section)
15500 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15501 else
15502 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15503 if (type_hash == NULL)
15504 return NULL;
15505
15506 ofs.offset = offset;
15507 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15508 if (slot)
15509 return slot->type;
15510 else
15511 return NULL;
15512 }
15513
15514 /* Look up the type for DIE in the appropriate type_hash table,
15515 or return NULL if DIE does not have a saved type. */
15516
15517 static struct type *
15518 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15519 {
15520 return get_die_type_at_offset (die->offset, cu->per_cu);
15521 }
15522
15523 /* Add a dependence relationship from CU to REF_PER_CU. */
15524
15525 static void
15526 dwarf2_add_dependence (struct dwarf2_cu *cu,
15527 struct dwarf2_per_cu_data *ref_per_cu)
15528 {
15529 void **slot;
15530
15531 if (cu->dependencies == NULL)
15532 cu->dependencies
15533 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15534 NULL, &cu->comp_unit_obstack,
15535 hashtab_obstack_allocate,
15536 dummy_obstack_deallocate);
15537
15538 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15539 if (*slot == NULL)
15540 *slot = ref_per_cu;
15541 }
15542
15543 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15544 Set the mark field in every compilation unit in the
15545 cache that we must keep because we are keeping CU. */
15546
15547 static int
15548 dwarf2_mark_helper (void **slot, void *data)
15549 {
15550 struct dwarf2_per_cu_data *per_cu;
15551
15552 per_cu = (struct dwarf2_per_cu_data *) *slot;
15553
15554 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15555 reading of the chain. As such dependencies remain valid it is not much
15556 useful to track and undo them during QUIT cleanups. */
15557 if (per_cu->cu == NULL)
15558 return 1;
15559
15560 if (per_cu->cu->mark)
15561 return 1;
15562 per_cu->cu->mark = 1;
15563
15564 if (per_cu->cu->dependencies != NULL)
15565 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15566
15567 return 1;
15568 }
15569
15570 /* Set the mark field in CU and in every other compilation unit in the
15571 cache that we must keep because we are keeping CU. */
15572
15573 static void
15574 dwarf2_mark (struct dwarf2_cu *cu)
15575 {
15576 if (cu->mark)
15577 return;
15578 cu->mark = 1;
15579 if (cu->dependencies != NULL)
15580 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15581 }
15582
15583 static void
15584 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15585 {
15586 while (per_cu)
15587 {
15588 per_cu->cu->mark = 0;
15589 per_cu = per_cu->cu->read_in_chain;
15590 }
15591 }
15592
15593 /* Trivial hash function for partial_die_info: the hash value of a DIE
15594 is its offset in .debug_info for this objfile. */
15595
15596 static hashval_t
15597 partial_die_hash (const void *item)
15598 {
15599 const struct partial_die_info *part_die = item;
15600
15601 return part_die->offset;
15602 }
15603
15604 /* Trivial comparison function for partial_die_info structures: two DIEs
15605 are equal if they have the same offset. */
15606
15607 static int
15608 partial_die_eq (const void *item_lhs, const void *item_rhs)
15609 {
15610 const struct partial_die_info *part_die_lhs = item_lhs;
15611 const struct partial_die_info *part_die_rhs = item_rhs;
15612
15613 return part_die_lhs->offset == part_die_rhs->offset;
15614 }
15615
15616 static struct cmd_list_element *set_dwarf2_cmdlist;
15617 static struct cmd_list_element *show_dwarf2_cmdlist;
15618
15619 static void
15620 set_dwarf2_cmd (char *args, int from_tty)
15621 {
15622 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15623 }
15624
15625 static void
15626 show_dwarf2_cmd (char *args, int from_tty)
15627 {
15628 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15629 }
15630
15631 /* If section described by INFO was mmapped, munmap it now. */
15632
15633 static void
15634 munmap_section_buffer (struct dwarf2_section_info *info)
15635 {
15636 if (info->map_addr != NULL)
15637 {
15638 #ifdef HAVE_MMAP
15639 int res;
15640
15641 res = munmap (info->map_addr, info->map_len);
15642 gdb_assert (res == 0);
15643 #else
15644 /* Without HAVE_MMAP, we should never be here to begin with. */
15645 gdb_assert_not_reached ("no mmap support");
15646 #endif
15647 }
15648 }
15649
15650 /* munmap debug sections for OBJFILE, if necessary. */
15651
15652 static void
15653 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15654 {
15655 struct dwarf2_per_objfile *data = d;
15656 int ix;
15657 struct dwarf2_section_info *section;
15658
15659 /* This is sorted according to the order they're defined in to make it easier
15660 to keep in sync. */
15661 munmap_section_buffer (&data->info);
15662 munmap_section_buffer (&data->abbrev);
15663 munmap_section_buffer (&data->line);
15664 munmap_section_buffer (&data->loc);
15665 munmap_section_buffer (&data->macinfo);
15666 munmap_section_buffer (&data->str);
15667 munmap_section_buffer (&data->ranges);
15668 munmap_section_buffer (&data->frame);
15669 munmap_section_buffer (&data->eh_frame);
15670 munmap_section_buffer (&data->gdb_index);
15671
15672 for (ix = 0;
15673 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
15674 ++ix)
15675 munmap_section_buffer (section);
15676
15677 VEC_free (dwarf2_section_info_def, data->types);
15678 }
15679
15680 \f
15681 /* The "save gdb-index" command. */
15682
15683 /* The contents of the hash table we create when building the string
15684 table. */
15685 struct strtab_entry
15686 {
15687 offset_type offset;
15688 const char *str;
15689 };
15690
15691 /* Hash function for a strtab_entry.
15692
15693 Function is used only during write_hash_table so no index format backward
15694 compatibility is needed. */
15695
15696 static hashval_t
15697 hash_strtab_entry (const void *e)
15698 {
15699 const struct strtab_entry *entry = e;
15700 return mapped_index_string_hash (INT_MAX, entry->str);
15701 }
15702
15703 /* Equality function for a strtab_entry. */
15704
15705 static int
15706 eq_strtab_entry (const void *a, const void *b)
15707 {
15708 const struct strtab_entry *ea = a;
15709 const struct strtab_entry *eb = b;
15710 return !strcmp (ea->str, eb->str);
15711 }
15712
15713 /* Create a strtab_entry hash table. */
15714
15715 static htab_t
15716 create_strtab (void)
15717 {
15718 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15719 xfree, xcalloc, xfree);
15720 }
15721
15722 /* Add a string to the constant pool. Return the string's offset in
15723 host order. */
15724
15725 static offset_type
15726 add_string (htab_t table, struct obstack *cpool, const char *str)
15727 {
15728 void **slot;
15729 struct strtab_entry entry;
15730 struct strtab_entry *result;
15731
15732 entry.str = str;
15733 slot = htab_find_slot (table, &entry, INSERT);
15734 if (*slot)
15735 result = *slot;
15736 else
15737 {
15738 result = XNEW (struct strtab_entry);
15739 result->offset = obstack_object_size (cpool);
15740 result->str = str;
15741 obstack_grow_str0 (cpool, str);
15742 *slot = result;
15743 }
15744 return result->offset;
15745 }
15746
15747 /* An entry in the symbol table. */
15748 struct symtab_index_entry
15749 {
15750 /* The name of the symbol. */
15751 const char *name;
15752 /* The offset of the name in the constant pool. */
15753 offset_type index_offset;
15754 /* A sorted vector of the indices of all the CUs that hold an object
15755 of this name. */
15756 VEC (offset_type) *cu_indices;
15757 };
15758
15759 /* The symbol table. This is a power-of-2-sized hash table. */
15760 struct mapped_symtab
15761 {
15762 offset_type n_elements;
15763 offset_type size;
15764 struct symtab_index_entry **data;
15765 };
15766
15767 /* Hash function for a symtab_index_entry. */
15768
15769 static hashval_t
15770 hash_symtab_entry (const void *e)
15771 {
15772 const struct symtab_index_entry *entry = e;
15773 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15774 sizeof (offset_type) * VEC_length (offset_type,
15775 entry->cu_indices),
15776 0);
15777 }
15778
15779 /* Equality function for a symtab_index_entry. */
15780
15781 static int
15782 eq_symtab_entry (const void *a, const void *b)
15783 {
15784 const struct symtab_index_entry *ea = a;
15785 const struct symtab_index_entry *eb = b;
15786 int len = VEC_length (offset_type, ea->cu_indices);
15787 if (len != VEC_length (offset_type, eb->cu_indices))
15788 return 0;
15789 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15790 VEC_address (offset_type, eb->cu_indices),
15791 sizeof (offset_type) * len);
15792 }
15793
15794 /* Destroy a symtab_index_entry. */
15795
15796 static void
15797 delete_symtab_entry (void *p)
15798 {
15799 struct symtab_index_entry *entry = p;
15800 VEC_free (offset_type, entry->cu_indices);
15801 xfree (entry);
15802 }
15803
15804 /* Create a hash table holding symtab_index_entry objects. */
15805
15806 static htab_t
15807 create_symbol_hash_table (void)
15808 {
15809 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15810 delete_symtab_entry, xcalloc, xfree);
15811 }
15812
15813 /* Create a new mapped symtab object. */
15814
15815 static struct mapped_symtab *
15816 create_mapped_symtab (void)
15817 {
15818 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15819 symtab->n_elements = 0;
15820 symtab->size = 1024;
15821 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15822 return symtab;
15823 }
15824
15825 /* Destroy a mapped_symtab. */
15826
15827 static void
15828 cleanup_mapped_symtab (void *p)
15829 {
15830 struct mapped_symtab *symtab = p;
15831 /* The contents of the array are freed when the other hash table is
15832 destroyed. */
15833 xfree (symtab->data);
15834 xfree (symtab);
15835 }
15836
15837 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15838 the slot.
15839
15840 Function is used only during write_hash_table so no index format backward
15841 compatibility is needed. */
15842
15843 static struct symtab_index_entry **
15844 find_slot (struct mapped_symtab *symtab, const char *name)
15845 {
15846 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15847
15848 index = hash & (symtab->size - 1);
15849 step = ((hash * 17) & (symtab->size - 1)) | 1;
15850
15851 for (;;)
15852 {
15853 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15854 return &symtab->data[index];
15855 index = (index + step) & (symtab->size - 1);
15856 }
15857 }
15858
15859 /* Expand SYMTAB's hash table. */
15860
15861 static void
15862 hash_expand (struct mapped_symtab *symtab)
15863 {
15864 offset_type old_size = symtab->size;
15865 offset_type i;
15866 struct symtab_index_entry **old_entries = symtab->data;
15867
15868 symtab->size *= 2;
15869 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15870
15871 for (i = 0; i < old_size; ++i)
15872 {
15873 if (old_entries[i])
15874 {
15875 struct symtab_index_entry **slot = find_slot (symtab,
15876 old_entries[i]->name);
15877 *slot = old_entries[i];
15878 }
15879 }
15880
15881 xfree (old_entries);
15882 }
15883
15884 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15885 is the index of the CU in which the symbol appears. */
15886
15887 static void
15888 add_index_entry (struct mapped_symtab *symtab, const char *name,
15889 offset_type cu_index)
15890 {
15891 struct symtab_index_entry **slot;
15892
15893 ++symtab->n_elements;
15894 if (4 * symtab->n_elements / 3 >= symtab->size)
15895 hash_expand (symtab);
15896
15897 slot = find_slot (symtab, name);
15898 if (!*slot)
15899 {
15900 *slot = XNEW (struct symtab_index_entry);
15901 (*slot)->name = name;
15902 (*slot)->cu_indices = NULL;
15903 }
15904 /* Don't push an index twice. Due to how we add entries we only
15905 have to check the last one. */
15906 if (VEC_empty (offset_type, (*slot)->cu_indices)
15907 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15908 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15909 }
15910
15911 /* Add a vector of indices to the constant pool. */
15912
15913 static offset_type
15914 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15915 struct symtab_index_entry *entry)
15916 {
15917 void **slot;
15918
15919 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15920 if (!*slot)
15921 {
15922 offset_type len = VEC_length (offset_type, entry->cu_indices);
15923 offset_type val = MAYBE_SWAP (len);
15924 offset_type iter;
15925 int i;
15926
15927 *slot = entry;
15928 entry->index_offset = obstack_object_size (cpool);
15929
15930 obstack_grow (cpool, &val, sizeof (val));
15931 for (i = 0;
15932 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15933 ++i)
15934 {
15935 val = MAYBE_SWAP (iter);
15936 obstack_grow (cpool, &val, sizeof (val));
15937 }
15938 }
15939 else
15940 {
15941 struct symtab_index_entry *old_entry = *slot;
15942 entry->index_offset = old_entry->index_offset;
15943 entry = old_entry;
15944 }
15945 return entry->index_offset;
15946 }
15947
15948 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15949 constant pool entries going into the obstack CPOOL. */
15950
15951 static void
15952 write_hash_table (struct mapped_symtab *symtab,
15953 struct obstack *output, struct obstack *cpool)
15954 {
15955 offset_type i;
15956 htab_t symbol_hash_table;
15957 htab_t str_table;
15958
15959 symbol_hash_table = create_symbol_hash_table ();
15960 str_table = create_strtab ();
15961
15962 /* We add all the index vectors to the constant pool first, to
15963 ensure alignment is ok. */
15964 for (i = 0; i < symtab->size; ++i)
15965 {
15966 if (symtab->data[i])
15967 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15968 }
15969
15970 /* Now write out the hash table. */
15971 for (i = 0; i < symtab->size; ++i)
15972 {
15973 offset_type str_off, vec_off;
15974
15975 if (symtab->data[i])
15976 {
15977 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15978 vec_off = symtab->data[i]->index_offset;
15979 }
15980 else
15981 {
15982 /* While 0 is a valid constant pool index, it is not valid
15983 to have 0 for both offsets. */
15984 str_off = 0;
15985 vec_off = 0;
15986 }
15987
15988 str_off = MAYBE_SWAP (str_off);
15989 vec_off = MAYBE_SWAP (vec_off);
15990
15991 obstack_grow (output, &str_off, sizeof (str_off));
15992 obstack_grow (output, &vec_off, sizeof (vec_off));
15993 }
15994
15995 htab_delete (str_table);
15996 htab_delete (symbol_hash_table);
15997 }
15998
15999 /* Struct to map psymtab to CU index in the index file. */
16000 struct psymtab_cu_index_map
16001 {
16002 struct partial_symtab *psymtab;
16003 unsigned int cu_index;
16004 };
16005
16006 static hashval_t
16007 hash_psymtab_cu_index (const void *item)
16008 {
16009 const struct psymtab_cu_index_map *map = item;
16010
16011 return htab_hash_pointer (map->psymtab);
16012 }
16013
16014 static int
16015 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16016 {
16017 const struct psymtab_cu_index_map *lhs = item_lhs;
16018 const struct psymtab_cu_index_map *rhs = item_rhs;
16019
16020 return lhs->psymtab == rhs->psymtab;
16021 }
16022
16023 /* Helper struct for building the address table. */
16024 struct addrmap_index_data
16025 {
16026 struct objfile *objfile;
16027 struct obstack *addr_obstack;
16028 htab_t cu_index_htab;
16029
16030 /* Non-zero if the previous_* fields are valid.
16031 We can't write an entry until we see the next entry (since it is only then
16032 that we know the end of the entry). */
16033 int previous_valid;
16034 /* Index of the CU in the table of all CUs in the index file. */
16035 unsigned int previous_cu_index;
16036 /* Start address of the CU. */
16037 CORE_ADDR previous_cu_start;
16038 };
16039
16040 /* Write an address entry to OBSTACK. */
16041
16042 static void
16043 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16044 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16045 {
16046 offset_type cu_index_to_write;
16047 char addr[8];
16048 CORE_ADDR baseaddr;
16049
16050 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16051
16052 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16053 obstack_grow (obstack, addr, 8);
16054 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16055 obstack_grow (obstack, addr, 8);
16056 cu_index_to_write = MAYBE_SWAP (cu_index);
16057 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16058 }
16059
16060 /* Worker function for traversing an addrmap to build the address table. */
16061
16062 static int
16063 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16064 {
16065 struct addrmap_index_data *data = datap;
16066 struct partial_symtab *pst = obj;
16067 offset_type cu_index;
16068 void **slot;
16069
16070 if (data->previous_valid)
16071 add_address_entry (data->objfile, data->addr_obstack,
16072 data->previous_cu_start, start_addr,
16073 data->previous_cu_index);
16074
16075 data->previous_cu_start = start_addr;
16076 if (pst != NULL)
16077 {
16078 struct psymtab_cu_index_map find_map, *map;
16079 find_map.psymtab = pst;
16080 map = htab_find (data->cu_index_htab, &find_map);
16081 gdb_assert (map != NULL);
16082 data->previous_cu_index = map->cu_index;
16083 data->previous_valid = 1;
16084 }
16085 else
16086 data->previous_valid = 0;
16087
16088 return 0;
16089 }
16090
16091 /* Write OBJFILE's address map to OBSTACK.
16092 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16093 in the index file. */
16094
16095 static void
16096 write_address_map (struct objfile *objfile, struct obstack *obstack,
16097 htab_t cu_index_htab)
16098 {
16099 struct addrmap_index_data addrmap_index_data;
16100
16101 /* When writing the address table, we have to cope with the fact that
16102 the addrmap iterator only provides the start of a region; we have to
16103 wait until the next invocation to get the start of the next region. */
16104
16105 addrmap_index_data.objfile = objfile;
16106 addrmap_index_data.addr_obstack = obstack;
16107 addrmap_index_data.cu_index_htab = cu_index_htab;
16108 addrmap_index_data.previous_valid = 0;
16109
16110 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16111 &addrmap_index_data);
16112
16113 /* It's highly unlikely the last entry (end address = 0xff...ff)
16114 is valid, but we should still handle it.
16115 The end address is recorded as the start of the next region, but that
16116 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16117 anyway. */
16118 if (addrmap_index_data.previous_valid)
16119 add_address_entry (objfile, obstack,
16120 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16121 addrmap_index_data.previous_cu_index);
16122 }
16123
16124 /* Add a list of partial symbols to SYMTAB. */
16125
16126 static void
16127 write_psymbols (struct mapped_symtab *symtab,
16128 htab_t psyms_seen,
16129 struct partial_symbol **psymp,
16130 int count,
16131 offset_type cu_index,
16132 int is_static)
16133 {
16134 for (; count-- > 0; ++psymp)
16135 {
16136 void **slot, *lookup;
16137
16138 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16139 error (_("Ada is not currently supported by the index"));
16140
16141 /* We only want to add a given psymbol once. However, we also
16142 want to account for whether it is global or static. So, we
16143 may add it twice, using slightly different values. */
16144 if (is_static)
16145 {
16146 uintptr_t val = 1 | (uintptr_t) *psymp;
16147
16148 lookup = (void *) val;
16149 }
16150 else
16151 lookup = *psymp;
16152
16153 /* Only add a given psymbol once. */
16154 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16155 if (!*slot)
16156 {
16157 *slot = lookup;
16158 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16159 }
16160 }
16161 }
16162
16163 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16164 exception if there is an error. */
16165
16166 static void
16167 write_obstack (FILE *file, struct obstack *obstack)
16168 {
16169 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16170 file)
16171 != obstack_object_size (obstack))
16172 error (_("couldn't data write to file"));
16173 }
16174
16175 /* Unlink a file if the argument is not NULL. */
16176
16177 static void
16178 unlink_if_set (void *p)
16179 {
16180 char **filename = p;
16181 if (*filename)
16182 unlink (*filename);
16183 }
16184
16185 /* A helper struct used when iterating over debug_types. */
16186 struct signatured_type_index_data
16187 {
16188 struct objfile *objfile;
16189 struct mapped_symtab *symtab;
16190 struct obstack *types_list;
16191 htab_t psyms_seen;
16192 int cu_index;
16193 };
16194
16195 /* A helper function that writes a single signatured_type to an
16196 obstack. */
16197
16198 static int
16199 write_one_signatured_type (void **slot, void *d)
16200 {
16201 struct signatured_type_index_data *info = d;
16202 struct signatured_type *entry = (struct signatured_type *) *slot;
16203 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16204 struct partial_symtab *psymtab = per_cu->v.psymtab;
16205 gdb_byte val[8];
16206
16207 write_psymbols (info->symtab,
16208 info->psyms_seen,
16209 info->objfile->global_psymbols.list
16210 + psymtab->globals_offset,
16211 psymtab->n_global_syms, info->cu_index,
16212 0);
16213 write_psymbols (info->symtab,
16214 info->psyms_seen,
16215 info->objfile->static_psymbols.list
16216 + psymtab->statics_offset,
16217 psymtab->n_static_syms, info->cu_index,
16218 1);
16219
16220 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16221 obstack_grow (info->types_list, val, 8);
16222 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16223 obstack_grow (info->types_list, val, 8);
16224 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16225 obstack_grow (info->types_list, val, 8);
16226
16227 ++info->cu_index;
16228
16229 return 1;
16230 }
16231
16232 /* A cleanup function for an htab_t. */
16233
16234 static void
16235 cleanup_htab (void *arg)
16236 {
16237 htab_delete (arg);
16238 }
16239
16240 /* Create an index file for OBJFILE in the directory DIR. */
16241
16242 static void
16243 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16244 {
16245 struct cleanup *cleanup;
16246 char *filename, *cleanup_filename;
16247 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16248 struct obstack cu_list, types_cu_list;
16249 int i;
16250 FILE *out_file;
16251 struct mapped_symtab *symtab;
16252 offset_type val, size_of_contents, total_len;
16253 struct stat st;
16254 char buf[8];
16255 htab_t psyms_seen;
16256 htab_t cu_index_htab;
16257 struct psymtab_cu_index_map *psymtab_cu_index_map;
16258
16259 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16260 return;
16261
16262 if (dwarf2_per_objfile->using_index)
16263 error (_("Cannot use an index to create the index"));
16264
16265 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16266 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16267
16268 if (stat (objfile->name, &st) < 0)
16269 perror_with_name (objfile->name);
16270
16271 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16272 INDEX_SUFFIX, (char *) NULL);
16273 cleanup = make_cleanup (xfree, filename);
16274
16275 out_file = fopen (filename, "wb");
16276 if (!out_file)
16277 error (_("Can't open `%s' for writing"), filename);
16278
16279 cleanup_filename = filename;
16280 make_cleanup (unlink_if_set, &cleanup_filename);
16281
16282 symtab = create_mapped_symtab ();
16283 make_cleanup (cleanup_mapped_symtab, symtab);
16284
16285 obstack_init (&addr_obstack);
16286 make_cleanup_obstack_free (&addr_obstack);
16287
16288 obstack_init (&cu_list);
16289 make_cleanup_obstack_free (&cu_list);
16290
16291 obstack_init (&types_cu_list);
16292 make_cleanup_obstack_free (&types_cu_list);
16293
16294 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16295 NULL, xcalloc, xfree);
16296 make_cleanup (cleanup_htab, psyms_seen);
16297
16298 /* While we're scanning CU's create a table that maps a psymtab pointer
16299 (which is what addrmap records) to its index (which is what is recorded
16300 in the index file). This will later be needed to write the address
16301 table. */
16302 cu_index_htab = htab_create_alloc (100,
16303 hash_psymtab_cu_index,
16304 eq_psymtab_cu_index,
16305 NULL, xcalloc, xfree);
16306 make_cleanup (cleanup_htab, cu_index_htab);
16307 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16308 xmalloc (sizeof (struct psymtab_cu_index_map)
16309 * dwarf2_per_objfile->n_comp_units);
16310 make_cleanup (xfree, psymtab_cu_index_map);
16311
16312 /* The CU list is already sorted, so we don't need to do additional
16313 work here. Also, the debug_types entries do not appear in
16314 all_comp_units, but only in their own hash table. */
16315 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16316 {
16317 struct dwarf2_per_cu_data *per_cu
16318 = dwarf2_per_objfile->all_comp_units[i];
16319 struct partial_symtab *psymtab = per_cu->v.psymtab;
16320 gdb_byte val[8];
16321 struct psymtab_cu_index_map *map;
16322 void **slot;
16323
16324 write_psymbols (symtab,
16325 psyms_seen,
16326 objfile->global_psymbols.list + psymtab->globals_offset,
16327 psymtab->n_global_syms, i,
16328 0);
16329 write_psymbols (symtab,
16330 psyms_seen,
16331 objfile->static_psymbols.list + psymtab->statics_offset,
16332 psymtab->n_static_syms, i,
16333 1);
16334
16335 map = &psymtab_cu_index_map[i];
16336 map->psymtab = psymtab;
16337 map->cu_index = i;
16338 slot = htab_find_slot (cu_index_htab, map, INSERT);
16339 gdb_assert (slot != NULL);
16340 gdb_assert (*slot == NULL);
16341 *slot = map;
16342
16343 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16344 obstack_grow (&cu_list, val, 8);
16345 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16346 obstack_grow (&cu_list, val, 8);
16347 }
16348
16349 /* Dump the address map. */
16350 write_address_map (objfile, &addr_obstack, cu_index_htab);
16351
16352 /* Write out the .debug_type entries, if any. */
16353 if (dwarf2_per_objfile->signatured_types)
16354 {
16355 struct signatured_type_index_data sig_data;
16356
16357 sig_data.objfile = objfile;
16358 sig_data.symtab = symtab;
16359 sig_data.types_list = &types_cu_list;
16360 sig_data.psyms_seen = psyms_seen;
16361 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16362 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16363 write_one_signatured_type, &sig_data);
16364 }
16365
16366 obstack_init (&constant_pool);
16367 make_cleanup_obstack_free (&constant_pool);
16368 obstack_init (&symtab_obstack);
16369 make_cleanup_obstack_free (&symtab_obstack);
16370 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16371
16372 obstack_init (&contents);
16373 make_cleanup_obstack_free (&contents);
16374 size_of_contents = 6 * sizeof (offset_type);
16375 total_len = size_of_contents;
16376
16377 /* The version number. */
16378 val = MAYBE_SWAP (5);
16379 obstack_grow (&contents, &val, sizeof (val));
16380
16381 /* The offset of the CU list from the start of the file. */
16382 val = MAYBE_SWAP (total_len);
16383 obstack_grow (&contents, &val, sizeof (val));
16384 total_len += obstack_object_size (&cu_list);
16385
16386 /* The offset of the types CU list from the start of the file. */
16387 val = MAYBE_SWAP (total_len);
16388 obstack_grow (&contents, &val, sizeof (val));
16389 total_len += obstack_object_size (&types_cu_list);
16390
16391 /* The offset of the address table from the start of the file. */
16392 val = MAYBE_SWAP (total_len);
16393 obstack_grow (&contents, &val, sizeof (val));
16394 total_len += obstack_object_size (&addr_obstack);
16395
16396 /* The offset of the symbol table from the start of the file. */
16397 val = MAYBE_SWAP (total_len);
16398 obstack_grow (&contents, &val, sizeof (val));
16399 total_len += obstack_object_size (&symtab_obstack);
16400
16401 /* The offset of the constant pool from the start of the file. */
16402 val = MAYBE_SWAP (total_len);
16403 obstack_grow (&contents, &val, sizeof (val));
16404 total_len += obstack_object_size (&constant_pool);
16405
16406 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16407
16408 write_obstack (out_file, &contents);
16409 write_obstack (out_file, &cu_list);
16410 write_obstack (out_file, &types_cu_list);
16411 write_obstack (out_file, &addr_obstack);
16412 write_obstack (out_file, &symtab_obstack);
16413 write_obstack (out_file, &constant_pool);
16414
16415 fclose (out_file);
16416
16417 /* We want to keep the file, so we set cleanup_filename to NULL
16418 here. See unlink_if_set. */
16419 cleanup_filename = NULL;
16420
16421 do_cleanups (cleanup);
16422 }
16423
16424 /* Implementation of the `save gdb-index' command.
16425
16426 Note that the file format used by this command is documented in the
16427 GDB manual. Any changes here must be documented there. */
16428
16429 static void
16430 save_gdb_index_command (char *arg, int from_tty)
16431 {
16432 struct objfile *objfile;
16433
16434 if (!arg || !*arg)
16435 error (_("usage: save gdb-index DIRECTORY"));
16436
16437 ALL_OBJFILES (objfile)
16438 {
16439 struct stat st;
16440
16441 /* If the objfile does not correspond to an actual file, skip it. */
16442 if (stat (objfile->name, &st) < 0)
16443 continue;
16444
16445 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16446 if (dwarf2_per_objfile)
16447 {
16448 volatile struct gdb_exception except;
16449
16450 TRY_CATCH (except, RETURN_MASK_ERROR)
16451 {
16452 write_psymtabs_to_index (objfile, arg);
16453 }
16454 if (except.reason < 0)
16455 exception_fprintf (gdb_stderr, except,
16456 _("Error while writing index for `%s': "),
16457 objfile->name);
16458 }
16459 }
16460 }
16461
16462 \f
16463
16464 int dwarf2_always_disassemble;
16465
16466 static void
16467 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16468 struct cmd_list_element *c, const char *value)
16469 {
16470 fprintf_filtered (file,
16471 _("Whether to always disassemble "
16472 "DWARF expressions is %s.\n"),
16473 value);
16474 }
16475
16476 static void
16477 show_check_physname (struct ui_file *file, int from_tty,
16478 struct cmd_list_element *c, const char *value)
16479 {
16480 fprintf_filtered (file,
16481 _("Whether to check \"physname\" is %s.\n"),
16482 value);
16483 }
16484
16485 void _initialize_dwarf2_read (void);
16486
16487 void
16488 _initialize_dwarf2_read (void)
16489 {
16490 struct cmd_list_element *c;
16491
16492 dwarf2_objfile_data_key
16493 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16494
16495 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16496 Set DWARF 2 specific variables.\n\
16497 Configure DWARF 2 variables such as the cache size"),
16498 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16499 0/*allow-unknown*/, &maintenance_set_cmdlist);
16500
16501 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16502 Show DWARF 2 specific variables\n\
16503 Show DWARF 2 variables such as the cache size"),
16504 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16505 0/*allow-unknown*/, &maintenance_show_cmdlist);
16506
16507 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16508 &dwarf2_max_cache_age, _("\
16509 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16510 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16511 A higher limit means that cached compilation units will be stored\n\
16512 in memory longer, and more total memory will be used. Zero disables\n\
16513 caching, which can slow down startup."),
16514 NULL,
16515 show_dwarf2_max_cache_age,
16516 &set_dwarf2_cmdlist,
16517 &show_dwarf2_cmdlist);
16518
16519 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16520 &dwarf2_always_disassemble, _("\
16521 Set whether `info address' always disassembles DWARF expressions."), _("\
16522 Show whether `info address' always disassembles DWARF expressions."), _("\
16523 When enabled, DWARF expressions are always printed in an assembly-like\n\
16524 syntax. When disabled, expressions will be printed in a more\n\
16525 conversational style, when possible."),
16526 NULL,
16527 show_dwarf2_always_disassemble,
16528 &set_dwarf2_cmdlist,
16529 &show_dwarf2_cmdlist);
16530
16531 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16532 Set debugging of the dwarf2 DIE reader."), _("\
16533 Show debugging of the dwarf2 DIE reader."), _("\
16534 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16535 The value is the maximum depth to print."),
16536 NULL,
16537 NULL,
16538 &setdebuglist, &showdebuglist);
16539
16540 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16541 Set cross-checking of \"physname\" code against demangler."), _("\
16542 Show cross-checking of \"physname\" code against demangler."), _("\
16543 When enabled, GDB's internal \"physname\" code is checked against\n\
16544 the demangler."),
16545 NULL, show_check_physname,
16546 &setdebuglist, &showdebuglist);
16547
16548 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16549 _("\
16550 Save a gdb-index file.\n\
16551 Usage: save gdb-index DIRECTORY"),
16552 &save_cmdlist);
16553 set_cmd_completer (c, filename_completer);
16554 }
This page took 0.385608 seconds and 4 git commands to generate.